Skip to content

Comments

feat: Zero-Copy Data Transfer with rust-numpy#73

Merged
debug-soham merged 2 commits intoetsi-ai:mainfrom
verdhanyash:feature/zero-copy-numpy-transfer
Feb 13, 2026
Merged

feat: Zero-Copy Data Transfer with rust-numpy#73
debug-soham merged 2 commits intoetsi-ai:mainfrom
verdhanyash:feature/zero-copy-numpy-transfer

Conversation

@verdhanyash
Copy link
Contributor

@verdhanyash verdhanyash commented Feb 11, 2026

Summary

Implements zero-copy data transfer between Python (NumPy) and Rust using rust-numpy's PyReadonlyArray2. This eliminates the pylist_to_vec2 bottleneck that iterated over Python list objects one element at a time, replacing it with direct buffer access to NumPy's contiguous memory.

Closes #17

Changes

Rust Core

  • lib.rs: Replaced pylist_to_vec2(&Bound<'_, PyList>) with ndarray_to_vec2(PyReadonlyArray2<f32>)
  • lib.rs: Updated train() and predict() signatures to accept PyReadonlyArray2<'_, f32>
  • Cargo.toml: Added numpy = 0.27 dependency

Python API

  • api.py: Passes contiguous float32 NumPy arrays directly to Rust (no .tolist() conversion)
  • preprocessing.py: Returns NumPy arrays from fit_transform() and transform() instead of .tolist()

Testing

  • Fixed test_tqdm_progress.py mock patching to work alongside compiled Rust extension
  • All 28 Rust tests pass
  • All 38 Python tests pass

Before vs After

Before:

numpy -> .tolist() -> Python list -> PyList -> per-element extract -> Vec<Vec<f32>>  

After:

numpy -> PyReadonlyArray2 (zero-copy buffer view) -> bulk row .to_vec() -> Vec<Vec<f32>>  

Acceptance Criteria

  • No manual loops for data conversion in lib.rs
  • pylist_to_vec2 and PyList completely removed
  • Training initialization time reduced by >50%% for large arrays
  • Internal Rust core architecture preserved
  • All tests pass (28 Rust + 38 Python)

Screenshots

image >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> image

Copilot AI review requested due to automatic review settings February 11, 2026 04:38
@github-actions
Copy link

Validation Successful!

This pull request has been verified and linked to issue #17. The system is now synchronizing metadata from the referenced issue. Kindly await maintainer review of your changes.

1 similar comment
@github-actions
Copy link

Validation Successful!

This pull request has been verified and linked to issue #17. The system is now synchronizing metadata from the referenced issue. Kindly await maintainer review of your changes.

@github-actions
Copy link

Thank you for opening this PR! Our automated system is currently verifying the PR requirements.
Internal Discussion: Discord

@github-actions github-actions bot added area: bridge Python-Rust interface and PyO3 bindings. Hard Complex architecture or advanced math. performance Speed and memory optimizations. labels Feb 11, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR replaces Python list-based data conversion with numpy array buffer access at the Python-Rust boundary. The implementation uses the rust-numpy crate to accept PyReadonlyArray2<f32> parameters instead of PyList, eliminating the need for element-by-element Python object extraction. The Python preprocessing layer now returns numpy arrays directly, and the API layer ensures contiguous float32 memory layout before passing data to Rust.

Changes:

  • Added numpy = 0.27 dependency to enable numpy array integration with pyo3
  • Replaced pylist_to_vec2(PyList) with ndarray_to_vec2(PyReadonlyArray2) for bulk row conversion
  • Updated Python preprocessing and API to return/pass numpy arrays instead of converting to lists

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
etna_core/Cargo.toml Added rust-numpy 0.27 dependency for PyReadonlyArray2 support
etna_core/src/lib.rs Replaced PyList-based conversion with ndarray-based bulk copy; removed PyList import
etna/preprocessing.py Returns numpy arrays directly instead of calling .tolist()
etna/api.py Ensures contiguous float32 arrays via np.ascontiguousarray before Rust calls
tests/test_tqdm_progress.py Fixed mock patching to ensure reloaded module uses mock extension
Cargo.lock Added transitive dependencies for numpy crate (ndarray, matrixmultiply, num-*, etc.)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link

Validation Successful!

This pull request has been verified and linked to issue #17. The system is now synchronizing metadata from the referenced issue. Kindly await maintainer review of your changes.

@verdhanyash verdhanyash force-pushed the feature/zero-copy-numpy-transfer branch from d685029 to f985953 Compare February 11, 2026 05:32
@github-actions
Copy link

Validation Successful!

This pull request has been verified and linked to issue #17. The system is now synchronizing metadata from the referenced issue. Kindly await maintainer review of your changes.

Aamod007
Aamod007 previously approved these changes Feb 11, 2026
Copy link
Collaborator

@Aamod007 Aamod007 left a comment

Choose a reason for hiding this comment

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

@verdhanyash Good work.

@github-actions
Copy link

Validation Successful!

This pull request has been verified and linked to issue #17. The system is now synchronizing metadata from the referenced issue. Kindly await maintainer review of your changes.

@github-actions github-actions bot requested a review from Aamod007 February 12, 2026 09:32
@debug-soham debug-soham added the SWoC26 Contributions specifically for the Social Winter of Code program. label Feb 12, 2026
@verdhanyash verdhanyash force-pushed the feature/zero-copy-numpy-transfer branch from 1c187e2 to 21851d2 Compare February 13, 2026 03:57
@github-actions
Copy link

Validation Successful!

This pull request has been verified and linked to issue #17. The system is now synchronizing metadata from the referenced issue. Kindly await maintainer review of your changes.

1 similar comment
@github-actions
Copy link

Validation Successful!

This pull request has been verified and linked to issue #17. The system is now synchronizing metadata from the referenced issue. Kindly await maintainer review of your changes.

@github-actions
Copy link

Validation Successful!

This pull request has been verified and linked to issue #17. The system is now synchronizing metadata from the referenced issue. Kindly await maintainer review of your changes.

Replace pylist_to_vec2 with numpy::PyReadonlyArray2 for zero-copy buffer access at the Python-Rust bridge. Eliminates per-element Python object extraction and .tolist() overhead.

Changes:

- etna_core/Cargo.toml: added numpy = 0.27 dependency

- etna_core/src/lib.rs: replaced pylist_to_vec2/PyList with ndarray_to_vec2/PyReadonlyArray2

- etna/preprocessing.py: return numpy arrays instead of .tolist()

- etna/api.py: pass contiguous float32 arrays directly to Rust

- tests/test_tqdm_progress.py: fixed mock patching for compiled extension
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@github-actions
Copy link

Validation Successful!

This pull request has been verified and linked to issue #17. The system is now synchronizing metadata from the referenced issue. Kindly await maintainer review of your changes.

Copy link
Collaborator

@Aamod007 Aamod007 left a comment

Choose a reason for hiding this comment

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

Great Work @verdhanyash !

@debug-soham debug-soham merged commit 2c18033 into etsi-ai:main Feb 13, 2026
2 checks passed
@github-actions
Copy link

This PR has been successfully merged. We appreciate your effort in improving the library and look forward to your future contributions to the Etsi AI ecosystem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: bridge Python-Rust interface and PyO3 bindings. Hard Complex architecture or advanced math. performance Speed and memory optimizations. SWoC26 Contributions specifically for the Social Winter of Code program.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bridge] Zero-Copy Data Transfer with rust-numpy

3 participants