From 05eaa3852a9c23194d6b5c406a11a7d66edba015 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Fri, 23 Jun 2023 22:23:16 +0800 Subject: [PATCH 01/15] torch.nn.functional.threshold --- paconvert/api_mapping.json | 10 ++++ paconvert/api_matcher.py | 25 ++++++++++ tests/test_nn_functional_threshold.py | 71 +++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 tests/test_nn_functional_threshold.py diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 99e9858ea..555125acb 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -8732,5 +8732,15 @@ "kwargs_change": { "eps": "epsilon" } + }, + "torch.nn.functional.threshold": { + "Matcher": "FunctionalThresholdedReluMatcher", + "paddle_api": "paddle.nn.functional.thresholded_relu", + "args_list": [ + "input", + "threshold", + "value", + "inplace" + ] } } diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index 0e5cc28f5..9f62d287d 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3645,3 +3645,28 @@ class SizeAverageMatcher(BaseMatcher): def generate_code(self, kwargs): process_reduce_and_size_average(kwargs) return GenericMatcher.generate_code(self, kwargs) + + +class FunctionalThresholdedReluMatcher(BaseMatcher): + def generate_code(self, kwargs): + if kwargs["inplace"] is True: + API_TEMPLATE = textwrap.dedent( + """ + {} = {} + {}[{}<{}]={} + {} + """ + ) + else: + API_TEMPLATE = textwrap.dedent( + """ + {} = {}.clone() + {}[{}<{}]={} + {} + """ + ) + out = get_unique_name("out") + code = API_TEMPLATE.format( + out, kwargs["input"], out, out, kwargs["threshold"], kwargs["value"], out + ) + return code diff --git a/tests/test_nn_functional_threshold.py b/tests/test_nn_functional_threshold.py new file mode 100644 index 000000000..6c9644b62 --- /dev/null +++ b/tests/test_nn_functional_threshold.py @@ -0,0 +1,71 @@ +# 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.threshold") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn.functional as F + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], + [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + result = F.threshold(x, 0, 0, inplace=False) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn.functional as F + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], + [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + result = F.threshold(x, 1, 0, inplace=False) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn.functional as F + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], + [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + result = F.threshold(x, 0, 1, inplace=False) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn.functional as F + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], + [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + result = F.threshold(x, 0, 0, inplace=True) + """ + ) + obj.run(pytorch_code, ["result"]) From c0abac1f3eb5afda2e7d80fcc493ae1233f5a1d2 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Fri, 23 Jun 2023 22:46:33 +0800 Subject: [PATCH 02/15] torch.nn.functional.threshold --- paconvert/api_mapping.json | 2 +- paconvert/api_matcher.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 555125acb..5544c00b0 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -8734,7 +8734,7 @@ } }, "torch.nn.functional.threshold": { - "Matcher": "FunctionalThresholdedReluMatcher", + "Matcher": "FunctionalThresholdMatcher", "paddle_api": "paddle.nn.functional.thresholded_relu", "args_list": [ "input", diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index 9f62d287d..d61e5d1de 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3647,7 +3647,7 @@ def generate_code(self, kwargs): return GenericMatcher.generate_code(self, kwargs) -class FunctionalThresholdedReluMatcher(BaseMatcher): +class FunctionalThresholdMatcher(BaseMatcher): def generate_code(self, kwargs): if kwargs["inplace"] is True: API_TEMPLATE = textwrap.dedent( From ba79698aaaa094cfcc9e1bbd28b1b176d6770b72 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Mon, 26 Jun 2023 22:33:32 +0800 Subject: [PATCH 03/15] FunctionalThresholdMatcher --- paconvert/api_matcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index d61e5d1de..04f7e83f7 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3649,7 +3649,7 @@ def generate_code(self, kwargs): class FunctionalThresholdMatcher(BaseMatcher): def generate_code(self, kwargs): - if kwargs["inplace"] is True: + if "inplace" in kwargs.keys() and kwargs["inplace"] is True: API_TEMPLATE = textwrap.dedent( """ {} = {} From 4ca34413a866022b507ec505d64bca4c55ff9a26 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Mon, 26 Jun 2023 22:36:28 +0800 Subject: [PATCH 04/15] FunctionalThresholdMatcher --- paconvert/api_matcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index be51a7b02..f817ae94c 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3671,7 +3671,7 @@ def generate_code(self, kwargs): ) return code - + class RandomSplitMatcher(BaseMatcher): def generate_code(self, kwargs): API_TEMPLATE = textwrap.dedent( From 2798c44dff2e44f454c6b2bc203e0893bef13ae1 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Wed, 28 Jun 2023 06:08:53 +0800 Subject: [PATCH 05/15] threshold --- paconvert/api_mapping.json | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 2d6bb0b9b..dd27d7328 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -390,6 +390,16 @@ "input": "x" } }, + "torch.nn.functional.threshold": { + "Matcher": "FunctionalThresholdMatcher", + "paddle_api": "paddle.nn.functional.thresholded_relu", + "args_list": [ + "input", + "threshold", + "value", + "inplace" + ] + }, "torch.is_nonzero": { "Matcher": "IsNonzeroMatcher", "args_list": [ @@ -8788,15 +8798,5 @@ "kwargs_change": { "eps": "epsilon" } - }, - "torch.nn.functional.threshold": { - "Matcher": "FunctionalThresholdMatcher", - "paddle_api": "paddle.nn.functional.thresholded_relu", - "args_list": [ - "input", - "threshold", - "value", - "inplace" - ] } } From f23b708ee4e40574d24c645f87c5c8669378df5c Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Wed, 28 Jun 2023 06:10:13 +0800 Subject: [PATCH 06/15] threshold --- paconvert/api_mapping.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 1f86f16a0..7b71e60d0 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -8938,7 +8938,7 @@ "kwargs_change": { "eps": "epsilon" } - } + }, "torch.autograd.grad": { "Matcher": "GenericMatcher", "paddle_api": "paddle.grad", From 9cfc83451d9545a8efce30c4c5689e9305d418d6 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Sat, 8 Jul 2023 05:43:06 +0800 Subject: [PATCH 07/15] add json --- paconvert/api_mapping.json | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 3f169ba4c..6655b2cb9 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -7905,6 +7905,19 @@ "input": "x" } }, + "torch.nn.functional.threshold": { + "Matcher": "GenericMatcher", + "paddle_api": "paddle.nn.functional.thresholded_relu", + "args_list": [ + "input", + "threshold", + "value", + "inplace" + ], + "unsupport_args": { + "input": "inplace" + } + }, "torch.nn.functional.triplet_margin_with_distance_loss": { "Matcher": "GenericMatcher", "paddle_api": "paddle.nn.functional.triplet_margin_with_distance_loss", From 89eb63db4a2a28e67977f62135e1a55f38ef71cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Wed, 12 Jul 2023 05:34:20 +0800 Subject: [PATCH 08/15] Update api_mapping.json --- paconvert/api_mapping.json | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 6655b2cb9..8218724c1 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -7914,9 +7914,13 @@ "value", "inplace" ], - "unsupport_args": { - "input": "inplace" - } + "kwargs_change": { + "input": "x" + }, + "unsupport_args": [ + "value", + "inplace" + ] }, "torch.nn.functional.triplet_margin_with_distance_loss": { "Matcher": "GenericMatcher", From 3d296565efcc0b5d24291baed635736967f3918d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Wed, 19 Jul 2023 07:26:29 +0800 Subject: [PATCH 09/15] Update api_mapping.json --- paconvert/api_mapping.json | 1 + 1 file changed, 1 insertion(+) diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 8218724c1..49131e3f9 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -8,6 +8,7 @@ "args_list": [ "device" ] + }, "torch.IntTensor": { "Matcher": "TensorMatcher", From 8de8ddfadd611237bc6a76e32403cc1e9211025d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Wed, 19 Jul 2023 07:26:40 +0800 Subject: [PATCH 10/15] Update api_mapping.json --- paconvert/api_mapping.json | 1 - 1 file changed, 1 deletion(-) diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 49131e3f9..8218724c1 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -8,7 +8,6 @@ "args_list": [ "device" ] - }, "torch.IntTensor": { "Matcher": "TensorMatcher", From 09bcd0c50f60b736e502a05bb6cd3c002c858f37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Sat, 19 Aug 2023 22:16:44 +0800 Subject: [PATCH 11/15] Apply suggestions from code review --- paconvert/api_mapping.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 8218724c1..5e84a3b1a 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -7915,7 +7915,8 @@ "inplace" ], "kwargs_change": { - "input": "x" + "input": "x", + "inplace": "" }, "unsupport_args": [ "value", From b15e1dc17a38aa36f81ec1d050cfadb570aeba14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Sat, 19 Aug 2023 22:17:47 +0800 Subject: [PATCH 12/15] Apply suggestions from code review --- paconvert/api_mapping.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 5e84a3b1a..c4e997293 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -7919,8 +7919,7 @@ "inplace": "" }, "unsupport_args": [ - "value", - "inplace" + "value" ] }, "torch.nn.functional.triplet_margin_with_distance_loss": { From f0e124fd816ca55b0ff5cedfca3baa2bf625117a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Sat, 19 Aug 2023 22:19:50 +0800 Subject: [PATCH 13/15] Apply suggestions from code review --- tests/test_nn_functional_threshold.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_nn_functional_threshold.py b/tests/test_nn_functional_threshold.py index 6c9644b62..9e393cee6 100644 --- a/tests/test_nn_functional_threshold.py +++ b/tests/test_nn_functional_threshold.py @@ -26,7 +26,7 @@ def test_case_1(): import torch.nn.functional as F x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) - result = F.threshold(x, 0, 0, inplace=False) + result = F.threshold(x, 0, 0) """ ) obj.run(pytorch_code, ["result"]) @@ -39,7 +39,7 @@ def test_case_2(): import torch.nn.functional as F x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) - result = F.threshold(x, 1, 0, inplace=False) + result = F.threshold(x, 1, 0) """ ) obj.run(pytorch_code, ["result"]) @@ -52,7 +52,7 @@ def test_case_3(): import torch.nn.functional as F x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) - result = F.threshold(x, 0, 1, inplace=False) + result = F.threshold(x, 0, 1) """ ) obj.run(pytorch_code, ["result"]) @@ -68,4 +68,4 @@ def test_case_4(): result = F.threshold(x, 0, 0, inplace=True) """ ) - obj.run(pytorch_code, ["result"]) + obj.run(pytorch_code, ["result"], unsupport=True) From a6d0d602ed84c212940712e8c0c023f3fb9e76af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Mon, 28 Aug 2023 20:49:07 +0800 Subject: [PATCH 14/15] Apply suggestions from code review --- tests/test_nn_functional_threshold.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_nn_functional_threshold.py b/tests/test_nn_functional_threshold.py index 9e393cee6..ef03408c7 100644 --- a/tests/test_nn_functional_threshold.py +++ b/tests/test_nn_functional_threshold.py @@ -68,4 +68,4 @@ def test_case_4(): result = F.threshold(x, 0, 0, inplace=True) """ ) - obj.run(pytorch_code, ["result"], unsupport=True) + obj.run(pytorch_code, ["result"], unsupport=True, reason="paddle does not support the `inplace` parameter") From eb6349e628947a6e9b51bd7069126f546e0152f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Mon, 28 Aug 2023 20:52:39 +0800 Subject: [PATCH 15/15] Update test_nn_functional_threshold.py --- tests/test_nn_functional_threshold.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tests/test_nn_functional_threshold.py b/tests/test_nn_functional_threshold.py index ef03408c7..a59911719 100644 --- a/tests/test_nn_functional_threshold.py +++ b/tests/test_nn_functional_threshold.py @@ -24,8 +24,7 @@ def test_case_1(): """ import torch import torch.nn.functional as F - x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], - [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) result = F.threshold(x, 0, 0) """ ) @@ -37,8 +36,7 @@ def test_case_2(): """ import torch import torch.nn.functional as F - x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], - [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) result = F.threshold(x, 1, 0) """ ) @@ -50,8 +48,7 @@ def test_case_3(): """ import torch import torch.nn.functional as F - x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], - [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) result = F.threshold(x, 0, 1) """ ) @@ -63,8 +60,7 @@ def test_case_4(): """ import torch import torch.nn.functional as F - x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], - [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) result = F.threshold(x, 0, 0, inplace=True) """ )