Skip to content

perf(core): affine_mm 8-byte tile loads on M5 and mlx-lm's plain SwiGLU - #871

Merged
ashhart merged 23 commits into
ashhart:mainfrom
BlivionIaG:lfm2/6-core-tensor
Oct 16, 2026
Merged

ashhart merged 23 commits into
ashhart:mainfrom
BlivionIaG:lfm2/6-core-tensor

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

This pull request adds two options to core/affine_mm for LFM2 on the M5 tensor units. It is part 6 for #820. It needs #868 (and so #821 to #823 and #867). Review only commit 2ceb640.

  • frag_get_tg (nax.h) 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, so GLM keeps its SwiGLU.
  • tf-tile-check adds a section for core/affine_mm on a GPU with tensor units, on the same matrices and rows as the tiles. Without tensor units the section is skipped.

No family uses .plain in this pull request. Part 7 (LFM2 prompt calls on the tensor units) will use it.

Receipt

Files: runs/m5-tensor-307befc6/session on the fork (tile_check_6.txt) and m4-part6.

  • 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.

  • Exactness, tf-tile-check on the M5 (3 widths passed, 0 failed, tensor units checked). At 4, 6 and 8 bits on a 128x1536 matrix: rows alone equal rows together, the gather equals the dense products, and gate-up SwiGLU equals the host's SwiGLU (6,144 of 6,144 outputs). Error against float64 over the same 8,960 outputs, as a fraction of the terms' magnitude:

    Bits tiles worst / mean tensor units worst / mean outputs where the tensor units are worse
    4 2.21e-3 / 2.65e-4 2.21e-3 / 2.65e-4 13
    6 2.30e-3 / 3.27e-4 2.30e-3 / 3.27e-4 0
    8 2.53e-3 / 3.12e-4 2.53e-3 / 3.12e-4 0

    On the M4 Pro the section is skipped ("no tensor units (tiles only)") and the existing checks pass.

  • GLM on the M5 Ultra, the user of the 8-byte tile loads today. Files: runs/glm-m5-871.

    • tf-mm-bench, affine_mm.metal before (87c3851) and after this change, random 4-bit matrices, the dense entry at GLM's 64 x 64 tiles: 0 outputs differ at 512 x 2048 x 2048, 129 x 2048 x 2048, 1000 x 6144 x 2048, 2048 x 2048 x 6144 and 4096 x 4096 x 4096, with bf16 and with fp32 outputs. TFLOPS (bf16 out): 61.1 → 68.8, 15.6 → 19.9, 62.7 → 68.2, 73.2 → 79.9, 75.2 → 82.5.
    • Vontra/GLM-5.3-Flash-MLX-4bit-MTP served by the native engine at perf(lfm2): windows of 16 rows or more read each picked expert once #868's head 2a5f091 (before) and this branch f364a81 (after), alternated before, after, after, before; default load limit (the MTP head stays on disk), --prompt-cache-gib 0. Every reply has the same token_sha before and after (7cd4ab7084c3 code, 4d09cdba4e48 chat), the two turns of an 8k chat are equal, and at 4 streams each reply equals its solo run (4 of 4).
    • Prompt speed, the 8k chat's first token: 4.83 / 4.84 s before, 4.59 / 4.59 s after. Decode speed is level (decode does not use core/affine_mm): 78.7–78.8 tok/s before, 78.5–78.7 after; 4 streams 121.1 and 120.8–120.9. Peak footprint 167.4 GB in both.
  • Prompt speed for LFM2: n/a in this part (no family uses .plain).

  • Tests run: zig build test test-golden -Dcpu=apple_m1 on the M4 Pro with an empty cache: 166 of 166 steps, 1,989 of 1,997 tests passed, 8 skipped; golden 0 unexpected differences. tf-tile-check on the M4 Pro and the M5 Ultra: passed. zig fmt --check: clean. tools/zig/lean_check.py: 527, the same as main.

  • Not run, and why: GLM with its MTP head (it is over the default load limit; I did not raise it). CUDA (Metal code). M1 to M3: not tested.

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 family path changes in this part.)
  • No precision traded for speed. If the bits change, the description says which sums change and why. (The table above.)
  • Prompt processing is no slower than the last release. (No family uses the new option in 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 new tf-tile-check section needs tensor units.)
  • 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.
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
…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 added a commit to BlivionIaG/TensorFold that referenced this pull request Oct 15, 2026
@ashhart
ashhart merged commit 916383c into ashhart:main Oct 16, 2026
4 checks passed
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 two 8-byte code-tile reads and the plain SwiGLU option.

#871 is included in 1.0.12 as merge 916383c, at f364a81. GLM keeps its clipped default, and the affine_mm test import is carried into zig/build/core_checks.zig.

On M5 Ultra, the tile test's tensor section ran, including 6,144 of 6,144 exact gate/up SwiGLU outputs. GLM's row-product checks match the previous values. Served GLM comparisons also pass on M3 Ultra using an eight-layer fixture; no served GLM run on M5 was available.

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
…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