From 5394638dc5a432dea70ba402ab32b5984bb2d136 Mon Sep 17 00:00:00 2001 From: Aditya Raj <109805994+zeus2x7@users.noreply.github.com> Date: Sat, 26 Aug 2023 03:39:49 +0530 Subject: [PATCH 1/8] Update loss.py --added sigmoid_focal_loss and square_error_cost --refactored mse_loss --- .../frontends/paddle/nn/functional/loss.py | 59 +++++++++++++++++-- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/ivy/functional/frontends/paddle/nn/functional/loss.py b/ivy/functional/frontends/paddle/nn/functional/loss.py index ba0aae117744..d7dd948e0350 100644 --- a/ivy/functional/frontends/paddle/nn/functional/loss.py +++ b/ivy/functional/frontends/paddle/nn/functional/loss.py @@ -252,16 +252,13 @@ def margin_ranking_loss(input, other, label, margin=0.0, reduction="mean", name= return out -@with_supported_dtypes({"2.4.2 and below": ("float32", "float64")}, "paddle") +@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle") @inputs_to_ivy_arrays def mse_loss(input, label, reduction="mean", name=None): reduction = _get_reduction_func(reduction) ret = ivy.square(input - label) ret = reduction(ret) - if ret.shape == (): - ret = ret.expand_dims() - return paddle.to_tensor(ret) @@ -298,6 +295,52 @@ def nll_loss( return output +@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle") +@to_ivy_arrays_and_back +def sigmoid_focal_loss( + logit, + label, + normalizer=None, + alpha=0.25, + gamma=2.0, + reduction="sum", + name=None, +): + if reduction not in ["sum", "mean", "none"]: + raise ValueError( + "The value of 'reduction' in sigmoid_focal_loss should be 'sum', 'mean' or" + f" 'none', but received {reduction}, which is not allowed." + ) + + if normalizer is not None and normalizer.ndim > 1: + raise ValueError( + "Expected zero or one dimension of normalizer in sigmoid_focal_loss but" + f" got {normalizer.ndim}." + ) + + if not isinstance(logit, ivy.Array): + logit = ivy.array(logit) + + if not isinstance(label, ivy.Array): + label = ivy.array(label) + + pred = ivy.sigmoid(logit) + loss = -( + label * alpha * ivy.pow((1 - pred), gamma) * ivy.log(pred) + + (1 - label) * (1 - alpha) * ivy.pow(pred, gamma) * ivy.log(1 - pred) + ) + + if normalizer is not None: + loss /= normalizer + + if reduction == "sum": + return ivy.sum(loss) + elif reduction == "mean": + return ivy.mean(loss) + + return loss + + @with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle") @to_ivy_arrays_and_back def smooth_l1_loss( @@ -324,6 +367,14 @@ def smooth_l1_loss( return out.astype(label.dtype) +@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle") +@to_ivy_arrays_and_back +def square_error_cost(input, label): + diff = input - label + out = ivy.square(diff) + return out + + @with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle") @to_ivy_arrays_and_back def triplet_margin_loss( From 4638a7d75d3ca85d569150b4db0862f50f2a0f9e Mon Sep 17 00:00:00 2001 From: Aditya Raj <109805994+zeus2x7@users.noreply.github.com> Date: Sat, 26 Aug 2023 03:42:36 +0530 Subject: [PATCH 2/8] Update test_loss.py --- .../test_nn/test_functional/test_loss.py | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py index 39ac77530903..d216f4952d74 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py @@ -550,3 +550,119 @@ def test_paddle_dice_loss( label=labels, epsilon=epsilon, ) + + +@handle_frontend_test( + fn_tree="paddle.nn.functional.square_error_cost", + dtype_and_x=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("float"), + num_arrays=2, + shared_dtype=True, + min_num_dims=1, + min_dim_size=1, + ), +) +def test_paddle_square_error_cost( + dtype_and_x, + on_device, + fn_tree, + frontend, + test_flags, + backend_fw, +): + x_dtype, x = dtype_and_x + + helpers.test_frontend_function( + input_dtypes=[ + x_dtype[0], + x_dtype[1], + ], + backend_to_test=backend_fw, + frontend=frontend, + test_flags=test_flags, + fn_tree=fn_tree, + on_device=on_device, + input=x[0], + label=x[1], + ) + + +@handle_frontend_test( + fn_tree="paddle.nn.functional.sigmoid_focal_loss", + dtype_and_x=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("float"), + num_arrays=1, + shared_dtype=False, + min_num_dims=3, + min_dim_size=3, + max_num_dims=3, + max_dim_size=3, + ), + dtype_and_normalizer=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("float"), + num_arrays=1, + shared_dtype=True, + min_num_dims=1, + min_dim_size=1, + max_num_dims=1, + max_dim_size=1, + ), + labels=st.lists( + ( + st.lists( + ( + st.lists( + st.integers(min_value=0, max_value=1), min_size=3, max_size=3 + ) + ), + min_size=3, + max_size=3, + ) + ), + min_size=1, + max_size=1, + ), + alpha=st.floats( + min_value=0.0, + max_value=1.0, + ), + gamma=st.floats( + min_value=0.0, + max_value=5.0, + ), + reduction=st.sampled_from(["mean", "sum", "none"]), +) +def test_paddle_sigmoid_focal_loss( + dtype_and_x, + dtype_and_normalizer, + labels, + alpha, + gamma, + reduction, + on_device, + fn_tree, + frontend, + test_flags, + backend_fw, +): + x_dtype, x = dtype_and_x + normalizer_dtype, normalizer = dtype_and_normalizer + x[0] = x[0].reshape([3, 3, 3]) + normalizer = [norm.reshape(-1) for norm in normalizer] + + labels = ivy.array(labels, dtype=ivy.int64) + labels = labels.reshape([3, 3, 1]) + helpers.test_frontend_function( + input_dtypes=[ivy.int64] + [ivy.float64] + x_dtype + normalizer_dtype, + backend_to_test=backend_fw, + frontend=frontend, + test_flags=test_flags, + fn_tree=fn_tree, + on_device=on_device, + logit=x[0], + label=labels, + alpha=alpha, + gamma=gamma, + normalizer=normalizer[0], + reduction=reduction, + ) From 36c7390e499bafb9b18feb0da62f13e3213b285d Mon Sep 17 00:00:00 2001 From: Aditya Raj <109805994+zeus2x7@users.noreply.github.com> Date: Tue, 29 Aug 2023 18:07:49 +0530 Subject: [PATCH 3/8] Update test_loss.py --- .../test_nn/test_functional/test_loss.py | 214 +++++++++--------- 1 file changed, 107 insertions(+), 107 deletions(-) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py index 3a9180454563..888caa8a37a6 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py @@ -468,27 +468,57 @@ def test_paddle_nll_loss( ) -# smooth_l1_loss @handle_frontend_test( - fn_tree="paddle.nn.functional.smooth_l1_loss", + fn_tree="paddle.nn.functional.sigmoid_focal_loss", dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, + available_dtypes=helpers.get_dtypes("float"), + num_arrays=1, + shared_dtype=False, + min_num_dims=3, + min_dim_size=3, + max_num_dims=3, + max_dim_size=3, + ), + dtype_and_normalizer=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("float"), + num_arrays=1, shared_dtype=True, - min_num_dims=2, - max_num_dims=5, + min_num_dims=1, min_dim_size=1, - max_dim_size=10, + max_num_dims=1, + max_dim_size=1, ), - delta=st.floats( - min_value=0.1, + labels=st.lists( + ( + st.lists( + ( + st.lists( + st.integers(min_value=0, max_value=1), min_size=3, max_size=3 + ) + ), + min_size=3, + max_size=3, + ) + ), + min_size=1, + max_size=1, + ), + alpha=st.floats( + min_value=0.0, max_value=1.0, ), + gamma=st.floats( + min_value=0.0, + max_value=5.0, + ), reduction=st.sampled_from(["mean", "sum", "none"]), ) -def test_paddle_smooth_l1_loss( +def test_paddle_sigmoid_focal_loss( dtype_and_x, - delta, + dtype_and_normalizer, + labels, + alpha, + gamma, reduction, on_device, fn_tree, @@ -496,67 +526,69 @@ def test_paddle_smooth_l1_loss( test_flags, backend_fw, ): - input_dtype, x = dtype_and_x + x_dtype, x = dtype_and_x + normalizer_dtype, normalizer = dtype_and_normalizer + x[0] = x[0].reshape([3, 3, 3]) + normalizer = [norm.reshape(-1) for norm in normalizer] + + labels = ivy.array(labels, dtype=ivy.int64) + labels = labels.reshape([3, 3, 1]) helpers.test_frontend_function( - input_dtypes=input_dtype, - frontend=frontend, + input_dtypes=[ivy.int64] + [ivy.float64] + x_dtype + normalizer_dtype, backend_to_test=backend_fw, + frontend=frontend, test_flags=test_flags, fn_tree=fn_tree, on_device=on_device, - input=x[0], - label=x[1], + logit=x[0], + label=labels, + alpha=alpha, + gamma=gamma, + normalizer=normalizer[0], reduction=reduction, - delta=delta, ) +# smooth_l1_loss @handle_frontend_test( - fn_tree="paddle.nn.functional.triplet_margin_loss", - dtype_and_inputs=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=3, - allow_inf=False, + fn_tree="paddle.nn.functional.smooth_l1_loss", + dtype_and_x=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("valid"), + num_arrays=2, shared_dtype=True, - min_value=0.0, - max_value=1.0, - min_num_dims=1, - max_num_dims=2, + min_num_dims=2, + max_num_dims=5, min_dim_size=1, + max_dim_size=10, ), - margin=st.floats(min_value=1e-6, max_value=1e6), - p=st.integers(min_value=0, max_value=2), - swap=st.booleans(), - reduction=st.sampled_from(["none", "mean", "sum"]), - test_with_out=st.just(False), + delta=st.floats( + min_value=0.1, + max_value=1.0, + ), + reduction=st.sampled_from(["mean", "sum", "none"]), ) -def test_paddle_triplet_margin_loss( - dtype_and_inputs, - margin, - p, - swap, +def test_paddle_smooth_l1_loss( + dtype_and_x, + delta, reduction, - test_flags, + on_device, fn_tree, - backend_fw, frontend, - on_device, + test_flags, + backend_fw, ): - input_dtype, x = dtype_and_inputs + input_dtype, x = dtype_and_x helpers.test_frontend_function( - input_dtypes=[input_dtype[0], input_dtype[1], input_dtype[2]], - backend_to_test=backend_fw, + input_dtypes=input_dtype, frontend=frontend, + backend_to_test=backend_fw, test_flags=test_flags, fn_tree=fn_tree, on_device=on_device, input=x[0], - positive=x[1], - negative=x[2], - margin=margin, - p=p, - swap=swap, + label=x[1], reduction=reduction, + delta=delta, ) @@ -596,81 +628,49 @@ def test_paddle_square_error_cost( @handle_frontend_test( - fn_tree="paddle.nn.functional.sigmoid_focal_loss", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=1, - shared_dtype=False, - min_num_dims=3, - min_dim_size=3, - max_num_dims=3, - max_dim_size=3, - ), - dtype_and_normalizer=helpers.dtype_and_values( + fn_tree="paddle.nn.functional.triplet_margin_loss", + dtype_and_inputs=helpers.dtype_and_values( available_dtypes=helpers.get_dtypes("float"), - num_arrays=1, + num_arrays=3, + allow_inf=False, shared_dtype=True, - min_num_dims=1, - min_dim_size=1, - max_num_dims=1, - max_dim_size=1, - ), - labels=st.lists( - ( - st.lists( - ( - st.lists( - st.integers(min_value=0, max_value=1), min_size=3, max_size=3 - ) - ), - min_size=3, - max_size=3, - ) - ), - min_size=1, - max_size=1, - ), - alpha=st.floats( min_value=0.0, max_value=1.0, + min_num_dims=1, + max_num_dims=2, + min_dim_size=1, ), - gamma=st.floats( - min_value=0.0, - max_value=5.0, - ), - reduction=st.sampled_from(["mean", "sum", "none"]), + margin=st.floats(min_value=1e-6, max_value=1e6), + p=st.integers(min_value=0, max_value=2), + swap=st.booleans(), + reduction=st.sampled_from(["none", "mean", "sum"]), + test_with_out=st.just(False), ) -def test_paddle_sigmoid_focal_loss( - dtype_and_x, - dtype_and_normalizer, - labels, - alpha, - gamma, +def test_paddle_triplet_margin_loss( + dtype_and_inputs, + margin, + p, + swap, reduction, - on_device, - fn_tree, - frontend, test_flags, + fn_tree, backend_fw, + frontend, + on_device, ): - x_dtype, x = dtype_and_x - normalizer_dtype, normalizer = dtype_and_normalizer - x[0] = x[0].reshape([3, 3, 3]) - normalizer = [norm.reshape(-1) for norm in normalizer] - - labels = ivy.array(labels, dtype=ivy.int64) - labels = labels.reshape([3, 3, 1]) + input_dtype, x = dtype_and_inputs helpers.test_frontend_function( - input_dtypes=[ivy.int64] + [ivy.float64] + x_dtype + normalizer_dtype, + input_dtypes=[input_dtype[0], input_dtype[1], input_dtype[2]], backend_to_test=backend_fw, frontend=frontend, test_flags=test_flags, fn_tree=fn_tree, on_device=on_device, - logit=x[0], - label=labels, - alpha=alpha, - gamma=gamma, - normalizer=normalizer[0], + input=x[0], + positive=x[1], + negative=x[2], + margin=margin, + p=p, + swap=swap, reduction=reduction, ) From c0bfa79b3550cf8f59c84ec444907f79576c1d70 Mon Sep 17 00:00:00 2001 From: Aditya Raj <109805994+zeus2x7@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:37:35 +0530 Subject: [PATCH 4/8] Update loss.py --- ivy/functional/frontends/paddle/nn/functional/loss.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/ivy/functional/frontends/paddle/nn/functional/loss.py b/ivy/functional/frontends/paddle/nn/functional/loss.py index d7dd948e0350..5bd5bcfaeb3e 100644 --- a/ivy/functional/frontends/paddle/nn/functional/loss.py +++ b/ivy/functional/frontends/paddle/nn/functional/loss.py @@ -367,14 +367,6 @@ def smooth_l1_loss( return out.astype(label.dtype) -@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle") -@to_ivy_arrays_and_back -def square_error_cost(input, label): - diff = input - label - out = ivy.square(diff) - return out - - @with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle") @to_ivy_arrays_and_back def triplet_margin_loss( From 056f49b51734fe242f004289526e163fa00a7a8d Mon Sep 17 00:00:00 2001 From: Aditya Raj <109805994+zeus2x7@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:38:50 +0530 Subject: [PATCH 5/8] Update test_loss.py --removed square_error_cost implementation --- .../test_nn/test_functional/test_loss.py | 35 ------------------- 1 file changed, 35 deletions(-) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py index 888caa8a37a6..e597b28af6c8 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py @@ -592,41 +592,6 @@ def test_paddle_smooth_l1_loss( ) -@handle_frontend_test( - fn_tree="paddle.nn.functional.square_error_cost", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - shared_dtype=True, - min_num_dims=1, - min_dim_size=1, - ), -) -def test_paddle_square_error_cost( - dtype_and_x, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - x_dtype, x = dtype_and_x - - helpers.test_frontend_function( - input_dtypes=[ - x_dtype[0], - x_dtype[1], - ], - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=x[0], - label=x[1], - ) - - @handle_frontend_test( fn_tree="paddle.nn.functional.triplet_margin_loss", dtype_and_inputs=helpers.dtype_and_values( From 5c874b3d2d35b3bfc9cc306e8218a65077258860 Mon Sep 17 00:00:00 2001 From: Aditya Raj <109805994+zeus2x7@users.noreply.github.com> Date: Fri, 1 Sep 2023 04:41:33 +0530 Subject: [PATCH 6/8] Update test_loss.py --- .../test_nn/test_functional/test_loss.py | 38 +++++++------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py index 079702e717dd..0e5c2c39c408 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py @@ -474,10 +474,8 @@ def test_paddle_nll_loss( available_dtypes=helpers.get_dtypes("float"), num_arrays=1, shared_dtype=False, - min_num_dims=3, - min_dim_size=3, - max_num_dims=3, - max_dim_size=3, + min_num_dims=1, + min_dim_size=1, ), dtype_and_normalizer=helpers.dtype_and_values( available_dtypes=helpers.get_dtypes("float"), @@ -488,20 +486,14 @@ def test_paddle_nll_loss( max_num_dims=1, max_dim_size=1, ), - labels=st.lists( - ( - st.lists( - ( - st.lists( - st.integers(min_value=0, max_value=1), min_size=3, max_size=3 - ) - ), - min_size=3, - max_size=3, - ) - ), - min_size=1, - max_size=1, + dtype_and_labels=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("float"), + num_arrays=1, + shared_dtype=False, + min_num_dims=1, + min_dim_size=1, + min_value=0, + max_value=1, ), alpha=st.floats( min_value=0.0, @@ -516,7 +508,7 @@ def test_paddle_nll_loss( def test_paddle_sigmoid_focal_loss( dtype_and_x, dtype_and_normalizer, - labels, + dtype_and_labels, alpha, gamma, reduction, @@ -528,20 +520,18 @@ def test_paddle_sigmoid_focal_loss( ): x_dtype, x = dtype_and_x normalizer_dtype, normalizer = dtype_and_normalizer - x[0] = x[0].reshape([3, 3, 3]) + label_dtype, labels = dtype_and_labels normalizer = [norm.reshape(-1) for norm in normalizer] - labels = ivy.array(labels, dtype=ivy.int64) - labels = labels.reshape([3, 3, 1]) helpers.test_frontend_function( - input_dtypes=[ivy.int64] + [ivy.float64] + x_dtype + normalizer_dtype, + input_dtypes=[ivy.int64] + [ivy.float64] + x_dtype + normalizer_dtype + label_dtype, backend_to_test=backend_fw, frontend=frontend, test_flags=test_flags, fn_tree=fn_tree, on_device=on_device, logit=x[0], - label=labels, + label=labels[0], alpha=alpha, gamma=gamma, normalizer=normalizer[0], From 0b4252920c2a3a705559f9f8fad2f8af4206754d Mon Sep 17 00:00:00 2001 From: Aditya Raj <109805994+zeus2x7@users.noreply.github.com> Date: Fri, 1 Sep 2023 04:44:16 +0530 Subject: [PATCH 7/8] Update test_loss.py --- .../test_paddle/test_nn/test_functional/test_loss.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py index 0e5c2c39c408..dfc538b74d06 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py @@ -524,7 +524,11 @@ def test_paddle_sigmoid_focal_loss( normalizer = [norm.reshape(-1) for norm in normalizer] labels = ivy.array(labels, dtype=ivy.int64) helpers.test_frontend_function( - input_dtypes=[ivy.int64] + [ivy.float64] + x_dtype + normalizer_dtype + label_dtype, + input_dtypes=[ivy.int64] + + [ivy.float64] + + x_dtype + + normalizer_dtype + + label_dtype, backend_to_test=backend_fw, frontend=frontend, test_flags=test_flags, From 12eb09ab891ab9971ca28355fd0c8f6c1a887f62 Mon Sep 17 00:00:00 2001 From: Aditya Raj <109805994+zeus2x7@users.noreply.github.com> Date: Fri, 1 Sep 2023 04:47:48 +0530 Subject: [PATCH 8/8] Update test_loss.py --- .../test_paddle/test_nn/test_functional/test_loss.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py index dfc538b74d06..884449aa6318 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_loss.py @@ -524,10 +524,10 @@ def test_paddle_sigmoid_focal_loss( normalizer = [norm.reshape(-1) for norm in normalizer] labels = ivy.array(labels, dtype=ivy.int64) helpers.test_frontend_function( - input_dtypes=[ivy.int64] - + [ivy.float64] - + x_dtype - + normalizer_dtype + input_dtypes=[ivy.int64] + + [ivy.float64] + + x_dtype + + normalizer_dtype + label_dtype, backend_to_test=backend_fw, frontend=frontend,