Skip to content

enable upstream MONAI to build and run on AMD Instinct GPUs (MI300X/MI325X/MI355X). - #1

Closed
nilapate wants to merge 1 commit into
devfrom
rocm-minimal-support
Closed

nilapate wants to merge 1 commit into
devfrom
rocm-minimal-support

Conversation

@nilapate

Copy link
Copy Markdown

Description

Stock upstream MONAI runs on ROCm with three small fixes and a new container:

  • setup.py: detect ROCM_HOME when CUDA_HOME is absent so HIP extensions build correctly.
  • loader.py: key the JIT cache on torch.version.hip to prevent all ROCm builds colliding on one slot.
  • Dockerfile.rocm: new AMD Instinct container. Filters CUDA-only deps to prevent ~1.2 GB of nvidia-* wheels clobbering the ROCm torch.
  • test_densenet.py: skip bit-exactness check on ROCm — conv algorithm selection differs per graph (measured max abs diff 6.68e-06 on MI300X).

PR Checklist

  • Non-breaking change.
  • Commit message clearly explains why this commit is needed.
  • Commit message includes how this PR/commit was tested.
  • Commit title includes ticket number.

Commit Message Details

What is this commit about?

Two ROCm bug fixes (broken HIP builds, JIT cache collisions), a new Dockerfile.rocm for AMD Instinct, and a ROCm-specific test skip.

Why is this commit needed?

Users currently maintain out-of-tree forks to work around these issues on AMD Instinct hardware.

How was this PR tested?

MI300X (gfx942), PyTorch 2.13.0+rocm10.0.0:

@nilapate
nilapate requested a review from vcsajjan September 30, 2026 08:55
@nilapate
nilapate force-pushed the rocm-minimal-support branch from 0319689 to 8afbc54 Compare September 30, 2026 09:32
Comment thread Dockerfile.rocm
@@ -0,0 +1,133 @@
# Copyright (c) MONAI Consortium

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If we are adding this file, then copyright should be AMD, not MONAI.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes, we need add AMD copy right banner

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

General Convention is to have of MONAI

Comment thread Dockerfile.rocm Outdated
# See the License for the specific language governing permissions and
# limitations under the License.

# MONAI on AMD ROCm (AMD Instinct MI300X / MI325X / MI355X).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You can drop the exact enumeration - just mention AMD Instinct.

Suggested change
# MONAI on AMD ROCm (AMD Instinct MI300X / MI325X / MI355X).
# MONAI on AMD ROCm (AMD Instinct GPUs).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

Comment thread Dockerfile.rocm Outdated
Comment on lines +14 to +15
# The default Dockerfile builds on the NVIDIA PyTorch container, which has no ROCm
# equivalent, so ROCm gets its own recipe rather than a branch inside that one.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Delete this line.

Suggested change
# The default Dockerfile builds on the NVIDIA PyTorch container, which has no ROCm
# equivalent, so ROCm gets its own recipe rather than a branch inside that one.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

Comment thread Dockerfile.rocm Outdated
Comment on lines +16 to +17
# This image installs the upstream `monai` package -- it is not a separate
# distribution and does not rename the wheel.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
# This image installs the upstream `monai` package -- it is not a separate
# distribution and does not rename the wheel.
# This image installs the upstream `monai` package.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

Comment thread Dockerfile.rocm Outdated
Comment on lines +24 to +26
# Select the GPU architecture with --build-arg AMDGPU_TARGETS=...:
# gfx942 MI300X, MI325X (default)
# gfx950 MI355X

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
# Select the GPU architecture with --build-arg AMDGPU_TARGETS=...:
# gfx942 MI300X, MI325X (default)
# gfx950 MI355X
# Select the GPU architecture with '--build-arg AMDGPU_TARGETS=gfx942|gfx950'. The default is gfx942.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

Comment thread Dockerfile.rocm Outdated
Comment on lines +83 to +85
ENV MIOPEN_USER_DB_PATH="/tmp/miopen"
ENV MIOPEN_CUSTOM_CACHE_DIR="/tmp/miopen"
RUN mkdir -p /tmp/miopen && chmod 1777 /tmp/miopen

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These are risky and prone to clashes if multiple users are on the same node.
Suggest using standard temporary file name generator.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed whole block, not needed here.

Comment thread Dockerfile.rocm Outdated
Comment on lines +97 to +101
# The "all" extra is installed via print_dependencies.py rather than as -e .[all,testing]
# so that cucim, nvidia-ml-py, and nni can be filtered out:
# cucim-cu13 → cupy-cuda13x[ctk] → cuda-toolkit → ~1.2 GB of nvidia-* wheels unusable on ROCm
# nvidia-ml-py loads libnvidia-ml.so.1 at import time; absent on ROCm, breaks collection
# nni hard-depends on nvidia-ml-py

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If possible, avoid the mention of NVIDIA.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

we need to filter them out.

Comment thread Dockerfile.rocm Outdated
Comment on lines +117 to +118
# Whole-slide-image support on ROCm is provided by hipCIM rather than cucim, which is
# CUDA-only. It is not currently published on PyPI at a usable version, so it is an

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
# Whole-slide-image support on ROCm is provided by hipCIM rather than cucim, which is
# CUDA-only. It is not currently published on PyPI at a usable version, so it is an
# Whole-slide-image support on ROCm is provided by hipCIM.
# It is not currently published on PyPI at a usable version, so it is an

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed.

Comment thread Dockerfile.rocm Outdated
Comment on lines +117 to +118
# Whole-slide-image support on ROCm is provided by hipCIM rather than cucim, which is
# CUDA-only. It is not currently published on PyPI at a usable version, so it is an

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why is it not usable? The 26.08 release is already public and should be usable.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

Comment thread Dockerfile.rocm Outdated
# Whole-slide-image support on ROCm is provided by hipCIM rather than cucim, which is
# CUDA-only. It is not currently published on PyPI at a usable version, so it is an
# optional layer: pass --build-arg HIPCIM_INDEX_URL=<index> to enable WSI workloads.
ARG HIPCIM_INDEX_URL=""

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use the published URL based on the 26.08 release.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

Comment thread Dockerfile.rocm
@@ -0,0 +1,133 @@
# Copyright (c) MONAI Consortium

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes, we need add AMD copy right banner

@nilapate
nilapate force-pushed the rocm-minimal-support branch from 8afbc54 to 9e06576 Compare September 30, 2026 11:03
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.

Signed-off-by: Patel, Nilaykumar K <NilaykumarKantibhai.Patel@amd.com>
@nilapate
nilapate force-pushed the rocm-minimal-support branch from 9e06576 to dfcf915 Compare October 5, 2026 09:13
@nilapate nilapate closed this Oct 7, 2026
@nilapate
nilapate deleted the rocm-minimal-support branch October 7, 2026 03:35
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