Skip to content

Replace deprecated cuda.cudart with cuda.bindings.runtime.#8790

Open
ytl0623 wants to merge 2 commits intoProject-MONAI:devfrom
ytl0623:fix-issue-8789
Open

Replace deprecated cuda.cudart with cuda.bindings.runtime.#8790
ytl0623 wants to merge 2 commits intoProject-MONAI:devfrom
ytl0623:fix-issue-8789

Conversation

@ytl0623
Copy link
Contributor

@ytl0623 ytl0623 commented Mar 24, 2026

Fixes #8789

Description

Replaces the deprecated cuda.cudart import in trt_compiler.py with cuda.bindings.runtime, which is the current API introduced in cuda-bindings >= 12.6 (pulled in by PyTorch 2.10+).

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.

Signed-off-by: ytl0623 <david89062388@gmail.com>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 24, 2026

📝 Walkthrough

Walkthrough

The patch modifies monai/networks/trt_compiler.py to prefer importing cuda.bindings.runtime and fall back to cuda.cudart only if the first is unavailable (avoiding a deprecation warning). The cuassert helper now treats cuda_ret as a tuple, checks err.value != 0, and when an error occurs attempts to enrich the raised RuntimeError with cudaGetErrorName and cudaGetErrorString, falling back to a basic message and logging a debug note if enrichment fails.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed Title directly and clearly describes the main change: replacing deprecated cuda.cudart with cuda.bindings.runtime.
Description check ✅ Passed Description follows template structure, links to issue #8789, explains the change, and notes it as non-breaking.
Linked Issues check ✅ Passed Code changes fully address issue #8789: prioritize cuda.bindings.runtime, fallback to cuda.cudart, and enrich error messaging.
Out of Scope Changes check ✅ Passed All changes in trt_compiler.py are directly related to the import migration and error handling within scope of issue #8789.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

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/networks/trt_compiler.py (1)

82-100: Docstring not updated for new behavior.

The docstring says cuda_ret: CUDA return code but the function now accesses err.value (an enum attribute). Consider updating to reflect that cuda_ret is a tuple containing a CUDA error enum. As per coding guidelines, docstrings should describe each variable.

📝 Suggested docstring update
 def cuassert(cuda_ret):
     """
     Error reporting method for CUDA calls.
+
     Args:
-     cuda_ret: CUDA return code.
+        cuda_ret: Tuple returned by CUDA runtime calls, where the first element
+            is a cudaError_t enum and subsequent elements are return values.
+
+    Raises:
+        RuntimeError: If the CUDA call returned an error.
     """
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@monai/networks/trt_compiler.py` around lines 82 - 100, Update the cuassert
docstring to reflect the actual input and output: state that cuda_ret is a tuple
where the first element is a CUDA error enum/object (accessed as err.value) and
an optional second element is a return value; document the Args (cuda_ret: tuple
of (cuda_error_enum, optional_result)) and the function's behavior/return
(raises RuntimeError on non-zero err.value, returns the second tuple element if
present, otherwise None). Mention the function name cuassert and the err.value
access in the description so the docstring matches the implementation.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@monai/networks/trt_compiler.py`:
- Around line 89-97: The bare "except Exception: pass" in the CUDA error path
silently hides failures when calling cudart.cudaGetErrorName and
cudart.cudaGetErrorString; update the except to capture the exception (e.g.,
except Exception as e) and record it (via logging.exception or logger.error with
the exception info) and/or append its text to err_msg before re-raising, so that
the RuntimeError raised from the block (around variables err, err_msg and calls
cudart.cudaGetErrorName / cudart.cudaGetErrorString) includes the original
lookup error details for debuggability.
- Line 42: The import removed the fallback and only attempts
optional_import("cuda.bindings.runtime"), breaking compatibility; restore the
fallback by attempting optional_import("cuda.bindings.runtime") first and if
cudart_imported is False (or cudart is None) then call
optional_import("cuda.cudart") to populate cudart and cudart_imported; update
the variables used elsewhere in this module (e.g., cudart, cudart_imported in
monai/networks/trt_compiler.py) so the rest of the code and tests that expect
the cuda.cudart fallback continue to work.

---

Nitpick comments:
In `@monai/networks/trt_compiler.py`:
- Around line 82-100: Update the cuassert docstring to reflect the actual input
and output: state that cuda_ret is a tuple where the first element is a CUDA
error enum/object (accessed as err.value) and an optional second element is a
return value; document the Args (cuda_ret: tuple of (cuda_error_enum,
optional_result)) and the function's behavior/return (raises RuntimeError on
non-zero err.value, returns the second tuple element if present, otherwise
None). Mention the function name cuassert and the err.value access in the
description so the docstring matches the implementation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ee2622a5-b0ee-452c-ac51-8f9b4ab7e6a3

📥 Commits

Reviewing files that changed from the base of the PR and between 5b71547 and 7a11b37.

📒 Files selected for processing (1)
  • monai/networks/trt_compiler.py

Comment on lines +89 to +97
if err.value != 0:
err_msg = f"CUDA ERROR: {err.value}"
try:
_, err_name = cudart.cudaGetErrorName(err)
_, err_str = cudart.cudaGetErrorString(err)
err_msg = f"CUDA ERROR {err.value}: {err_name} — {err_str}"
except Exception:
pass
raise RuntimeError(err_msg)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Bare except Exception: pass silently swallows errors.

