Skip to content

inkling2 metal: grouped NAX MoE, NAX/multi-TG attention, vectorized mv_fuse, and fused decode kernels - #4458

Closed
gilby wants to merge 14 commits into
ashhart:mainfrom
gilby:inkling2-metal-kernels-4337
Closed

gilby wants to merge 14 commits into
ashhart:mainfrom
gilby:inkling2-metal-kernels-4337

Conversation

@gilby

@gilby gilby commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

What this changes

Before: on #4337 (8d54cafcd4), Inkling 2 Mini Metal ran per-row experts_dot across all prompt widths, lacked NAX prefill attention and multi-TG decode attention, used scalar 1-row mv_fuse, and serialized ~1,056 dispatches per decode token (~387 tok/s 16K prefill on Apple M5 Ultra, ~101 tok/s on Apple M5 Pro). After: adds indirect-worklist BM=32, BN=64 grouped NAX/SIMD MoE (experts_grouped_nax for rows >= 17, preserving bit-identical experts_dot for rows = 1..16 with branchless E4M3 LUT), inkling2_attn_nax (rows >= 64) + per-Q-head inkling2_attn_decode_fast (rows < 64), 3-tier denseRows (rows >= 64 NAX 64x64, 16..63 bit-equal mv_mma, < 16 mv), coalesced bfloat4 multi-row mv_fuse (rows <= 16), 7 bit-exact fused decode glue kernels (~520 dispatches/tok and ~146 barriers/tok saved), and .concurrent encoder with hazard barriers.

