ultralytics/ultralytics · error · ValueError

Invalid {device_type.upper()} 'device={device}' requested. B

Error message

Invalid {device_type.upper()} 'device={device}' requested. Backend is not available.

What it means

After the torch_npu import gate, select_device checks hasattr(torch, device_type). If torch itself lacks the attribute (torch.npu / torch.xpu), the compiled torch build does not include that backend at all, so the requested device type cannot exist regardless of hardware.

Source

Thrown at ultralytics/utils/torch_utils.py:262

        if device.type not in {"cuda", "npu", "xpu"}:
            return device  # other torch.device inputs pass through; accelerator inputs canonicalize and validate below
    elif str(device).startswith(("tpu", "intel", "vulkan")):
        return device

    s = f"Ultralytics {__version__} 🚀 Python-{PYTHON_VERSION} torch-{TORCH_VERSION} "
    device = parse_device(device)

    if device.startswith(("npu", "xpu")):
        device_type = device.split(":", 1)[0]
        if device_type == "npu":
            try:
                import torch_npu  # noqa
            except ImportError:
                raise ValueError(
                    f"Invalid NPU 'device={device}'. Install 'torch_npu' at https://github.com/Ascend/pytorch"
                )
        if not hasattr(torch, device_type):
            raise ValueError(f"Invalid {device_type.upper()} 'device={device}' requested. Backend is not available.")
        backend = get_torch_device_backend(device_type)
        if not backend.is_available():
            raise ValueError(f"Invalid {device_type.upper()} 'device={device}' requested. Backend is not available.")

        requested = ["0"] if device == device_type else device[4:].split(",")
        indices = [int(x) for x in requested if x.isdigit()]
        if not indices or len(indices) != len(requested) or len(indices) != len(set(indices)):
            raise ValueError(
                f"Invalid {device_type.upper()} 'device={device}' format. "
                f"Use '{device_type}', '{device_type}:0', or '{device_type}:0,1'."
            )
        n = backend.device_count()
        if any(idx >= n for idx in indices):
            raise ValueError(
                f"Invalid {device_type.upper()} 'device={device}' requested. Only {n} device(s) available."
            )

        if len(indices) == 1:

View on GitHub (pinned to 0449ea011c)

Solutions

  1. For NPU: reinstall a matched torch + torch_npu pair (torch_npu patches torch.npu on import).
  2. For XPU: install an Intel-extension PyTorch build (intel-extension-for-pytorch / a torch with XPU support) matching your GPU driver.
  3. Confirm backend presence first: python -c "import torch; print(hasattr(torch, 'xpu'))".
  4. Otherwise fall back to device='cpu' or a CUDA device on supported hardware.

Example fix

# before
YOLO('yolo11n.pt', device='xpu')  # on stock cuda torch: ValueError, backend not available

# after
# install torch with XPU support (Intel build), then:
YOLO('yolo11n.pt', device='xpu')
# otherwise:
YOLO('yolo11n.pt', device='cpu')
Defensive patterns

Strategy: validation

Validate before calling

import torch

def backend_in_torch(device_type: str) -> bool:
    return hasattr(torch, device_type)  # 'npu' or 'xpu'

if str(device).startswith(('npu', 'xpu')) and not backend_in_torch(str(device).split(':')[0]):
    device = 'cpu'

Prevention

When it happens

Trigger: device='npu' with torch_npu importable but a torch build where torch.npu is not patched in (broken install order); device='xpu' (Intel) on a stock CPU/CUDA torch build that has no torch.xpu.

Common situations: Standard pip torch (cpu/cu121 wheels) with device='xpu' — torch.xpu only exists in Intel-extended builds; partially upgraded torch/torch_npu pairs.

Related errors


AI-assisted analysis of ultralytics/ultralytics@0449ea011c (2026-08-15). Data as JSON: /api/errors/3849339836381c3f. Report an issue: GitHub.