PyTorch Certified Associate (PTCA) free question 4 / 10 · Domain 1: PyTorch Fundamentals
Which expression fails according to the torch.device documentation?
- torch.ones(()).cuda() + torch.ones(()), a GPU scalar with a CPU scalar
- torch.ones(()) + torch.ones(1).cuda(), a CPU scalar with a GPU vector
- torch.ones(()) + torch.ones(()), a CPU scalar with a CPU scalar
- torch.ones(()).cuda() + torch.ones(1), a GPU scalar with a CPU vector
Show the answer and explanation
Answer: D
Why this is correct
The documentation states that tensors are never moved automatically between devices and require an explicit call from the user. The only exception is a scalar tensor, with dim() equal to 0, which is transferred automatically from CPU to GPU when needed because that can be done for free. A GPU scalar combined with a CPU vector does not fit the exception: the scalar is not transferred from GPU to CPU, and the non-scalar vector is not transferred from CPU to GPU. The documentation shows both orders of this case failing.
Why the other options are wrong
"torch.ones(()) + torch.ones(1).cuda(), a CPU scalar with a GPU vector" … The documentation marks this as OK. Scalar tensors are the one exception to the rule on explicit moves: a CPU scalar is transferred to the GPU when needed.
"torch.ones(()) + torch.ones(()), a CPU scalar with a CPU scalar" … Both operands are on the same device, so no transfer is involved. The failing cases involve a tensor on the GPU and one on the CPU.
"torch.ones(()).cuda() + torch.ones(()), a GPU scalar with a CPU scalar" … The documentation lists two scalars as OK in either order: the CPU scalar is transferred to the GPU.
Where people trip
The exception runs in one direction only, CPU scalar to GPU. Mixed-device arithmetic with a non-scalar needs an explicit .to() or .cuda().