hotkernels

NumPy on Apple Silicon and x86-64 Linux

The slow parts of NumPy, rewritten underneath it.

hotkernels replaces the loops inside 23 NumPy operations, and four functions of bottleneck, with hand-written kernels: ARM64 NEON on Apple Silicon and x86-64 AVX2 on Linux. Your code does not change. Every kernel is checked against NumPy's own output, and a call no kernel takes costs about a nanosecond more than before.

import numpy as np
import hotkernels

hotkernels.install()

x = np.random.standard_normal(1_000_000)
x[x > 0]           # 16x faster
np.clip(x, -1, 1)   # 5x faster
np.argmax(x)       # 7x faster
49kernels for 23 NumPy operations and four functions of bottleneck
5x+faster than NumPy for 29 of the 41 measured on an Apple M1 Pro, at a million elements
47 / 49return NumPy's result, or bottleneck's, bit for bit; the other two are opt-in
~1 nsadded to a call that no kernel takes

How much faster

Each bar is one NumPy call on an array of 1,048,576 elements, timed as a native function against NumPy’s own loop on an Apple M1 Pro. Switch to clang -O3 to see the same kernels against the plain C loop a compiler makes of the operation: that comparison is the one that says whether writing assembly was worth it. The x86-64 kernels, timed on an Intel Core i5-14600KF, have a chapter of their own.

Speedup of each kernel at 1,048,576 elements. Read from the kernel table in the repository's README when this site was built. The results chapter has every size, the confidence intervals, and the timings from Python.

What stands behind the numbers

There are limits, and the site does not hide them: each figure is from one machine and says which, an Apple M1 Pro for the ARM64 kernels, an Intel Core i5-14600KF for the x86-64 kernels, and an Apple M4 Max for the workloads tracked over time; one or two element types per operation, and a few results that depend on the arguments. They are set out in what the numbers do not say.

Whole expressions, fused (prototype)

install() makes each NumPy call faster on its own, so a * b + c is still two calls and two passes over memory. The repository also holds a prototype that computes such an expression whole. A lazy stand-in for numpy writes the program down as a graph, and fusion computes each float64 expression in it in one pass, with eighty-one kernels checked by the judge and NumPy’s results bit for bit. It runs with install() in effect: what fusion cannot take goes to the package’s kernels, and the rest to NumPy. It is not part of pip install hotkernels; Get started says how to try it from a clone.

Whole programs at their large size, fused with install() in effect, against plain NumPy, with install() alone beside it:

Program M1 Pro: fused M1 Pro: install() alone i5-14600KF: fused i5-14600KF: install() alone
Cavity flow, a stencil solver from NPBench 2.80x 1.00x 3.09x 1.00x
Horizontal diffusion, NPBench 2.34x 1.05x 2.31x 1.06x
Veros, equation of state 1.68x 0.97x 1.69x 1.00x
Particle filter 1.43x 1.12x 1.18x 1.09x
scikit-learn metrics 3.50x 3.56x 3.19x 3.24x
Character RNN, small arrays 0.98x 1.12x 0.97x 1.15x

Fusion gains where a program is expressions on large arrays. Where a program’s gain is single calls, it is install()'s, and the fused run keeps it. On small arrays the graph costs more than it saves. All twenty-six programs, the ten from other codebases and what is left are in what fusion gained.

How it was built, and why you might care

The kernels were written by coding agents, not by hand. An agent edits one assembly file, runs an automated judge, and keeps the edit only if the judge finds it correct and significantly faster. Nobody reviews the assembly line by line, so the judge is the only thing between a subtly wrong kernel and your data, and most of the engineering went into making it trustworthy.

The report walks through all of it from the ground up. It assumes you know NumPy well and have never read a line of assembly.

Where the time goes

A NumPy call is a fixed cost plus a loop. Which loops are slow, and which are already at the limit.

Chapter 1 →

The machine, in fifteen minutes

Vector registers, dependency chains, branch prediction and caches: the four ideas every optimisation here rests on.

Chapter 2 →

The judge

Guard pages, thousands of generated cases per kernel, and a bootstrap test that decides what counts as faster.

Chapter 3 →

Agents in a loop

442 logged attempts, 155 of them thrown away. Browse every one.

Chapter 4 →

The kernels

How x[mask] runs without a branch, why float cumsum cannot be both exact and fast, and a histogram that replaces arithmetic with a proof.

Chapters 5 and 6 →

Into NumPy

Seven kinds of hook that route unmodified code to the kernels, including x[mask], which is not a function at all, and a + b.

Chapter 7 →

Results, and what they do not say

Every number, where it came from, and the cases where the result is narrower than the headline.

Chapter 8 →

x86-64 with AVX2

The same 49 kernels for Linux on x86-64, against a NumPy that already has vector loops for some of these calls.

Chapter 9 →

A whole expression at once

A prototype, not in the package: NumPy code written down as a graph with NumPy's bits, and kernels that compute an expression from it in one pass.

Chapters 10 to 12 →

Over time

The workloads, run by run, with the merges that changed the package marked where they landed.

Chapter 13 →