Repository navigation
Conversation
When the output was not contiguous, an input stride equal to the itemsize in elements (4 for float32, 8 for float64) was taken for a contiguous input, so `np.negative(a[::4], out=out[::2])` read `a[:4]`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When the indices select subarrays (e.g. `np.add.at(sums, labels, rows)` with a 2-D `sums`), call the inner loop once per run of the last dimension instead of once per element. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR summary
np.add.at(sums, labels, rows)with a 2-Dsumsis ~30x slower than it needs to be. This is the scatter-add of mini-batch k-means (sum the rows of each cluster), and FAISS replaced it with a SciPy sparse product for that reason (facebookresearch/faiss#5849).When the indices select subarrays,
ufunc.atcannot use the indexed loops of numpy#23136 (they need a 1-D target), so it falls back to the loop of numpy#22889, which calls the inner loop once per element withcount = 1. A profile of the case below shows ~20% of the time inFLOAT_pairwise_sum: with zero strides andout == in1, each one-element add takes the reduction branch of the add loop.This PR calls the inner loop once per run of the last dimension instead, with the real strides. It only changes the no-cast, aligned path. numpy#23176 lists this case ("2d and more contiguous arrays could be sped up").
Median times on one P-core of a Core Ultra X7 358H:
Contiguous or strided
sumsand float32 or float64 give the same speed-up. One epoch of FAISS 1.16NumpyEmeanson GloVe-200 (1.18M x 200, k=256, 73 steps, 4 threads) goes from 1.82 s to 0.49 s, with the same objective.The first commit fixes a bug this change exposed.
np.negative(a[::4], out=out[::2])on float32 readsa[:4]: when the output is not contiguous, an input stride equal to the itemsize in elements (4 for 4-byte types, 8 for 8-byte types) is taken for a contiguous input. It is in 2.5.4 too.np.negative.aton rows hits it once the inner loop gets the real strides.Tests:
test_ufunc_at_subarrayscompares against applying the ufunc one index at a time (rows and columns, strided, reversed and transposed targets, broadcast and non-contiguous values)ufunc.atcalls (1 to 3 dims, two index arrays, views, 5 ufuncs and 5 dtypes) between main and this branch, with no differencenp.negativetest fails on main.First time contributor introduction
Not my first PR here (numpy#29837 and numpy#30070, on weighted quantiles), but a short intro anyway: I work on scikit-learn performance (see my recent post on scikit-learn's blog), and I often use AI agents to investigate what the benchmarks show. It pointed at
numpy.add.atand I remembered I had already wrote a numba-version of it for 2d arrays a couple of years ago because it was slow. I asked Claude to investigate and it came up with this, which looks like a very useful speedup.AI Disclosure
Claude Opus 5.5 did everything, I've only steered it and reviewed the code, but honestly I don't understand everything in it. I trust Claude to write better code than me (sadly), and most importantly, I trust the tests and the benchmarks (those I can understand).
"I" in the PR summary part means Claude.