blob: acbd92cff7e5787c8eec867b1d051ef7e2952012 [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;
Manuel Bottinicfac51c2021-06-18 15:47:28 +010046namespace cpu
47{
48namespace kernels
49{
50class CpuGemmLowpMatrixAReductionKernel;
51} // namespace kernels
52} // namespace cpu
Michele Di Giorgio47a89902020-03-09 19:32:33 +000053/** Basic function to run @ref NEQLSTMLayer
54 *
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000055 * This function calls the following kernels:
Michele Di Giorgio47a89902020-03-09 19:32:33 +000056 *
57 * -# @ref NEActivationLayer Activation functions (tanh and logistic)
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +000058 * -# @ref NEArithmeticAddition Elementwise addition
Sheri Zhangfc6744a2021-01-13 15:54:05 +000059 * -# @ref NEArithmeticSubtraction Elementwise subtraction
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +000060 * -# @ref NECopy Copy kernel for copying output_state_out to output
Michele Di Giorgio47a89902020-03-09 19:32:33 +000061 * -# @ref NEGEMMLowpMatrixMultiplyCore Quantized matrix multiplication core. Accumulators are 32-bit integers
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010062 * -# @ref NEGEMMLowpOutputStage Convert 32-bit integers into QSYMM16
Manuel Bottinicfac51c2021-06-18 15:47:28 +010063 * -# @ref cpu::kernels::CpuGemmLowpMatrixAReductionKernel For precomputing effective biases to use
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +000064 * -# @ref NEPixelWiseMultiplication Elementwise multiplication
Michele Di Giorgio47a89902020-03-09 19:32:33 +000065 * -# @ref NETranspose Transpose function for reshaping the weights
66 * */
67class NEQLSTMLayer : public IFunction
68{
69public:
70 /** Default constructor */
71 NEQLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
72 /** Prevent instances of this class from being copied (As this class contains pointers) */
73 NEQLSTMLayer(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(NEQLSTMLayer &&) = delete;
Michele Di Giorgio47a89902020-03-09 19:32:33 +000076 /** Prevent instances of this class from being copied (As this class contains pointers) */
77 NEQLSTMLayer &operator=(const NEQLSTMLayer &) = delete;
Michalis Spyrou770dfeb2020-11-04 18:55:34 +000078 /** Prevent instances of this class from being moved (As this class contains pointers) */
79 NEQLSTMLayer &operator=(NEQLSTMLayer &&) = delete;
Michalis Spyrouebcebf12020-10-21 00:04:14 +010080 /** Default destructor */
81 ~NEQLSTMLayer();
Michele Di Giorgio47a89902020-03-09 19:32:33 +000082 /** Initialize function's tensors.
83 *
Teresa Charlin62687422021-04-28 10:58:49 +010084 * Valid data layouts:
85 * - All
86 *
87 * Valid data type configurations:
88 * |src0 |src1 - src6 |src7 -src9 |src10 |src11 |dst0 |dst1 - dst2 |
89 * |:-------------|:------------|:------------|:------|:-------------|:------|:-----------------|
90 * |QASYMM8_SIGNED|QASYMM8 |S32 |QSYMM16|QASYMM8_SIGNED|QSYMM16|QASYMM8_SIGNED |
91 *
Michele Di Giorgio47a89902020-03-09 19:32:33 +000092 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: QASYMM8_SIGNED.
93 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
94 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
95 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
96 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
97 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
98 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
99 * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32.
100 * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32.
101 * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32.
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100102 * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: QSYMM16.
103 * @param[in] output_state_in 2D tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
104 * @param[out] cell_state_out Destination tensor. Output is a 2D tensor with dimensions [num_units, batch_size]. Data type supported: QSYMM16.
105 * @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.
106 * @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 +0000107 * @param[in] lstm_params Weights tensors used in peephole, CIFG and layer normalization optimizations:
108 * input_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at input gate.
109 * forget_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at forget gate.
110 * cell_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at cell gate.
111 * output_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at output gate.
112 * hidden_state_zero The zero point of the hidden state.
113 * hidden_state_scale The scale of the hidden state.
114 * input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
115 * recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
116 * cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: QSYMM16.
117 * cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
118 * cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
119 * input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: S32.
120 * projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
121 * projection_bias (Optional) 1D weights tensor with dimensions [output_size]. S32.
122 * input_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
123 * forget_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
124 * cell_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
125 * output_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
126 * cell_threshold (Optional) The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip].
127 * If set to 0.0 then clipping is disabled.
128 * projection_threshold (Optional) The clipping threshold for the output from the projection layer, such that values are bound within
129 * [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled.
130 */
131 void configure(const ITensor *input,
132 const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights,
133 const ITensor *recurrent_to_forget_weights, const ITensor *recurrent_to_cell_weights, const ITensor *recurrent_to_output_weights,
134 const ITensor *forget_gate_bias, const ITensor *cell_bias, const ITensor *output_gate_bias,
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100135 const ITensor *cell_state_in, ITensor *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100136 ITensor *cell_state_out, ITensor *output_state_out, ITensor *output,
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000137 const LSTMParams<ITensor> &lstm_params);
138
139 /** Static function to check if given info will lead to a valid configuration of @ref NEQLSTMLayer
140 *
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100141 * @param[in] input Source tensor info. Input is a 2D tensor info with dimensions [input_size, batch_size]. Data types supported: QASYMM8_SIGNED.
142 * @param[in] input_to_forget_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8.
143 * @param[in] input_to_cell_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8.
144 * @param[in] input_to_output_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8.
145 * @param[in] recurrent_to_forget_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8.
146 * @param[in] recurrent_to_cell_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8.
147 * @param[in] recurrent_to_output_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8.
148 * @param[in] forget_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32.
149 * @param[in] cell_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32.
150 * @param[in] output_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32.
151 * @param[in] cell_state_in 2D tensor info with dimensions [num_units, batch_size]. Data type supported: QSYMM16.
152 * @param[in] output_state_in 2D tensor info with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
153 * @param[in] cell_state_out Destination tensor info. Output is a 2D tensor info with dimensions [num_units, batch_size]. Data type supported: QSYMM16.
154 * @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.
155 * @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.
156 * @param[in] lstm_params Weights tensors info used in peephole, CIFG and layer normalization optimizations:
157 * input_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at input gate.
158 * forget_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at forget gate.
159 * cell_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at cell gate.
160 * output_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at output gate.
161 * hidden_state_zero The zero point of the hidden state.
162 * hidden_state_scale The scale of the hidden state.
163 * input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8.
164 * recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
165 * cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: QSYMM16.
166 * cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
167 * cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
168 * input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: S32.
169 * projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8.
170 * projection_bias (Optional) 1D weights tensor with dimensions [output_size]. S32.
171 * input_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
172 * forget_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
173 * cell_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
174 * output_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16.
175 * cell_threshold (Optional) The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip].
176 * If set to 0.0 then clipping is disabled.
177 * projection_threshold (Optional) The clipping threshold for the output from the projection layer, such that values are bound within
178 * [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled.
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000179 * @return a status
180 */
181 static Status validate(const ITensorInfo *input,
182 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
183 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
184 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
185 const ITensorInfo *cell_state_in, const ITensorInfo *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100186 const ITensorInfo *cell_state_out, const ITensorInfo *output_state_out, const ITensorInfo *output,
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000187 const LSTMParams<ITensorInfo> &lstm_params);
188
189 // Inherited methods overridden:
190 void run() override;
191 void prepare() override;
192
193private:
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100194 enum class LayerNormGate : uint8_t
195 {
196 Forget,
197 Cell,
198 Input,
199 Output,
200 Count
201 };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100202 static constexpr uint8_t _layer_norm_count = static_cast<uint8_t>(LayerNormGate::Count);
203 static constexpr uint32_t _out_state_output_size_dimension_idx = 0;
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100204
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000205 /** Internal method to configure matrix multiplication plus output stage of each gate.
206 *
207 * @param[in] mm Matrix multiplication function to use.
208 * @param[in] outstage Output stage function to use.
209 * @param[in] gemmlowp_info GEMMLowp metadata to be used by the output stage.
210 * @param[in] mm_input Input tensor to matrix multiplication function.
211 * @param[in] mm_weights Weights tensor to matrix multiplication function.
212 * @param[in] bias Bias tensor to matrix multiplication function.
213 * @param[in] outstage_res Tensor to be used for storing the result of the output stage.
214 * @param[in] gemmlowp_scale Real multiplier to be used computing multiplier and shift for requantization.
215 * @param[in] mm_res_info Tensor info to be used to initialize matrix multiplication result tensor.
216 * @param[in] mm_res_info Tensor info to be used to initialize output stage result tensor.
217 *
218 */
219 void configure_mm(NEGEMMLowpMatrixMultiplyCore &mm, NEGEMMLowpOutputStage &outstage, GEMMLowpOutputStageInfo &gemmlowp_info,
220 const ITensor *mm_input, const ITensor *mm_weights, const ITensor *bias, Tensor *mm_res,
221 Tensor *outstage_res, float gemmlowp_scale,
222 const TensorInfo &mm_res_info, const TensorInfo &outstage_tensor_info);
223
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100224 MemoryGroup _memory_group;
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000225
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100226 /** A small internel kernel do the copy between two tensors */
227 class TensorCopyKernel
228 {
229 static constexpr uint32_t max_dimension_supported = 2;
230
231 ITensor *_src{ nullptr };
232 ITensor *_dst{ nullptr };
233 size_t _row_size{};
234 Window _window{};
235
236 public:
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100237 /** Destructor */
238 ~TensorCopyKernel();
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100239 /** Static function to check if given info will lead to a valid configuration of @ref NEQLSTMLayer::TensorCopyKernel
240 *
241 * @param[in] src Source tensor info.
242 * @param[in] dst Destination tensor info
243 *
244 * @return a status
245 */
246 static Status validate(const ITensorInfo &src, const ITensorInfo &dst);
247 /** Set the input and output tensors.
248 *
249 * @param[in] src Source tensor
250 * @param[out] dst Destination tensor
251 */
252 void configure(ITensor &src, ITensor &dst);
253 /** run the kernel */
254 void run();
255 };
256
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000257 // Functions used
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100258 NETranspose _transpose_input_to_forget_weights;
259 NETranspose _transpose_input_to_cell_weights;
260 NETranspose _transpose_input_to_output_weights;
261 NETranspose _transpose_input_to_input_weights;
262 NETranspose _transpose_recurrent_to_forget_weights;
263 NETranspose _transpose_recurrent_to_cell_weights;
264 NETranspose _transpose_recurrent_to_output_weights;
265 NETranspose _transpose_recurrent_to_input_weights;
266 NETranspose _transpose_projection_weights;
267 std::unique_ptr<cpu::kernels::CpuGemmLowpMatrixAReductionKernel> _input_to_input_reduction;
268 std::unique_ptr<cpu::kernels::CpuGemmLowpMatrixAReductionKernel> _recurrent_to_input_reduction;
269 std::unique_ptr<cpu::kernels::CpuGemmLowpMatrixAReductionKernel> _input_to_forget_reduction;
270 std::unique_ptr<cpu::kernels::CpuGemmLowpMatrixAReductionKernel> _recurrent_to_forget_reduction;
271 std::unique_ptr<cpu::kernels::CpuGemmLowpMatrixAReductionKernel> _input_to_cell_reduction;
272 std::unique_ptr<cpu::kernels::CpuGemmLowpMatrixAReductionKernel> _recurrent_to_cell_reduction;
273 std::unique_ptr<cpu::kernels::CpuGemmLowpMatrixAReductionKernel> _input_to_output_reduction;
274 std::unique_ptr<cpu::kernels::CpuGemmLowpMatrixAReductionKernel> _recurrent_to_output_reduction;
275 std::unique_ptr<cpu::kernels::CpuGemmLowpMatrixAReductionKernel> _projection_reduction;
276 NEArithmeticAddition _projection_bias_add;
277 NEGEMMLowpMatrixMultiplyCore _mm_input_to_forget;
278 NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_forget;
279 NEPixelWiseMultiplication _pixelwise_mul_cell_to_forget;
280 NEGEMMLowpOutputStage _input_to_forget_outstage;
281 NEGEMMLowpOutputStage _recurrent_to_forget_outstage;
282 NEGEMMLowpOutputStage _cell_to_forget_outstage;
283 NEArithmeticAddition _accumulate_input_recurrent_forget;
284 NEArithmeticAddition _accumulate_cell_forget;
285 NEActivationLayer _forget_gate_sigmoid;
286 NEGEMMLowpMatrixMultiplyCore _mm_input_to_cell;
287 NEGEMMLowpOutputStage _input_to_cell_outstage;
288 NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_cell;
289 NEGEMMLowpOutputStage _recurrent_to_cell_outstage;
290 NEArithmeticAddition _accumulate_input_recurrent_modulation;
291 NEActivationLayer _cell_gate_tanh;
292 NEArithmeticSubtraction _input_gate_sub;
293 NEGEMMLowpMatrixMultiplyCore _mm_input_to_input;
294 NEGEMMLowpOutputStage _input_to_input_outstage;
295 NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_input;
296 NEGEMMLowpOutputStage _recurrent_to_input_outstage;
297 NEArithmeticAddition _accumulate_input_recurrent_input;
298 NEPixelWiseMultiplication _pixelwise_mul_cell_to_input;
299 NEGEMMLowpOutputStage _cell_to_input_outstage;
300 NEArithmeticAddition _accumulate_cell_input;
301 NEActivationLayer _input_gate_sigmoid;
302 NEPixelWiseMultiplication _pixelwise_mul_forget_cell;
303 NEPixelWiseMultiplication _pixelwise_mul_input_cell;
304 NEArithmeticAddition _add_forget_cell;
305 NEActivationLayer _cell_clip;
306 NEGEMMLowpMatrixMultiplyCore _mm_input_to_output;
307 NEGEMMLowpOutputStage _input_to_output_outstage;
308 NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_output;
309 NEGEMMLowpOutputStage _recurrent_to_output_outstage;
310 NEArithmeticAddition _accumulate_input_recurrent_output;
311 NEPixelWiseMultiplication _pixelwise_mul_cell_to_output;
312 NEGEMMLowpOutputStage _cell_to_output_outstage;
313 NEArithmeticAddition _accumulate_cell_to_output;
314 NEActivationLayer _output_gate_sigmoid;
315 NEActivationLayer _hidden_tanh;
316 NEPixelWiseMultiplication _pixelwise_mul_hidden;
317 NEGEMMLowpOutputStage _hidden_outstage;
318 NEGEMMLowpMatrixMultiplyCore _mm_projection;
319 NEGEMMLowpOutputStage _projection_outstage;
320 NEArithmeticAddition _accumulate_projection;
321 NEActivationLayer _projection_clip;
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100322
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100323 TensorCopyKernel _projection_bias_copy;
324 TensorCopyKernel _projection_output_to_accumulate_copy;
325 TensorCopyKernel _projection_accumulate_to_output_copy;
326 TensorCopyKernel _hidden_to_output_copy;
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100327
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100328 std::array<std::unique_ptr<NEQLSTMLayerNormalizationKernel>, _layer_norm_count> _layer_norms;
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000329
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100330 NECopy _copy_output;
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100331
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000332 // Tensor pointers
Michalis Spyrou173ba9b2020-06-23 17:25:43 +0100333 const ITensor *_input_to_input_weights
334 {
335 nullptr
336 };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000337 const ITensor *_recurrent_to_input_weights{ nullptr };
338 const ITensor *_projection_bias{ nullptr };
339 const ITensor *_input_to_forget_weights{ nullptr };
340 const ITensor *_input_to_cell_weights{ nullptr };
341 const ITensor *_input_to_output_weights{ nullptr };
342 const ITensor *_recurrent_to_forget_weights{ nullptr };
343 const ITensor *_recurrent_to_cell_weights{ nullptr };
344 const ITensor *_recurrent_to_output_weights{ nullptr };
345 const ITensor *_projection_weights{ nullptr };
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100346 std::array<const ITensor *, _layer_norm_count> _layer_norm_weights{};
347 std::array<const ITensor *, _layer_norm_count> _layer_norm_bias{};
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100348
349 using LayerNormIndexType = typename std::underlying_type<LayerNormGate>::type;
350 inline LayerNormIndexType getGateIndex(LayerNormGate g)
351 {
352 return static_cast<LayerNormIndexType>(g);
353 }
354
355 inline void set_layer_norm_weight(const ITensor *t, LayerNormGate g)
356 {
357 _layer_norm_weights[getGateIndex(g)] = t;
358 }
359
360 inline void set_layer_norm_bias(const ITensor *t, LayerNormGate g)
361 {
362 _layer_norm_bias[getGateIndex(g)] = t;
363 }
364
365 inline const ITensor *get_layer_norm_weight(LayerNormGate g)
366 {
367 return _layer_norm_weights[getGateIndex(g)];
368 }
369
370 inline const ITensor *get_layer_norm_bias(LayerNormGate g)
371 {
372 return _layer_norm_bias[getGateIndex(g)];
373 }
374
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100375 inline std::unique_ptr<NEQLSTMLayerNormalizationKernel> &get_layer_norm(LayerNormGate g)
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100376 {
377 return _layer_norms[getGateIndex(g)];
378 }
379
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100380 void configure_layer_norm(LayerNormGate g, const ITensor *in);
381 static Status validate_layer_norm(const ITensorInfo &in, const ITensorInfo &weight, const ITensorInfo &bias);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000382
383 // Temporary tensors
384 Tensor _input_to_forget_weights_transposed{ nullptr };
385 Tensor _input_to_cell_weights_transposed{ nullptr };
386 Tensor _input_to_output_weights_transposed{ nullptr };
387 Tensor _input_to_input_weights_transposed{ nullptr };
388 Tensor _recurrent_to_forget_weights_transposed{ nullptr };
389 Tensor _recurrent_to_cell_weights_transposed{ nullptr };
390 Tensor _recurrent_to_output_weights_transposed{ nullptr };
391 Tensor _recurrent_to_input_weights_transposed{ nullptr };
392 Tensor _projection_weights_transposed{ nullptr };
393 Tensor _input_to_input_eff_bias{ nullptr };
394 Tensor _recurrent_to_input_eff_bias{ nullptr };
395 Tensor _input_to_forget_eff_bias{ nullptr };
396 Tensor _recurrent_to_forget_eff_bias{ nullptr };
397 Tensor _input_to_cell_eff_bias{ nullptr };
398 Tensor _recurrent_to_cell_eff_bias{ nullptr };
399 Tensor _input_to_output_eff_bias{ nullptr };
400 Tensor _recurrent_to_output_eff_bias{ nullptr };
401 Tensor _projection_reduction_res{ nullptr };
402 Tensor _projection_eff_bias{ nullptr };
403 Tensor _mm_input_to_forget_res{ nullptr };
404 Tensor _mm_recurrent_to_forget_res{ nullptr };
405 Tensor _mul_cell_to_forget_res{ nullptr };
406 Tensor _input_to_forget_outstage_res{ nullptr };
407 Tensor _cell_to_forget_outstage_res{ nullptr };
408 Tensor _recurrent_to_forget_outstage_res{ nullptr };
409 Tensor _forget_gate{ nullptr };
410 Tensor _mm_input_to_cell_res{ nullptr };
411 Tensor _input_to_cell_outstage_res{ nullptr };
412 Tensor _mm_recurrent_to_cell_res{ nullptr };
413 Tensor _recurrent_to_cell_outstage_res{ nullptr };
414 Tensor _cell_gate{ nullptr };
415 Tensor _mul_input_cell_res{ nullptr };
416 Tensor _mm_input_to_input_res{ nullptr };
417 Tensor _input_to_input_outstage_res{ nullptr };
418 Tensor _mm_recurrent_to_input_res{ nullptr };
419 Tensor _mul_cell_to_input_res{ nullptr };
420 Tensor _cell_to_input_outstage_res{ nullptr };
421 Tensor _recurrent_to_input_outstage_res{ nullptr };
422 Tensor _input_gate{ nullptr };
423 Tensor _mm_input_to_output_res{ nullptr };
424 Tensor _input_to_output_outstage_res{ nullptr };
425 Tensor _mm_recurrent_to_output_res{ nullptr };
426 Tensor _mul_cell_to_output_res{ nullptr };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100427 Tensor _cell_to_output_outstage_res{ nullptr };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000428 Tensor _recurrent_to_output_outstage_res{ nullptr };
429 Tensor _output_gate{ nullptr };
430 Tensor _hidden_mul_res{ nullptr };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100431 Tensor _hidden_gate{ nullptr };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000432 Tensor _mm_projection_res{ nullptr };
433 Tensor _projection_outstage_res{ nullptr };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100434 Tensor _projection_out_res{ nullptr };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100435 Tensor _projection_accumulate_res{ nullptr };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000436 Tensor _ones{ nullptr };
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100437 std::array<Tensor, _layer_norm_count> _layer_norm_output{};
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100438
439 inline Tensor &get_layer_norm_output(LayerNormGate g)
440 {
441 return _layer_norm_output[getGateIndex(g)];
442 }
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000443
444 bool _is_prepared{ false };
445 bool _has_cifg{ false };
446 bool _has_cell_clipping{ false };
447 bool _has_projection{ false };
448 bool _has_projection_clipping{ false };
449 bool _has_peephole{ false };
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100450 bool _has_layer_norm{ false };
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100451 bool _projection_tensor_copy_required{ false };
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000452};
453} // namespace arm_compute
454#endif /* ARM_COMPUTE_NEQLSTMLAYER_H */