File size: 537 Bytes
2bfd19c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import torch
def get_tensors_memory_usage(tensor_list, unit='MB'):
total_bytes = 0
for t in tensor_list:
if isinstance(t, torch.Tensor) and t.is_cuda:
total_bytes += t.element_size() * t.numel()
unit = unit.upper()
scale_dict = {
'B': 1,
'KB': 1024,
'MB': 1024 ** 2,
'GB': 1024 ** 3,
}
if unit not in scale_dict:
raise ValueError(f"Unsupported unit: {unit}. Use 'B', 'KB', 'MB', or 'GB'.")
scale = scale_dict[unit]
return total_bytes / scale |