Repository navigation
hotkernels 0.2.0
hotkernels 0.2.0
Highlights
hotkernels now runs on Linux x86-64 as well as on Apple Silicon, installs with pip on any platform, and serves np.searchsorted, np.interp and np.digitize. It now requires NumPy 2.3 or later, for a reason that matters to anyone on 0.1.0 with an older NumPy; see the last point.
- Linux x86-64. All 23 kernels were written a second time, for AVX2, and are checked against NumPy as the Apple Silicon ones are.
install()works on any processor with the x86-64-v3 instruction sets (Intel since 2013, AMD since 2015), and wheels are published for glibc Linux on CPython 3.11 to 3.14. From Python at a million elements on an Intel Core i5-14600KF:np.sumonuint8is 41.6x faster than NumPy,x[mask] = 0.017.7x,x[mask]onfloat3215.7x,np.cumsumonfloat3213.0x (withinstall(inexact=True)),np.histogram8.15x andnp.argmax1.84x. NumPy has vector loops of its own fornp.clipandx.astype(np.float32)on this platform, so those are level with it at a million elements (1.02x to 1.04x) and ahead only on small arrays (7.67x fornp.clipat 16 elements). No call a kernel takes is slower than NumPy at any size timed. np.searchsorted,np.interpandnp.digitizeonfloat64. For a million values looked up in a million sorted elements,np.searchsortedis 8.57x faster than NumPy on an Apple M1 Pro andnp.interp22.4x; against 1,000 sorted elementsnp.searchsortedis 26.6x andnp.digitize26.7x. On the Intel Core i5-14600KF the same four are 4.20x, 5.30x, 4.92x and 4.78x.- The function forms of the mask calls.
np.compress,np.extract,np.putmask,np.copyto(x, v, where=mask)andnp.where(mask)reach the kernels thatx[mask],x[mask] = vandnp.nonzeroalready used. At a million elements with half the mask selected, on the M1 Pro:np.compress6.11x,np.putmask18.0x,np.copyto20.3x,np.where(mask)3.46x. On the Intel Core i5-14600KF: 2.00x, 15.6x, 18.5x and 1.57x. - Small arrays.
np.clipwith Python numbers as bounds is 9.76x to 11.1x faster than NumPy at 16 elements on the M1 Pro, where 0.1.0 gained nothing, andx.argmax()3.11x. pip install hotkernelsworks everywhere. A source distribution is published. Where the kernels do not run it builds without a compiler andinstall()does nothing;print(hotkernels.report())says why.python -m hotkernels script.pyruns an unmodified script with hotkernels installed.- NumPy 2.3 or later is required, and 0.1.0 on NumPy 2.0 to 2.2 changes two results. Those NumPy releases answer
np.clip(the sign of a zero) andnp.count_nonzero(a Pythonint, notnp.intp) differently from later ones, and with hotkernels 0.1.0 installed those calls give the later answer. From this releaseinstall()does nothing on NumPy 2.0 to 2.2. If you stay on 0.1.0, use it with NumPy 2.3 or later.
What is installed. 23 kernels for 15 NumPy operations. install() installs the 21 that return NumPy's result bit for bit; install(inexact=True) adds np.cumsum on float32 and float64.
Checked against NumPy itself. On each platform, 96,734 NumPy calls give the same result, exceptions and warnings with hotkernels as without. On Linux x86-64, NumPy's own test suite gives the same outcome for each of its 48,659 tests.
Where it runs. 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. On an older x86-64 processor, on any other platform and on free-threaded Python, install() does nothing and report() says why.
Measured from Python with uv run delivery time, one line per machine:
- Apple M1 Pro, NumPy 2.5.5, Python 3.12.7, on a machine in use (load average about 2), at revision
1aae15a:delivery/results/apple-m1-pro.md. - Intel Core i5-14600KF, NumPy 2.5.5, Python 3.13.15, pinned to one performance core (load average about 1), at revision
5ad3a94:delivery/results/intel-r-core-tm-i5-14600kf.md.
The same statements on an Apple M4 Max are in delivery/results/apple-m4-max.md.
New kernels
np.searchsorted(a, v)andnp.interp(x, xp, fp)onfloat64are served by kernels: about 8.6x and 22x faster than NumPy for a million values looked up in a million sorted elements, on an Apple M1 Pro.np.searchsortedis taken without asorter, andnp.interpwithoutleft,rightorperiod. (#42)np.searchsorted(a, v, side='right')onfloat64is served by a kernel, and with itnp.digitize(x, bins)with its default arguments: about 27x faster than NumPy for a million values placed among 1,000 sorted elements or bin edges, on an Apple M1 Pro.np.digitizeis taken when its bins increase. (#49)np.compress(mask, x)andnp.extract(mask, x)onfloat32andfloat64,np.putmask(x, mask, v)andnp.copyto(x, v, where=mask)onfloat64with a Python number as the value, andnp.where(mask)with only a boolean mask are served by the same kernels asx[mask],x[mask] = vandnp.nonzero. At a million elements with half the mask selected, on an Apple M1 Pro,np.compressis about 6x faster than NumPy,np.putmaskandnp.copytoabout 18x to 20x, andnp.where(mask)about 3.4x. (#61)
Faster
np.clipandx.cliponfloat32andfloat64are much faster on small arrays when the bounds are Python numbers (or NumPy scalars of the array's own type) and there is noout: about 10x faster than NumPy at 16 elements on an Apple M1 Pro, where they used to gain nothing.x.argmax()andx.argmin()on arrays shorter than 128 elements are served by the kernels too: about 3x faster at 16 elements. (#64)
Compatibility
pip install hotkernelsnow works on any platform: a source distribution is published, which builds without a compiler where the kernels do not run and leavesinstall()doing nothing, and builds the kernels on an Apple Silicon Mac whose Python has no wheel.python -m hotkernels script.pyruns an unmodified script with hotkernels installed. (#31)- hotkernels now requires NumPy 2.3 or later. NumPy 2.0 to 2.2 answer
np.clip(the sign of a zero) andnp.count_nonzero(a Pythonintinstead ofnp.intp) differently from later releases, and with hotkernels installed those calls gave the later answer; on those releasesinstall()now does nothing andreport()says why. (#60) - hotkernels now runs on Linux x86-64, with a second set of kernels written for AVX2:
install()works on any processor with the x86-64-v3 instruction sets (Intel since 2013, AMD since 2015), and wheels are published for glibc Linux. On an Intel Core i5-14600KF at a million elements,np.sumonuint8is about 42x faster than NumPy,x[mask]onfloat32about 16x,np.cumsumonfloat32about 13x andnp.argmaxabout 1.8x;np.clipandx.astype(np.float32)are level with NumPy there, which already has vector loops for them. (#82)