Pytorch c++ at::sum function error

jack_ma: <p>Hi!<br>
My code raises the error</p>
<pre><code class="lang-auto">src/local_relation_cuda.cpp:290:147: error: call of overloaded ‘sum(&lt;brace-enclosed initializer list&gt;)’ is ambiguous
      pos_weight_grad = pos_weight_grad + sim_grad_buffer.view({cur_batch_step, num_group, kernel_height, kernel_width, height * width}).sum({0, 4});
                                                                                                                                                   ^
In file included from /data/home/v-jining/anaconda3/lib/python3.7/site-packages/torch/include/ATen/Tensor.h:12:0,
                 from /data/home/v-jining/anaconda3/lib/python3.7/site-packages/torch/include/ATen/Context.h:4,
                 from /data/home/v-jining/anaconda3/lib/python3.7/site-packages/torch/include/ATen/ATen.h:5,
                 from /data/home/v-jining/anaconda3/lib/python3.7/site-packages/torch/include/torch/csrc/api/include/torch/types.h:3,
                 from /data/home/v-jining/anaconda3/lib/python3.7/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader_options.h:4,
                 from /data/home/v-jining/anaconda3/lib/python3.7/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader/base.h:3,
                 from /data/home/v-jining/anaconda3/lib/python3.7/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader/stateful.h:3,
                 from /data/home/v-jining/anaconda3/lib/python3.7/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader.h:3,
                 from /data/home/v-jining/anaconda3/lib/python3.7/site-packages/torch/include/torch/csrc/api/include/torch/data.h:3,
                 from /data/home/v-jining/anaconda3/lib/python3.7/site-packages/torch/include/torch/csrc/api/include/torch/all.h:4,
                 from /data/home/v-jining/anaconda3/lib/python3.7/site-packages/torch/include/torch/extension.h:4,
                 from src/local_relation_cuda.cpp:2:
/data/home/v-jining/anaconda3/lib/python3.7/site-packages/torch/include/ATen/core/TensorMethods.h:2692:15: note: candidate: at::Tensor at::Tensor::sum(c10::IntArrayRef, bool, c10::optional&lt;c10::ScalarType&gt;) const
 inline Tensor Tensor::sum(IntArrayRef dim, bool keepdim, c10::optional&lt;ScalarType&gt; dtype) const {
               ^
/data/home/v-jining/anaconda3/lib/python3.7/site-packages/torch/include/ATen/core/TensorMethods.h:2703:15: note: candidate: at::Tensor at::Tensor::sum(at::DimnameList, bool, c10::optional&lt;c10::ScalarType&gt;) const
 inline Tensor Tensor::sum(DimnameList dim, bool keepdim, c10::optional&lt;ScalarType&gt; dtype) const {
               ^
error: command 'gcc' failed with exit status 1
</code></pre>
<p>And I find two at::sum definitions:</p>
<pre><code class="lang-cpp">static Tensor at::sum(const Tensor &amp;self, IntArrayRef dim, bool keepdim = false, c10::optional&lt;ScalarType&gt; dtype = c10::nullopt)
static Tensor at::sum(const Tensor &amp;self, DimnameList dim, bool keepdim = false, c10::optional&lt;ScalarType&gt; dtype = c10::nullopt)
</code></pre>
<p>I want to know the difference between the two overloaded functions. And what is the difference between IntArrayRef and DimnameList? How to prevent the issue “call of overloaded ‘sum()’ is ambiguous”?<br>
Thank you!</p>

albanD: <p>Hi,</p>
<p>The difference is that one is used when you pass the dimension as an integer. While the other one is used when you use Tensors with named dimensions and so you pass the dimension as a string.</p>

jack_ma: <p>So why when I call <code>tensor.sum({0, 4})</code> it raises ambiguous error?</p>

jack_ma: <p>Could you give me an example of how to call the two functions explicitly without ambiguous?<br>
Thanks for your reply!</p>

albanD: <p>Not sure as I don’t use the cpp API a lot, but does something like <code>.sum(c10::IntArrayRef({0, 4}))</code> work?</p>
<p>cc <a class="mention" href="/u/yf225">@yf225</a> is this expected behavior?</p>

yf225: <p>The ambiguity between <code>IntArrayRef</code> and <code>DimnameList</code> when a braced init-list is passed is unfortunate and we should find a way to fix it. Right now the best workaround is to pass in a <code>std::vector&lt;int64_t&gt;({0, 4})</code> instead, as <code>c10::IntArrayRef</code> doesn’t own the underlying memory which could cause issues in some cases.</p>