Skip to content

Add HIP (AMD ROCm) backend - #257

Open
zhubonan wants to merge 7 commits into
lab-cosmo:mainfrom
bonan-group:HIP-support
Open

zhubonan wants to merge 7 commits into
lab-cosmo:mainfrom
bonan-group:HIP-support

Conversation

@zhubonan

@zhubonan zhubonan commented Nov 5, 2026 •

Copy link
Copy Markdown

Summary

This PR adds a HIP (AMD ROCm) backend to the core C++ library.

The previously included CUDA indexing and lifecycle changes have been removed from this branch and will be proposed separately. This PR now changes only HIP-facing build, API, test, example, CI, and documentation surfaces, apart from the small shared-kernel guard required by hipRTC.

HIP backend

  • Adds SPHERICART_ENABLE_HIP, mutually exclusive with the CUDA and SYCL backends.
  • Compiles the existing CUDA-dialect kernels at runtime with hipRTC. libhiprtc is loaded dynamically and kernels are launched through hipModuleLoadData, hipModuleGetFunction, and hipModuleLaunchKernel.
  • Requires only ROCm headers and libamdhip64 at build time; a HIP compiler is not required.
  • Adds sphericart::hip::SphericalHarmonics and SolidHarmonics, including synchronous compute, gradients, Hessians, backward, opt-in asynchronous gradients, and explicit release of lazily allocated device resources.
  • Resolves the runtime GCN target from hipGetDeviceProperties().gcnArchName, caches modules/functions per device, and reports shared-memory requests above the device opt-in limit before launch.
  • Reuses the existing kernel source. The sole shared-kernel change selects the 64-bit shuffle mask required by HIP while retaining CUDA's existing 32-bit mask.

Tests and examples

  • test_derivatives_hip: forward, gradients, Hessians, and backward parity over l_max 0-14 for spherical and solid harmonics.
  • test_hip_async: synchronous/asynchronous result parity, enqueue behavior, and release/reallocation transparency.
  • examples/hip/example.cpp and a gated CMake target.
  • hip-tests CI job using a ROCm development container. Tests skip cleanly when no AMD GPU is exposed, while still compiling the complete backend.

Verification

Verified on ROCm 7.14 with a gfx1151 device:

  • Fresh HIP build with the project warning flags: 9/9 tests passed, including both HIP tests and the HIP example.
  • Double-precision CPU/GPU relative error is approximately 1e-15; float32 is approximately 3e-7.
  • Fresh CPU-only build: 3/3 tests passed.
  • Configuring HIP and CUDA together fails with the documented mutual-exclusion diagnostic.
  • Downstream Symmetrix-XL HIP qualification exercises this backend on device, including stream-ordered asynchronous gradients.

Scope

The scope follows the existing SYCL backend: C++ API, build option, tests, example, CI, and documentation. C API, Python, Torch, and JAX HIP exposure are not included. Published wheels do not enable the HIP backend.

@Luthaf

Luthaf commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for taking the time to share this @zhubonan!

I would appreciate the first two commits to be sent as standalone PR! Then this PR should be able to contain only new HIP code, which should be easier to review.

@Luthaf

Luthaf commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

@RMeli once you are back from holidays: can we access the AMD GPU through the CSCS CI now? I remember it was not fully available at some point.

Port the HIP implementation qualified by symmetrix-xl (bonan-group/
symmetrix-xl) to sphericart's core library:

- Compile the existing CUDA-dialect kernels at runtime with hipRTC,
  loading libhiprtc dynamically (src/hiprtc_lite.{hpp,cpp}, a port of
  symmetrix-xl's jit_hiprtc) and launching through hipModuleLoadData /
  hipModuleLaunchKernel (src/hip_base.cpp), targeting the GCN
  architecture reported by hipGetDeviceProperties.
- Add sphericart::hip::SphericalHarmonics/SolidHarmonics mirroring the
  CUDA classes (src/sphericart_hip.cpp, include/*.hpp), with prefactors
  uploaded to the device of the input pointers.
- Guard the warp-shuffle mask in sphericart_impl.cu: hipRTC requires a
  64-bit mask where CUDA uses a 32-bit one.
- Wire SPHERICART_ENABLE_HIP in CMake following the SYCL backend
  conventions; only ROCm headers and libamdhip64 are needed at build
  time (no HIP compiler), mirroring the CUDA backend's gpu-lite
  philosophy. HIP is mutually exclusive with CUDA/SYCL builds.
- Add test_derivatives_hip (skips without a device) and a HIP example,
  both gated like their SYCL counterparts, plus a ROCm-container CI job
  mirroring the SYCL workflow.

Verified on a gfx1151 (Radeon 8060S, ROCm 7.14): l_max 0-14 float/double
gradients, hessians, and backward pass match finite-difference
references; the HIP example matches the CPU calculator to ~1e-15.
Mirror the async-lifecycle CUDA API on the HIP backend so consumers
(symmetric workloads like symmetrix-xl's spherical-harmonics evaluation)
can drive the same lifecycle on AMD GPUs:

- spherical_harmonics_hip_base takes an explicit synchronize flag
  (default true) and only calls hipStreamSynchronize when set;
  compute_with_gradients_async enqueues without synchronizing.
- release_device_resources frees the lazily allocated prefactors on the
  bound device; calculators are single-device and reject migration,
  matching the CUDA calculator semantics.
- Adopt the overflow-safe grid rounding from the async-lifecycle work.
- test_hip_async: async-vs-sync bit-for-bit parity, enqueue-must-beat-
  sync timing, and release/realloc transparency (skips without a GPU).
Mirror gpu-lite's actionable diagnostic instead of failing later inside
hipModuleLaunchKernel with an opaque invalid-argument error, and widen
the CI HIP ctest filter so test_hip_async runs alongside
test_derivatives_hip (both skip without a device; example_hip does not
skip and stays out of the filter).
- test_hip_async now measures the enqueue burst against a single stream
  drain with the CUDA test's 4x margin, instead of comparing against a
  synchronous launch loop, so the mirrored tests assert the same property.
- Install git in the HIP CI container alongside cmake and the compilers.
- README: the published wheels do not include the HIP backend.
@zhubonan zhubonan changed the title Add HIP (AMD ROCm) backend and opt-in async device lifecycle Add HIP (AMD ROCm) backend Nov 7, 2026
@zhubonan

zhubonan commented Nov 7, 2026

Copy link
Copy Markdown
Author

Updated the branch as requested: the CUDA 64-bit indexing and CUDA async/resource-lifecycle commits are no longer part of this PR. PR #257 now contains only the HIP backend and its HIP-specific API/tests. I am preparing the two CUDA commits separately and will not open that PR until the branch is ready for confirmation.

@RMeli

RMeli commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

@Luthaf yes, CI on the MI300 cluster should now be available. I can have a go at setting it up later today.

@Luthaf Luthaf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks clean, I have a couple of questions to understand the limitations better!

Comment thread sphericart/CMakeLists.txt Resolved
Comment thread sphericart/CMakeLists.txt Resolved
Comment thread sphericart/CMakeLists.txt Resolved
Comment thread sphericart/CMakeLists.txt Resolved
@Luthaf

Luthaf commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

cscs-ci run all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants