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
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.
What stands behind the numbers
- Checked against NumPy, not against a description of it. Every kernel is compared with NumPy’s own output on inputs that include NaN, infinities, signed zeros, ties and denormals. Forty-seven of the 49 match bit for bit, 43 of them NumPy and 4 the functions of bottleneck they stand in for. The two float
cumsumkernels regroup the additions, which is where their speed comes from; they stay inside a declared error bound and are installed only on request. - Memory-safe by test. Each kernel is run with its buffers pressed against pages the process may not touch. Reading or writing one byte outside a buffer crashes the check.
- Assembly only where it wins. A kernel keeps its assembly only if it is measurably faster than the compiled C. Where it is not, the C ships.
- Nothing breaks when a kernel does not apply. A strided array, another element type, an argument a kernel does not implement: the call goes to NumPy exactly as written. With hotkernels installed, NumPy’s own test suite gives the same outcome for each of its 47,234 tests.
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.
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.
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 →