Duplicated result when getting cloned Tensor`s data_ptr in list comprehension

username: Allen_Ling
cooked: <p>python3.5, torch 1.3</p>
<pre><code class="lang-auto">In [1]: import torch                                                                                                                                                                         

In [2]: x = torch.randn(1, 2, 3).to("cuda:0")                                                                                                                                                

In [3]: y = torch.randn(1, 2, 4).to("cuda:0")                                                                                                                                                

In [4]: bindings = [x.clone().data_ptr(), y.clone().data_ptr()]                                                                                                                              

In [5]: bindings                                                                                                                                                                             
Out[5]: [140582636749824, 140582636749824]

In [6]: y.clone().data_ptr()                                                                                                                                                                 
Out[6]: 140582636749824


</code></pre>
<p>data_ptrs in bindings are same as <code>y.clone().data_ptr()</code></p>
<p>and further more, clone will allocate a new data_ptr only after assigning cloned object to a var</p>
<p><strong>how`s that happen?</strong></p>
<pre><code class="lang-auto">In [1]: import torch                                                                                                                                                                         

In [2]: x = torch.randn(1, 2, 3).to("cuda:0")                                                                                                                                                
# ========&gt; x.clone().data_ptr() would not change
In [3]: x.clone().data_ptr()                                                                                                                                                                 
Out[3]: 140563644940800

In [4]: x.clone().data_ptr()                                                                                                                                                                 
Out[4]: 140563644940800

# ========&gt; but when assign cloned object to a var, x.clone().data_ptr() change
In [5]: a = x.clone()                                                                                                                                                                        

In [6]: a.data_ptr(), x.clone().data_ptr()                                                                                                                                                   
Out[6]: (140563644940800, 140563644941312)

# ========&gt; assignment and data_ptr change 
In [7]: x.clone().data_ptr()                                                                                                                                                                 
Out[7]: 140563644941312

In [8]: b = x.clone()                                                                                                                                                                        

In [9]: b.data_ptr(), x.clone().data_ptr()                                                                                                                                                   
Out[9]: (140563644941312, 140563644941824)

</code></pre>

username: albanD
cooked: <p>Hi,</p>
<p>This happens because we have a good caching allocator.<br>
When you do <code>x.clone().data_ptr()</code>. You first allocate new memory for the clone, then extract the pointer and delete everything but the value of the pointer. Because cpython is refcounted. The cloned tensor is immediately deleted and so its memory is freed back to the allocator.<br>
When you do <code>y.clone().data_ptr()</code> just after, the same memory space is now free and so the caching allocator returns this space to store the clone of <code>y</code>. Here again, the cloned Tensor will be deleted and only the value of the data pointer will be kept.</p>