Bring results to the CPU
Preparing accelerator results for CPU-only processing or inspection.
PyTorch · Module
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.
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.
Tensor.cpu(*memory_format=torch.preserve_format)| Name | Type | Description |
|---|---|---|
memory_format | torch.memory_format | Optional torch.memory_format specifying the desired memory format of the returned tensor; defaults to torch.preserve_format. |
Tensor in CPU memory; if the source is already on CPU, the original tensor is returned without copying.
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.
Preparing accelerator results for CPU-only processing or inspection.
Selecting the desired layout while transferring a tensor to CPU memory.
Avoiding unnecessary allocation when code accepts tensors from mixed devices.
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.
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.
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.toTensor.cudatorch.memory_format