Repository navigation
perf(lfm2): prompt calls on the M5 tensor units - #873
Merged
Merged
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.
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).
8 tasks done
This was referenced Oct 15, 2026
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).
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
…; no tensor attention without them
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
On a GPU with tensor units (
Device.tensorUnits(): Apple GPU generation 17, M5, and later), LFM2 prompt calls takecore/affine_mm(#871) for the dense projections, the gathered experts and gate-up with mlx-lm's SwiGLU, andlfm2_attn_naxfor attention. Decode and windows keep the tiles, the twin andlfm2_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
core/affine_mmon 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.tf-lfm2-exactnow 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.loadstarts fromundefined, soattn_naxkept stack bytes when no tensor-unit pipeline loaded; after 69fa1cb every prompt call reads it. On an M4 Pro,tf-lfm2-exactcrashed at 8 bits (twice) and at 6 bits (once of two runs).attn_naxis now set to null first.Receipt
Files:
runs/m5-tensor-74aff635(this code) andruns/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. CheckpointLFM2.5-24B-A2B-MLX-4bitat667408d4.Exactness on the M5, at 74aff63:
tf-lfm2-exact: 68 passed, 0 failed.--prompt-cache-gib 0server, and each repeat (8,051 cached). Turn 1 is equal with the cache on and off.Precision,
tf-lfm2-fp64on 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 speed in process (
tf-lfm2-bench/mlx_cold.py, tok/s, round 1 / round 2; 2,048-row chunks at 2k, 8,192 above):Decode speed, served (
bench_openai.pyandbench_concurrent.py --levels 1,4 --alone --serial, 256 tokens, tok/s, round 1 / round 2):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):
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-check3 widths passed, 0 failed, tensor units checked;tf-lfm2-exact68 of 68. M4 Pro:tf-lfm2-exact68 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_m1with an empty cache, 166 of 166 steps, 1,989 of 1,997 tests passed, 8 skipped, golden 0 unexpected differences;tf-tile-checkpassed (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
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.runs/m5-tensor-307befc6/resume-before-fix.)