{"record":{"id":"7d0bedc8505661e4","repo":"sgl-project/sglang","slug":"torch-npu-detected-but-npu-device-is-not-availabl","errorCode":null,"errorMessage":"torch_npu detected, but NPU device is not available or visible.","messagePattern":"torch_npu detected, but NPU device is not available or visible\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/utils/common.py","lineNumber":185,"sourceCode":"\ndef register_xpu_device_properties_for_dynamo() -> None:\n    if not is_xpu():\n        return\n\n    import torch._dynamo.utils as dynamo_utils\n\n    xpu_props_type = getattr(torch.xpu, \"_XpuDeviceProperties\", None)\n    if xpu_props_type is not None:\n        dynamo_utils.common_constant_types.add(xpu_props_type)\n\n\n@lru_cache(maxsize=1)\ndef is_npu() -> bool:\n    if not hasattr(torch, \"npu\"):\n        return False\n\n    if not torch.npu.is_available():\n        raise RuntimeError(\n            \"torch_npu detected, but NPU device is not available or visible.\"\n        )\n\n    return True\n\n\n@lru_cache(maxsize=1)\ndef is_host_cpu_x86() -> bool:\n    machine = platform.machine().lower()\n    return (\n        machine in (\"x86_64\", \"amd64\", \"i386\", \"i686\")\n        and hasattr(torch, \"cpu\")\n        and torch.cpu.is_available()\n    )\n\n\ndef is_host_cpu_arm64() -> bool:\n    machine = platform.machine().lower()","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/utils/common.py#L167-L203","documentation":"is_npu() detects Ascend NPU support: if the torch_npu module is loaded (torch has an 'npu' attribute) but torch.npu.is_available() is False — no visible NPU device — it raises RuntimeError instead of silently returning False, because a half-installed NPU environment is a misconfiguration.","triggerScenarios":"Importing torch_npu (directly or via a build with it bundled) on a machine with no Ascend NPU, or with NPU devices not visible (ASCEND_RT_VISIBLE_DEVICES empty/invalid).","commonSituations":"Running a torch_npu-enabled build on a plain CUDA/CPU box; mis-set ASCEND_RT_VISIBLE_DEVICES; driver/cdev permissions hiding NPUs in containers.","solutions":["Set ASCEND_RT_VISIBLE_DEVICES to a valid NPU id and verify with npu-smi info","If you don't intend NPU, uninstall torch_npu or prevent its import (it's monkeypatching torch) to fall back to CUDA/CPU","Fix container/device permissions so the NPU is visible"],"exampleFix":"# before\n# torch_npu installed, no NPU hardware -> RuntimeError on is_npu()\n# after\npip uninstall torch_npu  # or: export ASCEND_RT_VISIBLE_DEVICES=0 with NPU present","handlingStrategy":"fallback","validationCode":"import torch\nhas_npu_mod = hasattr(torch, 'npu')\nuse_npu = has_npu_mod and torch.npu.is_available() and torch.npu.device_count() > 0","typeGuard":"def npu_ready() -> bool:\n    return hasattr(torch, 'npu') and torch.npu.is_available()","tryCatchPattern":"try:\n    from sglang.srt.utils.common import is_npu\n    npu = is_npu()\nexcept RuntimeError:\n    npu = False  # half-installed torch_npu; fall back to CUDA/CPU","preventionTips":["Don't install torch_npu on non-Ascend machines","Set ASCEND_RT_VISIBLE_DEVICES and verify with npu-smi info before launching"],"tags":["npu","environment","hardware-detection"],"backgroundTag":"device-not-available","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}