Skip to content

Nemotron CUDA: a masked window replays a graph, captured on first use for each row count - #660

Merged
ashhart merged 1 commit into
ashhart:mainfrom
BobClawblaw:cuda-masked-graphs
Oct 12, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
BobClawblaw:cuda-masked-graphs

Conversation

@BobClawblaw

Copy link
Copy Markdown
Contributor

Follow-up to #615 and the 1.0.6 notes on #564/#596 ("a masked CUDA window runs without its captured graph").

What

A structured-output window on CUDA skipped the verify graphs and ran eagerly. Now a masked single-stream window
launches a graph of its row count and draw mode, captured the first time it runs on the bound sequence's buffers
(the own sequence included) and replayed after, the same path #615 gave other sequences, under its own ids
(4000 + mode × 17 + rows). A slide recapture's epoch drops them like any other sequence graph.

Why a graph can hold the mask

Every single-stream window's bits sit at one address: Cuda.putBits(0, …) writes slot 0 of the device-mapped
bits buffer, made once and kept to the end. The host rewrites the words before each launch, after the last
window's tokens were read, the same order the eager path relies on, so the graph reads them like the window's
ids. Engine.maskGraphs keeps the address the first masked window used; bits anywhere else run eagerly, as do
shared windows (verifyShared has no graphs on main either) and dumps.

Changes

  • cuda_engine.zig: Body.window carries a Mask; recordBody passes it to windowSegs (unmasked windows are
    unchanged: window() was windowSegs with no mask); launchGraph's capture-on-first-use half is
    launchCaptured; verifyMasked replays through it; deinit frees the own sequence's map.
  • cuda_lanes.zig: TF_GRAPH_LOG's line says "replays of graphs it captured for itself" (the own sequence now has some).
  • cuda_state.zig: the comment on Seq.graphs.

Checks (GB10, Nemotron 3.5 Lightning 30B-A3B MLX 4-bit d9d758fb, --parallel 4 --context 16384 --no-thinking)

16 structured cells (two JSON schemas, json_object, guided_regex, guided_choice, tool_choice: "required"
and a named function; greedy and sampled with a seed; each drafted and "draft": false) plus plain prose, with
libtfgrammar.so from the 1.0.7 archive. Main and this branch, two runs each, A B B A:

  • every reply's token_sha equals main's (32 cells × 4 runs, 0 mismatches); drafted equals "draft": false;
    a burst of the seven greedy structured cells at once equals their solo replies on both builds.
  • TF_GRAPH_LOG=1: main ran 10-127 eager windows per constrained request, this branch 0. The own sequence's first
    masked request captured 8 graphs (12 ms); later ones replay.
  • Decode rate (tokens_per_second, mean of two runs): under a grammar "draft": false +5.0% (12 cells, +1.3 to
    +6.3%), drafted +2.2% (−4.9 to +4.3%: replies of ~0.6 s pay their first captures, ~1.5 ms each); plain prose
    +0.2%.
  • zig build test and zig fmt --check pass on Linux aarch64.

… for each row count

A structured-output window ran eagerly: its grammar bits made it skip the verify
window graphs. Its bits live in one place for every single-stream window (the
lanes' device-mapped bits, slot 0), which the host rewrites before each launch,
so a graph can read them like any other input. A masked window now launches a
graph of its row count and draw mode, captured on the bound sequence's buffers
the first time it runs (the own sequence included), as ashhart#615 does for other
sequences, and captured again after a slide recapture. Bits in another place
than the first window's run eagerly, as do shared windows and dumps.

Nemotron 3.5 Lightning on GB10, --parallel 4, 16 structured cells (two JSON
schemas, json_object, regex, choice, required and named tool calls, greedy and
sampled) plus plain prose, main and this change run twice each (A B B A): every
reply's token_sha equals main's, drafted equals "draft": false, and a burst of
the seven greedy cells equals their solo replies. TF_GRAPH_LOG: 0 eager windows
where main had 10-127 a request. Decode under a grammar +5.0% with
"draft": false (12 cells, +1.3 to +6.3%), +2.2% drafted (short replies pay
their first captures, ~1.5 ms each); plain prose unchanged.
@ashhart
ashhart merged commit 05d27d0 into ashhart:main Oct 12, 2026
2 checks passed
@ashhart

ashhart commented Oct 12, 2026

Copy link
Copy Markdown
Owner

Thank you, @BobClawblaw. The argument in your description, that every single-stream window's bits sit at one address the host rewrites before each launch, holds up, and it closes the gap the 1.0.6 notes left open. Your commit 60cd6c921 landed in 1.0.8 under your name, so GitHub shows this PR as merged.

What we measured on a GB10, Nemotron 3.5 Lightning MLX 4-bit, your change against the same tree without it, both builds pricing windows from one window-cost table:

  • Same replies. 13 structured cases (four JSON schemas, json_object, guided_regex with a date and with anything for prose and for code, guided_choice, and four tool_choice cases), each greedy and seeded sampled, in four runs A B B A. All 104 drafted replies were valid and equal to their "draft": false replies, and every case gave the same token SHA in all four runs on both builds. The 17 release prompts gave the same tokens too.
  • Graphs. With TF_GRAPH_LOG=1, the old build ran 9,426 windows eagerly over 58 requests, up to 1,199 in one reply. Yours ran none eagerly: 22 captures the first time (42 to 53 ms in all), then about 9,430 replays.
  • Speed. Under a grammar, "draft": false decodes 4.4% faster (geometric mean of 26 cells, +3.0 to +5.4%, 94.2 to 98.4 tok/s). Drafted replies came out 1.0% faster, with cells from -9% to +9%. Half of those replies are under 50 tokens, so round-to-round variance and the first captures dominate them. Decode without a grammar is unchanged: x1.000 to x1.002 in the four prose and code cells.
  • A second stream. With --parallel 2, two structured requests at once with different grammars (a weather schema then a five-person team, json_object then a person schema) gave replies equal to each one alone and to "draft": false. The second request ran on its own sequence and replayed graphs it captured for itself, with no eager windows (the old build ran 291 eagerly there). A build that logged each masked window's bits address showed that both streams' single windows read slot 0, the one address, and that the second stream's shared-window rows sat at their own offsets and ran eagerly as before. Since a window's tokens are read before the next window's bits are written, a stream never sees another stream's bits.

One change on our side, in a commit after yours: cuda_engine.zig went past our 600-line limit with this change, so the engine's prompt pass (prefill through segmentSet) moved unchanged into cuda_prompt.zig. Your diff applies as you wrote it.

BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
cuda_engine.zig reached 608 lines with ashhart#660. Its prompt pass (prefill, prefillWith, prefillFrom, redrawFirst, beforeChunk, serialChunks, setSegments, promptHost, segmentSet) moves unchanged but for indent into cuda_prompt.zig, and Engine keeps each under its old name, so no caller changes. The two pinned offsets and two lesson helpers it reads become public, and the grid import goes with it. Reversing the move rebuilds the old file byte for byte. cuda_engine.zig is 516 lines, cuda_prompt.zig 121.
BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
…e served host (ashhart#659) and Nemotron's masked CUDA windows replaying graphs (ashhart#660)
@ashhart ashhart mentioned this pull request Oct 14, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
cuda_engine.zig reached 608 lines with ashhart#660. Its prompt pass (prefill, prefillWith, prefillFrom, redrawFirst, beforeChunk, serialChunks, setSegments, promptHost, segmentSet) moves unchanged but for indent into cuda_prompt.zig, and Engine keeps each under its old name, so no caller changes. The two pinned offsets and two lesson helpers it reads become public, and the grid import goes with it. Reversing the move rebuilds the old file byte for byte. cuda_engine.zig is 516 lines, cuda_prompt.zig 121.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
ashhart#660 landed above as 3fa4ea873 (the author's commit, rebased onto this candidate), with our pure move 928cd7473 after it. This keeps the PR's own head commit in the history, with no change to the tree.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…e served host (ashhart#659) and Nemotron's masked CUDA windows replaying graphs (ashhart#660)
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.

2 participants