blob: 743a32c47d0a05ce3b7df32ced6cfdb64c629204 [file] [log] [blame]
Michele Di Giorgio47a89902020-03-09 19:32:33 +00001/*
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +00002 * Copyright (c) 2020-2021 Arm Limited.
Michele Di Giorgio47a89902020-03-09 19:32:33 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef ARM_COMPUTE_NEQLSTMLAYER_H
25#define ARM_COMPUTE_NEQLSTMLAYER_H
26
Michele Di Giorgio47a89902020-03-09 19:32:33 +000027#include "arm_compute/core/Types.h"
28#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
Michalis Spyrou173ba9b2020-06-23 17:25:43 +010029#include "arm_compute/runtime/NEON/functions/NEArithmeticAddition.h"
30#include "arm_compute/runtime/NEON/functions/NEArithmeticSubtraction.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010031#include "arm_compute/runtime/NEON/functions/NECopy.h"
Michele Di Giorgio47a89902020-03-09 19:32:33 +000032#include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h"
33#include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h"
Michalis Spyrou6eb73452020-07-02 17:39:25 +010034#include "arm_compute/runtime/NEON/functions/NEPixelWiseMultiplication.h"
Michele Di Giorgio47a89902020-03-09 19:32:33 +000035#include "arm_compute/runtime/NEON/functions/NETranspose.h"
Michele Di Giorgio47a89902020-03-09 19:32:33 +000036#include "arm_compute/runtime/common/LSTMParams.h"
Georgios Pinitas40f51a62020-11-21 03:04:18 +000037
Michalis Spyrouebcebf12020-10-21 00:04:14 +010038#include <memory>
Michele Di Giorgio47a89902020-03-09 19:32:33 +000039
40namespace arm_compute
41{
42// Forward declarations
43class ITensor;
Michalis Spyrouebcebf12020-10-21 00:04:14 +010044class ITensorInfo;
45class NEQLSTMLayerNormalizationKernel;
46class NEGEMMLowpMatrixAReductionKernel;
Michele Di Giorgio47a89902020-03-09 19:32:33 +000047
48/** Basic function to run @ref NEQLSTMLayer
49 *
50 * This function calls the following NEON functions/kernels:
51 *
52 * -# @ref NEActivationLayer Activation functions (tanh and logistic)
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +000053 * -# @ref NEArithmeticAddition Elementwise addition
Sheri Zhangfc6744a2021-01-13 15:54:05 +000054 * -# @ref NEArithmeticSubtraction Elementwise subtraction
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +000055 * -# @ref NECopy Copy kernel for copying output_state_out to output
Michele Di Giorgio47a89902020-03-09 19:32:33 +000056 * -# @ref NEGEMMLowpMatrixMultiplyCore Quantized matrix multiplication core. Accumulators are 32-bit integers
57 * -# @ref NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint Convert 32-bit integers into QSYMM16
58 * -# @ref NEGEMMLowpMatrixAReductionKernel For precomputing effective biases to use
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +000059 * -# @ref NEPixelWiseMultiplication Elementwise multiplication
Michele Di Giorgio47a89902020-03-09 19:32:33 +000060 * -# @ref NETranspose Transpose function for reshaping the weights
61 * */
62class NEQLSTMLayer : public IFunction
63{
64public:
65 /** Default constructor */
66 NEQLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
67 /** Prevent instances of this class from being copied (As this class contains pointers) */
68 NEQLSTMLayer(const NEQLSTMLayer &) = delete;
Michalis Spyrou770dfeb2020-11-04 18:55:34 +000069 /** Prevent instances of this class from being moved (As this class contains pointers) */
70 NEQLSTMLayer(NEQLSTMLayer &&) = delete;
Michele Di Giorgio47a89902020-03-09 19:32:33 +000071 /** Prevent instances of this class from being copied (As this class contains pointers) */
72 NEQLSTMLayer &operator=(const NEQLSTMLayer &) = delete;
Michalis Spyrou770dfeb2020-11-04 18:55:34 +000073 /** Prevent instances of this class from being moved (As this class contains pointers) */
74 NEQLSTMLayer &operator=(NEQLSTMLayer &&) = delete;
Michalis Spyrouebcebf12020-10-21 00:04:14 +010075 /** Default destructor */
76 ~NEQLSTMLayer();
Michele Di Giorgio47a89902020-03-09 19:32:33 +000077 /** Initialize function's tensors.
78 *
79 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: QASYMM8_SIGNED.
80 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
81 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
82 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
83 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
84 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
85 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
86 * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32.
87 * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32.
88 * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32.
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +010089 * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: QSYMM16.
90 * @param[in] output_state_in 2D tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
91 * @param[out] cell_state_out Destination tensor. Output is a 2D tensor with dimensions [num_units, batch_size]. Data type supported: QSYMM16.
92 * @param[out] output_state_out Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size].Data types supported: Same as @p input.
93 * @param[out] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size].Data types supported: Same as @p input.
Michele Di Giorgio47a89902020-03-09 19:32:33 +000094 * @param[in] lstm_params Weights tensors used in peephole, CIFG and layer normalization optimizations:
95 * input_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at input gate.
96 * forget_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at forget gate.
97 * cell_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at cell gate.
98 * output_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at output gate.
99 * hidden_state_zero The zero point of the hidden state.
100 * hidden_state_scale The scale of the hidden state.
101 * input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
102 * recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
103 * cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: QSYMM16.
104 * cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
105 * cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
106 * input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: S32.
107 * projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
108 * projection_bias (Optional) 1D weights tensor with dimensions [output_size]. S32.
109 * input_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
110 * forget_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
111 * cell_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
112 * output_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
113 * cell_threshold (Optional) The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip].
114 * If set to 0.0 then clipping is disabled.
115 * projection_threshold (Optional) The clipping threshold for the output from the projection layer, such that values are bound within
116 * [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled.
117 */
118 void configure(const ITensor *input,
119 const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights,
120 const ITensor *recurrent_to_forget_weights, const ITensor *recurrent_to_cell_weights, const ITensor *recurrent_to_output_weights,
121 const ITensor *forget_gate_bias, const ITensor *cell_bias, const ITensor *output_gate_bias,
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100122 const ITensor *cell_state_in, ITensor *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100123 ITensor *cell_state_out, ITensor *output_state_out, ITensor *output,
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000124 const LSTMParams<ITensor> &lstm_params);
125
126 /** Static function to check if given info will lead to a valid configuration of @ref NEQLSTMLayer
127 *
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100128 * @param[in] input Source tensor info. Input is a 2D tensor info with dimensions [input_size, batch_size]. Data types supported: QASYMM8_SIGNED.
129 * @param[in] input_to_forget_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8.
130 * @param[in] input_to_cell_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8.
131 * @param[in] input_to_output_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8.
132 * @param[in] recurrent_to_forget_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8.
133 * @param[in] recurrent_to_cell_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8.
134 * @param[in] recurrent_to_output_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8.
135 * @param[in] forget_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32.
136 * @param[in] cell_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32.
137 * @param[in] output_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32.
138 * @param[in] cell_state_in 2D tensor info with dimensions [num_units, batch_size]. Data type supported: QSYMM16.
139 * @param[in] output_state_in 2D tensor info with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
140 * @param[in] cell_state_out Destination tensor info. Output is a 2D tensor info with dimensions [num_units, batch_size]. Data type supported: QSYMM16.
141 * @param[in] output_state_out Destination tensor info. Output is a 2D tensor info with dimensions [output_size, batch_size].Data types supported: Same as @p input.
142 * @param[in] output Destination tensor info. Output is a 2D tensor info with dimensions [output_size, batch_size].Data types supported: Same as @p input.
143 * @param[in] lstm_params Weights tensors info used in peephole, CIFG and layer normalization optimizations:
144 * input_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at input gate.
145 * forget_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at forget gate.
146 * cell_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at cell gate.
147 * output_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at output gate.
148 * hidden_state_zero The zero point of the hidden state.
149 * hidden_state_scale The scale of the hidden state.
150 * input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
151 * recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
152 * cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: QSYMM16.
153 * cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
154 * cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
155 * input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: S32.
156 * projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
157 * projection_bias (Optional) 1D weights tensor with dimensions [output_size]. S32.
158 * input_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
159 * forget_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
160 * cell_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
161 * output_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
162 * cell_threshold (Optional) The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip].
163 * If set to 0.0 then clipping is disabled.
164 * projection_threshold (Optional) The clipping threshold for the output from the projection layer, such that values are bound within
165 * [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled.
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000166 * @return a status
167 */
168 static Status validate(const ITensorInfo *input,
169 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
170 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
171 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
172 const ITensorInfo *cell_state_in, const ITensorInfo *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100173 const ITensorInfo *cell_state_out, const ITensorInfo *output_state_out, const ITensorInfo *output,
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000174 const LSTMParams<ITensorInfo> &lstm_params);
175
176 // Inherited methods overridden:
177 void run() override;
178 void prepare() override;
179
180private:
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100181 enum class LayerNormGate : uint8_t
182 {
183 Forget,
184 Cell,
185 Input,
186 Output,
187 Count
188 };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100189 static constexpr uint8_t _layer_norm_count = static_cast<uint8_t>(LayerNormGate::Count);
190 static constexpr uint32_t _out_state_output_size_dimension_idx = 0;
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100191
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000192 /** Internal method to configure matrix multiplication plus output stage of each gate.
193 *
194 * @param[in] mm Matrix multiplication function to use.
195 * @param[in] outstage Output stage function to use.
196 * @param[in] gemmlowp_info GEMMLowp metadata to be used by the output stage.
197 * @param[in] mm_input Input tensor to matrix multiplication function.
198 * @param[in] mm_weights Weights tensor to matrix multiplication function.
199 * @param[in] bias Bias tensor to matrix multiplication function.
200 * @param[in] outstage_res Tensor to be used for storing the result of the output stage.
201 * @param[in] gemmlowp_scale Real multiplier to be used computing multiplier and shift for requantization.
202 * @param[in] mm_res_info Tensor info to be used to initialize matrix multiplication result tensor.
203 * @param[in] mm_res_info Tensor info to be used to initialize output stage result tensor.
204 *
205 */
206 void configure_mm(NEGEMMLowpMatrixMultiplyCore &mm, NEGEMMLowpOutputStage &outstage, GEMMLowpOutputStageInfo &gemmlowp_info,
207 const ITensor *mm_input, const ITensor *mm_weights, const ITensor *bias, Tensor *mm_res,
208 Tensor *outstage_res, float gemmlowp_scale,
209 const TensorInfo &mm_res_info, const TensorInfo &outstage_tensor_info);
210
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100211 MemoryGroup _memory_group;
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000212
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100213 /** A small internel kernel do the copy between two tensors */
214 class TensorCopyKernel
215 {
216 static constexpr uint32_t max_dimension_supported = 2;
217
218 ITensor *_src{ nullptr };
219 ITensor *_dst{ nullptr };
220 size_t _row_size{};
221 Window _window{};
222
223 public:
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100224 /** Destructor */
225 ~TensorCopyKernel();
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100226 /** Static function to check if given info will lead to a valid configuration of @ref NEQLSTMLayer::TensorCopyKernel
227 *
228 * @param[in] src Source tensor info.
229 * @param[in] dst Destination tensor info
230 *
231 * @return a status
232 */
233 static Status validate(const ITensorInfo &src, const ITensorInfo &dst);
234 /** Set the input and output tensors.
235 *
236 * @param[in] src Source tensor
237 * @param[out] dst Destination tensor
238 */
239 void configure(ITensor &src, ITensor &dst);
240 /** run the kernel */
241 void run();
242 };
243
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000244 // Functions used
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100245 NETranspose _transpose_input_to_forget_weights;
246 NETranspose _transpose_input_to_cell_weights;
247 NETranspose _transpose_input_to_output_weights;
248 NETranspose _transpose_input_to_input_weights;
249 NETranspose _transpose_recurrent_to_forget_weights;
250 NETranspose _transpose_recurrent_to_cell_weights;
251 NETranspose _transpose_recurrent_to_output_weights;
252 NETranspose _transpose_recurrent_to_input_weights;
253 NETranspose _transpose_projection_weights;
254 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _input_to_input_reduction;
255 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _recurrent_to_input_reduction;
256 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _input_to_forget_reduction;
257 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _recurrent_to_forget_reduction;
258 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _input_to_cell_reduction;
259 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _recurrent_to_cell_reduction;
260 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _input_to_output_reduction;
261 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _recurrent_to_output_reduction;
262 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _projection_reduction;
263 NEArithmeticAddition _projection_bias_add;
264 NEGEMMLowpMatrixMultiplyCore _mm_input_to_forget;
265 NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_forget;
266 NEPixelWiseMultiplication _pixelwise_mul_cell_to_forget;
267 NEGEMMLowpOutputStage _input_to_forget_outstage;
268 NEGEMMLowpOutputStage _recurrent_to_forget_outstage;
269 NEGEMMLowpOutputStage _cell_to_forget_outstage;
270 NEArithmeticAddition _accumulate_input_recurrent_forget;
271 NEArithmeticAddition _accumulate_cell_forget;
272 NEActivationLayer _forget_gate_sigmoid;
273 NEGEMMLowpMatrixMultiplyCore _mm_input_to_cell;
274 NEGEMMLowpOutputStage _input_to_cell_outstage;
275 NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_cell;
276 NEGEMMLowpOutputStage _recurrent_to_cell_outstage;
277 NEArithmeticAddition _accumulate_input_recurrent_modulation;
278 NEActivationLayer _cell_gate_tanh;
279 NEArithmeticSubtraction _input_gate_sub;
280 NEGEMMLowpMatrixMultiplyCore _mm_input_to_input;
281 NEGEMMLowpOutputStage _input_to_input_outstage;
282 NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_input;
283 NEGEMMLowpOutputStage _recurrent_to_input_outstage;
284 NEArithmeticAddition _accumulate_input_recurrent_input;
285 NEPixelWiseMultiplication _pixelwise_mul_cell_to_input;
286 NEGEMMLowpOutputStage _cell_to_input_outstage;
287 NEArithmeticAddition _accumulate_cell_input;
288 NEActivationLayer _input_gate_sigmoid;
289 NEPixelWiseMultiplication _pixelwise_mul_forget_cell;
290 NEPixelWiseMultiplication _pixelwise_mul_input_cell;
291 NEArithmeticAddition _add_forget_cell;
292 NEActivationLayer _cell_clip;
293 NEGEMMLowpMatrixMultiplyCore _mm_input_to_output;
294 NEGEMMLowpOutputStage _input_to_output_outstage;
295 NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_output;
296 NEGEMMLowpOutputStage _recurrent_to_output_outstage;
297 NEArithmeticAddition _accumulate_input_recurrent_output;
298 NEPixelWiseMultiplication _pixelwise_mul_cell_to_output;
299 NEGEMMLowpOutputStage _cell_to_output_outstage;
300 NEArithmeticAddition _accumulate_cell_to_output;
301 NEActivationLayer _output_gate_sigmoid;
302 NEActivationLayer _hidden_tanh;
303 NEPixelWiseMultiplication _pixelwise_mul_hidden;
304 NEGEMMLowpOutputStage _hidden_outstage;
305 NEGEMMLowpMatrixMultiplyCore _mm_projection;
306 NEGEMMLowpOutputStage _projection_outstage;
307 NEArithmeticAddition _accumulate_projection;
308 NEActivationLayer _projection_clip;
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100309
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100310 TensorCopyKernel _projection_bias_copy;
311 TensorCopyKernel _projection_output_to_accumulate_copy;
312 TensorCopyKernel _projection_accumulate_to_output_copy;
313 TensorCopyKernel _hidden_to_output_copy;
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100314
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100315 std::array<std::unique_ptr<NEQLSTMLayerNormalizationKernel>, _layer_norm_count> _layer_norms;
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000316
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100317 NECopy _copy_output;
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100318
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000319 // Tensor pointers
Michalis Spyrou173ba9b2020-06-23 17:25:43 +0100320 const ITensor *_input_to_input_weights
321 {
322 nullptr
323 };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000324 const ITensor *_recurrent_to_input_weights{ nullptr };
325 const ITensor *_projection_bias{ nullptr };
326 const ITensor *_input_to_forget_weights{ nullptr };
327 const ITensor *_input_to_cell_weights{ nullptr };
328 const ITensor *_input_to_output_weights{ nullptr };
329 const ITensor *_recurrent_to_forget_weights{ nullptr };
330 const ITensor *_recurrent_to_cell_weights{ nullptr };
331 const ITensor *_recurrent_to_output_weights{ nullptr };
332 const ITensor *_projection_weights{ nullptr };
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100333 std::array<const ITensor *, _layer_norm_count> _layer_norm_weights{};
334 std::array<const ITensor *, _layer_norm_count> _layer_norm_bias{};
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100335
336 using LayerNormIndexType = typename std::underlying_type<LayerNormGate>::type;
337 inline LayerNormIndexType getGateIndex(LayerNormGate g)
338 {
339 return static_cast<LayerNormIndexType>(g);
340 }
341
342 inline void set_layer_norm_weight(const ITensor *t, LayerNormGate g)
343 {
344 _layer_norm_weights[getGateIndex(g)] = t;
345 }
346
347 inline void set_layer_norm_bias(const ITensor *t, LayerNormGate g)
348 {
349 _layer_norm_bias[getGateIndex(g)] = t;
350 }
351
352 inline const ITensor *get_layer_norm_weight(LayerNormGate g)
353 {
354 return _layer_norm_weights[getGateIndex(g)];
355 }
356
357 inline const ITensor *get_layer_norm_bias(LayerNormGate g)
358 {
359 return _layer_norm_bias[getGateIndex(g)];
360 }
361
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100362 inline std::unique_ptr<NEQLSTMLayerNormalizationKernel> &get_layer_norm(LayerNormGate g)
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100363 {
364 return _layer_norms[getGateIndex(g)];
365 }
366
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100367 void configure_layer_norm(LayerNormGate g, const ITensor *in);
368 static Status validate_layer_norm(const ITensorInfo &in, const ITensorInfo &weight, const ITensorInfo &bias);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000369
370 // Temporary tensors
371 Tensor _input_to_forget_weights_transposed{ nullptr };
372 Tensor _input_to_cell_weights_transposed{ nullptr };
373 Tensor _input_to_output_weights_transposed{ nullptr };
374 Tensor _input_to_input_weights_transposed{ nullptr };
375 Tensor _recurrent_to_forget_weights_transposed{ nullptr };
376 Tensor _recurrent_to_cell_weights_transposed{ nullptr };
377 Tensor _recurrent_to_output_weights_transposed{ nullptr };
378 Tensor _recurrent_to_input_weights_transposed{ nullptr };
379 Tensor _projection_weights_transposed{ nullptr };
380 Tensor _input_to_input_eff_bias{ nullptr };
381 Tensor _recurrent_to_input_eff_bias{ nullptr };
382 Tensor _input_to_forget_eff_bias{ nullptr };
383 Tensor _recurrent_to_forget_eff_bias{ nullptr };
384 Tensor _input_to_cell_eff_bias{ nullptr };
385 Tensor _recurrent_to_cell_eff_bias{ nullptr };
386 Tensor _input_to_output_eff_bias{ nullptr };
387 Tensor _recurrent_to_output_eff_bias{ nullptr };
388 Tensor _projection_reduction_res{ nullptr };
389 Tensor _projection_eff_bias{ nullptr };
390 Tensor _mm_input_to_forget_res{ nullptr };
391 Tensor _mm_recurrent_to_forget_res{ nullptr };
392 Tensor _mul_cell_to_forget_res{ nullptr };
393 Tensor _input_to_forget_outstage_res{ nullptr };
394 Tensor _cell_to_forget_outstage_res{ nullptr };
395 Tensor _recurrent_to_forget_outstage_res{ nullptr };
396 Tensor _forget_gate{ nullptr };
397 Tensor _mm_input_to_cell_res{ nullptr };
398 Tensor _input_to_cell_outstage_res{ nullptr };
399 Tensor _mm_recurrent_to_cell_res{ nullptr };
400 Tensor _recurrent_to_cell_outstage_res{ nullptr };
401 Tensor _cell_gate{ nullptr };
402 Tensor _mul_input_cell_res{ nullptr };
403 Tensor _mm_input_to_input_res{ nullptr };
404 Tensor _input_to_input_outstage_res{ nullptr };
405 Tensor _mm_recurrent_to_input_res{ nullptr };
406 Tensor _mul_cell_to_input_res{ nullptr };
407 Tensor _cell_to_input_outstage_res{ nullptr };
408 Tensor _recurrent_to_input_outstage_res{ nullptr };
409 Tensor _input_gate{ nullptr };
410 Tensor _mm_input_to_output_res{ nullptr };
411 Tensor _input_to_output_outstage_res{ nullptr };
412 Tensor _mm_recurrent_to_output_res{ nullptr };
413 Tensor _mul_cell_to_output_res{ nullptr };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100414 Tensor _cell_to_output_outstage_res{ nullptr };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000415 Tensor _recurrent_to_output_outstage_res{ nullptr };
416 Tensor _output_gate{ nullptr };
417 Tensor _hidden_mul_res{ nullptr };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100418 Tensor _hidden_gate{ nullptr };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000419 Tensor _mm_projection_res{ nullptr };
420 Tensor _projection_outstage_res{ nullptr };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100421 Tensor _projection_out_res{ nullptr };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100422 Tensor _projection_accumulate_res{ nullptr };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000423 Tensor _ones{ nullptr };
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100424 std::array<Tensor, _layer_norm_count> _layer_norm_output{};
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100425
426 inline Tensor &get_layer_norm_output(LayerNormGate g)
427 {
428 return _layer_norm_output[getGateIndex(g)];
429 }
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000430
431 bool _is_prepared{ false };
432 bool _has_cifg{ false };
433 bool _has_cell_clipping{ false };
434 bool _has_projection{ false };
435 bool _has_projection_clipping{ false };
436 bool _has_peephole{ false };
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100437 bool _has_layer_norm{ false };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100438 bool _projection_tensor_copy_required{ false };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000439};
440} // namespace arm_compute
441#endif /* ARM_COMPUTE_NEQLSTMLAYER_H */