Skip to main content
precise-numpy logo

precise-numpy

NumPy-compatible interval arrays with guaranteed numerical error bounds, powered by Rust SIMD.

PyPI Version Python Versions License: MIT Rust


Overview

precise-numpy is a high-performance Python library for numerical computing with provable error bounds. Standard floating-point arrays (float64) accumulate rounding error and catastrophic cancellation without diagnostic warnings. precise-numpy uses hardware FPU directed rounding (MXCSR on x86_64 and FPCR on ARM64) to track mathematical bounds ($midpoint \pm radius$) across every array operation.

Key capabilities:

  • Drop-in NumPy Ergonomics: Familiar array operations, indexing, broadcasting, and reductions.
  • Hardware-Directed Rounding: Enforces strict IEEE 754 lower/upper bounds for guaranteed mathematical enclosure.
  • Rust SIMD Acceleration: Single-pass streaming vectorization (AVX-512, AVX2/FMA, ARM NEON) and parallel execution via Rayon.
  • BLAS Matrix Multiplication: Multi-threaded assembly-tuned GEMM microkernels (matrixmultiply) for matrix operations.

Killer Use Case: Floating-Point Drift & AI Quantization Auditor

ML models and quantitative trading algorithms often exhibit unpredictable drift across machines or quantized precision levels (e.g. FP32 vs FP16). precise-numpy enables exact numerical audits of neural network layers and scientific pipelines.

import numpy as np
import precise_numpy as pnp

# Audit a Transformer Attention Layer under input noise (error = 1e-4)
X_raw = np.random.randn(128, 64)
W_q_raw = np.random.randn(64, 64) * 0.1

# Wrap input data with interval error bounds
X_pnp = pnp.array(X_raw.flatten().tolist(), error=1e-4).reshape([128, 64])
W_q_pnp = pnp.array(W_q_raw.flatten().tolist()).reshape([64, 64])

# Query projection with propagated error bounds
Q_pnp = X_pnp.matmul(W_q_pnp)

# Check maximum relative error amplification across the layer
print("Max Relative Error:", Q_pnp.max_relative_error())
print("Max Radius Error:", Q_pnp.max_radius())

(See complete runnable audit script in examples/quantization_safety_audit.py)


Performance Benchmarks

Benchmarked against standard single-float numpy on Python 3.11 (Intel / AMD AVX2 + FMA).

Understanding the Benchmarks:

  • Standard NumPy operates on single 64-bit float arrays (float64) without error tracking.
  • precise-numpy maintains two contiguous 64-bit float arrays ($midpoint$ and $radius$) per operation and executes hardware rounding mode switches to guarantee error bounds.
  • Small Arrays ($\le 1,000$ elements): precise-numpy is significantly faster than NumPy because our PyO3 C-extension eliminates Python ufunc engine dispatch overhead (~400ns vs ~5,200ns).
  • Large Arrays ($\ge 100,000$ elements): For pure raw memory throughput, precise-numpy stays within 1.3x–1.7x of single-float NumPy despite calculating double the data using single-pass streaming SIMD loops.

Element-Wise Operations

Array Size Operation Standard NumPy precise-numpy Performance Comparison
1,000 Add 1.8 µs 2.0 µs 1.11x slower
1,000 Subtract 1.7 µs 1.8 µs 1.06x slower
1,000 Multiply 1.6 µs 2.0 µs 1.25x slower
1,000,000 Add 5.3 ms 6.1 ms 1.15x slower (Tracks error bounds)
1,000,000 Subtract 3.7 ms 4.2 ms 1.13x slower (Tracks error bounds)
1,000,000 Multiply 2.7 ms 4.6 ms 1.71x slower (Tracks error bounds)

Reductions & Math Functions

Array Size Operation Standard NumPy precise-numpy Performance Comparison
1,000 mean 11.4 µs 1.2 µs 9.5x FASTER than NumPy
1,000 sum 3.3 µs 1.3 µs 2.5x FASTER than NumPy
1,000 sin 14.0 µs 11.5 µs 1.2x FASTER than NumPy
100,000 sum 73.5 µs 56.5 µs 1.3x FASTER than NumPy
100,000 mean 81.0 µs 53.3 µs 1.5x FASTER than NumPy

Matrix Multiplication (matmul)

Powered by multi-threaded & single-threaded optimized matrixmultiply (dgemm) assembly microkernels:

Matrix Shape Standard NumPy precise-numpy Performance Comparison
64 × 64 15.2 µs 78.4 µs Single-threaded assembly GEMM
128 × 128 231.8 µs 540.8 µs Low-overhead single-threaded GEMM
256 × 256 578.0 µs 2.9 ms Parallel row-block GEMM

