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
8 changes: 0 additions & 8 deletions paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -9774,14 +9774,6 @@
"paddle_api": "paddle.optimizer.Optimizer.state_dict",
"min_input_args": 0
},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

step应该是一个call调用

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果在api_mapping中使用ChangePrefixMatcher会导致opt.step()变成paddle.optim.Optimizer.step(),所以我放到attribute_mapping里面了

"torch.optim.Optimizer.step": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.optimizer.Optimizer.step",
"min_input_args": 0,
"unsupport_args": [
"closure"
]
},
"torch.optim.Optimizer.zero_grad": {
"Matcher": "ZeroGradMatcher",
"paddle_api": "paddle.optimizer.Optimizer.clear_gradients",
Expand Down
3 changes: 3 additions & 0 deletions paconvert/attribute_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@
"torch.nn": {
"Matcher": "ChangePrefixMatcher"
},
"torch.optim.Optimizer.step": {
"Matcher": "ChangePrefixMatcher"
},
"torch.pi": {
"Matcher": "ChangePrefixMatcher"
},
Expand Down
37 changes: 19 additions & 18 deletions tests/test_optim_Optimizer_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import textwrap

import numpy as np
from apibase import APIBase


Expand All @@ -30,7 +31,10 @@ def compare(
rtol=1.0e-6,
atol=0.0,
):
assert paddle_result == pytorch_result
if isinstance(paddle_result, np.ndarray):
np.testing.assert_array_equal(paddle_result, pytorch_result)
else:
assert paddle_result == pytorch_result


obj = optimOptimizerAPIBase("torch.optim.Optimizer.step")
Expand All @@ -47,27 +51,27 @@ def test_case_1():
result = type(optim.step)
"""
)
obj.run(
pytorch_code,
["result"],
)
obj.run(pytorch_code, ["result"])


def _test_case_2():
def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
import torch.nn as nn
theta = torch.tensor([1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0], requires_grad=True)
l = torch.nn.Linear(10, 1)
optim = torch.optim.SGD(l.parameters(), lr = 1.0)
torch.nn.init.constant_(l.weight, 3.0)
torch.nn.init.constant_(l.bias, 4.0)
optim = torch.optim.SGD(l.parameters(), lr=1.0)
z = l(theta)
z.backward()
optim.step()
result = optim.state_dict()
weight = l.weight.data.numpy(force=True)
bias = l.bias.data.numpy(force=True)
"""
)
obj.run(pytorch_code, ["result"])
obj.run(pytorch_code, ["weight", "bias"])


def test_case_3():
Expand All @@ -78,7 +82,9 @@ def test_case_3():

theta = torch.tensor([1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0], requires_grad=True)
l = torch.nn.Linear(10, 1)
optim = torch.optim.LBFGS(l.parameters(), lr=0.01)
torch.nn.init.constant_(l.weight, 3.0)
torch.nn.init.constant_(l.bias, 4.0)
optim = torch.optim.SGD(l.parameters(), lr=0.01)

def closure():
optim.zero_grad()
Expand All @@ -87,13 +93,8 @@ def closure():
loss.backward()
return loss

optim.step(closure=closure)
result = optim.state_dict()
loss_value = optim.step(closure=closure)
result = loss_value.numpy(force=True)
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle does not support 'closure' now!",
)
obj.run(pytorch_code, ["result"])
Loading