Skip to content

perf(lfm2): prompt calls on the M5 tensor units - #873

Merged
ashhart merged 28 commits into
ashhart:mainfrom
BlivionIaG:lfm2/7-lfm2-tensor
Oct 16, 2026
Merged

ashhart merged 28 commits into
ashhart:mainfrom
BlivionIaG:lfm2/7-lfm2-tensor

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

On a GPU with tensor units (Device.tensorUnits(): Apple GPU generation 17, M5, and later), LFM2 prompt calls take core/affine_mm (#871) for the dense projections, the gathered experts and gate-up with mlx-lm's SwiGLU, and lfm2_attn_nax for attention. Decode and windows keep the tiles, the twin and lfm2_attn_mma. On M1 to M4 no tensor-unit pipeline loads, and nothing changes. It is part 7 for #820. It needs #871. Review only commits 307befc, 69fa1cb and 76c3f4f.

Bits on the M5

  • A prompt row on the tensor units no longer equals the same row in one-row decode bit for bit: core/affine_mm on the tensor units sums in a different order from the tiles' and the twin's fp32 fma chains. GLM's prompt rows on the M5 have the same property.
  • Every prompt call goes to the tensor units, whatever its row count, so a prompt gives the same bits wherever the server cuts or resumes it. A first version sent calls under 33 rows to the tiles; there a turn resumed from the cache gave a different reply than the same turn sent fresh. 69fa1cb fixes that, and the tensor-unit attention's dispatch for slices of 8 rows or fewer.
  • Drafted output still equals plain output, and concurrent still equals solo: windows and decode rows use the twin on both sides.
  • tf-lfm2-exact now compares a window's kept state with the prompt prefix followed by one-row decode, not with a fresh prompt of the whole text, and compares prompts in 128-row chunks with one-row prompt calls. These hold on every GPU.

A chunk longer than 2,048 rows runs its attention in 2,048-row slices, and the conv kernel snapshots only the rows a call keeps, so a prompt call's scratch stays at 2,048 rows.

76c3f4f fixes a crash on GPUs without tensor units. Kernels.load starts from undefined, so attn_nax kept stack bytes when no tensor-unit pipeline loaded; after 69fa1cb every prompt call reads it. On an M4 Pro, tf-lfm2-exact crashed at 8 bits (twice) and at 6 bits (once of two runs). attn_nax is now set to null first.

Receipt

Files: runs/m5-tensor-74aff635 (this code) and runs/m5-tensor-307befc6 (resume-before-fix/, the failing resume receipt before 69fa1cb) on the fork.

  • Environment: Apple M5 Ultra (64-core GPU), 256 GB, macOS 27.0, -Dmetal-std=metal4.0; Apple M4 Pro (20-core GPU), 48 GB, macOS 26.5.1. Zig 0.17.0, -Dcpu=apple_m1. mlx 0.32.3, mlx-lm 0.32.0. Checkpoint LFM2.5-24B-A2B-MLX-4bit at 667408d4.

  • Exactness on the M5, at 74aff63:

    • tf-lfm2-exact: 68 passed, 0 failed.
    • Resume: on a cache-on server, turn 2 of a two-turn chat (8,055 tokens) resumed from turn 1 (7,987 cached tokens) equals the same turn-2 messages sent cold to a new cache-on server and to a --prompt-cache-gib 0 server, and each repeat (8,051 cached). Turn 1 is equal with the cache on and off.
    • Served, 4 streams: each reply equals its solo run (4 of 4, code and chat).
  • Precision, tf-lfm2-fp64 on the M5: each projection class and the prompt attention on the same rows, tensor units, tiles and decode twin, each against float64. In every class the tensor path's worst error and worst column equal the tiles' ("0 classes where the tensor path's worst error exceeds the tiles'"). Worst, as a fraction of the terms' magnitude: conv in 6.264e-4, conv out 5.371e-4, attention q 5.088e-4, k 5.059e-4, v 4.775e-4, out 6.530e-4, dense gate 7.285e-4, up 7.907e-4, down 1.776e-4, expert gate 5.554e-4, up 6.222e-4, down 6.582e-4; gate-up SwiGLU 4.605e-3 absolute; attention 2.625e-3 (0 cached keys) and 1.280e-4 (4,000) absolute. The tensor rows differ from the tiles on 0.28% of outputs or fewer in each class; the twin differs on 0.

  • Speed at the head of the chain (fd2f8e2; 76c3f4f adds only the fix above), the M5 Ultra, one session alternated: mlx-lm, PR head 5a6ee03, this change with the tensor units on, the same with them off (a local test build), off, on, PR head, mlx-lm. Files: runs/m5-final-fd2f8e27 (summary.md).

  • Prompt speed, cold first request (tools/bench_cache.py --require-cold, 32,768 tokens, ttft_s, round 1 / round 2): PR head 8.85 / 8.85 s, tensor units on 3.06 / 3.06 s, off 8.85 / 8.85 s.

  • Cold served prefill (token arrays, --prompt-cache-gib 0, tok/s, round 1 / round 2):

    Prompt PR head tensor units on off
    2k (two requests) 4,526, 4,667 / 4,521, 4,667 8,335, 13,655 / 12,583, 13,676 4,438, 4,656 / 4,521, 4,662
    8k (two requests) 4,428, 4,427 / 4,426, 4,428 13,031, 13,029 / 13,040, 13,028 4,430, 4,430 / 4,430, 4,429
    32k 3,707 / 3,707 10,725 / 10,730 3,707 / 3,707
    64k 3,046 / 3,046 8,636 / 8,639 3,047 / 3,047
    131k (round 1) 2,243 6,199 2,243
  • Prompt speed in process (tf-lfm2-bench / mlx_cold.py, tok/s, round 1 / round 2; 2,048-row chunks at 2k, 8,192 above):

    Prompt PR head tensor units on off mlx-lm
    2k 4,489 / 4,482 12,831 / 12,760 4,542 / 4,540 12,128 / 12,137
    8k 4,462 / 4,459 13,952 / 13,946 4,565 / 4,562 13,036 / 13,009
    32k 3,829 / 3,828 11,636 / 11,638 3,835 / 3,834 11,618 / 11,610
    64k 3,137 / 3,137 9,250 / 9,250 3,134 / 3,134 9,805 / 9,816
    131k (once) 2,292 6,511 2,290 7,234
  • 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 tensor units on off mlx-lm
    1 stream, code (drafts on) 290.9 / 290.2 278.8 / 280.5 299.5 / 298.3 181.2 / 182.0
    1 stream, chat 261.0 / 260.1 263.5 / 264.4 265.0 / 264.3 not comparable
    4 streams, code, total (steady) 490.1 (509.2) / 489.8 (509.0) 557.0 (573.2) / 556.4 (572.6) 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) 536.2 (552.7) / 535.1 (551.6) 523.3 (551.5) / 522.9 (551.0) 210.1 / 211.8
    slowest first token, 4 streams, code 0.11 / 0.11 s 0.07 / 0.07 s 0.11 / 0.11 s 4.38 / 4.37 s

    The 4-stream gain comes from perf(core): tile_qmm window twins, one weight read for the rows of a window #867 and perf(lfm2): windows of 16 rows or more read each picked expert once #868. With the tensor units on, one-stream code is lower because the reply is other text: the prompt rows have other bits, and the n-gram drafts are accepted less often on that reply (216 rounds, 39 of 100 drafts accepted, against 204 rounds, 51 of 90 with the units off and at the PR head). Off gives the PR head's token_sha (f66e3fd7e509 code, 57752ac6d7f0 chat, 6627de0e2fca at 32k); on gives dcc30ceea29d, ecb946063060 and acd378159058. Each reply equals its solo run at 4 streams (12 of 12) for every build in both rounds. In process, decode after a prompt is level or higher (262.2 against 256.2 tok/s after 2k, 117.5 against 115.7 after 131k). mlx-lm's chat rate is not comparable: it sends the thinking text at the end.

  • Two-turn chat with the cache on (8,055 tokens, 7,987 cached in turn 2): turn 2 first token 0.061 s, and 0.024 s repeated (8,051 cached); mlx-lm 0.065 and 0.027 s.

  • Peak physical footprint, served (vmmap, round 1 includes the 131k prompt): PR head 16.9 / 15.6 GB, on 16.4 / 15.2, off 16.4 / 15.2; mlx-lm 13.1 GB. In process at 131k (/usr/bin/time -l): PR head 21.9 GB, on 18.2, off 18.2, mlx-lm 21.3.

  • Speed on the M4 Pro at the head of the chain (fd2f8e2; no tensor units, so this change only changes scratch sizes there):

    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 at 76c3f4f: M5: tf-tile-check 3 widths passed, 0 failed, tensor units checked; tf-lfm2-exact 68 of 68. M4 Pro: tf-lfm2-exact 68 of 68 at 8 bits (three runs), 6 bits (two runs) and 4 bits. At fd2f8e2 on the M4 Pro: 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 passed (tensor section skipped). zig fmt --check: clean. tools/zig/lean_check.py: 527, the same as main.

  • Not run, and why: 6- and 8-bit checkpoints on the M5 (only the 4-bit one is on that machine). M1 to M3. CUDA (no CUDA path for this family).

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. (Bits on the M5, above.)
  • Prompt processing is no slower than the last release. (M5: the table above. M1 to M4 keep the tiles; not measured for this part.)
  • 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 resume receipt before 69fa1cb is in runs/m5-tensor-307befc6/resume-before-fix.)
  • 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.
