Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
510 changes: 37 additions & 473 deletions paconvert/api_mapping.json

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions tests/test_nn_BCELoss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
31 changes: 31 additions & 0 deletions tests/test_nn_CosineEmbeddingLoss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
75 changes: 75 additions & 0 deletions tests/test_nn_GaussianNLLLoss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
29 changes: 29 additions & 0 deletions tests/test_nn_HingeEmbeddingLoss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
31 changes: 31 additions & 0 deletions tests/test_nn_L1Loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
31 changes: 31 additions & 0 deletions tests/test_nn_MSELoss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
31 changes: 31 additions & 0 deletions tests/test_nn_MarginRankingLoss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
29 changes: 29 additions & 0 deletions tests/test_nn_MultiLabelMarginLoss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
30 changes: 30 additions & 0 deletions tests/test_nn_MultiLabelSoftMarginLoss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
29 changes: 29 additions & 0 deletions tests/test_nn_MultiMarginLoss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Loading