Skip to content

perf(qwen3_5): DeltaNet state written in place, no commit copy (8-stream decode 89 -> 142 tok/s, bits unchanged; alternative to #528) - #568

Closed
jkuepker wants to merge 3 commits into
ashhart:mainfrom
jkuepker:qwen35-state-inplace
Closed

jkuepker wants to merge 3 commits into
ashhart:mainfrom
jkuepker:qwen35-state-inplace

Conversation

@jkuepker

@jkuepker jkuepker commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Stacked on #520 (its two commits come first; this change is the last one). This is an alternative to #528, and the two conflict in state.zig.

What this changes

#528 found the cost: after every Qwen3.5 forward, Cache.commit copies about 151 MB of DeltaNet recurrence on the host (48 layers × 48 × 128 × 128 × 4 B on the 27B). #528 moves that copy onto the GPU. This change removes it:

  • gdn_chain writes each segment's last-row state straight into the stream's cache (a new state_final buffer). Each thread reads its own slice into registers before the loop, so writing in place is safe. commit now copies only the 2.9 MB of conv windows, and keep after a fully accepted window copies nothing.
  • A partially accepted draft window copies the kept row's recurrence back from the snapshot on the GPU (forward.keep, qwen35_state_copy). That is one command buffer for all streams; before, it was a second 151 MB host copy.

The values are the same floats written to a different buffer, so no bits change.

Receipt

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. (bench_concurrent.py --alone --serial above; resumed prompts not run separately, since the change does not touch the prompt cache)
  • No precision traded for speed. If the bits change, the description says which sums change and why. (No sums change.)
  • Prompt processing is no slower than the last release. (Same prompt kernels and one host copy fewer a chunk; not measured separately.)
  • 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. (No new tests: tf-qwen35-exact and the load-time check now run the GPU keep.)
  • 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.

BobClawblaw and others added 3 commits October 8, 2026 07:31
…7B is served

The family had the 2B's shape as comptime constants and as constexpr lines in each kernel. The kernels are now
templates whose dimensions the loader prepends as macros when it compiles them; the seven generated projection files
were one body with different constants and become one, compiled once per distinct shape of the checkpoint with the row
decoder's split-K and tile rules kept as code. config.zig reads the dimensions and matches them against a two-entry
table, the pinned 2B and Qwen3.8-27B (hidden 5,120, 64 layers, 48 DeltaNet value heads over 16 key heads, 24 query
heads over 4, a separate output head, bf16 A_log). Buffers, strides, grids and thread counts come from that geometry;
two were invisible on the 2B, where hidden and both mixers' rows are all 2,048: the query buffer takes the wider of the
DeltaNet and attention rows, and the attention partial kernel gets a simdgroup per query head of a group.

The 2B is bit-identical to main (tf-qwen35-forward logits on 300- and 777-token fixtures), passes its 49 exactness
checks and loads the same pipelines. The 27B passes the same 49 checks; against stock mlx-lm its top-1 agreement
matches mlx-lm's own one-row versus chunked agreement at every length (NLL within 0.002); behind the server 120 of 120
concurrent replies equal solo and 72 of 72 solo equal plain, 36 tok/s a stream and 104-108 at eight, 16 GiB RSS.
The fidelity tool falls back to the repository's docs for its third corpus now that the Python engine's left the tree.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
… by a copy kernel

gdn_chain writes each segment's last-row recurrence straight into the
stream's cache (state_final; each thread reads its own slice into registers
before the loop, so in place is safe), so Cache.commit no longer copies
151 MB of recurrence on the host after every forward; only the 2.9 MB of
conv windows stay host copies. A partial keep copies the kept row's
recurrence back on the GPU (forward.keep, qwen35_state_copy, one command
buffer for all streams); a whole-window keep copies nothing, since commit
already left the cache at the window's last row. The same floats land in
the cache, so no bits change.

Qwen3.8-27B 4-bit on M5 Max, -Dcpu=apple_m1, builds alternating:
bench_concurrent (code prompt, 256 tokens) aggregate tok/s at 1 / 4 / 8
streams 20.9 / 61.1 / 88.9 -> 22.8 / 81.6 / 142.1; bench_openai one stream
20.8-21.6 -> 22.4-23.1. tf-qwen35-exact 49/49 (partial keeps included),
bench_concurrent --alone --serial all equal, the 5,031-token probe reply
and the load-time wide-lane check unchanged.
@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thank you, @jkuepker. Writing the DeltaNet state in place instead of copying it is a clean idea, and your eight-stream table (89 to 142 tok/s, every reply equal to its solo run) is the kind of receipt we like to see.

We're closing this one because the 27B now has its own engine. 1.0.3 serves Qwen3.8-27B through a separate family (34c4424, https://github.com/ashhart/TensorFold/releases/tag/v1.0.3). A qwen3_5 checkpoint with the 27B's geometry opens that engine (zig/src/native/metal.zig), and its rounds already commit the DeltaNet state on the GPU (zig/src/families/qwen27/decode_round.zig:243-245). On top of #520 this change would give the 27B a second path through the 2B's row decoder.

Part of your result is still open on our side. The 27B in 1.0.3 serves one request at a time, and your run shows the lane rounds can carry eight. We've put that on the 27B engine's list with your name on it. If you'd like to try in-place state writes or concurrent streams on the 27B family, a PR against main is welcome, and reopen this if we've misread it.

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