frag_get_tg reads a 16x16 block of a threadgroup tile in two 8-byte loads;
affine_mm's code tile uses it on GPUs with tensor units. Format.glu .plain
selects mlx-lm's compiled silu(g) * u on bf16 rows for the gate-up gather;
the default stays .clipped.

tf-tile-check adds core/affine_mm on a GPU with tensor units, on the same
matrices and rows as the tiles: rows alone equal rows together, the gather
equals the dense products, the GLU within a bf16 ulp of the host's SwiGLU,
and the float64 error next to the tiles'. Without tensor units the
section is skipped.
On a GPU with tensor units (Device.tensorUnits: Apple GPU generation 17,
M5, and later), prompt calls of 33 rows or more take core/affine_mm for
the dense projections, the gathered experts and gate-up with mlx-lm's
SwiGLU, and lfm2_attn_nax for attention. Decode, windows (at most 32
rows) and shorter prompt calls keep the tiles, the twin and
lfm2_attn_mma. Other GPUs load no tensor-unit pipelines and run as
before.

A chunk longer than 2,048 rows runs its attention and merge in 2,048-row
slices, so the partials stay at that size; the conv kernel snapshots rows
from FIRST on: every row of a recorded window and only the last row of a
prompt call.

tf-lfm2-exact adds a 4,500-token prompt in 4,096-row chunks against
2,048-row chunks. With tensor units, its prompts in 128-row chunks are
compared with 64-row chunks instead of one row at a time. tf-lfm2-fp64 compares each projection class and the
prompt attention on the tensor units, the tiles and the decode twin on
the same rows, each against float64.
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.
…mpt equals a fresh one

