diff --git a/paconvert/global_var.py b/paconvert/global_var.py index bb5771e5f..0043bf718 100644 --- a/paconvert/global_var.py +++ b/paconvert/global_var.py @@ -34,10 +34,6 @@ class GlobalManager: with open(json_file, "r") as file: ALIAS_MAPPING = json.load(file) - json_file = os.path.dirname(__file__) + "/api_alias_mapping.json" - with open(json_file, "r") as file: - ALIAS_MAPPING = json.load(file) - # used to replace import (means replace api by all) IMPORT_PACKAGE_MAPPING = { "audiotools": "paddlespeech.audiotools", @@ -78,6 +74,23 @@ class GlobalManager: # 完全对齐的Pytorch API名单 NO_NEED_CONVERT_LIST = [ + # Edit by AI Agent + "torch.fmax", + "torch.fmin", + "torch.bincount", + "torch.diag", + "torch.atan", + "torch.tan", + "torch.bitwise_and", + "torch.bitwise_not", + "torch.bitwise_xor", + "torch.nextafter", + "torch.angle", + "torch.heaviside", + "torch.asinh", + "torch.reciprocal", + "torch.square", + # Manfredss "torch.asin", @@ -978,7 +991,6 @@ class GlobalManager: # algorithm1832 "torch.cosh", "torch.frac", - "torch.diag", "torch.Tensor.diag", # lijialin diff --git a/tests/test_angle.py b/tests/test_angle.py index 3d0247f66..6a0e40948 100644 --- a/tests/test_angle.py +++ b/tests/test_angle.py @@ -74,3 +74,63 @@ def test_case_5(): """ ) obj.run(pytorch_code, ["result", "out"]) + + +def test_case_6(): + """2D复数张量""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[1+1j, 2+2j], [3+3j, 4+4j]]) + result = torch.angle(x) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """3D复数张量""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[[1+1j, 2+2j], [3+3j, 4+4j]], [[5+5j, 6+6j], [7+7j, 8+8j]]]) + result = torch.angle(x) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """实数输入""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1.0, 2.0, 3.0]) + result = torch.angle(x) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """零值复数""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([0+0j, 1+0j, 0+1j]) + result = torch.angle(x) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """不同数据类型 - float64""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1+1j, 2+2j, 3+3j], dtype=torch.complex128) + result = torch.angle(x) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_asinh.py b/tests/test_asinh.py index 4c81f8f69..58743de07 100644 --- a/tests/test_asinh.py +++ b/tests/test_asinh.py @@ -75,3 +75,65 @@ def test_case_5(): """ ) obj.run(pytorch_code, ["out", "result"]) + + +def test_case_6(): + """2D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[0.1606, -1.4267], [-1.0899, -1.0250]]) + result = torch.asinh(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """3D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[[0.1, -1.0], [-1.5, 2.0]], [[3.0, -4.0], [5.0, -6.0]]]) + result = torch.asinh(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """边界值 - 零值和大值""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.0, 1.0, -1.0, 10.0, -10.0]) + result = torch.asinh(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """不同数据类型 - float64""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.1606, -1.4267, -1.0899], dtype=torch.float64) + result = torch.asinh(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """梯度计算测试""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.5, 1.0, 2.0], requires_grad=True) + y = torch.asinh(a) + y.sum().backward() + a_grad = a.grad + """ + ) + obj.run(pytorch_code, ["y", "a_grad"], check_stop_gradient=False) diff --git a/tests/test_atan.py b/tests/test_atan.py index d8c128953..c24616be4 100644 --- a/tests/test_atan.py +++ b/tests/test_atan.py @@ -75,3 +75,65 @@ def test_case_5(): """ ) obj.run(pytorch_code, ["out", "result"]) + + +def test_case_6(): + """2D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[0.2341, 0.2539], [-0.6256, -0.6448]]) + result = torch.atan(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """3D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[[0.1, 0.2], [0.3, 0.4]], [[0.5, 0.6], [0.7, 0.8]]]) + result = torch.atan(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """边界值 - 零值和无穷大""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.0, 1.0, -1.0, 100.0, -100.0]) + result = torch.atan(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """不同数据类型 - float64""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.2341, 0.2539, -0.6256], dtype=torch.float64) + result = torch.atan(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """梯度计算测试""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.5, 1.0, 2.0], requires_grad=True) + y = torch.atan(a) + y.sum().backward() + a_grad = a.grad + """ + ) + obj.run(pytorch_code, ["y", "a_grad"], check_stop_gradient=False) diff --git a/tests/test_bincount.py b/tests/test_bincount.py index 38e1fa128..347f09423 100644 --- a/tests/test_bincount.py +++ b/tests/test_bincount.py @@ -104,3 +104,52 @@ def test_case_7(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """边界值 - 空输入""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([], dtype=torch.int64) + result = torch.bincount(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """边界值 - 所有索引相同""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([2, 2, 2, 2]) + result = torch.bincount(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """不同数据类型 - float64""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([0, 1, 2, 1, 0]) + weights = torch.tensor([1.5, 2.5, 3.5, 4.5, 5.5], dtype=torch.float64) + result = torch.bincount(input, weights=weights) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_11(): + """不同数据类型 - int64输入""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([0, 1, 2, 1, 0], dtype=torch.int64) + result = torch.bincount(input) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_bitwise_and.py b/tests/test_bitwise_and.py index b90adc2dd..91bdaf6a0 100644 --- a/tests/test_bitwise_and.py +++ b/tests/test_bitwise_and.py @@ -102,3 +102,145 @@ def test_case_7(): """ ) obj.run(pytorch_code, ["result", "out"]) + + +def test_case_8(): + """测试不同数据类型:int16""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([15, 7, 3], dtype=torch.int16) + other = torch.tensor([9, 5, 12], dtype=torch.int16) + result = torch.bitwise_and(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """测试不同数据类型:int32""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([255, 127, 63], dtype=torch.int32) + other = torch.tensor([170, 85, 42], dtype=torch.int32) + result = torch.bitwise_and(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """测试不同数据类型:int64""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([1023, 511, 255], dtype=torch.int64) + other = torch.tensor([682, 341, 170], dtype=torch.int64) + result = torch.bitwise_and(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_11(): + """测试不同维度:2D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1, 2], [3, 4]], dtype=torch.int8) + other = torch.tensor([[5, 6], [7, 8]], dtype=torch.int8) + result = torch.bitwise_and(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_12(): + """测试不同维度:3D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=torch.int8) + other = torch.tensor([[[9, 10], [11, 12]], [[13, 14], [15, 16]]], dtype=torch.int8) + result = torch.bitwise_and(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_13(): + """测试广播机制""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.int8) + other = torch.tensor([7, 8, 9], dtype=torch.int8) + result = torch.bitwise_and(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_14(): + """测试混合参数顺序""" + pytorch_code = textwrap.dedent( + """ + import torch + other = torch.tensor([1, 0, 3], dtype=torch.int8) + result = torch.bitwise_and(other=other, input=torch.tensor([-1, -2, 3], dtype=torch.int8)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_15(): + """测试变量传参""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([True, False, True]) + other = torch.tensor([False, True, True]) + args = (input, other) + result = torch.bitwise_and(*args) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def _test_case_16(): + """测试标量输入 - Paddle不支持标量作为第二个参数""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([15, 7, 3], dtype=torch.int8) + result = torch.bitwise_and(input, 5) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_17(): + """测试空张量""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.empty(0, dtype=torch.int8) + other = torch.empty(0, dtype=torch.int8) + result = torch.bitwise_and(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_18(): + """测试表达式参数""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([15, 7, 3], dtype=torch.int8) + other = torch.tensor([9, 5, 12], dtype=torch.int8) + result = torch.bitwise_and(input, other + 1) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_bitwise_not.py b/tests/test_bitwise_not.py index 209d033e4..d05be844e 100644 --- a/tests/test_bitwise_not.py +++ b/tests/test_bitwise_not.py @@ -88,3 +88,123 @@ def test_case_6(): """ ) obj.run(pytorch_code, ["result", "out"]) + + +def test_case_7(): + """测试不同数据类型:int16""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([15, 7, 3], dtype=torch.int16) + result = torch.bitwise_not(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """测试不同数据类型:int32""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([255, 127, 63], dtype=torch.int32) + result = torch.bitwise_not(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """测试不同数据类型:int64""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([1023, 511, 255], dtype=torch.int64) + result = torch.bitwise_not(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """测试不同维度:2D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1, 2], [3, 4]], dtype=torch.int8) + result = torch.bitwise_not(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_11(): + """测试不同维度:3D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=torch.int8) + result = torch.bitwise_not(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_12(): + """测试混合参数顺序""" + pytorch_code = textwrap.dedent( + """ + import torch + result = torch.bitwise_not(input=torch.tensor([-1, -2, 3], dtype=torch.int8)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_13(): + """测试变量传参""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([True, False, True]) + args = (input,) + result = torch.bitwise_not(*args) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_14(): + """测试空张量""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.empty(0, dtype=torch.int8) + result = torch.bitwise_not(input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_15(): + """测试表达式参数""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([15, 7, 3], dtype=torch.int8) + result = torch.bitwise_not(input + 1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_16(): + """测试无符号整数类型""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([15, 7, 3], dtype=torch.uint8) + result = torch.bitwise_not(input) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_bitwise_xor.py b/tests/test_bitwise_xor.py index c953573f0..a3a973d9b 100644 --- a/tests/test_bitwise_xor.py +++ b/tests/test_bitwise_xor.py @@ -103,3 +103,158 @@ def test_case_7(): """ ) obj.run(pytorch_code, ["result", "out"]) + + +def test_case_8(): + """测试不同数据类型:int16""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([15, 7, 3], dtype=torch.int16) + other = torch.tensor([9, 5, 12], dtype=torch.int16) + result = torch.bitwise_xor(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """测试不同数据类型:int32""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([255, 127, 63], dtype=torch.int32) + other = torch.tensor([170, 85, 42], dtype=torch.int32) + result = torch.bitwise_xor(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """测试不同数据类型:int64""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([1023, 511, 255], dtype=torch.int64) + other = torch.tensor([682, 341, 170], dtype=torch.int64) + result = torch.bitwise_xor(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_11(): + """测试不同维度:2D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1, 2], [3, 4]], dtype=torch.int8) + other = torch.tensor([[5, 6], [7, 8]], dtype=torch.int8) + result = torch.bitwise_xor(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_12(): + """测试不同维度:3D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=torch.int8) + other = torch.tensor([[[9, 10], [11, 12]], [[13, 14], [15, 16]]], dtype=torch.int8) + result = torch.bitwise_xor(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_13(): + """测试广播机制""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.int8) + other = torch.tensor([7, 8, 9], dtype=torch.int8) + result = torch.bitwise_xor(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_14(): + """测试混合参数顺序""" + pytorch_code = textwrap.dedent( + """ + import torch + other = torch.tensor([1, 0, 3], dtype=torch.int8) + result = torch.bitwise_xor(other=other, input=torch.tensor([-1, -2, 3], dtype=torch.int8)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_15(): + """测试变量传参""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([True, False, True]) + other = torch.tensor([False, True, True]) + args = (input, other) + result = torch.bitwise_xor(*args) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def _test_case_16(): + """测试标量输入 - Paddle不支持标量作为第二个参数""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([15, 7, 3], dtype=torch.int8) + result = torch.bitwise_xor(input, 5) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_17(): + """测试空张量""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.empty(0, dtype=torch.int8) + other = torch.empty(0, dtype=torch.int8) + result = torch.bitwise_xor(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_18(): + """测试表达式参数""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([15, 7, 3], dtype=torch.int8) + other = torch.tensor([9, 5, 12], dtype=torch.int8) + result = torch.bitwise_xor(input, other + 1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_19(): + """测试无符号整数类型""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([15, 7, 3], dtype=torch.uint8) + other = torch.tensor([9, 5, 12], dtype=torch.uint8) + result = torch.bitwise_xor(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_diag.py b/tests/test_diag.py index 7c1dd01af..7f4e57119 100644 --- a/tests/test_diag.py +++ b/tests/test_diag.py @@ -96,3 +96,55 @@ def test_case_6(): """ ) obj.run(pytorch_code, ["result", "out"]) + + +def test_case_7(): + """不同数据类型 - float64""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([0.5950, -0.0872, 2.3298], dtype=torch.float64) + result = torch.diag(x) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """不同对角线偏移 - 正偏移1""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12]]) + result = torch.diag(x, 1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """不同对角线偏移 - 负偏移1""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12]]) + result = torch.diag(x, -1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """边界值 - 从向量构建对角矩阵(带偏移)""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1.0, 2.0, 3.0]) + result = torch.diag(x, 2) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nextafter.py b/tests/test_nextafter.py index 7d13b6299..c07c1d74e 100644 --- a/tests/test_nextafter.py +++ b/tests/test_nextafter.py @@ -21,21 +21,25 @@ def test_case_1(): + """基础用法 - 1D张量""" pytorch_code = textwrap.dedent( """ import torch - result = torch.nextafter(torch.tensor([1.0, 2.0]), torch.tensor([2.0, 1.0])) + input = torch.tensor([1.0, 2.0, 3.0]) + other = torch.tensor([1.1, 2.1, 3.1]) + result = torch.nextafter(input, other) """ ) obj.run(pytorch_code, ["result"]) def test_case_2(): + """位置参数""" pytorch_code = textwrap.dedent( """ import torch - a = torch.tensor([1.0, 2.0]) - b = torch.tensor([2.0, 1.0]) + a = torch.tensor([0.0, 1.0, 2.0]) + b = torch.tensor([0.5, 1.5, 2.5]) result = torch.nextafter(a, b) """ ) @@ -43,41 +47,106 @@ def test_case_2(): def test_case_3(): + """关键字参数""" pytorch_code = textwrap.dedent( """ import torch - a = [1.0, 2.0] - b = [2.0, 1.0] - out = torch.tensor(a) - result = torch.nextafter(torch.tensor(a), torch.tensor(b), out=out) + a = torch.tensor([0.0, 1.0, 2.0]) + b = torch.tensor([0.5, 1.5, 2.5]) + result = torch.nextafter(input=a, other=b) """ ) - obj.run(pytorch_code, ["result", "out"]) + obj.run(pytorch_code, ["result"]) -# generated by validate_unittest autofix, based on test_case_3 def test_case_4(): + """关键字参数乱序""" pytorch_code = textwrap.dedent( """ import torch - a = [1.0, 2.0] - b = [2.0, 1.0] - out = torch.tensor(a) - result = torch.nextafter(input=torch.tensor(a), other=torch.tensor(b), out=out) + a = torch.tensor([0.0, 1.0, 2.0]) + b = torch.tensor([0.5, 1.5, 2.5]) + result = torch.nextafter(other=b, input=a) """ ) - obj.run(pytorch_code, ["result", "out"]) + obj.run(pytorch_code, ["result"]) -# generated by validate_unittest autofix, based on test_case_3 def test_case_5(): + """out参数""" pytorch_code = textwrap.dedent( """ import torch - a = [1.0, 2.0] - b = [2.0, 1.0] - out = torch.tensor(a) - result = torch.nextafter(out=out, other=torch.tensor(b), input=torch.tensor(a)) + input = torch.tensor([1.0, 2.0, 3.0]) + other = torch.tensor([1.1, 2.1, 3.1]) + out = torch.empty_like(input) + result = torch.nextafter(input, other, out=out) """ ) - obj.run(pytorch_code, ["result", "out"]) + obj.run(pytorch_code, ["out", "result"]) + + +def test_case_6(): + """out参数 - 关键字形式""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([1.0, 2.0, 3.0]) + other = torch.tensor([1.1, 2.1, 3.1]) + out = torch.empty_like(input) + result = torch.nextafter(input, other, out=out) + """ + ) + obj.run(pytorch_code, ["out", "result"]) + + +def test_case_7(): + """2D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1.0, 2.0], [3.0, 4.0]]) + other = torch.tensor([[1.1, 2.1], [3.1, 4.1]]) + result = torch.nextafter(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """3D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]) + other = torch.tensor([[[1.1, 2.1], [3.1, 4.1]], [[5.1, 6.1], [7.1, 8.1]]]) + result = torch.nextafter(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """边界值 - 零值""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([0.0, -0.0, 1.0]) + other = torch.tensor([0.1, -0.1, 1.1]) + result = torch.nextafter(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """不同数据类型 - float64""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([1.0, 2.0], dtype=torch.float64) + other = torch.tensor([1.1, 2.1], dtype=torch.float64) + result = torch.nextafter(input, other) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_reciprocal.py b/tests/test_reciprocal.py index 56870de27..8d2b4513e 100644 --- a/tests/test_reciprocal.py +++ b/tests/test_reciprocal.py @@ -52,6 +52,56 @@ def test_case_3(): obj.run(pytorch_code, ["out"]) +def test_case_7(): + """3D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]) + result = torch.reciprocal(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """边界值 - 包含零值""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1.0, -1.0, 0.5, 10.0, 100.0]) + result = torch.reciprocal(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """不同数据类型 - float64""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.5, 1.0, 2.0], dtype=torch.float64) + result = torch.reciprocal(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """梯度计算测试""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.5, 1.0, 2.0], requires_grad=True) + y = torch.reciprocal(a) + y.sum().backward() + a_grad = a.grad + """ + ) + obj.run(pytorch_code, ["y", "a_grad"], check_stop_gradient=False) + + def test_case_4(): pytorch_code = textwrap.dedent( """ diff --git a/tests/test_square.py b/tests/test_square.py index 092cbec8f..6ee64e15f 100644 --- a/tests/test_square.py +++ b/tests/test_square.py @@ -52,6 +52,68 @@ def test_case_3(): obj.run(pytorch_code, ["out"]) +def test_case_6(): + """2D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[0.2970, 1.5420], [2.0, 3.0]]) + result = torch.square(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """3D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]) + result = torch.square(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """边界值 - 零值和负数""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.0, 1.0, -1.0, 10.0, -10.0]) + result = torch.square(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """不同数据类型 - float64""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.5, 1.0, 2.0], dtype=torch.float64) + result = torch.square(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """梯度计算测试""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.5, 1.0, 2.0], requires_grad=True) + y = torch.square(a) + y.sum().backward() + a_grad = a.grad + """ + ) + obj.run(pytorch_code, ["y", "a_grad"], check_stop_gradient=False) + + # generated by validate_unittest autofix, based on test_case_3 def test_case_4(): pytorch_code = textwrap.dedent( diff --git a/tests/test_tan.py b/tests/test_tan.py index af84fe998..56f9e0091 100644 --- a/tests/test_tan.py +++ b/tests/test_tan.py @@ -75,3 +75,53 @@ def test_case_5(): """ ) obj.run(pytorch_code, ["result", "out"]) + + +def test_case_6(): + """2D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[1.4309, 1.2706], [-0.8562, 0.9796]]) + result = torch.tan(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """3D张量""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[[0.1, 0.2], [0.3, 0.4]], [[0.5, 0.6], [0.7, 0.8]]]) + result = torch.tan(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """不同数据类型 - float64""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1.4309, 1.2706], dtype=torch.float64) + result = torch.tan(a) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """梯度计算测试""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([0.5, 1.0, 2.0], requires_grad=True) + y = torch.tan(a) + y.sum().backward() + a_grad = a.grad + """ + ) + obj.run(pytorch_code, ["y", "a_grad"], check_stop_gradient=False)