diff --git a/vllm/model_executor/models/gemma4.py b/vllm/model_executor/models/gemma4.py index 1b50db61c2..75f6945ccc 100644 --- a/vllm/model_executor/models/gemma4.py +++ b/vllm/model_executor/models/gemma4.py @@ -326,8 +326,9 @@ class Gemma4MoE(nn.Module): # Gemma4 routing: softmax over ALL experts → top-k → renormalize. # FusedMoE's built-in fused_topk scopes softmax differently, so # a custom routing function is needed for numerical correctness. - per_expert_scale = self.per_expert_scale - + # NOTE: self.per_expert_scale is read at call time (not captured into + # a local) so that torch.func.functional_call parameter substitution + # reaches the routing function correctly. def routing_function( hidden_states: torch.Tensor, gating_output: torch.Tensor, @@ -336,10 +337,12 @@ class Gemma4MoE(nn.Module): ) -> tuple[torch.Tensor, torch.Tensor]: if current_platform.is_cuda_alike() or current_platform.is_xpu(): return gemma4_fused_routing_kernel_triton( - gating_output, topk, per_expert_scale + gating_output, topk, self.per_expert_scale ) - return gemma4_routing_function_torch(gating_output, topk, per_expert_scale) + return gemma4_routing_function_torch( + gating_output, topk, self.per_expert_scale + ) # FusedMoE experts with custom Gemma4 routing self.experts = FusedMoE(