Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pybind11 + Autodiff::VectorXvar gives compiltion error #284

Open
ipcamit opened this issue Mar 25, 2023 · 0 comments
Open

Pybind11 + Autodiff::VectorXvar gives compiltion error #284

ipcamit opened this issue Mar 25, 2023 · 0 comments

Comments

@ipcamit
Copy link

ipcamit commented Mar 25, 2023

When I try to mix <pybind11/eigen.h> and <autodiff/reverse/var/eigen.h> I get the following error, however it works fine with <autodiff/forward/real/eigen.h>. Is there a known way to mix reverse with pybind11/eigen ?


Error:

In file included from /opt/mambaforge/mambaforge/envs/colabfit/include/pybind11/eigen/matrix.h:12,
                 from /opt/mambaforge/mambaforge/envs/colabfit/include/pybind11/eigen.h:12,
                 from /home/amit/Tutorials/autodiff/example_pybind.cpp:6:
/opt/mambaforge/mambaforge/envs/colabfit/include/pybind11/numpy.h: In instantiation of ‘struct pybind11::detail::npy_format_descriptor<autodiff::detail::Variable<double>, void>’:
/opt/mambaforge/mambaforge/envs/colabfit/include/pybind11/eigen/matrix.h:224:40:   required from ‘constexpr const auto pybind11::detail::EigenProps<Eigen::Matrix<autodiff::detail::Variable<double>, -1, 1, 0, -1, 1> >::descriptor’
/opt/mambaforge/mambaforge/envs/colabfit/include/pybind11/eigen/matrix.h:388:27:   required from ‘constexpr const auto pybind11::detail::type_caster<Eigen::Matrix<autodiff::detail::Variable<double>, -1, 1, 0, -1, 1>, void>::name’
/opt/mambaforge/mambaforge/envs/colabfit/include/pybind11/cast.h:1404:56:   required from ‘constexpr const auto pybind11::detail::argument_loader<MyFunc*, Eigen::Matrix<autodiff::detail::Variable<double>, -1, 1, 0, -1, 1>&, Eigen::Matrix<autodiff::detail::Variable<double>, -1, 1, 0, -1, 1>&>::arg_names’
/opt/mambaforge/mambaforge/envs/colabfit/include/pybind11/pybind11.h:291:31:   required from ‘void pybind11::cpp_function::initialize(Func&&, Return (*)(Args ...), const Extra& ...) [with Func = pybind11::cpp_function::cpp_function(Return (Class::*)(Arg ...), const Extra& ...) [with Return = void; Class = MyFunc; Arg = {Eigen::Matrix<autodiff::detail::Variable<double>, -1, 1, 0, -1, 1>&, Eigen::Matrix<autodiff::detail::Variable<double>, -1, 1, 0, -1, 1>&}; Extra = {pybind11::name, pybind11::is_method, pybind11::sibling}]::<lambda(MyFunc*, Eigen::Matrix<autodiff::detail::Variable<double>, -1, 1, 0, -1, 1>&, Eigen::Matrix<autodiff::detail::Variable<double>, -1, 1, 0, -1, 1>&)>; Return = void; Args = {MyFunc*, Eigen::Matrix<autodiff::detail::Variable<double>, -1, 1, 0, -1, 1>&, Eigen::Matrix<autodiff::detail::Variable<double>, -1, 1, 0, -1, 1>&}; Extra = {pybind11::name, pybind11::is_method, pybind11::sibling}]’
/opt/mambaforge/mambaforge/envs/colabfit/include/pybind11/pybind11.h:109:9:   required from ‘pybind11::cpp_function::cpp_function(Return (Class::*)(Arg ...), const Extra& ...) [with Return = void; Class = MyFunc; Arg = {Eigen::Matrix<autodiff::detail::Variable<double>, -1, 1, 0, -1, 1>&, Eigen::Matrix<autodiff::detail::Variable<double>, -1, 1, 0, -1, 1>&}; Extra = {pybind11::name, pybind11::is_method, pybind11::sibling}]’
/opt/mambaforge/mambaforge/envs/colabfit/include/pybind11/pybind11.h:1568:22:   required from ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def(const char*, Func&&, const Extra& ...) [with Func = void (MyFunc::*)(Eigen::Matrix<autodiff::detail::Variable<double>, -1, 1, 0, -1, 1>&, Eigen::Matrix<autodiff::detail::Variable<double>, -1, 1, 0, -1, 1>&); Extra = {}; type_ = MyFunc; options = {PyFunc}]’
/home/amit/Tutorials/autodiff/example_pybind.cpp:49:29:   required from here
/opt/mambaforge/mambaforge/envs/colabfit/include/pybind11/numpy.h:1420:37: error: static assertion failed: Attempt to use a non-POD or unimplemented POD type as a numpy dtype
 1420 |     static_assert(is_pod_struct<T>::value,
      |                                     ^~~~~
make[2]: *** [CMakeFiles/example_pybind11.dir/build.make:63: CMakeFiles/exam

C++ file:

// C++ includes
#include <iostream>

// Pybind11 includes
#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>

// autodiff include
// << This does not compile >>
#include <autodiff/reverse/var.hpp>
#include <autodiff/reverse/var/eigen.hpp>
typedef autodiff::VectorXvar  vector_kind;

// << This compiles >>
//#include <autodiff/forward/real.hpp>
//#include <autodiff/forward/real/eigen.hpp>
//typedef autodiff::VectorXreal  vector_kind;

using namespace autodiff;

// Base virtual class
class MyFunc {
public:
    virtual void f(vector_kind& x, vector_kind& y) = 0;};

// Concrete class
class MyFunc1 : public MyFunc {
public:
    void f(vector_kind& x, vector_kind& y) override { y = 2 * x;}};

// Pybind11 Trampoline
class PyFunc : public MyFunc {
    public:
        using MyFunc::MyFunc;

        void f(vector_kind& x, vector_kind& y) override {
            PYBIND11_OVERLOAD_PURE(void, MyFunc, f, x, y);
        }
};

PYBIND11_MODULE(example_pybind, m) {
    pybind11::class_<MyFunc, PyFunc>(m, "MyFunc")
        .def(pybind11::init<>())
        .def("f", &MyFunc::f);
}

CMAKE file:

cmake_minimum_required(VERSION 3.15)

project(example)
set(CMAKE_CXX_STANDARD 17)
include_directories(autodiff_includes)
find_package(PythonInterp REQUIRED)
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})

# PYBIND11
find_package(pybind11 REQUIRED)

pybind11_add_module(example_pybind11 example_pybind.cpp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant