test_sequence_pool.py 14.5 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15 16
from __future__ import print_function

17 18
import unittest
import numpy as np
19 20
import sys
sys.path.append("../")
21
from op_test import OpTest, skip_check_grad_ci
T
tensor-tang 已提交
22
from test_reorder_lod_tensor import convert_to_offset
23 24


25
def compute_seqpool_sum(x, offset, out, pad_value=0.0):
26 27 28
    level = len(offset) - 1
    for i in range(len(offset[level]) - 1):
        if offset[level][i] == offset[level][i + 1]:
29 30
            out[i] = pad_value
        else:
31
            sub_x = x[offset[level][i]:offset[level][i + 1], :]
32
            out[i] = sub_x.sum(axis=0)
T
tensor-tang 已提交
33 34


35
def compute_seqpool_avg(x, offset, out, pad_value=0.0):
36 37 38
    level = len(offset) - 1
    for i in range(len(offset[level]) - 1):
        if offset[level][i] == offset[level][i + 1]:
39 40
            out[i] = pad_value
        else:
41
            sub_x = x[offset[level][i]:offset[level][i + 1], :]
42
            out[i] = sub_x.mean(axis=0)
T
tensor-tang 已提交
43

44

45
def compute_seqpool_sqrt(x, offset, out, pad_value=0.0):
46 47 48
    level = len(offset) - 1
    for i in range(len(offset[level]) - 1):
        if offset[level][i] == offset[level][i + 1]:
49 50
            out[i] = pad_value
        else:
51 52
            sub_x = x[offset[level][i]:offset[level][i + 1], :]
            seq_len = offset[level][i + 1] - offset[level][i]
53
            out[i] = sub_x.sum(axis=0) / np.sqrt(seq_len)
T
tensor-tang 已提交
54 55 56


class TestSeqAvgPool(OpTest):
57 58 59
    def set_lod(self):
        return [[11]]

60
    def set_lod_data(self):
61
        x = np.random.uniform(0.1, 1, [11, 23]).astype('float32')
62 63 64 65
        return x

    def set_data(self):
        x = self.set_lod_data()
66
        lod = self.set_lod()
67
        level = len(lod) - 1
68
        self.inputs = {'X': (x, lod)}
T
tensor-tang 已提交
69
        offset = convert_to_offset(lod)
70
        out = np.zeros((len(lod[level]), x.shape[1])).astype('float32')
71
        self.outputs = {'Out': out}
72
        return x, lod, offset, out
73

74
    def compute(self, x, offset, out):
75 76
        self.attrs = {"pad_value": 0.0, 'pooltype': "AVERAGE"}
        compute_seqpool_avg(x, offset, out, self.attrs["pad_value"])
77

78
    def setUp(self):
79 80
        self.op_type = 'sequence_pool'
        x, lod, offset, out = self.set_data()
81
        self.compute(x, offset, out)
82
        if len(offset) > 1:
83
            self.outputs = {'Out': (out, [lod[0]])}
84 85

    def test_check_output(self):
H
hong 已提交
86
        self.check_output(check_dygraph=False)
87 88

    def test_check_grad(self):
89
        # Remove MaxIndex after check_grad is refined.
90 91
        out = self.outputs['Out']
        if isinstance(out, tuple): out = out[0]
92
        self.outputs['MaxIndex'] = \
93
            np.zeros(out.shape).astype('int32')
H
hong 已提交
94
        self.check_grad(["X"], "Out", check_dygraph=False)
95 96


97 98 99 100 101 102 103 104 105 106 107
class TestSeqAvgPoolBatch1(TestSeqAvgPool):
    def set_lod(self):
        return [[11]]

    def set_lod_data(self):
        lod = self.set_lod()
        x, _ = self.get_sequence_batch_size_1_input(
            lod=lod, shape=[lod[0][0], 23])
        return x


108 109 110 111 112
class TestSeqAvgPoolLen0(TestSeqAvgPool):
    def set_lod(self):
        return [[0, 4, 0, 7, 0]]


