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

Function is_torch_xpu_available

src/transformers/utils/import_utils.py:310–331  ·  view source on GitHub ↗

Checks if XPU acceleration is available via stock PyTorch (>=2.6) and potentially if a XPU is in the environment.

(check_device: bool = False)

Source from the content-addressed store, hash-verified

308
309@lru_cache
310def is_torch_xpu_available(check_device: bool = False) -> bool:
311 """
312 Checks if XPU acceleration is available via stock PyTorch (>=2.6) and
313 potentially if a XPU is in the environment.
314 """
315 if not is_torch_available():
316 return False
317
318 torch_version = version.parse(get_torch_version())
319 if torch_version.major == 2 and torch_version.minor < 6:
320 return False
321
322 import torch
323
324 if check_device:
325 try:
326 # Will raise a RuntimeError if no XPU is found
327 _ = torch.xpu.device_count()
328 return torch.xpu.is_available()
329 except RuntimeError:
330 return False
331 return hasattr(torch, "xpu") and torch.xpu.is_available()
332
333
334@lru_cache

Callers 15

print_env.pyFile · 0.90
flush_memoryFunction · 0.90
FbgemmFp8TestClass · 0.90
is_flash_attn_availableFunction · 0.85
masking_utils.pyFile · 0.85
set_seedFunction · 0.85
__init__Method · 0.85
startMethod · 0.85
stopMethod · 0.85

Calls 4

is_torch_availableFunction · 0.85
get_torch_versionFunction · 0.85
parseMethod · 0.45
is_availableMethod · 0.45