Installing a GPU-Compatible Version of PyTorch
A device is where computations occur and data resides, and the CPU and the GPU are both examples of devices.
Why Check the Runtime Before Using a GPU
Before you change any training code to use a GPU, it helps to ask a simple question first: does the runtime you are working in support GPU computing at all? PyTorch gives you a direct way to find out. This article starts with a short script that asks PyTorch for that answer, then uses what the script reveals to build an understanding of how PyTorch chooses where work happens.
Asking the Runtime Whether a GPU Is Usable
The function torch.cuda.is_available() reports whether GPU computing is available in the current runtime. Notice what it does not report: it does not tell you where any particular tensor currently sits. Those are two separate questions. The first is about the environment you are running in; the second is about the placement of data and work, which the rest of this article covers.
True or False, depending on the runtime you are usingtorch.cuda.is_available()returns a boolean for the current runtime- The printed value describes this environment, not PyTorch in general
The diagram shows the decision the check gives you. A True result tells you GPU computing can be used in this runtime. A False result tells you it cannot, so the work stays on the CPU. Either way, the check itself does not move any data or change where operations run. It only reports what the runtime supports.
- The if branch runs only when the check returns True
- The else branch covers the False case
What a Device Is in PyTorch
A device is where computations occur and data resides. The CPU and the GPU are both examples of devices.
Think of a device as a location with two jobs at once. It is the place where PyTorch carries out arithmetic, and it is also the place where the numbers involved in that arithmetic are held. Because both jobs belong to one concept, you cannot talk about where a computation happens without also talking about where its data lives.
Both the CPU and the GPU are devices, so both can host computations and data. They are not treated the same way by default, though. Computations in PyTorch are carried out on the CPU by default, so moving work to a GPU is a deliberate step rather than something that happens automatically.
Tracing Where a Two-Tensor Addition Runs
Now consider a small computation: two tensors that can be added together. Before reading the result, try to predict what the program prints. Focus on the values first; the device question comes next.
tensor([5, 7, 9])- a and b are two tensors with the same number of elements
- a + b performs element-wise addition
- The values in the output are 1+4, 2+5, and 3+6
What do you think happens?
Before running it, what values does a + b print?
Reveal answer
tensor([5, 7, 9])
Each position is added pairwise: 1 + 4 = 5, 2 + 5 = 7, and 3 + 6 = 9.
Notice what the code does not do: it never names a device. Yet the source states that this computation is carried out on the CPU by default. Because the tensors were created without any move to another device, the addition runs where the tensors already are, which is the default CPU location.
Why Tensors and Operations Share a Device
The source states a specific relationship: a PyTorch tensor resides in a device, and its operations are executed on that same device. This gives two consequences. First, a tensor is never floating in no location; it always sits on some device. Second, when you ask PyTorch to perform an operation on a tensor, that operation runs on the device where the tensor is. The tensor and the work it triggers are tied together.
Read the diagram as a single chain of reasoning. The tensor resides in a device. The operation executes on that same device. Because both are tied to one device, the answer to 'where does this operation run?' is always the answer to 'where does this tensor reside?'. This is why the device concept covers both the arithmetic and the data at once.
Common Mistakes with Device Reasoning
Common mistake Assuming that a True result from torch.cuda.is_available() means every tensor is already on the GPU
Why it failsThe check reports whether GPU computing is available in the current runtime. It is a separate question from where a given tensor resides.
Do this insteadTreat the availability check and the tensor's device as two distinct facts, and inspect each one separately.
Common mistake Assuming that computations move to the GPU automatically once it is available
Why it failsComputations in PyTorch are carried out on the CPU by default, so moving work to a GPU is a deliberate step.
Do this insteadExpect CPU execution unless you explicitly direct the work to a GPU.
Common mistake Assuming the same result on every machine
Why it failsThe check describes the current runtime. A different machine, or the same machine with a different installation, can give a different answer.
Do this insteadRun the check in each environment you work in rather than reusing a result from elsewhere.
Practice: Predicting the Device of an Operation
A program creates two tensors and adds them, without any step that moves them to another device. Using only the rule that a tensor resides in a device and its operations execute on that same device, state where the addition runs by default and explain why.
Hints
- Recall what the source says about where computations are carried out by default.
- Ask where the tensors reside, and what the operation does with that location.
Key Takeaways
- A device is where computations occur and data resides, and both the CPU and the GPU are devices.
torch.cuda.is_available()reports whether GPU computing is available in the current runtime; it does not report where a given tensor resides.- A PyTorch tensor resides in a device, and its operations execute on that same device, so location and computation are linked.
- Computations run on the CPU by default, so moving work to a GPU is a deliberate step.