Skip to content

Metal EXL3 projection: exact multi-row bodies and a fused gate/up SwiGLU for the DeepSeek-V4.1 shapes - #5054

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

ashhart merged 4 commits into
ashhart:mainfrom
BlivionIaG:feat/exl3-metal-rows-v41

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: Metal's EXL3 projection ran 2 to 16 decode rows through tf_exl3_matmul_hoisted (eight rows and 16 columns a
SIMDgroup, every k tile through threadgroup memory and two barriers), and #5050's one-row kernel takes one row only.
After: 2 to 16 unrouted rows can go through tf_exl3_matmul_rows{rp}x{q}, in which q lanes share a column's decode
(16/q values each, the rest by shuffle) and each lane keeps rp rows, every row with tf_exl3_matmul_row's chain, so
row r of a launch is bit-equal to that row launched alone and to tf_exl3_matmul_hoisted. Two rows always take them;
3 to 16 take them while the hoisted kernel would launch fewer than 12 simdgroups a GPU core (Pipelines.gpu_cores, 0
by default), the measured crossover on both machines. exl3_projection.gate_up runs an expert's w1 and w3 and one
epilogue, tf_exl3_swiglu (both rotate_outs, DeepSeek-V4.1's clamped SwiGLU and its act_quant), bit-equal to both
projections then main's tf_dsv41_f_swiglu. No family serves Metal EXL3 yet.

Checks on this head

  • Head commit: 4da3a5ca74
  • Lean checker, python3 tools/zig/lean_check.py: 0 new (python3 tools/pr_check.py --base <main>: 0 errors,
    1 advisory warning, the --pr number not yet known).
  • Tests run, with the command and pass, fail and skip counts:
    • M4 Pro: zig build test-exl3 test-exl3-gpu -Dcpu=apple_m1: all steps pass (the GPU binary: 30 of 30 tests);
      zig build test-metal-launch-map-host test-kernel-contract -Dcpu=apple_m1: 46/46 steps, 257 pass, 0 fail.
    • M4 Pro: tf-kernel-contract --only exl3 (SUMMARY below) and tf-kernel-contract --no-gates --only "exl3 rows" --offline DIR (M1 and M2 build all five pipelines for 32 threads).
    • Planted faults, run once on M4 Pro and reverted: the rows body's TwoSum error term dropped fails 3 of the 30
      test-exl3-gpu tests; the up clamp dropped fails the gate/up test.
    • zig fmt --check build.zig zig and git diff --check: clean.
  • CI: not run yet (no pull request).
  • Not run, and why: the test suites and the contract on the M5 Ultra at this head (only the bench sweep below ran
    there, on the same kernel body); CUDA, HIP and XPU are untouched; M1 to M3 have no device here (offline compiles
    only); zig build test test-golden was not run, only the EXL3, launch-map and kernel-contract steps above.

Receipt

  • Main against branch: no family calls Metal EXL3, so no served path changes; the projection's own before/after is
    tf-exl3-bench --mode prefill --candidate rows (GPU seconds, mean of 30, 6 halves mul1, rotations included),
    tf_exl3_matmul_hoisted against the rows kernels at K x N and rows. M4 Pro: 5120 x 2304 2/4/8 rows 497/309,
    659/407, 944/634 us; 5120 x 1280 8 rows 894/356; 5120 x 512 8 rows 952/219; 4096 x 1024 4 rows 440/203; 2304 x 5120
    2 rows 260/254. M5 Ultra: 5120 x 2304 8/16 rows 774/224, 772/383; 2304 x 5120 8 rows 357/191; 8192 x 5120 8 rows
    1208/640; 1280 x 32768 2 rows 218/203. Where the rule keeps the hoisted kernel (e.g. 1280 x 32768 at 4+ rows on
    both, 2304 x 5120 at 4+ rows on M4 Pro) the rows kernels ran up to 20% slower. Every run's output bits equal.
  • Boxes it reaches: Metal EXL3 projections at 2 to 16 unrouted rows (exl3_projection.encode), and the new
    exl3_projection.gate_up.encode; no family.
  • New switch? None (Pipelines.gpu_cores is a field the caller sets; 0 keeps 3 to 16 rows on the hoisted kernel).
  • Environment: head 4da3a5ca74 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 (20-core GPU, macOS 27.2) and Apple M5 Ultra (64-core GPU); no checkpoint (synthetic trellises).
  • Paths and platforms the change reaches, which I ran and which I could not: exl3_projection.encode at 2 to 16 rows
    without routes or descriptors and gate_up.encode at 1 to 16 rows; Metal only. Ran M4 Pro (tests, contract, bench)
    and M5 Ultra (bench); not M1 to M3.
  • Exactness: n/a for a served path; instead (a) at 2, 3, 4, 8 and 16 rows on the eight dense decode shapes, with NaN
    and +-Inf in x, every row's sums and outputs equal that row through tf_exl3_matmul_row alone, the launch equals
    tf_exl3_matmul_hoisted, and one row equals the serial reference exl3_projection_row_ref.zig; (b) at 1 to 16
    rows, routed both ways, the gate/up y and yq equal w1 and w3 through tf_exl3_matmul_hoisted then
    tf_dsv41_f_swiglu, with both clamps reached. No float64 claim is made; everything is bit-equality.
  • Main against this branch, same machine and checkpoint: no checkpoint path reaches it yet; the bench above is the
    same-machine comparison.
  • Decode speed before and after, from tools/bench_openai.py: n/a, no family serves Metal EXL3.
  • Prompt speed before and after: n/a, 17 rows and more are unchanged.
  • Licence of any new weights or drafter, or "n/a": n/a.

How it was derived: docs/receipts/dsv4.1-exl3-rebuild/p2.md lists the main and #5050 files consulted (main's
fused.metal for the SwiGLU and act_quant expressions), the arithmetic and the row-group choice, and states that no
file of the #2373-derived stack was opened. git blame attributes every line of the new files to this commit; the
normalized-line comparison against the mlx-serve, mlx-stream and turboexl3 histories gives a longest run of 1 to 3
lines in the changed files (abstracted: 1 to 4), against 1 (abstracted 1 to 4) for main's own 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_exl3_matmul_rows{1x2,1x4,1x8,2x8}
    through five registrations (mcg 2 and 8 bits, mul1 2.5 and 4 bits, 3inst 6 bits; bf16 and f32 out), replacing
    tf_exl3_matmul_hoisted at 2 to 16 rows in one launch on the eight dense decode shapes and the edges; and
    tf_exl3_swiglu (w1 mul1 2.5 / w3 mcg 2 bits; w1 3inst 6 / w3 mul1 1.5 bits with biases) replacing w1 and w3
    through tf_exl3_matmul_hoisted then tf_dsv41_f_swiglu at 1, 2, 4 and 16 rows; launch records dsv41 exl3 rows ... (32 threads, reserved by the source).
  • A planted control for each new kernel, and each one fails as planted: the rows body without its TwoSum error term,
    red at shapes (bytes differ at 16x1280x5120 from byte 0); tf_exl3_swiglu without the up clamp, red at shapes
    (1x2304x5120, byte 4).
  • Each backend's SUMMARY line and exit status:
    • Metal, M4 Pro: SUMMARY 12 kernels: 12 pass gates 1-4, 0 fail, 0 known findings; 3 controls: 3 red as planted, 0 missed; 0 limit problems; gates 56190 ms, limits 671 ms, exit 0.
  • Chips I could not run: M1, M2 and M3 (gate 5's offline compiles for M1 and M2 pass); the contract on M5 Ultra was
    not run at this head.

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

Thanks @BlivionIaG. The row mapping keeps each row's original compensated chain, and the fused clamps and quantization expressions match main. The reported byte comparisons are recorded with their chip coverage.

One new cleanup remains: if the private rows or pairs append fails, the initialized probe is not yet owned by the suite and leaks its pipelines. Please protect that transfer and test both failure paths. The inherited one-row setup cleanup also still needs the shared fix. The changelog merge is on our side.

  • 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. The rows probes and pairs now reach the suite through contract.register, and the same test covers the rows and pairs failure paths (2 leaks before the fix, none after). Arithmetic unchanged; the M4 gates are green. Head bfd5d72ea2.

@BlivionIaG

Copy link
Copy Markdown
Contributor Author

M5 Ultra results at bfd5d72ea2 (Apple M5 Ultra, macOS 27.0, Zig 0.17.0): zig build test-exl3 9/9 steps, 16/16 tests; zig build test-exl3-gpu 6/6 steps, 30/30 tests; tf-kernel-contract --only exl3: SUMMARY 12 kernels: 12 pass gates 1-4, 0 fail, 0 known findings; 3 controls: 3 red as planted, 0 missed; 0 limit problems, exit 0. The counts match the M4 Pro.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @BlivionIaG. The rows and pairs now transfer ownership through contract.register, the inherited one-row suite has the same cleanup, and the failure-path tests cover both list growths. The row chains, gate/up arithmetic, byte comparisons, and planted arithmetic controls remain intact.

One new validation gap remains in zig/src/core/exl3_gate_up.zig:41-50. The checks reject y overlapping gate scratch and yq overlapping up scratch, but not y overlapping up scratch or yq overlapping gate scratch. The fused epilogue reads both sum buffers while writing both outputs, so either allowed overlap can corrupt sums another threadgroup has not read and break equality with the separate-projection baseline.

Please add the two opposite-scratch ex.disjoint checks and failure tests expecting error.AliasedExl3View for both cases. No device run is needed to establish this validation gap.

  • Synthena

@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart. You were right: the cross matrixArgs calls rechecked y against the gate scratch and yq against the up scratch, so the opposite overlaps were never caught. encode now also checks ex.disjoint(y, ybytes, su, half) and ex.disjoint(yq, ybytes, sg, half). A new test in exl3_rows_test.zig expects error.AliasedExl3View for y on the up scratch and for yq on the gate scratch, then runs one valid encode. Without the checks, and with either one removed, it fails ("expected error.AliasedExl3View, found void"); with both it passes (test-exl3-gpu 31/31 on an M4 Pro). Merged your rebased #5050 (ba4453b2fd). Head 4da3a5ca74. Arithmetic unchanged: tf-kernel-contract --only exl3 12 pass, 3 controls red as planted.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you for adding both opposite-output/scratch checks and the valid-encode control.

The source now rejects y on the up scratch and yq on the gate scratch before encoding GPU work. The new test covers both failures and a successful call. The exact contract registrations and allocation-failure tests are retained, and the shader arithmetic is unchanged.

Your supplied current-head device run records 31/31 GPU tests and 12 passing EXL3 kernels with 3 planted controls red. That closes the requested fix. It is ready to land once we resolve the listed current-main conflicts, which we will do; no additional device run is a prerequisite. Integration remains on our side, with no rebase request.

  • Synthena

BlivionIaG and others added 4 commits November 26, 2026 04:27
… per row

Lanes share a column's decode and each row keeps tf_exl3_matmul_row's TwoSum chain, so row r of a launch equals that row alone and tf_exl3_matmul_hoisted. tf_exl3_swiglu fuses both rotate_outs with tf_dsv41_f_swiglu's clamped SwiGLU and act_quant.
…egister

A failed init, plant or list growth frees the probe once and unwinds the suite; test-dsv41-exl3-contract fails each allocation of create in turn, rows and pairs included.
The fused epilogue reads both matrices' sums while it writes y and yq, so each output must also be disjoint from the other matrix's scratch; a test expects error.AliasedExl3View for both overlaps.
@ashhart
ashhart force-pushed the feat/exl3-metal-rows-v41 branch from 99c4f0c to 28d0098 Compare November 26, 2026 04:27
@ashhart
ashhart merged commit 0b9458b 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