Repository navigation
#176: Run LFM2.5-24B-A2B on Apple silicon through MLX, row-exact - #177
paragontasx wants to merge 6 commits into
Conversation
|
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.
ebcb0a8 to
0f174f4
Compare
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:
tools/gpuadapter 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 commits
server/app.py: the loop'sh2dcomes fromengine/methods/hostcopy.py, which imports without Triton and hands a CUDA copy toengine/model.py'sh2d(which keepsHOST_ASYNCand the pinned path). The warm-up uses the served device instead of.cuda().tools/gpu/apple.pyandGpuProfile.backend(): "torch" on every adapter, "mlx" on Apple's. Without a CUDA deviceprofile()gives a Mac its adapter by the chip's name; the CPU and every other machine keep the base.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.mlx_board.py), the serving (mlx_serving.py), the dispatch and the tests.What to know before reading
tools/gpuis in each family's fingerprint, sotests/test_adapter_guards.py(test_goldens_hold) andtests/test_fp_same_rows.pyfail 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.pyand 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.simdgroup_multiply_accumulateon 8x8 matrices equals a sequentialfmachain. 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.python -m tools.measure runmarks its rows INVALID: it asks/healthfor 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.Lfm2MoeMLXServingrepeats the 26 methods ofLfm2MoeServing's interface, and the self-check exists on both sides. A shared base would mean editing the CUDA serving, which I cannot test.Tests
On the Mac (M5 Max, 128 GB, MLX 0.32.4), this head:
tests/test_lfm2_moe_mlx.pyholds 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.shonorigin/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 indocs/lfm2.5-24b-a2b.md):Every lookup text equals every plain text in all runs. By the client's clock (
tools.measure,decode.c1.shortanddecode.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)python -m tools.gate mapsays levelnone(docs/quality-gate.md)Quality gate
No fitting GPU: this was written and measured on Apple silicon, and
python -m tools.gate mapsays levelquick. Please addgate-maintainer. The rows this change could move on a CUDA box are none by construction (commit 1 keeps CUDA'sh2d; commit 2 adds an adapter no CUDA device matches).