Repository navigation
Coerce stream_ptr to int in view_as_dlpack - #3154
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Summary
Priority: ➖ Normal Change: Bug fix Merge Risk: ⚪ Minimal · up to
|
|
jpascucci-nv
left a comment
There was a problem hiding this comment.
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.
Coerce
stream_ptrtointinview_as_dlpackFixes 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, thestream_ptr: int | Noneannotations onStridedMemoryViewmade Cython convertany int-convertible object to a plain
inton entry. numba-cuda passes itsCUstream(0)wrapper, which is truthy and converts to0. Without theannotation typing, the wrapper reached
view_as_dlpackunchanged, andint(stream_ptr) if stream_ptr else Nonetook the truthy branch. The resultwas
numpy_array.__dlpack__(stream=0)instead ofstream=None, and NumPyraised
RuntimeError: NumPy only supports stream=None.The
stream_ptr == -1opt-out check had the same problem: a wrapper around-1was never recognized and was forwarded asstream=-1.Fix
view_as_dlpacknow convertsstream_ptrwithint()once, up front, so everylater check sees an
int. This matches whatview_as_caialready does. Thefix 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 | Noneannotated parameter incuda_corethat #2902 un-typed.
stream_ptrwas the only place that makes a decision onthe 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_ptrincuda_core/tests/test_utils_dlpack.pyneeds neither numba nor a GPU, so itruns in the regular (non-nightly) suite.
__bool__, so itis always truthy, like
CUstream.streamargument itreceives in
__dlpack__. NumPy then enforcesstream=Noneas in thenightly failure.
from_dlpack,from_any_interfaceand thedeprecated constructor, and over wrapper values
0and-1. It asserts thatthe exporter received
stream=Noneand that a view was produced.All six cases fail without the fix and pass with it.
🤖 Generated with Claude Code