Skip to content

Instantly share code, notes, and snippets.

@ngoldbaum
Created November 3, 2026 15:53
Show Gist options
  • Select an option

  • Save ngoldbaum/37a0d444df0f2b9b48822ea0b476476e to your computer and use it in GitHub Desktop.

Select an option

Save ngoldbaum/37a0d444df0f2b9b48822ea0b476476e to your computer and use it in GitHub Desktop.
diff --git a/numpy/_core/src/umath/ufunc_object.c b/numpy/_core/src/umath/ufunc_object.c
--- a/numpy/_core/src/umath/ufunc_object.c
+++ b/numpy/_core/src/umath/ufunc_object.c
@@ -2695,9 +2695,17 @@
npy_intp const *countptr, NpyIter_IterNextFunc *iternext,
int needs_api, npy_intp skip_first_count)
{
- /* A literal nout lets the compiler specialize the single-output case */
+ /*
+ * Specialize reduce, accumulate, and reduceat only for nout == 1 or 2:
+ * NumPy's built-in reducible ufuncs have at most two outputs.
+ */
if (context->method->nout == 1) {
return reduce_loop_impl(context, 1, strided_loop, auxdata, iter,
+ dataptrs, strides, countptr, iternext, needs_api,
+ skip_first_count);
+ }
+ if (context->method->nout == 2) {
+ return reduce_loop_impl(context, 2, strided_loop, auxdata, iter,
dataptrs, strides, countptr, iternext, needs_api,
skip_first_count);
}
@@ -3309,8 +3317,11 @@
}
else {
do {
- res = accumulate_along_axis(&context, nout, strided_loop, auxdata,
- copy_info, dataptr, fixed_strides, elsizes, count);
+ res = nout == 2
+ ? accumulate_along_axis(&context, 2, strided_loop, auxdata,
+ copy_info, dataptr, fixed_strides, elsizes, count)
+ : accumulate_along_axis(&context, nout, strided_loop, auxdata,
+ copy_info, dataptr, fixed_strides, elsizes, count);
} while (res == 0 && iternext(iter));
}
@@ -3439,6 +3450,11 @@
/* A literal nout lets the compiler specialize the single-output case */
if (context->method->nout == 1) {
return reduceat_segments_impl(context, 1, strided_loop, auxdata,
+ copy_info, dataptr, out_strides, fixed_strides,
+ reduceat_ind, ind_size, red_axis_size);
+ }
+ if (context->method->nout == 2) {
+ return reduceat_segments_impl(context, 2, strided_loop, auxdata,
copy_info, dataptr, out_strides, fixed_strides,
reduceat_ind, ind_size, red_axis_size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment