Skip to content

Nemotron CUDA prompt reuse - #467

Closed
AdrianBinDC wants to merge 1 commit into
ashhart:zig-flashnextfrom
AdrianBinDC:zig-flashnext-nemotron-reuse
Closed

AdrianBinDC wants to merge 1 commit into
ashhart:zig-flashnextfrom
AdrianBinDC:zig-flashnext-nemotron-reuse

Conversation

@AdrianBinDC

@AdrianBinDC AdrianBinDC commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Nemotron on CUDA, from tensorfold run, keeps a prompt snapshot so a later turn restores the shared prefix and reads only the new tokens. Drafts stay on. The bytes copied are the engine caches and the MTP head. No activation or sum changes type. The Metal server path is unchanged, and CUDA Nemotron is still not served.

Receipt

  • Environment, with the TensorFold commit, MLX or PyTorch version, machine and GPU, checkpoint and revision: base zig-flashnext at db28187, this commit on top. Zig 0.17.0. No MLX. PyTorch 2.13.0a0 was used only to capture the Triton cubins. DGX Spark, NVIDIA GB10. Checkpoint NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit at 8bbcb5b6, with mtp-4bit.safetensors. Greedy, context 1024.
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: those tools talk to the server, and this engine is not served. On this GPU, a resumed prefill reported cached 64. Plain (--no-drafts), drafted, and reused matched on these tokens: 1267, 1784, 16194, 1880. A separate 1000-token --no-drafts prompt hashed 95bce61a71a7 on the base and on this branch. Concurrent versus solo was not run.
  • Decode speed before and after, from tools/bench_openai.py: not run. That tool talks to the server, and this engine is not served yet.
  • Prompt speed before and after, from tools/prefill_cold.py: that tool talks to the server, so this is tensorfold run on the same GPU, against the parent commit, not against the 0.6.6 release. 1000 tokens, --max-tokens 1 --no-drafts --eager --context 1024, order before, after, after, before. Prefill 0.1217 s, 0.1212 s, 0.1228 s, 0.1218 s. About 0.122 s both ways.
  • Tests run, with pass, fail and skip counts: zig build test on this branch, host only, no GPU: 60 passed, 0 failed, 0 skipped. That includes a host test that a prompt chunk ends on a keep. The GPU token comparison above is not a test in the tree.
  • Not run, and why: Metal on M1 through M5, this machine is not a Mac. Concurrent against solo, one GPU and no server. bench_concurrent.py, bench_openai.py, and prefill_cold.py, because CUDA Nemotron is not wired into the server. pytest, because issue Landing next #286 freezes the Python engine.

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

@SvangenStudios

Copy link
Copy Markdown

I tried this PR on today's base and served it on a DGX Spark. The receipt above couldn't cover that: it was run on db28187, where CUDA Nemotron wasn't wired into the server yet. With the port below, the server's replies with the prompt cache on match the cache-off run token for token, including concurrent conversations and a cancelled request.

Port to f8fe17d (1.0.2). Three files conflict, because the CUDA host has changed since db28187 (the memory budget, Host.serve(), and lone runs in cuda_lone.zig). I kept both sides:

  • cuda_lanes.zig: today's core import, plus cuda_reuse.zig.
  • cuda_native.zig: the lone field, loneRun and explain stay. target and the snap_* exports are added beside them.
  • native/cuda.zig: the store is made in openWith before h.serve(...), from cacheBudget(o) and loaded.target. serve() sets h.host.cache = h.store before h.host.start(). close() frees the store after makeCurrent, as in your version.

Built with zig build native -Dnvcc=… -Dsm=121, using the 1.0.2 release's sm121 captures through TENSORFOLD_CUDA_KERNELS. zig build test passes apart from the direct_io test (#486/#500). I can push the port to a branch if that helps.

Setup.

  • Hardware and software: GB10, DGX OS 7.6.0 (kernel 7.0), driver 580.178.04.
  • Checkpoint: TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit @ d9d758fb.
  • Server flags: --context 69632 --parallel 4 --no-thinking, drafts on, greedy, max_tokens 96.
  • One run with --prompt-cache-gib 8, one with --prompt-cache-gib 0, each on a fresh process. I compared the server's token_sha and the reply text turn by turn.

Workload. Each conversation has a ~7.1K-token system prompt (the repo's LICENSE three times, under a per-conversation first line) and three user turns. Each reply is fed back as history. The run, in order:

  1. Conversation 0 alone. Before its second turn, a request with a different last message is cancelled after 12 streamed chunks; the real second turn then goes out on the same history.
  2. Conversations 0–3 at once, each turn waiting for the previous one.
  3. Conversation 1 again.

Result: 18/18 turns have the same token_sha and text with the cache on and off.

cached / prompt tokens TTFT, cache on / off
2nd and 3rd turns 6,617–7,315 of 7,184–7,383 0.04–0.23 s / 1.02–2.06 s
conversation 0 again, inside the concurrent run 7,144 / 7,150 3.44 s / 4.07 s, while three others prefill
conversation 1 again, alone 6,617, 7,140, 7,209 0.19, 0.11, 0.04 s / ~1.0 s
first turns of new conversations 0 1.12 / 1.00, 2.26 / 2.03, 1.11 / 1.00, 3.39 / 3.05 s

The first uncached turns were 11–12 % slower with the cache on in this run. I haven't alternated the runs, so I can't say whether that comes from the cache or from run-to-run variation.

The server received 19 requests and logged 18 as done. The cancelled one has no done line, and the turn after it matches between the two cache settings. Both runs include the cancellation; I didn't run a version without it.

Cache log at the end: 15 states, 8156 MiB … of 8192 MiB (hits 15, misses 4, evicted 9, refused 0, failed 0). After the run, /health showed 28.4 GB active with the cache against 19.8 GB without.

Two things to look at:

  • Without --prompt-cache-gib, cacheBudget turns the cache on at 70 % of RAM, capped at 16 GiB. On a GB10 that memory is the same pool the streams' budget comes from, and the startup line's "GiB left" is computed before the store fills.
  • Some second turns resume at 6,617 rather than near the end of the previous turn. That happened after evictions, with a 3-turn × 5-conversation workload in an 8 GiB budget. Eviction is a possible explanation, but I haven't established why the boundary is exactly 6,617.

Not run: Metal, sampled requests, and --require-reuse from #485 against this build.

@plotarmordev

Copy link
Copy Markdown
Contributor

@SvangenStudios thanks for the port and the 18/18 run. Could you push it to a branch? I'd like to add a receipt from a second Spark (GB10, kernel 6.17) on the same base.

@SvangenStudios

Copy link
Copy Markdown

@plotarmordev Here it is: SvangenStudios/TensorFold@port/467-on-1.0.2, one commit (cd6c795) on f8fe17d. It's this PR's commit, with the conflicts in cuda_lanes.zig, cuda_native.zig and native/cuda.zig resolved as described above. The author is unchanged.

I built and served it as before: zig build native -Dnvcc=… -Dsm=121, with TENSORFOLD_CUDA_KERNELS pointing at the 1.0.2 release's share/tensorfold/cuda/sm121.

@plotarmordev

Copy link
Copy Markdown
Contributor

Receipt for the port (SvangenStudios/TensorFold@cd6c795, on f8fe17d) from a second DGX Spark (GB10, kernel 6.17.0-1029-nvidia, driver 580.173.02), built with zig build native -Dnvcc=… -Dsm=121 and the 1.0.2 release's sm121 kernels. Same workload as @SvangenStudios's comment (3 × LICENSE system prompt, three turns, a stream cancelled after 12 chunks before conversation 0's second turn, then conversations 0–3 at once, then conversation 1 again), --context 69632 --parallel 4 --no-thinking, greedy, max_tokens 96, a fresh process per arm:

  • --prompt-cache-gib 8 vs --prompt-cache-gib 0: 18/18 turns have the same token_sha.
  • --prompt-cache-gib 0 vs stock main (f8fe17d): also 18/18, so the cache-off path is unchanged.
  • Later turns: 7,142–7,374 of 7,248–7,386 prompt tokens cached, TTFT 0.04–0.19 s with the cache vs 1.01–1.08 s without. Conversation 1 again, alone: 6,618 / 7,142 / 7,365 cached, 0.18 / 0.12 / 0.04 s.
  • Cache log at the end: 15 states, 8156 MiB … of 8192 MiB (hits 15, misses 4, evicted 9, refused 0, failed 0), the same counts as yours. /health active: 28.4 GB with the cache, 19.8 GB without.
  • 19 requests, 18 done lines; the cancelled one has none.

The uncached first turns were again a little slower with the cache on in the alone phase (1.10 s vs 0.99–1.00 s); in the concurrent phase the order varied between arms, so I wouldn't read anything into those.

@ashhart ashhart mentioned this pull request Oct 10, 2026
@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thank you, @AdrianBinDC, and thanks to @SvangenStudios for the port to 1.0.2 and @plotarmordev for the second
receipt. Nemotron on CUDA now keeps prompt states between turns in main: your commit, rebased onto today's main, is
b2bd19a under your name, authored with your GitHub noreply address, and our change follows it in e2dcb78.
Because the commit was rebased and re-authored, GitHub shows this PR as closed rather than merged.

Our change is the one we mentioned. chunkEnd ended a prompt chunk at any keep mark, so the same prompt could be cut
in different places with the cache on and off, and a row's bits are only guaranteed when every pass uses the same
chunks. Now a pass that keeps or resumes a state stays on the zero-anchored 2,048-row grid a cache-off pass uses.
The prompt cache floors its marks to that grid and resumes only there, a kept state is keyed one token past its
position because the MTP head has already read that token, and the cache's budget comes from the CUDA memory budget
left after the streams (16 GiB unless --prompt-cache-gib sizes it).

On a GB10, with a workload shaped like @SvangenStudios's (a 7,200-token system prompt, three turns, a stream
cancelled after 12 chunks, a turn that adds 2,400 tokens, three conversations at once and then alone), 29 of 29
replies were equal with the cache on and off, and equal to 1.0.4's. Nineteen turns resumed, at 6,144 or 8,192
tokens, and a resumed turn's first token came in 0.19 to 0.21 seconds against 1.06 to 1.08 without the cache.
Concurrent turns equal their solo runs, and drafted replies equal "draft": false. It ships in the next release.

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.

4 participants