Skip to main content

NanoGEMM ⚡

PyPI CI License: MIT Python SIMD Open In Colab Footprint Dev.to

NanoGEMM is a minimalist, bare-metal General Matrix Multiplication (GEMM) engine designed for sub-microsecond CPU inference and high-performance computing in Python.

Built with direct AVX2 / FMA (256-bit SIMD) assembly-level register tiling and cache blocking, NanoGEMM eliminates the heavy function-call dispatch, thread-pool barriers, and memory-packing overhead of heavyweight BLAS libraries (OpenBLAS, MKL) for small-to-medium tensors.


🚀 Performance Benchmarks

Measured on Intel/AMD x86-64 CPU (AVX2 + FMA) against NumPy 2.2.3 (single-precision float32):

Matrix Dimension NumPy 2.2.3 Latency NanoGEMM Latency Speedup Factor NanoGEMM Throughput
16 x 16 3.21 µs 1.23 µs (C: 0.65 µs) 🚀 2.83x FASTER 2.83 GFLOPS
32 x 32 5.75 µs 2.74 µs (C: 2.18 µs) 🚀 2.26x FASTER 15.13 GFLOPS
64 x 64 18.70 µs 17.76 µs (C: 16.39 µs) 🚀 1.10x FASTER 27.08 GFLOPS
128 x 128 114.07 µs 182.46 µs 0.60x 23.42 GFLOPS
256 x 256 426.24 µs 1501.24 µs 0.28x 22.35 GFLOPS

💡 Why is NanoGEMM faster on small/medium matrices?
Traditional BLAS engines incur 3–10 µs of fixed overhead per invocation due to dynamic runtime dispatch, argument sanitization, thread synchronization, and packing buffers. NanoGEMM utilizes a zero-allocation, direct register-tiled microkernel that executes in sub-microsecond time immediately upon invocation.


🛠 Architectural Design

1. Register Tiling ($6 \times 16$ Microkernel)

  • Register allocation: Utilizes 12 ymm registers (ymm0ymm11) as 256-bit floating-point accumulators storing a $6 \times 16$ tile of matrix $C$.
  • Vector broadcast & FMA: Two ymm registers load vectors from $B$, while individual scalar elements of $A$ are broadcast across ymm using _mm256_set1_ps and accumulated via fused multiply-add (_mm256_fmadd_ps).
  • Zero Spilling: Fits completely inside the 16 available x86-64 YMM registers without stack eviction.

2. Multi-Level Cache Blocking

  • $L_1$ / $L_2$ Cache Tiling: Matrices are processed in cache blocks ($M_c = 64, N_c = 128, K_c = 128$) to maintain maximum L1/L2 data cache hit ratios and eliminate memory bus thrashing.
  • Vectorized Edge Handling: Arbitrary matrix dimensions (non-multiples of 6 or 16) are processed using boundary SIMD edge loops without padding or buffer allocations.
       Matrix A (M x K)              Matrix B (K x N)
     [ . . . . . . . . ]           [ . . . ymm0 . . . ]
     [ . . . . . . . . ]           [ . . . ymm1 . . . ]
     [ a0 a1 a2 a3 . . ]     x     [ . . . . .  . . . ]
     [ . . . . . . . . ]           [ . . . . .  . . . ]
     [ . . . . . . . . ]
             │                             │
             └──────────────┬──────────────┘
                            ▼
                Matrix C (6 x 16 Tile)
             [ ymm0  ymm1  ] -> Row 0
             [ ymm2  ymm3  ] -> Row 1
             [ ymm4  ymm5  ] -> Row 2
             [ ymm6  ymm7  ] -> Row 3
             [ ymm8  ymm9  ] -> Row 4
             [ ymm10 ymm11 ] -> Row 5

📦 Installation & Quickstart

Installation via PyPI (Recommended)

pip install nanogemm
# or with uv
uv add nanogemm

Python Compatibility: Fully tested and verified across Python 3.9 through 3.15 (including 3.15.0rc2).

Build from Source

git clone https://github.com/eminsk/nanogemm.git
cd nanogemm
pip install -e .

Python Usage

import nanogemm as ng
import numpy as np

# Verify SIMD hardware acceleration
print("Active ISA:", ng.get_simd_isa())
# Output: Active ISA: AVX2+FMA (256-bit SIMD, 6x16 register tiling)

# Allocate input matrices
A = np.random.randn(32, 64).astype(np.float32)
B = np.random.randn(64, 128).astype(np.float32)

# Direct hardware-accelerated MatMul: C = A @ B
C = ng.matmul(A, B)

# Or with pre-allocated zero-copy output buffer for maximum performance:
out = np.empty((32, 128), dtype=np.float32)
ng.matmul(A, B, out=out)

