From d93f8e4fbabe3bcfb9a7d46be2f84e424819910a Mon Sep 17 00:00:00 2001 From: KevinUli Date: Fri, 16 Jun 2023 18:18:56 -0700 Subject: [PATCH 1/3] Implemented paddle binary_cross_entropy_with_logits frontend function and tests --- .../frontends/paddle/nn/functional/loss.py | 44 +++++++++++++ .../test_functional/test_paddle_loss.py | 65 +++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/ivy/functional/frontends/paddle/nn/functional/loss.py b/ivy/functional/frontends/paddle/nn/functional/loss.py index 5804c29a261af..82589fc19752d 100644 --- a/ivy/functional/frontends/paddle/nn/functional/loss.py +++ b/ivy/functional/frontends/paddle/nn/functional/loss.py @@ -1 +1,45 @@ # local +import ivy +from ivy.func_wrapper import with_supported_dtypes +import ivy.functional.frontends.paddle as paddle +from ivy.functional.frontends.paddle.func_wrapper import ( + inputs_to_ivy_arrays, +) + + +# helpers +def _get_reduction_func(reduction): + if reduction == "none": + ret = lambda x: x + elif reduction == "mean": + ret = ivy.mean + elif reduction == "sum": + ret = ivy.sum + else: + raise ivy.utils.exceptions.IvyException( + "{} is not a valid value for reduction".format(reduction) + ) + return ret + + +@with_supported_dtypes( + {"2.4.2 and below": ("float32",)}, + "paddle", +) +@inputs_to_ivy_arrays +def binary_cross_entropy_with_logits( + logit, + label, + weight=None, + reduction="mean", + pos_weight=None, + name=None, +): + ret = ivy.binary_cross_entropy( + label, logit, from_logits=True, reduction="none", pos_weight=pos_weight + ) + reduction = _get_reduction_func(reduction) + if weight is not None: + ret = ivy.multiply(weight, ret) + ret = reduction(ret).astype(label.dtype) + return paddle.to_tensor(ret.reshape([-1])) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_paddle_loss.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_paddle_loss.py index 0a9c8754f4fed..c1573e880d51f 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_paddle_loss.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_paddle_loss.py @@ -1,3 +1,68 @@ # 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 + + +@handle_frontend_test( + fn_tree="paddle.nn.functional.binary_cross_entropy_with_logits", + dtype_and_x=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("valid"), + num_arrays=2, + min_value=0, + max_value=1, + exclude_min=True, + exclude_max=True, + shared_dtype=True, + min_num_dims=1, + max_num_dims=1, + min_dim_size=2, + ), + dtype_and_weight=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("valid"), + min_value=1.0013580322265625e-05, + max_value=1.0, + min_num_dims=1, + max_num_dims=1, + ), + reduction=st.sampled_from(["mean", "none", "sum"]), + dtype_and_pos_weight=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("valid"), + min_value=0, + max_value=10, + exclude_min=True, + exclude_max=True, + ), +) +def test_paddle_binary_cross_entropy_with_logits( + dtype_and_x, + dtype_and_weight, + reduction, + dtype_and_pos_weight, + on_device, + fn_tree, + frontend, + test_flags, +): + x_dtype, x = dtype_and_x + weight_dtype, weight = dtype_and_weight + pos_weight_dtype, pos_weight = dtype_and_pos_weight + helpers.test_frontend_function( + input_dtypes=[ + x_dtype[0], + weight_dtype[0], + pos_weight_dtype[0], + ], + frontend=frontend, + test_flags=test_flags, + fn_tree=fn_tree, + on_device=on_device, + test_values=False, + logit=x[0], + label=x[0], + weight=weight[0], + reduction=reduction, + pos_weight=pos_weight[0], + ) From e2360ea9ab4a14425fea8b43dd6e1188dd970d0f Mon Sep 17 00:00:00 2001 From: KevinUli Date: Sun, 18 Jun 2023 18:40:51 -0700 Subject: [PATCH 2/3] Removed pos_weight kwarg from tests --- .../test_nn/test_functional/test_paddle_loss.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_paddle_loss.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_paddle_loss.py index c1573e880d51f..7bbb590e54784 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_paddle_loss.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_paddle_loss.py @@ -28,41 +28,31 @@ max_num_dims=1, ), reduction=st.sampled_from(["mean", "none", "sum"]), - dtype_and_pos_weight=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - min_value=0, - max_value=10, - exclude_min=True, - exclude_max=True, - ), ) def test_paddle_binary_cross_entropy_with_logits( dtype_and_x, dtype_and_weight, reduction, - dtype_and_pos_weight, on_device, fn_tree, frontend, test_flags, ): + # Testing without pos_weight kwarg due to it working differently for paddle as + # opposed to other frameworks. x_dtype, x = dtype_and_x weight_dtype, weight = dtype_and_weight - pos_weight_dtype, pos_weight = dtype_and_pos_weight helpers.test_frontend_function( input_dtypes=[ x_dtype[0], weight_dtype[0], - pos_weight_dtype[0], ], frontend=frontend, test_flags=test_flags, fn_tree=fn_tree, on_device=on_device, - test_values=False, logit=x[0], label=x[0], weight=weight[0], reduction=reduction, - pos_weight=pos_weight[0], ) From fa81176846ff27ed7226167b5a08f4a965acce56 Mon Sep 17 00:00:00 2001 From: KevinUli Date: Sun, 18 Jun 2023 21:08:56 -0700 Subject: [PATCH 3/3] Fixed test comment --- .../test_paddle/test_nn/test_functional/test_paddle_loss.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_paddle_loss.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_paddle_loss.py index 7bbb590e54784..37f56392fbf67 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_paddle_loss.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_paddle_loss.py @@ -38,8 +38,8 @@ def test_paddle_binary_cross_entropy_with_logits( frontend, test_flags, ): - # Testing without pos_weight kwarg due to it working differently for paddle as - # opposed to other frameworks. + # TODO: paddle's implementation of pos_weight is wrong + # https://github.com/PaddlePaddle/Paddle/blob/f0422a28d75f9345fa3b801c01cd0284b3b44be3/python/paddle/nn/functional/loss.py#L831 x_dtype, x = dtype_and_x weight_dtype, weight = dtype_and_weight helpers.test_frontend_function(