{"record":{"id":"6865128e2f87fc73","repo":"sgl-project/sglang","slug":"afmoeconfig-must-define-num-experts","errorCode":null,"errorMessage":"AfmoeConfig must define `num_experts`.","messagePattern":"AfmoeConfig must define `num_experts`\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"python/sglang/srt/models/afmoe.py","lineNumber":167,"sourceCode":"            denom = topk_weights.sum(dim=-1, keepdim=True).clamp(min=1e-20)\n            topk_weights = topk_weights / denom\n\n        return topk_weights.to(torch.float32), topk_ids.to(torch.int32)\n\n    def __init__(\n        self,\n        config: PretrainedConfig,\n        quant_config: Optional[QuantizationConfig] = None,\n        prefix: str = \"\",\n    ):\n        super().__init__()\n        self.config = config\n        self.rank = get_parallel().tp_rank\n        self.tp_size = get_parallel().tp_size\n\n        self.n_routed_experts = getattr(config, \"num_experts\", None)\n        if self.n_routed_experts is None:\n            raise ValueError(\"AfmoeConfig must define `num_experts`.\")\n        self.top_k = config.num_experts_per_tok\n        if self.tp_size > self.n_routed_experts:\n            raise ValueError(\n                f\"Tensor parallel size {self.tp_size} is greater than \"\n                f\"the number of experts {self.n_routed_experts}.\"\n            )\n\n        self.score_func = getattr(config, \"score_func\", \"softmax\")\n        self.route_norm = getattr(config, \"route_norm\", True)\n        self.route_scale = float(getattr(config, \"route_scale\", 1.0))\n        self.n_group = getattr(config, \"n_group\", 1)\n        self.topk_group = getattr(config, \"topk_group\", 1)\n        self.use_grouped_topk = self.n_group is not None and self.n_group > 1\n        self.num_shared_experts = getattr(config, \"num_shared_experts\", 0)\n\n        self.gate = ReplicatedLinear(\n            config.hidden_size,\n            self.n_routed_experts,","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/models/afmoe.py#L149-L185","documentation":"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.","triggerScenarios":"Loading an AFmoe checkpoint whose config.json lacks the `num_experts` field (or has it renamed/misspelled, e.g. `n_routed_experts`).","commonSituations":"Custom or fine-tuned AFMoE checkpoints converted from other frameworks; hand-edited config.json; config field renamed in a newer transformers version.","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"],"exampleFix":"// before\n{ \"hidden_size\": 4096, \"num_experts_per_tok\": 8 }\n// after\n{ \"hidden_size\": 4096, \"num_experts\": 64, \"num_experts_per_tok\": 8 }","handlingStrategy":"validation","validationCode":"assert getattr(config, \"num_experts\", None) is not None, \"config must define num_experts\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate required MoE config fields before launching the server","Keep checkpoint configs unmodified from the source repo"],"tags":["moe","config-validation","afmoe","model-loading"],"backgroundTag":"missing-config-field","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}