Repository navigation
feat(nanobind): Implement zero-copy Eigen mapping for MatrixDouble (#8538) - #8805
Conversation
|
I blocked you because you generated an automated answer for an issue. How can we be sure there is a real person behind this? |
|
Hi @timosachsenberg , I completely understand the concern regarding automated answers. To clarify, this is a manual contribution I been working on locally on my Mac Mini M4. I spent the last few hours debugging the specific constraints of the nano branch, and I hit several 'human' hurdles that required manual fixes: Hardware Bottleneck: My first build attempt with -j$(nproc) actually hung my entire system because the Eigen and nanobind were consuming too much RAM. I had manually pivot to a restricted -j2 build to get it to finish. CMake Linking Error: I initially hit a fatal error: 'Eigen/Core' file not found. I had manually investigate src/pyOpenMS/CMakeLists.txt and realized that while the core C++ library uses Eigen, the datastructures Python module wasn't explicitly linking to it. I fixed this by adding find_package(Eigen3 REQUIRED) and updating the target_link_libraries. Typing Bug: I ran into a compiler error where nb::ndarray wouldn't implicitly convert to nb::object in the get_matrix_as_view lambda. I had to remove the explicit return type deduction to allow the compiler to resolve the binding correctly. Zero-Copy Proof: I verified the fix with a local script (results in the PR description) confirming that the NumPy strides are now (8, 16) for a 2x2 double matrix, matching OpenMS’s Column-Major layout without memory duplication. I’m genuinely interested in contributing to OpenMS . Would you like me to use a template-based approach to expand this logic to MatrixFloat and MatrixInt as well, or should we keep this PR focused on the proof-of-concept for Double? |
|
Thanks for the clarification :) |
|
cool. can you add the test script to our pyopenms tests? |
|
sure |
|
@timosachsenberg i commit the changes |
|
|
||
| # Change in NumPy should reflect in C++ | ||
| view_arr[1, 1] = 42.0 | ||
| assert openms_mat.getValue(1, 1) == 42.0 |
There was a problem hiding this comment.
Minor: checking 0,0 and 1,1 does not check for potential C vs F Order bugs
There was a problem hiding this comment.
I've updated the test to check the off-diagonal elements (1,0) and (0,1)
|
@timosachsenberg do you have any other issues you'd recommend for me to look into? |
Description
This PR addresses the performance overhead ("AI slop") discussed in #8538 by implementing a true zero-copy architecture for
OpenMS::MatrixDoubleon thenanobranch.By leveraging
nanobind's Eigen bridging and explicit striding, this entirely removes the need for manualfor-loop memory copying and the inefficient.attr("T")transpose hack.Technical Changes
forloops inset_matrixandfromNdArraywithEigen::Map. It now dynamically checks thearr.stride(1)of the incoming NumPy array to safely route C-contiguous and F-contiguous buffers to their respective Eigen layouts without redundant allocations.get_matrix_as_viewto explicitly define the F-contiguous strides (int64_t strides[2] = {1, static_cast<int64_t>(self.rows())}). Python now natively views the C++ buffer directly.src/pyOpenMS/CMakeLists.txtto explicitly find and linkEigen3::Eigento the domain modules to support the new<nanobind/eigen/dense.h>headers.Validation
I created a local Python test script to verify that mutations to the C++ matrix immediately reflect in the Python NumPy view, confirming that the view is no longer detached.
Test Script Output