Add GPU-accelerated dcm image decoding#8954
Conversation
…mpression Signed-off-by: M Q <mingmelvinq@nvidia.com>
…ression Signed-off-by: M Q <mingmelvinq@nvidia.com>
Signed-off-by: M Q <mingmelvinq@nvidia.com>
📝 WalkthroughWalkthroughAdds env-driven DICOM reader selection, an nvImageCodec-backed pydicom reader and plugin wrapper, LoadImage registration and selection changes, plus docs, dev requirements, and tests. Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
monai/utils/misc.py (1)
577-583: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsolidate the DICOM reader env lookup
MONAIEnvVars.dicom_reader()has no callers, whileget_preferred_dicom_reader_key()readsMONAI_DICOM_READERdirectly and applies its own fallback/warning path. Drop the duplicate accessor or make the reader-key lookup use it so the behavior lives in one place.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@monai/utils/misc.py` around lines 577 - 583, The DICOM reader environment lookup is duplicated between MONAIEnvVars.dicom_reader() and get_preferred_dicom_reader_key(), so consolidate the logic in one place. Update get_preferred_dicom_reader_key() to use MONAIEnvVars.dicom_reader() for reading MONAI_DICOM_READER and handling the default, then keep any warning/fallback behavior there. If dicom_reader() is not needed elsewhere, remove the redundant accessor to avoid drift.Source: Path instructions
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@monai/data/image_reader.py`:
- Around line 1150-1156: verify_suffix in ImageReader currently blocks DICOM
selection whenever _nvimgcodec_available is false, which prevents the documented
pydicom fallback from ever being used. Update verify_suffix to accept DICOM
paths based on is_dicom_path(filename) and has_pydicom alone, without gating on
_nvimgcodec_available, so ImageReader can still be selected and decode through
the pydicom fallback path when nvImageCodec is unavailable.
In `@tests/data/test_nvimgcodec_pydicom_reader.py`:
- Around line 49-54: The current test coverage in
test_get_default_reader_registration_order only checks the pydicom override path
and misses the failing nvimgcodec fallback path. Add a test around
get_default_reader_registration_order and LoadImage auto-selection that sets
MONAI_DICOM_READER=nvimgcodec while simulating is_nvimgcodec_available() as
false, then assert the documented DICOM behavior still works (or assert the
intended fallback outcome). Use the existing LoadImage and
get_default_reader_registration_order symbols so the new case covers the
verify_suffix-related failure mode.
---
Nitpick comments:
In `@monai/utils/misc.py`:
- Around line 577-583: The DICOM reader environment lookup is duplicated between
MONAIEnvVars.dicom_reader() and get_preferred_dicom_reader_key(), so consolidate
the logic in one place. Update get_preferred_dicom_reader_key() to use
MONAIEnvVars.dicom_reader() for reading MONAI_DICOM_READER and handling the
default, then keep any warning/fallback behavior there. If dicom_reader() is not
needed elsewhere, remove the redundant accessor to avoid drift.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: e7cf914b-5fce-455f-b4c6-6b64b67c3e5e
📒 Files selected for processing (12)
CONTRIBUTING.mddocs/source/data.rstmonai/data/__init__.pymonai/data/image_reader.pymonai/data/nvimgcodec_pydicom_plugin.pymonai/transforms/io/array.pymonai/transforms/io/dictionary.pymonai/utils/misc.pypyproject.tomlrequirements-dev.txttests/data/test_init_reader.pytests/data/test_nvimgcodec_pydicom_reader.py
| def verify_suffix(self, filename: Sequence[PathLike] | PathLike) -> bool: | ||
| """ | ||
| Verify whether the specified file or files are DICOM and nvImageCodec is available. | ||
| """ | ||
| if not has_pydicom or not self._nvimgcodec_available: | ||
| return False | ||
| return is_dicom_path(filename) |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
No DICOM fallback when nvImageCodec is unavailable — contradicts the documented behavior.
verify_suffix returns False whenever self._nvimgcodec_available is False. Combined with get_default_reader_registration_order() (which registers only the single preferred DICOM reader, not ITK/pydicom), this means: with MONAI_DICOM_READER=nvimgcodec on a host lacking CUDA/nvimgcodec, the only registered DICOM reader rejects every DICOM in auto-selection, and LoadImage finds no suitable reader. That directly contradicts the class docstring promise that it "falls back to the default pydicom decoders (same behavior as PydicomReader)."
The plugin-registration fallback only affects decoding after selection; verify_suffix gating blocks selection entirely. Suggest dropping the _nvimgcodec_available gate so the reader can still serve DICOM via pydicom decoders.
🔧 Proposed fix
def verify_suffix(self, filename: Sequence[PathLike] | PathLike) -> bool:
"""
Verify whether the specified file or files are DICOM and nvImageCodec is available.
"""
- if not has_pydicom or not self._nvimgcodec_available:
+ if not has_pydicom:
return False
return is_dicom_path(filename)Confirm the intended behavior; if rejection is deliberate, the docstring fallback claim should be removed instead.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| def verify_suffix(self, filename: Sequence[PathLike] | PathLike) -> bool: | |
| """ | |
| Verify whether the specified file or files are DICOM and nvImageCodec is available. | |
| """ | |
| if not has_pydicom or not self._nvimgcodec_available: | |
| return False | |
| return is_dicom_path(filename) | |
| def verify_suffix(self, filename: Sequence[PathLike] | PathLike) -> bool: | |
| """ | |
| Verify whether the specified file or files are DICOM and nvImageCodec is available. | |
| """ | |
| if not has_pydicom: | |
| return False | |
| return is_dicom_path(filename) |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@monai/data/image_reader.py` around lines 1150 - 1156, verify_suffix in
ImageReader currently blocks DICOM selection whenever _nvimgcodec_available is
false, which prevents the documented pydicom fallback from ever being used.
Update verify_suffix to accept DICOM paths based on is_dicom_path(filename) and
has_pydicom alone, without gating on _nvimgcodec_available, so ImageReader can
still be selected and decode through the pydicom fallback path when nvImageCodec
is unavailable.
| def test_get_default_reader_registration_order(self): | ||
| with patch.dict(os.environ, {"MONAI_DICOM_READER": "pydicom"}): | ||
| order = get_default_reader_registration_order() | ||
| self.assertEqual(order[-1], "pydicomreader") | ||
| self.assertNotIn("itkreader", order) | ||
| self.assertNotIn("nvimgcodecpydicomreader", order) |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Coverage gap: the failure mode is untested.
Tests never exercise MONAI_DICOM_READER=nvimgcodec with is_nvimgcodec_available()==False going through LoadImage auto-selection. That's exactly the path that breaks (see verify_suffix comment) — a test here would have caught it. Add a case asserting DICOM still loads (or explicitly asserting the documented behavior).
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@tests/data/test_nvimgcodec_pydicom_reader.py` around lines 49 - 54, The
current test coverage in test_get_default_reader_registration_order only checks
the pydicom override path and misses the failing nvimgcodec fallback path. Add a
test around get_default_reader_registration_order and LoadImage auto-selection
that sets MONAI_DICOM_READER=nvimgcodec while simulating
is_nvimgcodec_available() as false, then assert the documented DICOM behavior
still works (or assert the intended fallback outcome). Use the existing
LoadImage and get_default_reader_registration_order symbols so the new case
covers the verify_suffix-related failure mode.
Signed-off-by: M Q <mingmelvinq@nvidia.com>
Signed-off-by: M Q <mingmelvinq@nvidia.com>
…NAI into mq/add_gpu_dcm_decoding
though the underlying bug is in LoadImage’s auto-select path.
Root cause
test_nibabel_reader_5 is TEST_CASE_4_1 with {"mmap": False} — not the NibabelReader(mmap=False) case.
What happens:
1. mmap=False is passed to all default readers, including ITKReader.
2. Your branch registers the DICOM reader last, so it is tried first in reverse auto-select order.
3. ITKReader.verify_suffix() returns True whenever ITK is installed (any file type).
4. LoadImage calls itk.imread(..., mmap=False), which ITK does not support → TypeError.
5. The auto-select path had no try/except, so it never fell back to NibabelReader.
On dev branch, ITK was tried later (dict order), so Nibabel usually won first.
Fix
Unified reader selection in LoadImage.__call__ so auto-select also catches read failures and tries
the next reader (same as the explicit-reader path)
Verification
- test_nibabel_reader_5 — passed
- All 7 test_nibabel_reader cases — passed
Signed-off-by: M Q <mingmelvinq@nvidia.com>
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
monai/transforms/io/array.py (1)
193-195: 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy liftKeep fallback DICOM readers registered.
get_default_reader_registration_order()now registers only the preferred DICOM reader. WithMONAI_DICOM_READER=nvimgcodec,NvImgCodecPydicomReader.verify_suffix()returnsFalsewhen nvimgcodec/CUDA is unavailable, so auto-selection skips the only DICOM reader and DICOM loads fail outright. Keep the other DICOM readers registered behind the preferred one so this path still falls back cleanly.Also applies to: 268-269
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@monai/transforms/io/array.py` around lines 193 - 195, The default reader registration in the reader setup path only registers the preferred DICOM reader, which prevents fallback when that reader is unavailable; update the registration logic in the reader initialization flow (the code that iterates over get_default_reader_registration_order() and calls self.register with SUPPORTED_READERS) so the preferred DICOM reader is registered first but the other DICOM readers remain registered afterward. Ensure NvImgCodecPydicomReader stays preferred when available, while keeping the remaining DICOM readers available for auto-selection if verify_suffix() rejects the preferred one.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@monai/transforms/io/array.py`:
- Around line 193-195: The default reader registration in the reader setup path
only registers the preferred DICOM reader, which prevents fallback when that
reader is unavailable; update the registration logic in the reader
initialization flow (the code that iterates over
get_default_reader_registration_order() and calls self.register with
SUPPORTED_READERS) so the preferred DICOM reader is registered first but the
other DICOM readers remain registered afterward. Ensure NvImgCodecPydicomReader
stays preferred when available, while keeping the remaining DICOM readers
available for auto-selection if verify_suffix() rejects the preferred one.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 7af1b16a-deec-4586-bafb-b0f0c73f5e06
📒 Files selected for processing (1)
monai/transforms/io/array.py
Description
Add GPU accelerated DICOM compressed image decoding using Pydicom and a custom decoder plugin based on NVIDIA nvimgcodec.
Implementation is done by subclassing the existing PydicomReader for its existing (and presumed verified) functionalities of loading dcm file(s) and parsing pixel data into image data array purely using pydicom library, and not its GPU direct load option which had been known to have serious deficiencies (omitting the proper and required interpretation of raw bytes!). This known issue needs to be addressed in a separate PR, so the new reader in this PR simply discards the GPU direct load option.
Introduced a mechanism to select DICOM reader at runtime by the use of a new env var, with the default being the ITK based reader.
It is also worth noting that the GPU accelerated decoder plugin for Pydicom was first developed, tested, and used in MONAI Deploy App SDK in this module, and then replicated in nvimgcodec for broader distribution. Full test of the decoder using all pydicom embedded test files of the supported transfer syntax is in MONAI Deploy App SDK. Future plan is to upstream this custom decoder plugin to pydicom to make the use even simpler.
Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.