Debian Codegraph @

arm_compute/runtime/NEON/functions/NELSTMLayerQuantized.h

raw
1 /*
2 * Copyright (c) 2019-2021, 2024 Arm Limited.
3 *
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 ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NELSTMLAYERQUANTIZED_H
25 #define ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NELSTMLAYERQUANTIZED_H
26
27 #include "arm_compute/core/Types.h"
28 #include "arm_compute/runtime/common/LSTMParams.h"
29 #include "arm_compute/runtime/MemoryManagerOnDemand.h"
30 #include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
31 #include "arm_compute/runtime/NEON/functions/NEArithmeticAddition.h"
32 #include "arm_compute/runtime/NEON/functions/NEConcatenateLayer.h"
33 #include "arm_compute/runtime/NEON/functions/NEDequantizationLayer.h"
34 #include "arm_compute/runtime/NEON/functions/NEElementwiseOperations.h"
35 #include "arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h"
36 #include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h"
37 #include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h"
38 #include "arm_compute/runtime/NEON/functions/NEPixelWiseMultiplication.h"
39 #include "arm_compute/runtime/NEON/functions/NEQuantizationLayer.h"
40 #include "arm_compute/runtime/NEON/functions/NESlice.h"
41 #include "arm_compute/runtime/NEON/functions/NETranspose.h"
42
43 namespace arm_compute
44 {
45 // Forward declarations
46 class ITensor;
47
48 /** Basic function to run @ref NELSTMLayerQuantized
49 *
50 * This function calls the following functions/kernels:
51 *
52 * -# @ref NEGEMMLowpMatrixMultiplyCore Quantized matrix multiplication core. Accumulators are 32-bit integers
53 * -# @ref NEGEMMLowpOutputStage Convert 32-bit integers into QSYMM16
54 * -# @ref NETranspose Matrix transpose
55 * -# @ref NEConcatenateLayer Tensor concatenation
56 * -# @ref NEActivationLayer Activation functions (tanh and logistic)
57 * -# @ref NEArithmeticAddition Elementwise addition
58 * -# @ref NEPixelWiseMultiplication Elementwise multiplication
59 * -# @ref NESlice Tensor slicing
60 * -# @ref NEDequantizationLayer Dequantize into float
61 * -# @ref NEQuantizationLayer Quantize from float
62 * */
63 class NELSTMLayerQuantized : public IFunction
64 {
65 public:
66 /** Default constructor */
67 NELSTMLayerQuantized(std::shared_ptr<IMemoryManager> memory_manager);
68 NELSTMLayerQuantized() : NELSTMLayerQuantized(MemoryManagerOnDemand::make_default())
69 {
70 }
71 /** Prevent instances of this class from being copied (As this class contains pointers) */
72 NELSTMLayerQuantized(const NELSTMLayerQuantized &) = delete;
73 /** Prevent instances of this class from being moved (As this class contains pointers) */
74 NELSTMLayerQuantized(NELSTMLayerQuantized &&) = delete;
75 /** Prevent instances of this class from being copied (As this class contains pointers) */
76 NELSTMLayerQuantized &operator=(const NELSTMLayerQuantized &) = delete;
77 /** Prevent instances of this class from being moved (As this class contains pointers) */
78 NELSTMLayerQuantized &operator=(NELSTMLayerQuantized &&) = delete;
79 /** Default destructor */
80 ~NELSTMLayerQuantized();
81 /** Initialize function's tensors.
82 *
83 * Valid data layouts:
84 * - All
85 *
86 * Valid data type configurations:
87 * |src0 - src8 |src9 - src12 |src13 |src14 |dst0 |dst1 |
88 * |:-----------|:------------|:-------|:------|:------|:------|
89 * |QASYMM8 |S32 |QSYMM16 |QASYMM8|QSYMM16|QASYMM8|
90 *
91 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: QASYMM8.
92 * @param[in] input_to_input_weights 2D weights tensor with dimensions [input_size, output_size]. Data type supported: Same as @p input.
93 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, output_size]. Data type supported: Same as @p input.
94 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, output_size]. Data type supported: Same as @p input.
95 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, output_size]. Data type supported: Same as @p input.
96 * @param[in] recurrent_to_input_weights 2D weights tensor with dimensions [output_size, output_size]. Data type supported: Same as @p input.
97 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, output_size]. Data type supported: Same as @p input.
98 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, output_size]. Data type supported: Same as @p input.
99 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, output_size]. Data type supported: Same as @p input.
100 * @param[in] input_gate_bias 1D weights tensor with dimensions [output_size]. Data type supported: S32.
101 * @param[in] forget_gate_bias 1D weights tensor with dimensions [output_size]. Data type supported: S32.
102 * @param[in] cell_bias 1D weights tensor with dimensions [output_size]. Data type supported: S32.
103 * @param[in] output_gate_bias 1D weights tensor with dimensions [output_size]. Data type supported: S32.
104 * @param[in] cell_state_in 2D tensor with dimensions [output_size, batch_size]. Data type supported: QSYMM16.
105 * @param[in] output_state_in 2D tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
106 * @param[out] cell_state_out Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size]. Data type supported: QSYMM16.
107 * @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.
108 */
109 void configure(const ITensor *input,
110 const ITensor *input_to_input_weights,
111 const ITensor *input_to_forget_weights,
112 const ITensor *input_to_cell_weights,
113 const ITensor *input_to_output_weights,
114 const ITensor *recurrent_to_input_weights,
115 const ITensor *recurrent_to_forget_weights,
116 const ITensor *recurrent_to_cell_weights,
117 const ITensor *recurrent_to_output_weights,
118 const ITensor *input_gate_bias,
119 const ITensor *forget_gate_bias,
120 const ITensor *cell_bias,
121 const ITensor *output_gate_bias,
122 ITensor *cell_state_in,
123 const ITensor *output_state_in,
124 ITensor *cell_state_out,
125 ITensor *output_state_out);
126
127 /** Static function to check if given info will lead to a valid configuration of @ref NELSTMLayer
128 *
129 * @param[in] input Source tensor info. Input is a 2D tensor info with dimensions [input_size, batch_size]. Data types supported: QASYMM8.
130 * @param[in] input_to_input_weights 2D weights tensor info with dimensions [input_size, output_size]. Data type supported: Same as @p input.
131 * @param[in] input_to_forget_weights 2D weights tensor info with dimensions [input_size, output_size]. Data type supported: Same as @p input.
132 * @param[in] input_to_cell_weights 2D weights tensor info with dimensions [input_size, output_size]. Data type supported: Same as @p input.
133 * @param[in] input_to_output_weights 2D weights tensor info with dimensions [input_size, output_size]. Data type supported: Same as @p input.
134 * @param[in] recurrent_to_input_weights 2D weights tensor info with dimensions [output_size, output_size]. Data type supported: Same as @p input.
135 * @param[in] recurrent_to_forget_weights 2D weights tensor info with dimensions [output_size, output_size]. Data type supported: Same as @p input.
136 * @param[in] recurrent_to_cell_weights 2D weights tensor info with dimensions [output_size, output_size]. Data type supported: Same as @p input.
137 * @param[in] recurrent_to_output_weights 2D weights tensor info with dimensions [output_size, output_size]. Data type supported: Same as @p input.
138 * @param[in] input_gate_bias 1D weights tensor info with dimensions [output_size]. Data type supported: S32.
139 * @param[in] forget_gate_bias 1D weights tensor info with dimensions [output_size]. Data type supported: S32.
140 * @param[in] cell_bias 1D weights tensor info with dimensions [output_size]. Data type supported: S32.
141 * @param[in] output_gate_bias 1D weights tensor info with dimensions [output_size]. Data type supported: S32.
142 * @param[in] cell_state_in 2D tensor info with dimensions [output_size, batch_size]. Data type supported: QSYMM16.
143 * @param[in] output_state_in 2D tensor info with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
144 * @param[out] cell_state_out Destination tensor info. Output is a 2D tensor info with dimensions [output_size, batch_size]. Data type supported: QSYMM16.
145 * @param[out] 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.
146 *
147 * @return a status
148 */
149 static Status validate(const ITensorInfo *input,
150 const ITensorInfo *input_to_input_weights,
151 const ITensorInfo *input_to_forget_weights,
152 const ITensorInfo *input_to_cell_weights,
153 const ITensorInfo *input_to_output_weights,
154 const ITensorInfo *recurrent_to_input_weights,
155 const ITensorInfo *recurrent_to_forget_weights,
156 const ITensorInfo *recurrent_to_cell_weights,
157 const ITensorInfo *recurrent_to_output_weights,
158 const ITensorInfo *input_gate_bias,
159 const ITensorInfo *forget_gate_bias,
160 const ITensorInfo *cell_bias,
161 const ITensorInfo *output_gate_bias,
162 const ITensorInfo *cell_state_in,
163 const ITensorInfo *output_state_in,
164 const ITensorInfo *cell_state_out,
165 const ITensorInfo *output_state_out);
166
167 // Inherited methods overridden:
168 void run() override;
169 void prepare() override;
170
171 private:
172 MemoryGroup _memory_group;
173
174 // Functions used
175 NEGEMMLowpMatrixMultiplyCore _gemmlowp;
176 NEGEMMLowpOutputStage _output_stage;
177 NETranspose _transpose_weights;
178 NEConcatenateLayer _concat_input_weights;
179 NEConcatenateLayer _concat_recurrent_weights;
180 NEConcatenateLayer _concat_weights;
181 NEConcatenateLayer _concat_inputs;
182 NEConcatenateLayer _concat_bias;
183 NEActivationLayer _sigmoid_forget_gate;
184 NEActivationLayer _sigmoid_input_gate;
185 NEActivationLayer _sigmoid_output_gate;
186 NEActivationLayer _tanh_modulation_gate;
187 NEActivationLayer _tanh_output_state;
188 NEArithmeticAddition _add1;
189 NEArithmeticAddition _add2;
190 NEPixelWiseMultiplication _mul1;
191 NEPixelWiseMultiplication _mul2;
192 NEPixelWiseMultiplication _mul3;
193 NESlice _slice_input_tensor;
194 NESlice _slice_forget_tensor;
195 NESlice _slice_cell_tensor;
196 NESlice _slice_output_tensor;
197 NEDequantizationLayer _dequantize;
198 NEQuantizationLayer _quantize;
199
200 // Tensor pointers
201 const ITensor *_input_to_input_weights;
202 const ITensor *_input_to_forget_weights;
203 const ITensor *_input_to_cell_weights;
204 const ITensor *_input_to_output_weights;
205 const ITensor *_recurrent_to_input_weights;
206 const ITensor *_recurrent_to_forget_weights;
207 const ITensor *_recurrent_to_cell_weights;
208 const ITensor *_recurrent_to_output_weights;
209 const ITensor *_input_gate_bias;
210 const ITensor *_forget_gate_bias;
211 const ITensor *_cell_bias;
212 const ITensor *_output_gate_bias;
213
214 // Temporary tensors
215 Tensor _recurrent_weights;
216 Tensor _input_weights;
217 Tensor _weights;
218 Tensor _input;
219 Tensor _weights_transposed;
220 Tensor _output_highp;
221 Tensor _output_lowp;
222 Tensor _bias;
223 Tensor _forget_gate_input;
224 Tensor _input_gate_input;
225 Tensor _output_gate_input;
226 Tensor _input_modulation_gate_input;
227 Tensor _forget_gate_output;
228 Tensor _input_gate_output;
229 Tensor _output_gate_output;
230 Tensor _input_modulation_gate_output;
231 Tensor _cell_state1;
232 Tensor _cell_state2;
233 Tensor _output_state_tmp;
234 Tensor _output_state_out_symm;
235 Tensor _output_state_out_f32;
236
237 bool _is_prepared;
238 };
239 } // namespace arm_compute
240 #endif // ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NELSTMLAYERQUANTIZED_H