Skip to content

Metal EXL3 projection: an exact one-row decode kernel for the DeepSeek-V4.1 dense shapes - #5050

Merged
ashhart merged 3 commits into
ashhart:mainfrom
BlivionIaG:feat/exl3-metal-projection-v41
Nov 25, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
BlivionIaG:feat/exl3-metal-projection-v41

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: Metal's EXL3 projection ran a decode row through tf_exl3_matmul_hoisted, whose SIMDgroup owns eight rows and
16 columns, so at one row half its lanes idle and every k tile passes through threadgroup memory and two barriers.
After: one unrouted row goes through tf_exl3_matmul_row, in which each lane owns one column, decodes that column's 16
values of each k tile and adds the exact fp32 products in ascending k with the same TwoSum, so every output bit equals
tf_exl3_matmul_hoisted's. The first piece of the DeepSeek-V4.1 EXL3 Metal lane rebuilt on main's own EXL3 code;
no family serves Metal EXL3 yet.

Checks on this head

  • Head commit: b9cb695de1
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 164 and 164, 0 new
    (python3 tools/pr_check.py --base 5c71eb84cd: 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 test-metal-launch-map-host test-kernel-contract -Dcpu=apple_m1:
      56/56 steps, 324 pass, 0 fail, 0 skip.
    • M4 Pro: zig build tf-kernel-contract -Dcpu=apple_m1, then tf-kernel-contract --only exl3 (SUMMARY below) and
      tf-kernel-contract --no-gates --only exl3 --offline DIR (M1 and M2 build the kernel for 32 threads).
    • M5 Ultra: zig build test-exl3 test-exl3-gpu -Dcpu=apple_m1: 12/12 steps, 40 pass, 0 fail, 0 skip (the same float64
      line, 6.464e-5); tf-kernel-contract --only exl3: SUMMARY below.
    • Planted faults, run once on M4 Pro and reverted: the TwoSum error term dropped fails 6 of the 26 test-exl3-gpu
      tests; a wrong tile placement fails 10.
    • 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 (gate 5's offline compiles only);
    zig build test test-golden (the full host suite) was not run on this head, 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 one on M4 Pro (GPU seconds, mean of 30, 6 halves mul1), tf_exl3_matmul_hoisted against
    tf_exl3_matmul_row at K x N: 5120 x 2304 372/303 us, 2304 x 5120 235/171, 1280 x 32768 746/460,
    4096 x 1024 289/225, 8192 x 5120 801/569, 5120 x 1280 357/300, 5120 x 512 359/280; every run's output bits equal the
    baseline's. On M5 Ultra, the same: 358/293, 173/145, 222/133, 299/244, 561/454, 363/297, 385/312.
  • Boxes it reaches: Metal EXL3 projections at one unrouted row (exl3_projection.encode with count == 1); no family.
  • New switch? None.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: head
    b9cb695de1 on main 5c71eb84cd, Zig 0.17.0, Xcode's Metal toolchain; Apple M4 Pro (48 GB, macOS 27.2) and Apple
    M5 Ultra (macOS 27.0); no checkpoint
    (synthetic trellises, and the real mcg 8.00 bpw block of UnstableLlama/Qwen3.5-2B-exl3-8.00bpw in
    zig/tests/fixtures/exl3_oracle.bin).
  • Paths and platforms the change reaches, which I ran and which I could not: exl3_projection.encode at one row
    without routes or descriptors; Metal 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) the kernel's decoded W_q is bit-equal to exl3_format.unpack at every codebook and width and to
    the oracle's real block, (b) its pre-rotation sums are bit-equal to the new serial reference
    zig/src/core/exl3_projection_row_ref.zig and to tf_exl3_matmul_hoisted at wq_a, wkv, wq_b, a wo_a
    group (as a column view), wo_b, the shared w1/w3 and w2 and ix_wq_b, (c) float64 agreement, reported and not a
    gate: on the real block, max |gpu - f64| 6.5e-5 at max |y| 0.48 (the format's fp16 rotated input).
  • 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; 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, the change takes one row only and 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/p1.md lists the main files and lines consulted, the
arithmetic (window ends, the inverse placement table, the codebooks' single fp16 rounding, the k order and TwoSum
rounding points), 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 4), against 1 to 2 (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_row (mcg 2 and 8 bits,
    mul1 2.5 and 4 bits, 3inst 6 bits; bf16 and f32 out), replacing tf_exl3_matmul_hoisted and bit-equal to it at the
    eight DeepSeek-V4.1 dense decode shapes and the N and K edges, each row launched alone; launch record
    dsv41 exl3 row tf_exl3_matmul_row (32 threads, reserved by the source).
  • A planted control for each new kernel, and each one fails as planted: the row kernel without its TwoSum error term,
    posing as tf_exl3_matmul_hoisted: red at shapes (bytes differ at 1x32768x1280).
  • Each backend's SUMMARY line and exit status:
    • Metal, M4 Pro: SUMMARY 5 kernels: 5 pass gates 1-4, 0 fail, 0 known findings; 1 controls: 1 red as planted, 0 missed; 0 limit problems; gates 24710 ms, limits 40 ms, exit 0.
    • Metal, M5 Ultra: SUMMARY 5 kernels: 5 pass gates 1-4, 0 fail, 0 known findings; 1 controls: 1 red as planted, 0 missed; 0 limit problems; gates 11744 ms, limits 378 ms, exit 0 (the control red at the same shape and byte).
  • Chips I could not run: M1, M2 and M3 (gate 5's offline compiles for M1 and M2 pass).

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 one-row mapping and compensated sum keep the existing order, with replacement byte comparisons in place.

One setup fix remains: Suite.create does not unwind if probe initialization or either append fails, so it leaks the suite/probes and any acquired pipelines. Please protect each ownership stage and add a failure-path regression, including both appends. The changelog merge is on our side; unchanged arithmetic needs no repeat run for this cleanup.

  • 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 main and used contract.register: Suite.create now unwinds the suite, the probe and its pipelines at each stage. A new test-dsv41-exl3-contract fails every allocation of create in turn (both lists' growth included), with 9 leaks before the fix and none after. Arithmetic unchanged, so no GPU rerun beyond the M4 gates (test-exl3, test-exl3-gpu, tf-kernel-contract --only exl3, all green). Head b9cb695de1.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @BlivionIaG. The ownership fix is confirmed: Suite.create unwinds the suite, probe, and initialized pipelines at each stage, and contract.register now publishes the probe and kernel lists together. The failure-path test reaches both list-growth failures and also unwinds an already registered probe.

The one-row arithmetic is unchanged. It retains the original placement, ascending-k products, TwoSum statements, and final compensated sum. The byte comparisons against tf_exl3_matmul_hoisted and the planted TwoSum control remain in place.

Those review items are closed, and I found no remaining code change to request. The changelog attribution is a landing detail on our side. The supplied current-head Metal checks are recorded; no new device run is needed for this cleanup.

  • Synthena

BlivionIaG and others added 3 commits November 25, 2026 23:25
One unrouted row now goes through tf_exl3_matmul_row: each lane owns one
output column, decodes that column's 16 values of each k tile and adds the
exact fp32 products in ascending k with tf_exl3_matmul's TwoSum, so every
output bit equals that kernel's, without threadgroup memory or barriers.

Tests hold the kernel's decoded W_q bit-equal to exl3_format.zig at every
codebook and width and to the oracle's real mcg 8.00 bpw block, and its
sums bit-equal to a new serial reference (exl3_projection_row_ref.zig) and
to tf_exl3_matmul_hoisted at the eight DeepSeek-V4.1 dense decode shapes.
The kernel is registered with the contract as a replacement of
tf_exl3_matmul_hoisted with a planted control (the TwoSum error term
dropped), and tf-exl3-bench gains a row candidate.
…register

A failed probe init, probe-list or kernel-list growth now frees the suite and every probe it took; test-dsv41-exl3-contract fails each allocation of create in turn on this GPU.
@ashhart
ashhart force-pushed the feat/exl3-metal-projection-v41 branch from ba4453b to 1626323 Compare November 25, 2026 23:25
@ashhart
ashhart merged commit 34b6d4a into ashhart:main Nov 25, 2026
12 checks passed
@ashhart

ashhart commented Nov 25, 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