Skip to content

glm metal: decode windows' attention in three launches, wider expert shapes, spread boundary sums; same bits - #3418

Closed
ashhart wants to merge 10 commits into
mainfrom
lead/glm-decode-kernels
Closed

ashhart wants to merge 10 commits into
mainfrom
lead/glm-decode-kernels

Conversation

@ashhart

@ashhart ashhart commented Nov 15, 2026

Copy link
Copy Markdown
Owner

GLM-5.3-Flash on Metal: faster decode windows of 2-4 rows, and every output bit is unchanged.

Byte budget for a 1-row window (M5 Ultra, each launch class run alone over every layer):

  • The window is about 640 launches reading 9.86 GB: 9.5 ms at a plain read (1,024-1,036 GB/s), against 14.34 ms for the whole window.
  • The big kernels already run at 850-1,017 GB/s: expert gate/up 926, expert down 924, KDA in-projection 949, lm_head 991.
  • What's left at 1 row is small launches (boundary mix and split 0.97 ms, KDA step 0.68, router 0.21, expert select 0.26) plus about 1.2 ms between launches.
  • At 2-4 rows, per-row attention launches and expert shapes cost real time.

Changes:

  • Dense attention (18b2136). A window's dense-attention rows (positions up to 2,048) take three launches in total instead of three per row; each row runs the one-row kernel body. Per layer at 400 keys: 4 rows 167 → 40 µs, 2 rows 84 → 39 µs.
  • Expert shapes (4945af2). Experts take two gate/up outputs a simdgroup up to 4 rows, and four down outputs from 2 rows. These are existing generated kernel shapes, so the arithmetic is the same: 2 rows -9%, 3 rows -4.4%.
  • Boundary expand (e8a89d3). The hyper-connection boundary's expand spreads each slice's partial sums over six threadgroups up to 4 rows, each sum in its original order: 1.1-1.35 µs less per boundary, 90 boundaries a window.
  • Profiler (0880195). The knock-out profile leaves out single launches of the fused expert path or the head, and GLM_PROFILE_ROWS takes a list.

Exactness:

  • tf-glm-attn-rows: 13.5M outputs bit-identical across 18 cases (1-2,048 keys, 1-16 rows, both size boundaries).
  • tf-glm-moe-rows: 4.1M outputs equal across all four shapes.
  • tf-glm-hc-rows: 1.42M outputs bit-identical at 1-16 rows.
  • On the full checkpoint, tf-glm-run at 100-2,600-token prompts matched base token for token, and depth 3 matched depth 0.

Measured on an M5 Ultra (16 bench prompts, 512 tokens, base, tip, tip, base):

Before After
Greedy 93.1 tok/s 99.7 tok/s (+7%)
Sampled 85.3 tok/s 90.4 tok/s (+6%)
"draft": false 69.4 tok/s 70.15 tok/s
Prose, greedy 86.0 tok/s 92.5 tok/s
Prose, sampled 77.8 tok/s 81.3 tok/s
  • Window GPU ms by rows (1 / 2 / 3 / 4 / 8 / 16): 14.40 / 19.40 / 23.17 / 27.72 / 48.39 / 88.52 → 14.27 / 18.23 / 21.49 / 25.83 / 45.06 / 81.43.
  • Replies: 48/48 equal in every arm, and greedy equals "draft": false on 16/16 prompts in all four arms.
  • Saved GLM prompt states on Metal are recomputed once after upgrading, since kernel sources changed.

Checks: lean finds nothing new. test-glm-window-contract-host, test-glm-contract-host and the GLM runtime-source compile gate pass.

Stacked on #3403.

  • Synthena (Ash's Agent)

…ed floor marks candidates, a merge selects among them), the same lists as glm_index_select

tf-glm-index-speed checks the new select against glm_index_select on real scores, ties, NaNs, signed
zeros, infinities, ramps, a hot span, a constant row and a row whose sampled blocks score high (the floor
overshoots, so the row is selected whole) at 15 positions and 1, 3 and 16 rows: 405 of 405 cases give the
same lists. On an M5 Max, a row at 90,000 tokens selects in 13.3 us instead of 26.6 us, at 120,000 in
13.3 us instead of 33.2 us.
…s at once, then the recurrence alone, kda_rows' values

glm_kda_window has kda_rows' buffers and arithmetic for every value; windows of two or more rows take it,
one-row steps keep kda_rows. tf-glm-kda-speed checks its outputs, final state and conv window against
kda_rows' bit for bit at 1 to 16 rows on three draws of inputs, for all 64 heads and TP2's 32: 96 of 96
cases are the same. On an M5 Max a 4-row step takes 27.5 us instead of 34.3 us, a 16-row step 79.2 us
instead of 106.3 us.
…x_select keeps rows of 3,073 to 8,192 blocks

The mark groups load their 16 words a simdgroup before the first compare, and the merge loads its
candidates' scores at once. On an M5 Ultra the mark took 8.3 us against 5.3 on an M5 Max, so the
sampled floor pays only past 8,192 blocks (32,768 tokens); up to there glm_index_select's one group a
row stays. tf-glm-index-speed: 405 of 405 cases give glm_index_select's lists; the speed tools warm
the GPU before timing. On an M5 Max a row at 90,000 tokens now selects in 11.8 us (26.6 us before the
new select).
…p against glm_index_select and kda_rows, with controls and launch records

The merge within 3,072 blocks and the mark and merge past 8,192 blocks (to rows past 65,536 blocks) run the loader's
glm_select.metal beside glm_index_select on the same scores; glm_kda_window and glm_kda_window_tp beside kda_rows and
kda_rows_tp at 2 to 16 rows. Gates 1-3 require the predecessor's bytes in every written buffer; each entry has a planted
source fault red at shapes. glm_launch_records.zig holds the four launches with this GPU's threadgroup bytes and the
M1/M2 limits from --offline.
…ll of them, each row the one-row launches' bits; tf-glm-attn-rows checks and times them
… and four down outputs from 2 rows, the generated kernels' bits; tf-glm-moe-rows checks and times the shapes
… sums on six threadgroups for windows up to 4 rows, the same sums; tf-glm-hc-rows checks and times it
…apes against main's launches, with controls and launch records

attendRows (glm_latent_scores_rows, glm_softmax_rows, glm_latent_values_rows) runs beside main's one-row scores,
softmax and values a row (a frozen glm_attn.metal) at 1 to 16 rows and 1 to 2,048 keys. core/hc.zig's boundary, with
tf_hc_expand_mix_parts and tf_hc_first_mix_parts to 4 rows, runs beside main's expand and split (a frozen
hc_boundary.metal) for separate streams, in place and at a first read, post and comb compared as raw words. The decode
experts at moe.zig's outputs a simdgroup run beside the four-output gathers at 1 to 8 rows. Gates 1-3 require the
predecessor's bytes; seven controls plant source faults red at shapes. The records add every launch with this GPU's
threadgroup bytes and the M1/M2 limits from --offline.
…h or the head, never the path; rows as a list
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner Author

This change landed on main with #3458 (merged 19 Nov), in commits bc422dfcf to f29077911, so this pull request closes.

  • Synthena

@ashhart ashhart closed this Nov 19, 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.

1 participant