Skip to content

Add ROCm support for AMD Instinct GPUs - #9153

Merged
ericspod merged 2 commits into
Project-MONAI:devfrom
AMD-Ecosystem:9152-rocm-support
Oct 6, 2026
Merged

ericspod merged 2 commits into
Project-MONAI:devfrom
AMD-Ecosystem:9152-rocm-support

Conversation

@nilapate

@nilapate nilapate commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Fixes #9152.

Description

Minimal ROCm enablement for AMD Instinct GPUs. Most of MONAI is already portable; this fixes the few places that assume a single GPU toolkit, plus adds a container recipe.

  • setup.py — CUDA_HOME is unset on a ROCm build, where the toolkit is located via ROCM_HOME, so BUILD_CUDA evaluated False and the C++ extensions were silently skipped. Accept either. CUDAExtension handles the source translation, so no kernel source changes are needed.
  • monai/_extensions/loader.py — the JIT build cache key included torch.version.cuda, unset on ROCm, so every ROCm toolkit version collided on one cache entry. Fall back to torch.version.hip.
  • tests/networks/nets/test_densenet.py — test_pretrain_consistency compares two separately-constructed module graphs holding identical weights for bit-exactness. Convolution algorithm selection is free to differ between the graphs, so this was never a guaranteed property; skipped on ROCm for now.
  • Dockerfile.rocm — the default Dockerfile uses a vendor-specific base image, so ROCm gets its own recipe. Installs the ROCm SDK and a matching PyTorch, filters packages that don't apply on this platform (cucim-cu*, nvidia-ml-py, nni), and installs hipCIM to provide the cucim module the whole-slide-image paths need.

3 lines of library code changed; everything else is additive. Existing code paths are unchanged — both edits short-circuit on the current value, so CUDA and CPU-only behaviour is identical.

Validated on MI300X (gfx942); MI355X (gfx950) validated separately. tests/networks/layers/test_gmm.py passes (5 passed), which exercises the loader.py change through a real JIT extension build.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

@coderabbitai

coderabbitai Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: Project-MONAI/MONAI/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3145d811-8963-4a55-8dcf-adce1c65cab0
📥 Commits

Reviewing files that changed from the base of the PR and between 1360ca8 and bc09a1e.

📒 Files selected for processing (3)
  • Dockerfile.rocm
  • setup.py
  • tests/networks/nets/test_densenet.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

Adds a configurable ROCm container recipe for MONAI. Extension detection accepts PyTorch’s ROCM_HOME, and extension cache keys use the HIP version when CUDA is unset. The pretrained DenseNet consistency test skips on ROCm.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to bc09a

The ROCm support is mergeable after normal checks; the supplied evidence identifies no outstanding defect.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 3 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding ROCm support for AMD Instinct GPUs.
Description check ✅ Passed The description includes the issue reference, a detailed change summary, and the applicable change types. It also reports validation and leaves unreported test commands unchecked.
Linked Issues check ✅ Passed For #9152, setup.py recognizes ROCM_HOME alongside CUDA_HOME when detecting the extension toolkit. Dockerfile.rocm provides an Ubuntu 24.04, Python 3.12 ROCm 10.0 container recipe for gfx942…
Out of Scope Changes check ✅ Passed All changes support #9152. The cache-key fallback, ROCm-specific test skip, and container configuration enable or validate the requested ROCm support. No unrelated changes are present in the reviewed …
Full details: Docstring Coverage

Explanation

Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 3 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Comment thread tests/networks/nets/test_densenet.py Outdated
Comment thread Dockerfile.rocm Outdated
Comment thread Dockerfile.rocm Outdated

@ericspod ericspod left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hi @nilapate thanks for this, we're happy to support ROCm in MONAI but we do have the issue of having no means to test it. The Dockerfile for example isn't something we can test without AMD hardware so we would have to rely on users to report any issues. I made a few comments about minor things but the changes look good to me as they are, if you can address things we should be good to merge once tests get through.

MONAI runs on ROCm unmodified for the most part, since a ROCm build of
PyTorch presents itself as `cuda`. Three places assume a CUDA toolkit
specifically, and the Docker image has no ROCm equivalent.

setup.py: `CUDA_HOME` is None on a ROCm torch, where the toolkit is found
via `ROCM_HOME` instead, so BUILD_CUDA evaluated False and the C++/HIP
extensions were silently skipped. Accept either. `CUDAExtension` hipifies
the .cu sources transparently, so no source changes are needed.

monai/_extensions/loader.py: the JIT build cache key included
`torch.version.cuda`, which is None on ROCm, so every ROCm toolkit version
collided on one cache entry. Fall back to `torch.version.hip`.

tests/networks/nets/test_densenet.py: test_pretrain_consistency compares
two separately-constructed module graphs holding identical weights for
bit-exactness. The backend may select different convolution algorithms per
graph, so this was never guaranteed; skip it on ROCm.

Dockerfile.rocm: the default Dockerfile builds on the NVIDIA PyTorch
container, so ROCm gets its own recipe. It installs the ROCm SDK and a
matching PyTorch from AMD's public index, filters the CUDA-only extras
(cucim-cu*, nvidia-ml-py, nni), and installs hipCIM, which provides the
`cucim` module that MONAI's whole-slide-image paths need on ROCm.

Assisted-by: Claude Opus 5 <noreply@anthropic.com>
Signed-off-by: Patel, Nilaykumar K <NilaykumarKantibhai.Patel@amd.com>
- Add ARG PYTHON_VERSION; the rocm-sdk site-packages paths were hardcoded
  to python3.12 and broke silently on a different BASE_IMAGE.
- Build the extensions with BUILD_MONAI=1 FORCE_CUDA=1 (the build host has
  no GPU, so setup.py would otherwise skip them) and set ENV BUILD_MONAI=1,
  which deviceconfig needs at runtime to gate USE_COMPILED.
- Drop the test_densenet skipTest that duplicated its @skipIf decorator.

Assisted-by: Claude Opus 5 <noreply@anthropic.com>
Signed-off-by: Patel, Nilaykumar K <NilaykumarKantibhai.Patel@amd.com>
@nilapate

nilapate commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor Author

hi @nilapate thanks for this, we're happy to support ROCm in MONAI but we do have the issue of having no means to test it. The Dockerfile for example isn't something we can test without AMD hardware so we would have to rely on users to report any issues. I made a few comments about minor things but the changes look good to me as they are, if you can address things we should be good to merge once tests get through.

Thanks @ericspod , I have addressed the comment.

For ongoing maintenance, I have to check, There is something called amd developer cloud(https://devcloud.amd.com/login) but not sure how to integrate here(if you mean that by having mean to test).

I also am planning to have one more non-invasive PR for clean install of MONAI for rocm, will raise PR soon.

@ericspod

ericspod commented Oct 6, 2026

Copy link
Copy Markdown
Member

Hi @nilapate we're good to merge now I think. I would want a means of automatically testing with AMD hardware if we could so I'm not sure how Developer Cloud fits, but we may want something for manual testing either way.

@ericspod
ericspod merged commit a58340a into Project-MONAI:dev Oct 6, 2026
30 checks passed
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.

Add ROCm support for AMD Instinct GPUs

2 participants