deep-learning-with-pytorch / dlwpt-code

Code for the book Deep Learning with PyTorch by Eli Stevens, Luca Antiga, and Thomas Viehmann.
https://www.manning.com/books/deep-learning-with-pytorch
4.69k stars 1.98k forks source link

p1ch3, id(points.storage()) == id(points_t.storage()) out:False on Colab #109

Closed katychou closed 1 year ago

katychou commented 1 year ago

points_t = points.t() #.t:transpose id(points.storage()) == id(points_t.storage()) out:False on Colab

t-vi commented 1 year ago

Yeah, this bit in the book is, unfortunately, bogus, as the interface between Python and C++ is delicate here: While points and points_t share the same C++ storage object, the Python object is (currently) newly created by PyTorch each time you call .storage(). As a result, you would not even get the same id except through reuse for the same thing:

a = points.storage()
b = points.storage()  # same tensor(!)
print(id(a), id(b))

will give different ids (as of PyTorch <= 2.0, there always are plans to do this differently, but I would not know the timeline).

What is the same is the points.data_ptr() and points_t.data_ptr() pointing to the memory region of the tensor's storage and also points.storage()._cdata and points_t.storage()._cdata which points to the C++ storage object wrapped by the Python storage(s).

katychou commented 1 year ago

Hi @t-vi , Thank you for answer, I try 1.pointa.storage().data_ptr() == points_a1.storage().data_ptr() 2.pointa.data_ptr() == points_a1.data_ptr() 3.pointa.storage()._cdata == points_a1.storage()._cdata the above three ways are True,, 1and2 are same addr., but 3 different addr.

Do you Have relationship images of the 3 storage(id(pointa.storage()) & data_ptr & _cdata) ?

t-vi commented 1 year ago

It's a bit involved, but here: The data_ptr is always a pointer to the raw data while the _cdata gives the C++ object corresponding to / underpinning the Python object (though with Tensor's there is a 1-1 relation for C++ vs Python objects, while for Storage there is not).

image