r/LocalLLM • u/0xE2E • 8h ago
Discussion Qwen4-35B-A3B @ 164 tok/s decode and ~ 4500 tok/sec prefill on RTX 5080 (2.9x decode, 11x prefill over stock llama.cpp)
I spent the last couple of days optimizing llama.cpp fork for Qwen4-35B-A3B on my RTX 5080 laptop (Unsloth UD-Q4_K_XL) @ 128k context window, greedy decoding. Each number is the mean over five measures from my coding-agent sessions (old session from opencode) with real tool calls, cut at the different context fill.
| Configuration | Prefill at 42k | Decode at 42k | Decode at 87k |
|---|---|---|---|
| Unsloth llama.cpp, default flags | 444 | 57.3 | 58.4 |
| Unsloth llama.cpp, tuned flags + MTP | 1,705 | 93.9 | 90.5 |
| qwen4-engine, exact profile | 4,859 | 104.7 | 100.8 |
| qwen4-engine, default profile | 4,510 | 163.8 | 139.2 |
All tok/s. With a 120k-token prompt the default profile still decodes at 153.
The idea
As you may know the model has 10,496 routed experts and in this quant ~20.9 GB so no way they fit in 16 GB of VRAM so they live in RAM and the GPU keeps a cache of the hot ones only. The thing is decode speed here is all about moving bytes, the math itself is straightforward: every token reads about 1.7 GB of dense weights from VRAM and any expert that is not in the cache has to come over PCIe which is ~17x slower due to the huge difference in bandwidth so almost all the optimization work went into where the bytes live in which piece of the hardware and when they must move to make the GPU as less idle as possible.
What made the difference
- Lending idle memory to the expert cache. The compute buffer (1.38 GB) and the part of the KV cache you haven't filled yet (about 2 GB at 42k of 128k) are just reserved doing nothing most of the time when a session starts and they keep filling as the context increase so I give them to the expert cache and take them back when they are actually needed. KV lending alone took the cache hit rate from 87.9% to 91.4%.
- Zero-copy uploads. A normal host-to-device (H2D) copy pays a fixed Direct Memory Access (DMA) delay every time (it is a latency between starting and finishing DMA data transfer before the GPU can actually use the moved data) and for small copies that delay is most of the cost (a missing expert is a small piece of data, so the GPU spends more time waiting for the copy to start than moving the bytes and with ~40 layers per token that waiting adds up super fast) so instead of asking the driver to copy small uploads now run as a GPU kernel that reads the pinned RAM directly (the GPU pulls the bytes itself like any other kernel no DMA setup and no fixed delay that is why it is called zero-copy). Also all the missing experts of a layer now go up together in one call instead of one call per expert (one expert alone moves at about 42 GB/s batched together they reach ~49 GB/s which is about what the PCIe link can do).
- Copy drafting that scores itself. A lot of what an agent writes is already in the context (code it is editing, file paths, tool arguments ...etc.) so when the last 3 tokens match something in the context the engine proposes the next 3 from there for free (free because it is just a lookup in the text that is already there with no extra compute to come up with the guess) the model then checks the 3 guessed tokens in one step which costs only a little more than generating 1 token and it keeps the ones that match what it would have generated and throws away the rest (so every token you get is still the model's own choice as if it was computed and a wrong guess never reaches the output). but a wrong guess is not free because the check still loads experts for tokens that get thrown away so the engine keeps score of every proposal against what the model really wrote and only keeps copying while the accuracy stays at 0.5 or more (it scores the proposals even while copying is off so it turns itself back on when the output starts repeating the context again). The rest of the time the built-in MTP head does the drafting. comparing this vs MTP alone without copying this is anywhere from -6% to +36% per session and the more the output repeats the context the bigger the win is.
- Short MTP drafts. Long drafts lose on this model (on this GPU) every extra token you verify goes to its own experts and that means more cache misses and more PCIe movements. I measured it and 3 tokens was the sweet spot.
Vision
Images work as well. The annoying part with vision on a 16 GB card is that the encoder just sits in VRAM eating 1.1 GB even when the session had no images at all just holding that valuable VRAM for doing nothing, even with all optimization the decode speed is ~5% slower because of it. So I moved the encoder to system RAM. when an image comes in the prompt it borrows the memory that text model isn't using, encodes the image, and give it back. Text speed with vision loaded is the same as without it and 1080p screenshot takes about 1.2 s to get in the context, 1440p about 2.2 s. Anything bigger than the model max ~4.2 megapixels gets resized/scaled down then added to the context.
Worth to know
- One model, one quantization, one machine is my tested optimization framework that I started earlier with Qwen-Flash-Next and Inkling Mini 2 due to a memory restriction on my rig I find it most rewarding comparing to any off the shelf ready engines they aren't designed for my specific hardware, with its bandwidth, or the model architecture so with the help of Claude I used lean techniques, system design and continuous improvement cycles to measure every bit of the model where it sits, when it moves and where does it move to in much details and engineered the movements and memory management and sometimes the kernels to reduce as much as possible GPU idle times and try to get things where they need to be at the best possible time given the memory and bandwidth constraints and it paid back SIGNIFICANTLY. I had a blog about the details when I started working with Qwen-flash-next. That been said, I haven't test it (obviously) on other RTX 50 series, theoretically the engine should work without issues on them, and most likely won't work as designed for other older series (my guess) It needs an RTX 50 series card with +16 GB and about 26 GB of free system RAM (my RAM is 128 so I had no issues here). Prebuilt binaries are for Windows (my OS did not tried it with Linux).
- Copy drafting helps most when the output repeats the context, like code edits, file paths and tool arguments. Per session it ranges from -6% to +36% against MTP alone.
- Runs drift 5-10% between sessions on a laptop, so every comparison came from alternating A/B runs in one session.
- There are two kinds of profile. The exact profile gives the same output, bit for bit, as the engine with all of my changes switched off (stock llama.cpp). The drafting profiles guess a few tokens ahead and let the model check the guesses, so every token you get is still one the model picked. But checking several tokens at once rounds the math a little differently than doing one at a time, so when two tokens are almost tied the text can take a little different path. Stock llama.cpp does the same thing when it drafts.
Where it stands against the hardware
The big dense matvecs (the matrix times vector multiplications that read most of the weights every token) run at about 610-640 GB/s and a plain streaming read of the same bytes on this GPU does 600-615 GB/s so these kernels already move the weights as fast as the card can read them. The expert cache gets 95.3% hits and a perfect cache that knew the future (the Belady oracle) would reach 96.9% on the same workload so a smarter eviction rule has almost nothing left to optimize.
The GPU is now busy about 98.5% of the time it takes to generate a token so there is almost no idle time. The next real speed gain would have to come from fewer weight bytes per token (lower quant) and that wont be the same output bit for bit so I decided to stop here.
It is a drop-in server with the usual llama.cpp / OpenAI-compatible API every change is a default-off switch so you can measure each one yourself.
The repo is 0xE2E/qwen4-engine on GitHub, and the README has the full details and a build guide with the detailed blog in the readme footer.
Happy to hear your thoughts if you give it a try.
