Fix stale GLM MoE routing after runtime weight updates (#35883)

Co-authored-by: Jiajun Li <jiajun.li@radixark.ai>
This commit is contained in:
Yuzhen Zhou
2026-08-30 14:13:34 -07:00
committed by GitHub
co-authored by Jiajun Li
parent 5ab97c4f44
commit 8a87079dbb
6 changed files with 68 additions and 32 deletions
+4 -7
View File
@@ -368,19 +368,16 @@ class Glm4MoeGate(nn.Module):
):
super().__init__()
self.weight = nn.Parameter(
torch.empty((config.n_routed_experts, config.hidden_size))
torch.empty(
(config.n_routed_experts, config.hidden_size), dtype=torch.float32
)
)
self.e_score_correction_bias = nn.Parameter(
torch.empty((config.n_routed_experts), dtype=torch.float32)
)
# GLM requires FP32 gate projection; cache to avoid per-forward cast.
# FIXME: if gate weight is updated at runtime (e.g. expert rebalancing), _weight_fp32 must be invalidated.
self.register_buffer("_weight_fp32", None, persistent=False)
def forward(self, hidden_states):
if self._weight_fp32 is None:
self._weight_fp32 = self.weight.data.to(torch.float32)
logits = F.linear(hidden_states.to(torch.float32), self._weight_fp32, None)
logits = F.linear(hidden_states.to(torch.float32), self.weight, None)
return logits
+4 -7
View File
@@ -156,19 +156,16 @@ class Glm4MoeLiteGate(nn.Module):
super().__init__()
self.is_nextn = is_nextn
self.weight = nn.Parameter(
torch.empty((config.n_routed_experts, config.hidden_size))
torch.empty(
(config.n_routed_experts, config.hidden_size), dtype=torch.float32
)
)
self.e_score_correction_bias = nn.Parameter(
torch.empty((config.n_routed_experts), dtype=torch.float32)
)
# GLM requires FP32 gate projection; cache to avoid per-forward cast.
# FIXME: if gate weight is updated at runtime (e.g. expert rebalancing), _weight_fp32 must be invalidated.
self.register_buffer("_weight_fp32", None, persistent=False)
def forward(self, hidden_states):
if self._weight_fp32 is None:
self._weight_fp32 = self.weight.data.to(torch.float32)
logits = F.linear(hidden_states.to(torch.float32), self._weight_fp32, None)
logits = F.linear(hidden_states.to(torch.float32), self.weight, None)
return logits
@@ -55,7 +55,6 @@ _NON_PERSISTENT_BUFFER_PATTERNS = (
"cos_sin_cache",
"inv_freq",
"freqs_cis",
"_weight_fp32",
"expert_mask_gpu",
)