blob: ee2b2f4b284df452470e80120101d09087e81ae6 [file] [log] [blame]
Michalis Spyrou25f45a42018-08-08 12:53:05 +01001/*
Georgios Pinitas13a20802019-01-16 18:21:08 +00002 * Copyright (c) 2018-2019 ARM Limited.
Michalis Spyrou25f45a42018-08-08 12:53:05 +01003 *
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#include "arm_compute/runtime/NEON/functions/NELSTMLayer.h"
25
26#include "arm_compute/core/PixelValue.h"
27#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/Validate.h"
29#include "arm_compute/core/utils/misc/ShapeCalculator.h"
30#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
31#include "arm_compute/runtime/common/LSTMParams.h"
32
33#include <cmath>
34#include <memory>
35#include <tuple>
36
37using namespace arm_compute;
38using namespace arm_compute::misc::shape_calculator;
39
40NELSTMLayer::NELSTMLayer(std::shared_ptr<IMemoryManager> memory_manager)
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +010041 : _memory_group(std::move(memory_manager)), _fully_connected_input_gate(), _accum_input_gate1(), _subtract_input_gate(), _pixelwise_mul_input_gate(), _activation_input_gate(),
42 _fully_connected_forget_gate(), _accum_forget_gate1(), _pixelwise_mul_forget_gate(), _activation_forget_gate(), _fully_connected_cell_state(), _gemm_cell_state1(), _transpose_cell_state(),
43 _accum_cell_state1(), _accum_cell_state2(), _pixelwise_mul_cell_state1(), _activation_cell_state(), _cell_clip(), _pixelwise_mul_cell_state2(), _fully_connected_output(),
44 _pixelwise_mul_output_state1(), _accum_output1(), _activation_output(), _activation_output_state(), _pixelwise_mul_output_state2(), _fully_connected_output_state(), _projection_clip(),
45 _copy_cell_state(), _copy_output(), _concat_scratch_buffer(), _concat_inputs_forget_gate(), _concat_weights_forget_gate(), _concat_weights_input_gate(), _concat_weights_output(),
46 _mean_std_norm_input_gate(), _pixelwise_mul_input_gate_coeff(), _accum_input_gate_bias(), _mean_std_norm_forget_gate(), _pixelwise_mul_forget_gate_coeff(), _accum_forget_gate_bias(),
47 _mean_std_norm_cell_gate(), _pixelwise_mul_cell_gate_coeff(), _accum_cell_gate_bias(), _mean_std_norm_output_gate(), _pixelwise_mul_output_gate_coeff(), _accum_output_gate_bias(), _input_gate_out1(),
48 _input_gate_out2(), _input_gate_out3(), _input_gate_out4(), _forget_gate_out1(), _forget_gate_out2(), _forget_gate_out3(), _forget_gate_out4(), _forget_gate_out5(), _forget_gate_out6(),
49 _cell_state_out1(), _cell_state_out2(), _cell_state_out3(), _cell_state_out4(), _cell_state_out5(), _output1(), _output2(), _output3(), _output4(), _cell_state_activation(), _output_state1(), _ones(),
50 _input_layer_norm_out1(), _input_layer_norm_out2(), _forget_layer_norm_out1(), _forget_layer_norm_out2(), _cell_layer_norm_out1(), _cell_layer_norm_out2(), _output_layer_norm_out1(),
51 _output_layer_norm_out2(), _run_peephole_opt(false), _run_cifg_opt(false), _perform_cell_clipping(false), _has_projection_weights(false), _perform_projection_clipping(false), _is_prepared(false),
52 _is_layer_norm_lstm(false)
Michalis Spyrou25f45a42018-08-08 12:53:05 +010053{
54}
55
56void NELSTMLayer::configure(const ITensor *input,
57 const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights,
58 const ITensor *recurrent_to_forget_weights, const ITensor *recurrent_to_cell_weights, const ITensor *recurrent_to_output_weights,
59 const ITensor *forget_gate_bias, const ITensor *cell_bias, const ITensor *output_gate_bias,
60 const ITensor *output_state_in, const ITensor *cell_state_in,
61 ITensor *scratch_buffer, ITensor *output_state_out, ITensor *cell_state_out, ITensor *output,
62 const LSTMParams<ITensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold)
63{
64 ARM_COMPUTE_ERROR_ON_NULLPTR(input,
65 input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
66 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
67 forget_gate_bias, cell_bias, output_gate_bias,
68 output_state_in, cell_state_in,
69 scratch_buffer, output_state_out, cell_state_out, output);
70
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +010071 _is_layer_norm_lstm = lstm_params.use_layer_norm();
72
Michalis Spyrou25f45a42018-08-08 12:53:05 +010073 // Set lstm parameters
74 LSTMParams<ITensorInfo> lstm_params_info;
75 if(lstm_params.has_peephole_opt())
76 {
77 lstm_params_info.set_peephole_params(lstm_params.cell_to_forget_weights()->info(), lstm_params.cell_to_output_weights()->info());
78 }
79 if(lstm_params.has_projection())
80 {
81 lstm_params_info.set_projection_params(lstm_params.projection_weights()->info(),
82 lstm_params.projection_bias() != nullptr ? lstm_params.projection_bias()->info() : nullptr);
83 }
84 if(!lstm_params.has_cifg_opt())
85 {
86 const ITensorInfo *cell_to_input_weights_info = (lstm_params.has_peephole_opt()) ? lstm_params.cell_to_input_weights()->info() : nullptr;
87 lstm_params_info.set_cifg_params(lstm_params.input_to_input_weights()->info(), lstm_params.recurrent_to_input_weights()->info(),
88 cell_to_input_weights_info, lstm_params.input_gate_bias()->info());
89 }
90
91 // Validate
92 ARM_COMPUTE_ERROR_THROW_ON(NELSTMLayer::validate(input->info(), input_to_forget_weights->info(),
93 input_to_cell_weights->info(), input_to_output_weights->info(),
94 recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(),
95 forget_gate_bias->info(), cell_bias->info(), output_gate_bias->info(),
96 output_state_in->info(), cell_state_in->info(),
97 scratch_buffer->info(), output_state_out->info(), cell_state_out->info(), output->info(),
98 lstm_params_info, activation_info, cell_threshold, projection_threshold));
99
Georgios Pinitasda953f22019-04-02 17:27:03 +0100100 const TensorShape cell_state_shape = cell_state_in->info()->tensor_shape();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100101
102 // Configure block that calculates the forget gate
103 // forget_gate = Activation(input * input_to_forget_weights + output_state_in * recurrent_to_forget_weights + PixelWiseMul(cell_state, cell_to_forget_weights) + forget_gate_bias)
John Kesapides917959c2019-02-04 12:37:29 +0000104 // We optimize this as follows:
105 // forget_gate = Activation( (input,output_state_in) * (input_to_forget_weights,recurrent_to_forget_weights) + PixelWiseMul(cell_state, cell_to_forget_weights) + forget_gate_bias)
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100106 _forget_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100107 _forget_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
108 _forget_gate_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
109
John Kesapides917959c2019-02-04 12:37:29 +0000110 std::vector<const ITensor *> inputs_vector;
111 inputs_vector.emplace_back(input);
112 inputs_vector.emplace_back(output_state_in);
113
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100114 _memory_group.manage(&_forget_gate_out2);
Georgios Pinitas09f24972019-05-17 18:14:40 +0100115 _concat_inputs_forget_gate.configure(inputs_vector, &_forget_gate_out2, Window::DimX);
John Kesapides917959c2019-02-04 12:37:29 +0000116
117 std::vector<const ITensor *> weights_vector;
118
119 weights_vector.emplace_back(input_to_forget_weights);
120 weights_vector.emplace_back(recurrent_to_forget_weights);
121
Georgios Pinitas09f24972019-05-17 18:14:40 +0100122 _concat_weights_forget_gate.configure(weights_vector, &_forget_gate_out6, Window::DimX);
John Kesapides917959c2019-02-04 12:37:29 +0000123
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100124 _memory_group.manage(&_forget_gate_out5);
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100125 _fully_connected_forget_gate.configure(&_forget_gate_out2, &_forget_gate_out6, (_is_layer_norm_lstm) ? nullptr : forget_gate_bias, &_forget_gate_out5);
John Kesapides917959c2019-02-04 12:37:29 +0000126 _memory_group.manage(&_forget_gate_out1);
127 _memory_group.manage(&_forget_gate_out3);
128 _forget_gate_out6.allocator()->allocate();
129
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100130 Tensor *forget_gate_out = &_forget_gate_out5;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100131 if(lstm_params.has_peephole_opt())
132 {
133 _forget_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
134
135 _run_peephole_opt = true;
136 _memory_group.manage(&_forget_gate_out4);
137 _pixelwise_mul_forget_gate.configure(cell_state_in, lstm_params.cell_to_forget_weights(), &_forget_gate_out4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100138 _accum_forget_gate1.configure(&_forget_gate_out5, &_forget_gate_out4, &_forget_gate_out3, ConvertPolicy::SATURATE);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100139 _forget_gate_out4.allocator()->allocate();
140 _forget_gate_out5.allocator()->allocate();
141 forget_gate_out = &_forget_gate_out3;
142 }
143 else
144 {
145 _forget_gate_out3.allocator()->allocate();
146 }
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100147 if(_is_layer_norm_lstm)
148 {
149 _forget_layer_norm_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
150 _forget_layer_norm_out2.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
151 _memory_group.manage(&_forget_layer_norm_out1);
152 _memory_group.manage(&_forget_layer_norm_out2);
153 _mean_std_norm_forget_gate.configure(forget_gate_out);
154 _pixelwise_mul_forget_gate_coeff.configure(forget_gate_out, lstm_params.forget_layer_norm_weights(), &_forget_layer_norm_out1, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
155 // forget_gate_out is going to be reassigned, so allocate the tensor that it was assigned to before
156 forget_gate_out->allocator()->allocate();
157 _accum_forget_gate_bias.configure(&_forget_layer_norm_out1, forget_gate_bias, &_forget_layer_norm_out2, ConvertPolicy::SATURATE);
158 _forget_layer_norm_out1.allocator()->allocate();
159 forget_gate_out = &_forget_layer_norm_out2;
160 }
Georgios Pinitas4f859822019-02-06 18:08:04 +0000161 _activation_forget_gate.configure(forget_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100162
163 // Configure block that calculates the input gate
164 // input_gate = Activation(input * input_to_input_weights + output_state * recurrent_to_input_weights + PixelWiseMul(cell_state, cell_to_input_weights) + input_gate_bias), without CIFG
165 // input_gate = 1 - forget_gate, with CIFG
John Kesapides917959c2019-02-04 12:37:29 +0000166 // We optimize this as follows:
167 // input_gate = Activation((input,output_state) * (input_to_input_weights,recurrent_to_input_weights) + PixelWiseMul(cell_state, cell_to_input_weights) + input_gate_bias), without CIFG
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100168 _input_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas4f859822019-02-06 18:08:04 +0000169 Tensor *input_gate_out = &_input_gate_out1;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100170 if(lstm_params.has_cifg_opt())
171 {
172 _memory_group.manage(&_input_gate_out1);
173 _ones.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas4f859822019-02-06 18:08:04 +0000174 _subtract_input_gate.configure(&_ones, forget_gate_out, &_input_gate_out1, ConvertPolicy::SATURATE);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100175 _ones.allocator()->allocate();
176 _run_cifg_opt = true;
177 }
178 else
179 {
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100180 _input_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
181 _input_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
John Kesapides917959c2019-02-04 12:37:29 +0000182
183 std::vector<const ITensor *> lstm_weights;
184 lstm_weights.emplace_back(lstm_params.input_to_input_weights());
185 lstm_weights.emplace_back(lstm_params.recurrent_to_input_weights());
186
Georgios Pinitas09f24972019-05-17 18:14:40 +0100187 _concat_weights_input_gate.configure(lstm_weights, &_input_gate_out2, Window::DimX);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100188
189 _memory_group.manage(&_input_gate_out1);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100190 _memory_group.manage(&_input_gate_out4);
John Kesapides917959c2019-02-04 12:37:29 +0000191
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100192 _fully_connected_input_gate.configure(&_forget_gate_out2, &_input_gate_out2, (_is_layer_norm_lstm) ? nullptr : lstm_params.input_gate_bias(), &_input_gate_out3);
John Kesapides917959c2019-02-04 12:37:29 +0000193 _input_gate_out2.allocator()->allocate();
194 input_gate_out = &_input_gate_out3;
195
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100196 if(_run_peephole_opt)
197 {
John Kesapides917959c2019-02-04 12:37:29 +0000198 _memory_group.manage(&_input_gate_out4);
199 _pixelwise_mul_input_gate.configure(cell_state_in, lstm_params.cell_to_input_weights(), &_input_gate_out4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100200 _accum_input_gate1.configure(&_input_gate_out3, &_input_gate_out4, &_input_gate_out1, ConvertPolicy::SATURATE);
John Kesapides917959c2019-02-04 12:37:29 +0000201 _input_gate_out3.allocator()->allocate();
Georgios Pinitas4f859822019-02-06 18:08:04 +0000202 _input_gate_out4.allocator()->allocate();
Georgios Pinitas4f859822019-02-06 18:08:04 +0000203 input_gate_out = &_input_gate_out1;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100204 }
Georgios Pinitas4f859822019-02-06 18:08:04 +0000205 else
206 {
207 _input_gate_out1.allocator()->allocate();
208 }
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100209
210 if(_is_layer_norm_lstm)
211 {
212 _input_layer_norm_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
213 _input_layer_norm_out2.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
214 _memory_group.manage(&_input_layer_norm_out1);
215 _memory_group.manage(&_input_layer_norm_out2);
216 _mean_std_norm_input_gate.configure(input_gate_out);
217 _pixelwise_mul_input_gate_coeff.configure(input_gate_out, lstm_params.input_layer_norm_weights(), &_input_layer_norm_out1, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
218 // input_gate_out is going to be reassigned, so allocate the tensor that it was assigned to before
219 input_gate_out->allocator()->allocate();
220 _accum_input_gate_bias.configure(&_input_layer_norm_out1, lstm_params.input_gate_bias(), &_input_layer_norm_out2, ConvertPolicy::SATURATE);
221 _input_layer_norm_out1.allocator()->allocate();
222 input_gate_out = &_input_layer_norm_out2;
223 }
Georgios Pinitas4f859822019-02-06 18:08:04 +0000224 _activation_input_gate.configure(input_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100225 }
226
227 // Configure block that calculates the cell state
228 // cell_state = Clip((PixelwiseMul(input_gate, Activation(input * input_to_cell_weights + output_state_in * recurrent_to_cell_weights + cell_bias)) + PixelwiseMul(forget_gate, cell_state)), cell_threshold)
229 TensorShape cell_state1_shape = compute_transposed_shape(*recurrent_to_output_weights->info());
230 _cell_state_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
231 _cell_state_out2.allocator()->init(TensorInfo(cell_state1_shape, 1, input->info()->data_type()));
232 _cell_state_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
233 _cell_state_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
234 _cell_state_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
235
236 _memory_group.manage(&_cell_state_out1);
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100237 _fully_connected_cell_state.configure(input, input_to_cell_weights, (_is_layer_norm_lstm) ? nullptr : cell_bias, &_cell_state_out1);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100238 _memory_group.manage(&_cell_state_out2);
239 _transpose_cell_state.configure(recurrent_to_cell_weights, &_cell_state_out2);
240 _memory_group.manage(&_cell_state_out3);
241 _gemm_cell_state1.configure(output_state_in, &_cell_state_out2, nullptr, &_cell_state_out3, 1.f, 0.f);
242 _cell_state_out2.allocator()->allocate();
243 _memory_group.manage(&_cell_state_out4);
244 _accum_cell_state1.configure(&_cell_state_out1, &_cell_state_out3, &_cell_state_out4, ConvertPolicy::SATURATE);
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100245 Tensor *cell_state_out_ptr = &_cell_state_out4;
246 if(_is_layer_norm_lstm)
247 {
248 _cell_layer_norm_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
249 _cell_layer_norm_out2.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
250 _memory_group.manage(&_cell_layer_norm_out1);
251 _memory_group.manage(&_cell_layer_norm_out2);
252 _mean_std_norm_cell_gate.configure(cell_state_out_ptr);
253 _pixelwise_mul_cell_gate_coeff.configure(cell_state_out_ptr, lstm_params.cell_layer_norm_weights(), &_cell_layer_norm_out1, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
254 // cell_state_out_ptr is going to be reassigned, so allocate the tensor that it was assigned to before
255 cell_state_out_ptr->allocator()->allocate();
256 _accum_cell_gate_bias.configure(&_cell_layer_norm_out1, cell_bias, &_cell_layer_norm_out2, ConvertPolicy::SATURATE);
257 _cell_layer_norm_out1.allocator()->allocate();
258 cell_state_out_ptr = &_cell_layer_norm_out2;
259 }
260 _activation_cell_state.configure(cell_state_out_ptr, nullptr, activation_info);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100261 _memory_group.manage(&_cell_state_out5);
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100262 _pixelwise_mul_cell_state1.configure(cell_state_out_ptr, input_gate_out, &_cell_state_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
263 cell_state_out_ptr->allocator()->allocate();
Georgios Pinitas4f859822019-02-06 18:08:04 +0000264 _pixelwise_mul_cell_state2.configure(forget_gate_out, cell_state_in, &_cell_state_out3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100265 _accum_cell_state2.configure(&_cell_state_out5, &_cell_state_out3, &_cell_state_out1, ConvertPolicy::SATURATE);
266 _cell_state_out3.allocator()->allocate();
267 _cell_state_out5.allocator()->allocate();
268 // Perform clipping
269 if(cell_threshold != 0.f)
270 {
271 _perform_cell_clipping = true;
272 _cell_clip.configure(&_cell_state_out1, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, cell_threshold));
273 }
274
275 // Configure block that calculates the output
276 // output_state_out = Activation(input * input_to_output_weights + output_state_in * recurrent_to_output_weights + PixelWiseMul(cell_state, cell_to_output_weights) + output_gate_bias)
John Kesapides917959c2019-02-04 12:37:29 +0000277 // We optimize this as follows:
278 // output_state_out = Activation( (input,output_state_in) * (input_to_output_weights, recurrent_to_output_weights) + PixelWiseMul(cell_state, cell_to_output_weights) + output_gate_bias)
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100279 _output1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
John Kesapides917959c2019-02-04 12:37:29 +0000280 _output4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100281
John Kesapides917959c2019-02-04 12:37:29 +0000282 std::vector<const ITensor *> in_out_weights;
283 in_out_weights.emplace_back(input_to_output_weights);
284 in_out_weights.emplace_back(recurrent_to_output_weights);
285
Georgios Pinitas09f24972019-05-17 18:14:40 +0100286 _concat_weights_output.configure(in_out_weights, &_output2, Window::DimX);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100287 _memory_group.manage(&_output1);
John Kesapides917959c2019-02-04 12:37:29 +0000288 _memory_group.manage(&_output4);
289
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100290 _fully_connected_output.configure(&_forget_gate_out2, &_output2, (_is_layer_norm_lstm) ? nullptr : output_gate_bias, &_output4);
John Kesapides917959c2019-02-04 12:37:29 +0000291
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100292 _output2.allocator()->allocate();
John Kesapides917959c2019-02-04 12:37:29 +0000293 _forget_gate_out2.allocator()->allocate();
294
295 Tensor *output_gate_out = &_output4;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100296 if(lstm_params.has_peephole_opt())
297 {
John Kesapides917959c2019-02-04 12:37:29 +0000298 _output3.allocator()->init(TensorInfo(_cell_state_out1.info()->tensor_shape(), 1, input->info()->data_type()));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100299
John Kesapides917959c2019-02-04 12:37:29 +0000300 _memory_group.manage(&_output3);
301 _pixelwise_mul_output_state1.configure(&_cell_state_out1, lstm_params.cell_to_output_weights(), &_output3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100302 _accum_output1.configure(&_output4, &_output3, &_output1, ConvertPolicy::SATURATE);
John Kesapides917959c2019-02-04 12:37:29 +0000303 _output4.allocator()->allocate();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100304 output_gate_out = &_output1;
305
306 // Allocate intermediate buffers
John Kesapides917959c2019-02-04 12:37:29 +0000307 _output3.allocator()->allocate();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100308 }
309 else
310 {
311 _output1.allocator()->allocate();
312 }
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100313 if(_is_layer_norm_lstm)
314 {
315 _output_layer_norm_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
316 _output_layer_norm_out2.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
317 _memory_group.manage(&_output_layer_norm_out1);
318 _memory_group.manage(&_output_layer_norm_out2);
319 _mean_std_norm_output_gate.configure(output_gate_out);
320 _pixelwise_mul_output_gate_coeff.configure(output_gate_out, lstm_params.output_layer_norm_weights(), &_output_layer_norm_out1, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
321 // output_gate_out is going to be reassigned, so allocate the tensor that it was assigned to before
322 output_gate_out->allocator()->allocate();
323 _accum_output_gate_bias.configure(&_output_layer_norm_out1, output_gate_bias, &_output_layer_norm_out2, ConvertPolicy::SATURATE);
324 _output_layer_norm_out1.allocator()->allocate();
325 output_gate_out = &_output_layer_norm_out2;
326 }
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100327 _activation_output.configure(output_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100328
329 // Configure block that calculates the output state
330 /** lstm_res = PixelwiseMul(output, Activation(cell_state))
331 *
332 * -- Clip(lstm_res * projection_weights + projection_bias, projection_threshold) , if there is a projection
333 * /
334 * output_state = --
335 * \
336 * -- lstm_res , otherwise
337 */
338 ITensor *output_state_out_tmp = lstm_params.has_projection() ? &_output_state1 : output_state_out;
339 _cell_state_activation.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
340 _output_state1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
341
342 _memory_group.manage(&_cell_state_activation);
343 _activation_output_state.configure(&_cell_state_out1, &_cell_state_activation, activation_info);
344 _pixelwise_mul_output_state2.configure(&_cell_state_activation, output_gate_out, output_state_out_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
345 _cell_state_activation.allocator()->allocate();
Georgios Pinitas13a20802019-01-16 18:21:08 +0000346 output_gate_out->allocator()->allocate();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100347
348 if(lstm_params.has_projection())
349 {
350 _has_projection_weights = true;
351 _fully_connected_output_state.configure(output_state_out_tmp, lstm_params.projection_weights(), lstm_params.projection_bias(), output_state_out);
352 _output_state1.allocator()->allocate();
353 // Perform clipping
354 if(projection_threshold != 0.f)
355 {
356 _perform_projection_clipping = true;
357 _projection_clip.configure(output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold));
358 }
359 }
360
361 // Copy cell state and output
362 _copy_cell_state.configure(&_cell_state_out1, cell_state_out);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100363 _copy_output.configure(output_state_out, output);
364
365 // Vector for holding the tensors to store in scratch buffer
366 std::vector<ITensor *> scratch_inputs;
Georgios Pinitas0cc37c32018-11-14 15:54:26 +0000367 if(!lstm_params.has_cifg_opt())
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100368 {
Georgios Pinitas4f859822019-02-06 18:08:04 +0000369 scratch_inputs.emplace_back(input_gate_out);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100370 }
371 scratch_inputs.emplace_back(&_cell_state_out1);
372 scratch_inputs.emplace_back(forget_gate_out);
373 scratch_inputs.emplace_back(output_gate_out);
Georgios Pinitas09f24972019-05-17 18:14:40 +0100374 _concat_scratch_buffer.configure(scratch_inputs, scratch_buffer, Window::DimX);
Georgios Pinitas4f859822019-02-06 18:08:04 +0000375 input_gate_out->allocator()->allocate();
376 _cell_state_out1.allocator()->allocate();
377 forget_gate_out->allocator()->allocate();
378 output_gate_out->allocator()->allocate();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100379}
380
381Status NELSTMLayer::validate(const ITensorInfo *input,
382 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
383 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
384 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
385 const ITensorInfo *output_state_in, const ITensorInfo *cell_state_in,
386 const ITensorInfo *scratch_buffer, const ITensorInfo *output_state_out, const ITensorInfo *cell_state_out, const ITensorInfo *output,
387 const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold)
388{
389 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input,
390 input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
391 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
392 forget_gate_bias, cell_bias, output_gate_bias,
393 output_state_in, cell_state_in,
394 scratch_buffer, output_state_out, cell_state_out, output);
395
396 // Check data types
397 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
398 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input,
399 input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
400 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
401 forget_gate_bias, cell_bias, output_gate_bias,
402 output_state_in, cell_state_in,
403 scratch_buffer, output_state_out, cell_state_out, output);
404
405 // Check dimensions
406 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2);
407 ARM_COMPUTE_RETURN_ERROR_ON(input_to_forget_weights->num_dimensions() > 2);
408 ARM_COMPUTE_RETURN_ERROR_ON(input_to_cell_weights->num_dimensions() > 2);
409 ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->num_dimensions() > 2);
410 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_forget_weights->num_dimensions() > 2);
411 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_cell_weights->num_dimensions() > 2);
412 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->num_dimensions() > 2);
413 ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->num_dimensions() > 1);
414 ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->num_dimensions() > 1);
415 ARM_COMPUTE_RETURN_ERROR_ON(output_gate_bias->num_dimensions() > 1);
416 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() > 2);
417 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->num_dimensions() > 2);
418 ARM_COMPUTE_RETURN_ERROR_ON(scratch_buffer->num_dimensions() > 2);
419 ARM_COMPUTE_RETURN_ERROR_ON(output_state_out->num_dimensions() > 2);
420 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_out->num_dimensions() > 2);
421 ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 2);
422 ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->dimension(0) * 4 != scratch_buffer->dimension(0)
423 && cell_bias->dimension(0) * 3 != scratch_buffer->dimension(0));
424
425 const unsigned int num_batches = input->dimension(1);
426 const unsigned int num_cells = input_to_output_weights->dimension(1);
427
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100428 if(lstm_params.use_layer_norm())
429 {
430 // If CIFG is used, input layer normalization weights tensor is omitted
431 if(lstm_params.has_cifg_opt())
432 {
433 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_layer_norm_weights() != nullptr);
434 }
435 else
436 {
437 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_layer_norm_weights());
438 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_layer_norm_weights()->num_dimensions() > 1);
439 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_layer_norm_weights()->dimension(0) != num_batches);
440 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, lstm_params.input_layer_norm_weights());
441 }
442
443 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.forget_layer_norm_weights(), lstm_params.cell_layer_norm_weights(), lstm_params.output_layer_norm_weights());
444 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, lstm_params.forget_layer_norm_weights(), lstm_params.cell_layer_norm_weights(), lstm_params.output_layer_norm_weights());
445 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.forget_layer_norm_weights()->num_dimensions() > 1);
446 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_layer_norm_weights()->num_dimensions() > 1);
447 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.output_layer_norm_weights()->num_dimensions() > 1);
448 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.forget_layer_norm_weights()->dimension(0) != num_batches);
449 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_layer_norm_weights()->dimension(0) != num_batches);
450 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.output_layer_norm_weights()->dimension(0) != num_batches);
451 }
452
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100453 // Check peephole optimization
454 if(lstm_params.has_peephole_opt())
455 {
456 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_output_weights(), lstm_params.cell_to_forget_weights());
457 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->num_dimensions() > 1);
458 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_output_weights()->num_dimensions() > 1);
459 }
460
461 TensorShape units_out_transposed_shape = compute_transposed_shape(*recurrent_to_output_weights);
462 TensorShape num_units_transposed_shape = compute_transposed_shape(*forget_gate_bias);
463 const TensorInfo units_out_transposed_info = TensorInfo(units_out_transposed_shape, 1, input->data_type());
464 const TensorInfo num_units_transposed_info = TensorInfo(num_units_transposed_shape, 1, input->data_type());
465
466 TensorInfo input_gate = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
467 TensorInfo forget_gate = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
468 TensorInfo output_gate_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
469 TensorInfo cell_state_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
470
John Kesapides917959c2019-02-04 12:37:29 +0000471 std::vector<const ITensorInfo *> inputs_vector;
472 inputs_vector.emplace_back(input);
473 inputs_vector.emplace_back(output_state_in);
Georgios Pinitas09f24972019-05-17 18:14:40 +0100474 const TensorShape concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, 0);
475 TensorInfo forget_gate_concat = TensorInfo(concat_shape, 1, input->data_type());
476 ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate(inputs_vector, &forget_gate_concat, Window::DimX));
John Kesapides917959c2019-02-04 12:37:29 +0000477
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100478 // Validate forget gate
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100479 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, input_to_forget_weights, (lstm_params.use_layer_norm()) ? nullptr : forget_gate_bias, &forget_gate));
John Kesapides917959c2019-02-04 12:37:29 +0000480
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100481 if(lstm_params.has_peephole_opt())
482 {
483 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_forget_weights(), &forget_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
484 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE));
485 }
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100486 if(lstm_params.use_layer_norm())
487 {
488 ARM_COMPUTE_RETURN_ON_ERROR(NEMeanStdDevNormalizationLayer::validate(&forget_gate));
489 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&forget_gate, lstm_params.forget_layer_norm_weights(), &forget_gate, 1, ConvertPolicy::SATURATE,
490 RoundingPolicy::TO_ZERO));
491 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&forget_gate, forget_gate_bias, &forget_gate, ConvertPolicy::SATURATE));
492 }
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100493 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&forget_gate, &forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
494
495 // Validate input gate
496 if(!lstm_params.has_cifg_opt())
497 {
498 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(),
499 lstm_params.recurrent_to_input_weights(),
500 lstm_params.input_gate_bias());
501 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_to_input_weights()->num_dimensions() > 2);
502 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.recurrent_to_input_weights()->num_dimensions() > 2);
503 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_gate_bias()->num_dimensions() > 1);
504
John Kesapides917959c2019-02-04 12:37:29 +0000505 std::vector<const ITensorInfo *> lstm_weights;
506 lstm_weights.emplace_back(lstm_params.input_to_input_weights());
507 lstm_weights.emplace_back(lstm_params.recurrent_to_input_weights());
Georgios Pinitas09f24972019-05-17 18:14:40 +0100508 TensorShape lstm_weights_concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(lstm_weights, 0);
509 TensorInfo lstm_gate_concat = TensorInfo(lstm_weights_concat_shape, 1, input->data_type());
510 ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate(lstm_weights, &lstm_gate_concat, Window::DimX));
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100511 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, lstm_params.input_to_input_weights(), (lstm_params.use_layer_norm()) ? nullptr : lstm_params.input_gate_bias(), &input_gate));
John Kesapides917959c2019-02-04 12:37:29 +0000512
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100513 if(lstm_params.has_peephole_opt())
514 {
515 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_input_weights());
516 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_input_weights()->num_dimensions() > 1);
517 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_input_weights(), &input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
518 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&input_gate, &input_gate, &input_gate, ConvertPolicy::SATURATE));
519 }
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100520
521 if(lstm_params.use_layer_norm())
522 {
523 ARM_COMPUTE_RETURN_ON_ERROR(NEMeanStdDevNormalizationLayer::validate(&input_gate));
524 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&input_gate, lstm_params.input_layer_norm_weights(), &input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
525 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&input_gate, lstm_params.input_gate_bias(), &input_gate, ConvertPolicy::SATURATE));
526 }
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100527 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&input_gate, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
528 }
529 else
530 {
531 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticSubtractionKernel::validate(&forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE));
532 }
533
534 // Validate cell state
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100535 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, input_to_cell_weights, (lstm_params.use_layer_norm()) ? nullptr : cell_bias, &cell_state_tmp));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100536 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMM::validate(output_state_in, &units_out_transposed_info, nullptr, &cell_state_tmp, 1.f, 0.f, GEMMInfo()));
537 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE));
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100538 if(lstm_params.use_layer_norm())
539 {
540 ARM_COMPUTE_RETURN_ON_ERROR(NEMeanStdDevNormalizationLayer::validate(&cell_state_tmp));
541 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, lstm_params.cell_layer_norm_weights(), &cell_state_tmp, 1, ConvertPolicy::SATURATE,
542 RoundingPolicy::TO_ZERO));
543 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&cell_state_tmp, cell_bias, &cell_state_tmp, ConvertPolicy::SATURATE));
544 }
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100545 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&cell_state_tmp, nullptr, activation_info));
546 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &input_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
547 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &forget_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
548 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE));
549 if(cell_threshold != 0.f)
550 {
551 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&cell_state_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold,
552 cell_threshold)));
553 }
554
555 // Validate output gate tmp
John Kesapides917959c2019-02-04 12:37:29 +0000556 std::vector<const ITensorInfo *> in_out_weights;
557 in_out_weights.emplace_back(input_to_output_weights);
558 in_out_weights.emplace_back(recurrent_to_output_weights);
Georgios Pinitas09f24972019-05-17 18:14:40 +0100559 TensorShape in_out_weights_concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(in_out_weights, 0);
560 TensorInfo in_out_gate_concat = TensorInfo(in_out_weights_concat_shape, 1, input->data_type());
561 ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate(in_out_weights, &in_out_gate_concat, Window::DimX));
John Kesapides917959c2019-02-04 12:37:29 +0000562
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100563 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, input_to_output_weights, (lstm_params.use_layer_norm()) ? nullptr : output_gate_bias, &output_gate_tmp));
John Kesapides917959c2019-02-04 12:37:29 +0000564
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100565 if(lstm_params.has_peephole_opt())
566 {
567 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, lstm_params.cell_to_output_weights(), &output_gate_tmp, 1, ConvertPolicy::SATURATE,
568 RoundingPolicy::TO_ZERO));
569 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&output_gate_tmp, &output_gate_tmp, &output_gate_tmp, ConvertPolicy::SATURATE));
570 }
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100571 if(lstm_params.use_layer_norm())
572 {
573 ARM_COMPUTE_RETURN_ON_ERROR(NEMeanStdDevNormalizationLayer::validate(&output_gate_tmp));
574 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&output_gate_tmp, lstm_params.output_layer_norm_weights(), &output_gate_tmp, 1, ConvertPolicy::SATURATE,
575 RoundingPolicy::TO_ZERO));
576 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&output_gate_tmp, output_gate_bias, &output_gate_tmp, ConvertPolicy::SATURATE));
577 }
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100578 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&output_gate_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
579
580 // Validate output state
581 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&cell_state_tmp, &cell_state_tmp, activation_info));
582 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &output_gate_tmp, &output_gate_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
583 if(lstm_params.has_projection())
584 {
585 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(&output_gate_tmp, lstm_params.projection_weights(), lstm_params.projection_bias(), output_state_out));
586 if(projection_threshold != 0.f)
587 {
588 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(output_state_out, output_state_out,
589 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold)));
590 }
591 }
592
593 // Validate copy kernel
594 ARM_COMPUTE_RETURN_ON_ERROR(NECopyKernel::validate(&cell_state_tmp, cell_state_out));
595 ARM_COMPUTE_RETURN_ON_ERROR(NECopyKernel::validate(output_state_out, output));
596
597 // Validate scratch concatenation
598 std::vector<ITensorInfo *> inputs_vector_info_raw;
Georgios Pinitas0cc37c32018-11-14 15:54:26 +0000599 if(!lstm_params.has_cifg_opt())
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100600 {
601 inputs_vector_info_raw.push_back(&input_gate);
602 }
603 inputs_vector_info_raw.push_back(&cell_state_tmp);
604 inputs_vector_info_raw.push_back(&forget_gate);
605 inputs_vector_info_raw.push_back(&output_gate_tmp);
606
Georgios Pinitas09f24972019-05-17 18:14:40 +0100607 ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate(inputs_vector_info_raw, scratch_buffer, Window::DimX));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100608 return Status{};
609}
610
611void NELSTMLayer::run()
612{
John Kesapides917959c2019-02-04 12:37:29 +0000613 prepare();
614
Georgios Pinitasda953f22019-04-02 17:27:03 +0100615 MemoryGroupResourceScope scope_mg(_memory_group);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100616
Michalis Spyrou2761c2f2019-03-22 13:06:08 +0000617 _concat_inputs_forget_gate.run();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100618 _fully_connected_forget_gate.run();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100619
620 if(_run_peephole_opt)
621 {
622 NEScheduler::get().schedule(&_pixelwise_mul_forget_gate, Window::DimY);
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100623 _accum_forget_gate1.run();
624 }
625 if(_is_layer_norm_lstm)
626 {
627 _mean_std_norm_forget_gate.run();
628 NEScheduler::get().schedule(&_pixelwise_mul_forget_gate_coeff, Window::DimY);
629 NEScheduler::get().schedule(&_accum_forget_gate_bias, Window::DimY);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100630 }
631 NEScheduler::get().schedule(&_activation_forget_gate, Window::DimY);
632
633 if(_run_cifg_opt)
634 {
635 if(_ones.info()->data_type() == DataType::F16)
636 {
637 std::fill_n(reinterpret_cast<half *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 1);
638 }
639 else
640 {
641 std::fill_n(reinterpret_cast<float *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 1);
642 }
643 NEScheduler::get().schedule(&_subtract_input_gate, Window::DimY);
644 }
645 else
646 {
647 _fully_connected_input_gate.run();
John Kesapides917959c2019-02-04 12:37:29 +0000648
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100649 if(_run_peephole_opt)
650 {
651 NEScheduler::get().schedule(&_pixelwise_mul_input_gate, Window::DimY);
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100652 _accum_input_gate1.run();
653 }
654
655 if(_is_layer_norm_lstm)
656 {
657 _mean_std_norm_input_gate.run();
658 NEScheduler::get().schedule(&_pixelwise_mul_input_gate_coeff, Window::DimY);
659 NEScheduler::get().schedule(&_accum_input_gate_bias, Window::DimY);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100660 }
661 NEScheduler::get().schedule(&_activation_input_gate, Window::DimY);
662 }
663
664 _fully_connected_cell_state.run();
665 NEScheduler::get().schedule(&_transpose_cell_state, Window::DimY);
666 _gemm_cell_state1.run();
667 NEScheduler::get().schedule(&_accum_cell_state1, Window::DimY);
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100668 if(_is_layer_norm_lstm)
669 {
670 _mean_std_norm_cell_gate.run();
671 NEScheduler::get().schedule(&_pixelwise_mul_cell_gate_coeff, Window::DimY);
672 NEScheduler::get().schedule(&_accum_cell_gate_bias, Window::DimY);
673 }
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100674 NEScheduler::get().schedule(&_activation_cell_state, Window::DimY);
675 NEScheduler::get().schedule(&_pixelwise_mul_cell_state1, Window::DimY);
676 NEScheduler::get().schedule(&_pixelwise_mul_cell_state2, Window::DimY);
677 NEScheduler::get().schedule(&_accum_cell_state2, Window::DimY);
678
679 if(_perform_cell_clipping)
680 {
681 NEScheduler::get().schedule(&_cell_clip, Window::DimY);
682 }
683
684 _fully_connected_output.run();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100685 if(_run_peephole_opt)
686 {
687 NEScheduler::get().schedule(&_pixelwise_mul_output_state1, Window::DimY);
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +0100688 _accum_output1.run();
689 }
690 if(_is_layer_norm_lstm)
691 {
692 _mean_std_norm_output_gate.run();
693 NEScheduler::get().schedule(&_pixelwise_mul_output_gate_coeff, Window::DimY);
694 NEScheduler::get().schedule(&_accum_output_gate_bias, Window::DimY);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100695 }
696 NEScheduler::get().schedule(&_activation_output, Window::DimY);
697
698 NEScheduler::get().schedule(&_activation_output_state, Window::DimY);
699 NEScheduler::get().schedule(&_pixelwise_mul_output_state2, Window::DimY);
700
701 if(_has_projection_weights)
702 {
703 _fully_connected_output_state.run();
704 if(_perform_projection_clipping)
705 {
706 NEScheduler::get().schedule(&_projection_clip, Window::DimY);
707 }
708 }
709
710 NEScheduler::get().schedule(&_copy_cell_state, Window::DimY);
711 NEScheduler::get().schedule(&_copy_output, Window::DimY);
712
713 _concat_scratch_buffer.run();
John Kesapides917959c2019-02-04 12:37:29 +0000714}
715
716void NELSTMLayer::prepare()
717{
718 if(!_is_prepared)
719 {
John Kesapides917959c2019-02-04 12:37:29 +0000720 _concat_weights_forget_gate.run();
721 if(!_run_cifg_opt)
722 {
723 _concat_weights_input_gate.run();
724 }
725 _concat_weights_output.run();
726 _is_prepared = true;
727 }
728}