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

added cosine_similarity and test to paddlepaddle frontend #16042

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
24 changes: 24 additions & 0 deletions ivy/functional/frontends/paddle/nn/functional/common.py
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
# local
import ivy
from ivy.functional.frontends.paddle.func_wrapper import to_ivy_arrays_and_back
from ivy.func_wrapper import with_unsupported_dtypes


@with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @diksha-shrivastava13 @RickSanchezStoic , in this code block, "torch" is mentioned. Shouldn't it be "paddle" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should, actually. I had been closely following PyTorch's implementation. I will open a pull request to refactor the same with
@with_supported_dtypes({"2.4.2 and below": ("float32", "float64")}, "paddle"). Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@to_ivy_arrays_and_back
def cosine_similarity(x1, x2, *, axis=1, eps=1e-08):
if len(x1.shape) == len(x2.shape) and len(x2.shape) >= 2:
numerator = ivy.sum(x1 * x2, axis=axis)
x1_squared_norm = ivy.sum(ivy.square(x1), axis=axis)
x2_squared_norm = ivy.sum(ivy.square(x2), axis=axis)
else:
numerator = ivy.sum(x1 * x2)
x1_squared_norm = ivy.sum(ivy.square(x1))
x2_squared_norm = ivy.sum(ivy.square(x2))

x1_norm = ivy.sqrt(x1_squared_norm)
x2_norm = ivy.sqrt(x2_squared_norm)
norm_mm = x1_norm * x2_norm
denominator = ivy.maximum(norm_mm, eps)

cosine = numerator / denominator
return cosine
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# global
from hypothesis import strategies as st

# local
import ivy_tests.test_ivy.helpers as helpers
from ivy_tests.test_ivy.helpers import handle_frontend_test


# Cosine Similarity
@handle_frontend_test(
fn_tree="paddle.nn.functional.common.cosine_similarity",
d_type_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float"),
num_arrays=2,
shared_dtype=True,
min_value=2,
max_value=5,
min_dim_size=2,
shape=(4, 4),
),
axis=st.integers(min_value=-1, max_value=1),
)
def test_paddle_cosine_similarity(
*,
d_type_and_x,
axis,
on_device,
fn_tree,
frontend,
test_flags,
):
dtype, x = d_type_and_x
helpers.test_frontend_function(
input_dtypes=dtype,
frontend=frontend,
test_flags=test_flags,
fn_tree=fn_tree,
on_device=on_device,
rtol=1e-01,
x1=x[0],
x2=x[1],
axis=axis,
)