113 114 115 116 117
class TestSeqAvgPoolLen0LoDLevel2(TestSeqAvgPool):
    def set_lod(self):
        return [[2, 0, 1, 2], [0, 4, 0, 7, 0]]


D
dzhwinter 已提交
118
class TestSeqSumPool(TestSeqAvgPool):
119
    def compute(self, x, offset, out):
120 121 122 123 124 125 126
        self.attrs = {"pad_value": 0.1, 'pooltype': "SUM"}
        compute_seqpool_sum(x, offset, out, self.attrs["pad_value"])


class TestSeqSumPoolLen0(TestSeqSumPool):
    def set_lod(self):
        return [[0, 4, 0, 7, 0]]
D
dzhwinter 已提交
127 128


129 130 131 132 133
class TestSeqSumPoolLen0LoDLevel2(TestSeqSumPool):
    def set_lod(self):
        return [[2, 0, 1, 2], [0, 4, 0, 7, 0]]


D
dzhwinter 已提交
134
class TestSeqMaxPool(TestSeqAvgPool):
135 136 137
    def set_lod(self):
        return [[13]]

D
dzhwinter 已提交
138 139 140
    def set_data(self):
        self.op_type = 'sequence_pool'
        x = np.random.uniform(0.1, 1, [13, 23]).astype('float32')
141
        lod = self.set_lod()
142
        level = len(lod) - 1
T
tensor-tang 已提交
143
        offset = convert_to_offset(lod)
144 145
        for i in range(len(offset[level]) - 1):
            l = offset[level][i + 1] - offset[level][i]
146
            if l > 0:
147
                x[offset[level][i] + np.random.randint(l), :] += 2.0
D
dzhwinter 已提交
148 149 150

        self.inputs = {'X': (x, lod)}

151
        out = np.zeros((len(lod[level]), 23)).astype('float32')
D
dzhwinter 已提交
152
        self.outputs = {'Out': out}
153
        return x, lod, offset, out
D
dzhwinter 已提交
154

155
    def compute(self, x, offset, out):
156
        self.attrs = {"pad_value": 0.5, 'pooltype': "MAX"}
157 158 159
        level = len(offset) - 1
        for i in range(len(offset[level]) - 1):
            if offset[level][i] == offset[level][i + 1]:
160 161
                out[i] = self.attrs["pad_value"]
            else:
162
                sub_x = x[offset[level][i]:offset[level][i + 1], :]
163 164 165 166 167 168
                out[i] = np.amax(sub_x, axis=0)


class TestSeqMaxPoolLen0(TestSeqMaxPool):
    def set_lod(self):
        return [[0, 1, 1, 5, 6, 0]]
D
dzhwinter 已提交
169 170


171 172 173 174 175
class TestSeqMaxPoolLen0LoDLevel2(TestSeqMaxPool):
    def set_lod(self):
        return [[2, 0, 3, 1], [0, 1, 1, 5, 6, 0]]


D
dzhwinter 已提交
176
class TestSeqSqrtPool(TestSeqAvgPool):
177
    def compute(self, x, offset, out):
178 179 180 181 182 183 184
        self.attrs = {"pad_value": 0.0, 'pooltype': "SQRT"}
        compute_seqpool_sqrt(x, offset, out, self.attrs["pad_value"])


class TestSeqSqrtPoolLen0(TestSeqSqrtPool):
    def set_lod(self):
        return [[0, 7, 0, 2, 2, 0]]
D
dzhwinter 已提交
185 186


187 188 189 190 191
class TestSeqSqrtPoolLen0LoDLevel2(TestSeqSqrtPool):
    def set_lod(self):
        return [[1, 2, 0, 3], [0, 7, 0, 2, 2, 0]]


D
dzhwinter 已提交
192
class TestSeqLastPool(TestSeqAvgPool):
193
    def compute(self, x, offset, out):
194
        self.attrs = {"pad_value": 0.0, 'pooltype': "LAST"}
195 196 197
        level = len(offset) - 1
        for i in range(len(offset[level]) - 1):
            if offset[level][i] == offset[level][i + 1]:
