Skip to content

Nemotron CUDA: kept prompt states hold each attention layer's first rows only - #722

Closed
plotarmordev wants to merge 3 commits into
ashhart:mainfrom
plotarmordev:perf/nemotron-prefix-states
Closed

plotarmordev wants to merge 3 commits into
ashhart:mainfrom
plotarmordev:perf/nemotron-prefix-states

Conversation

@plotarmordev

@plotarmordev plotarmordev commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

On top of #721 (its two commits come first here; this pull request adds the last one). A Nemotron CUDA prompt state kept after at tokens holds each attention layer's first at KV rows instead of the whole window's, and the prompt cache counts it at that size, so the cache holds more states and each keep copies less.

  • cuda_state.zig: Buffers.keptBytes(at), snapshotFrom(ops, at, from) and restoreAt(ops, saved, at) copy each attention layer's first at rows of the K and V planes and every other plane whole; snapshot/restore (the profiling tools') keep the whole window.
  • cuda_reuse.zig: bytes(at) reports that size; save and restore use it. The KV rows past at are left as they are on restore: no pass reads a row past its position before writing it.
  • tf-cuda-test prompt-cuts fills those rows with a value (bf16 10.0) before each restore and hashes the prompt's rows of the KV planes, so a pass whose result depended on one would fail it.

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

  • Exactness: prompt-cuts passes with the rows past each keep filled. Twelve conversations served turn by turn (each turn of every conversation, then the next turn), four turns, 4.7k to 8.5k-token prompts, --context 65536 --parallel 4 --prompt-cache-gib 4: all 48 replies equal the cache-off server's on both Sparks, two sessions each.

  • Prompt speed and cache, the same workload:

    states held (MiB each) later turns resumed at later turn prefill, median of 72 (Spark 1 / Spark 2) first turn (Spark 1 / Spark 2)
    Nemotron CUDA: keep prompt states where messages end, staged inside a chunk without an extra pass #721 7 (516) a 7-token prefix 963 ms / 964 ms 907 ms / 894 ms
    this change 24 (166) the history's end, 36 of 36 69 ms / 70 ms 890 ms / 886 ms
    cache off - - 898 ms / 894 ms 883 ms / 875 ms

    At --context 16384 and the default 16 GiB nothing was evicted in the earlier workloads either way, so the gain shows when kept states compete for the budget, as with many conversations or a long window.

  • 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: tf-cuda-test prompt-cuts with the filled rows (above).

  • Not run, and why: Metal (unchanged). 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.
  • 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. (The filled rows in prompt-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.
…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.
@plotarmordev

Copy link
Copy Markdown
Contributor Author

Closing this as superseded by 1.0.8. 97b0fea5 ("a kept prompt state holds its prompt's key and value rows, not the window's", merged in 8aeb2de2) does what this PR did: a kept state holds each attention layer's rows up to the prompt's length instead of the whole window. Thanks to @ashhart.

#723 builds on this branch. It will be brought onto 1.0.8's prompt states and re-measured there before it asks for review again.

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. Yes, 1.0.8 already keeps only a prompt's own key and value rows, so this one is covered.

Its test landed in 1.0.9: 0426a1833, under your name, makes tf-cuda-test prompt-cuts fill the key and value rows past each keep before it restores, so a resume that read a row it should have written fresh would fail the comparison. It runs against 1.0.8's sized states, and on a GB10 every resumed pass equals the uncut one on both checkpoints, MLX 4-bit and NVFP4, at all four prompt lengths we ran (3,073 to 8,193 tokens).

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

2 participants