The code snippet shows different initializations for CPU and CUDA tensors, not only for the GPU.
Alternatively to using this condition, you could also write device-agnostic code and call .to(device) on the newly created tensors.