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

PyTorch · Module

tensor.to

Converts a tensor to a requested dtype, device, or both. It can also match another tensor's dtype and device, and may avoid copying when no conversion is needed.

How it works

Call to with a dtype, device, or another tensor to obtain a tensor configured accordingly. If the requested dtype and device already match, the original tensor is returned; otherwise, PyTorch creates a converted copy. Conversion to an integer dtype disables gradient tracking because integer tensors cannot require gradients.

Syntax

torch.Tensor.to(*args, **kwargs) → Tensor

Parameters

NameTypeDescription
dtypetorch.dtype, optionalTarget data type for conversion.
devicetorch.device, optionalTarget device for the returned tensor.
non_blockingbool, optionalWhether the conversion may be performed asynchronously.
copybool, optionalWhether to force creation of a copy.
memory_formattorch.memory_format, optionalDesired memory format of the returned tensor.
othertorch.TensorTensor whose dtype and device should be matched.

Returns

A Tensor converted to the requested dtype and/or device, or matching another tensor; it may be the original tensor when no change is required.

Key points

  • Use Tensor.to to change a tensor's data type, device, or both.
  • The method interprets the supplied arguments to determine the target dtype and device.
  • A matching conversion can return the same tensor instead of allocating a copy.
  • When conversion is needed, the result is a copy with the requested properties.
  • The device form accepts a target device and optionally a target dtype.
  • Passing another tensor makes the result match that tensor's dtype and device.
  • The default memory-format behavior preserves the source layout when possible.

Common mistakes

  • Converting a gradient-tracking tensor to an integer dtype implicitly turns off requires_grad, since integer tensors cannot participate in gradient tracking.
  • With asynchronous conversion on a non-default CUDA stream, the caller must handle cross-stream synchronization.

Example: Convert dtype and device

import torch

x = torch.tensor([1.0, 2.0])
y = x.to(dtype=torch.float64, device="cpu")

print(y.dtype)
print(y.device)

Creates a floating-point tensor and converts it to float64 on the CPU.

When to use it

Move tensors between devices

Prepare model inputs or parameters for a selected accelerator or CPU.

Change numeric dtype

Standardize precision before arithmetic or model execution.

Match another tensor

Create a tensor with the same dtype and device as an existing reference tensor.

Optimize transfers

Request asynchronous transfers where the caller can manage synchronization.

Interview questions

QWhat does Tensor.to do, and what forms of input can it accept?

Tensor.to converts a tensor's dtype and/or device. It can receive a dtype, a device with optional dtype, or another tensor whose dtype and device should be matched.

Follow-up: What happens when the requested dtype and device already match? The method returns the original tensor rather than making an unnecessary copy.

QHow do non_blocking and copy affect conversion?

By default, non_blocking is False, and copy is False. Setting copy=True forces a new tensor even when the source already has the requested dtype and device.

Follow-up: Why might copy=True matter? It guarantees distinct storage, which is useful when callers require an independent tensor object.

QHow is memory format handled by Tensor.to?

The method preserves the tensor's memory format by default through torch.preserve_format. An explicit memory_format can request the layout for the returned tensor.

Follow-up: What limitation applies to integer conversion? A tensor converted to an integer dtype cannot retain requires_grad=True.

  • torch.dtype
  • torch.device
  • torch.memory_format
  • torch.preserve_format
  • Tensor.requires_grad

Source: torch.Tensor.to — PyTorch 2.14 documentation. Checked 2026-10-16.