Skip to content

#176: Run LFM2.5-24B-A2B on Apple silicon through MLX, row-exact - #177

Open
paragontasx wants to merge 6 commits into
TandemLLM:mainfrom
paragontasx:apple-silicon-lfm25-mlx
Open

paragontasx wants to merge 6 commits into
TandemLLM:mainfrom
paragontasx:apple-silicon-lfm25-mlx

Conversation

@paragontasx

@paragontasx paragontasx commented Nov 20, 2026 •

Copy link
Copy Markdown

Ticket: #176

What and why

LFM2.5-24B-A2B runs on a Mac's GPU through MLX, in BF16, one request at a time, with the lookup drafter and StairCut. Closes #176.

This replaces the first version of this pull request (one commit, 7,257 lines). What changed since:

  • Apple silicon is a tools/gpu adapter now (apple.py), and the family's server builder picks its engine by a new adapter question, backend(). The first version checked for a Mac inside the family's folder to keep the fingerprints still; that went around the adapter layout.
  • The 4-bit weight path is gone from this pull request (7 kernels and their branches). It was measured, not served.
  • Six commits instead of one, each passing its own tests.
  • The load-time self-check covers more block sizes, and the tests hold every row kernel at every block size from 1 to 33.

The commits

  1. server/app.py: the loop's h2d comes from engine/methods/hostcopy.py, which imports without Triton and hands a CUDA copy to engine/model.py's h2d (which keeps HOST_ASYNC and the pinned path). The warm-up uses the served device instead of .cuda().
  2. tools/gpu/apple.py and GpuProfile.backend(): "torch" on every adapter, "mlx" on Apple's. Without a CUDA device profile() gives a Mac its adapter by the chip's name; the CPU and every other machine keep the base.
  3. The Metal kernels (engine/models/lfm2_moe/csrc/mlx_kernels/, 23 files) and their wrappers (mlx_kernels.py). MLX's own matmul and attention choose their kernel by the number of rows, so a verify row and the decode step's row of the same token round differently. These compute every row in an order fixed by the weight's shape, the contract of the family's Triton kernels (kernels.py), stage for stage: residual and RMS norm, the short conv over the row's path, q/k norm and rotary, attention over the cache and the row's ancestors in fixed chunks, the router, the dense projections, the experts.
  4. The board (mlx_board.py), the serving (mlx_serving.py), the dispatch and the tests.
  5. The M5 Max price file and three tools (exactness on the real checkpoint, a bench, the price measurement).
  6. Docs and CHANGELOG.

What to know before reading

  • Fingerprints. Every file under tools/gpu is in each family's fingerprint, so tests/test_adapter_guards.py (test_goldens_hold) and tests/test_fp_same_rows.py fail on this branch. I left the goldens as they are, as Kernels: Ada (sm_89) Triton kernels with load-time verdicts #144 did. No CUDA row changes: engine/model.py and every CUDA kernel are the bytes they were. If you would rather have them moved here, I will add one commit that does only that.
  • Bit equality is measured, not guaranteed. The scalar kernel (1 or 2 rows) and the matrix-unit kernel (3 rows and more) write the same bits because, on this GPU, simdgroup_multiply_accumulate on 8x8 matrices equals a sequential fma chain. That is measured on one chip (M5 Max) and one MLX version (0.32.4). The self-check at load is the gate: 101 verify rows against decode steps, byte for byte (a 24-row chain, then chains of 2, 3, 4, 5, 9, 16 and 32 rows and a tree after a 300-token prompt), and one differing byte turns speculation off.
  • The gate's measurement does not run against this serving yet. python -m tools.measure run marks its rows INVALID: it asks /health for the slots decoding at once, which the server reports from its slot pool, and this serving has one slot and no pool. I did not change shared server or tool code for it.
  • Duplication I did not resolve. Lfm2MoeMLXServing repeats the 26 methods of Lfm2MoeServing's interface, and the self-check exists on both sides. A shared base would mean editing the CUDA serving, which I cannot test.
  • Off for now on the Mac: the prefix, session and disk caches, the KV pool, side slots and joint passes.

Tests

On the Mac (M5 Max, 128 GB, MLX 0.32.4), this head:

$ python tests/test_lfm2_moe_mlx.py
26 passed
$ python tests/test_gpu_adapters.py
15 passed
$ python tools/mlx_lfm_exact.py <LFM2.5-24B-A2B checkpoint>
verify rows against sequential decode: 36 rows, 0 differ in any bit
$ python server/app.py --model <checkpoint> --drafter mtp
[lfm-mlx-selfcheck] PASS: {'ok': True, 'bitwise': True, 'rows': 101, 'first_diff': None, 'max_diff': 0.0, 'picks': True}
$ python tools/fn_lookup_ab.py --compare plain.json lookup.json
all same: True

tests/test_lfm2_moe_mlx.py holds every row kernel's block to its rows alone at every size from 1 to 33, runs the served attention shape (head 64, GQA 4) through the board up to 64 rows after a 300-token prompt, checks that the self-check refuses one unit in one row, and checks the serving's state after the server's in-place ask. It skips where MLX is not installed.

On a DGX Spark, CPU only, tools/engine_suite.sh on origin/main (8713712) and on this head's tree, 320 files each: the two fingerprint tests above fail on this head and pass on main; no other file differs. Six files fail on both in that environment (test_client_smoke, test_fn_ref, test_q35m_ref, test_setup_cli, test_side_images, test_vision).

Speed, one greedy request a cell through the server, 512 tokens, the server's own timings: the median of 3 runs with the plain and lookup servers started in turn, tok/s (ranges in docs/lfm2.5-24b-a2b.md):

prose chat code edit copy
plain 94.4 94.7 94.6 94.1 94.5
lookup 93.2 93.1 96.3 315.2 320.1

Every lookup text equals every plain text in all runs. By the client's clock (tools.measure, decode.c1.short and decode.c1.1k, 30 rounds): 85.5 tok/s plain, 85.0 with the lookup.

  • python tests/script_suite.py --all, or the test files this change touches (TandemApp's suite and its page run in TandemApp's repository)
  • The engine writes the same text as before this change (docs/exactness.md), or the change says why it may not: no CUDA row changes; on the Mac the lookup's text is the plain text, by the checks above.
  • The gate report for this head is below, or python -m tools.gate map says level none (docs/quality-gate.md)
  • I have read the Contributor License Agreement in CONTRIBUTING.md and I agree to it.

Quality gate

