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

Reuse dpctl.tensor.sum for dpnp.sum #1426

Merged
merged 9 commits into from
Jun 14, 2023
4 changes: 2 additions & 2 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ def strides(self):

return self._array_obj.strides

def sum(self, axis=None, dtype=None, out=None, keepdims=False, initial=0, where=True):
def sum(self, /, *, axis=None, dtype=None, keepdims=False, out=None, initial=0, where=True):
"""
Returns the sum along a given axis.

Expand All @@ -996,7 +996,7 @@ def sum(self, axis=None, dtype=None, out=None, keepdims=False, initial=0, where=

"""

return dpnp.sum(self, axis, dtype, out, keepdims, initial, where)
return dpnp.sum(self, axis=axis, dtype=dtype, out=out, keepdims=keepdims, initial=initial, where=where)

# 'swapaxes',

Expand Down
60 changes: 34 additions & 26 deletions dpnp/dpnp_iface_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from .dpnp_utils import *

import dpnp
from dpnp.dpnp_array import dpnp_array

import numpy
import dpctl.tensor as dpt
Expand Down Expand Up @@ -175,7 +176,7 @@ def absolute(x,
-------
y : dpnp.ndarray
An array containing the absolute value of each element in `x`.

Limitations
-----------
Parameters `x` is only supported as either :class:`dpnp.ndarray` or :class:`dpctl.tensor.usm_ndarray`.
Expand Down Expand Up @@ -602,7 +603,7 @@ def divide(x1,
-------
y : dpnp.ndarray
The quotient ``x1/x2``, element-wise.

Limitations
-----------
Parameters `x1` and `x2` are supported as either scalar, :class:`dpnp.ndarray`
Expand Down Expand Up @@ -1362,7 +1363,7 @@ def power(x1,
-------
y : dpnp.ndarray
The bases in `x1` raised to the exponents in `x2`.

Limitations
-----------
Parameters `x1` and `x2` are supported as either scalar, :class:`dpnp.ndarray`
Expand Down Expand Up @@ -1558,7 +1559,7 @@ def subtract(x1,
-------
y : dpnp.ndarray
The difference of `x1` and `x2`, element-wise.

Limitations
-----------
Parameters `x1` and `x2` are supported as either scalar, :class:`dpnp.ndarray`
Expand Down Expand Up @@ -1605,45 +1606,52 @@ def subtract(x1,
return call_origin(numpy.subtract, x1, x2, out=out, where=where, dtype=dtype, subok=subok, **kwargs)


def sum(x1, axis=None, dtype=None, out=None, keepdims=False, initial=None, where=True):
def sum(x, /, *, axis=None, dtype=None, keepdims=False, out=None, initial=0, where=True):
"""
Sum of array elements over a given axis.

For full documentation refer to :obj:`numpy.sum`.

Returns
-------
y : dpnp.ndarray
an array containing the sums. If the sum was computed over the
entire array, a zero-dimensional array is returned. The returned
array has the data type as described in the `dtype` parameter
of the Python Array API standard for the `sum` function.

Limitations
-----------
Parameter `where`` is unsupported.
Input array data types are limited by DPNP :ref:`Data types`.
Parameters `x` is supported as either :class:`dpnp.ndarray`
or :class:`dpctl.tensor.usm_ndarray`.
Parameters `out`, `initial` and `where` are supported with their default values.
Otherwise the function will be executed sequentially on CPU.
Input array data types are limited by supported DPNP :ref:`Data types`.

vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved
Examples
--------
>>> import dpnp as np
>>> np.sum(np.array([1, 2, 3, 4, 5]))
15
>>> result = np.sum([[0, 1], [0, 5]], axis=0)
[0, 6]
array(15)
>>> np.sum(np.array(5))
array(5)
>>> result = np.sum(np.array([[0, 1], [0, 5]]), axis=0)
array([0, 6])

"""

x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)
if x1_desc:
if where is not True:
pass
else:
if dpnp.isscalar(out):
raise TypeError("output must be an array")
out_desc = dpnp.get_dpnp_descriptor(out, copy_when_nondefault_queue=False) if out is not None else None
result_obj = dpnp_sum(x1_desc, axis, dtype, out_desc, keepdims, initial, where).get_pyobj()
result = dpnp.convert_single_elem_array_to_scalar(result_obj, keepdims)

if x1_desc.size == 0 and axis is None:
result = dpnp.zeros_like(result)
if out is not None:
out[...] = result
return result
if out is not None:
pass
elif initial != 0:
pass
elif where is not True:
pass
else:
y = dpt.sum(dpnp.get_usm_ndarray(x), axis=axis, dtype=dtype, keepdims=keepdims)
return dpnp_array._create_from_usm_ndarray(y)

return call_origin(numpy.sum, x1, axis=axis, dtype=dtype, out=out, keepdims=keepdims, initial=initial, where=where)
return call_origin(numpy.sum, x, axis=axis, dtype=dtype, out=out, keepdims=keepdims, initial=initial, where=where)


def trapz(y1, x1=None, dx=1.0, axis=-1):
Expand Down
17 changes: 0 additions & 17 deletions tests/skipped_tests.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -829,22 +829,6 @@ tests/third_party/cupy/math_tests/test_rounding.py::TestRounding::test_rint
tests/third_party/cupy/math_tests/test_rounding.py::TestRounding::test_rint_negative
tests/third_party/cupy/math_tests/test_rounding.py::TestRounding::test_round_
tests/third_party/cupy/math_tests/test_rounding.py::TestRounding::test_trunc
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_all
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_all2
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_all_keepdims
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_all_transposed2
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axes
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axes2
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axes3
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axes4
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axis
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axis2
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axis_huge
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axis_transposed
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axis_transposed2
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_dtype
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_keepdims_and_dtype
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_keepdims_multiple_axes
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_out
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_out_wrong_shape
tests/third_party/cupy/math_tests/test_sumprod.py::TestCumprod::test_ndarray_cumprod_2dim_with_axis
Expand Down Expand Up @@ -879,7 +863,6 @@ tests/third_party/cupy/math_tests/test_sumprod.py::TestNansumNanprodLong_param_1
tests/third_party/cupy/math_tests/test_sumprod.py::TestNansumNanprodLong_param_15_{axis=0, func='nanprod', keepdims=False, shape=(20, 30, 40), transpose_axes=False}::test_nansum_axis_transposed
tests/third_party/cupy/math_tests/test_sumprod.py::TestNansumNanprodLong_param_9_{axis=0, func='nanprod', keepdims=True, shape=(2, 3, 4), transpose_axes=False}::test_nansum_all
tests/third_party/cupy/math_tests/test_sumprod.py::TestNansumNanprodLong_param_9_{axis=0, func='nanprod', keepdims=True, shape=(2, 3, 4), transpose_axes=False}::test_nansum_axis_transposed
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_all2
tests/third_party/cupy/math_tests/test_trigonometric.py::TestUnwrap::test_unwrap_1dim
tests/third_party/cupy/math_tests/test_trigonometric.py::TestUnwrap::test_unwrap_1dim_with_discont
tests/third_party/cupy/math_tests/test_trigonometric.py::TestUnwrap::test_unwrap_2dim_with_axis
Expand Down
29 changes: 3 additions & 26 deletions tests/skipped_tests_gpu.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,8 @@ tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_para
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_535_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int64), arg2=array([[0, 1, 2], [3, 4, 5]]), dtype=float64, name='floor_divide', use_dtype=False}::test_binary
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_543_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int64), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int64), dtype=float64, name='floor_divide', use_dtype=False}::test_binary

tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_external_prod_all
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_external_prod_axis
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_external_sum_all
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_external_sum_axis
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_prod_all
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_prod_axis
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_all
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_all2
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_all_keepdims
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_out
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_out_wrong_shape
tests/third_party/cupy/math_tests/test_sumprod.py::TestCumprod::test_cumprod_1dim
tests/third_party/cupy/math_tests/test_sumprod.py::TestCumprod::test_cumprod_2dim_without_axis
tests/third_party/cupy/math_tests/test_sumprod.py::TestCumsum_param_0_{axis=0}::test_cumsum
Expand Down Expand Up @@ -967,22 +960,6 @@ tests/third_party/cupy/math_tests/test_rounding.py::TestRounding::test_rint
tests/third_party/cupy/math_tests/test_rounding.py::TestRounding::test_rint_negative
tests/third_party/cupy/math_tests/test_rounding.py::TestRounding::test_round_
tests/third_party/cupy/math_tests/test_rounding.py::TestRounding::test_trunc

tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_all_transposed2
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axes
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axes2
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axes3
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axes4
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axis
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axis2
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axis_huge
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axis_transposed
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_axis_transposed2
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_dtype
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_keepdims_and_dtype
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_keepdims_multiple_axes
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_out
tests/third_party/cupy/math_tests/test_sumprod.py::TestSumprod::test_sum_out_wrong_shape
tests/third_party/cupy/math_tests/test_sumprod.py::TestCumprod::test_ndarray_cumprod_2dim_with_axis
tests/third_party/cupy/math_tests/test_sumprod.py::TestDiff::test_diff_1dim
tests/third_party/cupy/math_tests/test_sumprod.py::TestDiff::test_diff_1dim_with_n
Expand Down Expand Up @@ -1367,7 +1344,7 @@ tests/third_party/cupy/statistics_tests/test_histogram.py::TestHistogram::test_h
tests/third_party/cupy/statistics_tests/test_histogram.py::TestHistogram::test_histogram_array_bins
tests/third_party/cupy/statistics_tests/test_histogram.py::TestHistogram::test_histogram_bins_not_ordered
tests/third_party/cupy/statistics_tests/test_histogram.py::TestHistogram::test_histogram_complex_weights
tests/third_party/cupy/statistics_tests/test_histogram.py::TestHistogram::test_histogram_complex_weights_uneven_bins
tests/third_party/cupy/statistics_tests/test_histogram.py::TestHistogram::test_histogram_complex_weights_uneven_bins
tests/third_party/cupy/statistics_tests/test_histogram.py::TestHistogram::test_histogram_density
tests/third_party/cupy/statistics_tests/test_histogram.py::TestHistogram::test_histogram_empty
tests/third_party/cupy/statistics_tests/test_histogram.py::TestHistogram::test_histogram_float_weights
Expand Down
3 changes: 2 additions & 1 deletion tests/test_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def test_ediff1d_int(self, array, data_type):
expected = numpy.ediff1d(np_a)
assert_array_equal(expected, result)


