8000 BUG: move reduction initialization to ufunc initialization by ngoldbaum · Pull Request #28123 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: move reduction initialization to ufunc initialization #28123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 9, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
8000 Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MAINT: refactor to call get_initial_from_ufunc during init
  • Loading branch information
ngoldbaum committed Jan 8, 2025
commit 507bb98af6dd85818f19503a719e4c62c7ba1ac9
95 changes: 53 additions & 42 deletions numpy/_core/src/umath/legacy_array_method.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,24 @@ get_initial_from_ufunc(
Py_DECREF(identity_obj);
return 0;
}
if (context->descriptors[0]->type_num == NPY_OBJECT && !reduction_is_empty) {
if (PyTypeNum_ISUNSIGNED(context->descriptors[1]->type_num)
&& PyLong_CheckExact(identity_obj)) {
/*
* This is a bit of a hack until we have truly loop specific
* identities. Python -1 cannot be cast to unsigned so convert
* it to a NumPy scalar, but we use -1 for bitwise functions to
* signal all 1s.
* (A builtin identity would not overflow here, although we may
* unnecessary convert 0 and 1.)
*/
Py_SETREF(identity_obj, PyObject_CallFunctionObjArgs(
(PyObject *)&PyLongArrType_Type, identity_obj, NULL));
if (identity_obj == NULL) {
return -1;
}
}
else if (context->descriptors[0]->type_num == NPY_OBJECT
&& !reduction_is_empty) {
/* Allows `sum([object()])` to work, but use 0 when empty. */
Py_DECREF(identity_obj);
return 0;
Expand Down Expand Up @@ -409,62 +426,56 @@ PyArray_NewLegacyWrappingArrayMethod(PyUFuncObject *ufunc,
}
PyArrayMethodObject *res = bound_res->method;

// set cached initial value for numeric reductions
// set cached initial value for numeric reductions to avoid creating
// a python int in every reduction
if (PyTypeNum_ISNUMBER(bound_res->dtypes[0]->type_num)) {
npy_bool reorderable;
PyObject *identity_obj =
PyUFunc_GetDefaultIdentity(ufunc, &reorderable);
char *initial =
PyMem_Calloc(1, bound_res->dtypes[0]->singleton->elsize);

if (identity_obj == NULL) {
if (initial == NULL) {
PyErr_NoMemory();
return NULL;
}
if (identity_obj == Py_None) {
/* UFunc has no identity, ignore */
Py_INCREF(res);
Py_DECREF(bound_res);

Py_DECREF(identity_obj);
return res;
}
if (PyTypeNum_ISUNSIGNED(bound_res->dtypes[1]->type_num)
&& PyLong_CheckExact(identity_obj)) {
/*
* This is a bit of a hack until we have truly loop specific
* identities. Python -1 cannot be cast to unsigned so convert
* it to a NumPy scalar, but we use -1 for bitwise functions to
* signal all 1s.
* (A builtin identity would not overflow here, although we may
* unnecessary convert 0 and 1.)
*/
Py_SETREF(identity_obj, PyObject_CallFunctionObjArgs(
(PyObject *)&PyLongArrType_Type, identity_obj, NULL));
if (identity_obj == NULL) {
return NULL;
}
}
char *initial =
PyMem_Calloc(1, bound_res->dtypes[0]->singleton->elsize);
if (initial == NULL) {
PyArray_Descr **descrs = PyMem_Calloc(ufunc->nin + ufunc->nout,
sizeof(PyArray_Descr *));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the places where the NPY_ALLOC_WORSPACE could also be used, but I don't much, this isn't performance critical.
(And it might be that at least with mimalloc all of this barely matters anyway.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But more importantly. We only ever use the initial value for reductions. So I think you might as well hard-code all of this (if you like up to the knowledge that get_initial_form_ufunc() only usees descrs[0] anyway).

So make descrs[3] or just &singleton... (of course only enter the branch if nin == 2 and nout == 1)


if (descrs == NULL) {
PyErr_NoMemory();
Py_DECREF(identity_obj);
PyMem_Free(initial);
return NULL;
}

int pack_res = PyArray_Pack(bound_res->dtypes[0]->singleton, initial,
identity_obj);
Py_DECREF(identity_obj);
if (pack_res < 0) {

for (int i = 0; i < ufunc->nin + ufunc->nout; i++) {
// only dealing with numeric legacy dtypes so this should always be
// valid
descrs[i] = bound_res->dtypes[i]->singleton;
}

PyArrayMethod_Context context = {
(PyObject *)ufunc,
bound_res->method,
descrs,
};

int ret = get_initial_from_ufunc(&context, 0, initial);

if (ret < 0) {
PyMem_Free(initial);
PyMem_Free(descrs);
return NULL;
};
}

/* For numbers we can cache to avoid going via Python ints */
memcpy(bound_res->method->legacy_initial, initial,
bound_res->dtypes[0]->singleton->elsize);
bound_res->method->get_reduction_initial = &copy_cached_initial;
// only use the initial value if it's valid
if (ret > 0) {
memcpy(context.method->legacy_initial, initial,
context.descriptors[0]->elsize);
context.method->get_reduction_initial = &copy_cached_initial;
}

PyMem_Free(initial);
PyMem_Free(descrs);
}


Expand Down
0