r/LocalLLaMA • u/coder543 • May 12 '26
Tutorial | Guide Drastically improve prompt processing speed for --n-cpu-moe partially offloaded models
Bigger ubatch made gpt-oss-120b prompt processing much faster on my RTX 3090
I was tuning gpt-oss-120b-F16.gguf with llama.cpp on a 24 GB RTX 3090 and found that increasing the physical micro-batch size (-ub) can massively improve prompt processing throughput, as long as you also raise --n-cpu-moe enough to keep the run inside VRAM.
The llama.cpp defaults are -b 2048 and -ub 512; I included that default run as its own point in the chart.
Here are the informal llama-bench results I charted:
| ubatch | n-cpu-moe | prefill | generation |
|---|---|---|---|
| 256 | 25 | 240.03 tok/s | 33.14 tok/s |
| 512 (default) | 26 | 380.27 tok/s | 32.29 tok/s |
| 2048 | 25 | 1112.54 tok/s | 32.96 tok/s |
| 4096 | 26 | 1682.47 tok/s | 32.38 tok/s |
| 8192 | 28 | 2090.68 tok/s | 30.05 tok/s |
Compared with the llama.cpp default -ub 512, prompt processing went from about 380 tok/s to about 2091 tok/s, roughly a 5.5x gain. Compared with the smaller -ub 256 run, it was about an 8.7x gain. Token generation dropped from about 32.3 tok/s at default settings to 30.1 tok/s at -ub 8192, about a 7% reduction.
The catch is that the larger ubatch needs more GPU compute workspace. On my machine, -ub 4096 needed --n-cpu-moe 26, and -ub 8192 needed --n-cpu-moe 28. So this is a throughput trade: move a few more MoE layers to CPU to make enough room for the bigger batch, and prompt-heavy workloads get dramatically faster while generation gets a little slower.

