Skip to content

Commit 1628c71

Browse files
authored
[API Compatibility] add torch.nn.utils.rnn.pad_sequence and torch.nn.utils.rnn.unpad_sequence -part (#826)
add paconvert tests for torch.nn.utils.rnn.pad_sequence and torch.nn.utils.rnn.unpad_sequence
1 parent 5e7d7aa commit 1628c71

3 files changed

Lines changed: 390 additions & 0 deletions

File tree

paconvert/api_mapping.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10284,6 +10284,12 @@
1028410284
"torch.nn.utils.vector_to_parameters": {
1028510285
"Matcher": "ChangePrefixMatcher"
1028610286
},
10287+
"torch.nn.utils.rnn.pad_sequence": {
10288+
"Matcher": "ChangePrefixMatcher"
10289+
},
10290+
"torch.nn.utils.rnn.unpad_sequence": {
10291+
"Matcher": "ChangePrefixMatcher"
10292+
},
1028710293
"torch.nn.utils.weight_norm": {
1028810294
"Matcher": "GenericMatcher",
1028910295
"paddle_api": "paddle.nn.utils.weight_norm",
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import textwrap
16+
17+
from apibase import APIBase
18+
19+
obj = APIBase("torch.nn.utils.rnn.pad_sequence")
20+
21+
22+
def test_case_1():
23+
"""basic usage with default parameters"""
24+
pytorch_code = textwrap.dedent(
25+
"""
26+
import torch
27+
from torch.nn.utils.rnn import pad_sequence
28+
a = torch.tensor([1.0, 2.0, 3.0])
29+
b = torch.tensor([4.0, 5.0])
30+
c = torch.tensor([6.0])
31+
result = pad_sequence([a, b, c])
32+
"""
33+
)
34+
obj.run(pytorch_code, ["result"])
35+
36+
37+
def test_case_2():
38+
"""batch_first=True"""
39+
pytorch_code = textwrap.dedent(
40+
"""
41+
import torch
42+
from torch.nn.utils.rnn import pad_sequence
43+
a = torch.tensor([1.0, 2.0, 3.0])
44+
b = torch.tensor([4.0, 5.0])
45+
c = torch.tensor([6.0])
46+
result = pad_sequence([a, b, c], batch_first=True)
47+
"""
48+
)
49+
obj.run(pytorch_code, ["result"])
50+
51+
52+
def test_case_3():
53+
"""custom padding_value"""
54+
pytorch_code = textwrap.dedent(
55+
"""
56+
import torch
57+
from torch.nn.utils.rnn import pad_sequence
58+
a = torch.tensor([1.0, 2.0, 3.0])
59+
b = torch.tensor([4.0, 5.0])
60+
result = pad_sequence([a, b], padding_value=-1.0)
61+
"""
62+
)
63+
obj.run(pytorch_code, ["result"])
64+
65+
66+
def test_case_4():
67+
"""all positional arguments"""
68+
pytorch_code = textwrap.dedent(
69+
"""
70+
import torch
71+
from torch.nn.utils.rnn import pad_sequence
72+
a = torch.tensor([1.0, 2.0, 3.0])
73+
b = torch.tensor([4.0, 5.0])
74+
result = pad_sequence([a, b], True, -1.0)
75+
"""
76+
)
77+
obj.run(pytorch_code, ["result"])
78+
79+
80+
def test_case_5():
81+
"""all keyword arguments"""
82+
pytorch_code = textwrap.dedent(
83+
"""
84+
import torch
85+
from torch.nn.utils.rnn import pad_sequence
86+
a = torch.tensor([1.0, 2.0, 3.0])
87+
b = torch.tensor([4.0, 5.0])
88+
result = pad_sequence(sequences=[a, b], batch_first=True, padding_value=0.0)
89+
"""
90+
)
91+
obj.run(pytorch_code, ["result"])
92+
93+
94+
def test_case_6():
95+
"""keyword arguments in shuffled order"""
96+
pytorch_code = textwrap.dedent(
97+
"""
98+
import torch
99+
from torch.nn.utils.rnn import pad_sequence
100+
a = torch.tensor([1.0, 2.0, 3.0])
101+
b = torch.tensor([4.0, 5.0])
102+
result = pad_sequence(padding_value=2.0, batch_first=False, sequences=[a, b])
103+
"""
104+
)
105+
obj.run(pytorch_code, ["result"])
106+
107+
108+
def test_case_7():
109+
"""2D input tensors"""
110+
pytorch_code = textwrap.dedent(
111+
"""
112+
import torch
113+
from torch.nn.utils.rnn import pad_sequence
114+
a = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
115+
b = torch.tensor([[7.0, 8.0]])
116+
result = pad_sequence([a, b], batch_first=True, padding_value=0.0)
117+
"""
118+
)
119+
obj.run(pytorch_code, ["result"])
120+
121+
122+
def test_case_8():
123+
"""using torch.nn.utils.rnn.pad_sequence full path"""
124+
pytorch_code = textwrap.dedent(
125+
"""
126+
import torch
127+
a = torch.tensor([1.0, 2.0, 3.0])
128+
b = torch.tensor([4.0, 5.0])
129+
result = torch.nn.utils.rnn.pad_sequence([a, b], batch_first=True)
130+
"""
131+
)
132+
obj.run(pytorch_code, ["result"])
133+
134+
135+
def test_case_9():
136+
"""sequences of same length"""
137+
pytorch_code = textwrap.dedent(
138+
"""
139+
import torch
140+
from torch.nn.utils.rnn import pad_sequence
141+
a = torch.tensor([1.0, 2.0, 3.0])
142+
b = torch.tensor([4.0, 5.0, 6.0])
143+
result = pad_sequence([a, b], batch_first=True)
144+
"""
145+
)
146+
obj.run(pytorch_code, ["result"])
147+
148+
149+
def test_case_10():
150+
"""integer dtype input"""
151+
pytorch_code = textwrap.dedent(
152+
"""
153+
import torch
154+
from torch.nn.utils.rnn import pad_sequence
155+
a = torch.tensor([1, 2, 3, 4])
156+
b = torch.tensor([5, 6])
157+
result = pad_sequence([a, b], batch_first=False, padding_value=0)
158+
"""
159+
)
160+
obj.run(pytorch_code, ["result"])
161+
162+
163+
def test_case_11():
164+
"""batch_first=False with custom padding_value"""
165+
pytorch_code = textwrap.dedent(
166+
"""
167+
import torch
168+
from torch.nn.utils.rnn import pad_sequence
169+
a = torch.tensor([1.0, 2.0, 3.0, 4.0])
170+
b = torch.tensor([5.0, 6.0])
171+
c = torch.tensor([7.0, 8.0, 9.0])
172+
result = pad_sequence([a, b, c], batch_first=False, padding_value=-100.0)
173+
"""
174+
)
175+
obj.run(pytorch_code, ["result"])
176+
177+
178+
def test_case_12():
179+
"""single sequence input"""
180+
pytorch_code = textwrap.dedent(
181+
"""
182+
import torch
183+
from torch.nn.utils.rnn import pad_sequence
184+
a = torch.tensor([1.0, 2.0, 3.0])
185+
result = pad_sequence([a])
186+
"""
187+
)
188+
obj.run(pytorch_code, ["result"])
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import textwrap
16+
17+
from apibase import APIBase
18+
19+
obj = APIBase("torch.nn.utils.rnn.unpad_sequence")
20+
21+
22+
def test_case_1():
23+
"""basic usage with batch_first=True"""
24+
pytorch_code = textwrap.dedent(
25+
"""
26+
import torch
27+
from torch.nn.utils.rnn import pad_sequence, unpad_sequence
28+
a = torch.tensor([1.0, 2.0, 3.0])
29+
b = torch.tensor([4.0, 5.0])
30+
c = torch.tensor([6.0])
31+
padded = pad_sequence([a, b, c], batch_first=True)
32+
lengths = torch.tensor([3, 2, 1])
33+
result = unpad_sequence(padded, lengths, batch_first=True)
34+
"""
35+
)
36+
obj.run(pytorch_code, ["result"])
37+
38+
39+
def test_case_2():
40+
"""batch_first=False (default)"""
41+
pytorch_code = textwrap.dedent(
42+
"""
43+
import torch
44+
from torch.nn.utils.rnn import pad_sequence, unpad_sequence
45+
a = torch.tensor([1.0, 2.0, 3.0])
46+
b = torch.tensor([4.0, 5.0])
47+
padded = pad_sequence([a, b], batch_first=False)
48+
lengths = torch.tensor([3, 2])
49+
result = unpad_sequence(padded, lengths, batch_first=False)
50+
"""
51+
)
52+
obj.run(pytorch_code, ["result"])
53+
54+
55+
def test_case_3():
56+
"""all positional arguments"""
57+
pytorch_code = textwrap.dedent(
58+
"""
59+
import torch
60+
from torch.nn.utils.rnn import pad_sequence, unpad_sequence
61+
a = torch.tensor([1.0, 2.0, 3.0])
62+
b = torch.tensor([4.0, 5.0])
63+
padded = pad_sequence([a, b], True)
64+
lengths = torch.tensor([3, 2])
65+
result = unpad_sequence(padded, lengths, True)
66+
"""
67+
)
68+
obj.run(pytorch_code, ["result"])
69+
70+
71+
def test_case_4():
72+
"""all keyword arguments"""
73+
pytorch_code = textwrap.dedent(
74+
"""
75+
import torch
76+
from torch.nn.utils.rnn import pad_sequence, unpad_sequence
77+
a = torch.tensor([1.0, 2.0, 3.0])
78+
b = torch.tensor([4.0, 5.0])
79+
padded = pad_sequence([a, b], batch_first=True)
80+
lengths = torch.tensor([3, 2])
81+
result = unpad_sequence(padded_sequences=padded, lengths=lengths, batch_first=True)
82+
"""
83+
)
84+
obj.run(pytorch_code, ["result"])
85+
86+
87+
def test_case_5():
88+
"""keyword arguments in shuffled order"""
89+
pytorch_code = textwrap.dedent(
90+
"""
91+
import torch
92+
from torch.nn.utils.rnn import pad_sequence, unpad_sequence
93+
a = torch.tensor([1.0, 2.0, 3.0])
94+
b = torch.tensor([4.0, 5.0])
95+
padded = pad_sequence([a, b], batch_first=True)
96+
lengths = torch.tensor([3, 2])
97+
result = unpad_sequence(batch_first=True, lengths=lengths, padded_sequences=padded)
98+
"""
99+
)
100+
obj.run(pytorch_code, ["result"])
101+
102+
103+
def test_case_6():
104+
"""default batch_first (omitted)"""
105+
pytorch_code = textwrap.dedent(
106+
"""
107+
import torch
108+
from torch.nn.utils.rnn import pad_sequence, unpad_sequence
109+
a = torch.tensor([1.0, 2.0, 3.0])
110+
b = torch.tensor([4.0, 5.0])
111+
padded = pad_sequence([a, b])
112+
lengths = torch.tensor([3, 2])
113+
result = unpad_sequence(padded, lengths)
114+
"""
115+
)
116+
obj.run(pytorch_code, ["result"])
117+
118+
119+
def test_case_7():
120+
"""2D input tensors"""
121+
pytorch_code = textwrap.dedent(
122+
"""
123+
import torch
124+
from torch.nn.utils.rnn import pad_sequence, unpad_sequence
125+
a = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
126+
b = torch.tensor([[7.0, 8.0]])
127+
padded = pad_sequence([a, b], batch_first=True)
128+
lengths = torch.tensor([3, 1])
129+
result = unpad_sequence(padded, lengths, batch_first=True)
130+
"""
131+
)
132+
obj.run(pytorch_code, ["result"])
133+
134+
135+
def test_case_8():
136+
"""using full path torch.nn.utils.rnn.unpad_sequence"""
137+
pytorch_code = textwrap.dedent(
138+
"""
139+
import torch
140+
from torch.nn.utils.rnn import pad_sequence
141+
a = torch.tensor([1.0, 2.0, 3.0])
142+
b = torch.tensor([4.0, 5.0])
143+
padded = pad_sequence([a, b], batch_first=True)
144+
lengths = torch.tensor([3, 2])
145+
result = torch.nn.utils.rnn.unpad_sequence(padded, lengths, batch_first=True)
146+
"""
147+
)
148+
obj.run(pytorch_code, ["result"])
149+
150+
151+
def test_case_9():
152+
"""single sequence"""
153+
pytorch_code = textwrap.dedent(
154+
"""
155+
import torch
156+
from torch.nn.utils.rnn import pad_sequence, unpad_sequence
157+
a = torch.tensor([1.0, 2.0, 3.0])
158+
padded = pad_sequence([a], batch_first=True)
159+
lengths = torch.tensor([3])
160+
result = unpad_sequence(padded, lengths, batch_first=True)
161+
"""
162+
)
163+
obj.run(pytorch_code, ["result"])
164+
165+
166+
def test_case_10():
167+
"""integer dtype"""
168+
pytorch_code = textwrap.dedent(
169+
"""
170+
import torch
171+
from torch.nn.utils.rnn import pad_sequence, unpad_sequence
172+
a = torch.tensor([1, 2, 3, 4])
173+
b = torch.tensor([5, 6])
174+
padded = pad_sequence([a, b], batch_first=True)
175+
lengths = torch.tensor([4, 2])
176+
result = unpad_sequence(padded, lengths, batch_first=True)
177+
"""
178+
)
179+
obj.run(pytorch_code, ["result"])
180+
181+
182+
def test_case_11():
183+
"""multiple sequences with varying lengths"""
184+
pytorch_code = textwrap.dedent(
185+
"""
186+
import torch
187+
from torch.nn.utils.rnn import pad_sequence, unpad_sequence
188+
a = torch.tensor([1.0, 2.0, 3.0, 4.0])
189+
b = torch.tensor([5.0, 6.0])
190+
c = torch.tensor([7.0, 8.0, 9.0])
191+
padded = pad_sequence([a, b, c], batch_first=True)
192+
lengths = torch.tensor([4, 2, 3])
193+
result = unpad_sequence(padded, lengths, batch_first=True)
194+
"""
195+
)
196+
obj.run(pytorch_code, ["result"])

0 commit comments

Comments
 (0)