Why did I get the same two ids when using id() function on two different indexes of a pytorch tensor?

shelvey_jiang: <pre><code class="lang-auto">a = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(id(a[0]))
print(id(a[1]))
</code></pre>
<p>When I execute the code aboce, I get the following same two results:</p>
<pre><code class="lang-auto">2203226175040
2203226175040
</code></pre>
<p>I do know that two objects with non-overlapping lifetimes may have the same id() value, but shouldn’t these two tensors a[0] and a[1] exist together?</p>

Matias_Vasquez: <p>This is well explained in this answer.</p>
<aside class="quote" data-post="4" data-topic="97570">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/ba8739/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/id-function-of-tensor-changes-all-the-time/97570/4">Id function of tensor changes all the time</a> 
  </div>
  <blockquote>
    You’re using id() on wrapper objects, that are created on the fly. E.g. when you do a[100000] you get a new object that shares storage with “a”, but also records storage offset. Repeated call a[100000] creates a new wrapper, as it is impossible to cache these.
  </blockquote>
</aside>

<p>So when you do this ↓ you might get the same wrapper and just update the offset. This is not always the case. If you run your code multiple times, you will see that sometimes you get different addresses.</p>
<aside class="quote no-group" data-username="shelvey_jiang" data-post="1" data-topic="150469">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/shelvey_jiang/48/45998_2.png" class="avatar"> shelvey_jiang:</div>
<blockquote>
<pre><code class="lang-auto">a = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(id(a[0]))
print(id(a[1]))
</code></pre>
</blockquote>
</aside>
<p>As mentioned in the previous link, what remains stable is the address of tensor´s storage, which can be accessed with <code>a.storage().data_ptr()</code>.</p>
<pre data-code-wrap="python"><code class="lang-python"># Here we are referencing a new wrapper that shares memory with 'a'
a = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(a[0].data_ptr())
print(a[1].data_ptr())
# Output:
# That is why we get two different results
# a[0]: 114012224
# a[1]: 114012248
</code></pre>
<pre data-code-wrap="python"><code class="lang-python"># Here we are referencing where the actual tensor is stored
a = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(a[0].storage().data_ptr())
print(a[1].storage().data_ptr())
# Output:
# a[0]: 114011904
# a[1]: 114011904
</code></pre>
<p>But now, when we use <a href="https://pytorch.org/docs/stable/generated/torch.Tensor.data_ptr.html" rel="noopener nofollow ugc">data_ptr</a> we get the address of the <strong>first</strong> element of the tensor. Since it is the same tensor, the first element must be the same.</p>
<p>Here are some other useful answers</p>
<p><a href="https://discuss.pytorch.org/t/why-is-id-tensors-storage-different-every-time/100478">https://discuss.pytorch.org/t/why-is-id-tensors-storage-different-every-time/100478</a></p>
<p><a href="https://discuss.pytorch.org/t/any-way-to-check-if-two-tensors-have-the-same-base/44310/8">https://discuss.pytorch.org/t/any-way-to-check-if-two-tensors-have-the-same-base/44310/8</a></p>