Skip to content

fix(dsv41): a prefix two conversations share no longer stales the first one's kept states - #26

Merged
bertholomus merged 1 commit into
bertholomus:deepseek-v41-zigfrom
hashd1ve:dsv41-kept-state-placement
Oct 19, 2026
Merged

bertholomus merged 1 commit into
bertholomus:deepseek-v41-zigfrom
hashd1ve:dsv41-kept-state-placement

Conversation

@hashd1ve

Copy link
Copy Markdown

What this changes

Fixes a kept state that is lost when a second conversation shares its prefix. With an agent client this cost a full re-prefill whenever a subagent or a new session started while the main session was idle: 3–4 min at ~350–400K tokens.

  • Shared prefix. keptOver skipped every same-base sibling, so the second conversation resumed the shared cut in place and staled the first one's longer marks. A sibling longer than the resumed state now blocks the resume in place, and the second conversation copies the shared rows instead.
  • Room to grow. placeSoft first tries to leave growth_rows past each kept extent. A stream placed flush against a conversation made that conversation's next turn move, and a large one could find no room and stale its own state. It falls back to the current passes, so a full pool packs as before.
  • Logging. A kept state of 64K tokens or more that goes stale is logged with the reason.

Reported with a repro in bertholomus/deepseek-v4.1-tensorfold-tp2-2xgb10#29.

Receipt

  • Environment, with the TensorFold commit, MLX or PyTorch version, machine and GPU, checkpoint and revision: deepseek-v41-zig at 9f82099 plus this change. Two DGX Sparks (GB10, 128 GB each), TP=2 over RoCE with both rails, nvcr.io/nvidia/pytorch:26.07-py3. Checkpoint easthambuilds/DeepSeek-V4.1-Flash-1017-EXL3-2.9bpw at 2913ea75. Served with --context 1048576 --parallel 4, drafts and CUDA graphs on.

  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: not run. Instead, 12 greedy chat prompts (temperature 0, 128 tokens, drafts on) give byte-identical text to the unpatched 9f82099 image, with this change and the two companion PRs built together.

  • Decode speed before and after, from tools/bench_openai.py: not run. The recipe's kit_bench.py decode --set a gives prose 57.0 → 56.9 and structured 104.9 → 104.3 tok/s, with the three PRs together.

  • Prompt speed before and after, from tools/prefill_cold.py: not run. No prompt-pass code changes.

  • Tests run, with pass, fail and skip counts: zig build test (CUDA host tests, aarch64 Linux) 219 pass, 0 fail, 0 skip, with the three PRs together.

  • Not run, and why: Metal (no Mac set up for this branch); the three tools above (we measured with the recipe's tools before we read this template).

  • For this change, on the server: conversation A (a system prompt of ~9K words plus one tool, a 60K-word user turn), then a new conversation B with the same system prompt, then A's next turn.

    A1 B1 A2
    9f82099 69,273 tokens from 0, kept 3 resumed at 8,192 resumed at 0, failed 1
    this change same resumed at 8,192 resumed at 67,584
  • The PR fix(dsv41): protect retained cache extents during growth #10 test follows the new layout. The eight histories sit growth_rows apart, a larger continuation resumes in place within that room, and under pressure the oldest states still go first.

  • The room-to-grow case is covered by unit tests only; we have not reproduced it on the server.

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (not run as such; greedy replies equal the unpatched engine's)
  • No precision traded for speed. If the bits change, the description says which sums change and why.
  • Prompt processing is no slower than the last release. (not measured; placement only, and fewer prompts now run from 0)
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (they pass with it; I have not run them on 9f82099 alone, where both read the old behaviour)
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

…st one's kept states

A new conversation with the same system prompt and tools resumes from the shared cut, a mark inside the first
conversation's extent. keptOver skipped every state with the same base, so the second conversation resumed in place,
wrote over the first one's longer marks and staled them, and the first conversation's next turn ran from 0 (with an
agent client, a subagent or a new session started while the main session was idle). A sibling longer than the state
being resumed now counts: a conversation's own next turn still resumes in place, another one copies the shared rows
to a free extent.

Placement is first fit, so a new stream often landed flush against a kept extent's end; that conversation's next
turn then clashed with it and had to move, and a large one found no contiguous room and staled its own state.
placeSoft first tries to leave growth_rows (16 chunks) past each kept extent, then falls back to the extents and the
rows as before, so a full pool packs as it did.

A kept state of 64K tokens or more that goes stale is logged with the reason.

Tests: two new (a shared prefix, room to grow); the PR bertholomus#10 test follows the new layout (the eight histories sit
growth_rows apart, a larger continuation resumes in place within that room, under pressure the oldest states still
go first).
@bertholomus
bertholomus merged commit 629c524 into bertholomus:deepseek-v41-zig Oct 19, 2026
@bertholomus

Copy link
Copy Markdown
Owner

Thank you @hashd1ve for finding this and for the clear repro. Merged.

bertholomus pushed a commit that referenced this pull request Oct 21, 2026
…st one's kept states

A new conversation with the same system prompt and tools resumes from the shared cut, a mark inside the first
conversation's extent. keptOver skipped every state with the same base, so the second conversation resumed in place,
wrote over the first one's longer marks and staled them, and the first conversation's next turn ran from 0 (with an
agent client, a subagent or a new session started while the main session was idle). A sibling longer than the state
being resumed now counts: a conversation's own next turn still resumes in place, another one copies the shared rows
to a free extent.

Placement is first fit, so a new stream often landed flush against a kept extent's end; that conversation's next
turn then clashed with it and had to move, and a large one found no contiguous room and staled its own state.
placeSoft first tries to leave growth_rows (16 chunks) past each kept extent, then falls back to the extents and the
rows as before, so a full pool packs as it did.

A kept state of 64K tokens or more that goes stale is logged with the reason.

Tests: two new (a shared prefix, room to grow); the PR #10 test follows the new layout (the eight histories sit
growth_rows apart, a larger continuation resumes in place within that room, under pressure the oldest states still
go first).

