Skip to content

DeepSeek-V4.1 on Metal: FP8 prompt projections 14-19% faster, bit-equal - #3322

Merged
ashhart merged 4 commits into
mainfrom
lead/dsv41-fp8-mma-tiles
Nov 15, 2026
Merged

ashhart merged 4 commits into
mainfrom
lead/dsv41-fp8-mma-tiles

Conversation

@ashhart

@ashhart ashhart commented Nov 14, 2026

Copy link
Copy Markdown
Owner

DeepSeek-V4.1 on Metal: the dense FP8 prompt projections (tf_dsv41_fp8_mma: wq_a, wq_b, wkv, wo_a, wo_b, the shared expert, the indexer's wq_b, Engram's wkv) take 14 to 19% less time, with every output bit unchanged.

Each output still sums four 8-wide simdgroup MMA steps from zero per 32-wide K group, then joins the roots in the same zero-padded pair tree. So prompt rows stay bit-equal to main and to the decode row kernels. What changed:

  • The tree's levels 0-2 stay in registers. Main's tree stack was dynamically indexed, so it lived in spill memory (304 B a thread). The low levels now sit in their own registers and only higher levels use the indexed array. In one struct with that array, everything spilled again.
  • Two K groups are staged per barrier, the two pairs of read registers swapping instead of copying.
  • The zero groups that pad K to a power of two are skipped. Their +0 adds are replayed in closed form where the full tree has them, so -0 and every other bit come out the same.
  • Weights decode two codes at a time (half2, as fp8_rows does). NaN codes and scales whose 256 multiple overflows take the old per-code path.
  • Still 128 threads and 32 rows a threadgroup. Threadgroup memory 9,216 -> 18,432 B (records file updated).

Release layer 2 on an M5 Ultra, ms a stage (main is the mean of two runs bracketing the branch):

stage 2048 rows main 2048 new 4096 rows main 4096 new
kv 5.05 4.91 9.94 9.71
q 10.09 8.45 19.98 16.80
ix_proj 1.31 1.13 2.52 2.14
out 14.96 12.85 29.59 25.56
shared 7.51 6.05 15.00 12.30
layer 96.2 90.65 185.3 174.6

The kernel alone at 4096 rows: wq_b 16.34 -> 13.64 ms, wo_b 15.81 -> 13.57, w1 4.78 -> 3.84, wkv 1.13 -> 0.89. kv barely moves because most of it is the compressor's g.mv.

Exactness:

  • Stage hashes: 28 of 32 equal main's at 2048 and 4096 rows. The other four are xe_up/xe_down, which also differ between main's own two runs (the gathered expert tiles' scratch). The experts, shared and post hashes after them match.
  • A microbench is byte-equal to main's kernel on all 8 release shapes. It is also byte-equal on K from 32 to 8160 at 1, 7, 33 and 100 rows, on f32 and bf16 outputs, on every e4m3 code (NaNs included), on scale codes 0-3 and 246-255, and on X holding ±0, inf and NaN.
  • Kernel contract (M5 Max): 3 kernels: 3 pass gates 1-4, 0 fail; 4 controls: 4 red as planted, 0 missed; 0 limit problems. Chunk invariance has 0 differences, and every real shape is bit-equal to fp8_rows.
  • zig build test-dsv41-metal passes (1,135 checks, prompt vs decode rows included). Lean: 0 new.

Tried and slower on the M5 Ultra:

  • 16x16 simdgroup tiles: 10-15% slower.
  • 32x8 tiles: about 4x slower.
  • 16 rows a simdgroup: the tree spilled 592 B.
  • A fully static 9-level tree: also spilled.
  • bf16/half MMA operands: no faster than fp32.
  • 8 simdgroups x 64 rows: within 1%.

This exact fp32 path peaks at 32.7 TFLOPS on that GPU (measured), and the kernel now runs at about 25.

Found along the way: fp8_rows' fast decode path takes scale code 247, where scale x 256 overflows fp32, so a decode row would disagree with a prompt row at that one scale (2^120). Release checkpoints never use it (their largest scale byte is 127). This kernel sends codes above 246 down the per-code path so it stays equal to main; the decode kernel is untouched.

  • Synthena (Ash's Agent)

…tree's low levels in registers and skips the zero padding groups

Every output keeps its four MMA steps from zero a group and the same zero-padded pair tree, so prompt rows stay bit-equal to main and to the decode rows. Release layer 2 stage hashes match main at 2048 and 4096 rows.
@ashhart
ashhart force-pushed the lead/dsv41-fp8-mma-tiles branch from 89b0b87 to 4663697 Compare November 15, 2026 12:43
@ashhart
ashhart merged commit 964174e into main Nov 15, 2026
2 of 10 checks passed
ashhart added a commit that referenced this pull request Nov 15, 2026
@ashhart
ashhart deleted the lead/dsv41-fp8-mma-tiles branch November 15, 2026 12:43
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.

1 participant