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

feat(frontend): Added logsumexp function in paddlepaddle frontend #21725

Merged
merged 4 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 23 additions & 0 deletions ivy/functional/frontends/paddle/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,29 @@ def logit(x, eps=None, name=None):
return ivy.logit(x, eps=eps)


@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle")
@to_ivy_arrays_and_back
def logsumexp(x, axis=None, y=None):
x = ivy.asarray(x)
if y is not None:
y = ivy.asarray(y)
x = ivy.where(y != 0, x, -ivy.inf)
if axis is None:
amax = ivy.max(x)
expsub = ivy.exp(x - amax)
sumexp = ivy.sum(expsub)
out = ivy.log(sumexp) + amax
else:
amax = ivy.max(x, axis=axis, keepdims=True)
expsub = ivy.exp(x - amax)
sumexp = ivy.sum(expsub, axis=axis, keepdims=True)
out = ivy.log(sumexp) + amax
if y is not None:
sign = ivy.stop_gradient(ivy.sign(sumexp))
out = ivy.where(sign < 0, ivy.nan, out)
return out


@with_supported_dtypes(
{"2.5.2 and below": ("float32", "float64", "int32", "int64")}, "paddle"
)
Expand Down
33 changes: 33 additions & 0 deletions ivy_tests/test_ivy/test_frontends/test_paddle/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,39 @@ def test_paddle_logit(
)


# logsumexp
@handle_frontend_test(
fn_tree="paddle.tensor.math.logsumexp",
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("valid"),
max_num_dims=4,
num_arrays=2,
allow_inf=False,
shared_dtype=True,
),
)
def test_paddle_logsumexp(
*,
dtype_and_x,
on_device,
fn_tree,
backend_fw,
frontend,
test_flags,
):
input_dtypes, xs = dtype_and_x
helpers.test_frontend_function(
input_dtypes=input_dtypes,
backend_to_test=backend_fw,
frontend=frontend,
test_flags=test_flags,
fn_tree=fn_tree,
on_device=on_device,
x=xs[0],
axis=None,
)


# max
@handle_frontend_test(
fn_tree="paddle.max",
Expand Down
Loading