Skip to content

易用性提升69个API核对 #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 29, 2024
Merged
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
16 changes: 8 additions & 8 deletions paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -10505,8 +10505,7 @@
"remove_duplicate"
],
"kwargs_change": {
"recurse": "include_sublayers",
"remove_duplicate": ""
"recurse": "include_sublayers"
},
"min_input_args": 0
},
Expand All @@ -10523,10 +10522,12 @@
"prefix",
"remove_duplicate"
],
"unsupport_args": [
"memo",
"remove_duplicate"
],
"kwargs_change": {
"memo": "layers_set"
},
"paddle_default_kwargs": {
"include_self": "True"
},
"min_input_args": 0
},
"torch.nn.Module.named_parameters": {
Expand All @@ -10539,8 +10540,7 @@
"remove_duplicate"
],
"kwargs_change": {
"recurse": "include_sublayers",
"remove_duplicate": ""
"recurse": "include_sublayers"
}
},
"torch.nn.Module.parameters": {
Expand Down
16 changes: 11 additions & 5 deletions tests/test_linalg_lu_factor_ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def test_case_1():
import torch
x = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=torch.float64)
LU, pivots, info = torch.linalg.lu_factor_ex(x)
info = info.item()
"""
)
obj.run(pytorch_code, ["LU", "pivots", "info"])
Expand All @@ -37,7 +36,6 @@ def test_case_2():
import torch
x = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=torch.float64)
LU, pivots, info = torch.linalg.lu_factor_ex(A=x)
info = info.item()
"""
)
obj.run(pytorch_code, ["LU", "pivots", "info"])
Expand All @@ -49,7 +47,6 @@ def test_case_3():
import torch
x = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=torch.float64)
LU, pivots, info = torch.linalg.lu_factor_ex(pivot=True, A=x)
info = info.item()
"""
)
obj.run(pytorch_code, ["LU", "pivots", "info"])
Expand All @@ -62,7 +59,6 @@ def test_case_4():
x = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=torch.float64)
out = (torch.tensor([], dtype=torch.float64), torch.tensor([], dtype=torch.int), torch.tensor([], dtype=torch.int))
LU, pivots, info = torch.linalg.lu_factor_ex(x, pivot=True, check_errors=False, out=out)
info = info.item()
"""
)
obj.run(pytorch_code, ["LU", "pivots", "info"])
Expand All @@ -75,7 +71,17 @@ def test_case_5():
x = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=torch.float64)
out = (torch.tensor([], dtype=torch.float64), torch.tensor([], dtype=torch.int), torch.tensor([], dtype=torch.int))
LU, pivots, info = torch.linalg.lu_factor_ex(A=x, pivot=True, check_errors=True, out=out)
info = info.item()
"""
)
obj.run(pytorch_code, ["LU", "pivots", "info"])


def test_case_6():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]], dtype=torch.float64)
LU, pivots, info = torch.linalg.lu_factor_ex(x)
"""
)
obj.run(pytorch_code, ["LU", "pivots", "info"])
49 changes: 49 additions & 0 deletions tests/test_linalg_solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,52 @@ def test_case_9():
"""
)
obj.run(pytorch_code, ["result", "out"])


def test_case_10():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor([[3.0, 1],[1, 2]])
y = torch.tensor([[9.0, 8, 3], [-4.0, 6, 2]])
out = torch.tensor([])
result = torch.linalg.solve(A=x, B=y, out=out, left=True)
"""
)
obj.run(pytorch_code, ["result", "out"])


