diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index b1adeff2d..1bb60a8cb 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -10389,6 +10389,19 @@ "dim1" ] }, + "torch.trapezoid": { + "Matcher": "GenericMatcher", + "paddle_api": "paddle.trapezoid", + "args_list": [ + "y", + "x", + "dx", + "dim" + ], + "kwargs_change": { + "dim": "axis" + } + }, "torch.triangular_solve": { "Matcher": "TriangularSolveMatcher", "paddle_api": "paddle.linalg.triangular_solve", diff --git a/tests/test_trapezoid.py b/tests/test_trapezoid.py new file mode 100644 index 000000000..dac12678a --- /dev/null +++ b/tests/test_trapezoid.py @@ -0,0 +1,78 @@ +# 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.trapezoid") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + y = torch.tensor([1.0, 1, 1, 0, 1]) + result = torch.trapezoid(y) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + y = torch.tensor([1, 1, 1, 0, 1]).type(torch.float32) + x = torch.tensor([1, 2, 3, 0, 1]).type(torch.float32) + result = torch.trapezoid(y=y, x=x) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + y = torch.tensor([1, 1, 1, 0, 1]).type(torch.float32) + x = torch.tensor([1, 2, 3, 0, 1]).type(torch.float32) + result = torch.trapezoid(y=y, dim=-1, dx=2) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + y = torch.tensor([1, 1, 1, 0, 1]).type(torch.float32) + x = torch.tensor([1, 2, 3, 0, 1]).type(torch.float32) + result = torch.trapezoid(y, dx=2, dim=-1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + y = torch.tensor([1, 1, 1, 0, 1]).type(torch.float32) + x = torch.tensor([1, 2, 3, 0, 1]).type(torch.float32) + result = torch.trapezoid(y=y, x=x, dim=-1) + """ + ) + obj.run(pytorch_code, ["result"])