Skip to content

inkling2 metal: exact GQA 3:1 decode KV reuse and multi-stream batched attention (Refs #5191) - #5206

Merged
ashhart merged 2 commits into
ashhart:mainfrom
gilby:v6-1-batched-gqa-attn
Nov 27, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
gilby:v6-1-batched-gqa-attn

Conversation

@gilby

@gilby gilby commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Refs #5191

Summary

Adds exact 0-ULP GQA 3:1 KV-head reuse for Inkling 2 Mini Metal attention and batches multi-stream attendSegments across shared GPU descriptors without altering FP32 reduction order or BF16 output rounding:

  1. Exact 0-ULP GQA 3:1 decode/verify KV reuse (tf_ink2_attn_scalar_gqa and tf_ink2_attn_scalar_gqa_batched in zig/kernels/metal/inkling2/attention.metal):
    • One 32-lane simdgroup handles all 3 query heads sharing each (row, kv_head, chunk), loading K, V, and relative bias projections once instead of 3x while preserving the exact FP32 arithmetic and BF16 rounding order of tf_ink2_attn_scalar (0-ULP bit-equal).
    • Occupancy-gated in attention_plan.zig (r.rows >= 2 or activeChunks(l, r.last_pos) >= 16): keeps full threadgroup occupancy on lone-row short/sliding decode (rows == 1, chunks < 16) while activating GQA 3:1 KV reuse on multi-row batches (c > 1, tree/spec verify, prompt chunks) and long single-stream decode (>= 16 active chunks).
  2. Multi-stream batched attendSegments (tf_ink2_prep_batched, attention.zig, attention_plan.zig, forward.zig):
    • Batches multi-stream non-tree decode visits in attendSegments into a single prep, decode, and merge launch using per-segment GPU KV address descriptors and a uniform active chunk stride across segments.
  3. 4-Gate Kernel Contracts, Planted Red Controls & Launch Records:
    • Added metal_inkling2_attention_gqa.zig and metal_inkling2_attention_gqa_controls.zig (using shared contract.register, 6 planted red controls), plus M1–M5 launch records in inkling2_attention_metal_records.zig and host planning/ABI unit tests in attention_test.zig.

Verification

Static Checks & Kernel Contracts

  • python3 tools/pr_check.py --base 0460c536a4: 0 errors (clean merge verified against origin/main c6f2f86227).
  • Apple M5 Max 48 GiB (applegpu_g17, tf-kernel-contract --only "metal inkling2 attention"): 15/15 kernels pass gates 1–4 (0 fail, 0-ULP bit-equal to tf_ink2_attn_scalar), 13/13 planted controls red, 8/8 M1–M5 limits pass (0 problems).
  • Apple M3 Ultra 512 GiB (applegpu_g15, tf-kernel-contract --only "metal inkling2 attention"): 15/15 kernels pass gates 1–4 (0 fail, 0-ULP bit-equal to tf_ink2_attn_scalar), 13/13 planted controls red, 8/8 M1–M5 limits pass (0 problems).

Controlled Interleaved A-B-B-A Benchmarks (Inkling-2-Mini, --no-drafts --parallel 4 --context 8192)

Base 0460c536a4 vs New ae96e25b2c (2 reps per leg, A1 -> B1 -> B2 -> A2, pmset -g therm nominal on every leg, 0 cross-leg token mismatches across all 3 Macs):

Apple M5 Ultra 512 GiB

Cell Base (tok/s) New (tok/s) Delta Cross Mismatches
code T=0 c=1 36.00 35.85 -0.42% 0
code T=0 c=4 57.70 63.40 +9.88% 0
chat T=0 c=1 35.95 35.90 -0.14% 0
chat T=0 c=4 58.75 65.60 +11.66% 0
code T=1 c=1 35.75 35.50 -0.70% 0
code T=1 c=4 56.70 62.10 +9.52% 0
chat T=1 c=1 35.85 35.80 -0.14% 0
chat T=1 c=4 57.65 64.15 +11.27% 0
8,210-tok prefill 441.0 454.9 +3.14% 0
8,210-tok post-prefill decode (c=1) 17.86 17.82 -0.22% 0

Apple M5 Pro 64 GiB

Cell Base (tok/s) New (tok/s) Delta Cross Mismatches
code T=0 c=1 25.70 25.90 +0.78% 0
code T=0 c=4 51.85 54.75 +5.59% 0
chat T=0 c=1 25.80 26.15 +1.36% 0
chat T=0 c=4 54.05 57.75 +6.85% 0

Apple M3 Ultra 512 GiB

Cell Base (tok/s) New (tok/s) Delta Cross Mismatches
code T=0 c=1 27.35 27.35 0.00% 0
code T=0 c=4 43.20 46.85 +8.45% 0
chat T=0 c=1 27.35 27.40 +0.18% 0
chat T=0 c=4 44.15 48.50 +9.85% 0
code T=1 c=1 27.40 27.45 +0.18% 0
code T=1 c=4 42.35 45.70 +7.91% 0
chat T=1 c=1 27.55 27.30 -0.91% 0
chat T=1 c=4 43.30 47.50 +9.70% 0
8,210-tok prefill 532.2 583.9 +9.71% 0
8,210-tok post-prefill decode (c=1) 12.88 12.86 -0.19% 0

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. We ran this head (ae96e25b2c) against main on an M3 Ultra with Inkling-2-Mini, in base/new/new/base order. The result is GREEN: tokens matched in every chat and prompt cell, thinking on and off.

  • 2K-token prompt fill: 594.5 → 630.9 tok/s (+6.0 to +6.1%)
  • Four-stream decode: 39.8-40.1 → 44.8-45.5 tok/s (+12.2 to +13.6%, chat and code, temperature 0 and 1)
  • One-stream decode: 19.8 → 19.7-19.8 tok/s (-0.2 to -0.4%). That's inside our noise threshold, but it shows in every pair. If the batched path costs one stream anything, a note on where would help.

Landings are paused while we cut 1.3.6. This goes into the first trains after the release, and its train runs the remaining cells.

  • Synthena

@gilby

gilby commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the M3 Ultra A/B! On 1-stream decode (segments.len == 1, rows == 1), neither the batched path nor the GQA path runs:

  • planBatched returns null immediately on segs.len < 2.
  • attention_plan.attend requires r.rows >= 2 or activeChunks(l, r.last_pos) >= 16. With sliding_chunk = 256 (activeChunks <= 5) and global_chunk = 16384 (activeChunks == 1 at 2K), 1-stream decode still dispatches the unchanged tf_ink2_prep, tf_ink2_attn_scalar, and tf_ink2_merge kernels.

The only differences on 1-stream decode are:

  1. Filling specs[0] and checking planBatched (early return) on the host in attendSegments (48 visits/step).
  2. Tightening the 1-stream stride passed to attention.attend from state.max_attention_stride (64) to slotsFor(layer, last_pos + 1) (5 sliding / 1 global), plus 3 additional kernel entry points compiled into scalar_lib (attention.metal).

So the batched path itself does not run on 1 stream; the ~0.04–0.08 tok/s delta is within noise / minor per-visit host + Metal dispatch/library layout effects.

gilby and others added 2 commits November 27, 2026 00:22
…d attention

Add exact 0-ULP GQA 3:1 decode kernels (tf_ink2_attn_scalar_gqa and
tf_ink2_attn_scalar_gqa_batched) and multi-segment batched prep
(tf_ink2_prep_batched) using per-segment GPU KV address descriptors.

One 32-lane simdgroup now handles all 3 query heads sharing each
(row, kv_head, chunk), loading K, V, and relative bias projections once
instead of 3x while preserving the exact FP32 arithmetic and BF16 rounding
order of tf_ink2_attn_scalar. Multi-stream non-tree decode visits in
attendSegments now batch into a single prep, decode, and merge launch
using a uniform active chunk stride across segments.

Register launch records, host planning/ABI tests, 4-gate kernel contracts
with 0-ULP scalar parity checks, and 6 planted red controls.

Refs ashhart#5191
@ashhart
ashhart force-pushed the v6-1-batched-gqa-attn branch from ae96e25 to 0957970 Compare November 27, 2026 00:22
@ashhart
ashhart merged commit a2c68e4 into ashhart:main Nov 27, 2026
2 of 12 checks passed
@ashhart

ashhart commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

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