Skip to content

perf(core): tile_qmm window twins, one weight read for the rows of a window - #867

Merged
ashhart merged 17 commits into
ashhart:mainfrom
BlivionIaG:lfm2/4-core-windows
Oct 16, 2026
Merged

ashhart merged 17 commits into
ashhart:mainfrom
BlivionIaG:lfm2/4-core-windows

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

This pull request makes the decode twin of core/tile_qmm cheap 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_qmm today. In this part it gets the dense window kernels through encodeTwin, which picks the kernel by row count; its expert calls pass null for the new groups argument, so they keep one threadgroup row a pick. Part 5 wires the by-expert path into LFM2.

How it works

  • One row: tf_tile_qmv, unchanged.
  • 2 to 7 rows: tf_tile_qmv_few reads each weight once for up to 4 rows, at 4, 6 and 8 bits.
  • 8 to 32 rows: tf_tile_qmv_rows unpacks the codes in registers into simdgroup MMAs, the same 8x8 MMAs as the tiles with the operands swapped.
  • Experts, by expert: tf_tile_qmv_group sorts a window's picks by expert on the GPU. tf_tile_qmv_grouped and tf_tile_qmv_grouped_glu then read each picked expert once for all of its picks.
  • 4-bit codes: every path decodes each code to its exact integer, as the tiles do. Codes i and i + 4 of a word go into a half pair as 1024 + code, and 1024 is subtracted (e9bf19d).
  • Each output keeps the tiles' arithmetic: the group sums in k order, then acc = fma(bias, xsum, fma(scale, P, acc)). Thus every path gives the tiles' bits.
  • At load, twinExact checks 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-e9bf19d9 on the fork.

  • Exactness: tf-tile-check on 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-exact with 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):

    Build 1 2 4 8 16
    dd7c516 (pre-scaled input) 8.2–8.4 11.0–11.3 16.8–17.5 28.6–28.8 49.5–50.0
    132bf2a (shift, mask, convert) 8.5–8.6 11.9–12.2 19.4–19.8 34.5–35.0 61.9–62.9
    e9bf19d (half pair) 8.0–8.2 10.7–11.0 16.2 27.4–27.5 47.0–48.6
  • Served on the M5 Ultra, three alternated rounds (bench_openai.py and bench_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):

    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.

  • 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 as p4) and runs/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-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, 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.
    • twinExact at load: the one-row, few-row and MMA paths on every model shape.
    • Served: 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, 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.py and bench_concurrent.py --levels 1,4 --alone --serial, 256 tokens, tok/s, round 1 / round 2):

    Case PR head this part
    1 stream, code (drafts on) 129.3 / 129.0 136.9 / 136.8
    1 stream, chat 127.2 / 127.0 127.2 / 127.1
    4 streams, code, total (steady) 192.4 (201.8) / 191.2 (200.6) 229.3 (242.9) / 226.8 (240.4)
    4 streams, chat, total (steady) 189.0 (201.1) / 189.0 (201.1) 221.0 (237.7) / 220.6 (237.4)
    slowest first token, 4 streams, code 0.34 / 0.34 s 0.34 / 0.34 s
  • 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_m1 with an empty cache: 166 of 166 steps, 1,989 of 1,997 tests passed, 8 skipped; golden 0 unexpected differences. tf-lfm2-exact with 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

  • 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. (Part 5 checks these with the model.)
  • 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 tiles are untouched.)
  • 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-tile-check cases test new kernels. They need no model.)
  • 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.
@ashhart

ashhart commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Thanks, @BlivionIaG. Needs an exactness fix at dd7c516. zig/kernels/metal/core/tile_qmv.metal:37 scales 4-bit input j=7 by 2^-28; the new eight-row MMA path decodes the integer directly. Witness: K=64, N=32, scales 1, biases 0, only code 7 equal to 1, and only X[7]=2^-126. Solo underflows the intermediate 2^-154 to zero; eight identical rows should retain bf16 0x0080. Please remove the range-changing pre-scaling or make both paths agree, and add this deterministic case to the twin gate. This is a source witness, not an executed Metal result.

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. I ran your case on an M4 Pro. At dd7c516, the one-row twin gives 0x0000 and the tiles give 0x0080.

@ashhart

ashhart commented Oct 15, 2026 •

Copy link
Copy Markdown
Owner

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 6b9d392e there now, with your new #822 and #823 heads, and your tf-tile-check case joins its gates. We will review #867 and #868 at their new heads next.

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

Copy link
Copy Markdown
Contributor Author

@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 ((word >> 4i) & 0x000F000F | 0x64006400), and 1024 is subtracted. 1024 + c is exact in half for c in 0 to 15, and so is the subtraction, so every code is the same exact integer. The products and the fma order do not change, and the bits do not change. The kernels compile with mathMode = safe. I merged it forward into #822, #823, #868, #871 and #873.

Window cost at load on an M4 Pro, #867 builds, three alternated rounds (ms for 1 / 2 / 4 / 8 / 16 rows):

Build 1 2 4 8 16
132bf2a (shift, mask, convert) 8.5–8.6 11.9–12.2 19.4–19.8 34.5–35.0 61.9–62.9
e9bf19d (half pair) 8.0–8.2 10.7–11.0 16.2 27.4–27.5 47.0–48.6

On an M5 Ultra, same builds: served bench_openai (code) 293 → 299–300 tok/s; 4 streams, code (bench_concurrent, total) 542 → 563–565 tok/s.

Exactness:

Files: runs/fast-4bit-e9bf19d9 on the fork.

@ashhart

ashhart commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Thank you, @BlivionIaG, for recovering the half-pair speed while retaining the exact integer repair.

At e9bf19d, the decoder in zig/kernels/metal/core/tile_qmv.metal:13 obtains each nibble as 1024+c minus 1024. Values 0 through 15 remain exact, code order stays the same, and x keeps its exponent range. The six- and eight-bit readers are unchanged. The witness still requires bf16 0x0080 at every requested width.

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

@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Thank you, @BlivionIaG, for recovering the half-pair speed while retaining the exact integer repair.

At e9bf19d, the decoder in zig/kernels/metal/core/tile_qmv.metal:13 obtains each nibble as 1024+c minus 1024. Values 0 through 15 remain exact, code order stays the same, and x keeps its exponent range. The six- and eight-bit readers are unchanged. The witness still requires bf16 0x0080 at every requested width.

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

@ashhart
ashhart merged commit 9630b3d 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 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

BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…indow, and the 4-bit twin's exact half-pair decode
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
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