Installation

pip install precise-numpy

Build from source:

git clone https://github.com/your-org/precise-numpy.git
cd precise-numpy
maturin develop --release

Quick Start

import precise_numpy as pnp

# Create interval arrays with error bounds
a = pnp.array([1.0, 2.0, 3.0], error=0.01)
b = pnp.array([4.0, 5.0, 6.0], error=0.02)

# Arithmetic operations automatically propagate bounds
c = a + b
print(c)
# Output: IntervalArray([5.0+/-0.03, 7.0+/-0.03, 9.0+/-0.03])

# Inspect relative error
print("Max relative error:", c.max_relative_error())

# Scalar operations
d = a * 2.5 + 10.0

# Reductions
mid, err = c.sum()
print(f"Sum = {mid} ± {err}")

Technical Architecture

  1. Structure-of-Arrays (SoA) Buffer: Midpoints and radii are stored in contiguous, 64-byte aligned memory chunks (AlignedBuffer), allowing direct SIMD vector loads without interleaved packing overhead.
  2. Single-Pass Streaming SIMD (mul_intervals_stream): Fuses midpoint product $a_{mid} \cdot b_{mid}$ and radius error bound $|a_{mid}| b_{rad} + |b_{mid}| a_{rad} + a_{rad} b_{rad}$ into a single SIMD pass using AVX-512 and AVX2+FMA registers.
  3. Zero-Copy Reference Counting: IntervalArray uses Arc<AlignedBuffer>, enabling $O(1)$ zero-copy slicing, clones, and reshaping.
  4. GIL Release: Long computations drop the Python GIL via py.allow_threads(), enabling parallel multi-threaded computing with Rayon.

License

MIT License. See LICENSE for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

precise_numpy-0.1.1.tar.gz (424.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

precise_numpy-0.1.1-cp310-abi3-win_amd64.whl (206.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

precise_numpy-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (313.6 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

precise_numpy-0.1.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (279.8 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

precise_numpy-0.1.1-cp310-abi3-macosx_11_0_arm64.whl (251.9 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

precise_numpy-0.1.1-cp310-abi3-macosx_10_12_x86_64.whl (284.5 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file precise_numpy-0.1.1.tar.gz.

File metadata

  • Download URL: precise_numpy-0.1.1.tar.gz
  • Upload date:
  • Size: 424.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for precise_numpy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c1a3985791b6bbdc11adb67015e1509bf53404cef79e7857e816323c9da742fe
MD5 efd5bb1089cb02d56872aad25f457585
BLAKE2b-256 962c253cad89af2f0ddffa0997acd338ae8ec7012a9e80701320e18f71e19745

See more details on using hashes here.

File details

Details for the file precise_numpy-0.1.1-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for precise_numpy-0.1.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f97d3eb18f703a4d77b4030066d9e7313941153cec3987899bd1ed880193a994
MD5 5feb53f81e0fb4f2c840f6e85b06dd1f
BLAKE2b-256 19b36f2f095484f5f85139a54070e16ca1c0b8eb07345706f76d7034c90f66e8

See more details on using hashes here.

File details

Details for the file precise_numpy-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for precise_numpy-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 470cbafaf10178e96a98e082fe96e07e798af3ce2bfd92bb0e54914d06b9026e
MD5 1fb725c817b2f237a642c970728d967d
BLAKE2b-256 cc2a02af092e70206dde36c245a2e4419a609f757ef812dfb192a728a55907e2

See more details on using hashes here.

File details

Details for the file precise_numpy-0.1.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for precise_numpy-0.1.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 20fa0b09c6799849bafffa78098e2e5d53b564b441edb891a6e63fc57515dd8f
MD5 594654c45d76385c5d102e8017f92c66
BLAKE2b-256 a703cdb7a3a6b2aefc2b8b1cf9711b171677a62a6b59eaea6ad4ca3f1eda68f4

See more details on using hashes here.

File details

Details for the file precise_numpy-0.1.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for precise_numpy-0.1.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6fcbd57003dbab2b17ece6b056bab5ebf027fda136a9eff66321459984e43638
MD5 03947a8d6673a45412802af7ac937823
BLAKE2b-256 d8a880a1f7621234e28f6362c3f565fa8a00f077b3a41412277ed4798e6dd50d

See more details on using hashes here.

File details

Details for the file precise_numpy-0.1.1-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for precise_numpy-0.1.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 01a0f995552e31515ee8aa12bc4b0f68849bffbc12fcf594b6d75fddc4c00d93
MD5 1894075166a56efedce86fa60bf0dc01
BLAKE2b-256 4cfaee99a8e6cde3c49c09040319a280f5dfa1a109ddc77cb027e949020b1faa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page