From 2a33c033dcf08fd993ae7bd10101c3020430ce16 Mon Sep 17 00:00:00 2001 From: Vyron Vasileiadis Date: Tue, 27 Oct 2026 13:34:49 +0200 Subject: [PATCH 1/2] cuda.core: treat the NULL stream as a default-stream token Stream_is_default_token matched only CU_STREAM_LEGACY and CU_STREAM_PER_THREAD, so a Stream over handle 0, such as Stream.from_handle(0), still cached the first context and device it resolved. The driver resolves handle 0 against the current context like the other two, so after a context switch .context, .device, .resources and record() kept naming the old context. Closes #3242 Signed-off-by: Vyron Vasileiadis --- cuda_core/cuda/core/_stream.pyx | 5 +++-- cuda_core/docs/source/release/1.3.0-notes.rst | 7 +++++++ cuda_core/tests/test_green_context.py | 12 ++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/cuda_core/cuda/core/_stream.pyx b/cuda_core/cuda/core/_stream.pyx index 1876885e0cfb..f2bdf67c1385 100644 --- a/cuda_core/cuda/core/_stream.pyx +++ b/cuda_core/cuda/core/_stream.pyx @@ -522,13 +522,14 @@ cdef inline bint Stream_handle_is_default_token(cydriver.CUstream s) noexcept no These tokens carry no context of their own; they refer to whatever context is current, so nothing resolved from one may be cached on the object. + The NULL stream (handle 0) is such a token too. """ cdef uintptr_t h = s - return h == cydriver.CU_STREAM_LEGACY or h == cydriver.CU_STREAM_PER_THREAD + return h == 0 or h == cydriver.CU_STREAM_LEGACY or h == cydriver.CU_STREAM_PER_THREAD cdef inline bint Stream_is_default_token(Stream self) noexcept nogil: - """Return True when this stream is CU_STREAM_LEGACY or CU_STREAM_PER_THREAD.""" + """Return True when this stream is the NULL stream, CU_STREAM_LEGACY or CU_STREAM_PER_THREAD.""" return Stream_handle_is_default_token(as_cu(self._h_stream)) diff --git a/cuda_core/docs/source/release/1.3.0-notes.rst b/cuda_core/docs/source/release/1.3.0-notes.rst index f63ce1f1cff3..c092e159bac7 100644 --- a/cuda_core/docs/source/release/1.3.0-notes.rst +++ b/cuda_core/docs/source/release/1.3.0-notes.rst @@ -321,6 +321,13 @@ Fixes and enhancements ``repr`` now shows ``U`` for unified operands. (`#2649 `__) +- A :class:`Stream` over the NULL stream (handle 0), such as + ``Stream.from_handle(0)``, no longer caches the first context and device it + observes. Like ``LEGACY_DEFAULT_STREAM`` and ``PER_THREAD_DEFAULT_STREAM`` + since 1.2.0, it now resolves ``Stream.context``, ``Stream.device``, + ``Stream.resources`` and ``Stream.record()`` against the current context. + (`#3242 `__) + Deprecations ------------ diff --git a/cuda_core/tests/test_green_context.py b/cuda_core/tests/test_green_context.py index a51e8e0bc2b3..af2794721eb8 100644 --- a/cuda_core/tests/test_green_context.py +++ b/cuda_core/tests/test_green_context.py @@ -15,6 +15,7 @@ ProgramOptions, SMResource, SMResourceOptions, + Stream, WorkqueueResource, WorkqueueResourceOptions, launch, @@ -560,6 +561,17 @@ def test_stream_and_event_track_green_context(self, green_ctx): stream.sync() event.sync() + @pytest.mark.agent_authored(model="claude-opus-5-5") + def test_null_stream_follows_current_context(self, init_cuda, green_ctx): + stream = Stream.from_handle(0) + assert stream.context == init_cuda.context + + with use_context(init_cuda, green_ctx): + assert stream.context == green_ctx + assert stream.record().context == green_ctx + + assert stream.context == init_cuda.context + @pytest.mark.agent_authored(model="gpt-5.6") def test_device_receiver_targets_stored_green_context(self, init_cuda, green_ctx): primary_ctx = init_cuda.context From 99a4272143fef65466c2d0328776768f6643e17a Mon Sep 17 00:00:00 2001 From: Vyron Vasileiadis Date: Mon, 30 Nov 2026 16:43:03 +0200 Subject: [PATCH 2/2] cuda.core: document the NULL stream as a default-stream token The notes on Stream.context, .device, .resources and record() now list the NULL stream next to LEGACY_DEFAULT_STREAM and PER_THREAD_DEFAULT_STREAM. The release note moves to 1.4.0, since 1.3.0 has been released. test_null_stream_follows_current_context now skips when CUDA_PYTHON_CUDA_PER_THREAD_DEFAULT_STREAM is set. Handle 0 is then the per-thread default stream, which the driver rejects with CUDA_ERROR_INVALID_HANDLE under a green context. test_default_stream_follows_current_context also covers Stream.from_handle(0), which checks .device and .resources across devices. Signed-off-by: Vyron Vasileiadis --- cuda_core/cuda/core/_stream.pyx | 16 ++++++++-------- cuda_core/docs/source/release/1.4.0-notes.rst | 7 +++++++ cuda_core/tests/test_green_context.py | 6 ++++++ cuda_core/tests/test_stream.py | 5 ++++- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/cuda_core/cuda/core/_stream.pyx b/cuda_core/cuda/core/_stream.pyx index 4f40cdb9edac..50a651541cc1 100644 --- a/cuda_core/cuda/core/_stream.pyx +++ b/cuda_core/cuda/core/_stream.pyx @@ -294,8 +294,8 @@ cdef class Stream: Note ---- - A default stream (:obj:`LEGACY_DEFAULT_STREAM` or - :obj:`PER_THREAD_DEFAULT_STREAM`) carries no context of its own; it + A default stream (:obj:`LEGACY_DEFAULT_STREAM`, :obj:`PER_THREAD_DEFAULT_STREAM` + or the NULL stream, handle 0) carries no context of its own; it refers to whichever context is current, so a newly created event is associated with the current context at call time. @@ -385,8 +385,8 @@ cdef class Stream: Note ---- - A default stream (:obj:`LEGACY_DEFAULT_STREAM` or - :obj:`PER_THREAD_DEFAULT_STREAM`) carries no context of its own; it + A default stream (:obj:`LEGACY_DEFAULT_STREAM`, :obj:`PER_THREAD_DEFAULT_STREAM` + or the NULL stream, handle 0) carries no context of its own; it refers to whichever context is current, so this returns the device for the current context at call time. @@ -408,8 +408,8 @@ cdef class Stream: Note ---- - A default stream (:obj:`LEGACY_DEFAULT_STREAM` or - :obj:`PER_THREAD_DEFAULT_STREAM`) carries no context of its own; it + A default stream (:obj:`LEGACY_DEFAULT_STREAM`, :obj:`PER_THREAD_DEFAULT_STREAM` + or the NULL stream, handle 0) carries no context of its own; it refers to whichever context is current, so this returns the current context at call time. @@ -430,8 +430,8 @@ cdef class Stream: Note ---- - A default stream (:obj:`LEGACY_DEFAULT_STREAM` or - :obj:`PER_THREAD_DEFAULT_STREAM`) carries no context of its own; it + A default stream (:obj:`LEGACY_DEFAULT_STREAM`, :obj:`PER_THREAD_DEFAULT_STREAM` + or the NULL stream, handle 0) carries no context of its own; it refers to whichever context is current, so this queries the current context at call time. diff --git a/cuda_core/docs/source/release/1.4.0-notes.rst b/cuda_core/docs/source/release/1.4.0-notes.rst index 1be489a39591..e86d53bc92ae 100644 --- a/cuda_core/docs/source/release/1.4.0-notes.rst +++ b/cuda_core/docs/source/release/1.4.0-notes.rst @@ -59,3 +59,10 @@ Fixes and enhancements later driver; with a CUDA 12 build or an older driver, imports behave as before. (`#3502 `__) + +- A :class:`Stream` over the NULL stream (handle 0), such as + ``Stream.from_handle(0)``, no longer caches the first context and device it + observes. Like ``LEGACY_DEFAULT_STREAM`` and ``PER_THREAD_DEFAULT_STREAM`` + since 1.2.0, it now resolves ``Stream.context``, ``Stream.device``, + ``Stream.resources`` and ``Stream.record()`` against the current context. + (`#3242 `__) diff --git a/cuda_core/tests/test_green_context.py b/cuda_core/tests/test_green_context.py index 63eaef0eb61b..66c28b20c1b3 100644 --- a/cuda_core/tests/test_green_context.py +++ b/cuda_core/tests/test_green_context.py @@ -2,6 +2,8 @@ # # SPDX-License-Identifier: Apache-2.0 +import os + import numpy as np import pytest from helpers.contexts import assert_device_operations_use_bound_context, current_context_handle, use_context @@ -579,6 +581,10 @@ def test_stream_and_event_track_green_context(self, green_ctx): stream.sync() event.sync() + @pytest.mark.skipif( + bool(int(os.environ.get("CUDA_PYTHON_CUDA_PER_THREAD_DEFAULT_STREAM", "0"))), + reason="handle 0 is the per-thread default stream, which green contexts reject", + ) @pytest.mark.agent_authored(model="claude-opus-5-5") def test_null_stream_follows_current_context(self, init_cuda, green_ctx): stream = Stream.from_handle(0) diff --git a/cuda_core/tests/test_stream.py b/cuda_core/tests/test_stream.py index bb98108bf8f5..b1743b4e1c29 100644 --- a/cuda_core/tests/test_stream.py +++ b/cuda_core/tests/test_stream.py @@ -547,7 +547,10 @@ def _skip_unless_multi_gpu(): @pytest.mark.agent_authored(model="claude-opus-5") -@pytest.mark.parametrize("stream", [LEGACY_DEFAULT_STREAM, PER_THREAD_DEFAULT_STREAM]) +@pytest.mark.parametrize( + "stream", + [LEGACY_DEFAULT_STREAM, PER_THREAD_DEFAULT_STREAM, pytest.param(Stream.from_handle(0), id="null")], +) def test_default_stream_follows_current_context(stream): """A default-stream token denotes whatever context is current, so its queries follow a context switch instead of reporting the first context