diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 2b4e144f8..f6975d2a1 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -7829,21 +7829,15 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.SmoothL1Loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.SmoothL1Loss", + "Matcher": "ChangeAPIMatcher", + "paddle_api": "paddle.compat.nn.SmoothL1Loss", "min_input_args": 0, "args_list": [ "size_average", "reduce", "reduction", "beta" - ], - "kwargs_change": { - "beta": "delta" - }, - "paddle_default_kwargs": { - "is_huber": "False" - } + ] }, "torch.nn.SoftMarginLoss": { "Matcher": "SizeAverageMatcher", @@ -9014,8 +9008,8 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.smooth_l1_loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.smooth_l1_loss", + "Matcher": "ChangeAPIMatcher", + "paddle_api": "paddle.compat.nn.functional.smooth_l1_loss", "args_list": [ "input", "target", @@ -9024,13 +9018,6 @@ "reduction", "beta" ], - "kwargs_change": { - "target": "label", - "beta": "delta" - }, - "paddle_default_kwargs": { - "is_huber": "False" - }, "min_input_args": 2 }, "torch.nn.functional.soft_margin_loss": { diff --git a/tests/test_nn_SmoothL1Loss.py b/tests/test_nn_SmoothL1Loss.py index e69b2e714..899e0541f 100644 --- a/tests/test_nn_SmoothL1Loss.py +++ b/tests/test_nn_SmoothL1Loss.py @@ -86,7 +86,6 @@ def test_case_5(): obj.run(pytorch_code, ["result"]) -# paddle result has diff with pytorch result def test_case_6(): pytorch_code = textwrap.dedent( """ @@ -100,7 +99,6 @@ def test_case_6(): obj.run(pytorch_code, ["result"]) -# paddle result has diff with pytorch result def test_case_7(): pytorch_code = textwrap.dedent( """ @@ -113,3 +111,44 @@ def test_case_7(): """ ) obj.run(pytorch_code, ["result"]) + + +# beta=0 degrades to the L1 loss, aligned with PyTorch via paddle.compat +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.SmoothL1Loss(beta=0.0) + input = torch.tensor([[1., 2., 3.], [4., 5., 6.]]) + label = torch.tensor([[1.5, 0.5, 5.], [4., 2., 9.]]) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.SmoothL1Loss(beta=0.0, reduction='none') + input = torch.tensor([[1., 2., 3.], [4., 5., 6.]]) + label = torch.tensor([[1.5, 0.5, 5.], [4., 2., 9.]]) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# kwargs out of order +def test_case_10(): + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.SmoothL1Loss(reduction='sum', beta=0.5) + input = torch.ones([3, 3]).to(dtype=torch.float32) + label = torch.full([3, 3], 2).to(dtype=torch.float32) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_functional_smooth_l1_loss.py b/tests/test_nn_functional_smooth_l1_loss.py index 7f6e84511..796813574 100644 --- a/tests/test_nn_functional_smooth_l1_loss.py +++ b/tests/test_nn_functional_smooth_l1_loss.py @@ -156,3 +156,40 @@ def test_case_11(): """ ) obj.run(pytorch_code, ["result"]) + + +# beta=0 degrades to the L1 loss, aligned with PyTorch via paddle.compat +def test_case_12(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1., 2., 3.], [4., 5., 6.]]) + label = torch.tensor([[1.5, 0.5, 5.], [4., 2., 9.]]) + result = torch.nn.functional.smooth_l1_loss(input, label, beta=0.0) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_13(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1., 2., 3.], [4., 5., 6.]]) + label = torch.tensor([[1.5, 0.5, 5.], [4., 2., 9.]]) + result = torch.nn.functional.smooth_l1_loss(input, label, beta=0.0, reduction='sum') + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_14(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1., 2., 3.], [4., 5., 6.]]) + label = torch.tensor([[1.5, 0.5, 5.], [4., 2., 9.]]) + result = torch.nn.functional.smooth_l1_loss(input, label, beta=2.0, reduction='none') + """ + ) + obj.run(pytorch_code, ["result"])