Skip to content

[https://nvbugs/6482589][fix] Make CuError pickle-safe across process boundaries - #17289

Open
lowsfer wants to merge 1 commit into
NVIDIA:mainfrom
lowsfer:nvbug-6482589-cuerror-pickle-safe
Open

[https://nvbugs/6482589][fix] Make CuError pickle-safe across process boundaries#17289
lowsfer wants to merge 1 commit into
NVIDIA:mainfrom
lowsfer:nvbug-6482589-cuerror-pickle-safe

Conversation

@lowsfer

@lowsfer lowsfer commented Aug 5, 2026

Copy link
Copy Markdown
Member

Summary

  • preserve the numeric CUDA error code when CuError crosses a process boundary
  • bind and translate the C++ CuError so the Python backend retains its type and error_code
  • make the pure-Python CuError reconstruct from its numeric error code during pickling

Testing

  • pre-commit hooks passed during the commit rewrite
  • no dedicated runtime tests run

Dev Engineer Review

  • Added a C++ CuError binding with an error_code attribute.
  • Added a C++ exception translator that preserves the CUDA error code and message.
  • Exposed _cpp.CuError directly from the Python backend.
  • Added CuError.__reduce__ for reconstruction from the numeric error code.
  • The changes support pickle-safe CuError handling across process boundaries.
  • Pre-commit hooks passed.
  • No dedicated runtime tests were run.

QA Engineer Review

No test changes.

@lowsfer
lowsfer requested a review from a team as a code owner August 5, 2026 06:36
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The change adds C++ CuError binding and CUDA error-code translation. Python now requires the backend exception and supports reconstruction using error_code.

Changes

CuError exception propagation

Layer / File(s) Summary
Native CuError binding
cpp/tensorrt_llm/nanobind/batch_manager/kvCacheManagerV2.cpp
The nanobind module registers CuError, translates CUDA error codes to CUresult values or integers, preserves the message, and retains assertion-error translation behavior.
Python CuError integration
tensorrt_llm/runtime/kv_cache_manager_v2/__init__.py, tensorrt_llm/runtime/kv_cache_manager_v2/_exceptions.py
The runtime obtains CuError directly from the C++ backend. The exception supports reconstruction from error_code.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant kvCuError
  participant NanobindModule
  participant PythonCuError
  kvCuError->>NanobindModule: throw error with errorCode
  NanobindModule->>NanobindModule: convert errorCode to CUresult or integer
  NanobindModule->>PythonCuError: set error_code and preserve message
Loading

Suggested reviewers: simengliu-nv

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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
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 follows the required ticket and type format and clearly describes the pickle-safety fix for CuError.
Description check ✅ Passed The description explains the change and testing status, but it does not include the repository checklist.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

🧹 Nitpick comments (2)
cpp/tensorrt_llm/nanobind/batch_manager/kvCacheManagerV2.cpp (1)

527-529: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Brace the if (p) body.

Use braces for this control-flow body.

Proposed fix
-                if (p)
+                if (p)
+                {
                     std::rethrow_exception(p);
+                }

As per coding guidelines, “Use Allman brace style; always brace if/else, loop, and switch bodies.”

🤖 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 `@cpp/tensorrt_llm/nanobind/batch_manager/kvCacheManagerV2.cpp` around lines
527 - 529, Update the if (p) statement in the exception-handling block to wrap
std::rethrow_exception(p) in braces, following the project’s Allman brace style
and always-braced control-flow guideline.

Source: Coding guidelines

tensorrt_llm/runtime/kv_cache_manager_v2/_exceptions.py (1)

54-55: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a return annotation to CuError.__reduce__.

The repository requires annotations on every function. Annotate the reduction tuple type.

As per coding guidelines, “Annotate every function.”

🤖 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 `@tensorrt_llm/runtime/kv_cache_manager_v2/_exceptions.py` around lines 54 -
55, Update CuError.__reduce__ to include an explicit return annotation
describing the reduction tuple it returns, while preserving the existing
self.__class__ and self.error_code behavior.

Source: Coding guidelines

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

Nitpick comments:
In `@cpp/tensorrt_llm/nanobind/batch_manager/kvCacheManagerV2.cpp`:
- Around line 527-529: Update the if (p) statement in the exception-handling
block to wrap std::rethrow_exception(p) in braces, following the project’s
Allman brace style and always-braced control-flow guideline.

In `@tensorrt_llm/runtime/kv_cache_manager_v2/_exceptions.py`:
- Around line 54-55: Update CuError.__reduce__ to include an explicit return
annotation describing the reduction tuple it returns, while preserving the
existing self.__class__ and self.error_code behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 98fd52b9-255d-4486-bb72-968f06aa938e

📥 Commits

Reviewing files that changed from the base of the PR and between 7608520 and db80867.

📒 Files selected for processing (3)
  • cpp/tensorrt_llm/nanobind/batch_manager/kvCacheManagerV2.cpp
  • tensorrt_llm/runtime/kv_cache_manager_v2/__init__.py
  • tensorrt_llm/runtime/kv_cache_manager_v2/_exceptions.py

@lowsfer
lowsfer force-pushed the nvbug-6482589-cuerror-pickle-safe branch from ab22595 to 8c1a327 Compare August 5, 2026 06:47
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

CuError could not survive crossing a process boundary (e.g. disaggregated
serving). The pure-Python CuError.__init__ stores a formatted message string
in .args, so the default Exception pickling reconstructs it by calling the
constructor with that string. The constructor then feeds the string to
cuGetErrorString, which fails with "invalid literal for int() with base 10".

Fixes:
- Python backend: add CuError.__reduce__ so the exception is reconstructed
  from the numeric CUDA error code instead of the formatted message string.
- C++ backend: bind CuError in nanobind (it was never bound and fell back to
  RuntimeError, losing type identity and error_code). Add a custom exception
  translator that carries the numeric error_code onto the Python instance.
  The bound type uses the plain Exception.__init__, so BaseException.__reduce__
  round-trips both the message (.args) and error_code (__dict__) with no custom
  __reduce__ needed.
- Dispatcher: promote CuError to a first-class C++ port (_cpp.CuError) instead
  of the getattr(..., RuntimeError) fallback.

Signed-off-by: Yao Yao <lowsfer@users.noreply.github.com>
@lowsfer
lowsfer force-pushed the nvbug-6482589-cuerror-pickle-safe branch from 8c1a327 to 28f1c9b Compare August 5, 2026 06:48
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@lowsfer

lowsfer commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63974 [ run ] triggered by Bot. Commit: 28f1c9b Link to invocation

@lowsfer lowsfer changed the title [NVBUG-6482589][fix] Make CuError pickle-safe across process boundaries [https://nvbugs/6482589][fix] Make CuError pickle-safe across process boundaries Aug 5, 2026
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63974 [ run ] completed with state SUCCESS. Commit: 28f1c9b
/LLM/main/L0_MergeRequest_PR pipeline #51908 completed with status: 'FAILURE'

CI Report

⚠️ Multi-GPU Label Required:
Multi-GPU tests require the ci: full pre-merge approved label on this PR. Ask a member of NVIDIA/trt-llm-ci-approvers to add the label, then re-trigger CI with the same bot command (no rebase needed).

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@lowsfer

lowsfer commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64042 [ run ] triggered by Bot. Commit: 28f1c9b Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64042 [ run ] completed with state FAILURE. Commit: 28f1c9b
/LLM/main/L0_MergeRequest_PR pipeline #51972 completed with status: 'FAILURE'

CI Report

⚠️ Multi-GPU Label Required:
Multi-GPU tests require the ci: full pre-merge approved label on this PR. Ask a member of NVIDIA/trt-llm-ci-approvers to add the label, then re-trigger CI with the same bot command (no rebase needed).

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@lowsfer

lowsfer commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64058 [ run ] triggered by Bot. Commit: 28f1c9b Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64058 [ run ] completed with state SUCCESS. Commit: 28f1c9b
/LLM/main/L0_MergeRequest_PR pipeline #51987 completed with status: 'UNSTABLE'

CI Report

⚠️ Multi-GPU Label Required:
Multi-GPU tests require the ci: full pre-merge approved label on this PR. Ask a member of NVIDIA/trt-llm-ci-approvers to add the label, then re-trigger CI with the same bot command (no rebase needed).

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

Link to invocation

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.

2 participants