Skip to content

perf(core): tile_qmm, prompt tiles for Macs without tensor units, and their decode twin - #821

Merged
ashhart merged 4 commits into
ashhart:mainfrom
BlivionIaG:lfm2/1-tile-qmm
Oct 16, 2026
Merged

ashhart merged 4 commits into
ashhart:mainfrom
BlivionIaG:lfm2/1-tile-qmm

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

This pull request adds core/tile_qmm. It is a matrix multiply for MLX affine weights on Apple GPUs without tensor units. It is part 1 of 3 for #820. No family uses it in this pull request. The LFM2 family pull request (part 2) uses it.

It reads the weights as the checkpoint stores them: 4-, 6- or 8-bit codes, groups of 64, bf16 scales and biases. It does not write converted weights to memory. It expands the codes in threadgroup memory, one tile at a time.

How it works

The tiles compute 32 x 32 outputs in each threadgroup.

  • The MMAs take the exact codes and the bf16 inputs. They add in fp32.
  • For each group of 64 inputs, the group sum starts at zero. The MMAs add the 64 products in k order.
  • Then acc = fma(bias, xsum, fma(scale, group_sum, acc)), over the groups in order.
  • The tiles load the next stage of inputs and codes while the MMAs run.

The grouped tiles use rows that are sorted by expert. They read each row through a row map, so no gathered copy is necessary. A work list on the GPU gives each threadgroup its expert and its rows. The GLU form computes gate and up in the same tile and applies swiglu when it stores the result.

The twin gives decode rows and verify windows (1 to 32 rows) the same bits as the tiles. At load, a family compares the twin with the tiles (twinExact). If a GPU gives different bits, the family uses the tiles for all rows.

The bits of a row do not change with the other rows in the call or with the number of rows.

4-bit decode (6b9d392, 0cec945)

