Skip to content

Instantly share code, notes, and snippets.

@SupreethRao99
Created September 6, 2022 06:29
Show Gist options
  • Save SupreethRao99/b06609a5fd479f6627ae1d2308d5e694 to your computer and use it in GitHub Desktop.
Save SupreethRao99/b06609a5fd479f6627ae1d2308d5e694 to your computer and use it in GitHub Desktop.
PyTorch GPU Helper

Moving to a specific GPU

cuda = torch.device('cuda')     # Default CUDA device, In our case it's Nvidia RTX 3090 
cuda0 = torch.device('cuda:0')  # specifies the CUDA device to be NVIDIA GeForce RTX 3090
cuda1 = torch.device('cuda:1')  # specifies the CUDA device to be NVIDIA GeForce RTX 3080

Example:
  x = torch.tensor([1., 2.], device=cuda0)
  # x.device is device(type='cuda', index=0)

Printing Avaliable GPU's

available_gpus = [
        torch.cuda.get_device_name(i) for i in range(torch.cuda.device_count())
    ]
print(avaliable_gpus)

References

https://pytorch.org/docs/stable/notes/cuda.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment