Skip to content

feat(qwen3_5 cuda): --checkpoint-slots sets the prompt states the 27B's concurrent decoder keeps - #125

Closed
nood-co1 wants to merge 1 commit into
ashhart:mainfrom
blockbrain-ai:feat/27b-checkpoint-slots
Closed

nood-co1 wants to merge 1 commit into
ashhart:mainfrom
blockbrain-ai:feat/27b-checkpoint-slots

Conversation

@nood-co1

Copy link
Copy Markdown
Contributor

Summary

With --parallel above one, Qwen3.8-27B's concurrent decoder keeps KEEP = 3 prompt states (Q/engine.py:10). When two conversations share the server, their calls add states in turn, and each conversation's prompt-end state is evicted before its next call. The next call then resumes from an earlier message start and prefills the rest again.

This PR lets --checkpoint-slots, the existing option for cached conversation prefixes, set that count for the 27B on CUDA. The default stays 3. The startup estimate already charges each kept state against the window (stream_geometry), so a larger count leaves a smaller window, and the startup line now names the count.

Why

Q/ is src/tensorfold/families/qwen3_5/cuda/. References are at main (9cd52ab, 0.5.0).

  • MultiDecoder keeps states in a PrefixCache(keep): a message start at least MIN_GAP past the resumed prefix, and the prompt end one token early (Q/multi.py:137-195). Past keep, the oldest state never resumed from leaves first (cuda/streams.py, PrefixCache.add).
  • A coding agent that runs a sub-agent sends two conversations whose calls alternate. Each call adds one or two states. With keep = 3, the main conversation's prompt end has left by the time its next call arrives. That call resumes from the last state it still shares: in our logs, a message start 6,024 tokens in, or the 5,470-token system block.
  • One stream is different: its states share one attention buffer, and a resume drops the extensions it would overwrite (Q/engine.py:_drop_extensions). There a larger count does not help, so this PR leaves one stream as it is.
  • feat(cuda): host-RAM tier for Qwen3.8-27B prompt states (--ram-tier-gib), 126k window on a 32 GB R9700 #106 keeps evicted states in host RAM. It refuses GPUs that share the host's memory, "where its prefix cache already lives" (a DGX Spark, for one). On those GPUs a larger cache is the equivalent.

We measured it on our own agent traffic, not a public benchmark. A DeepSeek Harness coding agent ran 3 sessions over three public repositories (attrs, itsdangerous, requests), each with a sub-agent. That gave 55 requests (52 main calls and 3 title calls) and 997,402 prompt tokens. We replayed that recorded log twice at its recorded arrival times, each body capped at its recorded reply length, against the same build on one DGX Spark with --parallel 2:

--checkpoint-slots Prefilled tokens TTFT median TTFT p90
3 (the default) 235,437 2.47 s 10.09 s
6 206,366 (−12.3%) 1.61 s 7.04 s

At 6 slots, 4 of the 52 main calls resumed from a longer state and none from a shorter one. The 6-slot arm ran second, so part of its TTFT gain may be warm-up. A replay model of the store, using 0.5.0's own PrefixCache, resume_points, MIN_GAP and entry_end, agrees in direction: 209,319 and 196,316 tokens at 3 and 6 slots. It matches the live cached on 47 and 51 of the 52 calls.

Each kept state costs a window of keys and values. On one GB10 at a 131,072-token window, two streams with 3 kept states are estimated at 74.87 GiB within 104.09 GiB, and each further state at about 8 GiB (16 attention layers × 131,072 × 4 heads × 256 × keys and values × 2 bytes). 6 fits there; 8 does not. So this is the operator's choice.

Change

  • Q/engine.py: Qwen27Engine(..., keep=None). None keeps KEEP (3). The count goes to stream_geometry, to the drafter's geometry (kept=keep + 1), and to MultiDecoder(keep=...). With --tp 2 it joins the settings both ranks must share. The startup line reads 2 streams of N prompt/reply tokens, K prompt states kept.
  • families/qwen3_5/__init__.py: CUDA_CHECKPOINT_SLOTS = True; cuda_engine passes checkpoint_slots as keep.
  • cli.py: _serve_cuda passes --checkpoint-slots to families that declare CUDA_CHECKPOINT_SLOTS. The help names the 27B's use.
  • serve_options.py: for the 27B on CUDA, --checkpoint-slots below 1 is refused before any download. So is --checkpoint-slots where the effective stream count is one: auto, and any number _parallel turns into 1, such as 1, 01, +1, 0 and -1. Other families and backends are unchanged. The MLX server keeps its own meaning, and other CUDA engines still do not read the option.

Tests

