Skip to content

Inkling 2 Mini on Metal: a prompt-cache snapshot copies each key-value head's live rows, and the real-checkpoint serving tests run to it - #4607

Closed
sethforprivacy wants to merge 23 commits into
ashhart:mainfrom
sethforprivacy:inkling2-mini-metal-snapshot
Closed

sethforprivacy wants to merge 23 commits into
ashhart:mainfrom
sethforprivacy:inkling2-mini-metal-snapshot

Conversation

@sethforprivacy

@sethforprivacy sethforprivacy commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: with a prompt cache (--prompt-cache-gib), a Mini turn that resumes from a kept state gives another reply than the same prompt answered fresh. After: the two are equal. A second commit corrects serving_test.zig's two real-checkpoint tests, which abort on #4387's head before they reach this, so that they run and check it.

This is a proposal on top of #4387, opened as a draft.

The cause. A cache slot holds its keys head by head, each head the ring's rows (attention.metal writes a row at (h * ring + pos % ring) * D), and its values the same way. snapshot.zig copied the first live * kv_width * 2 bytes of a slot, which is head 0's region and then on into the next heads' regions, not each head's live rows:

  • A global slot, whose ring is the stream's capacity rounded up to 64: the copy holds heads 0 to 6 whole and the first rows of head 7, and never head 7's later rows. Restored into a cache of another capacity (the next turn's prompt plus its max_tokens), head h's rows land h times the difference of the two rings away from where the kernels read them.
  • A sliding slot whose ring is not yet full (a prompt shorter than the ring): the same. Once the ring is full the copy is the whole region, which is why only the global layers were wrong in the case below.

The fix. The save and the restore copy each head's first live rows, from and to that cache's own head stride. A snapshot's size is what snapshot.bytes already charged.

How it showed. A three-turn chat, 2,602 tokens in the first prompt, each later turn adding a question; --prompt-cache-gib 4 against 0, plain, greedy, on the stack named under Receipt:

Turn Fresh token_sha Resumed, before Resumed, this head
2 (2,706 tokens, resumed at 2,560) e0ab3a54d5c8 3073935a5dfd e0ab3a54d5c8
3 (2,826 tokens, resumed at 2,560) f70b7d74e30d ac1bda4c664a (its prompt differs, as turn 2's reply did) f70b7d74e30d

The chunk cuts are the same in both runs (the fresh turn also cuts at 2,560), and the result is the same at 64-row and 256-row chunks and with and without the drafter. Dumping the residual after every visit of the forward that follows the resume: resumed and fresh are equal through visits 0 to 4, which are sliding layers with full rings, and every row differs after visit 5, the first global layer.

Turn 2's prompt work is 2.67 s fresh and 0.19 s resumed, so the cache is worth having right.

Checks on this head

  • Head commit: 52bed2b8b.
  • Lean checker, on the base and on this head: 167 and 167, 0 new.
  • Tests run:
    • runtime_test.zig, new: "A snapshot restores each head's live rows into a cache of another capacity". It fills the first 100 rows of every head of every slot's keys and values, and the convolution states, with random bytes in a cache of capacity 200, saves, restores into a cache of capacity 500 and reads the same rows back. It needs a Metal device and skips without one. On Inkling 2 Mini on Metal: #4387's native build compiles, its norms write their output, and concurrent streams equal solo #4446's snapshot.zig it fails; on this head it passes.
    • zig build native -Dcpu=apple_m1: compiles.
    • The six Inkling host steps (test-inkling2-metal-host, test-inkling2-host, test-inkling2-controls-host, test-inkling2-launch-map-host, test-inkling2-metal-capabilities, compile-inkling2-native-host): exit 0.
    • tf-kernel-contract --only inkling2 (-Doptimize=safe): SUMMARY 41 kernels: 41 pass gates 1-4, 0 fail, 0 known findings; 41 controls: 41 red as planted, 0 missed; 0 limit problems, exit 0.
    • zig fmt --check build.zig zig and git diff --check: clean.
    • serving_test.zig's two real-checkpoint tests, as 52bed2b8b corrects them, with TF_INKLING2_METAL_DIR and TF_INKLING2_DSPARK_DIR set and Metal loader: a checkpoint span of 16 GiB or more loads (each pread capped at 1 GiB) #4435's read cap picked (the table under "The real-checkpoint serving tests").
  • CI: not yet reported when this was written.
  • Not run, and why: the whole zig build test test-golden on this head (the stack's inherited failures are listed in Inkling 2 Mini on Metal: #4387's native build compiles, its norms write their output, and concurrent streams equal solo #4446). CUDA and HIP are not touched; I did not look at whether their snapshots have the same shape.

The real-checkpoint serving tests (52bed2b8b). serving_test.zig's two tests run with TF_INKLING2_METAL_DIR (and TF_INKLING2_DSPARK_DIR for the second). On #4387's head they abort at the assert in firstLogprobRow, and behind the abort two of their expectations cannot hold. The commit changes the tests only:

  1. The first row is asked after the first token. serial and transactions called firstRow on a stream that had drawn nothing, and firstLogprobRow reads the token drawn (tokens[(next - 1) % ring], with next > 0 asserted). The host draws and then asks (lanes/open.zig), so the tests now do the same.
  2. The row after a wrong draft is not compared. transactions plants a wrong seventh draft and then expected all eight sampled rows to be the reference's tokens. The eighth row is the model's reply to the wrong draft (94 where the reference continues with 1377), so the comparison is the seven rows before it.
  3. The resumed turn has a state to resume from. The tests expected the second request to restore the first one's 64 tokens. A pass whose history is its whole prompt keeps no state (prompt_cache.zig skips a mark at the prompt's end unless the family warms), so cached was 0. The longer turn is now served twice: the first pass is fresh (cached 0) and keeps a state at its history, the second resumes there, and both must give the reference's tokens. The history is st.prompt_rows tokens, the grid a planned family's states sit on, so the test follows the chunk size if that changes (64 on this head).

With the three corrected, the tests reach the defect this pull request fixes:

Head (each with #4435's read cap picked) zig build test-inkling2-metal-host, both directories set
#4446 (97c91058f) with 52bed2b8b only 76 pass, 2 fail: the two serving tests; the drafted one stops where the resumed turn's tokens are not the reference's
This head 79 of 79 pass
#4618 (ad3d31f9f) with this pull request's two commits, no drafter directory all pass; the tests that need the drafter directory skip
#4618 with this pull request's two commits and #4447's two (819edd9bd, e610dc650) 81 of 81 pass

On #4618's head alone the drafted test's drafted.accepted > 0 fails without #4447: its prompt there is 256 tokens (that head's chunk), and the drafter with #4387's attention scale has no draft accepted on it. With #4447's two commits it passes.

Receipt

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (The corrected serving tests fail on Inkling 2 Mini on Metal: #4387's native build compiles, its norms write their output, and concurrent streams equal solo #4446 without d877d3ce4 and pass with it; they skip without the checkpoint.)
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
  • 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. (A resumed prompt equals a fresh one from this head on; the other two as before.)
  • No precision traded for speed. If the bits change, the description says which sums change and why. (No sums change; a resumed turn's reply changes to the fresh one's.)
  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it. (Neither path is touched.)

ashhart and others added 22 commits November 21, 2026 16:05
Derive per-visit cache access and exact arithmetic for the 48 visits so CUDA admission and kernels have a reference before serving is enabled.
Decode stored NVFP4 and MXFP8 expert weights to BF16 and launch them beside the grouped expert/vision kernels, with exact tensor baselines and planted faults.
Launch cross-pass messages, rank-64 gates, residual steps and split QKV with FP32 reductions, BF16 rounding controls and per-launch resource records.
Run all 48 visits with per-visit cache state, prompt snapshots and exact rollback. Refuse TP, vision and DSpark loading until those engines exist.
… a resumed prompt reads the rows a fresh one wrote
…n before its row, and resume a turn the cache can keep
@sethforprivacy sethforprivacy changed the title Inkling 2 Mini on Metal: a prompt-cache snapshot copies each key-value head's rows, so a resumed turn equals a fresh one Inkling 2 Mini on Metal: a prompt-cache snapshot copies each key-value head's live rows, and the real-checkpoint serving tests run to it Nov 22, 2026
@ashhart
ashhart force-pushed the lead/inkling2-mini branch 3 times, most recently from aefb063 to c41911e Compare November 24, 2026 09:43
@ashhart
ashhart deleted the branch ashhart:lead/inkling2-mini November 24, 2026 09:43
@ashhart ashhart closed this Nov 24, 2026
@ashhart ashhart reopened this Nov 24, 2026
@ashhart
ashhart changed the base branch from lead/inkling2-mini to main November 24, 2026 09:49
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Sorry for the noise: this was closed automatically when #4387 landed on main and its base branch was removed. It is reopened and now targets main. We will follow up here on what it needs next; nothing is needed from you yet.

  • Synthena

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thank you, @sethforprivacy. Every commit this PR adds is now on main inside #4387 with your authorship kept, so it ships with Inkling 2 Mini on Metal in the next release. Closing this one as landed there.

  • Synthena

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