Skip to content

dsv41 metal: simdgroup MMA prompt matvec (mv_mma) - #3668

Merged
ashhart merged 2 commits into
ashhart:mainfrom
gilby:dsv41-metal-opt/mv-mma
Nov 19, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
gilby:dsv41-metal-opt/mv-mma

Conversation

@gilby

@gilby gilby commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

What this changes

Replaces scalar/thread-per-row bf16 prompt matrix-vector dispatches (glue.mv in prompt_layer.zig for compressor, indexer key/weight, and MoE gate projections when rows >= 16 and k >= 32) with a 16 KiB threadgroup-tiled simdgroup_matrix MMA kernel (mv_mma.metal), keeping exact float32 reduction in K order bit-equal to glue.mv.

  • Issue: Refs DeepSeek-V4.1 on Metal: decode and prompt speed on M3 Ultra and M5 Ultra, token-exact (gate and order for the drafts) #2984 (prompt throughput optimization on deepseek_v41 Metal).
  • Stacked on: main (077998f7d9b14adebec3a2acc2caa47ff7ae6ec7; clean git merge-tree against current origin/main 699094f70570d547532f9d633502286202e7424a, 1 commit 5c5cbeabbdc94ce361b087a47149590ed0010840).
  • atc overlap --json: Run at 2026-11-17T08:07:39Z against origin/main (699094f705). Default scan hit timeLimit / pullRequestsLimit with 0 local conflicts reported; targeted --pulls 1721,1722,2950,2951,2952,2954,2973 returned complete: true and showed pre-existing open-PR file overlaps on unmerged stack PRs (1721, 1722, 2950, 2951, 2952, 2954, 2973), and cleanly merges against current origin/main (699094f705) and PR 2 / PR 5.

Checks on this head

  • Head commit: 5c5cbeabbdc94ce361b087a47149590ed0010840
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 221 and 221, 0 new (python3 tools/pr_check.py --base origin/main: 0 error(s), 1 warning(s), max touched source file 345 lines). zig fmt --check build.zig zig and git diff --check origin/main..HEAD pass.
  • Tests run, with the command and pass, fail and skip counts:
    • Apple M5 Max: zig build test test-golden --summary all -> 874/874 steps succeeded; 426/441 tests passed (15 skipped); zig build test-dsv41-metal-all --summary all -> 9/9 steps succeeded; 47/47 tests passed (32 + 15); zig build dev-tools -> exited 0.
    • Apple M5 Ultra: zig build test-dsv41-metal-all --summary all -> 9/9 steps succeeded; 47/47 tests passed (32 + 15); zig build test-environment-host --summary all -> 3/3 steps succeeded.
    • Apple M3 Ultra: zig build test-dsv41-metal-all and zig build test-environment-host -> exited 0.
  • CI: Not run yet (opening PR).
  • Not run, and why: Full unfiltered tf-kernel-contract stops on UnrecordedRotaryCompiler for rotary probes on macOS 27.2.1 (Xcode 27.2), so contract checks ran with --only dsv41; M1–M4, CUDA, HIP, Windows, and mobile targets were not run (no hardware in this environment).

Receipt

  • Main against branch: Full two-chip A-B-B-A served receipts and 1-layer prompt stage timings are in the public Gist (base 077998f7d9 vs 5c5cbeabb). Decode (20/20 cells): SLOWER: 0/20; MISMATCHES: 0 on both chips. Cold prefill TTFT (bench_cache --require-cold, 1 repeat):
Prompt Chip Main mean (s) Branch mean (s) Speedup token_sha256
2048 M3 Ultra 6.0828 6.1651 0.9867x EQUAL (330cd07dd3cf)
8192 M3 Ultra 13.1531 12.9801 1.0133x EQUAL (b2da1b2b327d)
32768 M3 Ultra 40.5642 40.4747 1.0022x EQUAL (0e3f2ebade35)
2048 M5 Ultra 4.2110 4.0841 1.0311x EQUAL (330cd07dd3cf)
8192 M5 Ultra 8.6837 7.6902 1.1292x EQUAL (b2da1b2b327d)
32768 M5 Ultra 24.7073 25.1826 0.9811x EQUAL (0e3f2ebade35)
8192 (PROMPT_UNITS=0) M5 Ultra 8.6090 8.5722 1.0043x EQUAL (b2da1b2b327d)
32768 (PROMPT_UNITS=0) M5 Ultra 27.2024 25.8778 1.0512x EQUAL (0e3f2ebade35)

1-layer test-dsv41-metal-prompt-time (4096 rows, layer 2, source, pos 0 / 122880): M5 Ultra total 143.46 -> 138.73 ms (pos 0) and 230.32 -> 225.57 ms (pos 122880, matching core/out/post hashes); M3 Ultra 247.84 -> 242.58 ms (pos 0) and 350.50 -> 345.28 ms (pos 122880).

  • Boxes it reaches: deepseek_v41 Metal prompt path (compress, indexer key/weight, moe router gate) when rows >= 16 and k >= 32.
  • New switch? Default, accepted values, profile row: n/a — no environment variable or flag added.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: base and head SHAs recorded per arm; deepseek-ai/DeepSeek-V4.1-Flash-1017 Hugging Face revision 1bae0e1088993b9b82de08f59de5be5a34e42170 (config.json sha256 8be45ce0476004a3f529fd896115a4a2e800a129ad2d3ec05b16050f52e21879); Zig 0.17.0; Apple M3 Ultra, 512 GB (macOS 26.6.2, Xcode 27.2 / 27B52, Metal 32023.922); Apple M5 Ultra, 512 GB (macOS 27.2.1, Xcode 27.2 / 27B52, Metal 32023.922); Apple M5 Max, 48 GB (macOS 27.0.1, Xcode 27.1 / 27A9275, Metal 32023.921); drafter: checkpoint built-in DSpark + copies (--context 1048576 --parallel 4 --prompt-cache-gib 0 --vision).
  • Paths and platforms the change reaches, which I ran and which I could not: Metal prompt path across M3 Ultra, M5 Ultra, and M5 Max (ran); Metal M1/M2/M4 and non-Metal backends (not run).
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: 0 failed, 0 --alone unequal, 0 --serial unequal across all N=1,2,3,4 (T=0.0,1.0, chat/code) on both M3 Ultra and M5 Ultra; token_sha256 identical across every decode and cold prefill cell (330cd07dd3cf, b2da1b2b327d, 0e3f2ebade35).
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): Posted above and in Gist.
  • Decode speed before and after, from tools/bench_openai.py: M3 Ultra (T=0.0 54.5 -> 54.55 fib, 43.8 -> 43.8 chat; T=1.0 50.5 -> 50.6 fib, 43.9 -> 44.0 chat); M5 Ultra (T=0.0 71.2 -> 71.0 fib, 56.7 -> 56.65 chat; T=1.0 66.85 -> 66.75 fib, 57.75 -> 57.7 chat). All 20/20 decode cells within noise (0.997–1.002x) on both chips.
  • Prompt speed before and after: the cold first request's ttft_s from tools/bench_cache.py --require-cold on a long prompt, or "n/a" with the reason when the change cannot reach prompt processing: See table above (e.g., M5 Ultra 8k 8.6837s -> 7.6902s, 1.1292x, and 32k with TF_DSV41_PROMPT_UNITS=0 27.2024s -> 25.8778s, 1.0512x; M3 Ultra 8k 13.1531s -> 12.9801s, 1.0133x).
  • Licence of any new weights or drafter, or "n/a": n/a — uses existing release weights and built-in drafter.
Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract, and the kernel each one replaces: dsv41 mv mma tf_dsv41_mv_mma_f32 and dsv41 mv mma tf_dsv41_mv_mma_bf16 (128 threads, 16384 B threadgroup memory), replacing glue.mv in prompt_layer.zig (compress, indexer, moe gate) for rows >= 16, k >= 32.
  • A planted control for each new kernel, and each one fails as planted: All 4 controls fail red as planted on M5 Max, M5 Ultra, and M3 Ultra:
    • CONTROL dsv41 mv mma control: a partial last K step dropped: red at shapes, as planted
    • CONTROL dsv41 mv mma control: non-finite inputs flushed: red at nonfinite, as planted
    • CONTROL dsv41 mv mma control: sums rounded to fp16: red at sums, as planted
    • CONTROL dsv41 mv mma control: writes past its output: red at repeat, as planted
  • Each backend's SUMMARY line and exit status:
    • Apple M5 Max (--only dsv41): SUMMARY 94 kernels: 94 pass gates 1-4, 0 fail, 0 known findings; 199 controls: 199 red as planted, 0 missed; 0 limit problems; gates 123101 ms, limits 285 ms (exit 0)
    • Apple M5 Ultra (--only dsv41): SUMMARY 94 kernels: 94 pass gates 1-4, 0 fail, 0 known findings; 199 controls: 199 red as planted, 0 missed; 0 limit problems; gates 119073 ms, limits 24 ms (exit 0; with #3522 tensorOnly check: 119142 ms, limits 236 ms, 0 limit problems, exit 0)
    • Apple M3 Ultra (--only dsv41): SUMMARY 91 kernels: 91 pass gates 1-4, 0 fail, 0 known findings; 190 controls: 190 red as planted, 0 missed; 4 limit problems; gates 120431 ms, limits 327 ms (4 pre-existing dsv41 xmma limit failures inherited from 077998f7d9; with #3522 tensorOnly fix applied locally: SUMMARY 91 kernels: 91 pass gates 1-4, 0 fail, 0 known findings; 190 controls: 190 red as planted, 0 missed; 0 limit problems; gates 120659 ms, limits 261 ms, exit 0)
  • Chips I could not run: Apple M1, M2, M4 (Metal); CUDA and HIP GPUs.

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.

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.

  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.

  • No precision traded for speed. If the bits change, the description says which sums change and why.

  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled

@ashhart

ashhart commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. The change does what the description says: the prompt layer's compressor, indexer key and weight, and MoE gate matvecs move to the new MMA kernel at 16 or more rows and K of 32 or more, and the contract compares it byte for byte with tf_dsv41_mv across odd and K-boundary shapes. The token hashes you posted are equal in every cell, so we treat it as a speed change with the same tokens. Two things before it can land:

  1. It no longer applies to current main. zig/tests/contract/metal_dsv41_main.zig conflicts in all six hunks: main has added the fp8_tile and vision suites at the same import, field, create, return, destroy and gates lines. Please rebase and rerun test-dsv41-metal-all and the contract with --only dsv41 on the rebased head.
  2. Two cold-prefill cells are slower and each was run once: M3 Ultra at 2048 (6.0828 s against 6.1651 s, 0.987x) and M5 Ultra at 32768 with default settings (24.7073 s against 25.1826 s, 0.981x). The checklist says prompt processing is no slower than main. Please rerun those two cells, main against the rebased branch on the same machine, with tools/bench_cache.py --require-cold --repeats 3 or more, and post the means and the token hashes. If they are still slower, please say why, or raise the 16-row threshold in Px.mv (prompt_layer.zig:68 on your head) to where it stops losing.
  • Synthena

@ashhart
ashhart force-pushed the dsv41-metal-opt/mv-mma branch from 5c5cbea to f4cb32e Compare November 19, 2026 00:50
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. We rebased this onto current main: zig/tests/contract/metal_dsv41_main.zig now carries main's fp8_tile and vision suites with mv_mma beside them in the import, field, create, return, destroy and gates lines. Your commit, authorship and message are unchanged, and we added no code. We were unable to run the Metal steps here, so test-dsv41-metal-all and the contract with --only dsv41 still need a run on the rebased head.

The second point still stands: please rerun the M3 Ultra 2048 and M5 Ultra 32768 (default settings) cold-prefill cells, main against the rebased branch on the same machine, with tools/bench_cache.py --require-cold --repeats 3 or more, and post the means and token hashes. If either is still slower, please say why, or raise the 16-row threshold in Px.mv (now prompt_layer.zig:68) to where it stops losing.

  • Synthena

@ashhart
ashhart force-pushed the dsv41-metal-opt/mv-mma branch from f4cb32e to 13e8864 Compare November 19, 2026 23:39
@ashhart
ashhart merged commit 0fa761a into ashhart:main Nov 19, 2026
2 of 11 checks passed
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. 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.

ashhart added a commit to bikepresentball/TensorFold that referenced this pull request Nov 19, 2026
ashhart added a commit that referenced this pull request Nov 19, 2026
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