Skip to content

feat: add --moe-n-expert flag for MoE expert count override - #18029

Open
pestopoppa wants to merge 4 commits into
ggml-org:masterfrom
pestopoppa:feature/moe-hard-mask
Open

pestopoppa wants to merge 4 commits into
ggml-org:masterfrom
pestopoppa:feature/moe-hard-mask

Conversation

@pestopoppa

Copy link
Copy Markdown
Contributor

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.

  • 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

Motivation

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:

Model Baseline 50% Experts Speedup
Qwen3-Coder-480B-A35B 2.5 t/s 3.7 t/s 48%
GLM-4.6-355B-A32B 2.2 t/s 3.0 t/s 36%
Qwen3-Coder-30B-A3B 26.6 t/s 33.6 t/s 26%
Qwen3-VL-30B-A3B 32.2 t/s 38.9 t/s 21%

Quality Assessment

  • 50% experts (e.g., Top-4 instead of Top-8): Excellent quality, nearly identical to baseline
  • 25% experts: Noticeable degradation, still coherent
  • 12.5% experts: Significant quality loss, not recommended

Usage

# Use 4 experts instead of default 8
llama-cli -m model.gguf --moe-n-expert 4 -p "prompt"

# Combine with other options
llama-cli -m qwen3-30b.gguf --moe-n-expert 4 -t 96 -c 4096 -p "Hello"

Test Plan

  • Verified baseline (no flag) produces identical output to before
  • Tested with --moe-n-expert values from 1 to N on multiple MoE models
  • Confirmed speedup scales with expert reduction
  • Validated quality remains acceptable at 50% expert count

🤖 Generated with Claude Code

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]>
@am17an

am17an commented Dec 14, 2025

Copy link
Copy Markdown
Contributor

I would be surprised if this survives perplexity tests. Seems like a lobotomy.

@CISC

CISC commented Dec 14, 2025

Copy link
Copy Markdown
Member

This seems to be based on some misunderstanding, the "hard mask" makes no sense and completely destroys warmup.

Besides you can easily override the <arch>.expert_used_count with --override-kv if you wish. :)

@IIIIIllllIIIIIlllll

Copy link
Copy Markdown

May I ask, I have seen similar settings in LM Studio. Is it the same as this PR?

f7e7656e-c7c9-4e10-a1f7-50d442136d96

@congson1293

Copy link
Copy Markdown

@pestopoppa your idea is the same as this repo and with llama-server we can change the expert_used_count by --override-kv param. Example: --override-kv qwen3moe.expert_used_count=int:4. I do not try with this idea, so like @am17an, I'm curious about the perplexity test when reducing 50% active experts

@pestopoppa

Copy link
Copy Markdown
Contributor Author

Thank you @am17an, @CISC, and @congson1293 for the feedback! You raised valid points that prompted us to investigate further.

Our Use Case: CPU-Only Inference

We'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 Comparison

Following @CISC's suggestion, we benchmarked --override-kv (soft mask) against our hard mask approach on Qwen3-VL-30B-A3B:

Approach Method Baseline 4 Experts Speedup
Soft mask --override-kv qwen3vlmoe.expert_used_count=int:4 25.1 t/s 29.4 t/s +17%
Hard mask --moe-n-expert 4 32.2 t/s 38.9 t/s +21%

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

Model Baseline Hard Mask (50%) Speedup
Qwen3-Coder-480B-A35B 2.5 t/s 3.7 t/s +48%
GLM-4.6-355B-A32B 2.2 t/s 3.0 t/s +36%
Qwen3-Coder-30B-A3B 26.6 t/s 33.6 t/s +26%

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.

Summary

For 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:

  1. Add perplexity benchmarks to validate quality claims?
  2. Document this as a CPU-specific optimization?
  3. Something else entirely?

Happy to adjust the PR based on your guidance!

pestopoppa and others added 3 commits December 15, 2025 02:48
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]>
@sanmai

sanmai commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Tried this with QFN UD-Q4_K_XL with --override-kv.

  • Dropping a single expert has 150% of the impact of going from f16/f16 to q4_0/q4_0 KV cache.
  • Each dropped expert roughly doubles the KLD.
  • On the other hand, dropping three experts is like going from UD-Q4_K_XL to IQ3_S.
  • But at the same time IQ3_S runs at 97.4 t/s decode, and dropping three runs 72.1 t/s on my machine.

I would say going to a smaller quant is a better compromise.

k Mean KLD PPL change Same top p Prefill t/s Decode t/s Cache hit rate
10 (f16 KV) 0 0% 100% 769 57.3 88.2%
10 (q4_0 KV) 0.026 +0.3% 94.9%
9 0.039 +1.8% 93.6% 783 61.2 88.8%
8 0.078 +4.2% 91.1% 851 66.5 89.2%
IQ3_S 0.135 +3.0% 88.9% 1238 97.4 94.5%
7 0.141 +8.9% 88.1% 826 72.1 90.1%
6 0.226 +15% 85.0% 967 78.3 90.8%
5 0.356 +27% 80.7% 1007 87.5 92.5%
4 0.555 +52% 75.4% 1149 93.4 93.2%
3 0.916 210% 67.2% 1203 105.6 95.0%
2 1.75 470% 51.8% 1356 106.4 94.4%
1 5.39 1810% 16.0% 1376 108.2 91.0%

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model Model specific

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants