{"record":{"id":"ae4ac8fb6e1bb40f","repo":"hiyouga/LlamaFactory","slug":"npuropekernel-requires-npu-current-accelerator-is","errorCode":null,"errorMessage":"NpuRoPEKernel requires NPU, current accelerator is {current}.","messagePattern":"NpuRoPEKernel requires NPU, current accelerator is (.+?)\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/llamafactory/v1/plugins/model_plugins/kernels/ops/rope/npu_rope.py","lineNumber":136,"sourceCode":"    \"qwen3_next\": _default_rope_patch(\"qwen3_next\"),\n    \"qwen3_omni_moe\": _default_rope_patch(\"qwen3_omni_moe\"),\n    \"qwen3_omni_moe_thinker\": _default_rope_patch(\"qwen3_omni_moe\"),\n    \"qwen3_vl\": _default_rope_patch(\"qwen3_vl\"),\n    \"qwen3_vl_moe\": _default_rope_patch(\"qwen3_vl_moe\"),\n    \"qwen3_5\": _default_rope_patch(\"qwen3_5\"),\n    \"qwen3_5_moe\": _default_rope_patch(\"qwen3_5_moe\"),\n}\n\n\n@KernelPlugin(\"npu_fused_rope\").register()\nclass NpuRoPEKernel(BaseKernel):\n    \"\"\"NPU Kernel for Rotary Position Embedding.\"\"\"\n\n    @staticmethod\n    def check_device() -> None:\n        current = get_current_accelerator().type\n        if current != DeviceType.NPU:\n            raise RuntimeError(f\"NpuRoPEKernel 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(\"NpuRoPEKernel requires torch_npu.\") from _TORCH_NPU_IMPORT_ERROR\n\n    @staticmethod\n    def _apply_model_patches(model_type: str) -> int:\n        patches = _MODEL_TYPE_TO_PATCHES.get(model_type)\n        if patches is None:\n            return 0\n\n        patched_count = 0\n        for module_name, replacements in patches:\n            try:\n                target_module = importlib.import_module(module_name)\n            except Exception as e:\n                logger.warning_rank0_once(f\"Failed to import {module_name} for NPU RoPE kernel: {e}\")","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/v1/plugins/model_plugins/kernels/ops/rope/npu_rope.py#L118-L154","documentation":"The npu_fused_rope plugin replaces rotary-embedding forwards with an NPU implementation. check_device() requires accelerator type DeviceType.NPU and raises RuntimeError otherwise, since npu RoPE ops do not exist on other backends.","triggerScenarios":"Applying npu_fused_rope on CUDA/CPU machines — wrong-hardware kernel list, or NPU hardware where the accelerator type is misreported because torch_npu is uninitialized.","commonSituations":"Heterogeneous cluster configs; smoke tests on laptops/GPU boxes; accelerator type queried before device backend init.","solutions":["Drop npu_fused_rope from kernels on non-NPU nodes","On NPU nodes verify accelerator type resolves to npu before the run","Make kernels selection device-aware"],"exampleFix":"# before\nkernels: [npu_fused_rope]  # on CUDA\n\n# after\nkernels: [flash_attn]  # device-appropriate choice","handlingStrategy":"validation","validationCode":"if get_current_accelerator().type != \"npu\":\n    kernels = [k for k in kernels if k != \"npu_fused_rope\"]","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Per-device kernel lists in heterogeneous clusters","Assert accelerator type in a startup sanity check"],"tags":["device-check","npu","rope","kernel-plugin"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}