-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add GPU-accelerated dcm image decoding #8954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MMelQin
wants to merge
8
commits into
dev
Choose a base branch
from
mq/add_gpu_dcm_decoding
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3b7bd64
Add new DICOM reader using Pydicom and nvimgcode GPU accelerated deco…
MMelQin fb8fed2
Clarify why GPU direct loading not compatible with accelerated decomp…
MMelQin e576dc5
Fix formatting complaints
MMelQin 9d040af
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 89b896a
Fix mypy complaints
MMelQin d805734
Fixed and tested doc build warnings and nits
MMelQin f86e635
Merge branch 'mq/add_gpu_dcm_decoding' of github.com:Project-MONAI/MO…
MMelQin 1781568
Fix test failures after this branch’s reader registration change,
MMelQin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Copyright (c) MONAI Consortium | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| MONAI integration helpers for the nvImageCodec pydicom decoder plugin. | ||
|
|
||
| The decoder implementation lives in ``nvidia.nvimgcodec.tools.dicom.pydicom_plugin`` | ||
| (shipped with ``nvidia-nvimgcodec-cuXX``). This module provides MONAI-facing helpers | ||
| and stable aliases for registration and availability checks. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| from monai.utils import optional_import | ||
|
|
||
| cp, has_cp = optional_import("cupy") | ||
| pydicom_plugin, has_pydicom_plugin = optional_import("nvidia.nvimgcodec.tools.dicom.pydicom_plugin") | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
| if has_pydicom_plugin: | ||
| DECODER_DEPENDENCIES = pydicom_plugin.DECODER_DEPENDENCIES | ||
| NVIMGCODEC_MIN_VERSION = pydicom_plugin.NVIMGCODEC_MIN_VERSION | ||
| NVIMGCODEC_MIN_VERSION_TUPLE = pydicom_plugin.NVIMGCODEC_MIN_VERSION_TUPLE | ||
| NVIMGCODEC_PLUGIN_LABEL = pydicom_plugin.NVIMGCODEC_PLUGIN_LABEL | ||
| SUPPORTED_DECODER_CLASSES = pydicom_plugin.SUPPORTED_DECODER_CLASSES | ||
| SUPPORTED_TRANSFER_SYNTAXES = pydicom_plugin.SUPPORTED_TRANSFER_SYNTAXES | ||
| is_available = pydicom_plugin.is_available | ||
| else: # pragma: no cover - optional dependency not installed | ||
| DECODER_DEPENDENCIES = {} | ||
| NVIMGCODEC_MIN_VERSION = "0.8.0" | ||
| NVIMGCODEC_MIN_VERSION_TUPLE = (0, 8, 0) | ||
| NVIMGCODEC_PLUGIN_LABEL = "0.8.0+nvimgcodec" | ||
| SUPPORTED_DECODER_CLASSES = [] | ||
| SUPPORTED_TRANSFER_SYNTAXES = [] | ||
|
|
||
| def is_available(uid) -> bool: # type: ignore[no-redef] | ||
| return False | ||
|
|
||
|
|
||
| def is_nvimgcodec_available() -> bool: | ||
| """Return ``True`` if nvImageCodec with CUDA support is available.""" | ||
| if not has_pydicom_plugin or getattr(pydicom_plugin, "nvimgcodec", None) is None or not has_cp: | ||
| _logger.debug("nvimgcodec pydicom plugin, nvimgcodec module, or CuPy missing.") | ||
| return False | ||
| try: | ||
| if not cp.cuda.is_available(): | ||
| _logger.debug("CUDA device not found.") | ||
| return False | ||
| except Exception as exc: # pragma: no cover - environment specific | ||
| _logger.debug(f"CUDA availability check failed: {exc}") | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| def register_as_decoder_plugin(module_path: str | None = None) -> bool: | ||
| """Register the nvImageCodec pydicom decoder plugin.""" | ||
| if not is_nvimgcodec_available(): | ||
| _logger.warning("nvImageCodec is not available; skipping pydicom decoder plugin registration.") | ||
| return False | ||
| if not has_pydicom_plugin: | ||
| return False | ||
| return bool(pydicom_plugin.register(module_path)) | ||
|
|
||
|
|
||
| def unregister_as_decoder_plugin() -> bool: | ||
| """Unregister the nvImageCodec pydicom decoder plugin.""" | ||
| if not has_pydicom_plugin: | ||
| return False | ||
| return bool(pydicom_plugin.unregister()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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_suffixreturnsFalsewheneverself._nvimgcodec_availableisFalse. Combined withget_default_reader_registration_order()(which registers only the single preferred DICOM reader, not ITK/pydicom), this means: withMONAI_DICOM_READER=nvimgcodecon a host lacking CUDA/nvimgcodec, the only registered DICOM reader rejects every DICOM in auto-selection, andLoadImagefinds no suitable reader. That directly contradicts the class docstring promise that it "falls back to the default pydicom decoders (same behavior asPydicomReader)."The plugin-registration fallback only affects decoding after selection;
verify_suffixgating blocks selection entirely. Suggest dropping the_nvimgcodec_availablegate 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
🤖 Prompt for AI Agents