Skip to content

inkling2 metal: snapshot DSpark lane state for prompt cache - #4542

Open
gilby wants to merge 1 commit into
ashhart:mainfrom
gilby:inkling2-lead-prompt-cache
Open

gilby wants to merge 1 commit into
ashhart:mainfrom
gilby:inkling2-lead-prompt-cache

Conversation

@gilby

@gilby gilby commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: on Inkling 2 Mini on Metal with --drafter, prompt-cache snapshots only saved target KV state, so warm prompt-cache hits dropped DSpark context (lane.from reset to 0) or risked reading another stream's shared tail-tap row at offset 0. After: rebased standalone onto main (head-major target KV snapshot fix is already on main, with no external chain-kernel dependencies), dspark_state.Snap + dspark.resolveTailTap + backend.SavedSnap compactly snapshot and restore each stream's DSpark K/V rings, newest fused row, and exact non-first-stream tail_off tap offset with stale-generation and bounds checks, plus rollback resets on restore failure.

Checks on this head

  • Head commit: 305f2e02de
  • Lean checker, tools/pr_check.py --pr 4542: lean 165 vs 165, 0 new, 4 changed files, 0 error(s).
  • Tests run (305f2e02de, Zig 0.17.0 -Dcpu=apple_m1 -Doptimize=ReleaseFast):
    • zig fmt --check build.zig zig and git diff --check origin/main..HEAD — exit 0.
    • zig build test-inkling2-metal-host test-inkling2-host --summary all (Apple M5 Max): 8/8 steps succeeded; 148/158 tests passed (10 skipped) (test-inkling2-host 45 pass, 4 skip; test-inkling2-metal-host 103 pass, 6 skip, including resolveTailTap preserves non-first-stream tap offset and validates generation and bounds and DSpark snapshot preserves non-first-stream tail tap byte offset and rejects out-of-range offsets).
    • tf-kernel-contract --only "metal inkling2":
      • Apple M5 Ultra 512 GiB: SUMMARY 61 kernels: 61 pass gates 1-4, 0 fail, 0 known findings; 65 controls: 65 red as planted, 0 missed; 0 limit problems (gates 9312 ms, limits 395 ms).
      • Apple M3 Ultra 512 GiB (evaluated with symmetric 2-line thread qualifier overlay on pre-existing prompt_attention.metal lines 11 and 15 for Metal 3): SUMMARY 61 kernels: 61 pass gates 1-4, 0 fail, 0 known findings; 66 controls: 66 red as planted, 0 missed; 0 limit problems (gates 11456 ms, limits 97 ms).
  • CI: Pending GitHub Actions on rebased head 305f2e02de.
  • Not run, and why: Non-Apple GPU backends (Metal-only change).

Receipt

  • main (a19197631f/3795e6054c) against branch (305f2e02de) on Inkling-2-Mini (ed40e7da625f594fc9c4990f5f66be24873c22d7), --parallel 4 --context 8192 --prompt-cache-gib 16, T=0.0, verifying single-stream 3-turn + branching chat AND concurrent 2-stream (c=2) non-first-stream (slot >= 1) warm save & restore against cold --no-drafts baseline:
    • Apple M5 Ultra 512 GiB (base_plain, new_plain, new_drafter all all_sha_match=True, all_warm_hits=True):
      • Single-stream (t1 prompt=1185 -> t2 prompt=1200 -> branched t3 prompt=1202):
        • t1 (cached=0): sha=e4a37eb322a0, ttft=12.886s, accepted=18/28, 22.9 tok/s
        • t2 (cached=1174): sha=c8a9d32679df (100% bit-identical to cold --no-drafts), ttft=0.328s (39.3x faster TTFT), accepted=18/26, 23.3 tok/s
        • t3 (cached=1174): sha=bd9b86e1fc2d (100% bit-identical to cold --no-drafts), ttft=0.348s (37.2x faster TTFT vs cold t3), accepted=17/27, 21.7 tok/s
      • Concurrent 2-stream non-first-stream save & restore (slotA 1395->1412, slotB 1473->1489):
        • slotA.t2 (cached=1386): sha=ee4178bda92a (exact match), prefill=0.327s, accepted=21/64
        • slotB.t2 (cached=1461): sha=1a8e6495980e (exact match), ttft=0.349s, accepted=21/66
      • Longer 6.6K multi-turn /v1/chat/completions (--context 16384): t1 (6632, cached=0, ttft=73.046s, accepted=19/39) -> t2 (6646, cached=6619, ttft=0.375s, 194.8x faster TTFT, accepted=20/37, 18.4 tok/s, exact sha=a4a25beb2197) -> t3 (6646, cached=6619, ttft=0.375s, accepted=20/37, 18.4 tok/s, exact sha=55c9d992a9bf).
    • Apple M3 Ultra 512 GiB (Metal 3, symmetric 2-line thread qualifier overlay on pre-existing prompt_attention.metal lines 11 and 15; base_plain, new_plain, new_drafter all all_sha_match=True, all_warm_hits=True):
      • Single-stream (t1 prompt=1185 -> t2 prompt=1200 -> t3 prompt=1202):
        • t1 (cached=0): sha=2ed647ae1997, ttft=29.349s, accepted=20/27, 18.2 tok/s
        • t2 (cached=1174): sha=c8a9d32679df (100% bit-identical to cold --no-drafts), ttft=0.661s (44.4x faster TTFT), accepted=18/26, 16.4 tok/s
        • t3 (cached=1174): sha=aca91013de0b (100% bit-identical to cold --no-drafts), ttft=0.706s (39.1x faster TTFT), accepted=16/25, 14.9 tok/s
      • Concurrent 2-stream non-first-stream save & restore (slotA 1395->1412, slotB 1473->1489):
        • slotA.t2 (cached=1386): sha=7da1cb791ff2 (exact match), prefill=0.661s, accepted=21/70
        • slotB.t2 (cached=1461): sha=a57ccadeca5a (exact match), ttft=0.707s, accepted=20/70
  • Boxes it reaches: inkling2 Metal backend.zig, dspark.zig, dspark_state.zig, native/inkling2.zig.
  • New switch? No.
  • Environment: Rebased head 305f2e02de on main (3795e6054c), Zig 0.17.0 -Dcpu=apple_m1 -Doptimize=ReleaseFast, Apple M5 Max / Apple M5 Ultra 512 GiB / Apple M3 Ultra 512 GiB, Inkling-2-Mini ed40e7da625f594fc9c4990f5f66be24873c22d7.
  • Paths and platforms the change reaches: Metal inkling2 prompt cache save/restore (plain and --drafter) across single-stream and concurrent streams.
  • Exactness: 100% token_sha256 equality across cold --no-drafts, base_plain, new_plain, and new_drafter for single-stream, branched, and concurrent c=2 non-first-stream save & restore on both M5 Ultra and M3 Ultra. (Note raw /v1/completions tools/bench_cache.py keeps 0 states by design because inkling2 uses .grid = st.prompt_rows, .tail = false, .repeat_min = 0.)
  • Decode speed before and after: Multi-turn warm chat preserves drafted decode speed (21.7–23.3 tok/s on M5 Ultra vs 14.6 tok/s plain; 14.9–16.4 tok/s on M3 Ultra vs 10.8 tok/s plain).
  • Prompt speed before and after: Warm multi-turn /v1/chat/completions TTFT 12.886s -> 0.328s (~1.2K) / 73.046s -> 0.375s (6.6K) on M5 Ultra, and 29.349s -> 0.661s (~1.2K) on M3 Ultra.
  • Licence of any new weights or drafter: n/a.

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.
  • 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.
  • No precision traded for speed.
  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled.