(cherry picked from commit 8dd20d2, TF PR #26, onto the served TP4 build 5c976359;
its two hunks that update a host test this line does not have are left out, its new tests are kept)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
(cherry picked from commit 2060ba0fc17ef4f2b5de41985fd99a42a2fc1216)
bertholomus added a commit that referenced this pull request Oct 21, 2026
The kept-states stack of fill29 (a shared prefix no longer stales the first conversation's kept states; prompts with
lane images keep their states, keyed by the images' digests; a resumed prompt skips the vision tower for the images
it holds; TF_FILL_CHUNKS, unset as served) onto zdirdb's candidate ffa2f163 (the shield, structured output, reply
ids, DSML for tool_choice required, the stats line, d143).

Conflicts:
- families/dsv41/lanes.zig: both sides' fields and release; structured output's first_masked and held in whole_vt
  (steps_vt copies it); the grammar's kept first row in prefillPart after the first token's draw (the lane core
  draws it in the same step: open.zig fillStream, opened, drawFirst); both sides' tests.
- server/prompt.zig: the history renders as the prompt does (renderIdsOf with the lane images), so with
  TENSORFOLD_PLAIN_SPECIALS a lane image's placeholder stays the template's in both and #28's history length
  holds; text requests render as before.

Every other file's change against dc464126 (5f1b1cbe + d143) is ffa2f163's against 89872573 (fbee5419 + d143).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
bertholomus added a commit that referenced this pull request Oct 21, 2026
The four-node line that serves DeepSeek-V4.1-Flash-1017 on four GB10 nodes comes in as its own commits since v0.2.0
(22, six of them merges). This merge keeps the branch a fast-forward of its head with pull requests #30 and #31
(b8b115e).

Against v0.2.0:
- A replay cut uploads its rows' positions into the chunk's aligned buffer (a prompt resumed after a kept text prefix
  no longer stops with MissingTritonVariant).
- Kept prompt states: pull request #26 (@hashd1ve), a prefix two conversations share no longer stales the first one's
  kept states; prompts with lane images keep and resume their states, matched by the images' digests (after #28 by
  @hashd1ve), and a resumed prompt skips the vision tower for the images its kept state holds; a kept state the lanes
  would refuse is passed over for an earlier one.
- TENSORFOLD_PLAIN_SPECIALS: a sender's special-token text stays text, and a lane-vision engine's image placeholders
  stay its image tokens.
- Structured output (response_format json_object and json_schema) through libtfgrammar; TENSORFOLD_GRAMMAR_LIB names
  its file.
- tool_choice "required" and a named function write DeepSeek's DSML calls block, and a forced call's marker is always
  its token.
- The served sampler draws top_k off (0 and -1) and any top_k.
- The draft head's chains counted by depth for /metrics, with an optional stats line (TENSORFOLD_STATS_EVERY).
- Off unless set: TF_FILL_CHUNKS (#29 by @hashd1ve, on TP4: a prompt pass in pieces between the other streams'
  rounds) and TENSORFOLD_REPLY_IDS.

Both sides changed zig/src/families/dsv41/model.zig and prompt.zig. model.zig takes both: #30's neighbor gather beside
the served line's changes. prompt.zig keeps the served line's replayCut, which uploads every cut's surviving positions
into the chunk's own buffer; #31 uploads them for small odd cuts and keeps the view otherwise, and the positions are
the same. #31's CPU test follows the served replayCut (one upload for every cut) and passes on it, and
replay_positions.md says so; the v0.2.0 baseline still fails it as the document says. Every other file is one side's:
40 the served line's, 17 this branch's.

With TF_DS_NEIGHBOR_RDMA unset, #30 opens no neighbor gather and every exchange takes the path it took before. Rank
0's startup settings now carry the neighbor mode (48 bytes, 40 before), so every rank runs the same build.

Thanks @swbiggart for #30 and @mrsergiopascal for #31.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
bertholomus added a commit that referenced this pull request Oct 21, 2026
…, forced tool calls, five contributions

The README's fork note adds what the branch serves since v0.3.0: kept prompt states for image turns too, a shared
4,194,304-position window, structured output and forced tool calls in DeepSeek's DSML. NOTICE lists the new files (the
prompt shield's, and the neighbor transport's document and receipts), every upstream file the branch changes against
1.0.12 (16 more since v0.2.0) and the contributions: Manrique's pull requests #26, #28 and #29, Scott Woolsey
Biggart's #30 and mrsergiopascal's #31. ATTRIBUTION.md adds them, the ideas taken from DeepSeek's DeepInfer and from
xgrammar's structural tags, and @den13501's report behind the stream keepalive. The family's section of
THIRD_PARTY_NOTICES.md names libtfgrammar in a deployment's kit.

The fork note also thanks the branch's contributors by name. ATTRIBUTION.md says that the EXL3 kernel code copied
from TensorFold's Python line was written before 0.6.0 and keeps its MIT notice (LICENSES/MIT.txt), and names the
co-authors the commits' trailers name.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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.

2 participants