Checks on this head

  • Head commit: 8b29cbedfb (stacked on Metal: experimental Inkling 2 Mini text, vision and bundled DSpark #4337 8d54cafcd4)
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 168 on base (8d54cafcd4), 168 on head (8b29cbedfb), 0 new findings.
  • Tests run:
    • zig build native dev-tools -Doptimize=ReleaseFast -Dcpu=apple_m1 — passed, 0 errors.
    • zig fmt --check build.zig zig and git diff --check — clean.
    • tf-kernel-contract --only inkling2 — 86/86 kernels PASS, 85/85 planted controls RED, 0 missed, 0 limit problems on Apple M5 Ultra, Apple M5 Pro, and Apple M3 Ultra (details below).
  • CI: Pending GitHub Actions on this head (draft stacked on Metal: experimental Inkling 2 Mini text, vision and bundled DSpark #4337).
  • Not run, and why: Apple M1/M2/M4 hardware not available for execution (Gate 5 static limits checked across M1–M5 with 0 problems); CUDA/HIP/Windows not touched.

Receipt

  • Main against branch (vs Metal: experimental Inkling 2 Mini text, vision and bundled DSpark #4337 8d54cafcd4 baseline on thinkingmachines/Inkling-2-Mini ed40e7da625f594fc9c4990f5f66be24873c22d7, measured at full stack head bf46d5b75b with tools/bench_openai.py and tools/bench_concurrent.py, T=0.0):
    • Apple M5 Ultra (80-core NAX, 512 GiB, macOS 27.2.1):
      • 16K–20.8K cold prefill (--prompt-cache-gib 0): 387 tok/s (56.0s TTFT) → 2,575.4 tok/s (8.03s TTFT, 6.65x)
      • Plain c=1 fibonacci-raw / gpu-chat: 39.1 / 38.9 tok/s → 41.62 / 41.57 tok/s
      • Plain c=4 chat aggregate: 81.0 tok/s → 127.6 tok/s (132.0 steady, +63%)
      • DSpark c=1 fibonacci-raw / gpu-chat: 52.3 / 46.4 tok/s → 80.15 / 54.84 tok/s (93.1–108.7 tok/s on prefill continuation)
    • Apple M5 Pro (20-core NAX, 64 GiB, macOS 27.2.1):
      • 16K–20.4K cold prefill: 101 tok/s → 840.7 tok/s (8.32x)
      • Plain c=1 / c=4 chat: 25.6 / 41.5 tok/s → 28.30 / 69.1 tok/s (73.3 steady)
      • DSpark c=1 fibonacci-raw / gpu-chat: 24.1 / 24.1 tok/s → 40.30 / 29.89 tok/s
    • Apple M3 Ultra (80-core non-NAX, 512 GiB, macOS 26.6.2):
      • 10.4K / 20.5K cold prefill: 1,257.5 / 1,222.5 tok/s
      • Plain c=1 / c=4 chat: 31.58 / 96.7 tok/s (102.8 steady); DSpark c=1 fibonacci-raw: 58.41 tok/s
  • Boxes it reaches: inkling2 Metal kernels, ops, experts, forward pass, and Metal contract tests.
  • New switch? No.
  • Environment: Base #4337 (8d54cafcd4), head 8b29cbedfb (stack head bf46d5b75b), Zig 0.17.0, Xcode Metal 4.0 toolchain, Apple M5 Ultra 512 GiB / M5 Pro 64 GiB (macOS 27.2.1) & M3 Ultra 512 GiB (macOS 26.6.2), thinkingmachines/Inkling-2-Mini ed40e7da625f594fc9c4990f5f66be24873c22d7 with bundled mtp.safetensors.
  • Paths and platforms the change reaches: Metal inkling2 text prefill, decode, and DSpark verify across M5 (NAX) and M3 (TF_SIMD_FRAGS); verified on M5 Ultra, M5 Pro, and M3 Ultra; M1/M2/M4 not run on hardware.
  • Exactness (T=0.0, token_sha256 on Apple M5 Ultra 512 GiB):
    • p15: plain = a5077f27220d93ba, dspark = a5077f27220d93ba (100% match)
    • p33: plain = 114eb7bb615a3609, dspark = 114eb7bb615a3609 (100% match)
    • p184: plain = da48844b05981221, dspark = da48844b05981221 (100% match)
  • Main against this branch: Same-machine before/after numbers posted above.
  • Decode speed before and after: Posted above from tools/bench_openai.py (--tokens 256 --reps 3 --temperatures 0).
  • Prompt speed before and after: Cold unique-salt prefill sweep (512/2048/8192/16384, --prompt-cache-gib 0), posted above.
  • 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:
    • inkling2_exp_bucket, inkling2_exp_grouped_{gate_up,down}_{nvfp4,mxfp8} (replaces experts_dot for rows >= 17; registered in inkling2_expert_records.zig and tested in metal_inkling2_experts.zig)
    • inkling2_attn_nax (replaces attention.metal for rows >= 64) and inkling2_attn_decode_fast (replaces attention.metal for rows < 64, heads >= 24; both registered in inkling2_metal_ops_records.zig + metal_limits.zig and tested in metal_inkling2_attention.zig)
    • inkling2_mv_fuse (multi-row bfloat4 vectorized upgrade, registered in inkling2_metal_ops_records.zig)
    • fused_decode.metal (bit-exact fused decode glue kernels wired in metal_fused_ops.zig / metal_forward_layer_fused.zig)
  • A planted control for each new kernel, and each one fails as planted:
    • 4 new grouped-expert planted controls added to metal_inkling2_experts.zig (NVFP4 outer factor skip, MXFP8 pre-SwiGLU BF16 rounding skip, NVFP4/MXFP8 ks=16 substep skip) — all 4 red as planted; fixed upstream metal_inkling2_loop.zig mutation string; all 85/85 controls red.
  • Each backend's SUMMARY line and exit status:
    • Apple M5 Ultra (applegpu_g17, tensor units true): SUMMARY 86 kernels: 86 pass gates 1-4, 0 fail, 0 known findings; 85 controls: 85 red as planted, 0 missed; 0 limit problems; gates 11365 ms, limits 557 ms (exit 0)
    • Apple M5 Pro (applegpu_g17, tensor units true): SUMMARY 86 kernels: 86 pass gates 1-4, 0 fail, 0 known findings; 85 controls: 85 red as planted, 0 missed; 0 limit problems; gates 11786 ms, limits 163 ms (exit 0)
    • Apple M3 Ultra (applegpu_g15, tensor units false): SUMMARY 86 kernels: 86 pass gates 1-4, 0 fail, 0 known findings; 85 controls: 85 red as planted, 0 missed; 0 limit problems; gates 12862 ms, limits 303 ms (exit 0)
  • Chips I could not run: Apple M1, M2, M4 (static Gate 5 limits verified across M1–M5 with 0 problems).

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.

BlivionIaG and others added 14 commits November 21, 2026 09:49
- Preserve the original release NVFP4 and MXFP8 expert weights and execute the looped text backbone on one Metal device.
- Keep the bundled DSpark head and plain prompt-cache reuse in the native lane backend.
- Refs ashhart#4274. Local M4 Pro evaluation, separate from the upstream full port.
- Keep the original vision tower, PNG/JPEG preparation, and the separate device and operator checks on the full integration branch.
- The padded-image real-weight accuracy gate remains open. This commit is not part of the text-only review branch.
- Match pinned Transformers eager QK and scaling boundaries before FP32 softmax.
- Keep the trained padded-image projector bound unchanged.
- Keep original NVFP4/MXFP8 checkpoint bytes and decode weights in registers, using BF16 activations and FP32 sums.
- Match pinned Transformers conversion and SwiGLU/route precision, with packed-weight and zero-scale controls.
- Refs ashhart#4274. Local original-weight M4 Pro qualification; not an upstream submission.
- Keep the shader and probe comments within the repository lean rules; no arithmetic changes.
- The rebase dropped the alias main uses for slide refusal, so the native server did not compile.
…-ups

- Comments are one line each again (339 runs collapsed, 40 shortened) and no `/*` text remains, so lean_check.py adds nothing to the base's 168 problems.
- TF_INKLING2_MINI_SMOKE reads through platform/environment.zig: a .metal/.path row, a docs row and a valid case in environment_test.zig.
- The load check sums every model*.safetensors shard, as core/checkpoint.zig lists them, plus mtp.safetensors when drafted.
- The state-bytes test covers Mini's looped shape: 24 physical layers as 48 visits, 8 global, visit 41 at physical 17.
- A new contract control isolates the experts' down projections; --loops and --policy refusals have a test that names them.
- The cacheBudget doc comment and the CHANGELOG state the DSpark prompt-cache limit: kept states need --no-drafts.
- The Inkling 2 contract suites move to metal_inkling2_main.zig, holding their probes for the run, which keeps metal_main.zig under 600 lines.
- The NVFP4 outer factor stays the unrounded quotient, pending the maintainer's F1 decision.
…ites

- `zig fmt --check build.zig zig` (what the Zig format workflow runs) is clean; the new suites and loaders had drifted.
- Layout and `@intFromEnum` -> `@backingInt` only: the contract, native, vision, forward and environment checks are rerun green on this tree.
…w projections

- Score GEMMs are a full fp32 dot, off the tensor-unit operand cut, so an M5 matches the product measured on earlier chips.
- One-row projections that share an input use one launch of the existing matvec lane chains. Same checkpoint token hash, about 2 ms off a 57 ms decode token.
- zig fmt on the forward layer, and the score-GEMM note is one line so lean check stays at the base count.
- Drop the blank lines zig fmt --check rejected on the projection fuse.
- f32_nt and f32_nn were filled from bf16 dyadics, so a 10-bit operand cut still matched the float64 product.
- Wide inputs now set the low fraction bits. On this M4 Pro the gates pass at 0.337 and 0.354 of the bound.
- Gate-down walked all 64 ranks in one threadgroup. Eight ranks a group, each rank's sum order unchanged.
- Decode on this M4 Pro is 23.2 tok/s, from 16.9, hash 57519a5a167f unchanged.
- Prefill chunks are 512 rows. A 1056-token prompt prefilled at 81 tok/s.
@gilby

gilby commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Closing as obsolete (#4337-based branch targeting main): its viable Metal kernel optimizations for lead/inkling2-mini have already been reimplemented cleanly on #4618/#4465 across #4592, #4593, #4594, #4596, #4683, #4685, #4699, and complementary PRs (#4747, #4784).

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