diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index b908a1837..f98ff47bc 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -5910,19 +5910,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.gcd": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.gcd", - "min_input_args": 2, - "args_list": [ - "input", - "other", - "*", - "out" - ], - "kwargs_change": { - "input": "x", - "other": "y" - } + "Matcher": "ChangePrefixMatcher" }, "torch.ge": { "Matcher": "ChangePrefixMatcher" @@ -6458,19 +6446,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.lcm": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.lcm", - "min_input_args": 2, - "args_list": [ - "input", - "other", - "*", - "out" - ], - "kwargs_change": { - "input": "x", - "other": "y" - } + "Matcher": "ChangePrefixMatcher" }, "torch.ldexp": { "Matcher": "GenericMatcher", diff --git a/tests/test_Tensor_t.py b/tests/test_Tensor_t.py index 6c50302f5..7487ecc1f 100644 --- a/tests/test_Tensor_t.py +++ b/tests/test_Tensor_t.py @@ -11,20 +11,31 @@ # 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") +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() + x = torch.arange(16).reshape(4, 4) + result = x.T + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.arange(16).reshape(4, 4).T """ ) obj.run(pytorch_code, ["result"]) diff --git a/tests/test_cuda_stream.py b/tests/test_cuda_stream.py index 0e243c322..17083df5d 100644 --- a/tests/test_cuda_stream.py +++ b/tests/test_cuda_stream.py @@ -18,7 +18,7 @@ import pytest from apibase import APIBase -obj = APIBase("torch.cuda.stream") +obj = APIBase("torch.cuda.Stream") @pytest.mark.skipif( @@ -29,13 +29,8 @@ def test_case_1(): pytorch_code = textwrap.dedent( """ import torch - data1 = torch.ones(size=[20]) - data2 = torch.ones(size=[20]) - - s = torch.cuda.Stream() - context = torch.cuda.stream(stream=s) - with context: - result = data1 + data2 + stream = torch.cuda.Stream() + result = stream.query() """ ) obj.run(pytorch_code, ["result"]) @@ -49,12 +44,8 @@ def test_case_2(): pytorch_code = textwrap.dedent( """ import torch - data1 = torch.ones(size=[20]) - data2 = torch.ones(size=[20]) - - context = torch.cuda.stream(stream=None) - with context: - result = data1 + data2 + stream = torch.cuda.Stream(priority=0) + result = stream.query() """ ) obj.run(pytorch_code, ["result"]) @@ -68,10 +59,8 @@ def test_case_3(): pytorch_code = textwrap.dedent( """ import torch - data1 = torch.ones(size=[50]) - data2 = torch.ones(size=[50]) - with torch.cuda.stream(stream = torch.cuda.Stream()): - result = data1 + data2 + stream = torch.cuda.Stream(priority=-1) + result = stream.query() """ ) obj.run(pytorch_code, ["result"]) @@ -85,10 +74,8 @@ def test_case_4(): pytorch_code = textwrap.dedent( """ import torch - data1 = torch.ones(size=[50]) - data2 = torch.ones(size=[50]) - with torch.cuda.stream(torch.cuda.Stream()): - result = data1 + data2 + stream = torch.cuda.Stream(device=1) + result = stream.query() """ ) obj.run(pytorch_code, ["result"]) @@ -102,11 +89,294 @@ def test_case_5(): pytorch_code = textwrap.dedent( """ import torch - data1 = torch.ones(size=[20]) - data2 = torch.ones(size=[20]) - context = torch.cuda.stream(torch.cuda.Stream()) - with context: - result = data1 + data2 + stream = torch.cuda.Stream(device=1,priority=-1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + stream = torch.cuda.Stream(device='cuda:1',priority=-1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + stream = torch.cuda.Stream(device='cuda',priority=-1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + stream = torch.cuda.Stream(0,-1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_9(): + pytorch_code = textwrap.dedent( + """ + import torch + stream = torch.cuda.Stream('cuda:0',-1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_10(): + pytorch_code = textwrap.dedent( + """ + import torch + cond = True + stream = torch.cuda.Stream(0 if cond else 1, -1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_11(): + pytorch_code = textwrap.dedent( + """ + import torch + cond = False + stream = torch.cuda.Stream('cuda:0' if cond else 'cuda:1', -1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_12(): + pytorch_code = textwrap.dedent( + """ + import torch + device = 0 + stream = torch.cuda.Stream(device,-1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_13(): + pytorch_code = textwrap.dedent( + """ + import torch + device = 'cuda:0' + stream = torch.cuda.Stream(device,-1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_14(): + pytorch_code = textwrap.dedent( + """ + import torch + cond = True + device = 0 if cond else 1 + stream = torch.cuda.Stream(device, -1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_15(): + pytorch_code = textwrap.dedent( + """ + import torch + cond = False + device = 'cuda:0' if cond else 'cuda:1' + stream = torch.cuda.Stream(device, -1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_16(): + pytorch_code = textwrap.dedent( + """ + import torch + stream = torch.cuda.Stream(torch.device(0), -1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_17(): + pytorch_code = textwrap.dedent( + """ + import torch + stream = torch.cuda.Stream(torch.device('cuda:0'), -1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_18(): + pytorch_code = textwrap.dedent( + """ + import torch + cond = True + stream = torch.cuda.Stream(torch.device(0 if cond else 1), -1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_19(): + pytorch_code = textwrap.dedent( + """ + import torch + cond = False + stream = torch.cuda.Stream(torch.device('cuda:0' if cond else 'cuda:1'), -1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_20(): + pytorch_code = textwrap.dedent( + """ + import torch + device = 0 + stream = torch.cuda.Stream(torch.device(device), -1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_21(): + pytorch_code = textwrap.dedent( + """ + import torch + device = 'cuda:0' + stream = torch.cuda.Stream(torch.device(device), -1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_22(): + pytorch_code = textwrap.dedent( + """ + import torch + cond = True + device = 0 if cond else 1 + stream = torch.cuda.Stream(torch.device(device), -1) + result = stream.query() + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skipif( + condition=not paddle.device.is_compiled_with_cuda(), + reason="can only run on paddle with CUDA", +) +def test_case_23(): + pytorch_code = textwrap.dedent( + """ + import torch + cond = False + device = 'cuda:0' if cond else 'cuda:1' + stream = torch.cuda.Stream(torch.device(device), -1) + result = stream.query() """ ) obj.run(pytorch_code, ["result"]) diff --git a/tests/test_gcd.py b/tests/test_gcd.py index 751f17835..9116bdbc5 100644 --- a/tests/test_gcd.py +++ b/tests/test_gcd.py @@ -88,3 +88,133 @@ def test_case_6(): """ ) obj.run(pytorch_code, ["result", "out"]) + + +def test_case_7(): + """Mixed positional and keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([5, 10, 15]) + b = torch.tensor([3, 4, 5]) + result = torch.gcd(a, other=b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """2D tensor input""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[12, 18], [24, 36]]) + b = torch.tensor([[8, 6], [16, 12]]) + result = torch.gcd(a, b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """Negative values""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([-5, 10, -15]) + b = torch.tensor([3, -4, -5]) + result = torch.gcd(a, b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """Broadcasting""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[12, 18, 24]]) + b = torch.tensor([6]) + result = torch.gcd(a, b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_11(): + """Out parameter with positional args""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([5, 10, 15]) + b = torch.tensor([3, 4, 5]) + out = torch.empty(3, dtype=torch.int64) + result = torch.gcd(a, b, out=out) + """ + ) + obj.run(pytorch_code, ["result", "out"]) + + +def test_case_12(): + """3D tensor input""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[[12, 18], [24, 36]], [[8, 14], [20, 28]]]) + b = torch.tensor([[[4, 6], [8, 12]], [[2, 7], [10, 14]]]) + result = torch.gcd(a, b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_13(): + """int32 dtype""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([5, 10, 15], dtype=torch.int32) + b = torch.tensor([3, 4, 5], dtype=torch.int32) + result = torch.gcd(a, b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_14(): + """Variable arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([12, 18, 24]) + y = torch.tensor([8, 6, 16]) + result = torch.gcd(x, y) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_15(): + """Zero values""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0, 10, 0]) + b = torch.tensor([5, 0, 0]) + result = torch.gcd(a, b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_16(): + """Expression as argument""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([5, 10, 15]) + result = torch.gcd(a, torch.tensor([3, 4, 5])) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_lcm.py b/tests/test_lcm.py index b30804eaa..3a6565212 100644 --- a/tests/test_lcm.py +++ b/tests/test_lcm.py @@ -100,3 +100,133 @@ def test_case_7(): """ ) obj.run(pytorch_code, ["result", "out"]) + + +def test_case_8(): + """Mixed positional and keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([5, 10, 15]) + b = torch.tensor([3, 4, 5]) + result = torch.lcm(a, other=b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """2D tensor input""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[12, 18], [24, 36]]) + b = torch.tensor([[8, 6], [16, 12]]) + result = torch.lcm(a, b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """Negative values""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([-5, 10, -15]) + b = torch.tensor([3, -4, -5]) + result = torch.lcm(a, b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_11(): + """Broadcasting""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[12, 18, 24]]) + b = torch.tensor([6]) + result = torch.lcm(a, b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_12(): + """Out parameter with positional args""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([5, 10, 15]) + b = torch.tensor([3, 4, 5]) + out = torch.empty(3, dtype=torch.int64) + result = torch.lcm(a, b, out=out) + """ + ) + obj.run(pytorch_code, ["result", "out"]) + + +def test_case_13(): + """3D tensor input""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[[12, 18], [24, 36]], [[8, 14], [20, 28]]]) + b = torch.tensor([[[4, 6], [8, 12]], [[2, 7], [10, 14]]]) + result = torch.lcm(a, b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_14(): + """int32 dtype""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([5, 10, 15], dtype=torch.int32) + b = torch.tensor([3, 4, 5], dtype=torch.int32) + result = torch.lcm(a, b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_15(): + """Variable arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([12, 18, 24]) + y = torch.tensor([8, 6, 16]) + result = torch.lcm(x, y) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_16(): + """Zero values""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0, 10, 0]) + b = torch.tensor([5, 0, 0]) + result = torch.lcm(a, b) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_17(): + """Expression as argument""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([5, 10, 15]) + result = torch.lcm(a, torch.tensor([3, 4, 5])) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_tensor.py b/tests/test_tensor.py index f69f798ee..9a8b92b87 100644 --- a/tests/test_tensor.py +++ b/tests/test_tensor.py @@ -14,77 +14,73 @@ import textwrap -import paddle -import pytest from apibase import APIBase -obj = APIBase("torch.tensor") +obj = APIBase("torch.Tensor") def test_case_1(): pytorch_code = textwrap.dedent( """ import torch - result = torch.tensor([2, 3]) + result = torch.Tensor(2, 3) """ ) - obj.run(pytorch_code, ["result"]) + obj.run(pytorch_code, ["result"], check_value=False) def test_case_2(): pytorch_code = textwrap.dedent( """ import torch - data = [2, 3] - result = torch.tensor(data) + shape = [2, 3] + result = torch.Tensor(*shape) """ ) - obj.run(pytorch_code, ["result"]) + obj.run(pytorch_code, ["result"], check_value=False) def test_case_3(): pytorch_code = textwrap.dedent( """ import torch - data = [2, 3] - result = torch.tensor(data, dtype=torch.float) + dim1, dim2 = 2, 3 + result = torch.Tensor(dim1, dim2) """ ) - obj.run(pytorch_code, ["result"]) + obj.run(pytorch_code, ["result"], check_value=False) def test_case_4(): pytorch_code = textwrap.dedent( """ import torch - data = [2, 3] - result = torch.tensor(data, dtype=torch.float, device=None) + def fun(x: torch.Tensor): + return x * 2 + + a = torch.Tensor(3, 4) + result = fun(a) """ ) - obj.run(pytorch_code, ["result"]) + obj.run(pytorch_code, ["result"], check_value=False) def test_case_5(): pytorch_code = textwrap.dedent( """ import torch - data = [2, 3] - result = torch.tensor(data, dtype=torch.float, device=None, requires_grad = False) + result = torch.Tensor([[3, 4], [5, 8]]) """ ) obj.run(pytorch_code, ["result"]) -@pytest.mark.skipif( - condition=not paddle.device.is_compiled_with_cuda(), - reason="can only run on paddle with CUDA", -) def test_case_6(): pytorch_code = textwrap.dedent( """ import torch - data = [2, 3] - result = torch.tensor(data, requires_grad=False, pin_memory=True) + a = torch.tensor([[3, 4], [5, 8]]) + result = torch.Tensor(a) """ ) obj.run(pytorch_code, ["result"]) @@ -94,8 +90,7 @@ def test_case_7(): pytorch_code = textwrap.dedent( """ import torch - data = [2, 3] - result = torch.tensor(data, requires_grad = False, pin_memory=False) + result = torch.Tensor((1, 2, 3)) """ ) obj.run(pytorch_code, ["result"]) @@ -105,19 +100,7 @@ def test_case_8(): pytorch_code = textwrap.dedent( """ import torch - data = [2, 3] - result = torch.tensor(data=data, dtype=torch.float, device=None, requires_grad=False, pin_memory=False) - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_9(): - pytorch_code = textwrap.dedent( - """ - import torch - data = [2, 3] - result = torch.tensor(device=None, dtype=torch.float, pin_memory=False, data=data, requires_grad=False) + result = torch.Tensor() """ ) obj.run(pytorch_code, ["result"]) diff --git a/tests/test_tensor_split.py b/tests/test_tensor_split.py index 4fe947c03..5be698bea 100644 --- a/tests/test_tensor_split.py +++ b/tests/test_tensor_split.py @@ -1,4 +1,4 @@ -# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. +# 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. @@ -12,20 +12,19 @@ # See the License for the specific language governing permissions and # limitations under the License. # - import textwrap from apibase import APIBase -obj = APIBase("torch.tensor_split") +obj = APIBase("torch.Tensor.split") def test_case_1(): pytorch_code = textwrap.dedent( """ import torch - a = torch.arange(8) - result = torch.tensor_split(a, 3) + a = torch.arange(80).reshape(8, 10) + result = a.split(4) """ ) obj.run(pytorch_code, ["result"]) @@ -35,8 +34,8 @@ def test_case_2(): pytorch_code = textwrap.dedent( """ import torch - a = torch.arange(7) - result = torch.tensor_split(a, sections = 3) + a = torch.arange(80).reshape(8, 10) + result = a.split(4, 0) """ ) obj.run(pytorch_code, ["result"]) @@ -46,8 +45,8 @@ def test_case_3(): pytorch_code = textwrap.dedent( """ import torch - a = torch.arange(7) - result = torch.tensor_split(a, (1, 6)) + a = torch.arange(80).reshape(8, 10) + result = a.split(split_size=2, dim=1) """ ) obj.run(pytorch_code, ["result"]) @@ -57,8 +56,8 @@ def test_case_4(): pytorch_code = textwrap.dedent( """ import torch - a = torch.arange(7) - result = torch.tensor_split(a, indices = (1, 6)) + a = torch.arange(80).reshape(8, 10) + result = a.split(split_size = [2, 6], dim = 0) """ ) obj.run(pytorch_code, ["result"]) @@ -68,8 +67,9 @@ def test_case_5(): pytorch_code = textwrap.dedent( """ import torch - a = torch.arange(7) - result = torch.tensor_split(a, [1, 6]) + a = torch.arange(80).reshape(8, 10) + b = 2 + result = a.split(b, dim=1) """ ) obj.run(pytorch_code, ["result"]) @@ -79,8 +79,9 @@ def test_case_6(): pytorch_code = textwrap.dedent( """ import torch - a = torch.arange(7) - result = torch.tensor_split(a, indices = [1, 6]) + a = torch.arange(80).reshape(8, 10) + b = 2 + result = a.split(b, dim = 0) """ ) obj.run(pytorch_code, ["result"]) @@ -90,118 +91,18 @@ def test_case_7(): pytorch_code = textwrap.dedent( """ import torch - a = torch.arange(14).reshape(2, 7) - result = torch.tensor_split(a, 3, dim = 1) - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_8(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.arange(14).reshape(2, 7) - result = torch.tensor_split(a, (1, 6), dim = 1) - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_9(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.arange(14).reshape(2, 7) - result = torch.tensor_split(a, 3, 1) - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_10(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.arange(14).reshape(2, 7) - result = torch.tensor_split(input=a, sections=3, dim=1) - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_11(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.arange(14).reshape(2, 7) - result = torch.tensor_split(sections=3, input=a, dim=1) - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_12(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.arange(7) - result = torch.tensor_split(input=a, indices = (1, 6), dim=-1) + a = torch.tensor([1, 2, 3]) # use the imported torch to avoid a useless import + str1 = '1,2,3' + str1.split(',') """ ) - obj.run(pytorch_code, ["result"]) - - -def test_case_13(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.arange(7) - result = torch.tensor_split(indices = (1, 6), input=a, dim=-1) + expect_code = textwrap.dedent( """ - ) - obj.run(pytorch_code, ["result"]) + import paddle - -def test_case_14(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.arange(7) - result = torch.tensor_split(input=a, indices = (1, 6), dim=-1) + a = paddle.tensor([1, 2, 3]) + str1 = "1,2,3" + str1.split(",") """ ) - obj.run(pytorch_code, ["result"]) - - -def test_case_15(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.arange(7) - result = torch.tensor_split(indices = (1, 6), input=a, dim=-1) - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_16(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.arange(14).reshape(2, 7) - result = torch.tensor_split(input=a, sections=3, dim=1) - """ - ) - obj.run(pytorch_code, ["result"]) - - -def test_case_17(): - pytorch_code = textwrap.dedent( - """ - import torch - a = torch.arange(14).reshape(2, 7) - result = torch.tensor_split(sections=3, input=a, dim=1) - """ - ) - obj.run(pytorch_code, ["result"]) + obj.run(pytorch_code, expect_paddle_code=expect_code) diff --git a/tests/torchvision_tests/test_normalize.py b/tests/torchvision_tests/test_normalize.py index a2a8aa53f..c334f6110 100644 --- a/tests/torchvision_tests/test_normalize.py +++ b/tests/torchvision_tests/test_normalize.py @@ -17,17 +17,16 @@ from apibase import APIBase from torchvision_tests.image_apibase import ImageAPIBase -obj = APIBase("torchvision.transforms.functional.normalize") -img_obj = ImageAPIBase("torchvision.transforms.functional.normalize") +obj = APIBase("torchvision.transforms.Normalize") +img_obj = ImageAPIBase("torchvision.transforms.Normalize") def test_case_1(): pytorch_code = textwrap.dedent( """ import torch - import torchvision.transforms.functional as F - mean = 0.5, 0.5, 0.5 - std = [0.5, 0.5, 0.5] + import torchvision.transforms as transforms + normalize = transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) img = torch.tensor([ [[0.5, 0.5], [0.5, 0.5]], @@ -36,7 +35,7 @@ def test_case_1(): [[0.5, 0.5], [0.5, 0.5]] ]) - result = F.normalize(img, mean, std) + result = normalize(img) """ ) obj.run(pytorch_code, ["result"]) @@ -46,12 +45,22 @@ def test_case_2(): pytorch_code = textwrap.dedent( """ import torch - import torchvision.transforms.functional as F + import torchvision.transforms as transforms + mean = [0.0, 0.0, 0.0] + std = [1.0, 1.0, 1.0] + normalize = transforms.Normalize(std=std, mean=mean) img = torch.tensor([ - [[0.2, 0.4], - [0.6, 0.8]] + [[1.0, 2.0, 3.0], + [4.0, 5.0, 6.0], + [7.0, 8.0, 9.0]], + [[10.0, 11.0, 12.0], + [13.0, 14.0, 15.0], + [16.0, 17.0, 18.0]], + [[19.0, 20.0, 21.0], + [22.0, 23.0, 24.0], + [25.0, 26.0, 27.0]] ]) - result = F.normalize(tensor=img, mean=0.5, std=[0.5]) + result = normalize(img) """ ) obj.run(pytorch_code, ["result"]) @@ -61,9 +70,8 @@ def test_case_3(): pytorch_code = textwrap.dedent( """ import torch - import torchvision.transforms.functional as F - mean = [0.485, 0.456, 0.406] - std = [0.229, 0.224, 0.225] + import torchvision.transforms as transforms + normalize = transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) img = torch.tensor([ [ [[0.5, 0.5, 0.5, 0.5], @@ -94,7 +102,7 @@ def test_case_3(): [0.8, 0.8, 0.8, 0.8]] ] ]) - result = F.normalize(tensor=img, std=std, mean=mean) + result = normalize(img) """ ) obj.run(pytorch_code, ["result"]) diff --git a/tests/torchvision_tests/test_pad.py b/tests/torchvision_tests/test_pad.py index b02b2f804..5c76ce4eb 100644 --- a/tests/torchvision_tests/test_pad.py +++ b/tests/torchvision_tests/test_pad.py @@ -17,18 +17,15 @@ from apibase import APIBase from torchvision_tests.image_apibase import ImageAPIBase -obj = APIBase("torchvision.transforms.functional.pad") -img_obj = ImageAPIBase("torchvision.transforms.functional.pad") +obj = APIBase("torchvision.transforms.Pad") +img_obj = ImageAPIBase("torchvision.transforms.Pad") def test_case_1(): pytorch_code = textwrap.dedent( """ import torch - import torchvision.transforms.functional as F - padding = 2 - fill = 0 - padding_mode = 'constant' + from torchvision.transforms import Pad img = torch.tensor([ [[1, 2], [3, 4]], @@ -37,7 +34,8 @@ def test_case_1(): [[9, 10], [11, 12]] ], dtype=torch.float) - result = F.pad(img=img, padding=padding, fill=fill, padding_mode=padding_mode) + pad = Pad(padding=2, fill=0, padding_mode='constant') + result = pad(img) """ ) obj.run(pytorch_code, ["result"]) @@ -47,7 +45,10 @@ def test_case_2(): pytorch_code = textwrap.dedent( """ import torch - import torchvision.transforms.functional as F + from torchvision.transforms import Pad + padding = [1, 2, 3, 4] + fill = 1.0 + padding_mode = 'constant' img = torch.tensor([ [[1, 2, 3], [4, 5, 6], @@ -59,7 +60,8 @@ def test_case_2(): [22, 23, 24], [25, 26, 27]] ], dtype=torch.float) - result = F.pad(img, padding=[1, 2, 3, 4], fill=1.0, padding_mode='constant') + pad = Pad(padding, fill, padding_mode) + result = pad(img) """ ) obj.run(pytorch_code, ["result"]) @@ -69,12 +71,13 @@ def test_case_3(): pytorch_code = textwrap.dedent( """ from PIL import Image - import torchvision.transforms.functional as F + from torchvision.transforms import Pad padding = [2, 3] fill = (255, 0, 0) padding_mode = 'constant' img = Image.new('RGB', (2, 2), color=(0, 255, 0)) - result = F.pad(padding=padding, fill=fill, padding_mode=padding_mode, img=img) + pad = Pad(padding=padding, fill=fill, padding_mode=padding_mode) + result = pad(img) """ ) img_obj.run(pytorch_code, ["result"]) @@ -84,7 +87,7 @@ def test_case_4(): pytorch_code = textwrap.dedent( """ from PIL import Image - import torchvision.transforms.functional as F + from torchvision.transforms import Pad padding = 1 padding_mode = 'reflect' img = Image.new('L', (3, 3)) @@ -97,7 +100,8 @@ def test_case_4(): img.putpixel((0, 2), 150) img.putpixel((1, 2), 200) img.putpixel((2, 2), 250) - result = F.pad(img, padding, padding_mode=padding_mode) + pad = Pad(padding=padding, padding_mode=padding_mode) + result = pad(img) """ ) img_obj.run(pytorch_code, ["result"]) @@ -107,14 +111,15 @@ def test_case_5(): pytorch_code = textwrap.dedent( """ from PIL import Image - import torchvision.transforms.functional as F + from torchvision.transforms import Pad padding = [1, 1, 1, 1] fill = (0, 0, 255, 128) padding_mode = 'symmetric' img = Image.new('RGBA', (5, 5), color=(0, 0, 255, 128)) img.putpixel((0, 0), (255, 0, 0, 255)) img.putpixel((4, 4), (0, 255, 0, 255)) - result = F.pad(img, padding, fill, padding_mode) + pad = Pad(padding=padding, fill=fill, padding_mode=padding_mode) + result = pad(img) """ ) img_obj.run(pytorch_code, ["result"]) diff --git a/tests/torchvision_tests/test_resize.py b/tests/torchvision_tests/test_resize.py index f97be6197..da7fd37a2 100644 --- a/tests/torchvision_tests/test_resize.py +++ b/tests/torchvision_tests/test_resize.py @@ -17,23 +17,22 @@ from apibase import APIBase from torchvision_tests.image_apibase import ImageAPIBase -obj = APIBase("torchvision.transforms.functional.resize") -img_obj = ImageAPIBase("torchvision.transforms.functional.resize") +obj = APIBase("torchvision.transforms.Resize") +img_obj = ImageAPIBase("torchvision.transforms.Resize") def test_case_1(): pytorch_code = textwrap.dedent( """ import torch - from torchvision.transforms import InterpolationMode - from torchvision.transforms.functional import resize + from torchvision.transforms import Resize, InterpolationMode from PIL import Image torch.manual_seed(1) - size = (3, 3) + resize = Resize((3, 3), InterpolationMode.BILINEAR) img = Image.new('RGB', (4, 4), color=(255, 255, 255)) img.putpixel((0, 0), (255, 0, 0)) img.putpixel((3, 3), (0, 255, 0)) - result = resize(img=img, size=size, interpolation=InterpolationMode.BILINEAR) + result = resize(img) """ ) img_obj.run(pytorch_code, ["result"]) @@ -43,10 +42,11 @@ def test_case_2(): pytorch_code = textwrap.dedent( """ import torch - from torchvision.transforms import InterpolationMode - from torchvision.transforms.functional import resize + from torchvision.transforms import Resize, InterpolationMode from PIL import Image torch.manual_seed(3) + size = 3 + resize = Resize(size=size, interpolation=InterpolationMode.BICUBIC) img = Image.new('L', (3, 3)) img.putpixel((0, 0), 50) img.putpixel((1, 0), 100) @@ -57,7 +57,7 @@ def test_case_2(): img.putpixel((0, 2), 150) img.putpixel((1, 2), 200) img.putpixel((2, 2), 250) - result = resize(img, size=3, interpolation=InterpolationMode.BICUBIC) + result = resize(img) """ ) img_obj.run(pytorch_code, ["result"]) @@ -67,10 +67,10 @@ def test_case_3(): pytorch_code = textwrap.dedent( """ import torch - from torchvision.transforms import InterpolationMode - from torchvision.transforms.functional import resize + from torchvision.transforms import Resize, InterpolationMode torch.manual_seed(4) size = [4, 4] + resize = Resize(interpolation=InterpolationMode.NEAREST, size=size) img = torch.tensor([ [ [[1, 2, 3, 4, 5], @@ -91,7 +91,7 @@ def test_case_3(): ] ], dtype=torch.float) img = img[0] - result = resize(img=img, size=size, interpolation=InterpolationMode.NEAREST) + result = resize(img) """ ) obj.run(pytorch_code, ["result"]) @@ -101,15 +101,15 @@ def test_case_4(): pytorch_code = textwrap.dedent( """ import torch - from torchvision.transforms import InterpolationMode - from torchvision.transforms.functional import resize + from torchvision.transforms import Resize, InterpolationMode from PIL import Image torch.manual_seed(5) size = (4, 4) + resize = Resize(size=size, interpolation=InterpolationMode.BICUBIC) img = Image.new('RGBA', (6, 6), color=(0, 0, 255, 128)) img.putpixel((0, 0), (255, 0, 0, 255)) img.putpixel((5, 5), (0, 255, 0, 255)) - result = resize(img, size, InterpolationMode.BICUBIC) + result = resize(img) """ ) img_obj.run(pytorch_code, ["result"])