GPU support
- Machine Learning course
- Advanced
- Lesson 11/78
- 5 min
PyTorch moves tensors to GPU for fast computation.
GPU support
Deep learning's speed comes from doing matrix multiplications in parallel on the GPU. In PyTorch a tensor or model is moved to the GPU with .to(device). Code is written so it falls back to the CPU if there is no GPU:
cihaz = "cuda" if torch.cuda.is_available() else "cpu"
x = torch.randn(1000, 1000).to(cihaz)
model = model.to(cihaz)
y = model(x) # computed on the chosen deviceOn Apple chips the "mps" device is used: torch.backends.mps.is_available().
Device patterns
In the training loop each batch is moved to the same device before being given to the model. To turn results into NumPy, you must first go back to the CPU:
for xb, yb in yukleyici:
xb, yb = xb.to(cihaz), yb.to(cihaz)
cikti = model(xb)
tahmin = cikti.argmax(dim=1).cpu().numpy() # GPU -> CPU -> NumPyCopying between GPU and CPU is slow; needless .cpu() calls inside the loop are avoided.
Practice Device-independent code
Choose the device with "cuda" if torch.cuda.is_available() else "cpu". Move x = torch.ones(2, 3) to the device, multiply by 3, bring the result back with .cpu() and print it with tolist(). Then print that the result's device.type is "cpu".
Starter code
import torch
# buraya yazSample solution
import torch
cihaz = "cuda" if torch.cuda.is_available() else "cpu"
x = torch.ones(2, 3).to(cihaz)
sonuc = (x * 3).cpu()
print(sonuc.tolist())
print(sonuc.device.type == "cpu")Expected output
[[3.0, 3.0, 3.0], [3.0, 3.0, 3.0]] True
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What happens if you add two tensors, one on the CPU and one on the GPU?
- It is moved automatically
- The result is on the CPU
- It works fine
- It errors; both must be on the same device Correct answer
Explanation: All tensors in an operation and the model must be on the same device.
2 Move the tensor to the chosen device.
x = x.____(cihaz)
x = x.to(cihaz)
Explanation: .to() can change both device and type.
3 Match the expressions.
| Term | Meaning |
|---|---|
torch.cuda.is_available() |
Is there a GPU? |
.to(cihaz) |
Move to device |
.cpu() |
Back to the CPU |
"mps" |
Apple GPU |