Repository navigation
Conversation
Co-Authored-By: Claude Fable 5.2 <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.
Stacked on numpy#33326 (branch
ufunc-at-rows), which calls the inner loop once per run of the last dimension when the indices select subarrays and no cast is needed. This does the same when an operand needs a cast, has a non-native byte order or is not aligned.What changes
ufunc_at__slow_iterhandles those cases with a bufferedNpyIterover dummy one-element operands: for every element it resets the base pointers (cast into the buffers), calls the loop withcount=1, and callsiternext(cast back).It now takes a
runargument. Whenrun > 1:runelements with the real strides, so one reset casts a whole run;do/whileoniternext, since a run longer than the buffer is split into chunks;execute_ufunc_loopdoes. The loop passed by the caller is the contiguous one, which was only valid becausecountwas 1;run, as inufunc_at__subspace_iter.With
run == 1the path is the same as before, except that the iterator is created withNPY_CORDERinstead ofNPY_KEEPORDER(no effect on one element, avoids flipping negative strides for a run).The condition for iterating by runs moved out of the no-cast branch so both paths share it.
Benchmark
np.add.at(sums, labels, rows), 16384 rows, 256 labels, 200 columns unless stated. Best of 7, 3 interleaved rounds, one pinned core of a Core Ultra X7 358H. "Before" is the numpy#33326 state (a build of 2026-11-10 in a different Python environment).Checks
test_ufunc_at_subarrays_cast: casts in both directions, byte-swapped, object and unaligned targets, runs longer than the buffer.numpy/_coreandnumpy/lib: 42760 passed.ufunc.atcalls (random views, indices, broadcasting, with and without casts) give results identical to the ENH: ufunc.at: call the inner loop once per indexed row (30x speed-up) numpy/numpy#33326 build.Not done: release note, and the
asvbenchmarktime_sum_at_rows_castwas added but not run throughasv.🤖 Generated with Claude Code