Skip to content

--learn for Nemotron: shared prompt states on disk, keyed by a startup probe (stacked on #497) - #499

Closed
BobClawblaw wants to merge 3 commits into
ashhart:mainfrom
BobClawblaw:nemotron-learn
Closed

BobClawblaw wants to merge 3 commits into
ashhart:mainfrom
BobClawblaw:nemotron-learn

Conversation

@BobClawblaw

Copy link
Copy Markdown
Contributor

Stacked on #497 (its commit is the first one here; this PR is the second). --learn keeps 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's write, read and forget functions, 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 an Imprint under --learn-dir with the identity below and hands it to the store; the vtable gains write/read/forget.
  • Docs: README's --learn row, 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.json and model.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-dir pointed at an empty directory, the same 9.2k-token system prompt and four-turn greedy chat as #497:

Server What happened Replies vs cache off
A, --learn learned the 8,192- and 9,213-token cuts (213 MB on disk); turns 2-4 resumed in memory equal, 4/4
B, --learn, fresh process opened the same identity, "2 known"; turn 1 resumed 9,213 tokens from disk in 0.27 s (A's turn 1: 1.30 s) equal, 4/4
C, --learn --prompt-cache-gib 0 identity opened, nothing resumed (no cache) equal, 4/4

B opening A's identity directory shows the probe's bits are stable across processes.

Receipts

  • Machine, toolchain, checkpoint revision and settings as in Prompt reuse for Nemotron on Metal: kept states copied on the GPU, exact against cache-off #497 (M5 Ultra 256 GB, macOS 27.0.1, Zig 0.17.0, Nemotron revision d9d758fb).
  • zig build test test-golden -Dcpu=apple_m1: all steps pass; golden 0 differ, the known differences unchanged from main.
  • tools/zig/lean_check.py: 512 on main, 512 after. zig fmt --check clean; every touched file under 600 lines.
  • Not run: CUDA, Metal on M1 to M4, --learn across 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

BobClawblaw and others added 3 commits October 7, 2026 20:29
…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]>
@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thank you, @BobClawblaw. Keying learned states with a probe of the build's own prompt bits ties them to the arithmetic
that made them, and the three-server receipt made it easy to check.

It's on main as c6e050a64 and 59d992301, under your name, with one change in the first: main already had a
families/nemotron/learned.zig (Sliding Weights' residual store), so your file landed as learned_prompts.zig. Our
follow-up c3f541d1c adds each shard file's size, inode and change times to the identity. --slide writes learned
weights into the shards in place, and after the first lesson config.json and the index stay byte-identical, so the probe
was the only thing that could notice. It also sets the Imprint field to null at startup, like the store, and gives
--learn below --context 4096 a clear refusal, since the probe is a 4,143-token prompt.

On an M3 Ultra a fresh server read 8,328 learned tokens from disk on a new conversation's first turn, and every reply
matched the cache-off server. After touching one shard the server started a new identity and resumed nothing from disk,
with replies still equal.

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.

@ashhart ashhart closed this Oct 10, 2026
eleqtrizit pushed a commit to eleqtrizit/TensorFold that referenced this pull request Oct 10, 2026
…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.
eleqtrizit pushed a commit to eleqtrizit/TensorFold that referenced this pull request Oct 10, 2026
@BobClawblaw
BobClawblaw deleted the nemotron-learn branch October 15, 2026 15:21
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…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.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
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