The answer is in two parts. From the documentation of sys.getsizeof, firstly
All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.
so it could be that for tensors __sizeof__ is undefined or defined differently than you would expect - this function is not something you can rely on. Secondly
Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.
which means that if the torch.Tensor object merely holds a reference to the actual memory, this won't show in sys.getsizeof. This is indeed the case, if you check the size of the underlying storage instead, you will see the expected number
import torch, sys
b = torch.randn(1, 1, 128, 256, dtype=torch.float64)
sys.getsizeof(b)
>> 72
sys.getsizeof(b.storage())
>> 262208
Note: I am setting dtype to float64 explicitly, because that is the default dtype in numpy, whereas torch uses float32 by default.
if you want get the size of tensor or network in cuda,you could use this code to calculate it size:
import torch
device = 'cuda:0'
# before
torch._C._cuda_clearCublasWorkspaces()
memory_before = torch.cuda.memory_allocated(device)
# your tensor or network
data5 = torch.randn((10000,100),device=device)
# after
memory_after = torch.cuda.memory_allocated(device)
latent_size = memory_after - memory_before
latent_size
# 4000256
get idea from this:https://github.com/pytorch/pytorch/blob/ee28b865ee9c87cce4db0011987baf8d125cc857/torch/distributed/pipeline/sync/_balance/profile.py#L102
Is there a way to get the underlying memory array size of a tensor?
python - How to find the size of a tensor in bytes? - Data Science Stack Exchange
Get the memory needed to store a tensor in PyTorch - Stack Overflow
Tensor type memory usage
Step 1 :
Get the dtype of the tensor.
This will tell you about the number of bytes e.g.float64 is 64 bits = 8 Bytes.
Step 2
Get the shape of the Tensor.
This will give you the number of place-holders of the dtype.
lets's assume shape = m x n x p
Count of the placeholders is C = m * n * p
Memory = 8 * C => Memory = 8 *m * n * p Bytes.
See this excerpt from Deep Learning with Python, by Francois Chollet
A 60-second, 144 × 256 YouTube video clip sampled at 4 frames per second would have 240 frames. A batch of four such video clips would be stored in a tensor of shape (4, 240, 144, 256, 3). That’s a total of 106,168,320 values! If the dtype of the tensor was float32, then each value would be stored in 32 bits, so the tensor would represent 405 MB
To do this in a line of code, use:
size_in_bytes = encoding.nelement() * encoding.element_size()
This multiplies the number of elements in your tensor by the size of the tensor in bytes, to get the total memory usage of the tensor - not including the Python object overhead, which can be found with sys.getsizeof().
» pip install pytorch-memlab
Hi,
for some scientific research project I'm using a very large matrix with dim(M) = 4 and about 90% sparsity. Therefore I want to use a sparse tensor. Unfortunately I need to use a method calculating the entries of the matrix, which outputs a dense numpy array.
From this numpy array I create a dense PyTorch tensor which is later converted to a sparse tensor with tensor.to_sparse(). Somehow this leads to an error in memory allocation while trying to allocate aprox 300Gb of RAM. This would usually not be a problem as the cluster I'm working on has 512Gb RAM, but due to the fact that I already have the dense tensor, memory allocation is not possible.
Does anyone of you have any idea how to prevent this or how to directly initialize the tensor from the numpy array to be sparse?
Thanks a lot