Skip to content

Commit 4a0c17f

Browse files
author
7908837174
committed
Fix issue #21936: Add missing clip and clip_ functions to paddle frontend
- Added clip function to ivy/functional/frontends/paddle/math.py - Added clip_ inplace function to ivy/functional/frontends/paddle/math.py - Added comprehensive tests for both functions in test_paddle/test_math.py - Functions follow established patterns with proper decorators and error handling - clip_ uses ivy.inplace_update pattern consistent with other inplace operations
1 parent 47b2ff2 commit 4a0c17f

File tree

2 files changed

+111
-0
lines changed
  • ivy_tests/test_ivy/test_frontends/test_paddle
  • ivy/functional/frontends/paddle

2 files changed

+111
-0
lines changed

ivy/functional/frontends/paddle/math.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,34 @@ def ceil(x, name=None):
140140
return ivy.ceil(x)
141141

142142

143+
@with_supported_dtypes(
144+
{"2.6.0 and below": ("float32", "float64", "int32", "int64")}, "paddle"
145+
)
146+
@to_ivy_arrays_and_back
147+
def clip(x, min=None, max=None, name=None):
148+
ivy.utils.assertions.check_all_or_any_fn(
149+
min,
150+
max,
151+
fn=ivy.exists,
152+
type="any",
153+
limit=[1, 2],
154+
message="at most one of min or max can be None",
155+
)
156+
if min is None:
157+
min = ivy.min(x)
158+
if max is None:
159+
max = ivy.max(x)
160+
return ivy.clip(x, min, max)
161+
162+
163+
@with_supported_dtypes(
164+
{"2.6.0 and below": ("float32", "float64", "int32", "int64")}, "paddle"
165+
)
166+
@to_ivy_arrays_and_back
167+
def clip_(x, min=None, max=None, name=None):
168+
return ivy.inplace_update(x, clip(x, min, max))
169+
170+
143171
@with_supported_dtypes(
144172
{
145173
"2.6.0 and below": (

ivy_tests/test_ivy/test_frontends/test_paddle/test_math.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ def _test_paddle_take_helper(draw):
6161
return dtypes, xs, indices, mode
6262

6363

64+
@st.composite
65+
def _get_clip_inputs_(draw):
66+
shape = draw(
67+
helpers.get_shape(
68+
min_num_dims=1, max_num_dims=5, min_dim_size=1, max_dim_size=10
69+
)
70+
)
71+
x_dtype, x = draw(
72+
helpers.dtype_and_values(
73+
available_dtypes=helpers.get_dtypes("valid"),
74+
shape=shape,
75+
min_value=0,
76+
max_value=50,
77+
)
78+
)
79+
80+
return x_dtype, x
81+
82+
6483
# --- Main --- #
6584
# ------------ #
6685

@@ -595,6 +614,70 @@ def test_paddle_ceil(
595614
)
596615

597616

617+
# clip
618+
@handle_frontend_test(
619+
fn_tree="paddle.clip",
620+
input_and_ranges=_get_clip_inputs_(),
621+
min=st.integers(min_value=0, max_value=5),
622+
max=st.integers(min_value=5, max_value=10),
623+
)
624+
def test_paddle_clip(
625+
*,
626+
input_and_ranges,
627+
min,
628+
max,
629+
frontend,
630+
fn_tree,
631+
test_flags,
632+
backend_fw,
633+
on_device,
634+
):
635+
input_dtype, x = input_and_ranges
636+
helpers.test_frontend_function(
637+
input_dtypes=input_dtype,
638+
backend_to_test=backend_fw,
639+
frontend=frontend,
640+
test_flags=test_flags,
641+
fn_tree=fn_tree,
642+
on_device=on_device,
643+
x=x[0],
644+
min=min,
645+
max=max,
646+
)
647+
648+
649+
# clip_
650+
@handle_frontend_test(
651+
fn_tree="paddle.clip_",
652+
input_and_ranges=_get_clip_inputs_(),
653+
min=st.integers(min_value=0, max_value=5),
654+
max=st.integers(min_value=5, max_value=10),
655+
)
656+
def test_paddle_clip_(
657+
*,
658+
input_and_ranges,
659+
min,
660+
max,
661+
frontend,
662+
fn_tree,
663+
test_flags,
664+
backend_fw,
665+
on_device,
666+
):
667+
input_dtype, x = input_and_ranges
668+
helpers.test_frontend_function(
669+
input_dtypes=input_dtype,
670+
backend_to_test=backend_fw,
671+
frontend=frontend,
672+
test_flags=test_flags,
673+
fn_tree=fn_tree,
674+
on_device=on_device,
675+
x=x[0],
676+
min=min,
677+
max=max,
678+
)
679+
680+
598681
# conj
599682
@handle_frontend_test(
600683
fn_tree="paddle.conj",

0 commit comments

Comments
 (0)