Skip to main content

SpearVM

CI

Accélérateur mathématique SIMD — noyaux transcendantals vectorisés AVX2, précision certifiée, matmul tuilé, couche FFN fusionnée avec backprop, mini-JIT x64 pour programmes élément-par-élément. Python/C.

pip install spur-math

Windows : wheel binaire prêt. Linux/macOS : le sdist embarque les sources C ; au premier import, spur_math se compile tout seul si gcc/clang est présent (cache dans spur_math/_native/), sinon suivez le message d'aide. CPU requis : AVX2 + FMA (Haswell 2013+, Zen 2017+) — vérifié à l'import.

Speedups mesurés (4M éléments, OpenMP multi-cœur)

Noyau Natif libm AVX2 SPEAR Speedup
GELU 120 ms 8 ms ×15.0
ERF 306 ms 9 ms ×34.0
TANH 234 ms 9 ms ×26.0

v0.4 — fp32 ×2, backprop complète, map 4 entrées

  • float32 natif : matmul_nt, matmul_nt_gelu, gelu, gelu_backward acceptent float32 (8 lanes/vector) → ×2.00 vs f64 mesuré (13.5 → 26.8 GFLOPS @1024³). Dispatch automatique sur le dtype numpy.
  • Backward erf/tanh/sigmoid : gradients exacts des approximations rationnelles, gradcheck ~1.6e-09. Training complet possible : erf_backward, tanh_backward, sigmoid_backward.
  • Map kernel 4 entrées : out[i] = F(a[i], b[i], c[i], d[i]) via tableau de pointeurs ; spur_free(handle) + slots réutilisables ; builds sérialisés (SRWLOCK) — exec réentrant.

Matmul (bench_mm)

examples/bench_mm.c — C = A·B double, validation vs naif incluse.

n naif ikj dot scalaire AVX2 dot AVX4 bloc + OMP
128 2.0 ms (2.1 GF) ×0.12 ×0.45 ×0.73 (série)
256 23 ms (1.5 GF) ×0.24 ×1.02 ×1.61
512 198 ms (1.4 GF) ×0.31 ×0.37 ×2.56 (3.5 GFLOPS)
  • err ≤ 1.6e-14 sur toutes les variantes
  • blocage registres 4 lignes : chaque ligne de Bᵀ sert 4 rangs de A, 4 chaînes FMA indépendantes
  • OpenMP activé seulement si n ≥ 192 ; lancer avec OMP_WAIT_POLICY=ACTIVE
  • timings VM bruités (±2× entre runs), gains observés jusqu'à ×12 à n=256 machine au repos

Matmul API (v0.2+)

sm.matmul_nt(A, B) — C = A·Bᵀ, convention BLAS NT, double précision.

  • Tuillage cache KC×NC (512 Ko/tuile en L2) + blocage registres 4 lignes, 4 chaînes FMA indépendantes, OpenMP
  • err ≤ 1.4e-13 vs numpy sur toutes tailles (512→2048), y compris queues m%4
  • matmul_nt_gelu : activation fusionnée — la gelu est non linéaire donc k n'est pas coupé, seulement le blocage colonnes
Cas SpearVM numpy BLAS
carré 512 24.5 GFLOPS 48.2 GFLOPS (×0.51)
carré 1024 13.9 GFLOPS 18.0 GFLOPS (×0.77)
carré 2048 10.3 GFLOPS 23.5 GFLOPS (×0.44)
FFN 1024×768×3072 fusionné gelu 353 ms 858 ms (×2.43)

Lecture honnête : en GEMM carré pur, OpenBLAS/MKL reste devant (packing AVX, microkernels plus larges). Là où SpearVM gagne, c'est le pipeline fusionné — un passage au lieu de deux, zéro buffer intermédiaire — et l'intégration NT sans copies.

Pipeline NN end-to-end (bench_nn)

Y = gelu(X · Wᵀ) — pattern FFN transformer. M=1024, K=768, N=3072 (4.8 GFLOP).

