Skip to content

perf(lfm2): windows of 16 rows or more read each picked expert once - #868

Merged
ashhart merged 20 commits into
ashhart:mainfrom
BlivionIaG:lfm2/5-lfm2-windows
Oct 16, 2026
Merged

ashhart merged 20 commits into
ashhart:mainfrom
BlivionIaG:lfm2/5-lfm2-windows

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

This pull request uses the window twins of part 4 in LFM2. For windows of 16 rows or more, each MoE layer groups the picks by expert and reads each picked expert once. It is part 5 for #820. It needs part 4, #867. Review only commit 87c3851.

Receipt at 2a5f091 (with the 4-bit fix from #867)

2a5f091 merges #867's head (e9bf19d) into this branch.

  • Exactness, Apple M4 Pro: tf-lfm2-exact with the real weights, 67 passed, 0 failed at 6 bits and at 8 bits (8 bits twice). The 4-bit case of perf(core): tile_qmm window twins, one weight read for the rows of a window #867 passes in tf-tile-check (the kernels are perf(core): tile_qmm window twins, one weight read for the rows of a window #867's).

  • Speed, Apple M5 Ultra with the tensor units switched off in a local test build: the head of the chain (fd2f8e2, which contains this branch) then runs the same path as M1 to M4. One session with the PR head 5a6ee03 and mlx-lm, alternated (mlx-lm, PR head, tensor units on, off, off, on, PR head, mlx-lm). Files: runs/m5-final-fd2f8e27 (off).

    Case (tok/s, round 1 / round 2) PR head chain head, tensor units off mlx-lm
    1 stream, code (drafts on) 290.9 / 290.2 299.5 / 298.3 181.2 / 182.0
    1 stream, chat 261.0 / 260.1 265.0 / 264.3 not comparable
    4 streams, code, total (steady) 490.1 (509.2) / 489.8 (509.0) 555.0 (580.9) / 554.3 (580.1) 177.2 / 177.9
    4 streams, chat, total (steady) 456.6 (477.9) / 456.7 (478.0) 523.3 (551.5) / 522.9 (551.0) not comparable
    window ms, 1 / 2 / 4 / 8 / 16 rows 3.8 / 5.2 / 7.4 / 11.4 / 18.7 3.7 / 4.6 / 6.2 / 10.4 / 18.7

    The same token_sha as the PR head (f66e3fd7e509 code, 57752ac6d7f0 chat, 6627de0e2fca at 32k), and at 4 streams each reply equals its solo run (12 of 12, both rounds). Prompt speed is unchanged: cold first request at 32k 8.852 / 8.847 s against 8.85 / 8.85 s; in process 2k 4,542 / 4,540 against 4,489 / 4,482 tok/s, 32k 3,835 / 3,834 against 3,829 / 3,828. The chain head also contains perf(lfm2): prompt calls on the M5 tensor units #873, which changes prompt scratch sizes on every GPU; peak footprint in process at 131k: 18.2 GB against 21.9 GB.

  • Speed, Apple M4 Pro at the head of the chain (fd2f8e2, which contains this branch):

    Case (tok/s, round 1 / round 2) PR head chain head mlx-lm
    1 stream, code (drafts on) 125.7 / 125.0 135.6 / 135.9 97.2 / 92.0
    1 stream, chat 122.8 / 121.9 122.8 / 123.0 not comparable
    4 streams, code, total (steady) 190.7 (200.1) / 190.4 (199.7) 237.4 (253.6) / 240.9 (256.3) 95.0 / 90.2
    4 streams, chat, total (steady) 186.9 (198.8) / 186.7 (198.6) 229.1 (247.1) / 226.5 (244.4) not comparable
    slowest first token, 4 streams, code 0.34 / 0.34 s 0.34 / 0.34 s 8.17 / 8.68 s
    window ms, 1 / 2 / 4 / 8 / 16 rows (five loads each) 8.0-8.3 / 11.8-12.2 / 19.7-20.2 / 35.4-36.2 / 67.5-82.1 7.9-8.2 / 10.6-11.0 / 15.9-16.3 / 27.0-28.5 / 42.6-48.8

    The same token_sha as the PR head on every request (f66e3fd7e509 code, 57752ac6d7f0 chat, 6627de0e2fca at 32k, and the prefill requests), the same draft rounds (204, 51 of 90 accepted, on code), and each reply equals its solo run (30 of 30 a session). Prompt speed is level: cold first request at 32k 33.12 / 32.08 s against 33.68 / 32.31 s; served prefill at 32k 1,028 / 1,042 against 1,036 / 1,031 tok/s, at 131k 607 against 601. Peak footprint in process at 131k: 16.8 GB against 20.2 GB (mlx-lm 19.9). The Mac was in use during the run (load average 3.2 to 7.4, logged per step); mlx-lm's first round overlapped another test and was run again (the table gives round 2 and the rerun). One session, alternated: mlx-lm, PR head, chain head, chain head, PR head, mlx-lm. Files: runs/m4-final-fd2f8e27.

Receipt at 87c3851

Files on the fork: runs/windows-final-87c3851a (this commit as p5) and runs/cheap-windows-0b96f2a4 (final/ gates, control/ mlx-lm control, ab/).

  • Environment: Apple M4 Pro (20-core GPU), 48 GB, macOS 26.5.1. Zig 0.17.0. mlx 0.32.3, mlx-lm 0.32.0. Checkpoint LFM2.5-24B-A2B-MLX-4bit at 667408d4. Baseline: feat(lfm2): prompt reuse through the lane host's store #823 head 5a6ee03. Served with --prompt-cache-gib 0, one process at a time.

  • Exactness:

    • tf-lfm2-exact (real weights): 67 passed, 0 failed, at 4, 6 and 8 bits (54 before). New cases: windows of 9 and 24 rows; four streams at prefixes 1, 127, 128 and 256 share one 32-row window, each keeps a different number of rows, then three of them share a 16-row window on the same caches. Each row is compared bit for bit with one-row decode, and each kept cache with a fresh prompt.
    • Served: every reply has the same token_sha as the PR head (f66e3fd7e509 code, 57752ac6d7f0 chat, 6627de0e2fca for the 32k prompt), and at 4 streams each reply equals its solo run (12 of 12).
  • Window cost at load (ms for 1 / 2 / 4 / 8 / 16 rows, both rounds the same to 0.2 ms): PR head 7.8 / 11.6 / 19.4 / 35.1 / 67.2, part 4 7.8 / 10.4 / 16.3 / 27.9 / 48.7, this change 7.8 / 10.4 / 16.3 / 27.9 / 45.2. Grouping starts at 16 rows, so only that width changes from part 4.

  • Decode speed, final builds, served (bench_openai.py and bench_concurrent.py --levels 1,4 --alone --serial, 256 tokens, tok/s). Order: PR head, part 4, this change, this change, part 4, PR head; round 1 / round 2:

    Case PR head part 4 this change
    1 stream, code (drafts on) 129.3 / 129.0 136.9 / 136.8 138.5 / 138.4
    1 stream, chat 127.2 / 127.0 127.2 / 127.1 127.2 / 127.3
    4 streams, code, total (steady) 192.4 (201.8) / 191.2 (200.6) 229.3 (242.9) / 226.8 (240.4) 233.3 (247.3) / 230.9 (245.1)
    4 streams, chat, total (steady) 189.0 (201.1) / 189.0 (201.1) 221.0 (237.7) / 220.6 (237.4) 220.8 (237.4) / 220.9 (237.6)
    slowest first token, 4 streams, code 0.34 / 0.34 s 0.34 / 0.34 s 0.34 / 0.34 s
  • mlx-lm control on the final builds, one session after the rounds above: mlx-lm, this change, PR head, mlx-lm. mlx-lm's own numbers show the machine state.

    Case PR head this change mlx-lm
    4 streams, code, total (steady) 186.4 (195.6) 225.2 (239.0) 92.2 / 90.5
    4 streams, chat, total (steady) 173.2 (184.3) 214.0 (230.0) not comparable
    1 stream, code (drafts on) 110.7 123.4 94.3 / 90.2
    slowest first token, 4 streams, code 0.35 s 0.35 s 8.74 / 8.60 s

    Both TensorFold builds were slower in this session than in rounds 1 and 2. At 4 streams, code, this change is 1.21 times the PR head, as in rounds 1 and 2. Same token_sha as above, and 12 of 12 equal to solo at 4 streams.

  • mlx-lm control, an earlier session with mlx-lm in it: mlx-lm, PR head, 285779c, mlx-lm, 285779c, PR head, mlx-lm. 285779c is this change's kernel code before the one-row split, the cleanups and the new tests. mlx-lm's own numbers show the machine state.

    Case PR head 285779c mlx-lm
    4 streams, code, total (steady) 182.6 (192.2) / 189.8 (199.0) 223.9 (237.1) / 223.6 (237.1) 91.6 / 88.5 / 95.4
    1 stream, code (drafts on) 118.6 / 124.8 129.9 / 127.8 94.4 / 94.5 / 94.2
    slowest first token, 4 streams, code 0.36 / 0.34 s 0.36 / 0.35 s 8.5 / 8.9 / 8.2 s

    In it, single-stream chat (plain one-row rounds) at 285779c was 2 to 7% lower than the PR head (115.2 / 115.9 against 118.0 / 123.8). The one-row split in part 4 fixed it: a drafts-off A/B, three runs each in alternated order, gives code 124.9 / 125.5 / 125.7 (PR head) and 125.0 / 124.9 / 124.6 (0b96f2a, the same tree as this change), chat 124.7 / 125.1 / 125.1 and 124.4 / 124.9 / 125.0 tok/s, with the same token_sha. The final builds above also give equal single-stream chat.

  • Prompt speed: this change does not touch prompt code. Cold first request, tools/bench_cache.py --require-cold, 32,768 tokens, ttft_s: PR head 31.94 / 31.92 s, this change 33.22 / 31.70 s. Cold served prefill (tok/s, round 1 / round 2): 32k 1,051 / 1,033 and 1,051 / 1,050; 64k 865 / 862 and 865 / 861; 131k (one run each in round 1, then two more in the order this change, part 4, PR head, PR head, part 4, this change): PR head 638 / 602 / 606, part 4 629 / 606 / 605, this change 617 / 637 / 597. The ranges overlap. 2k and 8k in the files.

  • Peak physical footprint: 16.7 GB for both in the round with a 131k prompt, 15.4 GB for both in the round without.

  • Tests run: zig build test test-golden -Dcpu=apple_m1 with an empty cache: 166 of 166 steps, 1,989 of 1,997 tests passed, 8 skipped; golden 0 unexpected differences. tf-tile-check: 3 widths passed, 0 failed. zig fmt --check: clean. tools/zig/lean_check.py: 527, the same as main.

  • Not run, and why: CUDA (no CUDA path for this family). Other Macs: I gated this change on the M4 Pro only. The mlx-lm server sends the thinking text at the end of a chat reply, so its chat rate is not comparable.

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.
  • No precision traded for speed. If the bits change, the description says which sums change and why. (No bits change.)
  • Prompt processing is no slower than the last release.
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (The new tf-lfm2-exact cases need a checkpoint path.)
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

… their decode twin

Adds core/tile_qmm: 32 x 32 bf16 MMA tiles on the exact MLX affine codes
(4, 6 or 8 bits, groups of 64), with fp32 group sums in k order. Adds
grouped tiles for rows sorted by expert, a fused gate-up GLU tile, and a
twin for 1-32 rows that gives the same bits as the tiles.

tf-tile-check (no model) checks the tiles, the twin and the grouped tiles
bit for bit. tools/zig/tile_qmm_reference.py compares them with float64.
…affine

Adds the LFM2 family on the lane core: 30 short-conv layers, 10 attention
layers and 64 experts with 4 active. Prompt rows use core/tile_qmm, and
decode rows and verify windows use its twin. Attention uses fixed
4,096-key chunks. Thus prompt, decode and window rows give the same bits.

tf-lfm2-exact checks prompt chunks, windows, partial keeps and two streams
in one window against one-row decode on the real weights.
Keeps the conv states and the attention keys and values up to a mark in
host memory, and restores them in the prompt pass. --prompt-cache-gib
sets the budget, as for the other families.
The tiles' twin (decode and windows of 1 to 32 rows) ran a threadgroup
row a row at 6 and 8 bits and four rows a code load at 4 bits, so a
window converted the same codes again for every row or every four rows.

The twin kernels move to core/tile_qmv.metal (the tiles stay in
tile_qmm.metal). One row keeps tf_tile_qmv. Windows of 2 to 7 rows take
tf_tile_qmv_few: four rows over one code load at every width, the row
count a template parameter. Windows of 8 to 32 rows take tf_tile_qmv_rows
(core/tile_qmv_rows.metal): Y^T = W X^T in simdgroup MMAs, each lane
unpacking its own column's codes in registers, the row sums as
tf_tile_xsum's fma chains. encodeTwinIndexed and encodeTwinGlu take an
optional `groups` list from encodeTwinGroup (tf_tile_qmv_group): then
tf_tile_qmv_grouped reads each picked expert once for all its slots.
Without the list they run by slot as before.

Every output keeps the tiles' bits: the same products in the same k
order a group, then the same group chain. twinExact compares every dense
twin kernel with the tiles at load. tf-tile-check compares the twin at
widths 1 to 32, and at 8 to 32 at K 2048 and 11776, and the by-expert
twin with skewed picks over 64 experts, with the tiles at 4, 6 and 8
bits. A host test ties window_slots to the kernels' TQ_SLOTS. The LFM2
call sites pass no group list.
A window's experts ran the twin a threadgroup row a slot, so a row that
picked an expert another row had picked read and converted its gate, up
and down again. Windows of 16 rows or more now group the picks by expert
on the GPU (encodeTwinGroup) and pass the list to encodeTwinGlu and
encodeTwinIndexed, which read each picked expert once for all its slots.
A row's own picks are distinct experts, so narrower windows keep a
threadgroup row a slot. The slots keep their bits.

Scratch holds the group list. tf-lfm2-exact adds windows of 9 and 24
rows and four streams at prefixes 1, 127, 128 and 256 that share 32
rows, keep unequal paths, then share 16 rows. tf-lfm2-bench's windows
section, after the budget timings, times the twin calls at widths 1 to
32 as forward.zig routes them.
@ashhart

ashhart commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Thanks, @BlivionIaG. The expert grouping maps slots back in pick order and its buffer sizing looks consistent, but this inherits #867's dense-twin blocker. With K=64, N=32, scales 1, biases 0, only code 7 equal to 1 and X[7]=2^-126, scalar input pre-scaling loses the value while eight-row MMAs preserve it. See zig/kernels/metal/core/tile_qmv.metal:37 and zig/src/core/tile_qmm.zig:132. Please carry the arithmetic fix into this stack and run the new unequal-stream/partial-keep checks plus that edge case. I have not run the hardware comparisons.

At 4 bits the one-row and few-row twin multiplied input j by 16^-j and
left code j in place. The product is the same unless x * 16^-j leaves
the float range: x[7] = 2^-126 gave 2^-154, which underflows to zero,
while the tiles and the MMA twin decode the integer and keep 2^-126.
Every 4-bit twin path now decodes the integer code.

tf-tile-check adds the case: 4 bits, K 64, code 7 is 1, scales 1,
biases 0, x[7] = 2^-126. The tiles and the twin at widths 1 to 32 must
all give bf16 0x0080.
At 4 bits the twin multiplied input j by 16^-j and left code j in
place. The product is the same unless x * 16^-j leaves the float range:
x[7] = 2^-126 gave 2^-154, which underflows to zero, while the tiles
decode the integer and keep 2^-126. The twin now decodes the integer
code.

tf-tile-check adds the case: 4 bits, K 64, code 7 is 1, scales 1,
biases 0, x[7] = 2^-126. The tiles and the twin at widths 1 to 32 must
all give bf16 0x0080.
# Conflicts:
#	zig/kernels/metal/core/tile_qmm.metal
#	zig/tests/tile_qmm_check.zig
@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Thanks, @ashhart. The fix is in this stack: 3fc6cb3 merges #867's head (132bf2a, every 4-bit twin path decodes the integer code) into this branch. At 3fc6cb3 on an M4 Pro:

  • tf-tile-check: your case (4 bits, K 64, code 7 is 1, scales 1, biases 0, x[7] = 2^-126) keeps bf16 0x0080 in the tiles and the twin at widths 1, 2, 7, 8, 16 and 32; 3 widths passed, 0 failed.
  • tf-lfm2-exact with the real weights: 67 passed, 0 failed, at 4, 6 and 8 bits. This includes the unequal-stream and partial-keep cases: windows of 9 and 24 rows, and four streams at prefixes 1, 127, 128 and 256 sharing a 32-row window with unequal keeps, then three sharing a 16-row window on the same caches.

…onversion

Each 4-bit word gives its eight codes as exact integers: codes i and
i + 4 go into the mantissas of a half pair as 1024 + code, and 1024 is
subtracted. This replaces the shift, mask and integer-to-float convert
of every code. The codes, the products and the fma order are the same,
so the bits are the same; tf-tile-check, its 0x0080 case included,
gives the same output.
# Conflicts:
#	zig/kernels/metal/core/tile_qmm.metal
The twin decoded each 4-bit code with a shift, a mask and an integer
to float conversion. It now puts codes i and i + 4 of a word in the
mantissas of a half pair as 1024 + code and subtracts 1024: one shift,
mask, or and half subtract for two codes, no conversion. The codes
are the same exact integers, so every product and fma chain is
unchanged and the twin keeps the tiles' bits (ashhart's witness
included).

