Repository navigation
perf(lfm2): windows of 16 rows or more read each picked expert once - #868
Conversation
… 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.
|
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 |
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
|
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:
|
…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.
|
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 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 |
…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.
…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).
|
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 |
…h picked expert once
…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.
…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).
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-exactwith 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 intf-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).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):
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 asp5) andruns/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-4bitat667408d4. 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.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.pyandbench_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: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.
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.
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_m1with 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
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.tf-lfm2-exactcases need a checkpoint path.)