test_lstm_cudnn_op.py 7.9 KB
Newer Older
P
phlrain 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

from __future__ import print_function

import unittest
import numpy as np

import paddle.fluid.core as core
from op_test import OpTest
P
phlrain 已提交
22
import paddle.fluid as fluid
G
GaoWei8 已提交
23
import paddle.fluid.layers as layers
P
phlrain 已提交
24 25 26 27

SIGMOID_THRESHOLD_MIN = -40.0
SIGMOID_THRESHOLD_MAX = 13.0
EXP_MAX_INPUT = 40.0
P
phlrain 已提交
28 29


G
GaoWei8 已提交
30
def lstm_naive(input, w):
P
phlrain 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    seq_len, batch_size, hidden_size = input.shape

    offset = 0
    wi = w[offset:offset + hidden_size * hidden_size].reshape(
        (hidden_size, hidden_size)).transpose()
    offset += hidden_size * hidden_size
    wf = w[offset:offset + hidden_size * hidden_size].reshape(
        (hidden_size, hidden_size)).transpose()
    offset += hidden_size * hidden_size
    wc = w[offset:offset + hidden_size * hidden_size].reshape(
        (hidden_size, hidden_size)).transpose()
    offset += hidden_size * hidden_size
    wo = w[offset:offset + hidden_size * hidden_size].reshape(
        (hidden_size, hidden_size)).transpose()
    offset += hidden_size * hidden_size
    ri = w[offset:offset + hidden_size * hidden_size].reshape(
        (hidden_size, hidden_size)).transpose()
    offset += hidden_size * hidden_size
    rf = w[offset:offset + hidden_size * hidden_size].reshape(
        (hidden_size, hidden_size)).transpose()
    offset += hidden_size * hidden_size
    rc = w[offset:offset + hidden_size * hidden_size].reshape(
        (hidden_size, hidden_size)).transpose()
    offset += hidden_size * hidden_size
    ro = w[offset:offset + hidden_size * hidden_size].reshape(
        (hidden_size, hidden_size)).transpose()
    offset += hidden_size * hidden_size

    bi_1 = w[offset:offset + hidden_size]
    offset += hidden_size
    bf_1 = w[offset:offset + hidden_size]
    offset += hidden_size
    bc_1 = w[offset:offset + hidden_size]
    offset += hidden_size
    bo_1 = w[offset:offset + hidden_size]
    offset += hidden_size

    bi_2 = w[offset:offset + hidden_size]
    offset += hidden_size
    bf_2 = w[offset:offset + hidden_size]
    offset += hidden_size
    bc_2 = w[offset:offset + hidden_size]
    offset += hidden_size
    bo_2 = w[offset:offset + hidden_size]

    def sigmoid(x):
P
phlrain 已提交
77 78 79 80
        y = np.copy(x)
        y[x < SIGMOID_THRESHOLD_MIN] = SIGMOID_THRESHOLD_MIN
        y[x > SIGMOID_THRESHOLD_MAX] = SIGMOID_THRESHOLD_MAX
        return 1. / (1. + np.exp(-y))
P
phlrain 已提交
81 82

    def tanh(x):
P
phlrain 已提交
83 84 85
        y = -2. * x
        y[y > EXP_MAX_INPUT] = EXP_MAX_INPUT
        return (2. / (1. + np.exp(y))) - 1.
P
phlrain 已提交
86 87

    output = []
G
GaoWei8 已提交
88 89
    pre_h = np.zeros((1, batch_size, hidden_size), dtype=input.dtype)
    pre_c = np.zeros((1, batch_size, hidden_size), dtype=input.dtype)
P
phlrain 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

    for i in range(seq_len):
        emb_1 = input[i]

        input_gate = sigmoid(
            np.matmul(emb_1, wi) + np.matmul(pre_h, ri) + bi_1 + bi_2)
        forget_gate = sigmoid(
            np.matmul(emb_1, wf) + np.matmul(pre_h, rf) + bf_1 + bf_2)
        output_gate = sigmoid(
            np.matmul(emb_1, wo) + np.matmul(pre_h, ro) + bo_1 + bo_2)
        c_t_temp = tanh(
            np.matmul(emb_1, wc) + np.matmul(pre_h, rc) + bc_1 + bc_2)
        new_c = input_gate * c_t_temp + forget_gate * pre_c
        new_h = output_gate * tanh(new_c)

        pre_h = new_h
        pre_c = new_c

        output.append(new_h)

    output = np.concatenate(output, -1)
    output = output.reshape((batch_size, -1, hidden_size))
    output = output.transpose((1, 0, 2))

P
phlrain 已提交
114
    return output, pre_h, pre_c
P
phlrain 已提交
115 116


