Created
June 24, 2019 18:09
-
-
Save lebedov/12dc7623656c32281383a4a60b5b3a94 to your computer and use it in GitHub Desktop.
Compute total memory consumed by PyTorch tensors
This file contains 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
#!/usr/bin/env python3 | |
""" | |
Compute total memory consumed by PyTorch tensors. | |
""" | |
import gc | |
import torch | |
def get_used_mem(): | |
host_mem = 0 | |
gpu_mem = 0 | |
for obj in gc.get_objects(): | |
if torch.is_tensor(obj): | |
mem = obj.numel()*obj.element_size() | |
if obj.is_cuda: | |
gpu_mem += mem | |
else: | |
host_mem += mem | |
return host_mem, gpu_mem | |
if __name__ == '__main__': | |
host_mem, gpu_mem = get_used_mem() | |
print(host_mem, gpu_mem) | |
x = torch.empty((10, 10), dtype=torch.float32) | |
host_mem, gpu_mem = get_used_mem() | |
print(host_mem, gpu_mem) | |
y = torch.empty((10, 10), dtype=torch.float32).to('cuda:0') | |
host_mem, gpu_mem = get_used_mem() | |
print(host_mem, gpu_mem) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment