Skip to content

Metal EXL3 routed experts for DeepSeek-V4.1, exact - #5063

Merged
ashhart merged 6 commits into
ashhart:mainfrom
BlivionIaG:feat/exl3-metal-experts-v41
Nov 26, 2026
Merged

ashhart merged 6 commits into
ashhart:mainfrom
BlivionIaG:feat/exl3-metal-experts-v41

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: Metal ran EXL3 only as a dense projection (tf_exl3_matmul_hoisted, and the one-row tf_exl3_matmul_row of
#5050); DeepSeek-V4.1's routed experts had no EXL3 path on Metal.
After: zig/kernels/metal/deepseek_v41/experts_exl3.metal runs the routed experts over EXL3 trellises: main's route
plan, main's tf_exl3_rotate_in per (token, expert) pair, new slot kernels tf_dsv41_x3_up (gate and up) and
tf_dsv41_x3_down in which a lane owns a column and adds each pair's products in tf_exl3_matmul_row's order, a new
epilogue tf_dsv41_x3_act (gate and up rotated out to bf16, then the MXFP4 lane's clamped SwiGLU and route weight,
rounded to bf16), main's tf_exl3_rotate_out into the MXFP4 lane's per-pair out layout, and main's combine. Experts
may differ in width and codebook. Every slot sum, act, expert output and combined row is bit-equal to a new serial
reference (experts_exl3_ref.zig), and each pair's sums equal tf_exl3_matmul_row run on that pair alone. No family
serves Metal EXL3 yet.

Receipt

  • Main against branch: no family calls Metal EXL3, so no served path changes; speed was not measured.
  • Boxes it reaches: deepseek_v41/metal/experts_exl3.zig's up and down; no family calls them.
  • New switch? None.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: head
    36bb2af604 on Metal EXL3 projection: an exact one-row decode kernel for the DeepSeek-V4.1 dense shapes #5050 25024cf736 (main 5c71eb84cd), Zig 0.17.0, Xcode's Metal toolchain; Apple M4 Pro (48 GB,
    macOS 27.2); no checkpoint (synthetic trellises: mul1 at 2 to 5 bits by expert, and
    mul1 2.5, mcg 2 and 3inst 3 bits mixed).
  • Paths and platforms the change reaches, which I ran and which I could not: the new Metal expert lane only. Ran M4
    Pro and M5 Ultra; not M1 to M3.
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: n/a, no served
    path; instead (a) four contract registrations hold slot sums, act, expert outputs and combined rows byte-equal to the
    serial reference (the one-row reference's dot per pair, flush-to-zero fp32 rotations, main's clamped SwiGLU and
    combine) at 1, 2, 3, 4, 6, 8, 12 and 16 rows and at one and four rows of 5120 x 2304, NaN as NaN; (b) a lane runs
    plan, up, down and combine at 1, 2, 3, 4, 8 and 16 rows and holds every pair's sums and outputs equal to
    tf_exl3_matmul_row launched on the pair alone and to routed tf_exl3_matmul_hoisted; (c) float64, reported and not
    a gate: row 0 at eight columns, max |gpu - f64| 6.9 at max |y| 3313 (gate, up, act and expert outputs are bf16, as
    in the MXFP4 lane).
  • The one value the reference takes from the device is silu: Metal's precise::exp and divide are not reproducible on
    the host (the device's silu differs from float64 rounded to fp32 at 3205 of 65280 finite bf16 gates, worst relative
    error 1.6e-7), so the reference reads it from a table tf_dsv41_x3_silu fills with the shipped function over every
    bf16 gate.
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you
    can't): no checkpoint path reaches it yet.
  • Decode speed before and after, from tools/bench_openai.py: n/a, no family serves Metal EXL3.
  • Prompt speed before and after: n/a, no family serves Metal EXL3.
  • Licence of any new weights or drafter, or "n/a": n/a.

