Skip to content

inkling2 metal: exact-serial grouped routed experts for rows >= 2 - #4995

Open
gilby wants to merge 1 commit into
ashhart:mainfrom
gilby:v4-14-exact-grouped-experts-rebased
Open

gilby wants to merge 1 commit into
ashhart:mainfrom
gilby:v4-14-exact-grouped-experts-rebased

Conversation

@gilby

@gilby gilby commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

What this changes

Before: on main, routedExperts dispatches one weight read per (token, slot) route pair via tf_ink2_{nvfp4,mxfp8}_{gu,down}, re-reading expert weights for every row when rows >= 2 (concurrent decode batches, speculative verification, and prompt prefill). After: when rows >= 2 (<= 64 rows per prompt chunk), routedExperts runs tf_ink2_expert_group on GPU without host readback to bucket route pairs by expert ID into <= 8-pair work tiles and dispatches tf_ink2_{nvfp4,mxfp8}_{gu,down}_grouped, sharing expert weight loads across up to 8 active pairs per tile while keeping each per-output dot identical to main's serial chain (0 ULP drift, exact bit-identical outputs); rows == 1 keeps the existing ungrouped path.

Checks on this head

  • Head commit: 38dfab19f7
  • Lean checker, python3 tools/pr_check.py --base ed70f71aa7: lean 165 vs 165, 0 new, 10 changed files, 0 error(s), 1 warning(s) (advisory --pr NUMBER). Touched file line counts max 535 lines (<= 600: zig/tests/contract/metal_inkling2_experts.zig 535, metal_inkling2_experts_grouped.zig 518, experts.metal 470, forward.zig 389, state.zig 376, experts.zig 202, experts_test.zig 162, metal_inkling2_experts_controls.zig 160, experts_plan.zig 150, inkling2_experts_metal_records.zig 27).
  • Tests run (-Dcpu=apple_m1 -Doptimize=ReleaseFast):
    • zig fmt --check build.zig zig and git diff --check ed70f71aa7..HEAD on 38dfab19f7: exit 0.
    • Apple M5 Max (38dfab19f7):
      • zig build test-inkling2-host test-inkling2-metal-host -Dcpu=apple_m1 -Doptimize=ReleaseFast: 8/8 steps succeeded (exit 0, also passes under default build zig build test-inkling2-host test-inkling2-metal-host).
      • ./zig-out/bin/tf-kernel-contract --only "metal inkling2": SUMMARY 66 kernels: 66 pass gates 1-4, 0 fail, 0 known findings; 78 controls: 78 red as planted, 0 missed; 0 limit problems; gates 12868 ms, limits 47 ms (exit 0).
    • Apple M3 Ultra 512 GiB (Metal 3 compile-fix overlay):
      • ./zig-out/bin/tf-kernel-contract --only "metal inkling2": SUMMARY 53 kernels: 53 pass gates 1-4, 0 fail, 0 known findings; 68 controls: 68 red as planted, 0 missed; 0 limit problems; gates 13170 ms, limits 8 ms (exit 0).
  • CI: Pending GitHub Actions on 38dfab19f7.
  • Not run, and why: Non-Apple GPU backends (Metal-only change); Apple M1/M2 not directly available.

