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 |
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
