torch.accelerator error: Cannot access accelerator device (tested)

RuntimeError: Cannot access accelerator device when none is available comes from torch.accelerator calls such as current_device_index() or synchronize() when no GPU is usable. Use torch.accelerator.current_accelerator(check_available=True), which returns None in that case, and fall back to CPU.

1. Reproduce

b1.py calls the device-generic torch.accelerator API on a machine without a GPU:

import torch
print("torch", torch.__version__)
print("is_available:", torch.accelerator.is_available())
print("current_accelerator:", torch.accelerator.current_accelerator())
print("device_count:", torch.accelerator.device_count())
for name, call in [("current_device_index()", torch.accelerator.current_device_index),
                   ("synchronize()", torch.accelerator.synchronize),
                   ("set_device_index(0)", lambda: torch.accelerator.set_device_index(0)),
                   ("current_accelerator(check_available=True)", lambda: torch.accelerator.current_accelerator(check_available=True))]:
    try:
        print(name, "->", call())
    except Exception as e:
        print(name, "->", type(e).__name__ + ":", e)

With the CPU-only build:

torch 2.14.1+cpu
is_available: False
current_accelerator: None
device_count: 0
current_device_index() -> RuntimeError: Cannot access accelerator device when none is available.
synchronize() -> RuntimeError: Cannot access accelerator device when none is available.
set_device_index(0) -> RuntimeError: Cannot access accelerator device when none is available.
current_accelerator(check_available=True) -> None

With a CUDA build on the same machine (no NVIDIA GPU or driver):

torch 2.14.1+cu130
is_available: False
current_accelerator: cuda
device_count: 0
current_device_index() -> RuntimeError: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx
synchronize() -> RuntimeError: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx
set_device_index(0) -> RuntimeError: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx
current_accelerator(check_available=True) -> None

The query functions (is_available, device_count, current_accelerator) never raise. Anything that touches a device (current_device_index, synchronize, set_device_index) raises RuntimeError: Cannot access accelerator device when none is available on a CPU build, or the backend's own error (Found no NVIDIA driver) on a CUDA build. The trap: on the CUDA build current_accelerator() returns cuda even though is_available() is False, so code that only checks current_accelerator() picks a device that cannot be used.

2. Fix: pass check_available=True, fall back to CPU

import torch
# Portable device pick: an accelerator only if it is really usable, else CPU
device = torch.accelerator.current_accelerator(check_available=True) or torch.device("cpu")
x = torch.ones(2, device=device)
print("device:", device, "| tensor on:", x.device)

Output on both builds:

device: cpu | tensor on: cpu
device: cpu | tensor on: cpu

Guard device calls with torch.accelerator.is_available(). If you expected a GPU, the error is a build or driver problem, see Torch not compiled with CUDA enabled.

Frequently asked questions

What does Cannot access accelerator device when none is available mean?

A torch.accelerator function that needs a device (current_device_index, synchronize, set_device_index) was called while torch.accelerator.is_available() is False.

Why does torch.accelerator.current_accelerator() return cuda without a GPU?

On a CUDA build it reports the compiled-in accelerator type. Pass check_available=True to get None when no device is usable.

How do I pick a device with torch.accelerator?

device = torch.accelerator.current_accelerator(check_available=True) or torch.device("cpu").

Written with AI assistance. Measured on 2026-10-24 with torch 2.14.1+cpu and 2.14.1+cu130 on Python 3.13.5, Linux x86_64 without an NVIDIA GPU. The scripts ran verbatim and the output was pasted unedited. PlainTables is our extension; PyTorch is free.