def test_case_11():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor([[3.0, 1],[1, 2]])
y = torch.tensor([[9.0, 8, 3], [-4.0, 6, 2]])
result = torch.linalg.solve(x, y, left=True)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_12():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor([[3.0, 1],[1, 2]])
y = torch.tensor([[9.0, 8, 3], [-4.0, 6, 2]])
result = torch.linalg.solve(A=x, B=y, left=True)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_13():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor([[3.0, 1],[1, 2]])
y = torch.tensor([[9.0, 8, 3], [-4.0, 6, 2]])
result = torch.linalg.solve(B=y, left=True, A=x)
"""
)
obj.run(pytorch_code, ["result"])
21 changes: 3 additions & 18 deletions tests/test_nn_Module_named_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ def test_case_1():
result = name_list
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="doesn't support param remove_duplicate",
)
obj.run(pytorch_code, ["result"])


def test_case_2():
Expand All @@ -65,12 +60,7 @@ def test_case_2():
result = name_list
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle only support to output remove duplicate version",
)
obj.run(pytorch_code, ["result"])


def test_case_3():
Expand All @@ -92,9 +82,4 @@ def test_case_3():
result = name_list
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle doesn't support the memo parameter",
)
obj.run(pytorch_code, ["result"])
96 changes: 96 additions & 0 deletions tests/test_nn_Module_named_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,99 @@ def forward(self, x):
"""
)
obj.run(pytorch_code, ["result"], check_value=False)


def test_case_8():
pytorch_code = textwrap.dedent(
"""
import torch
import torch.nn as nn
result = []
class TestForHook(nn.Module):
def __init__(self):
super().__init__()
self.register_parameter('w1', nn.Parameter(torch.randn(2, 3)))
self.register_parameter('w1', nn.Parameter(torch.randn(3, 3)))
self.register_parameter('w2', nn.Parameter(torch.randn(1, 3)))
self.register_parameter('w2', nn.Parameter(torch.randn(1, 2)))
def forward(self, x):
return x

a = TestForHook()
for a,b in a.named_parameters(remove_duplicate=False):
result.append(b)
"""
)
obj.run(pytorch_code, ["result"], check_value=False)


def test_case_9():
pytorch_code = textwrap.dedent(
"""
import torch
import torch.nn as nn
result = []
class TestForHook(nn.Module):
def __init__(self):
super().__init__()
self.register_parameter('w1', nn.Parameter(torch.randn(2, 3)))
self.register_parameter('w1', nn.Parameter(torch.randn(3, 3)))
self.register_parameter('w2', nn.Parameter(torch.randn(1, 3)))
self.register_parameter('w2', nn.Parameter(torch.randn(1, 2)))
def forward(self, x):
return x

a = TestForHook()
for a,b in a.named_parameters(remove_duplicate=True):
result.append(b)
"""
)
obj.run(pytorch_code, ["result"], check_value=False)


def test_case_10():
pytorch_code = textwrap.dedent(
"""
import torch
import torch.nn as nn
result = []
class TestForHook(nn.Module):
def __init__(self):
super().__init__()
self.register_parameter('w1', nn.Parameter(torch.randn(2, 3)))
self.register_parameter('w1', nn.Parameter(torch.randn(3, 3)))
self.register_parameter('w2', nn.Parameter(torch.randn(1, 3)))
self.register_parameter('w2', nn.Parameter(torch.randn(1, 2)))
def forward(self, x):
return x

a = TestForHook()
for a,b in a.named_parameters(prefix = "wfs", recurse = False, remove_duplicate = False):
result.append(b)
"""
)
obj.run(pytorch_code, ["result"], check_value=False)


def test_case_11():
pytorch_code = textwrap.dedent(
"""
import torch
import torch.nn as nn
result = []
class TestForHook(nn.Module):
def __init__(self):
super().__init__()
self.register_parameter('w1', nn.Parameter(torch.randn(2, 3)))
self.register_parameter('w1', nn.Parameter(torch.randn(3, 3)))
self.register_parameter('w2', nn.Parameter(torch.randn(1, 3)))
self.register_parameter('w2', nn.Parameter(torch.randn(1, 2)))
def forward(self, x):
return x

a = TestForHook()
for a,b in a.named_parameters(remove_duplicate = False, recurse = True, prefix = "wfs"):
result.append(b)
"""
)
obj.run(pytorch_code, ["result"], check_value=False)
4 changes: 1 addition & 3 deletions tests/test_optim_Adadelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ def test_case_6():
)


