Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Dockerfile.rocm
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,34 @@ COPY LICENSE CHANGELOG.md CODE_OF_CONDUCT.md CONTRIBUTING.md README.md versionee
COPY tests ./tests
COPY monai ./monai

# Use print_dependencies.py rather than -e .[all,testing] to filter CUDA-only packages:
# cucim-cu* pulls in cuda-toolkit (~1.2 GB); nvidia-ml-py fails at import on ROCm; nni depends on it.
# AMD's package index for this ROCm series, which serves amd-hipcim and amd-cupy.
# Set HIPCIM_INDEX_URL="" to build without them, and so without WSI/cucim and CuPy support.
ARG HIPCIM_INDEX_URL="https://pypi.amd.com/rocm-${ROCM_SERIES}.0/simple/"

# The "all_amd" extra is "all" with the AMD builds of cuCIM and CuPy in place of the NVIDIA-only packages.
# BUILD_MONAI=1 builds the C++/HIP extensions ahead of time; FORCE_CUDA=1 is required because the
# build host has no GPU, so setup.py's `torch.cuda.is_available()` check would otherwise skip them.
# Compilation itself needs only the toolkit and PYTORCH_ROCM_ARCH (set above), not a device.
# pytest is not in the "testing" extra; it is added explicitly for running tests by hand.
RUN python monai/config/print_dependencies.py build-system \
| xargs -d '\n' pip install --no-cache-dir --no-build-isolation \
&& python monai/config/print_dependencies.py all testing \
| grep -vE '^cucim-cu|^nvidia-ml-py|^nni' > /tmp/rocm-requirements-$$.txt \
&& python monai/config/print_dependencies.py all_amd testing > /tmp/rocm-requirements-$$.txt \
&& if [ -z "${HIPCIM_INDEX_URL}" ]; then sed -i '/^amd-/d' /tmp/rocm-requirements-$$.txt; fi \
&& BUILD_MONAI=1 FORCE_CUDA=1 pip install --no-cache-dir --no-build-isolation \
${HIPCIM_INDEX_URL:+--extra-index-url "${HIPCIM_INDEX_URL}"} \
-r /tmp/rocm-requirements-$$.txt pytest -e . \
&& rm -f /tmp/rocm-requirements-$$.txt

# Required at runtime too: monai.config.deviceconfig gates USE_COMPILED on this variable, so
# without it the extensions built above would be present but never used.
ENV BUILD_MONAI=1