One-row, few-row, per-pick and grouped expert paths share the helper.
@ashhart

ashhart commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Thank you, @BlivionIaG, for carrying the repair through the unequal-stream and partial-keep checks.

At 2a5f091, the change since our earlier review is confined to common tile_qmv code and its witness; the LFM2 family logic is unchanged. The decoder in zig/kernels/metal/core/tile_qmv.metal:13 preserves exact nibble values without pre-scaling x. The code-7/2^-126 witness requires bf16 0x0080 at widths 1, 2, 7, 8, 16 and 32.

The underflow finding is closed in source. It goes into our next landing batch with #867, right after 1.0.12.

Posted by Ash's AI Assistant Synthena

@ashhart
ashhart merged commit c98bfdc into ashhart:main Oct 16, 2026
4 checks passed
ashhart added a commit that referenced this pull request Oct 16, 2026
…nel reachable

#867 and #868 send windows of 8 or more rows to the MMA twin and 16 or more to the by-expert twin. If the load-time
check of wider lanes fails on a GPU, the backend keeps exact_width 1; the lane core then shares no forward between
streams (family_streams needs width 2 or more), so every forward is one stream's one row and only the one-row twin
runs. The facts now come from one function, and a test pins both sides: width 1 gives one stream a forward and no
drafts, the qualified width gives shared forwards across the streams.
ashhart added a commit that referenced this pull request Oct 16, 2026
…VIEW names the refused GLM pair requests rightly

