Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2018-2020 ARM Limited. |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_TEST_LSTM_LAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_LSTM_LAYER_FIXTURE |
| 26 | |
| 27 | #include "tests/Globals.h" |
| 28 | #include "tests/framework/Asserts.h" |
| 29 | #include "tests/framework/Fixture.h" |
| 30 | #include "tests/validation/reference/ActivationLayer.h" |
Georgios Pinitas | cbf39c6 | 2018-09-10 15:07:45 +0100 | [diff] [blame] | 31 | #include "tests/validation/reference/ArithmeticOperations.h" |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 32 | #include "tests/validation/reference/ConcatenateLayer.h" |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 33 | #include "tests/validation/reference/FullyConnectedLayer.h" |
| 34 | #include "tests/validation/reference/GEMM.h" |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 35 | #include "tests/validation/reference/MeanStdDevNormalizationLayer.h" |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 36 | #include "tests/validation/reference/PixelWiseMultiplication.h" |
| 37 | #include "tests/validation/reference/Transpose.h" |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace test |
| 42 | { |
| 43 | namespace validation |
| 44 | { |
| 45 | template <typename TensorType, typename AccessorType, typename FunctionType, typename FunctionParams, typename T> |
| 46 | class LSTMLayerValidationFixture : public framework::Fixture |
| 47 | { |
| 48 | public: |
| 49 | template <typename...> |
| 50 | void setup(TensorShape input_shape, TensorShape input_weights_shape, TensorShape recurrent_weights_shape, TensorShape cell_bias_shape, TensorShape output_cell_shape, TensorShape output_shape, |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 51 | TensorShape scratch_shape, ActivationLayerInfo info, float cell_threshold, float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt, |
| 52 | bool use_layer_norm) |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 53 | { |
| 54 | _target = compute_target(input_shape, input_weights_shape, recurrent_weights_shape, cell_bias_shape, output_cell_shape, output_shape, scratch_shape, info, cell_threshold, projection_threshold, |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 55 | data_type, projection_opt, peephole_opt, use_layer_norm); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 56 | _reference = compute_reference(input_shape, input_weights_shape, recurrent_weights_shape, cell_bias_shape, output_cell_shape, output_shape, scratch_shape, info, cell_threshold, projection_threshold, |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 57 | data_type, projection_opt, peephole_opt, use_layer_norm); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | protected: |
| 61 | template <typename U> |
| 62 | void fill(U &&tensor, int i) |
| 63 | { |
| 64 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 65 | library->fill(tensor, distribution, i); |
| 66 | } |
| 67 | template <typename U> |
| 68 | void fill_custom_val(U &&tensor, float num, int i) |
| 69 | { |
| 70 | std::uniform_real_distribution<> distribution(num, num); |
| 71 | library->fill(tensor, distribution, i); |
| 72 | } |
| 73 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &input_weights_shape, const TensorShape &recurrent_weights_shape, const TensorShape &cell_bias_shape, |
| 74 | const TensorShape &output_cell_shape, const TensorShape &output_shape, const TensorShape &scratch_shape, ActivationLayerInfo info, float cell_threshold, |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 75 | float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt, bool use_layer_norm) |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 76 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 77 | const unsigned int num_cells = input_weights_shape.y(); |
| 78 | const unsigned int num_outputs = recurrent_weights_shape.x(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 79 | |
| 80 | // Create tensors |
| 81 | TensorType input = create_tensor<TensorType>(input_shape, data_type); |
| 82 | TensorType input_to_forget_w = create_tensor<TensorType>(input_weights_shape, data_type); |
| 83 | TensorType input_to_cell_w = create_tensor<TensorType>(input_weights_shape, data_type); |
| 84 | TensorType input_to_output_w = create_tensor<TensorType>(input_weights_shape, data_type); |
| 85 | TensorType recurrent_to_forget_w = create_tensor<TensorType>(recurrent_weights_shape, data_type); |
| 86 | TensorType recurrent_to_cell_w = create_tensor<TensorType>(recurrent_weights_shape, data_type); |
| 87 | TensorType recurrent_to_output_w = create_tensor<TensorType>(recurrent_weights_shape, data_type); |
| 88 | TensorType forget_gate_bias = create_tensor<TensorType>(cell_bias_shape, data_type); |
| 89 | TensorType cell_bias = create_tensor<TensorType>(cell_bias_shape, data_type); |
| 90 | TensorType output_gate_bias = create_tensor<TensorType>(cell_bias_shape, data_type); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 91 | TensorType output_state_in = create_tensor<TensorType>(output_shape, data_type); |
| 92 | TensorType cell_state_in = create_tensor<TensorType>(output_cell_shape, data_type); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 93 | TensorType scratch = create_tensor<TensorType>(scratch_shape, data_type); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 94 | TensorType output_state_out = create_tensor<TensorType>(output_shape, data_type); |
| 95 | TensorType cell_state_out = create_tensor<TensorType>(output_cell_shape, data_type); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 96 | TensorType output = create_tensor<TensorType>(output_shape, data_type); |
| 97 | TensorType input_to_input_w; |
| 98 | TensorType recurrent_to_input_w; |
| 99 | TensorType cell_to_input_w; |
| 100 | TensorType cell_to_forget_w; |
| 101 | TensorType input_gate_bias; |
| 102 | TensorType cell_to_output_w; |
| 103 | TensorType projection_w; |
| 104 | TensorType projection_bias; |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 105 | TensorType input_layer_norm_w; |
| 106 | TensorType forget_layer_norm_w; |
| 107 | TensorType cell_layer_norm_w; |
| 108 | TensorType output_layer_norm_w; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 109 | |
Georgios Pinitas | 0cc37c3 | 2018-11-14 15:54:26 +0000 | [diff] [blame] | 110 | bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? false : true; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 111 | |
| 112 | FunctionParams lstm_params; |
| 113 | |
| 114 | if(!cifg_opt) |
| 115 | { |
| 116 | input_to_input_w = create_tensor<TensorType>(input_weights_shape, data_type); |
| 117 | recurrent_to_input_w = create_tensor<TensorType>(recurrent_weights_shape, data_type); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 118 | if(peephole_opt) |
| 119 | { |
| 120 | cell_to_input_w = create_tensor<TensorType>(cell_bias_shape, data_type); |
| 121 | } |
| 122 | input_gate_bias = create_tensor<TensorType>(cell_bias_shape, data_type); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 123 | lstm_params.set_cifg_params(&input_to_input_w, &recurrent_to_input_w, &cell_to_input_w, &input_gate_bias); |
| 124 | } |
| 125 | |
| 126 | if(peephole_opt) |
| 127 | { |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 128 | cell_to_forget_w = create_tensor<TensorType>(cell_bias_shape, data_type); |
| 129 | cell_to_output_w = create_tensor<TensorType>(cell_bias_shape, data_type); |
Michalis Spyrou | 09daf4d | 2018-06-28 17:07:22 +0100 | [diff] [blame] | 130 | lstm_params.set_peephole_params(&cell_to_forget_w, &cell_to_output_w); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | if(projection_opt) |
| 134 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 135 | projection_w = create_tensor<TensorType>(TensorShape(num_cells, num_outputs), data_type); |
| 136 | projection_bias = create_tensor<TensorType>(TensorShape(num_outputs), data_type); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 137 | lstm_params.set_projection_params(&projection_w, &projection_bias); |
| 138 | } |
| 139 | |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 140 | if(use_layer_norm) |
| 141 | { |
| 142 | forget_layer_norm_w = create_tensor<TensorType>(TensorShape(num_cells), data_type); |
| 143 | cell_layer_norm_w = create_tensor<TensorType>(TensorShape(num_cells), data_type); |
| 144 | output_layer_norm_w = create_tensor<TensorType>(TensorShape(num_cells), data_type); |
| 145 | if(!cifg_opt) |
| 146 | { |
| 147 | input_layer_norm_w = create_tensor<TensorType>(TensorShape(num_cells), data_type); |
| 148 | lstm_params.set_layer_normalization_params(&input_layer_norm_w, &forget_layer_norm_w, &cell_layer_norm_w, &output_layer_norm_w); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | lstm_params.set_layer_normalization_params(nullptr, &forget_layer_norm_w, &cell_layer_norm_w, &output_layer_norm_w); |
| 153 | } |
| 154 | } |
| 155 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 156 | // Create and configure function |
| 157 | FunctionType lstm; |
| 158 | lstm.configure(&input, &input_to_forget_w, &input_to_cell_w, &input_to_output_w, &recurrent_to_forget_w, |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 159 | &recurrent_to_cell_w, &recurrent_to_output_w, &forget_gate_bias, &cell_bias, &output_gate_bias, |
| 160 | &output_state_in, &cell_state_in, |
| 161 | &scratch, &output_state_out, &cell_state_out, &output, |
| 162 | lstm_params, info, cell_threshold, projection_threshold); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 163 | |
| 164 | ARM_COMPUTE_EXPECT(input.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 165 | ARM_COMPUTE_EXPECT(input_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 166 | ARM_COMPUTE_EXPECT(input_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 167 | ARM_COMPUTE_EXPECT(input_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 168 | ARM_COMPUTE_EXPECT(recurrent_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 169 | ARM_COMPUTE_EXPECT(recurrent_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 170 | ARM_COMPUTE_EXPECT(recurrent_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 171 | ARM_COMPUTE_EXPECT(forget_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 172 | ARM_COMPUTE_EXPECT(cell_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 173 | ARM_COMPUTE_EXPECT(output_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 174 | ARM_COMPUTE_EXPECT(output_state_in.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 175 | ARM_COMPUTE_EXPECT(cell_state_in.info()->is_resizable(), framework::LogLevel::ERRORS); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 176 | ARM_COMPUTE_EXPECT(scratch.info()->is_resizable(), framework::LogLevel::ERRORS); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 177 | ARM_COMPUTE_EXPECT(output_state_out.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 178 | ARM_COMPUTE_EXPECT(cell_state_out.info()->is_resizable(), framework::LogLevel::ERRORS); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 179 | ARM_COMPUTE_EXPECT(output.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 180 | |
| 181 | // Allocate tensors |
| 182 | input.allocator()->allocate(); |
| 183 | input_to_forget_w.allocator()->allocate(); |
| 184 | input_to_cell_w.allocator()->allocate(); |
| 185 | input_to_output_w.allocator()->allocate(); |
| 186 | recurrent_to_forget_w.allocator()->allocate(); |
| 187 | recurrent_to_cell_w.allocator()->allocate(); |
| 188 | recurrent_to_output_w.allocator()->allocate(); |
| 189 | forget_gate_bias.allocator()->allocate(); |
| 190 | cell_bias.allocator()->allocate(); |
| 191 | output_gate_bias.allocator()->allocate(); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 192 | output_state_in.allocator()->allocate(); |
| 193 | cell_state_in.allocator()->allocate(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 194 | scratch.allocator()->allocate(); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 195 | output_state_out.allocator()->allocate(); |
| 196 | cell_state_out.allocator()->allocate(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 197 | output.allocator()->allocate(); |
| 198 | |
| 199 | ARM_COMPUTE_EXPECT(!input.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 200 | ARM_COMPUTE_EXPECT(!input_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 201 | ARM_COMPUTE_EXPECT(!input_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 202 | ARM_COMPUTE_EXPECT(!input_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 203 | ARM_COMPUTE_EXPECT(!recurrent_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 204 | ARM_COMPUTE_EXPECT(!recurrent_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 205 | ARM_COMPUTE_EXPECT(!recurrent_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 206 | ARM_COMPUTE_EXPECT(!forget_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 207 | ARM_COMPUTE_EXPECT(!cell_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 208 | ARM_COMPUTE_EXPECT(!output_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 209 | ARM_COMPUTE_EXPECT(!output_state_in.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 210 | ARM_COMPUTE_EXPECT(!cell_state_in.info()->is_resizable(), framework::LogLevel::ERRORS); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 211 | ARM_COMPUTE_EXPECT(!scratch.info()->is_resizable(), framework::LogLevel::ERRORS); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 212 | ARM_COMPUTE_EXPECT(!output_state_out.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 213 | ARM_COMPUTE_EXPECT(!cell_state_out.info()->is_resizable(), framework::LogLevel::ERRORS); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 214 | ARM_COMPUTE_EXPECT(!output.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 215 | |
| 216 | // Fill tensors |
| 217 | fill(AccessorType(input), 0); |
| 218 | fill(AccessorType(input_to_forget_w), 1); |
| 219 | fill(AccessorType(input_to_cell_w), 2); |
| 220 | fill(AccessorType(input_to_output_w), 3); |
| 221 | fill(AccessorType(recurrent_to_forget_w), 4); |
| 222 | fill(AccessorType(recurrent_to_cell_w), 5); |
| 223 | fill(AccessorType(recurrent_to_output_w), 6); |
| 224 | fill(AccessorType(forget_gate_bias), 7); |
| 225 | fill(AccessorType(cell_bias), 8); |
| 226 | fill(AccessorType(output_gate_bias), 9); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 227 | fill(AccessorType(output_state_in), 10); |
| 228 | fill(AccessorType(cell_state_in), 11); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 229 | fill(AccessorType(scratch), 12); |
| 230 | |
| 231 | if(!cifg_opt) |
| 232 | { |
| 233 | ARM_COMPUTE_EXPECT(input_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 234 | ARM_COMPUTE_EXPECT(recurrent_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 235 | ARM_COMPUTE_EXPECT(cell_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 236 | ARM_COMPUTE_EXPECT(input_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 237 | input_to_input_w.allocator()->allocate(); |
| 238 | recurrent_to_input_w.allocator()->allocate(); |
| 239 | cell_to_input_w.allocator()->allocate(); |
| 240 | input_gate_bias.allocator()->allocate(); |
| 241 | ARM_COMPUTE_EXPECT(!input_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 242 | ARM_COMPUTE_EXPECT(!recurrent_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 243 | ARM_COMPUTE_EXPECT(!cell_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 244 | ARM_COMPUTE_EXPECT(!input_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 245 | fill(AccessorType(input_to_input_w), 13); |
| 246 | fill(AccessorType(recurrent_to_input_w), 14); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 247 | if(peephole_opt) |
| 248 | { |
| 249 | fill(AccessorType(cell_to_input_w), 15); |
| 250 | } |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 251 | fill(AccessorType(recurrent_to_input_w), 16); |
| 252 | fill(AccessorType(input_gate_bias), 17); |
| 253 | } |
| 254 | |
| 255 | if(peephole_opt) |
| 256 | { |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 257 | ARM_COMPUTE_EXPECT(cell_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 258 | ARM_COMPUTE_EXPECT(cell_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 259 | cell_to_forget_w.allocator()->allocate(); |
| 260 | cell_to_output_w.allocator()->allocate(); |
| 261 | ARM_COMPUTE_EXPECT(!cell_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 262 | ARM_COMPUTE_EXPECT(!cell_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 263 | fill(AccessorType(cell_to_forget_w), 18); |
| 264 | fill(AccessorType(cell_to_output_w), 19); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | if(projection_opt) |
| 268 | { |
| 269 | ARM_COMPUTE_EXPECT(projection_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 270 | ARM_COMPUTE_EXPECT(projection_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 271 | |
| 272 | projection_w.allocator()->allocate(); |
| 273 | projection_bias.allocator()->allocate(); |
| 274 | |
| 275 | ARM_COMPUTE_EXPECT(!projection_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 276 | ARM_COMPUTE_EXPECT(!projection_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 277 | |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 278 | fill(AccessorType(projection_w), 20); |
| 279 | fill(AccessorType(projection_bias), 21); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 282 | if(use_layer_norm) |
| 283 | { |
| 284 | if(!cifg_opt) |
| 285 | { |
| 286 | ARM_COMPUTE_EXPECT(input_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 287 | |
| 288 | input_layer_norm_w.allocator()->allocate(); |
| 289 | |
| 290 | ARM_COMPUTE_EXPECT(!input_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 291 | |
| 292 | fill(AccessorType(input_layer_norm_w), 22); |
| 293 | } |
| 294 | ARM_COMPUTE_EXPECT(forget_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 295 | ARM_COMPUTE_EXPECT(cell_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 296 | ARM_COMPUTE_EXPECT(output_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 297 | |
| 298 | forget_layer_norm_w.allocator()->allocate(); |
| 299 | cell_layer_norm_w.allocator()->allocate(); |
| 300 | output_layer_norm_w.allocator()->allocate(); |
| 301 | |
| 302 | ARM_COMPUTE_EXPECT(!forget_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 303 | ARM_COMPUTE_EXPECT(!cell_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 304 | ARM_COMPUTE_EXPECT(!output_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 305 | |
| 306 | fill(AccessorType(forget_layer_norm_w), 23); |
| 307 | fill(AccessorType(cell_layer_norm_w), 24); |
| 308 | fill(AccessorType(output_layer_norm_w), 25); |
| 309 | } |
| 310 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 311 | // Compute function |
| 312 | lstm.run(); |
| 313 | |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 314 | _target_scratch = std::move(scratch); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 315 | return output; |
| 316 | } |
| 317 | |
| 318 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &input_weights_shape, const TensorShape &recurrent_weights_shape, const TensorShape &cell_bias_shape, |
| 319 | const TensorShape &output_cell_shape, const TensorShape &output_shape, const TensorShape &scratch_shape, ActivationLayerInfo info, float cell_threshold, |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 320 | float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt, bool use_layer_norm) |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 321 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 322 | const unsigned int num_cells = input_weights_shape.y(); |
| 323 | const unsigned int num_outputs = recurrent_weights_shape.x(); |
| 324 | |
| 325 | // Create projection weights shape |
| 326 | TensorShape projection_weights_shape(num_cells, num_outputs); |
| 327 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 328 | // Create projection bias shape |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 329 | TensorShape projection_bias_shape(num_outputs); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 330 | |
| 331 | TensorShape gemm_shape{ 1, output_shape.y() }; |
| 332 | SimpleTensor<T> gemm_out{ gemm_shape, data_type }; |
| 333 | |
| 334 | // Create reference |
| 335 | SimpleTensor<T> input{ input_shape, data_type }; |
| 336 | SimpleTensor<T> input_to_input_w{ input_weights_shape, data_type }; |
| 337 | SimpleTensor<T> input_to_forget_w{ input_weights_shape, data_type }; |
| 338 | SimpleTensor<T> input_to_cell_w{ input_weights_shape, data_type }; |
| 339 | SimpleTensor<T> input_to_output_w{ input_weights_shape, data_type }; |
| 340 | SimpleTensor<T> recurrent_to_input_w{ recurrent_weights_shape, data_type }; |
| 341 | SimpleTensor<T> recurrent_to_forget_w{ recurrent_weights_shape, data_type }; |
| 342 | SimpleTensor<T> recurrent_to_cell_w{ recurrent_weights_shape, data_type }; |
| 343 | SimpleTensor<T> recurrent_to_output_w{ recurrent_weights_shape, data_type }; |
| 344 | SimpleTensor<T> cell_to_input_w{ cell_bias_shape, data_type }; |
| 345 | SimpleTensor<T> cell_to_forget_w{ cell_bias_shape, data_type }; |
| 346 | SimpleTensor<T> cell_to_output_w{ cell_bias_shape, data_type }; |
| 347 | SimpleTensor<T> input_gate_bias{ cell_bias_shape, data_type }; |
| 348 | SimpleTensor<T> forget_gate_bias{ cell_bias_shape, data_type }; |
| 349 | SimpleTensor<T> cell_bias{ cell_bias_shape, data_type }; |
| 350 | SimpleTensor<T> output_gate_bias{ cell_bias_shape, data_type }; |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 351 | SimpleTensor<T> projection_w{ projection_weights_shape, data_type }; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 352 | SimpleTensor<T> projection_bias{ projection_bias_shape, data_type }; |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 353 | SimpleTensor<T> output_state_in{ output_shape, data_type }; |
| 354 | SimpleTensor<T> cell_state_in{ output_cell_shape, data_type }; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 355 | SimpleTensor<T> scratch{ scratch_shape, data_type }; |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 356 | SimpleTensor<T> output_state_out{ output_shape, data_type }; |
| 357 | SimpleTensor<T> cell_state_out{ output_cell_shape, data_type }; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 358 | SimpleTensor<T> output{ output_shape, data_type }; |
| 359 | |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 360 | bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? false : true; |
| 361 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 362 | // Fill reference |
| 363 | fill(input, 0); |
| 364 | fill(input_to_forget_w, 1); |
| 365 | fill(input_to_cell_w, 2); |
| 366 | fill(input_to_output_w, 3); |
| 367 | fill(recurrent_to_forget_w, 4); |
| 368 | fill(recurrent_to_cell_w, 5); |
| 369 | fill(recurrent_to_output_w, 6); |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 370 | if(use_layer_norm) |
| 371 | { |
| 372 | fill_custom_val(forget_gate_bias, 0.f, 7); |
| 373 | fill_custom_val(cell_bias, 0.f, 8); |
| 374 | fill_custom_val(output_gate_bias, 0.f, 9); |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | fill(forget_gate_bias, 7); |
| 379 | fill(cell_bias, 8); |
| 380 | fill(output_gate_bias, 9); |
| 381 | } |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 382 | fill(output_state_in, 10); |
| 383 | fill(cell_state_in, 11); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 384 | fill(scratch, 12); |
| 385 | fill(input_to_input_w, 13); |
| 386 | fill(recurrent_to_input_w, 14); |
| 387 | fill(cell_to_input_w, 15); |
| 388 | fill(recurrent_to_input_w, 16); |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 389 | if(!cifg_opt && use_layer_norm) |
| 390 | { |
| 391 | fill_custom_val(input_gate_bias, 0.f, 17); |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | fill(input_gate_bias, 17); |
| 396 | } |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 397 | fill(cell_to_forget_w, 18); |
| 398 | fill(cell_to_output_w, 19); |
| 399 | fill(projection_w, 20); |
| 400 | fill(projection_bias, 21); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 401 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 402 | // Compute forget_gate |
| 403 | SimpleTensor<T> fully_connected_forget = reference::fully_connected_layer(input, input_to_forget_w, forget_gate_bias, output_cell_shape); |
| 404 | SimpleTensor<T> transposed_weights = reference::transpose(recurrent_to_forget_w); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 405 | SimpleTensor<T> gemm = reference::gemm(output_state_in, transposed_weights, cell_state_in, 1.f, 0.f); |
Georgios Pinitas | cbf39c6 | 2018-09-10 15:07:45 +0100 | [diff] [blame] | 406 | SimpleTensor<T> forget_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, fully_connected_forget, gemm, data_type, ConvertPolicy::SATURATE); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 407 | |
| 408 | if(peephole_opt) |
| 409 | { |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame^] | 410 | SimpleTensor<T> pixelwise_mul_forget_gate = reference::pixel_wise_multiplication<T, T, T>(cell_state_in, cell_to_forget_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO, data_type); |
Georgios Pinitas | cbf39c6 | 2018-09-10 15:07:45 +0100 | [diff] [blame] | 411 | forget_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, forget_gate, pixelwise_mul_forget_gate, data_type, ConvertPolicy::SATURATE); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 414 | if(use_layer_norm) |
| 415 | { |
| 416 | SimpleTensor<T> forget_layer_norm_w{ cell_bias_shape, data_type }; |
| 417 | fill(forget_layer_norm_w, 23); |
| 418 | forget_gate = reference::mean_std_normalization_layer(forget_gate); |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame^] | 419 | forget_gate = reference::pixel_wise_multiplication<T, T, T>(forget_gate, forget_layer_norm_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN, data_type); |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 420 | fill(forget_gate_bias, 7); |
| 421 | forget_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, forget_gate, forget_gate_bias, data_type, ConvertPolicy::SATURATE); |
| 422 | } |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 423 | forget_gate = reference::activation_layer(forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
| 424 | |
| 425 | // Compute input_gate |
| 426 | SimpleTensor<T> input_gate; |
| 427 | if(cifg_opt) |
| 428 | { |
| 429 | SimpleTensor<T> ones{ cell_bias_shape, data_type }; |
| 430 | fill_custom_val(ones, 1.f, 0); |
Georgios Pinitas | cbf39c6 | 2018-09-10 15:07:45 +0100 | [diff] [blame] | 431 | input_gate = reference::arithmetic_operation<T>(reference::ArithmeticOperation::SUB, ones, forget_gate, data_type, ConvertPolicy::SATURATE); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 432 | } |
| 433 | else |
| 434 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 435 | SimpleTensor<T> fully_connected_input = reference::fully_connected_layer(input, input_to_input_w, input_gate_bias, output_cell_shape); |
| 436 | transposed_weights = reference::transpose(recurrent_to_input_w); |
| 437 | gemm = reference::gemm(output_state_in, transposed_weights, cell_state_in, 1.f, 0.f); |
Georgios Pinitas | cbf39c6 | 2018-09-10 15:07:45 +0100 | [diff] [blame] | 438 | input_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, fully_connected_input, gemm, data_type, ConvertPolicy::SATURATE); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 439 | if(peephole_opt) |
| 440 | { |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame^] | 441 | SimpleTensor<T> pixelwise_mul_input_gate = reference::pixel_wise_multiplication<T, T, T>(cell_state_in, cell_to_input_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN, data_type); |
Georgios Pinitas | cbf39c6 | 2018-09-10 15:07:45 +0100 | [diff] [blame] | 442 | input_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, input_gate, pixelwise_mul_input_gate, data_type, ConvertPolicy::SATURATE); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 443 | } |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 444 | if(use_layer_norm) |
| 445 | { |
| 446 | SimpleTensor<T> input_layer_norm_w{ cell_bias_shape, data_type }; |
| 447 | fill(input_layer_norm_w, 22); |
| 448 | input_gate = reference::mean_std_normalization_layer(input_gate); |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame^] | 449 | input_gate = reference::pixel_wise_multiplication<T, T, T>(input_gate, input_layer_norm_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN, data_type); |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 450 | fill(input_gate_bias, 17); |
| 451 | input_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, input_gate, input_gate_bias, data_type, ConvertPolicy::SATURATE); |
| 452 | } |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 453 | input_gate = reference::activation_layer(input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | // Compute cell_state |
| 457 | SimpleTensor<T> fully_connected_cell_state = reference::fully_connected_layer(input, input_to_cell_w, cell_bias, output_cell_shape); |
| 458 | transposed_weights = reference::transpose(recurrent_to_cell_w); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 459 | gemm = reference::gemm(output_state_in, transposed_weights, cell_state_out, 1.f, 0.f); |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame^] | 460 | SimpleTensor<T> pixelwise_mul = reference::pixel_wise_multiplication<T, T, T>(cell_state_in, forget_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN, data_type); |
Georgios Pinitas | cbf39c6 | 2018-09-10 15:07:45 +0100 | [diff] [blame] | 461 | cell_state_out = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, fully_connected_cell_state, gemm, data_type, ConvertPolicy::SATURATE); |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 462 | if(use_layer_norm) |
| 463 | { |
| 464 | SimpleTensor<T> cell_layer_norm_w{ cell_bias_shape, data_type }; |
| 465 | fill(cell_layer_norm_w, 24); |
| 466 | cell_state_out = reference::mean_std_normalization_layer(cell_state_out); |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame^] | 467 | cell_state_out = reference::pixel_wise_multiplication<T, T, T>(cell_state_out, cell_layer_norm_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN, data_type); |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 468 | fill(cell_bias, 8); |
| 469 | cell_state_out = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, cell_state_out, cell_bias, data_type, ConvertPolicy::SATURATE); |
| 470 | } |
| 471 | cell_state_out = reference::activation_layer(cell_state_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame^] | 472 | cell_state_out = reference::pixel_wise_multiplication<T, T, T>(cell_state_out, input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN, data_type); |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 473 | cell_state_out = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, cell_state_out, pixelwise_mul, data_type, ConvertPolicy::SATURATE); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 474 | if(cell_threshold != 0.f) |
| 475 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 476 | cell_state_out = reference::activation_layer(cell_state_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, cell_threshold)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | // Compute output |
| 480 | SimpleTensor<T> fully_connected_output = reference::fully_connected_layer(input, input_to_output_w, output_gate_bias, output_cell_shape); |
| 481 | transposed_weights = reference::transpose(recurrent_to_output_w); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 482 | gemm = reference::gemm(output_state_in, transposed_weights, cell_state_out, 1.f, 0.f); |
Georgios Pinitas | cbf39c6 | 2018-09-10 15:07:45 +0100 | [diff] [blame] | 483 | output = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, fully_connected_output, gemm, data_type, ConvertPolicy::SATURATE); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 484 | if(peephole_opt) |
| 485 | { |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame^] | 486 | pixelwise_mul = reference::pixel_wise_multiplication<T, T, T>(cell_state_out, cell_to_output_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN, data_type); |
Georgios Pinitas | cbf39c6 | 2018-09-10 15:07:45 +0100 | [diff] [blame] | 487 | output = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, output, pixelwise_mul, data_type, ConvertPolicy::SATURATE); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 488 | } |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 489 | if(use_layer_norm) |
| 490 | { |
| 491 | SimpleTensor<T> output_layer_norm_w{ cell_bias_shape, data_type }; |
| 492 | fill(output_layer_norm_w, 25); |
| 493 | output = reference::mean_std_normalization_layer(output); |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame^] | 494 | output = reference::pixel_wise_multiplication<T, T, T>(output, output_layer_norm_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN, data_type); |
Michele Di Giorgio | 39438b4 | 2019-06-04 12:41:45 +0100 | [diff] [blame] | 495 | fill(output_gate_bias, 9); |
| 496 | output = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, output, output_gate_bias, data_type, ConvertPolicy::SATURATE); |
| 497 | } |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 498 | output = reference::activation_layer(output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
| 499 | |
| 500 | // Compute output state |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 501 | SimpleTensor<T> cell_state_activation = reference::activation_layer(cell_state_out, info); |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame^] | 502 | output_state_out = reference::pixel_wise_multiplication<T, T, T>(output, cell_state_activation, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN, data_type); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 503 | |
| 504 | if(projection_opt) |
| 505 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 506 | SimpleTensor<T> fully_connected_projection = reference::fully_connected_layer(output_state_out, projection_w, projection_bias, output_cell_shape); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 507 | if(projection_threshold != 0.f) |
| 508 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 509 | output_state_out = reference::activation_layer(fully_connected_projection, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 512 | |
| 513 | std::vector<SimpleTensor<T>> scratch_inputs; |
| 514 | if(!cifg_opt) |
| 515 | { |
| 516 | scratch_inputs.emplace_back(std::move(input_gate)); |
| 517 | } |
| 518 | scratch_inputs.emplace_back(std::move(cell_state_out)); |
| 519 | scratch_inputs.emplace_back(std::move(forget_gate)); |
| 520 | scratch_inputs.emplace_back(std::move(output)); |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 521 | scratch = reference::concatenate_layer(scratch_inputs, scratch, Window::DimX); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 522 | _reference_scratch = std::move(scratch); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 523 | return output_state_out; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | TensorType _target{}; |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 527 | TensorType _target_scratch{}; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 528 | SimpleTensor<T> _reference{}; |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 529 | SimpleTensor<T> _reference_scratch{}; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 530 | }; |
| 531 | } // namespace validation |
| 532 | } // namespace test |
| 533 | } // namespace arm_compute |
| 534 | #endif /* ARM_COMPUTE_TEST_LSTM_LAYER_FIXTURE */ |