From 2fb31605985d650e969ac27fd3ec026a9de9c621 Mon Sep 17 00:00:00 2001 From: Mayyyy <30303931+hustmf@users.noreply.github.com> Date: Thu, 25 Dec 2025 00:18:39 +0800 Subject: [PATCH] [NPU] Bug fix in device detect (#14137) --- python/sglang/srt/utils/common.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/python/sglang/srt/utils/common.py b/python/sglang/srt/utils/common.py index dc1df393e..03174bf1f 100644 --- a/python/sglang/srt/utils/common.py +++ b/python/sglang/srt/utils/common.py @@ -144,7 +144,15 @@ def is_xpu() -> bool: @lru_cache(maxsize=1) def is_npu() -> bool: - return hasattr(torch, "npu") and torch.npu.is_available() + if not hasattr(torch, "npu"): + return False + + if not torch.npu.is_available(): + raise RuntimeError( + "torch_npu detected, but NPU device is not available or visible." + ) + + return True @lru_cache(maxsize=1) @@ -1854,7 +1862,7 @@ def get_device(device_id: Optional[int] = None) -> str: return "xpu" return "xpu:{}".format(device_id) - if hasattr(torch, "npu") and torch.npu.is_available(): + if is_npu(): if device_id == None: return "npu" return "npu:{}".format(device_id)