Skip to content

cuda.core: treat the NULL stream as a default-stream token - #3243

Open
fedonman wants to merge 1 commit into
NVIDIA:mainfrom
fedonman:core-null-stream-default-token
Open

fedonman wants to merge 1 commit into
NVIDIA:mainfrom
fedonman:core-null-stream-default-token

Conversation

@fedonman

Copy link
Copy Markdown
Contributor

Stream_handle_is_default_token now also matches handle 0, so a Stream over the NULL stream resolves its context and device on every call, as LEGACY_DEFAULT_STREAM and PER_THREAD_DEFAULT_STREAM do since #2485. The synchronous and virtual memory resources, which check the same predicate before recording a deallocation stream, now handle a handle-0 stream like the other two tokens as well. The test switches into a green context so it runs on a single GPU, and there is a 1.3.0 release note.

Closes #3242

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 NVIDIA#3242

Signed-off-by: Vyron Vasileiadis <hi@fedonman.com>
@copy-pr-bot

copy-pr-bot Bot commented Oct 27, 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 27, 2026
@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Streams wrapping the NULL handle now resolve their context, device, resources, and recorded events using the current context, consistent with other default streams.
  • Tests
    • Added coverage verifying that a NULL-handle stream follows context changes.

Handle 0 is now recognized as a default-stream token. NULL-handle streams resolve context-dependent properties and record operations against the current context. A green-context test checks context and event resolution, and the release notes describe the behavior.

Changes: NULL-stream context resolution

Layer / File(s) Summary
Default-token resolution and validation
cuda_core/cuda/core/_stream.pyx, cuda_core/tests/test_green_context.py, cuda_core/docs/source/release/1.3.0-notes.rst
The default-token check now includes handle 0. The test checks context resolution before, during, and after a green-context scope, and checks the recorded event's context. The release notes describe current-context resolution for NULL-stream operations.

Assessment against linked issues

Objective Addressed Explanation
NULL-handle streams resolve context-dependent properties and recorded events against the current context [#3242] ✅

Possibly related issues

Possibly related PRs

  • NVIDIA/cuda-python#3034 — This PR adds a NULL-stream release note that may need to move to the generated release-note mechanism described in #3034.

Suggested labels: bug, cuda.core

Suggested reviewers: andy-jost

Priority: Normal

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Low · up to 2a33c

The change appears functional, but its regression test should cover all documented NULL-stream entry points.

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.

Nitpick comments: 1
cuda_core/tests/test_green_context.py: 1

565-573: Trivial · Maintainability & Code Quality · Quick win

suggestion: Add NULL-stream coverage for device, resources, and foreign wrappers.

The added test covers context and record() for Stream.from_handle(0) only. The existing default-stream test covers LEGACY_DEFAULT_STREAM and PER_THREAD_DEFAULT_STREAM, but not handle 0. Add device and resources assertions for the NULL stream, and create a foreign object whose __cuda_stream__ returns (0, 0). A handle-0 regression in either path can pass the current tests.


Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 590a8bf and 2a33c03.

Files selected for processing: 3
  • cuda_core/cuda/core/_stream.pyx
  • cuda_core/docs/source/release/1.3.0-notes.rst
  • cuda_core/tests/test_green_context.py

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

@leofang
leofang self-requested a review October 27, 2026 14:37
@leofang

leofang commented Oct 27, 2026

Copy link
Copy Markdown
Member

Stream_handle_is_default_token now also matches handle 0, so a Stream over the NULL stream resolves its context and device on every call, as LEGACY_DEFAULT_STREAM and PER_THREAD_DEFAULT_STREAM do since #2485.

FYI just a note, this is true for the "re-resolve, don't cache" property this PR cares about. It is not true semantically under a green ctx: PTDS with a green ctx current is a hard CUDA_ERROR_INVALID_HANDLE ("CUDA Green Contexts cannot be used with stream-per-thread mode"), while handle 0 and CU_STREAM_LEGACY resolve to the green ctx's own null stream, which is a distinct stream object from the primary ctx's null stream (and only exists if the green ctx was created with CU_GREEN_CTX_DEFAULT_STREAM). Its default sync semantics are also different — PTDS-like non-blocking rather than the legacy sync-with-all, blocking behavior of the primary ctx's NULL stream.

@leofang

leofang commented Oct 27, 2026

Copy link
Copy Markdown
Member

@fedonman could you resolve conflicts?

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.

[BUG]: Stream.from_handle(0) keeps the first context it sees

2 participants