MCPcopy
hub / github.com/huggingface/transformers / is_torch_npu_available

Function is_torch_npu_available

src/transformers/utils/import_utils.py:289–306  ·  view source on GitHub ↗

Checks if `torch_npu` is installed and potentially if a NPU is in the environment

(check_device=False)

Source from the content-addressed store, hash-verified

287
288@lru_cache
289def is_torch_npu_available(check_device=False) -> bool:
290 "Checks if `torch_npu` is installed and potentially if a NPU is in the environment"
291 if not is_torch_available() or not _is_package_available("torch_npu")[0]:
292 return False
293
294 import torch
295 import torch_npu # noqa: F401
296
297 if check_device:
298 try:
299 # Will raise a RuntimeError if no NPU is found
300 if hasattr(torch, "npu"):
301 _ = torch.npu.device_count()
302 return torch.npu.is_available()
303 return False
304 except RuntimeError:
305 return False
306 return hasattr(torch, "npu") and torch.npu.is_available()
307
308
309@lru_cache

Callers 15

test_kernels_fallbackMethod · 0.90
is_flash_attn_availableFunction · 0.85
_lazy_importsFunction · 0.85
set_seedFunction · 0.85
__init__Method · 0.85
startMethod · 0.85
stopMethod · 0.85
require_torch_npuFunction · 0.85
require_torch_multi_npuFunction · 0.85

Calls 3

is_torch_availableFunction · 0.85
_is_package_availableFunction · 0.85
is_availableMethod · 0.45

Tested by 5

test_kernels_fallbackMethod · 0.72
require_torch_npuFunction · 0.68
require_torch_multi_npuFunction · 0.68