Python binding for c++

I’m trying to write a simple function in c++ and then call in python but when I do that I’m getting the error that incompatible function arguments. The following argument types are supported: 1. (ag:0 at::Tensor) -> at::Tensor. What I am doing wrong?

This is my function:

#include <torch/script.h> // One-stop header.
#include <pybind11/pybind11.h>
#include <ATen/ATen.h>
at::Tensor test( at::Tensor& img){
    return img;
}
PYBIND11_MODULE(example, m) {
    m.doc() = "test function!";
    m.def("test", &test, "CudaTorch");
#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}

This is output:

I don’t know if it will solve, but you should maybe cast the input and output in the declaration of pybind11, like it is done here: https://github.com/LeviViana/torch_sampling/blob/master/csrc/sampling.cpp#L44

BTW, this repo contains an implementation of a pytorch extension, it can serve as tutorial as well.

I would use torch::Tensor even if they’re an alias these days.

All arguments should either be const torch::Tensor& (if not modified) or torch::Tensor (no reference).
(I think there is a brief mention of this in Chapter 15 of our book.)

Best regards

Thomas