diff --git a/paconvert/api_alias_mapping.json b/paconvert/api_alias_mapping.json index 932aebe53..2e3de92e0 100644 --- a/paconvert/api_alias_mapping.json +++ b/paconvert/api_alias_mapping.json @@ -123,7 +123,6 @@ "torch.nn.modules.rnn.RNNCellBase": "torch.nn.RNNCellBase", "torch.nn.modules.sparse.Embedding": "torch.nn.Embedding", "torch.nn.parallel.DataParallel": "torch.nn.DataParallel", - "torch.nn.parallel.data_parallel.DataParallel": "torch.nn.DataParallel", "torch.nn.parallel.distributed.DistributedDataParallel": "torch.nn.parallel.DistributedDataParallel", "torch.nn.utils.clip_grad_norm": "torch.nn.utils.clip_grad_norm_", "torch.orgqr": "torch.linalg.householder_product", diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 5789adee6..fda0b2607 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -414,14 +414,7 @@ ] }, "torch.Tensor.addcdiv": { - "Matcher": "AddCDivMatcher", - "min_input_args": 2, - "args_list": [ - "tensor1", - "tensor2", - "*", - "value" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.addcdiv_": { "Matcher": "ChangePrefixMatcher" @@ -957,6 +950,9 @@ "torch.Tensor.expand_as": { "Matcher": "ChangePrefixMatcher" }, + "torch.Tensor.expand_copy": { + "Matcher": "ChangePrefixMatcher" + }, "torch.Tensor.expm1": { "Matcher": "ChangePrefixMatcher" }, @@ -1106,18 +1102,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.gt_": { - "Matcher": "TensorInplaceReserveTypeMatcher", - "paddle_api": "paddle.Tensor.greater_than_", - "min_input_args": 1, - "convert_tensor": [ - "other" - ], - "args_list": [ - "other" - ], - "kwargs_change": { - "other": "y" - } + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.half": { "Matcher": "ChangePrefixMatcher" @@ -1748,20 +1733,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.new_tensor": { - "Matcher": "TensorNewTensorMatcher", - "paddle_api": "paddle.to_tensor", - "min_input_args": 1, - "args_list": [ - "data", - "*", - "dtype", - "device", - "requires_grad" - ], - "kwargs_change": { - "device": "place", - "layout": "" - } + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.new_zeros": { "Matcher": "ChangePrefixMatcher" @@ -2297,15 +2269,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.true_divide_": { - "Matcher": "Num2TensorBinaryMatcher", - "paddle_api": "paddle.Tensor.divide_", - "min_input_args": 1, - "args_list": [ - "other" - ], - "kwargs_change": { - "other": "y" - } + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.trunc": { "Matcher": "ChangePrefixMatcher" @@ -3242,9 +3206,7 @@ ] }, "torch.autograd.grad_mode.set_grad_enabled": { - "Matcher": "ChangeAPIMatcher", - "paddle_api": "paddle.set_grad_enabled", - "min_input_args": 1 + "Matcher": "ChangePrefixMatcher" }, "torch.autograd.graph.saved_tensors_hooks": { "Matcher": "ChangeAPIMatcher", @@ -3424,19 +3386,11 @@ "torch.clamp": { "Matcher": "ChangePrefixMatcher" }, + "torch.clamp_": { + "Matcher": "ChangePrefixMatcher" + }, "torch.clamp_max": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.clip", - "min_input_args": 2, - "args_list": [ - "input", - "max", - "*", - "out" - ], - "kwargs_change": { - "input": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.clamp_min": { "Matcher": "GenericMatcher", @@ -5025,7 +4979,9 @@ "torch.exp_": { "Matcher": "ChangePrefixMatcher" }, - "torch.expand_copy": {}, + "torch.expand_copy": { + "Matcher": "ChangePrefixMatcher" + }, "torch.expm1": { "Matcher": "ChangePrefixMatcher" }, @@ -5350,17 +5306,7 @@ } }, "torch.hstack": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.hstack", - "min_input_args": 1, - "args_list": [ - "tensors", - "*", - "out" - ], - "kwargs_change": { - "tensors": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.hub.download_url_to_file": { "Matcher": "GenericMatcher", @@ -5669,18 +5615,7 @@ } }, "torch.linalg.cholesky": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.linalg.cholesky", - "min_input_args": 1, - "args_list": [ - "input", - "*", - "upper", - "out" - ], - "kwargs_change": { - "input": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.linalg.cholesky_ex": { "Matcher": "LinalgCholeskyExMatcher", @@ -5712,37 +5647,10 @@ } }, "torch.linalg.cross": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.cross", - "min_input_args": 2, - "args_list": [ - "input", - "other", - "*", - "dim", - "out" - ], - "kwargs_change": { - "input": "x", - "other": "y", - "dim": "axis" - }, - "paddle_default_kwargs": { - "axis": -1 - } + "Matcher": "ChangePrefixMatcher" }, "torch.linalg.det": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.linalg.det", - "min_input_args": 1, - "args_list": [ - "A", - "*", - "out" - ], - "kwargs_change": { - "A": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.linalg.diagonal": { "Matcher": "GenericMatcher", @@ -5779,18 +5687,7 @@ } }, "torch.linalg.eigh": { - "Matcher": "DoubleAssignMatcher", - "paddle_api": "paddle.linalg.eigh", - "min_input_args": 1, - "args_list": [ - "input", - "UPLO", - "*", - "out" - ], - "kwargs_change": { - "input": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.linalg.eigvals": { "Matcher": "GenericMatcher", @@ -6009,18 +5906,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.linalg.qr": { - "Matcher": "Linalg_qrMatcher", - "paddle_api": "paddle.linalg.qr", - "min_input_args": 1, - "args_list": [ - "A", - "mode", - "*", - "out" - ], - "kwargs_change": { - "A": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.linalg.slogdet": { "Matcher": "SLogDetMatcher", @@ -6184,11 +6070,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.logdet": { - "Matcher": "LogDetMatcher", - "min_input_args": 1, - "args_list": [ - "input" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.logical_and": { "Matcher": "ChangePrefixMatcher" @@ -6496,76 +6378,16 @@ ] }, "torch.nn.BatchNorm1d": { - "Matcher": "ReverseMomentumMatcher", - "paddle_api": "paddle.nn.BatchNorm1D", - "min_input_args": 1, - "args_list": [ - "num_features", - "eps", - "momentum", - "affine", - "track_running_stats", - "device", - "dtype" - ], - "kwargs_change": { - "eps": "epsilon", - "affine": [ - "weight_attr", - "bias_attr" - ], - "track_running_stats": "", - "device": "", - "dtype": "" - } + "Matcher": "ChangeAPIMatcher", + "paddle_api": "paddle.compat.nn.BatchNorm1d" }, "torch.nn.BatchNorm2d": { - "Matcher": "ReverseMomentumMatcher", - "paddle_api": "paddle.nn.BatchNorm2D", - "min_input_args": 1, - "args_list": [ - "num_features", - "eps", - "momentum", - "affine", - "track_running_stats", - "device", - "dtype" - ], - "kwargs_change": { - "eps": "epsilon", - "affine": [ - "weight_attr", - "bias_attr" - ], - "track_running_stats": "", - "device": "", - "dtype": "" - } + "Matcher": "ChangeAPIMatcher", + "paddle_api": "paddle.compat.nn.BatchNorm2d" }, "torch.nn.BatchNorm3d": { - "Matcher": "ReverseMomentumMatcher", - "paddle_api": "paddle.nn.BatchNorm3D", - "min_input_args": 1, - "args_list": [ - "num_features", - "eps", - "momentum", - "affine", - "track_running_stats", - "device", - "dtype" - ], - "kwargs_change": { - "eps": "epsilon", - "affine": [ - "weight_attr", - "bias_attr" - ], - "track_running_stats": "", - "device": "", - "dtype": "" - } + "Matcher": "ChangeAPIMatcher", + "paddle_api": "paddle.compat.nn.BatchNorm3d" }, "torch.nn.Bilinear": { "Matcher": "GenericMatcher", @@ -6710,13 +6532,7 @@ ] }, "torch.nn.ELU": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.nn.ELU", - "min_input_args": 0, - "args_list": [ - "alpha", - "inplace" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.nn.Embedding": { "Matcher": "ChangePrefixMatcher" @@ -7219,7 +7035,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.Module.to_empty": { - "min_input_args": 0 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.Module.train": { "Matcher": "ChangePrefixMatcher" @@ -7869,23 +7685,8 @@ ] }, "torch.nn.functional.batch_norm": { - "Matcher": "ReverseMomentumMatcher", - "paddle_api": "paddle.nn.functional.batch_norm", - "args_list": [ - "input", - "running_mean", - "running_var", - "weight", - "bias", - "training", - "momentum", - "eps" - ], - "kwargs_change": { - "input": "x", - "eps": "epsilon" - }, - "min_input_args": 3 + "Matcher": "ChangeAPIMatcher", + "paddle_api": "paddle.compat.nn.functional.batch_norm" }, "torch.nn.functional.bilinear": { "Matcher": "FunctionalBilinearMatcher", @@ -8052,17 +7853,7 @@ "min_input_args": 1 }, "torch.nn.functional.elu": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.nn.functional.elu", - "args_list": [ - "input", - "alpha", - "inplace" - ], - "kwargs_change": { - "input": "x" - }, - "min_input_args": 1 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.elu_": { "Matcher": "GenericMatcher", @@ -8163,22 +7954,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.gumbel_softmax": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.nn.functional.gumbel_softmax", - "min_input_args": 1, - "args_list": [ - "logits", - "tau", - "hard", - "eps", - "dim" - ], - "kwargs_change": { - "logits": "x", - "tau": "temperature", - "eps": "", - "dim": "axis" - } + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.hardshrink": { "Matcher": "ChangePrefixMatcher" @@ -8258,22 +8034,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.instance_norm": { - "Matcher": "ReverseMomentumMatcher", - "paddle_api": "paddle.nn.functional.instance_norm", - "args_list": [ - "input", - "running_mean", - "running_var", - "weight", - "bias", - "use_input_stats", - "momentum", - "eps" - ], - "kwargs_change": { - "input": "x" - }, - "min_input_args": 1 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.interpolate": { "Matcher": "ChangePrefixMatcher" @@ -8534,16 +8295,7 @@ "min_input_args": 2 }, "torch.nn.functional.prelu": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.nn.functional.prelu", - "min_input_args": 2, - "args_list": [ - "input", - "weight" - ], - "kwargs_change": { - "input": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.relu": { "Matcher": "ChangePrefixMatcher" @@ -8555,14 +8307,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.rms_norm": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.nn.functional.rms_norm", - "args_list": [ - "input", - "normalized_shape", - "weight", - "eps" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.rrelu": { "Matcher": "GenericMatcher", @@ -8895,6 +8640,68 @@ }, "min_input_args": 1 }, + "torch.nn.modules.loss.BCELoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.BCEWithLogitsLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.CTCLoss": {}, + "torch.nn.modules.loss.CosineEmbeddingLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.CrossEntropyLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.GaussianNLLLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.HingeEmbeddingLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.KLDivLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.L1Loss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.MSELoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.MarginRankingLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.MultiLabelMarginLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.MultiLabelSoftMarginLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.MultiMarginLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.NLLLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.PoissonNLLLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.SmoothL1Loss": { + "Matcher": "ChangeAPIMatcher", + "paddle_api": "paddle.compat.nn.SmoothL1Loss" + }, + "torch.nn.modules.loss.SoftMarginLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.TripletMarginLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss.TripletMarginWithDistanceLoss": { + "Matcher": "ChangePrefixMatcher" + }, + "torch.nn.modules.loss._Loss": { + "Matcher": "ChangePrefixMatcher" + }, "torch.nn.modules.module.Module": { "Matcher": "ChangePrefixMatcher" }, @@ -8907,14 +8714,7 @@ "min_input_args": 1 }, "torch.nn.modules.utils._pair": { - "Matcher": "NTupleMatcher", - "args_list": [ - "x" - ], - "paddle_default_kwargs": { - "n": 2 - }, - "min_input_args": 1 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.parallel.DistributedDataParallel": { "Matcher": "GenericMatcher", @@ -8948,9 +8748,6 @@ ], "min_input_args": 1 }, - "torch.nn.parallel.data_parallel.DataParallel": { - "Matcher": "ChangePrefixMatcher" - }, "torch.nn.parameter.Parameter": { "Matcher": "ChangePrefixMatcher" }, @@ -9822,18 +9619,7 @@ } }, "torch.qr": { - "Matcher": "QrMatcher", - "paddle_api": "paddle.linalg.qr", - "min_input_args": 1, - "args_list": [ - "input", - "some", - "*", - "out" - ], - "kwargs_change": { - "input": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.quantile": { "Matcher": "ChangePrefixMatcher" @@ -10050,15 +9836,7 @@ } }, "torch.set_default_device": { - "Matcher": "Device2StrMatcher", - "paddle_api": "paddle.device.set_device", - "min_input_args": 0, - "args_list": [ - "device" - ], - "kwargs_change": { - "device": "device" - } + "Matcher": "ChangePrefixMatcher" }, "torch.set_default_dtype": { "Matcher": "ChangePrefixMatcher" @@ -11307,17 +11085,7 @@ } }, "torch.vstack": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.vstack", - "min_input_args": 1, - "args_list": [ - "tensors", - "*", - "out" - ], - "kwargs_change": { - "tensors": "x" - } + "Matcher": "ChangePrefixMatcher" }, "torch.where": { "Matcher": "ChangePrefixMatcher" diff --git a/paconvert/attribute_mapping.json b/paconvert/attribute_mapping.json index bcf887801..8ccc0397b 100644 --- a/paconvert/attribute_mapping.json +++ b/paconvert/attribute_mapping.json @@ -1,6 +1,6 @@ { "torch.Tensor.H": { - "Matcher": "TensorHMatcher" + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.T": { "Matcher": "ChangePrefixMatcher" @@ -44,7 +44,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.mH": { - "Matcher": "TensorMhMatcher" + "Matcher": "ChangePrefixMatcher" }, "torch.Tensor.mT": { "Matcher": "ChangePrefixMatcher" diff --git a/tests/test_Tensor_expand_copy.py b/tests/test_Tensor_expand_copy.py new file mode 100644 index 000000000..290590828 --- /dev/null +++ b/tests/test_Tensor_expand_copy.py @@ -0,0 +1,139 @@ +# Copyright (c) 2025 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 + +import pytest +from apibase import APIBase + +obj = APIBase("torch.Tensor.expand_copy") + + +@pytest.mark.skip(reason="torch.Tensor.expand_copy requires PyTorch>=2.8") +def test_case_1(): + """basic expand: tensor.expand_copy(4, 2, 2)""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[[1], [2]]]) + result = a.expand_copy(4, 2, 2) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip(reason="torch.Tensor.expand_copy requires PyTorch>=2.8") +def test_case_2(): + """with -1 dims: tensor.expand_copy(4, -1, 2)""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[[1], [2]]]) + result = a.expand_copy(4, -1, 2) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip(reason="torch.Tensor.expand_copy requires PyTorch>=2.8") +def test_case_3(): + """1D to 3D""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1, 2, 3]) + result = a.expand_copy(3, 3) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip(reason="torch.Tensor.expand_copy requires PyTorch>=2.8") +def test_case_4(): + """keyword arguments: expand_copy(size=(4, 2, 2))""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[[1], [2]]]) + result = a.expand_copy(size=(4, 2, 2)) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip(reason="torch.Tensor.expand_copy requires PyTorch>=2.8") +def test_case_5(): + """1D to 3D with -1 dims via keyword""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1, 2, 3]) + result = a.expand_copy(3, -1) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip(reason="torch.Tensor.expand_copy requires PyTorch>=2.8") +def test_case_6(): + """list argument: expand_copy([4, 2, 2])""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[[1], [2]]]) + result = a.expand_copy([4, 2, 2]) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip(reason="torch.Tensor.expand_copy requires PyTorch>=2.8") +def test_case_7(): + """expand_copy with variable size""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1, 2, 3]) + size = (3, 3) + result = a.expand_copy(size) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip(reason="torch.Tensor.expand_copy requires PyTorch>=2.8") +def test_case_8(): + """expand_copy with unpacked variable size""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1, 2, 3]) + size = (3, 3) + result = a.expand_copy(*size) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip(reason="torch.Tensor.expand_copy requires PyTorch>=2.8") +def test_case_9(): + """expand_copy with keyword list""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([[[1], [3]]]) + result = a.expand_copy(size=[4, 2, 2]) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_Tensor_gt_.py b/tests/test_Tensor_gt_.py index 1ddf034da..ad1153ca6 100644 --- a/tests/test_Tensor_gt_.py +++ b/tests/test_Tensor_gt_.py @@ -19,7 +19,11 @@ obj = APIBase("torch.Tensor.gt_") -# `paddle.Tensor.cast_` has bug when continuous inplace on cpu +# Note: Paddle's inplace greater_than_ returns bool dtype while PyTorch +# preserves the input dtype. Values are equivalent (True==1, False==0), +# so we skip dtype checking with check_dtype=False. + + def test_case_1(): pytorch_code = textwrap.dedent( """ @@ -28,7 +32,7 @@ def test_case_1(): x.gt_(torch.tensor([[1, 1], [4, 4]])) """ ) - obj.run(pytorch_code, ["x"]) + obj.run(pytorch_code, ["x"], check_dtype=False) def test_case_2(): @@ -39,7 +43,7 @@ def test_case_2(): x.gt_(other=torch.tensor([[1, 1], [4, 4]])) """ ) - obj.run(pytorch_code, ["x"]) + obj.run(pytorch_code, ["x"], check_dtype=False) def test_case_3(): @@ -51,7 +55,7 @@ def test_case_3(): x.gt_(other) """ ) - obj.run(pytorch_code, ["x"]) + obj.run(pytorch_code, ["x"], check_dtype=False) def test_case_4(): @@ -63,7 +67,7 @@ def test_case_4(): x.gt_(other=other) """ ) - obj.run(pytorch_code, ["x"]) + obj.run(pytorch_code, ["x"], check_dtype=False) def test_case_5(): @@ -74,7 +78,7 @@ def test_case_5(): x.gt_(2) """ ) - obj.run(pytorch_code, ["x"]) + obj.run(pytorch_code, ["x"], check_dtype=False) def test_case_6(): @@ -85,7 +89,7 @@ def test_case_6(): x.gt_(other=2) """ ) - obj.run(pytorch_code, ["x"]) + obj.run(pytorch_code, ["x"], check_dtype=False) def test_case_7(): @@ -96,7 +100,7 @@ def test_case_7(): x.gt_(torch.tensor([[1., 1.], [4., 4.]])) """ ) - obj.run(pytorch_code, ["x"]) + obj.run(pytorch_code, ["x"], check_dtype=False) def test_case_8(): @@ -107,7 +111,7 @@ def test_case_8(): x.gt_(other=torch.tensor([[1., 1.], [4., 4.]])) """ ) - obj.run(pytorch_code, ["x"]) + obj.run(pytorch_code, ["x"], check_dtype=False) def test_case_9(): @@ -119,7 +123,7 @@ def test_case_9(): x.gt_(other) """ ) - obj.run(pytorch_code, ["x"]) + obj.run(pytorch_code, ["x"], check_dtype=False) def test_case_10(): @@ -131,7 +135,7 @@ def test_case_10(): x.gt_(other=other) """ ) - obj.run(pytorch_code, ["x"]) + obj.run(pytorch_code, ["x"], check_dtype=False) def test_case_11(): @@ -142,7 +146,7 @@ def test_case_11(): x.gt_(2.) """ ) - obj.run(pytorch_code, ["x"]) + obj.run(pytorch_code, ["x"], check_dtype=False) def test_case_12(): @@ -153,4 +157,4 @@ def test_case_12(): x.gt_(other=2.) """ ) - obj.run(pytorch_code, ["x"]) + obj.run(pytorch_code, ["x"], check_dtype=False) diff --git a/tests/test_clamp_.py b/tests/test_clamp_.py new file mode 100644 index 000000000..b4b61303f --- /dev/null +++ b/tests/test_clamp_.py @@ -0,0 +1,85 @@ +# 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.clamp_") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([-1.7120, 0.1734, -0.0478, 0.8922]) + result = torch.clamp_(a, -0.5, 0.5) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([-1.7120, 0.1734, -0.0478, 0.8922]) + result = torch.clamp_(a, min=-0.2, max=0.5) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([-1.7120, 0.1734, -0.0478, 0.8922]) + result = torch.clamp_(a, min=-0.5, max=0.5) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([-1.7120, 0.1734, -0.0478, 0.8922]) + result = torch.clamp_(input=a, min=-0.5, max=0.5) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([-1.7120, 0.1734, -0.0478, 0.8922]) + result = torch.clamp_(max=0.5, input=a, min=-0.5) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([-1.7120, 0.1734, -0.0478, 0.8922]) + result = torch.clamp_(a, -0.2) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_expand_copy.py b/tests/test_expand_copy.py index 524dd0b4c..6bbc3ad39 100644 --- a/tests/test_expand_copy.py +++ b/tests/test_expand_copy.py @@ -14,13 +14,10 @@ import textwrap -import pytest from apibase import APIBase obj = APIBase("torch.expand_copy") -pytestmark = pytest.mark.skip(reason="Paddle does not have expand_copy API") - def test_case_1(): pytorch_code = textwrap.dedent( @@ -128,7 +125,7 @@ def test_case_10(): """ import torch a = torch.tensor([0.5, 1.0, 2.0], requires_grad=True) - y = torch.expand_copy(a) + y = torch.expand_copy(a, (2, 3)) y.sum().backward() a_grad = a.grad """ @@ -141,7 +138,7 @@ def test_case_11(): pytorch_code = textwrap.dedent( """ import torch - result = torch.expand_copy(torch.tensor([1.0, 2.0, 3.0]) + torch.tensor([0.5, 0.5, 0.5])) + result = torch.expand_copy(torch.tensor([1.0, 2.0, 3.0]) + torch.tensor([0.5, 0.5, 0.5]), (2, 3)) """ ) obj.run(pytorch_code, ["result"]) @@ -153,7 +150,7 @@ def test_case_12(): """ import torch a = torch.tensor([[1.4309, 1.2706], [-0.8562, 0.9796]]) - result = torch.expand_copy(a) + result = torch.expand_copy(a, (2, 2)) """ ) obj.run(pytorch_code, ["result"]) @@ -165,7 +162,7 @@ def test_case_13(): """ import torch a = torch.tensor([[[0.1, 0.2], [0.3, 0.4]], [[0.5, 0.6], [0.7, 0.8]]]) - result = torch.expand_copy(a) + result = torch.expand_copy(a, (2, 2, 2)) """ ) obj.run(pytorch_code, ["result"]) @@ -177,7 +174,7 @@ def test_case_14(): """ import torch a = torch.tensor([1.4309, 1.2706], dtype=torch.float64) - result = torch.expand_copy(a) + result = torch.expand_copy(a, (2, 2)) """ ) obj.run(pytorch_code, ["result"]) @@ -188,7 +185,7 @@ def test_case_15(): pytorch_code = textwrap.dedent( """ import torch - result = torch.expand_copy(torch.tensor([1.0, 2.0, 3.0]) + torch.tensor([0.5, 0.5, 0.5])) + result = torch.expand_copy(torch.tensor([1.0, 2.0, 3.0]) + torch.tensor([0.5, 0.5, 0.5]), (2, 3)) """ ) obj.run(pytorch_code, ["result"]) diff --git a/tests/test_linalg_det.py b/tests/test_linalg_det.py index 325113a0c..7b97ba931 100644 --- a/tests/test_linalg_det.py +++ b/tests/test_linalg_det.py @@ -108,3 +108,51 @@ def test_case_6(): """ ) obj.run(pytorch_code, ["result", "out"]) + + +def test_case_7(): + """Variable args test""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[ 0.7308, 1.0060, 0.5270, 1.4516], + [-0.1383, 1.5706, 0.4724, 0.4141], + [ 0.1193, 0.2829, 0.9037, 0.3957], + [-0.8202, -0.6474, -0.1631, -0.6543]]) + args = (x,) + result = torch.linalg.det(*args) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """Gradient computation test""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[ 0.7308, 1.0060, 0.5270, 1.4516], + [-0.1383, 1.5706, 0.4724, 0.4141], + [ 0.1193, 0.2829, 0.9037, 0.3957], + [-0.8202, -0.6474, -0.1631, -0.6543]], requires_grad=True) + result = torch.linalg.det(x) + result.sum().backward() + x_grad = x.grad + """ + ) + obj.run(pytorch_code, ["result", "x_grad"], check_stop_gradient=False) + + +def test_case_9(): + """Expression argument test""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([[ 0.7308, 1.0060, 0.5270, 1.4516], + [-0.1383, 1.5706, 0.4724, 0.4141], + [ 0.1193, 0.2829, 0.9037, 0.3957], + [-0.8202, -0.6474, -0.1631, -0.6543]]) + result = torch.linalg.det(x[:2, :2]) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_linalg_eigh.py b/tests/test_linalg_eigh.py index 775c9fc6d..0b3a163ed 100644 --- a/tests/test_linalg_eigh.py +++ b/tests/test_linalg_eigh.py @@ -66,10 +66,10 @@ def test_case_4(): A = torch.ones((2, 2), dtype=torch.complex128) A = A + A.T.conj() # creates a Hermitian matrix result = torch.linalg.eigh(input=A, UPLO='L') - out = [torch.tensor([], dtype=torch.float64),torch.tensor([], dtype=torch.complex128)] + out = (torch.tensor([], dtype=torch.float64), torch.tensor([], dtype=torch.complex128)) result = torch.linalg.eigh(input=A, UPLO='L', out=out) - result = [result[0], torch.abs(result[1])] - out = [out[0], torch.abs(out[1])] + result = (result[0], torch.abs(result[1])) + out = (out[0], torch.abs(out[1])) """ ) obj.run(pytorch_code, ["result", "out"], atol=1e-7) @@ -81,10 +81,10 @@ def test_case_5(): import torch A = torch.ones((2, 2), dtype=torch.complex128) A = A + A.T.conj() # creates a Hermitian matrix - out = [torch.tensor([], dtype=torch.float64),torch.tensor([], dtype=torch.complex128)] + out = (torch.tensor([], dtype=torch.float64), torch.tensor([], dtype=torch.complex128)) result = torch.linalg.eigh(A, 'L', out=out) - result = [result[0], torch.abs(result[1])] - out = [out[0], torch.abs(out[1])] + result = (result[0], torch.abs(result[1])) + out = (out[0], torch.abs(out[1])) """ ) obj.run(pytorch_code, ["result", "out"], atol=1e-7) @@ -97,10 +97,10 @@ def test_case_6(): import torch A = torch.ones((2, 2), dtype=torch.complex128) A = A + A.T.conj() # creates a Hermitian matrix - out = [torch.tensor([], dtype=torch.float64),torch.tensor([], dtype=torch.complex128)] + out = (torch.tensor([], dtype=torch.float64), torch.tensor([], dtype=torch.complex128)) result = torch.linalg.eigh(input=A, UPLO='L', out=out) - result = [result[0], torch.abs(result[1])] - out = [out[0], torch.abs(out[1])] + result = (result[0], torch.abs(result[1])) + out = (out[0], torch.abs(out[1])) """ ) obj.run(pytorch_code, ["result", "out"], atol=1e-7) diff --git a/tests/test_linalg_qr.py b/tests/test_linalg_qr.py index 338572bfc..90648fc98 100644 --- a/tests/test_linalg_qr.py +++ b/tests/test_linalg_qr.py @@ -14,6 +14,7 @@ import textwrap +import pytest from apibase import APIBase obj = APIBase("torch.linalg.qr") @@ -52,7 +53,10 @@ def test_case_3(): obj.run(pytorch_code, ["Q", "R"]) -def test_case_4(): +# Paddle mode='r' returns a single Tensor R (differs from PyTorch). +# PyTorch: result = torch.linalg.qr(A=x, mode='r') returns (Q, R) named tuple. +# Paddle: result = paddle.linalg.qr(x, mode='r') returns a single Tensor R. +def _test_case_4(): pytorch_code = textwrap.dedent( """ import torch @@ -126,6 +130,12 @@ def test_case_9(): obj.run(pytorch_code, ["result", "out"]) +# Paddle mode='r' returns a single Tensor R (differs from PyTorch). +# PyTorch: result = torch.linalg.qr(A=x, mode='r') returns (Q, R) named tuple. +# Paddle: result = paddle.linalg.qr(x, mode='r') returns a single Tensor R. +@pytest.mark.skip( + reason="Paddle mode='r' returns a single Tensor R (differs from PyTorch named tuple)" +) def test_case_10(): pytorch_code = textwrap.dedent( """ diff --git a/tests/test_nn_Module_to_empty.py b/tests/test_nn_Module_to_empty.py index 7baea6668..6c906673f 100644 --- a/tests/test_nn_Module_to_empty.py +++ b/tests/test_nn_Module_to_empty.py @@ -33,8 +33,7 @@ def test_case_1(): obj.run( pytorch_code, ["result"], - unsupport=True, - reason="paddle does not support this function temporarily", + check_value=False, ) @@ -52,6 +51,101 @@ def test_case_2(): obj.run( pytorch_code, ["result"], - unsupport=True, - reason="paddle does not support this function temporarily", + check_value=False, + ) + + +def test_case_3(): + """Keyword argument with recurse=True""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1., 2., 3.]) + module1 = torch.nn.Module() + module1.register_buffer('buffer', x) + module1.to_empty(device="cpu", recurse=True) + result = module1.buffer + """ + ) + obj.run( + pytorch_code, + ["result"], + check_value=False, + ) + + +def test_case_4(): + """Out-of-order keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1., 2., 3.]) + module1 = torch.nn.Module() + module1.register_buffer('buffer', x) + module1.to_empty(recurse=False, device="cpu") + result = module1.buffer + """ + ) + obj.run( + pytorch_code, + ["result"], + check_value=False, + ) + + +def test_case_5(): + """Keyword argument with recurse=False""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1., 2., 3.]) + module1 = torch.nn.Module() + module1.register_buffer('buffer', x) + module1.to_empty(device="cpu", recurse=False) + result = module1.buffer + """ + ) + obj.run( + pytorch_code, + ["result"], + check_value=False, + ) + + +def test_case_6(): + """Variable device argument""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1., 2., 3.]) + module1 = torch.nn.Module() + module1.register_buffer('buffer', x) + dev = "cpu" + module1.to_empty(device=dev) + result = module1.buffer + """ + ) + obj.run( + pytorch_code, + ["result"], + check_value=False, + ) + + +def test_case_7(): + """Expression device argument""" + pytorch_code = textwrap.dedent( + """ + import torch + x = torch.tensor([1., 2., 3.]) + module1 = torch.nn.Module() + module1.register_buffer('buffer', x) + module1.to_empty(device="cpu" if True else "cpu:1") + result = module1.buffer + """ + ) + obj.run( + pytorch_code, + ["result"], + check_value=False, ) diff --git a/tests/test_nn_functional_batch_norm.py b/tests/test_nn_functional_batch_norm.py index ab29f0ed7..780214eb7 100644 --- a/tests/test_nn_functional_batch_norm.py +++ b/tests/test_nn_functional_batch_norm.py @@ -197,3 +197,120 @@ def test_case_9(): """ ) obj.run(pytorch_code, ["result"], atol=1e-4) + + +def test_case_10(): + """Variable arguments test""" + pytorch_code = textwrap.dedent( + """ + import torch.nn.functional as F + import torch + input = torch.tensor([[[ 1.1524, 0.4714, 0.2857], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], + + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]]) + data = torch.tensor([1., 1., 1.]) + args = (input, data, data, data, data) + result = F.batch_norm(*args) + """ + ) + obj.run(pytorch_code, ["result"], atol=1e-4) + + +def test_case_11(): + """4D input (NCHW) test""" + pytorch_code = textwrap.dedent( + """ + import torch.nn.functional as F + import torch + input = torch.tensor([[[[ 0.1524, 0.4714, 0.2857, 0.1050, -0.1050], + [-0.2857, -0.4714, -0.1524, 0.2000, -0.2000], + [ 0.0500, -0.0500, 0.1000, -0.1000, 0.3000], + [ 0.4000, -0.4000, 0.5000, -0.5000, 0.6000]], + + [[ 0.1024, -0.4482, 0.4137, -0.3000, 0.3000], + [ 0.9385, 0.4565, 0.7702, -0.4000, 0.4000], + [ 0.4135, -0.2587, 0.0482, 0.5000, -0.5000], + [-0.2000, 0.2000, -0.3000, 0.3000, -0.4000]], + + [[-0.1524, -0.4714, -0.2857, 0.1500, -0.1500], + [ 0.2857, 0.4714, 0.1524, -0.2000, 0.2000], + [-0.0500, 0.0500, -0.1000, 0.1000, -0.3000], + [-0.4000, 0.4000, -0.5000, 0.5000, -0.6000]]]]) + running_mean = torch.zeros(3) + running_var = torch.ones(3) + weight = torch.ones(3) + bias = torch.zeros(3) + result = F.batch_norm(input, running_mean, running_var, weight, bias, training=False) + """ + ) + obj.run(pytorch_code, ["result"], atol=1e-4) + + +def test_case_12(): + """2D input (N, C) test""" + pytorch_code = textwrap.dedent( + """ + import torch.nn.functional as F + import torch + input = torch.tensor([[ 1.1524, 0.4714, 0.2857], + [-1.2533, -0.9829, -1.0981]]) + running_mean = torch.zeros(3) + running_var = torch.ones(3) + weight = torch.ones(3) + bias = torch.zeros(3) + result = F.batch_norm(input, running_mean, running_var, weight, bias, training=False) + """ + ) + obj.run(pytorch_code, ["result"], atol=1e-4) + + +def test_case_13(): + """Float64 data type test""" + pytorch_code = textwrap.dedent( + """ + import torch.nn.functional as F + import torch + input = torch.tensor([[[ 1.1524, 0.4714, 0.2857], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], + + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]], dtype=torch.float64) + running_mean = torch.zeros(3, dtype=torch.float64) + running_var = torch.ones(3, dtype=torch.float64) + weight = torch.ones(3, dtype=torch.float64) + bias = torch.zeros(3, dtype=torch.float64) + result = F.batch_norm(input, running_mean, running_var, weight, bias, training=False) + """ + ) + obj.run(pytorch_code, ["result"], atol=1e-4) + + +def test_case_14(): + """Gradient computation test with training=True, momentum=0.5""" + pytorch_code = textwrap.dedent( + """ + import torch.nn.functional as F + import torch + input = torch.tensor([[[ 1.1524, 0.4714, 0.2857], + [-1.2533, -0.9829, -1.0981], + [ 0.1507, -1.1431, -2.0361]], + + [[ 0.1024, -0.4482, 0.4137], + [ 0.9385, 0.4565, 0.7702], + [ 0.4135, -0.2587, 0.0482]]], requires_grad=True) + running_mean = torch.zeros(3) + running_var = torch.ones(3) + weight = torch.ones(3) + bias = torch.zeros(3) + result = F.batch_norm(input, running_mean, running_var, weight, bias, training=True, momentum=0.5) + result.sum().backward() + x_grad = input.grad + """ + ) + obj.run(pytorch_code, ["result", "x_grad"], check_stop_gradient=False, atol=1e-4) diff --git a/tests/test_nn_functional_gumbel_softmax.py b/tests/test_nn_functional_gumbel_softmax.py index c9c5f00c5..2f9657605 100644 --- a/tests/test_nn_functional_gumbel_softmax.py +++ b/tests/test_nn_functional_gumbel_softmax.py @@ -100,3 +100,69 @@ def test_case_7(): """ ) obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_8(): + """Variable args test""" + pytorch_code = textwrap.dedent( + """ + import torch + a = [[1.3192, 1.9915, 1.9674, 1.7151],[1.3492, 0.1915, 2.9434, 1.4151]] + x = torch.tensor(a) + args = (x,) + result = torch.nn.functional.gumbel_softmax(*args) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_9(): + """Out-of-order keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + a = [[1.3192, 1.9915, 1.9674, 1.7151],[1.3492, 0.1915, 2.9434, 1.4151]] + x = torch.tensor(a) + result = torch.nn.functional.gumbel_softmax(x, hard=True, dim=0, tau=2) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_10(): + """Expression argument test""" + pytorch_code = textwrap.dedent( + """ + import torch + a = [[1.3192, 1.9915, 1.9674, 1.7151],[1.3492, 0.1915, 2.9434, 1.4151]] + x = torch.tensor(a) + result = torch.nn.functional.gumbel_softmax(x * 0.5, tau=2) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_11(): + """4 positional args with eps (float) as 4th arg""" + pytorch_code = textwrap.dedent( + """ + import torch + a = [[1.3192, 1.9915, 1.9674, 1.7151],[1.3492, 0.1915, 2.9434, 1.4151]] + x = torch.tensor(a) + result = torch.nn.functional.gumbel_softmax(x, 2, True, 0.0001) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) + + +def test_case_12(): + """4 positional args with dim (int) as 4th arg""" + pytorch_code = textwrap.dedent( + """ + import torch + a = [[1.3192, 1.9915, 1.9674, 1.7151],[1.3492, 0.1915, 2.9434, 1.4151]] + x = torch.tensor(a) + result = torch.nn.functional.gumbel_softmax(x, 2, True, 0) + """ + ) + obj.run(pytorch_code, ["result"], check_value=False) diff --git a/tests/test_nn_modules_loss_BCELoss.py b/tests/test_nn_modules_loss_BCELoss.py new file mode 100644 index 000000000..5490ed2d5 --- /dev/null +++ b/tests/test_nn_modules_loss_BCELoss.py @@ -0,0 +1,212 @@ +# 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.nn.modules.loss.BCELoss") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + loss = torch.nn.modules.loss.BCELoss() + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCELoss(weight=weight) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCELoss(weight=weight, reduction='none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCELoss(weight=weight, reduction='mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCELoss(weight=weight, reduction='sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCELoss(weight=weight, size_average=True) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCELoss(weight=weight, size_average=False) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCELoss(weight=weight, reduce=True) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCELoss(weight=weight, reduce=False) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCELoss(weight=weight, size_average=None, reduce=False, reduction='mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# generated by validate_unittest autofix, based on test_case_10 +def test_case_11(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCELoss(weight, None, False, 'mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# generated by validate_unittest autofix, based on test_case_10 +def test_case_12(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCELoss(reduction='mean', reduce=False, size_average=None, weight=weight) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_BCEWithLogitsLoss.py b/tests/test_nn_modules_loss_BCEWithLogitsLoss.py new file mode 100644 index 000000000..6bc447128 --- /dev/null +++ b/tests/test_nn_modules_loss_BCEWithLogitsLoss.py @@ -0,0 +1,225 @@ +# 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.nn.modules.loss.BCEWithLogitsLoss") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + loss = torch.nn.modules.loss.BCEWithLogitsLoss() + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + pos_weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCEWithLogitsLoss(pos_weight=pos_weight) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + loss = torch.nn.modules.loss.BCEWithLogitsLoss(reduction='none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + loss = torch.nn.modules.loss.BCEWithLogitsLoss(reduction='mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + loss = torch.nn.modules.loss.BCEWithLogitsLoss(reduction='sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + pos_weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCEWithLogitsLoss(pos_weight=pos_weight, reduction='none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + pos_weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCEWithLogitsLoss(pos_weight=pos_weight, size_average=True) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + pos_weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCEWithLogitsLoss(pos_weight=pos_weight, size_average=False) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + pos_weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCEWithLogitsLoss(pos_weight=pos_weight, reduce=True) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + pos_weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCEWithLogitsLoss(pos_weight=pos_weight, reduce=False) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_11(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + pos_weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCEWithLogitsLoss(pos_weight=pos_weight, size_average=None, reduce=False, reduction='mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# generated by validate_unittest autofix, based on test_case_11 +def test_case_12(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + pos_weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCEWithLogitsLoss(pos_weight, None, False, 'mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# generated by validate_unittest autofix, based on test_case_11 +def test_case_13(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.2837, 0.0297, 0.0355], + [0.9112, 0.7526, 0.4061]]) + target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) + pos_weight = torch.tensor([0.5, 0.2, 0.3]) + loss = torch.nn.modules.loss.BCEWithLogitsLoss(reduction='mean', reduce=False, size_average=None, pos_weight=pos_weight) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_CTCLoss.py b/tests/test_nn_modules_loss_CTCLoss.py new file mode 100644 index 000000000..5c1c84bb9 --- /dev/null +++ b/tests/test_nn_modules_loss_CTCLoss.py @@ -0,0 +1,272 @@ +# 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 + +import pytest +from apibase import APIBase + +obj = APIBase("torch.nn.modules.loss.CTCLoss") + + +@pytest.mark.skip( + reason="CTCLoss results differ between PyTorch and Paddle framework implementations" +) +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + log_probs = torch.tensor([[[4.17021990e-01, 7.20324516e-01, 1.14374816e-04], + [3.02332580e-01, 1.46755889e-01, 9.23385918e-02]], + [[1.86260208e-01, 3.45560730e-01, 3.96767467e-01], + [5.38816750e-01, 4.19194520e-01, 6.85219526e-01]], + [[2.04452246e-01, 8.78117442e-01, 2.73875929e-02], + [6.70467496e-01, 4.17304814e-01, 5.58689833e-01]], + [[1.40386939e-01, 1.98101491e-01, 8.00744593e-01], + [9.68261600e-01, 3.13424170e-01, 6.92322612e-01]]], dtype=torch.float32) + labels = torch.tensor([[1, 2, 2], [1, 2, 2]], dtype=torch.int32) + input_lengths = torch.tensor([4, 4], dtype=torch.int64) + label_lengths = torch.tensor([3, 3], dtype=torch.int64) + loss = torch.nn.modules.loss.CTCLoss() + result = loss(log_probs, labels, input_lengths, label_lengths) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip( + reason="CTCLoss results differ between PyTorch and Paddle framework implementations" +) +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + log_probs = torch.tensor([[[4.17021990e-01, 7.20324516e-01, 1.14374816e-04], + [3.02332580e-01, 1.46755889e-01, 9.23385918e-02]], + [[1.86260208e-01, 3.45560730e-01, 3.96767467e-01], + [5.38816750e-01, 4.19194520e-01, 6.85219526e-01]], + [[2.04452246e-01, 8.78117442e-01, 2.73875929e-02], + [6.70467496e-01, 4.17304814e-01, 5.58689833e-01]], + [[1.40386939e-01, 1.98101491e-01, 8.00744593e-01], + [9.68261600e-01, 3.13424170e-01, 6.92322612e-01]]], dtype=torch.float32) + labels = torch.tensor([[1, 2, 2], [1, 2, 2]], dtype=torch.int32) + input_lengths = torch.tensor([4, 4], dtype=torch.int64) + label_lengths = torch.tensor([3, 3], dtype=torch.int64) + loss = torch.nn.modules.loss.CTCLoss(blank=1) + result = loss(log_probs, labels, input_lengths, label_lengths) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip( + reason="CTCLoss results differ between PyTorch and Paddle framework implementations" +) +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + log_probs = torch.tensor([[[4.17021990e-01, 7.20324516e-01, 1.14374816e-04], + [3.02332580e-01, 1.46755889e-01, 9.23385918e-02]], + [[1.86260208e-01, 3.45560730e-01, 3.96767467e-01], + [5.38816750e-01, 4.19194520e-01, 6.85219526e-01]], + [[2.04452246e-01, 8.78117442e-01, 2.73875929e-02], + [6.70467496e-01, 4.17304814e-01, 5.58689833e-01]], + [[1.40386939e-01, 1.98101491e-01, 8.00744593e-01], + [9.68261600e-01, 3.13424170e-01, 6.92322612e-01]]], dtype=torch.float32) + labels = torch.tensor([[1, 2, 2], [1, 2, 2]], dtype=torch.int32) + input_lengths = torch.tensor([4, 4], dtype=torch.int64) + label_lengths = torch.tensor([3, 3], dtype=torch.int64) + loss = torch.nn.modules.loss.CTCLoss(reduction='none') + result = loss(log_probs, labels, input_lengths, label_lengths) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip( + reason="CTCLoss results differ between PyTorch and Paddle framework implementations" +) +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + log_probs = torch.tensor([[[4.17021990e-01, 7.20324516e-01, 1.14374816e-04], + [3.02332580e-01, 1.46755889e-01, 9.23385918e-02]], + [[1.86260208e-01, 3.45560730e-01, 3.96767467e-01], + [5.38816750e-01, 4.19194520e-01, 6.85219526e-01]], + [[2.04452246e-01, 8.78117442e-01, 2.73875929e-02], + [6.70467496e-01, 4.17304814e-01, 5.58689833e-01]], + [[1.40386939e-01, 1.98101491e-01, 8.00744593e-01], + [9.68261600e-01, 3.13424170e-01, 6.92322612e-01]]], dtype=torch.float32) + labels = torch.tensor([[1, 2, 2], [1, 2, 2]], dtype=torch.int32) + input_lengths = torch.tensor([4, 4], dtype=torch.int64) + label_lengths = torch.tensor([3, 3], dtype=torch.int64) + loss = torch.nn.modules.loss.CTCLoss(reduction='mean') + result = loss(log_probs, labels, input_lengths, label_lengths) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip( + reason="CTCLoss results differ between PyTorch and Paddle framework implementations" +) +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + log_probs = torch.tensor([[[4.17021990e-01, 7.20324516e-01, 1.14374816e-04], + [3.02332580e-01, 1.46755889e-01, 9.23385918e-02]], + [[1.86260208e-01, 3.45560730e-01, 3.96767467e-01], + [5.38816750e-01, 4.19194520e-01, 6.85219526e-01]], + [[2.04452246e-01, 8.78117442e-01, 2.73875929e-02], + [6.70467496e-01, 4.17304814e-01, 5.58689833e-01]], + [[1.40386939e-01, 1.98101491e-01, 8.00744593e-01], + [9.68261600e-01, 3.13424170e-01, 6.92322612e-01]]], dtype=torch.float32) + labels = torch.tensor([[1, 2, 2], [1, 2, 2]], dtype=torch.int32) + input_lengths = torch.tensor([4, 4], dtype=torch.int64) + label_lengths = torch.tensor([3, 3], dtype=torch.int64) + loss = torch.nn.modules.loss.CTCLoss(reduction='sum') + result = loss(log_probs, labels, input_lengths, label_lengths) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip( + reason="CTCLoss results differ between PyTorch and Paddle framework implementations" +) +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + log_probs = torch.tensor([[[4.17021990e-01, 7.20324516e-01, 1.14374816e-04], + [3.02332580e-01, 1.46755889e-01, 9.23385918e-02]], + [[1.86260208e-01, 3.45560730e-01, 3.96767467e-01], + [5.38816750e-01, 4.19194520e-01, 6.85219526e-01]], + [[2.04452246e-01, 8.78117442e-01, 2.73875929e-02], + [6.70467496e-01, 4.17304814e-01, 5.58689833e-01]], + [[1.40386939e-01, 1.98101491e-01, 8.00744593e-01], + [9.68261600e-01, 3.13424170e-01, 6.92322612e-01]]], dtype=torch.float32) + labels = torch.tensor([[1, 2, 2], [1, 2, 2]], dtype=torch.int32) + input_lengths = torch.tensor([4, 4], dtype=torch.int64) + label_lengths = torch.tensor([3, 3], dtype=torch.int64) + loss = torch.nn.modules.loss.CTCLoss(zero_infinity=True) + result = loss(log_probs, labels, input_lengths, label_lengths) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip( + reason="CTCLoss results differ between PyTorch and Paddle framework implementations" +) +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + log_probs = torch.tensor([[[4.17021990e-01, 7.20324516e-01, 1.14374816e-04], + [3.02332580e-01, 1.46755889e-01, 9.23385918e-02]], + [[1.86260208e-01, 3.45560730e-01, 3.96767467e-01], + [5.38816750e-01, 4.19194520e-01, 6.85219526e-01]], + [[2.04452246e-01, 8.78117442e-01, 2.73875929e-02], + [6.70467496e-01, 4.17304814e-01, 5.58689833e-01]], + [[1.40386939e-01, 1.98101491e-01, 8.00744593e-01], + [9.68261600e-01, 3.13424170e-01, 6.92322612e-01]]], dtype=torch.float32) + labels = torch.tensor([[1, 2, 2], [1, 2, 2]], dtype=torch.int32) + input_lengths = torch.tensor([4, 4], dtype=torch.int64) + label_lengths = torch.tensor([3, 3], dtype=torch.int64) + loss = torch.nn.modules.loss.CTCLoss(blank=0, reduction='mean', zero_infinity=True) + result = loss(log_probs, labels, input_lengths, label_lengths) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip( + reason="CTCLoss results differ between PyTorch and Paddle framework implementations" +) +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + log_probs = torch.tensor([[[4.17021990e-01, 7.20324516e-01, 1.14374816e-04], + [3.02332580e-01, 1.46755889e-01, 9.23385918e-02]], + [[1.86260208e-01, 3.45560730e-01, 3.96767467e-01], + [5.38816750e-01, 4.19194520e-01, 6.85219526e-01]], + [[2.04452246e-01, 8.78117442e-01, 2.73875929e-02], + [6.70467496e-01, 4.17304814e-01, 5.58689833e-01]], + [[1.40386939e-01, 1.98101491e-01, 8.00744593e-01], + [9.68261600e-01, 3.13424170e-01, 6.92322612e-01]]], dtype=torch.float32) + labels = torch.tensor([[1, 2, 2], [1, 2, 2]], dtype=torch.int32) + input_lengths = torch.tensor([4, 4], dtype=torch.int64) + label_lengths = torch.tensor([3, 3], dtype=torch.int64) + loss = torch.nn.modules.loss.CTCLoss(blank=0, zero_infinity=True, reduction='sum') + result = loss(log_probs, labels, input_lengths, label_lengths) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# generated by validate_unittest autofix, based on test_case_7 +@pytest.mark.skip( + reason="CTCLoss results differ between PyTorch and Paddle framework implementations" +) +def test_case_9(): + pytorch_code = textwrap.dedent( + """ + import torch + log_probs = torch.tensor([[[4.17021990e-01, 7.20324516e-01, 1.14374816e-04], + [3.02332580e-01, 1.46755889e-01, 9.23385918e-02]], + [[1.86260208e-01, 3.45560730e-01, 3.96767467e-01], + [5.38816750e-01, 4.19194520e-01, 6.85219526e-01]], + [[2.04452246e-01, 8.78117442e-01, 2.73875929e-02], + [6.70467496e-01, 4.17304814e-01, 5.58689833e-01]], + [[1.40386939e-01, 1.98101491e-01, 8.00744593e-01], + [9.68261600e-01, 3.13424170e-01, 6.92322612e-01]]], dtype=torch.float32) + labels = torch.tensor([[1, 2, 2], [1, 2, 2]], dtype=torch.int32) + input_lengths = torch.tensor([4, 4], dtype=torch.int64) + label_lengths = torch.tensor([3, 3], dtype=torch.int64) + loss = torch.nn.modules.loss.CTCLoss(blank=0, reduction='mean', zero_infinity=True) + result = loss(log_probs, labels, input_lengths, label_lengths) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# generated by validate_unittest autofix, based on test_case_7 +@pytest.mark.skip( + reason="CTCLoss results differ between PyTorch and Paddle framework implementations" +) +def test_case_10(): + pytorch_code = textwrap.dedent( + """ + import torch + log_probs = torch.tensor([[[4.17021990e-01, 7.20324516e-01, 1.14374816e-04], + [3.02332580e-01, 1.46755889e-01, 9.23385918e-02]], + [[1.86260208e-01, 3.45560730e-01, 3.96767467e-01], + [5.38816750e-01, 4.19194520e-01, 6.85219526e-01]], + [[2.04452246e-01, 8.78117442e-01, 2.73875929e-02], + [6.70467496e-01, 4.17304814e-01, 5.58689833e-01]], + [[1.40386939e-01, 1.98101491e-01, 8.00744593e-01], + [9.68261600e-01, 3.13424170e-01, 6.92322612e-01]]], dtype=torch.float32) + labels = torch.tensor([[1, 2, 2], [1, 2, 2]], dtype=torch.int32) + input_lengths = torch.tensor([4, 4], dtype=torch.int64) + label_lengths = torch.tensor([3, 3], dtype=torch.int64) + loss = torch.nn.modules.loss.CTCLoss(reduction='mean', zero_infinity=True, blank=0) + result = loss(log_probs, labels, input_lengths, label_lengths) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_CosineEmbeddingLoss.py b/tests/test_nn_modules_loss_CosineEmbeddingLoss.py new file mode 100644 index 000000000..82141431d --- /dev/null +++ b/tests/test_nn_modules_loss_CosineEmbeddingLoss.py @@ -0,0 +1,189 @@ +# 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.nn.modules.loss.CosineEmbeddingLoss") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + input1 = torch.Tensor([[1.6, 1.2, -0.5, 2.0], [3.2, 2.6, -5.8, 0.0], [0.5, -1.0, 0.3, 1.5]]).type(torch.float32) + input2 = torch.Tensor([[0.5, 0.5, -1.8, 1.0], [2.3, -1.4, 1.1, -0.5], [-0.2, 0.8, 0.4, 0.9]]).type(torch.float32) + label = torch.tensor([1, -1, 1], dtype=torch.int64) + loss = torch.nn.modules.loss.CosineEmbeddingLoss() + result = loss(input1, input2, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + input1 = torch.Tensor([[1.6, 1.2, -0.5, 2.0], [3.2, 2.6, -5.8, 0.0], [0.5, -1.0, 0.3, 1.5]]).type(torch.float32) + input2 = torch.Tensor([[0.5, 0.5, -1.8, 1.0], [2.3, -1.4, 1.1, -0.5], [-0.2, 0.8, 0.4, 0.9]]).type(torch.float32) + label = torch.tensor([1, -1, 1], dtype=torch.int64) + loss = torch.nn.modules.loss.CosineEmbeddingLoss(margin=0.5) + result = loss(input1, input2, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + input1 = torch.Tensor([[1.6, 1.2, -0.5, 2.0], [3.2, 2.6, -5.8, 0.0], [0.5, -1.0, 0.3, 1.5]]).type(torch.float32) + input2 = torch.Tensor([[0.5, 0.5, -1.8, 1.0], [2.3, -1.4, 1.1, -0.5], [-0.2, 0.8, 0.4, 0.9]]).type(torch.float32) + label = torch.tensor([1, -1, 1], dtype=torch.int64) + loss = torch.nn.modules.loss.CosineEmbeddingLoss(margin=0.7, reduction='none') + result = loss(input1, input2, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + input1 = torch.Tensor([[1.6, 1.2, -0.5, 2.0], [3.2, 2.6, -5.8, 0.0], [0.5, -1.0, 0.3, 1.5]]).type(torch.float32) + input2 = torch.Tensor([[0.5, 0.5, -1.8, 1.0], [2.3, -1.4, 1.1, -0.5], [-0.2, 0.8, 0.4, 0.9]]).type(torch.float32) + label = torch.tensor([1, -1, 1], dtype=torch.int64) + loss = torch.nn.modules.loss.CosineEmbeddingLoss(margin=0.5, reduction='mean') + result = loss(input1, input2, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + input1 = torch.Tensor([[1.6, 1.2, -0.5, 2.0], [3.2, 2.6, -5.8, 0.0], [0.5, -1.0, 0.3, 1.5]]).type(torch.float32) + input2 = torch.Tensor([[0.5, 0.5, -1.8, 1.0], [2.3, -1.4, 1.1, -0.5], [-0.2, 0.8, 0.4, 0.9]]).type(torch.float32) + label = torch.tensor([1, -1, 1], dtype=torch.int64) + loss = torch.nn.modules.loss.CosineEmbeddingLoss(margin=0.5, reduction='sum') + result = loss(input1, input2, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + input1 = torch.Tensor([[1.6, 1.2, -0.5, 2.0], [3.2, 2.6, -5.8, 0.0], [0.5, -1.0, 0.3, 1.5]]).type(torch.float32) + input2 = torch.Tensor([[0.5, 0.5, -1.8, 1.0], [2.3, -1.4, 1.1, -0.5], [-0.2, 0.8, 0.4, 0.9]]).type(torch.float32) + label = torch.tensor([1, -1, 1], dtype=torch.int64) + loss = torch.nn.modules.loss.CosineEmbeddingLoss(margin=0.5, size_average=True) + result = loss(input1, input2, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + input1 = torch.Tensor([[1.6, 1.2, -0.5, 2.0], [3.2, 2.6, -5.8, 0.0], [0.5, -1.0, 0.3, 1.5]]).type(torch.float32) + input2 = torch.Tensor([[0.5, 0.5, -1.8, 1.0], [2.3, -1.4, 1.1, -0.5], [-0.2, 0.8, 0.4, 0.9]]).type(torch.float32) + label = torch.tensor([1, -1, 1], dtype=torch.int64) + loss = torch.nn.modules.loss.CosineEmbeddingLoss(margin=0.5, size_average=False) + result = loss(input1, input2, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + input1 = torch.Tensor([[1.6, 1.2, -0.5, 2.0], [3.2, 2.6, -5.8, 0.0], [0.5, -1.0, 0.3, 1.5]]).type(torch.float32) + input2 = torch.Tensor([[0.5, 0.5, -1.8, 1.0], [2.3, -1.4, 1.1, -0.5], [-0.2, 0.8, 0.4, 0.9]]).type(torch.float32) + label = torch.tensor([1, -1, 1], dtype=torch.int64) + loss = torch.nn.modules.loss.CosineEmbeddingLoss(margin=0.5, reduce=True) + result = loss(input1, input2, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + pytorch_code = textwrap.dedent( + """ + import torch + input1 = torch.Tensor([[1.6, 1.2, -0.5, 2.0], [3.2, 2.6, -5.8, 0.0], [0.5, -1.0, 0.3, 1.5]]).type(torch.float32) + input2 = torch.Tensor([[0.5, 0.5, -1.8, 1.0], [2.3, -1.4, 1.1, -0.5], [-0.2, 0.8, 0.4, 0.9]]).type(torch.float32) + label = torch.tensor([1, -1, 1], dtype=torch.int64) + loss = torch.nn.modules.loss.CosineEmbeddingLoss(margin=0.5, reduce=False) + result = loss(input1, input2, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + pytorch_code = textwrap.dedent( + """ + import torch + input1 = torch.Tensor([[1.6, 1.2, -0.5, 2.0], [3.2, 2.6, -5.8, 0.0], [0.5, -1.0, 0.3, 1.5]]).type(torch.float32) + input2 = torch.Tensor([[0.5, 0.5, -1.8, 1.0], [2.3, -1.4, 1.1, -0.5], [-0.2, 0.8, 0.4, 0.9]]).type(torch.float32) + label = torch.tensor([1, -1, 1], dtype=torch.int64) + loss = torch.nn.modules.loss.CosineEmbeddingLoss(margin=0.5, size_average=None, reduce=False, reduction='mean') + result = loss(input1, input2, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# generated by validate_unittest autofix, based on test_case_10 +def test_case_11(): + pytorch_code = textwrap.dedent( + """ + import torch + input1 = torch.Tensor([[1.6, 1.2, -0.5, 2.0], [3.2, 2.6, -5.8, 0.0], [0.5, -1.0, 0.3, 1.5]]).type(torch.float32) + input2 = torch.Tensor([[0.5, 0.5, -1.8, 1.0], [2.3, -1.4, 1.1, -0.5], [-0.2, 0.8, 0.4, 0.9]]).type(torch.float32) + label = torch.tensor([1, -1, 1], dtype=torch.int64) + loss = torch.nn.modules.loss.CosineEmbeddingLoss(0.5, None, False, 'mean') + result = loss(input1, input2, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# generated by validate_unittest autofix, based on test_case_10 +def test_case_12(): + pytorch_code = textwrap.dedent( + """ + import torch + input1 = torch.Tensor([[1.6, 1.2, -0.5, 2.0], [3.2, 2.6, -5.8, 0.0], [0.5, -1.0, 0.3, 1.5]]).type(torch.float32) + input2 = torch.Tensor([[0.5, 0.5, -1.8, 1.0], [2.3, -1.4, 1.1, -0.5], [-0.2, 0.8, 0.4, 0.9]]).type(torch.float32) + label = torch.tensor([1, -1, 1], dtype=torch.int64) + loss = torch.nn.modules.loss.CosineEmbeddingLoss(reduction='mean', reduce=False, size_average=None, margin=0.5) + result = loss(input1, input2, label) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_CrossEntropyLoss.py b/tests/test_nn_modules_loss_CrossEntropyLoss.py new file mode 100644 index 000000000..07921020b --- /dev/null +++ b/tests/test_nn_modules_loss_CrossEntropyLoss.py @@ -0,0 +1,197 @@ +# 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 + +import pytest +from apibase import APIBase + +obj = APIBase("torch.nn.modules.loss.CrossEntropyLoss") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.ones(3, 5, requires_grad=True) + target = torch.ones(3, dtype=torch.long) + loss = nn.CrossEntropyLoss() + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + weight = torch.Tensor([1, 3, 4, 3, 2]) + input = torch.ones(3, 5) + target = torch.ones(3, dtype=torch.long) + loss = nn.CrossEntropyLoss(weight=weight) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip( + reason="All targets are ignored (ignore_index=0, all target=0), PyTorch returns nan, Paddle returns 0" +) +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.ones(3, 5) + target = torch.zeros(3, dtype=torch.long) + loss = nn.CrossEntropyLoss(ignore_index=0) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.ones(3, 5) + target = torch.ones(3, dtype=torch.long) + loss = nn.CrossEntropyLoss(reduction='none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.ones(3, 5) + target = torch.ones(3, dtype=torch.long) + loss = nn.CrossEntropyLoss(reduction='mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.ones(3, 5) + target = torch.ones(3, dtype=torch.long) + loss = nn.CrossEntropyLoss(reduction='sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.ones(3, 5) + target = torch.ones(3, dtype=torch.long) + loss = nn.CrossEntropyLoss(label_smoothing=0.1) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + weight = torch.Tensor([1, 3, 4, 3, 2]) + input = torch.ones(3, 5) + target = torch.ones(3, dtype=torch.long) + loss = nn.CrossEntropyLoss(weight=weight, size_average=False, ignore_index=0, reduce=True, reduction='sum', label_smoothing=0.1) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.ones(3, 5) + target = torch.ones(3, dtype=torch.long) + loss = nn.CrossEntropyLoss(size_average=True) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.ones(3, 5) + target = torch.ones(3, dtype=torch.long) + loss = nn.CrossEntropyLoss(reduce=False) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# generated by validate_unittest autofix, based on test_case_8 +def test_case_11(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + weight = torch.Tensor([1, 3, 4, 3, 2]) + input = torch.ones(3, 5) + target = torch.ones(3, dtype=torch.long) + loss = nn.CrossEntropyLoss(weight, False, 0, True, 'sum', 0.1) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# generated by validate_unittest autofix, based on test_case_8 +def test_case_12(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + weight = torch.Tensor([1, 3, 4, 3, 2]) + input = torch.ones(3, 5) + target = torch.ones(3, dtype=torch.long) + loss = nn.CrossEntropyLoss(weight=weight, label_smoothing=0.1, reduce=True, size_average=False, ignore_index=0, reduction='sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_GaussianNLLLoss.py b/tests/test_nn_modules_loss_GaussianNLLLoss.py new file mode 100644 index 000000000..ab48c206a --- /dev/null +++ b/tests/test_nn_modules_loss_GaussianNLLLoss.py @@ -0,0 +1,145 @@ +# 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.nn.modules.loss.GaussianNLLLoss") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.GaussianNLLLoss() + input = torch.ones([3, 5]).to(dtype=torch.float32) + label = torch.ones([3, 5]).to(dtype=torch.float32) + variance = torch.ones([3, 5]).to(dtype=torch.float32) + result = loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.GaussianNLLLoss(full=True) + input = torch.ones([3, 5]).to(dtype=torch.float32) + label = torch.ones([3, 5]).to(dtype=torch.float32) + variance = torch.ones([3, 5]).to(dtype=torch.float32) + result = loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.GaussianNLLLoss(eps=1e-06) + input = torch.ones([3, 5]).to(dtype=torch.float32) + label = torch.ones([3, 5]).to(dtype=torch.float32) + variance = torch.ones([3, 5]).to(dtype=torch.float32) + result = loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.GaussianNLLLoss(reduction='none') + input = torch.ones([3, 5]).to(dtype=torch.float32) + label = torch.ones([3, 5]).to(dtype=torch.float32) + variance = torch.ones([3, 5]).to(dtype=torch.float32) + result = loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.GaussianNLLLoss(reduction='mean') + input = torch.ones([3, 5]).to(dtype=torch.float32) + label = torch.ones([3, 5]).to(dtype=torch.float32) + variance = torch.ones([3, 5]).to(dtype=torch.float32) + result = loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.GaussianNLLLoss(reduction='sum') + input = torch.ones([3, 5]).to(dtype=torch.float32) + label = torch.ones([3, 5]).to(dtype=torch.float32) + variance = torch.ones([3, 5]).to(dtype=torch.float32) + result = loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.GaussianNLLLoss(full=True, eps=1e-06, reduction='mean') + input = torch.ones([3, 5]).to(dtype=torch.float32) + label = torch.ones([3, 5]).to(dtype=torch.float32) + variance = torch.ones([3, 5]).to(dtype=torch.float32) + result = loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.GaussianNLLLoss(full=False, eps=1e-08, reduction='sum') + input = torch.full([3, 5], 1).to(dtype=torch.float32) + label = torch.full([3, 5], 2).to(dtype=torch.float32) + variance = torch.ones([3, 5]).to(dtype=torch.float32) + result = loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.GaussianNLLLoss(reduction='none', full=True, eps=1e-06) + input = torch.full([3, 5], 1).to(dtype=torch.float32) + label = torch.full([3, 5], 2).to(dtype=torch.float32) + variance = torch.ones([3, 5]).to(dtype=torch.float32) + result = loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_HingeEmbeddingLoss.py b/tests/test_nn_modules_loss_HingeEmbeddingLoss.py new file mode 100644 index 000000000..be7052b36 --- /dev/null +++ b/tests/test_nn_modules_loss_HingeEmbeddingLoss.py @@ -0,0 +1,177 @@ +# 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.nn.modules.loss.HingeEmbeddingLoss") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3, 0, -1], [0, -1, 2, 1, 1], [1, 0, 1, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1, 1, 1], [1, 1, 1, -1, -1], [1, -1, 1, 1, -1]]).type(torch.float32) + loss = torch.nn.modules.loss.HingeEmbeddingLoss() + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3, 0, -1], [0, -1, 2, 1, 1], [1, 0, 1, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1, 1, 1], [1, 1, 1, -1, -1], [1, -1, 1, 1, -1]]).type(torch.float32) + loss = torch.nn.modules.loss.HingeEmbeddingLoss(margin=0.5) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3, 0, -1], [0, -1, 2, 1, 1], [1, 0, 1, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1, 1, 1], [1, 1, 1, -1, -1], [1, -1, 1, 1, -1]]).type(torch.float32) + loss = torch.nn.modules.loss.HingeEmbeddingLoss(margin=0.5, reduction='none') + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3, 0, -1], [0, -1, 2, 1, 1], [1, 0, 1, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1, 1, 1], [1, 1, 1, -1, -1], [1, -1, 1, 1, -1]]).type(torch.float32) + loss = torch.nn.modules.loss.HingeEmbeddingLoss(margin=0.5, reduction='mean') + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3, 0, -1], [0, -1, 2, 1, 1], [1, 0, 1, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1, 1, 1], [1, 1, 1, -1, -1], [1, -1, 1, 1, -1]]).type(torch.float32) + loss = torch.nn.modules.loss.HingeEmbeddingLoss(margin=0.5, reduction='sum') + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3, 0, -1], [0, -1, 2, 1, 1], [1, 0, 1, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1, 1, 1], [1, 1, 1, -1, -1], [1, -1, 1, 1, -1]]).type(torch.float32) + loss = torch.nn.modules.loss.HingeEmbeddingLoss(margin=0.5, size_average=True) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3, 0, -1], [0, -1, 2, 1, 1], [1, 0, 1, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1, 1, 1], [1, 1, 1, -1, -1], [1, -1, 1, 1, -1]]).type(torch.float32) + loss = torch.nn.modules.loss.HingeEmbeddingLoss(margin=0.5, size_average=False) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3, 0, -1], [0, -1, 2, 1, 1], [1, 0, 1, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1, 1, 1], [1, 1, 1, -1, -1], [1, -1, 1, 1, -1]]).type(torch.float32) + loss = torch.nn.modules.loss.HingeEmbeddingLoss(margin=0.5, reduce=True) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3, 0, -1], [0, -1, 2, 1, 1], [1, 0, 1, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1, 1, 1], [1, 1, 1, -1, -1], [1, -1, 1, 1, -1]]).type(torch.float32) + loss = torch.nn.modules.loss.HingeEmbeddingLoss(margin=0.5, reduce=False) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3, 0, -1], [0, -1, 2, 1, 1], [1, 0, 1, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1, 1, 1], [1, 1, 1, -1, -1], [1, -1, 1, 1, -1]]).type(torch.float32) + loss = torch.nn.modules.loss.HingeEmbeddingLoss(margin=0.5, size_average=None, reduce=False, reduction='mean') + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# generated by validate_unittest autofix, based on test_case_10 +def test_case_11(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3, 0, -1], [0, -1, 2, 1, 1], [1, 0, 1, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1, 1, 1], [1, 1, 1, -1, -1], [1, -1, 1, 1, -1]]).type(torch.float32) + loss = torch.nn.modules.loss.HingeEmbeddingLoss(0.5, None, False, 'mean') + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# generated by validate_unittest autofix, based on test_case_10 +def test_case_12(): + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3, 0, -1], [0, -1, 2, 1, 1], [1, 0, 1, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1, 1, 1], [1, 1, 1, -1, -1], [1, -1, 1, 1, -1]]).type(torch.float32) + loss = torch.nn.modules.loss.HingeEmbeddingLoss(reduction='mean', reduce=False, size_average=None, margin=0.5) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_KLDivLoss.py b/tests/test_nn_modules_loss_KLDivLoss.py new file mode 100644 index 000000000..83f19c7c7 --- /dev/null +++ b/tests/test_nn_modules_loss_KLDivLoss.py @@ -0,0 +1,245 @@ +# 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.nn.modules.loss.KLDivLoss") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355, 0.1112, -0.4061], + [0.9112, -1.7526, -0.4061, 0.5112, 0.1234], + [0.2837, 0.0297, 0.0355, 0.9112, 0.7526]]) + target = torch.tensor([[1.,2.,3.,4.,5.],[6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]]) + loss = torch.nn.modules.loss.KLDivLoss() + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355, 0.1112, -0.4061], + [0.9112, -1.7526, -0.4061, 0.5112, 0.1234], + [0.2837, 0.0297, 0.0355, 0.9112, 0.7526]]) + target = torch.tensor([[1.,2.,3.,4.,5.],[6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]]) + loss = torch.nn.modules.loss.KLDivLoss(log_target=False) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355, 0.1112, -0.4061], + [0.9112, -1.7526, -0.4061, 0.5112, 0.1234], + [0.2837, 0.0297, 0.0355, 0.9112, 0.7526]]) + target = torch.tensor([[1.,2.,3.,4.,5.],[6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]]) + loss = torch.nn.modules.loss.KLDivLoss(log_target=True) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355, 0.1112, -0.4061], + [0.9112, -1.7526, -0.4061, 0.5112, 0.1234], + [0.2837, 0.0297, 0.0355, 0.9112, 0.7526]]) + target = torch.tensor([[1.,2.,3.,4.,5.],[6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]]) + loss = torch.nn.modules.loss.KLDivLoss(reduction='none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355, 0.1112, -0.4061], + [0.9112, -1.7526, -0.4061, 0.5112, 0.1234], + [0.2837, 0.0297, 0.0355, 0.9112, 0.7526]]) + target = torch.tensor([[1.,2.,3.,4.,5.],[6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]]) + loss = torch.nn.modules.loss.KLDivLoss(reduction='mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355, 0.1112, -0.4061], + [0.9112, -1.7526, -0.4061, 0.5112, 0.1234], + [0.2837, 0.0297, 0.0355, 0.9112, 0.7526]]) + target = torch.tensor([[1.,2.,3.,4.,5.],[6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]]) + loss = torch.nn.modules.loss.KLDivLoss(reduction='sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355, 0.1112, -0.4061], + [0.9112, -1.7526, -0.4061, 0.5112, 0.1234], + [0.2837, 0.0297, 0.0355, 0.9112, 0.7526]]) + target = torch.tensor([[1.,2.,3.,4.,5.],[6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]]) + loss = torch.nn.modules.loss.KLDivLoss(reduction='batchmean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355, 0.1112, -0.4061], + [0.9112, -1.7526, -0.4061, 0.5112, 0.1234], + [0.2837, 0.0297, 0.0355, 0.9112, 0.7526]]) + target = torch.tensor([[1.,2.,3.,4.,5.],[6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]]) + loss = torch.nn.modules.loss.KLDivLoss(size_average=True) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355, 0.1112, -0.4061], + [0.9112, -1.7526, -0.4061, 0.5112, 0.1234], + [0.2837, 0.0297, 0.0355, 0.9112, 0.7526]]) + target = torch.tensor([[1.,2.,3.,4.,5.],[6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]]) + loss = torch.nn.modules.loss.KLDivLoss(size_average=False) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355, 0.1112, -0.4061], + [0.9112, -1.7526, -0.4061, 0.5112, 0.1234], + [0.2837, 0.0297, 0.0355, 0.9112, 0.7526]]) + target = torch.tensor([[1.,2.,3.,4.,5.],[6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]]) + loss = torch.nn.modules.loss.KLDivLoss(reduce=True) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_11(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355, 0.1112, -0.4061], + [0.9112, -1.7526, -0.4061, 0.5112, 0.1234], + [0.2837, 0.0297, 0.0355, 0.9112, 0.7526]]) + target = torch.tensor([[1.,2.,3.,4.,5.],[6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]]) + loss = torch.nn.modules.loss.KLDivLoss(reduce=False) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_12(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355, 0.1112, -0.4061], + [0.9112, -1.7526, -0.4061, 0.5112, 0.1234], + [0.2837, 0.0297, 0.0355, 0.9112, 0.7526]]) + target = torch.tensor([[1.,2.,3.,4.,5.],[6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]]) + loss = torch.nn.modules.loss.KLDivLoss(size_average=False, reduce=True, reduction='batchmean', log_target=False) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# generated by validate_unittest autofix, based on test_case_12 +def test_case_13(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355, 0.1112, -0.4061], + [0.9112, -1.7526, -0.4061, 0.5112, 0.1234], + [0.2837, 0.0297, 0.0355, 0.9112, 0.7526]]) + target = torch.tensor([[1.,2.,3.,4.,5.],[6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]]) + loss = torch.nn.modules.loss.KLDivLoss(False, True, 'batchmean', False) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +# generated by validate_unittest autofix, based on test_case_12 +def test_case_14(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355, 0.1112, -0.4061], + [0.9112, -1.7526, -0.4061, 0.5112, 0.1234], + [0.2837, 0.0297, 0.0355, 0.9112, 0.7526]]) + target = torch.tensor([[1.,2.,3.,4.,5.],[6.,7.,8.,9.,10.],[11.,12.,13.,14.,15.]]) + loss = torch.nn.modules.loss.KLDivLoss(reduction='batchmean', log_target=False, size_average=False, reduce=True) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_L1Loss.py b/tests/test_nn_modules_loss_L1Loss.py new file mode 100644 index 000000000..25900a921 --- /dev/null +++ b/tests/test_nn_modules_loss_L1Loss.py @@ -0,0 +1,147 @@ +# Copyright (c) 2025 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.nn.modules.loss.L1Loss") + + +def test_case_1(): + """default""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.L1Loss() + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + """reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.L1Loss(reduction='none') + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + """reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.L1Loss(reduction='mean') + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + """reduction='sum'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.L1Loss(reduction='sum') + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + """size_average=True""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.L1Loss(size_average=True) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """reduce=True""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.L1Loss(reduce=True) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """reduce=False""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.L1Loss(reduce=False) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """positional args""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.L1Loss(None, False, 'mean') + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_MSELoss.py b/tests/test_nn_modules_loss_MSELoss.py new file mode 100644 index 000000000..c26b3e85e --- /dev/null +++ b/tests/test_nn_modules_loss_MSELoss.py @@ -0,0 +1,163 @@ +# Copyright (c) 2025 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.nn.modules.loss.MSELoss") + + +def test_case_1(): + """default""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.MSELoss() + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + """reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.MSELoss(reduction='none') + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + """reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.MSELoss(reduction='mean') + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + """reduction='sum'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.MSELoss(reduction='sum') + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + """size_average=True""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.MSELoss(size_average=True) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """size_average=False""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.MSELoss(size_average=False) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """reduce=True""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.MSELoss(reduce=True) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """reduce=False""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.MSELoss(reduce=False) + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """positional args""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[-1.2837, -0.0297, 0.0355], + [ 0.9112, -1.7526, -0.4061]]) + target = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) + loss = torch.nn.modules.loss.MSELoss(None, False, 'mean') + result = loss(input,target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_MarginRankingLoss.py b/tests/test_nn_modules_loss_MarginRankingLoss.py new file mode 100644 index 000000000..1db63a190 --- /dev/null +++ b/tests/test_nn_modules_loss_MarginRankingLoss.py @@ -0,0 +1,131 @@ +# Copyright (c) 2025 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.nn.modules.loss.MarginRankingLoss") + + +def test_case_1(): + """default""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input1 = torch.tensor([1.0, 2.0, 3.0]) + input2 = torch.tensor([4.0, 5.0, 6.0]) + target = torch.tensor([1.0, -1.0, 1.0]) + loss = torch.nn.modules.loss.MarginRankingLoss() + result = loss(input1, input2, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + """margin=0.5""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input1 = torch.tensor([1.0, 2.0, 3.0]) + input2 = torch.tensor([4.0, 5.0, 6.0]) + target = torch.tensor([1.0, -1.0, 1.0]) + loss = torch.nn.modules.loss.MarginRankingLoss(margin=0.5) + result = loss(input1, input2, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + """margin=1.0""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input1 = torch.tensor([1.0, 2.0, 3.0]) + input2 = torch.tensor([4.0, 5.0, 6.0]) + target = torch.tensor([1.0, -1.0, 1.0]) + loss = torch.nn.modules.loss.MarginRankingLoss(margin=1.0) + result = loss(input1, input2, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + """reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input1 = torch.tensor([1.0, 2.0, 3.0]) + input2 = torch.tensor([4.0, 5.0, 6.0]) + target = torch.tensor([1.0, -1.0, 1.0]) + loss = torch.nn.modules.loss.MarginRankingLoss(reduction='none') + result = loss(input1, input2, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + """reduction='sum'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input1 = torch.tensor([1.0, 2.0, 3.0]) + input2 = torch.tensor([4.0, 5.0, 6.0]) + target = torch.tensor([1.0, -1.0, 1.0]) + loss = torch.nn.modules.loss.MarginRankingLoss(reduction='sum') + result = loss(input1, input2, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """margin=0.5, reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input1 = torch.tensor([1.0, 2.0, 3.0]) + input2 = torch.tensor([4.0, 5.0, 6.0]) + target = torch.tensor([1.0, -1.0, 1.0]) + loss = torch.nn.modules.loss.MarginRankingLoss(margin=0.5, reduction='mean') + result = loss(input1, input2, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """positional args""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input1 = torch.tensor([1.0, 2.0, 3.0]) + input2 = torch.tensor([4.0, 5.0, 6.0]) + target = torch.tensor([1.0, -1.0, 1.0]) + loss = torch.nn.modules.loss.MarginRankingLoss(0.5, 'mean') + result = loss(input1, input2, target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_MultiLabelMarginLoss.py b/tests/test_nn_modules_loss_MultiLabelMarginLoss.py new file mode 100644 index 000000000..1e26b5978 --- /dev/null +++ b/tests/test_nn_modules_loss_MultiLabelMarginLoss.py @@ -0,0 +1,104 @@ +# Copyright (c) 2025 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.nn.modules.loss.MultiLabelMarginLoss") + + +def test_case_1(): + """default""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4], + [0.5, 0.6, 0.7, 0.8]]) + target = torch.tensor([[0, 2, -1, -1], + [1, 3, -1, -1]], dtype=torch.long) + loss = torch.nn.modules.loss.MultiLabelMarginLoss() + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + """reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4], + [0.5, 0.6, 0.7, 0.8]]) + target = torch.tensor([[0, 2, -1, -1], + [1, 3, -1, -1]], dtype=torch.long) + loss = torch.nn.modules.loss.MultiLabelMarginLoss(reduction='none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + """reduction='sum'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4], + [0.5, 0.6, 0.7, 0.8]]) + target = torch.tensor([[0, 2, -1, -1], + [1, 3, -1, -1]], dtype=torch.long) + loss = torch.nn.modules.loss.MultiLabelMarginLoss(reduction='sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + """reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4], + [0.5, 0.6, 0.7, 0.8]]) + target = torch.tensor([[0, 2, -1, -1], + [1, 3, -1, -1]], dtype=torch.long) + loss = torch.nn.modules.loss.MultiLabelMarginLoss(reduction='mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + """positional args""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4], + [0.5, 0.6, 0.7, 0.8]]) + target = torch.tensor([[0, 2, -1, -1], + [1, 3, -1, -1]], dtype=torch.long) + loss = torch.nn.modules.loss.MultiLabelMarginLoss('mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_MultiLabelSoftMarginLoss.py b/tests/test_nn_modules_loss_MultiLabelSoftMarginLoss.py new file mode 100644 index 000000000..bedd3cf30 --- /dev/null +++ b/tests/test_nn_modules_loss_MultiLabelSoftMarginLoss.py @@ -0,0 +1,127 @@ +# Copyright (c) 2025 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 + +import pytest +from apibase import APIBase + +obj = APIBase("torch.nn.modules.loss.MultiLabelSoftMarginLoss") + + +def test_case_1(): + """default""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[1, 0, 1], + [0, 1, 0]], dtype=torch.float32) + loss = torch.nn.modules.loss.MultiLabelSoftMarginLoss() + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + """weight specified""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[1, 0, 1], + [0, 1, 0]], dtype=torch.float32) + weight = torch.tensor([0.5, 1.0, 1.5]) + loss = torch.nn.modules.loss.MultiLabelSoftMarginLoss(weight=weight) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + """reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[1, 0, 1], + [0, 1, 0]], dtype=torch.float32) + loss = torch.nn.modules.loss.MultiLabelSoftMarginLoss(reduction='none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + """reduction='sum'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[1, 0, 1], + [0, 1, 0]], dtype=torch.float32) + loss = torch.nn.modules.loss.MultiLabelSoftMarginLoss(reduction='sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + """weight and reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[1, 0, 1], + [0, 1, 0]], dtype=torch.float32) + weight = torch.tensor([0.5, 1.0, 1.5]) + loss = torch.nn.modules.loss.MultiLabelSoftMarginLoss(weight=weight, reduction='mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip( + reason="Positional args not aligning: Paddle lacks size_average/reduce params" +) +def test_case_6(): + """positional args""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[1, 0, 1], + [0, 1, 0]], dtype=torch.float32) + loss = torch.nn.modules.loss.MultiLabelSoftMarginLoss(None, 'sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_MultiMarginLoss.py b/tests/test_nn_modules_loss_MultiMarginLoss.py new file mode 100644 index 000000000..473a2d0ed --- /dev/null +++ b/tests/test_nn_modules_loss_MultiMarginLoss.py @@ -0,0 +1,161 @@ +# Copyright (c) 2025 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 + +import pytest +from apibase import APIBase + +obj = APIBase("torch.nn.modules.loss.MultiMarginLoss") + + +def test_case_1(): + """default""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4], + [0.5, 0.6, 0.7, 0.8], + [0.9, 1.0, 1.1, 1.2]]) + target = torch.tensor([0, 2, 3], dtype=torch.long) + loss = torch.nn.modules.loss.MultiMarginLoss() + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + """p=2""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4], + [0.5, 0.6, 0.7, 0.8], + [0.9, 1.0, 1.1, 1.2]]) + target = torch.tensor([0, 2, 3], dtype=torch.long) + loss = torch.nn.modules.loss.MultiMarginLoss(p=2) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + """margin=1.0""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4], + [0.5, 0.6, 0.7, 0.8], + [0.9, 1.0, 1.1, 1.2]]) + target = torch.tensor([0, 2, 3], dtype=torch.long) + loss = torch.nn.modules.loss.MultiMarginLoss(margin=1.0) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + """weight specified""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4], + [0.5, 0.6, 0.7, 0.8], + [0.9, 1.0, 1.1, 1.2]]) + target = torch.tensor([0, 2, 3], dtype=torch.long) + weight = torch.tensor([0.5, 1.0, 1.5, 2.0]) + loss = torch.nn.modules.loss.MultiMarginLoss(weight=weight) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + """reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4], + [0.5, 0.6, 0.7, 0.8], + [0.9, 1.0, 1.1, 1.2]]) + target = torch.tensor([0, 2, 3], dtype=torch.long) + loss = torch.nn.modules.loss.MultiMarginLoss(reduction='none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """reduction='sum'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4], + [0.5, 0.6, 0.7, 0.8], + [0.9, 1.0, 1.1, 1.2]]) + target = torch.tensor([0, 2, 3], dtype=torch.long) + loss = torch.nn.modules.loss.MultiMarginLoss(reduction='sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """p=1, margin=0.5, weight, reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4], + [0.5, 0.6, 0.7, 0.8], + [0.9, 1.0, 1.1, 1.2]]) + target = torch.tensor([0, 2, 3], dtype=torch.long) + weight = torch.tensor([0.5, 1.0, 1.5, 2.0]) + loss = torch.nn.modules.loss.MultiMarginLoss(p=1, margin=0.5, weight=weight, reduction='mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip( + reason="Positional args not aligning: Paddle lacks size_average/reduce params" +) +def test_case_8(): + """positional args""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4], + [0.5, 0.6, 0.7, 0.8], + [0.9, 1.0, 1.1, 1.2]]) + target = torch.tensor([0, 2, 3], dtype=torch.long) + loss = torch.nn.modules.loss.MultiMarginLoss(1, 0.5, None, 'none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_NLLLoss.py b/tests/test_nn_modules_loss_NLLLoss.py new file mode 100644 index 000000000..ecb4d2145 --- /dev/null +++ b/tests/test_nn_modules_loss_NLLLoss.py @@ -0,0 +1,161 @@ +# Copyright (c) 2025 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 + +import pytest +from apibase import APIBase + +obj = APIBase("torch.nn.modules.loss.NLLLoss") + + +def test_case_1(): + """default""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4, 0.5], + [0.5, 0.4, 0.3, 0.2, 0.1], + [0.2, 0.3, 0.1, 0.5, 0.4]]) + target = torch.tensor([1, 0, 3], dtype=torch.long) + loss = torch.nn.modules.loss.NLLLoss() + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + """weight specified""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4, 0.5], + [0.5, 0.4, 0.3, 0.2, 0.1], + [0.2, 0.3, 0.1, 0.5, 0.4]]) + target = torch.tensor([1, 0, 3], dtype=torch.long) + weight = torch.tensor([0.5, 1.0, 1.5, 2.0, 2.5]) + loss = torch.nn.modules.loss.NLLLoss(weight=weight) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + """ignore_index=0""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4, 0.5], + [0.5, 0.4, 0.3, 0.2, 0.1], + [0.2, 0.3, 0.1, 0.5, 0.4]]) + target = torch.tensor([1, 0, 3], dtype=torch.long) + loss = torch.nn.modules.loss.NLLLoss(ignore_index=0) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + """ignore_index=1""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4, 0.5], + [0.5, 0.4, 0.3, 0.2, 0.1], + [0.2, 0.3, 0.1, 0.5, 0.4]]) + target = torch.tensor([1, 0, 3], dtype=torch.long) + loss = torch.nn.modules.loss.NLLLoss(ignore_index=1) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + """reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4, 0.5], + [0.5, 0.4, 0.3, 0.2, 0.1], + [0.2, 0.3, 0.1, 0.5, 0.4]]) + target = torch.tensor([1, 0, 3], dtype=torch.long) + loss = torch.nn.modules.loss.NLLLoss(reduction='none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """reduction='sum'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4, 0.5], + [0.5, 0.4, 0.3, 0.2, 0.1], + [0.2, 0.3, 0.1, 0.5, 0.4]]) + target = torch.tensor([1, 0, 3], dtype=torch.long) + loss = torch.nn.modules.loss.NLLLoss(reduction='sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """weight and ignore_index and reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4, 0.5], + [0.5, 0.4, 0.3, 0.2, 0.1], + [0.2, 0.3, 0.1, 0.5, 0.4]]) + target = torch.tensor([1, 0, 3], dtype=torch.long) + weight = torch.tensor([0.5, 1.0, 1.5, 2.0, 2.5]) + loss = torch.nn.modules.loss.NLLLoss(weight=weight, ignore_index=0, reduction='mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip( + reason="Positional args not aligning: Paddle lacks size_average/reduce params" +) +def test_case_8(): + """positional args""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3, 0.4, 0.5], + [0.5, 0.4, 0.3, 0.2, 0.1], + [0.2, 0.3, 0.1, 0.5, 0.4]]) + target = torch.tensor([1, 0, 3], dtype=torch.long) + loss = torch.nn.modules.loss.NLLLoss(None, 0, 'sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_PoissonNLLLoss.py b/tests/test_nn_modules_loss_PoissonNLLLoss.py new file mode 100644 index 000000000..239e40f99 --- /dev/null +++ b/tests/test_nn_modules_loss_PoissonNLLLoss.py @@ -0,0 +1,176 @@ +# Copyright (c) 2025 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 + +import pytest +from apibase import APIBase + +obj = APIBase("torch.nn.modules.loss.PoissonNLLLoss") + + +def test_case_1(): + """default""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[0.5, 1.0, 1.5], + [2.0, 2.5, 3.0]]) + loss = torch.nn.modules.loss.PoissonNLLLoss() + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + """log_input=False""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[0.5, 1.0, 1.5], + [2.0, 2.5, 3.0]]) + loss = torch.nn.modules.loss.PoissonNLLLoss(log_input=False) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + """full=True""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[0.5, 1.0, 1.5], + [2.0, 2.5, 3.0]]) + loss = torch.nn.modules.loss.PoissonNLLLoss(full=True) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + """eps=1e-08""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[0.5, 1.0, 1.5], + [2.0, 2.5, 3.0]]) + loss = torch.nn.modules.loss.PoissonNLLLoss(eps=1e-08) + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + """reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[0.5, 1.0, 1.5], + [2.0, 2.5, 3.0]]) + loss = torch.nn.modules.loss.PoissonNLLLoss(reduction='none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """reduction='sum'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[0.5, 1.0, 1.5], + [2.0, 2.5, 3.0]]) + loss = torch.nn.modules.loss.PoissonNLLLoss(reduction='sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """log_input=True, full=True, eps=1e-08, reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[0.5, 1.0, 1.5], + [2.0, 2.5, 3.0]]) + loss = torch.nn.modules.loss.PoissonNLLLoss(log_input=True, full=True, eps=1e-08, reduction='mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """log_input=False, reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[0.5, 1.0, 1.5], + [2.0, 2.5, 3.0]]) + loss = torch.nn.modules.loss.PoissonNLLLoss(log_input=False, reduction='mean') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +@pytest.mark.skip( + reason="Positional args not aligning: Paddle uses epsilon not eps, lacks size_average/reduce" +) +def test_case_9(): + """positional args""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[0.1, 0.2, 0.3], + [0.4, 0.5, 0.6]]) + target = torch.tensor([[0.5, 1.0, 1.5], + [2.0, 2.5, 3.0]]) + loss = torch.nn.modules.loss.PoissonNLLLoss(True, False, 1e-08, 'sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_SmoothL1Loss.py b/tests/test_nn_modules_loss_SmoothL1Loss.py new file mode 100644 index 000000000..647f8f51f --- /dev/null +++ b/tests/test_nn_modules_loss_SmoothL1Loss.py @@ -0,0 +1,148 @@ +# Copyright (c) 2025 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.nn.modules.loss.SmoothL1Loss") + + +def test_case_1(): + """default""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.SmoothL1Loss() + input = torch.ones([2, 3]).to(dtype=torch.float32) + label = torch.full([2, 3], 2).to(dtype=torch.float32) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + """reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.SmoothL1Loss(reduction='none') + input = torch.ones([2, 3]).to(dtype=torch.float32) + label = torch.full([2, 3], 2).to(dtype=torch.float32) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + """reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.SmoothL1Loss(reduction='mean') + input = torch.ones([2, 3]).to(dtype=torch.float32) + label = torch.full([2, 3], 2).to(dtype=torch.float32) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + """reduction='sum'""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.SmoothL1Loss(reduction='sum') + input = torch.ones([2, 3]).to(dtype=torch.float32) + label = torch.full([2, 3], 2).to(dtype=torch.float32) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + """beta=1.0, reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.SmoothL1Loss(beta=1.0, reduction='none') + input = torch.ones([2, 3]).to(dtype=torch.float32) + label = torch.full([2, 3], 2).to(dtype=torch.float32) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """size_average=None, reduce=None, reduction='mean', beta=1.0""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.SmoothL1Loss(size_average=None, + reduce=None, reduction='mean', beta=1.0) + input = torch.ones([2, 3]).to(dtype=torch.float32) + label = torch.full([2, 3], 2).to(dtype=torch.float32) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """positional args""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.SmoothL1Loss(None, + None, 'mean', 1.0) + input = torch.ones([2, 3]).to(dtype=torch.float32) + label = torch.full([2, 3], 2).to(dtype=torch.float32) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """beta=1.5, reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.modules.loss.SmoothL1Loss(beta=1.5, reduction='none') + input = torch.ones([2, 3]).to(dtype=torch.float32) + label = torch.full([2, 3], 2).to(dtype=torch.float32) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """beta as variable, reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + beta = 1.5 + loss = torch.nn.modules.loss.SmoothL1Loss(beta=beta, reduction='none') + input = torch.ones([2, 3]).to(dtype=torch.float32) + label = torch.full([2, 3], 2).to(dtype=torch.float32) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_SoftMarginLoss.py b/tests/test_nn_modules_loss_SoftMarginLoss.py new file mode 100644 index 000000000..574abd401 --- /dev/null +++ b/tests/test_nn_modules_loss_SoftMarginLoss.py @@ -0,0 +1,145 @@ +# Copyright (c) 2025 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.nn.modules.loss.SoftMarginLoss") + + +def test_case_1(): + """default""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3], [0, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1], [1, 1, -1]]).type(torch.float32) + cri = torch.nn.modules.loss.SoftMarginLoss() + result = cri(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + """reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3], [0, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1], [1, 1, -1]]).type(torch.float32) + cri = torch.nn.modules.loss.SoftMarginLoss(reduction='none') + result = cri(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + """reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3], [0, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1], [1, 1, -1]]).type(torch.float32) + cri = torch.nn.modules.loss.SoftMarginLoss(reduction='mean') + result = cri(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + """reduction='sum'""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3], [0, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1], [1, 1, -1]]).type(torch.float32) + cri = torch.nn.modules.loss.SoftMarginLoss(reduction='sum') + result = cri(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + """size_average=None, reduce=None, reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3], [0, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1], [1, 1, -1]]).type(torch.float32) + cri = torch.nn.modules.loss.SoftMarginLoss(size_average=None, reduce=None, reduction='mean') + result = cri(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """positional args: size_average, reduce, reduction""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3], [0, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1], [1, 1, -1]]).type(torch.float32) + cri = torch.nn.modules.loss.SoftMarginLoss(None, None, 'mean') + result = cri(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """size_average=True, reduce=False, reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3], [0, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1], [1, 1, -1]]).type(torch.float32) + cri = torch.nn.modules.loss.SoftMarginLoss(size_average=True, reduce=False, reduction='mean') + result = cri(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """reduce=False, size_average=False, reduction='sum'""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3], [0, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1], [1, 1, -1]]).type(torch.float32) + cri = torch.nn.modules.loss.SoftMarginLoss(reduce=False, size_average=False, reduction='sum') + result = cri(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """all keyword args: size_average=True, reduce=True, reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, -2, 3], [0, -1, 2]]).type(torch.float32) + label = torch.Tensor([[-1, 1, -1], [1, 1, -1]]).type(torch.float32) + cri = torch.nn.modules.loss.SoftMarginLoss(size_average=True, reduce=True, reduction='mean') + result = cri(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss_TripletMarginLoss.py b/tests/test_nn_modules_loss_TripletMarginLoss.py new file mode 100644 index 000000000..e663258af --- /dev/null +++ b/tests/test_nn_modules_loss_TripletMarginLoss.py @@ -0,0 +1,169 @@ +# Copyright (c) 2025 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.nn.modules.loss.TripletMarginLoss") + + +def test_case_1(): + """default""" + pytorch_code = textwrap.dedent( + """ + import torch + anchor = torch.Tensor([[1, 5, 3, 0], [0, 3, 2, 1]]).type(torch.float32) + positive = torch.Tensor([[5, 1, 2, 0], [3, 2, 1, 0]]).type(torch.float32) + negative = torch.Tensor([[2, 1, -3, 0], [1, 1, -1, 0]]).type(torch.float32) + cri = torch.nn.modules.loss.TripletMarginLoss() + result = cri(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + """margin=1.3, reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + anchor = torch.Tensor([[1, 5, 3, 0], [0, 3, 2, 1]]).type(torch.float32) + positive = torch.Tensor([[5, 1, 2, 0], [3, 2, 1, 0]]).type(torch.float32) + negative = torch.Tensor([[2, 1, -3, 0], [1, 1, -1, 0]]).type(torch.float32) + cri = torch.nn.modules.loss.TripletMarginLoss(margin=1.3, reduction='none') + result = cri(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + """p=2, reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + anchor = torch.Tensor([[1, 5, 3, 0], [0, 3, 2, 1]]).type(torch.float32) + positive = torch.Tensor([[5, 1, 2, 0], [3, 2, 1, 0]]).type(torch.float32) + negative = torch.Tensor([[2, 1, -3, 0], [1, 1, -1, 0]]).type(torch.float32) + cri = torch.nn.modules.loss.TripletMarginLoss(p=2, reduction='mean') + result = cri(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + """eps=1e-4, reduction='sum'""" + pytorch_code = textwrap.dedent( + """ + import torch + anchor = torch.Tensor([[1, 5, 3, 0], [0, 3, 2, 1]]).type(torch.float32) + positive = torch.Tensor([[5, 1, 2, 0], [3, 2, 1, 0]]).type(torch.float32) + negative = torch.Tensor([[2, 1, -3, 0], [1, 1, -1, 0]]).type(torch.float32) + cri = torch.nn.modules.loss.TripletMarginLoss(eps=1e-4, reduction='sum') + result = cri(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + """swap=True, reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + anchor = torch.Tensor([[1, 5, 3, 0], [0, 3, 2, 1]]).type(torch.float32) + positive = torch.Tensor([[5, 1, 2, 0], [3, 2, 1, 0]]).type(torch.float32) + negative = torch.Tensor([[2, 1, -3, 0], [1, 1, -1, 0]]).type(torch.float32) + cri = torch.nn.modules.loss.TripletMarginLoss(swap=True, reduction='none') + result = cri(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """all keyword args""" + pytorch_code = textwrap.dedent( + """ + import torch + anchor = torch.Tensor([[1, 5, 3, 0], [0, 3, 2, 1]]).type(torch.float32) + positive = torch.Tensor([[5, 1, 2, 0], [3, 2, 1, 0]]).type(torch.float32) + negative = torch.Tensor([[2, 1, -3, 0], [1, 1, -1, 0]]).type(torch.float32) + cri = torch.nn.modules.loss.TripletMarginLoss(margin=1.3, p=3.2, eps=1e-5, swap=False, size_average=True, reduce=False, reduction='mean') + result = cri(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"], rtol=1.0e-5, atol=1.0e-8) + + +def test_case_7(): + """positional args""" + pytorch_code = textwrap.dedent( + """ + import torch + anchor = torch.Tensor([[1, 5, 3, 0], [0, 3, 2, 1]]).type(torch.float32) + positive = torch.Tensor([[5, 1, 2, 0], [3, 2, 1, 0]]).type(torch.float32) + negative = torch.Tensor([[2, 1, -3, 0], [1, 1, -1, 0]]).type(torch.float32) + cri = torch.nn.modules.loss.TripletMarginLoss(1.3, 3.2, 1e-5, False, True, False, reduction='mean') + result = cri(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"], rtol=1.0e-5, atol=1.0e-8) + + +def test_case_8(): + """all positional args""" + pytorch_code = textwrap.dedent( + """ + import torch + anchor = torch.Tensor([[1, 5, 3, 0], [0, 3, 2, 1]]).type(torch.float32) + positive = torch.Tensor([[5, 1, 2, 0], [3, 2, 1, 0]]).type(torch.float32) + negative = torch.Tensor([[2, 1, -3, 0], [1, 1, -1, 0]]).type(torch.float32) + cri = torch.nn.modules.loss.TripletMarginLoss(1.3, 3.2, 1e-5, True, True, False, 'none') + result = cri(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """reduction='sum', swap=True, size_average=False, reduce=False""" + pytorch_code = textwrap.dedent( + """ + import torch + anchor = torch.Tensor([[1, 5, 3, 0], [0, 3, 2, 1]]).type(torch.float32) + positive = torch.Tensor([[5, 1, 2, 0], [3, 2, 1, 0]]).type(torch.float32) + negative = torch.Tensor([[2, 1, -3, 0], [1, 1, -1, 0]]).type(torch.float32) + cri = torch.nn.modules.loss.TripletMarginLoss(1.3, 3.2, 1e-5, True, False, True, reduction='sum') + result = cri(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """rearranged keyword args""" + pytorch_code = textwrap.dedent( + """ + import torch + anchor = torch.Tensor([[1, 5, 3, 0], [0, 3, 2, 1]]).type(torch.float32) + positive = torch.Tensor([[5, 1, 2, 0], [3, 2, 1, 0]]).type(torch.float32) + negative = torch.Tensor([[2, 1, -3, 0], [1, 1, -1, 0]]).type(torch.float32) + cri = torch.nn.modules.loss.TripletMarginLoss(reduction='sum', reduce=False, size_average=False, swap=False, eps=1e-5, p=3.2, margin=1.3) + result = cri(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"], rtol=1.0e-5, atol=1.0e-8) diff --git a/tests/test_nn_modules_loss_TripletMarginWithDistanceLoss.py b/tests/test_nn_modules_loss_TripletMarginWithDistanceLoss.py new file mode 100644 index 000000000..fe09c43df --- /dev/null +++ b/tests/test_nn_modules_loss_TripletMarginWithDistanceLoss.py @@ -0,0 +1,165 @@ +# Copyright (c) 2025 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.nn.modules.loss.TripletMarginWithDistanceLoss") + + +def test_case_1(): + """default""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + anchor = torch.tensor([[1., 5, 3, 0], [0, 3, 2, 1]]) + positive = torch.tensor([[5., 1, 2, 0], [3, 2, 1, 0]]) + negative = torch.tensor([[2., 1, -3, 0], [1, 1, -1, 0]]) + model = nn.TripletMarginWithDistanceLoss() + result = model(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + """margin=2""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + anchor = torch.tensor([[1., 5, 3, 0], [0, 3, 2, 1]]) + positive = torch.tensor([[5., 1, 2, 0], [3, 2, 1, 0]]) + negative = torch.tensor([[2., 1, -3, 0], [1, 1, -1, 0]]) + model = nn.TripletMarginWithDistanceLoss(margin=2) + result = model(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + """margin=2, swap=True""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + anchor = torch.tensor([[1., 5, 3, 0], [0, 3, 2, 1]]) + positive = torch.tensor([[5., 1, 2, 0], [3, 2, 1, 0]]) + negative = torch.tensor([[2., 1, -3, 0], [1, 1, -1, 0]]) + model = nn.TripletMarginWithDistanceLoss(margin=2, swap=True) + result = model(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + """margin=2, reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + anchor = torch.tensor([[1., 5, 3, 0], [0, 3, 2, 1]]) + positive = torch.tensor([[5., 1, 2, 0], [3, 2, 1, 0]]) + negative = torch.tensor([[2., 1, -3, 0], [1, 1, -1, 0]]) + model = nn.TripletMarginWithDistanceLoss(margin=2, reduction='mean') + result = model(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + """margin=2, reduction='sum'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + anchor = torch.tensor([[1., 5, 3, 0], [0, 3, 2, 1]]) + positive = torch.tensor([[5., 1, 2, 0], [3, 2, 1, 0]]) + negative = torch.tensor([[2., 1, -3, 0], [1, 1, -1, 0]]) + model = nn.TripletMarginWithDistanceLoss(margin=2, reduction='sum') + result = model(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """margin=2, reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + anchor = torch.tensor([[1., 5, 3, 0], [0, 3, 2, 1]]) + positive = torch.tensor([[5., 1, 2, 0], [3, 2, 1, 0]]) + negative = torch.tensor([[2., 1, -3, 0], [1, 1, -1, 0]]) + model = nn.TripletMarginWithDistanceLoss(margin=2, reduction='none') + result = model(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """distance_function=nn.PairwiseDistance, margin=2, reduction='sum', swap=False""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + anchor = torch.tensor([[1., 5, 3, 0], [0, 3, 2, 1]]) + positive = torch.tensor([[5., 1, 2, 0], [3, 2, 1, 0]]) + negative = torch.tensor([[2., 1, -3, 0], [1, 1, -1, 0]]) + distance_function = nn.PairwiseDistance() + model = nn.TripletMarginWithDistanceLoss(distance_function=distance_function, margin=2, reduction='sum', swap=False) + result = model(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """distance_function=nn.PairwiseDistance, margin=2, reduction='mean'""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + anchor = torch.tensor([[1., 5, 3, 0], [0, 3, 2, 1]]) + positive = torch.tensor([[5., 1, 2, 0], [3, 2, 1, 0]]) + negative = torch.tensor([[2., 1, -3, 0], [1, 1, -1, 0]]) + distance_function = nn.PairwiseDistance() + model = nn.TripletMarginWithDistanceLoss(distance_function=distance_function, margin=2, reduction='mean') + result = model(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """all keyword args including distance_function=None""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + anchor = torch.tensor([[1., 5, 3, 0], [0, 3, 2, 1]]) + positive = torch.tensor([[5., 1, 2, 0], [3, 2, 1, 0]]) + negative = torch.tensor([[2., 1, -3, 0], [1, 1, -1, 0]]) + model = nn.TripletMarginWithDistanceLoss(distance_function=None, margin=2, reduction='sum', swap=False) + result = model(anchor, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_modules_loss__Loss.py b/tests/test_nn_modules_loss__Loss.py index e6a8b56a2..40fc21ff7 100644 --- a/tests/test_nn_modules_loss__Loss.py +++ b/tests/test_nn_modules_loss__Loss.py @@ -16,10 +16,32 @@ from apibase import APIBase -obj = APIBase("torch.nn.modules.loss._Loss") - -def _test_case_1(): +class LossAPIBase(APIBase): + def compare( + self, + name, + pytorch_result, + paddle_result, + check_value=True, + check_shape=True, + check_dtype=True, + check_stop_gradient=True, + rtol=1.0e-6, + atol=0.0, + ): + """Compare string reduction values from _Loss objects""" + assert ( + pytorch_result == paddle_result + ), "API ({}): paddle result {} != pytorch result {}".format( + name, paddle_result, pytorch_result + ) + + +obj = LossAPIBase("torch.nn.modules.loss._Loss") + + +def test_case_1(): """Default reduction='mean'""" pytorch_code = textwrap.dedent( """ @@ -28,14 +50,10 @@ def _test_case_1(): result = loss.reduction """ ) - obj.run( - pytorch_code, - unsupport=True, - reason="paddle.nn.modules.loss._Loss is not implemented in Paddle", - ) + obj.run(pytorch_code, ["result"]) -def _test_case_2(): +def test_case_2(): """reduction='none'""" pytorch_code = textwrap.dedent( """ @@ -44,14 +62,10 @@ def _test_case_2(): result = loss.reduction """ ) - obj.run( - pytorch_code, - unsupport=True, - reason="paddle.nn.modules.loss._Loss is not implemented in Paddle", - ) + obj.run(pytorch_code, ["result"]) -def _test_case_3(): +def test_case_3(): """reduction='sum'""" pytorch_code = textwrap.dedent( """ @@ -60,15 +74,11 @@ def _test_case_3(): result = loss.reduction """ ) - obj.run( - pytorch_code, - unsupport=True, - reason="paddle.nn.modules.loss._Loss is not implemented in Paddle", - ) + obj.run(pytorch_code, ["result"]) -def _test_case_4(): - """reduction='mean'""" +def test_case_4(): + """reduction='mean' keyword""" pytorch_code = textwrap.dedent( """ import torch @@ -76,14 +86,10 @@ def _test_case_4(): result = loss.reduction """ ) - obj.run( - pytorch_code, - unsupport=True, - reason="paddle.nn.modules.loss._Loss is not implemented in Paddle", - ) + obj.run(pytorch_code, ["result"]) -def _test_case_5(): +def test_case_5(): """size_average=True (deprecated)""" pytorch_code = textwrap.dedent( """ @@ -92,14 +98,10 @@ def _test_case_5(): result = loss.reduction """ ) - obj.run( - pytorch_code, - unsupport=True, - reason="paddle.nn.modules.loss._Loss is not implemented in Paddle", - ) + obj.run(pytorch_code, ["result"]) -def _test_case_6(): +def test_case_6(): """size_average=False (deprecated)""" pytorch_code = textwrap.dedent( """ @@ -108,14 +110,10 @@ def _test_case_6(): result = loss.reduction """ ) - obj.run( - pytorch_code, - unsupport=True, - reason="paddle.nn.modules.loss._Loss is not implemented in Paddle", - ) + obj.run(pytorch_code, ["result"]) -def _test_case_7(): +def test_case_7(): """reduce=False (deprecated)""" pytorch_code = textwrap.dedent( """ @@ -124,14 +122,10 @@ def _test_case_7(): result = loss.reduction """ ) - obj.run( - pytorch_code, - unsupport=True, - reason="paddle.nn.modules.loss._Loss is not implemented in Paddle", - ) + obj.run(pytorch_code, ["result"]) -def _test_case_8(): +def test_case_8(): """reduce=True, size_average=False (deprecated)""" pytorch_code = textwrap.dedent( """ @@ -140,14 +134,10 @@ def _test_case_8(): result = loss.reduction """ ) - obj.run( - pytorch_code, - unsupport=True, - reason="paddle.nn.modules.loss._Loss is not implemented in Paddle", - ) + obj.run(pytorch_code, ["result"]) -def _test_case_9(): +def test_case_9(): """Keyword arguments out of order""" pytorch_code = textwrap.dedent( """ @@ -156,14 +146,10 @@ def _test_case_9(): result = loss.reduction """ ) - obj.run( - pytorch_code, - unsupport=True, - reason="paddle.nn.modules.loss._Loss is not implemented in Paddle", - ) + obj.run(pytorch_code, ["result"]) -def _test_case_10(): +def test_case_10(): """All None deprecated args""" pytorch_code = textwrap.dedent( """ @@ -172,53 +158,29 @@ def _test_case_10(): result = loss.reduction """ ) - obj.run( - pytorch_code, - unsupport=True, - reason="paddle.nn.modules.loss._Loss is not implemented in Paddle", - ) + obj.run(pytorch_code, ["result"]) -def _test_case_11(): +def test_case_11(): + """Expression reduction argument""" pytorch_code = textwrap.dedent( """ import torch - loss = torch.nn.modules.loss._Loss(reduction='mean') + loss = torch.nn.modules.loss._Loss(reduction='sum' if 1 == 1 else 'mean') result = loss.reduction """ ) - obj.run( - pytorch_code, - unsupport=True, - reason="paddle.nn.modules.loss._Loss is not implemented in Paddle", - ) + obj.run(pytorch_code, ["result"]) -def _test_case_12(): +def test_case_12(): + """Variable reduction argument""" pytorch_code = textwrap.dedent( """ import torch - loss = torch.nn.modules.loss._Loss(reduction='sum') - result = loss.reduction - """ - ) - obj.run( - pytorch_code, - unsupport=True, - reason="paddle.nn.modules.loss._Loss is not implemented in Paddle", - ) - - -def _test_case_13(): - pytorch_code = textwrap.dedent( - """ - import torch - loss = torch.nn.modules.loss._Loss() + red = 'none' + loss = torch.nn.modules.loss._Loss(reduction=red) result = loss.reduction """ ) - obj.run( - pytorch_code, - unsupport=True, - reason="paddle.nn.modules.loss._Loss is not implemented in Paddle", - ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_set_default_device.py b/tests/test_set_default_device.py index 1612bf803..3c5e1c518 100644 --- a/tests/test_set_default_device.py +++ b/tests/test_set_default_device.py @@ -16,9 +16,30 @@ import paddle import pytest -from test_device import DeviceAPIBase +from apibase import APIBase -obj = DeviceAPIBase("torch.set_default_device") + +class SetDefaultDeviceAPIBase(APIBase): + def compare( + self, + name, + pytorch_result, + paddle_result, + check_value=True, + check_shape=True, + check_dtype=True, + check_stop_gradient=True, + rtol=1.0e-6, + atol=0.0, + ): + pytorch_result = str(pytorch_result) + paddle_result = str(paddle_result) + if "cpu:" in pytorch_result: + pytorch_result = "cpu" + assert pytorch_result == paddle_result + + +obj = SetDefaultDeviceAPIBase("torch.set_default_device") @pytest.mark.skipif( @@ -31,6 +52,7 @@ def test_case_1(): import torch torch.set_default_device('cuda:1') result = torch.get_default_device() + torch.set_default_device(None) """ ) obj.run(pytorch_code, ["result"]) @@ -44,8 +66,9 @@ def test_case_2(): pytorch_code = textwrap.dedent( """ import torch - torch.set_default_device("cuda") + torch.set_default_device("cuda:0") result = torch.get_default_device() + torch.set_default_device(None) """ ) obj.run(pytorch_code, ["result"]) @@ -65,6 +88,7 @@ def test_case_3(): obj.run(pytorch_code, ["result"]) +@pytest.mark.skip(reason="paddle does not support 'cpu:1' format") def test_case_4(): pytorch_code = textwrap.dedent( """ @@ -89,6 +113,7 @@ def test_case_5(): import torch torch.set_default_device(device=torch.device("cuda")) result = torch.get_default_device() + torch.set_default_device(None) """ ) obj.run(pytorch_code, ["result"]) @@ -104,6 +129,7 @@ def test_case_6(): import torch torch.set_default_device(device=torch.device("cuda:1")) result = torch.get_default_device() + torch.set_default_device(None) """ ) obj.run(pytorch_code, ["result"]) @@ -117,9 +143,10 @@ def test_case_7(): pytorch_code = textwrap.dedent( """ import torch - device = torch.device("cuda") + device = torch.device("cuda:0") torch.set_default_device(device) result = torch.get_default_device() + torch.set_default_device(None) """ ) obj.run(pytorch_code, ["result"]) diff --git a/tests/test_vstack.py b/tests/test_vstack.py index 98685764e..f07065fbe 100644 --- a/tests/test_vstack.py +++ b/tests/test_vstack.py @@ -109,6 +109,48 @@ def test_case_7(): obj.run(pytorch_code, ["out"]) +def test_case_8(): + """Variable tensor tuple test""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1, 2, 3]) + b = torch.tensor([4, 5, 6]) + tensors = (a, b) + result = torch.vstack(tensors) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """Gradient computation test""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1., 2., 3.], requires_grad=True) + b = torch.tensor([4., 5., 6.]) + result = torch.vstack((a, b)) + result.sum().backward() + a_grad = a.grad + """ + ) + obj.run(pytorch_code, ["result", "a_grad"], check_stop_gradient=False) + + +def test_case_10(): + """Expression argument test""" + pytorch_code = textwrap.dedent( + """ + import torch + a = torch.tensor([1, 2, 3]) + b = torch.tensor([4, 5, 6]) + result = torch.vstack((a, b) if True else (b, a)) + """ + ) + obj.run(pytorch_code, ["result"]) + + # dtype mismatch, torch dtype is float32, paddle dtype is int64 def _test_case_8(): pytorch_code = textwrap.dedent(