Repository navigation
perf(core): tile_qmm window twins, one weight read for the rows of a window - #867
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.
|
Thanks, @BlivionIaG. Needs an exactness fix at dd7c516. |
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. I ran your case on an M4 Pro. At dd7c516, the one-row twin gives 0x0000 and the tiles give 0x0080.
|
|
Thank you, @BlivionIaG. You reproduced it on an M4 Pro within the hour, fixed it at the source (every 4-bit twin path now decodes the integer code, as the tiles do), and found the same pre-scaling in #821's one-row twin. #821 is already in the 1.0.12 candidate, so we are taking Posted by Ash's AI Assistant Synthena |
…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, a follow-up on the 4-bit fix, since #821 is in the 1.0.12 candidate: the integer decode in 6b9d392 (#821) and 132bf2a (#867) is exact, but on an M4 Pro it made the twin slower. The shift, mask and integer-to-float convert for each code cost more than the old pre-scaling. 0cec945 (#821) and e9bf19d (#867) keep the integer decode and remove the convert. Codes i and i + 4 of a word go into the mantissas of a half pair as 1024 + code ( Window cost at load on an M4 Pro, #867 builds, three alternated rounds (ms for 1 / 2 / 4 / 8 / 16 rows):
On an M5 Ultra, same builds: served Exactness:
Files: |
|
Thank you, @BlivionIaG, for recovering the half-pair speed while retaining the exact integer repair. At e9bf19d, the decoder in The underflow finding is closed in source. #867 and #868 go into our next landing batch, right after 1.0.12, where we run them on our Macs. Posted by Ash's AI Assistant Synthena |
Yup and there is a slight prefill bonus on glm5.3 flash now as well |
…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 the repaired half-pair decode and the 0x0080 control. #867 is included in 1.0.12 as merge 9630b3d, at e9bf19d. Its newer #821, #822 and #823 heads come with it. Windows of 8 to 32 rows share weight reads while keeping the one-row twin's bits. We retained the existing thread attributes when moving the twin into tile_qmv.metal. Commits 593d247 and 35c00e8 register the new launches and their compiled M1/M2 limits. The tile checks pass on M3 Ultra and M5 Ultra, including the 0x0080 witness. LFM2 forward/cache checks pass 72 of 72 at each of 4, 6 and 8 bits on M3 Ultra. In one run per configuration, shared LFM2 4-bit throughput rose about 11% at two streams and 17% at four, with replies unchanged; those timings were not alternated. Posted by Ash's AI Assistant Synthena |
…indow, and the 4-bit twin's exact half-pair decode
…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 makes the decode twin of
core/tile_qmmcheap for windows of 2 to 32 rows. It is part 4 for #820. It needs #823 (and so #821, #822). Review only commits dd7c516, 132bf2a (the 4-bit decode fix) and e9bf19d (the same decode, without the integer-to-float convert).A window is several rows in one step: the rows of a draft check, or one row from each stream. Decode time is mostly weight reads. Before this change, each extra row of a window cost about half of a one-row step. After it, the rows share the reads.
LFM2 is the only user of
core/tile_qmmtoday. In this part it gets the dense window kernels throughencodeTwin, which picks the kernel by row count; its expert calls passnullfor the newgroupsargument, so they keep one threadgroup row a pick. Part 5 wires the by-expert path into LFM2.How it works
tf_tile_qmv, unchanged.tf_tile_qmv_fewreads each weight once for up to 4 rows, at 4, 6 and 8 bits.tf_tile_qmv_rowsunpacks the codes in registers into simdgroup MMAs, the same 8x8 MMAs as the tiles with the operands swapped.tf_tile_qmv_groupsorts a window's picks by expert on the GPU.tf_tile_qmv_groupedandtf_tile_qmv_grouped_gluthen read each picked expert once for all of its picks.acc = fma(bias, xsum, fma(scale, P, acc)). Thus every path gives the tiles' bits.twinExactchecks the one-row, few-row and MMA paths against the tiles on this GPU. If a GPU differs, the family uses the tiles for every row.The one-row and few-row paths are separate kernels. A first version shared one kernel; Metal then sized its registers for the larger path, and one-row decode was 2 to 7% slower (single-stream chat at 285779c, in part 5's receipt). The split restores it.
Receipt at e9bf19d (after the 4-bit fix)
Files:
runs/fast-4bit-e9bf19d9on the fork.Exactness:
tf-tile-checkon an Apple M4 Pro and an Apple M5 Ultra: 3 widths passed, 0 failed, with the 4-bit 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 give bf16 0x0080). The case fails at dd7c516.tf-lfm2-exactwith the real 4-bit weights on the M5: 54 passed, 0 failed. Served token_sha unchanged (f66e3fd7e509 code, 57752ac6d7f0 chat), and at 4 streams each reply equals its solo run.Window cost at load, M4 Pro, three alternated rounds (ms for 1 / 2 / 4 / 8 / 16 rows; the Mac was also in use):
Served on the M5 Ultra, three alternated rounds (
bench_openai.pyandbench_concurrent.py, 256 tokens, tok/s): 1 stream, code: dd7c516 297–298, 132bf2a 293, e9bf19d 299–300. 4 streams, code, total: 555–558, 542 and 563–565.Served on the 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.Tests run: host tests at the head of the chain (perf(lfm2): prompt calls on the M5 tensor units #873, which contains this branch): 166 of 166 steps, 1,989 of 1,997 tests passed, 8 skipped; golden 0 unexpected differences.
zig fmt --check: clean.tools/zig/lean_check.py: 527.Receipt at dd7c516
Files on the fork:
runs/windows-final-87c3851a(speed, this commit asp4) andruns/cheap-windows-0b96f2a4/core-a(gates).Environment: Apple M4 Pro (20-core GPU), 48 GB, macOS 26.5.1. Zig 0.17.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, in the order PR head, this part, part 5, part 5, this part, PR head.Exactness:
tf-tile-check: 3 widths passed, 0 failed. It checks every twin path bit for bit against the tiles: widths 1 to 32 at K 1536, and 8, 9, 16, 17, 31 and 32 at the model's K 2048 and 11776; by expert at 16 and 32 rows with skewed picks (experts picked 1, 2, 3, 4, 5 or more times, and one expert picked by every row); gate-up SwiGLU; 4, 6 and 8 bits.twinExactat load: the one-row, few-row and MMA paths on every model shape.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, this part 7.8 / 10.4 / 16.3 / 27.9 / 48.7. In this part LFM2's dense projections share reads; the experts keep one read a pick. Part 5 groups them.
Decode speed, served (
bench_openai.pyandbench_concurrent.py --levels 1,4 --alone --serial, 256 tokens, tok/s, round 1 / round 2):Prompt speed: the tiles are untouched. Cold first request,
tools/bench_cache.py --require-cold, 32,768 tokens,ttft_s: PR head 31.94 / 31.92 s, this part 33.16 / 31.88 s. Cold served prefill (tok/s, round 1 / round 2): 32k 1,051 / 1,033 and 1,051 / 1,036; 64k 865 / 862 and 865 / 863; 131k (one run each in round 1, then two more in the order part 5, part 4, PR head, PR head, part 4, part 5): PR head 638 / 602 / 606, this part 629 / 606 / 605. 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 (PR head) and 15.5 GB (this part) 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-lfm2-exactwith the real weights: 54 passed, 0 failed, at 4, 6 and 8 bits.zig fmt --check: clean.tools/zig/lean_check.py: 527, the same as main.Not run, and why: CUDA (Metal code). Other Macs: I gated this change on the M4 Pro only. An mlx-lm control session on the final builds is in perf(lfm2): windows of 16 rows or more read each picked expert once #868.
Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Part 5 checks these with the model.)tf-tile-checkcases test new kernels. They need no model.)