Changing Tensor Datatypes with .to()
The .to() method transfers a tensor to a GPU, and it is the same method used to change a tensor's datatype.
One Method for Two Kinds of Change
You have already used .to() to change a tensor's datatype. This article shows that the same method can also transfer a tensor to a GPU. The source makes this connection on purpose: one method handles both kinds of change, and the argument you pass decides which change happens. Passing a device name such as "cuda" asks for the tensor to be moved there, while passing a datatype asks for a change of type.
The question this article answers is not how addition works. Addition stays the same. What changes is where the addition happens. The tensors are moved onto a GPU first, and then the same element-by-element sum is performed there. To follow that move, you need to know what the device information in a result means, and that is the thread running through the rest of the article.
What do you think happens?
Two tensors are each moved with .to("cuda:0"), then added and printed. Which device label do you expect to see in the printed result?
Reveal answer
cuda:0
The source states that the printed result of a transferred tensor carries device information such as device='cuda:0', which identifies the GPU holding it. Because both tensors were sent to the first GPU, the sum is produced there and reports that device.
Tracing Two Tensors Through a Transfer
The source's trace starts from two small one-dimensional tensors, tensor_1 and tensor_2. Each step does one of three things: moves a tensor with .to(), assigns the returned value back to the same name, or adds the tensors and prints the sum. The assignment matters. After the transfer lines run, the names tensor_1 and tensor_2 refer to the transferred versions, so the addition uses those versions.
tensor([5., 7., 9.], device='cuda:0')- tensor_1 and tensor_2 are created first, as in the earlier work on adding tensors.
- Each .to("cuda:0") call returns a tensor that is assigned back to the same name.
- The addition uses the transferred tensors, so the sum is computed on the GPU.
- The printed output includes the device field, which is the detail this article focuses on.
The code above is an illustration written for this article, not a line copied from the source. It needs a machine with a CUDA-capable GPU to run. The source describes the same pattern: transfer each tensor, reassign the results, add, and print. Watch the two things the source asks you to watch: the lines that move each tensor, and the device information in the printed result.
In the diagram, only the transfer step is marked as changed. That is deliberate. The transfer is the step where the device is set, while the addition uses whatever device the tensors already occupy.
Reading the Device Field
The printed result contains a device field. The source states that the resulting tensor now includes this device information and gives the example device='cuda:0'. Reading that field is the most direct way to confirm where a result resides. You do not need to guess from the code alone: the printed output tells you.
In that notation, the number after cuda identifies which GPU holds the tensor. The source explains that device='cuda:0' means the tensors reside on the first GPU. So when you see that label, you can read it directly: the tensor lives on the GPU numbered 0.
Choosing a GPU with a Device ID
The source notes that a machine can host more than one GPU. When it does, you choose which one receives the tensors by putting a device ID in the transfer command. The source gives .to("cuda:0") as the form for the first GPU and .to("cuda:1") as the form for another GPU, and says the pattern continues in the same way.
- "cuda" by itself is the form used in the source's main example and asks for the tensor to be moved to a GPU.
- "cuda:0" names the first GPU explicitly, and the source states that device='cuda:0' means the tensors reside on the first GPU.
- "cuda:1" names a second GPU using the same numbering pattern, and only the device ID changes between the two forms.
Mistakes When Reading Device Information
Common mistake Calling .to() without assigning the result back to a name
Why it failsThe source assigns each result back to the same name so that tensor_1 and tensor_2 refer to the transferred versions. If the returned tensor is discarded, the name keeps pointing at the original tensor.
Do this insteadWrite tensor_1 = tensor_1.to("cuda:0") so the name refers to the transferred tensor before it is used.
Common mistake Treating the device label as decoration
Why it failsThe source states that reading the device field is the most direct way to confirm where a result resides. Ignoring it means you are guessing about where the computation ran.
Do this insteadCheck the device field in every printed result when you need to confirm placement.
Common mistake Thinking the device ID changes what the tensor contains
Why it failsIn the source's example, only the device ID changes between the transfer lines. The device ID selects which GPU receives the tensor; it is not part of the tensor's values.
Do this insteadRead the number after cuda as a location, the index of the GPU that holds the tensor.
Tracing Where Tensors Reside
Trace this program line by line. After each assignment, state which device each tensor is on, then predict the device label in the final print.
a = torch.tensor([1.0, 2.0])
b = torch.tensor([3.0, 4.0])
a = a.to("cuda:1")
print(a + b)Hint: Ask first whether b has been moved at all, and remember that the device a result reports depends on the device its inputs occupy.
Hints
- Ask first whether b has been moved at all.
- Remember that the source introduces .to() as the transfer step; a tensor that never passes through it has not been moved by it.
- The number after cuda is the GPU index, so cuda:1 names a different GPU than cuda:0.
Key Takeaways
- .to() transfers a tensor to a GPU, and it is the same method used to change a tensor's datatype; the argument you pass determines which change happens.
- Assigning the result of .to() back to the same name makes that name refer to the transferred tensor for the lines that follow.
- The printed result includes a device field such as device='cuda:0', which is the most direct way to confirm where a result resides.
- A device ID such as 0 or 1 in .to("cuda:0") or .to("cuda:1") selects which GPU receives the tensor on a machine with multiple GPUs.