Skip to content

feat(lfm2): LFM2.5-24B-A2B (lfm2_moe) on Metal, 4-, 6- and 8-bit MLX affine - #822

Merged
ashhart merged 8 commits into
ashhart:mainfrom
BlivionIaG:lfm2/2-family
Oct 16, 2026
Merged

ashhart merged 8 commits into
ashhart:mainfrom
BlivionIaG:lfm2/2-family

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

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, -6bit and -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:

  • 40 layers: 30 short-conv layers and 10 attention layers (layers 2, 6, ..., 38).
  • Attention: 32 query heads and 8 KV heads, 64 dimensions each, QK norm, RoPE.
  • Layers 0 and 1: dense SwiGLU, 11,776 wide.
  • Other layers: 64 experts, 1,536 wide. A sigmoid router with an expert bias selects 4 experts. The bias changes only the selection.
  • The head is the embedding (tied).
  • Widths: 4, 6 or 8 bits, groups of 64. The embedding can have its own width.

How it works

  • Prompt rows use the tiles of core/tile_qmm. Decode rows and verify windows use its twin. Both give the same bits.
  • For prompts, the GPU sorts the experts' rows by expert. The gate and up projections use one fused tile.
  • Attention uses fixed chunks of 4,096 keys. Each chunk adds its 512-key parts in order. Prompt rows and decode rows run the same arithmetic in each 32-key block.
  • The engine measures the time of each window width at load. Plain rounds compete with drafted rounds.
  • The weights stay as the checkpoint stores them. They stay resident in GPU memory.
  • TensorFold refuses --learn, --speed-up and --slide for 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-bit 1deec53e, 8-bit 67ab417d (full revisions in the README). The model measurements ran at c88bd86. The zig/src and zig/kernels trees 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.
    • Drafted against "draft": false (scripts/draft_check.py, 3 prompts, 256 tokens each): the token_sha of 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.py compares 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.

    Width Fixture (tokens) NLL: native / mlx one row / mlx 32 rows Top-1 agreement with mlx one row: native / mlx 32 rows
    4-bit public-contribution-guide (512) 4.3438 / 4.3056 / 4.3300 84.4% / 86.3%
    4-bit public-changelog (2,048) 3.7455 / 3.7422 / 3.7933 90.5% / 85.6%
    6-bit public-contribution-guide (512) 4.3052 / 4.3450 / 4.3248 80.5% / 85.9%
    6-bit public-changelog (2,048) 3.6854 / 3.7033 / 3.7033 88.1% / 90.1%
    8-bit public-contribution-guide (512) 4.3253 / 4.3058 / 4.3247 86.9% / 87.5%
    8-bit public-changelog (2,048) 3.7038 / 3.7026 / 3.6920 90.2% / 90.9%

    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:

    Case TensorFold mlx-lm
    Served, bench_openai.py, 256 tokens, code 129.1 tok/s 102.9 tok/s
    Served, 4 streams, total, code / chat 192.2 / 189.1 tok/s 100.5 / 120.3 tok/s
    Served, slowest first token at 4 streams, code / chat 0.34 / 0.42 s 7.74 / 9.57 s
    In process, after 2,048 tokens (2 rounds) 124.5 / 124.6 tok/s 115.8 / 115.8 tok/s
    In process, after 8,192 tokens (2 rounds) 114.3 / 114.3 tok/s 106.2 / 105.2 tok/s
    In process, after 32,768 tokens (2 rounds) 87.7 / 86.1 tok/s 79.5 / 79.1 tok/s
    In process, after 65,536 tokens (2 rounds) 66.9 / 61.3 tok/s 59.2 / 58.9 tok/s
    In process, after 131,000 tokens 41.3 tok/s 38.8 tok/s

    The 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-bench and mlx-lm runs stream_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:

    Prompt TensorFold mlx-lm
    2,048 1,310 / 1,304 tok/s 1,311 / 1,321 tok/s
    8,192 1,255 / 1,257 tok/s 1,289 / 1,285 tok/s
    32,768 1,054 / 995 tok/s 1,078 / 1,019 tok/s
    65,536 867 / 779 tok/s 884 / 842 tok/s
    131,000 (window: 131,072) 607 tok/s 610 tok/s

    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):

    Width Prompt 8k: TensorFold / mlx-lm Prompt 32k Decode after 8k Decode after 32k
    6-bit 1,250 / 1,225 tok/s 989 / 986 90.6 / 85.5 72.4 / 67.2
    8-bit 1,242 / 1,258 tok/s 979 / 1,060 77.1 / 71.4 63.7 / 58.2
  • 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 0 had 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-exact with 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

  • 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. (Resumed prompts: part 3. In this part, tf-lfm2-exact checks that a kept window equals a new prompt.)
  • No precision traded for speed. If the bits change, the description says which sums change and why. (New family. The sums are in part 1 and 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-lfm2-exact tests a new family, so it has no state before the change. It needs a checkpoint path.)
  • 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.

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-5a6ee034 on the fork.

  • Exactness: tf-lfm2-exact 54 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):

    Case TensorFold mlx-lm
    Served, 256 tokens, code 290.2 / 290.6 / 289.8 178.8
    Served, code, --no-drafts 261.8 / 261.2 -
    Served, 4 streams, total, code 490.3 177.8
    Slowest first token at 4 streams, code 0.11 s 4.38 s
    In process, after 2,048 tokens 256.7 / 257.1 205.0 / 205.1
    In process, after 32,768 tokens 198.0 / 198.3 167.8 / 168.1
    In process, after 131,000 tokens 116.3 113.4
  • Prompt, in process, alternated (tok/s; mlx-lm round 2, its round 1 includes warm-up):

    Prompt TensorFold mlx-lm
    2,048 4,482 12,167
    8,192 4,396 13,322
    32,768 3,709 11,631
    65,536 3,052 9,817
    131,000 2,247 7,226

    On the M5, mlx-lm processes prompts about 3 times faster. The tiles of core/tile_qmm run 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.

… 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.
@ashhart

ashhart commented Oct 14, 2026

Copy link
Copy Markdown
Owner

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.

@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Thank you. The wraps and the dropped row.Pipelines load are fine with me. After the merge of main c7137bb, tf-lfm2-exact passed 54 of 54 at 4, 6 and 8 bits on the named weights.

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 0a838e3 into ashhart:main Oct 16, 2026
4 checks passed
ashhart added a commit that referenced this pull request Oct 16, 2026
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.
ashhart added a commit that referenced this pull request Oct 16, 2026
…717) onto the candidate

# Conflicts:
#	CHANGELOG.md
#	zig/src/native/metal.zig
ashhart added a commit that referenced this pull request Oct 16, 2026
…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).
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
…r families/lfm2_moe

The contributor's commit landed in this release with our wraps and the move to lfm2_moe; its head carries #822's. 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

ashhart commented Oct 16, 2026

Copy link
Copy Markdown
Owner

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 (c3b0879e2). Ours follow: 9ba3d8b55 wraps long lines and drops two unused loads, 1f3d72d45 moves the family to zig/src/families/lfm2_moe/ beside the CUDA LFM2-VL family from #844, and 01c5d178b caps the served window at the 131,072 positions its attention holds and refuses a larger --context before the load.

On an M3 Ultra tf-lfm2-exact passes at 4, 6 and 8 bits; drafted replies equal "draft": false (16 of 16, and 8 of 8 at 9k to 13k-token prompts); two and four concurrent requests equal their solo replies; one stream decodes at 194, 168 and 160 tok/s, and cold prompts read at 4,421 tok/s at 2k and 3,549 at 32k.

We merged aa55064c7 first (d7fbebe68), and your newest head, with the half-pair decode 0cec94543, came in with #867's head (9630b3d81), 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
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.
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
…ld benchmark (ashhart#717) onto the candidate

# Conflicts:
#	CHANGELOG.md
#	zig/src/native/metal.zig
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…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).
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
…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.
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