# if `weight_decay` set as int rather than float,
# paddle will raise TypeError: 'int' object is not callable
def _test_case_7():
def test_case_7():
pytorch_code = textwrap.dedent(
generate_optimizer_test_code(
"torch.optim.Adadelta(params=conv.parameters(), weight_decay=0)"
Expand Down
4 changes: 1 addition & 3 deletions tests/test_optim_Adagrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ def test_case_6():
obj.run(pytorch_code, ["result"], rtol=1.0e-5)


# if `weight_decay` set as int rather than float,
# paddle will raise TypeError: 'int' object is not callable
def _test_case_7():
def test_case_7():
pytorch_code = textwrap.dedent(
generate_optimizer_test_code(
"torch.optim.Adagrad(params=conv.parameters(), weight_decay=0)"
Expand Down
18 changes: 18 additions & 0 deletions tests/test_optim_Adam.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,21 @@ def test_case_7():
)
)
obj.run(pytorch_code, ["result"], rtol=1.0e-5)


def test_case_8():
pytorch_code = textwrap.dedent(
generate_optimizer_test_code(
"torch.optim.Adam(conv.parameters(), weight_decay=1)"
)
)
obj.run(pytorch_code, ["result"], rtol=1.0e-5)


def test_case_9():
pytorch_code = textwrap.dedent(
generate_optimizer_test_code(
"torch.optim.Adam(params=conv.parameters(), lr=0.001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0)"
)
)
obj.run(pytorch_code, ["result"], rtol=1.0e-5)
18 changes: 18 additions & 0 deletions tests/test_optim_AdamW.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,21 @@ def test_case_6():
generate_optimizer_test_code("torch.optim.AdamW(conv.parameters())")
)
obj.run(pytorch_code, ["result"], rtol=1.0e-5)


def test_case_7():
pytorch_code = textwrap.dedent(
generate_optimizer_test_code(
"torch.optim.AdamW(conv.parameters(), weight_decay=1)"
)
)
obj.run(pytorch_code, ["result"], rtol=1.0e-5)


def test_case_8():
pytorch_code = textwrap.dedent(
generate_optimizer_test_code(
"torch.optim.AdamW(params=conv.parameters(), lr=0.001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0)"
)
)
obj.run(pytorch_code, ["result"], rtol=1.0e-5)
18 changes: 18 additions & 0 deletions tests/test_optim_Adamax.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,21 @@ def test_case_6():
generate_optimizer_test_code("torch.optim.Adamax(conv.parameters())")
)
obj.run(pytorch_code, ["result"], rtol=1.0e-2)


def test_case_7():
pytorch_code = textwrap.dedent(
generate_optimizer_test_code(
"torch.optim.Adamax(conv.parameters(), weight_decay=0, eps=1e-7)"
)
)
obj.run(pytorch_code, ["result"], rtol=1.0e-2)


def test_case_8():
pytorch_code = textwrap.dedent(
generate_optimizer_test_code(
"torch.optim.Adamax(conv.parameters(), weight_decay=1, eps=1e-7)"
)
)
obj.run(pytorch_code, ["result"], rtol=1.0e-2)
4 changes: 1 addition & 3 deletions tests/test_optim_RMSprop.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ def test_case_4():
obj.run(pytorch_code, ["result"], rtol=1.0e-2)


# if `weight_decay` set as int rather than float,
# paddle will raise TypeError: 'int' object is not callable
def _test_case_5():
def test_case_5():
pytorch_code = textwrap.dedent(
generate_optimizer_test_code(
"torch.optim.RMSprop(conv.parameters(), weight_decay=0)"
Expand Down
4 changes: 1 addition & 3 deletions tests/test_optim_SGD.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ def test_case_3():
obj.run(pytorch_code, ["result"], rtol=1.0e-5)


# if `weight_decay` set as int rather than float,
# paddle will raise TypeError: 'int' object is not callable
def _test_case_4():
def test_case_4():
pytorch_code = textwrap.dedent(
generate_optimizer_test_code(
"torch.optim.SGD(conv.parameters(), lr=0.1, weight_decay=0)"
Expand Down