Skip to content

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

Open
fedonman wants to merge 2 commits into
NVIDIA:mainfrom
fedonman:core-replace-library-asserts
Open

fedonman wants to merge 2 commits into
NVIDIA:mainfrom
fedonman:core-replace-library-asserts

Conversation

@fedonman

@fedonman fedonman commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Replaces the 15 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. The issue's fifth _memoryview.pyx hit is a docstring example and stays; the fifteenth is the one in unsqueeze_extents in _layout.pyx. A 1.3.0 release note lists the entry points whose exception type changes. 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. Ruff does not read .pyx files, so it does not cover the Cython asserts. 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 →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

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

Files selected for processing: 3
  • cuda_core/cuda/core/_layout.pyx
  • cuda_core/docs/source/release/1.3.0-notes.rst
  • cuda_core/tests/test_memory.py
Files skipped from review as they are similar to previous changes: 1
  • cuda_core/tests/test_memory.py

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

📝 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; invalid device IDs, capsules, and producer streams raise ValueError.
    • Invalid device power states and event or field data with unexpected counts now raise ValueError, including when Python assertions are disabled.
    • Unsupported code types and other invalid inputs now produce specific exceptions. Unexpected internal conditions raise RuntimeError rather than relying on assertions.

The PR replaces assertions with explicit exceptions across CUDA device, memory, interop, program, cache, and system code. It adds tests for selected invalid inputs, changes Ruff’s S101 configuration, and documents the changes in release notes.

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, but the per-file ignore still applies to 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/*, cuda_core/cuda/core/_layout.pyx, cuda_core/tests/system/*, cuda_core/docs/source/release/1.3.0-notes.rst
P-state bounds, field-value and event counts, and an axis-count invariant now raise explicit errors. Tests check rejection of multiple field values and events. Release notes describe the exception changes.

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 still applies to cuda_core/cuda/core/**, including library code.

Suggested labels: bug

Suggested reviewers: mdboom, andy-jost, cpcloud

Priority: Low

Change: Bug fix

Merge Risk: Minimal · up to 00e3d

The changes are ready to merge subject to normal checks.

- [ ] 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.

Comment thread cuda_core/tests/test_memory.py Outdated
@Andy-Jost Andy-Jost added enhancement Any code-related improvements P2 Low priority - Nice to have labels Oct 26, 2026
@Andy-Jost Andy-Jost added this to the cuda.core 1.3.0 milestone Oct 26, 2026
@Andy-Jost

Copy link
Copy Markdown
Contributor

/ok to test 35983fa

@copy-pr-bot

copy-pr-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

/ok to test 35983fa

@Andy-Jost, there was an error processing your request: E2

See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/2/

@Andy-Jost Andy-Jost 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.

Thanks for taking this on, @fedonman. The conversions follow the rules in #3202, the exception types match who can trigger each check, and the new tests check the right things. Three items before this merges.

  1. cuda_core/cuda/core/_layout.pyx, line 1185. One library assert remains: assert in_i == ndim in unsqueeze_extents. The inventory on #2697 filed every _layout.pyx hit as a doctest line. This one is code. It is an internal invariant on the layout math, so under python -O the check vanishes. The function already raises under nogil at lines 1164 and 1167, so a raise is legal here. Please replace it with the internal-invariant form:

    if in_i != ndim:
        raise RuntimeError(
            f"internal cuda.core error, please report: unsqueeze_extents consumed {in_i} of {ndim} input axes"
        )

    Please also update the PR description. The count is 15, not 14. The fifth _memoryview.pyx hit that the issue counts is an example inside the args_viewable_as_strided_memory docstring and stays as text.

  2. cuda_core/docs/source/release/1.3.0-notes.rst, line 104. Please add a "Fixes and enhancements" entry. Callers now see ValueError or TypeError where they saw AssertionError before, or nothing under python -O. The affected entry points are StridedMemoryView.from_dlpack (producer reports a bad device id, or __dlpack__ returns a non-capsule), StridedMemoryView.from_cuda_array_interface (CAI stream of 0), ManagedMemoryResource.allocate (non-Stream argument), ObjectCode unpickling (unknown code type), and system.FieldValue / system.SystemEvent (payload length other than 1). Internal invariants now raise RuntimeError with the "internal cuda.core error, please report" prefix. Please reference #3202. The 1.3.0 notes already record exception-type changes of this kind, and error_handling.rst documents the contract these now follow.

  3. cuda_core/tests/test_memory.py, line 1158 (nit). test_managed_memory_resource_allocate_rejects_graph_builder closes gb but not mr. The test guide asks that each test release what it constructs. Please create mr and gb inside one try and close both in finally. CodeRabbit raised the same point.

I am setting the labels, milestone, and assignees and starting CI now; the diff touches library sources, tests, and ruff.toml only. Once the three items are in and CI is green, this is ready to merge.

unsqueeze_extents in _layout.pyx still checked an internal invariant
with assert, which python -O strips. It now raises the "internal
cuda.core error, please report" RuntimeError.

Add a 1.3.0 release note that lists the entry points whose failed
checks now raise ValueError or TypeError instead of AssertionError.

The managed memory allocate test now closes the resource as well as
the graph builder.
@fedonman

fedonman commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @Andy-Jost, all three are done, and the description now says 15. The release note also covers system.Device.performance_state, which raises ValueError the same way when NVML reports a P-state outside 0 to 15.

@Andy-Jost

Copy link
Copy Markdown
Contributor

/ok to test 00e3dd4

@github-actions

Copy link
Copy Markdown
Contributor

@Andy-Jost

Copy link
Copy Markdown
Contributor

Thanks, @fedonman: the three items are in and look right. Two more things before this can merge, both found by CI on 00e3dd4:

  1. The analyze job (the Bandit pass: ruff check --select S with the S codes of ruff.toml's global ignore list) now reports 911 S101 hits, all under cuda_bindings_12/. That tree is made of symlinks into cuda_bindings_13/, and ruff does not apply the negated per-file-ignores pattern ("!cuda_core/cuda/core/**") to files it reaches through those symlinks, while a positive pattern works. One line in [lint.per-file-ignores] fixes it:

    "cuda_bindings_12/**" = ["S101"]

    Checked with ruff 0.17.2: the same --select S --ignore S311,S404 run over the whole repository passes with that line.

  2. The two cuda_bindings_13 coredump test failures on the free-threaded 3.14t jobs (test_coredump_bool_uses_single_byte_storage, test_ffi_coverage_coredump_generation_flags) were fixed on main by Fix coredump tests on free-threaded builds #3226; please merge main into the branch. The Windows job that lost its runner is unrelated.

After the push I will start CI again and merge when it is green.

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 enhancement Any code-related improvements P2 Low priority - Nice to have

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

2 participants