198 199
                out[i] = self.attrs["pad_value"]
            else:
200
                sub_x = x[offset[level][i]:offset[level][i + 1], :]
201 202 203 204 205 206
                out[i] = sub_x[-1, :]


class TestSeqLastPoolLen0(TestSeqLastPool):
    def set_lod(self):
        return [[0, 3, 4, 0, 4, 0]]
D
dzhwinter 已提交
207 208


209 210 211 212 213
class TestSeqLastPoolLen0LoDLevel2(TestSeqLastPool):
    def set_lod(self):
        return [[1, 0, 2, 3], [0, 3, 4, 0, 4, 0]]


D
dzhwinter 已提交
214
class TestSeqFirstPool(TestSeqAvgPool):
215
    def compute(self, x, offset, out):
216
        self.attrs = {"pad_value": 0.3, 'pooltype': "FIRST"}
217 218 219
        level = len(offset) - 1
        for i in range(len(offset[level]) - 1):
            if offset[level][i] == offset[level][i + 1]:
220 221
                out[i] = self.attrs["pad_value"]
            else:
222
                sub_x = x[offset[level][i]:offset[level][i + 1], :]
223 224 225 226 227 228
                out[i] = sub_x[0, :]


class TestSeqFirstPoolLen0(TestSeqFirstPool):
    def set_lod(self):
        return [[0, 2, 0, 3, 6, 0]]
D
dzhwinter 已提交
229 230


231 232 233 234 235
class TestSeqFirstPoolLen0LoDLevel2(TestSeqFirstPool):
    def set_lod(self):
        return [[1, 0, 2, 3], [0, 2, 0, 3, 6, 0]]


236
class TestSeqAvgPool2D(TestSeqAvgPool):
237 238 239
    def set_lod(self):
        return [[4, 1, 3, 5]]

240 241
    def set_data(self):
        self.op_type = 'sequence_pool'
242
        x = np.random.uniform(0.1, 1, [13, 3, 17]).astype('float32')
243
        lod = self.set_lod()
244
        level = len(lod) - 1
245
        self.inputs = {'X': (x, lod)}
T
tensor-tang 已提交
246
        offset = convert_to_offset(lod)
247

248
        out = np.zeros((len(lod[level]), 3, 17)).astype('float32')
249
        self.outputs = {'Out': out}
250
        return x, lod, offset, out
251

252
    def compute(self, x, offset, out):
253
        self.attrs = {"pad_value": 0.0, 'pooltype': "AVERAGE"}
254 255 256
        level = len(offset) - 1
        for i in range(len(offset[level]) - 1):
            if offset[level][i] == offset[level][i + 1]:
257 258
                out[i] = self.attrs["pad_value"] * np.ones((3, 17))
            else:
259
                sub_x = np.reshape(x[offset[level][i]:offset[level][i + 1], :],
260 261 262 263 264 265 266
                                   (-1, 3 * 17))
                out[i] = np.reshape(sub_x.mean(axis=0), (3, 17))


class TestSeqAvgPool2DLen0(TestSeqAvgPool2D):
    def set_lod(self):
        return [[0, 5, 0, 8, 0]]
267 268


269 270 271 272 273
class TestSeqAvgPool2DLen0LoDLevel2(TestSeqAvgPool2D):
    def set_lod(self):
        return [[1, 0, 4], [0, 5, 0, 8, 0]]


274
class TestSeqSumPool2D(TestSeqAvgPool2D):
275
    def compute(self, x, offset, out):
276
        self.attrs = {"pad_value": 0.2, 'pooltype': "SUM"}
277 278 279
        level = len(offset) - 1
        for i in range(len(offset[level]) - 1):
            if offset[level][i] == offset[level][i + 1]:
280 281
                out[i] = self.attrs["pad_value"] * np.ones((3, 17))
            else:
282
                sub_x = np.reshape(x[offset[level][i]:offset[level][i + 1], :],
283 284 285 286 287 288 289
                                   (-1, 3 * 17))
                out[i] = np.reshape(sub_x.sum(axis=0), (3, 17))


class TestSeqSumPool2DLen0(TestSeqSumPool2D):
    def set_lod(self):
        return [[0, 8, 0, 5, 0]]
290 291


292 293 294 295 296
class TestSeqSumPool2DLen0LoDLevel2(TestSeqSumPool2D):
    def set_lod(self):
        return [[1, 0, 4], [0, 8, 0, 5, 0]]


L
Luo Tao 已提交
297
class TestSeqSqrtPool2D(TestSeqAvgPool2D):
298
    def compute(self, x, offset, out):
299
        self.attrs = {"pad_value": 0.0, 'pooltype': "SQRT"}
300 301 302
        level = len(offset) - 1
        for i in range(len(offset[level]) - 1):
            if offset[level][i] == offset[level][i + 1]:
303 304
                out[i] = self.attrs["pad_value"] * np.ones((3, 17))
            else:
305
                sub_x = np.reshape(x[offset[level][i]:offset[level][i + 1], :],
306
                                   (-1, 3 * 17))
307
                seq_len = offset[level][i + 1] - offset[level][i]
308 309
                out[i] = np.reshape(
                    sub_x.sum(axis=0) / np.sqrt(seq_len), (3, 17))
L
Luo Tao 已提交
310 311

    def test_check_grad(self):
312
        # Remove MaxIndex after check_grad is refined.
313 314 315
        out = self.outputs['Out']
        if isinstance(out, tuple):
            out = out[0]
316
        self.outputs['MaxIndex'] = \
317
            np.zeros(out.shape).astype('int32')
H
hong 已提交
318 319
        self.check_grad(
            ["X"], "Out", max_relative_error=0.06, check_dygraph=False)
L
Luo Tao 已提交
320 321


322 323 324 325 326
class TestSeqSqrtPool2DLen0(TestSeqSqrtPool2D):
    def set_lod(self):
        return [[0, 8, 0, 5, 0]]


327 328 329 330 331
class TestSeqSqrtPool2DLen0LoDLevel2(TestSeqSqrtPool2D):
    def set_lod(self):
        return [[1, 0, 2, 2], [0, 8, 0, 5, 0]]


L
Luo Tao 已提交
332
class TestSeqMaxPool2D(TestSeqAvgPool2D):
333 334 335
    def set_lod(self):
        return [[4, 1, 3, 5]]

336 337 338
    def set_data(self):
        self.op_type = 'sequence_pool'
        x = np.random.uniform(0.1, 1, [13, 3, 11]).astype('float32')
339 340 341 342
        lod = self.set_lod()
        level = len(lod) - 1
        self.inputs = {'X': (x, lod)}
        offset = convert_to_offset(lod)
343 344
        for i in range(len(offset[level]) - 1):
            l = offset[level][i + 1] - offset[level][i]
345 346
            if l == 0:
                continue
347
            x[offset[level][i] + np.random.randint(l), :] += 1.0
348

349
        out = np.zeros((len(lod[level]), 3, 11)).astype('float32')
350
        self.outputs = {'Out': out}
351
        return x, lod, offset, out
352

353
    def compute(self, x, offset, out):
354
        self.attrs = {"pad_value": 0.0, 'pooltype': "MAX"}
355 356 357
        level = len(offset) - 1
        for i in range(len(offset[level]) - 1):
            if offset[level][i] == offset[level][i + 1]:
358 359
                out[i] = self.attrs["pad_value"] * np.ones((3, 11))
                continue
360
            sub_x = np.reshape(x[offset[level][i]:offset[level][i + 1], :],
361
                               (-1, 3 * 11))
362
            out[i] = np.reshape(np.amax(sub_x, axis=0), (3, 11))
L
Luo Tao 已提交
363 364


365 366 367 368 369
class TestSeqMaxPool2DLen0(TestSeqMaxPool2D):
    def set_lod(self):
        return [[0, 3, 0, 10, 0]]