One line each for #881, #882, #883, #889, #890, #709, #848, #892, #867, #868, #871 and #873, crediting their
authors. ZIG-PREVIEW said a two-Mac GLM pair refuses "temperature-1" requests; it refuses any temperature above 0
(backend.zig and engine.zig both test temperature > 0).
@ashhart

ashhart commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Thank you, @BlivionIaG, for making each picked expert's weights serve all its rows.

#868 is included in 1.0.12 as merge c98bfdc, at 2a5f091. The family edits are in families/lfm2_moe. Windows of 16 rows or more use the shared expert reads.

Our c411e55 tests the existing fallback: when the wider-lane check fails, forwards use one stream's one row, keeping both wider kernels out of that path.

On M3 Ultra, served LFM2 at 4, 6 and 8 bits gives matching concurrent, solo and plain replies by full digest. The 4-bit replies also match the previous build.

Posted by Ash's AI Assistant Synthena

BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…nel reachable

ashhart#867 and ashhart#868 send windows of 8 or more rows to the MMA twin and 16 or more to the by-expert twin. If the load-time
check of wider lanes fails on a GPU, the backend keeps exact_width 1; the lane core then shares no forward between
streams (family_streams needs width 2 or more), so every forward is one stream's one row and only the one-row twin
runs. The facts now come from one function, and a test pins both sides: width 1 gives one stream a forward and no
drafts, the qualified width gives shared forwards across the streams.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…VIEW names the refused GLM pair requests rightly

One line each for ashhart#881, ashhart#882, ashhart#883, ashhart#889, ashhart#890, ashhart#709, ashhart#848, ashhart#892, ashhart#867, ashhart#868, ashhart#871 and ashhart#873, crediting their
authors. ZIG-PREVIEW said a two-Mac GLM pair refuses "temperature-1" requests; it refuses any temperature above 0
(backend.zig and engine.zig both test temperature > 0).
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