forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace SWIG bindings with Pybind11, and remove SWIG dependency (Robo…
…tLocomotion#4949) * hack-y first pass at using pybind11 in drake * evil hacks to make pybind11 work * most of the RBT tests now pass with pybind11 * autodiff types work in pybind11 wrappers * pybind11 bindings now work for rbtree and IK tests * work around weird IK constraint argument requirements * add ik interfaces needed by director * remove swig_matlab and swigmake dependencies * move new pybind11 bindings into the bindings folder * remove all #ifndef SWIG checks * slightly fewer evil build hacks * only build pybind11 bindings if pybind11 was found * fix drake autogenerated path file * localize build system hacks to the pybind11 folder * fix pybind11 linking on osx * clean up and simplify the pydrake autodiff interface Rather than exposing raw autodiff wrappers to the user, we now provide the pydrake.forwarddiff module, which has the following functions: derivative(f, x) gradient(f, x) jacobian(f, x) which all take a function f and an argument x and compute the relevant quantity. The exciting part is that now that function f can include any mix of drake code and python code, and the derivatives should still just work. However, this feature will require some additional bindings (like operator overloading for autodiff types) in order to be useful. * add basic operator support to forwarddiff.py * remove accidentally-committed file * clean up * more cleanup * fix issues identified by cpplint * rename all bindings files with pydrake prefix * various fixes for director's pydrake IK * comments * make python boilerplate consistent * attempt to avoid having to call find_package(pybind11) early * fix linting issues * restore default symbol visibility to fix cross-module pybind11 errors * Use mwoehlke-kitware's automatic numpy-eigen conversions for autodiff This substantially simplifies the python interface code and removes all of the monkey-patching that I had previously done to make autodiff types work naturally with numpy arrays. * todos * move python module suffix lookup to FindPython.cmake * make PYTHON_MODULE_EXTENSION required * fix cmake syntax * only set python_module_extension on success * the 0 of success * rename result var for clarity * address some comments * try to handle finding numpy * one more unused import * update iris for pybind11 compatibility
- Loading branch information
Showing
50 changed files
with
749 additions
and
582 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,2 @@ | ||
# Look for the swig provided by our superbuild. If it doesn't exist, | ||
# don't fall back to system swig because it may be the wrong version | ||
# and likely does not support C++11 | ||
find_program(SWIG_EXECUTABLE swig | ||
PATHS ${CMAKE_INSTALL_PREFIX}/bin NO_DEFAULT_PATH) | ||
if(SWIG_EXECUTABLE) | ||
add_subdirectory(swig) | ||
if(NOT DISABLE_PYTHON) | ||
add_subdirectory(python) | ||
endif() | ||
else() | ||
message(" | ||
-------------------------------------------------------------------------------- | ||
*** IMPORTANT: swig not found; disabling all swig bindings. *** | ||
-------------------------------------------------------------------------------- | ||
") | ||
endif() | ||
add_subdirectory(python) | ||
add_subdirectory(pybind11) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
set(PYDRAKE_INSTALL_DIR lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/dist-packages/pydrake) | ||
|
||
if(APPLE) | ||
string(REPLACE "-Wl,-undefined -Wl,error" "" CMAKE_MODULE_LINKER_FLAGS ${CMAKE_MODULE_LINKER_FLAGS}) | ||
else() | ||
string(REPLACE "-Wl,--no-undefined" "" CMAKE_MODULE_LINKER_FLAGS ${CMAKE_MODULE_LINKER_FLAGS}) | ||
endif() | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-shadow") | ||
include_directories("${NUMPY_INCLUDE_DIRS}") | ||
|
||
pybind11_add_module(_pydrake_rbtree pydrake_rbtree.cc) | ||
target_link_libraries(_pydrake_rbtree PRIVATE drakeRBM drakeMultibodyParsers Eigen3::Eigen) | ||
target_compile_options(_pydrake_rbtree PRIVATE "-fvisibility=default") | ||
install(TARGETS _pydrake_rbtree DESTINATION ${PYDRAKE_INSTALL_DIR}) | ||
|
||
pybind11_add_module(_pydrake_autodiffutils pydrake_autodiffutils.cc) | ||
target_link_libraries(_pydrake_autodiffutils PRIVATE drakeRBM drakeMultibodyParsers Eigen3::Eigen) | ||
target_compile_options(_pydrake_autodiffutils PRIVATE "-fvisibility=default") | ||
install(TARGETS _pydrake_autodiffutils DESTINATION ${PYDRAKE_INSTALL_DIR}) | ||
|
||
pybind11_add_module(_pydrake_ik pydrake_ik.cc) | ||
target_link_libraries(_pydrake_ik PRIVATE drakeIK Eigen3::Eigen) | ||
target_compile_options(_pydrake_autodiffutils PRIVATE "-fvisibility=default") | ||
install(TARGETS _pydrake_ik DESTINATION ${PYDRAKE_INSTALL_DIR}/solvers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <pybind11/pybind11.h> | ||
|
||
#include <Eigen/Core> | ||
#include <unsupported/Eigen/AutoDiff> | ||
|
||
typedef Eigen::AutoDiffScalar<Eigen::VectorXd> AutoDiffXd; | ||
PYBIND11_NUMPY_OBJECT_DTYPE(AutoDiffXd); | ||
|
||
typedef Eigen::Matrix<AutoDiffXd, Eigen::Dynamic, 1> VectorXAutoDiffXd; | ||
|
||
typedef Eigen::Matrix<AutoDiffXd, 3, Eigen::Dynamic> Matrix3XAutoDiffXd; | ||
|
||
typedef Eigen::Matrix<AutoDiffXd, 4, 4> Matrix44AutoDiffXd; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/eigen.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include "drake/bindings/pybind11/pydrake_autodiff_types.h" | ||
|
||
|
||
namespace py = pybind11; | ||
|
||
using std::sin; | ||
using std::cos; | ||
|
||
/** | ||
* Force Eigen to evaluate an autodiff expression. We need this function | ||
* because, for example, adding two Eigen::AutoDiffXd values produces an | ||
* Eigen::AutoDiffScalar<Eigen::CWiseBinaryOp> which cannot be returned to | ||
* python. This just forces an evaluation and conversion to AutoDiffXd which | ||
* would normally happen automatically in C++. | ||
*/ | ||
template <typename Derived> | ||
AutoDiffXd eval(const Eigen::AutoDiffScalar<Derived>& x) { | ||
return AutoDiffXd(x.value(), x.derivatives()); | ||
} | ||
|
||
PYBIND11_PLUGIN(_pydrake_autodiffutils) { | ||
py::module m("_pydrake_autodiffutils", "Bindings for Eigen AutoDiff Scalars"); | ||
|
||
py::class_<AutoDiffXd>(m, "AutoDiffXd") | ||
.def("__init__", | ||
[](AutoDiffXd& self, | ||
double value, | ||
const Eigen::VectorXd& derivatives) { | ||
new (&self) AutoDiffXd(value, derivatives); | ||
}) | ||
.def("value", [](const AutoDiffXd& self) { | ||
return self.value(); | ||
}) | ||
.def("derivatives", [](const AutoDiffXd& self) { | ||
return self.derivatives(); | ||
}) | ||
.def("sin", [](const AutoDiffXd& self) { return eval(sin(self)); }) | ||
.def("cos", [](const AutoDiffXd& self) { return eval(cos(self)); }) | ||
.def("__add__", [](const AutoDiffXd& self, const AutoDiffXd& other) { | ||
return eval(self + other); | ||
}) | ||
.def("__add__", [](const AutoDiffXd& self, double other) { | ||
return eval(self + other); | ||
}) | ||
.def("__radd__", [](const AutoDiffXd& self, double other) { | ||
return eval(other + self); | ||
}) | ||
.def("__sub__", [](const AutoDiffXd& self, const AutoDiffXd& other) { | ||
return eval(self - other); | ||
}) | ||
.def("__sub__", [](const AutoDiffXd& self, double other) { | ||
return eval(self - other); | ||
}) | ||
.def("__rsub__", [](const AutoDiffXd& self, double other) { | ||
return eval(other - self); | ||
}) | ||
.def("__mul__", [](const AutoDiffXd& self, const AutoDiffXd& other) { | ||
return eval(self * other); | ||
}) | ||
.def("__mul__", [](const AutoDiffXd& self, double other) { | ||
return eval(self * other); | ||
}) | ||
.def("__rmul__", [](const AutoDiffXd& self, double other) { | ||
return eval(other * self); | ||
}) | ||
.def("__truediv__", [](const AutoDiffXd& self, const AutoDiffXd& other) { | ||
return eval(self / other); | ||
}) | ||
.def("__truediv__", [](const AutoDiffXd& self, double other) { | ||
return eval(self / other); | ||
}) | ||
.def("__rtruediv__", [](const AutoDiffXd& self, double other) { | ||
return eval(other / self); | ||
}); | ||
|
||
return m.ptr(); | ||
} |
Oops, something went wrong.