Skip to content

feat(dsv41): prompts with images keep and resume their prompt states, matched by the images' digests - #28

Merged
bertholomus merged 1 commit into
bertholomus:deepseek-v41-zigfrom
hashd1ve:dsv41-image-cache-pr
Oct 19, 2026
Merged

bertholomus merged 1 commit into
bertholomus:deepseek-v41-zigfrom
hashd1ve:dsv41-image-cache-pr

Conversation

@hashd1ve

Copy link
Copy Markdown

What this changes

Prompts with images now keep and resume their prompt states. Before, such a prompt ran whole on every turn, because the cache keyed on tokens and an image's span is the same placeholder token for every image. An agent session with six screenshots reprocessed its whole 73K-token history on every turn (~40 s on two GB10s), and the other streams waited.

  • The cache key. The server gives the request a cache_key: the prompt's ids with each image span spelled by its image's digest. That is one id per position, past every vocabulary (Wyhash of the prepared payload).
  • The history length is now computed for image prompts too, shifted past the spans.
  • The lane host looks up, keeps and reports under that key.
  • The dsv41 lanes resume and keep states whatever images the prompt holds. A state the key matched holds the same images' rows; a prompt with another image at the same span matches only before it.

Receipt

  • Environment, with the TensorFold commit, MLX or PyTorch version, machine and GPU, checkpoint and revision: deepseek-v41-zig at 629c524a 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. The live checks below ran on our served build, which is this branch plus fix(server): DSML calls read from their own tags in the native server (the native side of #20) #25.

  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: not run. Instead, 12 greedy chat prompts give byte-identical text to the unpatched engine; a ~7K-token prompt resumed from its kept state, and again after another conversation, gives the same reply as fresh; /tokenize ids for 1,500 random conversations are unchanged.

  • 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.

  • Tests run, with pass, fail and skip counts: zig build test (CUDA host tests, aarch64 Linux) on this branch: 222 pass, 0 fail, 0 skip.

  • Prompt speed before and after, from tools/prefill_cold.py: not run. The prompt pass is unchanged; only which states it keeps and resumes.

  • For this change, on the server: a conversation with a 224×224 PNG and a ~7K-token file (thinking off, temperature 0). Its second turn, then the same second turn with another image. We recorded the reference replies on the unpatched engine, where every prompt ran whole.

    turn 1 turn 2, same image turn 2, another image
    unpatched 6,886 tokens, cached 0 cached 0, "Red." cached 0, "Blue."
    this change cached 0, same reply cached 6,144, "Red." cached 0, "Blue."
  • New tests:

    • Lane host: a prompt with an image resumes past the image when its key matches, and not when the image differs. It fails before this change: nothing is looked up for a prompt with images.
    • Server: the key's ids depend on the image and the position, and stay past the vocabulary. The server's tests build only on macOS, so I ran this one on its own.
  • Not run, and why: Metal (no Mac set up for this branch); the three tools above.

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. (resumed = fresh checked; draft:false not run)
  • 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 with prefill_cold.py; the pass itself is unchanged)
  • 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.
  • 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.

… matched by the images' digests

A prompt with images ran whole on every turn and kept nothing: the cache matched tokens, and an image's span is its
placeholder token repeated, the same for every image. An agent session with a few screenshots then prefilled its whole
history at each turn (73K tokens, ~40 s on two GB10s), and the other streams waited.

The server now gives the request a cache key: the prompt's ids with each image span spelled by its image's digest (one
id a position, past every vocabulary), and the history length past the spans. The lane host looks up, keeps and reports
under that key, and the dsv41 lanes resume and keep prompt states whatever images the prompt holds: a state the key
matched holds the same images' rows. A prompt with another image at the same span matches only before it.

Tests: the lane host resumes a prompt with an image past it when its key matches and not when the image differs
(fails before this change: nothing is looked up for a prompt with images); the key's ids depend on the image and the
position and stay past the vocabulary.
@bertholomus
bertholomus merged commit 3de79d8 into bertholomus:deepseek-v41-zig Oct 19, 2026
@bertholomus

Copy link
Copy Markdown
Owner

Thank you @hashd1ve for this, and for checking the replies against the unpatched engine. Merged.

bertholomus added a commit that referenced this pull request Oct 21, 2026
…ates, matched by the images' digests

A prompt with images ran whole and kept nothing: the prompt cache keys tokens, and a lane image's positions all hold
its placeholder, so two images under the same ids would have shared one state. The server now spells each image
span in the cache key (Request.cache_key, as --vision does): the span's first two words carry 62 bits of its
payload's Blake3 digest whole (image_key.open and close), its other rows image_key.row of those bits, all past every
vocabulary's ids. The lane host looks up and keeps image prompts by that key, and the dsv41 lanes resume and keep
them: a conversation's next turn with the same image resumes past it, another image under the same ids resumes no
row of it. The history length is shifted past the history's image spans, so a turn's kept state lands at its end
as it does for text. A request with lane images but no key one word a position (no caller makes one) neither
resumes nor keeps.

Blake3 over the largest payload (1,024 tokens: 10.8 MB) takes 4.4 ms on x86 at the baseline CPU (SHA-256 23.6 ms;
Wyhash 0.4 ms, but its collisions can be crafted).

Tests: placeSpans (the ids, the key one word a position, the history past its images, another image parts the key
at its span), spellRows, and the lane host (TF #28's test on lane images; a request with no key).

(ported from TF PR #28 by @hashd1ve onto d123's TP4 build 2060ba0f: the lane-vision path in place of upstream's
images, the key words in image_key.zig's form)

Co-Authored-By: Manrique <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
(cherry picked from commit 8dd38aa45b310ba4d27d36075696d729ca785952)
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