test_seq_concat_op.py 4.2 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

Y
Yancey1989 已提交
17 18
import unittest
import numpy as np
Y
Yu Yang 已提交
19
import sys
20
from op_test import OpTest
Y
Yancey1989 已提交
21 22


23 24 25 26 27 28 29 30
def to_abs_offset_lod(lod):
    offset_lod = [[0] for i in lod]
    for i, level in enumerate(lod):
        for seq_len in level:
            offset_lod[i].append(offset_lod[i][-1] + seq_len)

    if len(offset_lod) == 0 or len(offset_lod) == 1:
        return offset_lod
31
    import copy
32 33 34 35
    new_offset_lod = copy.deepcopy(offset_lod)
    for idx, val in enumerate(offset_lod[0]):
        new_offset_lod[0][idx] = offset_lod[1][val]
    return new_offset_lod
36 37 38 39 40 41 42 43 44


def seq_concat(inputs, level):
    lod0 = inputs['X'][0][1][1]
    lod1 = inputs['X'][1][1][1]
    x0 = inputs['X'][0][1][0]
    x1 = inputs['X'][1][1][0]
    level_idx = len(lod0) - level - 1
    outs = []
45 46 47 48 49
    for i in range(len(lod0[level_idx])):
        sub_x0 = x0[to_abs_offset_lod(lod0)[level_idx][i]:to_abs_offset_lod(
            lod0)[level_idx][i + 1], :]
        sub_x1 = x1[to_abs_offset_lod(lod1)[level_idx][i]:to_abs_offset_lod(
            lod1)[level_idx][i + 1], :]
50 51 52 53 54
        outs.append(np.concatenate((sub_x0, sub_x1), axis=0))
    return np.concatenate(outs, axis=0)


class TestSeqConcatOp(OpTest):
Y
Yancey1989 已提交
55 56
    def set_data(self):
        # two level, batch size is 3
Y
Yancey1989 已提交
57
        x0 = np.random.random((4, 6, 3)).astype('float32')
58
        lod0 = [[2, 2], [1, 1, 1, 1]]
Y
Yancey1989 已提交
59
        x1 = np.random.random((4, 8, 3)).astype('float32')
60
        lod1 = [[2, 2], [1, 1, 1, 1]]
Y
Yancey1989 已提交
61 62 63 64
        axis = 1
        level = 1
        self.inputs = {'X': [('x0', (x0, lod0)), ('x1', (x1, lod1))]}
        self.attrs = {'axis': axis, 'level': level}
65
        self.outputs = {'Out': (np.concatenate([x0, x1], axis=1), lod0)}
Y
Yancey1989 已提交
66 67 68 69 70 71 72 73 74 75 76 77

    def setUp(self):
        self.op_type = "sequence_concat"
        self.set_data()

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['x0'], 'Out')


78
class TestSeqConcatOpLevelZeroNestedSequence(TestSeqConcatOp):
Y
Yancey1989 已提交
79 80
    def set_data(self):
        # two level, batch size is 3
Y
Yancey1989 已提交
81
        x0 = np.random.random((4, 6, 3)).astype('float32')
82
        lod0 = [[2, 2], [1, 1, 1, 1]]
83
        x1 = np.random.random((7, 6, 3)).astype('float32')
84
        lod1 = [[2, 2], [1, 2, 2, 2]]
Y
Yancey1989 已提交
85
        axis = 0
86
        level = 0
Y
Yancey1989 已提交
87 88
        self.inputs = {'X': [('x0', (x0, lod0)), ('x1', (x1, lod1))]}
        self.attrs = {'axis': axis, 'level': level}
89
        out_lod = [[2, 2], [2, 3, 3, 3]]
90
        self.outputs = {'Out': (seq_concat(self.inputs, level), out_lod)}
Y
Yancey1989 已提交
91

92 93 94 95 96

class TestSeqConcatOplevelOneNestedSequence(TestSeqConcatOp):
    def set_data(self):
        # two level, batch size is 3
        x0 = np.random.random((4, 6, 3)).astype('float32')
97
        lod0 = [[2, 2], [1, 1, 1, 1]]
98
        x1 = np.random.random((7, 6, 3)).astype('float32')
99
        lod1 = [[3, 1], [1, 2, 2, 2]]
100 101 102 103
        axis = 0
        level = 1
        self.inputs = {'X': [('x0', (x0, lod0)), ('x1', (x1, lod1))]}
        self.attrs = {'axis': axis, 'level': level}
104
        out_lod = [[5, 3], [1, 1, 1, 2, 2, 1, 1, 2]]
105
        self.outputs = {'Out': (seq_concat(self.inputs, level), out_lod)}
Y
Yancey1989 已提交
106 107


108
class TestSeqConcatOpLevelZeroSequence(TestSeqConcatOp):
Y
Yancey1989 已提交
109 110 111
    def set_data(self):
        # two level, batch size is 3
        x0 = np.random.random((4, 3, 4)).astype('float32')
112
        lod0 = [[1, 1, 1, 1]]
113
        x1 = np.random.random((7, 3, 4)).astype('float32')
114
        lod1 = [[1, 2, 2, 2]]
Y
Yancey1989 已提交
115 116 117 118
        axis = 0
        level = 0
        self.inputs = {'X': [('x0', (x0, lod0)), ('x1', (x1, lod1))]}
        self.attrs = {'axis': axis, 'level': level}
119
        out_lod = [[2, 3, 3, 3]]
120
        self.outputs = {'Out': (seq_concat(self.inputs, level), out_lod)}
Y
Yancey1989 已提交
121 122 123 124


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