Skip to content

DeepSeek-V4.1 Metal: cone prefill replays only the decoder rows the kept state needs (TF_DSV41_CONE), and the pool capture race fix - #70

Draft
BlivionIaG wants to merge 3 commits into
pr/dsv41-handofffrom
pr/dsv41-cone
Draft

BlivionIaG wants to merge 3 commits into
pr/dsv41-handofffrom
pr/dsv41-cone

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

What this changes

Before: the served Metal DeepSeek-V4.1 engine ran all 40 layers over every prompt row, although the kept state (the window rings, the pool rows, the taps and the last logits) only depends on the last 2,668 rows (128 + 20 * 127) of the decoder layers 20 to 39. After: TF_DSV41_CONE (default 2 on this head; 0 is the full pass, 1 is one replay program) runs the encoder layers (0 to 19, plus layer 20's compressor-only attention pass) over every chunk of a pending prompt and stashes the state entering layer 20 for the last 2,668 rows in a 4,096-row ring (51,296 bytes per row); at the prompt's end the decoder layers replay over the cone, in mode 2 as programs over layers [20,24), [24,28), [28,32), [32,36), [36,40) (33.1k layer-rows against 53.4k), group by group so each layer sees its chunks in order. Anything that needs the slot's state mid-prompt flushes the run (snapshotSave, an ineligible call, a run of 64 chunks); a second prompt while one is pending takes the full path; contexts of 131,072 or more or TF_DSV41_BLOCK_FILTER use one program; the cone never runs while the layer-split gate is armed. The prompt cache's tail mark cuts a chunk under the cone instead of being captured inside a merged chunk. A check harness (TF_DSV41_CONE_CHECK=4096,8192,...) compares full against modes 1 and 2 region by region.

The second commit fixes a pool-kernel race the harness found: dsv41_pool ran one threadgroup per completed ratio-2 group and every group wrote its two raw rows to capture slot position % 8, so a chunk of more than four groups (any prefill chunk) had groups writing one slot concurrently and the slot kept whichever finished last. The capture is now written only by a call's last capture_rows / ratio groups (same bytes as before for calls of four groups or fewer); the layout is unchanged. The hand-off export's carry row reads that slot straight after a prefill. The third commit makes the cone the default (this is the branch head's choice; see the note below).

Measured results

An M5 Ultra Mac, integration build, one slot, 4,096-row steps, synthetic tokens, context 40,960, drafts on, 128 greedy steps; prefill tok/s, first run / second run.

prompt full pass cone 1 cone 2 cone 2 against full
4,096 1149 / 1148 1333 / 1334 1515 / 1514 1.32
8,192 1075 / 1135 1613 / 1666 1813 / 1813 1.69 / 1.60
16,384 1044 / 1118 1787 / 1905 1967 / 1996 1.88 / 1.79
32,768 1022 / 1099 1840 / 2045 2002 / 2100 1.96 / 1.91

Prefill seconds (first run), full / cone 2: 3.56 / 2.70, 7.62 / 4.52, 15.70 / 8.33, 32.07 / 16.37. The ratio is the figure to use (the full path here ran faster than an earlier 0.91 to 0.97k baseline).

Identity (harness TF_DSV41_CONE_CHECK, both modes, 4k, 16k and 32k with zeroed buffers before each prefill; 8k in the first run): last-token logits 0 of 129,280 differ bitwise, the greedy 128-token sha is equal (4,096: 9cec2789a3d379ad; 8,192: 71f08fb2a718748c; 16,384: 250a2c1fa995da1d; 32,768: 0195e8df9914b095), all 40 layers' window rings, the pool storage (0 of 36,454,400 bytes) and the taps (0 of 3,932,160) are identical. With the capture fix the live scratch (rings, partial scratch and the raw-row capture) is also identical: 0 of 2,863,104 bytes, on the build with the fix, and 24 five-row verify rounds with rollbacks to 1 to 4 rows after the prompt give equal logit/pick digests for full, mode 1 and mode 2 (4,096: ef18a284729fae33; 16,384: aace12b9816558cc; the 32,768 digest was not recorded when this was written). Before the fix the capture differed in about 81 to 88 KB of 128 KB for any two prefills, full against full included, because of the race.

  • Served end to end (M5, binary c8, drafts and taps on, step 4096, one slot, greedy, 64 tokens, two corpus slices per size): TTFT with TF_DSV41_CONE=0 vs the default (mode 2), reply sha equal in all 8 pairs:

    prompt tokens TTFT off TTFT on x
    about 4.1k (4,081 / 4,056) 4.06 / 3.99 s 3.17 / 2.96 s 1.28 / 1.35
    about 7.9k (7,890 / 7,882) 8.02 / 7.93 s 6.28 / 6.07 s 1.28 / 1.31
    about 15.4k (15,352 / 15,345) 15.55 / 15.04 s 9.95 / 9.68 s 1.56 / 1.55
    about 29.8k (29,777 / 29,785) 30.65 / 30.58 s 18.41 / 17.65 s 1.66 / 1.73

    The served gain is 15 to 20 % below the harness's, because the server adds tokenization, the template, the first decode step and, under the cone, the prompt-cache tail mark's extra replay (not itemized). Decode is unchanged (33.5 to 34.1 tok/s with drafts, both arms). The server's drafts ran (a drafting-off decision per request, first-draft acceptance 0.11 to 0.68), so the verify and keep path ran.

  • Merged build (integration build 9fb4702ca5, the cone merged with the attention, matvec and prefix work): the cone-on vs cone-off check on the M5 passed with 6 of 6 replies identical.

  • Not measured: head-of-line under mc_e2e and holbench with the cone off and on (the served table is solo prompts); the cone together with the batched-attention PR on this branch (a scratch merge was identity-checked, the branch does not contain it); the 32,768-token rollback digest; any prompt shorter than a chunk's worth of cone (short prompts take the full path). Per-layer row shrinking (one row less per layer instead of four-layer steps) would save about 0.1 s more per prompt and was not built.

  • Note on the default: the head of the source branch turned the cone on by default after the capture fix; the notes' own table was written for the opt-in knob. Exactness holds for the engine's own outputs (bit-identical to the full pass), which is why a default is allowed; reviewers can ask for the opt-in form (TF_DSV41_CONE=2 to enable) by dropping the last commit.

Checks on this head

  • Head commit: df8042e39c.
  • Lean checker (python3 tools/pr_check.py --base pr/dsv41-handoff): 384 on the base and on this head, 0 new; 0 errors; 1 advisory warning (no --pr).
  • Tests on a macOS arm64 host, Zig 0.17.0 (no Metal toolchain): zig fmt --check clean; test-dsv41-env-host 14/14, test-environment-host (cached, passed), test-environment-report-host (cached, passed); every step exit 0, fail count 0. The cone's own tests (stash helpers, the device test for the pool race) link Metal and were not run on this machine; they ran on the Mac per the author's notes.
  • CI: not run on the fork.
  • Not run on this head: zig build native, test-native, the device tests.

Receipt

  • Main against branch: the table above (prefill 1.3x to 2.0x at 4k to 32k, bit-identical logits, greedy, rings, pool and taps).
  • Boxes it reaches: Metal, DeepSeek-V4.1 served engine (local prefill; the hand-off landing path is unchanged).
  • New switch: TF_DSV41_CONE (default 2; 0 full pass; 1 one program); registry, docs/environment.md, the strict-environment count test.
  • Exactness: native outputs bit-identical to the full pass on the sizes above; no CUDA/Metal mixing.
  • Decode speed: unchanged; prompt speed: above.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it: the pool-race device test failed against the old kernel (slot of position 4090 held row 4058) and passes after the fix per the author's notes; not re-run here.
  • 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.

BlivionIaG added 3 commits November 11, 2026 20:39
…he last rows only (TF_DSV41_CONE)

The encoder layers run over every row and stash the state entering layer 20; at the prompt end the decoder layers replay over the cone the kept state needs, in four-layer programs. Check harness: TF_DSV41_CONE_CHECK.
…s last groups only

A chunk of more than four groups had every group write its slot (position % 8), so each slot held whichever group finished last. The check harness also compares the partial and capture scratch per owner and runs speculative rollback rounds at the prompt's end.

This branch has not been deployed

No deployments
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