<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Qwen4 on Code is cheap, let&#39;s talk</title>
    <link>https://blog.ferstar.org/en/tags/qwen4/</link>
    <description>Code is cheap, let&#39;s talk</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <copyright>© 2026 ferstar · [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en)</copyright>
    <lastBuildDate>Sun, 06 Dec 2026 01:20:00 +0800</lastBuildDate>
    <ttl>60</ttl><atom:link href="https://blog.ferstar.org/en/tags/qwen4/index.xml" rel="self" type="application/rss+xml" /><image>
      <url>https://blog.ferstar.org/site-logo.png</url>
      <title>Code is cheap, let&#39;s talk</title>
      <link>https://blog.ferstar.org/</link>
    </image>
    
    <item>
      <title>Adding CUDA DFlash2 Support for Qwen4 in Strata on an RTX 4090 D</title>
      <link>https://blog.ferstar.org/en/posts/cuda-dflash2-qwen4-strata/</link>
      <pubDate>Sun, 06 Dec 2026 01:20:00 +0800</pubDate>
      
      <guid isPermaLink="true">https://blog.ferstar.org/en/posts/cuda-dflash2-qwen4-strata/</guid>
      <description>Running Qwen4-35B-A3B on a single RTX 4090 D with external MTP speculative decoding consumed nearly 2 GB of VRAM and limited concurrency. By adding CUDA DFlash2, Prefill block-input taps, and concurrency fixes via PRs #7927 and #7929 in Strata, code throughput reached 435.67 tok/s, freeing 1.24 GiB of VRAM for stable 3-way concurrency.</description><content:encoded><![CDATA[<blockquote><p>I am not a native English speaker; this article was translated by AI.</p>
</blockquote><p>Right after Qwen4-35B-A3B dropped, I spun it up with Strata on a single RTX 4090 D (24,564 MiB, Ada SM89) using <code>UD-IQ3_XXS</code>, with all 10,240 experts resident in VRAM and a 32K INT8 main KV cache. With external MTP speculative decoding (<code>--spec 4 --spec-min-p 0.5</code>), 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.</p>
<p>Shortly after, the community released DFlash2 drafter weights for Qwen4-35B-A3B (515 MB in Q8_0). Upstream <code>Niko1221/Strata</code> 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).</p>

<h2 class="relative group">What Was Missing Upstream
    <div id="what-was-missing-upstream" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#what-was-missing-upstream" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Looking through the upstream repository, there were two separate unmerged open PRs: <code>#7797</code> generalized the geometry and added serve startup checks, but already conflicted with <code>main</code> and lacked causal attention; <code>#7810</code> added causal block attention on its own.</p>
<p>Both PRs missed a critical piece: CUDA Prefill <code>block-input</code> tap capture. The Verifier already captured features from <code>mixed_</code> (the vector after HC mix and per-stream norm, before attention, mapped with <code>id+1</code>: <code>[19,23,27,31,35] -> [20,24,28,32,36]</code>), whereas CUDA Prefill’s <code>run_impl</code> and <code>run_layer_major</code> still captured the post-layer residual mean, and neither CLI nor serve forwarded <code>--draft-tap block-input</code> to Prefill.</p>
<p>On long prompts, Prefill and decode/verify captured features from different points in the block, causing the draft acceptance rate to drop sharply.</p>

<h2 class="relative group">Engine Changes and Concurrency Fixes
    <div id="engine-changes-and-concurrency-fixes" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#engine-changes-and-concurrency-fixes" aria-label="Anchor">#</a>
    </span>
    
</h2>

<h3 class="relative group">Merging Geometry Generalization and Causal Attention
    <div id="merging-geometry-generalization-and-causal-attention" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#merging-geometry-generalization-and-causal-attention" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>We first combined <code>#7797</code> and <code>#7810</code> onto a single branch, removing the Flash-Next hardcoded dimensions (<code>N=2560</code>, <code>32Q/8KV</code>, <code>conv 640</code>) in <code>dflash2.cpp</code> and <code>dflash2.hpp</code> in favor of pack-driven validation to support Qwen4 (<code>N=2048</code>, <code>16Q/2KV</code>, <code>conv 512/256</code>, <code>scale stride 64</code>). We also merged the CUDA scalar and sm_80+ BF16 Tensor Core attention (<code>n_qgroups=NH/4=4</code>, two groups sharing one KV head) with a <code>key_row <= query_row</code> causal mask inside the 8-token speculative block (<code>STRATA_DF2_ATTN_MODE=causal</code>).</p>

<h3 class="relative group">Completing CUDA Prefill <code>block-input</code> Taps
    <div id="completing-cuda-prefill-block-input-taps" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#completing-cuda-prefill-block-input-taps" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>We plumbed <code>block_input</code> through Prefill’s <code>set_taps</code>, <code>reserve_taps</code>, and <code>generate.cpp</code> (covering both single-GPU and multi-GPU stages), capturing <code>m.mixed</code> after the attention-half HC mix in <code>run_impl</code> and <code>lm_run</code> so it matches Verifier’s <code>mixed_</code> and <code>tap_plan.hpp</code>. Measured max absolute error between Prefill and Verify taps was <code>0.00e+00</code>.</p>
<p>During community review, we also added cross-backend compatibility: updating SYCL <code>prefill.cpp</code>’s <code>reserve_taps</code> signature, lifting the restriction that mistakenly gated non-final stage <code>on_taps</code> callbacks under the Loop adapter, and adding <code>(void) causal</code> in the stub.</p>

<h3 class="relative group">Confidence Pruning and VRAM Footprint
    <div id="confidence-pruning-and-vram-footprint" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#confidence-pruning-and-vram-footprint" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Enabling <code>--spec 8</code> on DFlash2 without confidence pruning lets the wider verify window eat into the gains from extra accepted tokens. Pairing it with <code>--draft-conf 0.35</code> to prune low-confidence drafts brought verify overhead down significantly.</p>
<p>For VRAM, DFlash2 reuses the target GPU’s existing Q6_K NativeHead (<code>STRATA_DF2_HEAD=native</code>) 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.</p>

<h3 class="relative group">Fixing the Stream Race and Slot-Reuse Bleed
    <div id="fixing-the-stream-race-and-slot-reuse-bleed" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#fixing-the-stream-race-and-slot-reuse-bleed" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>Stability testing surfaced two concurrency bugs. First, <code>Dflash2Drafter::rebuild</code> initialized <code>rb_q_</code> and <code>rb_d_</code> with legacy-stream <code>cudaMemset</code>, racing against the non-blocking stream <code>cs_</code>; switching to <code>cudaMemsetAsync(..., cs_)</code> resolved it.</p>