Static analysis flags this pattern (Ruff S110, BLE001). If error name/string retrieval fails, consider logging the exception for debuggability rather than silently ignoring it.

🛠️ Proposed fix to log suppressed exceptions
         try:
             _, err_name = cudart.cudaGetErrorName(err)
             _, err_str = cudart.cudaGetErrorString(err)
             err_msg = f"CUDA ERROR {err.value}: {err_name} — {err_str}"
-        except Exception:
-            pass
+        except Exception as e:
+            get_logger("monai.networks.trt_compiler").debug(f"Failed to retrieve CUDA error details: {e}")
         raise RuntimeError(err_msg)
🧰 Tools
🪛 Ruff (0.15.6)

[error] 95-96: try-except-pass detected, consider logging the exception

(S110)


[warning] 95-95: Do not catch blind exception: Exception

(BLE001)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@monai/networks/trt_compiler.py` around lines 89 - 97, The bare "except
Exception: pass" in the CUDA error path silently hides failures when calling
cudart.cudaGetErrorName and cudart.cudaGetErrorString; update the except to
capture the exception (e.g., except Exception as e) and record it (via
logging.exception or logger.error with the exception info) and/or append its
text to err_msg before re-raising, so that the RuntimeError raised from the
block (around variables err, err_msg and calls cudart.cudaGetErrorName /
cudart.cudaGetErrorString) includes the original lookup error details for
debuggability.

1. fix: Docstring not updated for new behavior.
2. fix: Missing fallback breaks backward compatibility.
3. fix: Bare except Exception: pass silently swallows errors.

Signed-off-by: ytl0623 <david89062388@gmail.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
monai/networks/trt_compiler.py (1)

42-44: Extend tests around the new import preference.

The diff does not add targeted regression coverage for the preferred cuda.bindings.runtime path vs the cuda.cudart fallback. Please add a small mock-based test that forces both branches.

As per coding guidelines, "Ensure new or modified definitions will be covered by existing or new unit tests."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@monai/networks/trt_compiler.py` around lines 42 - 44, Add unit tests that
mock optional_import to force both import branches in
monai.networks.trt_compiler: one test should make
optional_import("cuda.bindings.runtime") return (mock_module, True) so the code
picks the preferred path, and assert behavior/state that relies on cudart being
the returned module; a second test should make
optional_import("cuda.bindings.runtime") return (None, False) and
optional_import("cuda.cudart") return (mock_module, True) to exercise the
fallback, asserting the same expected behavior. Use monkeypatch or
unittest.mock.patch to stub optional_import, reference the function
optional_import and the module monai.networks.trt_compiler (or the function that
uses cudart there) to import the target under test after patching so the import
branch is executed, and include assertions verifying that the chosen module was
used in each case.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@monai/networks/trt_compiler.py`:
- Around line 85-92: The cuassert() docstring currently omits a Returns section
and the implementation only returns cuda_ret[1] or None, which contradicts the
comment that "subsequent elements are return values"; update both the behavior
and docstring for cuassert(): change the function to return all subsequent
elements from cuda_ret (return None if there are none, return the single element
if exactly one, or return a tuple of elements if multiple) and add a
Google-style "Returns:" section describing these cases, and keep the existing
"Raises:" text for RuntimeError when the first element indicates an error.

---

Nitpick comments:
In `@monai/networks/trt_compiler.py`:
- Around line 42-44: Add unit tests that mock optional_import to force both
import branches in monai.networks.trt_compiler: one test should make
optional_import("cuda.bindings.runtime") return (mock_module, True) so the code
picks the preferred path, and assert behavior/state that relies on cudart being
the returned module; a second test should make
optional_import("cuda.bindings.runtime") return (None, False) and
optional_import("cuda.cudart") return (mock_module, True) to exercise the
fallback, asserting the same expected behavior. Use monkeypatch or
unittest.mock.patch to stub optional_import, reference the function
optional_import and the module monai.networks.trt_compiler (or the function that
uses cudart there) to import the target under test after patching so the import
branch is executed, and include assertions verifying that the chosen module was
used in each case.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 76d53fa1-999d-4a70-8d4e-3ce11b85e970

📥 Commits

Reviewing files that changed from the base of the PR and between 7a11b37 and 502a239.

📒 Files selected for processing (1)
  • monai/networks/trt_compiler.py

Comment on lines 85 to +92
"""
Error reporting method for CUDA calls.
Args:
cuda_ret: CUDA return code.
cuda_ret: Tuple returned by CUDA runtime calls, where the first element
is a cudaError_t enum and subsequent elements are return values.

Raises:
RuntimeError: If the CUDA call returned an error.
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Document cuassert()'s return contract fully.

The updated docstring adds Raises, but it still omits Returns, and the implementation only returns cuda_ret[1] or None. That is narrower than “subsequent elements are return values.”

As per coding guidelines, "Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings."

Also applies to: 104-105

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@monai/networks/trt_compiler.py` around lines 85 - 92, The cuassert()
docstring currently omits a Returns section and the implementation only returns
cuda_ret[1] or None, which contradicts the comment that "subsequent elements are
return values"; update both the behavior and docstring for cuassert(): change
the function to return all subsequent elements from cuda_ret (return None if
there are none, return the single element if exactly one, or return a tuple of
elements if multiple) and add a Google-style "Returns:" section describing these
cases, and keep the existing "Raises:" text for RuntimeError when the first
element indicates an error.

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.

FutureWarning: cuda.cudart module is deprecated

1 participant