How to run Qwen4 at nearly 300 tokens/s on an RTX 3090
🌐 Languages: 中文 · English
Cover: English-code decode throughput after each step, at short and at long context
One 24 GB RTX 3090 running Qwen4-35B-A3B with a 256K context, decoding at 288–319 tokens/s. That is the result @superalesha reported; llama.cpp on the same model of card only reaches 110–134 tokens/s.
I rented a 3090 and reproduced it under the same conditions: at short context Strata is 2.3x llama.cpp, and on a long-context task the improvement widens to 4.9x.
Figure 1: Decode throughput on English code, short context vs. long context
The improvement comes mainly from four things:
Speculative decoding. In the original author’s data this contributes 1.6–1.8x; I measured 1.0–1.5x, depending on the draft acceptance rate. The MTP head drafts a few tokens per round, the main model verifies them in one forward pass, and each round emits 1.3–2.4 tokens on average. Expert weights selected by several tokens are read once, the MTP experts are quantized to Q4_0, and the output vocabulary is trimmed to 40.5K English tokens.
Optimizing for the matrix shape. llama.cpp is a general compute graph; Strata is optimized for each specific matrix shape, for example by unrolling loops to a fixed trip count. This is the same idea as my earlier work on MindSpore AKG.
Removing kernel scheduling overhead. Each token actually reads 2.61 GB of weights, so the 3090’s bandwidth ceiling is about 327 tokens/s, which leaves only 76 μs per layer. llama.cpp spends 212 μs per layer because each token launches about 2,400 small kernels (about 61 per layer), and the time goes into launching and scheduling them. After fusion, Strata launches 944.
Keeping the n-gram table in host RAM, not in VRAM. If this 11 GB table went into VRAM it would take the space of the expert weights. In host RAM, there must be enough memory for it to stay resident: on a host with 31 GiB of RAM, llama.cpp reading it through a file mapping does only 110–118 tokens/s, and 134 tokens/s once the whole table is loaded into memory.
Looks simple, doesn’t it? These are all optimization methods I covered in my open-source book Understanding AI Infra.
Compute the physical ceiling first: 327 tokens/s
The basic method of Understanding AI Infra is to compute, from first principles, the ceiling the hardware allows, and then analyze the gap between the measurement and the ceiling. The gap comes either from a term the estimate left out or from system overhead that can be removed.
In decode at batch size 1, every generated token reads all the weights used in that step once, so the throughput ceiling is the memory bandwidth divided by the bytes read per token.
The common rough estimate is “3B active parameters at 4 bits is about 1.6 GB”, which gives about 580 tokens/s. I used this estimate at first too. It leaves out two things: the dense part is quantized at a higher bit width than the experts, and the output vocabulary head is read in full every step. Counting tensor by tensor from the GGUF header, the actual read is 2.61 GB, and the routed experts are only about a fifth of it.
Figure 2: What each generated token reads
For bandwidth I use the value measured on the same card, 853 GB/s (summing 8 GiB). So:
853 GB/s ÷ 2.61 GB ≈ 327 tokens/s, i.e. 3.06 ms per token and 76 μs per layer.
Two conclusions follow. First, llama.cpp reaches only 36% of the ceiling and Strata without MTP reaches 55%, so neither is limited by bandwidth; the gap is system overhead. Second, the original author’s 319.6 tokens/s is already close to the single-token bandwidth ceiling. Optimizing the single-token path alone cannot get there; one read of the weights has to produce more than one token.
Speculative decoding: one read of the weights, several tokens out
Qwen4 ships with a one-layer MTP draft head. Each round it proposes a few following tokens, the main model verifies all the candidates in one forward pass, and the prefix that matches its own output is accepted.
Following the accounting in Section 8.5.2 of the book, the average time per token is the total time divided by the total number of tokens emitted, so we only need the time per round and the tokens per round. Without MTP a round takes 5.6 ms and emits 1 token; with MTP a round takes 7.4–8.7 ms. Speculative decoding pays off when the tokens per round grow by more than the time per round.
Figure 3: Tokens per round and the break-even point of speculative decoding
Decode throughput on four kinds of text is shown below. Without MTP, each engine’s throughput does not depend on the kind of text; with MTP, the gain follows the acceptance rate.
Figure 4: Decode throughput on four kinds of text
A wider verification window costs little, for the same reason as the batching analysis in Section 8.2.1 of the book: the weights are read once and shared by several positions. Strata’s captured execution graphs show this directly: the graph that verifies 1 token has 944 kernels, and the graph that verifies 4 tokens has 959.
The draft vocabulary is trimmed to 40.5K English tokens, which lowers the cost of drafting at the price of a lower acceptance rate on non-English text. On Chinese prose the acceptance rate is 28% and each round emits 1.31 tokens, which is exactly the break-even point.
llama.cpp supports this MTP head as well. On the same card it raises English code from 118 to 157 tokens/s and does not help prose; and on 24 GB of VRAM it cannot fit a 256K context together with MTP, so the context has to drop to 192K.
Specializing for the tensor shape
llama.cpp uses a general compute graph, where operator shapes and loop bounds are determined at run time. Strata fixes the model shape at compile time: the build must specify -DSTRATA_GEOMETRY=q4_lite, or the engine refuses to load the model.
With the shape fixed, the loops inside a kernel can be unrolled by a constant. With a hidden size of 2048 and a thread block of 256 threads, exactly 4 iterations read one row of weights; once the loop is unrolled to a fixed 4 iterations, the memory requests of all 4 can be issued together instead of waiting for each in turn. The main work of the qwen4-opt branch is porting Strata’s specialized paths, which previously applied only to the previous-generation model (top-10 of 512 experts, hidden size 2560), to Qwen4’s shape (top-8 of 256, hidden size 2048).
This has the same starting point as the compiler methods discussed in Section 5.4 of the book and as AKG: when the shapes are known, tiling, fusion and loop transformations can be decided ahead of time. The difference is that AKG does it automatically on the polyhedral model, while Strata writes each kernel by hand. The price is portability: when a new model is released, the existing specialized paths cannot be reused.
Kernel count: 2,400 per token versus 944
The bandwidth floor is 3.06 ms per token; llama.cpp actually takes 8.47 ms and Strata 5.61 ms. The hypothesis is that the extra time goes into launching and scheduling a large number of small kernels. Two experiments test it.
Experiment 1: how many kernels each token launches. Counted by interposing cudaLaunchKernel with LD_PRELOAD.
Figure 5: Time per token and kernels per token
Experiment 2: the minimum time of a single kernel.
Figure 6: The minimum time of a single kernel
When a kernel’s work is as small as 1 MiB, its time is dominated by launch, thread-block scheduling and the latency of the first memory accesses, and the bandwidth cannot be fully used.
Putting the two experiments together: taking 0.94 μs, the time of an empty kernel under graph replay, as the floor, llama.cpp’s roughly 2,400 kernels cost at least 2.3 ms and Strata’s 944 cost at least 0.9 ms, a difference of 1.4 ms. The measured difference between the two is 2.9 ms per token. So the kernel count alone, by the most conservative floor, explains about half of the gap; the rest comes from small kernels using the bandwidth poorly and from the memory-access arrangement in the previous section.
The contribution of the qwen4-opt branch can be quantified the same way: the kernel count goes from 1,175 to 944, and the time per token from 6.13 ms to 5.61 ms. The changes merge the router’s top-8 selection and the expert-cache address planning into one kernel, merge the router scoring and the shared-expert gate into one kernel, and have the router compute the exponential only for the 8 selected logits.
Two techniques need to be told apart. Section 5.5.2 of the book says: reducing the number of kernels requires fusion, not graph replay. llama.cpp and Strata both use CUDA Graphs. With CUDA Graphs disabled, llama.cpp drops from 118 to 90 tokens/s, which shows that graph replay removes the cost of the host submitting kernels one by one, but the roughly 2,400 kernels in the graph still have to run one after another.
Where to put the n-gram embedding table
Besides the 35B trunk, Qwen4 has an n-gram embedding table with 20B parameters, about 11 GB after quantization, and each token looks up only a few rows of it.
In 24 GB of VRAM, all the routed experts take 16.3 GiB, the dense weights, vocabulary head and MTP head take a few GiB, and the rest is for KV. If the whole table went into VRAM, most experts could not stay resident; an existing reproduction reports that decode throughput drops by 31% when the expert cache is reduced to 4,096 entries.
This matches the analysis of Engram table placement in Section 6.7.4 of the book: a large table that is read-only, has only a few rows looked up per step, and whose lookup positions depend only on the token sequence should not occupy the scarcest tier of storage.
Long context: the gap widens to 3.3–4.9x
The original report and earlier reproductions measured only short prompts. Here the model first reads about 25K, 110K and 200K tokens of background text and then does the same English coding task:
Figure 7: Decode throughput vs. context length
From short context to 200K tokens, llama.cpp drops by 64%, Strata without MTP by 23%, and Strata with MTP by 25%; the MTP draft acceptance rate stays at 61%–69% at every length. A long-document summarization task gives the same conclusion: llama.cpp does 43 tokens/s, and Strata does 156 (without MTP) and 216 (with MTP). For workloads that really use a long context, this gap matters more than the short-prompt numbers. This article does not analyze why llama.cpp slows down.
In the other direction, llama.cpp’s prefill is 15%–25% faster than Strata’s at every length (2,873 versus 2,486 tokens/s at about 3K tokens).
Experimental conditions
One RTX 3090 (power cap 270 W), EPYC 7742, CUDA 12.8; llama.cpp b13002, Strata v0.2.27, branch qwen4-opt@a8f4598; the model is UD-IQ4_XS from unsloth/Qwen4-35B-A3B-GGUF. All three engines use the same client and the same set of prompts, greedy decoding, 512 generated tokens per run, and the median of 3 runs.
The Strata maintainers record 263.7–265.1 tokens/s on a 3090 in a source comment, and another independent reproduction reports 225–249 tokens/s; the results here are in the same range.
My card was capped at 270 W (the original author’s at 300 W), so the absolute numbers I measured are a little lower than the original author’s, but the conclusion is the same.
Thanks to @superalesha, author of the qwen4-opt branch, to the author of the Qwen4 port (PR #7774), and to the maintainers of Strata and llama.cpp.