At 4 bits the twin first multiplied input j by 16^-j and read code j in place. For a very small input this underflows where the tiles keep the value (@ashhart's case in #867: x[7] = 2^-126 gave 0, the tiles bf16 0x0080). 6b9d392 decodes each code to its integer, as the tiles do. 0cec945 does the same through a half pair: codes i and i + 4 of a word as 1024 + code, minus 1024, with no integer-to-float convert. The codes, the products and the fma order are the same, so the bits are the same; the speed is in #867.

Receipt

  • Environment: TensorFold f1bef8a (this branch, on main c126fe1), and again after the merge of main c7137bb (f884da0). The merge did not change the tile_qmm files. Zig 0.17.0. Apple M4 Pro (20-core GPU), 48 GB, macOS 26.5.1. The float64 reference uses mlx 0.32.3 for mx.quantized_matmul. No checkpoint: tf-tile-check uses random matrices.

  • Exactness: zig build tf-tile-check && ./zig-out/bin/tf-tile-check. It uses random 4-, 6- and 8-bit matrices (96 x 1536, 4 experts) and checks these results bit for bit:

    • 70 rows in one call are equal to each row alone.
    • The twin at 1 to 32 rows is equal to the tiles.
    • The grouped tiles are equal to the dense tiles of each expert.
    • The twin by slot and the gate-up swiglu are equal to the grouped tiles.

    Result: 3 widths passed, 0 failed.

  • Error against float64: tf-tile-check --dump DIR writes the matrices, the inputs and the outputs. tools/zig/tile_qmm_reference.py DIR calculates a float64 reference and runs mx.quantized_matmul on the same data. The table gives the error divided by the sum of the magnitudes of the terms, over 70 rows and 96 columns.

    Width tile_qmm worst / mean mlx worst / mean Worst column mean, tile_qmm / mlx
    4-bit 2.08e-3 / 2.51e-4 5.97e-3 / 8.81e-4 3.32e-4 / 1.28e-3
    6-bit 2.78e-3 / 3.30e-4 6.76e-3 / 1.06e-3 4.33e-4 / 1.40e-3
    8-bit 2.56e-3 / 3.23e-4 9.04e-3 / 1.05e-3 4.18e-4 / 1.29e-3

    At each width, the error of tile_qmm is 2.4 to 3.9 times less than the error of mlx: for the worst output, the mean and the worst column. The dump ran at 8779252. The tile_qmm Metal and Zig files at that commit are equal to the files in this pull request. Files: tile_qmm/.

  • Decode speed before and after: no change. No family uses this code in this pull request.

  • Prompt speed before and after: no change, for the same reason. For information, tf-lfm2-bench (part 2) times the tiles on the LFM2 shapes, 512 rows, with GPU timestamps: 6144 x 2048 at 6.57 TFLOPS, 2048 x 2048 at 6.40, 11776 x 2048 at 6.60, and the grouped tiles (1536 x 2048, 64 experts, 8,192 rows) at 6.45. File: tf_32k_r1.txt.

  • Tests run, after the merge of main c7137bb (head f884da0), each with an empty build cache: zig build test test-golden -Dcpu=apple_m1: 166 of 166 steps succeeded; 1,983 of 1,991 tests passed, 8 skipped, 0 failed. Golden: 0 unexpected differences. Main c7137bb gives 1,981 of 1,989 passed, 8 skipped. zig fmt --check: clean. tools/zig/lean_check.py: 527 problems, the same as main. tf-tile-check after the merge: 3 widths passed, 0 failed.

  • At 0cec945, Apple M4 Pro: tf-tile-check 3 widths passed, 0 failed. It adds 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 must all give bf16 0x0080. The case fails at f884da0 (the twin gives 0x0000) and passes at 6b9d392 and 0cec945, with the same output at both. 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.

  • Not run, and why: CUDA (this is Metal code). Other Macs: I have only an M4 Pro. The code does not use the M5 tensor units.

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off. (No serving path changes.)
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (No serving path changes. Part 2 and part 3 check these with the model.)
  • No precision traded for speed. If the bits change, the description says which sums change and why. (New code. The sums and the float64 comparison are above.)
  • Prompt processing is no slower than the last release. (No existing path changes.)
  • 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. (tf-tile-check tests new code, so it has no state before the change. It needs no model and no other dependency.)
  • 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.
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.
…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.
@ashhart
ashhart merged commit de21d93 into ashhart:main Oct 16, 2026
4 checks passed
ashhart added a commit that referenced this pull request Oct 16, 2026
ashhart added a commit that referenced this pull request Oct 16, 2026
ashhart added a commit that referenced this pull request Oct 16, 2026
ashhart added a commit that referenced this pull request Oct 16, 2026
…milies/lfm2_moe, with #821's twin fix merged into it

The contributor's commit landed in this release with our wraps, the move to lfm2_moe and the context cap; the twin fix came in with #821's head. This keeps the PR's head in the history and changes no file.
ashhart added a commit that referenced this pull request Oct 16, 2026
ashhart added a commit that referenced this pull request Oct 16, 2026
…er decode compiles

The contract records how many threads an M1 and an M2 pipeline takes for each registered Metal kernel, from offline
compiles, and fails a kernel whose compile no longer matches. #821 made the one-row twin decode 4-bit codes to integers,
as the tiles do, and its three twins now compile for M1 and M2 to 448 (was 512), 576 (704) and 576 (640) threads.
They launch with 64, well inside either. The records now say what the compiler gives; nothing else changes.
@ashhart

ashhart commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Thank you, @BlivionIaG. Prompt tiles that read the checkpoint's codes as stored, with a decode twin that gives their bits, are a good base for Macs without tensor units, and tf-tile-check makes the twin's promise checkable on any chip.

TensorFold 1.0.12 ships the tiles under your name (eb20445e1), with our 5ecfab589 wrapping the new lines and moving the tf-tile-check steps into zig/build/core_checks.zig, and your fix that decodes 4-bit codes to integers in the twin (merged in a001b863f). On an M3 Ultra and an M5 Ultra tf-tile-check passes at 4, 6 and 8 bits, the twin bit-equal to the tiles at 1 to 32 rows, and the build before your fix fails the new 2^-126 case. LFM2.5-24B-A2B (#822) runs on them.

Your newest commit, 0cec94543, the twin's half-pair decode, ships too: it came in with #867's head (merged in 9630b3d81), which keeps the one-row twin's bits and passes the 0x0080 witness on an M3 Ultra and an M5 Ultra, so this pull request shows as merged.

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
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
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…nder families/lfm2_moe, with ashhart#821's twin fix merged into it

The contributor's commit landed in this release with our wraps, the move to lfm2_moe and the context cap; the twin fix came in with ashhart#821's head. This keeps the PR's head in the history and changes no file.
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
…s integer decode compiles

The contract records how many threads an M1 and an M2 pipeline takes for each registered Metal kernel, from offline
compiles, and fails a kernel whose compile no longer matches. ashhart#821 made the one-row twin decode 4-bit codes to integers,
as the tiles do, and its three twins now compile for M1 and M2 to 448 (was 512), 576 (704) and 576 (640) threads.
They launch with 64, well inside either. The records now say what the compiler gives; nothing else changes.
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