Skip to content

hotkernels 0.2.0

Choose a tag to compare

@github-actions github-actions released this 18 Nov 19:46
· 881 commits to main since this release
5829ca2

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.sum on uint8 is 41.6x faster than NumPy, x[mask] = 0.0 17.7x, x[mask] on float32 15.7x, np.cumsum on float32 13.0x (with install(inexact=True)), np.histogram 8.15x and np.argmax 1.84x. NumPy has vector loops of its own for np.clip and x.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 for np.clip at 16 elements). No call a kernel takes is slower than NumPy at any size timed.
  • np.searchsorted, np.interp and np.digitize on float64. For a million values looked up in a million sorted elements, np.searchsorted is 8.57x faster than NumPy on an Apple M1 Pro and np.interp 22.4x; against 1,000 sorted elements np.searchsorted is 26.6x and np.digitize 26.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) and np.where(mask) reach the kernels that x[mask], x[mask] = v and np.nonzero already used. At a million elements with half the mask selected, on the M1 Pro: np.compress 6.11x, np.putmask 18.0x, np.copyto 20.3x, np.where(mask) 3.46x. On the Intel Core i5-14600KF: 2.00x, 15.6x, 18.5x and 1.57x.
  • Small arrays. np.clip with 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, and x.argmax() 3.11x.
  • pip install hotkernels works everywhere. A source distribution is published. Where the kernels do not run it builds without a compiler and install() does nothing; print(hotkernels.report()) says why. python -m hotkernels script.py runs 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) and np.count_nonzero (a Python int, not np.intp) differently from later ones, and with hotkernels 0.1.0 installed those calls give the later answer. From this release install() 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:

The same statements on an Apple M4 Max are in delivery/results/apple-m4-max.md.

New kernels

  • np.searchsorted(a, v) and np.interp(x, xp, fp) on float64 are 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.searchsorted is taken without a sorter, and np.interp without left, right or period. (#42)
  • np.searchsorted(a, v, side='right') on float64 is served by a kernel, and with it np.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.digitize is taken when its bins increase. (#49)
  • np.compress(mask, x) and np.extract(mask, x) on float32 and float64, np.putmask(x, mask, v) and np.copyto(x, v, where=mask) on float64 with a Python number as the value, and np.where(mask) with only a boolean mask are served by the same kernels as x[mask], x[mask] = v and np.nonzero. At a million elements with half the mask selected, on an Apple M1 Pro, np.compress is about 6x faster than NumPy, np.putmask and np.copyto about 18x to 20x, and np.where(mask) about 3.4x. (#61)

Faster

  • np.clip and x.clip on float32 and float64 are much faster on small arrays when the bounds are Python numbers (or NumPy scalars of the array's own type) and there is no out: about 10x faster than NumPy at 16 elements on an Apple M1 Pro, where they used to gain nothing. x.argmax() and x.argmin() on arrays shorter than 128 elements are served by the kernels too: about 3x faster at 16 elements. (#64)

Compatibility

  • pip install hotkernels now works on any platform: a source distribution is published, which builds without a compiler where the kernels do not run and leaves install() doing nothing, and builds the kernels on an Apple Silicon Mac whose Python has no wheel. python -m hotkernels script.py runs 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) and np.count_nonzero (a Python int instead of np.intp) differently from later releases, and with hotkernels installed those calls gave the later answer; on those releases install() now does nothing and report() 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.sum on uint8 is about 42x faster than NumPy, x[mask] on float32 about 16x, np.cumsum on float32 about 13x and np.argmax about 1.8x; np.clip and x.astype(np.float32) are level with NumPy there, which already has vector loops for them. (#82)