Skip to content

Add GPU-accelerated dcm image decoding#8954

Open
MMelQin wants to merge 8 commits into
devfrom
mq/add_gpu_dcm_decoding
Open

Add GPU-accelerated dcm image decoding#8954
MMelQin wants to merge 8 commits into
devfrom
mq/add_gpu_dcm_decoding

Conversation

@MMelQin

@MMelQin MMelQin commented Jun 27, 2026

Copy link
Copy Markdown

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

  • Non-breaking change (fix or new feature that would not break existing 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.

MMelQin added 3 commits June 26, 2026 14:13
…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>
@coderabbitai

coderabbitai Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Adds 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)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 38.24% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The description covers the feature, tests, docs, and change type checklist, matching the template closely.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly matches the main change: adding GPU-accelerated DICOM image decoding.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch mq/add_gpu_dcm_decoding

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.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

🧹 Nitpick comments (1)
monai/utils/misc.py (1)

577-583: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consolidate the DICOM reader env lookup

MONAIEnvVars.dicom_reader() has no callers, while get_preferred_dicom_reader_key() reads MONAI_DICOM_READER directly 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

📥 Commits

Reviewing files that changed from the base of the PR and between 4ba89bd and 9d040af.

📒 Files selected for processing (12)
  • CONTRIBUTING.md
  • docs/source/data.rst
  • monai/data/__init__.py
  • monai/data/image_reader.py
  • monai/data/nvimgcodec_pydicom_plugin.py
  • monai/transforms/io/array.py
  • monai/transforms/io/dictionary.py
  • monai/utils/misc.py
  • pyproject.toml
  • requirements-dev.txt
  • tests/data/test_init_reader.py
  • tests/data/test_nvimgcodec_pydicom_reader.py

Comment on lines +1150 to +1156
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)

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.

🩺 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.

Suggested change
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.

Comment on lines +49 to +54
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)

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.

🩺 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.

MMelQin added 4 commits June 26, 2026 19:09
Signed-off-by: M Q <mingmelvinq@nvidia.com>
Signed-off-by: M Q <mingmelvinq@nvidia.com>
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>

@coderabbitai coderabbitai Bot 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.

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 lift

Keep fallback DICOM readers registered.

get_default_reader_registration_order() now registers only the preferred DICOM reader. With MONAI_DICOM_READER=nvimgcodec, NvImgCodecPydicomReader.verify_suffix() returns False when 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

📥 Commits

Reviewing files that changed from the base of the PR and between f86e635 and 1781568.

📒 Files selected for processing (1)
  • monai/transforms/io/array.py

@MMelQin MMelQin changed the title Mq/add gpu dcm decoding Add GPU-accelerated dcm image decoding Jun 27, 2026
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.

1 participant