test_row_conv_op.py 6.1 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.

S
Siddharth Goyal 已提交
15
import unittest
16

S
Siddharth Goyal 已提交
17
import numpy as np
18
from op_test import OpTest
19

20
import paddle
21
from paddle import fluid
S
Siddharth Goyal 已提交
22

23 24
paddle.enable_static()

S
Siddharth Goyal 已提交
25 26 27

def row_conv_forward(x, lod, wt):
    out = np.zeros_like(x)
28 29 30 31
    num_sequences = len(lod[0])
    seq_info = [0]
    for seq_len in lod[0]:
        seq_info.append(seq_info[-1] + seq_len)
S
Siddharth Goyal 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    context_length = wt.shape[0]

    for i in range(num_sequences):  # loop over number of sequences
        start = seq_info[i]
        end = seq_info[i + 1]
        curinput = x[start:end, :]
        curoutput = out[start:end, :]

        cur_timesteps = end - start
        for j in range(cur_timesteps):  # loop over different timesteps
            for k in range(context_length):
                if j + k >= cur_timesteps:
                    continue
                curoutput[j, :] += curinput[j + k, :] * wt[k, :]

    return out


class TestRowConvOp1(OpTest):
    def setUp(self):

        self.op_type = "row_conv"
54 55
        lod = [[2, 3, 2]]
        T = sum(lod[0])
S
Siddharth Goyal 已提交
56
        D = 16
57
        context_length = 8
S
Siddharth Goyal 已提交
58 59 60 61 62 63 64 65 66

        x = np.random.random((T, D)).astype("float32")
        wt = np.random.random((context_length, D)).astype("float32")
        self.inputs = {'X': (x, lod), 'Filter': wt}

        out = row_conv_forward(x, lod, wt)
        self.outputs = {'Out': (out, lod)}

    def test_check_output(self):
H
hong 已提交
67
        self.check_output(check_dygraph=False)
S
Siddharth Goyal 已提交
68 69

    def test_check_grad_normal(self):
70
        self.check_grad(['X', 'Filter'], 'Out', check_dygraph=False)
S
Siddharth Goyal 已提交
71 72

    def test_check_grad_ignore_x(self):
73 74 75
        self.check_grad(
            ['Filter'], 'Out', no_grad_set=set('X'), check_dygraph=False
        )
S
Siddharth Goyal 已提交
76 77

    def test_check_grad_ignore_wt(self):
78 79 80
        self.check_grad(
            ['X'], 'Out', no_grad_set=set('Filter'), check_dygraph=False
        )
S
Siddharth Goyal 已提交
81 82 83 84 85 86


class TestRowConvOp2(OpTest):
    def setUp(self):

        self.op_type = "row_conv"
87 88
        lod = [[20, 30, 50]]
        T = sum(lod[0])
S
Siddharth Goyal 已提交
89 90 91 92 93 94 95 96 97 98 99
        D = 35
        context_length = 35

        x = np.random.random((T, D)).astype("float32")
        wt = np.random.random((context_length, D)).astype("float32")
        self.inputs = {'X': (x, lod), 'Filter': wt}

        out = row_conv_forward(x, lod, wt)
        self.outputs = {'Out': (out, lod)}

    def test_check_output(self):
H
hong 已提交
100
        self.check_output(check_dygraph=False)
S
Siddharth Goyal 已提交
101

102 103 104
    # max_relative_error is increased from 0.05 to 0.06 as for higher
    # dimensional input, the dX on CPU for some values has max_rel_error
    # slightly more than 0.05
S
Siddharth Goyal 已提交
105
    def test_check_grad_normal(self):
106 107 108
        self.check_grad(
            ['X', 'Filter'], 'Out', max_relative_error=0.06, check_dygraph=False
        )
S
Siddharth Goyal 已提交
109 110

    def test_check_grad_ignore_x(self):
111 112 113 114 115 116 117
        self.check_grad(
            ['Filter'],
            'Out',
            max_relative_error=0.06,
            no_grad_set=set('X'),
            check_dygraph=False,
        )
S
Siddharth Goyal 已提交
118 119

    def test_check_grad_ignore_wt(self):
120 121 122 123 124 125 126
        self.check_grad(
            ['X'],
            'Out',
            max_relative_error=0.06,
            no_grad_set=set('Filter'),
            check_dygraph=False,
        )
S
Siddharth Goyal 已提交
127 128


129 130 131 132 133 134
def row_conv_foward_Tensor(x, wt):
    out = np.zeros_like(x)
    num_sequence = x.shape[0]
    timesteps = x.shape[1]
    context_length = wt.shape[0]
    for i in range(num_sequence):
135 136
        cur_in = x[i : i + 1, :][0]
        cur_out = out[i : i + 1, :][0]
137 138 139 140 141 142 143 144 145 146 147
        for j in range(timesteps):
            for k in range(context_length):
                if j + k >= timesteps:
                    continue
                cur_out[j, :] += cur_in[j + k, :] * wt[k, :]
    return out


class TestRowOpWithTensorInput(OpTest):
    def setUp(self):
        self.op_type = "row_conv"
148
        length = [1, 2, 3]
149 150
        B = 2
        T = sum(length)
151 152
        D = 20
        context_length = 6
153 154 155 156 157 158 159 160 161

        x = np.random.random((B, T, D)).astype("float32")
        wt = np.random.random((context_length, D)).astype("float32")
        self.inputs = {'X': x, 'Filter': wt}

        out = row_conv_foward_Tensor(x, wt)
        self.outputs = {'Out': out}

    def test_check_output(self):
H
hong 已提交
162
        self.check_output(check_dygraph=False)
163 164

    def test_check_grad_ignore_x(self):
165 166 167
        self.check_grad(
            ['Filter'], 'Out', no_grad_set=set('X'), check_dygraph=False
        )
168 169

    def test_check_grad_normal(self):
170
        self.check_grad(['X', 'Filter'], 'Out', check_dygraph=False)
171 172

    def test_check_grad_ignore_wt(self):
173 174 175
        self.check_grad(
            ['X'], 'Out', no_grad_set=set('Filter'), check_dygraph=False
        )
176 177


178 179 180 181 182 183 184 185
class TestRowConvLayer(unittest.TestCase):
    def setUp(self):
        self.B = 2
        self.T = 6
        self.C = 20
        self.context_length = 6

        self.x = np.random.random((self.B, self.T, self.C)).astype("float32")
186 187 188
        self.w = np.random.random((self.context_length, self.C)).astype(
            "float32"
        )
189 190 191 192 193 194 195 196
        self.out = row_conv_foward_Tensor(self.x, self.w)

    def check_identity(self):
        start = fluid.Program()
        main = fluid.Program()
        with fluid.unique_name.guard():
            with fluid.program_guard(main, start):
                x = fluid.data("x", (-1, -1, self.C), "float32")
197
                out = paddle.static.nn.row_conv(
198 199
                    x,
                    self.context_length,
200 201
                    param_attr=fluid.initializer.NumpyArrayInitializer(self.w),
                )
202 203 204
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        exe.run(start)
205
        (out_np,) = exe.run(main, feed={'x': self.x}, fetch_list=[out])
206 207 208 209

        np.testing.assert_allclose(out_np, self.out)


S
Siddharth Goyal 已提交
210 211
if __name__ == '__main__':
    unittest.main()