Skip to content

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

Merged
ashhart merged 2 commits into
ashhart:mainfrom
gilby:v4-14-exact-grouped-experts-rebased
Nov 25, 2026
Merged

ashhart merged 2 commits 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 (chunked at <= 64 rows per prompt/batch slice), 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: ffc7b038cb
  • Lean checker, python3 tools/pr_check.py --base origin/main --pr 4995: lean 164 vs 164, 0 new, 10 changed files, 0 error(s) (1 advisory warning: no CHANGELOG.md line). Touched file line counts max 544 lines (<= 600: experts.metal 544, zig/tests/contract/metal_inkling2_experts.zig 535, metal_inkling2_experts_grouped.zig 518, forward.zig 422, state.zig 375, experts.zig 287, experts_test.zig 162, metal_inkling2_experts_controls.zig 161, 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 origin/main..HEAD on ffc7b038cb: exit 0.
    • Apple M5 Max (ffc7b038cb):
      • 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 pass, 4 skip (49 total) (exit 0).
      • zig build test-inkling2-controls-host: 24 pass (24 total) (exit 0).
      • zig build test-kernel-contract: 45/45 steps succeeded; 259/259 tests passed (exit 0).
      • zig build tf-kernel-contract -Doptimize=ReleaseFast: exit 0.
      • zig build test-inkling2-metal-host -Dcpu=apple_m1 -Doptimize=ReleaseFast: 112 pass, 6 skip (118 total) (0 failed; 2 JPEG tests skipped pending newer libjpeg-turbo, exit 0).
      • ./zig-out/bin/tf-kernel-contract --only "metal inkling2": SUMMARY 67 kernels: 67 pass gates 1-4, 0 fail, 0 known findings; 81 controls: 81 red as planted, 0 missed; 0 limit problems; gates 16821 ms, limits 212 ms (exit 0).
      • ./zig-out/bin/tf-kernel-contract --only "metal inkling2 experts": SUMMARY 17 kernels: 17 pass gates 1-4, 0 fail, 0 known findings; 30 controls: 30 red as planted, 0 missed; 0 limit problems; gates 6479 ms, limits 61 ms (exit 0).
    • Apple M3 Ultra 512 GiB (pre-rebase 38dfab19f7, Metal 3 compile-fix overlay; grouped kernel math unchanged):
      • ./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 ffc7b038cb.
  • 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: Rebased commit ffc7b038cb stacked on main (f3c0fa8c65, includes #5000 and #5058), 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 (A-B-B-A served receipts recorded pre-rebase on 41bd6e1162 against main 9961ca4920).
  • 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 81/81 planted controls across all metal inkling2 suites (30/30 in metal inkling2 experts, Apple M5 Max ffc7b038cb) and 68/68 planted controls (Apple M3 Ultra pre-rebase 38dfab19f7) 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 (ffc7b038cb, --only "metal inkling2"): SUMMARY 67 kernels: 67 pass gates 1-4, 0 fail, 0 known findings; 81 controls: 81 red as planted, 0 missed; 0 limit problems; gates 16821 ms, limits 212 ms (exit 0).
    • Apple M5 Max (ffc7b038cb, --only "metal inkling2 experts"): SUMMARY 17 kernels: 17 pass gates 1-4, 0 fail, 0 known findings; 30 controls: 30 red as planted, 0 missed; 0 limit problems; gates 6479 ms, limits 61 ms (exit 0).
    • Apple M3 Ultra (pre-rebase 38dfab19f7, 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

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks, @gilby. Each grouped output keeps its ascending serial sum and rounding points, and results return to their original route positions before the unchanged combine. The contracts compare complete bytes with the old kernels, with controls for missing blocks, pairs and arithmetic changes.

This is ready for the remaining comparisons. Please retain the full checkpoint/build identities and serial, resumed and memory evidence. The reported M5 single-stream latency increase and small M3 single-stream decode losses still need comparisons on an otherwise idle machine; gains in other cells do not settle them. The M3 overlay remains part of that receipt's scope.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

I reran the grouped-expert checks on M5 Max after trial-merging 0e09b9fb85 cleanly into 47a605aa12.

zig build test-inkling2-host test-inkling2-metal-host tf-kernel-contract -Dfixed-cpu -j2 --summary all
./zig-out/bin/tf-kernel-contract --only 'metal inkling2 experts'

150 host tests passed with 10 skipped. All 17 expert kernels passed, all 30 planted controls were caught, and there were zero launch-limit problems. Each of the four grouped NVFP4 and MXFP8 gate/up and down entries matched its single-pair reference bit for bit across 16 shapes.

This covers the rebased host/device contract. I didn't rerun the model-backed serial, resume, latency or memory comparisons on this head.

Igor Lessio - AIFLOW LABS

@AIFlowML

Copy link
Copy Markdown
Contributor

The rebased 41bd6e1162 trial-merges cleanly into 9961ca492. On M5 Max, 155 host tests passed with ten skipped. The focused Metal run passed all 17 kernels, caught all 30 planted controls and reported zero launch-limit failures. Each of the four grouped NVFP4 and MXFP8 gate/up and down kernels matched the original kernel bit for bit across 16 shapes.

zig build test-inkling2-host test-inkling2-metal-host tf-kernel-contract -Dfixed-cpu -j2 --summary all
./zig-out/bin/tf-kernel-contract --only 'metal inkling2 experts'

This verifies the rebased kernel and host tests. Whole-checkpoint latency, memory and resume comparisons were outside my run.

Igor Lessio - AIFLOW LABS

@ashhart
ashhart force-pushed the v4-14-exact-grouped-experts-rebased branch from ffc7b03 to 65642e7 Compare November 25, 2026 18:56
@ashhart
ashhart merged commit d810a36 into ashhart:main Nov 25, 2026
3 of 12 checks passed
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

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