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.
10000 Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
MAINT: simplify further
  • Loading branch information
ngoldbaum committed Jan 8, 2025
commit d301c0c71dec922223b92aa3556e6a32aef68a96
19 changes: 5 additions & 14 deletions numpy/_core/src/umath/legacy_array_method.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,18 +428,12 @@ PyArray_NewLegacyWrappingArrayMethod(PyUFuncObject *ufunc,

// 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)) {
PyArray_Descr **descrs = PyMem_Calloc(ufunc->nin + ufunc->nout,
sizeof(PyArray_Descr *));
if (PyTypeNum_ISNUMBER(bound_res->dtypes[0]->type_num) &&
ufunc->nin == 2 && ufunc->nout == 1) {

if (descrs == NULL) {
PyErr_NoMemory();
PyMem_Free(initial);
return NULL;
}
PyArray_Descr *descrs[3];


for (int i = 0; i < ufunc->nin + ufunc->nout; i++) {
for (int i = 0; i < 3; i++) {
// only dealing with numeric legacy dtypes so this should always be
// valid
descrs[i] = bound_res->dtypes[i]->singleton;
Expand All @@ -454,17 +448,14 @@ PyArray_NewLegacyWrappingArrayMethod(PyUFuncObject *ufunc,
int ret = get_initial_from_ufunc(&context, 0, context.method->legacy_initial);

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

// only use the cached initial value if it's valid
if (ret > 0) {
context.method->get_reduction_initial = &copy_cached_initial;
}

PyMem_Free(descrs);
}


Expand Down
Loading
0