It’s easier to use the flag CUDA_VISIBLE_DEVICES=‘1’ rather than coding using set_device.
If you need that it means you are working with a single gpu.
You can call .cuda(idx) which allocates in the gpu you want if you use several gpus.