Skip to content

CUDA runtime: Context.shared and Stream.borrowed, for a library inside a host's process - #946

Closed
drowzeys wants to merge 1 commit into
ashhart:mainfrom
drowzeys:upstream/cuda-context-hook
Closed

drowzeys wants to merge 1 commit into
ashhart:mainfrom
drowzeys:upstream/cuda-context-hook

Conversation

@drowzeys

Copy link
Copy Markdown
Contributor

Thank you, Ash. This is the small hook you suggested in #585: a stable way to share the CUDA context.

What now works: a library built on TensorFold's CUDA runtime can run inside a host framework's process, using the host's own context and stream. It needs no hand-built structs and no hard-coded device. The H3 video library (separate pull request) runs this way inside ComfyUI today. With this hook, it and any later library can depend on the runtime cleanly.

The proof, on DGX Spark (GB10), driver 580.173.02, CUDA 13.0.88, branch ef23b593 on main 46a08c18:

  • tf-cuda-test shared-context passes on the GPU. With a primary context current, as PyTorch leaves it:
    • the shared context is the host's (same handle, same device);
    • a fill on the borrowed legacy stream lands;
    • the host's context stays alive after the library releases its retain.
  • With no context current, it falls back to device 0's primary context.
  • Nothing existing changes: tf-cuda-test smoke passes as on main.
  • Lean check: 517 on main, 517 here.
  • Host suites: zig build test 84/84 steps (1376 passed, 7 skipped); zig build test-cuda-host 39/39 steps, 852/852.

What it adds

Five files: abi.zig +1, context.zig +17, stream.zig +5, and the test (main.zig +2, runtime_tests.zig +28).

  • Context.shared(driver): retains the primary context of the device that the calling thread's current context is on, or device 0's if none is current. It returns error.Invalid when the host's context is not a primary one.
  • Stream.borrowed(driver, handle): a stream the caller owns. null means the legacy default stream. The runtime never destroys it.
  • cuCtxGetDevice: added to the driver table.

Next steps

  • A test for the error.Invalid path. It needs a non-primary context, and the driver table has no cuCtxCreate to make one.
  • Multi-GPU hosts. These are untested; only one GB10 was available.

The code is Apache-2.0.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PfjmufXm4L7qsVNFyRw9ZK

@cwschroeder cwschroeder 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.

Scoped API/source review of ef23b593 on main 46a08c18: I found no new material issue. The current-device primary retain is balanced on foreign-context refusal, with no host-context switch there. Stream.borrowed explicitly requires callers not to deinit the borrowed stream.

The requested non-primary refusal branch can also be covered without adding cuCtxCreate to the driver table. A test-only patch with mutation controls, candidate 05486a33, adds five permanent cases to the standard CUDA test root: current primary on device 3 without a device-0 lookup or SetCurrent; foreign-context refusal with one retain/release of the same device and preserved host handle; fallback to ordinal 0; retain cleanup after an injected fallback switch error; null/non-null borrowed-handle forwarding with one synchronization call each.

The actual entry points run over CPU driver callbacks. All five focused cases pass in Debug and Fast; six separately mutated source copies fail at runtime. The full CUDA host target passes all 39 steps locally, with one JPEG-related skip in the incremental run; whole-tree format, whitespace and lean pass (517/517, zero new). Source review found no issue in this test delta. Fork validation #16 has all six checks green on exact candidate 05486a33, including the complete No GPU workflow (run 38928329054). These results remain pinned to the candidate based on 46a08c18; they do not qualify the rebased upstream head f3f959f1. The original No GPU run 38908400000 is action_required with zero jobs.

The virtual device-3 and foreign-context cases add host-logic coverage. They do not replace real non-primary-context or multi-GPU device checks, or extend your single-GB10 hardware receipt. The injected NotReady code tests unwind behavior rather than a claimed real cuCtxSetCurrent error combination.

@ashhart ashhart mentioned this pull request Oct 17, 2026
…e a host's process

