Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 1 | /* |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2021 Arm Limited. |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CLLSTMLAYERQUANTIZED_H |
| 25 | #define ARM_COMPUTE_CLLSTMLAYERQUANTIZED_H |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/Types.h" |
| 28 | #include "arm_compute/runtime/CL/functions/CLActivationLayer.h" |
| 29 | #include "arm_compute/runtime/CL/functions/CLConcatenateLayer.h" |
| 30 | #include "arm_compute/runtime/CL/functions/CLDequantizationLayer.h" |
| 31 | #include "arm_compute/runtime/CL/functions/CLElementwiseOperations.h" |
| 32 | #include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h" |
| 33 | #include "arm_compute/runtime/CL/functions/CLGEMMLowpOutputStage.h" |
| 34 | #include "arm_compute/runtime/CL/functions/CLPixelWiseMultiplication.h" |
| 35 | #include "arm_compute/runtime/CL/functions/CLQuantizationLayer.h" |
| 36 | #include "arm_compute/runtime/CL/functions/CLSlice.h" |
| 37 | #include "arm_compute/runtime/CL/functions/CLTranspose.h" |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 38 | #include "arm_compute/runtime/common/LSTMParams.h" |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | // Forward declarations |
| 43 | class ICLTensor; |
| 44 | |
| 45 | /** Basic function to run @ref CLLSTMLayerQuantized |
| 46 | * |
| 47 | * This function calls the following CL functions/kernels: |
| 48 | * |
Georgios Pinitas | 4a578b9 | 2021-06-25 12:13:49 +0100 | [diff] [blame] | 49 | * -# @ref CLGEMMLowpMatrixMultiplyCore Quantized matrix multiplication core. Accumulators are 32-bit integers |
| 50 | * -# @ref CLGEMMLowpOutputStage Convert 32-bit integers into QSYMM16 |
| 51 | * -# @ref CLTranspose Matrix transpose |
| 52 | * -# @ref CLConcatenateLayer Tensor concatenation |
| 53 | * -# @ref CLActivationLayer Activation functions (tanh and logistic) |
| 54 | * -# @ref CLArithmeticAddition Elementwise addition |
| 55 | * -# @ref CLPixelWiseMultiplication Elementwise multiplication |
| 56 | * -# @ref CLSlice Tensor slicing |
| 57 | * -# @ref CLDequantizationLayer Dequantize into float |
| 58 | * -# @ref CLQuantizationLayer Quantize from float |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 59 | * */ |
| 60 | class CLLSTMLayerQuantized : public IFunction |
| 61 | { |
| 62 | public: |
| 63 | /** Default constructor */ |
| 64 | CLLSTMLayerQuantized(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
| 65 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 66 | CLLSTMLayerQuantized(const CLLSTMLayerQuantized &) = delete; |
| 67 | /** Default move constructor */ |
| 68 | CLLSTMLayerQuantized(CLLSTMLayerQuantized &&) = default; |
| 69 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 70 | CLLSTMLayerQuantized &operator=(const CLLSTMLayerQuantized &) = delete; |
| 71 | /** Default move assignment operator */ |
| 72 | CLLSTMLayerQuantized &operator=(CLLSTMLayerQuantized &&) = default; |
| 73 | /** Initialize function's tensors. |
| 74 | * |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 75 | * Valid data layouts: |
| 76 | * - All |
| 77 | * |
| 78 | * Valid data type configurations: |
| 79 | * |src0 - src8 |src9 - src12 |src13 |src14 |dst0 |dst1 | |
| 80 | * |:-----------|:------------|:-------|:------|:------|:------| |
| 81 | * |QASYMM8 |S32 |QSYMM16 |QASYMM8|QSYMM16|QASYMM8| |
| 82 | * |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 83 | * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: QASYMM8. |
| 84 | * @param[in] input_to_input_weights 2D weights tensor with dimensions [input_size, output_size]. Data type supported: Same as @p input. |
| 85 | * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, output_size]. Data type supported: Same as @p input. |
| 86 | * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, output_size]. Data type supported: Same as @p input. |
| 87 | * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, output_size]. Data type supported: Same as @p input. |
| 88 | * @param[in] recurrent_to_input_weights 2D weights tensor with dimensions [output_size, output_size]. Data type supported: Same as @p input. |
| 89 | * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, output_size]. Data type supported: Same as @p input. |
| 90 | * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, output_size]. Data type supported: Same as @p input. |
| 91 | * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, output_size]. Data type supported: Same as @p input. |
| 92 | * @param[in] input_gate_bias 1D weights tensor with dimensions [output_size]. Data type supported: S32. |
| 93 | * @param[in] forget_gate_bias 1D weights tensor with dimensions [output_size]. Data type supported: S32. |
| 94 | * @param[in] cell_bias 1D weights tensor with dimensions [output_size]. Data type supported: S32. |
| 95 | * @param[in] output_gate_bias 1D weights tensor with dimensions [output_size]. Data type supported: S32. |
| 96 | * @param[in] cell_state_in 2D tensor with dimensions [output_size, batch_size]. Data type supported: QSYMM16. |
| 97 | * @param[in] output_state_in 2D tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. |
| 98 | * @param[out] cell_state_out Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size]. Data type supported: QSYMM16. |
| 99 | * @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. |
| 100 | */ |
| 101 | void configure(const ICLTensor *input, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 102 | const ICLTensor *input_to_input_weights, |
| 103 | const ICLTensor *input_to_forget_weights, |
| 104 | const ICLTensor *input_to_cell_weights, |
| 105 | const ICLTensor *input_to_output_weights, |
| 106 | const ICLTensor *recurrent_to_input_weights, |
| 107 | const ICLTensor *recurrent_to_forget_weights, |
| 108 | const ICLTensor *recurrent_to_cell_weights, |
| 109 | const ICLTensor *recurrent_to_output_weights, |
| 110 | const ICLTensor *input_gate_bias, |
| 111 | const ICLTensor *forget_gate_bias, |
| 112 | const ICLTensor *cell_bias, |
| 113 | const ICLTensor *output_gate_bias, |
| 114 | ICLTensor *cell_state_in, |
| 115 | const ICLTensor *output_state_in, |
| 116 | ICLTensor *cell_state_out, |
| 117 | ICLTensor *output_state_out); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 118 | /** Initialize function's tensors. |
| 119 | * |
| 120 | * @param[in] compile_context The compile context to be used. |
| 121 | * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: QASYMM8. |
| 122 | * @param[in] input_to_input_weights 2D weights tensor with dimensions [input_size, output_size]. Data type supported: Same as @p input. |
| 123 | * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, output_size]. Data type supported: Same as @p input. |
| 124 | * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, output_size]. Data type supported: Same as @p input. |
| 125 | * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, output_size]. Data type supported: Same as @p input. |
| 126 | * @param[in] recurrent_to_input_weights 2D weights tensor with dimensions [output_size, output_size]. Data type supported: Same as @p input. |
| 127 | * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, output_size]. Data type supported: Same as @p input. |
| 128 | * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, output_size]. Data type supported: Same as @p input. |
| 129 | * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, output_size]. Data type supported: Same as @p input. |
| 130 | * @param[in] input_gate_bias 1D weights tensor with dimensions [output_size]. Data type supported: S32. |
| 131 | * @param[in] forget_gate_bias 1D weights tensor with dimensions [output_size]. Data type supported: S32. |
| 132 | * @param[in] cell_bias 1D weights tensor with dimensions [output_size]. Data type supported: S32. |
| 133 | * @param[in] output_gate_bias 1D weights tensor with dimensions [output_size]. Data type supported: S32. |
| 134 | * @param[in] cell_state_in 2D tensor with dimensions [output_size, batch_size]. Data type supported: QSYMM16. |
| 135 | * @param[in] output_state_in 2D tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. |
| 136 | * @param[out] cell_state_out Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size]. Data type supported: QSYMM16. |
| 137 | * @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. |
| 138 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 139 | void configure(const CLCompileContext &compile_context, |
| 140 | const ICLTensor *input, |
| 141 | const ICLTensor *input_to_input_weights, |
| 142 | const ICLTensor *input_to_forget_weights, |
| 143 | const ICLTensor *input_to_cell_weights, |
| 144 | const ICLTensor *input_to_output_weights, |
| 145 | const ICLTensor *recurrent_to_input_weights, |
| 146 | const ICLTensor *recurrent_to_forget_weights, |
| 147 | const ICLTensor *recurrent_to_cell_weights, |
| 148 | const ICLTensor *recurrent_to_output_weights, |
| 149 | const ICLTensor *input_gate_bias, |
| 150 | const ICLTensor *forget_gate_bias, |
| 151 | const ICLTensor *cell_bias, |
| 152 | const ICLTensor *output_gate_bias, |
| 153 | ICLTensor *cell_state_in, |
| 154 | const ICLTensor *output_state_in, |
| 155 | ICLTensor *cell_state_out, |
| 156 | ICLTensor *output_state_out); |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 157 | |
| 158 | /** Static function to check if given info will lead to a valid configuration of @ref CLLSTMLayerQuantized |
| 159 | * |
| 160 | * @param[in] input Source tensor info. Input is a 2D tensor info with dimensions [input_size, batch_size]. Data types supported: QASYMM8. |
| 161 | * @param[in] input_to_input_weights 2D weights tensor info with dimensions [input_size, output_size]. Data type supported: Same as @p input. |
| 162 | * @param[in] input_to_forget_weights 2D weights tensor info with dimensions [input_size, output_size]. Data type supported: Same as @p input. |
| 163 | * @param[in] input_to_cell_weights 2D weights tensor info with dimensions [input_size, output_size]. Data type supported: Same as @p input. |
| 164 | * @param[in] input_to_output_weights 2D weights tensor info with dimensions [input_size, output_size]. Data type supported: Same as @p input. |
| 165 | * @param[in] recurrent_to_input_weights 2D weights tensor info with dimensions [output_size, output_size]. Data type supported: Same as @p input. |
| 166 | * @param[in] recurrent_to_forget_weights 2D weights tensor info with dimensions [output_size, output_size]. Data type supported: Same as @p input. |
| 167 | * @param[in] recurrent_to_cell_weights 2D weights tensor info with dimensions [output_size, output_size]. Data type supported: Same as @p input. |
| 168 | * @param[in] recurrent_to_output_weights 2D weights tensor info with dimensions [output_size, output_size]. Data type supported: Same as @p input. |
| 169 | * @param[in] input_gate_bias 1D weights tensor info with dimensions [output_size]. Data type supported: S32. |
| 170 | * @param[in] forget_gate_bias 1D weights tensor info with dimensions [output_size]. Data type supported: S32. |
| 171 | * @param[in] cell_bias 1D weights tensor info with dimensions [output_size]. Data type supported: S32. |
| 172 | * @param[in] output_gate_bias 1D weights tensor info with dimensions [output_size]. Data type supported: S32. |
| 173 | * @param[in] cell_state_in 2D tensor info with dimensions [output_size, batch_size]. Data type supported: QSYMM16. |
| 174 | * @param[in] output_state_in 2D tensor info with dimensions [output_size, batch_size]. Data type supported: Same as @p input. |
| 175 | * @param[out] cell_state_out Destination tensor info. Output is a 2D tensor info with dimensions [output_size, batch_size]. Data type supported: QSYMM16. |
| 176 | * @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. |
| 177 | * |
| 178 | * @return a status |
| 179 | */ |
| 180 | static Status validate(const ITensorInfo *input, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 181 | const ITensorInfo *input_to_input_weights, |
| 182 | const ITensorInfo *input_to_forget_weights, |
| 183 | const ITensorInfo *input_to_cell_weights, |
| 184 | const ITensorInfo *input_to_output_weights, |
| 185 | const ITensorInfo *recurrent_to_input_weights, |
| 186 | const ITensorInfo *recurrent_to_forget_weights, |
| 187 | const ITensorInfo *recurrent_to_cell_weights, |
| 188 | const ITensorInfo *recurrent_to_output_weights, |
| 189 | const ITensorInfo *input_gate_bias, |
| 190 | const ITensorInfo *forget_gate_bias, |
| 191 | const ITensorInfo *cell_bias, |
| 192 | const ITensorInfo *output_gate_bias, |
| 193 | const ITensorInfo *cell_state_in, |
| 194 | const ITensorInfo *output_state_in, |
| 195 | const ITensorInfo *cell_state_out, |
| 196 | const ITensorInfo *output_state_out); |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 197 | |
| 198 | // Inherited methods overridden: |
| 199 | void run() override; |
| 200 | void prepare() override; |
| 201 | |
| 202 | private: |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 203 | MemoryGroup _memory_group; |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 204 | |
| 205 | // Functions used |
Georgios Pinitas | 4a578b9 | 2021-06-25 12:13:49 +0100 | [diff] [blame] | 206 | CLGEMMLowpMatrixMultiplyCore _gemmlowp; |
| 207 | CLGEMMLowpOutputStage _output_stage; |
| 208 | CLTranspose _transpose_weights; |
| 209 | CLConcatenateLayer _concat_input_weights; |
| 210 | CLConcatenateLayer _concat_recurrent_weights; |
| 211 | CLConcatenateLayer _concat_weights; |
| 212 | CLConcatenateLayer _concat_inputs; |
| 213 | CLConcatenateLayer _concat_bias; |
| 214 | CLActivationLayer _sigmoid_forget_gate; |
| 215 | CLActivationLayer _sigmoid_input_gate; |
| 216 | CLActivationLayer _sigmoid_output_gate; |
| 217 | CLActivationLayer _tanh_modulation_gate; |
| 218 | CLActivationLayer _tanh_output_state; |
| 219 | CLArithmeticAddition _add_cell_state_tmps; |
| 220 | CLArithmeticAddition _add2; |
| 221 | CLPixelWiseMultiplication _mul_forget_gate_cell_state; |
| 222 | CLPixelWiseMultiplication _mul_input_gate_input_mod_gate; |
| 223 | CLPixelWiseMultiplication _mul_output_state_tmp_output_gate; |
| 224 | CLSlice _slice_input_tensor; |
| 225 | CLSlice _slice_forget_tensor; |
| 226 | CLSlice _slice_cell_tensor; |
| 227 | CLSlice _slice_output_tensor; |
| 228 | CLDequantizationLayer _dequantize; |
| 229 | CLQuantizationLayer _quantize; |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 230 | |
| 231 | // Tensor pointers |
| 232 | const ICLTensor *_input_to_input_weights; |
| 233 | const ICLTensor *_input_to_forget_weights; |
| 234 | const ICLTensor *_input_to_cell_weights; |
| 235 | const ICLTensor *_input_to_output_weights; |
| 236 | const ICLTensor *_recurrent_to_input_weights; |
| 237 | const ICLTensor *_recurrent_to_forget_weights; |
| 238 | const ICLTensor *_recurrent_to_cell_weights; |
| 239 | const ICLTensor *_recurrent_to_output_weights; |
| 240 | const ICLTensor *_input_gate_bias; |
| 241 | const ICLTensor *_forget_gate_bias; |
| 242 | const ICLTensor *_cell_bias; |
| 243 | const ICLTensor *_output_gate_bias; |
| 244 | |
| 245 | // Temporary tensors |
| 246 | CLTensor _recurrent_weights; |
| 247 | CLTensor _input_weights; |
| 248 | CLTensor _weights; |
| 249 | CLTensor _input; |
| 250 | CLTensor _weights_transposed; |
| 251 | CLTensor _output_highp; |
| 252 | CLTensor _output_lowp; |
| 253 | CLTensor _bias; |
| 254 | CLTensor _forget_gate_input; |
| 255 | CLTensor _input_gate_input; |
| 256 | CLTensor _output_gate_input; |
| 257 | CLTensor _input_modulation_gate_input; |
| 258 | CLTensor _forget_gate_output; |
| 259 | CLTensor _input_gate_output; |
| 260 | CLTensor _output_gate_output; |
| 261 | CLTensor _input_modulation_gate_output; |
| 262 | CLTensor _cell_state_tmp1; |
| 263 | CLTensor _cell_state_tmp2; |
| 264 | CLTensor _output_state_tmp; |
| 265 | CLTensor _output_state_out_symm; |
| 266 | CLTensor _output_state_out_f32; |
| 267 | |
| 268 | bool _is_prepared; |
| 269 | }; |
| 270 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 271 | #endif /* ARM_COMPUTE_CLLSTMLAYERQUANTIZED_H */ |