llama : add --n-cpu-ffn option - #26622
Conversation
|
As one of the persons who have been using Qwen 3.6 27B Q4_K_M and now Qwen 3.8 27B Q4_K_M on 16GB VRAM thanks to your explanation of ffn offloading I think there's a huge benefit in making an easily settable option like this as you propose in this PR. While I still see people sometimes claim they cannot run an MoE because they don't have the VRAM for it, most people now know it's possible by offloading experts to RAM precisely because of the cpu-moe and n-cpu-moe options in llama.cpp. Offloading for dense models and still keep respectable performance is still mostly unknown even though it really works and offers a viable path to running dense models on "low" VRAM cards - and with the corresponding ffn offload settings it will hopefully become much more common knowledge. |
|
Really worthwhile for low VRAM users. |
|
Would help if the pr op could stand on its own, and not requires the link to an external service. |
You are right. |
|
IMHO this is what llama.cpp is all about, enabling more people to run LLMs locally! I can actually run Qwen 3.x 27B at a usable speed on my low-end card with this FFN-on-CPU technique! |
|
Not sure why this was ignored, so I tested it on my rig (Epyc 9374F + RTX PRO 6000 Max-Q). Note that this is somewhat special case as my RAM bandwidth is quite high (if I remember correctly around 360 GB/s). Whole model in VRAMVRAM usage: 45258MiB 32 layers on CPU (-ngl 33)VRAM usage: 25042MiB We can lower VRAM usage by 20GB, but our PP is much lower and TG is crap for longer context length. FFN tensors on CPU in 32 layers (-ncffn 32)VRAM usage: 36602MiB So we traded 11.6GB of VRAM for a huge increase in TG, PP is also higher. Very nice! Matching -ncffn 32 VRAM usage with -nglBut then I thought - hey, I can simply increase my VRAM usage: 36722MiB Hmm, PP is slightly faster than with Edit: re-ran tests with 32 offloaded layers to make the difference more pronounced. |
|
Testing Qwen3.8-27B-IQ3_XXS.gguf on RTX 4060 Ti 16GB + Ryzen 7 7700X + DDR5 @ 4800MT/s. Both of the following runs used approximately the same amount of VRAM. Using
|
| inline std::string llm_ffn_exps_block_regex(int idx) { | ||
| return string_format("blk\\.%d%s", idx, LLM_FFN_EXPS_REGEX); | ||
| } |
There was a problem hiding this comment.
Either adapt this to take the regex and rename or replace it in llama-bench.
There was a problem hiding this comment.
Done. Should --n-cpu-ffn also become part of llama-bench, mirroring --n-cpu-moe?
There was a problem hiding this comment.
It would be useful, doesn't have to be in this PR though.
|
can we able to use the dflash2 with this ? |
|
Wonder what happened to CI? Will try to close/open... |
Yay, that worked. |
|
What are the differences between this and |
convenience On that note, it would be nice if the process would print ot parameters of what happend bc of fit, ngl, n-cpu-ffn etc. |
* common : dedupe --n-cpu-moe / --spec-draft-n-cpu-moe override loops * common : add --n-cpu-ffn to CPU-offload dense FFN weights of first N layers * common : generalize llm_ffn_block_regex over the FFN regex, drop TODO
Overview
Putting dense model layers on the CPU via
--nglcauses major slowdowns. I added an option similar to the existing--n-cpu-moewhich is--n-cpu-ffn. It puts user specified amount of FFN sublayers for dense models.Additional information
My reddit post shows that people are interested. This PR would significantly simplify it for them.
To keep it simple, I made
--n-cpu-ffnblindly take N layers from 0. An optimization is possible by prioritizing the largest FFN layers first for a possible future PR, which would allow for an extra 10% tg speed vs the current sequential method (tested on the new UD-Q4_K_M).Here is a speed comparison vs ngl (MTP has different speeds for prose and code so I separated both for accuracy):

I achieved a tolerable speed of 15-25 t/s tg using Q4_K_M model with a large context of over 90k. The speed/context results are not from -ot alone, but it is the main parameter that made it useable at a longer context using only 16 GB of VRAM.
HW:
RTX 4070 Ti SUPER (16 GB VRAM), i5-13600KF, 32 GB dual-channel DDR5 @ 5800 MHz + tuned timings, Ubuntu 24.04 LTS
Requirements