blob: b0b2a8fae13473634fcff502e687f703528ed796 [file] [log] [blame]
Michalis Spyroubcedf512018-03-22 14:55:08 +00001/*
Georgios Pinitas4f859822019-02-06 18:08:04 +00002 * Copyright (c) 2018-2019 ARM Limited.
Michalis Spyroubcedf512018-03-22 14:55:08 +00003 *
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 Pinitascbf39c62018-09-10 15:07:45 +010031#include "tests/validation/reference/ArithmeticOperations.h"
Michalis Spyroubcedf512018-03-22 14:55:08 +000032#include "tests/validation/reference/FullyConnectedLayer.h"
33#include "tests/validation/reference/GEMM.h"
34#include "tests/validation/reference/PixelWiseMultiplication.h"
35#include "tests/validation/reference/Transpose.h"
Georgios Pinitas4f859822019-02-06 18:08:04 +000036#include "tests/validation/reference/WidthConcatenateLayer.h"
Michalis Spyroubcedf512018-03-22 14:55:08 +000037
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44template <typename TensorType, typename AccessorType, typename FunctionType, typename FunctionParams, typename T>
45class LSTMLayerValidationFixture : public framework::Fixture
46{
47public:
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
58protected:
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 Pinitas8bc745d2018-07-18 19:51:24 +010075 const unsigned int num_cells = input_weights_shape.y();
76 const unsigned int num_outputs = recurrent_weights_shape.x();
Michalis Spyroubcedf512018-03-22 14:55:08 +000077
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 Pinitas8bc745d2018-07-18 19:51:24 +010089 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 Spyroubcedf512018-03-22 14:55:08 +000091 TensorType scratch = create_tensor<TensorType>(scratch_shape, data_type);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010092 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 Spyroubcedf512018-03-22 14:55:08 +000094 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
Georgios Pinitas0cc37c32018-11-14 15:54:26 +0000104 bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? false : true;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000105
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 Pinitas8bc745d2018-07-18 19:51:24 +0100112 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 Spyroubcedf512018-03-22 14:55:08 +0000117 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 Spyroubcedf512018-03-22 14:55:08 +0000122 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 Spyrou09daf4d2018-06-28 17:07:22 +0100124 lstm_params.set_peephole_params(&cell_to_forget_w, &cell_to_output_w);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000125 }
126
127 if(projection_opt)
128 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100129 projection_w = create_tensor<TensorType>(TensorShape(num_cells, num_outputs), data_type);
130 projection_bias = create_tensor<TensorType>(TensorShape(num_outputs), data_type);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000131 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 Pinitas8bc745d2018-07-18 19:51:24 +0100137 &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 Spyroubcedf512018-03-22 14:55:08 +0000141
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 Pinitas8bc745d2018-07-18 19:51:24 +0100152 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 Spyroubcedf512018-03-22 14:55:08 +0000154 ARM_COMPUTE_EXPECT(scratch.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100155 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 Spyroubcedf512018-03-22 14:55:08 +0000157 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 Pinitas8bc745d2018-07-18 19:51:24 +0100170 output_state_in.allocator()->allocate();
171 cell_state_in.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000172 scratch.allocator()->allocate();
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100173 output_state_out.allocator()->allocate();
174 cell_state_out.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000175 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 Pinitas8bc745d2018-07-18 19:51:24 +0100187 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 Spyroubcedf512018-03-22 14:55:08 +0000189 ARM_COMPUTE_EXPECT(!scratch.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100190 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 Spyroubcedf512018-03-22 14:55:08 +0000192 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 Pinitas8bc745d2018-07-18 19:51:24 +0100205 fill(AccessorType(output_state_in), 10);
206 fill(AccessorType(cell_state_in), 11);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000207 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 Pinitas8bc745d2018-07-18 19:51:24 +0100225 if(peephole_opt)
226 {
227 fill(AccessorType(cell_to_input_w), 15);
228 }
Michalis Spyroubcedf512018-03-22 14:55:08 +0000229 fill(AccessorType(recurrent_to_input_w), 16);
230 fill(AccessorType(input_gate_bias), 17);
231 }
232
233 if(peephole_opt)
234 {
Michalis Spyroubcedf512018-03-22 14:55:08 +0000235 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);
Georgios Pinitas4f859822019-02-06 18:08:04 +0000241 fill(AccessorType(cell_to_forget_w), 18);
242 fill(AccessorType(cell_to_output_w), 19);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000243 }
244
245 if(projection_opt)
246 {
247 ARM_COMPUTE_EXPECT(projection_w.info()->is_resizable(), framework::LogLevel::ERRORS);
248 ARM_COMPUTE_EXPECT(projection_bias.info()->is_resizable(), framework::LogLevel::ERRORS);
249
250 projection_w.allocator()->allocate();
251 projection_bias.allocator()->allocate();
252
253 ARM_COMPUTE_EXPECT(!projection_w.info()->is_resizable(), framework::LogLevel::ERRORS);
254 ARM_COMPUTE_EXPECT(!projection_bias.info()->is_resizable(), framework::LogLevel::ERRORS);
255
Georgios Pinitas4f859822019-02-06 18:08:04 +0000256 fill(AccessorType(projection_w), 20);
257 fill(AccessorType(projection_bias), 21);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000258 }
259
260 // Compute function
261 lstm.run();
262
Georgios Pinitas4f859822019-02-06 18:08:04 +0000263 _target_scratch = std::move(scratch);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000264 return output;
265 }
266
267 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &input_weights_shape, const TensorShape &recurrent_weights_shape, const TensorShape &cell_bias_shape,
268 const TensorShape &output_cell_shape, const TensorShape &output_shape, const TensorShape &scratch_shape, ActivationLayerInfo info, float cell_threshold,
269 float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt)
270 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100271 const unsigned int num_cells = input_weights_shape.y();
272 const unsigned int num_outputs = recurrent_weights_shape.x();
273
274 // Create projection weights shape
275 TensorShape projection_weights_shape(num_cells, num_outputs);
276
Michalis Spyroubcedf512018-03-22 14:55:08 +0000277 // Create projection bias shape
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100278 TensorShape projection_bias_shape(num_outputs);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000279
280 TensorShape gemm_shape{ 1, output_shape.y() };
281 SimpleTensor<T> gemm_out{ gemm_shape, data_type };
282
283 // Create reference
284 SimpleTensor<T> input{ input_shape, data_type };
285 SimpleTensor<T> input_to_input_w{ input_weights_shape, data_type };
286 SimpleTensor<T> input_to_forget_w{ input_weights_shape, data_type };
287 SimpleTensor<T> input_to_cell_w{ input_weights_shape, data_type };
288 SimpleTensor<T> input_to_output_w{ input_weights_shape, data_type };
289 SimpleTensor<T> recurrent_to_input_w{ recurrent_weights_shape, data_type };
290 SimpleTensor<T> recurrent_to_forget_w{ recurrent_weights_shape, data_type };
291 SimpleTensor<T> recurrent_to_cell_w{ recurrent_weights_shape, data_type };
292 SimpleTensor<T> recurrent_to_output_w{ recurrent_weights_shape, data_type };
293 SimpleTensor<T> cell_to_input_w{ cell_bias_shape, data_type };
294 SimpleTensor<T> cell_to_forget_w{ cell_bias_shape, data_type };
295 SimpleTensor<T> cell_to_output_w{ cell_bias_shape, data_type };
296 SimpleTensor<T> input_gate_bias{ cell_bias_shape, data_type };
297 SimpleTensor<T> forget_gate_bias{ cell_bias_shape, data_type };
298 SimpleTensor<T> cell_bias{ cell_bias_shape, data_type };
299 SimpleTensor<T> output_gate_bias{ cell_bias_shape, data_type };
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100300 SimpleTensor<T> projection_w{ projection_weights_shape, data_type };
Michalis Spyroubcedf512018-03-22 14:55:08 +0000301 SimpleTensor<T> projection_bias{ projection_bias_shape, data_type };
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100302 SimpleTensor<T> output_state_in{ output_shape, data_type };
303 SimpleTensor<T> cell_state_in{ output_cell_shape, data_type };
Michalis Spyroubcedf512018-03-22 14:55:08 +0000304 SimpleTensor<T> scratch{ scratch_shape, data_type };
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100305 SimpleTensor<T> output_state_out{ output_shape, data_type };
306 SimpleTensor<T> cell_state_out{ output_cell_shape, data_type };
Michalis Spyroubcedf512018-03-22 14:55:08 +0000307 SimpleTensor<T> output{ output_shape, data_type };
308
309 // Fill reference
310 fill(input, 0);
311 fill(input_to_forget_w, 1);
312 fill(input_to_cell_w, 2);
313 fill(input_to_output_w, 3);
314 fill(recurrent_to_forget_w, 4);
315 fill(recurrent_to_cell_w, 5);
316 fill(recurrent_to_output_w, 6);
317 fill(forget_gate_bias, 7);
318 fill(cell_bias, 8);
319 fill(output_gate_bias, 9);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100320 fill(output_state_in, 10);
321 fill(cell_state_in, 11);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000322 fill(scratch, 12);
323 fill(input_to_input_w, 13);
324 fill(recurrent_to_input_w, 14);
325 fill(cell_to_input_w, 15);
326 fill(recurrent_to_input_w, 16);
327 fill(input_gate_bias, 17);
Georgios Pinitas4f859822019-02-06 18:08:04 +0000328 fill(cell_to_forget_w, 18);
329 fill(cell_to_output_w, 19);
330 fill(projection_w, 20);
331 fill(projection_bias, 21);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000332
Georgios Pinitas4f859822019-02-06 18:08:04 +0000333 bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? false : true;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000334
335 // Compute forget_gate
336 SimpleTensor<T> fully_connected_forget = reference::fully_connected_layer(input, input_to_forget_w, forget_gate_bias, output_cell_shape);
337 SimpleTensor<T> transposed_weights = reference::transpose(recurrent_to_forget_w);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100338 SimpleTensor<T> gemm = reference::gemm(output_state_in, transposed_weights, cell_state_in, 1.f, 0.f);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100339 SimpleTensor<T> forget_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, fully_connected_forget, gemm, data_type, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000340
341 if(peephole_opt)
342 {
Georgios Pinitas4f859822019-02-06 18:08:04 +0000343 SimpleTensor<T> pixelwise_mul_forget_gate = reference::pixel_wise_multiplication(cell_state_in, cell_to_forget_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100344 forget_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, forget_gate, pixelwise_mul_forget_gate, data_type, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000345 }
346
347 forget_gate = reference::activation_layer(forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
348
349 // Compute input_gate
350 SimpleTensor<T> input_gate;
351 if(cifg_opt)
352 {
353 SimpleTensor<T> ones{ cell_bias_shape, data_type };
354 fill_custom_val(ones, 1.f, 0);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100355 input_gate = reference::arithmetic_operation<T>(reference::ArithmeticOperation::SUB, ones, forget_gate, data_type, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000356 }
357 else
358 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100359 SimpleTensor<T> fully_connected_input = reference::fully_connected_layer(input, input_to_input_w, input_gate_bias, output_cell_shape);
360 transposed_weights = reference::transpose(recurrent_to_input_w);
361 gemm = reference::gemm(output_state_in, transposed_weights, cell_state_in, 1.f, 0.f);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100362 input_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, fully_connected_input, gemm, data_type, ConvertPolicy::SATURATE);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100363 if(peephole_opt)
364 {
365 SimpleTensor<T> pixelwise_mul_input_gate = reference::pixel_wise_multiplication(cell_state_in, cell_to_input_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100366 input_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, input_gate, pixelwise_mul_input_gate, data_type, ConvertPolicy::SATURATE);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100367 }
368 input_gate = reference::activation_layer(input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000369 }
370
371 // Compute cell_state
372 SimpleTensor<T> fully_connected_cell_state = reference::fully_connected_layer(input, input_to_cell_w, cell_bias, output_cell_shape);
373 transposed_weights = reference::transpose(recurrent_to_cell_w);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100374 gemm = reference::gemm(output_state_in, transposed_weights, cell_state_out, 1.f, 0.f);
375 SimpleTensor<T> pixelwise_mul = reference::pixel_wise_multiplication(cell_state_in, forget_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100376 cell_state_out = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, fully_connected_cell_state, gemm, data_type, ConvertPolicy::SATURATE);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100377 cell_state_out = reference::activation_layer(cell_state_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
378 cell_state_out = reference::pixel_wise_multiplication(cell_state_out, input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100379 cell_state_out = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, cell_state_out, pixelwise_mul, data_type, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000380 if(cell_threshold != 0.f)
381 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100382 cell_state_out = reference::activation_layer(cell_state_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, cell_threshold));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000383 }
384
385 // Compute output
386 SimpleTensor<T> fully_connected_output = reference::fully_connected_layer(input, input_to_output_w, output_gate_bias, output_cell_shape);
387 transposed_weights = reference::transpose(recurrent_to_output_w);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100388 gemm = reference::gemm(output_state_in, transposed_weights, cell_state_out, 1.f, 0.f);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100389 output = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, fully_connected_output, gemm, data_type, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000390 if(peephole_opt)
391 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100392 pixelwise_mul = reference::pixel_wise_multiplication(cell_state_out, cell_to_output_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100393 output = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, output, pixelwise_mul, data_type, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000394 }
395 output = reference::activation_layer(output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
396
397 // Compute output state
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100398 SimpleTensor<T> cell_state_activation = reference::activation_layer(cell_state_out, info);
399 output_state_out = reference::pixel_wise_multiplication(output, cell_state_activation, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000400
401 if(projection_opt)
402 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100403 SimpleTensor<T> fully_connected_projection = reference::fully_connected_layer(output_state_out, projection_w, projection_bias, output_cell_shape);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000404 if(projection_threshold != 0.f)
405 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100406 output_state_out = reference::activation_layer(fully_connected_projection, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000407 }
408 }
Georgios Pinitas4f859822019-02-06 18:08:04 +0000409
410 std::vector<SimpleTensor<T>> scratch_inputs;
411 if(!cifg_opt)
412 {
413 scratch_inputs.emplace_back(std::move(input_gate));
414 }
415 scratch_inputs.emplace_back(std::move(cell_state_out));
416 scratch_inputs.emplace_back(std::move(forget_gate));
417 scratch_inputs.emplace_back(std::move(output));
418 scratch = reference::widthconcatenate_layer(scratch_inputs);
419 _reference_scratch = std::move(scratch);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100420 return output_state_out;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000421 }
422
423 TensorType _target{};
Georgios Pinitas4f859822019-02-06 18:08:04 +0000424 TensorType _target_scratch{};
Michalis Spyroubcedf512018-03-22 14:55:08 +0000425 SimpleTensor<T> _reference{};
Georgios Pinitas4f859822019-02-06 18:08:04 +0000426 SimpleTensor<T> _reference_scratch{};
Michalis Spyroubcedf512018-03-22 14:55:08 +0000427};
428} // namespace validation
429} // namespace test
430} // namespace arm_compute
431#endif /* ARM_COMPUTE_TEST_LSTM_LAYER_FIXTURE */