How it was derived: docs/receipts/dsv4.1-exl3-rebuild/p3.md lists the main and #5050 files and lines consulted, the
arithmetic (routing, rotations, the slot chain, the epilogue's order of clamp, SwiGLU and weight, the down's scatter
and the combine, the flush-to-zero emulation and the silu table), notes that CUDA's DeepSeek-V4.1 experts keep the
SwiGLU in fp32 and weight in the combine while this follows main's Metal MXFP4 lane, and states that no file of the
#2373-derived stack was opened. git blame attributes every added line to this commit; the normalized-line comparison
against the mlx-serve, mlx-stream and turboexl3 histories gives a longest run of 1 to 2 lines in the new files
(abstracted: 1 to 6), against 1 to 2 (abstracted 1 to 4) for main's own expert and EXL3 files as the control.

Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract, and the kernel each one replaces: tf_dsv41_x3_up with tf_dsv41_x3_act
    (mul1 2 to 5 bits by expert; mul1, mcg and 3inst by expert) and tf_dsv41_x3_down with main's combine (f32, mul1 2
    to 5 bits; bf16, mixed codebooks), new, against the serial reference; launch records dsv41 x3 up, dsv41 x3 down,
    dsv41 x3 act (32 threads, reserved by the source).
  • A planted control for each new kernel, and each one fails as planted: the down without its TwoSum error term, red at
    shapes (a slot sum differs at 1x512x256); the act without the route weight, red at shapes (an act differs at
    1x256x1024).
  • Each backend's SUMMARY line and exit status (tf-kernel-contract --only "x3 exl3"):
    • Metal, M4 Pro: SUMMARY 4 kernels: 4 pass gates 1-4, 0 fail, 0 known findings; 3 controls: 3 red as planted, 0 missed; 0 limit problems; gates 169793 ms, exit 0.
    • Metal, M5 Ultra (at the previous head d49bf194ef; not rerun at this head, the change since is a validation guard and one more planted control): SUMMARY 4 kernels: 4 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems, exit 0; test-exl3 14/14 and test-exl3-gpu 26/26 tests.
  • Chips I could not run: M1, M2 and M3 (gate 5's offline compiles for M1 and M2 pass: 32 threads each).

Checks on this head

  • Lean checker, python3 tools/zig/lean_check.py: 164 on Metal EXL3 projection: an exact one-row decode kernel for the DeepSeek-V4.1 dense shapes #5050's head and 164 on this head, 0 new
    (python3 tools/pr_check.py --base 25024cf736: 0 errors, 1 advisory warning, the --pr number not yet known).
  • M4 Pro: zig build test-exl3 test-exl3-gpu test-metal-launch-map-host test-kernel-contract -Dcpu=apple_m1:
    56/56 steps, 297 pass, 0 fail, 0 skip.
  • zig fmt --check build.zig zig and git diff --check: clean.
  • CI: not run yet (no pull request).
  • Not run, and why: CUDA, HIP and XPU are untouched; M1 to M3 have no device here; zig build test test-golden (the
    full host suite) was not run, only the steps above.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @BlivionIaG. The slot chains retain the one-row order, and the mixed-width and serial comparisons are useful.

Two fixes remain: a short descriptor table passes validation while the full route plan can send combine to skipped, unwritten output; and a failed kernel append frees a probe that the suite then frees again. Please refuse or consistently filter short tables and make probe publication transfer ownership once, with poisoned-output and failure-path tests. The inherited one-row cleanup still needs its shared fix.

  • Synthena

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

@BlivionIaG, about the probe-registration cleanup our review asked for: the same two-append pattern was on main in 85 places, and #5058 (now merged) fixes it there with one helper, contract.register. It reserves room in both lists before publishing, so a failed append can no longer free a probe the suite still holds:

errdefer p.deinit(); // with errdefer s.a.destroy(p) after create, as the suite's teardown frees it
try ct.register(s.a, &s.probes, &s.kernels, p, p.kernel());

Using it for this PR's new suites closes that item. zig/src/core/contract/register_test.zig shows the failure cases it covers.

  • Synthena

@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart. Merged #5050's fix. Short tables are now refused: check returns error.ShortExpertTable before any launch. A poisoned-output test shows the outputs untouched on refusal and fully written with the full table; before the fix the short table ran. Probes now reach the suite through contract.register, and the failure-path test that crashed on the double free passes. The M4 gates are green. Head d49bf194ef.

@BlivionIaG

Copy link
Copy Markdown
Contributor Author

M5 Ultra results at d49bf194ef (Apple M5 Ultra, macOS 27.0, Zig 0.17.0): zig build test-exl3 14/14 tests; zig build test-exl3-gpu 26/26 tests; tf-kernel-contract --only "x3 exl3": SUMMARY 4 kernels: 4 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems, exit 0; --only exl3 (9 kernels, 3 controls) also passes, exit 0. The contract counts match the M4 Pro.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @BlivionIaG. The short descriptor table is now refused before launch, the poisoned-output and full-table tests cover both up and down, and the probes use contract.register with failure-path coverage. The inherited one-row cleanup is also present.

One coverage item remains in zig/tests/contract/metal_dsv41_x3.zig:326-345. The TwoSum control selects the down variant, and the other control changes only the activation epilogue. Neither launches a deliberately broken tf_dsv41_x3_up, so the two red controls do not establish a negative test for every new kernel.

Please add an up-variant registration using the existing TwoSum mutation and require it to fail at shapes. The normal up registrations already compare slot sums and activations byte for byte; this adds the corresponding planted negative control. No device run is needed to identify that coverage gap.

  • Synthena

@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart. Added a tf_dsv41_x3_up registration with the down control's TwoSum mutation (error term dropped) as a planted control required to fail at shapes. On an M4 Pro it goes red as planted ("slot sum 0 differs" at 1x256x1024). --only "x3 exl3": 4 kernels pass (normal registrations unchanged), 3 controls red as planted, 0 missed, 0 limit problems. Merged your rebased #5050 (ba4453b2fd). Head 36bb2af604.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you for adding the up-variant TwoSum control.

The new registration really launches tf_dsv41_x3_up with the TwoSum error term removed and requires a red result at shapes. Your supplied device result shows the intended slot-sum mismatch at 1 x 256 x 1024. The four normal kernel registrations, short-table refusal, poisoned-output checks and contract ownership-failure tests remain intact.

That closes the remaining source request. It is ready to land once we resolve the four listed current-main conflicts. No additional device run is a prerequisite. We will preserve both sides during integration; no rebase is requested.

  • Synthena

@ashhart
ashhart force-pushed the feat/exl3-metal-experts-v41 branch from 36bb2af to fd50b96 Compare November 26, 2026 00:17
@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you, @BlivionIaG. We checked what can reach this on main: no served model or request type selects the routed Metal EXL3 path yet (DeepSeek-V4.1's served MoE stays on its MXFP4 pipelines), so no default output byte can change. We added one test commit (9d2ac43): a Linux host oracle that recomputes the routed expert arithmetic independently and compares it bit for bit, with a planted change that fails it. With your Metal contract evidence and that check, this goes into a landing train now.

  • Synthena

ashhart and others added 6 commits November 26, 2026 23:20
Routed experts over EXL3 trellises now run on Metal: main's route plan,
tf_exl3_rotate_in per (token, expert) pair, new slot kernels for gate/up
and down (tf_dsv41_x3_up, tf_dsv41_x3_down) in which a lane owns a column
and adds each pair's products in tf_exl3_matmul_row's order, an epilogue
(tf_dsv41_x3_act) that rotates gate and up out to bf16 and applies the
MXFP4 lane's clamped SwiGLU and route weight, tf_exl3_rotate_out into the
MXFP4 lane's per-pair layout, and main's combine. Experts may differ in
width and codebook.

A new serial reference (experts_exl3_ref.zig) composes the one-row
reference's dot per pair with flush-to-zero fp32 rotations, main's
clamped SwiGLU and main's combine; the kernel contract holds every slot
sum, act, expert output and combined row bit-equal to it at 1 to 16 rows
and at V4.1's sizes, each pair's sums equal to tf_exl3_matmul_row run on
that pair alone, with two planted controls (the TwoSum error term dropped,
the route weight dropped).
…through contract.register

check returns error.ShortExpertTable when a table holds fewer descriptors than the call's experts, before any launch, so no pair leaves a row the combine reads unwritten. test-dsv41-exl3-contract poisons the outputs and holds them untouched on refusal and fully written with the full table, and fails each allocation of the suite's create in turn.
The up registration with TwoSum's error term dropped goes red at shapes (a slot sum differs at 1 x 256 x 1024), so every new kernel has a negative control.
@ashhart
ashhart force-pushed the feat/exl3-metal-experts-v41 branch from d393336 to e1ccada Compare November 26, 2026 23:20
@ashhart
ashhart merged commit 1a3a10d into ashhart:main Nov 26, 2026
2 of 12 checks passed
@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you @BlivionIaG. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

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