Repository navigation
--learn for Nemotron: shared prompt states on disk, keyed by a startup probe (stacked on #497) - #499
BobClawblaw wants to merge 3 commits 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]>
…p probe of this build's prompt-pass bits A kept state at a shared cut (a system prompt and its tools) is written to disk once and read back by later servers, as GLM's are. families/nemotron/learned.zig holds the file format, the three prompt-cache functions (write, read, forget) and the probe: a fixed 4,143-token prompt whose state at a mark is hashed at startup, about a second. The identity a learned state lives under is the checkpoint's config and tensor index, the MTP file's header, the prefill step, the head, every kernel source (kernels.sourceHash) and the prebuilt library, the probe's bits, the OS build and the chip. On an M5 Ultra a fresh server resumed a new conversation on a 9.2k-token system prompt in 0.27 s instead of 1.30 s, reading 9,213 tokens from disk, with every reply equal to the cache-off reply. The receipt is in docs/recipes/nemotron-3.5.md. Co-Authored-By: Claude Fable 5.1 <[email protected]>
… change at 1.07 s a fresh prompt, under churn too Twelve fresh 9.2k-token prompts cost 1.07 s each with the cache off, with every state kept, and with a 0.3 GiB budget that evicts and reallocates two 106 MiB states a request (22 evictions). The GPU-side copy hides a new buffer's cost, so the pool goes to the guide's "Don't bother" list and ZIG-PREVIEW's help list points at --learn for Flash Next instead. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Thank you, @BobClawblaw. Keying learned states with a probe of the build's own prompt bits ties them to the arithmetic It's on main as On an M3 Ultra a fresh server read 8,328 learned tokens from disk on a new conversation's first turn, and every reply As with #497, 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 commits are yours. |
…entity, the field starts empty Lands ashhart#499 on main. Its commit applies with its new file at families/nemotron/learned_prompts.zig (nemotron.learned_prompts), since main's learned.zig is Sliding Weights' residual store. --slide writes learned weights into the model's own shards, in place once a projection is bf16, and then config.json and the tensor index read the same as before. The probe notices a weight change only through its own prompt's arithmetic, so the identity now also holds each shard's size, inode, modification and change times: weights written into a shard always start a new identity. A test rewrites a shard with the same bytes and checks the identity moves. The host's optional Imprint field, like the store, never took its default under gpa.create: without --learn, close() read undefined memory. It is set to null before --learn is read. The probe is a 4,143-token prompt, so --learn below --context 4096 failed with PromptTooLong and no reason; the server now names the fix.
…entity, the field starts empty Lands ashhart#499 on main. Its commit applies with its new file at families/nemotron/learned_prompts.zig (nemotron.learned_prompts), since main's learned.zig is Sliding Weights' residual store. --slide writes learned weights into the model's own shards, in place once a projection is bf16, and then config.json and the tensor index read the same as before. The probe notices a weight change only through its own prompt's arithmetic, so the identity now also holds each shard's size, inode, modification and change times: weights written into a shard always start a new identity. A test rewrites a shard with the same bytes and checks the identity moves. The host's optional Imprint field, like the store, never took its default under gpa.create: without --learn, close() read undefined memory. It is set to null before --learn is read. The probe is a 4,143-token prompt, so --learn below --context 4096 failed with PromptTooLong and no reason; the server now names the fix.
Stacked on #497 (its commit is the first one here; this PR is the second).
--learnkeeps Nemotron's shared prompt states on disk, as GLM's are, so a fresh server resumes a new conversation on a known system prompt without reading it again.What changed
zig/src/families/nemotron/learned.zig(new): a kept state's file (position, layout, size, bytes, written through a temporary file and renamed), the prompt cache'swrite,readandforgetfunctions, and the startup probe: a fixed 4,143-token prompt (a whole chunk, a middle one, a short one on the decode kernels) whose state at a mark is hashed through the same mark hook real requests use.zig/src/families/nemotron/kernels.zig:sourceHash()over every embedded Metal source, the NAX header, the prefill sources and the prebuilt library.zig/src/native/metal.zig: the Nemotron host opens anImprintunder--learn-dirwith the identity below and hands it to the store; the vtable gainswrite/read/forget.--learnrow, ZIG-PREVIEW's gap line and help list, and the receipt in the Nemotron recipe.The identity a learned state lives under: the checkpoint's
config.jsonandmodel.safetensors.index.json, the MTP file's safetensors header, the prefill step, whether the head is loaded, the kernel source hash, the probe's bits, the OS build and the device name. The probe adds about a second to startup (3.7 s loaded against 2.8 s).Exactness and speed
Three servers in a row on the same build,
--learn-dirpointed at an empty directory, the same 9.2k-token system prompt and four-turn greedy chat as #497:--learn--learn, fresh process--learn --prompt-cache-gib 0B opening A's identity directory shows the probe's bits are stable across processes.
Receipts
d9d758fb).zig build test test-golden -Dcpu=apple_m1: all steps pass; golden 0 differ, the known differences unchanged frommain.tools/zig/lean_check.py: 512 onmain, 512 after.zig fmt --checkclean; every touched file under 600 lines.--learnacross an OS or kernel change (the identity changes by design; a changed identity leaves the old states for the cap's sweep, as GLM's do).🤖 Generated with Claude Code