-
Notifications
You must be signed in to change notification settings - Fork 198
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
Initial support for the Eigen library #120
Conversation
The step responsible for checking whether a tensor has the right strides to invoke a C++ routine was incorrect and always accepted C-style NumPy arrays (because they leave the 'stride' field set to NULL). The whole conversion logic will be more heavily tested in a subsequent commit that adds a type caster for Eigen arrays.
- Prefer the buffer protocol over DLPack when converting C++ tensors into NumPy arrays. This makes it possible to wrap C++ memory through arrays that have their 'write' bit still enabled (NumPy is somewhat conservative here and disables the write bit when the conversion is done via DLPack). - Simplified the implementation of the internally used `nb_tensor` buffer protocol wrapper. This change requires a nanobind ABI bump. - Copy the contents when `rv_policy::copy/move` is specified to the tensor caster.
This commit adds custom casters for the Eigen linear algebra library. It uses the existing `nb_tensor` infrastructure to implement a conversion between Eigen types and NumPy arrays that currently supports: - Eigen::Matrix/Array<..> and derived types (row/column vectors) - Eigen::Map<..> - Eigen::Ref<..> - Expression templates (CwiseBinaryOp, etc.) There are a few limitations: - Eigen tensors are not supported yet. - Sparse matrix types are not supported yet.
I am very happy about this addition; but doesn't this cut against the explicit design goals of nanobind to be small and targeted? |
@ianhbell: the issue with pybind11 that motivated the creation of this library was that a number of relatively niche features added overheads to the core, reducing performance for everyone (even for users that did not use or want those features). Adding features to either library when they are in the form of self-contained/opt-in components seems perfectly fine in comparison. That said, the implementation of the Eigen support layer proposed in this PR is relatively "nano" compared to the one in pybind11. |
Agreed; opt-in is always the way to go. I'm looking forward to playing with the Eigen support in nanobind. |
Can you check with the latest version to see if it is needed? I moved the NumPy tensor exchange back onto the buffer protocol. |
This commit adds custom casters for the Eigen linear algebra library. It uses the existing
nb_tensor
infrastructure to implement a conversion between Eigen types and NumPy arrays that currently supports:Eigen::Matrix/Array<..>
and derived types (row/column vectors)Eigen::Map<..>
Eigen::Ref<..>
CwiseBinaryOp
, etc.)There are a few limitations: