blob: 7f11265932ae066b6d22b709655b5546c517771f [file] [log] [blame]
Michalis Spyroubcedf512018-03-22 14:55:08 +00001/*
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"
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"
36
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43template <typename TensorType, typename AccessorType, typename FunctionType, typename FunctionParams, typename T>
44class LSTMLayerValidationFixture : public framework::Fixture
45{
46public:
47 template <typename...>
48 void setup(TensorShape input_shape, TensorShape input_weights_shape, TensorShape recurrent_weights_shape, TensorShape cell_bias_shape, TensorShape output_cell_shape, TensorShape output_shape,
49 TensorShape scratch_shape, ActivationLayerInfo info, float cell_threshold, float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt)
50 {
51 _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,
52 data_type, projection_opt, peephole_opt);
53 _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,
54 data_type, projection_opt, peephole_opt);
55 }
56
57protected:
58 template <typename U>
59 void fill(U &&tensor, int i)
60 {
61 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
62 library->fill(tensor, distribution, i);
63 }
64 template <typename U>
65 void fill_custom_val(U &&tensor, float num, int i)
66 {
67 std::uniform_real_distribution<> distribution(num, num);
68 library->fill(tensor, distribution, i);
69 }
70 TensorType compute_target(const TensorShape &input_shape, const TensorShape &input_weights_shape, const TensorShape &recurrent_weights_shape, const TensorShape &cell_bias_shape,
71 const TensorShape &output_cell_shape, const TensorShape &output_shape, const TensorShape &scratch_shape, ActivationLayerInfo info, float cell_threshold,
72 float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt)
73 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010074 const unsigned int num_cells = input_weights_shape.y();
75 const unsigned int num_outputs = recurrent_weights_shape.x();
Michalis Spyroubcedf512018-03-22 14:55:08 +000076
77 // Create tensors
78 TensorType input = create_tensor<TensorType>(input_shape, data_type);
79 TensorType input_to_forget_w = create_tensor<TensorType>(input_weights_shape, data_type);
80 TensorType input_to_cell_w = create_tensor<TensorType>(input_weights_shape, data_type);
81 TensorType input_to_output_w = create_tensor<TensorType>(input_weights_shape, data_type);
82 TensorType recurrent_to_forget_w = create_tensor<TensorType>(recurrent_weights_shape, data_type);
83 TensorType recurrent_to_cell_w = create_tensor<TensorType>(recurrent_weights_shape, data_type);
84 TensorType recurrent_to_output_w = create_tensor<TensorType>(recurrent_weights_shape, data_type);
85 TensorType forget_gate_bias = create_tensor<TensorType>(cell_bias_shape, data_type);
86 TensorType cell_bias = create_tensor<TensorType>(cell_bias_shape, data_type);
87 TensorType output_gate_bias = create_tensor<TensorType>(cell_bias_shape, data_type);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010088 TensorType output_state_in = create_tensor<TensorType>(output_shape, data_type);
89 TensorType cell_state_in = create_tensor<TensorType>(output_cell_shape, data_type);
Michalis Spyroubcedf512018-03-22 14:55:08 +000090 TensorType scratch = create_tensor<TensorType>(scratch_shape, data_type);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010091 TensorType output_state_out = create_tensor<TensorType>(output_shape, data_type);
92 TensorType cell_state_out = create_tensor<TensorType>(output_cell_shape, data_type);
Michalis Spyroubcedf512018-03-22 14:55:08 +000093 TensorType output = create_tensor<TensorType>(output_shape, data_type);
94 TensorType input_to_input_w;
95 TensorType recurrent_to_input_w;
96 TensorType cell_to_input_w;
97 TensorType cell_to_forget_w;
98 TensorType input_gate_bias;
99 TensorType cell_to_output_w;
100 TensorType projection_w;
101 TensorType projection_bias;
102
Georgios Pinitas0cc37c32018-11-14 15:54:26 +0000103 bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? false : true;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000104
105 FunctionParams lstm_params;
106
107 if(!cifg_opt)
108 {
109 input_to_input_w = create_tensor<TensorType>(input_weights_shape, data_type);
110 recurrent_to_input_w = create_tensor<TensorType>(recurrent_weights_shape, data_type);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100111 if(peephole_opt)
112 {
113 cell_to_input_w = create_tensor<TensorType>(cell_bias_shape, data_type);
114 }
115 input_gate_bias = create_tensor<TensorType>(cell_bias_shape, data_type);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000116 lstm_params.set_cifg_params(&input_to_input_w, &recurrent_to_input_w, &cell_to_input_w, &input_gate_bias);
117 }
118
119 if(peephole_opt)
120 {
Michalis Spyroubcedf512018-03-22 14:55:08 +0000121 cell_to_forget_w = create_tensor<TensorType>(cell_bias_shape, data_type);
122 cell_to_output_w = create_tensor<TensorType>(cell_bias_shape, data_type);
Michalis Spyrou09daf4d2018-06-28 17:07:22 +0100123 lstm_params.set_peephole_params(&cell_to_forget_w, &cell_to_output_w);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000124 }
125
126 if(projection_opt)
127 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100128 projection_w = create_tensor<TensorType>(TensorShape(num_cells, num_outputs), data_type);
129 projection_bias = create_tensor<TensorType>(TensorShape(num_outputs), data_type);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000130 lstm_params.set_projection_params(&projection_w, &projection_bias);
131 }
132
133 // Create and configure function
134 FunctionType lstm;
135 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 +0100136 &recurrent_to_cell_w, &recurrent_to_output_w, &forget_gate_bias, &cell_bias, &output_gate_bias,
137 &output_state_in, &cell_state_in,
138 &scratch, &output_state_out, &cell_state_out, &output,
139 lstm_params, info, cell_threshold, projection_threshold);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000140
141 ARM_COMPUTE_EXPECT(input.info()->is_resizable(), framework::LogLevel::ERRORS);
142 ARM_COMPUTE_EXPECT(input_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS);
143 ARM_COMPUTE_EXPECT(input_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS);
144 ARM_COMPUTE_EXPECT(input_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS);
145 ARM_COMPUTE_EXPECT(recurrent_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS);
146 ARM_COMPUTE_EXPECT(recurrent_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS);
147 ARM_COMPUTE_EXPECT(recurrent_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS);
148 ARM_COMPUTE_EXPECT(forget_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS);
149 ARM_COMPUTE_EXPECT(cell_bias.info()->is_resizable(), framework::LogLevel::ERRORS);
150 ARM_COMPUTE_EXPECT(output_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100151 ARM_COMPUTE_EXPECT(output_state_in.info()->is_resizable(), framework::LogLevel::ERRORS);
152 ARM_COMPUTE_EXPECT(cell_state_in.info()->is_resizable(), framework::LogLevel::ERRORS);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000153 ARM_COMPUTE_EXPECT(scratch.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100154 ARM_COMPUTE_EXPECT(output_state_out.info()->is_resizable(), framework::LogLevel::ERRORS);
155 ARM_COMPUTE_EXPECT(cell_state_out.info()->is_resizable(), framework::LogLevel::ERRORS);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000156 ARM_COMPUTE_EXPECT(output.info()->is_resizable(), framework::LogLevel::ERRORS);
157
158 // Allocate tensors
159 input.allocator()->allocate();
160 input_to_forget_w.allocator()->allocate();
161 input_to_cell_w.allocator()->allocate();
162 input_to_output_w.allocator()->allocate();
163 recurrent_to_forget_w.allocator()->allocate();
164 recurrent_to_cell_w.allocator()->allocate();
165 recurrent_to_output_w.allocator()->allocate();
166 forget_gate_bias.allocator()->allocate();
167 cell_bias.allocator()->allocate();
168 output_gate_bias.allocator()->allocate();
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100169 output_state_in.allocator()->allocate();
170 cell_state_in.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000171 scratch.allocator()->allocate();
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100172 output_state_out.allocator()->allocate();
173 cell_state_out.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000174 output.allocator()->allocate();
175
176 ARM_COMPUTE_EXPECT(!input.info()->is_resizable(), framework::LogLevel::ERRORS);
177 ARM_COMPUTE_EXPECT(!input_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS);
178 ARM_COMPUTE_EXPECT(!input_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS);
179 ARM_COMPUTE_EXPECT(!input_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS);
180 ARM_COMPUTE_EXPECT(!recurrent_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS);
181 ARM_COMPUTE_EXPECT(!recurrent_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS);
182 ARM_COMPUTE_EXPECT(!recurrent_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS);
183 ARM_COMPUTE_EXPECT(!forget_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS);
184 ARM_COMPUTE_EXPECT(!cell_bias.info()->is_resizable(), framework::LogLevel::ERRORS);
185 ARM_COMPUTE_EXPECT(!output_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100186 ARM_COMPUTE_EXPECT(!output_state_in.info()->is_resizable(), framework::LogLevel::ERRORS);
187 ARM_COMPUTE_EXPECT(!cell_state_in.info()->is_resizable(), framework::LogLevel::ERRORS);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000188 ARM_COMPUTE_EXPECT(!scratch.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100189 ARM_COMPUTE_EXPECT(!output_state_out.info()->is_resizable(), framework::LogLevel::ERRORS);
190 ARM_COMPUTE_EXPECT(!cell_state_out.info()->is_resizable(), framework::LogLevel::ERRORS);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000191 ARM_COMPUTE_EXPECT(!output.info()->is_resizable(), framework::LogLevel::ERRORS);
192
193 // Fill tensors
194 fill(AccessorType(input), 0);
195 fill(AccessorType(input_to_forget_w), 1);
196 fill(AccessorType(input_to_cell_w), 2);
197 fill(AccessorType(input_to_output_w), 3);
198 fill(AccessorType(recurrent_to_forget_w), 4);
199 fill(AccessorType(recurrent_to_cell_w), 5);
200 fill(AccessorType(recurrent_to_output_w), 6);
201 fill(AccessorType(forget_gate_bias), 7);
202 fill(AccessorType(cell_bias), 8);
203 fill(AccessorType(output_gate_bias), 9);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100204 fill(AccessorType(output_state_in), 10);
205 fill(AccessorType(cell_state_in), 11);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000206 fill(AccessorType(scratch), 12);
207
208 if(!cifg_opt)
209 {
210 ARM_COMPUTE_EXPECT(input_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS);
211 ARM_COMPUTE_EXPECT(recurrent_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS);
212 ARM_COMPUTE_EXPECT(cell_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS);
213 ARM_COMPUTE_EXPECT(input_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS);
214 input_to_input_w.allocator()->allocate();
215 recurrent_to_input_w.allocator()->allocate();
216 cell_to_input_w.allocator()->allocate();
217 input_gate_bias.allocator()->allocate();
218 ARM_COMPUTE_EXPECT(!input_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS);
219 ARM_COMPUTE_EXPECT(!recurrent_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS);
220 ARM_COMPUTE_EXPECT(!cell_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS);
221 ARM_COMPUTE_EXPECT(!input_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS);
222 fill(AccessorType(input_to_input_w), 13);
223 fill(AccessorType(recurrent_to_input_w), 14);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100224 if(peephole_opt)
225 {
226 fill(AccessorType(cell_to_input_w), 15);
227 }
Michalis Spyroubcedf512018-03-22 14:55:08 +0000228 fill(AccessorType(recurrent_to_input_w), 16);
229 fill(AccessorType(input_gate_bias), 17);
230 }
231
232 if(peephole_opt)
233 {
Michalis Spyroubcedf512018-03-22 14:55:08 +0000234 ARM_COMPUTE_EXPECT(cell_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS);
235 ARM_COMPUTE_EXPECT(cell_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS);
236 cell_to_forget_w.allocator()->allocate();
237 cell_to_output_w.allocator()->allocate();
238 ARM_COMPUTE_EXPECT(!cell_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS);
239 ARM_COMPUTE_EXPECT(!cell_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS);
240 fill(AccessorType(cell_to_output_w), 18);
241 }
242
243 if(projection_opt)
244 {
245 ARM_COMPUTE_EXPECT(projection_w.info()->is_resizable(), framework::LogLevel::ERRORS);
246 ARM_COMPUTE_EXPECT(projection_bias.info()->is_resizable(), framework::LogLevel::ERRORS);
247
248 projection_w.allocator()->allocate();
249 projection_bias.allocator()->allocate();
250
251 ARM_COMPUTE_EXPECT(!projection_w.info()->is_resizable(), framework::LogLevel::ERRORS);
252 ARM_COMPUTE_EXPECT(!projection_bias.info()->is_resizable(), framework::LogLevel::ERRORS);
253
254 fill(AccessorType(projection_w), 19);
255 fill(AccessorType(projection_bias), 20);
256 }
257
258 // Compute function
259 lstm.run();
260
261 return output;
262 }
263
264 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &input_weights_shape, const TensorShape &recurrent_weights_shape, const TensorShape &cell_bias_shape,
265 const TensorShape &output_cell_shape, const TensorShape &output_shape, const TensorShape &scratch_shape, ActivationLayerInfo info, float cell_threshold,
266 float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt)
267 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100268 const unsigned int num_cells = input_weights_shape.y();
269 const unsigned int num_outputs = recurrent_weights_shape.x();
270
271 // Create projection weights shape
272 TensorShape projection_weights_shape(num_cells, num_outputs);
273
Michalis Spyroubcedf512018-03-22 14:55:08 +0000274 // Create projection bias shape
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100275 TensorShape projection_bias_shape(num_outputs);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000276
277 TensorShape gemm_shape{ 1, output_shape.y() };
278 SimpleTensor<T> gemm_out{ gemm_shape, data_type };
279
280 // Create reference
281 SimpleTensor<T> input{ input_shape, data_type };
282 SimpleTensor<T> input_to_input_w{ input_weights_shape, data_type };
283 SimpleTensor<T> input_to_forget_w{ input_weights_shape, data_type };
284 SimpleTensor<T> input_to_cell_w{ input_weights_shape, data_type };
285 SimpleTensor<T> input_to_output_w{ input_weights_shape, data_type };
286 SimpleTensor<T> recurrent_to_input_w{ recurrent_weights_shape, data_type };
287 SimpleTensor<T> recurrent_to_forget_w{ recurrent_weights_shape, data_type };
288 SimpleTensor<T> recurrent_to_cell_w{ recurrent_weights_shape, data_type };
289 SimpleTensor<T> recurrent_to_output_w{ recurrent_weights_shape, data_type };
290 SimpleTensor<T> cell_to_input_w{ cell_bias_shape, data_type };
291 SimpleTensor<T> cell_to_forget_w{ cell_bias_shape, data_type };
292 SimpleTensor<T> cell_to_output_w{ cell_bias_shape, data_type };
293 SimpleTensor<T> input_gate_bias{ cell_bias_shape, data_type };
294 SimpleTensor<T> forget_gate_bias{ cell_bias_shape, data_type };
295 SimpleTensor<T> cell_bias{ cell_bias_shape, data_type };
296 SimpleTensor<T> output_gate_bias{ cell_bias_shape, data_type };
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100297 SimpleTensor<T> projection_w{ projection_weights_shape, data_type };
Michalis Spyroubcedf512018-03-22 14:55:08 +0000298 SimpleTensor<T> projection_bias{ projection_bias_shape, data_type };
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100299 SimpleTensor<T> output_state_in{ output_shape, data_type };
300 SimpleTensor<T> cell_state_in{ output_cell_shape, data_type };
Michalis Spyroubcedf512018-03-22 14:55:08 +0000301 SimpleTensor<T> scratch{ scratch_shape, data_type };
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100302 SimpleTensor<T> output_state_out{ output_shape, data_type };
303 SimpleTensor<T> cell_state_out{ output_cell_shape, data_type };
Michalis Spyroubcedf512018-03-22 14:55:08 +0000304 SimpleTensor<T> output{ output_shape, data_type };
305
306 // Fill reference
307 fill(input, 0);
308 fill(input_to_forget_w, 1);
309 fill(input_to_cell_w, 2);
310 fill(input_to_output_w, 3);
311 fill(recurrent_to_forget_w, 4);
312 fill(recurrent_to_cell_w, 5);
313 fill(recurrent_to_output_w, 6);
314 fill(forget_gate_bias, 7);
315 fill(cell_bias, 8);
316 fill(output_gate_bias, 9);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100317 fill(output_state_in, 10);
318 fill(cell_state_in, 11);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000319 fill(scratch, 12);
320 fill(input_to_input_w, 13);
321 fill(recurrent_to_input_w, 14);
322 fill(cell_to_input_w, 15);
323 fill(recurrent_to_input_w, 16);
324 fill(input_gate_bias, 17);
325 fill(cell_to_output_w, 18);
326 fill(projection_w, 19);
327 fill(projection_bias, 20);
328
329 bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? true : false;
330
331 // Compute forget_gate
332 SimpleTensor<T> fully_connected_forget = reference::fully_connected_layer(input, input_to_forget_w, forget_gate_bias, output_cell_shape);
333 SimpleTensor<T> transposed_weights = reference::transpose(recurrent_to_forget_w);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100334 SimpleTensor<T> gemm = reference::gemm(output_state_in, transposed_weights, cell_state_in, 1.f, 0.f);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100335 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 +0000336
337 if(peephole_opt)
338 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100339 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 Pinitascbf39c62018-09-10 15:07:45 +0100340 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 +0000341 }
342
343 forget_gate = reference::activation_layer(forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
344
345 // Compute input_gate
346 SimpleTensor<T> input_gate;
347 if(cifg_opt)
348 {
349 SimpleTensor<T> ones{ cell_bias_shape, data_type };
350 fill_custom_val(ones, 1.f, 0);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100351 input_gate = reference::arithmetic_operation<T>(reference::ArithmeticOperation::SUB, ones, forget_gate, data_type, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000352 }
353 else
354 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100355 SimpleTensor<T> fully_connected_input = reference::fully_connected_layer(input, input_to_input_w, input_gate_bias, output_cell_shape);
356 transposed_weights = reference::transpose(recurrent_to_input_w);
357 gemm = reference::gemm(output_state_in, transposed_weights, cell_state_in, 1.f, 0.f);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100358 input_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, fully_connected_input, gemm, data_type, ConvertPolicy::SATURATE);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100359 if(peephole_opt)
360 {
361 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 +0100362 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 +0100363 }
364 input_gate = reference::activation_layer(input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000365 }
366
367 // Compute cell_state
368 SimpleTensor<T> fully_connected_cell_state = reference::fully_connected_layer(input, input_to_cell_w, cell_bias, output_cell_shape);
369 transposed_weights = reference::transpose(recurrent_to_cell_w);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100370 gemm = reference::gemm(output_state_in, transposed_weights, cell_state_out, 1.f, 0.f);
371 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 +0100372 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 +0100373 cell_state_out = reference::activation_layer(cell_state_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
374 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 +0100375 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 +0000376 if(cell_threshold != 0.f)
377 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100378 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 +0000379 }
380
381 // Compute output
382 SimpleTensor<T> fully_connected_output = reference::fully_connected_layer(input, input_to_output_w, output_gate_bias, output_cell_shape);
383 transposed_weights = reference::transpose(recurrent_to_output_w);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100384 gemm = reference::gemm(output_state_in, transposed_weights, cell_state_out, 1.f, 0.f);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100385 output = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, fully_connected_output, gemm, data_type, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000386 if(peephole_opt)
387 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100388 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 +0100389 output = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, output, pixelwise_mul, data_type, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000390 }
391 output = reference::activation_layer(output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
392
393 // Compute output state
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100394 SimpleTensor<T> cell_state_activation = reference::activation_layer(cell_state_out, info);
395 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 +0000396
397 if(projection_opt)
398 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100399 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 +0000400 if(projection_threshold != 0.f)
401 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100402 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 +0000403 }
404 }
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100405 return output_state_out;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000406 }
407
408 TensorType _target{};
409 SimpleTensor<T> _reference{};
410};
411} // namespace validation
412} // namespace test
413} // namespace arm_compute
414#endif /* ARM_COMPUTE_TEST_LSTM_LAYER_FIXTURE */