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

Update dpnp.clip() with Numpy 2.0 #2048

Merged
merged 7 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
23 changes: 11 additions & 12 deletions dpnp/dpnp_iface_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ def around(x, /, decimals=0, out=None):
)


def clip(a, a_min, a_max, *, out=None, order="K", **kwargs):
def clip(a, /, min=None, max=None, *, out=None, order="K", **kwargs):
"""
Clip (limit) the values in an array.

Expand All @@ -637,23 +637,25 @@ def clip(a, a_min, a_max, *, out=None, order="K", **kwargs):
----------
a : {dpnp.ndarray, usm_ndarray}
Array containing elements to clip.
a_min, a_max : {dpnp.ndarray, usm_ndarray, None}
min, max : {dpnp.ndarray, usm_ndarray, None}
Minimum and maximum value. If ``None``, clipping is not performed on
the corresponding edge. Only one of `a_min` and `a_max` may be
``None``. Both are broadcast against `a`.
the corresponding edge. If both ``min`` and ``max`` are ``None``,
vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved
the elements of the returned array stay the same.
Both are broadcast against `a`.
Default : ``None``.
out : {None, dpnp.ndarray, usm_ndarray}, optional
The results will be placed in this array. It may be the input array
for in-place clipping. `out` must be of the right shape to hold the
output. Its type is preserved.
vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved
order : {"C", "F", "A", "K", None}, optional
Memory layout of the newly output array, if parameter `out` is `None`.
Memory layout of the newly output array, if parameter `out` is ``None``.
If `order` is ``None``, the default value ``"K"`` will be used.

Returns
-------
out : dpnp.ndarray
An array with the elements of `a`, but where values < `a_min` are
replaced with `a_min`, and those > `a_max` with `a_max`.
An array with the elements of `a`, but where values < `min` are
replaced with `min`, and those > `max` with `max`.

Limitations
-----------
Expand Down Expand Up @@ -687,15 +689,12 @@ def clip(a, a_min, a_max, *, out=None, order="K", **kwargs):
if kwargs:
raise NotImplementedError(f"kwargs={kwargs} is currently not supported")

if a_min is None and a_max is None:
raise ValueError("One of max or min must be given")

if order is None:
order = "K"

usm_arr = dpnp.get_usm_ndarray(a)
usm_min = None if a_min is None else dpnp.get_usm_ndarray_or_scalar(a_min)
usm_max = None if a_max is None else dpnp.get_usm_ndarray_or_scalar(a_max)
usm_min = None if min is None else dpnp.get_usm_ndarray_or_scalar(min)
usm_max = None if max is None else dpnp.get_usm_ndarray_or_scalar(max)

usm_out = None if out is None else dpnp.get_usm_ndarray(out)
usm_res = dpt.clip(usm_arr, usm_min, usm_max, out=usm_out, order=order)
Expand Down
17 changes: 13 additions & 4 deletions tests/third_party/cupy/math_tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,15 @@ def test_clip_max_none(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
return a.clip(3, None)

# According to Python Array API, clip() should return
# an array with the same elements in `a` if `min` and `max` are `None`.
# Numpy < 2.0 is not compatible with this and throws a ValueError
@testing.with_requires("numpy>=2.0")
@testing.for_all_dtypes(no_bool=True, no_complex=True)
def test_clip_min_max_none(self, dtype):
for xp in (numpy, cupy):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
with pytest.raises(ValueError):
a.clip(None, None)
return a.clip(None, None)

@testing.for_all_dtypes(no_complex=True)
@testing.numpy_cupy_array_equal()
Expand All @@ -155,8 +158,14 @@ def test_external_clip3(self, xp, dtype):
def test_external_clip4(self, dtype):
for xp in (numpy, cupy):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
with pytest.raises(TypeError):
xp.clip(a, 3)
# dpnp.clip() has min, max arguments as None by default
# and allows passing one of these, according to Python Array API,
# while numpy throws a TypeError in this case
antonwolfy marked this conversation as resolved.
Show resolved Hide resolved
if xp is numpy:
with pytest.raises(TypeError):
xp.clip(a, 3)
else:
return xp.clip(a, 3)

@testing.for_all_dtypes(no_complex=True)
@testing.numpy_cupy_array_equal()
Expand Down
Loading