Skip to content

Nemotron CUDA: keep prompt states where messages end, staged inside a chunk without an extra pass - #721

Closed
plotarmordev wants to merge 2 commits into
ashhart:mainfrom
plotarmordev:perf/nemotron-staged-keep
Closed

plotarmordev wants to merge 2 commits into
ashhart:mainfrom
plotarmordev:perf/nemotron-staged-keep

Conversation

@plotarmordev

Copy link
Copy Markdown
Contributor

What this changes

Nemotron on CUDA keeps a conversation's prompt state where its messages end, and keeps it without an extra prompt pass. Two commits:

  1. Keeps and resumes off the 2,048-row grid (the change CUDA Nemotron: the prompt path is chunk-invariant, so the cache's 2,048 grid could go #670 proposes, thanks to @jschmied): a prompt pass ends a chunk at each keep; the Nemotron CUDA store's rules lose planned/grid (.{ .lookahead = 1 }); cuda_prompt_grid.zig takes the keeps (end, lastStart, resumable at any row inside the prompt); the lanes read the last chunk's hidden row from lastStart. New tf-cuda-test prompt-cuts MODEL IDS_FILE: a 9,000-token pass keeping at 1, 777, 2047, 2048, 2049, n-33 and n-1, and passes resumed from each kept state, against one uncut pass (caches and states, the head's caches, the last logits and hidden row).
  2. A keep inside a chunk is staged instead of ending the chunk there. The Mamba scan runs to the keep, its state and the three conv rows a cut would leave are copied to an engine stage buffer, and the scan runs on: the scan is sequential per row, so the bits are the same as one scan. The keep's saved state takes those two planes in place of the live ones (Buffers.snapshotFrom); the chunk's KV rows past the keep ride along and are written again before any pass reads them. A chunk holding several keeps still ends at the first. A later turn, which keeps where its messages end a few tokens before the prompt's end, now prefills in one pass instead of two.

Receipt

  • Environment: two DGX Sparks (GB10, sm_121, driver 580.173.02, CUDA 13), TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit @ d9d758fb, on e7c523eb, zig build native -Dnvcc=… -Dsm=121, built-in kernels, --context 16384 --parallel 4.

  • Exactness:

    • Chunk invariance first, with a local switch that cut one extra chunk (cache off): at rows 1000, 2047, 2049, 3001, 5000 and 6143, or 1, 7, 33, 500 and 1999 rows before the end, on ~1k, ~5k and ~9k-token prompts, drafted and "draft": false: all 11 positions equal the uncut run (token_sha and the 20-alternative logprob rows).
    • prompt-cuts passes after each commit (the second exercises cut and staged keeps). It fails, as it should, when a local change makes the scan's dt clamp depend on the chunk's rows.
    • Six conversations of six turns over 5k to 10k-token documents, two of them concurrent: all 36 replies (token_sha and logprob rows) equal the cache-off server's (--prompt-cache-gib 0, e7c523eb) after each commit, on both Sparks, two sessions each.
  • Prompt speed, the same conversations (each later turn's prefill_seconds, median of 60):

    tokens read again a turn (median) prefill a turn, Spark 1 Spark 2
    e7c523eb 1,039 188.0 ms 188.3 ms
    commit 1 61 95.3 ms 96.5 ms
    commit 2 61 72.1 ms 73.2 ms

    Every turn resumed at the same token in commit 1 and commit 2.

  • Decode speed: unchanged (no change to windows or rounds).

  • Memory: one stage buffer, the size of one sequence's Mamba states and conv rows.

  • 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: tf-cuda-test prompt-cuts after each commit (above).

  • Not run, and why: Metal (unchanged). The NVFP4 checkpoint, RTX cards (no hardware here). --segments above 1 only runs a pass that keeps nothing, so it is unchanged.

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.
  • 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. (prompt-cuts needs the model and a GPU; the grid test's expectations follow the new cuts.)
  • 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.
@jschmied

Copy link
Copy Markdown
Contributor

Thanks for taking #670 on. prompt-cuts passes here too (GB10, a 9,000-token prompt of my own).

One gap: with the rules now .{ .lookahead = 1 }, the default min_prompt (4,096) applies again, so prompts under 4,096 tokens keep nothing. Commit 2 removes the extra pass that the default guards against. Adding .min_prompt = 0 (as GLM's CUDA rules have) cuts later turns' TTFT in a 12-turn agent loop of 1.2k to 4.8k tokens from 0.386 s to 0.134 s, with all replies equal. The first turn costs 0.12 s more, from its three keeps in one chunk. Worth adding here, or a follow-up after this lands?

@plotarmordev

Copy link
Copy Markdown
Contributor Author

Thanks for running prompt-cuts on your own prompt.

I'd keep the floor out of this pull request and send it as a follow-up once this lands. That keeps this one to the two changes the receipt covers, and leaves the floor's value to be settled beside #669, which reasons about the same floor for the planned families.

I measured it on this branch's second commit, with a local switch for the floor: 1,024 on both Sparks, 0 on one. Eight conversations of 0.5k to 3.8k tokens, six turns each:

  • With min_prompt 1,024 and with 0, all 48 replies equal the cache-off server's, two sessions each.
  • Later turns: 242 to 287 ms before, 55 to 75 ms after (the two floors within 1 ms of each other).
  • First turns (one Spark): +1.2% and +1.4%, overlapping the default's runs.

Your 0.12 s first-turn cost fits what I'd expect from this branch: only a lone keep in a chunk is staged, so a chunk holding several keeps still ends at the first. Staging every keep in a chunk would remove that cost, and I'd pair it with the floor in the follow-up.

@plotarmordev

Copy link
Copy Markdown
Contributor Author

Closing this as superseded by 1.0.8. #670's change, prompt states that keep and resume at any row, with a keep inside a chunk costing no prompt call (cc692e6f, 3f4850c0, merged in dd96ffaf), covers what this PR did: it kept states where a conversation's messages end and staged that keep inside the chunk. Thanks to @jschmied for #670 and to @ashhart for landing it.

#720 and #723 stay open. I'm merging 1.0.8 into both and will re-measure them on it, with The Grill behind the speed receipts.

plotarmordev added a commit to plotarmordev/TensorFold that referenced this pull request Oct 13, 2026
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.
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.
@ashhart

ashhart commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Thank you, @plotarmordev, and agreed: 1.0.8's in-chunk keeps from #670 cover what this PR did, so closing it was right.

Your test for it landed in 1.0.9. tf-cuda-test prompt-cuts (your commit 06401a5bf, under your name) checks one prompt pass that keeps states inside chunks and at their ends against a single uncut pass, then resumes a pass from every keep and compares the states, the MTP head's caches, the last hidden row, the logits and the first token. It runs against 1.0.8's own keeps.

On a GB10 (sm_121) it passes on both Nemotron checkpoints, MLX 4-bit and NVFP4, at 3,073, 4,096, 4,097 and 8,193 tokens, the last one running one row into a third 4,096-row pass.

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