Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytorch: Why is the memory occupied by the `tensor` variable so small?

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.

like image 599
laridzhang Avatar asked Jan 25 '19 08:01

laridzhang


People also ask

Why does PyTorch use so much memory?

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.

How do you increase the size of the PyTorch tensor?

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.

How do you find the memory size of tensor PyTorch?

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.

How tensors are stored in memory?

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.


1 Answers

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.

like image 167
Jatentaki Avatar answered Sep 29 '22 04:09

Jatentaki