diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 2b4e144f8..4a33f5295 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -469,16 +469,7 @@ ] }, "torch.Tensor.addmv_": { - "Matcher": "AddMR_Matcher", - "paddle_api": "paddle.mm", - "min_input_args": 2, - "args_list": [ - "mat", - "vec", - "*", - "beta", - "alpha" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.addr": { "Matcher": "AddMRMatcher", @@ -493,16 +484,7 @@ ] }, "torch.Tensor.addr_": { - "Matcher": "AddMR_Matcher", - "paddle_api": "paddle.outer", - "min_input_args": 2, - "args_list": [ - "vec1", - "vec2", - "*", - "beta", - "alpha" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.adjoint": { "Matcher": "AdjointMatcher", @@ -752,8 +734,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.col_indices": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.Tensor.cols" + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.conj": { "Matcher": "ChangePrefixMatcher" @@ -815,8 +796,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.crow_indices": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.Tensor.crows" + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.cuda": { "Matcher": "ChangePrefixMatcher" @@ -1013,9 +993,7 @@ "min_input_args": 0 }, "torch.Tensor.fix_": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.Tensor.trunc_", - "min_input_args": 0 + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.flatten": { "Matcher": "ChangePrefixMatcher" @@ -1480,15 +1458,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.logaddexp": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.Tensor.logaddexp", - "min_input_args": 1, - "args_list": [ - "other" - ], - "kwargs_change": { - "other": "y" - } + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.logaddexp2": { "Matcher": "LogAddExp2Matcher", @@ -1619,9 +1589,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.matrix_exp": { - "Matcher": "TensorFunc2PaddleFunc", - "paddle_api": "paddle.linalg.matrix_exp", - "min_input_args": 0 + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.matrix_power": { "Matcher": "ChangePrefixMatcher" @@ -1720,9 +1688,7 @@ "min_input_args": 1 }, "torch.Tensor.mvlgamma_": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.Tensor.multigammaln_", - "min_input_args": 1 + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.nan_to_num": { "Matcher": "ChangePrefixMatcher" @@ -1731,18 +1697,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.nanmean": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.Tensor.nanmean", - "min_input_args": 0, - "args_list": [ - "dim", - "keepdim", - "*", - "dtype" - ], - "kwargs_change": { - "dim": "axis" - } + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.nanmedian": { "Matcher": "GenericMatcher", @@ -1763,19 +1718,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.nansum": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.Tensor.nansum", - "min_input_args": 0, - "args_list": [ - "dim", - "keepdim", - "*", - "dtype" - ], - "kwargs_change": { - "dim": "axis", - "dtype": "dtype" - } + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.narrow": { "Matcher": "ChangePrefixMatcher" @@ -1821,9 +1764,7 @@ "min_input_args": 0 }, "torch.Tensor.negative_": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.Tensor.neg_", - "min_input_args": 0 + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.nelement": { "Matcher": "ChangePrefixMatcher" @@ -2090,18 +2031,7 @@ ] }, "torch.Tensor.select_scatter": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.Tensor.select_scatter", - "min_input_args": 3, - "args_list": [ - "src", - "dim", - "index" - ], - "kwargs_change": { - "src": "values", - "dim": "axis" - } + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.set_": { "Matcher": "TensorSetMatcher", @@ -2159,23 +2089,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.slice_scatter": { - "Matcher": "SliceScatterMatcher", - "paddle_api": "paddle.Tensor.slice_scatter", - "min_input_args": 1, - "args_list": [ - "src", - "dim", - "start", - "end", - "step" - ], - "kwargs_change": { - "src": "value", - "dim": "axes", - "start": "starts", - "end": "ends", - "step": "strides" - } + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.slogdet": { "Matcher": "SLogDetMatcher", @@ -2321,12 +2235,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.take": { - "Matcher": "TensorTakeMatcher", - "paddle_api": "paddle.Tensor.take", - "min_input_args": 1, - "args_list": [ - "index" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.take_along_dim": { "Matcher": "ChangePrefixMatcher" @@ -2357,9 +2266,7 @@ }, "torch.Tensor.to_mkldnn": {}, "torch.Tensor.to_sparse": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.Tensor.to_sparse_coo", - "min_input_args": 1 + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.to_sparse_bsc": {}, "torch.Tensor.to_sparse_bsr": {}, @@ -3070,32 +2977,10 @@ "Matcher": "ChangePrefixMatcher" }, "torch.addmv": { - "Matcher": "AddMRMatcher", - "paddle_api": "paddle.mm", - "min_input_args": 3, - "args_list": [ - "input", - "mat", - "vec", - "*", - "beta", - "alpha", - "out" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.addr": { - "Matcher": "AddMRMatcher", - "paddle_api": "paddle.outer", - "min_input_args": 3, - "args_list": [ - "input", - "vec1", - "vec2", - "*", - "beta", - "alpha", - "out" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.adjoint": { "Matcher": "AdjointMatcher", @@ -3253,9 +3138,7 @@ ] }, "torch.autograd.enable_grad": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.enable_grad", - "min_input_args": 0 + "Matcher": "ChangePrefixMatcher" }, "torch.autograd.function.FunctionCtx": { "Matcher": "ChangePrefixMatcher" @@ -5250,14 +5133,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.fix": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.trunc", - "min_input_args": 1, - "args_list": [ - "input", - "*", - "out" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.flatten": { "Matcher": "ChangePrefixMatcher" @@ -5476,17 +5352,7 @@ }, "torch.hinge_embedding_loss": {}, "torch.histc": { - "Matcher": "HistcMatcher", - "paddle_api": "paddle.histogram", - "min_input_args": 1, - "args_list": [ - "input", - "bins", - "min", - "max", - "*", - "out" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.histogram": { "Matcher": "HistogramMatcher", @@ -6394,19 +6260,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.logaddexp": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.logaddexp", - "min_input_args": 2, - "args_list": [ - "input", - "other", - "*", - "out" - ], - "kwargs_change": { - "input": "x", - "other": "y" - } + "Matcher": "ChangePrefixMatcher" }, "torch.logaddexp2": { "Matcher": "LogAddExp2Matcher", @@ -6441,26 +6295,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.logspace": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.logspace", - "min_input_args": 3, - "args_list": [ - "start", - "end", - "steps", - "base", - "*", - "out", - "dtype", - "layout", - "device", - "requires_grad" - ], - "kwargs_change": { - "end": "stop", - "steps": "num", - "dtype": "dtype" - } + "Matcher": "ChangePrefixMatcher" }, "torch.logsumexp": { "Matcher": "ChangePrefixMatcher" @@ -6509,17 +6344,7 @@ }, "torch.margin_ranking_loss": {}, "torch.masked_fill": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.masked_fill", - "min_input_args": 3, - "args_list": [ - "input", - "mask", - "value" - ], - "kwargs_change": { - "input": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.masked_select": { "Matcher": "ChangePrefixMatcher" @@ -6573,17 +6398,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.moveaxis": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.moveaxis", - "min_input_args": 3, - "args_list": [ - "input", - "source", - "destination" - ], - "kwargs_change": { - "input": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.movedim": { "Matcher": "GenericMatcher", @@ -6652,37 +6467,10 @@ } }, "torch.nan_to_num": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.nan_to_num", - "min_input_args": 1, - "args_list": [ - "input", - "nan", - "posinf", - "neginf", - "*", - "out" - ], - "kwargs_change": { - "input": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.nanmean": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.nanmean", - "min_input_args": 1, - "args_list": [ - "input", - "dim", - "keepdim", - "*", - "dtype", - "out" - ], - "kwargs_change": { - "input": "x", - "dim": "axis" - } + "Matcher": "ChangePrefixMatcher" }, "torch.nanmedian": { "Matcher": "ChangeAPIMatcher", @@ -6692,22 +6480,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nansum": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.nansum", - "min_input_args": 1, - "args_list": [ - "input", - "dim", - "keepdim", - "*", - "dtype", - "out" - ], - "kwargs_change": { - "input": "x", - "dim": "axis", - "dtype": "dtype" - } + "Matcher": "ChangePrefixMatcher" }, "torch.narrow": { "Matcher": "ChangePrefixMatcher" @@ -10446,20 +10219,7 @@ ] }, "torch.select_scatter": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.select_scatter", - "min_input_args": 4, - "args_list": [ - "input", - "src", - "dim", - "index" - ], - "kwargs_change": { - "input": "x", - "src": "values", - "dim": "axis" - } + "Matcher": "ChangePrefixMatcher" }, "torch.selu": { "Matcher": "GenericMatcher", @@ -10538,17 +10298,7 @@ } }, "torch.sgn": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.sgn", - "min_input_args": 1, - "args_list": [ - "input", - "*", - "out" - ], - "kwargs_change": { - "input": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.sigmoid": { "Matcher": "ChangePrefixMatcher" @@ -10756,17 +10506,7 @@ } }, "torch.signbit": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.signbit", - "min_input_args": 1, - "args_list": [ - "input", - "*", - "out" - ], - "kwargs_change": { - "input": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.sin": { "Matcher": "ChangePrefixMatcher" @@ -10781,25 +10521,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.slice_scatter": { - "Matcher": "SliceScatterMatcher", - "paddle_api": "paddle.slice_scatter", - "min_input_args": 2, - "args_list": [ - "input", - "src", - "dim", - "start", - "end", - "step" - ], - "kwargs_change": { - "input": "x", - "src": "value", - "dim": "axes", - "start": "starts", - "end": "ends", - "step": "strides" - } + "Matcher": "ChangePrefixMatcher" }, "torch.slogdet": { "Matcher": "SLogDetMatcher", @@ -11171,15 +10893,7 @@ } }, "torch.special.round": { - "Matcher": "RoundMatcher", - "paddle_api": "paddle.round", - "min_input_args": 1, - "args_list": [ - "input", - "*", - "decimals", - "out" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.special.scaled_modified_bessel_k0": {}, "torch.special.scaled_modified_bessel_k1": {}, @@ -11341,16 +11055,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.take": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.take", - "min_input_args": 2, - "args_list": [ - "input", - "index" - ], - "kwargs_change": { - "input": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.take_along_dim": { "Matcher": "ChangePrefixMatcher" @@ -11371,20 +11076,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.tensordot": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.tensordot", - "min_input_args": 2, - "args_list": [ - "a", - "b", - "dims", - "out" - ], - "kwargs_change": { - "a": "x", - "b": "y", - "dims": "axes" - } + "Matcher": "ChangePrefixMatcher" }, "torch.testing.assert_allclose": { "Matcher": "Assert_AllcloseMatcher", @@ -11469,55 +11161,20 @@ "Matcher": "ChangePrefixMatcher" }, "torch.tril_indices": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.tril_indices", - "min_input_args": 2, - "args_list": [ - "row", - "col", - "offset", - "*", - "dtype", - "device", - "layout" - ], - "kwargs_change": { - "dtype": "dtype" - } + "Matcher": "ChangePrefixMatcher" }, "torch.triplet_margin_loss": {}, "torch.triu": { "Matcher": "ChangePrefixMatcher" }, "torch.triu_indices": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.triu_indices", - "min_input_args": 2, - "args_list": [ - "row", - "col", - "offset", - "*", - "dtype", - "device", - "layout" - ], - "kwargs_change": { - "dtype": "dtype" - } + "Matcher": "ChangePrefixMatcher" }, "torch.true_divide": { "Matcher": "ChangePrefixMatcher" }, "torch.trunc": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.trunc", - "min_input_args": 1, - "args_list": [ - "input", - "*", - "out" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.unbind": { "Matcher": "ChangePrefixMatcher" @@ -11763,17 +11420,7 @@ "min_input_args": 2 }, "torch.vander": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.vander", - "min_input_args": 1, - "args_list": [ - "x", - "N", - "increasing" - ], - "kwargs_change": { - "N": "n" - } + "Matcher": "ChangePrefixMatcher" }, "torch.var": { "Matcher": "ChangePrefixMatcher" diff --git a/tests/test_col_indices.py b/tests/test_col_indices.py new file mode 100644 index 000000000..08ba24adb --- /dev/null +++ b/tests/test_col_indices.py @@ -0,0 +1,30 @@ +# Copyright (c) 2026 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.col_indices") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.randn(3, 4).to_sparse_csr() + result = x.col_indices() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_crow_indices.py b/tests/test_crow_indices.py new file mode 100644 index 000000000..df6cc4c0a --- /dev/null +++ b/tests/test_crow_indices.py @@ -0,0 +1,30 @@ +# Copyright (c) 2026 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.crow_indices") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.randn(3, 4).to_sparse_csr() + result = x.crow_indices() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_erf_.py b/tests/test_erf_.py new file mode 100644 index 000000000..110720929 --- /dev/null +++ b/tests/test_erf_.py @@ -0,0 +1,41 @@ +# Copyright (c) 2026 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.erf_") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([-2.0, -0.5, 1.5, 3.0], dtype=torch.float32) + result = x.erf_() + """ + ) + obj.run(pytorch_code, ["x", "result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[-3.0, -1.25], [0.75, 2.5]], dtype=torch.float64) + result = x.erf_() + """ + ) + obj.run(pytorch_code, ["x", "result"]) diff --git a/tests/test_fix_.py b/tests/test_fix_.py new file mode 100644 index 000000000..caf8fdb7a --- /dev/null +++ b/tests/test_fix_.py @@ -0,0 +1,41 @@ +# Copyright (c) 2026 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.fix_") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([-2.5, -0.5, 1.5, 3.7], dtype=torch.float32) + result = x.fix_() + """ + ) + obj.run(pytorch_code, ["x", "result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[-3.5, -1.25], [0.75, 2.5]], dtype=torch.float64) + result = x.fix_() + """ + ) + obj.run(pytorch_code, ["x", "result"]) diff --git a/tests/test_matrix_exp.py b/tests/test_matrix_exp.py new file mode 100644 index 000000000..1debf1bde --- /dev/null +++ b/tests/test_matrix_exp.py @@ -0,0 +1,41 @@ +# Copyright (c) 2026 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.matrix_exp") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.eye(3, dtype=torch.float32) + result = x.matrix_exp() + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.eye(3, dtype=torch.float64) + result = x.matrix_exp() + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_mvlgamma_.py b/tests/test_mvlgamma_.py new file mode 100644 index 000000000..03faf20e7 --- /dev/null +++ b/tests/test_mvlgamma_.py @@ -0,0 +1,41 @@ +# Copyright (c) 2026 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.mvlgamma_") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1.5, 2.5, 3.5], dtype=torch.float32) + result = x.mvlgamma_(2) + """ + ) + obj.run(pytorch_code, ["x", "result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[1.5, 2.5], [3.5, 4.5]], dtype=torch.float64) + result = x.mvlgamma_(3) + """ + ) + obj.run(pytorch_code, ["x", "result"]) diff --git a/tests/test_negative_.py b/tests/test_negative_.py new file mode 100644 index 000000000..dd8671af2 --- /dev/null +++ b/tests/test_negative_.py @@ -0,0 +1,41 @@ +# Copyright (c) 2026 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.negative_") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([-2.0, -0.5, 1.5, 3.0], dtype=torch.float32) + result = x.negative_() + """ + ) + obj.run(pytorch_code, ["x", "result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[-3.0, -1.25], [0.75, 2.5]], dtype=torch.float64) + result = x.negative_() + """ + ) + obj.run(pytorch_code, ["x", "result"]) diff --git a/tests/test_retain_grad.py b/tests/test_retain_grad.py new file mode 100644 index 000000000..e69c77548 --- /dev/null +++ b/tests/test_retain_grad.py @@ -0,0 +1,33 @@ +# Copyright (c) 2026 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 + x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True) + x.retain_grad() + y = x.sum() + y.backward() + result = x.grad + """ + ) + obj.run(pytorch_code, ["result"], check_stop_gradient=False) diff --git a/tests/test_sparse_mask.py b/tests/test_sparse_mask.py new file mode 100644 index 000000000..eaddc7058 --- /dev/null +++ b/tests/test_sparse_mask.py @@ -0,0 +1,46 @@ +# Copyright (c) 2026 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.sparse_mask") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.randn(3, 4) + indices = torch.tensor([[0, 1], [0, 1]]) + values = torch.tensor([1.0, 1.0]) + sparse_mask = torch.sparse_coo_tensor(indices, values, (3, 4)) + result = x.sparse_mask(sparse_mask) + """ + ) + expect_paddle_code = textwrap.dedent( + """ + import paddle + + x = paddle.randn(3, 4) + indices = paddle.tensor([[0, 1], [0, 1]]) + values = paddle.tensor([1.0, 1.0]) + sparse_mask = paddle.sparse.sparse_coo_tensor( + indices=indices, values=values, shape=(3, 4) + ) + result = x.sparse_mask(sparse_mask) + """ + ) + obj.run(pytorch_code, expect_paddle_code=expect_paddle_code) diff --git a/tests/test_to_sparse.py b/tests/test_to_sparse.py new file mode 100644 index 000000000..ea8a4de07 --- /dev/null +++ b/tests/test_to_sparse.py @@ -0,0 +1,57 @@ +# Copyright (c) 2026 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.to_sparse") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.randn(3, 4) + result = x.to_sparse() + """ + ) + expect_paddle_code = textwrap.dedent( + """ + import paddle + + x = paddle.randn(3, 4) + result = x.to_sparse() + """ + ) + obj.run(pytorch_code, expect_paddle_code=expect_paddle_code) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.randn(3, 4) + result = x.to_sparse(sparse_dim=2) + """ + ) + expect_paddle_code = textwrap.dedent( + """ + import paddle + + x = paddle.randn(3, 4) + result = x.to_sparse(sparse_dim=2) + """ + ) + obj.run(pytorch_code, expect_paddle_code=expect_paddle_code)