perf(qwen35): one replay commit instead of three GDN state copies per DFlash step

Merged
GR
1mo ago
+3281-73

Summary

Speeds up single-GPU DFlash decode on Qwen3.5/3.6/3.8 by cutting the recurrent-state (GDN/SSM) traffic of each speculative step. Output stays byte-identical.

Each step today does three full SSM-state copies: the snapshot, the final-state write and the per-token F32 checkpoints in the verify graph, and the rollback copy. After this change it does one replay-log commit of the accepted prefix:

before                                   after                                       
snapshot_kv   conv copy + SSM copy       conv copy; SSM snapshot deferred (storage exchange)
verify_batch  writes final SSM state     leaves SSM at its pre-step value
              + q_len F32 checkpoints    records a replay log [g | k | delta] per token, head
rollback_to   copy intermediate[n-1]     one kernel applies the first n log entries
              (skipped if all accepted)  into the spare buffer, then exchanges live/spare

The change has three parts, all in qwen35/gdn_single_commit.h, and they switch on and off together:

  • KEEP_BASE. The chain capture verify drops ggml_cpy(new_state, ssm_state), and rollback commits the accepted state. With F32 checkpoints, intermediate[q_len-1] is bitwise the final state: the kernels store both from the same registers.
  • SWAP_SNAPSHOT. The SSM snapshot exchanges data, buffer and extra of the live and spare tensors instead of copying them. The tensor objects stay, so the prefix cache, the disk cache and kvflash are untouched. Reset and free put the pairs back first.
  • SINGLE_REPLAY. On the grouped-cols GDN kernel (S_v 128, at most 16 tokens), the verify runs with WRITE_INTER=false and records the replay log. A new ggml_backend_cuda_gdn_chain_commit replays the accepted prefix with the kernel's own fmaf(k, delta, g * s), so it reproduces intermediate[n-1] bit for bit. Other shapes keep the F32 checkpoints.
server/src/qwen35/
  gdn_single_commit.h            + parts, exclusions, StateBook, rollback plan (pure, unit-tested)
  qwen35_dflash_target.{h,cpp}   ~ snapshot_kv / verify_batch / rollback_to / restore_kv
  graph_builders.{h,cpp}         ~ keep_base_ssm_state, single_replay_slot_ids
  qwen35_target_graph.cpp        ~ skip final write; mapped verify with replay log; unswap on reset
server/deps/llama.cpp/ggml/
  include/ggml-cuda-gdn-chain.h  + chain-commit API
  src/ggml-cuda/gdn-chain-commit.cu + commit kernel
  src/ggml-cuda/gated_delta_net.cu  ~ route query (grouped-cols?); env overrides -> test hook

There is no runtime switch: the three parts are always on. Each keeps the old handling, per call, with kvflash, SpecLA, tensor-parallel or multi-slot caches, MoE, DDTree and F16 checkpoints. Tests turn parts off with set_parts_for_test.

The PR also removes the LUCE_GDN_FORCE_GROUPED_COLS / LUCE_GDN_NO_GROUPED_COLS env overrides. The GDN op always picks its kernel by device, as it did when they were unset: grouped-cols everywhere except Ampere. Tests pin a route with the test-only ggml_backend_cuda_set_gdn_route_override (test_gdn_replay_log and kernel_qualification_gdn moved to it).

Evidence

Throughput, R9700. Setup:

  • Qwen3.8-27B UD-IQ4_XS target, DFlash2 Q8_0 drafter, --profile qwen38-r9700, greedy, thinking on, max_tokens 2048, one request at a time.
  • 30 prompts, 6 each from gsm8k, math500, humaneval, mbpp and mt-bench.
  • Main (34b20876f) against this branch, interleaved ABBA twice: 8 server runs, each with a warm-up request. These runs predate the rebase onto f3cc3ec9c, whose three commits (#935–#937) change qwen4exp and DeepSeek 4.1 code.
  • Values are tokens/s per request, geometric mean per suite.
suitemainthis PR
gsm8k100.4107.7 (+7.2%)
math500104.9112.7 (+7.5%)
humaneval80.586.7 (+7.6%)
mbpp93.6100.7 (+7.5%)
mt-bench73.879.3 (+7.5%)
all89.8696.58 (+7.47%)
  • Noise: the four main runs span 89.86 to 89.87, and each block shows the same gain (+7.49% and +7.46%).
  • Identity: every run produced 26,327 tokens and 30/30 outputs identical to main by token ids. This holds in the shipped config, and also with the AR burst pinned off (main 89.85, this PR 96.47), identical. The build without the runtime switch, run the same way against main, also gave 30/30 identical (89.93 → 96.46).
  • After the rebase: this exact commit (59f1e27f0) against main f3cc3ec9c, AR burst pinned off, single sequential runs: 30/30 identical, 26,327 tokens each, 90.33 → 96.88 tok/s (+7.3%). Main itself got about 0.5% faster between 34b20876f and f3cc3ec9c.
  • Per part: an earlier interleaved run of the same change on c5179e4b5 put KEEP_BASE + SWAP_SNAPSHOT alone at +2.3%. SINGLE_REPLAY brings the rest.

Concurrency 4. Setup: paged attention, --max-concurrency 8, 4 clients in a closed loop, 40 prompts × 1024 tokens, ABBA twice.

  • Per-request rate goes from 55.48 to 55.66 (+0.32%), inside the ±1.4% run-to-run spread, with 0 errors. This is the expected result: the concurrent path does not use this code, because it already verifies through a mapped verify with a replay log.
  • Outputs at C > 1 are not compared byte for byte: the engine is not batch-invariant, so even two runs of main differ on most prompts.

Where the time goes. A rocprofv3 trace of 5 requests, steady rounds:

  • Device copy time per round falls from 2.51 to 1.28 ms.
  • The round falls from 65.1 to 60.1 ms.
  • The commit kernel bench (48 layers × 16 tokens, H 48, S_v 128) runs at 12.65 µs per layer. The 48 checkpoint copies of the old rollback cost 14.14 µs per layer.
  • The GDN op takes 57.1 µs per layer instead of 67.0, because it skips the checkpoint writes. The snapshot copy is gone too.

Correctness.

  • test_gdn_single_commit (CPU, 18 cases) covers the gating, the rollback and restore plans, and the storage exchange. It also has a toy model that replays the spec-decode protocol twice, once with the old semantics and once with the new ones, and requires the same committed state after every call.
  • test_gdn_single_commit_gpu --parts off|k|ks|kr|ksr checks two things:
    • the GDN final state equals the last F32 checkpoint byte for byte (grouped-cols and scalar kernels, 1 to 16 tokens);
    • Qwen35DFlashTarget over a synthetic one-layer model runs through every do_spec_decode call sequence: commits, emit-phase repairs, the slow path, AR seeds, a failed verify and a reset. Every part set reproduces the off run's digest (1488c652d1229ee8).
    • Each rollback runs the chain commit exactly when SINGLE_REPLAY applies, and never otherwise (ggml_backend_cuda_get_gdn_chain_commit_count).
  • test_gdn_single_replay_gpu: at the production shape, the commit equals the checkpoint byte for byte, both in place and out of place.
  • gdn_replay_log (3 tests) and kernel_qualification_gdn pass with their routes pinned by the new override.
  • test_server_unit: 1224 passed, 0 failed. It includes the graph tests for the skipped final write, the mapped verify and the storage exchange.

Merge Danger

Door: two-way, by revert. There is no runtime switch any more, so undoing the change means reverting the commit. No format or API change is visible outside qwen35 single-GPU decode. A deployment that set LUCE_GDN_FORCE_GROUPED_COLS or LUCE_GDN_NO_GROUPED_COLS now gets the device default.

Blast Radius: narrow. Only the Qwen3.5/3.6/3.8 single-GPU DFlash chain verify changes. Paged, concurrent, layer-split, DDTree, MoE, kvflash and SpecLA paths keep their current handling. The new commit kernel runs only on the grouped-cols route. A missed state transition would show up as different output, and the unit test replays the spec-decode protocol against the parent's semantics to guard against that.

🤖 Generated with Claude Code