Suppose I perform my_awesome_cuda_training() and I want to determine the maximum amount of memory that was used. Is that as simple as using torch.cuda.memory_stats()?
# pip install pynvml
from pynvml import * # too lazy, not a good thing
def show_gpu(msg):
nvmlInit()
handle = nvmlDeviceGetHandleByIndex(0)
info = nvmlDeviceGetMemoryInfo(handle)
total = info.total
free = info.free
used = info.used
pct = used/total;
print('\n' + msg, f'Used {100*pct:2.1f}% ({used} out of {total} {free}')
The whole point of determining the peak memory usage is that I don’t want do intermediate manually checking. For me it is irrelevant how much memory is used at a specific time. All I want is to determine after my code has run how much memory was used at a maximum, i. e. how much memory is required to run my code.
Yes, the .peak stats will give you the maximum. You can use torch.cuda.reset_peak_memory_stats() to reset this peak if you need to monitor another peak usage.