Implement C++ autograd function and use it in Python

I’m new to the C++ version of Pytorch and I’m not very familiar with C++. I want to write a custom autograd function in C++ and use it in Python. My goal is similar to what this tutorial is doing, except that I don’t want to wrap the function in Python using autograd.Function, what I want is a custom C++ autograd Function that can be used directly.

Currently, I know how to use the autograd function in C++, according to this file(My function is also implemented based on this file’s example). The problem with this is that I don’t know how to bind the SOME_FUNC::apply to python using pybind11.

namespace py = pybind11;
PYBIND11_MODULE(module, m) {
	py::class_<SOME_FUNC>(m, "PTanh")
		.def_static("forward", &SOME_FUNC::forward)
                //.def_static("apply",&SOME_FUNC::apply) //This line will produce an error.
		.def_static("backward", &SOME_FUNC::backward);
}

Can someone tell me a way to bind the apply or some way to make the autograd function in C++ directly callable in Python. Thank u very much.

Note: I’m compiling using pybind11 with setuptools to compile the code, like how it is in the tutorial.

I managed to find a work around, which is to wrap the SOME_FUNC::apply in another C++ function and bind that function to Python. But I still hope to know a direct way.

Hey I am trying to do the same thing. Did you find a more direct way to do it?