Implémentation Total Détail
SpearVM (matmul_nt_gelu, tuilé) 353 ms (×2.43 vs numpy) 13.7 GFLOPS équivalents, err 0.0797
  • err max vs référence exacte : 0.0797 (= contrat datasheet gelu ≤0.079)
  • la forme linéarisée sature proprement hors domaine : err bornée ~0.002·|x| jusqu'à ±25
  • validation : bin/bench_nn.exe puis python examples/check_nn.py
gcc -O3 -mavx2 -mfma -fopenmp examples/bench_nn.c src/spur_kernels.c -o bench_nn -lm
OMP_WAIT_POLICY=ACTIVE ./bench_nn && python examples/check_nn.py

Précision (datasheet)

Chaque noyau documente son erreur max vs IEEE :

  • gelu : ≤ 0.079 sur [-2, 2]
  • erf : ≤ 0.011 sur [-2, 2]
  • tanh : ≤ 0.009 sur [-3, 3]
  • lse2 : hard-max (écart ≤ ln 2)

Build

make              # DLL + tests
gcc -O3 -mavx2 -mfma -fopenmp src/spur_kernels.c -shared -o spur_kernels.dll

Installation Python

pip install spur-math          # apres publication PyPI

Ou depuis les sources (compile la DLL puis installe) :

python build_package.py --install

Note : le wheel actuel embarque un binaire Windows x64. Sur Linux/macOS, compilez le .so vous-meme avec la ligne gcc ci-dessus (le package affiche la commande exacte si la lib manque).

def gelu(x): """GELU approximatif AVX2. Erreur max 0.079 sur [-2, 2], sature proprement au-dela."""


## Training (backprop)

```python
import spur_math as sm

# forward fusionne
Y = sm.matmul_nt_gelu(X, W)            # Y = gelu(X . W^T), un seul passage

# backward — les gradients passent par le meme matmul
T  = sm.matmul_nt(X, W)
dY = dLdY * sm.gelu_backward(np.ones_like(T), T)   # ou directement gelu_backward(dLdY, T)
dW = sm.matmul_nt(dY.T, X.T)
dX = sm.matmul_nt(dY, W.T)

Gradients verifies par differences finies (err ~1e-10) et SGD convergeant (loss /87 en 300 pas, tests/test_train.py).

Usage Python

import ctypes
lib = ctypes.CDLL("spur_kernels.dll")
# voir examples/bench_kernels.c pour l'API C complète

Architecture

src/spur_kernels.c    Kernels AVX2 vectorisés + OpenMP
include/spur.h        API publique
examples/             Benchmarks et démos
tests/                Tests de correction

Limitations

  • Windows x64 uniquement
  • Précision approximative (±0.01–0.09 selon noyau)
  • Pas de support GPU

Licence

MIT

Metadata

Release files for spur-math 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spur-math 0.4.2
File Size Uploaded Provenance Actions
spur_math-0.4.2.tar.gz 320.2 kB Without provenance Details for spur_math-0.4.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for spur-math 0.4.2
File Interpreter ABI Platform Actions
spur_math-0.4.2-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for spur_math-0.4.2-py3-none-win_amd64.whl

Total release size: 623.3 kB

Release files / spur_math-0.4.2.tar.gz

Download URL spur_math-0.4.2.tar.gz
Size 320.2 kB
Tags Source
SHA-256 checksum
How to use checksums
14865bfc835b075260046c5186ef29567ba1efd266b7b1bc17eeacd6f6bdc8d7
BLAKE2b-256 checksum
How to use checksums
1fb5abd4cee062998fafbe487e8b0e8b767efbcf6e459f9ce6643aa158918fb5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.4

Release files / spur_math-0.4.2-py3-none-win_amd64.whl

Download URL spur_math-0.4.2-py3-none-win_amd64.whl
Size 303.1 kB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
9d9bcea341fa394ebd8f4d37328d2b8815a3d70d1e2a7ad5add8972dc9af5b81
BLAKE2b-256 checksum
How to use checksums
95fb9ab585df31e8c45bb47f9d481485b8285908d861a8c4de05411fa06e43fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.4

Security for spur-math 0.4.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page