sgl-project/sglang · critical · ValueError
AfmoeConfig must define `num_experts`.
Error message
AfmoeConfig must define `num_experts`.
What it means
The AFMoE model initializer requires the HF config to define `num_experts`, the number of routed experts. The code uses getattr(config, 'num_experts', None) and raises when the attribute is missing. Without it the router and expert layout cannot be constructed.
Source
Thrown at python/sglang/srt/models/afmoe.py:167
denom = topk_weights.sum(dim=-1, keepdim=True).clamp(min=1e-20)
topk_weights = topk_weights / denom
return topk_weights.to(torch.float32), topk_ids.to(torch.int32)
def __init__(
self,
config: PretrainedConfig,
quant_config: Optional[QuantizationConfig] = None,
prefix: str = "",
):
super().__init__()
self.config = config
self.rank = get_parallel().tp_rank
self.tp_size = get_parallel().tp_size
self.n_routed_experts = getattr(config, "num_experts", None)
if self.n_routed_experts is None:
raise ValueError("AfmoeConfig must define `num_experts`.")
self.top_k = config.num_experts_per_tok
if self.tp_size > self.n_routed_experts:
raise ValueError(
f"Tensor parallel size {self.tp_size} is greater than "
f"the number of experts {self.n_routed_experts}."
)
self.score_func = getattr(config, "score_func", "softmax")
self.route_norm = getattr(config, "route_norm", True)
self.route_scale = float(getattr(config, "route_scale", 1.0))
self.n_group = getattr(config, "n_group", 1)
self.topk_group = getattr(config, "topk_group", 1)
self.use_grouped_topk = self.n_group is not None and self.n_group > 1
self.num_shared_experts = getattr(config, "num_shared_experts", 0)
self.gate = ReplicatedLinear(
config.hidden_size,
self.n_routed_experts,View on GitHub (pinned to 0132848349)
Solutions
- Add "num_experts": <N> to the model's config.json (also ensure num_experts_per_tok is set, since it is read unconditionally next)
- If your config uses a different key name (e.g. n_routed_experts), rename it to num_experts
Example fix
// before
{ "hidden_size": 4096, "num_experts_per_tok": 8 }
// after
{ "hidden_size": 4096, "num_experts": 64, "num_experts_per_tok": 8 } Defensive patterns
Strategy: validation
Validate before calling
assert getattr(config, "num_experts", None) is not None, "config must define num_experts"
Prevention
- Validate required MoE config fields before launching the server
- Keep checkpoint configs unmodified from the source repo
When it happens
Trigger: Loading an AFmoe checkpoint whose config.json lacks the `num_experts` field (or has it renamed/misspelled, e.g. `n_routed_experts`).
Common situations: Custom or fine-tuned AFMoE checkpoints converted from other frameworks; hand-edited config.json; config field renamed in a newer transformers version.
Related errors
- The hpc_ops MoE runner backend does not support fused shared
- The hpc_ops MoE runner backend does not support apply_router
- The hpc_ops MoE runner backend does not support no_combine (
- The hpc_ops MoE runner backend runs a plain SiLU-and-mul; it
- The hpc_ops MoE runner backend only supports FP8-quantized M
AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28).
Data as JSON: /api/errors/6865128e2f87fc73.
Report an issue: GitHub.