370 371 372 373 374
class TestSeqMaxPool2DLen0LoDLevel2(TestSeqMaxPool2D):
    def set_lod(self):
        return [[1, 0, 2, 2], [0, 3, 0, 10, 0]]


375 376
@skip_check_grad_ci(reason="Grad computation does not apply to Sequence MAX "
                    "Pool executed when is_test is true.")
J
Jacek Czaja 已提交
377 378
class TestSeqMaxPool2DInference(TestSeqMaxPool2D):
    def compute(self, x, offset, out):
379
        self.attrs = {"pad_value": 1.0, 'pooltype': "MAX", 'is_test': True}
380 381 382
        level = len(offset) - 1
        for i in range(len(offset[level]) - 1):
            if offset[level][i] == offset[level][i + 1]:
383 384
                out[i] = self.attrs["pad_value"] * np.ones((3, 11))
            else:
385
                sub_x = np.reshape(x[offset[level][i]:offset[level][i + 1], :],
386 387
                                   (-1, 3 * 11))
                out[i] = np.reshape(np.amax(sub_x, axis=0), (3, 11))
J
Jacek Czaja 已提交
388 389

    def test_check_grad(self):
390
        """Grad computation does not apply to Sequence MAX
J
Jacek Czaja 已提交
391 392 393 394
            Pool executed when is_test is true """
        return


395 396 397 398 399
class TestSeqMaxPool2DInferenceLen0(TestSeqMaxPool2DInference):
    def set_lod(self):
        return [[0, 3, 0, 10, 0]]


400 401 402 403 404
class TestSeqMaxPool2DInferenceLen0LoDLevel2(TestSeqMaxPool2DInference):
    def set_lod(self):
        return [[1, 0, 2, 2], [0, 3, 0, 10, 0]]


L
Luo Tao 已提交
405
class TestSeqLastPool2D(TestSeqAvgPool2D):
406
    def compute(self, x, offset, out):
407
        self.attrs = {"pad_value": 0.0, 'pooltype': "LAST"}
408 409 410
        level = len(offset) - 1
        for i in range(len(offset[level]) - 1):
            if offset[level][i] == offset[level][i + 1]:
411 412
                out[i] = self.attrs["pad_value"] * np.ones((3, 17))
            else:
413
                sub_x = np.reshape(x[offset[level][i]:offset[level][i + 1], :],
414 415 416 417 418 419 420
                                   (-1, 3 * 17))
                out[i] = np.reshape(sub_x[-1, :], (3, 17))


class TestSeqLastPool2DLen0(TestSeqLastPool2D):
    def set_lod(self):
        return [[0, 3, 0, 1, 9, 0]]
L
Luo Tao 已提交
421 422


423 424 425 426 427
class TestSeqLastPool2DLen0LoDLevel2(TestSeqLastPool2D):
    def set_lod(self):
        return [[1, 0, 2, 3], [0, 3, 0, 1, 9, 0]]


L
Luo Tao 已提交
428
class TestSeqFirstPool2D(TestSeqAvgPool2D):
429
    def compute(self, x, offset, out):
430
        self.attrs = {"pad_value": 0.0, 'pooltype': "FIRST"}
431 432 433
        level = len(offset) - 1
        for i in range(len(offset[level]) - 1):
            if offset[level][i] == offset[level][i + 1]:
434 435
                out[i] = self.attrs["pad_value"] * np.ones((3, 17))
            else:
436
                sub_x = np.reshape(x[offset[level][i]:offset[level][i + 1], :],
437 438 439 440 441 442 443
                                   (-1, 3 * 17))
                out[i] = np.reshape(sub_x[0, :], (3, 17))


class TestSeqFirstPool2DLen0(TestSeqFirstPool2D):
    def set_lod(self):
        return [[0, 3, 0, 3, 7, 0]]
L
Luo Tao 已提交
444 445


446 447 448 449 450
class TestSeqFirstPool2DLen0LoDLevel2(TestSeqFirstPool2D):
    def set_lod(self):
        return [[1, 0, 2, 3], [0, 3, 0, 3, 7, 0]]


451 452
if __name__ == '__main__':
    unittest.main()