{"record":{"id":"d0e23c7c9591ebe9","repo":"hiyouga/LlamaFactory","slug":"npuswiglukernel-requires-torch-npu","errorCode":null,"errorMessage":"NpuSwiGluKernel requires torch_npu.","messagePattern":"NpuSwiGluKernel requires torch_npu\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/llamafactory/v1/plugins/model_plugins/kernels/ops/mlp/npu_swiglu.py","lineNumber":105,"sourceCode":"        \"Qwen3_5MoeMLP\": npu_swiglu_forward,\n    },\n}\n\n\n@KernelPlugin(\"npu_fused_swiglu\").register()\nclass NpuSwiGluKernel(BaseKernel):\n    \"\"\"NPU Kernel for fused SwiGLU activation.\"\"\"\n\n    @staticmethod\n    def check_device() -> None:\n        current = get_current_accelerator().type\n        if current != DeviceType.NPU:\n            raise RuntimeError(f\"NpuSwiGluKernel requires NPU, current accelerator is {current}.\")\n\n    @staticmethod\n    def check_deps() -> None:\n        if _TORCH_NPU_IMPORT_ERROR is not None:\n            raise RuntimeError(\"NpuSwiGluKernel requires torch_npu.\") from _TORCH_NPU_IMPORT_ERROR\n\n    @staticmethod\n    def _get_patch_forward(model_type: str, module: torch.nn.Module):\n        \"\"\"Return the NPU forward function for a matched SwiGLU MLP module.\"\"\"\n        model_patches = _MODEL_TYPE_TO_PATCHES.get(model_type, {})\n        patch_forward = model_patches.get(module.__class__.__name__)\n        if patch_forward is None:\n            return None\n\n        config = getattr(module, \"config\", None)\n        if getattr(config, \"hidden_act\", None) != \"silu\":\n            return None\n\n        return patch_forward\n\n    @staticmethod\n    def _apply(**kwargs) -> \"HFModel\":\n        \"\"\"Applies the NPU fused SwiGLU kernel to the model.","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/v1/plugins/model_plugins/kernels/ops/mlp/npu_swiglu.py#L87-L123","documentation":"The npu_fused_swiglu plugin needs torch_npu to call Ascend fused operators. The module-level import error is stored in _TORCH_NPU_IMPORT_ERROR; check_deps() converts it into RuntimeError('NpuSwiGluKernel requires torch_npu.') chained to the original failure.","triggerScenarios":"Applying npu_fused_swiglu where `import torch_npu` fails: package absent, torch/torch_npu version mismatch, or CANN runtime not initialized.","commonSituations":"Same class of issues as other NPU kernels: Ascend images without the right wheel, torch upgraded independently, CANN env vars missing.","solutions":["Install matching torch_npu for your torch+CANN versions and re-run","Check the chained __cause__ for the real ImportError text","Source CANN set_env.sh in the launch script/container entrypoint"],"exampleFix":"# before\n# torch_npu absent → RuntimeError at check_deps\n# after\npip install torch_npu==<matching-version>","handlingStrategy":"validation","validationCode":"try:\n    import torch_npu  # noqa: F401\nexcept ImportError:\n    kernels = [k for k in kernels if k != \"npu_fused_swiglu\"]","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Install matching torch_npu wheel; verify import before launch"],"tags":["dependencies","npu","torch-npu","swiglu"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}