Repository navigation
perf(core): affine_mm 8-byte tile loads on M5 and mlx-lm's plain SwiGLU - #871
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.
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
This was referenced Oct 15, 2026
…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
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 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
…LU option; GLM keeps its clipped default
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
This pull request adds two options to
core/affine_mmfor 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 = .plainselects mlx-lm's compiledsilu(g) * uon bf16 rows for the gate-up gather. The default stays.clipped, so GLM keeps its SwiGLU.tf-tile-checkadds a section forcore/affine_mmon a GPU with tensor units, on the same matrices and rows as the tiles. Without tensor units the section is skipped.No family uses
.plainin this pull request. Part 7 (LFM2 prompt calls on the tensor units) will use it.Receipt
Files:
runs/m5-tensor-307befc6/sessionon the fork (tile_check_6.txt) andm4-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-checkon 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: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.metalbefore (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-MTPserved 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).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_m1on 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-checkon 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
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (No family path changes in this part.)tf-tile-checksection needs tensor units.)