Skip to content

DeepSeek-V4.1 on Metal: prompt experts on M5 tensor units, bit-equal (exact groups only) - #3330

Merged
ashhart merged 5 commits into
mainfrom
lead/dsv41-xmma-tiles
Nov 15, 2026
Merged

ashhart merged 5 commits into
mainfrom
lead/dsv41-xmma-tiles

Conversation

@ashhart

@ashhart ashhart commented Nov 14, 2026

Copy link
Copy Markdown
Owner

DeepSeek-V4.1 on Metal: on M5 GPUs (macOS 27), the prompt's grouped routed experts (gate/up and down) run on the tensor units. Every output stays bit-equal to the decode expert kernels, as it is on main.

Why it is exact. The decode kernels sum a 32-wide K group with 32 fmas in k order. A tensor unit sums in its own order, so in general its bits differ: on data built to round, 48 of 2M sums did. But some groups have every partial sum an exact fp32 number, and there any order gives the same bits. That holds here because:

  • act_quant codes are E4M3 values, which are integers times 2^-9, and fp4 weights doubled are integers of at most 12.
  • So each product is an integer multiple of half the block's lowest set bit low.
  • If 12 * sum|v| < 2^24 * low, every partial sum of the group is an integer below 2^24 in units of low/2, so it is exact in fp32.

act_quant (and a small xu_dprep pass for down's inputs) flags each (row, group) block safe or not. Safe groups run on the tensor unit (Metal 4 matmul2d), with the fp4 weights read straight from the checkpoint layout. Unsafe groups run the decode kernels' 32 fmas in k order: 0.19% of up blocks and 0.27% of down blocks on release layer 2. The chains, scale fmas and group tree are unchanged. Other GPUs, and M5 machines on older macOS, keep the simdgroup kernels, and if the tensor-unit kernels fail to build, the simdgroup kernels load instead.

The tensor unit keeps all 24 bits. A stress harness on an M5 Ultra ran groups that pass the check and sit at the bound. That includes sums within 0.1% of 2^24 * low, k-order partial sums reaching 99.9% of it, and 9.3M sums of at least 2^23 units with the lowest unit set.

layout safe group sums tensor unit vs exact decode chain vs exact tensor unit vs chain
up 61,515,584 0 0 0
down 57,801,024 0 0 0

The unsafe controls (groups that fail the check) do differ: 115,500 and 135,903 of 5.6M / 9.3M sums against the exact sum. So the harness can see a difference when one exists.

Speed. Release layer 2, position 0, an M5 Ultra, ms (two main runs and two branch runs in one session):

stage main 2048 branch 2048 main 4096 branch 4096
xe_up 25.14 12.90 45.17 23.55
xe_down 12.17 7.22 21.80 13.22
layer 96.2 79.1 185.4 154.7

The simdgroup kernels were already at about 82% of the simdgroup fp32 MMA peak, so retiling alone could give about 1.2x at best.

Checks:

  • Kernel contract (--only "dsv41 xmma", both kernel sets): 6 kernels: 6 pass gates 1-4, 0 fail; 16 controls: 16 red as planted, 0 missed; 0 limit problems.
  • Equality with the decode kernels and chunk invariance: 0 values differ for both sets.
  • The whole dsv41 subset: 92 kernels pass, 194 controls red.
  • zig build test-dsv41-metal passes, including grouped vs row kernels at chunks 64 and 300 (0 differ). Lean: 0 new.
  • Stage hashes: main's own xe_up/xe_down hashes change run to run (the plan's atomics order pairs inside a slot). Hashing act and eout in pair-id order gives identical values on all four runs, and the experts, shared and post hashes equal main's.

On M5 the prompt scratch grows by about 35 KB a chunk row (about 141 MB at 4096 rows) for the half codes and down's inputs.

  • Synthena (Ash's Agent)

ashhart added a commit that referenced this pull request Nov 14, 2026
…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.
…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.
…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.
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.
…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).
@ashhart
ashhart force-pushed the lead/dsv41-xmma-tiles branch from 44784df to 1c63e8e Compare November 15, 2026 12:46
@ashhart
ashhart merged commit baed910 into main Nov 15, 2026
13 of 20 checks passed
ashhart added a commit that referenced this pull request Nov 15, 2026
…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.
@ashhart
ashhart deleted the lead/dsv41-xmma-tiles branch November 15, 2026 13:21
ashhart added a commit that referenced this pull request Nov 17, 2026
…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
ashhart added a commit that referenced this pull request Nov 17, 2026
ashhart added a commit that referenced this pull request Nov 17, 2026
…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
ashhart added a commit that referenced this pull request Nov 17, 2026
ashhart added a commit that referenced this pull request Nov 18, 2026
…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
ashhart added a commit that referenced this pull request Nov 18, 2026
ashhart added a commit that referenced this pull request Nov 18, 2026
…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
ashhart added a commit that referenced this pull request Nov 18, 2026
ashhart pushed a commit to gilby/TensorFold that referenced this pull request Nov 19, 2026
ashhart#3330 added four "dsv41 xmma units " launch records built only on GPUs with tensor units, without adding a pre-M5 gate in metal_limits.supported. On GPUs without tensor units, Gate 5 failed with LibraryLoad on those four records.

Add metal_dsv41_limits.tensorOnly, gate "dsv41 xmma units " launches through metal_support.tensor in metal_limits.supported, and cover both xmma variants with a host test in metal_mapping_test.
ashhart pushed a commit that referenced this pull request Nov 19, 2026
#3330 added four "dsv41 xmma units " launch records built only on GPUs with tensor units, without adding a pre-M5 gate in metal_limits.supported. On GPUs without tensor units, Gate 5 failed with LibraryLoad on those four records.

Add metal_dsv41_limits.tensorOnly, gate "dsv41 xmma units " launches through metal_support.tensor in metal_limits.supported, and cover both xmma variants with a host test in metal_mapping_test.
ashhart added a commit that referenced this pull request Nov 19, 2026
…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
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.

1 participant