@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 force-pushed the inkling2-lead-prompt-cache branch from 2e0f59e to 6ac6cb3 Compare November 24, 2026 11:57
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. Main already retains the head-major target snapshot fix.

Two issues remain in this slice: its drafter loads chain kernels that the standalone tree does not provide, and snapshot saving drops the byte offset of a shared tail-tap row, so a later stream can save the first stream's row instead. Please keep the snapshot change independent or supply its complete dependency, and preserve the tail view's offset with a non-first-stream restore check.

  • Synthena

@gilby
gilby force-pushed the inkling2-lead-prompt-cache branch from c12c6ab to 305f2e0 Compare November 25, 2026 01:36
@gilby
gilby marked this pull request as ready for review November 25, 2026 01:36
@gilby gilby changed the title Inkling 2 Mini on Metal: fix head-major KV snapshot copy and snapshot DSpark lane state for prompt cache inkling2 metal: snapshot DSpark lane state for prompt cache Nov 25, 2026
@gilby

gilby commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Both review items from your previous comment are now addressed on 305f2e02de (rebased onto main 3795e6054c), and this PR is marked Ready for Review:

  1. Standalone dependency / main rebase: Rebased directly onto main (3795e6054c) as a 4-file change (backend.zig, dspark.zig, dspark_state.zig, native/inkling2.zig) — no missing chain-kernel dependency (pr_check.py --pr 4542: 0 errors, lean 165 vs 165, 0 new).
  2. Non-first-stream shared tail-tap byte offset & verification:
    • dspark.resolveTailTap validates tap.gen, computes tap.off + (lane_end - tap.start) * tap_row_bytes, checks bounds, and passes the exact offset to dspark_state.Snap.save.
    • Unit tests (zig build test-inkling2-metal-host: 103 pass, 6 skip on Apple M5 Max) cover resolveTailTap preserves non-first-stream tap offset and validates generation and bounds and DSpark snapshot preserves non-first-stream tail tap byte offset and rejects out-of-range offsets.
    • End-to-end live concurrent c=2 non-first-stream (slot >= 1) warm save & restore (--prompt-cache-gib 16 --drafter, comparing base_plain, new_plain, and new_drafter against cold --no-drafts) passed 100% bit-identical token_sha256 across both single-stream (1174 cached), branched turns (1174 cached), and concurrent 2-stream slotA (1386 cached) + slotB (1461 cached) on Apple M5 Ultra 512 GiB (61/61 contracts, 18/26–21/66 accepted drafts, 0.328s–0.349s TTFT) and Apple M3 Ultra 512 GiB (with symmetric 2-line thread qualifier overlay on pre-existing prompt_attention.metal, 61/61 contracts, 16/25–21/70 accepted drafts, 0.661s–0.707s TTFT).

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. Both earlier issues are closed: this is now an independent snapshot change, and the shared tail view’s byte offset is preserved and checked. The new copy tests and reported non-first-stream concurrent restores cover the missing case. The M3 shader overlay is clearly identified as a limit on that receipt.

This head merges cleanly and is ready to land through the normal final-tree checks. The target snapshot fix already on main remains separate from this drafter-state improvement.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

I checked the landing interaction with the other Inkling changes. On 4c79cfe346, #4724 (b918bc4b61), #4990 (8f85e2f4a2), and this PR (305f2e02de) merge cleanly in that order.

zig build test-inkling2-metal-host test-inkling2-host test-inkling2-metal-capabilities -Dfixed-cpu -j2 --summary all

The combined tree passed 158 tests with ten skips on the M5 Max, with all 71 build steps successful. That includes the non-first-stream snapshot copy and Metal capability checks. The combined checkpoint-backed serving run remains unverified.

Igor Lessio - AIFLOW LABS

This branch has not been deployed

No deployments
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.

3 participants