Skip to content

glm metal: follow-up prompt turns 3-4% sooner (index scores read queries once, expert gathers load ahead); same bits - #3458

Merged
ashhart merged 34 commits into
mainfrom
lead/glm-prompt-long
Nov 19, 2026
Merged

ashhart merged 34 commits into
mainfrom
lead/glm-prompt-long

Conversation

@ashhart

@ashhart ashhart commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

GLM-5.3-Flash on Metal: a coding-agent follow-up turn reaches its first token 3-4% sooner, with outputs unchanged.

The case. A short new chunk (500-8,000 tokens) on a cached 30-90K-token conversation. Measured on an M5 Ultra:

  • Time to first token is about 0.21 s a turn plus 0.465 ms a new token. Position matters little: 30K → 90K adds 0.6-3.4%.
  • The cache hand-off costs about 2 ms and never re-prefills.
  • Profile with routes and key lists replayed (c19c7a4, GLM_PROFILE_TAPE, so knock-outs keep later layers as served):
    • expert gate/up and down are 41-54%;
    • KDA in-projection and sparse attention already run near tensor peak;
    • the index scores are the one part that grows with position.

Changes:

  • Index scores (38cef78). The prompt index scores give each simdgroup 16 rows × 64 blocks, so a row's queries are read once per tile, not twice. Tiles are 128 rows, or 64 where 128 would leave the GPU half idle; the products, head order and sums are unchanged. Per 256-row batch: 720 → 449 µs at 90K, 313 → 198 at 32K.
  • Expert gathers (c5bcf92). The routed-expert gathers load each step's codes, scales and biases one step ahead; the tiles, products and sums are unchanged. Gate/up -6.8% at 2,048 rows and -5.7% at 500; down -5 to -6%.

Bit identity (final head, M5 Ultra):

  • Base vs head on 300, 8,192, 20,000 and 70,000-token prompts, with default and 6,144-row chunks.
  • Last-row logits, all last-row taps, KDA state, conv tail, MLA key and pooled-key hashes, and 24-token replies at MTP depth 3 are byte-equal.
  • tf-glm-prompt-select and tf-glm-mm-groups match bit for bit.

Kernel contract:

  • The new scores kernel against main's (88097af): 8 shapes covering both tile heights, NaN and infinities planted, control red, launch record.
  • A planted control for the step-ahead loads (79c9dd5).
  • Full --only glm on an M5 Max: 94 kernels pass, 54/54 controls red, 0 limit problems.

Measured on an M5 Ultra:

  • Follow-up turns, ABBA (on a build with the same kernels except always 128-row score tiles), at 90K:
New tokens Before After
523 0.458 s 0.442 s
2,023 1.169 s 1.121 s
8,023 4.115 s 4.034 s

Across 30-90K prefixes the gains are 3.1-4.1% at 523 and 2,023 tokens and 0.8-2.0% at 8,023.

New tokens Before After Change
53 0.168 s 0.161 s -3.6%
523 0.460 s 0.442 s -4.0%
2,022 1.171 s 1.117 s -4.6%
8,023 4.126 s 4.025 s -2.4%

Across 30-90K prefixes: 3.0-4.6% at 53-2,023 new tokens, 1.1-2.4% at 8,023, and 0.3-0.9% on the first (cold) turn.

Checks: lean finds nothing new. GLM contract host tests, window contract host tests, launch map (762 launches, 0 missing) and runtime-source compile gate (64 libraries) pass.

Stacked on #3448.

  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 15, 2026

Copy link
Copy Markdown
Owner Author

Final-head check done (head 1946890, base #3448 fd51c14, M5 Ultra):

  • Follow-up turns, base/head/head/base: 3.0-4.6% sooner at 53-2,023 new tokens on 30-90K cached conversations, 1.1-2.4% at 8,023. The table is in the description.
  • 48/48 replies equal in all four base/head pairings; drafted greedy == "draft": false on 16/16 in every arm.
  • Decode unchanged (greedy 101.8 vs 102.0, plain 74.7 vs 74.6 tok/s). Cold prompts: 2K 2,010 -> 2,073 tok/s, 8K and 32K flat.

Ready to land after #3448.

  • 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.
…bers from each row's slots by expert and the groups' offsets from simd prefix sums, the same bytes; tf-glm-route-select checks and times it
…ghts take one launch, glm_rms', glm_layer_norm's and glm_scale's bits; tf-glm-mla-norms checks and times it
…kernel loads them, glm_scale's bits, one launch fewer an MLA layer; tf-glm-attn-rows checks and times it
…ch combines the picks' rows by their weights as the combine kernel did, the same bits, one launch fewer each MoE layer but the last; tf-glm-hc-rows checks and times it
… group replays the route select's picks with its routine, slot u takes pick u and the last pick's first group writes the picks and weights, the same bytes; tf-glm-moe-pick checks and times it
…s a fourth role, glm_rms' bits, one launch fewer an MLA layer; tf-glm-mla-norms checks and times it
…le, combining expand and one-row picked experts, with controls and launch records

tf_route_select runs beside main's select (a frozen moe_route.metal) at GLM's 288 experts, every expert or either held
half, and at Step-5's 352. glm_mla_norms with three and four roles runs beside main's glm_rms, glm_layer_norm and
glm_scale (a frozen glm_glue.metal). The attention entry's scores kernel scales the queries as it loads them, against
main's glm_scale and one-row launches. tf_hc_expand_mix_moe and its parts run beside main's combine then expand and
split. A one-row MoE layer's gate/up groups replaying the select run beside main's select then the one-row gathers.
Gates 1-3 require the predecessor's bytes; eight new controls plant source faults red at shapes. The records add the
seven launches with this GPU's threadgroup bytes and the M1/M2 limits from --offline.
…experts' gate/up and down, the shared expert's halves, the combine and the boundary by part, and takes a later chunk (GLM_PROFILE_AT) after the rows before it
…LM_STATE_HASH): KDA states, convolution tails, MLA keys and pooled keys
…ir tiles in groups of 32 column tiles, each group taking every row tile in turn: core/affine_mm.zig's tiles, staging and sums; tf-glm-mm-groups checks and times them
…ainst core/affine_mm.zig's gathers and matmul, with controls and launch records

glm_gather_glu_32/_64 and glm_gather_scatter_32/_64 run beside tf_affine_gather_glu and tf_affine_gather_scatter on one
route over six experts (means of 2, 43 and 183 rows: both heights of each launch), NaN and infinities planted in the
activations and in down's scales (the SwiGLU's clip keeps them out of the activations). glm_mm_grouped runs beside
tf_affine_mm at 4096, 2240 and 24896 columns (a partial last group). Gates 1-3 require the predecessor's bytes; two
controls plant source faults red at shapes. The records add the five launches (nax.h: no M1/M2 build ahead of time).
…ck from a recorded pass (GLM_PROFILE_TAPE), so a knock-out leaves later layers' expert loads and selections as served; the route and the selection are timed live against it
…locks, so a row's queries are read once a tile instead of twice, in 128-row tiles of 8 simdgroups (64-row tiles where those would leave the GPU half idle): the same products, head order and sums; tf-glm-prompt-select checks them against main's kernel and times them at long positions
…'s kernel, NaN and infinities planted in the queries and weights, with a control and a launch record
…cales and biases a step ahead, so the next step's weight reads overlap this step's products: the same tiles, products and sums; tf-glm-mm-groups times them from 128 rows with the weight bandwidth
…tep's codes again in place of the next step's
@ashhart
ashhart force-pushed the lead/glm-prompt-long branch from b4d5de5 to 906e1d3 Compare November 19, 2026 10:28
@ashhart
ashhart merged commit d42deff into main Nov 19, 2026
2 of 11 checks passed
@ashhart
ashhart deleted the lead/glm-prompt-long branch November 19, 2026 10:28
ashhart added a commit to gilby/TensorFold that referenced this pull request Nov 19, 2026
ashhart added a commit that referenced this pull request Nov 19, 2026
@krunkosaurus

Copy link
Copy Markdown
Contributor

I measured this stack on a second M5 Ultra while fixing our production agent serving, and it is running there now. The numbers are for the stack as a whole (#3418, #3424, #3442, #3448 and #3458 at b4d5de5) with #3402 on top, merged onto v1.2.10 plus #3903 and #3906. The base arm is v1.2.10 plus #3903 and #3906 (62b362a).

Setup

  • Mac Studio M5 Ultra, 512 GB, macOS 27.2 (26B118). Zig 0.17.0, Metal toolchain 32023.922.4. TensorFold/GLM-5.3-Flash-MLX-4bit-MTP @ d2b17f52.
  • serve --context 1048576 --parallel 4 --max-tokens 32768 --thinking --vision --prompt-cache-gib 64 --keep-warm 900, a fresh server per arm, the machine serving nothing else.
  • One series, base then stack about 20 minutes apart, not interleaved. Thermal pressure read 1 at the end of the base arm's bench and 2 at the end of the stack's. A background photo-analysis process was using 83% of a core when the stack arm started and 0 at every later reading.

Decode

tools/bench_concurrent.py --alone --serial --tokens 256 --reps 3 --temperatures 1.0,0 --levels 1,4, aggregate tok/s:

base stack
code, sampled, 1 stream 110.7 124.5 +12.5%
code, sampled, 4 streams 123.8 133.0 +7.4%
chat, sampled, 1 stream 74.1 84.0 +13.4%
chat, sampled, 4 streams 111.8 119.1 +6.5%
code, greedy, 1 stream 99.4 108.8 +9.5%
code, greedy, 4 streams 170.3 180.8 +6.2%
chat, greedy, 1 stream 81.4 87.9 +8.0%
chat, greedy, 4 streams 154.8 155.2 +0.3%

The sampled rows include #3402's keyed drafts. I did not measure the pull requests one by one.

Same tokens

Merging

In production

Serving our agent traffic since 2026-11-19 11:11 UTC with #3903 and #3906 at 3M tokens. No warnings or errors in the first 33 minutes and 100 requests.

My agent (Claude) did the builds, the runs and this write-up. The load script and the raw JSON are available if you want them.

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