Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2021 Arm Limited. |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [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 | */ |
| 24 | #ifndef ARM_COMPUTE_NEQLSTMLAYER_H |
| 25 | #define ARM_COMPUTE_NEQLSTMLAYER_H |
| 26 | |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Types.h" |
| 28 | #include "arm_compute/runtime/NEON/functions/NEActivationLayer.h" |
Michalis Spyrou | 173ba9b | 2020-06-23 17:25:43 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/NEON/functions/NEArithmeticAddition.h" |
| 30 | #include "arm_compute/runtime/NEON/functions/NEArithmeticSubtraction.h" |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/NEON/functions/NECopy.h" |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 32 | #include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h" |
| 33 | #include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h" |
Michalis Spyrou | 6eb7345 | 2020-07-02 17:39:25 +0100 | [diff] [blame] | 34 | #include "arm_compute/runtime/NEON/functions/NEPixelWiseMultiplication.h" |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 35 | #include "arm_compute/runtime/NEON/functions/NETranspose.h" |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 36 | #include "arm_compute/runtime/common/LSTMParams.h" |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 37 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 38 | #include <memory> |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | // Forward declarations |
| 43 | class ITensor; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 44 | class ITensorInfo; |
| 45 | class NEQLSTMLayerNormalizationKernel; |
| 46 | class NEGEMMLowpMatrixAReductionKernel; |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 47 | |
| 48 | /** Basic function to run @ref NEQLSTMLayer |
| 49 | * |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 50 | * This function calls the following kernels: |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 51 | * |
| 52 | * -# @ref NEActivationLayer Activation functions (tanh and logistic) |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 53 | * -# @ref NEArithmeticAddition Elementwise addition |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 54 | * -# @ref NEArithmeticSubtraction Elementwise subtraction |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 55 | * -# @ref NECopy Copy kernel for copying output_state_out to output |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 56 | * -# @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 Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 59 | * -# @ref NEPixelWiseMultiplication Elementwise multiplication |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 60 | * -# @ref NETranspose Transpose function for reshaping the weights |
| 61 | * */ |
| 62 | class NEQLSTMLayer : public IFunction |
| 63 | { |
| 64 | public: |
| 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 Spyrou | 770dfeb | 2020-11-04 18:55:34 +0000 | [diff] [blame] | 69 | /** Prevent instances of this class from being moved (As this class contains pointers) */ |
| 70 | NEQLSTMLayer(NEQLSTMLayer &&) = delete; |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 71 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 72 | NEQLSTMLayer &operator=(const NEQLSTMLayer &) = delete; |
Michalis Spyrou | 770dfeb | 2020-11-04 18:55:34 +0000 | [diff] [blame] | 73 | /** Prevent instances of this class from being moved (As this class contains pointers) */ |
| 74 | NEQLSTMLayer &operator=(NEQLSTMLayer &&) = delete; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 75 | /** Default destructor */ |
| 76 | ~NEQLSTMLayer(); |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 77 | /** Initialize function's tensors. |
| 78 | * |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 79 | * Valid data layouts: |
| 80 | * - All |
| 81 | * |
| 82 | * Valid data type configurations: |
| 83 | * |src0 |src1 - src6 |src7 -src9 |src10 |src11 |dst0 |dst1 - dst2 | |
| 84 | * |:-------------|:------------|:------------|:------|:-------------|:------|:-----------------| |
| 85 | * |QASYMM8_SIGNED|QASYMM8 |S32 |QSYMM16|QASYMM8_SIGNED|QSYMM16|QASYMM8_SIGNED | |
| 86 | * |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 87 | * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: QASYMM8_SIGNED. |
| 88 | * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8. |
| 89 | * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8. |
| 90 | * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8. |
| 91 | * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8. |
| 92 | * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8. |
| 93 | * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8. |
| 94 | * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32. |
| 95 | * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32. |
| 96 | * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32. |
Michele Di Giorgio | beb2d45 | 2020-05-11 16:17:51 +0100 | [diff] [blame] | 97 | * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: QSYMM16. |
| 98 | * @param[in] output_state_in 2D tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. |
| 99 | * @param[out] cell_state_out Destination tensor. Output is a 2D tensor with dimensions [num_units, batch_size]. Data type supported: QSYMM16. |
| 100 | * @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. |
| 101 | * @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 Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 102 | * @param[in] lstm_params Weights tensors used in peephole, CIFG and layer normalization optimizations: |
| 103 | * input_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at input gate. |
| 104 | * forget_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at forget gate. |
| 105 | * cell_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at cell gate. |
| 106 | * output_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at output gate. |
| 107 | * hidden_state_zero The zero point of the hidden state. |
| 108 | * hidden_state_scale The scale of the hidden state. |
| 109 | * input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8. |
| 110 | * recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8. |
| 111 | * cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: QSYMM16. |
| 112 | * cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. |
| 113 | * cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. |
| 114 | * input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: S32. |
| 115 | * projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8. |
| 116 | * projection_bias (Optional) 1D weights tensor with dimensions [output_size]. S32. |
| 117 | * input_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. |
| 118 | * forget_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. |
| 119 | * cell_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. |
| 120 | * output_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. |
| 121 | * cell_threshold (Optional) The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip]. |
| 122 | * If set to 0.0 then clipping is disabled. |
| 123 | * projection_threshold (Optional) The clipping threshold for the output from the projection layer, such that values are bound within |
| 124 | * [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled. |
| 125 | */ |
| 126 | void configure(const ITensor *input, |
| 127 | const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights, |
| 128 | const ITensor *recurrent_to_forget_weights, const ITensor *recurrent_to_cell_weights, const ITensor *recurrent_to_output_weights, |
| 129 | const ITensor *forget_gate_bias, const ITensor *cell_bias, const ITensor *output_gate_bias, |
Sang-Hoon Park | 840a72c | 2020-09-23 13:24:13 +0100 | [diff] [blame] | 130 | const ITensor *cell_state_in, ITensor *output_state_in, |
Michele Di Giorgio | beb2d45 | 2020-05-11 16:17:51 +0100 | [diff] [blame] | 131 | ITensor *cell_state_out, ITensor *output_state_out, ITensor *output, |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 132 | const LSTMParams<ITensor> &lstm_params); |
| 133 | |
| 134 | /** Static function to check if given info will lead to a valid configuration of @ref NEQLSTMLayer |
| 135 | * |
Michele Di Giorgio | beb2d45 | 2020-05-11 16:17:51 +0100 | [diff] [blame] | 136 | * @param[in] input Source tensor info. Input is a 2D tensor info with dimensions [input_size, batch_size]. Data types supported: QASYMM8_SIGNED. |
| 137 | * @param[in] input_to_forget_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8. |
| 138 | * @param[in] input_to_cell_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8. |
| 139 | * @param[in] input_to_output_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8. |
| 140 | * @param[in] recurrent_to_forget_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8. |
| 141 | * @param[in] recurrent_to_cell_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8. |
| 142 | * @param[in] recurrent_to_output_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8. |
| 143 | * @param[in] forget_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32. |
| 144 | * @param[in] cell_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32. |
| 145 | * @param[in] output_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32. |
| 146 | * @param[in] cell_state_in 2D tensor info with dimensions [num_units, batch_size]. Data type supported: QSYMM16. |
| 147 | * @param[in] output_state_in 2D tensor info with dimensions [output_size, batch_size]. Data type supported: Same as @p input. |
| 148 | * @param[in] cell_state_out Destination tensor info. Output is a 2D tensor info with dimensions [num_units, batch_size]. Data type supported: QSYMM16. |
| 149 | * @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. |
| 150 | * @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. |
| 151 | * @param[in] lstm_params Weights tensors info used in peephole, CIFG and layer normalization optimizations: |
| 152 | * input_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at input gate. |
| 153 | * forget_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at forget gate. |
| 154 | * cell_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at cell gate. |
| 155 | * output_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at output gate. |
| 156 | * hidden_state_zero The zero point of the hidden state. |
| 157 | * hidden_state_scale The scale of the hidden state. |
| 158 | * input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8. |
| 159 | * recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8. |
| 160 | * cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: QSYMM16. |
| 161 | * cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. |
| 162 | * cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. |
| 163 | * input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: S32. |
| 164 | * projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8. |
| 165 | * projection_bias (Optional) 1D weights tensor with dimensions [output_size]. S32. |
| 166 | * input_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. |
| 167 | * forget_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. |
| 168 | * cell_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. |
| 169 | * output_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. |
| 170 | * cell_threshold (Optional) The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip]. |
| 171 | * If set to 0.0 then clipping is disabled. |
| 172 | * projection_threshold (Optional) The clipping threshold for the output from the projection layer, such that values are bound within |
| 173 | * [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled. |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 174 | * @return a status |
| 175 | */ |
| 176 | static Status validate(const ITensorInfo *input, |
| 177 | const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights, |
| 178 | const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights, |
| 179 | const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias, |
| 180 | const ITensorInfo *cell_state_in, const ITensorInfo *output_state_in, |
Michele Di Giorgio | beb2d45 | 2020-05-11 16:17:51 +0100 | [diff] [blame] | 181 | const ITensorInfo *cell_state_out, const ITensorInfo *output_state_out, const ITensorInfo *output, |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 182 | const LSTMParams<ITensorInfo> &lstm_params); |
| 183 | |
| 184 | // Inherited methods overridden: |
| 185 | void run() override; |
| 186 | void prepare() override; |
| 187 | |
| 188 | private: |
Sang-Hoon Park | 9230e27 | 2020-04-18 00:46:34 +0100 | [diff] [blame] | 189 | enum class LayerNormGate : uint8_t |
| 190 | { |
| 191 | Forget, |
| 192 | Cell, |
| 193 | Input, |
| 194 | Output, |
| 195 | Count |
| 196 | }; |
Sang-Hoon Park | d5c020a | 2020-05-06 21:01:19 +0100 | [diff] [blame] | 197 | static constexpr uint8_t _layer_norm_count = static_cast<uint8_t>(LayerNormGate::Count); |
| 198 | static constexpr uint32_t _out_state_output_size_dimension_idx = 0; |
Sang-Hoon Park | 9230e27 | 2020-04-18 00:46:34 +0100 | [diff] [blame] | 199 | |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 200 | /** Internal method to configure matrix multiplication plus output stage of each gate. |
| 201 | * |
| 202 | * @param[in] mm Matrix multiplication function to use. |
| 203 | * @param[in] outstage Output stage function to use. |
| 204 | * @param[in] gemmlowp_info GEMMLowp metadata to be used by the output stage. |
| 205 | * @param[in] mm_input Input tensor to matrix multiplication function. |
| 206 | * @param[in] mm_weights Weights tensor to matrix multiplication function. |
| 207 | * @param[in] bias Bias tensor to matrix multiplication function. |
| 208 | * @param[in] outstage_res Tensor to be used for storing the result of the output stage. |
| 209 | * @param[in] gemmlowp_scale Real multiplier to be used computing multiplier and shift for requantization. |
| 210 | * @param[in] mm_res_info Tensor info to be used to initialize matrix multiplication result tensor. |
| 211 | * @param[in] mm_res_info Tensor info to be used to initialize output stage result tensor. |
| 212 | * |
| 213 | */ |
| 214 | void configure_mm(NEGEMMLowpMatrixMultiplyCore &mm, NEGEMMLowpOutputStage &outstage, GEMMLowpOutputStageInfo &gemmlowp_info, |
| 215 | const ITensor *mm_input, const ITensor *mm_weights, const ITensor *bias, Tensor *mm_res, |
| 216 | Tensor *outstage_res, float gemmlowp_scale, |
| 217 | const TensorInfo &mm_res_info, const TensorInfo &outstage_tensor_info); |
| 218 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 219 | MemoryGroup _memory_group; |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 220 | |
Sang-Hoon Park | d5c020a | 2020-05-06 21:01:19 +0100 | [diff] [blame] | 221 | /** A small internel kernel do the copy between two tensors */ |
| 222 | class TensorCopyKernel |
| 223 | { |
| 224 | static constexpr uint32_t max_dimension_supported = 2; |
| 225 | |
| 226 | ITensor *_src{ nullptr }; |
| 227 | ITensor *_dst{ nullptr }; |
| 228 | size_t _row_size{}; |
| 229 | Window _window{}; |
| 230 | |
| 231 | public: |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 232 | /** Destructor */ |
| 233 | ~TensorCopyKernel(); |
Sang-Hoon Park | d5c020a | 2020-05-06 21:01:19 +0100 | [diff] [blame] | 234 | /** Static function to check if given info will lead to a valid configuration of @ref NEQLSTMLayer::TensorCopyKernel |
| 235 | * |
| 236 | * @param[in] src Source tensor info. |
| 237 | * @param[in] dst Destination tensor info |
| 238 | * |
| 239 | * @return a status |
| 240 | */ |
| 241 | static Status validate(const ITensorInfo &src, const ITensorInfo &dst); |
| 242 | /** Set the input and output tensors. |
| 243 | * |
| 244 | * @param[in] src Source tensor |
| 245 | * @param[out] dst Destination tensor |
| 246 | */ |
| 247 | void configure(ITensor &src, ITensor &dst); |
| 248 | /** run the kernel */ |
| 249 | void run(); |
| 250 | }; |
| 251 | |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 252 | // Functions used |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 253 | NETranspose _transpose_input_to_forget_weights; |
| 254 | NETranspose _transpose_input_to_cell_weights; |
| 255 | NETranspose _transpose_input_to_output_weights; |
| 256 | NETranspose _transpose_input_to_input_weights; |
| 257 | NETranspose _transpose_recurrent_to_forget_weights; |
| 258 | NETranspose _transpose_recurrent_to_cell_weights; |
| 259 | NETranspose _transpose_recurrent_to_output_weights; |
| 260 | NETranspose _transpose_recurrent_to_input_weights; |
| 261 | NETranspose _transpose_projection_weights; |
| 262 | std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _input_to_input_reduction; |
| 263 | std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _recurrent_to_input_reduction; |
| 264 | std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _input_to_forget_reduction; |
| 265 | std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _recurrent_to_forget_reduction; |
| 266 | std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _input_to_cell_reduction; |
| 267 | std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _recurrent_to_cell_reduction; |
| 268 | std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _input_to_output_reduction; |
| 269 | std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _recurrent_to_output_reduction; |
| 270 | std::unique_ptr<NEGEMMLowpMatrixAReductionKernel> _projection_reduction; |
| 271 | NEArithmeticAddition _projection_bias_add; |
| 272 | NEGEMMLowpMatrixMultiplyCore _mm_input_to_forget; |
| 273 | NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_forget; |
| 274 | NEPixelWiseMultiplication _pixelwise_mul_cell_to_forget; |
| 275 | NEGEMMLowpOutputStage _input_to_forget_outstage; |
| 276 | NEGEMMLowpOutputStage _recurrent_to_forget_outstage; |
| 277 | NEGEMMLowpOutputStage _cell_to_forget_outstage; |
| 278 | NEArithmeticAddition _accumulate_input_recurrent_forget; |
| 279 | NEArithmeticAddition _accumulate_cell_forget; |
| 280 | NEActivationLayer _forget_gate_sigmoid; |
| 281 | NEGEMMLowpMatrixMultiplyCore _mm_input_to_cell; |
| 282 | NEGEMMLowpOutputStage _input_to_cell_outstage; |
| 283 | NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_cell; |
| 284 | NEGEMMLowpOutputStage _recurrent_to_cell_outstage; |
| 285 | NEArithmeticAddition _accumulate_input_recurrent_modulation; |
| 286 | NEActivationLayer _cell_gate_tanh; |
| 287 | NEArithmeticSubtraction _input_gate_sub; |
| 288 | NEGEMMLowpMatrixMultiplyCore _mm_input_to_input; |
| 289 | NEGEMMLowpOutputStage _input_to_input_outstage; |
| 290 | NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_input; |
| 291 | NEGEMMLowpOutputStage _recurrent_to_input_outstage; |
| 292 | NEArithmeticAddition _accumulate_input_recurrent_input; |
| 293 | NEPixelWiseMultiplication _pixelwise_mul_cell_to_input; |
| 294 | NEGEMMLowpOutputStage _cell_to_input_outstage; |
| 295 | NEArithmeticAddition _accumulate_cell_input; |
| 296 | NEActivationLayer _input_gate_sigmoid; |
| 297 | NEPixelWiseMultiplication _pixelwise_mul_forget_cell; |
| 298 | NEPixelWiseMultiplication _pixelwise_mul_input_cell; |
| 299 | NEArithmeticAddition _add_forget_cell; |
| 300 | NEActivationLayer _cell_clip; |
| 301 | NEGEMMLowpMatrixMultiplyCore _mm_input_to_output; |
| 302 | NEGEMMLowpOutputStage _input_to_output_outstage; |
| 303 | NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_output; |
| 304 | NEGEMMLowpOutputStage _recurrent_to_output_outstage; |
| 305 | NEArithmeticAddition _accumulate_input_recurrent_output; |
| 306 | NEPixelWiseMultiplication _pixelwise_mul_cell_to_output; |
| 307 | NEGEMMLowpOutputStage _cell_to_output_outstage; |
| 308 | NEArithmeticAddition _accumulate_cell_to_output; |
| 309 | NEActivationLayer _output_gate_sigmoid; |
| 310 | NEActivationLayer _hidden_tanh; |
| 311 | NEPixelWiseMultiplication _pixelwise_mul_hidden; |
| 312 | NEGEMMLowpOutputStage _hidden_outstage; |
| 313 | NEGEMMLowpMatrixMultiplyCore _mm_projection; |
| 314 | NEGEMMLowpOutputStage _projection_outstage; |
| 315 | NEArithmeticAddition _accumulate_projection; |
| 316 | NEActivationLayer _projection_clip; |
Sang-Hoon Park | d5c020a | 2020-05-06 21:01:19 +0100 | [diff] [blame] | 317 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 318 | TensorCopyKernel _projection_bias_copy; |
| 319 | TensorCopyKernel _projection_output_to_accumulate_copy; |
| 320 | TensorCopyKernel _projection_accumulate_to_output_copy; |
| 321 | TensorCopyKernel _hidden_to_output_copy; |
Sang-Hoon Park | d5c020a | 2020-05-06 21:01:19 +0100 | [diff] [blame] | 322 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 323 | std::array<std::unique_ptr<NEQLSTMLayerNormalizationKernel>, _layer_norm_count> _layer_norms; |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 324 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 325 | NECopy _copy_output; |
Michele Di Giorgio | beb2d45 | 2020-05-11 16:17:51 +0100 | [diff] [blame] | 326 | |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 327 | // Tensor pointers |
Michalis Spyrou | 173ba9b | 2020-06-23 17:25:43 +0100 | [diff] [blame] | 328 | const ITensor *_input_to_input_weights |
| 329 | { |
| 330 | nullptr |
| 331 | }; |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 332 | const ITensor *_recurrent_to_input_weights{ nullptr }; |
| 333 | const ITensor *_projection_bias{ nullptr }; |
| 334 | const ITensor *_input_to_forget_weights{ nullptr }; |
| 335 | const ITensor *_input_to_cell_weights{ nullptr }; |
| 336 | const ITensor *_input_to_output_weights{ nullptr }; |
| 337 | const ITensor *_recurrent_to_forget_weights{ nullptr }; |
| 338 | const ITensor *_recurrent_to_cell_weights{ nullptr }; |
| 339 | const ITensor *_recurrent_to_output_weights{ nullptr }; |
| 340 | const ITensor *_projection_weights{ nullptr }; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 341 | std::array<const ITensor *, _layer_norm_count> _layer_norm_weights{}; |
| 342 | std::array<const ITensor *, _layer_norm_count> _layer_norm_bias{}; |
Sang-Hoon Park | 9230e27 | 2020-04-18 00:46:34 +0100 | [diff] [blame] | 343 | |
| 344 | using LayerNormIndexType = typename std::underlying_type<LayerNormGate>::type; |
| 345 | inline LayerNormIndexType getGateIndex(LayerNormGate g) |
| 346 | { |
| 347 | return static_cast<LayerNormIndexType>(g); |
| 348 | } |
| 349 | |
| 350 | inline void set_layer_norm_weight(const ITensor *t, LayerNormGate g) |
| 351 | { |
| 352 | _layer_norm_weights[getGateIndex(g)] = t; |
| 353 | } |
| 354 | |
| 355 | inline void set_layer_norm_bias(const ITensor *t, LayerNormGate g) |
| 356 | { |
| 357 | _layer_norm_bias[getGateIndex(g)] = t; |
| 358 | } |
| 359 | |
| 360 | inline const ITensor *get_layer_norm_weight(LayerNormGate g) |
| 361 | { |
| 362 | return _layer_norm_weights[getGateIndex(g)]; |
| 363 | } |
| 364 | |
| 365 | inline const ITensor *get_layer_norm_bias(LayerNormGate g) |
| 366 | { |
| 367 | return _layer_norm_bias[getGateIndex(g)]; |
| 368 | } |
| 369 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 370 | inline std::unique_ptr<NEQLSTMLayerNormalizationKernel> &get_layer_norm(LayerNormGate g) |
Sang-Hoon Park | 9230e27 | 2020-04-18 00:46:34 +0100 | [diff] [blame] | 371 | { |
| 372 | return _layer_norms[getGateIndex(g)]; |
| 373 | } |
| 374 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 375 | void configure_layer_norm(LayerNormGate g, const ITensor *in); |
| 376 | static Status validate_layer_norm(const ITensorInfo &in, const ITensorInfo &weight, const ITensorInfo &bias); |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 377 | |
| 378 | // Temporary tensors |
| 379 | Tensor _input_to_forget_weights_transposed{ nullptr }; |
| 380 | Tensor _input_to_cell_weights_transposed{ nullptr }; |
| 381 | Tensor _input_to_output_weights_transposed{ nullptr }; |
| 382 | Tensor _input_to_input_weights_transposed{ nullptr }; |
| 383 | Tensor _recurrent_to_forget_weights_transposed{ nullptr }; |
| 384 | Tensor _recurrent_to_cell_weights_transposed{ nullptr }; |
| 385 | Tensor _recurrent_to_output_weights_transposed{ nullptr }; |
| 386 | Tensor _recurrent_to_input_weights_transposed{ nullptr }; |
| 387 | Tensor _projection_weights_transposed{ nullptr }; |
| 388 | Tensor _input_to_input_eff_bias{ nullptr }; |
| 389 | Tensor _recurrent_to_input_eff_bias{ nullptr }; |
| 390 | Tensor _input_to_forget_eff_bias{ nullptr }; |
| 391 | Tensor _recurrent_to_forget_eff_bias{ nullptr }; |
| 392 | Tensor _input_to_cell_eff_bias{ nullptr }; |
| 393 | Tensor _recurrent_to_cell_eff_bias{ nullptr }; |
| 394 | Tensor _input_to_output_eff_bias{ nullptr }; |
| 395 | Tensor _recurrent_to_output_eff_bias{ nullptr }; |
| 396 | Tensor _projection_reduction_res{ nullptr }; |
| 397 | Tensor _projection_eff_bias{ nullptr }; |
| 398 | Tensor _mm_input_to_forget_res{ nullptr }; |
| 399 | Tensor _mm_recurrent_to_forget_res{ nullptr }; |
| 400 | Tensor _mul_cell_to_forget_res{ nullptr }; |
| 401 | Tensor _input_to_forget_outstage_res{ nullptr }; |
| 402 | Tensor _cell_to_forget_outstage_res{ nullptr }; |
| 403 | Tensor _recurrent_to_forget_outstage_res{ nullptr }; |
| 404 | Tensor _forget_gate{ nullptr }; |
| 405 | Tensor _mm_input_to_cell_res{ nullptr }; |
| 406 | Tensor _input_to_cell_outstage_res{ nullptr }; |
| 407 | Tensor _mm_recurrent_to_cell_res{ nullptr }; |
| 408 | Tensor _recurrent_to_cell_outstage_res{ nullptr }; |
| 409 | Tensor _cell_gate{ nullptr }; |
| 410 | Tensor _mul_input_cell_res{ nullptr }; |
| 411 | Tensor _mm_input_to_input_res{ nullptr }; |
| 412 | Tensor _input_to_input_outstage_res{ nullptr }; |
| 413 | Tensor _mm_recurrent_to_input_res{ nullptr }; |
| 414 | Tensor _mul_cell_to_input_res{ nullptr }; |
| 415 | Tensor _cell_to_input_outstage_res{ nullptr }; |
| 416 | Tensor _recurrent_to_input_outstage_res{ nullptr }; |
| 417 | Tensor _input_gate{ nullptr }; |
| 418 | Tensor _mm_input_to_output_res{ nullptr }; |
| 419 | Tensor _input_to_output_outstage_res{ nullptr }; |
| 420 | Tensor _mm_recurrent_to_output_res{ nullptr }; |
| 421 | Tensor _mul_cell_to_output_res{ nullptr }; |
Sang-Hoon Park | d5c020a | 2020-05-06 21:01:19 +0100 | [diff] [blame] | 422 | Tensor _cell_to_output_outstage_res{ nullptr }; |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 423 | Tensor _recurrent_to_output_outstage_res{ nullptr }; |
| 424 | Tensor _output_gate{ nullptr }; |
| 425 | Tensor _hidden_mul_res{ nullptr }; |
Sang-Hoon Park | d5c020a | 2020-05-06 21:01:19 +0100 | [diff] [blame] | 426 | Tensor _hidden_gate{ nullptr }; |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 427 | Tensor _mm_projection_res{ nullptr }; |
| 428 | Tensor _projection_outstage_res{ nullptr }; |
Sang-Hoon Park | d5c020a | 2020-05-06 21:01:19 +0100 | [diff] [blame] | 429 | Tensor _projection_out_res{ nullptr }; |
Sang-Hoon Park | d5c020a | 2020-05-06 21:01:19 +0100 | [diff] [blame] | 430 | Tensor _projection_accumulate_res{ nullptr }; |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 431 | Tensor _ones{ nullptr }; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 432 | std::array<Tensor, _layer_norm_count> _layer_norm_output{}; |
Sang-Hoon Park | 9230e27 | 2020-04-18 00:46:34 +0100 | [diff] [blame] | 433 | |
| 434 | inline Tensor &get_layer_norm_output(LayerNormGate g) |
| 435 | { |
| 436 | return _layer_norm_output[getGateIndex(g)]; |
| 437 | } |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 438 | |
| 439 | bool _is_prepared{ false }; |
| 440 | bool _has_cifg{ false }; |
| 441 | bool _has_cell_clipping{ false }; |
| 442 | bool _has_projection{ false }; |
| 443 | bool _has_projection_clipping{ false }; |
| 444 | bool _has_peephole{ false }; |
Sang-Hoon Park | 9230e27 | 2020-04-18 00:46:34 +0100 | [diff] [blame] | 445 | bool _has_layer_norm{ false }; |
Sang-Hoon Park | d5c020a | 2020-05-06 21:01:19 +0100 | [diff] [blame] | 446 | bool _projection_tensor_copy_required{ false }; |
Michele Di Giorgio | 47a8990 | 2020-03-09 19:32:33 +0000 | [diff] [blame] | 447 | }; |
| 448 | } // namespace arm_compute |
| 449 | #endif /* ARM_COMPUTE_NEQLSTMLAYER_H */ |