diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 9b78582e2..7e42b1c55 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -431,14 +431,7 @@ ] }, "torch.Tensor.addcmul": { - "Matcher": "AddCMulMatcher", - "min_input_args": 2, - "args_list": [ - "tensor1", - "tensor2", - "*", - "value" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.addcmul_": { "Matcher": "AddCMul_Matcher", @@ -3681,16 +3674,7 @@ ] }, "torch.addcmul": { - "Matcher": "AddCMulMatcher", - "min_input_args": 3, - "args_list": [ - "input", - "tensor1", - "tensor2", - "*", - "value", - "out" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.addmm": { "Matcher": "GenericMatcher", diff --git a/tests/test_Tensor_addcmul.py b/tests/test_Tensor_addcmul.py index 9726b65d9..b1ca5f1c7 100644 --- a/tests/test_Tensor_addcmul.py +++ b/tests/test_Tensor_addcmul.py @@ -100,3 +100,75 @@ def test_case_6(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """2D tensor test""" + pytorch_code = textwrap.dedent( + """ + import torch + tensor1 = torch.tensor([[1., 2.], [3., 4.]]) + tensor2 = torch.tensor([[5., 6.], [7., 8.]]) + input = torch.tensor([[1., 1.], [1., 1.]]) + result = input.addcmul(tensor1, tensor2, value=2) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """3D tensor test""" + pytorch_code = textwrap.dedent( + """ + import torch + tensor1 = torch.tensor([[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]]]) + tensor2 = torch.tensor([[[1., 1.], [1., 1.]], [[1., 1.], [1., 1.]]]) + input = torch.tensor([[[0., 0.], [0., 0.]], [[0., 0.], [0., 0.]]]) + result = input.addcmul(tensor1, tensor2) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """float64 dtype test""" + pytorch_code = textwrap.dedent( + """ + import torch + tensor1 = torch.tensor([1., 2., 3.], dtype=torch.float64) + tensor2 = torch.tensor([4., 5., 6.], dtype=torch.float64) + input = torch.tensor([7., 8., 9.], dtype=torch.float64) + result = input.addcmul(tensor1, tensor2, value=2.0) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """gradient computation test""" + pytorch_code = textwrap.dedent( + """ + import torch + tensor1 = torch.tensor([1., 2., 3.], requires_grad=True) + tensor2 = torch.tensor([4., 5., 6.], requires_grad=True) + input = torch.tensor([7., 8., 9.], requires_grad=True) + result = input.addcmul(tensor1, tensor2, value=2.0) + result.sum().backward() + input_grad = input.grad + """ + ) + obj.run(pytorch_code, ["result", "input_grad"], check_stop_gradient=False) + + +def test_case_11(): + """expression argument test""" + pytorch_code = textwrap.dedent( + """ + import torch + tensor1 = torch.tensor([1., 2., 3.]) + tensor2 = torch.tensor([4., 5., 6.]) + input = torch.tensor([7., 8., 9.]) + result = input.addcmul(tensor1, tensor2, value=1 + 1) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_addcmul.py b/tests/test_addcmul.py index 5c035c004..7ff891a52 100644 --- a/tests/test_addcmul.py +++ b/tests/test_addcmul.py @@ -114,3 +114,75 @@ def test_case_7(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """2D tensor test""" + pytorch_code = textwrap.dedent( + """ + import torch + tensor1 = torch.tensor([[1., 2.], [3., 4.]]) + tensor2 = torch.tensor([[5., 6.], [7., 8.]]) + input = torch.tensor([[1., 1.], [1., 1.]]) + result = torch.addcmul(input, tensor1, tensor2, value=2) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """3D tensor test""" + pytorch_code = textwrap.dedent( + """ + import torch + tensor1 = torch.tensor([[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]]]) + tensor2 = torch.tensor([[[1., 1.], [1., 1.]], [[1., 1.], [1., 1.]]]) + input = torch.tensor([[[0., 0.], [0., 0.]], [[0., 0.], [0., 0.]]]) + result = torch.addcmul(input, tensor1, tensor2) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """float64 dtype test""" + pytorch_code = textwrap.dedent( + """ + import torch + tensor1 = torch.tensor([1., 2., 3.], dtype=torch.float64) + tensor2 = torch.tensor([4., 5., 6.], dtype=torch.float64) + input = torch.tensor([7., 8., 9.], dtype=torch.float64) + result = torch.addcmul(input, tensor1, tensor2, value=2.0) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_11(): + """gradient computation test""" + pytorch_code = textwrap.dedent( + """ + import torch + tensor1 = torch.tensor([1., 2., 3.], requires_grad=True) + tensor2 = torch.tensor([4., 5., 6.], requires_grad=True) + input = torch.tensor([7., 8., 9.], requires_grad=True) + result = torch.addcmul(input, tensor1, tensor2, value=2.0) + result.sum().backward() + input_grad = input.grad + """ + ) + obj.run(pytorch_code, ["result", "input_grad"], check_stop_gradient=False) + + +def test_case_12(): + """expression argument test""" + pytorch_code = textwrap.dedent( + """ + import torch + tensor1 = torch.tensor([1., 2., 3.]) + tensor2 = torch.tensor([4., 5., 6.]) + input = torch.tensor([7., 8., 9.]) + result = torch.addcmul(input, tensor1, tensor2, value=1 + 1) + """ + ) + obj.run(pytorch_code, ["result"])