# Set HIPCIM_INDEX_URL="" to build without WSI/cucim support.
# CuImage is imported (not just cucim) because cucim uses lazy_loader -- a bare import
# succeeds even when the native library is unresolvable. Failing here is deliberate: if
# hipCIM was requested, an image where the cucim backends silently do not work is worse
# than no image at all.
ARG HIPCIM_INDEX_URL="https://pypi.amd.com/rocm-${ROCM_SERIES}.0/simple/"
RUN if [ -n "${HIPCIM_INDEX_URL}" ]; then \
pip install --no-cache-dir --extra-index-url "${HIPCIM_INDEX_URL}" "amd-hipcim" \
&& python -c "from cucim import CuImage"; \
python -c "from cucim import CuImage"; \
else \
echo "hipCIM not installed; whole-slide-image (cucim) backends are unavailable."; \
fi
Expand Down
53 changes: 51 additions & 2 deletions docs/source/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [Installation Guide](#installation-guide)
- [Table of Contents](#table-of-contents)
- [GPU-enabled installation (CUDA and CuPy)](#gpu-enabled-installation-cuda-and-cupy)
- [GPU-enabled installation (AMD ROCm)](#gpu-enabled-installation-amd-rocm)
- [From PyPI](#from-pypi)
- [Milestone release](#milestone-release)
- [Weekly preview release](#weekly-preview-release)
Expand Down Expand Up @@ -64,6 +65,53 @@ your CUDA toolkit.

---

## GPU-enabled installation (AMD ROCm)

On AMD GPUs MONAI runs on the [ROCm](https://rocm.docs.amd.com/) build of PyTorch. The ROCm packages are
served from AMD's package indexes rather than PyPI, so the index URLs are given on the command line.

1. Install the ROCm SDK with the ROCm builds of PyTorch and Torchvision, naming the architecture of your GPU
(`gfx942` for the MI300 series, `gfx950` for the MI350 series), then unpack the SDK's development files:

```bash
pip install --index-url https://stable.repo.amd.com/rocm/whl-next/ \
"rocm[libraries,devel,device-gfx950]==10.0.*" "torch[device-gfx950]" "torchvision[device-gfx950]"
rocm-sdk init
```

1. Install MONAI with the AMD extras, adding AMD's extensions index for the same ROCm series:

```bash
pip install --extra-index-url https://pypi.amd.com/rocm-10.0.0/simple/ 'monai[all_amd]'
```

Three extras are provided for ROCm:

- `hipcim` installs [hipCIM](https://rocm.docs.amd.com/projects/hipCIM/en/latest/) (`amd-hipcim`), the ROCm
build of cuCIM.
- `amd_cupy` installs `amd-cupy`, the ROCm build of CuPy.
- `all_amd` is the counterpart of `all`: the optional dependencies that do not need NVIDIA hardware, together
with `hipcim` and `amd_cupy`.

`amd-hipcim` provides the same `cucim` module as `cucim-cu12`/`cucim-cu13`, and `amd-cupy` the same `cupy`
module as `cupy-cuda13x`, so MONAI code using either works unchanged. For the same reason only one of each
pair can be installed in an environment: do not combine the AMD extras with `all`, `cucim`, `cupy` or
`nvimgcodec`.

A few points to be aware of:

- `amd-cupy` and `amd-hipcim` are currently released for ROCm 10.0. Keep the ROCm SDK on that series, as the
`==10.0.*` pin above does.
- Install Torchvision from the ROCm index in the first step. If it is left to the second step, `pip` takes
the PyPI build, which is not built for ROCm.
- `amd-cupy` and `amd-hipcim` are served from AMD's extensions index, so the `--extra-index-url` option is
required in the second step.
- `amd-hipcim` is currently released for CPython 3.12 on x86_64 Linux.
- `Dockerfile.rocm` in the repository root sets up a complete environment, including the compiler and library
paths needed to build the C++/HIP extensions.

---

## From PyPI

### Milestone release
Expand All @@ -77,10 +125,10 @@ pip install monai
MONAI supports the extras syntax such as `pip install 'monai[nibabel]'`. The options are

```text
clearml, cucim, cupy, einops, fire, gdown, h5py, huggingface_hub, hyena, ignite, imagecodecs, itk, jsonschema, lmdb, matplotlib, metrics_reloaded, mlflow, nibabel, nni, nvimgcodec, onnx, openslide, optuna, pandas, pillow, polygraphy, psutil, pyamg, pybind11, pydicom, pynrrd, pynvml, pyyaml, requests, segment_anything, scipy, skimage, tensorboard, tensorboardX, tifffile, torchio, torchvision, tqdm, transformers, zarr
amd_cupy, clearml, cucim, cupy, einops, fire, gdown, h5py, hipcim, huggingface_hub, hyena, ignite, imagecodecs, itk, jsonschema, lmdb, matplotlib, metrics_reloaded, mlflow, nibabel, nni, nvimgcodec, onnx, openslide, optuna, pandas, pillow, polygraphy, psutil, pyamg, pybind11, pydicom, pynrrd, pynvml, pyyaml, requests, segment_anything, scipy, skimage, tensorboard, tensorboardX, tifffile, torchio, torchvision, tqdm, transformers, zarr
```

which correspond to the packages: `clearml`, `cucim` (`cucim-cu12` or `cucim-cu13`), `cupy-cuda13x`, `einops`, `fire`, `gdown`, `h5py`, `huggingface_hub`, `nvsubquadratic`, `omegaconf`, `pytorch-ignite`, `imagecodecs`, `itk`, `jsonschema`, `lmdb`, `matplotlib`, `MetricsReloaded`, `mlflow`, `nibabel`, `nni`, `filelock`, `nvidia-nvimgcodec-cu13`, `onnx`, `onnxruntime`, `onnx_graphsurgeon`, `onnxscript`, `openslide-python`, `openslide-bin`, `optuna`, `pandas`, `pillow`, `polygraphy`, `psutil`, `pyamg`, `pybind11`, `pydicom`, `pynrrd`, `nvidia-ml-py`, `pyyaml`, `requests`, `segment_anything`, `scipy`, `scikit-image`, `tensorboard`, `tensorboardX`, `tifffile`, `torchio`, `torchvision`, `tqdm`, `transformers`, `zarr`.
which correspond to the packages: `amd-cupy`, `clearml`, `cucim` (`cucim-cu12` or `cucim-cu13`), `cupy-cuda13x`, `einops`, `fire`, `gdown`, `h5py`, `amd-hipcim`, `huggingface_hub`, `nvsubquadratic`, `omegaconf`, `pytorch-ignite`, `imagecodecs`, `itk`, `jsonschema`, `lmdb`, `matplotlib`, `MetricsReloaded`, `mlflow`, `nibabel`, `nni`, `filelock`, `nvidia-nvimgcodec-cu13`, `onnx`, `onnxruntime`, `onnx_graphsurgeon`, `onnxscript`, `openslide-python`, `openslide-bin`, `optuna`, `pandas`, `pillow`, `polygraphy`, `psutil`, `pyamg`, `pybind11`, `pydicom`, `pynrrd`, `nvidia-ml-py`, `pyyaml`, `requests`, `segment_anything`, `scipy`, `scikit-image`, `tensorboard`, `tensorboardX`, `tifffile`, `torchio`, `torchvision`, `tqdm`, `transformers`, `zarr`.

The AlexNet, VGG, and SqueezeNet options in `PerceptualLoss` require `torchvision`
(`pip install "monai[torchvision]"`). The external `lpips` package is no longer required.
Expand All @@ -94,6 +142,7 @@ The existing `monai[lpips]` extra is retained as an alias for `monai[torchvision

Almost all of these can be installed together with the `all` option. For development on MONAI, this should be accompanied by `testing` which will install the testing static checking packages. Cupy and `nvimgcodec` are omitted from `all` since the choice between
Cuda 12 and 13 versions of the libraries can't be resolved when installing and must be manually installed.
On AMD ROCm use `all_amd` in place of `all`, see [GPU-enabled installation (AMD ROCm)](#gpu-enabled-installation-amd-rocm).

The `nvimgcodec` extra installs GPU-accelerated DICOM decoding for `NvImgCodecPydicomReader`
(`pip install 'monai[nvimgcodec]'`). It is Linux-only in the extra definition; CUDA 13 is the
Expand Down
17 changes: 15 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,28 @@ Documentation = "https://monai.readthedocs.io/"
# All dependencies are included here except some omitted for compatibility. Testing dependencies are typically not
# needed and so present only in "testing". Ensure requirement changes in other lists are reflected here as well.
all=[
"monai[all_python,pynvml,cucim]",
# nni is listed here rather than in all_python because it depends on nvidia-ml-py
"monai[all_python,nni,pynvml,cucim]",
# per #5882 these requirements must be on one line to get removed by sed in weekly-preview.yml
"MetricsReloaded @ git+https://github.com/Project-MONAI/MetricsReloaded@monai-support",
"segment_anything @ git+https://github.com/facebookresearch/segment-anything.git@6fdee8f2727f4506cfbbe553e23b895e27956588"
]
# The counterpart of "all" for AMD ROCm, do not combine it with "all", "cucim" or "cupy": the AMD and NVIDIA packages
# install the same cucim and cupy modules. See the ROCm section of docs/source/installation.md.
all_amd=[
"monai[all_python,amd_cupy,hipcim]",
# per #5882 these requirements must be on one line to get removed by sed in weekly-preview.yml
"MetricsReloaded @ git+https://github.com/Project-MONAI/MetricsReloaded@monai-support",
"segment_anything @ git+https://github.com/facebookresearch/segment-anything.git@6fdee8f2727f4506cfbbe553e23b895e27956588"
]
# All dependencies which are not conditional on platform are here, included as MONAI optionals defined below this one.
all_python=[
"monai[clearml,einops,fire,gdown,h5py,huggingface_hub,ignite,imagecodecs,itk,jsonschema,lmdb,matplotlib,mlflow]",
"monai[nibabel,nni,onnx,openslide,optuna,pandas,pandas,pillow,polygraphy,psutil,pyamg,pybind11,pydicom,pynrrd]",
"monai[nibabel,onnx,openslide,optuna,pandas,pandas,pillow,polygraphy,psutil,pyamg,pybind11,pydicom,pynrrd]",
"monai[pyyaml,requests,scipy,skimage,tensorboard,tensorboardX,tifffile,torchio,torchvision,tqdm,transformers,zarr]"
]
# AMD ROCm build of CuPy, from AMD's package index; the floor skips the 0.0.2 placeholder on PyPI
amd_cupy = ["amd-cupy>=14.1.1; platform_system == 'Linux'"]
clearml = ["clearml>=1.10.0rc0"]
cucim = [
"cucim-cu12; platform_system == 'Linux' and python_version <= '3.10'",
Expand All @@ -78,6 +89,8 @@ einops = ["einops"]
fire = ["fire"]
gdown = ["gdown>=4.7.3"]
h5py = ["h5py"]
# hipCIM, the AMD ROCm build of cuCIM, from AMD's package index; the floor skips the 0.0.2 placeholder on PyPI
hipcim = ["amd-hipcim>=26.6.0; platform_system == 'Linux'"]
huggingface_hub = ["huggingface_hub"]
hyena = ["nvsubquadratic>=0.1.1", "omegaconf", "einops"] # omitted from all for compatibility
ignite = ["pytorch-ignite"]
Expand Down
15 changes: 15 additions & 0 deletions tests/config/test_print_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
from __future__ import annotations

import os
import re
import unittest
from contextlib import redirect_stdout
from io import StringIO
from pathlib import Path
from tempfile import NamedTemporaryFile
from unittest.mock import patch

Expand Down Expand Up @@ -46,6 +48,9 @@
(["recursive_weekly"], ["black", "coverage", "numpy", "torch"]),
]

PYPROJECT = Path(__file__).parents[2] / "pyproject.toml"
NVIDIA_ONLY = re.compile(r"cucim-cu|cupy-cuda|nvidia|nni\b", re.IGNORECASE)


@patch("monai.config.print_dependencies.PROJ", "test")
@patch("monai.config.print_dependencies.PROJ_WEEKLY", "test-weekly")
Expand Down Expand Up @@ -82,5 +87,15 @@ def test_print_dependencies(self):
print_dependencies_argv()


@unittest.skipUnless(PYPROJECT.is_file(), "requires pyproject.toml from the source tree")
class TestAmdExtras(unittest.TestCase):
def test_all_amd_has_no_nvidia_packages(self):
"""`all_amd` is for ROCm machines, where packages needing NVIDIA hardware install but do not work."""
deps = parse_dependencies(str(PYPROJECT), ["all_amd"])
self.assertEqual([], [d for d in deps if NVIDIA_ONLY.match(d)])
for package in ("amd-cupy", "amd-hipcim"):
self.assertTrue(any(d.startswith(package) for d in deps), f"{package} missing from all_amd")


if __name__ == "__main__":
unittest.main()
Loading