Skip to content

cuda.core: replace library asserts with explicit errors - #3223

Open
fedonman wants to merge 1 commit into
NVIDIA:mainfrom
fedonman:core-replace-library-asserts
Open

fedonman wants to merge 1 commit into
NVIDIA:mainfrom
fedonman:core-replace-library-asserts

Conversation

@fedonman

Copy link
Copy Markdown
Contributor

Replaces the 14 remaining assert statements in cuda_core/cuda/core with explicit errors, following the rules in the issue: a value from a caller, a DLPack or CAI producer, or an NVML payload raises ValueError or TypeError naming the value, and an internal invariant raises the "internal cuda.core error, please report" RuntimeError. The assert False in view_as_dlpack gets a ValueError instead, since a producer reaches it by returning anything other than an unconsumed capsule. S101 is enforced for cuda_core/cuda/core/** only, because enabling it everywhere outside tests/ flags about 180 asserts in pathfinder, the samples and the test helpers. The two in _ipc.pyx stay with #2704.

Closes #3202

python -O strips assert statements, and Cython drops them too, so the
checks they carried vanished in optimized runs. Values that come from a
caller, a DLPack or CUDA Array Interface producer, or an NVML payload
now raise ValueError or TypeError naming the value. Internal invariants
raise RuntimeError("internal cuda.core error, please report: ...").

The unmatched-capsule branch in view_as_dlpack was an assert False, but
a producer reaches it by returning anything other than an unconsumed
DLPack capsule, so it raises ValueError rather than the internal error.

Enforce ruff S101 for cuda_core/cuda/core only. Enabling it for
everything outside tests would flag about 180 asserts in pathfinder,
the samples and the shared test helpers.

The two asserts in _memory/_ipc.pyx are handled by NVIDIA#2704.
@copy-pr-bot

copy-pr-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the cuda.core Everything related to the cuda.core module label Oct 26, 2026
@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Invalid CUDA contexts, streams, memory locations, and program options now produce clear exceptions instead of assertion failures.
    • DLPack and CUDA Array Interface inputs receive explicit validation, with invalid device IDs, capsules, and producer streams reported as ValueError.
    • Invalid device power states and event or field data with unexpected counts now raise ValueError, including when Python assertions are disabled.
    • Additional invalid inputs, such as unsupported code types, are reported with specific exceptions.

The PR replaces assertions with explicit exceptions across CUDA device, memory, interop, program-cache, and system code. It adds tests for several invalid inputs and changes Ruff’s S101 ignore configuration.

Changes: Library assertion replacements

Layer / File(s) Summary
CUDA and interop validation
cuda_core/cuda/core/_device.pyx, cuda_core/cuda/core/_memory/*, cuda_core/cuda/core/_memoryview.pyx, cuda_core/tests/test_memory.py, cuda_core/tests/test_utils.py, cuda_core/tests/test_utils_dlpack.py, ruff.toml
Device, memory, and interop checks now raise explicit exceptions for invalid states or inputs. Tests cover invalid stream types, DLPack device IDs and capsules, and CUDA Array Interface stream values. Ruff’s global S101 ignore is removed, with a per-file ignore added for cuda_core/cuda/core/**.
Program and cache checks
cuda_core/cuda/core/_module.pyx, cuda_core/cuda/core/_program.pyx, cuda_core/cuda/core/utils/_program_cache/*, cuda_core/tests/test_module.py
Unsupported code types, missing architecture values, and missing program-cache exceptions now raise explicit errors. A test checks rejection of an unsupported code type.
System value validation
cuda_core/cuda/core/system/_device.pyx, cuda_core/cuda/core/system/_field_values.pxi, cuda_core/cuda/core/system/_system_events.pyx, cuda_core/tests/system/*
P-state bounds, field-value count, and event count checks now raise explicit errors. Tests check rejection of multiple field values and events.

Assessment against linked issues

Objective Addressed Explanation
Replace the remaining library assertions with explicit errors [#3202] ✅
Enable Ruff S101 for library code and limit the per-file ignore to tests [#3202] ❌ The per-file ignore applies to cuda_core/cuda/core/**, so Ruff still ignores S101 for library code.

Possibly related PRs

  • NVIDIA/cuda-python#3222: Its tests overlap this PR’s coverage of invalid code types, stream types, and DLPack validation.

Suggested labels: bug, cuda.core

Suggested reviewers: mdboom, andy-jost, cpcloud

Priority: Normal

Change: Bug fix

Merge Risk: Low · up to 35983

A failing test may leave a managed resource alive and cause subsequent CUDA test teardown failures; add explicit cleanup.

Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1


Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between a2fb804 and 35983fa.

Files selected for processing: 18
  • cuda_core/cuda/core/_device.pyx
  • cuda_core/cuda/core/_memory/_managed_buffer.py
  • cuda_core/cuda/core/_memory/_managed_memory_resource.pyx
  • cuda_core/cuda/core/_memoryview.pyx
  • cuda_core/cuda/core/_module.pyx
  • cuda_core/cuda/core/_program.pyx
  • cuda_core/cuda/core/system/_device.pyx
  • cuda_core/cuda/core/system/_field_values.pxi
  • cuda_core/cuda/core/system/_system_events.pyx
  • cuda_core/cuda/core/utils/_program_cache/_file_stream.py
  • cuda_core/cuda/core/utils/_program_cache/_keys.py
  • cuda_core/tests/system/test_system_device.py
  • cuda_core/tests/system/test_system_events.py
  • cuda_core/tests/test_memory.py
  • cuda_core/tests/test_module.py
  • cuda_core/tests/test_utils.py
  • cuda_core/tests/test_utils_dlpack.py
  • ruff.toml

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

def test_managed_memory_resource_allocate_rejects_graph_builder(init_cuda):
device = init_cuda
skip_if_managed_memory_unsupported(device)
mr = create_managed_memory_resource_or_skip(ManagedMemoryResourceOptions(preferred_location=device.device_id))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Stability & Availability · Quick win

important: Close the owned managed memory resource. ManagedMemoryResourceOptions(...) creates an owned pool, but the finally block closes only gb. If the assertion fails and pytest retains the test frame, mr can remain live through fixture teardown and trigger an unreleased-resource failure. Close mr in a finally block that also covers graph-builder creation. As per coding guidelines, “Release what you construct regardless”. As per path instructions, “Release constructed resources; use context managers where suitable”.

Sources: Coding guidelines, Path instructions

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.core Everything related to the cuda.core module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cuda.core: replace bare assert statements in library code with explicit errors

1 participant