Skip to content

deepseek-v41-zig: two fixes from main (template append in place, JSON writer bound) - #24

Merged
bertholomus merged 3 commits into
bertholomus:deepseek-v41-zigfrom
hashd1ve:dsv41-port-template-json
Oct 19, 2026
Merged

bertholomus merged 3 commits into
bertholomus:deepseek-v41-zigfrom
hashd1ve:dsv41-port-template-json

Conversation

@hashd1ve

Copy link
Copy Markdown

What this changes

Brings two fixes from TensorFold main that deepseek-v41-zig predates, cherry-picked with their authors:

Without the first fix, DeepSeek-V4.1's chat template ({% set ns.out = ns.out + ... %}, 22 times per pass, rendered twice per request) took rank 0 ~10.9 KB of host memory per prompt token in long agent sessions. That was ~3 GiB per request at 288K tokens, enough to take a GB10 head to its memory floor. Details and measurements are 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: rank 0's peak for 40K tokens in 2,000 messages went from +1,555 to +244 MiB with our rewritten template. The /tokenize ids for 1,500 random conversations are identical before and after. Upstream's new template test fails on 9f82099 (OutOfMemory within its 24 MiB budget) and passes with the change.

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; no prompt-pass code changes)
  • 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.

@bertholomus
bertholomus merged commit 0937cc1 into bertholomus:deepseek-v41-zig Oct 19, 2026
@bertholomus

Copy link
Copy Markdown
Owner

Thank you @hashd1ve for bringing these two upstream fixes over with their authors kept, and for the memory measurements. Merged.

bertholomus pushed a commit that referenced this pull request Oct 20, 2026
…che whole (#18)

Loader.experts() visits a layer's expert entries pass by pass (every gate trellis, then every up, then every down, then each suh/svh stack), but the rank cache stores them expert by expert, so a load made nine strided sweeps over each layer's ~2.4 GiB and defeated kernel readahead (recipe issue #24). experts() now takes the byte extent of the layer's expert entries from the index, reads it whole with O_DIRECT (core/direct_io, 64 MiB pieces) into one aligned buffer and serves the entries inside it as views; visiting order, uploads and layouts are unchanged, and a failed direct read logs a warning and falls back to per-entry reads. rank_cache.writeFake is pub for the new test. 2x GB10, 2.9 bpw pack, cold page cache: tf-dsv41-load --cache 66.3 / 67.4 s -> 15.7 / 15.7 s per rank, served rank 0 loaded in 70.8 s -> 19.3 s; 2123 of 2252 tensors byte-identical across builds (the other 129 are the *.ffn.experts.ptr0/1/2 device-address tables) and greedy replies on three prompts unchanged. zig build test green (19/19 steps, 184/184 tests) on Zig 0.17.0 aarch64; lean_check at the branch's 1420.

Co-authored-by: lepetitprince716-prog <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 9a0c014)
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.

4 participants