Skip to content

Commit c32bc51

Browse files
committed
WIP: do NEP-50 compatibility with a promoter
Signed-off-by: Iason Krommydas <[email protected]>
1 parent 9a45e86 commit c32bc51

2 files changed

Lines changed: 80 additions & 17 deletions

File tree

‎numpy/_core/src/umath/unwrap.cpp‎

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
*
3333
* The operands are, in order, the values `p` (core `(n)`), the scalars
3434
* `discont` and `period`, and the output (core `(n)`). The Python wrapper in
35-
* numpy/lib/_function_base_impl.py fills the `discont`/`period` defaults,
36-
* resolves the common dtype and casts the scalars to it.
35+
* numpy/lib/_function_base_impl.py only fills the `discont`/`period`
36+
* defaults; dtype resolution happens in unwrap_promoter below.
3737
*
3838
* gufunc strided-loop layout for signature (n),(),()->(n):
3939
* dimensions[0] outer (broadcast) loop count
@@ -223,6 +223,76 @@ unwrap_half_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
223223
}
224224

225225

226+
/*
227+
* T = common_dtype(p, period), i.e. np.result_type(p, period). discont gets
228+
* T's own precision when that's what it naturally promotes to (the NEP 50
229+
* weak-scalar case, for the float dtypes with a native loop), otherwise it
230+
* widens to double (or longdouble when T is longdouble).
231+
*/
232+
static int
233+
unwrap_promoter(PyObject *NPY_UNUSED(ufunc),
234+
PyArray_DTypeMeta *const op_dtypes[],
235+
PyArray_DTypeMeta *const NPY_UNUSED(signature[]),
236+
PyArray_DTypeMeta *new_op_dtypes[])
237+
{
238+
PyArray_DTypeMeta *T = PyArray_CommonDType(op_dtypes[0], op_dtypes[2]);
239+
if (T == NULL) {
240+
/* no common type for p and period; fail via the loop lookup below */
241+
PyErr_Clear();
242+
T = op_dtypes[0];
243+
Py_INCREF(T);
244+
}
245+
246+
PyArray_DTypeMeta *natural_D = PyArray_CommonDType(T, op_dtypes[1]);
247+
if (natural_D == NULL) {
248+
PyErr_Clear();
249+
natural_D = T;
250+
Py_INCREF(natural_D);
251+
}
252+
253+
PyArray_DTypeMeta *D;
254+
if (T == &PyArray_LongDoubleDType) {
255+
D = &PyArray_LongDoubleDType;
256+
}
257+
else if (natural_D == T &&
258+
(T == &PyArray_HalfDType || T == &PyArray_FloatDType ||
259+
T == &PyArray_DoubleDType)) {
260+
D = T;
261+
}
262+
else {
263+
D = &PyArray_DoubleDType;
264+
}
265+
Py_DECREF(natural_D);
266+
267+
Py_INCREF(T);
268+
new_op_dtypes[0] = T;
269+
Py_INCREF(D);
270+
new_op_dtypes[1] = D;
271+
Py_INCREF(T);
272+
new_op_dtypes[2] = T;
273+
new_op_dtypes[3] = T;
274+
return 0;
275+
}
276+
277+
static int
278+
add_unwrap_promoter(PyObject *ufunc)
279+
{
280+
PyObject *DType_tuple = PyTuple_Pack(4, Py_None, Py_None, Py_None, Py_None);
281+
if (DType_tuple == NULL) {
282+
return -1;
283+
}
284+
PyObject *capsule = PyCapsule_New((void *)unwrap_promoter,
285+
"numpy._ufunc_promoter", NULL);
286+
if (capsule == NULL) {
287+
Py_DECREF(DType_tuple);
288+
return -1;
289+
}
290+
int res = PyUFunc_AddPromoter(ufunc, DType_tuple, capsule);
291+
Py_DECREF(capsule);
292+
Py_DECREF(DType_tuple);
293+
return res;
294+
}
295+
226296
static int
227297
add_unwrap_loop(PyObject *ufunc, int typenum,
228298
PyArrayMethod_StridedLoop *loop, int discont_typenum)
@@ -284,10 +354,9 @@ init_unwrap_ufunc(PyObject *umath)
284354

285355
/*
286356
* float dtypes get two loops: discont at native precision (the NEP 50
287-
* weak-scalar case, where the python wrapper matches discont to p's
288-
* dtype) and at wider precision (explicitly-typed/cross-kind discont).
289-
* Integer dtypes only get the wider loop -- NEP 50 native-int discont
290-
* is not implemented, so the wrapper always widens for those.
357+
* weak-scalar case) and at wider precision (explicitly-typed/cross-kind
358+
* discont). Integer dtypes only get the wider loop -- unwrap_promoter
359+
* always widens discont for those.
291360
*/
292361
Loop loops[] = {
293362
{NPY_HALF, unwrap_half_loop<npy_half>, NPY_HALF},
@@ -315,6 +384,10 @@ init_unwrap_ufunc(PyObject *umath)
315384
}
316385
}
317386

387+
if (res == 0 && add_unwrap_promoter(ufunc) < 0) {
388+
res = -1;
389+
}
390+
318391
Py_DECREF(ufunc);
319392
return res;
320393
}

‎numpy/lib/_function_base_impl.py‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,18 +1872,8 @@ def unwrap(p, discont=None, axis=-1, *, period=2 * pi):
18721872
p = asanyarray(p)
18731873
if discont is None:
18741874
discont = period / 2
1875-
dtype = np.result_type(p, period)
1876-
# match NEP 50: a discont that would collapse to dtype anyway (the
1877-
# weak-scalar or same-dtype case) is compared at that native precision;
1878-
# anything wider (explicit stronger scalar, or cross-kind on an integer
1879-
# dtype) is compared at >=double
1880-
discont_type = np.longdouble if dtype.type is np.longdouble else np.float64
1881-
if _nx.issubdtype(dtype, _nx.floating) and np.result_type(dtype, discont) == dtype:
1882-
discont_type = dtype.type
18831875
try:
1884-
return _unwrap(p, discont, period,
1885-
signature=(type(dtype), discont_type, type(dtype), type(dtype)),
1886-
axis=axis)
1876+
return _unwrap(p, discont, period, axis=axis)
18871877
except np._core._exceptions._UFuncNoLoopError:
18881878
# object and user DTypes fall back; check p.dtype, not the promoted
18891879
# dtype, since a user DType can promote through a builtin

0 commit comments

Comments
 (0)