torch.accelerator
Lazy Initialization and Fork Safety
Accelerator runtimes (CUDA, XPU, MPS, etc.) are initialized lazily — only
when the first operation that touches the device runs. This ensures that
import torch and capability queries do not poison subsequent fork()
calls. See Poison fork in multiprocessing for background.
Certain APIs need to be callable before forking (e.g., device_count(),
is_available()), and some backends provide opt-in mechanisms to make
these fork-safe (e.g., CUDA via NVML, XPU via Level Zero Sysman). To keep
behavior and runtime state consistent between and
per-backend modules, the following APIs are delegated to each backend: