Python / PyTorch Fundamentals Interview Questions
How do you move tensors and models between CPU and GPU in PyTorch?
PyTorch's device abstraction allows the same code to run on CPU or GPU with minimal changes. The fundamental rule: a model and its input tensors must reside on the same device before any computation, or PyTorch raises a RuntimeError.
import torch import torch.nn as nn # Device-agnostic pattern â always write code this way device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(f"Using device: {device}") # Move a model to the device model = nn.Linear(10, 1).to(device) # Move data to the same device, every batch, inside the loop for X_batch, y_batch in loader: X_batch = X_batch.to(device, non_blocking=True) y_batch = y_batch.to(device, non_blocking=True) pred = model(X_batch) # works â both on same device # WRONG â mismatched devices raises RuntimeError # model_cpu = nn.Linear(10, 1) # stays on CPU # x_gpu = torch.randn(4, 10).to("cuda") # model_cpu(x_gpu) # RuntimeError: Expected all tensors on same device # Checking tensor device t = torch.randn(3) print(t.device) # cpu t_gpu = t.cuda() # or t.to("cuda:0") print(t_gpu.device) # cuda:0 # GPU memory diagnostics if torch.cuda.is_available(): print(torch.cuda.memory_allocated() / 1e9, "GB allocated") print(torch.cuda.max_memory_allocated() / 1e9, "GB peak") torch.cuda.empty_cache() # release unused cached memory # Moving a tensor back to CPU (required before .numpy()) result = t_gpu.cpu().numpy() # numpy() requires a CPU tensor # Apple Silicon (M1/M2/M3) GPU support mps_device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
| Method | Effect |
|---|---|
| tensor.to(device) | Moves to specified device — most flexible, recommended |
| tensor.cuda() | Shorthand for .to('cuda') |
| tensor.cpu() | Moves back to CPU (required before .numpy()) |
| model.to(device) | Moves all model parameters and buffers |
| non_blocking=True | Allows async transfer when paired with pin_memory=True |
More Related questions...