C++ indexing error no matching function for call to ‘at::Tensor::index(at::Tensor&)’

Hi. I am trying to index a tensor using the following syntax

tensor = tensor.index(booleanIndexTensor);

This works before for some previous version of Torch c++. However, when I try with Pytorch 1.8, it throws the following error

error: no matching function for call to ‘at::Tensor::index(at::Tensor&)’

It seems like the API has changed, but I couldn’t find the new syntax anywhere else. Could you please let me know how I can adapt it for newer versions. Thanks a lot!

FYI, the last version I can compile is 1.7.

As far as I can tell, we have tensor[index] and tensor.index({index}) as indexing with a single tensor in PyTorch.
Edit: In the 1.8.0 release notes, your case is in the backward compatibility notes here (from PR Making ops c10-full: list of optional tensors by smessmer · Pull Request #49138 · pytorch/pytorch · GitHub ) and will need to change into tensor.index({index}).

Best regards

Thomas

1 Like

Thank you very much @tom. Indeed, it compiled fine when I changed to tensor.index({index}).

I copied the related note from the link you sent here, just for anyone having the same problem.

This will break backwards compatibility for the C++ API. There is no implicit conversion from ArrayRef (which was the old argument type) to List<optional>. One common call pattern is tensor.index({indices_tensor}), where indices_tensor is another Tensor, and that will continue working because the {} initializer_list constructor for List<optional> can take Tensor elements that are implicitly converted to optional, but another common call pattern was tensor.index(indices_tensor), where previously, the Tensor got implicitly converted to an ArrayRef, and to implicitly convert Tensor → optional → List<optional> would be two implicit conversions. C++ doesn’t allow chaining. two implicit conversions. So those call sites have to be rewritten to tensor.index({indices_tensor}).