Torch c++ index_put doesn't inplace change the tensor

xiesc: <pre><code class="lang-auto">torch::Tensor a = torch::ones({10}, torch::TensorOptions().dtype(torch::kI64));
torch::Tensor b = torch::zeros({3}, torch::TensorOptions().dtype(torch::kI64));
torch::Tensor torchIdx_test = at::tensor({0,2,7}).toType(torch::kInt64);
a.index_put({torchIdx_test}, b);
std::cout &lt;&lt; a&lt;&lt;std::endl;
</code></pre>
<p>but the result is not as expect</p>
<pre><code class="lang-auto"> 1
 1
 1
 1
 1
 1
 1
 1
 1
 1
[ CPULongType{10} ]
</code></pre>
<p>however</p>
<pre><code class="lang-auto">a = a.index_put({torchIdx_test}, b);
</code></pre>
<p>it will work as expectation</p>
<pre><code class="lang-auto"> 0
 1
 0
 1
 1
 1
 1
 0
 1
 1
[ CPULongType{10} ]
</code></pre>
<p>I wonder is there any in-place operation?  If not, is “=” operation will do real copy of memory?</p>
<p>Btw, I also tried operation like “a.index() = b”. it didn’t work either.</p>

eqy: <p>I believe this is because a tuple or something iterable of tensors is expected as according to the documentation:<br>
<a href="https://pytorch.org/docs/stable/generated/torch.Tensor.index_put_.html#torch.Tensor.index_put_" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.Tensor.index_put_.html#torch.Tensor.index_put_</a><br>
e.g., in the Python side</p>
<pre><code class="lang-auto">import torch

a = torch.ones(10, dtype=torch.long)
b = torch.zeros(3, dtype=torch.long)
torchIdx_test = torch.tensor([0, 2, 7], dtype=torch.long)
print(a.index_put(torchIdx_test, b))
</code></pre>
<p>yields</p>
<pre><code class="lang-auto">TypeError: index_put(): argument 'indices' (position 1) must be tuple of Tensors, not Tensor
</code></pre>
<p>but</p>
<pre><code class="lang-auto">import torch

a = torch.ones(10, dtype=torch.long)
b = torch.zeros(3, dtype=torch.long)
torchIdx_test = torch.tensor([0, 2, 7], dtype=torch.long)
print(a.index_put((torchIdx_test,), b))
</code></pre>
<p>will work as expected.<br>
If you want an in-place version, call <code>index_put_</code>:</p>
<pre><code class="lang-auto">import torch

a = torch.ones(10, dtype=torch.long)
b = torch.zeros(3, dtype=torch.long)
torchIdx_test = torch.tensor([0, 2, 7], dtype=torch.long)
a.index_put_((torchIdx_test,), b)
print(a)
</code></pre>
<pre><code class="lang-auto">tensor([0, 1, 0, 1, 1, 1, 1, 0, 1, 1])
</code></pre>

xiesc: <p>Thanks for replay.<br>
In c++, the input type of index is</p>
<pre><code class="lang-auto">const c10::List&lt;c10::optional&lt;at::Tensor&gt;&gt; &amp; indices
</code></pre>
<p>List is like tuple as you said. So I call the initialization function using {}, e.g. {torchIdx_test}. But still didn’t work.</p>

eqy: <p>Sorry, I misunderstood the two examples given in the question. You would need to call <code>index_put_</code> for the in-place version rather than <code>index_put</code>.</p>

xiesc: <p><code>index_put_</code> works, It solved my problem! really appreciate that!<br>
And there is still something weird.<br>
When I tried</p>
<pre><code class="lang-auto">a.index({torchIdx_test}) = b;
std::cout &lt;&lt; a&lt;&lt;std::endl;
</code></pre>
<p>I got</p>
<pre><code class="lang-auto"> 1
 1
 1
 1
 1
 1
 1
 1
 1
 1
[ CPULongType{10} ]
</code></pre>
<p>but if I try</p>
<pre><code class="lang-auto">a.index({Slice(0,3)}) = b;
std::cout &lt;&lt; a&lt;&lt;std::endl;
</code></pre>
<p>the result will be as expected</p>
<pre><code class="lang-auto"> 0
 0
 0
 1
 1
 1
 1
 1
 1
 1
[ CPULongType{10} ]
</code></pre>
<p>Do you have any ideas?</p>