Challenge - 5 Problems
GPU Tensor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output device of the tensor?
Consider the following PyTorch code snippet. What device will the tensor be on after execution?
PyTorch
import torch x = torch.tensor([1, 2, 3]) x = x.to('cuda') print(x.device)
Attempts:
2 left
💡 Hint
The .to('cuda') method moves the tensor to the default GPU device, usually cuda:0.
✗ Incorrect
The .to('cuda') call moves the tensor to the first GPU device, which is 'cuda:0'. The print statement shows the exact device.
❓ Predict Output
intermediate2:00remaining
What happens when moving a tensor to a non-existent GPU?
What error will this PyTorch code raise if the machine has only one GPU (cuda:0)?
PyTorch
import torch x = torch.tensor([1, 2, 3]) x = x.to('cuda:1')
Attempts:
2 left
💡 Hint
Check what happens if you specify a GPU index that does not exist.
✗ Incorrect
If the device index is invalid (like cuda:1 on a single GPU machine), PyTorch raises a RuntimeError about invalid device ordinal.
❓ Model Choice
advanced2:00remaining
Which code correctly moves a model and its input tensor to GPU?
You want to run a PyTorch model on GPU. Which option correctly moves both the model and input tensor to the GPU?
Attempts:
2 left
💡 Hint
Both model and input must be on the same device for computation.
✗ Incorrect
To run on GPU, both model and input must be moved to the GPU device using .cuda() or .to('cuda'). Option C does this correctly.
❓ Hyperparameter
advanced2:00remaining
What is the effect of using .to(device) with a variable device?
Given device = torch.device('cuda' if torch.cuda.is_available() else 'cpu'), what does model.to(device) do?
Attempts:
2 left
💡 Hint
The device variable depends on GPU availability.
✗ Incorrect
The device variable is set to 'cuda' if GPU is available, else 'cpu'. model.to(device) moves model accordingly.
🔧 Debug
expert2:00remaining
Why does this code raise a RuntimeError about device mismatch?
Examine the code below and select the reason for the RuntimeError: Expected all tensors to be on the same device.
PyTorch
import torch model = torch.nn.Linear(2, 2) input = torch.tensor([[1.0, 2.0]]).to('cuda') output = model(input)
Attempts:
2 left
💡 Hint
Check where the model and input tensors are located.
✗ Incorrect
The model is on CPU by default, but input is moved to GPU. This causes a device mismatch error during forward pass.