Skip to content

Flash Next: --learn keeps shared prompt prefixes on disk, on one Mac - #519

Merged
ashhart merged 1 commit into
ashhart:mainfrom
BobClawblaw:flashnext-learn
Oct 11, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
BobClawblaw:flashnext-learn

Conversation

@BobClawblaw

Copy link
Copy Markdown
Contributor

--learn for Flash Next on the native server, as GLM has it: shared prompt prefixes (a system prompt and its tools) go to disk, and a fresh server, or an upgrade that computes the same bits, resumes them without reading them again. ZIG-PREVIEW's known gap "Nemotron and Flash Next keep theirs in memory", the Flash Next half.

What changes

  • zig/src/families/flashnext/learned.zig (new, 123 lines): a kept state's file (<key>.bin: header with position, layout, size and the n-gram history, then the bytes; written to a .part and renamed), read back into a buffer from the engine's snapshot pool; sourceHash() over the checked-in kernel sources and roles; and probe(), a prompt pass over fixed tokens (a full call, a middle and a short one) with two marks whose kept states are hashed.
  • zig/src/families/flashnext/snapshot.zig: blank(), an empty state in a pool buffer for a file to fill; layoutOf public.
  • zig/src/families/flashnext/engine.zig: identity_hash, the checkpoint identity text pack_io already computes, hashed and kept.
  • zig/src/native/flashnext_host.zig: write/read/forget on the store's vtable (a read-back state is named by the request's tokens as a saved one is, so h.prompt is set before store.begin), learnedStates() opening the imprint under --learn-dir keyed by checkpoint identity, kernel sources, the xnew / hc_up / tensor-unit modes, the probe's bits, OS build and chip; open takes learn and learn_cap and refuses --learn on a speed-up pair with a message.
  • zig/src/native/metal.zig: passes --learn through, as for GLM.

One Mac for now. A speed-up pair keeps rank 1's halves of each state under the same key over the link (peer_kept); persisting those on rank 1 and loading them back in step with rank 0 is the GLM mirror .persist/.load/.forget pattern, and I have one Mac here, so the pair refuses --learn plainly rather than half-working.

Measured

M5 Ultra (256 GB, macOS 27.0.1), Zig 0.17.0, Metal Toolchain 27A266a, TensorFold/Qwen3.8-Flash-Next-MLX-4bit-MTP at 2b170fa6, checked-in kernels (no dump), greedy, 96-token replies, a four-turn chat on a 4,985-token system prompt (the repo README):

Server Turn 1 prompt cached time Turns 2-4
--learn, first chat 4,985 0 3.08 s 1.97 s, kept states
same server, same chat again 4,985 4,980 1.92 s 1.90 s
fresh server, --learn 4,985 4,455 from disk 2.06 s 1.94 s
rebuilt binary, fresh server 4,985 4,455 from disk 2.06 s 1.93-1.94 s
--prompt-cache-gib 0 4,985 0 3.06 s 3.02-3.04 s
  • The second chat on the first server wrote the two shared cuts to disk (2,919 and 4,455 tokens; 203 and 249 MB files). The fresh server found both at startup ("2 known"), read the 4,455-token one back in 33 ms (lookup and restore) and computed the remaining 530 tokens in 287 ms.
  • Every reply on every server equals the cache-off server's, all four turns of all four chats.
  • The rebuilt binary (comment-only changes) keyed to the same learned directory, as an upgrade that computes the same bits should: "2 known", 4,455 tokens read from disk, replies equal.
  • Startup to first request served: 19.1 s without --learn, 19.6 s with it (the probe is one 4,143-token pass with two marks).

Checks

  • zig build test -Dcpu=apple_m1: passes.
  • Lean checker: 512 problems, the baseline.
  • Nothing in zig/src/cuda or the Python engine is touched.

Docs

README's --learn row (GLM and Flash Next on one Mac), ZIG-PREVIEW's Flash Next paragraph and known gaps.

Independent of #497, #499, #502 and #518 (no overlapping hunks; #499 is the Nemotron half of the same gap and edits the same README cell, a one-line merge either way).

🤖 Generated with Claude Code

The native server's Flash Next host writes the prompt cache's shared cuts to disk under --learn-dir and reads them back
in later sessions and servers, as GLM does. learned.zig holds a kept state's file format (header, then the pool
buffer's bytes), the kernel source hash and a startup probe (one prompt pass over fixed tokens, two marks' states
hashed) that keys this build's bits; the imprint identity also takes the checkpoint identity text pack_io computes,
the xnew / hc_up / tensor-unit modes, the OS build and the chip. A speed-up pair refuses --learn with a message:
rank 1's halves are not persisted yet.

M5 Ultra, Flash Next 4-bit MTP, a 4,985-token system prompt: a fresh server read the 4,455-token learned state back in
33 ms and computed the remaining 530 tokens in 287 ms; every reply equals the cache-off server's. A rebuilt binary
keyed to the same states. Startup 19.6 s with --learn against 19.1 s without.
@ashhart
ashhart merged commit 008571a into ashhart:main Oct 11, 2026
2 checks passed
@ashhart

ashhart commented Oct 11, 2026

Copy link
Copy Markdown
Owner

Thank you, @BobClawblaw, and sorry it waited. Main had moved 368 commits since 1.0.2, so we rebased it for you: merge 8be26714e resolves the five conflicts (packs stay in main's cache folder with your identity hash beside them, open() takes --learn with main's error mapping plus your pair refusal, and the docs list Flash Next beside GLM and Nemotron), and e8a52e11a is zig fmt turning @intFromEnum into Zig 0.17's @backingInt in learned.zig. It's in 1.0.6. Your original commit lands under your name as 0eaf47236, with its AI co-author line left out (we keep AI attribution lines out of the public history), and your rebased head be6293f13 is merged too, so the PR shows as merged.

Our gate, on one M5 Ultra with TensorFold/Qwen3.8-Flash-Next-MLX-6bit-MTP, greedy, a four-turn chat on the README as the system prompt (6,122 tokens):

server first turn cached turns 2-4 cached replies
--prompt-cache-gib 0 0 0 reference
main, no --learn (twice) 0 6,115 / 6,231 / 6,347 equal
--learn, two chats 0, then 6,115 as main equal; 496 MB written
fresh server, same --learn dir 5,591 from disk ("2 known") as main equal

Every reply on every server equalled the cache-off server's, all four turns. We didn't run the speed-up pair; its refusal is from reading the code.

jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 11, 2026
…n disk, on one Mac

Thanks @BobClawblaw. Rebased onto 1.0.6's candidate by the merge: packs stay in main's cache folder with the identity hash beside them, open() takes --learn with main's error mapping plus the pair refusal, and the docs list Flash Next beside GLM and Nemotron. The PR's commit lands with its AI co-author line left out.
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 11, 2026
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 11, 2026
@BobClawblaw
BobClawblaw deleted the flashnext-learn branch October 11, 2026 20:35
BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
…rves Nemotron; the served list names the 27B
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…n disk, on one Mac

Thanks @BobClawblaw. Rebased onto 1.0.6's candidate by the merge: packs stay in main's cache folder with the identity hash beside them, open() takes --learn with main's error mapping plus the pair refusal, and the docs list Flash Next beside GLM and Nemotron. The PR's commit lands with its AI co-author line left out.
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
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
The author rebased ashhart#519 onto main after our merge; this keeps the PR's current 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
…rves Nemotron; the served list names the 27B
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