Skip to content

hotkernels 0.4.0

Latest

Choose a tag to compare

@github-actions github-actions released this 30 Nov 11:09
· 50 commits to main since this release
48c4d73

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") on float64, int64 and uint64 is 13x faster at a million elements, and 28x on keys with few distinct values.
    • np.median on float64 is 7.8x faster at a million elements and 53x at 16.
    • np.unique(x) on int64 labels 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) and np.fmax.accumulate(a, axis=0) on a C-ordered float64 matrix 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) and np.where(m) on a boolean matrix are 27x faster at a million elements with few True, and np.argwhere(m) 20x.
    • np.argmax(m, axis=1) and np.argmin(m, axis=1) are about 3.5x faster on rows of ten float64.
  • Sums. np.sum(x) and x.sum() on float32 and float64 keep 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 on float64.
    • w -= b and w += b on large float64 arrays right after a matrix product has read w are about 5x faster on Apple Silicon. Elsewhere they run at NumPy's speed.
  • Searches.

    • np.searchsorted on int64 is 2.2x faster with as many needles as elements, and about 2x with one needle at any length.
    • np.searchsorted on float64 with float32 needles is 8x faster.
  • bottleneck. When bottleneck is installed, its nanmean, nanstd, nanmin and nanmax along axis=0 of a float64 matrix, 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) and x += y, skip most of NumPy's call path. At 16 elements a + b is about 2x faster, a > 0 about 3x and np.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 on uint8.
    • x[mask] = v on float32 and int32: 31x on float32.
    • np.isin on int32: 20x at a million elements.
    • A single np.round on a float64 scalar: 16x to 18x.
    • np.unique on int64 labels: about 39x at a million elements.

    On Apple Silicon, the kernels for x[mask] on one- and two-byte types, for x[mask] = v on float32 and int32, and for np.isin on int32 are the same kernels compiled from C. np.unique has ARM64 assembly of its own. The scalar np.round is 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 on float32 and 3.2x on float64, 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.clip on float32 is 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, and install(inexact=True) adds np.cumsum on float32 and float64.
  • 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. The w -= b figure is from the gradient-step study of #210, and the bottleneck and argmax(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.md is still the run recorded for 0.3.0.

New kernels

  • np.clip(x, lo, hi, out=x) and x.clip(lo, hi, out=x) on float32 and float64, 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 × 100 float64 array and 2.7x at a million elements, measured on an Apple M1 Pro. (#197)
  • np.argmax(m, axis=1) and np.argmin(m, axis=1) (and m.argmax(axis=1)) on a C-ordered two-dimensional float32 or float64 array are served: about 3.5x faster than NumPy on 300,000 rows of ten float64 and 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) on int64 labels, 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, with sorted=False, or on widely spread values go to NumPy as before. (#206)
  • When bottleneck is installed, its nanmean, nanstd, nanmin and nanmax on two-dimensional float64 arrays along an axis whose other axis is contiguous (axis=0 of 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, and install(bottleneck=False) leaves it alone. (#211)
  • np.argsort(x, kind="stable") on one-dimensional float64, int64 and uint64 arrays is served by a kernel, as are kind="mergesort" and stable=True: about 13x faster than NumPy at a million float64 elements 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.searchsorted on a sorted int64 array is served by a kernel, on either side, for int64 needles: an array of them, one np.int64 or 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 as int32 or a float, go to NumPy as before. (#245)
  • m.any(axis=0) and np.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 with keepdims, out or where go to NumPy as before. (#248)
  • np.sum(x) and x.sum() on a one-dimensional float32 array 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) and x.sum() on a one-dimensional float64 array 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) and np.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) and x.sum() on a one-dimensional float32 array 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 (and m.nonzero(), np.where(m) and np.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) and x.sum() on a one-dimensional float64 array 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] = v on float32, int32 and int64 (and np.putmask, np.copyto(x, v, where=mask)) is served by a kernel when v is a plain number of the array's kind, and x[mask], np.compress and np.extract on int32 too: on x86-64, x[mask] = 0.0 on a million float32 with half the mask True is about 31x faster than NumPy, and 23x at 4,096 elements. (#256)
  • np.isin on int32 is served by a kernel when the test values are an int32 array 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.compress and np.extract on uint8, int8, bool, int16 and uint16 are 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 on uint8 and bool, 14x and 36x on int16. (#258)
  • np.searchsorted on a float64 array with float32 needles (and np.digitize with float32 values) is served by the float64 search, 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.round and np.around on a single float64 number, an np.float64 or a zero-dimensional array, are answered by hotkernels with NumPy's result, an np.float64: 16x to 18x faster than NumPy on x86-64 (np.around(x, 1) on an np.float64, 98 ns against 1.75 µs). (#260)

Faster

  • a.cumsum(axis=0), a.cumprod(axis=0), np.maximum.accumulate(a, axis=0) and np.fmax.accumulate(a, axis=0) on a C-ordered float64 array 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 -= b and w += b on float64 arrays of 131,072 elements or more are much faster right after a large matrix product has read w, as in the gradient step w -= lr * g of 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.median on float64 arrays of more than 128 elements, and np.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) on int64 labels 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.searchsorted on a sorted int64 array with an array of int64 needles (and np.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) and x += y, on bool, integer, float32 and float64 arrays 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 16 float64 elements a + b is about 2x faster and a > 0 about 3x, measured on an Apple M1 Pro. (#301)

Fixes

  • with hotkernels.enabled(inexact=True): inside an install() left the inexact np.cumsum kernels on after the block. On leaving the block hotkernels is now as the block found it, with the arguments it was installed with. (#304)