Skip to content

DeepSeek-V4.1 on Metal: decode attention bf16 widening and a deeper key prefetch, bit-equal - #3360

Closed
ashhart wants to merge 7 commits into
mainfrom
lead/dsv41-attn-dec
Closed

ashhart wants to merge 7 commits into
mainfrom
lead/dsv41-attn-dec

Conversation

@ashhart

@ashhart ashhart commented Nov 14, 2026

Copy link
Copy Markdown
Owner

Stacked on #3351 (this branch carries its commits; it lands after it).

DeepSeek-V4.1 on Metal: the decode attention kernel (tf_dsv41_attn_dec) gets #3351's bf16 widening, with the same bits:

  • tf_attn_lo/tf_attn_hi widen through the bfloat type (same values), so both kernels share one tf_attn_x16.
  • prefetch_rows goes from 2 to 64. With the widening in, loading the next step's keys a step ahead beats the plain pipeline at every row count measured (1 to 64).
  • A 72-row decode shape is added to the contract, so the plain pipeline is still exercised.

Kernel-only A/B on an M5 Ultra, µs (output bytes equal to main at every row count):

rows 1 2 3 4 8 16 32 64
main 31.0 30.8 42.1 42.6 74.9 144.7 282 538
this 30.7 30.6 33.5 35.9 60.6 125.1 245 479

Release layer 2 decode (time-dsv41-metal-real, main and this alternated twice in one run): attention 47.5 / 46.5 / 53.9 / 52.7 µs -> 45.5 / 44.8 / 44.1 / 43.7 µs at 1 / 2 / 3 / 4 rows. Output and KV-state hashes are identical to main at 1 and 4 rows. The whole layer goes 632 -> 627 µs at 1 row and 1046 -> 1034 µs at 4 rows.

Served, 16 prompts at 512 tokens, on the decode stack (#3287, #3288, #3292, #3296, gilby's #2973/#2983) plus the prefill PRs, back to back in one run: greedy 89.5 -> 90.3, sampled 72.8 -> 73.4, "draft": false 51.6 -> 51.5 tok/s. All 48 reply hashes equal the reference.

One-row attention barely moves. Removing any single part of the work at 1-2 rows (exp, the shuffle tree, most of the dot fmas, the ballot, the PV fmas) changed nothing, so load latency and the per-step arithmetic chain cost about the same.

Tried and slower or level, all keeping bits:

  • Four keys a step: +9% at 1-2 rows, 9-19% slower at 4-8.
  • Prefetching two steps ahead.
  • Scoring the next step early: 20% slower at 1 row.
  • A three-pass chunk: exact but 2x slower.

Checks on an M5 Max:

  • Kernel contract --only dsv41_attn passes gates 1-4 on 22 decode shapes, with the 5 planted controls red.

  • zig build test-dsv41-metal (1,135 checks, prompt vs decode rows 0 differ) and test-dsv41-metal-serve pass. Lean: 0 new.

  • Synthena (Ash's Agent)

…there and the rest by decode fmas

The simdgroup kernels already ran near the fp32 MMA rate. On M5 GPUs (macOS 27) a
routed expert's 32-wide group now goes to the tensor unit, fp4 weights read as
stored, when its act_quant codes bound every fp32 partial sum below 2^24 times their
lowest set bit: then any summation order gives the decode chain's bits. Other groups
(about 0.2% up, 0.3% down on layer 2) run the decode kernels' 32 fmas in k order.
The chains, scale fmas and tree are the decode kernels'; one simdgroup runs a chain
and the tree joins them in threadgroup memory. Down's input halves and flags come
from a small prep pass over act. Other GPUs keep the simdgroup kernels; the contract
gates both sets, and both match the decode kernels per row.
--
2.43.0
…generation 17

runtime_sources/dsv41.zig called the experts' source with the old three
arguments; it now lists the simdgroup source and, where the loader would take
it, the tensor-unit source, so the runtime compile gate builds experts_units.metal.
The tensor-unit kernels now run only on generation 17 (M5), the one checked; the
modes test holds the runtime list to that rule. CHANGELOG: 155 ms, #3330.
--
2.43.0
TF_CTL 6 sends every group to the tensor unit. The down gates' random data now
carries one group that decode's fmas round (2^24, 2^23, 2^16, then four terms of
half an fp32 ulp, landing on a bf16 tie) and exact sums do not: the kernels match
the oracle through the fallback, and the control goes red at the shapes gate.
--
2.43.0
…nd on an M5

units_test.zig checks xu_exact's rule against exact integer sums: a safe group's
fp32 sum is exact in six orders, and past the bound some order rounds. On an M5
it builds rows at and past the bound (tops at the largest code, lows at the lowest
bit, columns that swing and cancel), runs the production act_quant and dprep, then
the tensor unit as xu_chain does and decode's fmas as the fallback does. Every safe
sum matches the exact sum on both paths, past the bound the two paths split, and
the kernels' flags equal the rule. TF_DSV41_UNITS_BATCHES sets the batches (2).
--
2.43.0
…ough bfloat

tf_dsv41_attn_pre gives each simdgroup one row and two heads, so both heads' dots
use one load of each key. Keys come straight from device memory (no window tiles
or barriers), widen through the bfloat type, and a selected run's indices load
four at a time. Each key is one step: both dots, the pair-order butterfly, a
rescale of any head whose max rose, then each head's weight and sums, the steps
of tf_attn_update. Bits equal main's and the decode kernel's.
…tched to 64 rows

tf_attn_lo and tf_attn_hi widen through the bfloat type (the same values), so the
decode kernel's dots and sums spend fewer instructions a key; the prompt kernel's
own widening copy goes. With that, loading each step's keys a step ahead wins at
every row count measured, so decode launches take the prefetch pipeline up to 64
rows. A 72-row decode shape keeps the plain pipeline under the contract. Bits equal
main's.
@ashhart
ashhart force-pushed the lead/dsv41-attn-dec branch from c02ea70 to b573e1c Compare November 18, 2026 03:48
@ashhart

ashhart commented Nov 22, 2026

Copy link
Copy Markdown
Owner Author

Both changes here are on main now: the prompt attention change (two heads a simdgroup, bf16 widened through bfloat) landed as 8116c32 with #3351, and the decode attention change (bf16 widening, keys prefetched to 64 rows) as 596c059 with #3459. A rebase onto current main leaves no difference beyond one comment line, so I am closing this as included.

  • Synthena

@ashhart ashhart closed this Nov 22, 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.

1 participant