Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
cherry-pick
  • Loading branch information
vtavana committed Oct 22, 2023
1 parent 0f23b47 commit e0b9f89
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 125 deletions.
25 changes: 0 additions & 25 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,31 +1033,6 @@ def prod(

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

def ptp(
self,
axis=None,
out=None,
keepdims=numpy._NoValue,
device=None,
usm_type=None,
sycl_queue=None,
):
"""
Range of values (maximum - minimum) along an axis.
For full documentation refer to :obj:`numpy.ptp`.
"""

return dpnp.ptp(
self,
axis=axis,
out=out,
keepdims=keepdims,
device=device,
usm_type=usm_type,
sycl_queue=sycl_queue,
)

def put(self, indices, vals, /, *, axis=None, mode="wrap"):
"""
Puts values of an array into another array along a given axis.
Expand Down
64 changes: 0 additions & 64 deletions dpnp/dpnp_iface_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
"ogrid",
"ones",
"ones_like",
"ptp",
"trace",
"tri",
"tril",
Expand Down Expand Up @@ -1602,69 +1601,6 @@ def ones_like(
return call_origin(numpy.ones_like, x1, dtype, order, subok, shape)


def ptp(
arr,
/,
axis=None,
out=None,
keepdims=numpy._NoValue,
*,
device=None,
usm_type=None,
sycl_queue=None,
):
"""
Range of values (maximum - minimum) along an axis.
For full documentation refer to :obj:`numpy.ptp`.
Returns
-------
ptp : dpnp.ndarray
The range of a given array.
Limitations
-----------
Input array is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray`.
Parameters `out` and `keepdims` are supported only with default values.
Otherwise the function will be executed sequentially on CPU.
Examples
--------
>>> import dpnp as np
>>> x = np.array([[4, 9, 2, 10],[6, 9, 7, 12]])
>>> np.ptp(x, axis=1)
array([8, 6])
>>> np.ptp(x, axis=0)
array([2, 0, 5, 2])
>>> np.ptp(x)
array(10)
"""
if not isinstance(arr, (dpnp.ndarray, dpt.usm_ndarray)):
pass
elif axis is not None and not isinstance(axis, int):
pass
elif out is not None:
pass
elif keepdims is not numpy._NoValue:
pass
else:
max_array = dpnp.max(arr, axis=axis)
min_array = dpnp.min(arr, axis=axis)

_usm_type = arr.usm_type if usm_type is None else usm_type
_sycl_queue = dpnp.get_normalized_queue_device(
arr, sycl_queue=sycl_queue, device=device
)
return dpnp.array(
max_array - min_array, usm_type=_usm_type, sycl_queue=_sycl_queue
)

return call_origin(numpy.ptp, arr, axis, out, keepdims)


def trace(x1, offset=0, axis1=0, axis2=1, dtype=None, out=None):
"""
Return the sum along diagonals of the array.
Expand Down
50 changes: 50 additions & 0 deletions dpnp/dpnp_iface_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"mean",
"median",
"min",
"ptp",
"nanvar",
"std",
"var",
Expand Down Expand Up @@ -621,6 +622,55 @@ def min(a, axis=None, out=None, keepdims=False, initial=None, where=True):
return call_origin(numpy.min, a, axis, out, keepdims, initial, where)


def ptp(
a,
/,
axis=None,
out=None,
keepdims=False,
):
"""
Range of values (maximum - minimum) along an axis.
For full documentation refer to :obj:`numpy.ptp`.
Returns
-------
ptp : dpnp.ndarray
The range of a given array.
Limitations
-----------
Input array is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray`.
Otherwise the function will be executed sequentially on CPU.
Examples
--------
>>> import dpnp as np
>>> x = np.array([[4, 9, 2, 10],[6, 9, 7, 12]])
>>> np.ptp(x, axis=1)
array([8, 6])
>>> np.ptp(x, axis=0)
array([2, 0, 5, 2])
>>> np.ptp(x)
array(10)
"""

if not isinstance(a, (dpnp.ndarray, dpt.usm_ndarray)):
pass
else:
return dpnp.subtract(
dpnp.max(a, axis=axis, keepdims=keepdims),
dpnp.min(a, axis=axis, keepdims=keepdims),
out=out,
)

return call_origin(numpy.ptp, a, axis, out, keepdims)


def nanvar(x1, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
"""
Compute the variance along the specified axis, while ignoring NaNs.
Expand Down
21 changes: 0 additions & 21 deletions tests/skipped_tests.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -117,31 +117,10 @@ tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayToBytes
tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayToBytes_param_3_{order='C', shape=(2, 3)}::test_item
tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayToBytes_param_4_{order='F', shape=(2, 3)}::test_item

<<<<<<< HEAD
<<<<<<< HEAD
tests/third_party/cupy/core_tests/test_ndarray_copy_and_view.py::TestArrayFlatten::test_flatten_order
tests/third_party/cupy/core_tests/test_ndarray_copy_and_view.py::TestArrayFlatten::test_flatten_order_copied
tests/third_party/cupy/core_tests/test_ndarray_copy_and_view.py::TestArrayFlatten::test_flatten_order_transposed

=======
>>>>>>> implement dpnp.max and dpnp.max using dpctl.tensor functions
=======
>>>>>>> rework implementation of diag, diagflat, vander, and ptp
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_all
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_all_keepdims
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_axis0
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_axis1
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_axis2
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_axis_large
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_multiple_axes
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_multiple_axes_keepdims
=======

>>>>>>> rework implementation of diag, diagflat, vander, and ptp
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_nan
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_nan_imag
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_nan_real

tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_0_{order='C', shape=(10,)}::test_cub_max
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_0_{order='C', shape=(10,)}::test_cub_min
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_1_{order='C', shape=(10, 20)}::test_cub_max
Expand Down
4 changes: 1 addition & 3 deletions tests/skipped_tests_gpu.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ tests/third_party/cupy/core_tests/test_ndarray_copy_and_view.py::TestArrayFlatte
tests/third_party/cupy/core_tests/test_ndarray_copy_and_view.py::TestArrayFlatten::test_flatten_order_copied
tests/third_party/cupy/core_tests/test_ndarray_copy_and_view.py::TestArrayFlatten::test_flatten_order_transposed

tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_nan
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_nan_imag
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_nan_real


tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_0_{order='C', shape=(10,)}::test_cub_max
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_0_{order='C', shape=(10,)}::test_cub_min
Expand Down
23 changes: 11 additions & 12 deletions tests/third_party/cupy/core_tests/test_ndarray_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from tests.third_party.cupy import testing


@pytest.mark.usefixtures("allow_fall_back_on_numpy")
@testing.gpu
class TestArrayReduction(unittest.TestCase):
@testing.for_all_dtypes()
Expand Down Expand Up @@ -149,70 +148,70 @@ def test_min_nan_imag(self, xp, dtype):
@testing.numpy_cupy_allclose()
def test_ptp_all(self, xp, dtype):
a = testing.shaped_random((2, 3), xp, dtype)
return a.ptp()
return xp.ptp(a)

@testing.with_requires("numpy>=1.15")
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose()
def test_ptp_all_keepdims(self, xp, dtype):
a = testing.shaped_random((2, 3), xp, dtype)
return a.ptp(keepdims=True)
return xp.ptp(a, keepdims=True)

@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose()
def test_ptp_axis_large(self, xp, dtype):
a = testing.shaped_random((3, 1000), xp, dtype)
return a.ptp(axis=0)
return xp.ptp(a, axis=0)

@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose()
def test_ptp_axis0(self, xp, dtype):
a = testing.shaped_random((2, 3, 4), xp, dtype)
return a.ptp(axis=0)
return xp.ptp(a, axis=0)

@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose()
def test_ptp_axis1(self, xp, dtype):
a = testing.shaped_random((2, 3, 4), xp, dtype)
return a.ptp(axis=1)
return xp.ptp(a, axis=1)

@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose()
def test_ptp_axis2(self, xp, dtype):
a = testing.shaped_random((2, 3, 4), xp, dtype)
return a.ptp(axis=2)
return xp.ptp(a, axis=2)

@testing.with_requires("numpy>=1.15")
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose()
def test_ptp_multiple_axes(self, xp, dtype):
a = testing.shaped_random((2, 3, 4), xp, dtype)
return a.ptp(axis=(1, 2))
return xp.ptp(a, axis=(1, 2))

@testing.with_requires("numpy>=1.15")
@testing.for_all_dtypes(no_bool=True)
@testing.numpy_cupy_allclose()
def test_ptp_multiple_axes_keepdims(self, xp, dtype):
a = testing.shaped_random((2, 3, 4), xp, dtype)
return a.ptp(axis=(1, 2), keepdims=True)
return xp.ptp(a, axis=(1, 2), keepdims=True)

@testing.for_float_dtypes()
@testing.numpy_cupy_allclose()
def test_ptp_nan(self, xp, dtype):
a = xp.array([float("nan"), 1, -1], dtype)
return a.ptp()
return xp.ptp(a)

@testing.for_complex_dtypes()
@testing.numpy_cupy_allclose()
def test_ptp_nan_real(self, xp, dtype):
a = xp.array([float("nan"), 1, -1], dtype)
return a.ptp()
return xp.ptp(a)

@testing.for_complex_dtypes()
@testing.numpy_cupy_allclose()
def test_ptp_nan_imag(self, xp, dtype):
a = xp.array([float("nan") * 1.0j, 1.0j, -1.0j], dtype)
return a.ptp()
return xp.ptp(a)


# This class compares CUB results against NumPy's
Expand Down

0 comments on commit e0b9f89

Please sign in to comment.