First release of a high-performance N-dimensional array library built in Rust for macOS.
Project description
zanpy
A high-performance numerical computing library for Rust.
zanpy is a lightweight, hardware-aware multidimensional array library. It was built to explore the intersection of systems programming and data science, bridging the gap between Python's ease of use and Rust's raw computational power.
Performance Benchmarks
In a $1024 \times 1024$ double-precision ($f64$) matrix multiplication task, zanpy delivers performance that rivals industry-standard BLAS implementations.
| Implementation | Avg. Time (1024x1024) | Speedup vs. Python | Ratio vs. NumPy |
|---|---|---|---|
| NumPy (Accelerate/vecLib) | 7.9ms | ~3000x | 1.0x |
| zanpy (Rayon + SIMD) | 43.5ms | ~550x | 5.48x |
| Pure Python (Nested Loops) | ~24,000ms | 1x | 3000x+ |
Benchmarks conducted on Apple M1 (8-core), 16GB RAM. Results may vary based on thermal throttling and background processes.
Core Technologies
- SIMD Vectorization: Leverages 128-bit wide registers via the
widecrate to execute multiple floating-point operations per clock cycle (ARM NEON). - Work-Stealing Parallelism: Utilizes
rayonto distribute workloads across all available CPU cores, maximizing throughput on multi-core architectures. - PyO3 Bindings: High-efficiency FFI (Foreign Function Interface) allowing the library to be imported and used as a native Python module.
- Manual Memory Management: Uses raw pointer arithmetic and
MaybeUninitto bypass the overhead of standard collection initialization.
Architecture
Achieving 5.48x of NumPy (a library written in C and Assembly) required deep architectural optimizations:
1. Spatial Locality & IKJ Reordering
I moved away from the naive $O(n^3)$ loop structure in favor of an IKJ-ordered kernel. By processing the matrix in this order, I ensured contiguous memory access (streaming). This allows the CPU's hardware prefetcher to load data into the L1 cache before the execution unit even requests it.
2. Cache Boundary Awareness
The library was tuned to respect the 128-byte cache line size of the Apple M1. Through iterative profiling, I balanced "Cache Tiling" overhead against "Instruction Pressure," eventually settling on a streamlined parallel SIMD kernel that minimizes branch mispredictions.
Usage
Zanpy is available on pip:
bash
pip install zanpy
Prerequisites
You will need the Rust toolchain and maturin installed to build the project from source.
bash
# Clone the repository
git clone [https://github.com/yourusername/zanpy.git](https://github.com/yourusername/zanpy.git)
cd zanpy
# Build with release optimizations
maturin develop --release
Testing
Run
bash
python3.12 test.py
Core Features
zanpy provides a robust API for multidimensional array manipulation, optimized for performance via its Rust backend.
Array Creation & Manipulation
- Flexible Initialization: Create arrays from Python lists, or use built-in constructors like
ones(),zeros(),identity(), andarange(). - Advanced Reshaping: Modify array dimensions with
reshape()or reorder axes usingpermute()andtranspose(). - High-Performance Access: Efficient indexing via the
get()method with bounds checking handled by Rust.
Element-wise Operations
zanpy overloads standard Python operators to perform multi-threaded, SIMD-accelerated element-wise math:
- Arithmetic: Support for
+(__add__),-(__sub__),*(__mul__), and/(__truediv__). - Broadcasting Ready: Designed to handle element-wise operations across compatible shapes.
Reductions & Statistics
Quickly compute aggregate values across your datasets:
- Sum & Mean: Fast summation and averaging.
- Extrema: Rapid
max()andmin()identification using optimized comparison kernels.
Linear Algebra (The Engine)
This is where zanpy shines, utilizing specialized Rust kernels for heavy computations:
- Matrix Multiplication (
@): Implemented via the__matmul__operator, featuring the optimized IKJ-tiled SIMD kernel. - Dot Product: Vector-vector and matrix-vector dot products.
- Matrix Inverse: Compute the inverse of square matrices using optimized linear algebra routines.
Technical Implementation Details
For those interested in the bridge between Python and Rust:
- PyO3 Integration: We use
#[pyclass]and#[pymethods]to expose theNdArrayRust struct as a native Python object, minimizing FFI overhead. - Memory Ownership: Data is stored in contiguous memory in Rust. When accessing
.datafrom Python, a copy is safely provided to maintain Rust's strict memory safety and avoid dangling pointers. - Error Handling: Rust
Resulttypes are automatically mapped to PythonValueErrorexceptions, ensuring that dimension mismatches or non-invertible matrices provide clear, actionable feedback to the Python user.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file zanpy-1.0.0-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: zanpy-1.0.0-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 241.5 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6c62ad1a7b0b9124a556e0e18367a3efc89108253631be579d9bd0184fde213
|
|
| MD5 |
d577533ae2b9620605213b5d97537964
|
|
| BLAKE2b-256 |
0c7494268c16e6bcdd6ccda2612134391027bde2357897014232946b436b597c
|
Provenance
The following attestation bundles were made for zanpy-1.0.0-cp312-cp312-macosx_11_0_arm64.whl:
Publisher:
release.yml on Mersautschip/zanpy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zanpy-1.0.0-cp312-cp312-macosx_11_0_arm64.whl -
Subject digest:
f6c62ad1a7b0b9124a556e0e18367a3efc89108253631be579d9bd0184fde213 - Sigstore transparency entry: 1303680357
- Sigstore integration time:
-
Permalink:
Mersautschip/zanpy@c4c85c8a0f6583a9f95b0d688551e9b285b318e9 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/Mersautschip
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c4c85c8a0f6583a9f95b0d688551e9b285b318e9 -
Trigger Event:
push
-
Statement type: