Repository navigation
Prompt reuse for Nemotron on Metal: kept states copied on the GPU, exact against cache-off - #497
BobClawblaw wants to merge 1 commit into
Conversation
…act against cache-off Nemotron keeps each stream's prompt state at the prompt cache's marks and resumes a later request whose prompt extends one, as Flash Next and GLM do. A kept state is every Mamba conv and SSM state, the KV rows and the draft head's KV rows, in one Metal buffer (families/nemotron/snapshot.zig). Save and restore are tf_copy_u32 dispatches on the engine's fenced queue, a save right after the chunk that ends at the mark and a restore right before the first chunk, so neither waits for the host. The prompt pass in backend.zig restores before its first chunk and calls the mark hook after each chunk at a mark, which covers the shared-lane and lone-stream paths alike. The Nemotron host in native/metal.zig sizes its store as Flash Next does (cache_fit, or --prompt-cache-gib; 0 turns it off). On an M5 Ultra, turns 2-4 of a 9.2k-token chat take 0.28-0.31 s instead of 1.29-1.36 s, a fresh prompt costs the same on or off (1.07 s), and every reply equals its cache-off reply: alone, two conversations at once, and with --no-drafts. The receipt is in docs/recipes/nemotron-3.5.md. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Spark (GB10) checkSame steps on clean
The CUDA build starts from The one failure is on Not run here: a live Nemotron generation on GB10 (no checkpoint on this box, and no CUDA code changed). |
Mac (M5 Ultra) checkThe Mac side of the check the Spark comment refers to: the same
1,115 replies, 0 mismatches, 0 errors the reference did not also give, 0 second runs that read their prompt again.
Peak memory was measured separately in the PR description (25.08 GiB off, 25.59 GiB on, two 9k-token conversations). |
|
Thank you, @BobClawblaw. This gives Nemotron the prompt reuse the other families already had, and doing the keep and It's on main as We checked it on an M3 Ultra, a chip without tensor units. All 21 replies matched the cache-off server: three chats, We re-made the commits without their AI-tool trailer lines, which main's history doesn't carry, so GitHub shows this PR as closed rather than merged. The commit is yours. |
…hy, metrics and /memory see it Lands ashhart#497 on main. The Nemotron host is gpa.create'd, so its optional store field never took its default: with --prompt-cache-gib 0 close() read undefined memory. It is set to null before the budget is planned, as the Sliding Weights field is. The budget now comes from cache_fit.budget, as for Flash Next and the 27B: the same 70% rule, its plan on /memory, and the full refusal text when --prompt-cache-gib passes the cap. The server prints an engine's reason when the engine fails after giving one, where it printed only the error's name. The lane host reports prompt_cache, so /metrics counts restored prompt tokens. The store is the lane host's own (h.host.cache), so a Sliding Weights step that moves weights drops every kept state; a lane-host test now checks that the turn after such a step prefills from the start.
…hy, metrics and /memory see it Lands ashhart#497 on main. The Nemotron host is gpa.create'd, so its optional store field never took its default: with --prompt-cache-gib 0 close() read undefined memory. It is set to null before the budget is planned, as the Sliding Weights field is. The budget now comes from cache_fit.budget, as for Flash Next and the 27B: the same 70% rule, its plan on /memory, and the full refusal text when --prompt-cache-gib passes the cap. The server prints an engine's reason when the engine fails after giving one, where it printed only the error's name. The lane host reports prompt_cache, so /metrics counts restored prompt tokens. The store is the lane host's own (h.host.cache), so a Sliding Weights step that moves weights drops every kept state; a lane-host test now checks that the turn after such a step prefills from the start.
Nemotron keeps each stream's prompt state at the prompt cache's marks and resumes a later request whose prompt extends one, as Flash Next and GLM do. This is item 1 of ZIG-PREVIEW's "where you can help".
What changed
zig/src/families/nemotron/snapshot.zig(new): a kept state is every Mamba conv and SSM state, the KV rows and the draft head's KV rows, in one Metal buffer. Save and restore aretf_copy_u32dispatches submitted on the engine's fenced queue: a save right after the chunk that ends at the mark, a restore right before the first chunk, so neither waits for the host. A drop drains first, since the queue holds unretained references. The module also carries theprompt_cache.Snapshotsvtable functions.zig/src/families/nemotron/backend.zig: the prompt pass restores a kept state before its first chunk and calls the mark hook after each chunk that ends at a mark. Both the shared-lane and the lone-stream paths runprefillFn, so one change covers both.zig/src/native/metal.zig: the Nemotron host gets aprompt_cache.Storewith the same budget rule as Flash Next (cache_fit: what 70% of RAM leaves past the loaded footprint, or--prompt-cache-gib;0turns it off).--prompt-cache-giband--prompt-cache-over-caprows in README, the known gaps and help list in ZIG-PREVIEW, RUNBOOK, the family guide's ownership table, and a receipt section in the Nemotron recipe.No arithmetic changes: the prompt kernels, decode kernels and reduction orders are untouched. The kept bytes are copies of live rows.
Not in this PR, listed as the next step in ZIG-PREVIEW:
--learnfor Nemotron (its states stay in memory), and a pool of kept buffers as Flash Next has. Each state allocates its own buffer today; that showed no cost at this size (below).Exactness
Same binary, same checkpoint, same settings; cache on against
--prompt-cache-gib 0. Four-turn greedy chats, a 9.2k-token system prompt, 96 reply tokens a turn.--parallel 4), each vs its solo cache-off run--no-drafts, cached vs cache off--no-draftsResumes happened at 9,213 tokens (the shared system block's cut) on turns 2 and 3 and at 9,556 (the previous turn's state) on turn 4, so both kinds of kept state were exercised.
Speed
Memory
Peak physical footprint (
/usr/bin/time -l) for the server through two four-turn conversations of about 9k tokens: 25.08 GiB with the cache off, 25.59 GiB with it on. A kept state at 9.2k tokens is 106 MiB, most of it the fixed Mamba state.Receipts
TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit, revisiond9d758fb83953437f7263256b0d96157e2a348b8.serve --context 32768 --temperature 0 --no-thinking,max_tokens96 (8 for the fresh-prompt timing), the repository'sCHANGELOG.mdanddocs/recipes/qwen3.8-flash-next.md(first 30k characters) as system prompts, four questions each.zig build test -Dcpu=apple_m1: 91/91 steps, 93/93 tests on the final tree, and on five earlier runs. One early run, before the final tree, had one failing host test that I did not capture and that did not recur in seven later runs.zig build test-golden -Dcpu=apple_m1: 188 equal, 0 differ, 2 known (errors), the same on cleanmain; the leak reports it prints come fromgolden_checkitself and are the same onmain.tools/zig/lean_check.py: 512 problems onmain, 512 after. The five findings in the touched files are inherited (backend.zig:299,backend.zig:392,native/metal.zig:1, and two inflashnext/snapshot.zig).zig fmt --checkclean; every touched source file stays under 600 lines.🤖 Generated with Claude Code