Skip to main content

How to Check Whether PyTorch Can Use a GPU (CUDA or Apple MPS)

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

PyTorch runs much faster on a graphics processor (GPU) than on the main processor, but only if it can find one. NVIDIA GPUs are reached through CUDA, and the GPU in Apple silicon Macs through MPS. This guide asks PyTorch 2.15.0 which one it can use, then puts a tensor on it, on an Apple M1 Pro.

Syntax​

Syntax
torch.cuda.is_available()
torch.backends.mps.is_available()
torch.accelerator.current_accelerator(check_available=True)
CallWhat it returns
torch.cuda.is_available()True when an NVIDIA GPU can be used through CUDA
torch.backends.mps.is_available()True when the GPU of an Apple silicon Mac can be used through MPS
torch.accelerator.current_accelerator(check_available=True)The accelerator PyTorch can use, such as cuda or mps, or None when there's none

PyTorch's own descriptions: torch.cuda.is_available() will "Return a bool indicating if CUDA is currently available", and torch.backends.mps.is_available() will "Return a bool indicating if MPS is currently available." For current_accelerator, passing check_available=True adds a runtime check on top of the check of how PyTorch was built.

MPS is Apple's Metal Performance Shaders. PyTorch's MPS backend notes say the mps device "enables high-performance training on GPU for macOS devices with Metal programming framework."

The Example​

A script prints PyTorch's version and the three answers, then makes a small tensor on whichever accelerator was found, falling back to the CPU.

Prerequisites​

  • uv. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Create a Project with PyTorch​

Terminal
uv init --python 3.15 gpu-check
cd gpu-check
uv add torch

uv add torch installed PyTorch 2.15.0 and the 9 packages it needs.

Which GPU support you get depends on your system, because this command installs PyTorch from PyPI. uv's PyTorch guide explains that PyPI "hosts CPU-only wheels for Windows and macOS, and GPU-accelerated wheels on Linux":

  • Mac with Apple silicon: "CPU-only" here means without CUDA. The Mac build still includes MPS, as the output below shows.
  • Linux with an NVIDIA GPU: the build from PyPI includes CUDA.
  • Windows with an NVIDIA GPU: the build from PyPI has no CUDA. uv's guide shows how to install a CUDA build from PyTorch's own index instead.

Step 2: Write the Check​

check_gpu.py
import torch

print("PyTorch:", torch.__version__)
print("NVIDIA GPU (CUDA) available:", torch.cuda.is_available())
print("Apple GPU (MPS) available:", torch.backends.mps.is_available())
print("Accelerator:", torch.accelerator.current_accelerator(check_available=True))

device = torch.accelerator.current_accelerator(check_available=True) or torch.device("cpu")
numbers = torch.ones(3, device=device)
print("Tensor is on:", numbers.device)

The check_gpu.py script in a macOS-style code window: it prints the PyTorch version, torch.cuda.is_available(), torch.backends.mps.is_available() and the current accelerator, then makes a tensor of three ones on the accelerator or the CPU and prints its device.

The second-to-last line picks the device: the accelerator if there is one, otherwise the CPU, because or uses the right side when the left side is None. torch.ones(3, device=device) then creates the tensor directly on that device.

Step 3: Run It​

Terminal
uv run python check_gpu.py
Output
/Users/you/source/gpu-check/.venv/lib/python3.15/site-packages/torch/_subclasses/functional_tensor.py:377: UserWarning: Failed to initialize NumPy: No module named 'numpy' (Triggered internally at /Users/runner/work/pytorch/pytorch/torch/csrc/utils/tensor_numpy.cpp:82.)
cpu = _conversion_method_template(device=torch.device("cpu"))
PyTorch: 2.15.0
NVIDIA GPU (CUDA) available: False
Apple GPU (MPS) available: True
Accelerator: mps
Tensor is on: mps:0

The terminal after uv run python check_gpu.py: a NumPy UserWarning, then PyTorch 2.15.0, CUDA available False, MPS available True, accelerator mps, and the tensor on mps:0.

On this M1 Pro there's no NVIDIA GPU, so CUDA is False, while the Apple GPU is available: MPS is True, the accelerator is mps, and the tensor was created on mps:0, the first (and only) Apple GPU. On a PC with a working NVIDIA GPU, you'd see CUDA True, cuda and cuda:0 instead. The command works unchanged in PowerShell.

The warning at the top doesn't affect the check: this project has PyTorch alone, and PyTorch doesn't install NumPy. Fixed: UserWarning: Failed to initialize NumPy: No module named 'numpy' explains it and removes it with uv add numpy.

Common Mistakes​

MistakeWhat happensDo this instead
Checking only torch.cuda.is_available() on a MacIt's always False on a Mac, so the code falls back to the slower CPUCheck MPS too, or use torch.accelerator
Writing device="cuda" in code that should also run on a MacAssertionError: Torch not compiled with CUDA enabledPick the device at run time, as the script does
Calling current_accelerator() without check_available=TrueIt reports the accelerator PyTorch was built for, even if it can't be used right nowPass check_available=True

FAQ​

Why does the device say mps:0 and not just mps?​

The :0 is the device's number. A computer can have several GPUs; the first one is number 0.

Does it work on Windows?​

The script runs there unchanged, but with uv add torch you get PyPI's Windows build, which has no CUDA, so CUDA is False even on a PC with an NVIDIA GPU. Install a CUDA build as uv's PyTorch guide describes, then run the check again. I ran this guide on a Mac only.

Conclusion​

On an Apple M1 Pro, PyTorch 2.15.0 reported CUDA False, MPS True and the accelerator mps, and created the tensor on mps:0. torch.accelerator.current_accelerator(check_available=True) picks the right device on any computer, with the CPU as the fallback.

Additional Resources​