Add cuda.reset_max_memory_*#15985
Closed
ssnl wants to merge 1 commit into
Closed
Conversation
stas00
approved these changes
Jan 12, 2019
Contributor
stas00
left a comment
There was a problem hiding this comment.
Validated it to work with the following test case:
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
soumith
approved these changes
Jan 14, 2019
Contributor
facebook-github-bot
left a comment
There was a problem hiding this comment.
@soumith is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
zdevito
pushed a commit
to zdevito/ATen
that referenced
this pull request
Jan 14, 2019
Summary: Addresses #15968 Pull Request resolved: pytorch/pytorch#15985 Differential Revision: D13649916 Pulled By: soumith fbshipit-source-id: a207aea5709a79dba7a6fc541d0a70103f49efff
laurentdupin
pushed a commit
to laurentdupin/pytorch
that referenced
this pull request
Apr 24, 2026
Summary: Addresses pytorch#15968 Pull Request resolved: pytorch#15985 Differential Revision: D13649916 Pulled By: soumith fbshipit-source-id: a207aea5709a79dba7a6fc541d0a70103f49efff
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses #15968