Skip to content
Open
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
23 changes: 5 additions & 18 deletions paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
43 changes: 41 additions & 2 deletions tests/test_nn_SmoothL1Loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand All @@ -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(
"""
Expand All @@ -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"])
37 changes: 37 additions & 0 deletions tests/test_nn_functional_smooth_l1_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Loading