Created
January 12, 2019 18:32
-
-
Save stas00/30cdd583d13b205f28aa2c239c088ace to your computer and use it in GitHub Desktop.
pytorch tracemalloc.get_traced_memory equivalent (for this to work need to wait for https://github.com/pytorch/pytorch/pull/15985 to be merged and then it'll probably appear in pytorch 1.0.1)
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
import torch | |
def consume_gpu_ram(n): return torch.ones((n, n)).cuda() | |
def consume_gpu_ram_256mb(): return consume_gpu_ram(2**13) | |
def b2mb(x): return int(x/2**20) | |
class TorchTracemalloc(): | |
def __enter__(self): | |
self.begin = torch.cuda.memory_allocated() | |
torch.cuda.reset_max_memory_allocated() # reset the peak gauge to zero | |
return self | |
def __exit__(self, *exc): | |
self.end = torch.cuda.memory_allocated() | |
self.peak = torch.cuda.max_memory_allocated() | |
self.used = b2mb(self.end-self.begin) | |
self.peaked = b2mb(self.peak-self.begin) | |
print(f"delta used/peak {self.used:4d}/{self.peaked:4d}") | |
# push the process' peak gauge high up and then release all the memory | |
# expecting 0 used / 1024 peaked | |
with TorchTracemalloc() as tt: | |
z = [consume_gpu_ram_256mb() for i in range(4)] # 1GB | |
del z | |
assert tt.used == 0 and tt.peaked == 1024 | |
# allocate, allocate, release half | |
# expecting 256 used / 512 peaked | |
with TorchTracemalloc() as tt: | |
# should be: 256 used, 512 peaked | |
c1 = consume_gpu_ram_256mb() | |
c2 = consume_gpu_ram_256mb() | |
del c1 | |
assert tt.used == 256 and tt.peaked == 512 | |
del c2 # reset for next test | |
# allocate, allocate, release all | |
# expecting 0 used / 512 peaked | |
with TorchTracemalloc() as tt: | |
# should be: 0 used, 512 peaked | |
c1 = consume_gpu_ram_256mb() | |
c2 = consume_gpu_ram_256mb() | |
del c1, c2 | |
assert tt.used == 0 and tt.peaked == 512 | |
# allocate, don't release | |
# expecting 1536 used / 1536 peaked | |
with TorchTracemalloc() as tt: | |
z = [consume_gpu_ram_256mb() for i in range(6)] | |
assert tt.used == 1536 and tt.peaked == 1536 | |
del z # reset for next test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment