Are you referring to tensor.size().numel()
?
If so, I imagine it refers to the size of the view of the underlaying data of a tensor. I am still looking for a way to get the exact allocated memory of the underlaying data of a tensor
.
BUT, you can compute it yourself if you allocated the tensor yourself, or you know the size of the underlaying data. Pretty much multiply the number of elements originally allocated, by the size of each element, and round up by 512
. I did some tests, and for some reason, pytorch allocates blocks of 512 bytes
. The reserved memory increases by 2 MB
.
For instance,
import torch
import math
from scripts.utils.memory import print_memory_info
def ceiling(x, factor=512):
return math.ceil(x/factor)*factor
device = torch.device('cuda')
print_memory_info()
print(f'diff in memory_allocated will be => ceiling(64*4, 512) = {ceiling(64*4)}')
x1 = torch.randint(16, size=(64, ), dtype=torch.float32, device=device)
print_memory_info()
print(f'diff in memory_allocated will be => ceiling(128*4, 512) = {ceiling(128*4)}')
x2 = torch.randint(16, size=(128, ), dtype=torch.float32, device=device)
print_memory_info()
print(f'diff in memory_allocated will be => ceiling(200*4, 512) = {ceiling(200*4)}')
x3 = torch.randint(16, size=(200, ), dtype=torch.float32, device=device)
print_memory_info()
print(f'diff in memory_allocated will be => ceiling(256*4, 512) = {ceiling(256*4)}')
x4 = torch.randint(16, size=(256, ), dtype=torch.float32, device=device)
print_memory_info()
memory.py
import torch
_cache_ = {
'memory_allocated': 0,
'max_memory_allocated': 0,
'memory_reserved': 0,
'max_memory_reserved': 0,
}
def _get_memory_info(info_name, unit):
tab = '\t'
if info_name == 'memory_allocated':
current_value = torch.cuda.memory.memory_allocated()
elif info_name == 'max_memory_allocated':
current_value = torch.cuda.memory.max_memory_allocated()
elif info_name == 'memory_reserved':
tab = '\t\t'
current_value = torch.cuda.memory.memory_reserved()
elif info_name == 'max_memory_reserved':
current_value = torch.cuda.memory.max_memory_reserved()
else:
raise ValueError()
divisor = 1
if unit.lower() == 'kb':
divisor = 1024
elif unit.lower() == 'mb':
divisor = 1024*1024
elif unit.lower() == 'gb':
divisor = 1024*1024*1024
else:
raise ValueError()
diff_value = current_value - _cache_[info_name]
_cache_[info_name] = current_value
return f"{info_name}: \t {current_value} ({current_value/divisor:.3f} {unit.upper()})" \
f"\t diff_{info_name}: {diff_value} ({diff_value/divisor:.3f} {unit.upper()})"
def print_memory_info(unit='kb'):
print(_get_memory_info('memory_allocated', unit))
print(_get_memory_info('max_memory_allocated', unit))
print(_get_memory_info('memory_reserved', unit))
print(_get_memory_info('max_memory_reserved', unit))
print('')
Output
memory_allocated: 0 (0.000 KB) diff_memory_allocated: 0 (0.000 KB)
max_memory_allocated: 0 (0.000 KB) diff_max_memory_allocated: 0 (0.000 KB)
memory_reserved: 0 (0.000 KB) diff_memory_reserved: 0 (0.000 KB)
max_memory_reserved: 0 (0.000 KB) diff_max_memory_reserved: 0 (0.000 KB)
diff in memory_allocated will be => ceiling(64*4, 512) = 512
memory_allocated: 512 (0.500 KB) diff_memory_allocated: 512 (0.500 KB)
max_memory_allocated: 512 (0.500 KB) diff_max_memory_allocated: 512 (0.500 KB)
memory_reserved: 2097152 (2048.000 KB) diff_memory_reserved: 2097152 (2048.000 KB)
max_memory_reserved: 2097152 (2048.000 KB) diff_max_memory_reserved: 2097152 (2048.000 KB)
diff in memory_allocated will be => ceiling(128*4, 512) = 512
memory_allocated: 1024 (1.000 KB) diff_memory_allocated: 512 (0.500 KB)
max_memory_allocated: 1024 (1.000 KB) diff_max_memory_allocated: 512 (0.500 KB)
memory_reserved: 2097152 (2048.000 KB) diff_memory_reserved: 0 (0.000 KB)
max_memory_reserved: 2097152 (2048.000 KB) diff_max_memory_reserved: 0 (0.000 KB)
diff in memory_allocated will be => ceiling(200*4, 512) = 1024
memory_allocated: 2048 (2.000 KB) diff_memory_allocated: 1024 (1.000 KB)
max_memory_allocated: 2048 (2.000 KB) diff_max_memory_allocated: 1024 (1.000 KB)
memory_reserved: 2097152 (2048.000 KB) diff_memory_reserved: 0 (0.000 KB)
max_memory_reserved: 2097152 (2048.000 KB) diff_max_memory_reserved: 0 (0.000 KB)
diff in memory_allocated will be => ceiling(256*4, 512) = 1024
memory_allocated: 3072 (3.000 KB) diff_memory_allocated: 1024 (1.000 KB)
max_memory_allocated: 3072 (3.000 KB) diff_max_memory_allocated: 1024 (1.000 KB)
memory_reserved: 2097152 (2048.000 KB) diff_memory_reserved: 0 (0.000 KB)
max_memory_reserved: 2097152 (2048.000 KB) diff_max_memory_reserved: 0 (0.000 KB)