|
| 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