Skip to content

server: the JSON writer stays in bounds on a UTF-8 sequence cut off at a string's end (DeepSeek-V4.1 stack 1/14) - #634

Merged
ashhart merged 1 commit into
ashhart:mainfrom
jayleaton:stack/01-json-bounds
Oct 11, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
jayleaton:stack/01-json-bounds

Conversation

@jayleaton

Copy link
Copy Markdown
Contributor

Part 1 of 14 of the DeepSeek-V4.1 Zig engine stack.

The DeepSeek-V4.1 stack

This is the first of 14 pull requests that bring a DeepSeek-V4.1-Flash engine to the Zig runtime. It serves
DeepSeek-V4.1-Flash (EXL3 2.8 bpw) on two DGX Spark nodes with TP=2 over RoCE. The engine has run in production
against the Python serving path, and the recipe with its measurements is public in
jayleaton/deepseek-v41-tensorfold-spark#18.

Each layer builds on the one below and keeps every existing family's behaviour; new paths are off until a family
installs them. GitHub cannot stack pull requests across forks, so each one targets main and names the one it
depends on: please review only the commits new in that layer. Layers 1–7 are generic building blocks; 8–14 are the
family itself.

  1. JSON writer bounds on a cut-off UTF-8 sequence
  2. CUDA keyed graph cache, device arena, launch error names
  3. Tensor-parallel fabric: bootstrap, NCCL, mailbox and RoCE collectives, plan link, fail-fast
  4. Session memory for paged-KV families: page pool, prefix trie, RAM budget, NVMe tier
  5. Grammar-constrained decoding over xgrammar 0.2.8
  6. Lane host: a family's round planner with prompt pieces; halt at a round boundary
  7. Server: family chat hooks, Spark-compatible wire, drain on stop, pre-first-token keepalives
  8. DeepSeek-V4.1 core: config, weights, attention (compressed and sparse-indexed KV), MoE, mHC (in parts)
  9. KV layout and rows
  10. Prefill: pieces, CED replay, heat bands
  11. Drafting and sampling
  12. Serving: DeepSeek encoding, reasoning and tool calls, the family's server glue
  13. Speed settings, one change each
  14. Vision (only once the GPU path is bit-exact)

DeepSeek-V4.1 brings its own architecture to the runtime: compressed KV with a sparse indexer, Engram lookups,
mHC residual mixing, MTP drafting, and DSML tool calls.

Measured against the production Python path on the same two nodes, same pack and bench scripts, the same night
(tok/s; decode cells are the best of 3 warm repetitions, prompt cells 3 cold prefills; from the published recipe):

Cell Zig Python Δ
code T0, 4 streams 156.4 144.1 +8.6 %
prose T0, 4 streams 104.7 98.2 +6.6 %
code T0, 1 stream 90.3 88.0 +2.6 %
32K prompt prefill 2,686 2,440 +10.1 %
131K prompt prefill 2,496 2,264 +10.3 %
131K decode 69.26 67.04 +3.3 %

Replies were identical to the Python path's on the bench (16/16 short replies and the long reply hashes). One pair
of nodes, one night, no confidence intervals.

The stack is incremental; layers 8–14 follow as each passes its gates.

This layer

Review the commit new in this layer:

  • 4284048d The JSON writer escapes a UTF-8 sequence cut off at a string's end as U+FFFD instead of slicing past it.

What it adds and why

The JSON writer escapes a UTF-8 sequence that is cut off at the end of a string as U+FFFD instead of slicing past the
string's end. A streamed reply can end mid-sequence (a token boundary inside a multi-byte character), and the writer
read past the slice in that case.

How

  • json_write.zig: the escape loop clamps a sequence's length to the bytes left; a short tail is written as
    \ufffd and consumes only the bytes that are there. With a test.

Test evidence

Host only: Linux x86_64, Zig 0.17.0, one job at a time with -j4, a fresh empty --cache-dir for every run. Base is main at 38de96bf.

python3 tools/zig/lean_check.py
zig fmt --check <the Zig files this stack adds or changes>
zig build -j4
zig build test -j4
Check Base main This layer
lean_check.py 542 problems 542 problems, all inherited from main: 0 new fingerprints
zig fmt --check 1 file, 0 unformatted
zig build 8/8 steps 8/8 steps
zig build test 37/37 steps; 400/400 tests passed 37/37 steps; 401/401 tests passed

Not run: test-golden (not available on Linux, base main included), any GPU or device step and any checkpoint gate. This code, before its rebase onto current main and the generic renames, serves DeepSeek-V4.1 in production on two DGX Spark nodes; this tree itself has had no GPU run.

@plotarmordev

Copy link
Copy Markdown
Contributor

Checked on Linux aarch64 (DGX Spark, Zig 0.17.0), CPU only:

  • The new test on main (a6aa4782, the test appended to its json_write.zig): panic: start index 5 is larger than end index 3, the slice past the string's end. On this PR (4284048d): passes.
  • zig build test on this PR: 37/37 steps, 401/401 tests. zig fmt --check zig/src/server/json_write.zig: clean.

Reading the change: i += n now advances only over the bytes that are there, and a 2-byte remainder of a 3-byte sequence no longer goes through decodeWtf8's 2-byte arm (which gave a wrong code point rather than U+FFFD). Looks right to me.

@ashhart
ashhart merged commit ec3041d into ashhart:main Oct 11, 2026
2 checks passed
@ashhart

ashhart commented Oct 11, 2026

Copy link
Copy Markdown
Owner

Thank you, @jayleaton. This is in 1.0.6 as merge ec3041d34, unchanged: a UTF-8 sequence cut off at the end of a string now becomes one U+FFFD, and the writer stays inside the string. Your four test cases pass (zig test zig/src/server/json_write.zig), and the CHANGELOG credits you.

Parts 2 to 5 of the stack are in review. Each one is building blocks with no family using them yet, so we're reading them alongside the parts that put them to use.

BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
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.

3 participants