Skip to content

hotkernels 0.3.0

Choose a tag to compare

@github-actions github-actions released this 19 Nov 07:24
· 754 commits to main since this release
b0ac0e8

hotkernels 0.3.0

Highlights

Eleven more NumPy calls are served, chosen from the calls a program in use makes rather than from a list of benchmarks. Eight kernels are new, and three calls on short arrays now skip NumPy's own call path. The new kernels are written for Linux x86-64; on Apple Silicon they run as compiled C until their ARM64 assembly exists, so every figure in these notes is from x86-64, measured from Python on an Intel Core i5-14600KF.

  • Boolean masks. np.cumsum(mask) is 9.7x faster than NumPy at 4,096 elements and 9.1x at a million. np.sum(mask) and mask.sum() are 12.5x faster at a million and 14x to 33x at 16. m.any(axis=1) on a matrix of 22 columns is 16x faster at 4,096 elements and 43x at a million.
  • np.where(mask, a, b) on float64 or int64 arrays of one shape, and on a float64 array beside a single number, as in np.where(mask, a, np.nan): 5.9x faster at a million elements with half the mask True, 2.5x for the form with a number and a mask that is mostly True, and 4.6x at 16.
  • Running maxima, and maxima of runs. np.maximum.accumulate on int64 is 11.5x faster at 4,096 elements and 5.2x at a million; np.fmax.accumulate on float64 6.8x and 5.1x. np.fmax.reduceat on runs of 16 elements is 12x faster at 4,096 and at a million.
  • np.bincount on int16 and int64: 4.5x and 1.9x faster at a million elements on codes from 0 to 63.
  • More arrays reach kernels that already existed. x[mask], np.compress and np.extract take int64 (10x at a million elements with half selected), and x[mask] and x[mask] = v take arrays of any number of dimensions when the mask has the array's shape (10x and 18x at a million).
  • Short arrays. np.median(x) on up to 128 float64 elements is 44x faster at 16 elements. np.clip on int64 and np.round on float64, on up to 4,096 elements, are 15x and 7.5x faster at 16. Above those sizes the calls go to NumPy as before, at NumPy's speed.
  • What goes to NumPy on purpose. For a few inputs NumPy's own answer depends on which of its builds is running: the sign of a zero maximum when the data holds zeros of both signs, and which NaN comes back when everything is NaN. np.fmax.accumulate and np.fmax.reduceat leave those inputs to NumPy, so the result is NumPy's on every machine.

No wrong result was fixed in this release.

What is installed. 31 kernels for 21 NumPy operations. install() installs the 29 that return NumPy's result bit for bit; install(inexact=True) adds np.cumsum on float32 and float64. The three short-array calls are loops inside the package, listed in report() beside the kernels.

Checked against NumPy itself. On Linux x86-64, 166,029 NumPy calls give the same result, exceptions and warnings with hotkernels as without, and NumPy's own test suite gives the same outcome for each of its 48,659 tests. On Apple Silicon the same calls were checked on every pull request of this release.

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.

Measured from Python with uv run delivery time, one line per machine:

New kernels

  • np.sum(mask) and mask.sum() on a boolean array are served by the kernel that sums uint8: about 12x faster than NumPy at a million elements and 15x to 30x on small arrays, measured on x86-64. (#145)
  • x[mask], np.compress(mask, x) and np.extract(mask, x) on int64 are served by the kernel that selects float64: about 10x faster than NumPy at a million elements with half the mask True, measured on x86-64. (#146)
  • x[mask] and x[mask] = v are served by their kernels for arrays of more than one dimension, when the mask has the shape of x: x[mask] = 0.0 on a two-dimensional float64 array is about 10x faster than NumPy at 4,096 elements, measured on x86-64. (#147)
  • np.cumsum(mask) and mask.cumsum() on a boolean array are served by a kernel: about 9.7x faster than NumPy at 4,096 elements and 9x at a million, measured on x86-64. (#148)
  • np.where(mask, a, b) on float64 and int64 arrays of one shape, and on a float64 array beside a single number such as np.nan or 0, is served by a kernel: measured on x86-64, about 6x faster than NumPy at a million elements with half the mask True, and 2.5x for np.where(mask, a, np.nan) with most of it True. On ARM64 the same calls run a plain C loop for now. (#149)
  • np.maximum.accumulate on int64, and np.fmax.accumulate on int64 and float64, are served by kernels for one-dimensional arrays: measured on x86-64, about 5.3x and 5x faster than NumPy at a million elements and 2.6x and 2.8x at 16. np.maximum.accumulate on floats, which carries NaN forward, stays with NumPy, and so does a float64 array holding a signalling NaN, a negative zero, or NaNs of different bit patterns. On ARM64 the same calls run a plain C loop for now. (#150)
  • np.fmax.reduceat(x, starts) on a one-dimensional float64 array with int64 starts is served by a kernel: on runs of 16 elements with 5% NaN it is about 12x faster than NumPy at a million elements, measured on x86-64. The gain is on short runs; on runs of a thousand elements or more in large arrays it can be slower than NumPy. np.fmax.accumulate on float64 is faster too (about 5x at a million elements, was 3.3x), and neither call now leaves the floating-point invalid flag set. (#151)
  • m.any(axis=1) and np.any(m, axis=1) on a two-dimensional boolean array in C order are served by a kernel: on rows of 22 columns about 42x faster than NumPy at a million elements and 5x on a single row, measured on x86-64. m.any(), other axes and calls with out, keepdims or where stay with NumPy. (#152)
  • np.bincount on int16 and int64 arrays is served by kernels: on codes from 0 to 63, about 4.5x faster than NumPy for int16 and 1.9x for int64 at a million elements, measured on x86-64. An array holding a negative element still goes to NumPy, which raises as before. (#153)

Faster

  • np.clip(x, lo, hi) and x.clip(lo, hi) on an int64 array of up to 4,096 elements, with bounds that are Python ints or np.int64, skip NumPy's call path: about 15x faster than NumPy at 16 elements and 3.6x at 4,096, measured on x86-64. Longer arrays go to NumPy as before. (#154)
  • np.round(x, decimals), np.around and x.round(decimals) on a float64 array of up to 4,096 elements, with decimals a Python int from 0 to 15, skip NumPy's call path and give NumPy's result bit for bit: np.round(x, 2) is about 7.5x faster than NumPy at 16 elements and 2.2x at 4,096, measured on x86-64. Longer arrays, and calls where NumPy would warn, go to NumPy as before. (#155)
  • np.median(x) on a float64 array of up to 128 elements, with no other argument, skips NumPy's Python implementation and gives its result bit for bit: about 44x faster than NumPy at 16 elements, measured on x86-64. Longer arrays, arrays holding a NaN, and calls with axis or any other argument go to NumPy as before. (#156)