Repository navigation
hotkernels 0.4.0
Highlights
More of the calls real programs make are served. The calls were chosen from what scikit-learn, pandas, training loops and published benchmarks spend their time in. There are 18 new kernels, and seven changes make calls faster that were already served or were left to NumPy. The package now has 49 kernels where 0.3.1 had 31. Every figure below is from Python on an Apple M1 Pro, against plain NumPy, unless it names the Intel Core i5-14600KF.
-
Sorting and selecting.
np.argsort(x, kind="stable")onfloat64,int64anduint64is 13x faster at a million elements, and 28x on keys with few distinct values.np.medianonfloat64is 7.8x faster at a million elements and 53x at 16.np.unique(x)onint64labels close together, such as class labels, is 17x faster.
-
Reductions and searches over matrices.
a.cumsum(axis=0),a.cumprod(axis=0),np.maximum.accumulate(a, axis=0)andnp.fmax.accumulate(a, axis=0)on a C-orderedfloat64matrix are 20x to 25x faster at a million elements.m.any(axis=0)on a boolean matrix is 43x faster with 31 columns.np.nonzero(m)andnp.where(m)on a boolean matrix are 27x faster at a million elements with few True, andnp.argwhere(m)20x.np.argmax(m, axis=1)andnp.argmin(m, axis=1)are about 3.5x faster on rows of tenfloat64.
-
Sums.
np.sum(x)andx.sum()onfloat32andfloat64keep NumPy's result bit for bit. They are 23x faster at 16 elements, and 3.0x (float32) and 1.8x (float64) at a million. -
In place.
np.clip(g, lo, hi, out=g), the gradient-clipping idiom, used to go to NumPy and is now served: 16x faster at 16 elements and 2.5x at a million onfloat64.w -= bandw += bon largefloat64arrays right after a matrix product has readware about 5x faster on Apple Silicon. Elsewhere they run at NumPy's speed.
-
Searches.
np.searchsortedonint64is 2.2x faster with as many needles as elements, and about 2x with one needle at any length.np.searchsortedonfloat64withfloat32needles is 8x faster.
-
bottleneck. When bottleneck is installed, its
nanmean,nanstd,nanminandnanmaxalongaxis=0of afloat64matrix, which pandas and empyrical call, are served with bottleneck's results bit for bit. They are about 4x to 7x faster than bottleneck. -
Small arrays. Elementwise operations on up to 16,384 elements, such as
a + b,a > 0,np.exp(a)andx += y, skip most of NumPy's call path. At 16 elementsa + bis about 2x faster,a > 0about 3x andnp.exp(a)1.6x, with the same results and warnings. -
Linux x86-64. Several calls got faster on the Intel Core i5-14600KF, with the figures measured on it in their pull requests:
x[mask]on one-byte and two-byte types: 56x at a million elements onuint8.x[mask] = vonfloat32andint32: 31x onfloat32.np.isinonint32: 20x at a million elements.- A single
np.roundon afloat64scalar: 16x to 18x. np.uniqueonint64labels: about 39x at a million elements.
On Apple Silicon, the kernels for
x[mask]on one- and two-byte types, forx[mask] = vonfloat32andint32, and fornp.isinonint32are the same kernels compiled from C.np.uniquehas ARM64 assembly of its own. The scalarnp.roundis a loop inside hotkernels on both architectures.
One wrong result fixed. with hotkernels.enabled(inexact=True): inside an install() left the inexact np.cumsum kernels on after the block. Until the next install() or uninstall(), np.cumsum on float32 and float64 gave results within the inexact kernels' tolerance instead of NumPy's bit for bit. On leaving the block, hotkernels is now as the block found it.
One figure went down.
- What changed.
np.clip(x, lo, hi)on a million elements is 8.0x faster than NumPy onfloat32and 3.2x onfloat64, where 0.3.1 measured 11x and 4.7x. - What did not. The kernel is the same, NumPy's time at a million elements is the same, and the figures at 4,096 elements are unchanged. At 16 elements
np.cliponfloat32is 10x faster where it was 11x, in a run on a newer Python. - Where it starts. The difference starts with the in-place add and subtract loops of this release. It looks like how memory for the result is reused, not the kernel, and is being looked into in #345.
What is installed.
- The count. 49 kernels for 23 NumPy operations and four functions of bottleneck.
install()installs the 47 that return NumPy's or bottleneck's result bit for bit, andinstall(inexact=True)addsnp.cumsumonfloat32andfloat64. - ARM64. 37 of the 49 are hand-written assembly, and the rest are compiled C.
- x86-64. 42 are assembly.
Checked against NumPy itself. On Apple Silicon, 403,023 calls to NumPy and bottleneck give the same result, exceptions and warnings with hotkernels as without. release.yml runs NumPy's own test suite against every wheel.
Where it runs. Unchanged: macOS on Apple Silicon and Linux on x86-64 (glibc, x86-64-v3), Python 3.11 to 3.14, NumPy 2.3 or a later NumPy 2.
Not in the package. The repository also has a prototype lazy layer, which fuses whole NumPy expressions into one pass. It is not part of this release; its README describes it.
Measured from Python with uv run delivery time, one line per machine:
- Apple M1 Pro. NumPy 2.5.5, Python 3.13.12, load average 1.13, at revision
3da180b, whose package and kernels are those of this release:delivery/results/apple-m1-pro.md. Thew -= bfigure is from the gradient-step study of #210, and the bottleneck andargmax(axis=1)figures are from their pull requests on the same machine. - Intel Core i5-14600KF. Not re-measured for this release. The x86-64 figures above are from the pull requests that added each kernel.
delivery/results/intel-r-core-tm-i5-14600kf.mdis still the run recorded for 0.3.0.
New kernels
np.clip(x, lo, hi, out=x)andx.clip(lo, hi, out=x)onfloat32andfloat64, the usual way to clip gradients in place, are served by the clip kernels instead of NumPy: about 4x faster than NumPy on a 100 × 100float64array and 2.7x at a million elements, measured on an Apple M1 Pro. (#197)np.argmax(m, axis=1)andnp.argmin(m, axis=1)(andm.argmax(axis=1)) on a C-ordered two-dimensionalfloat32orfloat64array are served: about 3.5x faster than NumPy on 300,000 rows of tenfloat64and about 15x on rows of two or three, on an Apple M1 Pro.report()now counts such a call once, however many rows NumPy splits it into. (#201)np.unique(x)onint64labels, a few distinct values close together such as the class labels scikit-learn checks on every call, is served by a kernel: about 18x faster than NumPy at a million elements on an Apple M1 Pro (ARM64). Calls with counts, an inverse or indices, withsorted=False, or on widely spread values go to NumPy as before. (#206)- When bottleneck is installed, its
nanmean,nanstd,nanminandnanmaxon two-dimensionalfloat64arrays along an axis whose other axis is contiguous (axis=0of a C-ordered array or of a rolling-window view), which pandas and empyrical call in place of NumPy's, are served by kernels with bottleneck's results bit for bit: about 4x to 7x faster than bottleneck at 252 × 4,789 on an Apple M1 Pro. hotkernels never imports bottleneck, andinstall(bottleneck=False)leaves it alone. (#211) np.argsort(x, kind="stable")on one-dimensionalfloat64,int64anduint64arrays is served by a kernel, as arekind="mergesort"andstable=True: about 13x faster than NumPy at a millionfloat64elements and 15x to 28x on integers, measured on an Apple M1 Pro. The result is NumPy's, ties, NaNs and zeros of both signs included. The default kind,np.argsort(x), is unchanged. (#215)np.searchsortedon a sortedint64array is served by a kernel, on either side, forint64needles: an array of them, onenp.int64or a Python int. On x86-64 (Intel Core i5-14600KF) a call with one needle is about 2.1x faster than NumPy at any length, and one with 4,096 needles against 4,096 elements about 3.3x. Needles of another type, such asint32or a float, go to NumPy as before. (#245)m.any(axis=0)andnp.any(m, axis=0)on a two-dimensional boolean array in C order are served by a kernel. On x86-64 (Intel Core i5-14600KF) a mask of 30 rows by 31 columns is about 9x faster than NumPy, and a matrix of 31 columns about 18x at 4,096 elements and 37x at a million. Calls withkeepdims,outorwherego to NumPy as before. (#248)np.sum(x)andx.sum()on a one-dimensionalfloat32array are served by a kernel on Apple Silicon, with NumPy's result bit for bit: from Python on an Apple M1 Pro, about 20x faster than NumPy at 16 elements, 10x at 4,096 and 3x at a million. It is installed by default; other arguments, other shapes, and sums that overflow or meet a NaN or an infinity stay with NumPy. (#250)np.sum(x)andx.sum()on a one-dimensionalfloat64array are served by a kernel on Apple Silicon, with NumPy's result bit for bit: from Python on an Apple M1 Pro, about 20x faster than NumPy at 16 elements, 6x at 4,096 and 1.9x at a million. It is installed by default; other arguments, other shapes, and sums that overflow or meet a NaN or an infinity stay with NumPy. (#251)np.nonzero(m),m.nonzero(),np.where(m)andnp.argwhere(m)on a two-dimensional boolean array in C order are served by a kernel on Apple Silicon, with NumPy's result down to its layout: about 5x faster than NumPy for 40 rows of 22 with 5% True, and 27x at a million elements (Apple M1 Pro). (#252)np.sum(x)andx.sum()on a one-dimensionalfloat32array are served by a kernel that gives NumPy's result bit for bit: on x86-64,x.sum()is about 8x faster than NumPy at 4,096 elements and 2.6x at a million. (#253)np.nonzero(m)on a two-dimensional boolean array in C order (andm.nonzero(),np.where(m)andnp.argwhere(m)) is served by a kernel, with NumPy's result, views and all: on x86-64 about 5x faster than NumPy on 40 rows of 22 with 5% True, and 24x at a million elements. (#254)np.sum(x)andx.sum()on a one-dimensionalfloat64array are now served on x86-64 too, by a kernel that gives NumPy's result bit for bit:np.sum(x)is about 9x faster than NumPy at 4,096 elements and 1.6x at a million on x86-64. (#255)x[mask] = vonfloat32,int32andint64(andnp.putmask,np.copyto(x, v, where=mask)) is served by a kernel whenvis a plain number of the array's kind, andx[mask],np.compressandnp.extractonint32too: on x86-64,x[mask] = 0.0on a millionfloat32with half the mask True is about 31x faster than NumPy, and 23x at 4,096 elements. (#256)np.isinonint32is served by a kernel when the test values are anint32array or a list of Python ints, up to eight of them: about 29x faster than NumPy at 4,096 elements and 20x at a million, on x86-64. (#257)x[mask],np.compressandnp.extractonuint8,int8,bool,int16anduint16are served by kernels: on x86-64, with half the mask True, about 16x faster than NumPy at 4,096 elements and 56x at a million onuint8andbool, 14x and 36x onint16. (#258)np.searchsortedon afloat64array withfloat32needles (andnp.digitizewithfloat32values) is served by thefloat64search, either side: about 15x faster than NumPy with 4,096 needles on x86-64. A signalling NaN among the needles still goes to NumPy, which warns about it. (#259)np.roundandnp.aroundon a singlefloat64number, annp.float64or a zero-dimensional array, are answered by hotkernels with NumPy's result, annp.float64: 16x to 18x faster than NumPy on x86-64 (np.around(x, 1)on annp.float64, 98 ns against 1.75 µs). (#260)
Faster
a.cumsum(axis=0),a.cumprod(axis=0),np.maximum.accumulate(a, axis=0)andnp.fmax.accumulate(a, axis=0)on a C-orderedfloat64array of two or more dimensions, in place too, now give NumPy's exact result about 7x to 10x faster at 253 × 4,789 on an Apple M1 Pro, by walking the rows where NumPy walks the columns. (#199)- On Apple Silicon,
w -= bandw += bonfloat64arrays of 131,072 elements or more are much faster right after a large matrix product has readw, as in the gradient stepw -= lr * gof a training loop: in such a loop on a 784 × 256 matrix the step went from about 460 µs to about 95 µs, and the program from 260 ms to 153 ms. Elsewhere they run at about NumPy's speed, and the result is NumPy's bit for bit. (#210) np.medianonfloat64arrays of more than 128 elements, andnp.median(x, axis=None)at every size, are now served by hotkernels: about 8x faster than NumPy at a million elements of random data on an Apple M1 Pro. On data that is already in order the gain is smaller, and on sorted data with a little noise added it is slower, 0.74x at a million elements. (#230)np.unique(x)onint64labels is faster on Linux x86-64: about 1.3x at a million elements and 1.8x at 4,096, on an Intel Core i5-14600KF, which is about 39x faster than NumPy at a million. The gain is for labels that keep to 64 neighbouring values or come sorted; labels spread evenly over a wider range run about as before. (#242)- On Apple Silicon,
np.searchsortedon a sortedint64array with an array ofint64needles (andnp.digitize, which calls it) is served by hand-written assembly: with as many needles as elements, about 2.5x faster than NumPy at 4,096 elements and 2.1x at 1,048,576 on an Apple M1 Pro. (#246) - On Apple Silicon,
m.any(axis=0)on a two-dimensional boolean array is served by hand-written assembly: in the judge, on an Apple M1 Pro, the kernel is about 18x faster than the compiled C at a million elements in rows of 31 and 2.2x in rows of 256. (#249) - Elementwise operations on small arrays, such as
a + b,a * 2.0,a > 0,np.exp(a)andx += y, on bool, integer,float32andfloat64arrays of up to 16,384 elements, now skip most of NumPy's call path and run NumPy's own loop, with the same results and warnings. At 16float64elementsa + bis about 2x faster anda > 0about 3x, measured on an Apple M1 Pro. (#301)
Fixes
with hotkernels.enabled(inexact=True):inside aninstall()left the inexactnp.cumsumkernels on after the block. On leaving the block hotkernels is now as the block found it, with the arguments it was installed with. (#304)