In Pytorch 1.0.0, I found that a tensor
variable occupies very small memory. I wonder how it stores so much data.
Here's the code.
a = np.random.randn(1, 1, 128, 256)
b = torch.tensor(a, device=torch.device('cpu'))
a_size = sys.getsizeof(a)
b_size = sys.getsizeof(b)
a_size
is 262288. b_size
is 72.
If you are seeing an increase in the allocated memory usage, you are most likely storing tensors in e.g. a list , which are still attached to the computation graph, so you would need to detach() them assuming you won't want to backpropagate through them anymore.
We can resize the tensors in PyTorch by using the view() method. view() method allows us to change the dimension of the tensor but always make sure the total number of elements in a tensor must match before and after resizing tensors.
For each tensor, you have a method element_size() that will give you the size of one element in byte. And a function nelement() that returns the number of elements. So the size of a tensor a in memory (cpu memory for a cpu tensor and gpu memory for a gpu tensor) is a. element_size() * a.
The commonly used way to store such data is in a single array that is laid out as a single, contiguous block within memory. More concretely, a 3x3x3 tensor would be stored simply as a single array of 27 values, one after the other.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With