Metadata-Version: 2.4
Name: rns_engine
Version: 0.13.0
Summary: Exact modular arithmetic and matrix multiplication over integers and rationals.
Author: Evan Wesley
License-Expression: AGPL-3.0-only
Project-URL: Repository, https://github.com/playfularchitect/rns_engine
Project-URL: Benchmarks, https://github.com/playfularchitect/rns_engine/tree/main/benchmarks
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: C++
Classifier: Operating System :: OS Independent
Requires-Python: <3.15,>=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.23
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: tomli>=1; python_version < "3.11" and extra == "test"
Requires-Dist: numpy==2.2.6; python_version < "3.14" and extra == "test"
Requires-Dist: numpy==2.4.5; python_version >= "3.14" and extra == "test"
Dynamic: license-file

# RNS Engine

RNS Engine is a Python library for exact arithmetic with a native C++ core. It began as an AVX2 CPU engine for modular integer arithmetic and now includes exact integer and shared-scale rational matrix multiplication.

## Install and run

Use Python 3.10–3.14. The CPU engine runs without a GPU. The x86-64 builds require AVX2. [PyPI package](https://pypi.org/project/rns-engine/)

```bash
python -m pip install rns_engine
```

Compute 1,000 exact updates of `x ← (1_000_003 × x + 7) mod M` per input and check the result against Python integers.

```python
import numpy as np
import rns_engine as rns

values = np.array([1, 5, 123_456_789], dtype=np.uint64)
result = rns.affine_repeat_u64_io(values, 1_000_003, 7, 1_000)

reference = [int(value) for value in values]
for _ in range(1_000):
    reference = [(1_000_003 * value + 7) % rns.M for value in reference]

assert result.tolist() == reference
print(result.tolist())
```

## Original CPU engine

The engine encodes integers as residues modulo **127, 8,191, 65,536, and 524,287**. It performs arithmetic on those residues and reconstructs the result with Garner-style Chinese remainder reconstruction. The base engine works modulo **35,742,890,181,197,824**.

In the original **two-core Google Colab CPU benchmark**, AVX2 and two OpenMP threads averaged **108.92 billion equivalent exact modular multiply-add steps per second**, with a best run of **112.07 billion**. The workload applied 1,000 repeated updates to one million inputs. The engine composes the updates into one exact transformation per input, so the rate counts the logical steps that transformation represents.

[Original Colab notebook](https://colab.research.google.com/drive/18RIQSLf4Vf5xUnwEmDwYNPJDLSM_Stqa?usp=sharing) · [Original source (v0.4.1)](https://github.com/playfularchitect/rns_engine/tree/v0.4.1) · [CPU benchmark details](https://github.com/playfularchitect/rns_engine/tree/main/benchmarks/cpu)

## Exact matrix multiplication on NVIDIA Tesla T4

The published benchmark compared RNS Engine's exact matrix multiplication (GEMM) with NVIDIA **cuBLASLt FP16-input GEMM** across 1,024 shapes on a Tesla T4.

| Exact workload | Wins | Losses | Ties | Errors | Geometric mean speedup |
| --- | ---: | ---: | ---: | ---: | --- |
| Signed INT8 inputs, INT32 output | **938** | 86 | 0 | 0 | **1.235×** across all shapes |
| Shared-scale rational inputs | **870** | 110 | 41 | 3 | **1.417×** across wins |

Shared-scale rational matrices use one common denominator per matrix. The integer benchmark timed GPU-resident computation. The rational benchmark also included the bookkeeping needed to return the exact rational result. Python call overhead and host–device transfers are outside the comparison.

[T4 benchmark methodology and evidence](https://github.com/playfularchitect/rns_engine/tree/main/benchmarks/t4)

## Repository

- [Source](https://github.com/playfularchitect/rns_engine/tree/main/src/rns_engine) contains the native CPU core, Python API, and matrix multiplication implementations.
- [Benchmarks](https://github.com/playfularchitect/rns_engine/tree/main/benchmarks) contains CPU reproduction material and the frozen Tesla T4 evidence.
- [Tests](https://github.com/playfularchitect/rns_engine/tree/main/tests) covers arithmetic, exactness, and packaging.

Licensed under [AGPL-3.0-only](https://github.com/playfularchitect/rns_engine/blob/main/LICENSE).
