Moving Models to GPU
You'll learn: How to transfer an entire PyTorch model to GPU in one operation, automatically moving all its parameters and internal state together.
The Core Idea
In the previous lesson, you learned to move individual tensors to GPU. But models contain many components—dozens or hundreds of parameter tensors, plus buffers for things like running statistics in batch normalization. Manually moving each piece would be tedious and error-prone.
Fortunately, PyTorch's nn.Module class provides built-in methods that move everything at once. When you call .to(device) or .cuda() on a model, PyTorch automatically transfers:
- All learnable parameters (weights and biases)
- All registered buffers (non-trainable state like batch norm statistics)
- All nested sub-modules recursively
Think of it like moving your entire office to a new building—you don't carry each pen and paper individually; you pack everything and move it all together.
How It Works
import torch
import torch.nn as nn
# Create a simple model
model = nn.Sequential(
nn.Linear(10, 20),
nn.ReLU(),
nn.Linear(20, 5)
)
# Move entire model to GPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device) # or model.cuda()
# Now all parameters are on GPU
for param in model.parameters():
print(param.device) # cuda:0
The assignment model = model.to(device) isn't strictly necessary (.to() modifies in-place), but it's good practice for clarity and consistency.
Why This Matters
Moving the model once ensures all computations happen on the same device. Your input tensors must still match the model's device, but you avoid the complexity of tracking individual layers.
Key Takeaway: Use .to(device) on your entire model to transfer all parameters and buffers to GPU in one operation, ensuring consistent device placement throughout your network.