I accidentally found this:
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:
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?
id(), at least in practice, gives you the location in memory of the python object.
When you run
id(x.untyped_storage()), a temporary
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
UntypedStorage object is created that happens to occupy the same
memory that was occupied by the just-destroyed
To see the memory where the actual data in tensors
y reside, look
These will be different.
Thank you for your thorough explanation!