Skip to main content

How to Move a Tensor Back to the CPU in PyTorch with .cpu()

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Results computed on the GPU often have to come back to the CPU: NumPy, for one, only works with data in the computer's main memory. .cpu() makes that copy. This guide moves a tensor from the GPU of an Apple M1 Pro back to the CPU in PyTorch 2.15.0.

Syntax​

Syntax
tensor.cpu()
PartWhat it means
tensorThe tensor to bring back, on any device
.cpu()Returns a copy of it on the CPU. The original stays where it was

PyTorch's reference says it "Returns a copy of this object in CPU memory. If this object is already in CPU memory, then no copy is performed and the original object is returned."

The Example​

A script makes a tensor on the GPU, brings a copy back to the CPU, 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​

to_cpu.py
import torch

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

on_gpu = torch.tensor([2.0, 4.0, 6.0], device=device)
back_on_cpu = on_gpu.cpu()

print(on_gpu.device)
print(back_on_cpu.device)
print(back_on_cpu)

The to_cpu.py script in a macOS-style code window: the accelerator or the CPU picked as the device, a tensor made on it, on_gpu.cpu() kept as back_on_cpu, and prints of both devices and the copy.

The first two lines pick the GPU when PyTorch can use one, and the CPU otherwise: How to Check Whether PyTorch Can Use a GPU (CUDA or Apple MPS) explains them. device=device makes the tensor straight on the GPU, and on_gpu.cpu() copies it back.

Step 2: Run It​

Terminal
uv run python to_cpu.py
Output
mps:0
cpu
tensor([2., 4., 6.])

The terminal after uv run python to_cpu.py: mps:0, then cpu, then tensor([2., 4., 6.]).

The original is still on mps:0, the M1 Pro's GPU, and the copy is on cpu. The copy prints without a device= label, because it's on the CPU. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
.numpy() on a tensor on the GPUTypeError: can't convert mps:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.on_gpu.cpu().numpy(), which printed [2. 4.] for a two-value tensor
Expecting .cpu() to move the tensor itselfon_gpu was still on mps:0 after the callKeep the copy: back_on_cpu = on_gpu.cpu()

FAQ​

Is .cpu() the same as .to("cpu")?​

They do the same here: on_gpu.to("cpu").device printed cpu too. .cpu() is the shorter way to write it.

Do I need .cpu() before printing a tensor?​

No. print() shows a GPU tensor as it is, with device='mps:0' after its values. The copy matters for libraries that read the numbers directly, such as NumPy.

Conclusion​

on_gpu.cpu() copied a tensor from mps:0, the GPU of an Apple M1 Pro, back to the cpu, while the original stayed on the GPU. NumPy's .numpy() needed that copy first.

Additional Resources​