Repository navigation
feat: add --moe-n-expert flag for MoE expert count override - #18029
pestopoppa wants to merge 4 commits into
Conversation
Add ability to reduce the number of active experts in MoE models at runtime, providing significant speedup with minimal quality loss when using 50% of default experts. Implementation: - Add moe_n_expert_override parameter to llama_context_params - Add --moe-n-expert CLI flag to override n_expert_used - Implement "Hard Mask" in build_moe_ffn() that slices expert tensors - Uses ggml_view_2d/3d + ggml_cont to reduce actual computation Benchmark results (AOCL BLIS 5.0, AMD EPYC 9655): - Qwen3-Coder-480B-A35B: 2.5 → 3.7 t/s (48% speedup) - GLM-4.6-355B-A32B: 2.2 → 3.0 t/s (36% speedup) - Qwen3-Coder-30B-A3B: 26.6 → 33.6 t/s (26% speedup) - Qwen3-VL-30B-A3B: 32.2 → 38.9 t/s (21% speedup) Quality: Excellent at 50% experts, degraded at 25%, gibberish at 12.5% Usage: llama-cli -m model.gguf --moe-n-expert 4 -p "prompt" 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
|
I would be surprised if this survives perplexity tests. Seems like a lobotomy. |
|
This seems to be based on some misunderstanding, the "hard mask" makes no sense and completely destroys warmup. Besides you can easily override the |
|
@pestopoppa your idea is the same as this repo and with |
|
Thank you @am17an, @CISC, and @congson1293 for the feedback! You raised valid points that prompted us to investigate further. Our Use Case: CPU-Only InferenceWe're optimizing for CPU-only inference on an AMD EPYC 9655 (96 cores, 1.13TB DDR5). Memory capacity isn't our constraint—bandwidth is. We wanted to understand if reducing active experts could improve throughput. Hard Mask vs Soft Mask ComparisonFollowing @CISC's suggestion, we benchmarked
Hard mask is ~32% faster in absolute terms (38.9 vs 29.4 t/s). The difference appears to be that hard mask skips the routing computation for excluded experts entirely, while soft mask still routes over all 8 before computing only 4. Larger Models Benefit More
Quality Assessment@am17an - You're right that we need perplexity measurements. Subjectively, at 50% experts the output quality appears unchanged for conversational tasks. At 25% we saw degradation, and at 12.5% it was gibberish. We'll add formal perplexity benchmarks. SummaryFor CPU inference where memory isn't the bottleneck, hard mask provides better speedups than soft mask. The routing "warmup" concern may be more relevant for GPU scenarios where VRAM is precious and you want proper expert selection. Would you recommend we:
Happy to adjust the PR based on your guidance! |
Adds n_layer_exit parameter to control how many layers to compute, enabling early exit speculation techniques like CAS-Spec and CLaSp. Changes: - Add n_layer_exit to llama_context_params (public API) - Add n_layer_exit to llama_cparams (internal) - Add --n-layer-exit CLI parameter - Implement layer skip in model graph builders: - llama.cpp (models) - qwen2.cpp - qwen3.cpp - qwen3moe.cpp When n_layer_exit > 0 and < n_layer, the model will exit early after computing that many layers. This is useful for generating draft tokens in speculative decoding scenarios. Example: --n-layer-exit 7 on a 28-layer model gives ~2.2x speedup 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
Extends layer skip / early exit support to additional model architectures: - qwen3vl-moe.cpp (Qwen3-VL-30B-A3B and similar VL MoE models) - qwen3next.cpp (Qwen3-Next-80B-A3B and similar hybrid attention models) Results after adding layer skip support: - Qwen3-VL-30B-A3B: 3.4x speedup with 16 layers (vs all 48) - Qwen3-Next-80B-A3B: 3.7x speedup with 8 layers - Qwen3-Coder-480B-A35B: 5.0x speedup with 16 layers 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
49162df to
0215468
Compare
|
Tried this with QFN UD-Q4_K_XL with
I would say going to a smaller quant is a better compromise.
Compared to say Qwen3.8-27B, dropping one expert is like going from bf16 to q4_0, and dropping two experts is like going from bf16 to q3_K. Without a doubt, that compounds with the effects from quantizing the model itself. |

Summary
Add ability to reduce the number of active experts in MoE models at runtime, providing significant speedup with minimal quality loss when using 50% of default experts.
moe_n_expert_overrideparameter tollama_context_params--moe-n-expertCLI flag to overriden_expert_usedbuild_moe_ffn()that slices expert tensorsggml_view_2d/3d+ggml_contto reduce actual computationMotivation
MoE models like Qwen3 and GLM-4 select Top-K experts per token (e.g., Top-8 out of 128). By reducing the number of active experts at runtime, we can trade a small amount of quality for significant speedup - useful for drafting, interactive use, or resource-constrained environments.
Benchmark Results
Tested on AMD EPYC 9655 (96 cores) with AOCL BLIS 5.0:
Quality Assessment
Usage
Test Plan
--moe-n-expertvalues from 1 to N on multiple MoE models🤖 Generated with Claude Code