From 96300cb7db4fd8f348ceb284b920e888fb9b3264 Mon Sep 17 00:00:00 2001 From: Fengsheng Wang Date: Wed, 5 Jul 2023 17:08:18 +0800 Subject: [PATCH] Add api test case (#154) Add api matcher (#89) * add fix api * fix bug * fix bug --- paconvert/api_mapping.json | 51 ++++++----- paconvert/api_matcher.py | 38 +++----- tests/test_Module_named_parameters.py | 111 ++++++++++++++++++++++++ tests/test_Module_register_buffer.py | 49 +++++++++++ tests/test_Tensor_arcsinh.py | 45 ++++++++++ tests/test_Tensor_arctanh.py | 30 +++++++ tests/test_Tensor_asinh.py | 30 +++++++ tests/test_Tensor_atanh.py | 30 +++++++ tests/test_Tensor_logdet.py | 2 +- tests/test_Tensor_masked_fill.py | 55 ++++++++++++ tests/test_Tensor_nonzero.py | 93 ++++++++++++++++++++ tests/test_Tensor_normal_.py | 63 ++++++++++++++ tests/test_Tensor_numpy.py | 30 +++++++ tests/test_Tensor_permute.py | 41 +++++++++ tests/test_Tensor_pin_memory.py | 32 +++++++ tests/test_Tensor_pinverse.py | 2 +- tests/test_Tensor_rad2deg.py | 51 +++++++++++ tests/test_Tensor_register_hook.py | 32 +++++++ tests/test_Tensor_remainder.py | 77 ++++++++++++++++ tests/test_Tensor_remainder_.py | 52 +++++++++++ tests/test_Tensor_requires_grad_.py | 52 +++++++++++ tests/test_Tensor_retain_grad.py | 31 +++++++ tests/test_Tensor_roll.py | 63 ++++++++++++++ tests/test_Tensor_rot90.py | 73 ++++++++++++++++ tests/test_Tensor_round.py | 2 +- tests/test_Tensor_rsqrt.py | 40 +++++++++ tests/test_Tensor_rsqrt_.py | 40 +++++++++ tests/test_Tensor_select.py | 2 +- tests/test_Tensor_sigmoid.py | 30 +++++++ tests/test_Tensor_sign.py | 14 +++ tests/test_Tensor_sin.py | 30 +++++++ tests/test_Tensor_sinh.py | 30 +++++++ tests/test_Tensor_slogdet.py | 12 +-- tests/test_Tensor_softmax.py | 67 ++++++++++++++ tests/test_Tensor_sort.py | 74 ++++++++++++++++ tests/test_Tensor_sqrt.py | 40 +++++++++ tests/test_Tensor_sqrt_.py | 40 +++++++++ tests/test_Tensor_square.py | 40 +++++++++ tests/test_Tensor_squeeze.py | 62 +++++++++++++ tests/test_Tensor_squeeze_.py | 62 +++++++++++++ tests/test_Tensor_std.py | 86 ++++++++++++++++++ tests/test_Tensor_sum.py | 86 ++++++++++++++++++ tests/test_Tensor_t.py | 33 +++++++ tests/test_Tensor_tan.py | 30 +++++++ tests/test_Tensor_tanh.py | 33 +++++++ tests/test_Tensor_tanh_.py | 30 +++++++ tests/test_Tensor_tile.py | 30 +++++++ tests/test_Tensor_tolist.py | 30 +++++++ tests/test_Tensor_topk.py | 74 ++++++++++++++++ tests/test_Tensor_trace.py | 30 +++++++ tests/test_Tensor_true_divide.py | 30 +++++++ tests/test_Tensor_trunc.py | 30 +++++++ tests/test_Tensor_unbind.py | 41 +++++++++ tests/test_Tensor_uniform_.py | 63 ++++++++++++++ tests/test_Tensor_unique_consecutive.py | 74 ++++++++++++++++ tests/test_Tensor_unsqueeze.py | 63 ++++++++++++++ tests/test_Tensor_unsqueeze_.py | 63 ++++++++++++++ tests/test_Tensor_var.py | 109 +++++++++++++++++++++++ tests/test_Tensor_where.py | 52 +++++++++++ tests/test_Tensor_zero_.py | 30 +++++++ tests/test_register_forward_hook.py | 45 ++++++++++ tests/test_register_forward_pre_hook.py | 45 ++++++++++ 62 files changed, 2766 insertions(+), 59 deletions(-) create mode 100644 tests/test_Module_named_parameters.py create mode 100644 tests/test_Module_register_buffer.py create mode 100644 tests/test_Tensor_arcsinh.py create mode 100644 tests/test_Tensor_arctanh.py create mode 100644 tests/test_Tensor_asinh.py create mode 100644 tests/test_Tensor_atanh.py create mode 100644 tests/test_Tensor_masked_fill.py create mode 100644 tests/test_Tensor_nonzero.py create mode 100644 tests/test_Tensor_normal_.py create mode 100644 tests/test_Tensor_numpy.py create mode 100644 tests/test_Tensor_permute.py create mode 100644 tests/test_Tensor_pin_memory.py create mode 100644 tests/test_Tensor_rad2deg.py create mode 100644 tests/test_Tensor_register_hook.py create mode 100644 tests/test_Tensor_remainder.py create mode 100644 tests/test_Tensor_remainder_.py create mode 100644 tests/test_Tensor_requires_grad_.py create mode 100644 tests/test_Tensor_retain_grad.py create mode 100644 tests/test_Tensor_roll.py create mode 100644 tests/test_Tensor_rot90.py create mode 100644 tests/test_Tensor_rsqrt.py create mode 100644 tests/test_Tensor_rsqrt_.py create mode 100644 tests/test_Tensor_sigmoid.py create mode 100644 tests/test_Tensor_sign.py create mode 100644 tests/test_Tensor_sin.py create mode 100644 tests/test_Tensor_sinh.py create mode 100644 tests/test_Tensor_softmax.py create mode 100644 tests/test_Tensor_sort.py create mode 100644 tests/test_Tensor_sqrt.py create mode 100644 tests/test_Tensor_sqrt_.py create mode 100644 tests/test_Tensor_square.py create mode 100644 tests/test_Tensor_squeeze.py create mode 100644 tests/test_Tensor_squeeze_.py create mode 100644 tests/test_Tensor_std.py create mode 100644 tests/test_Tensor_sum.py create mode 100644 tests/test_Tensor_t.py create mode 100644 tests/test_Tensor_tan.py create mode 100644 tests/test_Tensor_tanh.py create mode 100644 tests/test_Tensor_tanh_.py create mode 100644 tests/test_Tensor_tile.py create mode 100644 tests/test_Tensor_tolist.py create mode 100644 tests/test_Tensor_topk.py create mode 100644 tests/test_Tensor_trace.py create mode 100644 tests/test_Tensor_true_divide.py create mode 100644 tests/test_Tensor_trunc.py create mode 100644 tests/test_Tensor_unbind.py create mode 100644 tests/test_Tensor_uniform_.py create mode 100644 tests/test_Tensor_unique_consecutive.py create mode 100644 tests/test_Tensor_unsqueeze.py create mode 100644 tests/test_Tensor_unsqueeze_.py create mode 100644 tests/test_Tensor_var.py create mode 100644 tests/test_Tensor_where.py create mode 100644 tests/test_Tensor_zero_.py create mode 100644 tests/test_register_forward_hook.py create mode 100644 tests/test_register_forward_pre_hook.py diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 786b9dee4..3f169ba4c 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -207,7 +207,8 @@ }, "torch.Tensor.arcsin_": {}, "torch.Tensor.arcsinh": { - "Matcher": "TensorUnchangeMatcher" + "Matcher": "TensorFunc2PaddleFunc", + "paddle_api": "paddle.asinh" }, "torch.Tensor.arcsinh_": {}, "torch.Tensor.arctan": { @@ -226,7 +227,8 @@ "torch.Tensor.arctan2_": {}, "torch.Tensor.arctan_": {}, "torch.Tensor.arctanh": { - "Matcher": "TensorUnchangeMatcher" + "Matcher": "TensorFunc2PaddleFunc", + "paddle_api": "paddle.atanh" }, "torch.Tensor.arctanh_": {}, "torch.Tensor.argmax": { @@ -1755,8 +1757,8 @@ "torch.Tensor.orgqr": {}, "torch.Tensor.ormqr": {}, "torch.Tensor.outer": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.Tensor.outer", + "Matcher": "TensorFunc2PaddleFunc", + "paddle_api": "paddle.outer", "args_list": [ "vec2" ], @@ -1820,8 +1822,8 @@ "paddle_api": "paddle.Tensor.flatten" }, "torch.Tensor.reciprocal": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.Tensor.reciprocal" + "Matcher": "TensorFunc2PaddleFunc", + "paddle_api": "paddle.reciprocal" }, "torch.Tensor.reciprocal_": { "Matcher": "GenericMatcher", @@ -1837,8 +1839,8 @@ ] }, "torch.Tensor.remainder": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.Tensor.remainder", + "Matcher": "TensorFunc2PaddleFunc", + "paddle_api": "paddle.remainder", "args_list": [ "divisor" ], @@ -1847,8 +1849,8 @@ } }, "torch.Tensor.remainder_": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.Tensor.remainder_", + "Matcher": "TensorFunc2PaddleFunc", + "paddle_api": "paddle.remainder_", "args_list": [ "divisor" ], @@ -1909,7 +1911,7 @@ "torch.Tensor.resolve_neg": {}, "torch.Tensor.retain_grad": { "Matcher": "GenericMatcher", - "paddle_api": "paddle.Tensor.retain_grad" + "paddle_api": "paddle.Tensor.retain_grads" }, "torch.Tensor.retains_grad": {}, "torch.Tensor.roll": { @@ -1917,21 +1919,21 @@ "paddle_api": "paddle.Tensor.roll", "args_list": [ "shifts", - "dim" + "dims" ], "kwargs_change": { - "dim": "axis" + "dims": "axis" } }, "torch.Tensor.rot90": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.Tensor.rot90", + "Matcher": "TensorFunc2PaddleFunc", + "paddle_api": "paddle.rot90", "args_list": [ "k", "dims" ], "kwargs_change": { - "dims": "axis" + "dims": "axes" } }, "torch.Tensor.round": { @@ -2008,7 +2010,7 @@ }, "torch.Tensor.slice_scatter": {}, "torch.Tensor.slogdet": { - "Matcher": "TensorSLogDetMatcher", + "Matcher": "SLogDetMatcher", "paddle_api": "paddle.linalg.slogdet", "args_list": [ "out" @@ -2022,15 +2024,19 @@ ] }, "torch.Tensor.sort": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.Tensor.sort", + "Matcher": "TensorFunc2PaddleFunc", + "paddle_api": "paddle.sort", "args_list": [ "dim", - "descending" + "descending", + "stable" ], "kwargs_change": { "dim": "axis" - } + }, + "unsupport_args": [ + "stable" + ] }, "torch.Tensor.sparse_dim": {}, "torch.Tensor.sparse_mask": {}, @@ -2169,7 +2175,8 @@ } }, "torch.Tensor.tan": { - "Matcher": "TensorUnchangeMatcher" + "Matcher": "TensorFunc2PaddleFunc", + "paddle_api": "paddle.tan" }, "torch.Tensor.tan_": {}, "torch.Tensor.tanh": { diff --git a/paconvert/api_matcher.py b/paconvert/api_matcher.py index 81512b1b1..f6148c70d 100644 --- a/paconvert/api_matcher.py +++ b/paconvert/api_matcher.py @@ -2475,6 +2475,8 @@ def generate_code(self, kwargs): class NumelMatcher(BaseMatcher): def generate_code(self, kwargs): + if "input" not in kwargs: + kwargs["input"] = self.paddleClass return "{}.size".format(kwargs["input"]) @@ -2948,7 +2950,13 @@ def generate_code(self, kwargs): out_v = kwargs.pop("out") if "out" in kwargs else None if "input" in kwargs: - kwargs["A"] = kwargs.pop("input") + x_v = kwargs.pop("input") + + elif "A" in kwargs: + x_v = kwargs.pop("A") + + else: + x_v = self.paddleClass if out_v: API_TEMPLATE = textwrap.dedent( @@ -2957,7 +2965,7 @@ def generate_code(self, kwargs): paddle.assign(res[0], {}[0]), paddle.assign(res[1], {}[1]) """ ) - code = API_TEMPLATE.format(kwargs["A"], out_v, out_v) + code = API_TEMPLATE.format(x_v, out_v, out_v) else: API_TEMPLATE = textwrap.dedent( """ @@ -2965,7 +2973,7 @@ def generate_code(self, kwargs): res[0], res[1] """ ) - code = API_TEMPLATE.format(kwargs["A"]) + code = API_TEMPLATE.format(x_v) return code @@ -3769,27 +3777,3 @@ def generate_code(self, kwargs): code = "{}".format(self.get_paddle_api()) return code - - -class TensorSLogDetMatcher(BaseMatcher): - def generate_code(self, kwargs): - out_v = kwargs.pop("out") if "out" in kwargs else None - - if out_v: - API_TEMPLATE = textwrap.dedent( - """ - res = paddle.linalg.slogdet({}) - paddle.assign(res[0], {}[0]), paddle.assign(res[1], {}[1]) - """ - ) - code = API_TEMPLATE.format(self.paddleClass, out_v, out_v) - else: - API_TEMPLATE = textwrap.dedent( - """ - res = paddle.linalg.slogdet({}) - res[0], res[1] - """ - ) - code = API_TEMPLATE.format(self.paddleClass) - - return code diff --git a/tests/test_Module_named_parameters.py b/tests/test_Module_named_parameters.py new file mode 100644 index 000000000..e894016db --- /dev/null +++ b/tests/test_Module_named_parameters.py @@ -0,0 +1,111 @@ +# 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.Moudle.named_parameters") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + result = [] + class TestForHook(nn.Module): + def __init__(self): + super().__init__() + + self.linear_1 = nn.Linear(in_features=2, out_features=2) + def forward(self, x): + x1 = self.linear_1(x) + return x, x, x1 + a = TestForHook() + for a,b in a.named_parameters(prefix="wfs"): + result.append(b) + result = result[0] + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + result = [] + class TestForHook(nn.Module): + def __init__(self): + super().__init__() + + self.linear_1 = nn.Linear(in_features=2, out_features=2) + def forward(self, x): + x1 = self.linear_1(x) + return x, x, x1 + a = TestForHook() + for a,b in a.named_parameters(prefix="wfs", recurse=True): + result.append(b) + result = result[0] + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + result = [] + class TestForHook(nn.Module): + def __init__(self): + super().__init__() + + self.linear_1 = nn.Linear(in_features=2, out_features=2) + def forward(self, x): + x1 = self.linear_1(x) + return x, x, x1 + a = TestForHook() + for a,b in a.named_parameters(prefix="wfs", recurse=True, remove_duplicate = True): + result.append(b) + result = result[0] + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + result = [] + class TestForHook(nn.Module): + def __init__(self): + super().__init__() + + self.linear_1 = nn.Linear(in_features=2, out_features=2) + def forward(self, x): + x1 = self.linear_1(x) + return x, x, x1 + a = TestForHook() + for a,b in a.named_parameters(remove_duplicate = True): + result.append(b) + result = result[0] + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) diff --git a/tests/test_Module_register_buffer.py b/tests/test_Module_register_buffer.py new file mode 100644 index 000000000..f0a970d3f --- /dev/null +++ b/tests/test_Module_register_buffer.py @@ -0,0 +1,49 @@ +# 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.Moudle.register_buffer") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1., 2., 3.]) + module1 = torch.nn.Module() + module1.register_buffer('buffer', x) + module2 = torch.nn.Module() + module2.register_module('submodule', module1) + result = module2.submodule.buffer + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1., 2., 3.]) + module1 = torch.nn.Module() + module1.register_buffer('buffer', x) + module2 = torch.nn.Module() + module2.register_module(name='submodule', module=module1) + result = module2.submodule.buffer + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_arcsinh.py b/tests/test_Tensor_arcsinh.py new file mode 100644 index 000000000..d6a33f6ca --- /dev/null +++ b/tests/test_Tensor_arcsinh.py @@ -0,0 +1,45 @@ +# 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.arcsinh") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([1.3192, 1.9915, 1.9674, 1.7151]).arcsinh() + """ + ) + obj.run(pytorch_code, ["result"]) + + +test_case_1() + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1.3192, 1.9915, 1.9674, 1.7151]) + result = a.arcsinh() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_arctanh.py b/tests/test_Tensor_arctanh.py new file mode 100644 index 000000000..6e05638d0 --- /dev/null +++ b/tests/test_Tensor_arctanh.py @@ -0,0 +1,30 @@ +# 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.arctanh") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.arctanh() + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) diff --git a/tests/test_Tensor_asinh.py b/tests/test_Tensor_asinh.py new file mode 100644 index 000000000..67dea14ad --- /dev/null +++ b/tests/test_Tensor_asinh.py @@ -0,0 +1,30 @@ +# 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.asinh") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.asinh() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_atanh.py b/tests/test_Tensor_atanh.py new file mode 100644 index 000000000..76515814a --- /dev/null +++ b/tests/test_Tensor_atanh.py @@ -0,0 +1,30 @@ +# 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.atanh") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.atanh() + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) diff --git a/tests/test_Tensor_logdet.py b/tests/test_Tensor_logdet.py index a3b57683e..62f439ce5 100644 --- a/tests/test_Tensor_logdet.py +++ b/tests/test_Tensor_logdet.py @@ -15,7 +15,7 @@ from apibase import APIBase -obj = APIBase("torch.Tenosor.logdet") +obj = APIBase("torch.Tensor.logdet") def test_case_1(): diff --git a/tests/test_Tensor_masked_fill.py b/tests/test_Tensor_masked_fill.py new file mode 100644 index 000000000..c8dfab2ea --- /dev/null +++ b/tests/test_Tensor_masked_fill.py @@ -0,0 +1,55 @@ +# 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.masked_fill") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.0,0.2], [0.3,0.4]]) + b = torch.Tensor([[1,0], [1,1]]) + result = a.masked_fill(b==1, a) + """ + ) + obj.run(pytorch_code, [], reason="torch and paddle where api diff") + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.0,0.2], [0.3,0.4]]) + b = torch.Tensor([[1,0], [1,1]]) + result = a.masked_fill(b==1, 1.0) + """ + ) + obj.run(pytorch_code, [], reason="torch and paddle where api diff") + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.0,0.2], [0.3,0.4]]) + b = torch.Tensor([[1,0], [1,1]]) + result = a.masked_fill(b==1, 0.1) + """ + ) + obj.run(pytorch_code, [], reason="torch and paddle where api diff") diff --git a/tests/test_Tensor_nonzero.py b/tests/test_Tensor_nonzero.py new file mode 100644 index 000000000..01459940c --- /dev/null +++ b/tests/test_Tensor_nonzero.py @@ -0,0 +1,93 @@ +# 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.nonzero") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([1, 1, 1, 0, 1]).nonzero() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([[0.6, 0.0, 0.0, 0.0], + [0.0, 0.4, 0.0, 0.0], + [0.0, 0.0, 1.2, 0.0], + [0.0, 0.0, 0.0,-0.4]]).nonzero() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[0.6, 0.0, 0.0, 0.0], + [0.0, 0.4, 0.0, 0.0], + [0.0, 0.0, 1.2, 0.0], + [0.0, 0.0, 0.0,-0.4]]) + result = x.nonzero(as_tuple=True) + """ + ) + obj.run( + pytorch_code, + [], + reason="The return shape is inconsistent when as_tuple=True", + ) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[0.6, 0.0, 0.0, 0.0], + [0.0, 0.4, 0.0, 0.0], + [0.0, 0.0, 1.2, 0.0], + [0.0, 0.0, 0.0,-0.4]]) + as_tuple = True + result = x.nonzero(as_tuple=as_tuple) + """ + ) + obj.run( + pytorch_code, + [], + reason="The return shape is inconsistent when as_tuple=True", + ) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[0.6, 0.0, 0.0, 0.0], + [0.0, 0.4, 0.0, 0.0], + [0.0, 0.0, 1.2, 0.0], + [0.0, 0.0, 0.0,-0.4]]) + result = x.nonzero(as_tuple=False) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_normal_.py b/tests/test_Tensor_normal_.py new file mode 100644 index 000000000..872a6c020 --- /dev/null +++ b/tests/test_Tensor_normal_.py @@ -0,0 +1,63 @@ +# 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.normal_") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.Tensor([[1.,2.], [3.,4.]]) + result.normal_() + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.Tensor([[1.,2.], [3.,4.]]) + result.normal_(mean=0) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.Tensor([[1.,2.], [3.,4.]]) + result.normal_(mean=0, std=1) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.Tensor([[1.,2.], [3.,4.]]) + result.normal_(mean=0, std=2) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) diff --git a/tests/test_Tensor_numpy.py b/tests/test_Tensor_numpy.py new file mode 100644 index 000000000..90880a18b --- /dev/null +++ b/tests/test_Tensor_numpy.py @@ -0,0 +1,30 @@ +# 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.numpy") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = torch.tensor(a.numpy()) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_permute.py b/tests/test_Tensor_permute.py new file mode 100644 index 000000000..3161f7861 --- /dev/null +++ b/tests/test_Tensor_permute.py @@ -0,0 +1,41 @@ +# 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.permute") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.permute(dims=(0,1)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.permute((0, 1)) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_pin_memory.py b/tests/test_Tensor_pin_memory.py new file mode 100644 index 000000000..7a75ebc9d --- /dev/null +++ b/tests/test_Tensor_pin_memory.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.pin_memory") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = None + if torch.cuda.is_available(): + result = a.pin_memory() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_pinverse.py b/tests/test_Tensor_pinverse.py index 561a1f33a..e3986b94d 100644 --- a/tests/test_Tensor_pinverse.py +++ b/tests/test_Tensor_pinverse.py @@ -15,7 +15,7 @@ from apibase import APIBase -obj = APIBase("torch.Tenosor.pinverse") +obj = APIBase("torch.Tensor.pinverse") def test_case_1(): diff --git a/tests/test_Tensor_rad2deg.py b/tests/test_Tensor_rad2deg.py new file mode 100644 index 000000000..afbe34b14 --- /dev/null +++ b/tests/test_Tensor_rad2deg.py @@ -0,0 +1,51 @@ +# 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.rad2deg") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[3.142, -3.142], [6.283, -6.283], [1.570, -1.570]]) + result = x.rad2deg() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([[3.142, -3.142], [6.283, -6.283], [1.570, -1.570]]).rad2deg() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([3.142, -3.142, 6.283, -6.283]) + result = x.rad2deg() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_register_hook.py b/tests/test_Tensor_register_hook.py new file mode 100644 index 000000000..9e89219af --- /dev/null +++ b/tests/test_Tensor_register_hook.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.register_hook") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + v = torch.tensor([0., 0., 0.], requires_grad=True) + h = v.register_hook(lambda grad: grad * 2) # double the gradient + v.backward(torch.tensor([1., 2., 3.])) + result = torch.tensor(v.grad) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_remainder.py b/tests/test_Tensor_remainder.py new file mode 100644 index 000000000..7fbbab36c --- /dev/null +++ b/tests/test_Tensor_remainder.py @@ -0,0 +1,77 @@ +# 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.remainder") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([-3., -2, -1, 1, 2, 3]) + result = a.remainder(torch.tensor(2.)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# The paddle input does not support integer type +def _test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1, 2, 3, 4, 5]) + result = a.remainder(torch.tensor(1.5)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([-3., -2, -1, 1, 2, 3]).remainder(torch.tensor(2.)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1., 2, 3, 4, 5]) + b = torch.tensor([1, 0.5, 0.6, 1.2, 2.4]) + result = a.remainder(b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# paddle.remainder not support type promote and x/y must have same dtype +def _test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1, 2, 3, 4, 5]) + b = torch.tensor([1, 0.5, 0.6, 1.2, 2.4]) + result = a.remainder(b) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_remainder_.py b/tests/test_Tensor_remainder_.py new file mode 100644 index 000000000..4fec4124a --- /dev/null +++ b/tests/test_Tensor_remainder_.py @@ -0,0 +1,52 @@ +# 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.remainder_") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([-3., -2, -1, 1, 2, 3]) + result = a.remainder_(torch.tensor(2.)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([-3., -2, -1, 1, 2, 3]).remainder_(torch.tensor(2.)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([1., 2, 3, 4, 5]) + b = torch.tensor([1, 0.5, 0.6, 1.2, 2.4]) + result.remainder_(b) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_requires_grad_.py b/tests/test_Tensor_requires_grad_.py new file mode 100644 index 000000000..baeb0f841 --- /dev/null +++ b/tests/test_Tensor_requires_grad_.py @@ -0,0 +1,52 @@ +# 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.sigmoid") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.requires_grad_(True) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.requires_grad_() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.requires_grad_(requires_grad=True) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_retain_grad.py b/tests/test_Tensor_retain_grad.py new file mode 100644 index 000000000..254e4a57d --- /dev/null +++ b/tests/test_Tensor_retain_grad.py @@ -0,0 +1,31 @@ +# 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.retain_grad") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.Tensor([[1.,2.], [3.,4.]]) + result.requires_grad = True + result.retain_grad() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_roll.py b/tests/test_Tensor_roll.py new file mode 100644 index 000000000..a7f559546 --- /dev/null +++ b/tests/test_Tensor_roll.py @@ -0,0 +1,63 @@ +# 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.roll") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[1, 2], [3, 4], [5, 6], [7, 8]]) + result = x.roll(1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[1, 2], [3, 4], [5, 6], [7, 8]]) + result = x.roll(1, 0) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[1, 2], [3, 4], [5, 6], [7, 8]]) + result = x.roll(shifts=-1, dims=0) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[1, 2], [3, 4], [5, 6], [7, 8]]) + result = x.roll(shifts=(2, 1), dims=(0, 1)) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_rot90.py b/tests/test_Tensor_rot90.py new file mode 100644 index 000000000..854b2271b --- /dev/null +++ b/tests/test_Tensor_rot90.py @@ -0,0 +1,73 @@ +# 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.rot90") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + result = x.rot90() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + result = x.rot90(2) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]) + result = x.rot90(dims=[1, 2]) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + result = x.rot90(k=2) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]).rot90(dims=[1, 2]) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_round.py b/tests/test_Tensor_round.py index 8bffc8567..e279247a8 100644 --- a/tests/test_Tensor_round.py +++ b/tests/test_Tensor_round.py @@ -15,7 +15,7 @@ from apibase import APIBase -obj = APIBase("torch.Tenosor.round") +obj = APIBase("torch.Tensor.round") def test_case_1(): diff --git a/tests/test_Tensor_rsqrt.py b/tests/test_Tensor_rsqrt.py new file mode 100644 index 000000000..f3ab18a4a --- /dev/null +++ b/tests/test_Tensor_rsqrt.py @@ -0,0 +1,40 @@ +# 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.rsqrt") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([0.2970, 1.5420, 4]).rsqrt() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.2970, 1.5420, 4]) + result = a.rsqrt() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_rsqrt_.py b/tests/test_Tensor_rsqrt_.py new file mode 100644 index 000000000..0a5ce8b40 --- /dev/null +++ b/tests/test_Tensor_rsqrt_.py @@ -0,0 +1,40 @@ +# 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.rsqrt_") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([0.2970, 1.5420, 4]).rsqrt_() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([0.2970, 1.5420, 4]) + result.rsqrt_() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_select.py b/tests/test_Tensor_select.py index 6b11eb97e..dc53c8f29 100644 --- a/tests/test_Tensor_select.py +++ b/tests/test_Tensor_select.py @@ -15,7 +15,7 @@ from apibase import APIBase -obj = APIBase("torch.Tenosor.select") +obj = APIBase("torch.Tensor.select") def test_case_1(): diff --git a/tests/test_Tensor_sigmoid.py b/tests/test_Tensor_sigmoid.py new file mode 100644 index 000000000..4089e0184 --- /dev/null +++ b/tests/test_Tensor_sigmoid.py @@ -0,0 +1,30 @@ +# 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.sigmoid") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.sigmoid() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_sign.py b/tests/test_Tensor_sign.py new file mode 100644 index 000000000..dbcf2ab31 --- /dev/null +++ b/tests/test_Tensor_sign.py @@ -0,0 +1,14 @@ +# 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. +# diff --git a/tests/test_Tensor_sin.py b/tests/test_Tensor_sin.py new file mode 100644 index 000000000..acc8107fb --- /dev/null +++ b/tests/test_Tensor_sin.py @@ -0,0 +1,30 @@ +# 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.sin") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.sin() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_sinh.py b/tests/test_Tensor_sinh.py new file mode 100644 index 000000000..5253c3fb5 --- /dev/null +++ b/tests/test_Tensor_sinh.py @@ -0,0 +1,30 @@ +# 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.sinh") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.sinh() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_slogdet.py b/tests/test_Tensor_slogdet.py index ce304ba7b..58ec6742f 100644 --- a/tests/test_Tensor_slogdet.py +++ b/tests/test_Tensor_slogdet.py @@ -23,11 +23,11 @@ def test_case_1(): pytorch_code = textwrap.dedent( """ import torch - a = torch.tensor([[ 0.0032, -0.2239, -1.1219], - [-0.6690, 0.1161, 0.4053], - [-1.6218, -0.9273, -0.0082]]) - result = a.slogdet() - result = result + x = torch.tensor([[ 0.7308, 1.0060, 0.5270, 1.4516], + [-0.1383, 1.5706, 0.4724, 0.4141], + [ 0.1193, 0.2829, 0.9037, 0.3957], + [-0.8202, -0.6474, -0.1631, -0.6543]]) + result1, result2 = x.slogdet() """ ) - obj.run(pytorch_code, ["result"]) + obj.run(pytorch_code, ["result1", "result2"]) diff --git a/tests/test_Tensor_softmax.py b/tests/test_Tensor_softmax.py new file mode 100644 index 000000000..f631689d8 --- /dev/null +++ b/tests/test_Tensor_softmax.py @@ -0,0 +1,67 @@ +# 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.softmax") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + result = input.softmax(dim=0) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + result = input.softmax(dim=1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + result = input.softmax(dim=1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + result = input.softmax(dim=1, dtype=torch.float32) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_sort.py b/tests/test_Tensor_sort.py new file mode 100644 index 000000000..db1118835 --- /dev/null +++ b/tests/test_Tensor_sort.py @@ -0,0 +1,74 @@ +# 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.sort") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[4, 9], [23, 2]]) + result = a.sort() + if isinstance(result, tuple): + result = result[0] + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[4, 9], [23, 2]]) + result = a.sort(0) + if isinstance(result, tuple): + result = result[0] + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[4, 9], [23, 2]]) + result = a.sort(dim=1, descending=True, stable=True) + if isinstance(result, tuple): + result = result[0] + """ + ) + obj.run( + pytorch_code, ["result"], unsupport=True, reason="unsupprt stable parameter" + ) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + descending=False + a = torch.tensor([[4, 9], [23, 2]]) + result = a.sort(dim=1, descending=descending) + if isinstance(result, tuple): + result = result[0] + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_sqrt.py b/tests/test_Tensor_sqrt.py new file mode 100644 index 000000000..1dc396316 --- /dev/null +++ b/tests/test_Tensor_sqrt.py @@ -0,0 +1,40 @@ +# 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.sqrt") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([0.2970, 1.5420, 4]).sqrt() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.2970, 1.5420, 4]) + result = a.sqrt() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_sqrt_.py b/tests/test_Tensor_sqrt_.py new file mode 100644 index 000000000..6c847a64e --- /dev/null +++ b/tests/test_Tensor_sqrt_.py @@ -0,0 +1,40 @@ +# 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.sqrt_") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([0.2970, 1.5420, 4]).sqrt_() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([0.2970, 1.5420, 4]) + result.sqrt_() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_square.py b/tests/test_Tensor_square.py new file mode 100644 index 000000000..bf1376336 --- /dev/null +++ b/tests/test_Tensor_square.py @@ -0,0 +1,40 @@ +# 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.square") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.tensor([0.2970, 1.5420, 4]).square() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.2970, 1.5420, 4]) + result = a.square() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_squeeze.py b/tests/test_Tensor_squeeze.py new file mode 100644 index 000000000..9c48e9ac5 --- /dev/null +++ b/tests/test_Tensor_squeeze.py @@ -0,0 +1,62 @@ +# 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.squeeze") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.zeros(2, 1, 2, 1, 2) + result = x.squeeze() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.zeros(2, 1, 2, 1, 2).squeeze() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.zeros(2, 1, 2, 1, 2) + result = x.squeeze(1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.zeros(2, 1, 2, 1, 2) + result = x.squeeze(dim=1) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_squeeze_.py b/tests/test_Tensor_squeeze_.py new file mode 100644 index 000000000..2404e50b0 --- /dev/null +++ b/tests/test_Tensor_squeeze_.py @@ -0,0 +1,62 @@ +# 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.squeeze_") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.zeros(2, 1, 2, 1, 2) + result.squeeze_() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.zeros(2, 1, 2, 1, 2).squeeze_() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.zeros(2, 1, 2, 1, 2) + result.squeeze_(1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.zeros(2, 1, 2, 1, 2) + result.squeeze_(dim=1) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_std.py b/tests/test_Tensor_std.py new file mode 100644 index 000000000..675fb181e --- /dev/null +++ b/tests/test_Tensor_std.py @@ -0,0 +1,86 @@ +# 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.std") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([1.4907, 1.0593, 1.5696]) + result = input.std(unbiased=False) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1.4907, 1.0593, 1.5696], [1.4907, 1.0593, 1.5696]]) + result = input.std(unbiased=False) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1.4907, 1.0593, 1.5696], [1.4907, 1.0593, 1.5696]]) + result = input.std(1, unbiased=False, keepdim=True) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1.4907, 1.0593, 1.5696], [1.4907, 1.0593, 1.5696]]) + result = input.std(dim=1, unbiased=False, keepdim=True) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1.4907, 1.0593, 1.5696], [1.4907, 1.0593, 1.5696]]) + out = torch.tensor([[1.4907, 1.0593, 1.5696], [1.4907, 1.0593, 1.5696]]) + result = input.std(dim=1, unbiased=False, keepdim=True) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1.4907, 1.0593, 1.5696], [1.4907, 1.0593, 1.5696]]) + result = input.std(dim=1, unbiased=True, keepdim=True) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_sum.py b/tests/test_Tensor_sum.py new file mode 100644 index 000000000..20b3907eb --- /dev/null +++ b/tests/test_Tensor_sum.py @@ -0,0 +1,86 @@ +# 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.sum") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([1.4907, 1.0593, 1.5696]) + result = input.sum() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1.4907, 1.0593, 1.5696], [1.4907, 1.0593, 1.5696]]) + result = input.sum(1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1.4907, 1.0593, 1.5696], [1.4907, 1.0593, 1.5696]]) + result = input.sum(1, keepdim=True) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1.4907, 1.0593, 1.5696], [1.4907, 1.0593, 1.5696]]) + result = input.sum(dim=1, keepdim=True) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1.4907, 1.0593, 1.5696], [1.4907, 1.0593, 1.5696]]) + out = torch.tensor([[1.4907, 1.0593, 1.5696], [1.4907, 1.0593, 1.5696]]) + result = input.sum(dim=1, keepdim=True) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([1, 2, 3]) + result = input.sum() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_t.py b/tests/test_Tensor_t.py new file mode 100644 index 000000000..db0ab6923 --- /dev/null +++ b/tests/test_Tensor_t.py @@ -0,0 +1,33 @@ +# 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.t") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.t() + """ + ) + obj.run(pytorch_code, ["result"]) + + +test_case_1() diff --git a/tests/test_Tensor_tan.py b/tests/test_Tensor_tan.py new file mode 100644 index 000000000..b47fa3d52 --- /dev/null +++ b/tests/test_Tensor_tan.py @@ -0,0 +1,30 @@ +# 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.tan") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.tan() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_tanh.py b/tests/test_Tensor_tanh.py new file mode 100644 index 000000000..9f4afd0ec --- /dev/null +++ b/tests/test_Tensor_tanh.py @@ -0,0 +1,33 @@ +# 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.tanh") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.tanh() + """ + ) + obj.run(pytorch_code, ["result"]) + + +test_case_1() diff --git a/tests/test_Tensor_tanh_.py b/tests/test_Tensor_tanh_.py new file mode 100644 index 000000000..4bfb3977d --- /dev/null +++ b/tests/test_Tensor_tanh_.py @@ -0,0 +1,30 @@ +# 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.tanh_") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.Tensor([[1.,2.], [3.,4.]]) + result.tanh_() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_tile.py b/tests/test_Tensor_tile.py new file mode 100644 index 000000000..354619329 --- /dev/null +++ b/tests/test_Tensor_tile.py @@ -0,0 +1,30 @@ +# 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.tile") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.tile((1,)) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_tolist.py b/tests/test_Tensor_tolist.py new file mode 100644 index 000000000..d8c2438b4 --- /dev/null +++ b/tests/test_Tensor_tolist.py @@ -0,0 +1,30 @@ +# 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.tolist") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result =torch.tensor(a.tolist()) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_topk.py b/tests/test_Tensor_topk.py new file mode 100644 index 000000000..c065d88eb --- /dev/null +++ b/tests/test_Tensor_topk.py @@ -0,0 +1,74 @@ +# 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.topk") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.topk(2) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.topk(2, dim=0) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.topk(2, dim=0, largest=False) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.topk(2, dim=0, largest=True) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.topk(2, dim=0, largest=False, sorted=True) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_trace.py b/tests/test_Tensor_trace.py new file mode 100644 index 000000000..900e68833 --- /dev/null +++ b/tests/test_Tensor_trace.py @@ -0,0 +1,30 @@ +# 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.sigmoid") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.trace() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_true_divide.py b/tests/test_Tensor_true_divide.py new file mode 100644 index 000000000..da5e4feda --- /dev/null +++ b/tests/test_Tensor_true_divide.py @@ -0,0 +1,30 @@ +# 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.true_divide") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.true_divide(a) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_trunc.py b/tests/test_Tensor_trunc.py new file mode 100644 index 000000000..77a84d17d --- /dev/null +++ b/tests/test_Tensor_trunc.py @@ -0,0 +1,30 @@ +# 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.trunc") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.trunc() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_unbind.py b/tests/test_Tensor_unbind.py new file mode 100644 index 000000000..bedc0c945 --- /dev/null +++ b/tests/test_Tensor_unbind.py @@ -0,0 +1,41 @@ +# 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.unbind") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.unbind() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.unbind(dim=0) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_uniform_.py b/tests/test_Tensor_uniform_.py new file mode 100644 index 000000000..4c9cf60eb --- /dev/null +++ b/tests/test_Tensor_uniform_.py @@ -0,0 +1,63 @@ +# 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.uniformal_") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.Tensor([[1.,2.], [3.,4.]]) + result.uniform_() + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.Tensor([[1.,2.], [3.,4.]]) + result.uniform_(0, to=1) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.Tensor([[1.,2.], [3.,4.]]) + result.uniform_(0,1) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.Tensor([[1.,2.], [3.,4.]]) + result.uniform_(0, 2) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) diff --git a/tests/test_Tensor_unique_consecutive.py b/tests/test_Tensor_unique_consecutive.py new file mode 100644 index 000000000..8354fdaf8 --- /dev/null +++ b/tests/test_Tensor_unique_consecutive.py @@ -0,0 +1,74 @@ +# 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.sigmoid") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1, 1, 2, 2, 3, 1, 1, 2]) + result = torch.unique_consecutive(x) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1, 1, 2, 2, 3, 1, 1, 2]) + result, inverse_indices = torch.unique_consecutive(x, return_inverse=True) + """ + ) + obj.run(pytorch_code, ["result", "inverse_indices"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1, 1, 2, 2, 3, 1, 1, 2]) + result, counts = torch.unique_consecutive(x, return_counts=True) + """ + ) + obj.run(pytorch_code, ["result", "counts"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1, 1, 2, 2, 3, 1, 1, 2]) + result, inverse_indices, counts = torch.unique_consecutive(x, return_counts=True, return_inverse=True) + """ + ) + obj.run(pytorch_code, ["result", "counts", "inverse_indices"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1, 1, 2, 2, 3, 1, 1, 2]) + result = torch.unique_consecutive(x, dim=0) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_unsqueeze.py b/tests/test_Tensor_unsqueeze.py new file mode 100644 index 000000000..5e73e1d46 --- /dev/null +++ b/tests/test_Tensor_unsqueeze.py @@ -0,0 +1,63 @@ +# 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.unsqueeze") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.zeros(2, 2, 2) + result = x.unsqueeze(0) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.zeros(2, 2, 1, 2).unsqueeze(3) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.zeros(2, 2, 1, 2) + result = x.unsqueeze(dim=-1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.zeros(2, 2, 1, 2) + result = x.unsqueeze(dim=1) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_unsqueeze_.py b/tests/test_Tensor_unsqueeze_.py new file mode 100644 index 000000000..ebf78a3b5 --- /dev/null +++ b/tests/test_Tensor_unsqueeze_.py @@ -0,0 +1,63 @@ +# 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.unsqueeze_") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.zeros(2, 2, 2) + result.unsqueeze_(0) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.zeros(2, 2, 1, 2).unsqueeze_(3) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.zeros(2, 2, 1, 2) + result.unsqueeze_(dim=-1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.zeros(2, 2, 1, 2) + result.unsqueeze_(dim=1) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_var.py b/tests/test_Tensor_var.py new file mode 100644 index 000000000..e261c95b8 --- /dev/null +++ b/tests/test_Tensor_var.py @@ -0,0 +1,109 @@ +# 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.var") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor( + [[ 0.2035, 1.2959, 1.8101, -0.4644], + [ 1.5027, -0.3270, 0.5905, 0.6538], + [-1.5745, 1.3330, -0.5596, -0.6548], + [ 0.1264, -0.5080, 1.6420, 0.1992]]) + result = a.var() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor( + [[ 0.2035, 1.2959, 1.8101, -0.4644], + [ 1.5027, -0.3270, 0.5905, 0.6538], + [-1.5745, 1.3330, -0.5596, -0.6548], + [ 0.1264, -0.5080, 1.6420, 0.1992]]) + result = a.var() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor( + [[ 0.2035, 1.2959, 1.8101, -0.4644], + [ 1.5027, -0.3270, 0.5905, 0.6538], + [-1.5745, 1.3330, -0.5596, -0.6548], + [ 0.1264, -0.5080, 1.6420, 0.1992]]) + result = a.var(dim=1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor( + [[ 0.2035, 1.2959, 1.8101, -0.4644], + [ 1.5027, -0.3270, 0.5905, 0.6538], + [-1.5745, 1.3330, -0.5596, -0.6548], + [ 0.1264, -0.5080, 1.6420, 0.1992]]) + result = a.var(dim=(0, 1)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor( + [[ 0.2035, 1.2959, 1.8101, -0.4644], + [ 1.5027, -0.3270, 0.5905, 0.6538], + [-1.5745, 1.3330, -0.5596, -0.6548], + [ 0.1264, -0.5080, 1.6420, 0.1992]]) + result = a.var(dim=1, keepdim=True) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor( + [[ 0.2035, 1.2959, 1.8101, -0.4644], + [ 1.5027, -0.3270, 0.5905, 0.6538], + [-1.5745, 1.3330, -0.5596, -0.6548], + [ 0.1264, -0.5080, 1.6420, 0.1992]]) + result = a.var(dim=1, unbiased=False, keepdim=True) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_where.py b/tests/test_Tensor_where.py new file mode 100644 index 000000000..4c0ede29a --- /dev/null +++ b/tests/test_Tensor_where.py @@ -0,0 +1,52 @@ +# 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.where") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.where(a>0, 0) + """ + ) + obj.run(pytorch_code, [], reason="torch and paddle where api diff") + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.where(a>0==0, y=0) + """ + ) + obj.run(pytorch_code, [], reason="torch and paddle where api diff") + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.Tensor([[1.,2.], [3.,4.]]) + result = a.where(condition = a>0, y = 0) + """ + ) + obj.run(pytorch_code, [], reason="torch and paddle where api diff") diff --git a/tests/test_Tensor_zero_.py b/tests/test_Tensor_zero_.py new file mode 100644 index 000000000..7592c3611 --- /dev/null +++ b/tests/test_Tensor_zero_.py @@ -0,0 +1,30 @@ +# 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.zero_") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.Tensor([[1.,2.], [3.,4.]]) + result.zero_() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_register_forward_hook.py b/tests/test_register_forward_hook.py new file mode 100644 index 000000000..09a36037d --- /dev/null +++ b/tests/test_register_forward_hook.py @@ -0,0 +1,45 @@ +# 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.register_forward_hook") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + result = [] + class TestForHook(nn.Module): + def __init__(self): + super().__init__() + + self.linear_1 = nn.Linear(in_features=2, out_features=2) + def forward(self, x): + x1 = self.linear_1(x) + return x, x, x1 + def hook(module, fea_in, fea_out): + result.append(1) + + net = TestForHook() + net.register_forward_hook(hook) + a = torch.tensor([0.,0.]) + net(a) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_register_forward_pre_hook.py b/tests/test_register_forward_pre_hook.py new file mode 100644 index 000000000..9fc1c4f0b --- /dev/null +++ b/tests/test_register_forward_pre_hook.py @@ -0,0 +1,45 @@ +# 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.register_forward_pre_hook") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + result = [] + class TestForHook(nn.Module): + def __init__(self): + super().__init__() + + self.linear_1 = nn.Linear(in_features=2, out_features=2) + def forward(self, x): + x1 = self.linear_1(x) + return x, x1 + def hook(module, fea_in): + result.append(1) + + net = TestForHook() + net.register_forward_pre_hook(hook) + a = torch.tensor([0.,0.]) + net(a) + """ + ) + obj.run(pytorch_code, ["result"])