Repository navigation
feat(lfm2): LFM2.5-24B-A2B (lfm2_moe) on Metal, 4-, 6- and 8-bit MLX affine - #822
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.
|
Hello @BlivionIaG, Thank you for LFM2.5-24B-A2B on Metal, at 4, 6, and 8 bits, with KV in bf16. This lands after #821, with our wraps at kernels.zig:8, zig/kernels/metal/lfm2/lfm2.metal:343, and README.md:179. We drop the unused row.Pipelines load at kernels.zig:48. The gate is tf-lfm2-exact with the named weights. Thank you. |
|
Thank you. The wraps and the dropped |
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.
The tiles and their twin at 4, 6 and 8 bits run gates 1 to 4 against float64 (MLX's affine form), served at the expert shapes of LFM2.5-24B-A2B, which #822 will run on them; the twin must equal the tiles bit for bit at every shape. All 20 of tile_qmm's pipelines join gate 5: the tiles by the 128 threads their source reserves, the twins, the work list and the group sums by their compiled M1/M2 limits. A Metal launch can declare the threadgroup bytes its encoder sets (the twin's 10,240), counted with its source's own against each family and the GPU, and the offline check no longer asks a launch that reserves its threads for a record, as the registry check already did not.
…717) onto the candidate # Conflicts: # CHANGELOG.md # zig/src/native/metal.zig
…717) CHANGELOG in landing order; the other files both sides changed merged clean (LFM2's open branch sits after the --vision refusal, so an LFM2 checkpoint served with --vision is refused by name).
|
Thank you, @BlivionIaG. LFM2.5-24B-A2B on the lane core, with an exactness program that checks its decode twin against the prompt tiles, is a model people asked for. It ships in TensorFold 1.0.12 under your name ( On an M3 Ultra We merged Posted by Ash's AI Assistant Synthena |
The tiles and their twin at 4, 6 and 8 bits run gates 1 to 4 against float64 (MLX's affine form), served at the expert shapes of LFM2.5-24B-A2B, which ashhart#822 will run on them; the twin must equal the tiles bit for bit at every shape. All 20 of tile_qmm's pipelines join gate 5: the tiles by the 128 threads their source reserves, the twins, the work list and the group sums by their compiled M1/M2 limits. A Metal launch can declare the threadgroup bytes its encoder sets (the twin's 10,240), counted with its source's own against each family and the GPU, and the offline check no longer asks a launch that reserves its threads for a record, as the registry check already did not.
…ld benchmark (ashhart#717) onto the candidate # Conflicts: # CHANGELOG.md # zig/src/native/metal.zig
…ld benchmark (ashhart#717) CHANGELOG in landing order; the other files both sides changed merged clean (LFM2's open branch sits after the --vision refusal, so an LFM2 checkpoint served with --vision is refused by name).
…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.
…ed under families/lfm2_moe The contributor's commit landed in this release with our wraps and the move to lfm2_moe; its head carries ashhart#822's. 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
What this changes
This pull request adds the LFM2 family to the native Metal engine. It serves LiquidAI's
LFM2.5-24B-A2B-MLX-4bit,-6bitand-8bit. The README gives the revisions. It is part 2 of 3 for #820.It needs part 1, #821 (
core/tile_qmm). This branch contains the commit of part 1. Review only commit 0c9b435. The merge commits after it only bring in main and part 1.The model
The config admits only the 24B-A2B geometry:
How it works
core/tile_qmm. Decode rows and verify windows use its twin. Both give the same bits.--learn,--speed-upand--slidefor this family before it loads the weights.Receipt
All files are on
lfm2-receipts: raw JSON, logs, token arrays and scripts. Its README lists each file.Environment: Apple M4 Pro (20-core GPU), 48 GB, macOS 26.5.1. mlx 0.32.3, mlx-lm 0.32.0. Checkpoints: 4-bit
667408d4, 6-bit1deec53e, 8-bit67ab417d(full revisions in the README). The model measurements ran at c88bd86. Thezig/srcandzig/kernelstrees at c88bd86 are equal to part 3 (this pull request and prompt reuse) before the merge of main c7137bb. I did not run the speed measurements again after the merge. The cold measurements ran with--prompt-cache-gib 0. One model process ran at a time.Exactness:
tf-lfm2-exact MODEL(real weights). It checks that prompt chunks of 128 rows are equal to one row at a time (1 to 300 tokens). It checks that windows of 1 to 32 rows after prefixes of 0 to 256 tokens are equal to one-row decode, and it keeps part of each window. It checks that two streams of different lengths in one window are equal to each stream alone, and that a kept window is equal to a new prompt of the same tokens. Result: 54 passed, 0 failed, at 4, 6 and 8 bits.tools/bench_concurrent.py --levels 1,4 --alone --serial: each reply at 4 streams was equal to its solo run, 12 of 12, for code and for chat."draft": false(scripts/draft_check.py, 3 prompts, 256 tokens each): thetoken_shaof each drafted reply is equal to the plain one. Drafts were accepted on 2 prompts (10 of 30, and 216 of 216). The third prompt had no drafts. File:tf_draft_check.log.Fidelity:
tools/zig/lfm2_fidelity.pycompares teacher-forced logits with mlx-lm. It runs mlx-lm one row at a time and in 32-row chunks. The JSON lists each top-token difference with the margins of both engines.On these 6 rows, the native NLL is lower than both mlx-lm NLLs 2 times (6-bit), between them 1 time, and higher than both 3 times. The largest difference above the nearer mlx-lm NLL is +0.0138 (4-bit, 512 tokens). The top-1 agreement of native with mlx-lm one row is from 5.4 points lower to 4.9 points higher than the agreement of mlx-lm with itself in 32-row chunks.
Decode speed: no "before" exists, because this is a new family. Against mlx-lm, 4-bit:
bench_openai.py, 256 tokens, codeThe mlx-lm server sends the thinking text at the end of a chat reply. Thus I do not compare its one-stream chat rate. In process, TensorFold runs
tf-lfm2-benchand mlx-lm runsstream_generate. Each TensorFold run alternates with an mlx-lm run on the same prompt.Prompt speed: no "before" exists. Cold prompts, 4-bit, in process, alternated, one cell for each round:
Served with the exact token arrays (
tokens/), cached tokens 0: 1,297-1,303 tok/s at 2k, 1,203-1,223 at 8k, 1,051 at 32k, 860 at 64k and 605 at 131k. At 32k, mlx-lm timed by part (mlx_parts_32k.json) spends 18.4 s in the experts, 7.1 s in attention and 5.5 s in the conv projections.6-bit and 8-bit, in process, one run each (not alternated):
Memory: in process at 131,000 tokens, the peak memory footprint (
/usr/bin/time -l) was 17.7 GB for TensorFold (tf_131k_mem.txt) and 21.2 GB for mlx-lm (mlx_131k.txt). Machine: 48 GB. The TensorFold server with--prompt-cache-gib 0had a 16.7 GB peak physical footprint after cold prompts up to 131,000 tokens (vmmap).Tests run, after the merge of main c7137bb (head e68003d), each with an empty build cache:
zig build test test-golden -Dcpu=apple_m1: 166 of 166 steps succeeded; 1,987 of 1,995 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-lfm2-exactwith the real weights after the merge: 54 passed, 0 failed, at 4, 6 and 8 bits.Not run, and why: CUDA (this family has no CUDA path). Other Macs: M4 Pro above, M5 Ultra below; I did not test M1 to M3. 5-bit: the config does not admit it. 6-bit and 8-bit served runs: I ran them in process only.
Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Resumed prompts: part 3. In this part,tf-lfm2-exactchecks that a kept window equals a new prompt.)tf-lfm2-exacttests a new family, so it has no state before the change. It needs a checkpoint path.)Apple M5 Ultra
The same code (5a6ee03, with main c7137bb merged) on an Apple M5 Ultra (64-core GPU, 256 GB, macOS 27.0), built with
-Dcpu=apple_m1. mlx 0.32.3, mlx-lm 0.32.0. 4-bit checkpoint. Files:runs/m5-ultra-5a6ee034on the fork.Exactness:
tf-lfm2-exact54 passed, 0 failed. Served replies have the same token_sha as on the M4 Pro (f66e3fd7e509 code, 57752ac6d7f0 chat). At 4 streams each reply equals its solo run (12 of 12, code and chat, drafts on and off).Decode (tok/s):
--no-draftsPrompt, in process, alternated (tok/s; mlx-lm round 2, its round 1 includes warm-up):
On the M5, mlx-lm processes prompts about 3 times faster. The tiles of
core/tile_qmmrun on the plain ALUs and do not use the M5 tensor units.Peak memory footprint in process at 131,000 tokens (
/usr/bin/time -l): TensorFold 17.9 GB, mlx-lm 21.3 GB.Window cost at load (ms for 1 / 2 / 4 / 8 / 16 rows): 3.8 / 5.2 / 7.4 / 11.4 / 18.7.