Terminate called after throwing an instance of 'c10::IndexError' what(): index 1701270418 is out of bounds for dimension 0 with size 500

Ramansh_Sharma: <p>Hi,</p>
<p>I am to trying to train a model in which I am updating only some indices of a PyTorch tensor with the evaluations of a network. For this, I am using the</p>
<p><code>output_tensor.index_put_({torch::from_blob(indices.data(), {indices.size()}, torch::kInt32)}, output_tensor.index({torch::from_blob(indices.data(), {indices].size()}, torch::kInt32)}) + networks-&gt;forward(points_tensor));</code></p>
<p>Where <code>output_tensor</code> is a tensor with some shape Nx1, <code>indices</code> is a <code>std::vector&lt;int&gt;</code> with the indices of the <code>output_tensor</code> I want to add the network prediction to. I keep getting this error:</p>
<p><code>terminate called after throwing an instance of 'c10::IndexError'   what():  index 1701270418 is out of bounds for dimension 0 with size 500</code></p>
<p>Where the really large integer index out of bound changes to a different large integer every time I run the code. Can anyone help me debug this?</p>
<p>Thank you.</p>

Ramansh_Sharma: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>. Do you know what might be causing this error?</p>
<p>Thank you.</p>

ptrblck: <p>The underlying data is most likely freed and the tensor thus contains garbage. <code>.clone()</code> the tensor before <code>indices</code> is deleted to make sure the values are valid.</p>

Ramansh_Sharma: <p>Thank you so much for your reply <a class="mention" href="/u/ptrblck">@ptrblck</a>. I tried cloning the tensor but the error persists. This is what I changed the code to:</p>
<p><code>output_tensor.index_put_({torch::from_blob(indices.data(), {indices.size()}, torch::kInt32).clone()}, output_tensor.index({torch::from_blob(indices.data(), {indices].size()}, torch::kInt32).clone()}) + networks-&gt;forward(points_tensor));</code></p>
<p>It is worth mentioning that the forward pass of this code goes perfectly smoothly. Only the backward pass has a problem. Could it be that the in place <code>index_put_</code> operation is not backward compatible? In which case I should do something like this,</p>
<pre><code class="lang-auto">output_tensor.index(torch::from_blob(indices.data(), {indices.size()}, torch::kInt32)) = output_tensor.index(torch::from_blob(indices.data(), {indices.size()}, torch::kInt32)) + network-&gt;forward(points_tensor);
</code></pre>

ptrblck: <aside class="quote no-group" data-username="Ramansh_Sharma" data-post="4" data-topic="190516">
<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/ramansh_sharma/48/55510_2.png" class="avatar"> Ramansh_Sharma:</div>
<blockquote>
<p>Could it be that the in place <code>index_put_</code> operation is not backward compatible?</p>
</blockquote>
</aside>
<p>“Backward compatible” refers to software versioning, but I assume you are asking about the support of the backward pass? If so, <code>index_put_</code> won’t break the backward pass and the error still sounds as if a tensor/data is going out of scope. You could write a quick check in Python, which should work also in the backward pass.</p>

Ramansh_Sharma: <p>Ah yes, sorry, I meant as in the backward pass. I will try writing a quick check in Python. Thank you.</p>