Skip to main content

How to Check Which Device a Tensor Is On in PyTorch

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A PyTorch tensor lives on one device: the computer's main processor (CPU) or a GPU. Tensors on different devices can't be combined, so knowing where one is saves a lot of confusion. This guide reads it with .device in PyTorch 2.15.0 on an Apple M1 Pro.

Syntax​

Syntax
tensor.device
tensor.device.type
PartWhat it gives
tensor.deviceThe device the tensor is on, such as cpu or mps:0. A GPU's name ends in its number, counted from 0
tensor.device.typeThe kind of device without the number: cpu, mps for the GPU of an Apple silicon Mac, cuda for an NVIDIA GPU

PyTorch's reference describes a torch.device as holding "a device type (most commonly "cpu" or "cuda", but also potentially "mps", …)" and an optional "device ordinal", the number.

The Example​

A script makes one tensor on the CPU and one on the GPU, when PyTorch can use one, and prints where each lives.

Prerequisites​

  • A uv project with PyTorch and NumPy. uv add torch numpy adds both; without NumPy, PyTorch 2.15.0 prints a warning each time it starts. This guide uses PyTorch 2.15.0 and NumPy 2.5.5 on Python 3.15.2, with uv 0.13.10, on an Apple M1 Pro.

Step 1: Write the Script​

tensor_device.py
import torch

on_cpu = torch.tensor([1.0, 2.0, 3.0])
print(on_cpu.device)

accelerator = torch.accelerator.current_accelerator(check_available=True)
device = accelerator or torch.device("cpu")
on_gpu = torch.tensor([1.0, 2.0, 3.0], device=device)
print(on_gpu.device)
print(on_gpu.device.type)

The tensor_device.py script in a macOS-style code window: one tensor made on the CPU, then the accelerator or the CPU picked as the device, a second tensor made on it, and print of each device.

on_cpu is made without a device, so it goes to the CPU. The next two lines pick the GPU: torch.accelerator.current_accelerator(check_available=True) returns the GPU PyTorch can use right now, or None when there isn't one, and or torch.device("cpu") falls back to the CPU in that case. How to Check Whether PyTorch Can Use a GPU (CUDA or Apple MPS) explains that check. device=device then makes on_gpu straight on that device.

Step 2: Run It​

Terminal
uv run python tensor_device.py
Output
cpu
mps:0
mps

The terminal after uv run python tensor_device.py: cpu, then mps:0, then mps.

The first tensor is on cpu. The second is on mps:0, the first (and only) GPU of this Mac, and its .device.type is just mps. On a computer without a GPU PyTorch can use, the fallback puts on_gpu on the CPU too, and all three lines say cpu. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
on_gpu.device == torch.device("mps")It printed False for a tensor on mps:0: a device without a number doesn't equal one with a numberon_gpu.device.type == "mps", which printed True
Combining tensors on two devices, such as on_gpu + on_cpuRuntimeError: Expected all tensors to be on the same device, but found at least two devices, mps:0 and cpu!Move one first, as the fix shows
Writing "cuda" on a MacAssertionError: Torch not compiled with CUDA enabledLet PyTorch pick the device, as Step 1 does

FAQ​

What does the :0 in mps:0 mean?​

It's the GPU's number, which PyTorch's reference calls the "device ordinal", counted from 0. A computer with two NVIDIA GPUs has cuda:0 and cuda:1. This Mac has one GPU: torch.mps.device_count() printed 1, so its tensors are always on mps:0.

How do I move a tensor to the GPU?​

With .to(device): How to Move a Tensor to the GPU in PyTorch shows it.

Conclusion​

.device showed cpu for a tensor made without a device and mps:0 for one made on the M1 Pro's GPU, and .device.type gave the kind alone, mps.

Additional Resources​