Skip to content

dsv41 metal: faster long-prompt indexer passes - #3669

Merged
ashhart merged 2 commits into
ashhart:mainfrom
gilby:dsv41-metal-opt/ix-long
Nov 20, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
gilby:dsv41-metal-opt/ix-long

Conversation

@gilby

@gilby gilby commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Speeds up long-context indexer passes in deepseek_v41 Metal prefill by specializing candidate-masked vs unmasked tf_dsv41_ix_scores execution (MASKED=false keeps the straight-line unpredicated tf_ix_tile<IXKT> loop with zero mask overhead; MASKED=true uses simd_any per block over IXKT to skip dead candidate blocks or dispatch a single straight-line tf_ix_tile<1u> block when only one block is live), aggregating topk512.metal pass-0 bin-3 (-inf) histogram atomics per simdgroup, and fixing r1masked source mask setup in prompt_time.zig (prompt.zig keeps ix_rows = 64 so scratch reserves are unchanged vs main).

Checks on this head

  • Head commit: afc377f83ae0e521c88f9e70e1d7d3d510821787
  • Lean checker, python3 tools/zig/lean_check.py, on the base (813ff4ccb) and on this head (afc377f83): 191 and 191, 0 new (python3 tools/pr_check.py --base origin/main --pr 3669: 0 error(s), 1 warning(s), max touched source file 342 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 Ultra (afc377f83):
      • zig build dsv41-metal-serve tf-kernel-contract test test-golden --summary all -> 912/912 steps succeeded; 477/492 tests passed (15 skipped) (322/335 + 55/55 equal to server goldens, 0 unexpected differences)
      • zig build test-dsv41-metal-all --summary all -> 9/9 steps succeeded; 48/48 tests passed (33 + 15)
      • ./zig-out/bin/tf-kernel-contract --only dsv41 -> exit 0 (107 kernels pass gates 1-4, 205 controls red as planted, 0 limit problems)
    • Apple M3 Ultra (afc377f83):
      • zig build tf-kernel-contract -Doptimize=ReleaseFast -> 43/43 steps succeeded
      • zig build test-dsv41-metal-all --summary all -> 9/9 steps succeeded; 47/48 tests passed (1 skipped) (32 pass, 1 skip + 15 pass)
      • ./zig-out/bin/tf-kernel-contract --only dsv41 -> exit 1 (104/104 pass gates 1-4, 196/196 controls red; 4 pre-existing dsv41 xmma units LibraryLoad checks tf_dsv41_xu_{quant,up,dprep,down})
  • CI: All 9 required checks green on afc377f83 (No GPU/zig, No GPU/zig-b, No GPU/zig-windows, No GPU/zig-macos-dev-tools, No GPU/pytest, No GPU/benchmark-clients Ubuntu & Windows, No GPU/release-paths, Zig format/fmt; host-nightly-{linux,macos} skipped as scheduled).
  • Not run, and why: Full unfiltered tf-kernel-contract stops on UnrecordedRotaryCompiler on macOS 27.2.1, so contract checks ran with --only dsv41; M1–M4, CUDA, HIP, Windows, and mobile targets were not run.

Receipt

  • Main (813ff4ccb) against branch (afc377f83): Cooled M5 Ultra 4-pass A-B-B-A served receipts (2K–131K cold prefill + decode) and 1-layer stage timings are in the public Gist. All cached_tokens == 0, token_sha256 identical across every arm.
  • Cold prefill TTFT on Apple M5 Ultra (afc377f83 vs 813ff4ccb, bench_cache.py --require-cold, 4-pass A-B-B-A; 5 total requests/arm at 2048, 6 at 8192/32768, 2 cold bench_cold_single.py requests/arm at 131072):
Prompt main mean (s) Branch mean (s) Speedup token_sha256 prefix
2048 2.5307 2.5332 0.9990x EQUAL (330cd07dd3cf)
8192 7.1923 7.0459 1.0208x EQUAL (b2da1b2b327d)
32768 23.9373 23.8813 1.0023x EQUAL (0e3f2ebade35)
131072 95.4618 95.9260 0.9952x EQUAL (1e2921aaf094)
  • Prior Apple M3 Ultra served baseline (measured on earlier single-commit head 49e71c32f vs 077998f7d9; decode 0/20 slower, 0 mismatches): 2048 6.0828 -> 5.9359s (1.0248x), 8192 13.1531 -> 13.0820s (1.0054x), 32768 40.5642 -> 40.5371s (1.0007x), 131072 160.0994 -> 159.7526s (1.0022x), identical token_sha256 prefixes above. (afc377f83 re-verified test-dsv41-metal-all + --only dsv41 on M3 Ultra.)
  • Isolated 1-layer test-dsv41-metal-prompt-time on M5 Ultra (4096 rows, ixrows=64, reps=5):
    • layer 2, source (unmasked, mask_stride == 0): pos 28672 ix_score 20.98 -> 20.92 ms (total 164.93 -> 165.45 ms); pos 122880 ix_score 78.16 -> 78.06 ms, ix_select 10.77 -> 10.80 ms (total 230.32 -> 230.86 ms), bit-identical stage hashes (87d886cc2239db1b, 6df19c701050059b).
    • layer 2, r1masked (candidate-masked, branch initializes .r1 candidate mask per prompt_time.zig fix): pos 28672 ix_score 39.46 -> 39.77 ms, ix_select 7.40 -> 5.51 ms (total 178.67 -> 178.62 ms); pos 122880 ix_score 154.40 -> 104.87 ms, ix_select 29.33 -> 23.33 ms, total 315.91 -> 261.83 ms (12966 -> 15644 rows/s, 1.207x).
  • Boxes it reaches: deepseek_v41 Metal prompt indexer (index.metal, topk512.metal) and prompt_time.zig.
  • New switch? Default, accepted values, profile row: n/a — no environment variable added.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: base 813ff4ccb and head afc377f83; deepseek-ai/DeepSeek-V4.1-Flash-1017 Hugging Face revision 1bae0e1088993b9b82de08f59de5be5a34e42170; Zig 0.17.0; Apple Metal 32023.922; macOS 27.2.1 on Apple M5 Ultra 512 GB and macOS 26.6.2 on Apple M3 Ultra 512 GB; served flags --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 on M5 Ultra and M3 Ultra (afc377f83), M5 Max on earlier head; M1/M2/M4 and non-Metal backends not run.
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: token_sha256 identical across all M5 Ultra afc377f83 cold-prefill (2K–131K) and decode (T=0, 1.0, 4 cells) A-B-B-A arms (4 unique decode SHA sets matching across A1/B1/B2/A2); test-dsv41-metal-serve confirms draft:false == drafted on both M5 Ultra and M3 Ultra.
  • 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: M5 Ultra (afc377f83 vs 813ff4ccb, tokens=256, reps=3): fibonacci-raw T=0 71.74 -> 71.69 tok/s (0.9994x), T=1.0 67.62 -> 67.49 tok/s (0.9981x); gpu-chat-no-think T=0 56.80 -> 56.71 tok/s (0.9985x), T=1.0 56.77 -> 56.80 tok/s (1.0005x).
  • 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: At 32768: 23.9373s -> 23.8813s (1.0023x); first-only 24.3873s -> 23.6796s (1.0299x). At 131072 (2 cold requests/arm across A-B-B-A): 95.4618s -> 95.9260s (0.9952x, B1 min 93.5985s vs A1 95.1861s).
  • 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: Modifies existing registered kernels dsv41 ix (index.metal) and dsv41 topk512 tf_topk512 (topk512.metal) in place.
  • A planted control for each new kernel, and each one fails as planted: All dsv41 ix controls and dsv41 topk512 controls pass red as planted on M5 Ultra and M3 Ultra.
  • Each backend's SUMMARY line and exit status:
    • Apple M5 Ultra (--only dsv41): SUMMARY 107 kernels: 107 pass gates 1-4, 0 fail, 0 known findings; 205 controls: 205 red as planted, 0 missed; 0 limit problems; gates 129203 ms, limits 42 ms (exit 0)
    • Apple M3 Ultra (--only dsv41): SUMMARY 104 kernels: 104 pass gates 1-4, 0 fail, 0 known findings; 196 controls: 196 red as planted, 0 missed; 4 limit problems; gates 131162 ms, limits 320 ms (exit 1; 4 pre-existing dsv41 xmma units LibraryLoad checks tf_dsv41_xu_{quant,up,dprep,down})
  • Chips I could not run: Apple M1, M2, M4 (Metal); CUDA, HIP, and XPU 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. It applies cleanly to current main. The three parts read as exact: a key block skipped by the simd_any check in index.metal has no live key in any of the simdgroup's rows, so its scores are written as -inf as before; the topk512.metal change only batches the pass-0 counts of bin 3 (the -inf keys) per simdgroup; and ix_rows only changes how many rows are scored at once. Your token hashes are equal through 131072 tokens. Before it can land:

  1. Two M5 Ultra cold-prefill cells are slower: 8192 (8.6837 s against 8.9074 s, 0.975x) and 32768 (24.7073 s against 25.4393 s, 0.971x), while the checklist says prompt processing is no slower than main. Please rerun them, main against this branch on the same machine, with tools/bench_cache.py --require-cold --repeats 3 or more, and post the means and token hashes. If they stay slower, please say which of the three parts costs time there. A part that does not pay for itself can come out.
  2. ix_rows going from 64 to 128 (prompt.zig:38) doubles the index score and block score scratch. Please post how many more bytes that reserves at --context 1048576, and whether the startup reserve still fits on the smallest Mac this family supports.
  3. Small: topk512.metal also rewords the TF_CTL comment on line 6 and changes three loop literals from 0 to 0u, which the description does not mention. Please drop them, or say why they are needed. In index.metal, if (kf_live[0] || kf_live[1]) assumes IXKT is 2. Please write it as a loop over IXKT, or add a static check on IXKT.
  • Synthena

@gilby
gilby force-pushed the dsv41-metal-opt/ix-long branch from 49e71c3 to afc377f Compare November 19, 2026 12:56
@gilby

gilby commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Addressed all three points from your review and re-verified on M5 Ultra and M3 Ultra (receipts and scrubbed logs in the updated Gist, head afc377f83 vs main 813ff4ccb):

1. M5 Ultra cold prefill rerun (--require-cold), ablation, and index.metal specialization

  • 3-way ablation isolating the regression: Re-running M5 cold prefill (tools/bench_cache.py --require-cold, 5 requests/arm at 8192/32768, 1 at 131072) with ix_rows reverted from 128 back to 64 (v_ix64: index.metal + topk512.metal at ix_rows=64, vs single-part v_only_idx, v_only_topk, and bracketing main-1/main-2) eliminated the ix_rows=128 slowdown at 32768 (24.1603s main -> 23.2006s v_ix64), so prompt.zig (ix_rows) was kept unchanged at 64.
  • 1-layer test-dsv41-metal-prompt-time diagnosis & fix: Even with ix_rows=64, if (kf_live[kf]) inside the 32-head MMA loop in index.metal added +2.16 ms (78.20 -> 80.36 ms at pos 122880) on unmasked class=source layers (mask_stride == 0), where 8192 and half of 32768 layers run. Splitting tf_dsv41_ix_scores into MASKED=false / MASKED=true with straight-line tf_ix_tile<KT> (tf_ix_tile<IXKT> on full/unmasked tiles and tf_ix_tile<1u> on half-live candidate-masked tiles) restored unmasked class=source (pos 122880 ix_score 78.16 -> 78.06 ms, bit-identical hashes 6df19c701050059b) and improved candidate-masked class=r1masked (pos 122880 ix_score 154.40 -> 104.87 ms, ix_select 29.33 -> 23.33 ms, total 315.91 -> 261.83 ms, 1.207x).
  • Final cooled M5 Ultra 4-pass A-B-B-A (afc377f83 vs 813ff4ccb, cached_tokens == 0, decode 0.9981x–1.0005x):
Prompt main mean (s) Branch mean (s) Speedup token_sha256 (all arms equal)
2048 2.5307 2.5332 0.9990x 330cd07dd3cf9c94be53d7f60da903a20c09fa92ef93c07770cfbca645f62b69
8192 7.1923 7.0459 1.0208x b2da1b2b327db99418d423e402ede4d80b44a7735bd6348c07f1ab237a6a64ce
32768 23.9373 23.8813 1.0023x 0e3f2ebade352a4c79bcc121ddbb4f400d7e936262b65eb01e35bbdf16cc5836
131072 95.4618 95.9260 0.9952x 1e2921aaf09463439be75b450cf3146ebd46b1d2321b318e9e608d5bbcd7bfb2

2. Extra scratch reserve and minimum supported Mac RAM tier

  • Doubling ix_rows from 64 -> 128 reserved +301,989,888 B (+288 MiB) at --context 1048576 (target: 15.7 GiB -> 16.0 GiB of buffers; 6.8 GiB at --context 262144 / +72 MiB).
  • The smallest Mac RAM tier this ~285 GB checkpoint (284.2 GiB kept wired between requests, inside 70% of 512 GiB of RAM) fits on is 512 GB, where both 15.7 GiB and 16.0 GiB fit.
  • After reverting prompt.zig ix_rows back to 64, extra scratch is +0 B (target: 15.7 GiB, matching main).

3. topk512.metal and index.metal cleanups

  • Reverted the TF_CTL line-6 comment rewording and the three 0 -> 0u loop literal changes in topk512.metal.
  • Generalized the CM candidate block check in index.metal as an UNR for (uint kf = 0; kf < IXKT; ++kf) loop (and for (uint b = b0; b <= b1; ++b)) instead of hardcoding kf_live[0] || kf_live[1] for IXKT == 2.
  • Verification on afc377f83: M5 Ultra --only dsv41 (107/107 pass, 205/205 controls red, 0 limit problems) & test-dsv41-metal-all (48/48), M3 Ultra test-dsv41-metal-all (47/48, 1 skip) & --only dsv41 (104/104 pass, 196/196 controls red, only the 4 pre-existing dsv41 xmma units LibraryLoad checks), plus tools/pr_check.py --pr 3669 (0 errors). All 9 required GitHub Actions checks are green on afc377f83.

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. All three items are closed: the ablation isolated the regression to the 128-row pass and reverting it removed both the slowdown and the extra scratch, the masked specialization turned the 8,192 cell into a gain with every hash equal, and the small cleanups are in. The contract runs on both Macs and the green checks on the head close it.

Nothing is left from our side.

  • Synthena

@ashhart
ashhart force-pushed the dsv41-metal-opt/ix-long branch from afc377f to f4a3b05 Compare November 20, 2026 09:11
@ashhart
ashhart merged commit 3426614 into ashhart:main Nov 20, 2026
2 of 11 checks passed
@ashhart

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