Skip to content

Instantly share code, notes, and snippets.

@changx03
Created July 21, 2023 13:51
Show Gist options
  • Save changx03/b17b0175db8c72670490af5b9d344efd to your computer and use it in GitHub Desktop.
Save changx03/b17b0175db8c72670490af5b9d344efd to your computer and use it in GitHub Desktop.
Testing for GPU availability
import torch
device = torch.device('cuda:0') if torch.cuda.is_available() else 'cpu'
print(f'Using: {device}')
print('GPU Memory Usage:')
print('Allocated:', round(torch.cuda.memory_allocated(0) / 1024**3, 1), 'GB')
print('Cached: ', round(torch.cuda.memory_reserved(0) / 1024**3, 1), 'GB')
# Run a trivial task
x = torch.rand(10).to(device)
x = x.sum().item()
print(f'Sum random numbers from {device}:', x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment