Skip to content

Nemotron CUDA: prefill each reply in the background for the next turn - #723

Merged
ashhart merged 5 commits into
ashhart:mainfrom
plotarmordev:perf/nemotron-warm-turns
Oct 13, 2026
Merged

ashhart merged 5 commits into
ashhart:mainfrom
plotarmordev:perf/nemotron-warm-turns

Conversation

@plotarmordev

@plotarmordev plotarmordev commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

On top of #722 (its three commits come first here; this pull request adds the last one). Nemotron on CUDA takes warm turns, as the Qwen3.8-27B does: after a reply, the conversation, the reply and the next turn's opening are prefilled in a background pass, so the next turn resumes past the reply and prefills only its new message.

  • native/cuda.zig: the Nemotron store's rules are warm, and the host reports warm_turns.
  • core/prompt_cache.zig: a warm pass (its history the whole prompt) of a family keyed past its state keeps one row before its end, however near it resumed; core/lane_host.zig keeps a warm pass's end itself only for a family that keys no lookahead (the 27B, unchanged). A test expectation covers the new mark.
  • cuda_lanes.zig: the prompt's cancel check also asks Stream.yields(), so a background pass steps aside at its next chunk when a request arrives.

Trade-off: a message sent the instant its reply lands waits behind that reply's background pass, which has already begun: about 30 ms later than without this change. From a 0.15 s gap on, the next turn starts about twice as fast (below).

