blob: fcabc1d0c4c09d0de53d5b6df478cf59cda270d7 [file] [log] [blame]
Michele Di Giorgio47a89902020-03-09 19:32:33 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2020 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"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010036#include "support/MemorySupport.h"
Michele Di Giorgio47a89902020-03-09 19:32:33 +000037
38#include "arm_compute/runtime/common/LSTMParams.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010039#include <memory>
Michele Di Giorgio47a89902020-03-09 19:32:33 +000040
41namespace arm_compute
42{
43// Forward declarations
44class ITensor;
Michalis Spyrouebcebf12020-10-21 00:04:14 +010045class ITensorInfo;
46class NEQLSTMLayerNormalizationKernel;
47class NEGEMMLowpMatrixAReductionKernel;
Michele Di Giorgio47a89902020-03-09 19:32:33 +000048
49/** Basic function to run @ref NEQLSTMLayer
50 *
51 * This function calls the following NEON functions/kernels:
52 *
53 * -# @ref NEActivationLayer Activation functions (tanh and logistic)
Michalis Spyrou173ba9b2020-06-23 17:25:43 +010054 * -# @ref NEArithmeticAddition Elementwise addition
Michele Di Giorgio47a89902020-03-09 19:32:33 +000055 * -# @ref NEArithmeticSubtractionKernel Elementwise subtraction
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +010056 * -# @ref NECopyKernel Copy kernel for copying output_state_out to output
Michele Di Giorgio47a89902020-03-09 19:32:33 +000057 * -# @ref NEGEMMLowpMatrixMultiplyCore Quantized matrix multiplication core. Accumulators are 32-bit integers
58 * -# @ref NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint Convert 32-bit integers into QSYMM16
59 * -# @ref NEGEMMLowpMatrixAReductionKernel For precomputing effective biases to use
Michalis Spyrou6eb73452020-07-02 17:39:25 +010060 * -# @ref NEPixelWiseMultiplication Elementwise multiplication
Michele Di Giorgio47a89902020-03-09 19:32:33 +000061 * -# @ref NETranspose Transpose function for reshaping the weights
62 * */
63class NEQLSTMLayer : public IFunction
64{
65public:
66 /** Default constructor */
67 NEQLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
68 /** Prevent instances of this class from being copied (As this class contains pointers) */
69 NEQLSTMLayer(const NEQLSTMLayer &) = delete;
Michalis Spyrou770dfeb2020-11-04 18:55:34 +000070 /** Prevent instances of this class from being moved (As this class contains pointers) */
71 NEQLSTMLayer(NEQLSTMLayer &&) = delete;
Michele Di Giorgio47a89902020-03-09 19:32:33 +000072 /** Prevent instances of this class from being copied (As this class contains pointers) */
73 NEQLSTMLayer &operator=(const NEQLSTMLayer &) = delete;
Michalis Spyrou770dfeb2020-11-04 18:55:34 +000074 /** Prevent instances of this class from being moved (As this class contains pointers) */
75 NEQLSTMLayer &operator=(NEQLSTMLayer &&) = delete;
Michalis Spyrouebcebf12020-10-21 00:04:14 +010076 /** Default destructor */
77 ~NEQLSTMLayer();
Michele Di Giorgio47a89902020-03-09 19:32:33 +000078 /** Initialize function's tensors.
79 *
80 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: QASYMM8_SIGNED.
81 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
82 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
83 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
84 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
85 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
86 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
87 * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32.
88 * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32.
89 * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32.
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +010090 * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: QSYMM16.
91 * @param[in] output_state_in 2D tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
92 * @param[out] cell_state_out Destination tensor. Output is a 2D tensor with dimensions [num_units, batch_size]. Data type supported: QSYMM16.
93 * @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.
94 * @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 +000095 * @param[in] lstm_params Weights tensors used in peephole, CIFG and layer normalization optimizations:
96 * input_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at input gate.
97 * forget_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at forget gate.
98 * cell_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at cell gate.
99 * output_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at output gate.
100 * hidden_state_zero The zero point of the hidden state.
101 * hidden_state_scale The scale of the hidden state.
102 * input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
103 * recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
104 * cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: QSYMM16.
105 * cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
106 * cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
107 * input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: S32.
108 * projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
109 * projection_bias (Optional) 1D weights tensor with dimensions [output_size]. S32.
110 * input_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
111 * forget_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
112 * cell_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
113 * output_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
114 * cell_threshold (Optional) The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip].
115 * If set to 0.0 then clipping is disabled.
116 * projection_threshold (Optional) The clipping threshold for the output from the projection layer, such that values are bound within
117 * [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled.
118 */
119 void configure(const ITensor *input,
120 const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights,
121 const ITensor *recurrent_to_forget_weights, const ITensor *recurrent_to_cell_weights, const ITensor *recurrent_to_output_weights,
122 const ITensor *forget_gate_bias, const ITensor *cell_bias, const ITensor *output_gate_bias,
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100123 const ITensor *cell_state_in, ITensor *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100124 ITensor *cell_state_out, ITensor *output_state_out, ITensor *output,
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000125 const LSTMParams<ITensor> &lstm_params);
126
127 /** Static function to check if given info will lead to a valid configuration of @ref NEQLSTMLayer
128 *
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100129 * @param[in] input Source tensor info. Input is a 2D tensor info with dimensions [input_size, batch_size]. Data types supported: QASYMM8_SIGNED.
130 * @param[in] input_to_forget_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8.
131 * @param[in] input_to_cell_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8.
132 * @param[in] input_to_output_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8.
133 * @param[in] recurrent_to_forget_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8.
134 * @param[in] recurrent_to_cell_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8.
135 * @param[in] recurrent_to_output_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8.
136 * @param[in] forget_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32.
137 * @param[in] cell_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32.
138 * @param[in] output_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32.
139 * @param[in] cell_state_in 2D tensor info with dimensions [num_units, batch_size]. Data type supported: QSYMM16.
140 * @param[in] output_state_in 2D tensor info with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
141 * @param[in] cell_state_out Destination tensor info. Output is a 2D tensor info with dimensions [num_units, batch_size]. Data type supported: QSYMM16.
142 * @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.
143 * @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.
144 * @param[in] lstm_params Weights tensors info used in peephole, CIFG and layer normalization optimizations:
145 * input_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at input gate.
146 * forget_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at forget gate.
147 * cell_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at cell gate.
148 * output_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at output gate.
149 * hidden_state_zero The zero point of the hidden state.
150 * hidden_state_scale The scale of the hidden state.
151 * input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
152 * recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
153 * cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: QSYMM16.
154 * cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
155 * cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
156 * input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: S32.
157 * projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
158 * projection_bias (Optional) 1D weights tensor with dimensions [output_size]. S32.
159 * input_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
160 * forget_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
161 * cell_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
162 * output_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
163 * cell_threshold (Optional) The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip].
164 * If set to 0.0 then clipping is disabled.
165 * projection_threshold (Optional) The clipping threshold for the output from the projection layer, such that values are bound within
166 * [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled.
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000167 * @return a status
168 */
169 static Status validate(const ITensorInfo *input,
170 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
171 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
172 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
173 const ITensorInfo *cell_state_in, const ITensorInfo *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100174 const ITensorInfo *cell_state_out, const ITensorInfo *output_state_out, const ITensorInfo *output,
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000175 const LSTMParams<ITensorInfo> &lstm_params);
176
177 // Inherited methods overridden:
178 void run() override;
179 void prepare() override;
180
181private:
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100182 enum class LayerNormGate : uint8_t
183 {
184 Forget,
185 Cell,
186 Input,
187 Output,
188 Count
189 };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100190 static constexpr uint8_t _layer_norm_count = static_cast<uint8_t>(LayerNormGate::Count);
191 static constexpr uint32_t _out_state_output_size_dimension_idx = 0;
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100192
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000193 /** Internal method to configure matrix multiplication plus output stage of each gate.
194 *
195 * @param[in] mm Matrix multiplication function to use.
196 * @param[in] outstage Output stage function to use.
197 * @param[in] gemmlowp_info GEMMLowp metadata to be used by the output stage.
198 * @param[in] mm_input Input tensor to matrix multiplication function.
199 * @param[in] mm_weights Weights tensor to matrix multiplication function.
200 * @param[in] bias Bias tensor to matrix multiplication function.
201 * @param[in] outstage_res Tensor to be used for storing the result of the output stage.
202 * @param[in] gemmlowp_scale Real multiplier to be used computing multiplier and shift for requantization.
203 * @param[in] mm_res_info Tensor info to be used to initialize matrix multiplication result tensor.
204 * @param[in] mm_res_info Tensor info to be used to initialize output stage result tensor.
205 *
206 */
207 void configure_mm(NEGEMMLowpMatrixMultiplyCore &mm, NEGEMMLowpOutputStage &outstage, GEMMLowpOutputStageInfo &gemmlowp_info,
208 const ITensor *mm_input, const ITensor *mm_weights, const ITensor *bias, Tensor *mm_res,
209 Tensor *outstage_res, float gemmlowp_scale,
210 const TensorInfo &mm_res_info, const TensorInfo &outstage_tensor_info);
211
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100212 MemoryGroup _memory_group;
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000213
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100214 /** A small internel kernel do the copy between two tensors */
215 class TensorCopyKernel
216 {
217 static constexpr uint32_t max_dimension_supported = 2;
218
219 ITensor *_src{ nullptr };
220 ITensor *_dst{ nullptr };
221 size_t _row_size{};
222 Window _window{};
223
224 public:
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100225 /** Destructor */
226 ~TensorCopyKernel();
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100227 /** Static function to check if given info will lead to a valid configuration of @ref NEQLSTMLayer::TensorCopyKernel
228 *
229 * @param[in] src Source tensor info.
230 * @param[in] dst Destination tensor info
231 *
232 * @return a status
233 */
234 static Status validate(const ITensorInfo &src, const ITensorInfo &dst);
235 /** Set the input and output tensors.
236 *
237 * @param[in] src Source tensor
238 * @param[out] dst Destination tensor
239 */
240 void configure(ITensor &src, ITensor &dst);
241 /** run the kernel */
242 void run();
243 };
244
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000245 // Functions used
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100246 NETranspose _transpose_input_to_forget_weights;
247 NETranspose _transpose_input_to_cell_weights;
248 NETranspose _transpose_input_to_output_weights;
249 NETranspose _transpose_input_to_input_weights;
250 NETranspose _transpose_recurrent_to_forget_weights;
251 NETranspose _transpose_recurrent_to_cell_weights;
252 NETranspose _transpose_recurrent_to_output_weights;
253 NETranspose _transpose_recurrent_to_input_weights;
254 NETranspose _transpose_projection_weights;
255 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _input_to_input_reduction;
256 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _recurrent_to_input_reduction;
257 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _input_to_forget_reduction;
258 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _recurrent_to_forget_reduction;
259 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _input_to_cell_reduction;
260 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _recurrent_to_cell_reduction;
261 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _input_to_output_reduction;
262 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _recurrent_to_output_reduction;
263 std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _projection_reduction;
264 NEArithmeticAddition _projection_bias_add;
265 NEGEMMLowpMatrixMultiplyCore _mm_input_to_forget;
266 NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_forget;
267 NEPixelWiseMultiplication _pixelwise_mul_cell_to_forget;
268 NEGEMMLowpOutputStage _input_to_forget_outstage;
269 NEGEMMLowpOutputStage _recurrent_to_forget_outstage;
270 NEGEMMLowpOutputStage _cell_to_forget_outstage;
271 NEArithmeticAddition _accumulate_input_recurrent_forget;
272 NEArithmeticAddition _accumulate_cell_forget;
273 NEActivationLayer _forget_gate_sigmoid;
274 NEGEMMLowpMatrixMultiplyCore _mm_input_to_cell;
275 NEGEMMLowpOutputStage _input_to_cell_outstage;
276 NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_cell;
277 NEGEMMLowpOutputStage _recurrent_to_cell_outstage;
278 NEArithmeticAddition _accumulate_input_recurrent_modulation;
279 NEActivationLayer _cell_gate_tanh;
280 NEArithmeticSubtraction _input_gate_sub;
281 NEGEMMLowpMatrixMultiplyCore _mm_input_to_input;
282 NEGEMMLowpOutputStage _input_to_input_outstage;
283 NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_input;
284 NEGEMMLowpOutputStage _recurrent_to_input_outstage;
285 NEArithmeticAddition _accumulate_input_recurrent_input;
286 NEPixelWiseMultiplication _pixelwise_mul_cell_to_input;
287 NEGEMMLowpOutputStage _cell_to_input_outstage;
288 NEArithmeticAddition _accumulate_cell_input;
289 NEActivationLayer _input_gate_sigmoid;
290 NEPixelWiseMultiplication _pixelwise_mul_forget_cell;
291 NEPixelWiseMultiplication _pixelwise_mul_input_cell;
292 NEArithmeticAddition _add_forget_cell;
293 NEActivationLayer _cell_clip;
294 NEGEMMLowpMatrixMultiplyCore _mm_input_to_output;
295 NEGEMMLowpOutputStage _input_to_output_outstage;
296 NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_output;
297 NEGEMMLowpOutputStage _recurrent_to_output_outstage;
298 NEArithmeticAddition _accumulate_input_recurrent_output;
299 NEPixelWiseMultiplication _pixelwise_mul_cell_to_output;
300 NEGEMMLowpOutputStage _cell_to_output_outstage;
301 NEArithmeticAddition _accumulate_cell_to_output;
302 NEActivationLayer _output_gate_sigmoid;
303 NEActivationLayer _hidden_tanh;
304 NEPixelWiseMultiplication _pixelwise_mul_hidden;
305 NEGEMMLowpOutputStage _hidden_outstage;
306 NEGEMMLowpMatrixMultiplyCore _mm_projection;
307 NEGEMMLowpOutputStage _projection_outstage;
308 NEArithmeticAddition _accumulate_projection;
309 NEActivationLayer _projection_clip;
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100310
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100311 TensorCopyKernel _projection_bias_copy;
312 TensorCopyKernel _projection_output_to_accumulate_copy;
313 TensorCopyKernel _projection_accumulate_to_output_copy;
314 TensorCopyKernel _hidden_to_output_copy;
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100315
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100316 std::array<std::unique_ptr<NEQLSTMLayerNormalizationKernel>, _layer_norm_count> _layer_norms;
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000317
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100318 NECopy _copy_output;
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100319
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000320 // Tensor pointers
Michalis Spyrou173ba9b2020-06-23 17:25:43 +0100321 const ITensor *_input_to_input_weights
322 {
323 nullptr
324 };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000325 const ITensor *_recurrent_to_input_weights{ nullptr };
326 const ITensor *_projection_bias{ nullptr };
327 const ITensor *_input_to_forget_weights{ nullptr };
328 const ITensor *_input_to_cell_weights{ nullptr };
329 const ITensor *_input_to_output_weights{ nullptr };
330 const ITensor *_recurrent_to_forget_weights{ nullptr };
331 const ITensor *_recurrent_to_cell_weights{ nullptr };
332 const ITensor *_recurrent_to_output_weights{ nullptr };
333 const ITensor *_projection_weights{ nullptr };
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100334 std::array<const ITensor *, _layer_norm_count> _layer_norm_weights{};
335 std::array<const ITensor *, _layer_norm_count> _layer_norm_bias{};
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100336
337 using LayerNormIndexType = typename std::underlying_type<LayerNormGate>::type;
338 inline LayerNormIndexType getGateIndex(LayerNormGate g)
339 {
340 return static_cast<LayerNormIndexType>(g);
341 }
342
343 inline void set_layer_norm_weight(const ITensor *t, LayerNormGate g)
344 {
345 _layer_norm_weights[getGateIndex(g)] = t;
346 }
347
348 inline void set_layer_norm_bias(const ITensor *t, LayerNormGate g)
349 {
350 _layer_norm_bias[getGateIndex(g)] = t;
351 }
352
353 inline const ITensor *get_layer_norm_weight(LayerNormGate g)
354 {
355 return _layer_norm_weights[getGateIndex(g)];
356 }
357
358 inline const ITensor *get_layer_norm_bias(LayerNormGate g)
359 {
360 return _layer_norm_bias[getGateIndex(g)];
361 }
362
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100363 inline std::unique_ptr<NEQLSTMLayerNormalizationKernel> &get_layer_norm(LayerNormGate g)
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100364 {
365 return _layer_norms[getGateIndex(g)];
366 }
367
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100368 void configure_layer_norm(LayerNormGate g, const ITensor *in);
369 static Status validate_layer_norm(const ITensorInfo &in, const ITensorInfo &weight, const ITensorInfo &bias);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000370
371 // Temporary tensors
372 Tensor _input_to_forget_weights_transposed{ nullptr };
373 Tensor _input_to_cell_weights_transposed{ nullptr };
374 Tensor _input_to_output_weights_transposed{ nullptr };
375 Tensor _input_to_input_weights_transposed{ nullptr };
376 Tensor _recurrent_to_forget_weights_transposed{ nullptr };
377 Tensor _recurrent_to_cell_weights_transposed{ nullptr };
378 Tensor _recurrent_to_output_weights_transposed{ nullptr };
379 Tensor _recurrent_to_input_weights_transposed{ nullptr };
380 Tensor _projection_weights_transposed{ nullptr };
381 Tensor _input_to_input_eff_bias{ nullptr };
382 Tensor _recurrent_to_input_eff_bias{ nullptr };
383 Tensor _input_to_forget_eff_bias{ nullptr };
384 Tensor _recurrent_to_forget_eff_bias{ nullptr };
385 Tensor _input_to_cell_eff_bias{ nullptr };
386 Tensor _recurrent_to_cell_eff_bias{ nullptr };
387 Tensor _input_to_output_eff_bias{ nullptr };
388 Tensor _recurrent_to_output_eff_bias{ nullptr };
389 Tensor _projection_reduction_res{ nullptr };
390 Tensor _projection_eff_bias{ nullptr };
391 Tensor _mm_input_to_forget_res{ nullptr };
392 Tensor _mm_recurrent_to_forget_res{ nullptr };
393 Tensor _mul_cell_to_forget_res{ nullptr };
394 Tensor _input_to_forget_outstage_res{ nullptr };
395 Tensor _cell_to_forget_outstage_res{ nullptr };
396 Tensor _recurrent_to_forget_outstage_res{ nullptr };
397 Tensor _forget_gate{ nullptr };
398 Tensor _mm_input_to_cell_res{ nullptr };
399 Tensor _input_to_cell_outstage_res{ nullptr };
400 Tensor _mm_recurrent_to_cell_res{ nullptr };
401 Tensor _recurrent_to_cell_outstage_res{ nullptr };
402 Tensor _cell_gate{ nullptr };
403 Tensor _mul_input_cell_res{ nullptr };
404 Tensor _mm_input_to_input_res{ nullptr };
405 Tensor _input_to_input_outstage_res{ nullptr };
406 Tensor _mm_recurrent_to_input_res{ nullptr };
407 Tensor _mul_cell_to_input_res{ nullptr };
408 Tensor _cell_to_input_outstage_res{ nullptr };
409 Tensor _recurrent_to_input_outstage_res{ nullptr };
410 Tensor _input_gate{ nullptr };
411 Tensor _mm_input_to_output_res{ nullptr };
412 Tensor _input_to_output_outstage_res{ nullptr };
413 Tensor _mm_recurrent_to_output_res{ nullptr };
414 Tensor _mul_cell_to_output_res{ nullptr };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100415 Tensor _cell_to_output_outstage_res{ nullptr };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000416 Tensor _recurrent_to_output_outstage_res{ nullptr };
417 Tensor _output_gate{ nullptr };
418 Tensor _hidden_mul_res{ nullptr };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100419 Tensor _hidden_gate{ nullptr };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000420 Tensor _mm_projection_res{ nullptr };
421 Tensor _projection_outstage_res{ nullptr };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100422 Tensor _projection_out_res{ nullptr };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100423 Tensor _projection_accumulate_res{ nullptr };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000424 Tensor _ones{ nullptr };
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100425 std::array<Tensor, _layer_norm_count> _layer_norm_output{};
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100426
427 inline Tensor &get_layer_norm_output(LayerNormGate g)
428 {
429 return _layer_norm_output[getGateIndex(g)];
430 }
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000431
432 bool _is_prepared{ false };
433 bool _has_cifg{ false };
434 bool _has_cell_clipping{ false };
435 bool _has_projection{ false };
436 bool _has_projection_clipping{ false };
437 bool _has_peephole{ false };
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100438 bool _has_layer_norm{ false };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100439 bool _projection_tensor_copy_required{ false };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000440};
441} // namespace arm_compute
442#endif /* ARM_COMPUTE_NEQLSTMLAYER_H */