Skip to content

inkling2 metal: fuse router at rows > 32, bfloat4 conv_dynamic, loop RMS hoist, 2D bank_and_reset, and 32-row logits_all cap - #4594

Open
gilby wants to merge 42 commits into
ashhart:mainfrom
gilby:inkling2-lead-prefill-exact
Open

gilby wants to merge 42 commits into
ashhart:mainfrom
gilby:inkling2-lead-prefill-exact

Conversation

@gilby

@gilby gilby commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Rebases the 0-ULP prefill and scratch pieces onto #4618 (row-gated fused router, bfloat4 conv_dynamic, loop RMS hoist, 2D bank_and_reset, and 32-row logits_all cap) with zero standalone decode regression.

What this changes

Keeps 16-TG parallel tf_inkling2_route_scores + 32-thread zero-barrier tf_inkling2_route_select for decode/verify (rows <= 32) while switching rows > 32 to the single 256-thread fused tf_inkling2_router kernel (512 B TG, shared ink_select_top8), restores 256-thread bfloat4 vectorized tf_inkling2_conv_dynamic (128 B TG), hoists looped-attention bank RMS into tf_inkling2_loop_rms + cached bank lanes in tf_inkling2_loop_messages, vectorizes bankAndReset via 2D tf_ink2_bank_and_reset, records all loop (rms, messages, gates, zeta, blend) and glue (route_scores, route_select, plus the 9 main glue kernels) Metal launches in inkling2_loop_metal_records.zig / inkling2_glue_metal_records.zig + loop_plan.zig / glue_plan.zig / loop_test.zig / glue_test.zig, and caps logits_all at batch_rows (32 rows, saving ~179 MiB scratch) while leaving prompt_attention.metal and prompt_rows = 256 untouched.

Checks on this head

  • Head commit: 9d54e30c29 (records tf_inkling2_loop_rms, tf_inkling2_loop_zeta, tf_inkling2_loop_blend, tf_inkling2_route_scores, and tf_inkling2_route_select in contract launch records and plan inventories)
  • Lean checker, python3 tools/zig/lean_check.py: 167 problems (0 new vs base ad3d31f9ff); tools/pr_check.py --pr 4594 --base ad3d31f9ff: 0 error(s), 1 warning(s) (changelog reminder).
  • Tests run:
    • Host CPU & cross-compile checks on 9d54e30c29: zig fmt --check exit 0; git diff --check exit 0; test-inkling2-controls-host 15/15 pass; test-inkling2-host 44 pass, 4 skip; test-inkling2-metal-host (CPU) 76 pass, 5 skip (verifies all 5 loop and 11 glue Launch records 1-to-1 against kernel void counts and symbol/thread/tg_memory specs); test-metal-launch-map-host 8/8 pass (1215 registered launches, 2 controls, 0 missing); compile-inkling2-native-host (macOS/Linux/Windows) pass; test-inkling2-metal-host compiles cleanly (0 errors) for x86_64-linux and x86_64-windows.
    • M5 Max Metal kernel contract & offline M1/M2 compiler checks on 9d54e30c29:
      • ./zig-out/bin/tf-kernel-contract --only "metal inkling2": 47/47 pass gates 1-4, 47/47 controls red, 0 limit problems (gates 6323 ms, limits 12 ms).
      • ./zig-out/bin/tf-kernel-contract --no-gates --offline <dir> --only "metal inkling2 loop" and --only "metal inkling2 glue": all 5 loop launches (rms=256, messages=256, gates=256, zeta=256, blend=32) and all 11 glue launches (including route_scores=256, route_select=32) pass M1/M2 offline reservation compilation (0 limit problems).
    • Real-checkpoint test-inkling2-metal-host (TF_INKLING2_METAL_DIR set to Inkling-2-Mini-ed40e7da, ReleaseFast -Dcpu=apple_m1): 80 passed; 3 skipped; 0 failed on Apple M5 Ultra on 3248767ddc (and M5 Pro / M3 Ultra on parent d42b52c884; 3 skips are DSpark-gated tests when TF_INKLING2_DSPARK_DIR is unset).
  • CI: Host checks, Linux/Windows cross-compilation, M5 Max contract (47/47 + 47 red) + offline M1/M2 limits (0 limit problems) passed on 9d54e30c29, and full M5 Ultra contract/real-ckpt/serving/cache runs passed on 3248767ddc (only [[max_total_threads_per_threadgroup(32)]] attribute on tf_inkling2_loop_blend and host/contract launch inventory tests changed in 9d54e30c29; M5 Pro and M3 Ultra baseline runs were captured at d42b52c884).
  • Not run, and why: Non-Apple backends; all 4 Apple Silicon hardware tiers (M5 Max, M5 Ultra, M5 Pro, M3 Ultra) ran on real hardware.

