lstmcell.cpp 6.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/**
 * \file dnn/test/naive/lstmcell.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */
#include "megdnn/dtype.h"
#include "megdnn/oprs.h"
#include "test/common/checker.h"
#include "test/naive/fixture.h"

namespace megdnn {
namespace test {
TEST_F(NAIVE, LSTMCELL) {
    Checker<LSTMCell> checker(handle(), true);
    for (size_t batch : {1, 4})
        for (size_t n : {3, 4, 5, 23, 100})
            for (size_t out : {3, 6, 25, 100}) {
                checker.exec(
                        {{batch, n},
                         {out * 4, n},
                         {1, out * 4},
                         {batch, out},
                         {out * 4, out},
                         {1, out * 4},
                         {batch, out},
                         {},
                         {},
                         {}});
            }
    size_t batch_size = 2;
    size_t input_size = 3;
    size_t hidden_size = 2;
    checker.exect(
            Testcase{
                    TensorValue(
                            {batch_size, input_size}, dtype::Float32(),
                            {1, 2, 3, 4, 5, 6}),  // input
                    TensorValue(
                            {4 * hidden_size, input_size}, dtype::Float32(),
                            {
                                    0.3535, 0.3535, 0.3535, 0.3535, 0.3535, 0.3535,
                                    0.3535, 0.3535, 0.3535, 0.3535, 0.3535, 0.3535,
                                    0.3535, 0.3535, 0.3535, 0.3535, 0.3535, 0.3535,
                                    0.3535, 0.3535, 0.3535, 0.3535, 0.3535, 0.3535,
                            }),  // weight_ih
                    TensorValue(
                            {4 * hidden_size}, dtype::Float32(),
                            {0, 0, 0, 0, 0, 0, 0, 0}),  // bias_ih
                    TensorValue(
                            {batch_size, hidden_size}, dtype::Float32(),
                            {1, 2, 3, 4}),  // hx
                    TensorValue(
                            {4 * hidden_size, hidden_size}, dtype::Float32(),
                            {0.3535, 0.3535, 0.3535, 0.3535, 0.3535, 0.3535, 0.3535,
                             0.3535, 0.3535, 0.3535, 0.3535, 0.3535, 0.3535, 0.3535,
                             0.3535, 0.3535}),  // weight_hh
                    TensorValue(
                            {4 * hidden_size}, dtype::Float32(),
                            {0, 0, 0, 0, 0, 0, 0, 0}),  // bias_hh
                    TensorValue(
                            {batch_size, hidden_size}, dtype::Float32(),
                            {2, 3, 4, 5}),  // cx
                    {},
                    {},
                    {}},
            Testcase{
                    {},
                    {},
                    {},
                    {},
                    {},
                    {},
                    {},
                    TensorValue(
                            {batch_size, hidden_size}, dtype::Float32(),
                            {0.9541, 0.9593, 0.9995, 0.9996}),  // hy
                    TensorValue(
                            {batch_size, hidden_size}, dtype::Float32(),
                            {2.8771, 3.8373, 4.9979, 5.9975}),  // cy
                    TensorValue(
                            {batch_size, 4 * hidden_size}, dtype::Float32(),
                            {3.18198, 3.18198, 7.7781, 7.7781, 3.18198, 3.18198,
                             7.77817, 7.77817, 3.18198, 3.18198, 7.77817, 7.77817,
                             3.18198, 3.18198, 7.77817, 7.77817}),  // cy
            });
    batch_size = 2;
    input_size = 2;
    hidden_size = 1;
    checker.exect(
            Testcase{
                    TensorValue(
                            {batch_size, input_size}, dtype::Float32(),
                            {1, 2, 3, 4}),  // input
                    TensorValue(
                            {4 * hidden_size, input_size}, dtype::Float32(),
                            {0.3535, 0.3535, 0.3535, 0.3535, 0.3535, 0.3535, 0.3535,
                             0.3535}),  // weight_ih
                    TensorValue(
                            {4 * hidden_size}, dtype::Float32(),
                            {0.3535, 0.3535, 0.3535, 0.3535}),  // bias_ih
                    TensorValue(
                            {batch_size, hidden_size}, dtype::Float32(), {1, 2}),  // hx
                    TensorValue(
                            {4 * hidden_size, hidden_size}, dtype::Float32(),
                            {0.3535, 0.3535, 0.3535, 0.3535}),  // weight_hh
                    TensorValue(
                            {4 * hidden_size}, dtype::Float32(),
                            {0.3535, 0.3535, 0.3535, 0.3535}),  // bias_hh
                    TensorValue(
                            {batch_size, hidden_size}, dtype::Float32(), {4, 5}),  // cx
                    {},
                    {},
                    {}},
            Testcase{
                    {},
                    {},
                    {},
                    {},
                    {},
                    {},
                    {},
                    TensorValue(
                            {batch_size, hidden_size}, dtype::Float32(),
                            {0.8927, 0.9799}),  // hy
                    TensorValue(
                            {batch_size, hidden_size}, dtype::Float32(),
                            {4.4393, 5.8788}),  // cy
                    TensorValue(
                            {batch_size, 4 * hidden_size}, dtype::Float32(),
                            {2.1210, 3.8885, 2.1210, 3.8885, 2.1210, 3.8885, 2.1210,
                             3.8885}),  // gates
            });
}
}  // namespace test
}  // namespace megdnn