Host: macOS, Python 3.13, PyTorch 2.11.0 CPU.

  • tests/test_cuda_cli.py:
    • The option reaches a CUDA engine only when its family declares it.
    • It is refused before any download on one stream (no --parallel, and 1, 01, +1, 0, -1), and below 1.
    • It passes with --parallel 2, and on MLX.
    • cuda_engine hands it to Qwen27Engine as keep, and None without it.
  • tests/test_cuda_parallel_admission.py: two ranks started with different counts refuse to start (a new case of test_two_ranks_with_different_streams_or_context_refuse_to_start).
  • tests/cuda/test_qwen27_checkpoint_slots.py (GPU): two two-stream engines at 8,192 tokens, the default and keep=5. The decoder keeps 3, then 5. The startup line names the count, and the startup estimate is larger with 5.

Without the change (main's four source files), 10 of the 13 new host cases in tests/test_cuda_cli.py fail. The 3 that pass are the two allowed-combination cases and the case where a family does not declare the option. The new two-rank case fails without the count in the handshake.

Run Result
Whole host suite (tests without tests/cuda and tests/test_qwen4_exp_prefill.py, whose indexer-kernel tests abort the interpreter on this Mac on main too), main (9cd52ab) 15 failed, 3,662 passed, 350 skipped
Same, this branch (38e1891) 15 failed, 3,676 passed, 350 skipped

The same 15 ids fail on both, none in a file this PR changes. ruff check on the changed files finds only the two findings main already has in Q/engine.py (a lambda assignment and an unused result).

GPU status

One DGX Spark (GB10), the tensorfold-bench image (PyTorch 2.13.0a0, CUDA 13.3), TORCH_CUDA_ARCH_LIST=12.1, Qwen3.8-27B MLX 4-bit with the DFlash2 drafter, on 0.5.0. The GPU runs used d649f74. 38e1891 adds only the count to the two-rank handshake and its host case, which one-GPU runs do not execute.

  • tests/cuda/test_qwen27_checkpoint_slots.py: 1 passed (62 s).
  • Startup at --parallel 2 --context 131072:
    • 2 streams of 131072 prompt/reply tokens, 3 prompt states kept, estimate 74.87 GiB within 104.09 GiB;
    • 2 streams of 131072 prompt/reply tokens, 6 prompt states kept, estimate 99.41 GiB within 104.15 GiB.
    • No NV_ERR_NO_MEMORY or out-of-memory line in either arm.
  • Exactness at 6 slots, in a live run of the same agent: 9 drafted replies sent again with "draft": false all gave the same tokens. A tenth was stopped by our client's time bound before it ended.

Submitted by an agent from Blockbrain Labs (https://x.com/blockbrain_labs, https://github.com/blockbrain-ai).

…'s concurrent decoder keeps

With --parallel 2 or more, the concurrent decoder keeps KEEP = 3 prompt states. When a main agent and its
sub-agent share the server, their calls add states in turn, and each conversation's prompt end is evicted
before its next call; the next call then resumes from an earlier message start. --checkpoint-slots now sets
that count for Qwen3.8-27B on CUDA (default 3, unchanged). The startup estimate already holds each kept state
against the window (stream_geometry), so a larger count leaves a smaller window, and the startup line names it.
One stream keeps its 4 states in one attention buffer, so --checkpoint-slots without --parallel 2 is refused
before any download, as is a count below 1.
ashhart added a commit that referenced this pull request Sep 30, 2026
…t resends resume (#124), streamed tool calls (#114)

- CUDA prompts take bf16 activations by default; --prefill-fp8 restores FP8 prompts; both ranks agree on the precision;
  block-scaled FP8 checkpoints load (#126)
- Flash Next keeps each prompt's state one token early, so identical resends and next thinking turns resume (#124)
- Tool-call arguments stream as they are written on the CUDA server (#114)
- Before M5, Flash Next hyper-connection projections of 8+ rows run in 8-row tiles with the per-row bits
- --checkpoint-slots on CUDA (#125)
@ashhart

ashhart commented Sep 30, 2026

Copy link
Copy Markdown
Owner

This is a useful knob and a clean change. We measured it on a GB10 with the 27B and DFlash2, --parallel 2, replaying two alternating agent conversations, 12 calls and 163k prompt tokens: the default three kept states let each conversation resume only from its first turn, and --checkpoint-slots 6 let each resume from its latest, so 28% fewer prompt tokens were prefilled, 75,259 to 54,062, and the median time to first token fell from 3.91 s to 2.38 s, for 0.60 GiB more at startup, with every reply identical by token sha. On 0.6.0 one GPU's caches grow with use, so we updated the help and the GPU test's startup line, and the README names the CUDA meaning; without the flag it behaves exactly as next-0.6.0.

@ashhart ashhart closed this Sep 30, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…t resends resume (ashhart#124), streamed tool calls (#114)

- CUDA prompts take bf16 activations by default; --prefill-fp8 restores FP8 prompts; both ranks agree on the precision;
  block-scaled FP8 checkpoints load (ashhart#126)
- Flash Next keeps each prompt's state one token early, so identical resends and next thinking turns resume (ashhart#124)
- Tool-call arguments stream as they are written on the CUDA server (#114)
- Before M5, Flash Next hyper-connection projections of 8+ rows run in 8-row tiles with the per-row bits
- --checkpoint-slots on CUDA (ashhart#125)
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