From fa50d1d4f86d9127a637f4bc3f04bdc5207c868c Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Tue, 20 Jun 2023 11:28:36 +0000 Subject: [PATCH 01/12] add nn.Upsample,CUDAExtension,CppExtension,SequentialSampler,is_sparse --- paconvert/api_mapping.json | 36 +++++ paconvert/api_matcher.py | 14 ++ paconvert/attribute_mapping.json | 5 +- tests/test_Tensor_is_sparse.py | 32 ++++ tests/test_nn_Upsample.py | 127 ++++++++++++++++ .../test_utils_cpp_extension_CUDAExtension.py | 35 +++++ .../test_utils_cpp_extension_CppExtension.py | 34 +++++ tests/test_utils_data_SequentialSampler.py | 138 ++++++++++++++++++ 8 files changed, 420 insertions(+), 1 deletion(-) create mode 100644 tests/test_Tensor_is_sparse.py create mode 100644 tests/test_nn_Upsample.py create mode 100644 tests/test_utils_cpp_extension_CUDAExtension.py create mode 100644 tests/test_utils_cpp_extension_CppExtension.py create mode 100644 tests/test_utils_data_SequentialSampler.py diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index cf8286596..4003f0953 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -8657,5 +8657,41 @@ "kwargs_change": { "eps": "epsilon" } + }, + "torch.nn.Upsample": { + "Matcher": "GenericMatcher", + "paddle_api": "paddle.nn.Upsample", + "args_list": [ + "size", + "scale_factor", + "mode", + "align_corners" + ], + "unsupport_args": [ + "recompute_scale_factor" + ] + }, + "torch.utils.data.SequentialSampler": { + "Matcher": "GenericMatcher", + "paddle_api": "paddle.io.SequenceSampler", + "args_list": [ + "data_source" + ] + }, + "torch.utils.cpp_extension.CUDAExtension": { + "Matcher": "UtilsCppExtensionMatcher", + "paddle_api": "paddle.utils.cpp_extension.CUDAExtension", + "args_list": [ + "name", + "sources" + ] + }, + "torch.utils.cpp_extension.CppExtension": { + "Matcher": "UtilsCppExtensionMatcher", + "paddle_api": "paddle.utils.cpp_extension.CppExtension", + "args_list": [ + "name", + "sources" + ] } } diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index 6857fcb06..55a6675af 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3948,3 +3948,17 @@ def generate_code(self, kwargs): if "dim" not in kwargs: return None return GenericMatcher.generate_code(self, kwargs) + + +class UtilsCppExtensionMatcher(BaseMatcher): + def generate_code(self, kwargs): + if "name" in kwargs.keys(): + kwargs.pop("name") + return GenericMatcher.generate_code(self, kwargs) + + +class TensorIsSpareMatcher(BaseMatcher): + def get_paddle_class_attribute_nodes(self, node): + self.parse_func(node) + code = "{}()".format(self.paddle_api) + return ast.parse(code).body[0].value diff --git a/paconvert/attribute_mapping.json b/paconvert/attribute_mapping.json index 4a9d5026d..de6ab085d 100644 --- a/paconvert/attribute_mapping.json +++ b/paconvert/attribute_mapping.json @@ -31,7 +31,10 @@ "Matcher": "TensorRequires_GradMatcher", "paddle_api": "paddle.Tensor.stop_gradient" }, - "torch.Tensor.is_sparse": {}, + "torch.Tensor.is_sparse": { + "Matcher": "TensorIsSpareMatcher", + "paddle_api": "paddle.Tensor.is_sparse" + }, "torch.Tensor.is_cuda": {}, "torch.Tensor.is_quantized": {}, "torch.Tensor.is_meta": {}, diff --git a/tests/test_Tensor_is_sparse.py b/tests/test_Tensor_is_sparse.py new file mode 100644 index 000000000..4b8c4474f --- /dev/null +++ b/tests/test_Tensor_is_sparse.py @@ -0,0 +1,32 @@ +# 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.Tensor.is_sparse") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[ 0.9254, -0.6213]]) + result = a.is_sparse + """ + ) + obj.run(pytorch_code, ["result"]) + + +test_case_1() diff --git a/tests/test_nn_Upsample.py b/tests/test_nn_Upsample.py new file mode 100644 index 000000000..72e754eeb --- /dev/null +++ b/tests/test_nn_Upsample.py @@ -0,0 +1,127 @@ +# 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.Upsample") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[[[ 1.1524, 0.4714, 0.2857], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], + + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]]) + m = torch.nn.Upsample(scale_factor=2, mode='nearest') + result = m(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[[[ 1.1524, 0.4714, 0.2857], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], + + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]]) + m = torch.nn.Upsample(scale_factor=2, mode='bilinear') + result = m(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[[[ 1.1524, 0.4714, 0.2857], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], + + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]]) + m = torch.nn.Upsample(scale_factor=2, mode='bilinear',align_corners=True) + result = m(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[[[ 1.1524, 0.4714, 0.2857], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], + + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]]) + m = torch.nn.Upsample(size=(2,2)) + result = m(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[[[ 1.1524, 0.4714, 0.2857], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], + + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]]) + m = torch.nn.Upsample(scale_factor=2, mode='bilinear',align_corners=False) + result = m(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[[[ 1.1524, 0.4714, 0.2857], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], + + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]]) + m = torch.nn.Upsample(scale_factor=2, mode='bilinear',recompute_scale_factor=True) + result = m(input) + """ + ) + obj.run(pytorch_code, unsupport=True, reason="paddle unsupport") diff --git a/tests/test_utils_cpp_extension_CUDAExtension.py b/tests/test_utils_cpp_extension_CUDAExtension.py new file mode 100644 index 000000000..32711b30f --- /dev/null +++ b/tests/test_utils_cpp_extension_CUDAExtension.py @@ -0,0 +1,35 @@ +# 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.utils.cpp_extension.CUDAExtension") + + +def _test_case_1(): + pytorch_code = textwrap.dedent( + """ + from torch.utils.cpp_extension import CUDAExtension + + CUDAExtension( + name='cuda_extension', + sources=['extension.cpp', 'extension_kernel.cu'], + extra_compile_args={'cxx': ['-g'], + 'nvcc': ['-O2']}) + + """ + ) + obj.run(pytorch_code, unsupport=True, reason="Execute Compilation Options") diff --git a/tests/test_utils_cpp_extension_CppExtension.py b/tests/test_utils_cpp_extension_CppExtension.py new file mode 100644 index 000000000..71babcef8 --- /dev/null +++ b/tests/test_utils_cpp_extension_CppExtension.py @@ -0,0 +1,34 @@ +# 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.utils.cpp_extension.CppExtension") + + +def _test_case_1(): + pytorch_code = textwrap.dedent( + """ + from torch.utils.cpp_extension import CppExtension + + CppExtension( + name='cuda_extension', + sources=['extension.cpp'], + extra_compile_args=['-g']) + + """ + ) + obj.run(pytorch_code, unsupport=True, reason="Execute Compilation Options") diff --git a/tests/test_utils_data_SequentialSampler.py b/tests/test_utils_data_SequentialSampler.py new file mode 100644 index 000000000..2da3b3fa3 --- /dev/null +++ b/tests/test_utils_data_SequentialSampler.py @@ -0,0 +1,138 @@ +# 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.utils.data.SequentialSampler") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + from torch.utils.data import SequentialSampler + from torch.utils.data import Dataset + import numpy as np + + class Data(Dataset): + def __init__(self): + self.x = np.arange(0,100,1) + + def __getitem__(self, idx): + return self.x[idx] + + def __len__(self): + return self.x.shape[0] + + class MySampler(SequentialSampler): + def __init__(self, data_source): + self.data_source = data_source + + def __iter__(self): + return iter(range(len(self.data_source))) + + def __len__(self): + return len(self.data_source) + + data = Data() + s = MySampler(data) + result = [] + for d in s: + result.append(d) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + from torch.utils.data import SequentialSampler + from torch.utils.data import Dataset + import numpy as np + + class Data(Dataset): + def __init__(self): + self.x = np.arange(0,100,1) + + def __getitem__(self, idx): + return self.x[idx] + + def __len__(self): + return self.x.shape[0] + + class MySampler(SequentialSampler): + def __init__(self, data): + self.data_source = data + + def __iter__(self): + return iter(range(len(self.data_source))) + + def __len__(self): + return len(self.data_source) + + data = Data() + s = MySampler(data=data) + result = [] + for d in s: + result.append(d) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + from torch.utils.data import SequentialSampler + from torch.utils.data import Dataset + import numpy as np + import torch + + class Data(Dataset): + def __init__(self): + self.x = np.arange(0,100,1).reshape(10, 10) + self.y = np.arange(0, 10, 1) + + def __getitem__(self, idx): + return self.x[idx], self.y[idx] + + def __len__(self): + return self.x.shape[0] + + class MySampler(SequentialSampler): + def __init__(self, data): + self.data_source = data + + def __iter__(self): + return iter(range(1, len(self.data_source)+1)) + + def __len__(self): + return len(self.data_source) + + data = Data() + s = MySampler(data) + result = [] + for idx in s: + result.append(idx) + result = torch.tensor(result) + """ + ) + obj.run(pytorch_code, ["result"]) + + +test_case_1() +test_case_2() +test_case_3() From 8f519c019a687973817a2ebfe0bb6428eb2c12aa Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Wed, 21 Jun 2023 06:40:30 +0000 Subject: [PATCH 02/12] fix cpp_extension ut --- tests/test_utils_cpp_extension_CUDAExtension.py | 4 ++-- tests/test_utils_cpp_extension_CppExtension.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_utils_cpp_extension_CUDAExtension.py b/tests/test_utils_cpp_extension_CUDAExtension.py index 32711b30f..8afb580ff 100644 --- a/tests/test_utils_cpp_extension_CUDAExtension.py +++ b/tests/test_utils_cpp_extension_CUDAExtension.py @@ -29,7 +29,7 @@ def _test_case_1(): sources=['extension.cpp', 'extension_kernel.cu'], extra_compile_args={'cxx': ['-g'], 'nvcc': ['-O2']}) - + result = True """ ) - obj.run(pytorch_code, unsupport=True, reason="Execute Compilation Options") + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_utils_cpp_extension_CppExtension.py b/tests/test_utils_cpp_extension_CppExtension.py index 71babcef8..ad054e305 100644 --- a/tests/test_utils_cpp_extension_CppExtension.py +++ b/tests/test_utils_cpp_extension_CppExtension.py @@ -28,7 +28,7 @@ def _test_case_1(): name='cuda_extension', sources=['extension.cpp'], extra_compile_args=['-g']) - + result = True """ ) - obj.run(pytorch_code, unsupport=True, reason="Execute Compilation Options") + obj.run(pytorch_code, ["result"]) From 99bbe85125fa5800066dc5f0ed64dfd1ea139033 Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Wed, 21 Jun 2023 06:44:35 +0000 Subject: [PATCH 03/12] fix cpp_extension ut --- tests/test_utils_cpp_extension_CUDAExtension.py | 1 + tests/test_utils_cpp_extension_CppExtension.py | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/test_utils_cpp_extension_CUDAExtension.py b/tests/test_utils_cpp_extension_CUDAExtension.py index 8afb580ff..f4e9f8067 100644 --- a/tests/test_utils_cpp_extension_CUDAExtension.py +++ b/tests/test_utils_cpp_extension_CUDAExtension.py @@ -19,6 +19,7 @@ obj = APIBase("torch.utils.cpp_extension.CUDAExtension") +# The cuda compile not supports def _test_case_1(): pytorch_code = textwrap.dedent( """ diff --git a/tests/test_utils_cpp_extension_CppExtension.py b/tests/test_utils_cpp_extension_CppExtension.py index ad054e305..e9b5037b9 100644 --- a/tests/test_utils_cpp_extension_CppExtension.py +++ b/tests/test_utils_cpp_extension_CppExtension.py @@ -19,6 +19,7 @@ obj = APIBase("torch.utils.cpp_extension.CppExtension") +# The cpp compile not supports def _test_case_1(): pytorch_code = textwrap.dedent( """ From 600c754f381b778f99e0e3ac7106f7a8996d3248 Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Wed, 21 Jun 2023 09:01:17 +0000 Subject: [PATCH 04/12] fix UtilsCppExtensionMatcher ci --- 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 f38b131de..6265393a9 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3649,7 +3649,7 @@ def generate_code(self, kwargs): class UtilsCppExtensionMatcher(BaseMatcher): def generate_code(self, kwargs): - if "name" in kwargs.keys(): + if "name" in kwargs: kwargs.pop("name") return GenericMatcher.generate_code(self, kwargs) From aa5ebeca388cfc87dc183f3438683cd92caadf41 Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Wed, 21 Jun 2023 09:21:50 +0000 Subject: [PATCH 05/12] fix UtilsCppExtensionMatcher --- paconvert/api_matcher.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index 6265393a9..dea5ddcea 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3651,7 +3651,8 @@ class UtilsCppExtensionMatcher(BaseMatcher): def generate_code(self, kwargs): if "name" in kwargs: kwargs.pop("name") - return GenericMatcher.generate_code(self, kwargs) + code = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) + return code class TensorIsSpareMatcher(BaseMatcher): From 43d58d83f64710beb1c6b07093aabfa1e10a4a2c Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Wed, 21 Jun 2023 09:29:49 +0000 Subject: [PATCH 06/12] fix cpp_extension ut --- tests/test_utils_cpp_extension_CUDAExtension.py | 2 +- tests/test_utils_cpp_extension_CppExtension.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_utils_cpp_extension_CUDAExtension.py b/tests/test_utils_cpp_extension_CUDAExtension.py index f4e9f8067..18c020885 100644 --- a/tests/test_utils_cpp_extension_CUDAExtension.py +++ b/tests/test_utils_cpp_extension_CUDAExtension.py @@ -33,4 +33,4 @@ def _test_case_1(): result = True """ ) - obj.run(pytorch_code, ["result"]) + obj.run(pytorch_code, unsupport=True, reason="not support check") diff --git a/tests/test_utils_cpp_extension_CppExtension.py b/tests/test_utils_cpp_extension_CppExtension.py index e9b5037b9..ea2e4a576 100644 --- a/tests/test_utils_cpp_extension_CppExtension.py +++ b/tests/test_utils_cpp_extension_CppExtension.py @@ -32,4 +32,4 @@ def _test_case_1(): result = True """ ) - obj.run(pytorch_code, ["result"]) + obj.run(pytorch_code, unsupport=True, reason="not support check") From 5d53f8b15daa801f4067591f06ecff3febcef630 Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Sun, 25 Jun 2023 03:08:47 +0000 Subject: [PATCH 07/12] UtilsCppExtensionMatcher bug --- 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 dea5ddcea..e33ac3a7f 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3649,7 +3649,7 @@ def generate_code(self, kwargs): class UtilsCppExtensionMatcher(BaseMatcher): def generate_code(self, kwargs): - if "name" in kwargs: + if "name" in list(kwargs.leys()): kwargs.pop("name") code = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) return code From 5bc36b367e532118afe9eaa8174f932979a39a81 Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Sun, 25 Jun 2023 03:17:58 +0000 Subject: [PATCH 08/12] UtilsCppExtensionMatcher bug --- 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 e33ac3a7f..1053cb351 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3649,7 +3649,7 @@ def generate_code(self, kwargs): class UtilsCppExtensionMatcher(BaseMatcher): def generate_code(self, kwargs): - if "name" in list(kwargs.leys()): + if "name" in list(kwargs.keys()): kwargs.pop("name") code = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) return code From 99217471707dcba203bd9234e216f35bc689d1fb Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Mon, 26 Jun 2023 02:04:39 +0000 Subject: [PATCH 09/12] fix UtilsCppExtensionMatcher pop --- 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 1053cb351..f976a3a1b 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3650,7 +3650,7 @@ def generate_code(self, kwargs): class UtilsCppExtensionMatcher(BaseMatcher): def generate_code(self, kwargs): if "name" in list(kwargs.keys()): - kwargs.pop("name") + name = kwargs.pop("name") code = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) return code From 22ab3e580516df9c89f6c598850d8c60faa404ef Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Mon, 26 Jun 2023 02:15:02 +0000 Subject: [PATCH 10/12] fix cpp test --- paconvert/api_matcher.py | 10 ++++++---- tests/test_utils_cpp_extension_CUDAExtension.py | 2 +- tests/test_utils_cpp_extension_CppExtension.py | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index f976a3a1b..9bfa1e0e5 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3649,10 +3649,12 @@ def generate_code(self, kwargs): class UtilsCppExtensionMatcher(BaseMatcher): def generate_code(self, kwargs): - if "name" in list(kwargs.keys()): - name = kwargs.pop("name") - code = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(kwargs)) - return code + new_kwargs = {} + for k in kwargs.keys(): + if "name" in k: + continue + new_kwargs[k] = kwargs[k] + return GenericMatcher.generate_code(self, new_kwargs) class TensorIsSpareMatcher(BaseMatcher): diff --git a/tests/test_utils_cpp_extension_CUDAExtension.py b/tests/test_utils_cpp_extension_CUDAExtension.py index 18c020885..f4e9f8067 100644 --- a/tests/test_utils_cpp_extension_CUDAExtension.py +++ b/tests/test_utils_cpp_extension_CUDAExtension.py @@ -33,4 +33,4 @@ def _test_case_1(): result = True """ ) - obj.run(pytorch_code, unsupport=True, reason="not support check") + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_utils_cpp_extension_CppExtension.py b/tests/test_utils_cpp_extension_CppExtension.py index ea2e4a576..e9b5037b9 100644 --- a/tests/test_utils_cpp_extension_CppExtension.py +++ b/tests/test_utils_cpp_extension_CppExtension.py @@ -32,4 +32,4 @@ def _test_case_1(): result = True """ ) - obj.run(pytorch_code, unsupport=True, reason="not support check") + obj.run(pytorch_code, ["result"]) From b4c1d472f1c316b520660eb79dffe328a139fb38 Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Mon, 26 Jun 2023 02:31:06 +0000 Subject: [PATCH 11/12] add cpp test --- tests/test_utils_cpp_extension_CUDAExtension.py | 2 +- tests/test_utils_cpp_extension_CppExtension.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_utils_cpp_extension_CUDAExtension.py b/tests/test_utils_cpp_extension_CUDAExtension.py index f4e9f8067..753042cc5 100644 --- a/tests/test_utils_cpp_extension_CUDAExtension.py +++ b/tests/test_utils_cpp_extension_CUDAExtension.py @@ -20,7 +20,7 @@ # The cuda compile not supports -def _test_case_1(): +def test_case_1(): pytorch_code = textwrap.dedent( """ from torch.utils.cpp_extension import CUDAExtension diff --git a/tests/test_utils_cpp_extension_CppExtension.py b/tests/test_utils_cpp_extension_CppExtension.py index e9b5037b9..ebe613ee2 100644 --- a/tests/test_utils_cpp_extension_CppExtension.py +++ b/tests/test_utils_cpp_extension_CppExtension.py @@ -20,7 +20,7 @@ # The cpp compile not supports -def _test_case_1(): +def test_case_1(): pytorch_code = textwrap.dedent( """ from torch.utils.cpp_extension import CppExtension From ed9da8de2e79200ca9eafecb5b63d32b8b4d36fe Mon Sep 17 00:00:00 2001 From: LokeZhou Date: Tue, 4 Jul 2023 11:07:56 +0000 Subject: [PATCH 12/12] add Attribute2Func --- paconvert/api_matcher.py | 2 +- paconvert/attribute_mapping.json | 2 +- tests/test_nn_Upsample.py | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index 5597053a8..84c3b3afc 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -3539,7 +3539,7 @@ def generate_code(self, kwargs): return GenericMatcher.generate_code(self, kwargs) -class TensorIsSpareMatcher(BaseMatcher): +class Attribute2Func(BaseMatcher): def get_paddle_class_attribute_nodes(self, node): self.parse_func(node) code = "{}()".format(self.paddle_api) diff --git a/paconvert/attribute_mapping.json b/paconvert/attribute_mapping.json index 5ded3641c..88f4d8ef3 100644 --- a/paconvert/attribute_mapping.json +++ b/paconvert/attribute_mapping.json @@ -23,7 +23,7 @@ "torch.Tensor.is_meta": {}, "torch.Tensor.is_quantized": {}, "torch.Tensor.is_sparse": { - "Matcher": "TensorIsSpareMatcher", + "Matcher": "Attribute2Func", "paddle_api": "paddle.Tensor.is_sparse" }, "torch.Tensor.mH": {}, diff --git a/tests/test_nn_Upsample.py b/tests/test_nn_Upsample.py index 72e754eeb..b136c6bff 100644 --- a/tests/test_nn_Upsample.py +++ b/tests/test_nn_Upsample.py @@ -124,4 +124,6 @@ def test_case_6(): result = m(input) """ ) - obj.run(pytorch_code, unsupport=True, reason="paddle unsupport") + obj.run( + pytorch_code, unsupport=True, reason="paddle unsupport recompute_scale_factor " + )