117 118
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
P
phlrain 已提交
119
class TestCUDNNLstmOp(OpTest):
G
GaoWei8 已提交
120
    # TODO(GaoWei8):when input dtype is fp64, precision threshold should be removed.
P
phlrain 已提交
121 122
    def setUp(self):
        self.op_type = "cudnn_lstm"
G
GaoWei8 已提交
123
        self.dtype = np.float64
P
phlrain 已提交
124

G
GaoWei8 已提交
125
        seq_length = 20
P
phlrain 已提交
126 127
        batch_size = 5
        hidden_size = 20
P
phlrain 已提交
128 129 130 131 132 133

        input_weight_size = (hidden_size * hidden_size) * 4
        hidden_weight_size = (hidden_size * hidden_size) * 4
        weight_size = input_weight_size + hidden_weight_size
        weight_size += hidden_size * 8

P
phlrain 已提交
134
        input = np.random.uniform(
G
GaoWei8 已提交
135
            low=-0.1, high=0.1, size=(seq_length, batch_size,
P
phlrain 已提交
136 137 138
                                      hidden_size)).astype(self.dtype)
        flat_w = np.random.uniform(
            low=-0.1, high=0.1, size=(weight_size)).astype(self.dtype)
P
phlrain 已提交
139

P
phlrain 已提交
140
        output, last_hidden, last_cell = lstm_naive(input, flat_w)
P
phlrain 已提交
141

G
GaoWei8 已提交
142 143 144 145
        init_h = np.zeros((1, batch_size, hidden_size), dtype=np.float64)
        init_c = np.zeros((1, batch_size, hidden_size), dtype=np.float64)
        state_out = np.ndarray((300)).astype("uint8")

P
phlrain 已提交
146
        self.inputs = {
G
GaoWei8 已提交
147 148 149 150
            'Input': input,
            'W': flat_w,
            'InitH': init_h,
            'InitC': init_c
P
phlrain 已提交
151 152 153 154 155 156 157 158
        }
        self.attrs = {
            'dropout_prob': 0.0,
            'is_bidirec': False,
            'input_size': hidden_size,
            'hidden_size': hidden_size,
            'num_layers': 1,
        }
P
phlrain 已提交
159 160
        self.outputs = {
            'Out': output,
G
GaoWei8 已提交
161 162 163 164
            "LastH": last_hidden,
            'LastC': last_cell,
            'Reserve': np.ndarray((400)).astype("uint8"),
            'StateOut': state_out
P
phlrain 已提交
165
        }
P
phlrain 已提交
166 167

    def test_output_with_place(self):
168
        # depend on the scope structure
169
        place = core.CUDAPlace(0)
G
GaoWei8 已提交
170 171
        self.check_output_with_place(
            place, no_check_set=['Reserve', 'StateOut'])
P
phlrain 已提交
172

P
phlrain 已提交
173
    def test_grad_with_place(self):
174
        # depend on the scope structure
175 176 177
        place = core.CUDAPlace(0)
        self.check_grad_with_place(
            place,
G
GaoWei8 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
            set(['Input', 'W', 'InitH', 'InitC']), ['Out', 'LastH', 'LastC'],
            max_relative_error=1e-4)


@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
class TestCUDNNlstmAPI(unittest.TestCase):
    def test_lstm(self):
        seq_len = 20
        batch_size = 5
        hidden_size = 20
        dropout_prob = 0.0
        num_layers = 1
        input = fluid.data(
            name='input',
            shape=[seq_len, batch_size, hidden_size],
            dtype='float64')
        init_h = layers.fill_constant([num_layers, batch_size, hidden_size],
                                      'float64', 0.0)
        init_c = layers.fill_constant([num_layers, batch_size, hidden_size],
                                      'float64', 0.0)
        rnn_out, last_h, last_c = layers.lstm(input, init_h, init_c, seq_len,
                                              hidden_size, num_layers,
                                              dropout_prob)
        exe = fluid.Executor(fluid.CUDAPlace(0))
        exe.run(fluid.default_startup_program())
        input_i = np.random.uniform(
            low=-0.1, high=0.1, size=(seq_len, batch_size,
                                      hidden_size)).astype("float64")
        out = exe.run(fluid.default_main_program(),
                      feed={'input': input_i},
                      fetch_list=[rnn_out, last_h, last_c, 'cudnn_lstm_0.w_0'])

        output, last_hidden, last_cell = lstm_naive(input_i, out[3])

        self.assertTrue(np.allclose(output, out[0], atol=1e-5))
        self.assertTrue(np.allclose(last_hidden, out[1], atol=1e-5))
        self.assertTrue(np.allclose(last_cell, out[2], atol=1e-5))
P
phlrain 已提交
216

P
phlrain 已提交
217 218 219

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