Repository navigation
Metal EXL3 routed experts for DeepSeek-V4.1, exact - #5063
Conversation
|
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.
|
|
@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, 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.
|
|
Thanks @ashhart. Merged #5050's fix. Short tables are now refused: |
|
M5 Ultra results at |
|
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 One coverage item remains in Please add an up-variant registration using the existing TwoSum mutation and require it to fail at
|
|
Thanks @ashhart. Added a |
|
Thank you for adding the up-variant TwoSum control. The new registration really launches 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.
|
36bb2af to
fd50b96
Compare
|
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.
|
9d2ac43 to
d393336
Compare
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.
d393336 to
e1ccada
Compare
|
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. |
What this changes
Before: Metal ran EXL3 only as a dense projection (
tf_exl3_matmul_hoisted, and the one-rowtf_exl3_matmul_rowof#5050); DeepSeek-V4.1's routed experts had no EXL3 path on Metal.
After:
zig/kernels/metal/deepseek_v41/experts_exl3.metalruns the routed experts over EXL3 trellises: main's routeplan, main's
tf_exl3_rotate_inper (token, expert) pair, new slot kernelstf_dsv41_x3_up(gate and up) andtf_dsv41_x3_downin which a lane owns a column and adds each pair's products intf_exl3_matmul_row's order, a newepilogue
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_outinto the MXFP4 lane's per-pairoutlayout, and main's combine. Expertsmay 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 equaltf_exl3_matmul_rowrun on that pair alone. No familyserves Metal EXL3 yet.
prompt attention and the drafter.
25024cf736); one commit,36bb2af604.atc overlap --json: not run (ATC is not installed on this machine).Receipt
deepseek_v41/metal/experts_exl3.zig'supanddown; no family calls them.36bb2af604on Metal EXL3 projection: an exact one-row decode kernel for the DeepSeek-V4.1 dense shapes #505025024cf736(main5c71eb84cd), 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).
Pro and M5 Ultra; not M1 to M3.
tools/bench_concurrent.py --alone --serialortoken_shaagainst"draft": false: n/a, no servedpath; 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_rowlaunched on the pair alone and to routedtf_exl3_matmul_hoisted; (c) float64, reported and nota 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).
precise::expand divide are not reproducible onthe 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_silufills with the shipped function over everybf16 gate.
can't): no checkpoint path reaches it yet.
tools/bench_openai.py: n/a, no family serves Metal EXL3.How it was derived:
docs/receipts/dsv4.1-exl3-rebuild/p3.mdlists the main and #5050 files and lines consulted, thearithmetic (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 blameattributes every added line to this commit; the normalized-line comparisonagainst 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
tf_dsv41_x3_upwithtf_dsv41_x3_act(mul1 2 to 5 bits by expert; mul1, mcg and 3inst by expert) and
tf_dsv41_x3_downwith main's combine (f32, mul1 2to 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).shapes(a slot sum differs at 1x512x256); the act without the route weight, red atshapes(an act differs at1x256x1024).
SUMMARYline and exit status (tf-kernel-contract --only "x3 exl3"):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.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-exl314/14 andtest-exl3-gpu26/26 tests.Checks on this head
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--prnumber not yet known).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 zigandgit diff --check: clean.zig build test test-golden(thefull host suite) was not run, only the steps above.
Checklist