Skip to content

DeepSeek-V4.1 on Metal: thinking follow-ups and new conversations resume from the prompt cache - #3345

Closed
ashhart wants to merge 4 commits into
mainfrom
lead/dsv41-think-snapshot
Closed

ashhart wants to merge 4 commits into
mainfrom
lead/dsv41-think-snapshot

Conversation

@ashhart

@ashhart ashhart commented Nov 14, 2026

Copy link
Copy Markdown
Owner

DeepSeek-V4.1 on Metal: the prompt cache kept whole prompts only, and two common follow-ups never matched one.

  • The next turn of a thinking conversation. A thinking prompt ends in <|Assistant|><think>, and the template renders that turn in the next prompt as <|Assistant|></think>… (reasoning dropped), so the previous prompt was never a prefix. Every turn read the whole conversation again: 7.5K tokens of an agent harness's system prompt plus the conversation, about 6-8 s a turn at the start and tens of seconds once a session grows.
  • A new conversation with the same system prompt and tools. The previous entry also held the old first message, so nothing matched.

Prefill now runs a prompt in up to three parts and keeps a state at each cut: before the first <|User|> (once the system part is 256 tokens or more and not already kept), and before the final <think> of a thinking prompt. A thinking prompt's own whole-prompt entry is no longer saved, since a later prompt would only ever match it exactly. Resuming from a kept state is the path cached prompts already take, so replies are unchanged. The <think> cut costs one extra row. The system cut costs the decoder cone once when a system prompt is first seen.

The three token ids are new config fields with the release vocabulary's values (user 128803, assistant 128804, think_open 128821); the fixtures set their own.

Tests (new served cases):

  • (c2) a thinking turn and its follow-up: the follow-up resumed 301 rows (its turn before <think>), and both replies equal fresh runs.
  • (c3) two conversations sharing a 300-token system part: the second resumed exactly 300 rows, and both replies equal fresh runs.
  • test-dsv41-metal-serve and test-dsv41-metal pass on an M5 Max (1,137 checks). Lean: 0 new.

On an M5 Ultra, served to an agent harness (omp) with thinking on:

before after
thinking follow-up turn (7.5K prompt) cached 0, ttft 6.4 s cached 7,464 / 7,533, ttft 0.33 s
new conversation, same system prompt cached 0, ttft ~8 s cached 7,360 / 7,456, ttft 0.41 s
two-turn probe (1.8K tokens) cached 0 cached 1,839 / 1,857

The first conversation after a server start is still cold (6.9 s for 7.5K tokens).

  • Synthena (Ash's Agent)

…ame launch

TF_DSV41_TREE=2..4 ended served replies after a few tokens on Metal: the
target refused every window whose parents were not one chain (TreeWindow)
and every keep that was not a prefix.

A tree window now splits into chains: one from row 0 and one from each row
that is not its parent's first child, with the path before it. Chain 0 runs
on the stream's slot; each other chain runs on a shadow slot that equals the
slot below the window (copied whole when first bound, then only the
positions committed since: window rows, pool and index-key rows, carries),
all in one launch. A row run again in front of a branch must choose as on
its own chain. A kept path in a shadow's chain trades the two slot states,
so the slot holds what the accepted chain would; the taps map points the
drafter's ingest at the kept rows. Shadows are made on the first tree window.

serve_lanes.Model gains `trees` so tests set the knobs without the
environment. New tests: the target test runs trees whose main chain, first
or second sibling, or a deeper branch wins, and checks every choice against
serial decode, every kept row's taps bit for bit against the path run as a
chain, and a cached turn after trees against a fresh one; served (g) runs
trees with one and two siblings, drafted == draft:false == serial.
…; kept branches copy into the slot

Review of #3326: the target held one tree state, so two streams' tree windows in one window call
(draft/lanes.zig sends every stream's window together and keeps each after) would overwrite each
other's chains and shadow, and the first stream's keep would fail with NotAPath.

Each slot now has its own tree window (Tree.open, made on the first tree), and all tree windows of a
call are prepared first, then their chains run together, R rows a launch. A chain's shadow is one no
pending tree holds (its slot's own first, then a free one, another slot's, or a new one), so trees of
one round never share a shadow.

A kept sibling no longer swaps slot states: its kept rows (window rows, pool and index-key rows),
carries, compressor inputs and taps rows are copied into the slot, so the slot's buffers never change
and stay the ones the target joined to the weights' residency owner. A new shadow joins that owner
when the target did (Resident.tryAddNow, refused past its load limit) and leaves it before it is freed.

Tests: the tree target test also runs two slots' trees in every window call (each choice == serial,
each kept row's taps == its chain's) and checks the shadows join and leave the residency owner (the
fixture can now open with one); served (h) runs two streams with trees on two slots, overlapping,
both buying trees, each == "draft": false == serial.
…urn and before a thinking prompt's <think>

A thinking prompt ends in <|Assistant|><think>, and the next turn renders that turn as <|Assistant|></think>..., so the
whole-prompt entry was never a prefix of a follow-up and every thinking turn read the whole conversation again. A new
conversation sharing the system prompt and tools found nothing either. Prefill now runs the prompt in up to three parts
and keeps a state at each cut (before the first <|User|> once the system part is 256 tokens or more, and before the final
<think>); the resume path is the one cached prompts already take.
@ashhart

ashhart commented Nov 15, 2026

Copy link
Copy Markdown
Owner Author

Landed on main with #3384, which was stacked on it: its commits are on main as 7c7281b and ebb12cd, and its files on main equal this PR's head. Closing as landed.

  • Synthena (Ash's Agent)

@ashhart ashhart closed this Nov 15, 2026
ashhart added a commit that referenced this pull request Nov 17, 2026
Seven duplicate entries go (a PR's own line beside its lines-file line), five outside authors are thanked, #1503 gets the line its close note promised, and eleven lines now match their PRs' code and receipts (#3129, #3194, #3258, #2696, #2921, #3365, #3345, #3384, #3679, #3682, #3699, #3393).
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