@pytest.mark.usefixtures("allow_fall_back_on_numpy")
def test_ediff1d_args(self):
np_a = numpy.array([1, 2, 4, 7, 0])
Expand Down Expand Up @@ -934,6 +934,7 @@ def test_sum_empty(dtype, axis):
assert_array_equal(numpy_res, dpnp_res.asnumpy())


@pytest.mark.usefixtures("allow_fall_back_on_numpy")
@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True, no_bool=True))
def test_sum_empty_out(dtype):
a = dpnp.empty((1, 2, 0, 4), dtype=dtype)
Expand Down
24 changes: 18 additions & 6 deletions tests/test_sum.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import pytest

import dpnp
from tests.helper import get_float_dtypes, has_support_aspect64

import numpy


def test_sum_float64():
a = numpy.array([[[-2., 3.], [9.1, 0.2]], [[-2., 5.0], [-2, -1.2]], [[1.0, -2.], [5.0, -1.1]]])
# 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
@pytest.mark.parametrize("dtype", get_float_dtypes())
def test_sum_float(dtype):
a = numpy.array([[[-2., 3.], [9.1, 0.2]], [[-2., 5.0], [-2, -1.2]], [[1.0, -2.], [5.0, -1.1]]], dtype=dtype)
ia = dpnp.array(a)

for axis in range(len(a)):
result = dpnp.sum(ia, axis=axis)
expected = numpy.sum(a, axis=axis)
if dtype == dpnp.float32 and has_support_aspect64():
expected = numpy.sum(a, axis=axis, dtype=numpy.float64)
else:
expected = numpy.sum(a, axis=axis)
numpy.testing.assert_array_equal(expected, result)


Expand All @@ -23,9 +32,12 @@ def test_sum_int():


def test_sum_axis():
a = numpy.array([[[-2., 3.], [9.1, 0.2]], [[-2., 5.0], [-2, -1.2]], [[1.0, -2.], [5.0, -1.1]]])
a = numpy.array([[[-2., 3.], [9.1, 0.2]], [[-2., 5.0], [-2, -1.2]], [[1.0, -2.], [5.0, -1.1]]], dtype='f4')
ia = dpnp.array(a)

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