Skip to content

Commit 5a3e078

Browse files
authored
Align with new array api version in dpctl (#1774)
* Use utility function from dpctl.tensor._type_utils * Align sum and prod tests * Align logsumexp and reduce_hypot tests * Updated docstring of impacted functions
1 parent 42f2644 commit 5a3e078

File tree

7 files changed

+72
-125
lines changed

7 files changed

+72
-125
lines changed

dpnp/dpnp_iface_mathematical.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040

4141
import dpctl.tensor as dpt
42+
import dpctl.tensor._type_utils as dtu
4243
import numpy
4344
from numpy.core.numeric import (
4445
normalize_axis_index,
@@ -2799,25 +2800,10 @@ def sum(
27992800
If ``None``, the sum is computed over the entire array.
28002801
Default: ``None``.
28012802
dtype : dtype, optional
2802-
Data type of the returned array. If ``None``, the default data
2803-
type is inferred from the "kind" of the input array data type.
2804-
* If `a` has a real-valued floating-point data type,
2805-
the returned array will have the default real-valued
2806-
floating-point data type for the device where input
2807-
array `a` is allocated.
2808-
* If `a` has signed integral data type, the returned array
2809-
will have the default signed integral type for the device
2810-
where input array `a` is allocated.
2811-
* If `a` has unsigned integral data type, the returned array
2812-
will have the default unsigned integral type for the device
2813-
where input array `a` is allocated.
2814-
* If `a` has a complex-valued floating-point data type,
2815-
the returned array will have the default complex-valued
2816-
floating-pointer data type for the device where input
2817-
array `a` is allocated.
2818-
* If `a` has a boolean data type, the returned array will
2819-
have the default signed integral type for the device
2820-
where input array `a` is allocated.
2803+
Data type of the returned array. If ``None``, it defaults to the dtype
2804+
of `a`, unless `a` has an integer dtype with a precision less than that
2805+
of the default platform integer. In that case, the default platform
2806+
integer is used.
28212807
If the data type (either specified or resolved) differs from the
28222808
data type of `a`, the input array elements are cast to the
28232809
specified data type before computing the sum.
@@ -2905,8 +2891,6 @@ def sum(
29052891
)
29062892
)
29072893
):
2908-
from dpctl.tensor._reduction import _default_reduction_dtype
2909-
29102894
from dpnp.backend.extensions.sycl_ext import _sycl_ext_impl
29112895

29122896
input = a
@@ -2916,7 +2900,7 @@ def sum(
29162900

29172901
queue = input.sycl_queue
29182902
out_dtype = (
2919-
_default_reduction_dtype(input.dtype, queue)
2903+
dtu._default_accumulation_dtype(input.dtype, queue)
29202904
if dtype is None
29212905
else dtype
29222906
)

dpnp/dpnp_iface_nanfunctions.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -714,25 +714,10 @@ def nansum(
714714
If ``None``, the sum is computed over the entire array.
715715
Default: ``None``.
716716
dtype : dtype, optional
717-
Data type of the returned array. If ``None``, the default data
718-
type is inferred from the "kind" of the input array data type.
719-
* If `a` has a real-valued floating-point data type,
720-
the returned array will have the default real-valued
721-
floating-point data type for the device where input
722-
array `a` is allocated.
723-
* If `a` has signed integral data type, the returned array
724-
will have the default signed integral type for the device
725-
where input array `a` is allocated.
726-
* If `a` has unsigned integral data type, the returned array
727-
will have the default unsigned integral type for the device
728-
where input array `a` is allocated.
729-
* If `a` has a complex-valued floating-point data type,
730-
the returned array will have the default complex-valued
731-
floating-pointer data type for the device where input
732-
array `a` is allocated.
733-
* If `a` has a boolean data type, the returned array will
734-
have the default signed integral type for the device
735-
where input array `a` is allocated.
717+
Data type of the returned array. If ``None``, it defaults to the dtype
718+
of `a`, unless `a` has an integer dtype with a precision less than that
719+
of the default platform integer. In that case, the default platform
720+
integer is used.
736721
If the data type (either specified or resolved) differs from the
737722
data type of `a`, the input array elements are cast to the
738723
specified data type before computing the sum.

dpnp/dpnp_iface_trigonometric.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,20 +1352,14 @@ def logsumexp(x, axis=None, out=None, dtype=None, keepdims=False):
13521352
If provided, the result will be inserted into this array. It should
13531353
be of the appropriate shape and dtype.
13541354
dtype : data type, optional
1355-
Data type of the returned array. If ``None``, the default data
1356-
type is inferred from the "kind" of the input array data type.
1357-
* If `x` has a real-valued floating-point data type,
1358-
the returned array will have the default real-valued
1359-
floating-point data type for the device where input
1360-
array `x` is allocated.
1361-
* If `x` has a boolean or integral data type, the returned array
1362-
will have the default floating point data type for the device
1363-
where input array `x` is allocated.
1364-
* If `x` has a complex-valued floating-point data type,
1365-
an error is raised.
1355+
Data type of the returned array. If ``None``, it defaults to the dtype
1356+
of `a`, unless `a` has an integer dtype with a precision less than that
1357+
of the default platform integer. In that case, the default platform
1358+
integer is used.
13661359
If the data type (either specified or resolved) differs from the
13671360
data type of `x`, the input array elements are cast to the
1368-
specified data type before computing the result. Default: ``None``.
1361+
specified data type before computing the result.
1362+
Default: ``None``.
13691363
keepdims : bool
13701364
If ``True``, the reduced axes (dimensions) are included in the result
13711365
as singleton dimensions, so that the returned array remains
@@ -1498,20 +1492,14 @@ def reduce_hypot(x, axis=None, out=None, dtype=None, keepdims=False):
14981492
If provided, the result will be inserted into this array. It should
14991493
be of the appropriate shape and dtype.
15001494
dtype : data type, optional
1501-
Data type of the returned array. If ``None``, the default data
1502-
type is inferred from the "kind" of the input array data type.
1503-
* If `x` has a real-valued floating-point data type,
1504-
the returned array will have the default real-valued
1505-
floating-point data type for the device where input
1506-
array `x` is allocated.
1507-
* If `x` has a boolean or integral data type, the returned array
1508-
will have the default floating point data type for the device
1509-
where input array `x` is allocated.
1510-
* If `x` has a complex-valued floating-point data type,
1511-
an error is raised.
1512-
If the data type (either specified or resolved) differs from the
1513-
data type of `x`, the input array elements are cast to the
1514-
specified data type before computing the result. Default: ``None``.
1495+
Data type of the returned array. If ``None``, it defaults to the dtype
1496+
of `a`, unless `a` has an integer dtype with a precision less than that
1497+
of the default platform integer. In that case, the default platform
1498+
integer is used.
1499+
If the data type (either specified or resolved) differs from the
1500+
data type of `x`, the input array elements are cast to the
1501+
specified data type before computing the result.
1502+
Default: ``None``.
15151503
keepdims : bool
15161504
If ``True``, the reduced axes (dimensions) are included in the result
15171505
as singleton dimensions, so that the returned array remains

tests/test_arithmetic.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@
66
from tests.third_party.cupy import testing
77

88

9-
# Note: numpy.sum() always upcast integers to (u)int64 and float32 to
10-
# float64 for dtype=None. `np.sum` does that too for integers, but not for
11-
# float32, so we need to special-case it for these tests
12-
def _get_dtype_kwargs(xp, dtype):
13-
if xp is numpy and dtype == numpy.float32 and has_support_aspect64():
14-
return {"dtype": numpy.float64}
15-
return {}
16-
17-
189
class TestArithmetic(unittest.TestCase):
1910
@testing.for_float_dtypes()
2011
@testing.numpy_cupy_allclose()
@@ -42,7 +33,7 @@ def test_nanprod(self, xp, dtype):
4233
@testing.numpy_cupy_allclose()
4334
def test_nansum(self, xp, dtype):
4435
a = xp.array([-2.5, -1.5, xp.nan, 10.5, 1.5, xp.nan], dtype=dtype)
45-
return xp.nansum(a, **_get_dtype_kwargs(xp, a.dtype))
36+
return xp.nansum(a)
4637

4738
@testing.for_float_dtypes()
4839
@testing.numpy_cupy_allclose()

tests/test_mathematical.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,7 +1884,9 @@ class TestLogSumExp:
18841884
def test_logsumexp(self, dtype, axis, keepdims):
18851885
a = dpnp.ones((3, 4, 5, 6, 7), dtype=dtype)
18861886
res = dpnp.logsumexp(a, axis=axis, keepdims=keepdims)
1887-
exp_dtype = dpnp.default_float_type(a.device)
1887+
exp_dtype = (
1888+
dpnp.default_float_type(a.device) if dtype == dpnp.bool else None
1889+
)
18881890
exp = numpy.logaddexp.reduce(
18891891
dpnp.asnumpy(a), axis=axis, keepdims=keepdims, dtype=exp_dtype
18901892
)
@@ -1896,11 +1898,17 @@ def test_logsumexp(self, dtype, axis, keepdims):
18961898
@pytest.mark.parametrize("keepdims", [True, False])
18971899
def test_logsumexp_out(self, dtype, axis, keepdims):
18981900
a = dpnp.ones((3, 4, 5, 6, 7), dtype=dtype)
1899-
exp_dtype = dpnp.default_float_type(a.device)
1901+
exp_dtype = (
1902+
dpnp.default_float_type(a.device) if dtype == dpnp.bool else None
1903+
)
19001904
exp = numpy.logaddexp.reduce(
19011905
dpnp.asnumpy(a), axis=axis, keepdims=keepdims, dtype=exp_dtype
19021906
)
1903-
dpnp_out = dpnp.empty(exp.shape, dtype=exp_dtype)
1907+
1908+
exp_dtype = exp.dtype
1909+
if exp_dtype == numpy.float64 and not has_support_aspect64():
1910+
exp_dtype = numpy.float32
1911+
dpnp_out = dpnp.empty_like(a, shape=exp.shape, dtype=exp_dtype)
19041912
res = dpnp.logsumexp(a, axis=axis, out=dpnp_out, keepdims=keepdims)
19051913

19061914
assert res is dpnp_out
@@ -1926,7 +1934,9 @@ class TestReduceHypot:
19261934
def test_reduce_hypot(self, dtype, axis, keepdims):
19271935
a = dpnp.ones((3, 4, 5, 6, 7), dtype=dtype)
19281936
res = dpnp.reduce_hypot(a, axis=axis, keepdims=keepdims)
1929-
exp_dtype = dpnp.default_float_type(a.device)
1937+
exp_dtype = (
1938+
dpnp.default_float_type(a.device) if dtype == dpnp.bool else None
1939+
)
19301940
exp = numpy.hypot.reduce(
19311941
dpnp.asnumpy(a), axis=axis, keepdims=keepdims, dtype=exp_dtype
19321942
)
@@ -1938,11 +1948,17 @@ def test_reduce_hypot(self, dtype, axis, keepdims):
19381948
@pytest.mark.parametrize("keepdims", [True, False])
19391949
def test_reduce_hypot_out(self, dtype, axis, keepdims):
19401950
a = dpnp.ones((3, 4, 5, 6, 7), dtype=dtype)
1941-
exp_dtype = dpnp.default_float_type(a.device)
1951+
exp_dtype = (
1952+
dpnp.default_float_type(a.device) if dtype == dpnp.bool else None
1953+
)
19421954
exp = numpy.hypot.reduce(
19431955
dpnp.asnumpy(a), axis=axis, keepdims=keepdims, dtype=exp_dtype
19441956
)
1945-
dpnp_out = dpnp.empty(exp.shape, dtype=exp_dtype)
1957+
1958+
exp_dtype = exp.dtype
1959+
if exp_dtype == numpy.float64 and not has_support_aspect64():
1960+
exp_dtype = numpy.float32
1961+
dpnp_out = dpnp.empty_like(a, shape=exp.shape, dtype=exp_dtype)
19461962
res = dpnp.reduce_hypot(a, axis=axis, out=dpnp_out, keepdims=keepdims)
19471963

19481964
assert res is dpnp_out

tests/test_sum.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ def test_sum_axis():
6161
ia = dpnp.array(a)
6262

6363
result = dpnp.sum(ia, axis=1)
64-
if has_support_aspect64():
65-
expected = numpy.sum(a, axis=1, dtype=numpy.float64)
66-
else:
67-
expected = numpy.sum(a, axis=1)
64+
expected = numpy.sum(a, axis=1)
6865
assert_array_equal(expected, result)
6966

7067

0 commit comments

Comments
 (0)