Repository navigation
sched : copy only the used experts when offloading prompt processing - #15346
Conversation
ggerganov
left a comment
There was a problem hiding this comment.
With models where some experts are often unused, such as gpt-oss
Interesting - I would have expected that a batch of 2048 tokens would pretty much always activate all experts.
| const size_t padding = 512; | ||
| const size_t padding_end = last_id < input->ne[2] - 1 ? std::min<size_t>(expert_size, padding) : 0; | ||
|
|
||
| ggml_backend_tensor_set_async(split_backend, | ||
| input_cpy, | ||
| (const uint8_t *)input->data + expert_offset, expert_offset, | ||
| // copy a bit extra to ensure there are no NaNs in the padding | ||
| expert_size_copy + padding_end); |
There was a problem hiding this comment.
I'm not sure I understand the padding logic and why it is needed.
There was a problem hiding this comment.
The padding is necessary for MMQ. Normally the CUDA backend adds the padding at the end of the tensor, but with MoE, the padding of each expert is effectively the beggining of the next expert. We need to ensure that the padding after every the expert does not contain NaNs, and to do that we copy a few bytes from the next expert. It would also be possible to use ggml_backend_tensor_memset, but this way we avoid an extra call, at the cost of increasing the transfer size by a negligible amount.
I was surprised too, I was only expecting to improve small batch sizes. This is how it looks when processing some code with BS 512blk.0.ffn_gate_exps.weight: 95/128 experts used BS 2048blk.0.ffn_up_exps.weight: 128/128 experts used |
|
Hello, I was wondering what would be the behavior when using a system with multiGPU and then offloading some experts to other GPUs and to the CPU? i.e., I have this complex offload for DeepSeek Would be the copy of only the used experts also apply to the GPUs that manage some others experts? |
|
This only affects experts stored in the CPU. |
|
Thanks, slaren. I have been using this branch for a few days, and it has been of great help improving PP for the GLM 4.5 and gpt-oss models that can't be fully offloaded to GPU on my system. I hope it wasn't forgotten and others can soon enjoy these improvements in master. |
…code steps Two changes to ggml_backend_sched in ggml-backend.cpp: 1. Backend assignment bypass for MUL_MAT_ID with CPU-resident weights: Skip weight-based backend selection so MUL_MAT_ID falls through to the highest-priority backend (CUDA). The scheduler's selective expert copy mechanism (PR ggml-org#15346) handles the CPU→GPU transfer. Previously, --cpu-moe caused all expert matmuls to run on CPU because CUDA's supports_buft returns false for CPU buffers. This change moves expert computation to GPU where it's 10-100x faster, even accounting for PCIe transfer overhead. 2. Expert presence cache: tracks which experts are already in the GPU copy buffer via a per-tensor bitset. Skips re-copying experts that haven't changed between tokens. Reaches 99%+ hit rate during decode due to temporal locality in expert activation patterns. Results on RTX 2070 Max-Q (SM75) with Gemma 4 26B-A4B Q4_K_M --cpu-moe: Decode: 20.8 → 54.4 tok/s (2.6x) Prompt: 45 → 292-404 tok/s (6.5-9x) Cache hit rate: 99%+ after warm-up VRAM usage: unchanged (~2.3 GB) Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
- The driver paragraph now quotes the full module option line that ran both cards on stock 580.196.07 (Rm4598628 together with the resizable-BAR and RMDisableFeatureDisablement keys) and says Rm4598628 alone was never isolated. - Credits: apollo-mg's sm_60 slow-fp16 fix (ggml-org#25593) is what 37c6d14 applies; Kmic-68's P100 fork has the same GGML_CUDA_GRAPHS_PRE_VOLTA opt-in; -ot is slaren's (ggml-org#11397); experts on a second GPU were discussed in ggml-org#22183 and ggml-org#23855; the expert shadows ride on op offload (ggml-org#15346); Strata has its own GP100 kernels.
When offloading prompt processing to GPU with MoE weights on CPU, all the experts are copied to VRAM. With this change, only the experts that are actually used are copied. With models where some experts are often unused, such as gpt-oss, this can reduce significantly the amount of data that needs to be copied to VRAM.
GGML_CUDA=1 scripts/compare-commits.sh master sl/sched-used-experts llama-bench -m gpt-oss-20b-mxfp4.gguf -ot "exps=CPU" -fa 1 -n 0 -p 2048 -ub "128-2048*2"
GGML_CUDA=1 scripts/compare-commits.sh master sl/sched-used-experts llama-bench -m gpt-oss-120b-mxfp4-00001-of-00003.gguf -ot "exps=CPU" -fa 1 -n 0 -p 2048 -ub "128-2048*2" -r 1
Short tests with real data and respecting the chat template (using
llama-cli -f <prompt.txt>) also show a significant improvement.