Skip to main content
precise-numpy logo

precise-numpy

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

License: MIT Rust Python Version


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.3 µs 1.5 µs 1.15x slower
1,000 Subtract 1.3 µs 1.5 µs 1.15x slower
1,000 Multiply 1.2 µs 1.4 µs 1.17x slower
1,000,000 Add 2.4 ms 3.8 ms 1.60x slower (Tracks error bounds)
1,000,000 Subtract 2.5 ms 4.1 ms 1.63x slower (Tracks error bounds)
1,000,000 Multiply 2.6 ms 4.4 ms 1.71x slower (Tracks error bounds)

Reductions & Math Functions

Array Size Operation Standard NumPy precise-numpy Performance Comparison
1,000 mean 5.2 µs 400 ns 13.0x FASTER than NumPy
1,000 sum 2.0 µs 600 ns 3.3x FASTER than NumPy
1,000 sin 10.2 µs 8.1 µs 1.25x FASTER than NumPy
100,000 sum 31.7 µs 29.0 µs 1.1x FASTER than NumPy
100,000 mean 33.4 µs 29.3 µs 1.14x FASTER than NumPy

Matrix Multiplication (matmul)

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

Matrix Shape Standard NumPy precise-numpy Performance Comparison
64 × 64 12.9 µs 61.3 µs Assembly GEMM microkernel
128 × 128 105.2 µs 410.3 µs Multi-threaded parallel GEMM
256 × 256 394.3 µs 1.7 ms 12x speedup vs naive loop

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.0.tar.gz (422.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.0-cp310-abi3-win_amd64.whl (203.5 kB view details)

Uploaded CPython 3.10+Windows x86-64

precise_numpy-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (310.6 kB view details)

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

precise_numpy-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (278.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

precise_numpy-0.1.0-cp310-abi3-macosx_11_0_arm64.whl (250.5 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

precise_numpy-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl (281.4 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: precise_numpy-0.1.0.tar.gz
  • Upload date:
  • Size: 422.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.0.tar.gz
Algorithm Hash digest
SHA256 6d6f8ecc46e740988aeb208cbd6dde90ee5284e1623def81f217629e9a51f633
MD5 864c9c1108d1840dd4cbece66695ff31
BLAKE2b-256 6639bbf2cb92b4ae8b064281f3fc15803b3fdd003f750d0e09d84bb5eb6b784c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for precise_numpy-0.1.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4d771dce9a7992068895507a9791ae9478f69d365cb4dfd5635defbc8e32753b
MD5 03c8bef75c09842ba758d8194dee9ae1
BLAKE2b-256 6f46fa276b19df4a1a76c4af6771a21e322cc9ecabff7a87a881eb910d6e3cfa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for precise_numpy-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2db8e12ae6f5581ab976d2308d53488a2c88eec57afbd25239b0c2380f83d81e
MD5 4e197cda0012f09373034d8ae9d750de
BLAKE2b-256 19d8f9e17b92fc0d048c1b4016ce7b37e25115d825fbef5b5c92424ddd60bbce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for precise_numpy-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8712cf10fc30666b5660cebd7908f978e7eb1108106b9f2006b54cf8fe7dbdf4
MD5 a175018fd6475326239a934e4f94a38c
BLAKE2b-256 010c9b1c8551b399f6e3f695f50d299c04eaa28002074aa96343278f79faeebb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for precise_numpy-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2d734fa42f0f8e2d56de418501bbd7c0ab203fcf93e9da3bcd332e85af000f4a
MD5 a400e8d430230bc835c712492c0c2f89
BLAKE2b-256 e0a526039070389c6c95d7574fbc4b7d603c17419c5d663b13ee5dfc607b35b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for precise_numpy-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 90fe1f96b862d107b300ada12f5658f2a0521937c88e324cd955056566bedfba
MD5 9de096114b3000fdb2628c013e4e277f
BLAKE2b-256 d7bb6bc0c75e4e7dfa50e7653c7ec51eaa19c61c15e9a461925de6d9bbc86171

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