Repository navigation
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
Draft
BlivionIaG wants to merge 3 commits into
BlivionIaG wants to merge 3 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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(default2on this head;0is the full pass,1is 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 orTF_DSV41_BLOCK_FILTERuse 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_poolran one threadgroup per completed ratio-2 group and every group wrote its two raw rows to capture slotposition % 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 lastcapture_rows / ratiogroups (same bytes as before for calls of four groups or fewer); the layout is unchanged. The hand-off export'scarryrow 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).pr/dsv41-handoff). Own commits: the cone, the capture fix, the default.atc overlap --json: not run (the installed atc refuses this repository's.github/atc.json). Files:deepseek_v41/forward_cone.zig(new),forward_impl.zig,attention_*.zig,model*.zig,kv.zig, thekv.metalpool kernel,native/deepseek_v41_cone_check.zig; dsv41 metal: the lane target, the prompt cache and the native server over Metal ashhart/TensorFold#1720 to dsv41 metal: image prompts ashhart/TensorFold#1722 touch the other DeepSeek-V4.1 Metal stack's files.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.
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=0vs the default (mode 2), reply sha equal in all 8 pairs: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_e2eand 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=2to enable) by dropping the last commit.Checks on this head
df8042e39c.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).zig fmt --checkclean;test-dsv41-env-host14/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.zig build native,test-native, the device tests.Receipt
TF_DSV41_CONE(default 2; 0 full pass; 1 one program); registry,docs/environment.md, the strict-environment count test.Checklist