Chapter 9 of 139 min read
The same kernels on a second architecture
The 49 kernels for x86-64 with AVX2, written against the same specs and judge as ARM64: what carried over, what did not, and what they are worth where NumPy already has vector loops.
The chapters before this one were measured on an Apple M1 Pro. Every kernel also exists for x86-64 with AVX2, written against the same specs and the same judge, and the package serves Linux on x86-64 as it serves Apple Silicon. This chapter is what that showed: which ideas carried over, which did not, and what the same 49 kernels are worth on a processor where NumPy is a different opponent.
Written again, not translated
The specs and the reference C are shared; the assembly is not. A kernel’s contract, its oracle and its correctness mode are the same files on both architectures. Its kernel.s is written from the idea and the proofs of the ARM64 one, not from its instructions, and an attempt rejected on one processor is not skipped on the other.
- Machine. Intel Core i5-14600KF, one performance core, Linux. Instruction set: the x86-64-v3 level (AVX2, FMA, BMI2), which the judge enforces and which
install()asks the processor for. - The loop was the same. 191 logged attempts across the 49 kernels on x86-64: 142 kept and 49 reverted. Of those taken to
judge compare, 95 came back significantly faster, 18 significantly slower and 21 with no significant difference. The logs are beside each kernel, inkernels/<name>/x86_64/attempts.md. - Fewer attempts than on ARM64 (442 there). The ideas were already known; what had to be found again was what this processor makes cheap and what it makes dear.
A different opponent
NumPy already has AVX2 loops for argmax, clip and astype here, which were scalar or slow on the M1. And a chain of scalar additions costs one cycle per element on this core against three on the M1, while the caches deliver about the same. Two things follow.
- Where NumPy is still scalar, the gains are of the same kind as on ARM64: mask selection, masked assignment,
isin,histogram, the searches. - Where the compiler’s loop is already at the speed of memory, there is little left. With the data in the second-level cache a plain copy takes 0.115 ns per 8 bytes and a running sum of
int64in C takes 0.19. No kernel can do better than the copy, socumsumonint64gains 1.18x over the C where the ARM64 kernel gains 4x.
The numbers
Each row is one locked run of uv run judge run on that machine, taken on main with every kernel merged. The first two columns of figures are geometric means over eleven sizes from 16 to 16,777,216 elements; NumPy is timed from Python, where a call costs half a microsecond, so the last column, at one large size, is the one that compares loops.
| Kernel | Input | vs naive C | vs NumPy | vs NumPy at 1,048,576 |
|---|---|---|---|---|
argmax_f32 |
random | 12.9x [12.8 – 12.9] | 7.46x | 1.90x |
argmax_f64 |
random | 6.61x [6.55 – 6.65] | 5.93x | 1.72x |
argmin_f32 |
random | 13.1x [13.1 – 13.2] | 7.51x | 1.90x |
argmin_f64 |
random | 6.82x [6.81 – 6.82] | 6.08x | 1.87x |
compress_f32 |
half / 5% selected | 2.38x / 2.89x | 37.5x / 14.8x | 32.7x / 12.6x |
compress_f64 |
half / 5% selected | 1.37x / 2.74x | 17.6x / 12.4x | 16.6x / 9.38x |
nonzero_bool |
half / 5% true | 1.78x / 4.57x | 8.65x / 21.6x | 3.16x / 19.8x |
maskfill_f64 |
half / 5% selected | 5.85x / 2.58x | 20.8x / 12.4x | 18.4x / 7.30x |
isin_i64 |
values 0 to 999 / whole int64 range | 1.95x / 1.08x | 83.2x / 20.2x | 39.8x / 3.78x |
searchsorted_f64 |
64 / 1,000 needles | 3.42x / 9.56x | 6.13x / 9.76x | 2.90x / 6.19x |
searchsorted_right_f64 |
64 / 1,000 needles | 3.45x / 8.78x | 6.66x / 10.8x | 2.78x / 6.58x |
interp_f64 |
64 / 1,000 queries | 2.48x / 8.02x | 4.72x / 9.63x | 3.14x / 10.6x |
histogram_f64 |
random | 6.94x [6.93 – 6.94] | 29.2x | 7.49x |
bincount_u8 |
random | 1.04x [1.04 – 1.05] | 7.54x | 4.18x |
cumsum_i64 |
random | 1.18x [1.17 – 1.18] | 13.9x | 5.34x |
cumsum_f64 |
random | 1.81x [1.81 – 1.81] | 16.1x | 6.09x |
cumsum_f32 |
random | 2.96x [2.96 – 2.96] | 26.7x | 13.3x |
sum_u8 |
random | 6.23x [6.22 – 6.24] | 112x | 42.1x |
count_nonzero_f64 |
random | 1.06x [1.06 – 1.06] | 10.3x | 4.36x |
count_nonzero_i64 |
random | 1.21x [1.20 – 1.21] | 4.31x | 1.26x |
clip_f32 |
all six tables | 1.30x [1.30 – 1.30] | 13.1x | 1.05x (bounds -0.5, 0.5, random) |
clip_f64 |
all six tables | 1.09x [1.09 – 1.09] | 6.78x | |
cast_u8_f32 |
random | 1.21x [1.20 – 1.21] | 6.10x | 1.05x |
cumsum_bool |
random | 1.93x [1.92 – 1.94] | 38.8x | 11.9x |
where_f64 |
both tables | 1.37x [1.37 – 1.37] | 8.09x | 5.53x (half true), 1.54x (95% true) |
cummax_i64 |
random / rising | 1.21x / 1.12x, all 1.16x [1.16 – 1.16] | 12.3x / 11.7x | 5.97x / 5.53x |
cumfmax_f64 |
random / rising / 5% NaN | 8.90x / 8.53x / 8.25x, all 8.55x [8.54 – 8.56] | 12.9x / 12.4x / 14.8x | 5.94x / 4.85x / 7.00x |
fmax_reduce_f64 |
random / 5% NaN | 6.62x / 6.64x, all 6.63x [6.63 – 6.63] | 6.24x / 6.85x | 1.22x / 1.21x |
any_rows_bool |
sparse, rows of 22 / 4 / 64 | 5.04x / 17.6x / 3.78x, all 6.94x [6.92 – 6.96] | 91.7x / 304x / 75.4x | 57.1x / 288x / 33.5x |
bincount_i16 |
uniform, 64 / 1,000 counters; four values, 64 / 1,000 | 1.23x / 1.04x / 1.62x / 1.21x, all 1.26x [1.26 – 1.26] | 8.76x / 5.82x / 7.91x / 5.08x | 5.46x / 3.66x / 6.07x / 3.53x |
bincount_i64 |
uniform, 64 / 1,000 counters; four values, 64 / 1,000 | 1.13x / 1.01x / 1.33x / 1.18x, all 1.16x [1.16 – 1.16] | 4.26x / 3.49x / 4.46x / 3.39x | 3.04x / 2.32x / 3.75x / 2.80x |
unique_i64 |
two / ten values, a range of ten; two / ten values, a range of 256 | 2.74x / 2.84x / 3.19x / 3.27x, all 3.00x [2.97 – 3.04] | 175x / 158x / 137x / 127x | 83.2x / 80.2x / 75.3x / 70.4x |
searchsorted_i64 |
64 / 1,000 needles | 2.86x / 11.1x, all 5.63x [5.62 – 5.64] | 5.11x / 2.65x | 2.07x / 1.45x |
searchsorted_right_i64 |
64 / 1,000 needles | 2.85x / 8.76x, all 5.00x [4.99 – 5.00] | 5.17x / 2.66x | 2.20x / 1.48x |
any_cols_bool |
sparse, rows of 31 / 8 / 64 / 256 | 8.55x / 14.2x / 4.81x / 1.71x, all 5.72x [5.69 – 5.74] | 80.2x / 183x / 61.7x / 28.1x | 37.5x / 135x / 19.2x / 6.3x |
maskfill_f32 |
half / 5% selected | 1.07x / 1.05x, all 1.06x [1.06 – 1.06] | 50.5x / 19.7x | 35.8x / 10.9x |
isin_i32 |
values 0 to 999 / whole int32 range | 1.76x / 1.17x, all 1.44x [1.44 – 1.44] | 126x / 29.9x | 108x / 5.28x |
interp_f64 is timed against a reference that had to be corrected first: NumPy’s x86-64 build rounds the last multiply and add of np.interp separately, its ARM64 build fuses them, and the shared reference had only the second.
From Python, with the binding and NumPy’s own call path on both sides, at 1,048,576 elements on the same machine (NumPy 2.5.5, Python 3.13.15):
| Statement | Data | Faster by |
|---|---|---|
np.argmax(x) |
float32 | 1.88x |
x.argmax() |
float32 | 1.79x |
np.argmax(x) |
float64 | 1.79x |
np.argmin(x) |
float32 | 1.81x |
np.argmin(x) |
float64 | 1.79x |
np.clip(x, -0.5, 0.5) |
float32 | 1.04x |
np.clip(x, -0.5, 0.5) |
float64 | 1.05x |
np.clip(x, 0, 1) |
float64 | 1.03x |
x.clip(-0.5, 0.5) |
float32 | 1.04x |
x[mask] |
float32, half selected | 14.66x |
x[mask] |
float64, half selected | 10.11x |
x[mask] |
float64, few selected | 7.03x |
x[mask] |
int64, half selected | 10.35x |
x[mask] = 0.0 |
float64, half selected | 17.69x |
np.compress(mask, x) |
float64, half selected | 2.04x |
np.extract(mask, x) |
float64, half selected | 2.07x |
x[mask] = 0.0 |
float64, few selected | 6.42x |
x[mask] = 0.0 |
float64, two-dimensional, half selected | 17.64x |
x[mask] |
float64, two-dimensional, half selected | 10.33x |
np.putmask(x, mask, 0.0) |
float64, half selected | 15.53x |
np.copyto(x, 0.0, where=mask) |
float64, half selected | 18.41x |
np.nonzero(mask) |
bool, half selected | 1.58x |
np.nonzero(mask) |
bool, few selected | 12.31x |
np.where(mask) |
bool, half selected | 1.58x |
np.where(mask, a, b) |
float64, half true | 5.92x |
np.where(mask, a, np.nan) |
float64, mostly true | 2.54x |
np.cumsum(x) |
int64 | 5.98x |
np.maximum.accumulate(x) |
int64 | 5.19x |
np.fmax.accumulate(x) |
float64 | 5.07x |
np.fmax.reduceat(x, starts) |
float64, runs of 16, 5% NaN | 11.98x |
np.cumsum(mask) |
bool, half selected | 9.05x |
m.any(axis=1) |
bool, 22 columns, few True | 42.57x |
np.any(m, axis=1) |
bool, 22 columns, few True | 42.54x |
np.cumsum(x) |
float32 | 12.68x |
np.cumsum(x) |
float64 | 6.93x |
x.astype(np.float32) |
uint8 | 1.04x |
np.sum(x) |
uint8 | 41.68x |
x.sum() |
uint8 | 41.47x |
np.sum(mask) |
bool, half selected | 12.50x |
mask.sum() |
bool, half selected | 12.49x |
np.count_nonzero(x) |
int64 | 1.24x |
np.count_nonzero(x) |
float64 | 4.28x |
np.histogram(x, 50, (-3.0, 3.0)) |
float64 | 7.21x |
np.bincount(x) |
uint8 | 4.13x |
np.bincount(x) |
int16, codes 0 to 63 | 4.54x |
np.bincount(x) |
int64, codes 0 to 63 | 1.93x |
np.isin(x, eight_values) |
int64, values close together | 34.58x |
np.searchsorted(a, v) |
float64, as many needles as elements | 3.94x |
np.searchsorted(a, v) |
float64, 1,000 elements | 4.90x |
np.searchsorted(a, v, side='right') |
float64, as many needles as elements | 3.66x |
np.searchsorted(a, v, side='right') |
float64, 1,000 elements | 4.78x |
np.digitize(x, bins) |
float64, 1,000 bin edges | 4.76x |
np.interp(x, xp, fp) |
float64, as many queries as points | 5.58x |
np.interp(x, xp, fp) |
float64, 1,000 points | 3.64x |
No statement a kernel takes is slower than NumPy at any of the three sizes timed.
What carried over, and what did not
The shapes carried over. Reduce blocks and find the position afterwards; store without asking and advance by the count; build what one output adds to the last from the input alone; run many searches in step. So did every proof: where infinities land in a regrouped running sum, and which elements of a histogram need no edge correction.
The instructions did not, and several ARM64 caveats went with them.
clipis two instructions and one path.vmaxps a, breturns its second operand when the two are unordered or equal, zeros of either sign included, which is exactly C’sa > b ? a : b. With the bound first, a maximum and a minimum compute NumPy’s result bit for bit for every ordered pair of bounds. The ARM64 kernel needs a path chosen by the bounds, a second look at data with negative zeros, and the floating-point status register.- A mask is 32 bits in one instruction.
vpmovmskbturns 32 mask bytes into a word,tzcntandblsrwalk its set bits, and a table indexed by a byte of it permutes eight elements to the front. The mask kernels have no weak spot near 10% density, and a mask byte is true when it is not zero, whatever its value. argmaxis one pass with a bounded second look. The accumulators are never reset, so one compare per block says whether the block holds a new maximum, and only that block is looked at again.vmaxpsloses a NaN, whichfmaxon ARM64 does not, so the NaN test is its own compare.histogramhas no divide per element. A divide of four doubles takes eight cycles here. The proof only ever needed an estimate within a known distance of the true position, so the kernel multiplies by a scale computed once, and reads bin and fraction out of one conversion to fixed point.- The searches run 64 at a time, or 512. Four probes share one
vgatherqpd. The chain from one step of a search to the next is about thirty cycles and a gather can start every two or three, so sixteen searches in step, the ARM64 kernel’s first width, left most of the processor idle.
Some things this processor punishes that the M1 does not: a 32-byte load or store that straddles two cache lines, and a store that lands just behind the data about to be read, which the processor mistakes for a store to it. Kernels that copy or accumulate into an output of the input’s size load a block ahead of their stores for that reason.
Fusion on x86-64
The lazy layer’s prototype and its fused kernels (chapters 10 to 12) run on this processor too. Three of the eighty-one fused kernels are AVX2 assembly, for np.sqrt(a), s / np.sqrt(a) and np.where(a <= s, b, c); the other seventy-eight are built from their C references, which clang vectorises about as well as the AVX2 written for them. What fusion saves is passes over memory, not instructions, so the gains are about those of ARM64. Fused with install() in effect, against plain NumPy, on the same i5-14600KF pinned to one core with one BLAS thread, the cavity flow runs 3.09x and the horizontal diffusion 2.31x, where install() alone gives 1.00x and 1.06x; four of the twenty-six programs are behind NumPy by 3% to 15%. The ten programs from other codebases run 0.94x to 2.04x of NumPy. The tables are at the end of chapter 12 and in docs/lazy-fusion-design.md.
What it does not say
- One machine again. An Intel performance core. Gathers, which the search kernels use, are slow on some other x86-64 processors; nothing here was measured on one.
- Four kernels beat the compiler by little:
count_nonzeroonfloat64,bincount,cliponfloat64and theuint8cast. Shipping the C for them would be defensible. - Narrower than the number:
isinon int64 is fast only for test values from 0 to 32,767 here (on int32, added later, for any eight within 65,533 of each other), and with 64 needles against a very large array the searches are level with NumPy, because the judge repeats the same needles and a branching search learns them.
The full account, per family and with what was tried and thrown away, is in docs/kernel-findings.md and docs/x86-64-design.md.