Skip to content

inkling2 metal: fused AR+RMSNorm, vectorized 192-thread loop messages/gates, and bfloat4 attention prep - #4777

Open
cwschroeder wants to merge 21 commits into
ashhart:mainfrom
cwschroeder:pr/inkling2-m5max-fused-ar-loop-prep
Open

cwschroeder wants to merge 21 commits into
ashhart:mainfrom
cwschroeder:pr/inkling2-m5max-fused-ar-loop-prep

Conversation

@cwschroeder

Copy link
Copy Markdown
Contributor

What this changes

Stacked on #4766 (59fce1473, over lead/inkling2-mini 3e0e9bcb8). Keeps zero overlap with #4689 (tf_inkling2_ar and convolution kernels are untouched):

  1. Fused AR + RMSNorm for ungated visits (tf_ink2_fused_ar_norm in glue.metal, glue.zig, forward.zig):
    • Fuses attentive residual mixing and post-AR RMSNorm + BF16 weight multiply into one 192-thread (6-simdgroup) float4/bfloat4 vectorized kernel (240 B TG memory, 3 barriers, queries loaded once instead of up to 5x), eliminating the FP32 intermediate round-trip and separate norm launch across ungated attention, ungated MLP, and final norm stages.
    • Includes full Gates 1-4 contract probe, independent FP64 oracle (blockResidualNorm), count-checked red control, and Metal launch record (glue_plan.zig, inkling2_glue_metal_records.zig, metal_inkling2_glue.zig, metal_inkling2_glue_controls.zig).
  2. Vectorized single-pass 192-thread tf_inkling2_loop_messages and tf_inkling2_loop_gates (loop.metal, loop.zig, loop_plan.zig, loop_test.zig, inkling2_loop_metal_records.zig):
    • Loads bfloat4/float4 rows once into registers (cutting loop_messages bank reads 3x -> 1x and query reads 2x -> 1x, and loop_gates h/m reads 2x -> 1x), halves barrier counts (4 -> 2 in loop_messages, 2 -> 1 in loop_gates), reduces TG memory (144 -> 96 B and 80 -> 48 B), and writes coalesced float4/bfloat4 outputs.
  3. Vectorized bfloat4 loads and stores in ink_prep_qrel and ink_prep_kv (attention.metal):
    • Replaces scalar 4-element loops in tf_ink2_prep and tf_ink2_prep_slots with bfloat4 loads/stores while preserving the exact FP32 sum order and BF16 rounding steps (0-ULP bit-identical).

Checks on this head

  • zig fmt --check build.zig zig: PASS (Zig 0.17.0)
  • tools/pr_check.py --base origin/lead/inkling2-mini: 0 errors (lean 167 vs 167, 0 new, all modified files <= 600 lines)
  • zig build native -Doptimize=Debug: PASS
  • zig build test-inkling2-controls-host test-inkling2-metal-host test-metal-launch-map-host: PASS (1235 registered launches, 2 controls, 0 missing)
  • GPU contract suites on Apple M5 Max (applegpu_g17, tensor units true):
    • tf-kernel-contract --only "inkling2 glue": 10/10 kernels pass Gates 1-4, 10/10 controls red as planted, 0 limit problems
    • tf-kernel-contract --only "inkling2 loop": 2/2 kernels pass Gates 1-4, 2/2 controls red as planted, 0 limit problems
    • tf-kernel-contract --only "inkling2 attention": 10/10 kernels pass Gates 1-4, 8/8 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).
  • Exactness & width invariance (--no-drafts 1-row vs drafted multi-row verify):
    • 100% bit-exact width invariance on both greedy (sha=658383e87632) and temp=1.0, top_p=0.95, top_k=20, seed=42 (sha=3c62ce56afc5).
    • 128-token greedy probe matches baseline bit for bit (sha=b5017c21c071).
  • Clock-normalized Metal 4 timeline speedups (normalized to untouched tf_ink2_nvfp4_gu_units_active on fwd_verify and tf_ink2_attn_prompt_sliding on fwd_prefill, powermode 0):
    • tf_ink2_fused_ar_norm: -20.8% to -29.1% on fwd_verify (1.566 -> 1.111-1.240 ms), -34.4% on fwd_prefill (3.061 -> 2.009 ms/chunk)
    • tf_inkling2_loop_gates: -20.6% to -31.4% on fwd_verify (0.593 -> 0.407-0.471 ms), -24.1% on fwd_prefill (2.469 -> 1.873 ms/chunk)
    • tf_ink2_prep: -5.8% to -20.8% on fwd_verify (0.371 -> 0.294-0.349 ms)
  • Combined served decode (Apple M5 Max, powermode 0, 128 tokens): 111.1 tok/s at temp=1.0, top_p=0.95, top_k=20, seed=42 (decode=1.143 s) and 99.4 tok/s greedy (decode=1.278 s, sha=b5017c21c071).

@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-fused-ar-loop-prep branch 2 times, most recently from aee31d7 to 44d5883 Compare November 24, 2026 10:16
sethforprivacy and others added 17 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.
…size prompt part_o by capacity, and widen glue/loop scratch to 512
@cwschroeder
cwschroeder force-pushed the pr/inkling2-m5max-fused-ar-loop-prep branch from 44d5883 to 2599d5a 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