Skip to content

inkling2 metal: width-invariant M5 NAX route_scores/loop_zeta/loop_blend/rel_table and vectorized bfloat4 stores - #4766

Closed
cwschroeder wants to merge 23 commits into
ashhart:lead/inkling2-minifrom
cwschroeder:pr/inkling2-m5max-nax-prefill-ops
Closed

cwschroeder wants to merge 23 commits into
ashhart:lead/inkling2-minifrom
cwschroeder:pr/inkling2-m5max-nax-prefill-ops

Conversation

@cwschroeder

Copy link
Copy Markdown
Contributor

What this changes

Stacked on #4761 (6d4ea7da4, over lead/inkling2-mini 3e0e9bcb8). Zero overlap with open Inkling 2 Mini PRs outside this stack (no DSpark, no host lane sampler, no pre-M5 simdgroup MMA, no prompt attention changes):

  1. Width-invariant M5 NAX kernels in dense_units.metal / dense_units.zig / loop.zig / glue.zig / forward.zig (all widths 1..512, preserving bit-exact plain == drafted width invariance):
    • tf_ink2_route_scores_units_16 / tf_ink2_route_scores_units_64 (256 threads, 8 KiB TG): [rows, 2304] x [128, 2304]^T -> FP32 [rows, 128] with identical 8-way K-split order across bands (5.55 -> 1.42 ms clock-norm in 512-row prefill, 3.91x faster).
    • loop_zeta via existing dense_units.encode ([rows, 4608] x [64, 4608]^T -> BF16 [rows, 64], exact 8-way K-split order across all widths).
    • tf_ink2_loop_blend_units (32 threads, 0 B TG): [rows, 64] x [2304, 64]^T for forget + gain via 4 exact FP32 NAX ops plus the four-step FP32 blend (11.21 -> 3.42 ms clock-norm in 512-row prefill, 3.28x faster).
    • tf_ink2_rel_table_units (32 threads, 0 B TG): [rows*24, 16] x [16, 1024] -> BF16 via single non-transposed exact ink2_mma_nt op per 16x32 tile (6.13 -> 1.38 ms clock-norm in 512-row prefill, 4.44x faster).
  2. Vectorized bfloat4 / bfloat2 stores & aligned ushort/uint weight loads in dense_units.metal and experts_units.metal:
    • dense_units.metal writes bfloat4 when full_n (dense6_units_64 -21.1% clock-norm, dense6_units_16 -5.5%).
    • experts_units.metal replaces unaligned 4-byte loads + variable shifts with aligned ushort (NVFP4) / uint (MXFP8) loads and bfloat2/bfloat4 stores (nvfp4_gu_units -6.4%, nvfp4_down_units -10.1%, mxfp8_down_units -8.4%).
  3. Full Gates 1–4 contract probes, red controls, and launch records in metal_inkling2_nax_ops.zig, metal_inkling2_nax_ops_controls.zig, metal_inkling2_main.zig, metal_inkling2_controls_host.zig, and inkling2_dense_units_metal_records.zig.

Checks on this head (59fce1473)

  • zig fmt --check build.zig zig: PASS (Zig 0.17.0)
  • tools/zig/lean_check.py vs 6d4ea7da4: 167 vs 167, 0 new
  • tools/pr_check.py --base origin/lead/inkling2-mini: 0 errors (1 advisory warning for --pr NUMBER)
  • zig build native -Doptimize=Debug: PASS
  • zig build test-inkling2-controls-host test-inkling2-metal-host test-metal-launch-map-host: PASS (1234 registered launches, 2 controls, 0 missing)
  • GPU contract suite (tf-kernel-contract on Apple M5 Max, applegpu_g17, tensor units true):
    • --only "inkling2 dense_units": 10/10 kernels pass Gates 1–4 (6 dense_units + 4 new NAX ops), 10/10 controls red as planted, 0 limit problems
    • --only "inkling2 units": 4/4 kernels pass Gates 1–4, 4/4 controls red as planted, 0 limit problems

Receipt

  • Environment: Apple M5 Max (40-core GPU, Metal 4, 128 GiB), Zig 0.17.0, thinkingmachines/Inkling-2-Mini@ed40e7da625f594fc9c4990f5f66be24873c22d7 (unmodified BF16+NVFP4+MXFP8, powermode 0).
  • Width invariance & exactness:
    • Plain (--no-drafts, 1-row) vs drafted (multi-row verify): greedy 233dbb7e56f4 == 233dbb7e56f4, sampled temp=1.0, top_p=0.95, top_k=20, seed=42 ed31ffde9b47 == ed31ffde9b47 (100% bit-exact).
    • Baseline SHA parity preserved across short greedy (b5017c21c071), short temp=1.0 (3854e647286d), and 5,588-token prefill (df4a96126cd8).
  • Measured impact:

sethforprivacy and others added 23 commits November 21, 2026 17:41
…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.
…size prompt part_o by capacity, and widen glue/loop scratch to 512
…, 8 KiB active-expert NAX, and blocked stage_conv
@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks @cwschroeder. #4777 is built directly on this branch and contains all of its commits, so I'm closing this one to keep the review in one place. Its one extra commit reworks some of the same loop and attention code, so #4777 is the version to review from here. The red macOS dev-tools check comes from the base branch, not from your work.

  • Synthena

@ashhart ashhart closed this Nov 23, 2026
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