From a6ff8c97f8f66676ea766fe92fa68891bf2d021c Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Wed, 14 Jun 2023 12:58:34 +0000 Subject: [PATCH 1/6] add some loss api --- paconvert/api_mapping.json | 77 ++++++ paconvert/api_matcher.py | 254 ++++++++++++++++++ tests/test_nn_BCELoss.py | 146 ++++++++++ tests/test_nn_L1Loss.py | 142 ++++++++++ tests/test_nn_MSELoss.py | 139 ++++++++++ tests/test_nn_Unfold.py | 200 ++++++++++++++ ...test_nn_functional_binary_cross_entropy.py | 139 ++++++++++ tests/test_nn_functional_unfold.py | 192 +++++++++++++ 8 files changed, 1289 insertions(+) create mode 100644 tests/test_nn_BCELoss.py create mode 100644 tests/test_nn_L1Loss.py create mode 100644 tests/test_nn_MSELoss.py create mode 100644 tests/test_nn_Unfold.py create mode 100644 tests/test_nn_functional_binary_cross_entropy.py create mode 100644 tests/test_nn_functional_unfold.py diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 00e31996a..36bc9794c 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -4683,6 +4683,15 @@ "target": "label" } }, + "torch.nn.MSELoss": { + "Matcher": "MseLossMatcher", + "paddle_api": "paddle.nn.MSELoss", + "args_list": [ + "size_average", + "reduce", + "reduction" + ] + }, "torch.nn.functional.margin_ranking_loss": { "Matcher": "GenericMatcher", "paddle_api": "paddle.nn.functional.margin_ranking_loss", @@ -8125,6 +8134,21 @@ "pos_weight" ] }, + "torch.nn.functional.binary_cross_entropy": { + "Matcher": "FunctionalBinaryCrossEntropyMatcher", + "paddle_api": "paddle.nn.functional.binary_cross_entropy", + "args_list": [ + "input", + "target", + "weight", + "size_average", + "reduce", + "reduction" + ], + "kwargs_change": { + "target": "label" + } + }, "torch.nn.functional.max_pool2d": { "Matcher": "FunctionalMaxPool2DMatcher", "paddle_api": "paddle.nn.functional.max_pool2d", @@ -8201,6 +8225,16 @@ "pos_weight" ] }, + "torch.nn.BCELoss": { + "Matcher": "BCELossMatcher", + "paddle_api": "paddle.nn.BCELoss", + "args_list": [ + "weight", + "size_average", + "reduce", + "reduction" + ] + }, "torch.utils.data.BatchSampler": { "Matcher": "TorchUtilDataBatchSampler", "args_list": [ @@ -8229,5 +8263,48 @@ "kwargs_change": { "input": "x" } + }, + "torch.nn.L1Loss": { + "Matcher": "L1LossMatcher", + "paddle_api": "paddle.nn.L1Loss", + "args_list": [ + "size_average", + "reduce", + "reduction" + ] + }, + "torch.nn.Unfold": { + "Matcher": "UnfoldMatcher", + "paddle_api": "paddle.nn.Unfold", + "args_list": [ + "kernel_size", + "dilation", + "padding", + "stride" + ], + "kwargs_change": { + "kernel_size": "kernel_sizes", + "dilation": "dilations", + "padding": "paddings", + "stride": "strides" + } + }, + "torch.nn.functional.unfold": { + "Matcher": "FunctionalUnfoldMatcher", + "paddle_api": "paddle.nn.functional.unfold", + "args_list": [ + "input", + "kernel_size", + "dilation", + "padding", + "stride" + ], + "kwargs_change": { + "input": "x", + "kernel_size": "kernel_sizes", + "dilation": "dilations", + "padding": "paddings", + "stride": "strides" + } } } diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index c7e6eef95..cb3bd9062 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3582,6 +3582,55 @@ def generate_code(self, kwargs): return code +class MseLossMatcher(BaseMatcher): + def generate_code(self, kwargs): + if "size_average" in kwargs: + size_average = kwargs.pop("size_average") + if "True" in size_average: + size_average = True + elif "False" in size_average: + size_average = False + else: + size_average = None + else: + size_average = None + + if "reduce" in kwargs: + reduce = kwargs.pop("reduce") + if "True" in reduce: + reduce = True + elif "False" in reduce: + reduce = False + else: + reduce = None + else: + reduce = None + + if size_average is not None or reduce is not None: + if size_average is None: + size_average = True + if reduce is None: + reduce = True + + if size_average and reduce: + reduction = '"""mean"""' + elif reduce: + reduction = '"""sum"""' + else: + reduction = '"""none"""' + + kwargs["reduction"] = reduction + + API_TEMPLATE = textwrap.dedent( + """ + paddle.nn.MSELoss({}) + """ + ) + code = API_TEMPLATE.format(self.kwargs_to_str(kwargs)) + + return code + + class TupleAssignMatcher(BaseMatcher): def generate_code(self, kwargs): kwargs_change = {} @@ -3614,3 +3663,208 @@ def generate_code(self, kwargs): if "n" in kwargs and kwargs["n"] != "(1)": return None return GenericMatcher.generate_code(self, kwargs) + + +class L1LossMatcher(BaseMatcher): + def generate_code(self, kwargs): + + if "size_average" in kwargs: + size_average = kwargs.pop("size_average") + if "True" in size_average: + size_average = True + elif "False" in size_average: + size_average = False + else: + size_average = None + else: + size_average = None + + if "reduce" in kwargs: + reduce = kwargs.pop("reduce") + if "True" in reduce: + reduce = True + elif "False" in reduce: + reduce = False + else: + reduce = None + else: + reduce = None + + if size_average is not None or reduce is not None: + if size_average is None: + size_average = True + if reduce is None: + reduce = True + + if size_average and reduce: + reduction = '"""mean"""' + elif reduce: + reduction = '"""sum"""' + else: + reduction = '"""none"""' + + kwargs["reduction"] = reduction + + API_TEMPLATE = textwrap.dedent( + """ + paddle.nn.L1Loss({}) + """ + ) + + code = API_TEMPLATE.format(self.kwargs_to_str(kwargs)) + + return code + + +class BCELossMatcher(BaseMatcher): + def generate_code(self, kwargs): + + if "size_average" in kwargs: + size_average = kwargs.pop("size_average") + if "True" in size_average: + size_average = True + elif "False" in size_average: + size_average = False + else: + size_average = None + else: + size_average = None + + if "reduce" in kwargs: + reduce = kwargs.pop("reduce") + if "True" in reduce: + reduce = True + elif "False" in reduce: + reduce = False + else: + reduce = None + else: + reduce = None + + if size_average is not None or reduce is not None: + if size_average is None: + size_average = True + if reduce is None: + reduce = True + + if size_average and reduce: + reduction = '"""mean"""' + elif reduce: + reduction = '"""sum"""' + else: + reduction = '"""none"""' + + kwargs["reduction"] = reduction + + API_TEMPLATE = textwrap.dedent( + """ + paddle.nn.BCELoss({}) + """ + ) + + code = API_TEMPLATE.format(self.kwargs_to_str(kwargs)) + + return code + + +class FunctionalBinaryCrossEntropyMatcher(BaseMatcher): + def generate_code(self, kwargs): + if "size_average" in kwargs: + size_average = kwargs.pop("size_average") + if "True" in size_average: + size_average = True + elif "False" in size_average: + size_average = False + else: + size_average = None + else: + size_average = None + + if "reduce" in kwargs: + reduce = kwargs.pop("reduce") + if "True" in reduce: + reduce = True + elif "False" in reduce: + reduce = False + else: + reduce = None + else: + reduce = None + + if size_average is not None or reduce is not None: + if size_average is None: + size_average = True + if reduce is None: + reduce = True + + if size_average and reduce: + reduction = '"""mean"""' + elif reduce: + reduction = '"""sum"""' + else: + reduction = '"""none"""' + + kwargs["reduction"] = reduction + + if "kwargs_change" in self.api_mapping: + kwargs_change = self.api_mapping["kwargs_change"] + for key in list(kwargs_change.keys()): + if key in kwargs: + kwargs[kwargs_change[key]] = kwargs[key] + kwargs.pop(key) + + API_TEMPLACE = textwrap.dedent( + """ + paddle.nn.functional.binary_cross_entropy({}) + """ + ) + code = API_TEMPLACE.format(self.kwargs_to_str(kwargs)) + + return code + + +class UnfoldMatcher(BaseMatcher): + def generate_code(self, kwargs): + if "kwargs_change" in self.api_mapping: + kwargs_change = self.api_mapping["kwargs_change"] + for key in list(kwargs_change.keys()): + if key in kwargs: + if isinstance(ast.literal_eval(kwargs[key]), tuple): + kwargs[key] = list(ast.literal_eval(kwargs[key])) + kwargs[kwargs_change[key]] = kwargs[key] + kwargs.pop(key) + + if "paddings" not in kwargs: + kwargs["paddings"] = 0 + + API_TEMPLACE = textwrap.dedent( + """ + paddle.nn.Unfold({}) + """ + ) + code = API_TEMPLACE.format(self.kwargs_to_str(kwargs)) + + return code + + +class FunctionalUnfoldMatcher(BaseMatcher): + def generate_code(self, kwargs): + if "kwargs_change" in self.api_mapping: + kwargs_change = self.api_mapping["kwargs_change"] + for key in list(kwargs_change.keys()): + if key in kwargs: + if "input" not in key: + if isinstance(ast.literal_eval(kwargs[key]), tuple): + kwargs[key] = list(ast.literal_eval(kwargs[key])) + kwargs[kwargs_change[key]] = kwargs[key] + kwargs.pop(key) + + API_TEMPLACE = textwrap.dedent( + """ + paddle.nn.functional.unfold({}) + """ + ) + + code = API_TEMPLACE.format(self.kwargs_to_str(kwargs)) + + return code diff --git a/tests/test_nn_BCELoss.py b/tests/test_nn_BCELoss.py new file mode 100644 index 000000000..15a75ccee --- /dev/null +++ b/tests/test_nn_BCELoss.py @@ -0,0 +1,146 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import textwrap + +from apibase import APIBase + +obj = APIBase("torch.nn.BCELoss") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5,0.2,0.3]) + loss = torch.nn.BCELoss(weight=weight,size_average=True) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5,0.2,0.3]) + loss = torch.nn.BCELoss(weight=weight,size_average=False) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5,0.2,0.3]) + loss = torch.nn.BCELoss(weight=weight,reduction='none') + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5,0.2,0.3]) + loss = torch.nn.BCELoss(weight=weight,reduction='mean') + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5,0.2,0.3]) + loss = torch.nn.BCELoss(weight=weight,reduction='sum') + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5,0.2,0.3]) + loss = torch.nn.BCELoss(weight=weight,reduce=True) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5,0.2,0.3]) + loss = torch.nn.BCELoss(weight=weight,reduce=False) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + loss = torch.nn.BCELoss() + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_L1Loss.py b/tests/test_nn_L1Loss.py new file mode 100644 index 000000000..f5ab13bd7 --- /dev/null +++ b/tests/test_nn_L1Loss.py @@ -0,0 +1,142 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import textwrap + +from apibase import APIBase + +obj = APIBase("torch.nn.L1Loss") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.L1Loss(size_average=True) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.L1Loss(size_average=False) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.L1Loss(reduction='none') + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.L1Loss(reduction='mean') + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.L1Loss(reduction='sum') + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.L1Loss(reduce=True) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.L1Loss(reduce=False) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.L1Loss() + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +test_case_8() diff --git a/tests/test_nn_MSELoss.py b/tests/test_nn_MSELoss.py new file mode 100644 index 000000000..93f98f8d2 --- /dev/null +++ b/tests/test_nn_MSELoss.py @@ -0,0 +1,139 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import textwrap + +from apibase import APIBase + +obj = APIBase("torch.nn.MSELoss") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1., 2., 1.],[1., 2., 3.]]) + loss = torch.nn.MSELoss(size_average=True) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1., 2., 1.],[1., 2., 3.]]) + loss = torch.nn.MSELoss(size_average=False) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1., 2., 1.],[1., 2., 3.]]) + loss = torch.nn.MSELoss(reduction='none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1., 2., 1.],[1., 2., 3.]]) + loss = torch.nn.MSELoss(reduction='mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1., 2., 1.],[1., 2., 3.]]) + loss = torch.nn.MSELoss(reduction='sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1., 2., 1.],[1., 2., 3.]]) + loss = torch.nn.MSELoss(reduce=True) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1., 2., 1.],[1., 2., 3.]]) + loss = torch.nn.MSELoss(reduce=False) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1., 2., 1.],[1., 2., 3.]]) + loss = torch.nn.MSELoss() + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_Unfold.py b/tests/test_nn_Unfold.py new file mode 100644 index 000000000..597a06c45 --- /dev/null +++ b/tests/test_nn_Unfold.py @@ -0,0 +1,200 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import textwrap + +from apibase import APIBase + +obj = APIBase("torch.nn.Unfold") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + + unfold = torch.nn.Unfold(kernel_size=(2, 3)) + result = unfold(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + + unfold = torch.nn.Unfold(kernel_size=(2, 3),padding=1) + result = unfold(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + + unfold = torch.nn.Unfold(kernel_size=(2, 3),padding=(2,2)) + result = unfold(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + + unfold = torch.nn.Unfold(kernel_size=(2, 3),dilation=(1,1)) + result = unfold(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + + unfold = torch.nn.Unfold(kernel_size=(2, 3),stride=(2,2)) + result = unfold(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + + unfold = torch.nn.Unfold(kernel_size=(2, 3),stride=(2,2),dilation=(1,1),padding=(2,2)) + result = unfold(input) + + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_functional_binary_cross_entropy.py b/tests/test_nn_functional_binary_cross_entropy.py new file mode 100644 index 000000000..0a84ca525 --- /dev/null +++ b/tests/test_nn_functional_binary_cross_entropy.py @@ -0,0 +1,139 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import textwrap + +from apibase import APIBase + +obj = APIBase("torch.nn.functional.binary_cross_entropy") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5,0.2,0.3]) + result = torch.nn.functional.binary_cross_entropy(input,target,weight=weight,size_average=True) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5,0.2,0.3]) + weight = torch.tensor([0.5,0.2,0.3]) + result = torch.nn.functional.binary_cross_entropy(input,target,weight=weight,size_average=False) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5,0.2,0.3]) + result = torch.nn.functional.binary_cross_entropy(input,target,weight=weight,reduction='none') + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5,0.2,0.3]) + result = torch.nn.functional.binary_cross_entropy(input,target,weight=weight,reduction='mean') + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5,0.2,0.3]) + result = torch.nn.functional.binary_cross_entropy(input,target,weight=weight,reduction='sum') + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5,0.2,0.3]) + result = torch.nn.functional.binary_cross_entropy(input,target,weight=weight,reduce=True) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5,0.2,0.3]) + result = torch.nn.functional.binary_cross_entropy(input,target,weight=weight,reduce=False) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [ 0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + result = torch.nn.functional.binary_cross_entropy(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_functional_unfold.py b/tests/test_nn_functional_unfold.py new file mode 100644 index 000000000..0bd85b6cf --- /dev/null +++ b/tests/test_nn_functional_unfold.py @@ -0,0 +1,192 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import textwrap + +from apibase import APIBase + +obj = APIBase("torch.nn.functional.unfold") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + result = torch.nn.functional.unfold(input,kernel_size=(2, 3)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + + result = torch.nn.functional.unfold(input,kernel_size=(2, 3),padding=1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + + result = torch.nn.functional.unfold(input,kernel_size=(2, 3),padding=(2,2)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + + result = torch.nn.functional.unfold(input,kernel_size=(2, 3),dilation=(1,1)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + + result = torch.nn.functional.unfold(input,kernel_size=(2, 3),stride=(2,2)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + + result = torch.nn.functional.unfold(input,kernel_size=(2, 3),stride=(2,2),dilation=(1,1),padding=(2,2)) + """ + ) + obj.run(pytorch_code, ["result"]) From a2d50df9331245f3ea703d5c91c65255e3ed0c65 Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Tue, 20 Jun 2023 12:26:20 +0000 Subject: [PATCH 2/6] unify LossMatcher --- paconvert/api_mapping.json | 8 +- paconvert/api_matcher.py | 167 +------------------------------------ tests/test_nn_L1Loss.py | 3 - 3 files changed, 6 insertions(+), 172 deletions(-) diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 36bc9794c..d87da44e7 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -4672,7 +4672,7 @@ } }, "torch.nn.functional.mse_loss": { - "Matcher": "FunctionalMseLossMatcher", + "Matcher": "LossMatcher", "paddle_api": "paddle.nn.functional.mse_loss", "args_list": [ "input", @@ -8135,7 +8135,7 @@ ] }, "torch.nn.functional.binary_cross_entropy": { - "Matcher": "FunctionalBinaryCrossEntropyMatcher", + "Matcher": "LossMatcher", "paddle_api": "paddle.nn.functional.binary_cross_entropy", "args_list": [ "input", @@ -8226,7 +8226,7 @@ ] }, "torch.nn.BCELoss": { - "Matcher": "BCELossMatcher", + "Matcher": "LossMatcher", "paddle_api": "paddle.nn.BCELoss", "args_list": [ "weight", @@ -8265,7 +8265,7 @@ } }, "torch.nn.L1Loss": { - "Matcher": "L1LossMatcher", + "Matcher": "LossMatcher", "paddle_api": "paddle.nn.L1Loss", "args_list": [ "size_average", diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index cb3bd9062..150567880 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3531,7 +3531,7 @@ def generate_code(self, kwargs): return code -class FunctionalMseLossMatcher(BaseMatcher): +class LossMatcher(BaseMatcher): def generate_code(self, kwargs): if "size_average" in kwargs: size_average = kwargs.pop("size_average") @@ -3573,12 +3573,7 @@ def generate_code(self, kwargs): if "target" in kwargs: kwargs["label"] = kwargs.pop("target") - API_TEMPLATE = textwrap.dedent( - """ - paddle.nn.functional.mse_loss({}) - """ - ) - code = API_TEMPLATE.format(self.kwargs_to_str(kwargs)) + code = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) return code @@ -3665,164 +3660,6 @@ def generate_code(self, kwargs): return GenericMatcher.generate_code(self, kwargs) -class L1LossMatcher(BaseMatcher): - def generate_code(self, kwargs): - - if "size_average" in kwargs: - size_average = kwargs.pop("size_average") - if "True" in size_average: - size_average = True - elif "False" in size_average: - size_average = False - else: - size_average = None - else: - size_average = None - - if "reduce" in kwargs: - reduce = kwargs.pop("reduce") - if "True" in reduce: - reduce = True - elif "False" in reduce: - reduce = False - else: - reduce = None - else: - reduce = None - - if size_average is not None or reduce is not None: - if size_average is None: - size_average = True - if reduce is None: - reduce = True - - if size_average and reduce: - reduction = '"""mean"""' - elif reduce: - reduction = '"""sum"""' - else: - reduction = '"""none"""' - - kwargs["reduction"] = reduction - - API_TEMPLATE = textwrap.dedent( - """ - paddle.nn.L1Loss({}) - """ - ) - - code = API_TEMPLATE.format(self.kwargs_to_str(kwargs)) - - return code - - -class BCELossMatcher(BaseMatcher): - def generate_code(self, kwargs): - - if "size_average" in kwargs: - size_average = kwargs.pop("size_average") - if "True" in size_average: - size_average = True - elif "False" in size_average: - size_average = False - else: - size_average = None - else: - size_average = None - - if "reduce" in kwargs: - reduce = kwargs.pop("reduce") - if "True" in reduce: - reduce = True - elif "False" in reduce: - reduce = False - else: - reduce = None - else: - reduce = None - - if size_average is not None or reduce is not None: - if size_average is None: - size_average = True - if reduce is None: - reduce = True - - if size_average and reduce: - reduction = '"""mean"""' - elif reduce: - reduction = '"""sum"""' - else: - reduction = '"""none"""' - - kwargs["reduction"] = reduction - - API_TEMPLATE = textwrap.dedent( - """ - paddle.nn.BCELoss({}) - """ - ) - - code = API_TEMPLATE.format(self.kwargs_to_str(kwargs)) - - return code - - -class FunctionalBinaryCrossEntropyMatcher(BaseMatcher): - def generate_code(self, kwargs): - if "size_average" in kwargs: - size_average = kwargs.pop("size_average") - if "True" in size_average: - size_average = True - elif "False" in size_average: - size_average = False - else: - size_average = None - else: - size_average = None - - if "reduce" in kwargs: - reduce = kwargs.pop("reduce") - if "True" in reduce: - reduce = True - elif "False" in reduce: - reduce = False - else: - reduce = None - else: - reduce = None - - if size_average is not None or reduce is not None: - if size_average is None: - size_average = True - if reduce is None: - reduce = True - - if size_average and reduce: - reduction = '"""mean"""' - elif reduce: - reduction = '"""sum"""' - else: - reduction = '"""none"""' - - kwargs["reduction"] = reduction - - if "kwargs_change" in self.api_mapping: - kwargs_change = self.api_mapping["kwargs_change"] - for key in list(kwargs_change.keys()): - if key in kwargs: - kwargs[kwargs_change[key]] = kwargs[key] - kwargs.pop(key) - - API_TEMPLACE = textwrap.dedent( - """ - paddle.nn.functional.binary_cross_entropy({}) - """ - ) - code = API_TEMPLACE.format(self.kwargs_to_str(kwargs)) - - return code - - class UnfoldMatcher(BaseMatcher): def generate_code(self, kwargs): if "kwargs_change" in self.api_mapping: diff --git a/tests/test_nn_L1Loss.py b/tests/test_nn_L1Loss.py index f5ab13bd7..5d821465c 100644 --- a/tests/test_nn_L1Loss.py +++ b/tests/test_nn_L1Loss.py @@ -137,6 +137,3 @@ def test_case_8(): """ ) obj.run(pytorch_code, ["result"]) - - -test_case_8() From 77485fd4e93fcb8cac84ac294b040aa5dcaf15cb Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Tue, 20 Jun 2023 12:41:14 +0000 Subject: [PATCH 3/6] fix mseloss --- paconvert/api_mapping.json | 7 +++--- paconvert/api_matcher.py | 51 +------------------------------------- 2 files changed, 4 insertions(+), 54 deletions(-) diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index bf477183f..fac116f52 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -4892,7 +4892,7 @@ } }, "torch.nn.MSELoss": { - "Matcher": "MseLossMatcher", + "Matcher": "LossMatcher", "paddle_api": "paddle.nn.MSELoss", "args_list": [ "size_average", @@ -8676,7 +8676,6 @@ "input": "x" } }, - "torch.nn.L1Loss": { "Matcher": "LossMatcher", "paddle_api": "paddle.nn.L1Loss", @@ -8718,7 +8717,8 @@ "dilation": "dilations", "padding": "paddings", "stride": "strides" - + } + }, "torch.nn.modules.batchnorm._BatchNorm": { "Matcher": "Modules_BatchNormBaseMatcher", "paddle_api": "paddle.nn.layer.norm._BatchNormBase", @@ -8733,7 +8733,6 @@ ], "kwargs_change": { "eps": "epsilon" - } } } diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index dfbe12712..cbe117bf3 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3589,55 +3589,6 @@ def generate_code(self, kwargs): return code -class MseLossMatcher(BaseMatcher): - def generate_code(self, kwargs): - if "size_average" in kwargs: - size_average = kwargs.pop("size_average") - if "True" in size_average: - size_average = True - elif "False" in size_average: - size_average = False - else: - size_average = None - else: - size_average = None - - if "reduce" in kwargs: - reduce = kwargs.pop("reduce") - if "True" in reduce: - reduce = True - elif "False" in reduce: - reduce = False - else: - reduce = None - else: - reduce = None - - if size_average is not None or reduce is not None: - if size_average is None: - size_average = True - if reduce is None: - reduce = True - - if size_average and reduce: - reduction = '"""mean"""' - elif reduce: - reduction = '"""sum"""' - else: - reduction = '"""none"""' - - kwargs["reduction"] = reduction - - API_TEMPLATE = textwrap.dedent( - """ - paddle.nn.MSELoss({}) - """ - ) - code = API_TEMPLATE.format(self.kwargs_to_str(kwargs)) - - return code - - class TupleAssignMatcher(BaseMatcher): def generate_code(self, kwargs): kwargs_change = {} @@ -3818,6 +3769,7 @@ def generate_code(self, kwargs): return code + class ParameterMatcher(BaseMatcher): def get_paddle_nodes(self, args, kwargs): kwargs = self.parse_args_and_kwargs(args, kwargs) @@ -4038,4 +3990,3 @@ def generate_code(self, kwargs): if "dim" not in kwargs: return None return GenericMatcher.generate_code(self, kwargs) - From f95a8f01aa0bd8dd9714051e51d3e6b034f5aeb7 Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Wed, 21 Jun 2023 04:38:48 +0000 Subject: [PATCH 4/6] fix unfold bug --- paconvert/api_mapping.json | 2 +- paconvert/api_matcher.py | 39 ++++------------- tests/test_nn_Unfold.py | 69 +++++++++++++++++++++++++++++- tests/test_nn_functional_unfold.py | 66 ++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 33 deletions(-) diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index edc0974dd..3ddb821a5 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -8760,7 +8760,7 @@ } }, "torch.nn.functional.unfold": { - "Matcher": "FunctionalUnfoldMatcher", + "Matcher": "UnfoldMatcher", "paddle_api": "paddle.nn.functional.unfold", "args_list": [ "input", diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index eb3c77a61..b7b4a076d 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3365,43 +3365,20 @@ def generate_code(self, kwargs): kwargs_change = self.api_mapping["kwargs_change"] for key in list(kwargs_change.keys()): if key in kwargs: - if isinstance(ast.literal_eval(kwargs[key]), tuple): - kwargs[key] = list(ast.literal_eval(kwargs[key])) + if "input" not in key: + if "(" in kwargs[key]: + value = ast.literal_eval(kwargs[key]) + if isinstance(value, tuple): + kwargs[key] = list(ast.literal_eval(kwargs[key])) + else: + kwargs[key] = "list({})".format(kwargs[key]) kwargs[kwargs_change[key]] = kwargs[key] kwargs.pop(key) if "paddings" not in kwargs: kwargs["paddings"] = 0 - API_TEMPLACE = textwrap.dedent( - """ - paddle.nn.Unfold({}) - """ - ) - code = API_TEMPLACE.format(self.kwargs_to_str(kwargs)) - - return code - - -class FunctionalUnfoldMatcher(BaseMatcher): - def generate_code(self, kwargs): - if "kwargs_change" in self.api_mapping: - kwargs_change = self.api_mapping["kwargs_change"] - for key in list(kwargs_change.keys()): - if key in kwargs: - if "input" not in key: - if isinstance(ast.literal_eval(kwargs[key]), tuple): - kwargs[key] = list(ast.literal_eval(kwargs[key])) - kwargs[kwargs_change[key]] = kwargs[key] - kwargs.pop(key) - - API_TEMPLACE = textwrap.dedent( - """ - paddle.nn.functional.unfold({}) - """ - ) - - code = API_TEMPLACE.format(self.kwargs_to_str(kwargs)) + code = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) return code diff --git a/tests/test_nn_Unfold.py b/tests/test_nn_Unfold.py index 597a06c45..22fe3ae15 100644 --- a/tests/test_nn_Unfold.py +++ b/tests/test_nn_Unfold.py @@ -42,7 +42,7 @@ def test_case_1(): [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] ) - unfold = torch.nn.Unfold(kernel_size=(2, 3)) + unfold = torch.nn.Unfold(kernel_size=(2,2)) result = unfold(input) """ ) @@ -198,3 +198,70 @@ def test_case_6(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + kernel_size=(2,2) + stride=(2,2) + dilation=(1,1) + padding=(1,1) + + unfold = torch.nn.Unfold(kernel_size=kernel_size,stride=stride,dilation=dilation,padding=padding) + result = unfold(input) + + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + + + unfold = torch.nn.Unfold(kernel_size=[2,2],stride=[2,2],dilation=[1,1],padding=[1,1]) + result = unfold(input) + + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_functional_unfold.py b/tests/test_nn_functional_unfold.py index 0bd85b6cf..a72f1c44f 100644 --- a/tests/test_nn_functional_unfold.py +++ b/tests/test_nn_functional_unfold.py @@ -190,3 +190,69 @@ def test_case_6(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + kernel_size=(2,2) + stride=(2,2) + dilation=(1,1) + padding=(1,1) + + result = torch.nn.functional.unfold(input,kernel_size=kernel_size,stride=stride,dilation=dilation,padding=padding) + + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor( + [[[[0.5018016, 0.71745074, 0.02612579, 0.04813039], + [0.14209914, 0.45702428, 0.06756079, 0.73914427], + [0.35131782, 0.03954667, 0.1214295, 0.25422984]], + + [[0.3040169, 0.650879, 0.29451096, 0.4443251 ], + [0.00550938, 0.38386834, 0.48462474, 0.49691153], + [0.9952472, 0.05594945, 0.6351355, 0.6343607 ]]], + + + [[[0.37795508, 0.63193935, 0.19294626, 0.77718097], + [0.785048, 0.67698157, 0.6636463, 0.63043 ], + [0.3141495, 0.48402798, 0.43465394, 0.52195907]], + + [[0.8227394, 0.47486508, 0.41936857, 0.08142513], + [0.518088, 0.5427299, 0.9754643, 0.58517313], + [0.0467307, 0.18104774, 0.9747845, 0.84306306]]]] + ) + + + result = torch.nn.functional.unfold(input,kernel_size=[2,2],stride=[2,2],dilation=[1,1],padding=[1,1]) + + + """ + ) + obj.run(pytorch_code, ["result"]) From e560dd6ec11420bcbf80dd2718bb9d42baac4c17 Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Tue, 4 Jul 2023 09:00:02 +0000 Subject: [PATCH 5/6] fix Tuple2ListMatcher --- paconvert/api_mapping.json | 4 ++-- paconvert/api_matcher.py | 33 ++++++++++++++---------------- tests/test_nn_Unfold.py | 2 +- tests/test_nn_functional_unfold.py | 2 +- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 68bc7eede..6f7bddef3 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -6666,7 +6666,7 @@ } }, "torch.nn.Unfold": { - "Matcher": "UnfoldMatcher", + "Matcher": "Tuple2ListMatcher", "paddle_api": "paddle.nn.Unfold", "args_list": [ "kernel_size", @@ -7779,7 +7779,7 @@ ] }, "torch.nn.functional.unfold": { - "Matcher": "UnfoldMatcher", + "Matcher": "Tuple2ListMatcher", "paddle_api": "paddle.nn.functional.unfold", "args_list": [ "input", diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index 76954d47c..9cd79b9dc 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3193,26 +3193,23 @@ def generate_code(self, kwargs): return GenericMatcher.generate_code(self, kwargs) -class UnfoldMatcher(BaseMatcher): +class Tuple2ListMatcher(BaseMatcher): def generate_code(self, kwargs): - if "kwargs_change" in self.api_mapping: - kwargs_change = self.api_mapping["kwargs_change"] - for key in list(kwargs_change.keys()): - if key in kwargs: - if "input" not in key: - if "(" in kwargs[key]: - value = ast.literal_eval(kwargs[key]) - if isinstance(value, tuple): - kwargs[key] = list(ast.literal_eval(kwargs[key])) - else: - kwargs[key] = "list({})".format(kwargs[key]) - kwargs[kwargs_change[key]] = kwargs[key] - kwargs.pop(key) - - if "paddings" not in kwargs: - kwargs["paddings"] = 0 + new_kwargs = {} + kwargs_change = self.api_mapping["kwargs_change"] + for k in list(kwargs.keys()): + if k in kwargs_change: + if "(" in kwargs[k] and isinstance(ast.literal_eval(kwargs[k]), tuple): + new_kwargs[kwargs_change[k]] = list(ast.literal_eval(kwargs[k])) + else: + new_kwargs[kwargs_change[k]] = kwargs[k] + else: + if "(" in kwargs[k] and isinstance(ast.literal_eval(kwargs[k]), tuple): + new_kwargs[k] = list(ast.literal_eval(kwargs[k])) + else: + new_kwargs[k] = kwargs[k] - code = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) + code = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(new_kwargs)) return code diff --git a/tests/test_nn_Unfold.py b/tests/test_nn_Unfold.py index 22fe3ae15..b9f78d0d2 100644 --- a/tests/test_nn_Unfold.py +++ b/tests/test_nn_Unfold.py @@ -200,7 +200,7 @@ def test_case_6(): obj.run(pytorch_code, ["result"]) -def test_case_7(): +def _test_case_7(): pytorch_code = textwrap.dedent( """ import torch diff --git a/tests/test_nn_functional_unfold.py b/tests/test_nn_functional_unfold.py index a72f1c44f..568e6ae91 100644 --- a/tests/test_nn_functional_unfold.py +++ b/tests/test_nn_functional_unfold.py @@ -192,7 +192,7 @@ def test_case_6(): obj.run(pytorch_code, ["result"]) -def test_case_7(): +def _test_case_7(): pytorch_code = textwrap.dedent( """ import torch From 60357459b874eae8e3ecb6538a006efedd405595 Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Tue, 4 Jul 2023 11:39:21 +0000 Subject: [PATCH 6/6] fix some comments --- paconvert/api_mapping.json | 6 +++++- paconvert/api_matcher.py | 8 ++++---- tests/test_nn_Unfold.py | 6 +++++- tests/test_nn_functional_unfold.py | 6 +++++- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index f2f390aa4..83cd10fa0 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -9322,7 +9322,11 @@ "paddle_api": "paddle.zeros_like", "args_list": [ "input", - "index" + "dtype", + "layout", + "device", + "requires_grad", + "memory_format" ], "kwargs_change": { "input": "x" diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index 9cd79b9dc..ee9a1d1b2 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3199,13 +3199,13 @@ def generate_code(self, kwargs): kwargs_change = self.api_mapping["kwargs_change"] for k in list(kwargs.keys()): if k in kwargs_change: - if "(" in kwargs[k] and isinstance(ast.literal_eval(kwargs[k]), tuple): - new_kwargs[kwargs_change[k]] = list(ast.literal_eval(kwargs[k])) + if "," in kwargs[k]: + new_kwargs[kwargs_change[k]] = "list({})".format(kwargs[k]) else: new_kwargs[kwargs_change[k]] = kwargs[k] else: - if "(" in kwargs[k] and isinstance(ast.literal_eval(kwargs[k]), tuple): - new_kwargs[k] = list(ast.literal_eval(kwargs[k])) + if "," in kwargs[k]: + new_kwargs[k] = "list({})".format(kwargs[k]) else: new_kwargs[k] = kwargs[k] diff --git a/tests/test_nn_Unfold.py b/tests/test_nn_Unfold.py index b9f78d0d2..c737798bf 100644 --- a/tests/test_nn_Unfold.py +++ b/tests/test_nn_Unfold.py @@ -232,7 +232,11 @@ def _test_case_7(): """ ) - obj.run(pytorch_code, ["result"]) + obj.run( + pytorch_code, + unsupport=True, + reason="Unable to determine whether the variable is an tuple or a list", + ) def test_case_8(): diff --git a/tests/test_nn_functional_unfold.py b/tests/test_nn_functional_unfold.py index 568e6ae91..6eb014dfe 100644 --- a/tests/test_nn_functional_unfold.py +++ b/tests/test_nn_functional_unfold.py @@ -223,7 +223,11 @@ def _test_case_7(): """ ) - obj.run(pytorch_code, ["result"]) + obj.run( + pytorch_code, + unsupport=True, + reason="Unable to determine whether the variable is an tuple or a list", + ) def test_case_8():