From 763555137622e85926b3dff3cd3b015586e3e7c3 Mon Sep 17 00:00:00 2001 From: luq Date: Mon, 11 Dec 2023 14:46:37 +0000 Subject: [PATCH 1/3] adapt TestSquareErrorCost --- test/legacy_test/test_square_error_cost.py | 43 +++++++++++++--------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/test/legacy_test/test_square_error_cost.py b/test/legacy_test/test_square_error_cost.py index 3dd54557e051e5..0d71960d2dd770 100644 --- a/test/legacy_test/test_square_error_cost.py +++ b/test/legacy_test/test_square_error_cost.py @@ -20,34 +20,43 @@ from paddle import base from paddle.base import core from paddle.base.executor import Executor +from paddle.pir_utils import test_with_pir_api class TestSquareErrorCost(unittest.TestCase): + @test_with_pir_api def test_square_error_cost(self): - input_val = np.random.uniform(0.1, 0.5, (2, 3)).astype("float32") - label_val = np.random.uniform(0.1, 0.5, (2, 3)).astype("float32") + paddle.enable_static() + shape = [2, 3] + input_val = np.random.uniform(0.1, 0.5, shape).astype("float32") + label_val = np.random.uniform(0.1, 0.5, shape).astype("float32") sub = input_val - label_val np_result = sub * sub - input_var = paddle.tensor.create_tensor(dtype="float32", name="input") - label_var = paddle.tensor.create_tensor(dtype="float32", name="label") - output = paddle.nn.functional.square_error_cost( - input=input_var, label=label_var - ) - for use_cuda in ( [False, True] if core.is_compiled_with_cuda() else [False] ): - place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() - exe = Executor(place) - (result,) = exe.run( - base.default_main_program(), - feed={"input": input_val, "label": label_val}, - fetch_list=[output], - ) - - np.testing.assert_allclose(np_result, result, rtol=1e-05) + with paddle.static.program_guard(paddle.static.Program()): + input_var = paddle.static.data( + name="input", shape=shape, dtype="float32" + ) + label_var = paddle.static.data( + name="label", shape=shape, dtype="float32" + ) + output = paddle.nn.functional.square_error_cost( + input=input_var, label=label_var + ) + + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = Executor(place) + (result,) = exe.run( + paddle.static.default_main_program(), + feed={"input": input_val, "label": label_val}, + fetch_list=[output], + ) + + np.testing.assert_allclose(np_result, result, rtol=1e-05) class TestSquareErrorInvalidInput(unittest.TestCase): From 99e94efd69badcf9a3d18bb5ee22d624b89af1ec Mon Sep 17 00:00:00 2001 From: luq Date: Fri, 15 Dec 2023 06:47:28 +0000 Subject: [PATCH 2/3] update multi_label_soft_margin_loss --- test/legacy_test/test_multi_label_soft_margin_loss.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/legacy_test/test_multi_label_soft_margin_loss.py b/test/legacy_test/test_multi_label_soft_margin_loss.py index c2e68191ffc36f..c9b455bd4ac405 100644 --- a/test/legacy_test/test_multi_label_soft_margin_loss.py +++ b/test/legacy_test/test_multi_label_soft_margin_loss.py @@ -17,6 +17,7 @@ import numpy as np import paddle +from paddle.pir_utils import test_with_pir_api def call_MultiLabelSoftMarginLoss_layer( @@ -139,6 +140,7 @@ def LogSigmoid(x): class TestMultiLabelMarginLoss(unittest.TestCase): + @test_with_pir_api def test_MultiLabelSoftMarginLoss(self): input = np.random.uniform(0.1, 0.8, size=(5, 5)).astype(np.float64) label = np.random.randint(0, 2, size=(5, 5)).astype(np.float64) @@ -209,6 +211,7 @@ def test_MultiLabelSoftMarginLoss_error(self): ) paddle.enable_static() + @test_with_pir_api def test_MultiLabelSoftMarginLoss_weights(self): input = np.random.uniform(0.1, 0.8, size=(5, 5)).astype(np.float64) label = np.random.randint(0, 2, size=(5, 5)).astype(np.float64) From 9f90f2df6f1aa38493c3917bc1b2c217b11a51da Mon Sep 17 00:00:00 2001 From: luq Date: Fri, 15 Dec 2023 06:50:53 +0000 Subject: [PATCH 3/3] update multi_margin_loss --- test/legacy_test/test_multimarginloss.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/legacy_test/test_multimarginloss.py b/test/legacy_test/test_multimarginloss.py index da0b91029dcd60..5e499f5560c2ce 100644 --- a/test/legacy_test/test_multimarginloss.py +++ b/test/legacy_test/test_multimarginloss.py @@ -17,6 +17,7 @@ import numpy as np import paddle +from paddle.pir_utils import test_with_pir_api def call_MultiMarginLoss_layer( @@ -238,6 +239,7 @@ def calc_multi_margin_loss( class TestMultiMarginLoss(unittest.TestCase): + @test_with_pir_api def test_MultiMarginLoss(self): batch_size = 5 num_classes = 2 @@ -330,6 +332,7 @@ def test_MultiMarginLoss_dimension(self): ) paddle.enable_static() + @test_with_pir_api def test_MultiMarginLoss_p(self): p = 2 batch_size = 5 @@ -383,6 +386,7 @@ def test_MultiMarginLoss_p(self): np.testing.assert_allclose(static_functional, dy_functional) np.testing.assert_allclose(dy_functional, expected) + @test_with_pir_api def test_MultiMarginLoss_weight(self): batch_size = 5 num_classes = 2 @@ -436,6 +440,7 @@ def test_MultiMarginLoss_weight(self): np.testing.assert_allclose(static_functional, dy_functional) np.testing.assert_allclose(dy_functional, expected) + @test_with_pir_api def test_MultiMarginLoss_static_data_shape(self): batch_size = 5 num_classes = 2