<p>Second, <code>Dflash2Drafter::copy_state_from</code> and <code>Dflash2Drafter::reset</code> missed calling <code>clear_deferred()</code>. 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 (<code>#7929</code>), added the <code>dflash2_clear_deferred_test</code> unit test, and verified 0 errors under <code>compute-sanitizer memcheck</code>.</p>

<h2 class="relative group">Same-Binary A/B Benchmarks and Third-Party Ablation
    <div id="same-binary-ab-benchmarks-and-third-party-ablation" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#same-binary-ab-benchmarks-and-third-party-ablation" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>All comparisons used the same <code>sm_89</code> 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.</p>
<table>
  <thead>
      <tr>
          <th style="text-align: left">Scenario</th>
          <th style="text-align: center">No Spec</th>
          <th style="text-align: center">MTP</th>
          <th style="text-align: center">DFlash2 Q8_0</th>
          <th style="text-align: center">vs MTP</th>
          <th style="text-align: center">Free VRAM (DFlash2 vs MTP)</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td style="text-align: left"><code>zh_long</code></td>
          <td style="text-align: center">214.82 tok/s</td>
          <td style="text-align: center">292.44 tok/s</td>
          <td style="text-align: center">319.62 tok/s</td>
          <td style="text-align: center">+9.3%</td>
          <td style="text-align: center">4,887 vs 4,175 MiB</td>
      </tr>
      <tr>
          <td style="text-align: left"><code>en_long</code></td>
          <td style="text-align: center">215.14 tok/s</td>
          <td style="text-align: center">291.40 tok/s</td>
          <td style="text-align: center">336.21 tok/s</td>
          <td style="text-align: center">+15.4%</td>
          <td style="text-align: center">4,929 vs 4,225 MiB</td>
      </tr>
      <tr>
          <td style="text-align: left"><code>code_tool</code></td>
          <td style="text-align: center">214.76 tok/s</td>
          <td style="text-align: center">313.48 tok/s</td>
          <td style="text-align: center">435.67 tok/s</td>
          <td style="text-align: center">+39.0%</td>
          <td style="text-align: center">4,817 vs 4,165 MiB</td>
      </tr>
      <tr>
          <td style="text-align: left"><code>ctx8k</code></td>
          <td style="text-align: center">204.51 tok/s</td>
          <td style="text-align: center">279.97 tok/s</td>
          <td style="text-align: center">308.69 tok/s</td>
          <td style="text-align: center">+10.3%</td>
          <td style="text-align: center">5,519 vs 4,159 MiB</td>
      </tr>
      <tr>
          <td style="text-align: left"><code>ctx33k</code></td>
          <td style="text-align: center">197.99 tok/s</td>
          <td style="text-align: center">275.16 tok/s</td>
          <td style="text-align: center">388.11 tok/s</td>
          <td style="text-align: center">+41.0%</td>
          <td style="text-align: center">5,509 vs 4,159 MiB</td>
      </tr>
  </tbody>
</table>
<p>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.</p>
<p>After PR <code>#7927</code> was opened, community contributor <code>zacharyblasczyk</code> ran an independent verification on an RTX 5090 (<code>sm_120</code>, Q4_0 pack + <code>draft_vocab_en.bin</code>), also confirming bit-exact greedy parity. On <code>UD-Q4_K_XL</code> (2,611-token prompt), their ablation showed that removing the Prefill <code>block-input</code> 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, <code>UD-IQ4_XS</code> reached 649.53 tok/s (+26.3%) and <code>UD-IQ2_XXS</code> reached 880.33 tok/s (+29.2%).</p>

<h2 class="relative group">Production Cutover and Concurrency Sweep
    <div id="production-cutover-and-concurrency-sweep" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#production-cutover-and-concurrency-sweep" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>After cutting <code>strata.service</code> over to DFlash2 Q8_0 (<code>--spec 6 --draft-conf 0.35</code>), single-stream free VRAM increased by about 1.24 GiB over MTP, and end-to-end Chinese <code>/v1/chat/completions</code> measured 334.7 to 342.7 tok/s.</p>
<p>I then swept concurrency from <code>N=1</code> to <code>N=12</code> (roughly 811 MiB per slot):</p>
<table>
  <thead>
      <tr>
          <th style="text-align: center">Concurrency</th>
          <th style="text-align: center">Aggregate</th>
          <th style="text-align: center">Per-Stream</th>
          <th style="text-align: center">TTFT (P50 / P95)</th>
          <th style="text-align: center">Experts Resident</th>
          <th style="text-align: center">Min Free VRAM</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td style="text-align: center">N=1</td>
          <td style="text-align: center">411.92 tok/s</td>
          <td style="text-align: center">411.92 tok/s</td>
          <td style="text-align: center">20.5 / 20.5 ms</td>
          <td style="text-align: center">100%</td>
          <td style="text-align: center">5,478 MiB</td>
      </tr>
      <tr>
          <td style="text-align: center">N=3</td>
          <td style="text-align: center">400.10 tok/s</td>
          <td style="text-align: center">157.21 tok/s</td>
          <td style="text-align: center">129.2 / 240.3 ms</td>
          <td style="text-align: center">100%</td>
          <td style="text-align: center">1,251 MiB</td>
      </tr>
      <tr>
          <td style="text-align: center">N=4</td>
          <td style="text-align: center">343.40 tok/s</td>
          <td style="text-align: center">94.57 tok/s</td>
          <td style="text-align: center">-</td>
          <td style="text-align: center">96.4%</td>
          <td style="text-align: center">-</td>
      </tr>
      <tr>
          <td style="text-align: center">N=8</td>
          <td style="text-align: center">402.05 tok/s</td>
          <td style="text-align: center">58.42 tok/s</td>
          <td style="text-align: center">631.3 / 1,390.2 ms</td>
          <td style="text-align: center">71.6%</td>
          <td style="text-align: center">-</td>
      </tr>
      <tr>
          <td style="text-align: center">N=12</td>
          <td style="text-align: center">328.19 tok/s</td>
          <td style="text-align: center">-</td>
          <td style="text-align: center">-</td>
          <td style="text-align: center">46.8%</td>
          <td style="text-align: center">15 MiB</td>
      </tr>
  </tbody>
</table>
<p>On 24 GB VRAM, 3-way concurrency is the zero-eviction sweet spot. At <code>N=4</code>, KV slots begin evicting expert weights and aggregate throughput dips first; pushing to <code>N=8</code> reaches 402 tok/s aggregate (428.8 tok/s in wave 2), but TTFT climbs sharply; <code>N=12</code> hits the hard VRAM limit.</p>
<p>Production now runs at <code>parallel=3</code> (with the <code>clear_deferred</code> 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.</p>
<p>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.</p>

<h2 class="relative group">Links
    <div id="links" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#links" aria-label="Anchor">#</a>
    </span>
    
</h2>
<ul>
<li>Upstream repo: <a href="https://github.com/Niko1221/Strata"  target="_blank" rel="noreferrer">Niko1221/Strata</a></li>
<li>Main PR (Qwen4 CUDA DFlash2 + causal attention + Prefill block-input tap): <a href="https://github.com/Niko1221/Strata/pull/7927"  target="_blank" rel="noreferrer">PR #7927</a></li>
<li>Community RTX 5090 verification and ablation: <a href="https://github.com/Niko1221/Strata/pull/7927#issuecomment-7438695638"  target="_blank" rel="noreferrer">PR #7927 Comment</a></li>
<li>Slot-reuse bleed bugfix PR: <a href="https://github.com/Niko1221/Strata/pull/7929"  target="_blank" rel="noreferrer">PR #7929</a></li>
<li>Drafter weights: <a href="https://huggingface.co/RadixArk/Qwen4-35B-A3B-Dflash2"  target="_blank" rel="noreferrer">RadixArk/Qwen4-35B-A3B-Dflash2</a></li>
</ul>
]]></content:encoded>
      
    </item>
    
  </channel>
</rss>
