Skip to content

Commit 9a45e86

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

3 files changed

Lines changed: 53 additions & 21 deletions

File tree

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

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,17 @@ floor_mod(T a, T b)
8686
}
8787

8888
/*
89-
* One loop per real dtype. T is the storage/compute type; discont is read
90-
* as npy_longdouble only for the longdouble loop, npy_double otherwise.
91-
* npy_half has no native arithmetic, so it gets its own loop below.
89+
* One loop per (T, D) pair. T is the storage/compute type; D is discont's
90+
* type, either T itself (NEP 50 weak-scalar match) or a wider float type
91+
* for explicitly-typed/cross-kind discont. npy_half has no native
92+
* arithmetic, so it gets its own loop below.
9293
*/
93-
template <typename T>
94+
template <typename T, typename D>
9495
static int
9596
unwrap_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
9697
npy_intp const dimensions[], npy_intp const strides[],
9798
NpyAuxData *NPY_UNUSED(auxdata))
9899
{
99-
using D = std::conditional_t<std::is_same_v<T, npy_longdouble>,
100-
npy_longdouble, npy_double>;
101-
102100
npy_intp n_outer = dimensions[0];
103101
npy_intp n = dimensions[1];
104102
if (n == 0) {
@@ -162,6 +160,7 @@ unwrap_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
162160
static inline float h2f(npy_half h) { return npy_half_to_float(h); }
163161
static inline npy_half f2h(float f) { return npy_float_to_half(f); }
164162

163+
template <typename D>
165164
static int
166165
unwrap_half_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
167166
npy_intp const dimensions[], npy_intp const strides[],
@@ -181,7 +180,15 @@ unwrap_half_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
181180

182181
for (npy_intp k = 0; k < n_outer; k++,
183182
p_o += s_p, disc_o += s_disc, per_o += s_per, out_o += s_out) {
184-
npy_double discont = *(const npy_double *)disc_o;
183+
/* npy_half discont is read through h2f, matching the NEP 50 weak-
184+
* scalar case where discont is compared at half precision */
185+
double discont;
186+
if constexpr (std::is_same_v<D, npy_half>) {
187+
discont = h2f(*(const npy_half *)disc_o);
188+
}
189+
else {
190+
discont = *(const D *)disc_o;
191+
}
185192
npy_half period = *(const npy_half *)per_o;
186193
npy_half interval_high = f2h(h2f(period) / 2.0f);
187194
npy_half interval_low = f2h(-h2f(interval_high));
@@ -218,7 +225,7 @@ unwrap_half_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
218225

219226
static int
220227
add_unwrap_loop(PyObject *ufunc, int typenum,
221-
PyArrayMethod_StridedLoop *loop, int discont_typenum = NPY_DOUBLE)
228+
PyArrayMethod_StridedLoop *loop, int discont_typenum)
222229
{
223230
PyArray_DTypeMeta *dt = PyArray_DTypeFromTypeNum(typenum);
224231
if (dt == NULL) {
@@ -272,28 +279,37 @@ init_unwrap_ufunc(PyObject *umath)
272279
struct Loop {
273280
int typenum;
274281
PyArrayMethod_StridedLoop *loop;
282+
int discont_typenum;
275283
};
276284

285+
/*
286+
* 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.
291+
*/
277292
Loop loops[] = {
278-
{NPY_HALF, unwrap_half_loop},
279-
{NPY_FLOAT, unwrap_loop<npy_float>},
280-
{NPY_DOUBLE, unwrap_loop<npy_double>},
281-
{NPY_LONGDOUBLE, unwrap_loop<npy_longdouble>},
282-
{NPY_BYTE, unwrap_loop<npy_byte>},
283-
{NPY_SHORT, unwrap_loop<npy_short>},
284-
{NPY_INT, unwrap_loop<npy_int>},
285-
{NPY_LONG, unwrap_loop<npy_long>},
286-
{NPY_LONGLONG, unwrap_loop<npy_longlong>},
293+
{NPY_HALF, unwrap_half_loop<npy_half>, NPY_HALF},
294+
{NPY_HALF, unwrap_half_loop<npy_double>, NPY_DOUBLE},
295+
{NPY_FLOAT, unwrap_loop<npy_float, npy_float>, NPY_FLOAT},
296+
{NPY_FLOAT, unwrap_loop<npy_float, npy_double>, NPY_DOUBLE},
297+
{NPY_DOUBLE, unwrap_loop<npy_double, npy_double>, NPY_DOUBLE},
298+
{NPY_LONGDOUBLE, unwrap_loop<npy_longdouble, npy_longdouble>,
299+
NPY_LONGDOUBLE},
300+
{NPY_BYTE, unwrap_loop<npy_byte, npy_double>, NPY_DOUBLE},
301+
{NPY_SHORT, unwrap_loop<npy_short, npy_double>, NPY_DOUBLE},
302+
{NPY_INT, unwrap_loop<npy_int, npy_double>, NPY_DOUBLE},
303+
{NPY_LONG, unwrap_loop<npy_long, npy_double>, NPY_DOUBLE},
304+
{NPY_LONGLONG, unwrap_loop<npy_longlong, npy_double>, NPY_DOUBLE},
287305
/* object dtype falls back to the python implementation instead */
288306
// {NPY_OBJECT, object_unwrap_loop},
289307
};
290308

291309
int res = 0;
292310
for (const auto& entry : loops) {
293-
int discont_typenum = entry.typenum == NPY_LONGDOUBLE
294-
? NPY_LONGDOUBLE : NPY_DOUBLE;
295311
if (add_unwrap_loop(ufunc, entry.typenum, entry.loop,
296-
discont_typenum) < 0) {
312+
entry.discont_typenum) < 0) {
297313
res = -1;
298314
break;
299315
}

‎numpy/lib/_function_base_impl.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,13 @@ def unwrap(p, discont=None, axis=-1, *, period=2 * pi):
18731873
if discont is None:
18741874
discont = period / 2
18751875
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
18761880
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
18771883
try:
18781884
return _unwrap(p, discont, period,
18791885
signature=(type(dtype), discont_type, type(dtype), type(dtype)),

‎numpy/lib/tests/test_function_base.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2468,6 +2468,16 @@ def test_longdouble_discont_precision(self):
24682468
truncated = np.float64(discont)
24692469
assert_array_equal(unwrap(p, discont=truncated, period=period), [0, -3])
24702470

2471+
def test_discont_weak_scalar_narrow_dtype(self):
2472+
# a bare python float discont is a weak scalar (NEP 50), so main
2473+
# downcasts it to p's dtype before comparing; here that downcast
2474+
# rounds 3.0001 down to exactly 3.0, so the diff of 3 is no longer
2475+
# judged smaller than discont and does get corrected
2476+
p = np.array([0, 3], dtype=np.float16)
2477+
period = 4
2478+
discont = 3 + 1e-4
2479+
assert_array_equal(unwrap(p, discont=discont, period=period), [0, -1])
2480+
24712481
def test_masked_array(self):
24722482
# the gufunc's C loop can't propagate masking through the scan the
24732483
# way the fallback's mask-aware ops do, so this must match the

0 commit comments

Comments
 (0)