Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Module

tensor.cpu

Moves a tensor to CPU memory, returning a copy when needed. If it is already on CPU, no copy occurs and the original object is returned.

How it works

Tensor.cpu() produces a tensor in CPU memory, copying the object when it resides elsewhere. When the tensor is already on the CPU, PyTorch avoids copying and returns the same object; memory_format can control the returned tensor's desired memory layout.

Syntax

Tensor.cpu(*memory_format=torch.preserve_format)

Parameters

NameTypeDescription
memory_formattorch.memory_formatOptional torch.memory_format specifying the desired memory format of the returned tensor; defaults to torch.preserve_format.

Returns

Tensor in CPU memory; if the source is already on CPU, the original tensor is returned without copying.

Key points

  • Use cpu() to obtain a tensor stored in CPU memory.
  • For tensors already on the CPU, cpu() avoids allocation and preserves object identity.
  • The optional memory_format argument controls the returned tensor's desired layout.
  • If omitted, memory_format defaults to torch.preserve_format.

Common mistakes

  • Calling cpu() on a CPU tensor does not create a new object; code that requires an independent copy should use an explicit clone.

Example: Ensure a tensor is in CPU memory

import torch

x = torch.tensor([1, 2], device="cpu")
y = x.cpu()

assert y.device.type == "cpu"
assert y is x  # already on CPU, so no copy is performed
print(y)

Creates a CPU tensor and calls cpu(); because the tensor is already on CPU, the returned object is the original tensor.

When to use it

Bring results to the CPU

Preparing accelerator results for CPU-only processing or inspection.

Request a memory format

Selecting the desired layout while transferring a tensor to CPU memory.

Normalize device location

Avoiding unnecessary allocation when code accepts tensors from mixed devices.

Interview questions

QWhat does Tensor.cpu() do?

cpu() returns a tensor located in CPU memory. It copies the tensor when necessary, but returns the original object when the tensor is already on the CPU.

Follow-up: How can you force a distinct CPU tensor? Use tensor.cpu().clone(), because cpu() alone may return the original object for a CPU tensor.

QWhat is the purpose and default of memory_format?

The optional memory_format argument specifies the desired memory format of the returned tensor. Its documented default is torch.preserve_format.

Follow-up: What happens if memory_format is omitted? PyTorch uses torch.preserve_format as the default.

QDoes Tensor.cpu() always allocate a new tensor?

The method returns a Tensor in CPU memory. For a tensor already in CPU memory, the method performs no copy and returns that same tensor object.

Follow-up: Why might object identity matter? Mutating or comparing the result can affect the original reference when no copy occurs.

  • Tensor.to
  • Tensor.cuda
  • torch.memory_format

Source: torch.Tensor.cpu. Checked 2026-10-16.