Skip to content
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

Align with new array api version in dpctl #1774

Merged
merged 4 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions dpnp/dpnp_iface_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@


import dpctl.tensor as dpt
import dpctl.tensor._type_utils as dtu
import numpy
from numpy.core.numeric import (
normalize_axis_index,
Expand Down Expand Up @@ -2905,8 +2906,6 @@ def sum(
)
)
):
from dpctl.tensor._reduction import _default_reduction_dtype

from dpnp.backend.extensions.sycl_ext import _sycl_ext_impl

input = a
Expand All @@ -2916,7 +2915,7 @@ def sum(

queue = input.sycl_queue
out_dtype = (
_default_reduction_dtype(input.dtype, queue)
dtu._default_accumulation_dtype(input.dtype, queue)
if dtype is None
else dtype
)
Expand Down
11 changes: 1 addition & 10 deletions tests/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@
from tests.third_party.cupy import testing


# Note: numpy.sum() always upcast integers to (u)int64 and float32 to
# float64 for dtype=None. `np.sum` does that too for integers, but not for
# float32, so we need to special-case it for these tests
def _get_dtype_kwargs(xp, dtype):
if xp is numpy and dtype == numpy.float32 and has_support_aspect64():
return {"dtype": numpy.float64}
return {}


class TestArithmetic(unittest.TestCase):
@testing.for_float_dtypes()
@testing.numpy_cupy_allclose()
Expand Down Expand Up @@ -42,7 +33,7 @@ def test_nanprod(self, xp, dtype):
@testing.numpy_cupy_allclose()
def test_nansum(self, xp, dtype):
a = xp.array([-2.5, -1.5, xp.nan, 10.5, 1.5, xp.nan], dtype=dtype)
return xp.nansum(a, **_get_dtype_kwargs(xp, a.dtype))
return xp.nansum(a)

@testing.for_float_dtypes()
@testing.numpy_cupy_allclose()
Expand Down
28 changes: 22 additions & 6 deletions tests/test_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,9 @@ class TestLogSumExp:
def test_logsumexp(self, dtype, axis, keepdims):
a = dpnp.ones((3, 4, 5, 6, 7), dtype=dtype)
res = dpnp.logsumexp(a, axis=axis, keepdims=keepdims)
exp_dtype = dpnp.default_float_type(a.device)
exp_dtype = (
dpnp.default_float_type(a.device) if dtype == dpnp.bool else None
)
exp = numpy.logaddexp.reduce(
dpnp.asnumpy(a), axis=axis, keepdims=keepdims, dtype=exp_dtype
)
Expand All @@ -1896,11 +1898,17 @@ def test_logsumexp(self, dtype, axis, keepdims):
@pytest.mark.parametrize("keepdims", [True, False])
def test_logsumexp_out(self, dtype, axis, keepdims):
a = dpnp.ones((3, 4, 5, 6, 7), dtype=dtype)
exp_dtype = dpnp.default_float_type(a.device)
exp_dtype = (
dpnp.default_float_type(a.device) if dtype == dpnp.bool else None
)
exp = numpy.logaddexp.reduce(
dpnp.asnumpy(a), axis=axis, keepdims=keepdims, dtype=exp_dtype
)
dpnp_out = dpnp.empty(exp.shape, dtype=exp_dtype)

exp_dtype = exp.dtype
if exp_dtype == numpy.float64 and not has_support_aspect64():
exp_dtype = numpy.float32
dpnp_out = dpnp.empty_like(a, shape=exp.shape, dtype=exp_dtype)
res = dpnp.logsumexp(a, axis=axis, out=dpnp_out, keepdims=keepdims)

assert res is dpnp_out
Expand All @@ -1926,7 +1934,9 @@ class TestReduceHypot:
def test_reduce_hypot(self, dtype, axis, keepdims):
a = dpnp.ones((3, 4, 5, 6, 7), dtype=dtype)
res = dpnp.reduce_hypot(a, axis=axis, keepdims=keepdims)
exp_dtype = dpnp.default_float_type(a.device)
exp_dtype = (
dpnp.default_float_type(a.device) if dtype == dpnp.bool else None
)
exp = numpy.hypot.reduce(
dpnp.asnumpy(a), axis=axis, keepdims=keepdims, dtype=exp_dtype
)
Expand All @@ -1938,11 +1948,17 @@ def test_reduce_hypot(self, dtype, axis, keepdims):
@pytest.mark.parametrize("keepdims", [True, False])
def test_reduce_hypot_out(self, dtype, axis, keepdims):
a = dpnp.ones((3, 4, 5, 6, 7), dtype=dtype)
exp_dtype = dpnp.default_float_type(a.device)
exp_dtype = (
dpnp.default_float_type(a.device) if dtype == dpnp.bool else None
)
exp = numpy.hypot.reduce(
dpnp.asnumpy(a), axis=axis, keepdims=keepdims, dtype=exp_dtype
)
dpnp_out = dpnp.empty(exp.shape, dtype=exp_dtype)

exp_dtype = exp.dtype
if exp_dtype == numpy.float64 and not has_support_aspect64():
exp_dtype = numpy.float32
dpnp_out = dpnp.empty_like(a, shape=exp.shape, dtype=exp_dtype)
res = dpnp.reduce_hypot(a, axis=axis, out=dpnp_out, keepdims=keepdims)

assert res is dpnp_out
Expand Down
5 changes: 1 addition & 4 deletions tests/test_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ def test_sum_axis():
ia = dpnp.array(a)

result = dpnp.sum(ia, axis=1)
if has_support_aspect64():
expected = numpy.sum(a, axis=1, dtype=numpy.float64)
else:
expected = numpy.sum(a, axis=1)
expected = numpy.sum(a, axis=1)
assert_array_equal(expected, result)


Expand Down
62 changes: 24 additions & 38 deletions tests/third_party/cupy/math_tests/test_sumprod.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@
from tests.third_party.cupy import testing


# Note: numpy.sum() always upcast integers to (u)int64 and float32 to
# float64 for dtype=None. `np.sum` does that too for integers, but not for
# float32, so we need to special-case it for these tests
def _get_dtype_kwargs(xp, dtype):
if xp is numpy and dtype == numpy.float32 and has_support_aspect64():
return {"dtype": numpy.float64}
return {}


class TestSumprod:
def tearDown(self):
# Free huge memory for slow test
Expand All @@ -26,43 +17,43 @@ def tearDown(self):
@testing.numpy_cupy_allclose()
def test_sum_all(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
return a.sum(**_get_dtype_kwargs(xp, dtype))
return a.sum()

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_sum_all_keepdims(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
return a.sum(**_get_dtype_kwargs(xp, dtype), keepdims=True)
return a.sum(keepdims=True)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_external_sum_all(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
return xp.sum(a, **_get_dtype_kwargs(xp, dtype))
return xp.sum(a)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose(rtol=1e-06)
def test_sum_all2(self, xp, dtype):
a = testing.shaped_arange((20, 30, 40), xp, dtype)
return a.sum(**_get_dtype_kwargs(xp, dtype))
return a.sum()

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_sum_all_transposed(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype).transpose(2, 0, 1)
return a.sum(**_get_dtype_kwargs(xp, dtype))
return a.sum()

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose(rtol=1e-06)
def test_sum_all_transposed2(self, xp, dtype):
a = testing.shaped_arange((20, 30, 40), xp, dtype).transpose(2, 0, 1)
return a.sum(**_get_dtype_kwargs(xp, dtype))
return a.sum()

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_sum_axis(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
return a.sum(**_get_dtype_kwargs(xp, dtype), axis=1)
return a.sum(axis=1)

@testing.slow
@testing.numpy_cupy_allclose()
Expand All @@ -74,57 +65,57 @@ def test_sum_axis_huge(self, xp):
@testing.numpy_cupy_allclose()
def test_external_sum_axis(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
return xp.sum(a, **_get_dtype_kwargs(xp, dtype), axis=1)
return xp.sum(a, axis=1)

# float16 is omitted, since NumPy's sum on float16 arrays has more error
# than CuPy's.
@testing.for_all_dtypes(no_float16=True)
@testing.numpy_cupy_allclose()
def test_sum_axis2(self, xp, dtype):
a = testing.shaped_arange((20, 30, 40), xp, dtype)
return a.sum(**_get_dtype_kwargs(xp, dtype), axis=1)
return a.sum(axis=1)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose(contiguous_check=False)
def test_sum_axis_transposed(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype).transpose(2, 0, 1)
return a.sum(**_get_dtype_kwargs(xp, dtype), axis=1)
return a.sum(axis=1)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose(contiguous_check=False)
def test_sum_axis_transposed2(self, xp, dtype):
a = testing.shaped_arange((20, 30, 40), xp, dtype).transpose(2, 0, 1)
return a.sum(**_get_dtype_kwargs(xp, dtype), axis=1)
return a.sum(axis=1)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_sum_axes(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4, 5), xp, dtype)
return a.sum(**_get_dtype_kwargs(xp, dtype), axis=(1, 3))
return a.sum(axis=(1, 3))

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose(rtol=1e-4)
def test_sum_axes2(self, xp, dtype):
a = testing.shaped_arange((20, 30, 40, 50), xp, dtype)
return a.sum(**_get_dtype_kwargs(xp, dtype), axis=(1, 3))
return a.sum(axis=(1, 3))

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose(rtol=1e-6)
def test_sum_axes3(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4, 5), xp, dtype)
return a.sum(**_get_dtype_kwargs(xp, dtype), axis=(0, 2, 3))
return a.sum(axis=(0, 2, 3))

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose(rtol=1e-6)
def test_sum_axes4(self, xp, dtype):
a = testing.shaped_arange((20, 30, 40, 50), xp, dtype)
return a.sum(**_get_dtype_kwargs(xp, dtype), axis=(0, 2, 3))
return a.sum(axis=(0, 2, 3))

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_sum_empty_axis(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4, 5), xp, dtype)
return a.sum(**_get_dtype_kwargs(xp, dtype), axis=())
return a.sum(axis=())

@testing.for_all_dtypes_combination(names=["src_dtype", "dst_dtype"])
@testing.numpy_cupy_allclose()
Expand All @@ -142,7 +133,7 @@ def test_sum_keepdims_and_dtype(self, xp, src_dtype, dst_dtype):
@testing.numpy_cupy_allclose()
def test_sum_keepdims_multiple_axes(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
return a.sum(**_get_dtype_kwargs(xp, dtype), axis=(1, 2), keepdims=True)
return a.sum(axis=(1, 2), keepdims=True)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
Expand All @@ -162,25 +153,25 @@ def test_sum_out_wrong_shape(self):
@testing.numpy_cupy_allclose()
def test_prod_all(self, xp, dtype):
a = testing.shaped_arange((2, 3), xp, dtype)
return a.prod(**_get_dtype_kwargs(xp, dtype))
return a.prod()

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_external_prod_all(self, xp, dtype):
a = testing.shaped_arange((2, 3), xp, dtype)
return xp.prod(a, **_get_dtype_kwargs(xp, dtype))
return xp.prod(a)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_prod_axis(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
return a.prod(axis=1, **_get_dtype_kwargs(xp, dtype))
return a.prod(axis=1)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_external_prod_axis(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
return xp.prod(a, axis=1, **_get_dtype_kwargs(xp, dtype))
return xp.prod(a, axis=1)

@testing.for_all_dtypes_combination(names=["src_dtype", "dst_dtype"])
@testing.numpy_cupy_allclose()
Expand Down Expand Up @@ -228,12 +219,7 @@ def _test(self, xp, dtype):
if not issubclass(dtype, xp.integer):
a[:, 1] = xp.nan
func = getattr(xp, self.func)
return func(
a,
**_get_dtype_kwargs(xp, dtype),
axis=self.axis,
keepdims=self.keepdims,
)
return func(a, axis=self.axis, keepdims=self.keepdims)

@testing.for_all_dtypes(no_bool=True, no_float16=True)
@testing.numpy_cupy_allclose(type_check=has_support_aspect64())
Expand Down Expand Up @@ -299,15 +285,15 @@ def test_nansum_axes(self, xp, dtype):
a = testing.shaped_arange(self.shape, xp, dtype)
if not issubclass(dtype, xp.integer):
a[:, 1] = xp.nan
return xp.nansum(a, **_get_dtype_kwargs(xp, dtype), axis=self.axis)
return xp.nansum(a, axis=self.axis)


class TestNansumNanprodHuge:
def _test(self, xp, nan_slice):
a = testing.shaped_random((2048, 1, 1024), xp, "f")
a[nan_slice] = xp.nan
a = xp.broadcast_to(a, (2048, 256, 1024))
return xp.nansum(a, **_get_dtype_kwargs(xp, a.dtype), axis=2)
return xp.nansum(a, axis=2)

@testing.slow
@testing.numpy_cupy_allclose(atol=1e-1)
Expand Down
Loading