Prompt calls under 33 rows took the tiles while longer ones took the
tensor units, whose bits differ, so a prompt's bits depended on where the
server cut or resumed it. Every prompt call now takes the tensor units on
a GPU with them. lfm2_attn_nax writes one partial a chunk, so it always
runs with one sub-span and 16-row tiles; calls of 8 rows or fewer used
eight sub-spans and left partials unwritten for the merge.

tf-lfm2-exact compares 128-row prompt chunks with one-row prompt calls
on every GPU, and windows' kept rows with a prompt plus one-row decode.
Decode, windows and GPUs without tensor units are unchanged.
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
…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.
Kernels.load builds into an undefined value, so attn_nax's null default did not apply. Without tensor units it kept stack garbage, and every prompt call took the tensor attention path on a bogus pipeline (a SIGSEGV in setComputePipelineState on the M4 Pro).
@ashhart
ashhart merged commit 028d0e7 into ashhart:main Oct 16, 2026
4 checks passed
ashhart added a commit that referenced this pull request Oct 16, 2026
…tf-lfm2-fp64 reads in bounds at each width

Three fixes to #873 from review. naxLoad counted only finished widths, so a pipeline that failed part way through a
width left the ones before it in that width unreleased; it now counts every pipeline made and releases exactly
those. tf-lfm2-fp64's float64 reference read a code's second byte even when the code ends in its first, one byte
past a 4- or 8-bit row's end at the last code; it now reads that byte only when the code runs into it. The tool
also ran every class through the 4-bit tensor pipelines, so a 6- or 8-bit checkpoint was compared against kernels
reading its codes as 4 bits; each matrix now takes the pipelines for its own width.
ashhart added a commit that referenced this pull request Oct 16, 2026
…nst one row at a time

#873 cuts a prompt call's attention into 2,048-row slices; the fixture's prompts stopped at 300 tokens, and its one
long case compared 4,096-row calls with 2,048-row calls, never with one row at a time. Prompts of 2,047, 2,049,
4,097 and 8,193 tokens in the served 2,048-row chunks must now equal the same prompt one row at a time: the
committed logits, every conv state and every key and value byte, past one and two 4,096-key cuts.
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 fixing attn_nax initialization on GPUs without tensor units.

#873 is included in 1.0.12 as merge 028d0e7, at 76c3f4f. Its prompt tensor path is selected at every row count, including resumed tails, in families/lfm2_moe.

Our 67df462 fixes partial pipeline cleanup, bounds the FP64 reader's second-byte access and selects reference pipelines by each matrix's width. 2a431e0 adds prompt seams at 2,047, 2,049, 4,097 and 8,193 tokens.

The M3 Ultra forward/cache checks pass at all three widths. On M5 Ultra, only the synthetic tensor section ran; the real LFM2 FP64 comparison, tiny-Q control and served resume checks remain owed. Pipeline-failure injection is also still owed.

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
…tf-lfm2-fp64 reads in bounds at each width

Three fixes to ashhart#873 from review. naxLoad counted only finished widths, so a pipeline that failed part way through a
width left the ones before it in that width unreleased; it now counts every pipeline made and releases exactly
those. tf-lfm2-fp64's float64 reference read a code's second byte even when the code ends in its first, one byte
past a 4- or 8-bit row's end at the last code; it now reads that byte only when the code runs into it. The tool
also ran every class through the 4-bit tensor pipelines, so a 6- or 8-bit checkpoint was compared against kernels
reading its codes as 4 bits; each matrix now takes the pipelines for its own width.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…nst one row at a time

ashhart#873 cuts a prompt call's attention into 2,048-row slices; the fixture's prompts stopped at 300 tokens, and its one
long case compared 4,096-row calls with 2,048-row calls, never with one row at a time. Prompts of 2,047, 2,049,
4,097 and 8,193 tokens in the served 2,048-row chunks must now equal the same prompt one row at a time: the
committed logits, every conv state and every key and value byte, past one and two 4,096-key cuts.
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