Pytorch c++ at::sum function error

Hi!
My code raises the error

src/local_relation_cuda.cpp:290:147: error: call of overloaded ‘sum(<brace-enclosed initializer list>)’ 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<c10::ScalarType>) const
 inline Tensor Tensor::sum(IntArrayRef dim, bool keepdim, c10::optional<ScalarType> 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<c10::ScalarType>) const
 inline Tensor Tensor::sum(DimnameList dim, bool keepdim, c10::optional<ScalarType> dtype) const {
               ^
error: command 'gcc' failed with exit status 1

And I find two at::sum definitions:

static Tensor at::sum(const Tensor &self, IntArrayRef dim, bool keepdim = false, c10::optional<ScalarType> dtype = c10::nullopt)
static Tensor at::sum(const Tensor &self, DimnameList dim, bool keepdim = false, c10::optional<ScalarType> dtype = c10::nullopt)

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”?
Thank you!

Hi,

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.

So why when I call tensor.sum({0, 4}) it raises ambiguous error?

Could you give me an example of how to call the two functions explicitly without ambiguous?
Thanks for your reply!

Not sure as I don’t use the cpp API a lot, but does something like .sum(c10::IntArrayRef({0, 4})) work?

cc @yf225 is this expected behavior?

The ambiguity between IntArrayRef and DimnameList 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 std::vector<int64_t>({0, 4}) instead, as c10::IntArrayRef doesn’t own the underlying memory which could cause issues in some cases.

3 Likes