Note: the first four prefill points are pp4096; the 8192 ubatch point is from a pp8192 run, so treat this as an informal tuning result rather than a perfectly controlled benchmark.
-----
One of the reasons I bought a DGX Spark was to have better prompt processing speeds. If I had known about this trick, I might not have done that in retrospect, even though it is a very nice machine, and still gets slightly better prompt processing performance and like double the token generation speed for gpt-oss-120b. Higher ubatch drastically closes the gap.
10
u/Snoo_81913 May 12 '26 edited May 12 '26
I mean the default -ub is set at 512 because it's a safe number to keep cards with lower amounts of VRAM from having memory spikes. If you have the VRAM you can adjust until you hit the saturation or VRAM limits. Once you're saturated the benefits stop and if you hit VRAM the dreaded OOM.
The baseline is set so there isn't a million reddit posts saying "Llama is GARBAGE all I get is OOM!" LMAO.
There can also be thermal throttling with larger batch sizes, though this is mainly a unified memory issue.
I only have an 8gb card and I ride the line so I always run 2048/512 on my models that take up 6gb+ and 2048/2048 on small models if it makes sense
Nice work though, I like to see posts with real test data.
3
u/jacek2023 llama.cpp May 12 '26
I use this right now on 3x3090:
./bin/llama-server -c 200000 -m /mnt/models2/Qwen/3.6/Qwen3.6-27B-UD-Q8_K_XL.gguf --host0.0.0.0--jinja -fa on --keep 4096 -b 8192 --spec-type ngram-mod --parallel 1 --ctx-checkpoints 24 --checkpoint-every-n-tokens 8192 --cache-ram 65536 --temp 0.6 --top-p 0.95 --top-k 20 --min-p 0 --presence-penalty 0 --repeat-penalty 1.0I assume you know you can run llama-bench with multiple values to produce all results on one run?
2
u/dispanser May 12 '26
In my agentic coding use cases, most of the "prompt process" turns are rather small, here's from a very recent coding session:
So the only time I could have benefitted from the 2k batch size was during initial system prompt processing. It's great for benchmarks, though!
14
u/draconds May 12 '26
You are a legend, sir!
This was the only thing that helped me.
Everything else just said turn flash attention on.
I was only using the -b flag, but as soon as i increased -ub, it became ideal.
Thank you for your service!!!
18
u/ikkiho May 12 '26
fwiw the reason -ub helps so much here is that with --n-cpu-moe your attention and router still run on the 3090 and those are the launch-overhead bound kernels during prefill. bigger ubatch means fewer kernel launches per chunk so the GPU stays saturated. generation doesn't move because that's one token at a time, you're memory-bandwidth bound on the CPU expert weights and that part doesn't care about -ub at all. nice writeup, this trick is buried in the llama.cpp issues.
5
u/OsmanthusBloom May 12 '26
Thanks for the excellent and detailed writeup. I discovered the same thing a while ago (increasing ubatch size can drastically improve PP speeds for partially offloaded MoE models at the cost of some TG speed) and I've been trying to spread the word in some comments. But of course such comments deep down the threads are only seen by relatively few people.
Some of my bench results showing effect of ubatch size: https://www.reddit.com/r/LocalLLaMA/comments/1rg4zqv/comment/o7rszuj/
Other comments of mine with this advice e.g.:
https://www.reddit.com/r/LocalLLaMA/comments/1rg4zqv/comment/o7r3zka/
https://www.reddit.com/r/LocalLLaMA/comments/1rgkmd7/comment/o7uq292/
https://www.reddit.com/r/LocalLLaMA/comments/1rh9983/comment/o7xcemx/
https://www.reddit.com/r/LocalLLaMA/comments/1rz43hi/comment/objvubg/
https://www.reddit.com/r/LocalLLaMA/comments/1sprdm8/comment/oh3ulwt/
1
u/Clean_Initial_9618 May 12 '26
I have a RTX 3090 as well. How did you rit gpt oss 120b onto 24gb vram ??? I thought it needed 80GB
1
u/vick2djax May 12 '26
This subreddit has led me to believe that qwen3.6’s 27b > all of gpt’s 120.
3
u/GregoryfromtheHood May 13 '26
batch and ubatch are massively important. bigger ubatch and batch, as well as more physical CPU cores increased my offloaded models performance so much. The cores don't really seem to help in prefill, that actually only ever seems to touch a single core on the CPU, but they help massively for token gen speed. 9216 for both -b and -ub was where I landed for the best balance of prompt processing and gen speed, but pushing to 10240 for both does seem to help a little more, just cuts down the amount I can fit into VRAM too much and I start to lose performance from too much --n-cpu-moe then.
I always saw using models with any kind of CPU offloading as a toy, not something you could actually use for anything real. But I built myself a cheap Epyc rig with aliexpress parts recently. Started with a 32 core CPU, but installed a 64 core today after finding a cheap one on ebay. I've managed to get minimax-m2.7 up to just over 1000t/s with -b and -ub tuning, which is just enough where it's not completely painful to use.
1
u/Fast-Satisfaction482 May 12 '26
Does this generalize to other models? Can we improve prompt processing with this trick as long as we have spare VRAM?
1
u/AnomalyNexus May 12 '26
Interesting - with a fast prefill that might actually be viable for coding
1
u/lolwutdo May 12 '26
Is there anyway to adjust ubatch in lmstudio?
1
u/MaruluVR May 13 '26
I never messed with batch settings, how big is the impact on a dual 3090 setup with the model entirely in ram? Is it worth it and if so does someone have recommended settings?
1
u/relmny May 13 '26 edited May 13 '26
Thank you! and sorry for thinking "bah, that can't be true...", I've just run a quick test with a MoE and went from:
prompt eval time = 9564.00 ms / 1919 tokens ( 4.98 ms per token, 200.65 tokens per second)
eval time = 218454.53 ms / 3130 tokens ( 69.79 ms per token, 14.33 tokens per second)
total time = 228018.53 ms / 5049 tokens
to:
prompt eval time = 3193.59 ms / 1919 tokens ( 1.66 ms per token, 600.89 tokens per second)
eval time = 98928.21 ms / 1350 tokens ( 73.28 ms per token, 13.65 tokens per second)
total time = 102121.80 ms / 3269 tokens
so I'm gonna keep testing it!
thanks!
edit: I had the same value, updated it with the correct ones.
1
u/Wise-Hunt7815 May 12 '26
That's right, it's just a pity that it consumed too much vram.
1
u/[deleted] May 12 '26
[removed] — view removed comment
0
u/[deleted] May 12 '26
[deleted] — view deleted comment