Skip to content
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
10 changes: 1 addition & 9 deletions paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -8447,15 +8447,7 @@
"Matcher": "ChangePrefixMatcher"
},
"torch.nn.ParameterDict": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.nn.ParameterDict",
"min_input_args": 0,
"args_list": [
"values"
],
"kwargs_change": {
"values": "parameters"
}
"Matcher": "ChangePrefixMatcher"
},
"torch.nn.ParameterList": {
"Matcher": "GenericMatcher",
Expand Down
161 changes: 161 additions & 0 deletions tests/test_nn_ParameterDict.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,164 @@ def test_case_5():
"""
)
obj.run(pytorch_code, ["result"])


def test_case_6():
pytorch_code = textwrap.dedent(
"""
import torch.nn as nn
import torch
choices = nn.ParameterDict(parameters={
'a': nn.Parameter(torch.ones(2, 3)),
'b': nn.Parameter(torch.zeros(4)),
})
result = list(choices)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_7():
pytorch_code = textwrap.dedent(
"""
import torch.nn as nn
import torch
choices = nn.ParameterDict([
('a', nn.Parameter(torch.ones(2, 3))),
('b', nn.Parameter(torch.zeros(4))),
])
result = list(choices)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_8():
pytorch_code = textwrap.dedent(
"""
import torch.nn as nn
import torch
choices = nn.ParameterDict({'w': nn.Parameter(torch.ones(2, 3))})
result = choices['w']
"""
)
obj.run(pytorch_code, ["result"], check_stop_gradient=False)


def test_case_9():
pytorch_code = textwrap.dedent(
"""
import torch.nn as nn
import torch
choices = nn.ParameterDict({
'a': nn.Parameter(torch.ones(1)),
'b': nn.Parameter(torch.ones(2)),
'c': nn.Parameter(torch.ones(3)),
})
result = len(choices)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_10():
pytorch_code = textwrap.dedent(
"""
import torch.nn as nn
import torch
choices = nn.ParameterDict({
'a': nn.Parameter(torch.ones(1)),
'b': nn.Parameter(torch.ones(2)),
})
result = list(choices.keys())
"""
)
obj.run(pytorch_code, ["result"])


def test_case_11():
pytorch_code = textwrap.dedent(
"""
import torch.nn as nn
import torch
choices = nn.ParameterDict({
'a': nn.Parameter(torch.ones(2, 3)),
'b': nn.Parameter(torch.zeros(4)),
})
result = list(choices.values())
"""
)
obj.run(pytorch_code, ["result"], check_stop_gradient=False)


def test_case_12():
pytorch_code = textwrap.dedent(
"""
import torch.nn as nn
import torch
choices = nn.ParameterDict({'a': nn.Parameter(torch.ones(2))})
result = 'a' in choices
"""
)
obj.run(pytorch_code, ["result"])


def test_case_14():
pytorch_code = textwrap.dedent(
"""
import torch.nn as nn
import torch
choices = nn.ParameterDict(parameters={
'a': nn.Parameter(torch.ones(2, 3)),
'b': nn.Parameter(torch.zeros(4)),
})
result = choices['a']
"""
)
obj.run(pytorch_code, ["result"], check_stop_gradient=False)
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.

check_stop_gradient 这个关闭的原因是?



def test_case_15():
pytorch_code = textwrap.dedent(
"""
import torch.nn as nn
import torch
choices = nn.ParameterDict(parameters={
'a': nn.Parameter(torch.ones(2, 3)),
'b': nn.Parameter(torch.zeros(4)),
})
result = choices['b']
"""
)
obj.run(pytorch_code, ["result"], check_stop_gradient=False)


def test_case_13():
pytorch_code = textwrap.dedent(
"""
import torch.nn as nn
import torch
choices = nn.ParameterDict({
'a': nn.Parameter(torch.ones(2, 3)),
'b': nn.Parameter(torch.zeros(4)),
})
result = choices.pop('a')
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.

torch支持的话,这个也应该支持下,改paddle吧

"""
)
obj.run(pytorch_code, ["result"], check_stop_gradient=False)


def test_case_16():
pytorch_code = textwrap.dedent(
"""
import torch.nn as nn
import torch
pd1 = nn.ParameterDict({
'a': nn.Parameter(torch.ones(2, 3)),
'b': nn.Parameter(torch.zeros(4)),
})
pd2 = nn.ParameterDict(pd1)
result = list(pd2)
"""
)
obj.run(pytorch_code, ["result"])