↓ Skip to main content
  1. Archive/

Adding CUDA DFlash2 Support for Qwen4 in Strata on an RTX 4090 D

Study & Export
Table of Contents

I am not a native English speaker; this article was translated by AI.

Right after Qwen4-35B-A3B dropped, I spun it up with Strata on a single RTX 4090 D (24,564 MiB, Ada SM89) using UD-IQ3_XXS, with all 10,240 experts resident in VRAM and a 32K INT8 main KV cache. With external MTP speculative decoding (--spec 4 --spec-min-p 0.5), single-stream long outputs hit around 268 tok/s, but MTP alone took 1,957 MiB, pushing total VRAM usage to 19,952 MiB and leaving too little headroom for concurrency.

Shortly after, the community released DFlash2 drafter weights for Qwen4-35B-A3B (515 MB in Q8_0). Upstream Niko1221/Strata had not finished merging CUDA DFlash2 support for Qwen4 yet, so I had AI work directly on the engine codebase, benchmarked the concurrency ceiling, and submitted two upstream PRs (#7927 and #7929).

What Was Missing Upstream
#

Looking through the upstream repository, there were two separate unmerged open PRs: #7797 generalized the geometry and added serve startup checks, but already conflicted with main and lacked causal attention; #7810 added causal block attention on its own.

Both PRs missed a critical piece: CUDA Prefill block-input tap capture. The Verifier already captured features from mixed_ (the vector after HC mix and per-stream norm, before attention, mapped with id+1: [19,23,27,31,35] -> [20,24,28,32,36]), whereas CUDA Prefill’s run_impl and run_layer_major still captured the post-layer residual mean, and neither CLI nor serve forwarded --draft-tap block-input to Prefill.

On long prompts, Prefill and decode/verify captured features from different points in the block, causing the draft acceptance rate to drop sharply.

Engine Changes and Concurrency Fixes
#

Merging Geometry Generalization and Causal Attention
#

We first combined #7797 and #7810 onto a single branch, removing the Flash-Next hardcoded dimensions (N=2560, 32Q/8KV, conv 640) in dflash2.cpp and dflash2.hpp in favor of pack-driven validation to support Qwen4 (N=2048, 16Q/2KV, conv 512/256, scale stride 64). We also merged the CUDA scalar and sm_80+ BF16 Tensor Core attention (n_qgroups=NH/4=4, two groups sharing one KV head) with a key_row <= query_row causal mask inside the 8-token speculative block (STRATA_DF2_ATTN_MODE=causal).

Completing CUDA Prefill block-input Taps
#

We plumbed block_input through Prefill’s set_taps, reserve_taps, and generate.cpp (covering both single-GPU and multi-GPU stages), capturing m.mixed after the attention-half HC mix in run_impl and lm_run so it matches Verifier’s mixed_ and tap_plan.hpp. Measured max absolute error between Prefill and Verify taps was 0.00e+00.

During community review, we also added cross-backend compatibility: updating SYCL prefill.cpp’s reserve_taps signature, lifting the restriction that mistakenly gated non-final stage on_taps callbacks under the Loop adapter, and adding (void) causal in the stub.

Confidence Pruning and VRAM Footprint
#

Enabling --spec 8 on DFlash2 without confidence pruning lets the wider verify window eat into the gains from extra accepted tokens. Pairing it with --draft-conf 0.35 to prune low-confidence drafts brought verify overhead down significantly.

For VRAM, DFlash2 reuses the target GPU’s existing Q6_K NativeHead (STRATA_DF2_HEAD=native) and target embeddings instead of duplicating the full vocabulary head. The draft ring uses a fixed 2048 sliding window at 20.16 MiB per slot, so it does not grow linearly with the 262K logical context.

Fixing the Stream Race and Slot-Reuse Bleed
#

Stability testing surfaced two concurrency bugs. First, Dflash2Drafter::rebuild initialized rb_q_ and rb_d_ with legacy-stream cudaMemset, racing against the non-blocking stream cs_; switching to cudaMemsetAsync(..., cs_) resolved it.

Second, Dflash2Drafter::copy_state_from and Dflash2Drafter::reset missed calling clear_deferred(). When a slot was reused, deferred rows from the previous session could replay into the new one. We split this into a standalone bugfix PR (#7929), added the dflash2_clear_deferred_test unit test, and verified 0 errors under compute-sanitizer memcheck.

Same-Binary A/B Benchmarks and Third-Party Ablation
#

All comparisons used the same sm_89 Release binary with all experts resident in VRAM, taking the median of 3 runs per arm. Every scenario (EOS, Chinese, English, code/tool-calling, suffix toggle, 8K and 33K contexts) passed token-for-token greedy parity.

Scenario No Spec MTP DFlash2 Q8_0 vs MTP Free VRAM (DFlash2 vs MTP)
zh_long 214.82 tok/s 292.44 tok/s 319.62 tok/s +9.3% 4,887 vs 4,175 MiB
en_long 215.14 tok/s 291.40 tok/s 336.21 tok/s +15.4% 4,929 vs 4,225 MiB
code_tool 214.76 tok/s 313.48 tok/s 435.67 tok/s +39.0% 4,817 vs 4,165 MiB
ctx8k 204.51 tok/s 279.97 tok/s 308.69 tok/s +10.3% 5,519 vs 4,159 MiB
ctx33k 197.99 tok/s 275.16 tok/s 388.11 tok/s +41.0% 5,509 vs 4,159 MiB

Across the five workloads, code and 33K long-context saw the largest gains at roughly +40% over MTP, while freeing 650 MiB to 1.35 GiB of additional VRAM.

After PR #7927 was opened, community contributor zacharyblasczyk ran an independent verification on an RTX 5090 (sm_120, Q4_0 pack + draft_vocab_en.bin), also confirming bit-exact greedy parity. On UD-Q4_K_XL (2,611-token prompt), their ablation showed that removing the Prefill block-input tap dropped throughput to 468.25 tok/s—slower than MTP’s 482.85 tok/s—while enabling it jumped back to 570.52 tok/s (579.04 tok/s with the full setup). On code prompts, UD-IQ4_XS reached 649.53 tok/s (+26.3%) and UD-IQ2_XXS reached 880.33 tok/s (+29.2%).

Production Cutover and Concurrency Sweep
#

After cutting strata.service over to DFlash2 Q8_0 (--spec 6 --draft-conf 0.35), single-stream free VRAM increased by about 1.24 GiB over MTP, and end-to-end Chinese /v1/chat/completions measured 334.7 to 342.7 tok/s.

I then swept concurrency from N=1 to N=12 (roughly 811 MiB per slot):

Concurrency Aggregate Per-Stream TTFT (P50 / P95) Experts Resident Min Free VRAM
N=1 411.92 tok/s 411.92 tok/s 20.5 / 20.5 ms 100% 5,478 MiB
N=3 400.10 tok/s 157.21 tok/s 129.2 / 240.3 ms 100% 1,251 MiB
N=4 343.40 tok/s 94.57 tok/s - 96.4% -
N=8 402.05 tok/s 58.42 tok/s 631.3 / 1,390.2 ms 71.6% -
N=12 328.19 tok/s - - 46.8% 15 MiB

On 24 GB VRAM, 3-way concurrency is the zero-eviction sweet spot. At N=4, KV slots begin evicting expert weights and aggregate throughput dips first; pushing to N=8 reaches 402 tok/s aggregate (428.8 tok/s in wave 2), but TTFT climbs sharply; N=12 hits the hard VRAM limit.

Production now runs at parallel=3 (with the clear_deferred fix): 22,587 MiB idle, 23,531 MiB under full 3-way load (1,033 MiB headroom), producing 4,608 tokens in 11.95 seconds (385.67 tok/s aggregate) with zero cross-session bleed.

Watching 3-way concurrency hold steady at 385 tok/s, with both PRs getting third-party 5090 verification in the community—as someone who knows nothing about CUDA, pulling off this kind of low-level optimization with AI would have been unthinkable back in the hand-coded era.

Links #

Related