Skip to content

feat(nanobind): Implement zero-copy Eigen mapping for MatrixDouble (#8538) - #8805

Merged
timosachsenberg merged 3 commits into
OpenMS:nanofrom
satyam102006:feature/8538-matrix-zerocopy
Feb 27, 2026
Merged

timosachsenberg merged 3 commits into
OpenMS:nanofrom
satyam102006:feature/8538-matrix-zerocopy

Conversation

@satyam102006

Copy link
Copy Markdown
Contributor

Description

This PR addresses the performance overhead ("AI slop") discussed in #8538 by implementing a true zero-copy architecture for OpenMS::MatrixDouble on the nano branch.

By leveraging nanobind's Eigen bridging and explicit striding, this entirely removes the need for manual for-loop memory copying and the inefficient .attr("T") transpose hack.

Technical Changes

  1. Memory Mapping: Replaced nested for loops in set_matrix and fromNdArray with Eigen::Map. It now dynamically checks the arr.stride(1) of the incoming NumPy array to safely route C-contiguous and F-contiguous buffers to their respective Eigen layouts without redundant allocations.
  2. Zero-Copy Views: Rewrote get_matrix_as_view to 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.
  3. CMake Integration: Updated src/pyOpenMS/CMakeLists.txt to explicitly find and link Eigen3::Eigen to 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
1. Creating C-contiguous numpy array...
NumPy strides: (16, 8) (C-contiguous)

2. Passing to OpenMS Matrix (fromNdArray)...
OpenMS Matrix value at (0,0): 1.0

3. Testing Zero-Copy View (get_matrix_as_view)...
View strides: (8, 16) (Should be F-contiguous)

4. Modifying the OpenMS Matrix...
Did the Python view update automatically?
Expected: 99.0, Actual: 99.0
✅ SUCCESS: True Zero-Copy achieved! No memory was duplicated.

@timosachsenberg

Copy link
Copy Markdown
Contributor

I blocked you because you generated an automated answer for an issue. How can we be sure there is a real person behind this?

@satyam102006

Copy link
Copy Markdown
Contributor Author

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?

@timosachsenberg

Copy link
Copy Markdown
Contributor

Thanks for the clarification :)

@timosachsenberg

Copy link
Copy Markdown
Contributor

cool. can you add the test script to our pyopenms tests?

@satyam102006

Copy link
Copy Markdown
Contributor Author

sure

@satyam102006

Copy link
Copy Markdown
Contributor Author

@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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: checking 0,0 and 1,1 does not check for potential C vs F Order bugs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the test to check the off-diagonal elements (1,0) and (0,1)

@timosachsenberg
timosachsenberg merged commit 31f0e56 into OpenMS:nano Feb 27, 2026
timosachsenberg pushed a commit that referenced this pull request Feb 28, 2026
…8538) (#8805)

* .

* Create test_matrix_zerocopy.py

* Update test_matrix_zerocopy.py
@satyam102006

Copy link
Copy Markdown
Contributor Author

@timosachsenberg do you have any other issues you'd recommend for me to look into?

timosachsenberg pushed a commit that referenced this pull request Mar 1, 2026
…8538) (#8805)

* .

* Create test_matrix_zerocopy.py

* Update test_matrix_zerocopy.py
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

Successfully merging this pull request may close these issues.

2 participants