{"record":{"id":"827b27fd493ff562","repo":"hiyouga/LlamaFactory","slug":"ligerkernel-requires-cuda-or-npu-current-accelera","errorCode":null,"errorMessage":"LigerKernel requires CUDA or NPU, current accelerator is {current}.","messagePattern":"LigerKernel requires CUDA or NPU, current accelerator is (.+?)\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/llamafactory/v1/plugins/model_plugins/kernels/liger_kernel_ops.py","lineNumber":52,"sourceCode":"    \"qwen3\": \"apply_liger_kernel_to_qwen3\",\n    \"qwen3_moe\": \"apply_liger_kernel_to_qwen3_moe\",\n    \"qwen3_next\": \"apply_liger_kernel_to_qwen3_next\",\n    \"qwen3_5\": \"apply_liger_kernel_to_qwen3_5\",\n    \"qwen3_5_text\": \"apply_liger_kernel_to_qwen3_5_text\",\n    \"qwen3_5_moe\": \"apply_liger_kernel_to_qwen3_5_moe\",\n    \"qwen3_5_moe_text\": \"apply_liger_kernel_to_qwen3_5_moe_text\",\n}\n\n\n@KernelPlugin(\"liger_kernel\").register()\nclass LigerKernel(BaseKernel):\n    \"\"\"Liger Kernel for optimized model training.\"\"\"\n\n    @staticmethod\n    def check_device() -> None:\n        current = get_current_accelerator().type\n        if current not in (DeviceType.CUDA, DeviceType.NPU):\n            raise RuntimeError(f\"LigerKernel requires CUDA or NPU, current accelerator is {current}.\")\n\n    @staticmethod\n    def check_deps() -> None:\n        \"\"\"Checks if the required dependencies for the kernel are available.\"\"\"\n        try:\n            import liger_kernel  # noqa: F401\n        except ImportError:\n            raise RuntimeError(\"Liger kernel is not installed.\") from None\n\n    @staticmethod\n    def _apply(**kwargs) -> \"HFModel\":\n        \"\"\"Applies the Liger kernel to the model.\n\n        Args:\n            **kwargs: Must include ``model``. Optional ``use_kernels`` is a list of Liger op\n                names to enable exclusively, or the string ``\"auto\"`` to use each\n                ``apply_liger_kernel_to_*`` function's signature defaults (same as calling\n                upstream with only ``model``). Optional ``require_logits`` forces non-fused","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/v1/plugins/model_plugins/kernels/liger_kernel_ops.py#L34-L70","documentation":"LigerKernel.check_device() gates the kernel to CUDA or NPU accelerators. On any other accelerator (CPU, XPU, MLP, HPU, MPS) it raises RuntimeError naming the current device type, before any patching is attempted.","triggerScenarios":"kernel_config.name includes 'liger_kernel' while get_current_accelerator().type resolves to CPU (e.g. quick CPU smoke test), Apple Silicon MPS, or Intel XPU.","commonSituations":"Running the same training YAML on a laptop/CI CPU runner that worked on an A100; NPU images without correct torch_npu setup so the accelerator falls back to CPU; enabling kernels globally in shared configs.","solutions":["Run on a CUDA (or NPU) machine — Liger kernels are GPU-only.","Remove liger_kernel from kernel_config.name for CPU runs, or make the config conditional per environment.","Use \"auto\" kernel selection: _apply_auto_kernels picks kernels per device_type, skipping Liger on CPU.","For NPU, ensure torch_npu is installed and initialized so the accelerator type is detected as NPU."],"exampleFix":"# before (shared config)\nkernel_config:\n  name: \"liger_kernel\"   # fails on CPU CI\n\n# after\nkernel_config:\n  name: \"auto\"           # auto selects only device-appropriate kernels","handlingStrategy":"validation","validationCode":"from llamafactory.v1.core.accelerator import get_current_accelerator\nassert get_current_accelerator().type in ('cuda', 'npu'), f'Liger needs CUDA/NPU, got {get_current_accelerator().type}'","typeGuard":"def liger_supported() -> bool:\n    \"\"\"True when the current accelerator is CUDA or NPU.\"\"\"\n    return get_current_accelerator().type in (DeviceType.CUDA, DeviceType.NPU)","tryCatchPattern":"try:\n    model = KernelPlugin('liger_kernel').apply(model=model)\nexcept RuntimeError as e:\n    if 'requires CUDA or NPU' in str(e):\n        logger.warning('skipping liger on %s', get_current_accelerator().type)\n    else:\n        raise","preventionTips":["Gate kernel selection on accelerator type in launch scripts.","Prefer kernel name 'auto' for configs shared across device types.","Smoke-test configs on the target hardware before long runs."],"tags":["kernels","device","liger","hardware"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}