Receipt

  • Base against branch (Apple M5 Ultra / M5 Pro / M3 Ultra, Inkling-2-Mini-ed40e7da, plain --no-drafts vs #4618 ad3d31f9ff, measured at 3248767ddc on M5 Ultra and d42b52c884 on M5 Pro/M3 Ultra):
    • Apple M5 Ultra (3248767ddc, bench_concurrent.py --levels 1,4,16 --temperatures 0,1.0 --tokens 128 --reps 2 --alone):
      • code T=0.0: c=1 57.3 vs 53.6 (+6.9%), c=4 198.0 (st=207.7) vs 199.1, c=16 492.9 (st=568.7) vs 491.2 tok/s (sha=4490e6bbcfbd exact match, alone_unequal=0)
      • chat T=0.0: c=1 57.7 vs 57.6, c=4 197.7 (st=206.2) vs 196.8, c=16 445.6 (st=544.5) vs 452.7 tok/s (sha=32ddb869bec4 exact match, alone_unequal=0)
      • code T=1.0: c=1 34.9 vs 34.4, c=4 58.3 (st=59.4) vs 57.8, c=16 70.1 (st=71.6) vs 69.4; chat T=1.0: c=1 34.9 vs 34.7, c=4 59.7 (st=60.2) vs 59.4, c=16 72.7 (st=73.8) vs 71.9 (exact sha set match)
      • bench_cache.py --tokens 64 --repeats 2 --streams 4 --require-cold (pt=8704): qualified=True, cold ttft_s=8.7662s (rep_min=8.6760s) vs 8.7536s ref, dec=48.4 (rep_max=48.4) vs 48.5 tok/s (sha=95e612cfdcaf exact match)
    • Apple M5 Pro 20-core (measured at parent d42b52c884 prior to 3248767ddc, bench_concurrent.py --levels 1,4 --temperatures 0,1.0 --tokens 128 --reps 2 --alone):
      • code T=0.0: c=1 31.2 vs 31.8, c=4 110.6 (st=117.9) vs 110.7 (st=118.0, sha=4490e6bbcfbd exact match, alone_unequal=0)
      • chat T=0.0: c=1 30.5 vs 31.5, c=4 101.8 (st=113.9) vs 102.6 (st=114.6, sha=32ddb869bec4 exact match, alone_unequal=0)
      • code T=1.0: c=1 22.7 vs 22.5, c=4 38.4 (st=39.4) vs 39.1; chat T=1.0: c=1 22.9 vs 23.1, c=4 40.0 (st=41.4) vs 40.2 (exact sha set match)
      • bench_cache.py --tokens 32 --repeats 2 --streams 2 --require-cold (pt=2571): qualified=True, cold ttft_s=12.4280s (rep_min=12.5297s) vs 12.6426s ref, dec=29.3 (rep_max=28.6) vs 29.6 (sha=1fbe89b22692 exact match)
    • Apple M3 Ultra (Metal 3, measured at parent d42b52c884 prior to 3248767ddc, bench_concurrent.py --levels 1,4 --temperatures 0,1.0 --tokens 128 --reps 2 --alone):
      • code T=0.0: c=1 43.4 vs 45.4, c=4 104.2 (st=109.2) vs 106.7 (sha=4490e6bbcfbd exact match, alone_unequal=0)
      • chat T=0.0: c=1 43.4 vs 45.3, c=4 99.8 (st=106.4) vs 100.9 (sha=34819971792d exact match, alone_unequal=0)
      • code T=1.0: c=1 27.1 vs 28.3, c=4 42.3 (st=43.2) vs 43.9; chat T=1.0: c=1 27.4 vs 28.4, c=4 42.1 (st=43.0) vs 43.5 (exact sha set match)
      • bench_cache.py --tokens 32 --repeats 2 --streams 2 --require-cold (pt=2571): qualified=True, cold ttft_s=8.5404s (rep_min=8.5223s) vs 8.7351s ref (-0.195s), dec=36.1 (rep_max=35.1) vs 37.6 (sha=1fbe89b22692 exact match)
  • Boxes it reaches: Apple Silicon Metal (M5 Max, M5 Ultra, M5 Pro, and M3 Ultra all verified).
  • New switch? None (router_fused_min_rows = 33 selects 16-TG route_scores + 32-thread route_select for rows <= 32 and fused 256-thread router for rows > 32; prompt_rows = 256 and batch_rows = 32 unchanged).
  • Environment: Commits d42b52c884, 3248767ddc, and 9d54e30c29 on ad3d31f9ff, Zig 0.17.0 -Dcpu=apple_m1 -Doptimize=ReleaseFast, Apple M5 Max (applegpu_g17), Apple M5 Ultra (applegpu_g17), Apple M5 Pro (applegpu_g17), Apple M3 Ultra (applegpu_g15), checkpoint Inkling-2-Mini-ed40e7da.
  • Paths and platforms the change reaches: Inkling 2 Mini Metal runtime (router, conv_dynamic, loop_messages, bank_and_reset, logits_all scratch sizing) across all 4 Apple Silicon hardware tiers.
  • Exactness: alone_unequal=0 across all T=0.0 and T=1.0 cells; 100% T=0.0, T=1.0, and cache token_sha parity against #4618 across M5 Ultra, M5 Pro, and M3 Ultra.
  • 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 replacements:
    • Updated tf_inkling2_router (rows <= 32 runs 256-thread tf_inkling2_route_scores + 32-thread zero-barrier tf_inkling2_route_select with 512 B TG; rows > 32 runs fused 256-thread tf_inkling2_router with 512 B TG; contract tested at rows = 1, 5, 64).
    • Updated tf_inkling2_conv_dynamic (256 threads, 128 B TG, bfloat4 Stage-1 dot).
    • Updated tf_inkling2_loop_messages (256 threads, 80 B TG, replaces 144 B TG) paired with tf_inkling2_loop_rms (256 threads, 64 B TG), plus full Launch limit records for loop_gates (256 threads, 80 B TG), loop_zeta (256 threads, 0 B TG), and loop_blend (32 threads, 0 B TG).
    • Added tf_ink2_bank_and_reset (256 threads, 0 B TG, replaces per-row copyBf16 loop + prelude restore).
  • Planted controls and status: Existing tf_inkling2_conv_dynamic, tf_inkling2_router, tf_inkling2_loop_messages, and new tf_ink2_bank_and_reset controls all fail red as planted on M5 Max, M5 Ultra, M5 Pro, and M3 Ultra.
  • Backend SUMMARY lines and exit status:
    • M5 Max (applegpu_g17, 9d54e30c29): SUMMARY 47 kernels: 47 pass gates 1-4, 0 fail, 0 known findings; 47 controls: 47 red as planted, 0 missed; 0 limit problems; gates 6323 ms, limits 12 ms (exit 0).
    • M5 Ultra (applegpu_g17, 3248767ddc): SUMMARY 47 kernels: 47 pass gates 1-4, 0 fail, 0 known findings; 47 controls: 47 red as planted, 0 missed; 0 limit problems; gates 5727 ms, limits 383 ms (exit 0).
    • M5 Pro (applegpu_g17): SUMMARY 47 kernels: 47 pass gates 1-4, 0 fail, 0 known findings; 47 controls: 47 red as planted, 0 missed; 0 limit problems; gates 5654 ms, limits 381 ms (exit 0).
    • M3 Ultra (applegpu_g15): SUMMARY 42 kernels: 42 pass gates 1-4, 0 fail, 0 known findings; 42 controls: 42 red as planted, 0 missed; 0 limit problems; gates 6058 ms, limits 9 ms (exit 0).
  • Chips not yet run: None (all 4 Apple Silicon hardware tiers completed).

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. If the bits change, the description says which sums change and why.
  • 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 and others added 30 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.
@gilby
gilby force-pushed the inkling2-lead-prefill-exact branch from 50bcdea to c741dcd Compare November 22, 2026 19:13
@gilby gilby changed the title inkling2 metal: widen units to 32 pairs, fuse sliding prompt finish, size prompt part_o by capacity, and widen glue/loop scratch to 512 inkling2 metal: fuse router, hoist loop RMS, vectorize prompt attention and conv_dynamic, and widen to 512 rows Nov 22, 2026
…2D bank_and_reset, and 32-row logits_all cap

- conv_dynamic: dispatch 32 threads with bfloat4 sequential dot and simd_shuffle (0 B TG)
- router: fuse route_scores and router into one 256-thread launch with 512 B TG and ascending tie-broken IDs
- loop_messages: hoist per-row bank RMS into tf_inkling2_loop_rms and cache bank values per entry (80 B TG)
- bankAndReset: replace per-row copyBf16 loops and prelude restore with 2D bfloat4 launch tf_ink2_bank_and_reset
- state/forward: cap logits_all scratch allocation at batch_rows (32) and guard options.all rows <= batch_rows

Refs ashhart#4652
@gilby
gilby force-pushed the inkling2-lead-prefill-exact branch from c741dcd to d42b52c Compare November 23, 2026 06:01
@gilby gilby changed the title inkling2 metal: fuse router, hoist loop RMS, vectorize prompt attention and conv_dynamic, and widen to 512 rows inkling2 metal: fuse router, 32-thread conv_dynamic, loop RMS hoist, 2D bank_and_reset, and 32-row logits_all cap Nov 23, 2026
@gilby
gilby marked this pull request as ready for review November 23, 2026 06:01
@gilby

gilby commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Note on interaction with #4593 and standalone decode numbers:

  • #4594 is branched independently off #4618 (ad3d31f9ff), not stacked on #4593.
  • The ~1–4% standalone decode softness vs #4618 comes from the tf_inkling2_conv_dynamic switch (256 threads / 128 B TG -> 32 threads / 0 B TG), which improves 256-row prefill occupancy (-0.20s cold TTFT on M3 Ultra and M5 Pro) at the expense of low-row decode utilization.
  • When paired with #4593, forward_layers.zig routes segments.len > 1 or base <= state.window_rows (all c=1..16 decode and verify windows rows <= 8) through #4593's 256-thread tf_inkling2_conv_dynamic_segs, leaving #4594's 32-thread tf_inkling2_conv_dynamic strictly for prefill chunks (rows > 8).
  • Happy to either merge #4593 ahead of #4594, or drop / row-gate the 32-thread conv_dynamic hunk (keep 256 threads for rows <= 32) if maintainers prefer #4594 to be strictly non-regressive standalone.

@gilby gilby changed the title inkling2 metal: fuse router, 32-thread conv_dynamic, loop RMS hoist, 2D bank_and_reset, and 32-row logits_all cap inkling2 metal: fuse router at rows > 32, bfloat4 conv_dynamic, loop RMS hoist, 2D bank_and_reset, and 32-row logits_all cap Nov 23, 2026
@gilby

gilby commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Updated in 3248767ddc so #4594 is standalone non-regressive on decode without depending on #4593:

  • Restored 256-thread (128 B TG) tf_inkling2_conv_dynamic while keeping its bfloat4 vectorized Stage-1 dot.
  • Row-gated router at router_fused_min_rows = 33: rows <= 32 uses 16-TG tf_inkling2_route_scores + 32-thread zero-barrier tf_inkling2_route_select (512 B TG, shared ink_select_top8), preserving low-row parallelism across GPU cores, while rows > 32 uses the fused single-launch 256-thread tf_inkling2_router.
  • Expanded the metal_inkling2_glue .router contract shapes to {1, 5, 64} so both paths and the planted control pass Gates 1–4 (M5 Max 47/47 + 47 red, M5 Ultra 47/47 + 47 red), plus M5 Ultra real-ckpt serving parity (alone_unequal=0, code c=1/4/16 57.3 / 198.0 / 492.9 vs #4618 53.6 / 199.1 / 491.2, chat c=1/4 57.7 / 197.7 vs 57.6 / 196.8, T=1.0 34.9 / 58.3–59.7 / 70.1–72.7 vs 34.4–34.7 / 57.8–59.4 / 69.4–71.9, and cache pt=8704 rep_min=8.6760s / dec=48.4 with exact token_sha match).

@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. The 256-thread conv_dynamic and the row-gated router answer the speed concern. Two launch records are still missing at 3248767dd:

  • zig/src/families/inkling2/metal/loop.zig:32,38,40 runs tf_inkling2_loop_rms, loop_zeta and loop_blend, but zig/src/core/contract/inkling2_loop_metal_records.zig records only loop_messages and loop_gates. Please add the three, and add them to the inventory in loop_plan.zig so its test can catch a missing one.
  • zig/src/families/inkling2/metal/glue.zig:134-145 runs tf_inkling2_route_scores and tf_inkling2_route_select for every 1 to 32-row call, and neither is in inkling2_glue_metal_records.zig or glue_plan.zig. Please record both.

The branch conflicts with main now. We'll handle that rebase, so no need to do it.

  • Synthena

@gilby

gilby commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 9d54e30c29 (left un-rebased per your note):

  1. Loop helper launches (loop_rms, loop_zeta, loop_blend): added all three to zig/src/core/contract/inkling2_loop_metal_records.zig (rms: 256 threads / 64 B TG / reserved 256; zeta: 256 threads / 0 B TG / reserved 256; blend: 32 threads / 0 B TG / reserved 32, backed by [[max_total_threads_per_threadgroup(32)]] in loop.metal), added plan.Launch in loop_plan.zig, wired loop.zig pipeline loading to plan.Launch.*.symbol(), and updated loop_test.zig to verify 1-to-1 correspondence between plan.Launch, records, and kernel void definitions in loop.metal.
  2. Router split launches (route_scores, route_select): added both to zig/src/core/contract/inkling2_glue_metal_records.zig (route_scores: 256 threads / 0 B TG / reserved 256; route_select: 32 threads / 512 B TG / reserved 32), added plan.Launch in glue_plan.zig, wired glue.zig Pipelines.loadSource to std.enums.values(plan.Launch), and updated glue_test.zig to enforce 1-to-1 correspondence across plan.Launch, records, and glue.metal.

Verified on 9d54e30c29: zig fmt --check, git diff --check, test-inkling2-host (44 pass, 4 skip), test-inkling2-metal-host (76 pass, 5 skip), test-inkling2-controls-host (15/15), test-metal-launch-map-host (1215 registered launches, 2 controls, 0 missing, 8/8), compile-inkling2-native-host (macOS/Linux/Windows), test-inkling2-metal-host cross-compilation (x86_64-linux / x86_64-windows, 0 compile errors), python3 tools/pr_check.py --pr 4594 --base ad3d31f9ff (0 new lean findings), M5 Max GPU contract (tf-kernel-contract --only "metal inkling2" -> SUMMARY 47 kernels: 47 pass gates 1-4, 0 fail, 0 known findings; 47 controls: 47 red as planted, 0 missed; 0 limit problems), and --no-gates --offline M1/M2 compiler checks for all 5 loop and all 11 glue launches (0 limit problems).

@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:46
@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 branch was removed. It is reopened and now targets main. We will rebase it onto main for you; nothing is needed from you.

  • Synthena

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks for your patience, @gilby. Since #4387 landed, this PR's own commits sit on top of an earlier Inkling Metal stack that has not landed on main, and that stack includes a change to the FP32 summation order we have kept out for exactness. Your commits do not apply to main's kernels without it, so we are holding this PR until that stack is decided, rather than carrying unreviewed work along with it. We will follow up here; nothing is needed from you now.

  • Synthena

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