{"record":{"id":"ad175023574c138c","repo":"hiyouga/LlamaFactory","slug":"npuswiglukernel-requires-npu-current-accelerator","errorCode":null,"errorMessage":"NpuSwiGluKernel requires NPU, current accelerator is {current}.","messagePattern":"NpuSwiGluKernel requires NPU, current accelerator is (.+?)\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/llamafactory/v1/plugins/model_plugins/kernels/ops/mlp/npu_swiglu.py","lineNumber":100,"sourceCode":"    },\n    \"qwen3_5\": {\n        \"Qwen3_5MLP\": npu_swiglu_forward,\n    },\n    \"qwen3_5_moe\": {\n        \"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","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/v1/plugins/model_plugins/kernels/ops/mlp/npu_swiglu.py#L82-L118","documentation":"The npu_fused_swiglu plugin replaces SwiGLU MLP forwards with an NPU-fused implementation. check_device() requires the accelerator type to be DeviceType.NPU and raises RuntimeError on any other device, since the replacement forward only exists for Ascend.","triggerScenarios":"Applying the npu_fused_swiglu kernel plugin on cuda/cpu/other accelerators — wrong-hardware config, or Ascend hardware where torch_npu is not initialized so the accelerator reports cpu.","commonSituations":"Shared configs across CUDA and NPU clusters; debugging NPU configs on a GPU workstation; accelerator not yet initialized when the plugin check runs.","solutions":["Drop npu_fused_swiglu from kernels on non-NPU nodes","On Ascend nodes, confirm `torch.npu.is_available()` / accelerator type is npu before applying","Make the kernel list device-conditional"],"exampleFix":"# before\nkernels: [npu_fused_swiglu]  # on CUDA node\n\n# after\nkernels: []  # or device-appropriate kernel","handlingStrategy":"validation","validationCode":"if get_current_accelerator().type != \"npu\":\n    kernels = [k for k in kernels if k != \"npu_fused_swiglu\"]","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Device-gate NPU kernels in shared configs","Test accelerator detection early in the launch script"],"tags":["device-check","npu","swiglu","kernel-plugin"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}