Skip to content

inkling2 metal: snapshot DFlash 2 lane state for prompt cache - #5032

Draft
gilby wants to merge 2 commits into
ashhart:mainfrom
gilby:v4-5-dflash2-prompt-cache-rebased
Draft

gilby wants to merge 2 commits into
ashhart:mainfrom
gilby:v4-5-dflash2-prompt-cache-rebased

Conversation

@gilby

@gilby gilby commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: stacked on #4917 (alongside the generic prompt-cache drafter snapshot contract from #4542), DFlash 2 had no prompt-cache snapshot/restore support (prepare only saved DSpark lane state when active), causing warm prompt-cache hits under --drafter <DFlash2> to miss DFlash 2 lane state restoration or skip context snapshot reuse.

After (stacked on #4917, commit 9a4a1619c2):

Checks on this head

  • Head commit: 228614678b (8 changed files, 1202 insertions(+), 171 deletions(-))
  • Lean checker, python3 tools/pr_check.py --base 9a4a1619c2 --pr 5032 (python3 tools/zig/lean_check.py): lean 164 vs 164, 0 new, 0 error(s) (8 changed files, max touched file 591 lines <= 600); zig fmt --check build.zig zig and git diff --check 9a4a1619c2..HEAD exit 0.
  • Builds & tests (-Dcpu=apple_m1 -Doptimize=ReleaseFast):
    • zig build -Dcpu=apple_m1 -Doptimize=ReleaseFast: exit 0.
    • zig build compile-inkling2-native-host: exit 0; zig build compile-inkling2-native-host -Dtarget=x86_64-windows: exit 0; zig build compile-native-cuda-windows: exit 0.
    • zig build test-inkling2-host -Dcpu=apple_m1 -Doptimize=ReleaseFast: 45 passed; 4 skipped; 0 failed (exit 0).
    • zig build test-inkling2-controls-host: 27 tests passed (0 failed); zig build test-kernel-contract: exit 0; zig build tf-kernel-contract -Doptimize=ReleaseFast: exit 0.
    • zig build test-inkling2-metal-host -Dcpu=apple_m1 -Doptimize=ReleaseFast test binary: 118 passed; 6 skipped; 0 failed (includes dflash2_snap_test.zig; JPEG and checkpoint env tests skipped).
  • Contracts (./zig-out/bin/tf-kernel-contract):
    • Apple M5 Max (228614678b, --only "metal inkling2 dflash2"): SUMMARY 11 kernels: 11 pass gates 1-4, 0 fail, 0 known findings; 13 controls: 13 red as planted, 0 missed; 0 limit problems; gates 62 ms, limits 58 ms (exit 0).
    • Apple M5 Max (prior commit on same kernels, --only "metal inkling2"): SUMMARY 72 kernels: 72 pass gates 1-4, 0 fail, 0 known findings; 78 controls: 78 red as planted, 0 missed; 0 limit problems; gates 14654 ms, limits 736 ms (exit 0).
  • CI: Rebased onto updated inkling2 metal: native DFlash 2 drafter support (DFlash2DraftModel) #4917 (9a4a1619c2, inheriting transactional publishProbe + OOM test); force-pushed cleanly.
  • Not run, and why: Adds no new .metal kernels (snapshot/restore operates on DFlash 2 buffers from inkling2 metal: native DFlash 2 drafter support (DFlash2DraftModel) #4917); Apple M1/M2/M3/M4 live prompt-cache runs not directly executed; 2-Mac live prompt-cache verification covers Apple M5 Ultra 512 GiB and Apple M5 Pro 64 GiB.

Receipt

  • Main against branch: Live prompt-cache & concurrent determinism suite (base_plain vs new_plain vs new_drafter with --drafter {DFLASH2}, comparing against cold no-cache reference cold_ref_summary; all_sha_match: true, all_warm_hits: true across every suite on both machines):
    • Apple M5 Ultra 512 GiB:
      • base_plain (all_sha_match=true, all_warm_hits=true):
        • single: t1 dt=14.997s (cached=0, ttft=12.834s, 14.58 tok/s), t2 dt=2.451s (cached=1174, ttft=0.312s, 14.57 tok/s), t3 dt=2.483s (cached=1174, ttft=0.331s, 14.57 tok/s).
        • c=2: slotA t1 dt=33.943s, t2 dt=3.589s (cached=1386, ttft=0.640s, 10.64 tok/s); slotB t1 dt=33.943s, t2 dt=3.589s (cached=1461, ttft=0.333s, 9.63 tok/s).
      • new_plain (all_sha_match=true, all_warm_hits=true):
        • single: t1 dt=14.988s (cached=0, ttft=12.845s, 14.58 tok/s), t2 dt=2.446s (cached=1174, ttft=0.309s, 14.59 tok/s), t3 dt=2.471s (cached=1174, ttft=0.329s, 14.57 tok/s).
        • c=2: slotA t1 dt=34.008s, t2 dt=3.575s (cached=1386, ttft=0.318s, 9.57 tok/s); slotB t1 dt=34.008s, t2 dt=3.575s (cached=1461, ttft=0.645s, 10.64 tok/s).
      • new_drafter (--drafter {DFLASH2}, all_sha_match=true, all_warm_hits=true):
        • single: t1 dt=14.310s (cached=0, ttft=12.881s, 21.88 tok/s), t2 dt=1.326s (cached=1174, ttft=0.318s, 31.11 tok/s), t3 dt=1.460s (cached=1174, ttft=0.336s, 28.23 tok/s).
        • c=2: slotA t1 dt=32.604s, t2 dt=2.374s (cached=1386, ttft=0.653s, 18.15 tok/s); slotB t1 dt=32.782s, t2 dt=2.184s (cached=1461, ttft=0.339s, 16.92 tok/s).
    • Apple M5 Pro 64 GiB:
      • base_plain (all_sha_match=true, all_warm_hits=true):
        • single: t1 dt=41.312s (cached=0, ttft=38.730s, 12.14 tok/s), t2 dt=3.418s (cached=1174, ttft=0.829s, 12.11 tok/s), t3 dt=3.472s (cached=1174, ttft=0.885s, 12.12 tok/s).
        • c=2: slotA t1 dt=98.464s, t2 dt=5.726s (cached=1386, ttft=0.829s, 6.38 tok/s); slotB t1 dt=98.463s, t2 dt=5.726s (cached=1461, ttft=1.712s, 7.80 tok/s).
      • new_plain (all_sha_match=true, all_warm_hits=true):
        • single: t1 dt=41.213s (cached=0, ttft=38.594s, 12.14 tok/s), t2 dt=3.418s (cached=1174, ttft=0.827s, 12.13 tok/s), t3 dt=3.468s (cached=1174, ttft=0.885s, 12.13 tok/s).
        • c=2: slotA t1 dt=97.943s, t2 dt=5.712s (cached=1386, ttft=1.712s, 7.81 tok/s); slotB t1 dt=97.943s, t2 dt=5.711s (cached=1461, ttft=0.887s, 6.46 tok/s).
      • new_drafter (--drafter {DFLASH2}, all_sha_match=true, all_warm_hits=true):
        • single: t1 dt=41.159s (cached=0, ttft=38.869s, 13.70 tok/s), t2 dt=2.626s (cached=1174, ttft=0.845s, 17.71 tok/s), t3 dt=2.942s (cached=1174, ttft=0.903s, 15.44 tok/s).
        • c=2: slotA t1 dt=97.533s, t2 dt=5.432s (cached=1386, ttft=0.849s, 6.80 tok/s); slotB t1 dt=97.843s, t2 dt=5.008s (cached=1461, ttft=1.750s, 9.61 tok/s).
  • Boxes it reaches: dflash2_state.zig, dflash2_snap_test.zig, dflash2.zig, dspark_state.zig, backend.zig, state.zig, host_test.zig, native/inkling2.zig.
  • New switch? No (active whenever --drafter <DFlash2> runs with prompt cache enabled).
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: Commit 228614678b stacked on inkling2 metal: native DFlash 2 drafter support (DFlash2DraftModel) #4917 (9a4a1619c2), Zig 0.17.0 -Dcpu=apple_m1 -Doptimize=ReleaseFast, Apple M5 Max (applegpu_g17), Apple M5 Ultra 512 GiB, Apple M5 Pro 64 GiB; checkpoint Inkling-2-Mini and DFlash 2 drafter (incoai/Inkling-2-Mini-DFlash2).
  • Paths and platforms the change reaches, which I ran and which I could not: Inkling 2 Mini DFlash 2 prompt-cache snapshot/restore across Apple Silicon Metal GPUs; Apple M1/M2/M3/M4 not directly run on this slice.
  • Exactness: all_sha_match=true and all_warm_hits=true across every turn and concurrent slot (single t1..t3, concurrent_c2 slotA/slotB t1..t2) on both Apple M5 Ultra and Apple M5 Pro against the cold no-cache baseline.
  • Decode speed before and after: Unlocks warm prompt-cache DFlash 2 decode (single warm t2/t3: 14.59 -> 31.11 / 28.23 tok/s on M5 Ultra and 12.13 -> 17.71 / 15.44 tok/s on M5 Pro, cutting warm turn elapsed dt_s from 2.446s -> 1.326s / 3.575s -> 2.184s (c=2) on M5 Ultra and 3.418s -> 2.626s / 5.711s -> 5.008s (c=2) on M5 Pro) while preserving plain parity.
  • Prompt speed before and after: cached_tokens (1174, 1386, 1461) resume identically between plain and DFlash 2 warm turns.
  • Licence of any new weights or drafter: n/a.
Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract, and the kernel each one replaces: Adds no new .metal compute kernels (snapshots DFlash 2 device state buffers from inkling2 metal: native DFlash 2 drafter support (DFlash2DraftModel) #4917).
  • A planted control for each new kernel, and each one fails as planted: All 13/13 DFlash 2 controls (228614678b, --only "metal inkling2 dflash2") and 78/78 full Inkling 2 controls (prior commit on same kernels) on Apple M5 Max fail red as planted (0 missed).
  • Each backend's SUMMARY line and exit status:
    • Apple M5 Max (228614678b, --only "metal inkling2 dflash2"): SUMMARY 11 kernels: 11 pass gates 1-4, 0 fail, 0 known findings; 13 controls: 13 red as planted, 0 missed; 0 limit problems; gates 62 ms, limits 58 ms (exit 0).
  • Chips I could not run: Apple M1/M2/M3/M4 not directly run on this slice.

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.

@gilby

gilby commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

/atc depends 4917

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. The snapshot slice preserves ring positions, handles the pending tail tap and charges drafter bytes alongside target state. The early-mark horizon and non-first-slot checks are useful.

The shared parent still has a contract-probe ownership bug: a failed kernel-list append can free a probe already retained by the suite, which cleanup then follows again. Please make that ownership transfer transactional and cover the failing allocation. The full head needs that repair; the batching combination must also retain the snapshot hooks and mark horizon alongside its Q4/batch paths.

  • Synthena

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

@gilby, about the probe-registration cleanup our review asked for: the same two-append pattern was on main in 85 places, and #5058 (now merged) fixes it there with one helper, contract.register. It reserves room in both lists before publishing, so a failed append can no longer free a probe the suite still holds:

errdefer p.deinit(); // with errdefer s.a.destroy(p) after create, as the suite's teardown frees it
try ct.register(s.a, &s.probes, &s.kernels, p, p.kernel());

Using it for this PR's new suites closes that item. zig/src/core/contract/register_test.zig shows the failure cases it covers.

  • Synthena

@gilby
gilby force-pushed the v4-5-dflash2-prompt-cache-rebased branch from 4484a13 to 70de70f Compare November 25, 2026 16:00
@gilby

gilby commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Updated #5032 (head 70de70f59c, stacked on #4917 867fcf6e49):

  • Added transactional registerProbe (ensureUnusedCapacity + appendAssumeCapacity on both probes and kernels) in metal_inkling2_dflash2_controls.zig and switched metal_inkling2_dflash2.zig (Suite.addKernel and Suite.addControl) to use it, preventing a failed kernel-list append from leaving a freed probe retained by Suite.
  • Added std.testing.FailingAllocator regression coverage in metal_inkling2_dflash2_controls.zig for normal and control probe OOM cleanup, verifying error.OutOfMemory preserves existing probes, cleans up the candidate once, and completes teardown with zero leaks or double-frees (zig build test-inkling2-controls-host: all 22 tests pass; Apple M5 Max tf-kernel-contract --only "metal inkling2": 72/72 kernels pass, 78/78 controls red as planted).
  • The full combined branch will also retain both the prompt-cache snapshot hooks / mark horizon and the transactional probe registration across its Q4/batch suites.

@gilby
gilby force-pushed the v4-5-dflash2-prompt-cache-rebased branch from 70de70f to 2286146 Compare November 25, 2026 16:43
@gilby

gilby commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased #5032 onto updated #4917 (9a4a1619c2, clean merge-tree b7fb81ecc9 against f3c0fa8c65; head 228614678b):

  • Inherited inkling2 metal: native DFlash 2 drafter support (DFlash2DraftModel) #4917's transactional publishProbe and std.testing.FailingAllocator regression tests directly in metal_inkling2_dflash2.zig and metal_inkling2_dflash2_controls.zig.
  • Kept only the V4-5 snapshot/restore changes (including the dense_mma = null target Model test initialization update in dflash2_snap_test.zig and preserving dspark.warm in native/inkling2.zig).
  • Verified zig build test-inkling2-controls-host (all 27 tests pass), zig build test-inkling2-metal-host (118 passed, 6 skipped, 0 failed), and Apple M5 Max ./zig-out/bin/tf-kernel-contract --only "metal inkling2 dflash2" (11/11 kernels pass, 13/13 controls red as planted).
  • The full combined branch will also retain both the prompt-cache snapshot hooks / mark horizon and the transactional publishProbe + FailingAllocator coverage across its Q4/batch suites.

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