Concepts / Installing a GPU-Compatible Version of PyTorch

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.

  • Programming

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.

python
Output (expected)
True or False, depending on the runtime you are using
Notes on the code
  • torch.cuda.is_available() returns a boolean for the current runtime
  • The printed value describes this environment, not PyTorch in general
torch.cuda.is_available()Returns True?GPU computingavailableCPU computing bydefaultreportsyesno
What path does the program take when is_available() returns True versus False, and which device ends up being used in each case?

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.

python
Notes on the code
  • The if branch runs only when the check returns True
  • The else branch covers the False case

What a Device Is in PyTorch

Definition

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.

CPUA deviceDefault forcomputationsGPUA deviceRequires a deliberatestep
How do the CPU and GPU differ as places where computation and data reside?

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.

python
Output (expected)
tensor([5, 7, 9])
Notes on the code
  • 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?

  • tensor([5, 7, 9])

  • tensor([3, 6, 9])

  • tensor([4, 5, 6])

Reveal answer
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.

TensorDeviceOperationresides inexecutes oninput to
When a tensor sits on a device and an operation is applied to it, which device runs the operation, and how are the tensor's location and the computation linked?

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 fails

    The check reports whether GPU computing is available in the current runtime. It is a separate question from where a given tensor resides.

    Do this instead

    Treat 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 fails

    Computations in PyTorch are carried out on the CPU by default, so moving work to a GPU is a deliberate step.

    Do this instead

    Expect CPU execution unless you explicitly direct the work to a GPU.

  • Common mistake

    Assuming the same result on every machine

    Why it fails

    The check describes the current runtime. A different machine, or the same machine with a different installation, can give a different answer.

    Do this instead

    Run the check in each environment you work in rather than reusing a result from elsewhere.

Practice: Predicting the Device of an Operation

EASY

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.

Related concepts

More concepts starting with I