I’m unsure what the actual issue is. Are you trying to mask specific GPUs and this is not working correctly using the os.environ call? If so, set CUDA_VISIBLE_DEVICES in the terminal (either as part of the actual command or export it).
Or are you making all GPUs visible and cannot use any other device?
In that case, what does x = torch.randn(1, device='cuda:1') return and what does nvidia-smi show?