A library that runs kernels for a host framework (PyTorch, say) has to work in the host's context and on its
stream. Context.shared retains the primary context of the device the calling thread's current context is on
(device 0's when none is current) and refuses a context that is not a primary one; Stream.borrowed wraps a stream
the caller owns, null being the legacy default stream. Nothing that exists changes; cuCtxGetDevice joins the
driver table. tf-cuda-test shared-context checks both on a GPU.
@drowzeys
drowzeys force-pushed the upstream/cuda-context-hook branch from ef23b59 to f3f959f Compare October 17, 2026 01:30
@drowzeys

Copy link
Copy Markdown
Contributor Author

Rebased on main 6ee828c2; head ef23b593 is now f3f959f1. The pull request's diff against main is patch-identical to before (main's new commits only touch docs); zig fmt --check clean on the changed Zig files; python3 tools/zig/lean_check.py reports no new findings against main.

drowzeys added a commit to drowzeys/TensorFold that referenced this pull request Oct 17, 2026
drowzeys added a commit to drowzeys/TensorFold that referenced this pull request Oct 17, 2026
Model.load always retained device 0's primary context, though the library
runs inside a host process that may have made another device current. It now
takes Context.shared (ashhart#946): the current context's device, and device 0 only
when the host has none yet; a current context that is not its device's
primary context is refused. The legacy default stream is taken as
Stream.borrowed.
ashhart added a commit that referenced this pull request Oct 18, 2026
…at runs kernels inside a host framework's process
ashhart added a commit that referenced this pull request Oct 18, 2026
…ollow-ups named as ours

After review of the fourteen lines that overstated: #710's fresh-quantization gain is one measured case; #902's stop
and group cleanup hold where the watch lines are open and through the shared helper; #887 says its chunks change the
attention bits below 8,192, on M5 too, with no larger error against float64; #899 records the ring start and the
wider refusal; #802 keeps the author's B70 receipts apart from our host-checked lifetime fixes; #836's equalities are
the GB10's and its Windows build the author's; #888 says a failed restore's pinned memory is kept, never freed; #734's
tree rounds are checked against the host's, not float64; the Living Weights lines (#876, #877, #878, #891) say which
runs their numbers come from, the ten-fact costs, and the M3 Ultra's four-fact result; #946 and #910 read plainer.
@ashhart

ashhart commented Oct 18, 2026

Copy link
Copy Markdown
Owner

Thank you, @drowzeys, for sharing a host framework's CUDA context and stream through explicit runtime hooks.

We merged your work for 1.1.0 in 079f476. Fake-driver controls cover a current primary context, foreign-context refusal, retained-context cleanup, fallback, make-current failure and borrowed-stream identity.

The host still owns the borrowed stream. Deinitializing the retained context synchronizes the current context, including the host's work.

Host current-context/borrowed-stream controls passed; a final real-framework device integration run is not recorded.

Posted by Ash's AI Assistant Synthena

@ashhart

ashhart commented Oct 18, 2026

Copy link
Copy Markdown
Owner

Shipped in 1.1.0 as 079f476e9; thanks @drowzeys. We're closing it as landed.

Posted by Ash's AI Assistant Synthena

@ashhart ashhart closed this Oct 18, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…rary that runs kernels inside a host framework's process
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…ollow-ups named as ours

After review of the fourteen lines that overstated: ashhart#710's fresh-quantization gain is one measured case; ashhart#902's stop
and group cleanup hold where the watch lines are open and through the shared helper; ashhart#887 says its chunks change the
attention bits below 8,192, on M5 too, with no larger error against float64; ashhart#899 records the ring start and the
wider refusal; ashhart#802 keeps the author's B70 receipts apart from our host-checked lifetime fixes; ashhart#836's equalities are
the GB10's and its Windows build the author's; ashhart#888 says a failed restore's pinned memory is kept, never freed; ashhart#734's
tree rounds are checked against the host's, not float64; the Living Weights lines (ashhart#876, ashhart#877, ashhart#878, ashhart#891) say which
runs their numbers come from, the ten-fact costs, and the M3 Ultra's four-fact result; ashhart#946 and ashhart#910 read plainer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants