diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index f940997b9..67ec299b2 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -6842,27 +6842,10 @@ "paddle_api": "paddle.compat.nn.AvgPool3d" }, "torch.nn.BCELoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.BCELoss", - "min_input_args": 0, - "args_list": [ - "weight", - "size_average", - "reduce", - "reduction" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.nn.BCEWithLogitsLoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.BCEWithLogitsLoss", - "min_input_args": 0, - "args_list": [ - "weight", - "size_average", - "reduce", - "reduction", - "pos_weight" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.nn.BatchNorm1d": { "Matcher": "ReverseMomentumMatcher", @@ -7075,31 +7058,13 @@ "min_input_args": 3 }, "torch.nn.CosineEmbeddingLoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.CosineEmbeddingLoss", - "min_input_args": 0, - "args_list": [ - "margin", - "size_average", - "reduce", - "reduction" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.nn.CosineSimilarity": { "Matcher": "ChangePrefixMatcher" }, "torch.nn.CrossEntropyLoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.CrossEntropyLoss", - "args_list": [ - "weight", - "size_average", - "ignore_index", - "reduce", - "reduction", - "label_smoothing" - ], - "min_input_args": 0 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.DataParallel": { "Matcher": "GenericMatcher", @@ -7270,18 +7235,7 @@ "min_input_args": 2 }, "torch.nn.GaussianNLLLoss": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.nn.GaussianNLLLoss", - "min_input_args": 0, - "args_list": [ - "*", - "full", - "eps", - "reduction" - ], - "kwargs_change": { - "eps": "epsilon" - } + "Matcher": "ChangePrefixMatcher" }, "torch.nn.GroupNorm": { "Matcher": "ChangePrefixMatcher" @@ -7320,15 +7274,7 @@ } }, "torch.nn.HingeEmbeddingLoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.HingeEmbeddingLoss", - "min_input_args": 0, - "args_list": [ - "margin", - "size_average", - "reduce", - "reduction" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.nn.HuberLoss": { "Matcher": "ChangePrefixMatcher" @@ -7409,25 +7355,10 @@ } }, "torch.nn.KLDivLoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.KLDivLoss", - "args_list": [ - "size_average", - "reduce", - "reduction", - "log_target" - ], - "min_input_args": 0 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.L1Loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.L1Loss", - "args_list": [ - "size_average", - "reduce", - "reduction" - ], - "min_input_args": 0 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.LPPool1d": { "Matcher": "ChangePrefixMatcher" @@ -7511,25 +7442,10 @@ } }, "torch.nn.MSELoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.MSELoss", - "min_input_args": 0, - "args_list": [ - "size_average", - "reduce", - "reduction" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.nn.MarginRankingLoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.MarginRankingLoss", - "args_list": [ - "margin", - "size_average", - "reduce", - "reduction" - ], - "min_input_args": 0 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.MaxPool1d": { "Matcher": "ChangePrefixMatcher" @@ -7719,54 +7635,20 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.MultiLabelMarginLoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.MultiLabelMarginLoss", - "min_input_args": 0, - "args_list": [ - "size_average", - "reduce", - "reduction" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.nn.MultiLabelSoftMarginLoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.MultiLabelSoftMarginLoss", - "min_input_args": 0, - "args_list": [ - "weight", - "size_average", - "reduce", - "reduction" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.nn.MultiMarginLoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.MultiMarginLoss", - "min_input_args": 0, - "args_list": [ - "p", - "margin", - "weight", - "size_average", - "reduce", - "reduction" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.nn.MultiheadAttention": { "Matcher": "ChangeAPIMatcher", "paddle_api": "paddle.compat.nn.MultiheadAttention" }, "torch.nn.NLLLoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.NLLLoss", - "min_input_args": 0, - "args_list": [ - "weight", - "size_average", - "ignore_index", - "reduce", - "reduction" - ] + "Matcher": "ChangePrefixMatcher" }, "torch.nn.PReLU": { "Matcher": "GenericMatcher", @@ -7810,20 +7692,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.PoissonNLLLoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.PoissonNLLLoss", - "min_input_args": 0, - "args_list": [ - "log_input", - "full", - "size_average", - "eps", - "reduce", - "reduction" - ], - "kwargs_change": { - "eps": "epsilon" - } + "Matcher": "ChangePrefixMatcher" }, "torch.nn.RNN": { "Matcher": "RNNMatcher", @@ -7954,31 +7823,10 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.SmoothL1Loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.SmoothL1Loss", - "min_input_args": 0, - "args_list": [ - "size_average", - "reduce", - "reduction", - "beta" - ], - "kwargs_change": { - "beta": "delta" - }, - "paddle_default_kwargs": { - "is_huber": "False" - } + "Matcher": "ChangePrefixMatcher" }, "torch.nn.SoftMarginLoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.SoftMarginLoss", - "args_list": [ - "size_average", - "reduce", - "reduction" - ], - "min_input_args": 0 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.Softmax": { "Matcher": "ChangeAPIMatcher", @@ -8187,21 +8035,7 @@ "min_input_args": 2 }, "torch.nn.TripletMarginLoss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.TripletMarginLoss", - "args_list": [ - "margin", - "p", - "eps", - "swap", - "size_average", - "reduce", - "reduction" - ], - "kwargs_change": { - "eps": "epsilon" - }, - "min_input_args": 0 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.TripletMarginWithDistanceLoss": { "Matcher": "ChangePrefixMatcher" @@ -8390,38 +8224,10 @@ } }, "torch.nn.functional.binary_cross_entropy": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.binary_cross_entropy", - "args_list": [ - "input", - "target", - "weight", - "size_average", - "reduce", - "reduction" - ], - "kwargs_change": { - "target": "label" - }, - "min_input_args": 2 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.binary_cross_entropy_with_logits": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.binary_cross_entropy_with_logits", - "args_list": [ - "input", - "target", - "weight", - "size_average", - "reduce", - "reduction", - "pos_weight" - ], - "kwargs_change": { - "input": "logit", - "target": "label" - }, - "min_input_args": 2 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.celu": { "Matcher": "GenericMatcher", @@ -8511,42 +8317,13 @@ } }, "torch.nn.functional.cosine_embedding_loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.cosine_embedding_loss", - "args_list": [ - "input1", - "input2", - "target", - "margin", - "size_average", - "reduce", - "reduction" - ], - "kwargs_change": { - "target": "label" - }, - "min_input_args": 3 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.cosine_similarity": { "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.cross_entropy": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.cross_entropy", - "args_list": [ - "input", - "target", - "weight", - "size_average", - "ignore_index", - "reduce", - "reduction", - "label_smoothing" - ], - "kwargs_change": { - "target": "label" - }, - "min_input_args": 2 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.dropout": { "Matcher": "ChangePrefixMatcher" @@ -8664,22 +8441,7 @@ ] }, "torch.nn.functional.gaussian_nll_loss": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.nn.functional.gaussian_nll_loss", - "min_input_args": 3, - "args_list": [ - "input", - "target", - "var", - "full", - "eps", - "reduction" - ], - "kwargs_change": { - "target": "label", - "var": "variance", - "eps": "epsilon" - } + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.gelu": { "Matcher": "ChangePrefixMatcher" @@ -8770,20 +8532,7 @@ } }, "torch.nn.functional.hinge_embedding_loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.hinge_embedding_loss", - "args_list": [ - "input", - "target", - "margin", - "size_average", - "reduce", - "reduction" - ], - "kwargs_change": { - "target": "label" - }, - "min_input_args": 2 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.huber_loss": { "Matcher": "GenericMatcher", @@ -8821,35 +8570,10 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.kl_div": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.kl_div", - "args_list": [ - "input", - "target", - "size_average", - "reduce", - "reduction", - "log_target" - ], - "kwargs_change": { - "target": "label" - }, - "min_input_args": 2 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.l1_loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.l1_loss", - "args_list": [ - "input", - "target", - "size_average", - "reduce", - "reduction" - ], - "kwargs_change": { - "target": "label" - }, - "min_input_args": 2 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.layer_norm": { "Matcher": "ChangePrefixMatcher" @@ -8906,23 +8630,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.margin_ranking_loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.margin_ranking_loss", - "args_list": [ - "input1", - "input2", - "target", - "margin", - "size_average", - "reduce", - "reduction" - ], - "kwargs_change": { - "input1": "input", - "input2": "other", - "target": "label" - }, - "min_input_args": 3 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.max_pool1d": { "Matcher": "ChangePrefixMatcher" @@ -8985,85 +8693,19 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.mse_loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.mse_loss", - "args_list": [ - "input", - "target", - "size_average", - "reduce", - "reduction" - ], - "kwargs_change": { - "target": "label" - }, - "min_input_args": 2 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.multi_margin_loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.multi_margin_loss", - "args_list": [ - "input", - "target", - "p", - "margin", - "weight", - "size_average", - "reduce", - "reduction" - ], - "kwargs_change": { - "target": "label" - }, - "min_input_args": 2 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.multilabel_margin_loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.multi_label_margin_loss", - "args_list": [ - "input", - "target", - "size_average", - "reduce", - "reduction" - ], - "kwargs_change": { - "target": "label" - }, - "min_input_args": 2 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.multilabel_soft_margin_loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.multi_label_soft_margin_loss", - "args_list": [ - "input", - "target", - "weight", - "size_average", - "reduce", - "reduction" - ], - "kwargs_change": { - "target": "label" - }, - "min_input_args": 2 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.nll_loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.nll_loss", - "args_list": [ - "input", - "target", - "weight", - "size_average", - "ignore_index", - "reduce", - "reduction" - ], - "kwargs_change": { - "target": "label" - }, - "min_input_args": 2 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.normalize": { "Matcher": "ChangePrefixMatcher" @@ -9097,23 +8739,7 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.poisson_nll_loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.poisson_nll_loss", - "args_list": [ - "input", - "target", - "log_input", - "full", - "size_average", - "eps", - "reduce", - "reduction" - ], - "kwargs_change": { - "target": "label", - "eps": "epsilon" - }, - "min_input_args": 2 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.prelu": { "Matcher": "GenericMatcher", @@ -9197,39 +8823,10 @@ "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.smooth_l1_loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.smooth_l1_loss", - "args_list": [ - "input", - "target", - "size_average", - "reduce", - "reduction", - "beta" - ], - "kwargs_change": { - "target": "label", - "beta": "delta" - }, - "paddle_default_kwargs": { - "is_huber": "False" - }, - "min_input_args": 2 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.soft_margin_loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.soft_margin_loss", - "args_list": [ - "input", - "target", - "size_average", - "reduce", - "reduction" - ], - "kwargs_change": { - "target": "label" - }, - "min_input_args": 2 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.softmax": { "Matcher": "ChangeAPIMatcher", @@ -9311,43 +8908,10 @@ } }, "torch.nn.functional.triplet_margin_loss": { - "Matcher": "SizeAverageMatcher", - "paddle_api": "paddle.nn.functional.triplet_margin_loss", - "min_input_args": 3, - "args_list": [ - "anchor", - "positive", - "negative", - "margin", - "p", - "eps", - "swap", - "size_average", - "reduce", - "reduction" - ], - "kwargs_change": { - "anchor": "input", - "eps": "epsilon" - } + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.triplet_margin_with_distance_loss": { - "Matcher": "GenericMatcher", - "paddle_api": "paddle.nn.functional.triplet_margin_with_distance_loss", - "args_list": [ - "anchor", - "positive", - "negative", - "*", - "distance_function", - "margin", - "swap", - "reduction" - ], - "kwargs_change": { - "anchor": "input" - }, - "min_input_args": 3 + "Matcher": "ChangePrefixMatcher" }, "torch.nn.functional.unfold": { "Matcher": "ChangeAPIMatcher", diff --git a/tests/test_nn_BCELoss.py b/tests/test_nn_BCELoss.py index 0ab233759..152eea17b 100644 --- a/tests/test_nn_BCELoss.py +++ b/tests/test_nn_BCELoss.py @@ -194,3 +194,36 @@ def test_case_11(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_12(): + """*args unpacking""" + 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]) + args = (weight,) + loss = torch.nn.BCELoss(*args, reduction='sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_13(): + """Different dim (1D) and float64 dtype""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([0.5, 0.6, 0.7, 0.4], dtype=torch.float64) + target = torch.tensor([1., 0., 1., 0.], dtype=torch.float64) + loss = torch.nn.BCELoss(reduction='none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_CosineEmbeddingLoss.py b/tests/test_nn_CosineEmbeddingLoss.py index 53594f1e9..e06d7b89a 100644 --- a/tests/test_nn_CosineEmbeddingLoss.py +++ b/tests/test_nn_CosineEmbeddingLoss.py @@ -548,3 +548,34 @@ def test_case_36(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_37(): + """*args unpacking with reduction kwarg""" + pytorch_code = textwrap.dedent( + """ + import torch + input1 = torch.Tensor([[1.6, 1.2, -0.5], [3.2, 2.6, -5.8]]).type(torch.float32) + input2 = torch.Tensor([[0.5, 0.5, -1.8], [2.3, -1.4, 1.1]]).type(torch.float32) + label = torch.Tensor([1, -1]).type(torch.int64) + args = (0.5,) + loss = torch.nn.CosineEmbeddingLoss(*args, reduction='sum') + result = loss(input1, input2, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_38(): + """Out-of-order keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + input1 = torch.Tensor([[1.6, 1.2, -0.5], [3.2, 2.6, -5.8]]).type(torch.float32) + input2 = torch.Tensor([[0.5, 0.5, -1.8], [2.3, -1.4, 1.1]]).type(torch.float32) + label = torch.Tensor([1, -1]).type(torch.int64) + loss = torch.nn.CosineEmbeddingLoss(reduction='none', margin=0.3) + result = loss(input1, input2, label) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_GaussianNLLLoss.py b/tests/test_nn_GaussianNLLLoss.py index 65f50197d..863f6795f 100644 --- a/tests/test_nn_GaussianNLLLoss.py +++ b/tests/test_nn_GaussianNLLLoss.py @@ -92,3 +92,78 @@ def test_case_5(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """All keyword arguments, reduction='none'""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.GaussianNLLLoss(full=True, eps=1e-06, reduction='none') + input = torch.tensor([[0.5, -0.3], [1.2, 0.7]], dtype=torch.float32) + label = torch.tensor([[0.4, -0.2], [1.0, 0.5]], dtype=torch.float32) + variance = torch.tensor([[0.1, 0.2], [0.3, 0.4]], dtype=torch.float32) + result = loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """Out-of-order keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.GaussianNLLLoss(reduction='sum', eps=1e-08, full=False) + input = torch.tensor([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=torch.float32) + label = torch.tensor([[0.0, 0.1, 0.2], [0.3, 0.4, 0.5]], dtype=torch.float32) + variance = torch.tensor([[0.5, 0.5, 0.5], [0.5, 0.5, 0.5]], dtype=torch.float32) + result = loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """Default args (no kwargs)""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.GaussianNLLLoss() + input = torch.tensor([[1.5, 2.3], [-0.5, 1.7]], dtype=torch.float32) + label = torch.tensor([[1.0, 2.0], [-0.3, 1.5]], dtype=torch.float32) + variance = torch.tensor([[0.2, 0.3], [0.4, 0.5]], dtype=torch.float32) + result = loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """3D inputs""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.GaussianNLLLoss(full=False, eps=1e-06, reduction='mean') + input = torch.ones([2, 3, 4], dtype=torch.float32) * 0.5 + label = torch.ones([2, 3, 4], dtype=torch.float32) + variance = torch.ones([2, 3, 4], dtype=torch.float32) * 2.0 + result = loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """float64 dtype""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.GaussianNLLLoss(eps=1e-10) + input = torch.tensor([[0.5, -0.3], [1.2, 0.7]], dtype=torch.float64) + label = torch.tensor([[0.4, -0.2], [1.0, 0.5]], dtype=torch.float64) + variance = torch.tensor([[0.1, 0.2], [0.3, 0.4]], dtype=torch.float64) + result = loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_HingeEmbeddingLoss.py b/tests/test_nn_HingeEmbeddingLoss.py index 998033142..dc7435590 100644 --- a/tests/test_nn_HingeEmbeddingLoss.py +++ b/tests/test_nn_HingeEmbeddingLoss.py @@ -358,3 +358,32 @@ def test_case_25(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_26(): + """*args unpacking""" + 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) + args = (1.5,) + cri = torch.nn.HingeEmbeddingLoss(*args, reduction='sum') + result = cri(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_27(): + """Out-of-order keyword arguments""" + 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.HingeEmbeddingLoss(reduction='none', margin=2.0) + result = cri(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_L1Loss.py b/tests/test_nn_L1Loss.py index 540bc250b..283bb2cdb 100644 --- a/tests/test_nn_L1Loss.py +++ b/tests/test_nn_L1Loss.py @@ -184,3 +184,34 @@ def test_case_11(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_12(): + """*args unpacking""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[1.0, 2.0], [3.0, 4.0]]) + target = torch.tensor([[1.5, 1.5], [3.5, 3.5]]) + args = () + loss = torch.nn.L1Loss(*args, reduction='sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_13(): + """3D inputs with float64 dtype""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.ones([2, 3, 4], dtype=torch.float64) * 0.5 + target = torch.ones([2, 3, 4], dtype=torch.float64) + loss = torch.nn.L1Loss(reduction='none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_MSELoss.py b/tests/test_nn_MSELoss.py index e219d25bb..323b987c6 100644 --- a/tests/test_nn_MSELoss.py +++ b/tests/test_nn_MSELoss.py @@ -184,3 +184,34 @@ def test_case_11(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_12(): + """*args unpacking""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.tensor([[1.0, 2.0], [3.0, 4.0]]) + target = torch.tensor([[1.5, 1.5], [3.5, 3.5]]) + args = () + loss = torch.nn.MSELoss(*args, reduction='sum') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_13(): + """3D inputs with float64 dtype""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.ones([2, 3, 4], dtype=torch.float64) * 0.5 + target = torch.ones([2, 3, 4], dtype=torch.float64) + loss = torch.nn.MSELoss(reduction='none') + result = loss(input, target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_MarginRankingLoss.py b/tests/test_nn_MarginRankingLoss.py index 3b90667b6..bc65bc1eb 100644 --- a/tests/test_nn_MarginRankingLoss.py +++ b/tests/test_nn_MarginRankingLoss.py @@ -383,3 +383,34 @@ def test_case_25(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_26(): + """*args unpacking""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, 2], [3, 4]]).type(torch.float32) + other = torch.Tensor([[2, 1], [2, 4]]).type(torch.float32) + label = torch.Tensor([[1, -1], [-1, -1]]).type(torch.float32) + args = (0.5,) + loss = torch.nn.MarginRankingLoss(*args, reduction='sum') + result = loss(input, other, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_27(): + """Out-of-order keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, 2], [3, 4]]).type(torch.float32) + other = torch.Tensor([[2, 1], [2, 4]]).type(torch.float32) + label = torch.Tensor([[1, -1], [-1, -1]]).type(torch.float32) + loss = torch.nn.MarginRankingLoss(reduction='none', margin=1.0) + result = loss(input, other, label) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_MultiLabelMarginLoss.py b/tests/test_nn_MultiLabelMarginLoss.py index 839d89b3d..210d6257e 100644 --- a/tests/test_nn_MultiLabelMarginLoss.py +++ b/tests/test_nn_MultiLabelMarginLoss.py @@ -121,3 +121,32 @@ def test_case_8(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """*args unpacking""" + pytorch_code = textwrap.dedent( + """ + import torch + args = () + loss = torch.nn.MultiLabelMarginLoss(*args, reduction='none') + input = torch.tensor([[0.1, 0.2, 0.4, 0.8], [0.2, 0.5, 0.3, 0.1]]).to(dtype=torch.float32) + label = torch.LongTensor([[3, 0, -1, -1], [0, 2, -1, -1]]) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """Out-of-order keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.MultiLabelMarginLoss(reduction='sum', reduce=None, size_average=None) + input = torch.tensor([[0.1, 0.2, 0.4, 0.8], [0.2, 0.5, 0.3, 0.1]]).to(dtype=torch.float32) + label = torch.LongTensor([[3, 0, -1, -1], [0, 2, -1, -1]]) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_MultiLabelSoftMarginLoss.py b/tests/test_nn_MultiLabelSoftMarginLoss.py index d2bcb2b76..24c33b6be 100644 --- a/tests/test_nn_MultiLabelSoftMarginLoss.py +++ b/tests/test_nn_MultiLabelSoftMarginLoss.py @@ -85,3 +85,33 @@ def test_case_5(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """*args unpacking""" + pytorch_code = textwrap.dedent( + """ + import torch + weight = torch.tensor([1.0, 0.5, 1.0]) + args = (weight,) + loss = torch.nn.MultiLabelSoftMarginLoss(*args, reduction='sum') + input = torch.tensor([[1, -2, 3], [0, -1, 2]]).to(dtype=torch.float32) + label = torch.tensor([[-1, 1, -1], [1, 1, 1]]).to(dtype=torch.float32) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """Out-of-order keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.MultiLabelSoftMarginLoss(reduction='none', weight=None) + input = torch.tensor([[1, -2, 3], [0, -1, 2]]).to(dtype=torch.float32) + label = torch.tensor([[-1, 1, -1], [1, 1, 1]]).to(dtype=torch.float32) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_MultiMarginLoss.py b/tests/test_nn_MultiMarginLoss.py index ebdc87022..d4c18761f 100644 --- a/tests/test_nn_MultiMarginLoss.py +++ b/tests/test_nn_MultiMarginLoss.py @@ -87,3 +87,32 @@ def test_case_5(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """*args unpacking""" + pytorch_code = textwrap.dedent( + """ + import torch + args = (1, 1.0) + loss = torch.nn.MultiMarginLoss(*args, reduction='sum') + input = torch.tensor([[1, -2, 3], [0, -1, 2], [1, 0, 1]]).to(dtype=torch.float32) + label = torch.tensor([0, 1, 2]) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """Out-of-order keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.MultiMarginLoss(reduction='none', margin=2.0, p=2) + input = torch.tensor([[1, -2, 3], [0, -1, 2], [1, 0, 1]]).to(dtype=torch.float32) + label = torch.tensor([0, 1, 2]) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_NLLLoss.py b/tests/test_nn_NLLLoss.py index 5ff007083..efa8765f1 100644 --- a/tests/test_nn_NLLLoss.py +++ b/tests/test_nn_NLLLoss.py @@ -302,3 +302,37 @@ def test_case_19(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_20(): + """*args unpacking""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.arange(0, 15, dtype=torch.float32, requires_grad=True).reshape((3, 5)) + target = torch.tensor([1, 0, 4]) + m = nn.LogSoftmax(dim=1) + weight = torch.tensor([1.0, 0.5, 1.0, 0.7, 1.2]) + args = (weight,) + loss = nn.NLLLoss(*args, ignore_index=2, reduction='sum') + result = loss(m(input), target) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_21(): + """Out-of-order keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + input = torch.arange(0, 15, dtype=torch.float32, requires_grad=True).reshape((3, 5)) + target = torch.tensor([1, 0, 4]) + m = nn.LogSoftmax(dim=1) + loss = nn.NLLLoss(reduction='none', ignore_index=-100, weight=None) + result = loss(m(input), target) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_PoissonNLLLoss.py b/tests/test_nn_PoissonNLLLoss.py index 19328e4e1..a972e34b2 100644 --- a/tests/test_nn_PoissonNLLLoss.py +++ b/tests/test_nn_PoissonNLLLoss.py @@ -87,3 +87,32 @@ def test_case_5(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """*args unpacking with reduction kwarg""" + pytorch_code = textwrap.dedent( + """ + import torch + args = (True, False) + loss = torch.nn.PoissonNLLLoss(*args, eps=1e-06, reduction='mean') + input = torch.full([5, 2], 1).to(dtype=torch.float32) + label = torch.full([5, 2], 2).to(dtype=torch.float32) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """Out-of-order keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + loss = torch.nn.PoissonNLLLoss(reduction='none', eps=1e-08, full=False, log_input=True) + input = torch.full([5, 2], 1).to(dtype=torch.float32) + label = torch.full([5, 2], 2).to(dtype=torch.float32) + result = loss(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_SoftMarginLoss.py b/tests/test_nn_SoftMarginLoss.py index cb17ae046..ba61b17b3 100644 --- a/tests/test_nn_SoftMarginLoss.py +++ b/tests/test_nn_SoftMarginLoss.py @@ -358,3 +358,32 @@ def test_case_25(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_26(): + """*args unpacking""" + 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) + args = () + cri = torch.nn.SoftMarginLoss(*args, reduction='sum') + result = cri(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_27(): + """Out-of-order keyword arguments""" + 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.SoftMarginLoss(reduction='none', reduce=None, size_average=None) + result = cri(input, label) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_TripletMarginLoss.py b/tests/test_nn_TripletMarginLoss.py index 1e4290cad..7362f400a 100644 --- a/tests/test_nn_TripletMarginLoss.py +++ b/tests/test_nn_TripletMarginLoss.py @@ -503,3 +503,34 @@ def test_case_33(): """ ) obj.run(pytorch_code, ["result"], rtol=1.0e-5, atol=1.0e-8) + + +def test_case_34(): + """*args unpacking""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, 5, 3], [0, 3, 2], [1, 4, 1]]).type(torch.float32) + positive= torch.Tensor([[5, 1, 2], [3, 2, 1], [3, -1, 1]]).type(torch.float32) + negative = torch.Tensor([[2, 1, -3], [1, 1, -1], [4, -2, 1]]).type(torch.float32) + args = (1.5, 2) + cri = torch.nn.TripletMarginLoss(*args, eps=1e-06, swap=True, reduction='sum') + result = cri(input, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"], rtol=1.0e-5, atol=1.0e-8) + + +def test_case_35(): + """Out-of-order keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.Tensor([[1, 5, 3], [0, 3, 2], [1, 4, 1]]).type(torch.float32) + positive= torch.Tensor([[5, 1, 2], [3, 2, 1], [3, -1, 1]]).type(torch.float32) + negative = torch.Tensor([[2, 1, -3], [1, 1, -1], [4, -2, 1]]).type(torch.float32) + cri = torch.nn.TripletMarginLoss(reduction='none', swap=True, eps=1e-06, p=2, margin=1.0) + result = cri(input, positive, negative) + """ + ) + obj.run(pytorch_code, ["result"], rtol=1.0e-5, atol=1.0e-8) diff --git a/tests/test_nn_functional_gaussian_nll_loss.py b/tests/test_nn_functional_gaussian_nll_loss.py index 20954254d..aec0a6b11 100644 --- a/tests/test_nn_functional_gaussian_nll_loss.py +++ b/tests/test_nn_functional_gaussian_nll_loss.py @@ -82,3 +82,102 @@ def test_case_5(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_6(): + """All keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[0.5, -0.3], [1.2, 0.7], [-0.8, 0.4]], dtype=torch.float32) + label = torch.tensor([[0.4, -0.2], [1.0, 0.5], [-0.6, 0.3]], dtype=torch.float32) + variance = torch.tensor([[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]], dtype=torch.float32) + result = torch.nn.functional.gaussian_nll_loss(input=input, target=label, var=variance, full=True, eps=1e-08, reduction='sum') + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """Mixed positional and keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[0.5, -0.3], [1.2, 0.7]], dtype=torch.float32) + label = torch.tensor([[0.4, -0.2], [1.0, 0.5]], dtype=torch.float32) + variance = torch.tensor([[0.1, 0.2], [0.3, 0.4]], dtype=torch.float32) + result = torch.nn.functional.gaussian_nll_loss(input, label, variance, full=True, reduction='none') + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """Out-of-order keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[0.5, -0.3, 0.1], [1.2, 0.7, -0.5]], dtype=torch.float32) + label = torch.tensor([[0.4, -0.2, 0.0], [1.0, 0.5, -0.4]], dtype=torch.float32) + variance = torch.tensor([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=torch.float32) + result = torch.nn.functional.gaussian_nll_loss(reduction='mean', eps=1e-06, full=False, var=variance, target=label, input=input) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """3D input tensors""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.ones([2, 3, 4], dtype=torch.float32) * 0.5 + label = torch.ones([2, 3, 4], dtype=torch.float32) + variance = torch.ones([2, 3, 4], dtype=torch.float32) * 2.0 + result = torch.nn.functional.gaussian_nll_loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """float64 input""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[0.5, -0.3], [1.2, 0.7]], dtype=torch.float64) + label = torch.tensor([[0.4, -0.2], [1.0, 0.5]], dtype=torch.float64) + variance = torch.tensor([[0.1, 0.2], [0.3, 0.4]], dtype=torch.float64) + result = torch.nn.functional.gaussian_nll_loss(input, label, variance, reduction='sum') + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_11(): + """Variable args unpacking""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.ones([5, 2], dtype=torch.float32) + label = torch.ones([5, 2], dtype=torch.float32) + variance = torch.ones([5, 2], dtype=torch.float32) + args = (input, label, variance) + result = torch.nn.functional.gaussian_nll_loss(*args) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_12(): + """Default args omitted""" + pytorch_code = textwrap.dedent( + """ + import torch + input = torch.tensor([[1.5, 2.3], [-0.5, 1.7]], dtype=torch.float32) + label = torch.tensor([[1.0, 2.0], [-0.3, 1.5]], dtype=torch.float32) + variance = torch.tensor([[0.2, 0.3], [0.4, 0.5]], dtype=torch.float32) + result = torch.nn.functional.gaussian_nll_loss(input, label, variance) + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_nn_functional_triplet_margin_with_distance_loss.py b/tests/test_nn_functional_triplet_margin_with_distance_loss.py index 311e5beee..08e430503 100644 --- a/tests/test_nn_functional_triplet_margin_with_distance_loss.py +++ b/tests/test_nn_functional_triplet_margin_with_distance_loss.py @@ -116,3 +116,95 @@ def test_case_6(): """ ) obj.run(pytorch_code, ["result"]) + + +def test_case_7(): + """All keyword arguments with all options""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + import numpy as np + np.random.seed(101) + anchor = torch.tensor(np.random.rand(64, 32), dtype=torch.float32) + positive = torch.tensor(np.random.rand(64, 32), dtype=torch.float32) + negative = torch.tensor(np.random.rand(64, 32), dtype=torch.float32) + result = torch.nn.functional.triplet_margin_with_distance_loss( + anchor=anchor, positive=positive, negative=negative, + distance_function=nn.PairwiseDistance(p=2), + margin=1.5, swap=False, reduction='sum') + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_8(): + """Mixed positional and keyword arguments""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + import numpy as np + np.random.seed(102) + anchor = torch.tensor(np.random.rand(8, 16), dtype=torch.float32) + positive = torch.tensor(np.random.rand(8, 16), dtype=torch.float32) + negative = torch.tensor(np.random.rand(8, 16), dtype=torch.float32) + result = torch.nn.functional.triplet_margin_with_distance_loss( + anchor, positive, negative, margin=2.0, swap=True) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_9(): + """Default args omitted (no distance_function, all defaults)""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + import numpy as np + np.random.seed(103) + anchor = torch.tensor(np.random.rand(4, 8), dtype=torch.float32) + positive = torch.tensor(np.random.rand(4, 8), dtype=torch.float32) + negative = torch.tensor(np.random.rand(4, 8), dtype=torch.float32) + result = torch.nn.functional.triplet_margin_with_distance_loss( + anchor, positive, negative, reduction='none') + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_10(): + """Variable args unpacking""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + import numpy as np + np.random.seed(104) + anchor = torch.tensor(np.random.rand(16, 8), dtype=torch.float32) + positive = torch.tensor(np.random.rand(16, 8), dtype=torch.float32) + negative = torch.tensor(np.random.rand(16, 8), dtype=torch.float32) + args = (anchor, positive, negative) + result = torch.nn.functional.triplet_margin_with_distance_loss(*args, margin=1.0) + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_11(): + """float64 dtype""" + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + import numpy as np + np.random.seed(105) + anchor = torch.tensor(np.random.rand(8, 8), dtype=torch.float64) + positive = torch.tensor(np.random.rand(8, 8), dtype=torch.float64) + negative = torch.tensor(np.random.rand(8, 8), dtype=torch.float64) + result = torch.nn.functional.triplet_margin_with_distance_loss( + anchor, positive, negative, margin=0.5) + """ + ) + obj.run(pytorch_code, ["result"])