Skip to content

inkling2 metal: pre-sorted combine, simdgroup group, split-pow2 MXFP8, 8 KiB active-expert NAX, and blocked stage_conv - #4747

Open
cwschroeder wants to merge 18 commits into
ashhart:mainfrom
cwschroeder:pr/inkling2-m5max-moe-prefill
Open

cwschroeder wants to merge 18 commits into
ashhart:mainfrom
cwschroeder:pr/inkling2-m5max-moe-prefill

Conversation

@cwschroeder

Copy link
Copy Markdown
Contributor

Bit-exact Metal kernel optimizations for Inkling 2 Mini on Apple Silicon (verified on Apple M5 Max, 40-core GPU, applegpu_g17, tensor units true), audited for zero overlap against #4593, #4594, #4596, #4465, and #4699.

What this changes

  1. tf_ink2_combine (zig/kernels/metal/inkling2/experts.metal): Pre-sort a.topk <= 32 route slots once per threadgroup in threadgroup memory instead of repeating the $O(\text{topk}^3)$ scan per channel (-94.8% prefill 52.13 -> 2.73 ms, -85.0% verify 1.51 -> 0.23 ms).
  2. tf_ink2_group (zig/kernels/metal/inkling2/experts.metal): 1 simdgroup (32 lanes) per expert with coalesced ids loads and simd_prefix_exclusive_sum (-81.2% prefill 12.00 -> 2.25 ms).
  3. ink2_nv_quad & ink2_mx_quad (zig/kernels/metal/inkling2/experts_units.metal): Use ink2_e4m3_bits and exact split-power-of-two IEEE754 multiplies in place of ldexp (bit-identical across all 65,536 byte pairs; -28.0% MXFP8 Down / -12.6% MXFP8 GU).
  4. 8 KiB part[2048] + active-expert NAX dispatch (units.metal, experts_units.metal, experts.zig, forward.zig):
    • Split ink2_units_store into two 8-row halves reusing part[2048] (8 KiB instead of 16 KiB, doubling threadgroup occupancy across all widths).
    • tf_ink2_*_units_1 (1 KiB scratch, dispatches only the 8 active route slots directly for rows == 1, skipping tf_ink2_group completely; -21.8% fwd_1 NVFP4 experts 6.71 -> 5.25 ms).
    • tf_ink2_group_compact (single 128-thread TG) + tf_ink2_*_units_active (dispatches only min(rows * topk, 128) compacted experts for 2 <= rows <= 8; -16.5% fwd_verify NVFP4 Down / -8.2% GU).
  5. tf_ink2_stage_conv (runtime.metal, runtime_plan.zig, forward.zig): 16-row block parallelism with a zero-division fast path for rows <= 16 (-86.8% prefill 15.97 -> 2.11 ms, zero decode regression).
  6. tf_ink2_attn_scalar & tf_ink2_attn_slots (attention.metal): Register-hoist qv before the KV loop and vectorize V loads with bfloat4.

Verification (Apple M5 Max, 40-core GPU, 128 GiB, Zig 0.17.0, -Doptimize=ReleaseFast -Dcpu=apple_m1)

  • zig fmt --check build.zig zig: PASS
  • python3 tools/zig/lean_check.py: 0 new findings (all modified files <= 600 lines)
  • test-inkling2-host, test-inkling2-metal-host, test-inkling2-controls-host, test-metal-launch-map-host: ALL PASS
  • ./zig-out/bin/tf-kernel-contract --only inkling2: 63/63 PASS gates 1-4, 0 fail, 62/62 controls red as planted, 0 limit problems
  • Checkpoint thinkingmachines/Inkling-2-Mini@ed40e7da625f594fc9c4990f5f66be24873c22d7 (unmodified BF16 + NVFP4 + MXFP8): 100% bit-exact output parity (sha=b5017c21c071).

@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks @cwschroeder for the new Mini Metal kernels and multi-stream attention paths. While reviewing, I found that snapshot save and restore in the shared base under this branch copies each KV region flat, although the live cache is stored per head, so a partly filled sliding ring can resume incorrectly. That code is not from your changes, and we are fixing it separately in #4542. Nothing is needed from you on this point, and we will rebase your branch onto the fix.

  • Synthena

@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:48
@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 base branch was removed. It is reopened and now targets main. We will follow up here on what it needs next; nothing is needed from you yet.

  • Synthena

@cwschroeder
cwschroeder force-pushed the pr/inkling2-m5max-moe-prefill branch 2 times, most recently from 63b736a to b345a64 Compare November 24, 2026 10:16
sethforprivacy and others added 14 commits November 24, 2026 12:06
…t attention, and use mv_mma for prompt dense projections
Replace the scalar stride-2304 tf_inkling2_logits kernel with a 256-thread (8-simdgroup) batched implementation where each simdgroup computes one vocab row across up to 8 token rows, sharing coalesced bfloat4 unembed loads and preserving width-invariant FP32 reduction and BF16 softcap rounding. Update glue dispatch, contract limits record, host check, and 8x8 tile/interval contract tests.
@cwschroeder
cwschroeder force-pushed the pr/inkling2-m5max-moe-prefill branch from b345a64 to 1a50f48 Compare November 24, 2026 11:06
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thank you for rebasing this onto main, @cwschroeder. We went through the new head commit by commit.

It still carries the earlier Inkling Metal stack underneath your work, including commits that change the order FP32 dot products are summed in (the lane-strided sum and the simdgroup router, loop-gate and grouped-expert dots). TensorFold keeps every output bit equal to the serial reference, by default and behind a switch, so those commits cannot land, and the PR is held until the stack is rebuilt without them. Where your own commits reshape a reduction, the same applies; where they only reorder work, scheduling or launches, they can be reviewed once they sit on a base without those changes.

An exact version of the faster router and gate dots, one thread per output in the original summation order, is something we are looking at on our side. Nothing is needed from you right now; we will follow up here.

  • 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.

4 participants