Skip to content
47 changes: 40 additions & 7 deletions ivy/functional/frontends/paddle/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,32 @@ def ceil(x, name=None):
return ivy.ceil(x)


@with_supported_dtypes(
{"2.6.0 and below": ("float32", "float64", "int32", "int64")}, "paddle"
)
@to_ivy_arrays_and_back
def clip(x, min=None, max=None, name=None):
ivy.utils.assertions.check_all_or_any_fn(
min,
max,
fn=ivy.exists,
type="any",
limit=[1, 2],
message="at most one of min or max can be None",
)
if min is None:
min = ivy.min(x)
if max is None:
max = ivy.max(x)
res = ivy.clip(x, min, max)
if res.dtype != x.dtype:
res = ivy.astype(res, x.dtype)
return res





@with_supported_dtypes(
{
"2.6.0 and below": (
Expand Down Expand Up @@ -208,7 +234,15 @@ def cumsum(x, axis=None, dtype=None, name=None):
@with_unsupported_dtypes({"2.6.0 and below": ("float16", "bfloat16")}, "paddle")
@to_ivy_arrays_and_back
def deg2rad(x, name=None):
return ivy.deg2rad(x)
# Handle overflow cases to match Paddle's behavior
result = ivy.deg2rad(x)
# Clamp infinite values to large finite values to match Paddle
if ivy.any(ivy.isinf(result)):
dtype_info = ivy.finfo(result.dtype)
max_val = dtype_info.max
result = ivy.where(ivy.isinf(result) & (result > 0), max_val, result)
result = ivy.where(ivy.isinf(result) & (result < 0), -max_val, result)
return result


@with_supported_dtypes(
Expand Down Expand Up @@ -529,7 +563,7 @@ def outer(x, y, name=None):


@with_supported_dtypes(
{"2.6.0 and below": ("float16", "float32", "float64", "int32", "int64")}, "paddle"
{"2.6.0 and below": ("float32", "float64", "int32", "int64", "complex64", "complex128", "bfloat16")}, "paddle"
)
@to_ivy_arrays_and_back
def pow(x, y, name=None):
Expand All @@ -556,7 +590,9 @@ def reciprocal(x, name=None):
return ivy.reciprocal(x)


@with_unsupported_dtypes({"2.6.0 and below": ("float16", "bfloat16")}, "paddle")
@with_supported_dtypes(
{"2.6.0 and below": ("float32", "float64", "int32", "int64")}, "paddle"
)
@to_ivy_arrays_and_back
def remainder(x, y, name=None):
return ivy.remainder(x, y)
Expand Down Expand Up @@ -639,10 +675,7 @@ def subtract(x, y, name=None):
return ivy.subtract(x, y)


@with_unsupported_dtypes({"2.6.0 and below": ("float16", "bfloat16")}, "paddle")
@to_ivy_arrays_and_back
def subtract_(x, y, name=None):
return ivy.inplace_update(x, subtract(x, y))



@with_supported_dtypes(
Expand Down
73 changes: 40 additions & 33 deletions ivy_tests/test_ivy/test_frontends/test_paddle/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def _test_paddle_take_helper(draw):
return dtypes, xs, indices, mode





# --- Main --- #
# ------------ #

Expand Down Expand Up @@ -595,6 +598,42 @@ def test_paddle_ceil(
)


# clip
@handle_frontend_test(
fn_tree="paddle.clip",
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("valid"),
min_value=0,
max_value=50,
),
min=st.integers(min_value=0, max_value=5),
max=st.integers(min_value=5, max_value=10),
)
def test_paddle_clip(
*,
dtype_and_x,
min,
max,
frontend,
fn_tree,
test_flags,
backend_fw,
on_device,
):
input_dtype, x = dtype_and_x
helpers.test_frontend_function(
input_dtypes=input_dtype,
backend_to_test=backend_fw,
frontend=frontend,
test_flags=test_flags,
fn_tree=fn_tree,
on_device=on_device,
x=x[0],
min=min,
max=max,
)


# conj
@handle_frontend_test(
fn_tree="paddle.conj",
Expand Down Expand Up @@ -2570,39 +2609,7 @@ def test_paddle_subtract(
)


# subtract_
@handle_frontend_test(
fn_tree="paddle.subtract_",
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float"),
num_arrays=2,
allow_inf=False,
large_abs_safety_factor=2,
small_abs_safety_factor=2,
safety_factor_scale="log",
shared_dtype=True,
),
)
def test_paddle_subtract_(
*,
dtype_and_x,
on_device,
fn_tree,
frontend,
test_flags,
backend_fw,
):
input_dtype, x = dtype_and_x
helpers.test_frontend_function(
input_dtypes=input_dtype,
backend_to_test=backend_fw,
frontend=frontend,
fn_tree=fn_tree,
test_flags=test_flags,
on_device=on_device,
x=x[0],
y=x[1],
)



@handle_frontend_test(
Expand Down