diff --git a/python/sglang/srt/models/glm4_moe.py b/python/sglang/srt/models/glm4_moe.py index 7ab4603f01..03e3d0a7b5 100644 --- a/python/sglang/srt/models/glm4_moe.py +++ b/python/sglang/srt/models/glm4_moe.py @@ -1277,6 +1277,9 @@ class GlmMoeDsaForCausalLM(DeepseekV2ForCausalLM): class GlmMoeDsaForCausalLMNextN(DeepseekV3ForCausalLMNextN): + # ModelConfig rewrites a GLM draft's architecture to this class's own name, + # so the inherited DeepSeek name would never match what the gate compares. + fused_shared_experts_architecture = "GlmMoeDsaForCausalLMNextN" # GLM-5.2's MTP layer index differs from DeepSeek's (61), so the inherited # substr mapping would wrongly rewrite GLM's real layer-61 weights. # exclude_layers remapping for the MTP layer is handled explicitly in @@ -1285,6 +1288,14 @@ class GlmMoeDsaForCausalLMNextN(DeepseekV3ForCausalLMNextN): _NEXTN_SPEC_WEIGHT_NAMES = ("shared_head.norm", "eh_proj", "enorm", "hnorm") + @classmethod + def shared_experts_fusion_disable_reason(cls, hf_config, quant_config): + # The PTPC cast only rewrites routed experts, so the shared expert never + # matches the FP8 per-channel slot it would be fused into. + if should_apply_glm_nextn_moe_ptpc(quant_config, hf_config.num_hidden_layers): + return "GLM NextN PTPC does not support shared experts fusion." + return super().shared_experts_fusion_disable_reason(hf_config, quant_config) + @classmethod def _map_mtp_ckpt_name(cls, name: str, layer_prefix: str) -> str: # Keep this mapping in sync with DeepseekV2WeightLoaderMixin's diff --git a/test/registered/unit/models/test_glm_nextn_moe_ptpc.py b/test/registered/unit/models/test_glm_nextn_moe_ptpc.py index e047cbf502..28c2bc8630 100644 --- a/test/registered/unit/models/test_glm_nextn_moe_ptpc.py +++ b/test/registered/unit/models/test_glm_nextn_moe_ptpc.py @@ -29,8 +29,10 @@ register_cpu_ci(est_time=5, suite="base-a-test-cpu") LAYER = 78 PREFIX = f"model.layers.{LAYER}" EXPERT_LEAF = f"{PREFIX}.mlp.experts.0.w1" +SHARED_EXPERT_LEAF = f"{PREFIX}.mlp.shared_experts.gate_proj" ATTN_LEAF = f"{PREFIX}.self_attn.q_proj" EXPERT_WEIGHT = f"{PREFIX}.mlp.experts.0.gate_proj.weight" +SHARED_EXPERT_WEIGHT = f"{PREFIX}.mlp.shared_experts.gate_proj.weight" _PTPC_ENV = "sglang.srt.models.glm4_moe.envs.SGLANG_GLM_NEXTN_MOE_PTPC.get" @@ -69,6 +71,21 @@ class TestEnableGlmNextnMoePtpc(CustomTestCase): should_apply_glm_nextn_moe_ptpc(_quark_cfg(exclude=[ATTN_LEAF]), LAYER) ) + def test_ptpc_disables_shared_expert_fusion(self): + hf_config = SimpleNamespace( + architectures=["GlmMoeDsaForCausalLMNextN"], + n_routed_experts=256, + n_shared_experts=1, + num_hidden_layers=LAYER, + ) + # Shared expert BF16 (excluded) or still quantized: neither fits the slot. + for exclude in ([EXPERT_LEAF, SHARED_EXPERT_LEAF], [EXPERT_LEAF]): + with self.subTest(exclude=exclude), patch(_PTPC_ENV, return_value=True): + reason = GlmMoeDsaForCausalLMNextN.shared_experts_fusion_disable_reason( + hf_config, _quark_cfg(exclude=exclude) + ) + self.assertIn("PTPC", reason or "") + class TestResolveNextnQuantConfigPtpcOn(CustomTestCase): def _resolve(self, cfg, flag: bool): @@ -132,7 +149,13 @@ class TestMaybeQuantGlmNextnMoeToPtpc(CustomTestCase): loader = GlmMoeDsaForCausalLMNextN.__new__(GlmMoeDsaForCausalLMNextN) loader.quant_config = cfg loader.config = SimpleNamespace(num_hidden_layers=LAYER) - weights = [(EXPERT_WEIGHT, torch.ones(4, 8, dtype=torch.bfloat16))] + weights = [ + (EXPERT_WEIGHT, torch.ones(4, 8, dtype=torch.bfloat16)), + ( + SHARED_EXPERT_WEIGHT, + torch.full((4, 8), 1e-4, dtype=torch.bfloat16), + ), + ] with patch( _PTPC_ENV, return_value=flag, @@ -146,14 +169,15 @@ class TestMaybeQuantGlmNextnMoeToPtpc(CustomTestCase): self.assertIn(EXPERT_WEIGHT[: -len("weight")] + "weight_scale", names) weight = dict(out)[EXPERT_WEIGHT] self.assertEqual(weight.dtype, torch.float8_e4m3fn) + self.assertEqual(dict(out)[SHARED_EXPERT_WEIGHT].dtype, torch.bfloat16) + self.assertNotIn(SHARED_EXPERT_WEIGHT[: -len("weight")] + "weight_scale", names) def test_flag_on_does_not_cast_when_experts_not_excluded(self): src = _quark_cfg(exclude=[ATTN_LEAF]) out = self._cast(src, flag=True) - self.assertEqual(len(out), 1) - name, tensor = out[0] - self.assertEqual(name, EXPERT_WEIGHT) - self.assertEqual(tensor.dtype, torch.bfloat16) + self.assertEqual(len(out), 2) + self.assertEqual(dict(out)[EXPERT_WEIGHT].dtype, torch.bfloat16) + self.assertEqual(dict(out)[SHARED_EXPERT_WEIGHT].dtype, torch.bfloat16) if __name__ == "__main__": diff --git a/test/registered/unit/models/test_shared_experts_fusion_gates.py b/test/registered/unit/models/test_shared_experts_fusion_gates.py index bbd1ce6201..9220408722 100644 --- a/test/registered/unit/models/test_shared_experts_fusion_gates.py +++ b/test/registered/unit/models/test_shared_experts_fusion_gates.py @@ -317,6 +317,29 @@ class TestGlmMoeGate(_FusionGateCase): "GlmMoeDsaForCausalLM", ) + def test_the_dsa_nextn_draft_declares_its_own_architecture(self): + from sglang.srt.models.glm4_moe import GlmMoeDsaForCausalLMNextN + + self.assertEqual( + GlmMoeDsaForCausalLMNextN.fused_shared_experts_architecture, + "GlmMoeDsaForCausalLMNextN", + ) + + def test_the_dsa_nextn_draft_can_fuse_the_target_layout(self): + from sglang.srt.models.glm4_moe import GlmMoeDsaForCausalLMNextN + + self._seed() + draft_config = SimpleNamespace( + architectures=["GlmMoeDsaForCausalLMNextN"], + n_routed_experts=256, + n_shared_experts=1, + num_hidden_layers=78, + ) + self.assertNotIn( + "does not support", + self._reason(GlmMoeDsaForCausalLMNextN, draft_config) or "", + ) + class TestGlm5NextGate(_FusionGateCase): def _config(self, **kw):