Skip to content

Coerce stream_ptr to int in view_as_dlpack - #3154

Merged
mdboom merged 1 commit into
NVIDIA:mainfrom
mdboom:stream-coercion
Oct 19, 2026
Merged

mdboom merged 1 commit into
NVIDIA:mainfrom
mdboom:stream-coercion

Conversation

@mdboom

@mdboom mdboom commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Coerce stream_ptr to int in view_as_dlpack

Fixes the nightly numba-cuda failure that started after #2902
(https://github.com/NVIDIA/cuda-python/actions/runs/39190777771/job/117862954433).

Problem

#2902 set Cython's annotation_typing=False. Until then, the
stream_ptr: int | None annotations on StridedMemoryView made Cython convert
any int-convertible object to a plain int on entry. numba-cuda passes its
CUstream(0) wrapper, which is truthy and converts to 0. Without the
annotation typing, the wrapper reached view_as_dlpack unchanged, and
int(stream_ptr) if stream_ptr else None took the truthy branch. The result
was numpy_array.__dlpack__(stream=0) instead of stream=None, and NumPy
raised RuntimeError: NumPy only supports stream=None.

The stream_ptr == -1 opt-out check had the same problem: a wrapper around
-1 was never recognized and was forwarded as stream=-1.

Fix

view_as_dlpack now converts stream_ptr with int() once, up front, so every
later check sees an int. This matches what view_as_cai already does. The
fix is explicit rather than a per-function @cython.annotation_typing(True),
so it does not depend on the build-wide directive.

Audit for similar regressions from #2902

I went through every int / int | None annotated parameter in cuda_core
that #2902 un-typed. stream_ptr was the only place that makes a decision on
the raw object before converting it. The other sites store the value, check it
with is None, or convert it directly to a C type (<CUdeviceptr>dptr,
size_t, unsigned int, ...), and those conversions accept any __index__
object with or without annotation typing.

Test

test_cpu_dlpack_accepts_int_convertible_stream_ptr in
cuda_core/tests/test_utils_dlpack.py needs neither numba nor a GPU, so it
runs in the regular (non-nightly) suite.

  • A small stand-in stream wrapper is int-convertible with no __bool__, so it
    is always truthy, like CUstream.
  • A CPU DLPack source wraps a NumPy array and records the stream argument it
    receives in __dlpack__. NumPy then enforces stream=None as in the
    nightly failure.
  • The test is parametrized over from_dlpack, from_any_interface and the
    deprecated constructor, and over wrapper values 0 and -1. It asserts that
    the exporter received stream=None and that a view was produced.

All six cases fail without the fix and pass with it.

🤖 Generated with Claude Code

@mdboom mdboom added this to the cuda.core next milestone Oct 19, 2026
@mdboom mdboom self-assigned this Oct 19, 2026
@mdboom mdboom added bug Something isn't working P0 High priority - Must do! cuda.core Everything related to the cuda.core module labels Oct 19, 2026
@mdboom
mdboom requested a review from jpascucci-nv October 19, 2026 18:43
@coderabbitai

coderabbitai Bot commented Oct 19, 2026

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: NVIDIA/cuda-python/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: bc26c87c-7eef-4c47-bfa8-0d9abcb98b02
📥 Commits

Reviewing files that changed from the base of the PR and between 9d903cd and c72fa87.

📒 Files selected for processing: 2
  • cuda_core/cuda/core/_memoryview.pyx
  • cuda_core/tests/test_utils_dlpack.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
    • Improved compatibility when creating views from DLPack objects using stream handles that can be converted to integers.
    • Applied consistent stream-handle handling across the available DLPack import methods, including for CPU-backed objects.

view_as_dlpack now converts each non-None stream pointer to an integer before validation and DLPack export. Tests cover handles that convert to 0 or -1 across three view-construction paths.

Changes: DLPack stream handling

Layer / File(s) Summary
Stream handle normalization and export tests
cuda_core/cuda/core/_memoryview.pyx, cuda_core/tests/test_utils_dlpack.py
view_as_dlpack converts non-None stream handles before its checks and DLPack call. Tests verify that the CPU exporter receives stream=None for handles that convert to 0 or -1.

Suggested reviewers: rluo8

Priority: ➖ Normal

Change: Bug fix

Merge Risk: ⚪ Minimal · up to c72fa

The change addresses wrapped stream handles and adds coverage for both sentinel values across the construction paths. No specific remaining issue warrants delaying the merge.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@github-actions

Copy link
Copy Markdown
Contributor

@jpascucci-nv jpascucci-nv 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.

It took me a little while to figure out. if stream_ptr = int(CUstream(0)) ends up 0, it's going to trigger the else of line 1065, so it calls it with None. Ok.

@mdboom
mdboom enabled auto-merge (squash) October 19, 2026 19:26
@mdboom
mdboom merged commit 5797a44 into NVIDIA:main Oct 19, 2026
227 of 229 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working cuda.core Everything related to the cuda.core module P0 High priority - Must do!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants