Skip to content

glm metal: two-group boundary split and KDA heads, prefetched dense attention, one-row router; same bits - #3424

Closed
ashhart wants to merge 15 commits into
mainfrom
lead/glm-small-launches
Closed

ashhart wants to merge 15 commits into
mainfrom
lead/glm-small-launches

Conversation

@ashhart

@ashhart ashhart commented Nov 15, 2026

Copy link
Copy Markdown
Owner

GLM-5.3-Flash on Metal: faster small decode launches, with every output bit unchanged.

Changes (per-launch times on an M5 Ultra; bit checks pass on an M5 Max and an M5 Ultra):

  • Boundary split (93579c4). The hyper-connection boundary's split runs on two threadgroups a row: one does the collapse and norm with one barrier, the other does post and the Sinkhorn comb in parallel. Split launch 5.21 → 4.11 µs, about 0.1 ms a window. tf-glm-hc-rows checks it against the old one-group split (kept in tests): 2,842,400 outputs equal at 1-16 rows, including -0, zeros and large values.
  • KDA heads on two threadgroups (885bc1b). On an Ultra-class GPU at 64 heads, windows of 2+ rows put each KDA head on two threadgroups of 512 threads, each taking half the value columns, then a second launch norms each head. Per step: 2 rows 20.7 → 16.8 µs, 4 rows 29.0 → 22.0, 16 rows 81.3 → 58.6. tf-glm-kda-speed checks it against kda_rows: 144/144 equal (outputs, state, conv window). One-row windows, the M5 Max and TP2 keep the old kernels, where the split didn't pay.
  • Dense attention prefetch (04deff4). Dense latent attention loads each step's blocks during the step before, and reads blocks fully inside the keys without bounds checks. Per layer: 38.5 → 28.2 µs at 400 keys, 92.3 → 60.4 at 2,048. tf-glm-attn-rows checks it against each row's own launches of the old kernels: 13,492,480 outputs equal.
  • One-row router (5257185). One-row windows use a GLM-only router and indexer-gate kernel that sums row 0 alone, appended to the core source so other families are unchanged. Router 9.48 → 5.00 µs a launch. tf-glm-route-one: 79,872 logits equal.

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

Before After
Greedy 99.9 tok/s 101.6 tok/s
Sampled 89.8 tok/s 92.1 tok/s
"draft": false 70.3 tok/s 71.8 tok/s
Prose, greedy 92.6 tok/s 94.1 tok/s
Prose, sampled 81.4 tok/s 83.4 tok/s
  • Window GPU ms (1 / 2 / 4 / 16 rows): 14.22 / 18.19 / 25.81 / 81.45 → 13.91 / 17.91 / 25.46 / 80.50.
  • Replies: 48/48 equal in every arm, and greedy equals "draft": false on 16/16 in all four arms.
  • Checkpoint replies at 100-2,600-token prompts and draft depths 0 and 3 equal base.

Checks: lean finds nothing new. The GLM runtime-source compile gate (61 libraries) and both GLM contract host tests pass.

Stacked on #3418.

  • 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
…ow, the collapse and norm on one and post with the Sinkhorn comb on the other, the same bits; tf-glm-hc-rows checks and times it

The one-group split ran simdgroup 0's 20 Sinkhorn steps between the mixes and the collapse, so every row's norm
waited on them. Group 0 now takes pre from the slice sums in each simdgroup (lanes 0-3, then shuffles) and norms
with one barrier; group 1's first simdgroup takes post and the comb, the old steps in the old order.
tf-glm-hc-rows checks the boundary against the one-group expand and the one-group split (kept in
tests/glm_hc_split_one.metal) at 1-16 rows on two draws, the second with wider values, zeros, -0 and large
streams: 2,842,400 outputs equal.
…d on two threadgroups, half the value columns each, then norms each row's head in a second launch, kda_rows' values; tf-glm-kda-speed checks and times it

At 64 threadgroups of 1,024 threads an M5 Ultra runs the step on about half its cores. glm_kda_parts gives each
head two groups of 512 threads: both take the rows' decays, q and k whole with kda_rows' sums, each its own 64
columns' v, gates, recurrence and state; the raw outputs go to y and the gates to gu, and glm_kda_norm norms and
gates each row's head in kda_rows' order. kernels.splitKda turns it on for a GPU named Ultra at one Mac's 64
heads and two or more rows; one-row steps, a Max (bandwidth bound) and TP2's 32 heads keep their kernels.
tf-glm-kda-speed checks it against kda_rows at 1-16 rows on three draws (the third with -0 and large inputs) for
outputs, state and conv window: 144 of 144 cases equal. With states past the caches on an M5 Ultra a step takes
16.8 us instead of 20.7 at 2 rows, 22.0 instead of 29.0 at 4 and 33.5 instead of 45.9 at 8. In a real window at
one row the second launch cost more than the split saved (the knock-out profile's KDA class 0.75 ms instead of
0.64), so one-row steps stay on kda_rows.
…before and reads blocks inside the keys unchecked, the same products in the same order; tf-glm-attn-rows checks and times it

The scores' 32 steps over the latent dims and the values' steps over the keys each waited on their own loads.
Both now load a step ahead, and a block wholly inside the keys reads without the per-element bounds (the same
values). tf-glm-attn-rows checks every window row against its own one-row launches of the previous kernels
(tests/glm_attn_ref.metal) at 1-2,048 keys and 1-16 rows: 13,492,480 outputs equal. On an M5 Max a layer's
dense attention at 400 keys takes 24.0 us instead of 32.2 at one row, at 2,048 keys 39.2 instead of 75.6.
…with row 0's sums alone, tf_route_logits' bits; tf-glm-route-one checks and times it

The core router kernel takes four rows a threadgroup and, for a one-row window, sums row 0 four times. GLM's
glm_route_logits_one (appended to core/moe_route.metal's source at GLM's two shapes, so Flint and Step-5 keep
theirs) does row 0's products, simd sums and partials in the same order and nothing else. tf-glm-route-one checks
it against tf_route_logits for the router (288 experts, fp32) and the indexer gate (128, bf16) on 64 weight copies
and three draws of rows, the third with zeros, -0, large and tiny values: 79,872 logits equal. On an M5 Max with
the weights past the caches a router launch takes 8.58 us instead of 10.98, an indexer gate 3.05 instead of 4.13.
…two-group split and prefetched attention, with controls and launch records

glm_route_logits_one at the router's fp32 and the indexer gate's bf16 outputs meets float64 at gates 1 and 4 with each
row its own one-row launch, and gives tf_route_logits' bits. glm_kda_parts then glm_kda_norm run beside kda_rows at 2
to 16 rows. The boundary entries now run the two-group tf_hc_split against main's split, and the attention entry runs
the prefetched score and value bodies against main's one-row kernels. New controls: a router partial left out, the
second KDA group on the first group's columns, the split's post from the pre mixes and a scores step multiplying the
prefetched block. The records add the parts, norm and both router launches and the split's threadgroup bytes.
@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