Skip to content

inkling2 metal: split-K GQA decode, multi-row NAX verify attention, and 8 KiB bfloat4 dense_units - #4761

Open
cwschroeder wants to merge 19 commits into
ashhart:mainfrom
cwschroeder:pr/inkling2-m5max-attn-dense
Open

cwschroeder wants to merge 19 commits into
ashhart:mainfrom
cwschroeder:pr/inkling2-m5max-attn-dense

Conversation

@cwschroeder

Copy link
Copy Markdown
Contributor

What this changes

Stacked on #4747 (ca63d0505, over lead/inkling2-mini 3e0e9bcb8). Touches only 8 files across Inkling 2 Mini Metal attention and dense_units (no DSpark or host lane changes):

  1. 8-simdgroup split-K global GQA decode (tf_ink2_attn_split, 256 threads, 4,160 B TG) & sliding GQA decode (tf_ink2_attn_gqa, 32 threads) in attention.metal / attention.zig / inkling2_attention_metal_records.zig:
    • Pairs up to 2 rows x 3 query heads per KV head (6x KV reuse for global, 3x for sliding) and splits each 1,024-key global chunk across 8 simdgroups to saturate all 40 cores at long contexts.
    • NaN/Inf-safe single-exp2 online softmax path when score <= peak.
  2. Multi-row M5 NAX verify attention (tf_ink2_attn_verify_slide_nax, 32 threads, 0 B TG; tf_ink2_attn_verify_global_nax, 256 threads, 9,216 B TG) in prompt_attention.metal / attention.zig / inkling2_attention_metal_records.zig:
    • Processes 16 (row, head) query pairs per NAX tile directly via bfloat4 loads (16x KV reuse) for single-stream multi-row verification (rows >= 2, last_pos >= 256), with 8-simdgroup split-K across global chunks.
    • Full Gates 1–4 contract probes and discriminating red controls added in metal_inkling2_attention.zig and metal_inkling2_attention_controls.zig.
  3. 8 KiB part[2048] + bfloat4 fragment loads in dense_units.metal (inkling2_dense_units_metal_records.zig updated to .tg_memory = 8192):

Checks on this head (6d4ea7da4)

  • zig fmt --check build.zig zig: PASS (Zig 0.17.0)
  • tools/zig/lean_check.py vs ca63d0505: 167 vs 167, 0 new
  • tools/pr_check.py --base origin/lead/inkling2-mini: 0 errors (1 advisory warning for --pr NUMBER)
  • zig build test-inkling2-controls-host test-inkling2-metal-host test-metal-launch-map-host: PASS (1230 registered launches, 2 controls, 0 missing)
  • zig build -Dfixed-cpu test-kernel-contract: 279/280 PASS (1 pre-existing lead/inkling2-mini CUDA SM failure in inkling2_modelopt / inkling2_loop_math, 0 Metal issues)
  • GPU contract suite (tf-kernel-contract on Apple M5 Max, applegpu_g17, tensor units true):
    • --only "inkling2 attention": 10/10 kernels pass Gates 1–4, 8/8 controls red as planted, 0 limit problems
    • --only "inkling2 prompt": 4/4 kernels pass Gates 1–4 (incl. tf_ink2_attn_verify_slide_nax worst 0.060 of bound and tf_ink2_attn_verify_global_nax worst 0.046 of bound), 4/4 controls red as planted, 0 limit problems
    • --only "inkling2 dense_units": 6/6 kernels pass Gates 1–4, 6/6 controls red as planted, 0 limit problems (8,192 B TG exact on all 6)
  • atc overlap --json: incomplete (pullFilesUnavailable, timeLimit); diff against lead/inkling2-mini shows only the base inkling2 metal: GQA 3:1 sliding KV sharing, multi-stream slots, and batch_rows=64 #4596/inkling2 metal: pre-sorted combine, simdgroup group, split-pow2 MXFP8, 8 KiB active-expert NAX, and blocked stage_conv #4747 stack plus these 8 files, with zero overlap against open Inkling 2 Mini PRs outside this stack.

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: 100% bit-exact greedy decode output across short and 4K-context prompts (sha=b5017c21c071, 4K sha=2469b1cbb494).
  • Measured impact (clock-normalized Metal 4 timelines at 4K context):
    • fwd_1 single-token decode: total 57.95 -> 45.51 ms (-21.5%), 48-layer attention 14.58 -> 5.66 ms (-61.2%), 8 global layers ~12.0 -> 2.74 ms (~4.4x faster).
    • fwd_verify multi-row verify: total 35.24 -> 33.32 ms (-5.4%), 40 sliding layers 1.591 -> 1.029 ms, 8 global layers 0.722 -> 0.433 ms (54.1 us/layer), total 48-layer verify attention 1.462 ms (~10.7x faster than scalar).
    • dense_units: dense6_units_16 -20.7%, dense_units_16 -29.7%.

@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks @cwschroeder for the split-K decode and multi-row NAX verify work.

We found one edge case in the two new verify kernels, tf_ink2_attn_verify_slide_nax and tf_ink2_attn_verify_global_nax. A tile can load V for a key that only a later row may attend to. The earlier row's probability for that key is zero, but its V still enters the tensor multiply, and 0 * NaN is NaN. With rows at positions 256 and 257 and a NaN in V[257], the row at 256 can become NaN while scalar attention stays finite.

Could you keep masked V out of each row's sum, without clearing V the other row needs, and add a contract case comparing finite and NaN future V against scalar attention? lfm2_masked_pv handles the same case and may be a useful starting point, though the sliding path also needs each row's lower window bound.

The older prompt loop on the base branch has the same pattern. That one is ours to fix, so you can leave it.

  • Synthena

@ashhart
ashhart force-pushed the lead/inkling2-mini branch 4 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:47
@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-attn-dense branch 2 times, most recently from a57ef2e to 2c7549c Compare November 24, 2026 10:16
sethforprivacy and others added 16 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-attn-dense branch from 2c7549c to 6d5ac6e 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