Skip to content

inkling2 metal: batched multi-stream DFlash 2 drafting - #5029

Draft
gilby wants to merge 3 commits into
ashhart:mainfrom
gilby:v4-2-batched-dflash2-rebased
Draft

gilby wants to merge 3 commits into
ashhart:mainfrom
gilby:v4-2-batched-dflash2-rebased

Conversation

@gilby

@gilby gilby commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: stacked on #4917 and #5024, DFlash 2 drafting (absorbRows and hold) processed one stream at a time, re-reading weights per stream and dispatching separate command buffers/kernels when multiple concurrent requests were active, and the batched contract suites appended probes to their owner list before s.kernels.append.

After (stacked on #5024 and #4917):

  • zig/kernels/metal/inkling2/dflash2.metal & dflash2_kernels.zig: Batches tf_ink2_df2_conv, tf_ink2_df2_ctx_kv, tf_ink2_df2_block_qkv, tf_ink2_df2_attend, tf_ink2_df2_attend_merge, and tf_ink2_df2_walk across up to 8 concurrent streams per pass (DF2_MAX_BATCH = 8) with isolated per-stream KV rings and candidate slices.

  • zig/src/families/inkling2/metal/{dflash2,dflash2_prepared}.zig & zig/src/native/inkling2.zig: Batches DFlash 2 absorb and hold rounds so N > 1 streams share BF16 and Q4 linear projections and selector walks in one command buffer with bit-identical results to N = 1 serial runs (+23.1 MiB fixed batch scratch when batch=4).

  • Tests & kernel contracts: Adds dflash2_batch_test.zig, metal_inkling2_dflash2_{attn,batch,batch_controls,walk}.zig, and updates metal_inkling2_dflash2*.zig, metal_inkling2_main.zig, and host_test.zig; makes probe ownership transfer transactional via publishProbe across batch, attention (plain/streams and control cases), and walk (normal and control) suites with std.testing.FailingAllocator coverage in metal_inkling2_dflash2_batch_controls.zig.

  • Issue: Refs Inkling 2 Mini Metal V4: exact-serial scaling, GPU sampling, DFlash 2 acceleration, and kernel fusions #4961

  • Stacked on: inkling2 metal: opt-in --drafter-bits 4 quantization for DFlash 2 #5024 (db54905fa2, V4-1) and inkling2 metal: native DFlash 2 drafter support (DFlash2DraftModel) #4917 (9a4a1619c2); git merge-tree --write-tree origin/main HEAD clean (65b0cd3a5d) against origin/main (f3c0fa8c65).

Checks on this head

  • Head commit: d298c5f89e (16 changed files, 2245 insertions(+), 209 deletions(-))
  • Lean checker, python3 tools/pr_check.py --base db54905fa2 --pr 5029 (python3 tools/zig/lean_check.py): lean 164 vs 164, 0 new, 0 error(s) (16 changed files, max touched file 584 lines <= 600); zig fmt --check build.zig zig and git diff --check db54905fa2..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: 30 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: 144 passed; 6 skipped; 0 failed (includes batched DFlash 2 host/GPU tests in dflash2_batch_test.zig; JPEG and checkpoint env tests skipped).
  • Contracts (./zig-out/bin/tf-kernel-contract):
    • Apple M5 Max (d298c5f89e, --only "metal inkling2 dflash2"): SUMMARY 17 kernels: 17 pass gates 1-4, 0 fail, 0 known findings; 21 controls: 21 red as planted, 0 missed; 0 limit problems; gates 7480 ms, limits 563 ms (exit 0).
    • Apple M5 Max (prior commit on same kernels, --only "metal inkling2"): SUMMARY 78 kernels: 78 pass gates 1-4, 0 fail, 0 known findings; 86 controls: 86 red as planted, 0 missed; 0 limit problems; gates 26189 ms, limits 764 ms (exit 0).
    • Apple M5 Pro (kc_v4_2_mod, prior commit; kernels unchanged): SUMMARY 54 kernels: 54 pass gates 1-4, 0 fail, 0 known findings; 63 controls: 63 red as planted, 0 missed; 0 limit problems; gates 15855 ms, limits 74 ms (exit 0).
  • CI: Rebased onto mergeable inkling2 metal: opt-in --drafter-bits 4 quantization for DFlash 2 #5024 (db54905fa2) and inkling2 metal: native DFlash 2 drafter support (DFlash2DraftModel) #4917 (9a4a1619c2) cleanly against origin/main (f3c0fa8c65); CI checks triggered on force-push.
  • Not run, and why: Apple M1/M2/M3/M4 served A/Bs not directly run on this slice; 2-Mac served A-B-B-A receipts cover Apple M5 Ultra 512 GiB and Apple M5 Pro 64 GiB.

Receipt

  • Main against branch: Matched paired A-B-B-A served runs (tools/bench_concurrent.py --tokens 128 --reps 2, {TARGET} --drafter {DFLASH2} --drafter-bits 4 --parallel 4 --context 8192, 0 cross-leg token_sha256 mismatches across all cells on both machines; 16/16 cells win decode throughput, with 1 TTFT regression on Apple M5 Ultra code T=1.0 c=1 +7.14%):
    • Apple M5 Ultra 512 GiB (unbatched bits=4, fixed_mib=416.8 vs batched fixed_mib=439.9, batch=4, pool 363.2 GiB):
      • code T=0.0 c=1: 32.00 -> 32.25 (+0.78%, TTFT 0.218 -> 0.213s, -2.06%)
      • code T=0.0 c=4: 44.60 -> 47.50 (+6.50%, TTFT 0.528 -> 0.504s, -4.45%)
      • chat T=0.0 c=1: 28.90 -> 30.30 (+4.84%, TTFT 0.504 -> 0.504s, -0.10%)
      • chat T=0.0 c=4: 44.65 -> 47.50 (+6.38%, TTFT 1.796 -> 1.764s, -1.81%)
      • code T=1.0 c=1: 22.20 -> 22.95 (+3.38%, TTFT 0.217 -> 0.232s, +7.14% regression)
      • code T=1.0 c=4: 38.55 -> 40.95 (+6.23%, TTFT 0.512 -> 0.506s, -1.27%)
      • chat T=1.0 c=1: 21.20 -> 22.50 (+6.13%, TTFT 0.506 -> 0.500s, -0.99%)
      • chat T=1.0 c=4: 34.05 -> 36.10 (+6.02%, TTFT 1.802 -> 1.769s, -1.83%)
    • Apple M5 Pro 64 GiB (unbatched bits=4, fixed_mib=416.8 vs batched fixed_mib=439.9, batch=4, pool 27.2 GiB):
      • code T=0.0 c=1: 16.70 -> 17.70 (+5.99%, TTFT 0.552 -> 0.530s, -3.89%)
      • code T=0.0 c=4: 18.80 -> 19.15 (+1.86%, TTFT 1.296 -> 1.276s, -1.58%)
      • chat T=0.0 c=1: 15.40 -> 16.10 (+4.55%, TTFT 1.307 -> 1.288s, -1.42%)
      • chat T=0.0 c=4: 19.10 -> 19.60 (+2.62%, TTFT 5.029 -> 4.973s, -1.10%)
      • code T=1.0 c=1: 12.75 -> 13.50 (+5.88%, TTFT 0.537 -> 0.530s, -1.40%)
      • code T=1.0 c=4: 16.30 -> 16.70 (+2.45%, TTFT 1.332 -> 1.289s, -3.19%)
      • chat T=1.0 c=1: 12.90 -> 13.55 (+5.04%, TTFT 1.336 -> 1.312s, -1.83%)
      • chat T=1.0 c=4: 14.10 -> 14.15 (+0.35%, TTFT 5.288 -> 5.245s, -0.81%)
  • Boxes it reaches: dflash2.metal, dflash2_kernels.zig, dflash2.zig, dflash2_prepared.zig, dflash2_batch_test.zig, host_test.zig, native/inkling2.zig, metal_inkling2_controls_host.zig, metal_inkling2_main.zig, metal_inkling2_dflash2*.zig.
  • New switch? No (always active when serving multiple DFlash 2 streams, up to DF2_MAX_BATCH = 8 per batch).
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: Commit d298c5f89e stacked on inkling2 metal: opt-in --drafter-bits 4 quantization for DFlash 2 #5024 (db54905fa2) and 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 Metal drafting across concurrent streams on Apple Silicon Metal GPUs; Apple M1/M2/M3/M4 served A/Bs not directly run on this slice.
  • Exactness: 0 cross-leg token_sha256 mismatches across all cells (T=0.0,1.0, c=1,4) on both Apple M5 Ultra and Apple M5 Pro; multi-stream kernels maintain bit-identical arithmetic and isolated KV/candidate state vs N = 1 serial execution.
  • Decode speed before and after: Wins 8/8 cells on Apple M5 Ultra (up to +6.50%, c=4 +6.02%..+6.50%) and 8/8 cells on Apple M5 Pro (up to +5.99%, c=4 +0.35%..+2.62%).
  • Prompt speed before and after: TTFT improved in 15/16 cells (up to -4.45% on Apple M5 Ultra and -3.89% on Apple M5 Pro), with 1 regression on Apple M5 Ultra code T=1.0 c=1 (0.217 -> 0.232s, +7.14%).
  • 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: Batches tf_ink2_df2_conv, tf_ink2_df2_ctx_kv, tf_ink2_df2_block_qkv, tf_ink2_df2_attend, tf_ink2_df2_attend_merge, and tf_ink2_df2_walk across DF2_MAX_BATCH = 8 streams in dflash2.metal / dflash2_kernels.zig, replacing single-stream-only dispatch.
  • A planted control for each new kernel, and each one fails as planted: All 21/21 DFlash 2 controls (d298c5f89e, --only "metal inkling2 dflash2") and 86/86 full Inkling 2 controls (prior commit on same kernels) on Apple M5 Max, plus 63/63 controls on Apple M5 Pro (kc_v4_2_mod), fail red as planted (0 missed), including per-stream isolation mutations in metal_inkling2_dflash2_batch_controls.zig.
  • Each backend's SUMMARY line and exit status:
    • Apple M5 Max (d298c5f89e, --only "metal inkling2 dflash2"): SUMMARY 17 kernels: 17 pass gates 1-4, 0 fail, 0 known findings; 21 controls: 21 red as planted, 0 missed; 0 limit problems; gates 7480 ms, limits 563 ms (exit 0).
    • Apple M5 Pro (kc_v4_2_mod, prior commit; kernels unchanged): SUMMARY 54 kernels: 54 pass gates 1-4, 0 fail, 0 known findings; 63 controls: 63 red as planted, 0 missed; 0 limit problems; gates 15855 ms, limits 74 ms (exit 0).
  • Chips I could not run: Apple M1/M2/M3/M4 served A/Bs 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 5024

@gilby

gilby commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

/atc depends 4917

@gilby
gilby force-pushed the v4-2-batched-dflash2-rebased branch from a0c1a56 to ddaba92 Compare November 25, 2026 11:36
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. The batch offsets keep streams separate, and proposals still reach the unchanged target verifier. The posted equality checks are useful; the TTFT regression should remain visible too.

The new contract suites have an allocation-failure ownership bug: after the probe enters the owner list, a failed kernel append frees it locally, then suite cleanup follows the retained pointer. Please make both appends transactional and add a focused failing-allocator case for that sequence, including the attention/walk/control variants. The snapshot combination will also need both sets of state hooks retained.

  • 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-2-batched-dflash2-rebased branch from ddaba92 to c0fd524 Compare November 25, 2026 15:50
@gilby

gilby commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Updated #5029 (head c0fd5240f9, rebased onto updated #5024 755e0476f9 -> #4917 867fcf6e49):

  • Switched metal_inkling2_dflash2_batch.zig, metal_inkling2_dflash2_attn.zig, and metal_inkling2_dflash2_walk.zig (Suite.collect, covering production and control cases) to transactional registerProbe (ensureUnusedCapacity + appendAssumeCapacity on both owner and kernel lists), inheriting the shared parent fix in metal_inkling2_dflash2.zig from inkling2 metal: opt-in --drafter-bits 4 quantization for DFlash 2 #5024.
  • Added std.testing.FailingAllocator coverage in metal_inkling2_dflash2_batch_controls.zig exercising batch, attention (non-control + nonfinite/shapes controls), and walk (normal + control) registration on allocation failure, verifying error.OutOfMemory preserves the owner list, cleans up the candidate once, and tears down with zero leaks or double-frees (zig build test-inkling2-controls-host: all 25 tests pass; Apple M5 Max tf-kernel-contract --only "metal inkling2": 78/78 kernels pass, 86/86 controls red as planted).
  • Kept the Apple M5 Ultra code T=1.0 c=1 TTFT regression (0.217 -> 0.232s, +7.14%) explicitly called out in the receipt summary.

Batch up to 8 concurrent streams per pass across DFlash 2 Metal absorb and hold paths so N > 1 streams share BF16 and Q4 linear projections, multi-ring KV updates and attention, and parallel selector walks in one command buffer with bit-identical results to N = 1 serial runs. Make batch, attention, and walk contract-probe publishing transactional via publishProbe with FailingAllocator coverage. (Refs ashhart#4961)
@gilby
gilby force-pushed the v4-2-batched-dflash2-rebased branch from c0fd524 to d298c5f Compare November 25, 2026 16:31
@gilby

gilby commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Rebased #5029 onto rebased #5024 (db54905fa2) and rebased #4917 (9a4a1619c2, clean merge-tree 65b0cd3a5d against main; head d298c5f89e):

  • Aligned metal_inkling2_dflash2_batch.zig, metal_inkling2_dflash2_attn.zig, metal_inkling2_dflash2_walk.zig, and metal_inkling2_dflash2_batch_controls.zig with inkling2 metal: native DFlash 2 drafter support (DFlash2DraftModel) #4917's transactional publishProbe (ensureUnusedCapacity + appendAssumeCapacity across both lists), alongside the shared metal_inkling2_dflash2.zig fix from inkling2 metal: native DFlash 2 drafter support (DFlash2DraftModel) #4917.
  • Updated the std.testing.FailingAllocator suite in metal_inkling2_dflash2_batch_controls.zig to test publishProbe across batch, attention (plain/streams and nonfinite/shapes controls), and walk (normal and control) sequences (zig build test-inkling2-controls-host: all 30 tests pass; zig build test-inkling2-metal-host: 144 passed, 6 skipped, 0 failed; Apple M5 Max tf-kernel-contract --only "metal inkling2 dflash2": 17/17 kernels pass, 21/21 controls red as planted).
  • Kept the Apple M5 Ultra code T=1.0 c=1 TTFT regression (0.217 -> 0.232s, +7.14%) visible in the PR receipt; CI checks are now triggered cleanly.

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