Receipt

  • Main against branch: Matched paired A-B-B-A served runs (--tokens 128 --reps 2, --parallel 4 --context 8192) comparing main vs grouped experts on Inkling-2-Mini (BF16/NVFP4/MXFP8), with 0 cross-leg token_sha256 mismatches across every cell:
    • Apple M5 Ultra 512 GiB:
      • Plain (--no-drafts, --skip-serial):
        • c=1: code T=0.0: 16.15 -> 16.20 (+0.31%, TTFT 0.222 -> 0.179s, -19.37%); chat T=0.0: 16.10 -> 16.15 (+0.31%, TTFT 0.511 -> 0.483s, -5.48%); code T=1.0: 16.15 -> 16.25 (+0.62%, TTFT 0.207 -> 0.235s, +13.25%); chat T=1.0: 16.10 -> 16.15 (+0.31%, TTFT 0.507 -> 0.398s, -21.40%).
        • c=4: code T=0.0: 34.85 -> 40.40 tok/s (+15.93%, TTFT 0.496 -> 0.426s, -14.10%); chat T=0.0: 34.95 -> 39.95 tok/s (+14.31%, TTFT 1.720 -> 1.064s, -38.16%); code T=1.0: 34.45 -> 40.95 tok/s (+18.87%, TTFT 0.508 -> 0.418s, -17.70%); chat T=1.0: 34.80 -> 40.85 tok/s (+17.39%, TTFT 1.716 -> 1.051s, -38.75%).
      • Self-drafted (--drafter target):
        • c=1: code T=0.0: 26.10 -> 31.30 tok/s (+19.92%, TTFT -30.41%); chat T=0.0: 23.25 -> 27.95 tok/s (+20.22%, TTFT -34.99%); code T=1.0: 19.10 -> 23.05 tok/s (+20.68%, TTFT -9.93%); chat T=1.0: 23.00 -> 27.70 tok/s (+20.43%, TTFT -36.50%).
        • c=4: code T=0.0: 29.90 -> 46.50 tok/s (+55.52%, TTFT 0.555 -> 0.354s, -36.13%); chat T=0.0: 23.10 -> 35.95 tok/s (+55.63%, TTFT 1.815 -> 1.122s, -38.18%); code T=1.0: 22.80 -> 33.30 tok/s (+46.05%, TTFT 0.524 -> 0.358s, -31.61%); chat T=1.0: 21.70 -> 32.05 tok/s (+47.70%, TTFT 1.824 -> 1.122s, -38.47%).
      • Cold prefill (--no-drafts --context 16384 --prompt-cache-gib 0):
        • 2048 (2077 prompt tokens): 92.3 -> 174.9 tok/s (+89.54%, 22.496s -> 11.870s).
        • 8192 (8233 prompt tokens): 90.9 -> 170.8 tok/s (+87.84%, 90.582s -> 48.208s).
    • Apple M3 Ultra 512 GiB (Metal 3 compile-fix overlay):
      • Plain (--no-drafts, --skip-serial):
        • c=1: code T=0.0: 12.40 -> 12.40 (0.00%, TTFT 0.401 -> 0.209s, -47.94%); chat T=0.0: 12.50 -> 12.40 (-0.80%, TTFT 0.967 -> 0.458s, -52.64%); code T=1.0: 12.40 -> 12.40 (0.00%, TTFT 0.399 -> 0.174s, -56.34%); chat T=1.0: 12.50 -> 12.45 (-0.40%, TTFT 0.960 -> 0.492s, -48.70%).
        • c=4: code T=0.0: 23.70 -> 33.65 tok/s (+41.98%, TTFT 0.980 -> 0.469s, -52.19%); chat T=0.0: 24.85 -> 34.10 tok/s (+37.22%, TTFT 3.510 -> 1.442s, -58.94%); code T=1.0: 23.75 -> 34.30 tok/s (+44.42%, TTFT 0.982 -> 0.412s, -57.97%); chat T=1.0: 24.95 -> 34.90 tok/s (+39.88%, TTFT 3.473 -> 1.393s, -59.88%).
      • Self-drafted (--drafter target):
        • c=1: code T=0.0: 17.20 -> 22.20 tok/s (+29.07%, TTFT -54.48%); chat T=0.0: 15.25 -> 19.80 tok/s (+29.84%, TTFT -54.50%); code T=1.0: 14.40 -> 18.10 tok/s (+25.69%, TTFT -55.57%); chat T=1.0: 15.10 -> 19.70 tok/s (+30.46%, TTFT -56.37%).
        • c=4: code T=0.0: 14.95 -> 36.00 tok/s (+140.80%, TTFT 1.031 -> 0.448s, -56.60%); chat T=0.0: 10.30 -> 24.60 tok/s (+138.83%, TTFT 3.617 -> 1.461s, -59.62%); code T=1.0: 12.70 -> 26.85 tok/s (+111.42%, TTFT 1.017 -> 0.476s, -53.20%); chat T=1.0: 10.10 -> 22.80 tok/s (+125.74%, TTFT 3.644 -> 1.460s, -59.94%).
      • Cold prefill (--no-drafts --context 16384 --prompt-cache-gib 0):
        • 2048 (2077 prompt tokens): 42.2 -> 129.9 tok/s (+207.34%, 49.220s -> 16.001s).
        • 8192 (8233 prompt tokens): 36.9 -> 124.9 tok/s (+238.48%, 223.364s -> 65.915s).
  • Boxes it reaches: inkling2 Metal routed experts shader (experts.metal), expert dispatch/plan/host tests (experts.zig, experts_plan.zig, experts_test.zig), Metal forward/scratch (forward.zig, state.zig), and kernel contract registry/tests (inkling2_experts_metal_records.zig, metal_inkling2_experts.zig, metal_inkling2_experts_controls.zig, metal_inkling2_experts_grouped.zig).
  • New switch? No (always active for rows >= 2).
  • Environment: Commit 38dfab19f7 stacked on ed70f71aa7, Zig 0.17.0 -Dcpu=apple_m1 -Doptimize=ReleaseFast, Apple M5 Max (applegpu_g17), Apple M5 Ultra 512 GiB, Apple M3 Ultra 512 GiB; checkpoint Inkling-2-Mini.
  • Paths and platforms the change reaches: Routed expert gu and down projections when rows >= 2 across Apple Silicon Metal GPUs (NVFP4 layers 0..19, MXFP8 layers 20..23).
  • Exactness: 0 cross-leg token_sha256 mismatches across all A-B-B-A legs (plain, dspark, prefill) on both Apple M5 Ultra and Apple M3 Ultra; grouped kernels compute identical per-pair serial dot chains as main.
  • Decode speed before and after: See A-B-B-A tables above (up to +18.87% plain / +55.63% self-drafted on Apple M5 Ultra; up to +44.42% plain / +140.80% self-drafted on Apple M3 Ultra).
  • Prompt speed before and after: Cold first-request prefill (--prompt-cache-gib 0): +89.54% (2048) / +87.84% (8192) on Apple M5 Ultra; +207.34% (2048) / +238.48% (8192) on Apple M3 Ultra; TTFT improved up to -38.75% (M5 Ultra) and -59.94% (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 the kernel each one replaces:
    • metal inkling2 experts tf_ink2_expert_group (128 threads, 2096 bytes TG memory) — GPU route-pair bucketing kernel (<= 8 pairs/tile).
    • metal inkling2 experts tf_ink2_nvfp4_gu_grouped (32 threads) — grouped companion to tf_ink2_nvfp4_gu for rows >= 2.
    • metal inkling2 experts tf_ink2_nvfp4_down_grouped (32 threads) — grouped companion to tf_ink2_nvfp4_down for rows >= 2.
    • metal inkling2 experts tf_ink2_mxfp8_gu_grouped (32 threads) — grouped companion to tf_ink2_mxfp8_gu for rows >= 2.
    • metal inkling2 experts tf_ink2_mxfp8_down_grouped (32 threads) — grouped companion to tf_ink2_mxfp8_down for rows >= 2.
  • A planted control for each new kernel, and each one fails as planted: All 78 planted controls (Apple M5 Max 38dfab19f7) and 68 planted controls (Apple M3 Ultra) fail red as planted (0 missed), including grouped bucket, tile, and output controls in metal_inkling2_experts_controls.zig.
  • Each backend's SUMMARY line and exit status:
    • Apple M5 Max (38dfab19f7): SUMMARY 66 kernels: 66 pass gates 1-4, 0 fail, 0 known findings; 78 controls: 78 red as planted, 0 missed; 0 limit problems; gates 12868 ms, limits 47 ms (exit 0).
    • Apple M3 Ultra (Metal 3 overlay): SUMMARY 53 kernels: 53 pass gates 1-4, 0 fail, 0 known findings; 68 controls: 68 red as planted, 0 missed; 0 limit problems; gates 13170 ms, limits 8 ms (exit 0).
  • Chips I could not run: Apple M1/M2/M4 not directly available.

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.

@AIFlowML

Copy link
Copy Markdown
Contributor

I tested main 4c79cfe346d3c239ff7dfd7aa694df799755bafa, followed by #4980 920224e129, #4981 e299674146 and this head 38dfab19f70ea77c5fbf326bb99e0d032df5d1ea. All three trial merges were clean. On the combined tree, Apple M5 Max 128 GB, macOS 27.2, Zig 0.17.0:

zig build tf-kernel-contract -Dcpu=apple_m1 -Doptimize=ReleaseFast
./zig-out/bin/tf-kernel-contract --only 'metal inkling2'

Exit 0. SUMMARY 66 kernels: 66 pass gates 1-4, 0 fail, 0 known findings; 78 controls: 78 red as planted, 0 missed; 0 limit problems; gates 13986 ms, limits 529 ms.

I also checked the reported #3076 overlap. At f9ca20f824, it conflicts in the same nine files on plain main and on the #4995-only trial merge, with identical conflict contents after removing the generated merge labels. This run covers the M5 kernel contract and integration with the two shader PRs. It did not run an M3 or a live checkpoint.

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.

2 participants