Receipt

  • Environment: as Nemotron CUDA: keep prompt states where messages end, staged inside a chunk without an extra pass #721 (two DGX Sparks, GB10, driver 580.173.02, Nemotron 3.5 Lightning MLX-4bit @ d9d758fb, on e7c523eb), --context 16384 --parallel 4.

  • Exactness: six conversations of six turns over 5k to 10k-token documents with 200-token replies: all 36 replies equal the cache-off server's on both Sparks, and equal across every gap below, two sessions each.

  • Time to first token of the later turns (the server's time_to_first_token, queue wait included; medians per turn, 30 turns a session):

    gap before the next message Nemotron CUDA: kept prompt states hold each attention layer's first rows only #722 this change
    1.5 s (Spark 1 / Spark 2) 101-113 ms 48-53 ms -48% to -57%, separated, both
    0.3 s 101-112 ms 49-51 ms -50% to -55%, separated
    0.15 s 102-112 ms 49-54 ms -47% to -55%, separated
    0.05 s 102-112 ms 82-96 ms -13% to -19%
    0 s (Spark 1 / Spark 2) 102-114 ms 131-148 ms +28% to +33%

    I also tried letting a background pass wait 25 ms before it starts so an instant message goes first; it was worse (a skipped background pass leaves the next turn resuming further back), so it is not here.

  • Decode speed: unchanged.

  • Tests run: zig build test 39/39 steps, 451/451 tests; zig build test-cuda-host 24/24 steps; the server tests 127/127 and 104/104 (zig/tests/server/zig_test.sh); the frozen server goldens 309 of 319 equal, 0 unexpected differences, as on main; zig fmt --check clean on the changed files; lean_check.py 534 problems, as on main. GPU: the conversations above; no new GPU test.

  • Not run, and why: Metal (its stores are not warm; the core change only touches warm stores with a lookahead). The NVFP4 checkpoint, RTX cards (no hardware here).

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.
  • No precision traded for speed. If the bits change, the description says which sums change and why. (No bits change.)
  • Prompt processing is no slower than the last release. (A prompt's own pass is unchanged, but a message sent the instant its reply lands starts about 30 ms later; see the trade-off above.)
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not. (CUDA GB10 only.)
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

…messages end

A prompt pass cuts a chunk at each keep, since a row's bits do not depend on where its chunk is cut, so the prompt
cache keeps states at the history's end instead of the 2,048-row grid point below it. tf-cuda-test prompt-cuts checks
a pass cut on and off the grid, and passes resumed from each cut, against one uncut pass.
…the chunk there

The Mamba scan runs to the keep, its state and the conv rows a cut would leave are copied aside, and the scan runs on
with the same bits; the keep's saved state takes those planes in place of the live ones. A chunk with several keeps
still ends at the first. A conversation's later turn now prefills in one pass.
…ws only

A state kept after at tokens copies each layer's first at KV rows instead of the whole window's, and the cache counts
it at that size; the rows past it, which no pass reads before writing them, stay as they are on restore. prompt-cuts
fills those rows before each restore.
The Nemotron CUDA store is warm and its host takes warm turns: after a reply, the conversation, the reply and the next
turn's opening are prefilled in a background pass that steps aside at a chunk boundary for a foreground request. A warm
pass of a family keyed past its state keeps one row before its end, however near it resumed, through its marks.
ashhart#721 and ashhart#722 were superseded by 1.0.8 (ashhart#670: states keep and resume at any row, kept states hold the
prompt's rows); this merge takes 1.0.8's prompt states and keeps only this PR's change on top.
@plotarmordev

Copy link
Copy Markdown
Contributor Author

Brought up to date with 1.0.8 (0efbd447) by a merge, with no force-push (head e7adba5b). #721 and #722, which this PR was stacked on, are closed as superseded by 1.0.8's prompt states (#670). This merge takes 1.0.8's prompt states as they are and keeps only this PR's own change on top. That change is 19 lines in lane_host.zig, prompt_cache.zig (and its test), cuda_lanes.zig and native/cuda.zig. The store keeps 1.0.8's min_prompt = 0 and its spare budget.

Re-checked at this head on a GB10, against a 1.0.8 build, in two sessions:

  • Later turns of 5k- to 10k-token conversations with 200-token replies, 1.5 s between turns: first token in a median 48.6 to 50.5 ms against 100.7 to 110.3 ms on 1.0.8, every turn separated.
  • All 36 replies in each of the two sessions are identical to 1.0.8's, logprob fingerprints included.
  • tf-cuda-test resume: 8 marks, each resumed equal to the fresh pass.
  • zig build test 616/621 (5 skipped), server tests 155/155 and 96/96, goldens 309/319 with 0 unexpected differences. All as on 1.0.8.

Not re-run on 1.0.8: the 0.05 s gap case and the ~30 ms cost when a message arrives the instant a reply lands. The CHANGELOG entry gives those from the 1.0.7 receipt.

@ashhart
ashhart merged commit 1b1b288 into ashhart:main Oct 13, 2026
2 checks passed
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 13, 2026
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 13, 2026
The contributor's commit landed above on 1.0.8's prompt states. The head also holds ashhart#721's and ashhart#722's commits, whose changes 1.0.8 had already made in its own form and whose tests landed above; this keeps the head in the history with no change to the tree.
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 13, 2026
@ashhart

ashhart commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Thank you, @plotarmordev. Prefilling the reply in the background is what makes a conversation's next turn cheap on CUDA, as it already is on the Macs. It landed in 1.0.9 under your name (c6a64a34f), carried by us onto 1.0.8's in-chunk keeps and sized states. Your current head, a merge of 1.0.8, is merged into the history with no change to the tree (1b1b288b5), so GitHub shows this PR as merged.

What we measured on a GB10 (sm_121):

  • Exact. Two six-step chats (three turns, then the last turn regenerated, edited and deleted) gave the same replies as the build without it and as a server with the prompt cache off, 12 of 12 on MLX 4-bit and 12 of 12 on NVFP4. Four streams at once equal each one alone, greedy and sampled, on both.
  • Reuse. As landed, a second turn sent the moment the first reply ended resumed 7,366 of its 7,389 prompt tokens in those chats, where the build without it resumed 7,228: the reply was already prefilled.

Before the release we changed when the pass runs (c724dd9d2). In our release gate, an unrelated ~1,000-token first turn sent the moment another reply ended waited about 40 ms for the pass's first chunk, because on a MoE even a 20-row chunk reads most of the experts' weights. The pass now starts only after the engine has had no other work for 150 ms, so such a request goes first and the pass is dropped before it reaches the GPU; a pass that is running steps aside after at most 256 rows and keeps the state it reached; and each turn keeps its own history state again, so back-to-back and regenerated turns resume as they did in 1.0.8. With a second between turns, later turns resume past each reply and start 2.0 to 2.2 times sooner than in 1.0.8. In the release gate on the final build (GB10, MLX 4-bit, each turn sent the moment the last reply ended), all 41 chat turns gave one reply across 1.0.9, 1.0.8 and the build before the change, drafted equal to "draft": false, and each turn resumed as many tokens as in 1.0.8; an unrelated ~1,034-token first turn sent right after another reply started in 0.204 s, against 0.203 s on 1.0.8 and 0.243 s before the change.

The batch waited a day on a speed check of ours, and the cause was ours too. After the merge, a served A/B read prose greedy 1.5% slower. Both builds make the same draft decisions on the same window-cost table, so the difference came from the table the draft-depth rule reads, and our calibration had timed its windows over the MTP head's own drafts, which loop and read too cheap. That is fixed in our commit after yours (0e081cfee); with it, every decode cell is within noise of the build without it on a shared table, and the one cell that differs on each build's own table moves with the table, not the build.

@ashhart ashhart mentioned this pull request Oct 14, 2026
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
The contributor's commit landed above on 1.0.8's prompt states. The head also holds ashhart#721's and ashhart#722's commits, whose changes 1.0.8 had already made in its own form and whose tests landed above; this keeps the head in the history with no change to the tree.
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
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