blob: 5036126aea968f5ee1d4e6d08c069be794b94b90 [file] [log] [blame]
Michalis Spyroubcedf512018-03-22 14:55:08 +00001/*
Sheri Zhang7e20e292021-02-02 11:49:34 +00002 * Copyright (c) 2018-2021 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#include "arm_compute/runtime/CL/functions/CLLSTMLayer.h"
25
Michalis Spyroubcedf512018-03-22 14:55:08 +000026#include "arm_compute/core/Utils.h"
27#include "arm_compute/core/Validate.h"
Michele Di Giorgio47a89902020-03-09 19:32:33 +000028#include "arm_compute/core/utils/misc/InfoHelpers.h"
Michalis Spyroubcedf512018-03-22 14:55:08 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
30#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
31#include "arm_compute/runtime/CL/CLScheduler.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010032#include "src/core/CL/kernels/CLDepthConvertLayerKernel.h"
33#include "src/core/CL/kernels/CLFillBorderKernel.h"
34#include "src/core/CL/kernels/CLGEMMLowpMatrixMultiplyNativeKernel.h"
35#include "src/core/CL/kernels/CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel.h"
36#include "src/core/CL/kernels/CLGEMMLowpOffsetContributionKernel.h"
37#include "src/core/CL/kernels/CLGEMMLowpOffsetContributionOutputStageKernel.h"
38#include "src/core/CL/kernels/CLGEMMLowpReductionKernel.h"
39#include "src/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h"
40#include "src/core/CL/kernels/CLGEMMMatrixMultiplyReshapedKernel.h"
41#include "src/core/CL/kernels/CLGEMMMatrixMultiplyReshapedOnlyRHSKernel.h"
42#include "src/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h"
43#include "src/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010044#include "src/core/CL/kernels/CLTransposeKernel.h"
Michalis Spyroubcedf512018-03-22 14:55:08 +000045
Michele Di Giorgio47a89902020-03-09 19:32:33 +000046namespace arm_compute
47{
Michalis Spyroubcedf512018-03-22 14:55:08 +000048using namespace arm_compute::misc::shape_calculator;
Michele Di Giorgio47a89902020-03-09 19:32:33 +000049using namespace arm_compute::utils::info_helpers;
Michalis Spyroubcedf512018-03-22 14:55:08 +000050
51CLLSTMLayer::CLLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager)
Michele Di Giorgio39438b42019-06-04 12:41:45 +010052 : _memory_group(std::move(memory_manager)), _fully_connected_input_gate(), _accum_input_gate1(), _subtract_input_gate(), _pixelwise_mul_input_gate(), _activation_input_gate(),
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010053 _fully_connected_forget_gate(), _accum_forget_gate1(), _pixelwise_mul_forget_gate(), _activation_forget_gate(), _fully_connected_cell_state(), _gemm_cell_state1(),
Georgios Pinitas40f51a62020-11-21 03:04:18 +000054 _transpose_cell_state(std::make_unique<CLTransposeKernel>()), _accum_cell_state1(), _accum_cell_state2(), _pixelwise_mul_cell_state1(), _activation_cell_state(), _cell_clip(),
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010055 _pixelwise_mul_cell_state2(), _fully_connected_output(), _pixelwise_mul_output_state1(), _accum_output1(), _activation_output(), _activation_output_state(), _pixelwise_mul_output_state2(),
Sheri Zhang7e20e292021-02-02 11:49:34 +000056 _fully_connected_output_state(), _projection_clip(), _copy_cell_state(), _copy_output(), _concat_scratch_buffer(), _concat_inputs_forget_gate(), _concat_weights_forget_gate(),
57 _concat_weights_input_gate(), _concat_weights_output(), _ones_fill(), _mean_std_norm_input_gate(), _pixelwise_mul_input_gate_coeff(), _accum_input_gate_bias(), _mean_std_norm_forget_gate(),
58 _pixelwise_mul_forget_gate_coeff(), _accum_forget_gate_bias(), _mean_std_norm_cell_gate(), _pixelwise_mul_cell_gate_coeff(), _accum_cell_gate_bias(), _mean_std_norm_output_gate(),
59 _pixelwise_mul_output_gate_coeff(), _accum_output_gate_bias(), _input_gate_out1(), _input_gate_out2(), _input_gate_out3(), _input_gate_out4(), _forget_gate_out1(), _forget_gate_out2(),
60 _forget_gate_out3(), _forget_gate_out4(), _forget_gate_out5(), _forget_gate_out6(), _cell_state_out1(), _cell_state_out2(), _cell_state_out3(), _cell_state_out4(), _cell_state_out5(), _output1(),
61 _output2(), _output3(), _output4(), _cell_state_activation(), _output_state1(), _ones(), _input_layer_norm_out1(), _input_layer_norm_out2(), _forget_layer_norm_out1(), _forget_layer_norm_out2(),
62 _cell_layer_norm_out1(), _cell_layer_norm_out2(), _output_layer_norm_out1(), _output_layer_norm_out2(), _run_peephole_opt(false), _run_cifg_opt(false), _perform_cell_clipping(false),
63 _has_projection_weights(false), _perform_projection_clipping(false), _is_prepared(false), _is_layer_norm_lstm(false)
Michalis Spyroubcedf512018-03-22 14:55:08 +000064{
65}
66
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010067CLLSTMLayer::~CLLSTMLayer() = default;
68
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010069void CLLSTMLayer::configure(const ICLTensor *input,
70 const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
Michalis Spyroubcedf512018-03-22 14:55:08 +000071 const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
72 const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
Michalis Spyrou1009e872020-07-27 12:48:34 +010073 const ICLTensor *output_state_in, ICLTensor *cell_state_in,
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010074 ICLTensor *scratch_buffer, ICLTensor *output_state_out, ICLTensor *cell_state_out, ICLTensor *output,
75 const LSTMParams<ICLTensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold)
Michalis Spyroubcedf512018-03-22 14:55:08 +000076{
Manuel Bottini2b84be52020-04-08 10:15:51 +010077 configure(CLKernelLibrary::get().get_compile_context(), input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights, recurrent_to_forget_weights, recurrent_to_cell_weights,
78 recurrent_to_output_weights, forget_gate_bias, cell_bias, output_gate_bias, output_state_in, cell_state_in, scratch_buffer, output_state_out, cell_state_out, output, lstm_params, activation_info,
79 cell_threshold, projection_threshold);
80}
81
82void CLLSTMLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input,
83 const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
84 const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
85 const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
Michalis Spyrou1009e872020-07-27 12:48:34 +010086 const ICLTensor *output_state_in, ICLTensor *cell_state_in,
Manuel Bottini2b84be52020-04-08 10:15:51 +010087 ICLTensor *scratch_buffer, ICLTensor *output_state_out, ICLTensor *cell_state_out, ICLTensor *output,
88 const LSTMParams<ICLTensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold)
89{
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010090 ARM_COMPUTE_ERROR_ON_NULLPTR(input,
91 input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
92 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
93 forget_gate_bias, cell_bias, output_gate_bias,
94 output_state_in, cell_state_in,
95 scratch_buffer, output_state_out, cell_state_out, output);
96
Michele Di Giorgio39438b42019-06-04 12:41:45 +010097 _is_layer_norm_lstm = lstm_params.use_layer_norm();
98
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010099 // Set lstm parameters
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000100 LSTMParams<ITensorInfo> lstm_params_info{};
101 build_lstm_params_tensor_info(lstm_params, &lstm_params_info);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100102
103 // Validate
Michalis Spyroubcedf512018-03-22 14:55:08 +0000104 ARM_COMPUTE_ERROR_THROW_ON(CLLSTMLayer::validate(input->info(), input_to_forget_weights->info(),
105 input_to_cell_weights->info(), input_to_output_weights->info(),
106 recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(),
107 forget_gate_bias->info(), cell_bias->info(), output_gate_bias->info(),
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100108 output_state_in->info(), cell_state_in->info(),
109 scratch_buffer->info(), output_state_out->info(), cell_state_out->info(), output->info(),
110 lstm_params_info, activation_info, cell_threshold, projection_threshold));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000111
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100112 const TensorShape cell_state_shape = cell_state_in->info()->tensor_shape();
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100113 // Configure block that calculates the forget gate
114 // 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 Kesapidescafec8f2019-02-19 15:53:59 +0000115 // We optimize this as follows:
116 // 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 Spyroubcedf512018-03-22 14:55:08 +0000117 _forget_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000118 _forget_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas42a31722018-07-09 14:35:32 +0100119 _forget_gate_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000120
John Kesapidescafec8f2019-02-19 15:53:59 +0000121 std::vector<const ICLTensor *> inputs_vector;
122 inputs_vector.emplace_back(input);
123 inputs_vector.emplace_back(output_state_in);
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +0100124 const TensorShape concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, 0);
John Kesapidescafec8f2019-02-19 15:53:59 +0000125 _forget_gate_out2.allocator()->init(TensorInfo(concat_shape, 1, input->info()->data_type()));
126
Michalis Spyroubcedf512018-03-22 14:55:08 +0000127 _memory_group.manage(&_forget_gate_out2);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100128 _concat_inputs_forget_gate.configure(compile_context, inputs_vector, &_forget_gate_out2, Window::DimX);
John Kesapidescafec8f2019-02-19 15:53:59 +0000129
130 std::vector<const ICLTensor *> weights_vector;
131
132 weights_vector.emplace_back(input_to_forget_weights);
133 weights_vector.emplace_back(recurrent_to_forget_weights);
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +0100134 const TensorShape weights_concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(weights_vector, 0);
John Kesapidescafec8f2019-02-19 15:53:59 +0000135 _forget_gate_out6.allocator()->init(TensorInfo(weights_concat_shape, 1, input->info()->data_type()));
136
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100137 _concat_weights_forget_gate.configure(compile_context, weights_vector, &_forget_gate_out6, Window::DimX);
John Kesapidescafec8f2019-02-19 15:53:59 +0000138
Georgios Pinitas42a31722018-07-09 14:35:32 +0100139 _memory_group.manage(&_forget_gate_out5);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100140 _fully_connected_forget_gate.configure(compile_context, &_forget_gate_out2, &_forget_gate_out6, (_is_layer_norm_lstm) ? nullptr : forget_gate_bias, &_forget_gate_out5);
John Kesapidescafec8f2019-02-19 15:53:59 +0000141 _memory_group.manage(&_forget_gate_out1);
142 _memory_group.manage(&_forget_gate_out3);
143 _forget_gate_out6.allocator()->allocate();
144
Georgios Pinitas42a31722018-07-09 14:35:32 +0100145 CLTensor *forget_gate_out = &_forget_gate_out5;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000146 if(lstm_params.has_peephole_opt())
147 {
Georgios Pinitas42a31722018-07-09 14:35:32 +0100148 _forget_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000149
150 _run_peephole_opt = true;
151 _memory_group.manage(&_forget_gate_out4);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100152 _pixelwise_mul_forget_gate.configure(compile_context, cell_state_in, lstm_params.cell_to_forget_weights(), &_forget_gate_out4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
153 _accum_forget_gate1.configure(compile_context, &_forget_gate_out5, &_forget_gate_out4, &_forget_gate_out3, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000154 _forget_gate_out4.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000155 _forget_gate_out5.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000156 forget_gate_out = &_forget_gate_out3;
157 }
158 else
159 {
160 _forget_gate_out3.allocator()->allocate();
161 }
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100162 if(_is_layer_norm_lstm)
163 {
164 _forget_layer_norm_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
165 _forget_layer_norm_out2.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
166 _memory_group.manage(&_forget_layer_norm_out1);
167 _memory_group.manage(&_forget_layer_norm_out2);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100168 _mean_std_norm_forget_gate.configure(compile_context, forget_gate_out);
169 _pixelwise_mul_forget_gate_coeff.configure(compile_context, forget_gate_out, lstm_params.forget_layer_norm_weights(), &_forget_layer_norm_out1, 1, ConvertPolicy::SATURATE,
170 RoundingPolicy::TO_NEAREST_EVEN);
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100171 // forget_gate_out is going to be reassigned, so allocate the tensor that it was assigned to before
172 forget_gate_out->allocator()->allocate();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100173 _accum_forget_gate_bias.configure(compile_context, &_forget_layer_norm_out1, forget_gate_bias, &_forget_layer_norm_out2, ConvertPolicy::SATURATE);
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100174 _forget_layer_norm_out1.allocator()->allocate();
175 forget_gate_out = &_forget_layer_norm_out2;
176 }
Manuel Bottini2b84be52020-04-08 10:15:51 +0100177 _activation_forget_gate.configure(compile_context, forget_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000178
Michalis Spyroubcedf512018-03-22 14:55:08 +0000179 // Configure block that calculates the input gate
Georgios Pinitas42a31722018-07-09 14:35:32 +0100180 // 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
Michalis Spyroubcedf512018-03-22 14:55:08 +0000181 // input_gate = 1 - forget_gate, with CIFG
John Kesapidescafec8f2019-02-19 15:53:59 +0000182 // We optimize this as follows:
183 // 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
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100184 _input_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas4f859822019-02-06 18:08:04 +0000185 CLTensor *input_gate_out = &_input_gate_out1;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000186 if(lstm_params.has_cifg_opt())
187 {
188 _memory_group.manage(&_input_gate_out1);
189 _ones.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Sheri Zhang7e20e292021-02-02 11:49:34 +0000190 _ones_fill.configure(compile_context, &_ones, PixelValue(1, _ones.info()->data_type()));
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100191 _subtract_input_gate.configure(compile_context, &_ones, forget_gate_out, &_input_gate_out1, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000192 _ones.allocator()->allocate();
193 _run_cifg_opt = true;
194 }
195 else
196 {
Michalis Spyroubcedf512018-03-22 14:55:08 +0000197 _input_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas42a31722018-07-09 14:35:32 +0100198 _input_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
John Kesapidescafec8f2019-02-19 15:53:59 +0000199
200 std::vector<const ICLTensor *> lstm_weights;
201 lstm_weights.emplace_back(lstm_params.input_to_input_weights());
202 lstm_weights.emplace_back(lstm_params.recurrent_to_input_weights());
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +0100203 TensorShape lstm_weights_concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(lstm_weights, 0);
John Kesapidescafec8f2019-02-19 15:53:59 +0000204 _input_gate_out2.allocator()->init(TensorInfo(lstm_weights_concat_shape, 1, input->info()->data_type()));
205
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100206 _concat_weights_input_gate.configure(compile_context, lstm_weights, &_input_gate_out2, Window::DimX);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000207
208 _memory_group.manage(&_input_gate_out1);
John Kesapidescafec8f2019-02-19 15:53:59 +0000209
Michalis Spyroubcedf512018-03-22 14:55:08 +0000210 _memory_group.manage(&_input_gate_out3);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100211 _fully_connected_input_gate.configure(compile_context, &_forget_gate_out2, &_input_gate_out2, (_is_layer_norm_lstm) ? nullptr : lstm_params.input_gate_bias(), &_input_gate_out3);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000212 _input_gate_out2.allocator()->allocate();
John Kesapidescafec8f2019-02-19 15:53:59 +0000213
214 input_gate_out = &_input_gate_out3;
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100215 if(_run_peephole_opt)
216 {
John Kesapidescafec8f2019-02-19 15:53:59 +0000217 _memory_group.manage(&_input_gate_out4);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100218 _pixelwise_mul_input_gate.configure(compile_context, cell_state_in, lstm_params.cell_to_input_weights(), &_input_gate_out4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
219 _accum_input_gate1.configure(compile_context, &_input_gate_out3, &_input_gate_out4, &_input_gate_out1, ConvertPolicy::SATURATE);
John Kesapidescafec8f2019-02-19 15:53:59 +0000220 _input_gate_out3.allocator()->allocate();
Georgios Pinitas4f859822019-02-06 18:08:04 +0000221 _input_gate_out4.allocator()->allocate();
Georgios Pinitas4f859822019-02-06 18:08:04 +0000222 input_gate_out = &_input_gate_out1;
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100223 }
Georgios Pinitas4f859822019-02-06 18:08:04 +0000224 else
225 {
226 _input_gate_out1.allocator()->allocate();
227 }
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100228
229 if(_is_layer_norm_lstm)
230 {
231 _input_layer_norm_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
232 _input_layer_norm_out2.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
233 _memory_group.manage(&_input_layer_norm_out1);
234 _memory_group.manage(&_input_layer_norm_out2);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100235 _mean_std_norm_input_gate.configure(compile_context, input_gate_out);
236 _pixelwise_mul_input_gate_coeff.configure(compile_context, input_gate_out, lstm_params.input_layer_norm_weights(), &_input_layer_norm_out1, 1, ConvertPolicy::SATURATE,
237 RoundingPolicy::TO_NEAREST_EVEN);
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100238 // input_gate_out is going to be reassigned, so allocate the tensor that it was assigned to before
239 input_gate_out->allocator()->allocate();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100240 _accum_input_gate_bias.configure(compile_context, &_input_layer_norm_out1, lstm_params.input_gate_bias(), &_input_layer_norm_out2, ConvertPolicy::SATURATE);
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100241 _input_layer_norm_out1.allocator()->allocate();
242 input_gate_out = &_input_layer_norm_out2;
243 }
Manuel Bottini2b84be52020-04-08 10:15:51 +0100244 _activation_input_gate.configure(compile_context, input_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000245 }
246
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100247 // Configure block that calculates the cell state
248 // 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)
Michalis Spyroubcedf512018-03-22 14:55:08 +0000249 TensorShape cell_state1_shape = compute_transposed_shape(*recurrent_to_output_weights->info());
250 _cell_state_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
251 _cell_state_out2.allocator()->init(TensorInfo(cell_state1_shape, 1, input->info()->data_type()));
252 _cell_state_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
253 _cell_state_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
254 _cell_state_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
255
Michalis Spyroubcedf512018-03-22 14:55:08 +0000256 _memory_group.manage(&_cell_state_out1);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100257 _fully_connected_cell_state.configure(compile_context, input, input_to_cell_weights, (_is_layer_norm_lstm) ? nullptr : cell_bias, &_cell_state_out1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000258 _memory_group.manage(&_cell_state_out2);
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100259 _transpose_cell_state->configure(compile_context, recurrent_to_cell_weights, &_cell_state_out2);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000260 _memory_group.manage(&_cell_state_out3);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100261 _gemm_cell_state1.configure(compile_context, output_state_in, &_cell_state_out2, nullptr, &_cell_state_out3, 1.f, 0.f);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000262 _cell_state_out2.allocator()->allocate();
263 _memory_group.manage(&_cell_state_out4);
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100264 _accum_cell_state1.configure(compile_context, &_cell_state_out1, &_cell_state_out3, &_cell_state_out4, ConvertPolicy::SATURATE);
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100265 CLTensor *cell_state_out_ptr = &_cell_state_out4;
266 if(_is_layer_norm_lstm)
267 {
268 _cell_layer_norm_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
269 _cell_layer_norm_out2.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
270 _memory_group.manage(&_cell_layer_norm_out1);
271 _memory_group.manage(&_cell_layer_norm_out2);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100272 _mean_std_norm_cell_gate.configure(compile_context, cell_state_out_ptr);
273 _pixelwise_mul_cell_gate_coeff.configure(compile_context, cell_state_out_ptr, lstm_params.cell_layer_norm_weights(), &_cell_layer_norm_out1, 1, ConvertPolicy::SATURATE,
274 RoundingPolicy::TO_NEAREST_EVEN);
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100275 // cell_state_out_ptr is going to be reassigned, so allocate the tensor that it was assigned to before
276 cell_state_out_ptr->allocator()->allocate();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100277 _accum_cell_gate_bias.configure(compile_context, &_cell_layer_norm_out1, cell_bias, &_cell_layer_norm_out2, ConvertPolicy::SATURATE);
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100278 _cell_layer_norm_out1.allocator()->allocate();
279 cell_state_out_ptr = &_cell_layer_norm_out2;
280 }
Manuel Bottini2b84be52020-04-08 10:15:51 +0100281 _activation_cell_state.configure(compile_context, cell_state_out_ptr, nullptr, activation_info);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000282 _memory_group.manage(&_cell_state_out5);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100283 _pixelwise_mul_cell_state1.configure(compile_context, cell_state_out_ptr, input_gate_out, &_cell_state_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100284 cell_state_out_ptr->allocator()->allocate();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100285 _pixelwise_mul_cell_state2.configure(compile_context, forget_gate_out, cell_state_in, &_cell_state_out3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100286 _accum_cell_state2.configure(compile_context, &_cell_state_out5, &_cell_state_out3, &_cell_state_out1, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000287 _cell_state_out3.allocator()->allocate();
288 _cell_state_out5.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000289 // Perform clipping
290 if(cell_threshold != 0.f)
291 {
292 _perform_cell_clipping = true;
Manuel Bottini2b84be52020-04-08 10:15:51 +0100293 _cell_clip.configure(compile_context, &_cell_state_out1, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, cell_threshold));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000294 }
295
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100296 // Configure block that calculates the output
297 // 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 Kesapidescafec8f2019-02-19 15:53:59 +0000298 // We optimize this as follows:
299 // 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 Spyroubcedf512018-03-22 14:55:08 +0000300 _output1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
John Kesapidescafec8f2019-02-19 15:53:59 +0000301 _output4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
302 std::vector<const ICLTensor *> in_out_weights;
303 in_out_weights.emplace_back(input_to_output_weights);
304 in_out_weights.emplace_back(recurrent_to_output_weights);
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +0100305 TensorShape in_out_weights_concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(in_out_weights, 0);
John Kesapidescafec8f2019-02-19 15:53:59 +0000306 _output2.allocator()->init(TensorInfo(in_out_weights_concat_shape, 1, input->info()->data_type()));
307
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100308 _concat_weights_output.configure(compile_context, in_out_weights, &_output2, Window::DimX);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000309
Michalis Spyroubcedf512018-03-22 14:55:08 +0000310 _memory_group.manage(&_output1);
John Kesapidescafec8f2019-02-19 15:53:59 +0000311 _memory_group.manage(&_output4);
312
Manuel Bottini2b84be52020-04-08 10:15:51 +0100313 _fully_connected_output.configure(compile_context, &_forget_gate_out2, &_output2, (_is_layer_norm_lstm) ? nullptr : output_gate_bias, &_output4);
John Kesapidescafec8f2019-02-19 15:53:59 +0000314
Michalis Spyroubcedf512018-03-22 14:55:08 +0000315 _output2.allocator()->allocate();
John Kesapidescafec8f2019-02-19 15:53:59 +0000316 _forget_gate_out2.allocator()->allocate();
317
318 CLTensor *output_gate_out = &_output4;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000319 if(lstm_params.has_peephole_opt())
320 {
John Kesapidescafec8f2019-02-19 15:53:59 +0000321 _output3.allocator()->init(TensorInfo(_cell_state_out1.info()->tensor_shape(), 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000322
John Kesapidescafec8f2019-02-19 15:53:59 +0000323 _memory_group.manage(&_output3);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100324 _pixelwise_mul_output_state1.configure(compile_context, &_cell_state_out1, lstm_params.cell_to_output_weights(), &_output3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
325 _accum_output1.configure(compile_context, &_output4, &_output3, &_output1, ConvertPolicy::SATURATE);
John Kesapidescafec8f2019-02-19 15:53:59 +0000326 _output4.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000327 output_gate_out = &_output1;
328
329 // Allocate intermediate buffers
John Kesapidescafec8f2019-02-19 15:53:59 +0000330 _output3.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000331 }
332 else
333 {
334 _output1.allocator()->allocate();
335 }
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100336 if(_is_layer_norm_lstm)
337 {
338 _output_layer_norm_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
339 _output_layer_norm_out2.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
340 _memory_group.manage(&_output_layer_norm_out1);
341 _memory_group.manage(&_output_layer_norm_out2);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100342 _mean_std_norm_output_gate.configure(compile_context, output_gate_out);
343 _pixelwise_mul_output_gate_coeff.configure(compile_context, output_gate_out, lstm_params.output_layer_norm_weights(), &_output_layer_norm_out1, 1, ConvertPolicy::SATURATE,
344 RoundingPolicy::TO_NEAREST_EVEN);
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100345 // output_gate_out is going to be reassigned, so allocate the tensor that it was assigned to before
346 output_gate_out->allocator()->allocate();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100347 _accum_output_gate_bias.configure(compile_context, &_output_layer_norm_out1, output_gate_bias, &_output_layer_norm_out2, ConvertPolicy::SATURATE);
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100348 _output_layer_norm_out1.allocator()->allocate();
349 output_gate_out = &_output_layer_norm_out2;
350 }
Manuel Bottini2b84be52020-04-08 10:15:51 +0100351 _activation_output.configure(compile_context, output_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000352
Michalis Spyroubcedf512018-03-22 14:55:08 +0000353 // Configure block that calculates the output state
354 /** lstm_res = PixelwiseMul(output, Activation(cell_state))
355 *
356 * -- Clip(lstm_res * projection_weights + projection_bias, projection_threshold) , if there is a projection
357 * /
358 * output_state = --
359 * \
360 * -- lstm_res , otherwise
361 */
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100362 ICLTensor *output_state_out_tmp = lstm_params.has_projection() ? &_output_state1 : output_state_out;
363 _cell_state_activation.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
364 _output_state1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
365
Michalis Spyroubcedf512018-03-22 14:55:08 +0000366 _memory_group.manage(&_cell_state_activation);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100367 _activation_output_state.configure(compile_context, &_cell_state_out1, &_cell_state_activation, activation_info);
368 _pixelwise_mul_output_state2.configure(compile_context, &_cell_state_activation, output_gate_out, output_state_out_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000369 _cell_state_activation.allocator()->allocate();
370
371 if(lstm_params.has_projection())
372 {
373 _has_projection_weights = true;
Manuel Bottini2b84be52020-04-08 10:15:51 +0100374 _fully_connected_output_state.configure(compile_context, output_state_out_tmp, lstm_params.projection_weights(), lstm_params.projection_bias(), output_state_out);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100375 _output_state1.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000376 // Perform clipping
377 if(projection_threshold != 0.f)
378 {
379 _perform_projection_clipping = true;
Manuel Bottini2b84be52020-04-08 10:15:51 +0100380 _projection_clip.configure(compile_context, output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000381 }
Michalis Spyroubcedf512018-03-22 14:55:08 +0000382 }
383
384 // Copy cell state and output
Sheri Zhang7e20e292021-02-02 11:49:34 +0000385 _copy_cell_state.configure(compile_context, &_cell_state_out1, cell_state_out);
386 _copy_output.configure(compile_context, output_state_out, output);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000387
388 // Vector for holding the tensors to store in scratch buffer
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100389 std::vector<const ICLTensor *> scratch_inputs;
Georgios Pinitas0cc37c32018-11-14 15:54:26 +0000390 if(!lstm_params.has_cifg_opt())
Michalis Spyroubcedf512018-03-22 14:55:08 +0000391 {
Georgios Pinitas4f859822019-02-06 18:08:04 +0000392 scratch_inputs.emplace_back(input_gate_out);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000393 }
394 scratch_inputs.emplace_back(&_cell_state_out1);
395 scratch_inputs.emplace_back(forget_gate_out);
396 scratch_inputs.emplace_back(output_gate_out);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100397 _concat_scratch_buffer.configure(compile_context, scratch_inputs, scratch_buffer, Window::DimX);
Georgios Pinitas4f859822019-02-06 18:08:04 +0000398 input_gate_out->allocator()->allocate();
Michele Di Giorgiodd2619a2018-11-05 16:46:09 +0000399 _cell_state_out1.allocator()->allocate();
400 forget_gate_out->allocator()->allocate();
401 output_gate_out->allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000402}
403
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100404Status CLLSTMLayer::validate(const ITensorInfo *input,
405 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
Michalis Spyroubcedf512018-03-22 14:55:08 +0000406 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
407 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100408 const ITensorInfo *output_state_in, const ITensorInfo *cell_state_in,
409 const ITensorInfo *scratch_buffer, const ITensorInfo *output_state_out, const ITensorInfo *cell_state_out, const ITensorInfo *output,
Michalis Spyroubcedf512018-03-22 14:55:08 +0000410 const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold)
411{
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100412 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input,
413 input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
414 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
415 forget_gate_bias, cell_bias, output_gate_bias,
416 output_state_in, cell_state_in,
417 scratch_buffer, output_state_out, cell_state_out, output);
418
419 // Check data types
Michalis Spyroubcedf512018-03-22 14:55:08 +0000420 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100421 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input,
422 input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
423 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
424 forget_gate_bias, cell_bias, output_gate_bias,
425 output_state_in, cell_state_in,
426 scratch_buffer, output_state_out, cell_state_out, output);
427
428 // Check dimensions
Georgios Pinitas42447c12018-07-16 17:01:20 +0100429 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2);
430 ARM_COMPUTE_RETURN_ERROR_ON(input_to_forget_weights->num_dimensions() > 2);
431 ARM_COMPUTE_RETURN_ERROR_ON(input_to_cell_weights->num_dimensions() > 2);
432 ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->num_dimensions() > 2);
433 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_forget_weights->num_dimensions() > 2);
434 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_cell_weights->num_dimensions() > 2);
435 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->num_dimensions() > 2);
436 ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->num_dimensions() > 1);
437 ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->num_dimensions() > 1);
438 ARM_COMPUTE_RETURN_ERROR_ON(output_gate_bias->num_dimensions() > 1);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100439 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() > 2);
440 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->num_dimensions() > 2);
Georgios Pinitas42447c12018-07-16 17:01:20 +0100441 ARM_COMPUTE_RETURN_ERROR_ON(scratch_buffer->num_dimensions() > 2);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100442 ARM_COMPUTE_RETURN_ERROR_ON(output_state_out->num_dimensions() > 2);
443 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_out->num_dimensions() > 2);
Georgios Pinitas42447c12018-07-16 17:01:20 +0100444 ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 2);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100445 ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->dimension(0) * 4 != scratch_buffer->dimension(0)
446 && cell_bias->dimension(0) * 3 != scratch_buffer->dimension(0));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000447
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100448 const unsigned int num_batches = input->dimension(1);
449 const unsigned int num_cells = input_to_output_weights->dimension(1);
450
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100451 if(lstm_params.use_layer_norm())
452 {
453 // If CIFG is used, input layer normalization weights tensor is omitted
454 if(lstm_params.has_cifg_opt())
455 {
456 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_layer_norm_weights() != nullptr);
457 }
458 else
459 {
460 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_layer_norm_weights());
461 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_layer_norm_weights()->num_dimensions() > 1);
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100462 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_layer_norm_weights()->dimension(0) != num_cells);
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100463 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, lstm_params.input_layer_norm_weights());
464 }
465
466 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.forget_layer_norm_weights(), lstm_params.cell_layer_norm_weights(), lstm_params.output_layer_norm_weights());
467 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());
468 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.forget_layer_norm_weights()->num_dimensions() > 1);
469 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_layer_norm_weights()->num_dimensions() > 1);
470 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.output_layer_norm_weights()->num_dimensions() > 1);
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100471 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.forget_layer_norm_weights()->dimension(0) != num_cells);
472 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_layer_norm_weights()->dimension(0) != num_cells);
473 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.output_layer_norm_weights()->dimension(0) != num_cells);
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100474 }
475
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100476 // Check peephole optimization
Michalis Spyroubcedf512018-03-22 14:55:08 +0000477 if(lstm_params.has_peephole_opt())
478 {
Michalis Spyrou09daf4d2018-06-28 17:07:22 +0100479 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_output_weights(), lstm_params.cell_to_forget_weights());
Georgios Pinitas42447c12018-07-16 17:01:20 +0100480 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->num_dimensions() > 1);
481 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_output_weights()->num_dimensions() > 1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000482 }
483
484 TensorShape units_out_transposed_shape = compute_transposed_shape(*recurrent_to_output_weights);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000485 TensorShape num_units_transposed_shape = compute_transposed_shape(*forget_gate_bias);
486 const TensorInfo units_out_transposed_info = TensorInfo(units_out_transposed_shape, 1, input->data_type());
Michalis Spyroubcedf512018-03-22 14:55:08 +0000487 const TensorInfo num_units_transposed_info = TensorInfo(num_units_transposed_shape, 1, input->data_type());
488
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100489 TensorInfo input_gate = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
490 TensorInfo forget_gate = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
491 TensorInfo output_gate_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
492 TensorInfo cell_state_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
493
Michalis Spyroubcedf512018-03-22 14:55:08 +0000494 // Validate forget gate
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100495 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_forget_weights, (lstm_params.use_layer_norm()) ? nullptr : forget_gate_bias, &forget_gate));
John Kesapidescafec8f2019-02-19 15:53:59 +0000496
497 std::vector<const ITensorInfo *> inputs_vector;
498 inputs_vector.emplace_back(input);
499 inputs_vector.emplace_back(output_state_in);
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +0100500 const TensorShape concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, 0);
John Kesapidescafec8f2019-02-19 15:53:59 +0000501 TensorInfo forget_gate_concat = TensorInfo(concat_shape, 1, input->data_type());
502
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100503 ARM_COMPUTE_RETURN_ON_ERROR(CLConcatenateLayer::validate(inputs_vector, &forget_gate_concat, Window::DimX));
John Kesapidescafec8f2019-02-19 15:53:59 +0000504
Michalis Spyroubcedf512018-03-22 14:55:08 +0000505 if(lstm_params.has_peephole_opt())
506 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100507 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(cell_state_in, lstm_params.cell_to_forget_weights(), &forget_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN));
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100508 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000509 }
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100510 if(lstm_params.use_layer_norm())
511 {
512 ARM_COMPUTE_RETURN_ON_ERROR(CLMeanStdDevNormalizationLayer::validate(&forget_gate));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100513 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&forget_gate, lstm_params.forget_layer_norm_weights(), &forget_gate, 1, ConvertPolicy::SATURATE,
514 RoundingPolicy::TO_NEAREST_EVEN));
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100515 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&forget_gate, forget_gate_bias, &forget_gate, ConvertPolicy::SATURATE));
516 }
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100517 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&forget_gate, &forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000518
519 // Validate input gate
520 if(!lstm_params.has_cifg_opt())
521 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100522 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(),
523 lstm_params.recurrent_to_input_weights(),
524 lstm_params.input_gate_bias());
Georgios Pinitas42447c12018-07-16 17:01:20 +0100525 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_to_input_weights()->num_dimensions() > 2);
526 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.recurrent_to_input_weights()->num_dimensions() > 2);
Georgios Pinitas42447c12018-07-16 17:01:20 +0100527 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_gate_bias()->num_dimensions() > 1);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100528
John Kesapidescafec8f2019-02-19 15:53:59 +0000529 std::vector<const ITensorInfo *> lstm_weights;
530 lstm_weights.emplace_back(lstm_params.input_to_input_weights());
531 lstm_weights.emplace_back(lstm_params.recurrent_to_input_weights());
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +0100532 TensorShape lstm_weights_concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(lstm_weights, 0);
John Kesapidescafec8f2019-02-19 15:53:59 +0000533 TensorInfo lstm_gate_concat = TensorInfo(lstm_weights_concat_shape, 1, input->data_type());
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100534 ARM_COMPUTE_RETURN_ON_ERROR(CLConcatenateLayer::validate(lstm_weights, &lstm_gate_concat, Window::DimX));
John Kesapidescafec8f2019-02-19 15:53:59 +0000535
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100536 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, lstm_params.input_to_input_weights(), (lstm_params.use_layer_norm()) ? nullptr : lstm_params.input_gate_bias(), &input_gate));
John Kesapidescafec8f2019-02-19 15:53:59 +0000537
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100538 if(lstm_params.has_peephole_opt())
539 {
540 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_input_weights());
541 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_input_weights()->num_dimensions() > 1);
Michalis Spyrou1009e872020-07-27 12:48:34 +0100542 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(cell_state_in, lstm_params.cell_to_input_weights(), &input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN));
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100543 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&input_gate, &input_gate, &input_gate, ConvertPolicy::SATURATE));
544 }
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100545
546 if(lstm_params.use_layer_norm())
547 {
548 ARM_COMPUTE_RETURN_ON_ERROR(CLMeanStdDevNormalizationLayer::validate(&input_gate));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100549 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&input_gate, lstm_params.input_layer_norm_weights(), &input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN));
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100550 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&input_gate, lstm_params.input_gate_bias(), &input_gate, ConvertPolicy::SATURATE));
551 }
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100552 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&input_gate, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000553 }
554 else
555 {
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100556 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticSubtraction::validate(&forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000557 }
558
559 // Validate cell state
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100560 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_cell_weights, (lstm_params.use_layer_norm()) ? nullptr : cell_bias, &cell_state_tmp));
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100561 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(output_state_in, &units_out_transposed_info, nullptr, &cell_state_tmp, 1.f, 0.f, GEMMInfo()));
562 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE));
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100563 if(lstm_params.use_layer_norm())
564 {
565 ARM_COMPUTE_RETURN_ON_ERROR(CLMeanStdDevNormalizationLayer::validate(&cell_state_tmp));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100566 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&cell_state_tmp, lstm_params.cell_layer_norm_weights(), &cell_state_tmp, 1, ConvertPolicy::SATURATE,
567 RoundingPolicy::TO_NEAREST_EVEN));
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100568 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&cell_state_tmp, cell_bias, &cell_state_tmp, ConvertPolicy::SATURATE));
569 }
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100570 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&cell_state_tmp, nullptr, activation_info));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100571 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&cell_state_tmp, &input_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN));
572 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&cell_state_tmp, &forget_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN));
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100573 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000574 if(cell_threshold != 0.f)
575 {
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100576 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&cell_state_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold,
577 cell_threshold)));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000578 }
579
John Kesapidescafec8f2019-02-19 15:53:59 +0000580 std::vector<const ITensorInfo *> in_out_weights;
581 in_out_weights.emplace_back(input_to_output_weights);
582 in_out_weights.emplace_back(recurrent_to_output_weights);
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +0100583 TensorShape in_out_weights_concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(in_out_weights, 0);
John Kesapidescafec8f2019-02-19 15:53:59 +0000584 TensorInfo in_out_gate_concat = TensorInfo(in_out_weights_concat_shape, 1, input->data_type());
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100585 ARM_COMPUTE_RETURN_ON_ERROR(CLConcatenateLayer::validate(in_out_weights, &in_out_gate_concat, Window::DimX));
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100586 // Validate output gate tmp
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100587 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_output_weights, (lstm_params.use_layer_norm()) ? nullptr : output_gate_bias, &output_gate_tmp));
John Kesapidescafec8f2019-02-19 15:53:59 +0000588
Michalis Spyroubcedf512018-03-22 14:55:08 +0000589 if(lstm_params.has_peephole_opt())
590 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100591 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&cell_state_tmp, lstm_params.cell_to_output_weights(), &output_gate_tmp, 1, ConvertPolicy::SATURATE,
592 RoundingPolicy::TO_NEAREST_EVEN));
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100593 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&output_gate_tmp, &output_gate_tmp, &output_gate_tmp, ConvertPolicy::SATURATE));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000594 }
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100595 if(lstm_params.use_layer_norm())
596 {
597 ARM_COMPUTE_RETURN_ON_ERROR(CLMeanStdDevNormalizationLayer::validate(&output_gate_tmp));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100598 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&output_gate_tmp, lstm_params.output_layer_norm_weights(), &output_gate_tmp, 1, ConvertPolicy::SATURATE,
599 RoundingPolicy::TO_NEAREST_EVEN));
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100600 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&output_gate_tmp, output_gate_bias, &output_gate_tmp, ConvertPolicy::SATURATE));
601 }
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100602 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&output_gate_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000603
604 // Validate output state
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100605 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&cell_state_tmp, &cell_state_tmp, activation_info));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100606 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&cell_state_tmp, &output_gate_tmp, &output_gate_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000607 if(lstm_params.has_projection())
608 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100609 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(&output_gate_tmp, lstm_params.projection_weights(), lstm_params.projection_bias(), output_state_out));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000610 if(projection_threshold != 0.f)
611 {
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100612 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output_state_out, output_state_out,
613 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold)));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000614 }
615 }
616
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100617 // Validate copy kernel
Sheri Zhang7e20e292021-02-02 11:49:34 +0000618 ARM_COMPUTE_RETURN_ON_ERROR(CLCopy::validate(&cell_state_tmp, cell_state_out));
619 ARM_COMPUTE_RETURN_ON_ERROR(CLCopy::validate(output_state_out, output));
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100620
621 // Validate scratch concatenation
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100622 std::vector<const ITensorInfo *> inputs_vector_info_raw;
Georgios Pinitas0cc37c32018-11-14 15:54:26 +0000623 if(!lstm_params.has_cifg_opt())
Michalis Spyroubcedf512018-03-22 14:55:08 +0000624 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100625 inputs_vector_info_raw.push_back(&input_gate);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000626 }
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100627 inputs_vector_info_raw.push_back(&cell_state_tmp);
628 inputs_vector_info_raw.push_back(&forget_gate);
629 inputs_vector_info_raw.push_back(&output_gate_tmp);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000630
Georgios Pinitas09f24972019-05-17 18:14:40 +0100631 ARM_COMPUTE_RETURN_ON_ERROR(CLConcatenateLayer::validate(inputs_vector_info_raw, scratch_buffer, Window::DimX));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000632 return Status{};
633}
634
635void CLLSTMLayer::run()
636{
John Kesapidescafec8f2019-02-19 15:53:59 +0000637 prepare();
638
Georgios Pinitasda953f22019-04-02 17:27:03 +0100639 MemoryGroupResourceScope scope_mg(_memory_group);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000640
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100641 _concat_inputs_forget_gate.run();
John Kesapidescafec8f2019-02-19 15:53:59 +0000642
Michalis Spyroubcedf512018-03-22 14:55:08 +0000643 _fully_connected_forget_gate.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000644
645 if(_run_peephole_opt)
646 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100647 _pixelwise_mul_forget_gate.run();
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100648 _accum_forget_gate1.run();
649 }
650 if(_is_layer_norm_lstm)
651 {
652 _mean_std_norm_forget_gate.run();
Michalis Spyrou1009e872020-07-27 12:48:34 +0100653 _pixelwise_mul_forget_gate_coeff.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100654 _accum_forget_gate_bias.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000655 }
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100656 _activation_forget_gate.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000657
658 if(_run_cifg_opt)
659 {
Sheri Zhang7e20e292021-02-02 11:49:34 +0000660 _ones_fill.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100661 _subtract_input_gate.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000662 }
663 else
664 {
665 _fully_connected_input_gate.run();
John Kesapidescafec8f2019-02-19 15:53:59 +0000666
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100667 if(_run_peephole_opt)
668 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100669 _pixelwise_mul_input_gate.run();
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100670 _accum_input_gate1.run();
671 }
672
673 if(_is_layer_norm_lstm)
674 {
675 _mean_std_norm_input_gate.run();
Michalis Spyrou1009e872020-07-27 12:48:34 +0100676 _pixelwise_mul_input_gate_coeff.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100677 _accum_input_gate_bias.run();
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100678 }
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100679 _activation_input_gate.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000680 }
681
682 _fully_connected_cell_state.run();
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100683 CLScheduler::get().enqueue(*_transpose_cell_state);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000684 _gemm_cell_state1.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100685 _accum_cell_state1.run();
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100686 if(_is_layer_norm_lstm)
687 {
688 _mean_std_norm_cell_gate.run();
Michalis Spyrou1009e872020-07-27 12:48:34 +0100689 _pixelwise_mul_cell_gate_coeff.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100690 _accum_cell_gate_bias.run();
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100691 }
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100692 _activation_cell_state.run();
Michalis Spyrou1009e872020-07-27 12:48:34 +0100693 _pixelwise_mul_cell_state1.run();
694 _pixelwise_mul_cell_state2.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100695 _accum_cell_state2.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000696
697 if(_perform_cell_clipping)
698 {
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100699 _cell_clip.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000700 }
701
702 _fully_connected_output.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000703
704 if(_run_peephole_opt)
705 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100706 _pixelwise_mul_output_state1.run();
Michele Di Giorgio39438b42019-06-04 12:41:45 +0100707 _accum_output1.run();
708 }
709 if(_is_layer_norm_lstm)
710 {
711 _mean_std_norm_output_gate.run();
Michalis Spyrou1009e872020-07-27 12:48:34 +0100712 _pixelwise_mul_output_gate_coeff.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100713 _accum_output_gate_bias.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000714 }
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100715 _activation_output.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000716
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100717 _activation_output_state.run();
Michalis Spyrou1009e872020-07-27 12:48:34 +0100718 _pixelwise_mul_output_state2.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000719
720 if(_has_projection_weights)
721 {
722 _fully_connected_output_state.run();
723 if(_perform_projection_clipping)
724 {
Georgios Pinitasab23dd02020-07-06 14:57:36 +0100725 _projection_clip.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000726 }
727 }
728
Sheri Zhang7e20e292021-02-02 11:49:34 +0000729 _copy_cell_state.run();
730 _copy_output.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000731
732 _concat_scratch_buffer.run();
giuros01164a2722018-11-20 18:34:46 +0000733}
John Kesapidescafec8f2019-02-19 15:53:59 +0000734
735void CLLSTMLayer::prepare()
736{
737 if(!_is_prepared)
738 {
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100739 _concat_weights_forget_gate.run();
John Kesapidescafec8f2019-02-19 15:53:59 +0000740 if(!_run_cifg_opt)
741 {
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100742 _concat_weights_input_gate.run();
John Kesapidescafec8f2019-02-19 15:53:59 +0000743 }
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100744 _concat_weights_output.run();
John Kesapidescafec8f2019-02-19 15:53:59 +0000745 _is_prepared = true;
746 }
747}
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000748} // namespace arm_compute