Id(untyped_storage()) returns the same for different input tensor, why?

I accidentally found this:

import torch
x = torch.rand(10)
y = torch.rand(10)
print(id(x.untyped_storage()) == id(y.untyped_storage()))  # output (why?): True

Further more, if I do this repetitively:

print(id(x.untyped_storage()), id(y.untyped_storage()))

I find that it will print two same numbers every time, but the number keeps changing per each run. What is happening behind the scene?

Hi Fei!

id(), at least in practice, gives you the location in memory of the python object.

When you run id(x.untyped_storage()), a temporary UntypedStorage
object is created. It lives long enough for its id() to be taken, but when it
goes out of scope (which is after the call to id()), it is destroyed and its
memory is reclaimed. Then when you run id(y.untyped_storage()), a
new UntypedStorage object is created that happens to occupy the same
memory that was occupied by the just-destroyed x.untyped_storage().

To see the memory where the actual data in tensors x and y reside, look
at x.untyped_storage().data_ptr() and y.untyped_storage().data_ptr().
These will be different.

Best.

K. Frank

1 Like

Thank you for your thorough explanation!