Skip to content

glm metal: faster long-context key select and windowed KDA step, bit-identical - #3403

Closed
ashhart wants to merge 7 commits into
mainfrom
lead/glm-longctx
Closed

ashhart wants to merge 7 commits into
mainfrom
lead/glm-longctx

Conversation

@ashhart

@ashhart ashhart commented Nov 15, 2026

Copy link
Copy Markdown
Owner

GLM-5.3-Flash on Metal: two exact kernel speedups for long-context decode, plus a measurement tool. Every output bit is unchanged.

Where long-context decode time goes (M5 Ultra, 90,044-token prompt against a 36-token one):

  • Plain decode is 14.87 vs 14.41 ms a token, only +3% at 90K.
  • A knock-out profile (no per-stage stamps) puts all of that growth in the indexer's scores plus top-k select, 0.22 to 0.58 ms a window. Sparse attention over the 2,048 chosen keys stays flat.

Changes:

  • Decode key lists past 3,072 blocks on many threadgroups (7b64f92, 489d9f2). A floor sampled from the row marks candidate blocks, and one threadgroup selects among them, issuing its loads before its compares. If the candidates come out too few or too many, the row is selected whole as before. The old single-group kernel stays for 3,073-8,192 blocks, where it is faster on the Ultra. Ultra: 27.7 to 17.8 µs a row at 90K, 34.2 to 17.7 at 120K. Exactness: 405 of 405 cases give glm_index_select's lists on an M5 Max and an M5 Ultra (real scores, ties, NaN, ±0, infinities, ramps, a hot span, a constant row, a forced fallback; 15 positions; 1, 3 and 16 rows).
  • A windowed KDA step (aea2e4e). Windows of 2+ rows compute every row's gates, convolutions and norms at once, then the recurrence alone, with the row kernel's arithmetic. A 4-row step goes from 33.6 to 24.6 µs. Exactness: 96 of 96 cases are bit-identical (outputs, state, conv window) at 1-16 rows, 64 and 32 heads, on both Macs.
  • Measurement only (27ad25b). The decode select honours the existing mla_select knock-out, and tf-glm-index-speed checks and times it. Behaviour is unchanged unless the profile sets it.

Measured round GPU time at 90K (alternated base, after select, after KDA, base):

  • A 4-row round plus 3 MTP steps goes from 31.38 to 31.04 ms (-1.1%).
  • A 1-row window goes from 14.87 to 14.79 ms.

Served: base == tip on 54 of 54 replies (16 bench prompts plus a 90K and a short prompt, each greedy, sampled and "draft": false). Drafted greedy == "draft": false on 18 of 18. Served tok/s moved within noise (plain at 90K 67.26 to 67.79). Saved GLM prompt states are recomputed once after upgrading, since kernel sources changed.

Tests: both check tools pass on both Macs. All Metal runtime source libraries compile. Lean finds nothing new.

  • 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.
@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