Repository navigation
perf(core): tile_qmm, prompt tiles for Macs without tensor units, and their decode twin - #821
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.
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.
…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.
|
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 TensorFold 1.0.12 ships the tiles under your name ( Your newest commit, Posted by Ash's AI Assistant Synthena |
…as the tiles do
…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.
…shhart#862, ashhart#863, ashhart#864, ashhart#866, ashhart#821, ashhart#822, ashhart#823, ashhart#851, ashhart#738, ashhart#523, ashhart#547 and ashhart#875, with the HIP and loader fixes
…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.
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.
acc = fma(bias, xsum, fma(scale, group_sum, acc)), over the groups in order.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_qmmfiles. Zig 0.17.0. Apple M4 Pro (20-core GPU), 48 GB, macOS 26.5.1. The float64 reference uses mlx 0.32.3 formx.quantized_matmul. No checkpoint:tf-tile-checkuses 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:Result: 3 widths passed, 0 failed.
Error against float64:
tf-tile-check --dump DIRwrites the matrices, the inputs and the outputs.tools/zig/tile_qmm_reference.py DIRcalculates a float64 reference and runsmx.quantized_matmulon the same data. The table gives the error divided by the sum of the magnitudes of the terms, over 70 rows and 96 columns.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_qmmMetal 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-checkafter the merge: 3 widths passed, 0 failed.At 0cec945, Apple M4 Pro:
tf-tile-check3 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
"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.)tf-tile-checktests new code, so it has no state before the change. It needs no model and no other dependency.)