{"record":{"id":"724f249e150e13bf","repo":"hiyouga/LlamaFactory","slug":"self-class-name-requires-a-gate-tensor-f","errorCode":null,"errorMessage":"{self.__class__.__name__} requires a gate tensor for NPU Gated RMSNorm.","messagePattern":"(.+?) requires a gate tensor for NPU Gated RMSNorm\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/llamafactory/v1/plugins/model_plugins/kernels/ops/rms_norm/npu_rms_norm.py","lineNumber":108,"sourceCode":"    \"\"\"NPU forward implementation for Gated RMSNorm with high-precision FP32 computation.\n\n    This function performs RMSNorm and gated SiLU multiplication in FP32 for numerical\n    stability. The supported gated RMSNorm modules use ``scale = weight`` with weight\n    initialized to 1, unlike the residual RMSNorm variants that use ``1.0 + weight``.\n\n    Args:\n        self (nn.Module): The Gated RMSNorm module instance.\n        hidden_states (Tensor): Input hidden states tensor.\n        gate (Tensor): Gate tensor for SiLU activation.\n\n    Returns:\n        Tensor: Output tensor cast back to the original input dtype.\n\n    Raises:\n        ValueError: If the gate tensor is not provided.\n    \"\"\"\n    if gate is None:\n        raise ValueError(f\"{self.__class__.__name__} requires a gate tensor for NPU Gated RMSNorm.\")\n\n    input_dtype = hidden_states.dtype\n    hidden_states = hidden_states.to(torch.float32)\n    _eps = getattr(self, \"variance_epsilon\", None) or getattr(self, \"eps\", 1e-6)\n\n    hidden_states = torch_npu.npu_rms_norm(hidden_states, self.weight.float(), epsilon=_eps)[0]\n    hidden_states = hidden_states * F.silu(gate.to(torch.float32))\n\n    return hidden_states.to(input_dtype)\n\n\n_MODEL_TYPE_TO_PATCHES = {\n    \"qwen3\": {\n        \"Qwen3RMSNorm\": npu_rms_norm_forward,\n    },\n    \"qwen3_moe\": {\n        \"Qwen3MoeRMSNorm\": npu_rms_norm_forward,\n    },","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/v1/plugins/model_plugins/kernels/ops/rms_norm/npu_rms_norm.py#L90-L126","documentation":"The gated RMSNorm NPU forward applies RMSNorm then multiplies by SiLU(gate); the gate tensor is mandatory. If forward is called with gate=None (the patched signature allows it), the code raises ValueError naming the module class, rather than passing None into a fused op.","triggerScenarios":"Calling a patched gated RMSNorm module's forward without the gate argument — a caller/site written for the unpatched module where gate was optional, or a model path (e.g. non-gated layer variant) that never produces a gate tensor while the module class got patched.","commonSituations":"Model variants sharing a module class where only some paths supply a gate; upstream code calling hidden_states-only forward after the NPU patch was applied; partial application of patches across mixed layers.","solutions":["Ensure every call site of the gated RMSNorm module passes the gate tensor","If the layer genuinely has no gate, prevent that module from being patched (check _MODEL_TYPE_TO_PATCHES matching for the model type)","Upgrade/downgrade transformers so gated and non-gated variants use distinct classes matching the patch map"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"# before invoking the patched module, ensure a gate tensor is available\nif gate is None:\n    raise ValueError(\"gated RMSNorm requires gate; this layer must not be patched\")","typeGuard":null,"tryCatchPattern":"try:\n    out = module(hidden_states, gate=gate)\nexcept ValueError as e:\n    if \"requires a gate tensor\" in str(e):\n        # fall back to unpatched forward\n        out = module._original_forward(hidden_states, gate)\n    else:\n        raise","preventionTips":["Ensure all call sites pass gate for gated-norm models","Keep patched/unpatched classes distinct when customizing model code"],"tags":["runtime","npu","rmsnorm","api-contract"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}