Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 ARM Limited. |
| 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" |
| 31 | #include "tests/validation/reference/ArithmeticAddition.h" |
| 32 | #include "tests/validation/reference/ArithmeticSubtraction.h" |
| 33 | #include "tests/validation/reference/FullyConnectedLayer.h" |
| 34 | #include "tests/validation/reference/GEMM.h" |
| 35 | #include "tests/validation/reference/PixelWiseMultiplication.h" |
| 36 | #include "tests/validation/reference/Transpose.h" |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace test |
| 41 | { |
| 42 | namespace validation |
| 43 | { |
| 44 | template <typename TensorType, typename AccessorType, typename FunctionType, typename FunctionParams, typename T> |
| 45 | class LSTMLayerValidationFixture : public framework::Fixture |
| 46 | { |
| 47 | public: |
| 48 | template <typename...> |
| 49 | void setup(TensorShape input_shape, TensorShape input_weights_shape, TensorShape recurrent_weights_shape, TensorShape cell_bias_shape, TensorShape output_cell_shape, TensorShape output_shape, |
| 50 | TensorShape scratch_shape, ActivationLayerInfo info, float cell_threshold, float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt) |
| 51 | { |
| 52 | _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, |
| 53 | data_type, projection_opt, peephole_opt); |
| 54 | _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, |
| 55 | data_type, projection_opt, peephole_opt); |
| 56 | } |
| 57 | |
| 58 | protected: |
| 59 | template <typename U> |
| 60 | void fill(U &&tensor, int i) |
| 61 | { |
| 62 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 63 | library->fill(tensor, distribution, i); |
| 64 | } |
| 65 | template <typename U> |
| 66 | void fill_custom_val(U &&tensor, float num, int i) |
| 67 | { |
| 68 | std::uniform_real_distribution<> distribution(num, num); |
| 69 | library->fill(tensor, distribution, i); |
| 70 | } |
| 71 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &input_weights_shape, const TensorShape &recurrent_weights_shape, const TensorShape &cell_bias_shape, |
| 72 | const TensorShape &output_cell_shape, const TensorShape &output_shape, const TensorShape &scratch_shape, ActivationLayerInfo info, float cell_threshold, |
| 73 | float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt) |
| 74 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 75 | const unsigned int num_cells = input_weights_shape.y(); |
| 76 | const unsigned int num_outputs = recurrent_weights_shape.x(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 77 | |
| 78 | // Create tensors |
| 79 | TensorType input = create_tensor<TensorType>(input_shape, data_type); |
| 80 | TensorType input_to_forget_w = create_tensor<TensorType>(input_weights_shape, data_type); |
| 81 | TensorType input_to_cell_w = create_tensor<TensorType>(input_weights_shape, data_type); |
| 82 | TensorType input_to_output_w = create_tensor<TensorType>(input_weights_shape, data_type); |
| 83 | TensorType recurrent_to_forget_w = create_tensor<TensorType>(recurrent_weights_shape, data_type); |
| 84 | TensorType recurrent_to_cell_w = create_tensor<TensorType>(recurrent_weights_shape, data_type); |
| 85 | TensorType recurrent_to_output_w = create_tensor<TensorType>(recurrent_weights_shape, data_type); |
| 86 | TensorType forget_gate_bias = create_tensor<TensorType>(cell_bias_shape, data_type); |
| 87 | TensorType cell_bias = create_tensor<TensorType>(cell_bias_shape, data_type); |
| 88 | TensorType output_gate_bias = create_tensor<TensorType>(cell_bias_shape, data_type); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 89 | TensorType output_state_in = create_tensor<TensorType>(output_shape, data_type); |
| 90 | TensorType cell_state_in = create_tensor<TensorType>(output_cell_shape, data_type); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 91 | TensorType scratch = create_tensor<TensorType>(scratch_shape, data_type); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 92 | TensorType output_state_out = create_tensor<TensorType>(output_shape, data_type); |
| 93 | TensorType cell_state_out = create_tensor<TensorType>(output_cell_shape, data_type); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 94 | TensorType output = create_tensor<TensorType>(output_shape, data_type); |
| 95 | TensorType input_to_input_w; |
| 96 | TensorType recurrent_to_input_w; |
| 97 | TensorType cell_to_input_w; |
| 98 | TensorType cell_to_forget_w; |
| 99 | TensorType input_gate_bias; |
| 100 | TensorType cell_to_output_w; |
| 101 | TensorType projection_w; |
| 102 | TensorType projection_bias; |
| 103 | |
| 104 | bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? true : false; |
| 105 | |
| 106 | FunctionParams lstm_params; |
| 107 | |
| 108 | if(!cifg_opt) |
| 109 | { |
| 110 | input_to_input_w = create_tensor<TensorType>(input_weights_shape, data_type); |
| 111 | recurrent_to_input_w = create_tensor<TensorType>(recurrent_weights_shape, data_type); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 112 | if(peephole_opt) |
| 113 | { |
| 114 | cell_to_input_w = create_tensor<TensorType>(cell_bias_shape, data_type); |
| 115 | } |
| 116 | input_gate_bias = create_tensor<TensorType>(cell_bias_shape, data_type); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 117 | lstm_params.set_cifg_params(&input_to_input_w, &recurrent_to_input_w, &cell_to_input_w, &input_gate_bias); |
| 118 | } |
| 119 | |
| 120 | if(peephole_opt) |
| 121 | { |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 122 | cell_to_forget_w = create_tensor<TensorType>(cell_bias_shape, data_type); |
| 123 | cell_to_output_w = create_tensor<TensorType>(cell_bias_shape, data_type); |
Michalis Spyrou | 09daf4d | 2018-06-28 17:07:22 +0100 | [diff] [blame] | 124 | 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] | 125 | } |
| 126 | |
| 127 | if(projection_opt) |
| 128 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 129 | projection_w = create_tensor<TensorType>(TensorShape(num_cells, num_outputs), data_type); |
| 130 | projection_bias = create_tensor<TensorType>(TensorShape(num_outputs), data_type); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 131 | lstm_params.set_projection_params(&projection_w, &projection_bias); |
| 132 | } |
| 133 | |
| 134 | // Create and configure function |
| 135 | FunctionType lstm; |
| 136 | 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^] | 137 | &recurrent_to_cell_w, &recurrent_to_output_w, &forget_gate_bias, &cell_bias, &output_gate_bias, |
| 138 | &output_state_in, &cell_state_in, |
| 139 | &scratch, &output_state_out, &cell_state_out, &output, |
| 140 | lstm_params, info, cell_threshold, projection_threshold); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 141 | |
| 142 | ARM_COMPUTE_EXPECT(input.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 143 | ARM_COMPUTE_EXPECT(input_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 144 | ARM_COMPUTE_EXPECT(input_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 145 | ARM_COMPUTE_EXPECT(input_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 146 | ARM_COMPUTE_EXPECT(recurrent_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 147 | ARM_COMPUTE_EXPECT(recurrent_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 148 | ARM_COMPUTE_EXPECT(recurrent_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 149 | ARM_COMPUTE_EXPECT(forget_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 150 | ARM_COMPUTE_EXPECT(cell_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 151 | ARM_COMPUTE_EXPECT(output_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 152 | ARM_COMPUTE_EXPECT(output_state_in.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 153 | ARM_COMPUTE_EXPECT(cell_state_in.info()->is_resizable(), framework::LogLevel::ERRORS); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 154 | ARM_COMPUTE_EXPECT(scratch.info()->is_resizable(), framework::LogLevel::ERRORS); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 155 | ARM_COMPUTE_EXPECT(output_state_out.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 156 | ARM_COMPUTE_EXPECT(cell_state_out.info()->is_resizable(), framework::LogLevel::ERRORS); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 157 | ARM_COMPUTE_EXPECT(output.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 158 | |
| 159 | // Allocate tensors |
| 160 | input.allocator()->allocate(); |
| 161 | input_to_forget_w.allocator()->allocate(); |
| 162 | input_to_cell_w.allocator()->allocate(); |
| 163 | input_to_output_w.allocator()->allocate(); |
| 164 | recurrent_to_forget_w.allocator()->allocate(); |
| 165 | recurrent_to_cell_w.allocator()->allocate(); |
| 166 | recurrent_to_output_w.allocator()->allocate(); |
| 167 | forget_gate_bias.allocator()->allocate(); |
| 168 | cell_bias.allocator()->allocate(); |
| 169 | output_gate_bias.allocator()->allocate(); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 170 | output_state_in.allocator()->allocate(); |
| 171 | cell_state_in.allocator()->allocate(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 172 | scratch.allocator()->allocate(); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 173 | output_state_out.allocator()->allocate(); |
| 174 | cell_state_out.allocator()->allocate(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 175 | output.allocator()->allocate(); |
| 176 | |
| 177 | ARM_COMPUTE_EXPECT(!input.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 178 | ARM_COMPUTE_EXPECT(!input_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 179 | ARM_COMPUTE_EXPECT(!input_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 180 | ARM_COMPUTE_EXPECT(!input_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 181 | ARM_COMPUTE_EXPECT(!recurrent_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 182 | ARM_COMPUTE_EXPECT(!recurrent_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 183 | ARM_COMPUTE_EXPECT(!recurrent_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 184 | ARM_COMPUTE_EXPECT(!forget_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 185 | ARM_COMPUTE_EXPECT(!cell_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 186 | ARM_COMPUTE_EXPECT(!output_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 187 | ARM_COMPUTE_EXPECT(!output_state_in.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 188 | ARM_COMPUTE_EXPECT(!cell_state_in.info()->is_resizable(), framework::LogLevel::ERRORS); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 189 | ARM_COMPUTE_EXPECT(!scratch.info()->is_resizable(), framework::LogLevel::ERRORS); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 190 | ARM_COMPUTE_EXPECT(!output_state_out.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 191 | ARM_COMPUTE_EXPECT(!cell_state_out.info()->is_resizable(), framework::LogLevel::ERRORS); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 192 | ARM_COMPUTE_EXPECT(!output.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 193 | |
| 194 | // Fill tensors |
| 195 | fill(AccessorType(input), 0); |
| 196 | fill(AccessorType(input_to_forget_w), 1); |
| 197 | fill(AccessorType(input_to_cell_w), 2); |
| 198 | fill(AccessorType(input_to_output_w), 3); |
| 199 | fill(AccessorType(recurrent_to_forget_w), 4); |
| 200 | fill(AccessorType(recurrent_to_cell_w), 5); |
| 201 | fill(AccessorType(recurrent_to_output_w), 6); |
| 202 | fill(AccessorType(forget_gate_bias), 7); |
| 203 | fill(AccessorType(cell_bias), 8); |
| 204 | fill(AccessorType(output_gate_bias), 9); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 205 | fill(AccessorType(output_state_in), 10); |
| 206 | fill(AccessorType(cell_state_in), 11); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 207 | fill(AccessorType(scratch), 12); |
| 208 | |
| 209 | if(!cifg_opt) |
| 210 | { |
| 211 | ARM_COMPUTE_EXPECT(input_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 212 | ARM_COMPUTE_EXPECT(recurrent_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 213 | ARM_COMPUTE_EXPECT(cell_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 214 | ARM_COMPUTE_EXPECT(input_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 215 | input_to_input_w.allocator()->allocate(); |
| 216 | recurrent_to_input_w.allocator()->allocate(); |
| 217 | cell_to_input_w.allocator()->allocate(); |
| 218 | input_gate_bias.allocator()->allocate(); |
| 219 | ARM_COMPUTE_EXPECT(!input_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 220 | ARM_COMPUTE_EXPECT(!recurrent_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 221 | ARM_COMPUTE_EXPECT(!cell_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 222 | ARM_COMPUTE_EXPECT(!input_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 223 | fill(AccessorType(input_to_input_w), 13); |
| 224 | fill(AccessorType(recurrent_to_input_w), 14); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 225 | if(peephole_opt) |
| 226 | { |
| 227 | fill(AccessorType(cell_to_input_w), 15); |
| 228 | } |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 229 | fill(AccessorType(recurrent_to_input_w), 16); |
| 230 | fill(AccessorType(input_gate_bias), 17); |
| 231 | } |
| 232 | |
| 233 | if(peephole_opt) |
| 234 | { |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 235 | ARM_COMPUTE_EXPECT(cell_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 236 | ARM_COMPUTE_EXPECT(cell_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 237 | cell_to_forget_w.allocator()->allocate(); |
| 238 | cell_to_output_w.allocator()->allocate(); |
| 239 | ARM_COMPUTE_EXPECT(!cell_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 240 | ARM_COMPUTE_EXPECT(!cell_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 241 | fill(AccessorType(cell_to_output_w), 18); |
| 242 | } |
| 243 | |
| 244 | if(projection_opt) |
| 245 | { |
| 246 | ARM_COMPUTE_EXPECT(projection_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 247 | ARM_COMPUTE_EXPECT(projection_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 248 | |
| 249 | projection_w.allocator()->allocate(); |
| 250 | projection_bias.allocator()->allocate(); |
| 251 | |
| 252 | ARM_COMPUTE_EXPECT(!projection_w.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 253 | ARM_COMPUTE_EXPECT(!projection_bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 254 | |
| 255 | fill(AccessorType(projection_w), 19); |
| 256 | fill(AccessorType(projection_bias), 20); |
| 257 | } |
| 258 | |
| 259 | // Compute function |
| 260 | lstm.run(); |
| 261 | |
| 262 | return output; |
| 263 | } |
| 264 | |
| 265 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &input_weights_shape, const TensorShape &recurrent_weights_shape, const TensorShape &cell_bias_shape, |
| 266 | const TensorShape &output_cell_shape, const TensorShape &output_shape, const TensorShape &scratch_shape, ActivationLayerInfo info, float cell_threshold, |
| 267 | float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt) |
| 268 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 269 | const unsigned int num_cells = input_weights_shape.y(); |
| 270 | const unsigned int num_outputs = recurrent_weights_shape.x(); |
| 271 | |
| 272 | // Create projection weights shape |
| 273 | TensorShape projection_weights_shape(num_cells, num_outputs); |
| 274 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 275 | // Create projection bias shape |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 276 | TensorShape projection_bias_shape(num_outputs); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 277 | |
| 278 | TensorShape gemm_shape{ 1, output_shape.y() }; |
| 279 | SimpleTensor<T> gemm_out{ gemm_shape, data_type }; |
| 280 | |
| 281 | // Create reference |
| 282 | SimpleTensor<T> input{ input_shape, data_type }; |
| 283 | SimpleTensor<T> input_to_input_w{ input_weights_shape, data_type }; |
| 284 | SimpleTensor<T> input_to_forget_w{ input_weights_shape, data_type }; |
| 285 | SimpleTensor<T> input_to_cell_w{ input_weights_shape, data_type }; |
| 286 | SimpleTensor<T> input_to_output_w{ input_weights_shape, data_type }; |
| 287 | SimpleTensor<T> recurrent_to_input_w{ recurrent_weights_shape, data_type }; |
| 288 | SimpleTensor<T> recurrent_to_forget_w{ recurrent_weights_shape, data_type }; |
| 289 | SimpleTensor<T> recurrent_to_cell_w{ recurrent_weights_shape, data_type }; |
| 290 | SimpleTensor<T> recurrent_to_output_w{ recurrent_weights_shape, data_type }; |
| 291 | SimpleTensor<T> cell_to_input_w{ cell_bias_shape, data_type }; |
| 292 | SimpleTensor<T> cell_to_forget_w{ cell_bias_shape, data_type }; |
| 293 | SimpleTensor<T> cell_to_output_w{ cell_bias_shape, data_type }; |
| 294 | SimpleTensor<T> input_gate_bias{ cell_bias_shape, data_type }; |
| 295 | SimpleTensor<T> forget_gate_bias{ cell_bias_shape, data_type }; |
| 296 | SimpleTensor<T> cell_bias{ cell_bias_shape, data_type }; |
| 297 | SimpleTensor<T> output_gate_bias{ cell_bias_shape, data_type }; |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 298 | SimpleTensor<T> projection_w{ projection_weights_shape, data_type }; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 299 | SimpleTensor<T> projection_bias{ projection_bias_shape, data_type }; |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 300 | SimpleTensor<T> output_state_in{ output_shape, data_type }; |
| 301 | SimpleTensor<T> cell_state_in{ output_cell_shape, data_type }; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 302 | SimpleTensor<T> scratch{ scratch_shape, data_type }; |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 303 | SimpleTensor<T> output_state_out{ output_shape, data_type }; |
| 304 | SimpleTensor<T> cell_state_out{ output_cell_shape, data_type }; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 305 | SimpleTensor<T> output{ output_shape, data_type }; |
| 306 | |
| 307 | // Fill reference |
| 308 | fill(input, 0); |
| 309 | fill(input_to_forget_w, 1); |
| 310 | fill(input_to_cell_w, 2); |
| 311 | fill(input_to_output_w, 3); |
| 312 | fill(recurrent_to_forget_w, 4); |
| 313 | fill(recurrent_to_cell_w, 5); |
| 314 | fill(recurrent_to_output_w, 6); |
| 315 | fill(forget_gate_bias, 7); |
| 316 | fill(cell_bias, 8); |
| 317 | fill(output_gate_bias, 9); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 318 | fill(output_state_in, 10); |
| 319 | fill(cell_state_in, 11); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 320 | fill(scratch, 12); |
| 321 | fill(input_to_input_w, 13); |
| 322 | fill(recurrent_to_input_w, 14); |
| 323 | fill(cell_to_input_w, 15); |
| 324 | fill(recurrent_to_input_w, 16); |
| 325 | fill(input_gate_bias, 17); |
| 326 | fill(cell_to_output_w, 18); |
| 327 | fill(projection_w, 19); |
| 328 | fill(projection_bias, 20); |
| 329 | |
| 330 | bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? true : false; |
| 331 | |
| 332 | // Compute forget_gate |
| 333 | SimpleTensor<T> fully_connected_forget = reference::fully_connected_layer(input, input_to_forget_w, forget_gate_bias, output_cell_shape); |
| 334 | SimpleTensor<T> transposed_weights = reference::transpose(recurrent_to_forget_w); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 335 | SimpleTensor<T> gemm = reference::gemm(output_state_in, transposed_weights, cell_state_in, 1.f, 0.f); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 336 | SimpleTensor<T> forget_gate = reference::arithmetic_addition(fully_connected_forget, gemm, data_type, ConvertPolicy::SATURATE); |
| 337 | |
| 338 | if(peephole_opt) |
| 339 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 340 | SimpleTensor<T> pixelwise_mul_forget_gate = reference::pixel_wise_multiplication(cell_state_in, cell_to_forget_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 341 | forget_gate = reference::arithmetic_addition(forget_gate, pixelwise_mul_forget_gate, data_type, ConvertPolicy::SATURATE); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | forget_gate = reference::activation_layer(forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
| 345 | |
| 346 | // Compute input_gate |
| 347 | SimpleTensor<T> input_gate; |
| 348 | if(cifg_opt) |
| 349 | { |
| 350 | SimpleTensor<T> ones{ cell_bias_shape, data_type }; |
| 351 | fill_custom_val(ones, 1.f, 0); |
| 352 | input_gate = reference::arithmetic_subtraction<T, T, T>(ones, forget_gate, data_type, ConvertPolicy::SATURATE); |
| 353 | } |
| 354 | else |
| 355 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 356 | SimpleTensor<T> fully_connected_input = reference::fully_connected_layer(input, input_to_input_w, input_gate_bias, output_cell_shape); |
| 357 | transposed_weights = reference::transpose(recurrent_to_input_w); |
| 358 | gemm = reference::gemm(output_state_in, transposed_weights, cell_state_in, 1.f, 0.f); |
| 359 | input_gate = reference::arithmetic_addition(fully_connected_input, gemm, data_type, ConvertPolicy::SATURATE); |
| 360 | if(peephole_opt) |
| 361 | { |
| 362 | SimpleTensor<T> pixelwise_mul_input_gate = reference::pixel_wise_multiplication(cell_state_in, cell_to_input_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); |
| 363 | input_gate = reference::arithmetic_addition(input_gate, pixelwise_mul_input_gate, data_type, ConvertPolicy::SATURATE); |
| 364 | } |
| 365 | input_gate = reference::activation_layer(input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | // Compute cell_state |
| 369 | SimpleTensor<T> fully_connected_cell_state = reference::fully_connected_layer(input, input_to_cell_w, cell_bias, output_cell_shape); |
| 370 | transposed_weights = reference::transpose(recurrent_to_cell_w); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 371 | gemm = reference::gemm(output_state_in, transposed_weights, cell_state_out, 1.f, 0.f); |
| 372 | SimpleTensor<T> pixelwise_mul = reference::pixel_wise_multiplication(cell_state_in, forget_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); |
| 373 | cell_state_out = reference::arithmetic_addition(fully_connected_cell_state, gemm, data_type, ConvertPolicy::SATURATE); |
| 374 | cell_state_out = reference::activation_layer(cell_state_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
| 375 | cell_state_out = reference::pixel_wise_multiplication(cell_state_out, input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); |
| 376 | cell_state_out = reference::arithmetic_addition(cell_state_out, pixelwise_mul, data_type, ConvertPolicy::SATURATE); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 377 | if(cell_threshold != 0.f) |
| 378 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 379 | 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] | 380 | } |
| 381 | |
| 382 | // Compute output |
| 383 | SimpleTensor<T> fully_connected_output = reference::fully_connected_layer(input, input_to_output_w, output_gate_bias, output_cell_shape); |
| 384 | transposed_weights = reference::transpose(recurrent_to_output_w); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 385 | gemm = reference::gemm(output_state_in, transposed_weights, cell_state_out, 1.f, 0.f); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 386 | output = reference::arithmetic_addition(fully_connected_output, gemm, data_type, ConvertPolicy::SATURATE); |
| 387 | if(peephole_opt) |
| 388 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 389 | pixelwise_mul = reference::pixel_wise_multiplication(cell_state_out, cell_to_output_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 390 | output = reference::arithmetic_addition(output, pixelwise_mul, data_type, ConvertPolicy::SATURATE); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 391 | } |
| 392 | output = reference::activation_layer(output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
| 393 | |
| 394 | // Compute output state |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 395 | SimpleTensor<T> cell_state_activation = reference::activation_layer(cell_state_out, info); |
| 396 | output_state_out = reference::pixel_wise_multiplication(output, cell_state_activation, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 397 | |
| 398 | if(projection_opt) |
| 399 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 400 | 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] | 401 | if(projection_threshold != 0.f) |
| 402 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 403 | 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] | 404 | } |
| 405 | } |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame^] | 406 | return output_state_out; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | TensorType _target{}; |
| 410 | SimpleTensor<T> _reference{}; |
| 411 | }; |
| 412 | } // namespace validation |
| 413 | } // namespace test |
| 414 | } // namespace arm_compute |
| 415 | #endif /* ARM_COMPUTE_TEST_LSTM_LAYER_FIXTURE */ |