Created
July 21, 2023 13:51
-
-
Save changx03/b17b0175db8c72670490af5b9d344efd to your computer and use it in GitHub Desktop.
Testing for GPU availability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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