No fitting GPU: this was written and measured on Apple silicon, and python -m tools.gate map says level quick. Please add gate-maintainer. The rows this change could move on a CUDA box are none by construction (commit 1 keeps CUDA's h2d; commit 2 adds an adapter no CUDA device matches).

@github-project-automation github-project-automation Bot moved this to Triage in TandemLLM Nov 20, 2026
@github-actions github-actions Bot added engine/core Engine: model code, loaders, the decode and verify loop engine/drafting Engine: drafters and speculation (lookup, StairCut, MTP, DFlash2) server/api Server: the OpenAI API, tool calls, streaming, structured output models/lfm2.5 Model: LFM2.5-24B-A2B ops Service scripts, profiles, watchdog, release tooling labels Nov 20, 2026
@paragontasx

Copy link
Copy Markdown
Author

What the two kernel sets share. They implement the same seven stages of a row with the same contract (no reduction crosses rows, no tile depends on the row count): residual + RMS norm, the short conv over the row's path, q/k norm + rotary, attention over the cache and the row's ancestors in fixed chunks, the router, the dense projection, and the experts with their weighted sum. kernels.py and mlx_kernels.py map one to one on those. Above them the reference (ref.py), the config, the engine interface, the lookup drafter, StairCut and the price-file format are already shared.

What is duplicated. Lfm2MoeMLXServing repeats all 26 of its methods from Lfm2MoeServing; the load-time self-check exists twice; and the Mac's chip name and price key sit in lfm2_moe/apple.py instead of a tools/gpu adapter.

How it is handled now. The family's server builder (lfm2_moe/dispatch.py) checks for Apple silicon and loads the MLX serving there, the CUDA serving everywhere else. Both expose the same engine interface, so the server's loop doesn't know which one it has. Outside the family's folder the only platform-related edits are in server/app.py: the loop's h2d import now loads without Triton, and the warm-up uses the served device instead of .cuda().

How it ended up this way. My first version followed the tools/gpu pattern: an AppleSilicon(GpuProfile) next to gb10 and ada, plus a backend() on the base profile that the family's builder dispatched on. Every file under tools/gpu is in each family's fingerprint, so that moved all six goldens in tests/test_adapter_guards.py and would have rebuilt disk caches on CUDA boxes for a change that touches no CUDA row. I took it out to keep this PR Apple-only.

… Triton

engine/methods/hostcopy.py copies plainly off CUDA and hands a CUDA copy to
engine/model.py's h2d, which keeps HOST_ASYNC and the pinned path. The
server's loop imports it instead of engine.model, so the loop loads on a
machine without Triton. The warm-up uses the served device, not .cuda().
No CUDA row changes.
tools/gpu/apple.py claims Apple silicon by the chip's name, never by a CUDA
capability, builds no inline extension and keys per-device files by the chip
(apple-m5-max). backend() says which array library a device's models run on:
torch on every adapter but Apple's, which answers mlx. Without a CUDA device
profile() gives a Mac its adapter for the current device; the CPU and every
other machine keep the base.

The fingerprint goldens are left as they are: every file under tools/gpu is
in each family's fingerprint, so tests/test_adapter_guards.py test_goldens_hold
and tests/test_fp_same_rows.py fail on this commit until the goldens move.
No CUDA row changes.
MLX's own matmul and attention choose their kernel by the number of rows, so
a verify row and the decode step's row of the same token round differently.
These kernels compute every row in an order fixed by the weight's shape, the
contract of engine/models/lfm2_moe/kernels.py: residual and RMS norm, the
short conv over the row's path, q/k norm and rotary, attention over the cache
and the row's ancestors in fixed chunks, the router, the dense projections
and the experts. mlx_kernels.py wraps them; its _SRC table names every
source for tests/test_native_sources.py.
…oard

mlx_board.py is the reference's block algebra on MLX with the row kernels;
mlx_serving.py is the engine interface the server's loop drives, in BF16, one
request at a time, with the lookup drafter and StairCut. The family's builder
(dispatch.py) picks it where the device's adapter answers backend() == mlx.

The load-time self-check compares 101 verify rows with decode steps byte for
byte (a 24-row chain, then chains of 2, 3, 4, 5, 9, 16 and 32 rows and a tree
after a 300-token prompt); one differing byte turns speculation off. The
prefix, session and disk caches, the KV pool, side slots and joint passes are
off.

tests/test_lfm2_moe_mlx.py: 25 tests, skipped where MLX is not installed:
every row kernel's block against its rows alone at every size from 1 to 33,
the served attention shape through the board up to 64 rows, the self-check
refusing one unit in one row, the serving's state after an in-place ask.
It also holds the dispatch to the adapter's backend() answer, on any machine.
docs/reference/variables.md is regenerated (mlx_serving.py reads QSE_WEIGHTS).
tools/mlx_lfm_exact.py checks the real checkpoint's verify rows against
sequential decode; mlx_lfm_bench.py times decode and prefill and compares
logits with mlx-lm; mlx_lfm_prices.py measures StairCut's price file.
docs/lfm2.5-24b-a2b.md gains the Apple silicon section with its numbers,
how they were measured and its checks; CHANGELOG.
@paragontasx
paragontasx force-pushed the apple-silicon-lfm25-mlx branch from ebcb0a8 to 0f174f4 Compare November 20, 2026 13:52
@0xBakeer 0xBakeer added the gate-maintainer The author has no fitting GPU: a maintainer runs the quality gate instead label Nov 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

engine/core Engine: model code, loaders, the decode and verify loop engine/drafting Engine: drafters and speculation (lookup, StairCut, MTP, DFlash2) gate-maintainer The author has no fitting GPU: a maintainer runs the quality gate instead models/lfm2.5 Model: LFM2.5-24B-A2B ops Service scripts, profiles, watchdog, release tooling server/api Server: the OpenAI API, tool calls, streaming, structured output

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LFM2.5-24B-A2B on Apple silicon through MLX, with row-exact verify

2 participants