# Standard BLAS SGEMM interface: C = alpha * (A @ B) + beta * C
res = ng.sgemm(A, B, alpha=2.0, beta=0.5, c=out)

🧪 Testing & Verification

Run the comprehensive correctness test suite comparing NanoGEMM with NumPy reference outputs across random uniforms, normals, non-square dimensions, and prime shapes:

python tests/test_correctness.py

Run the official benchmark against your installed NumPy BLAS:

python benchmarks/bench_vs_numpy.py

🌐 High-Performance Systems Ecosystem

NanoGEMM is developed by @eminsk as part of an engineering ecosystem focused on low-level hardware performance, assembly programming, and native desktop computing:

  • 🎥 screenvideo — Lightweight desktop screen recorder featuring WASAPI loopback audio and a standalone pure x64 Flat Assembler (FASM) native edition.
  • 📊 xlsx_vievers — Desktop spreadsheet processor with 80+ formula functions, Chart Wizard, and hardware-accelerated SIMD SSE2 math engine.
  • 📈 yfinance-ta-patterns — Candlestick pattern scanner and AI ranking suite powered by TA-Lib and quantitative backtesting.
  • 🔍 StackOverflowAPI — Desktop client for Stack Overflow built with CustomTkinter and native FASM x64 search client.

📄 License

MIT License — Copyright (c) 2026 eminsk.

Download files

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

Source Distribution

nanogemm-0.2.0.tar.gz (57.2 kB view details)

Uploaded Source

Built Distributions

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

nanogemm-0.2.0-cp315-cp315-win_amd64.whl (56.8 kB view details)

Uploaded CPython 3.15Windows x86-64

nanogemm-0.2.0-cp314-cp314-win_amd64.whl (56.8 kB view details)

Uploaded CPython 3.14Windows x86-64

nanogemm-0.2.0-cp313-cp313-win_amd64.whl (55.5 kB view details)

Uploaded CPython 3.13Windows x86-64

nanogemm-0.2.0-cp312-cp312-win_amd64.whl (55.5 kB view details)

Uploaded CPython 3.12Windows x86-64

File details

Details for the file nanogemm-0.2.0.tar.gz.

File metadata

  • Download URL: nanogemm-0.2.0.tar.gz
  • Upload date:
  • Size: 57.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nanogemm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4212043de169d745feeb31ae9d9ad5a546e633059c2e0b3d3bb61691f948895f
MD5 73053d8de04dd868a4da2b3ee54db282
BLAKE2b-256 7b3444d7181304d9f6a25f5a67bea4561e8ce573f36b4608255e2779f7a5508b

See more details on using hashes here.

File details

Details for the file nanogemm-0.2.0-cp315-cp315-win_amd64.whl.

File metadata

  • Download URL: nanogemm-0.2.0-cp315-cp315-win_amd64.whl
  • Upload date:
  • Size: 56.8 kB
  • Tags: CPython 3.15, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nanogemm-0.2.0-cp315-cp315-win_amd64.whl
Algorithm Hash digest
SHA256 b5e18a8099e1fe4dcf3ed8b4cbc1e8296c4d5beb25a32e695c1e1dbffe9160a1
MD5 df73cf1d568b2ab943c415fe2d8f142c
BLAKE2b-256 e834fc55c534279d71ec463bb80bf4c01d1859554225f2fe0b16527ae6fe32dd

See more details on using hashes here.

File details

Details for the file nanogemm-0.2.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: nanogemm-0.2.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 56.8 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nanogemm-0.2.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 75c0c4659fc7365c186322c5697c900ac56c701918639c1e02f81c0e65341c02
MD5 1789d5282fe765878fc1b7407c908572
BLAKE2b-256 ed344ba8a9e7515c6f55ae950a1af16d4a74f7d852a7466bc079c7aa8064d946

See more details on using hashes here.

File details

Details for the file nanogemm-0.2.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: nanogemm-0.2.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 55.5 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nanogemm-0.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 0ed8dedccc4d8928eb6dd3d84389c1f1848e46c80a762c45682aadc6fd6102f4
MD5 84f6bd1d04b0af509efb864761148073
BLAKE2b-256 b911aff176d7a396596882c4a13d8152fe6fa3575d17ec985f089975f565eb7e

See more details on using hashes here.

File details

Details for the file nanogemm-0.2.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: nanogemm-0.2.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 55.5 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nanogemm-0.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 2cda21bfd3c2da1e9f5e00cd01e652da199b6eaf48b2b0ae732f1ab9b12cbc9d
MD5 03eedfc7673e1324f849a3fae337ecfd
BLAKE2b-256 6e4b3f102729d3b57f5975eabae1a292b193de24e0cd860271af3f72b8f919dd

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.0 This release

5 files

0.1.0

1 file

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