From 47a899017e67556ffffef78571c9be61dd7bc3f0 Mon Sep 17 00:00:00 2001 From: Michele Di Giorgio Date: Mon, 9 Mar 2020 19:32:33 +0000 Subject: COMPMID-3237: Implement NEQLSTMLayer COMPMID-3082: Extend NEQLSTMLayer with enhancements Change-Id: I88175b7bf69494a4eae510b74176fe8a0d6cd770 Signed-off-by: Michele Di Giorgio Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/2969 Tested-by: Arm Jenkins Reviewed-by: Sang-Hoon Park Reviewed-by: Sheri Zhang Comments-Addressed: Arm Jenkins --- Android.bp | 1 + .../NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h | 6 +- arm_compute/core/utils/misc/InfoHelpers.h | 35 +- arm_compute/runtime/NEON/NEFunctions.h | 3 +- .../NEON/functions/NEGEMMLowpMatrixMultiplyCore.h | 4 +- arm_compute/runtime/NEON/functions/NELSTMLayer.h | 60 +- arm_compute/runtime/NEON/functions/NEQLSTMLayer.h | 332 ++++++++ arm_compute/runtime/common/LSTMParams.h | 66 +- .../kernels/NEGEMMLowpMatrixMultiplyKernel.cpp | 4 +- .../NEON/kernels/NEGEMMLowpReductionKernel.cpp | 6 +- src/runtime/CL/functions/CLLSTMLayer.cpp | 31 +- .../functions/NEGEMMLowpMatrixMultiplyCore.cpp | 2 +- src/runtime/NEON/functions/NELSTMLayer.cpp | 31 +- src/runtime/NEON/functions/NEQLSTMLayer.cpp | 850 +++++++++++++++++++++ 14 files changed, 1311 insertions(+), 120 deletions(-) create mode 100644 arm_compute/runtime/NEON/functions/NEQLSTMLayer.h create mode 100644 src/runtime/NEON/functions/NEQLSTMLayer.cpp diff --git a/Android.bp b/Android.bp index 80ea3e36b8..7a04eec29f 100644 --- a/Android.bp +++ b/Android.bp @@ -694,6 +694,7 @@ cc_library_static { "src/runtime/NEON/functions/NEPixelWiseMultiplication.cpp", "src/runtime/NEON/functions/NEPoolingLayer.cpp", "src/runtime/NEON/functions/NEPriorBoxLayer.cpp", + "src/runtime/NEON/functions/NEQLSTMLayer.cpp", "src/runtime/NEON/functions/NEQuantizationLayer.cpp", "src/runtime/NEON/functions/NERNNLayer.cpp", "src/runtime/NEON/functions/NEROIAlignLayer.cpp", diff --git a/arm_compute/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h b/arm_compute/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h index c6b40eb30e..8f47c5089d 100644 --- a/arm_compute/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h +++ b/arm_compute/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 ARM Limited. + * Copyright (c) 2017-2020 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -63,14 +63,14 @@ public: * kernels change the layout of the original matrices to be more cache-friendly. * * @param[in] input0 Input tensor containing the interleaved Matrix A. Data type supported: U8/QASYMM8/S8/QASYMM8_SIGNED - * @param[in] input1 Input tensor containing the transposed1xW Matrix B. Data type supported: U8/QASYMM8/S8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL + * @param[in] input1 Input tensor containing the transposed1xW Matrix B. Data type supported: U8/QASYMM8/S8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL * @param[out] output Output tensor to store the result of matrix multiplication. Data type supported: S32 */ void configure(const ITensor *input0, const ITensor *input1, ITensor *output); /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMLowpMatrixMultiplyKernel * * @param[in] input0 Input tensor info containing the interleaved Matrix A. Data type supported: U8/QASYMM8/S8/QASYMM8_SIGNED - * @param[in] input1 Input tensor info containing the transposed Matrix B. Data type supported: U8/QASYMM8/S8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL + * @param[in] input1 Input tensor info containing the transposed Matrix B. Data type supported: U8/QASYMM8/S8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL * @param[in] output Output tensor info to store the result of matrix multiplication. Data type supported: S32 * * @return a status diff --git a/arm_compute/core/utils/misc/InfoHelpers.h b/arm_compute/core/utils/misc/InfoHelpers.h index b572de2433..8cf701c124 100644 --- a/arm_compute/core/utils/misc/InfoHelpers.h +++ b/arm_compute/core/utils/misc/InfoHelpers.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 ARM Limited. + * Copyright (c) 2019-2020 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -26,6 +26,7 @@ #include "arm_compute/core/Error.h" #include "arm_compute/core/Types.h" +#include "arm_compute/runtime/common/LSTMParams.h" namespace arm_compute { @@ -58,6 +59,38 @@ inline bool is_relu6(ActivationLayerInfo activation_info) && activation_info.a() == 6.f; return activation_info.enabled() && (is_lu_bounded_relu || is_bounded_relu); } + +/** Build LSTMParams object by extracting the metadata from each + * tensor. + * + * @param[in] lstm_params The LSTMParams object containing the tensors. + * @param[out] lstm_params_info The LSTMParams to be constructed. + * + */ +template +inline void build_lstm_params_tensor_info(const LSTMParams &lstm_params, + LSTMParams *lstm_params_info) +{ + if(lstm_params.has_peephole_opt()) + { + ARM_COMPUTE_ERROR_ON_NULLPTR(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights()); + lstm_params_info->set_peephole_params(lstm_params.cell_to_forget_weights()->info(), lstm_params.cell_to_output_weights()->info()); + } + if(lstm_params.has_projection()) + { + ARM_COMPUTE_ERROR_ON_NULLPTR(lstm_params.projection_weights()); + lstm_params_info->set_projection_params(lstm_params.projection_weights()->info(), + lstm_params.projection_bias() != nullptr ? lstm_params.projection_bias()->info() : nullptr); + } + if(!lstm_params.has_cifg_opt()) + { + ARM_COMPUTE_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights(), lstm_params.input_gate_bias()); + + const ITensorInfo *cell_to_input_weights_info = (lstm_params.has_peephole_opt()) ? lstm_params.cell_to_input_weights()->info() : nullptr; + lstm_params_info->set_cifg_params(lstm_params.input_to_input_weights()->info(), lstm_params.recurrent_to_input_weights()->info(), + cell_to_input_weights_info, lstm_params.input_gate_bias()->info()); + } +} } // namespace info_helpers } // namespace utils } // namespace arm_compute diff --git a/arm_compute/runtime/NEON/NEFunctions.h b/arm_compute/runtime/NEON/NEFunctions.h index abad8d482e..de364fa9af 100644 --- a/arm_compute/runtime/NEON/NEFunctions.h +++ b/arm_compute/runtime/NEON/NEFunctions.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2019 ARM Limited. + * Copyright (c) 2016-2020 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -120,6 +120,7 @@ #include "arm_compute/runtime/NEON/functions/NEPixelWiseMultiplication.h" #include "arm_compute/runtime/NEON/functions/NEPoolingLayer.h" #include "arm_compute/runtime/NEON/functions/NEPriorBoxLayer.h" +#include "arm_compute/runtime/NEON/functions/NEQLSTMLayer.h" #include "arm_compute/runtime/NEON/functions/NEQuantizationLayer.h" #include "arm_compute/runtime/NEON/functions/NERNNLayer.h" #include "arm_compute/runtime/NEON/functions/NEROIAlignLayer.h" diff --git a/arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h b/arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h index 74dedcf4c5..11683c5b95 100644 --- a/arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h +++ b/arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h @@ -84,7 +84,7 @@ public: * @note The @p output type is S32 if @p gemm_info.type == GEMMLowpOutputStageType::NONE. It is QASYMM8/QASYMM8_SIGNED otherwise * * @param[in] a First input tensor (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED. - * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a + * @param[in] b Second input tensor (Matrix B). Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL. * @param[in] c Third input tensor (Matrix C). It can be a nullptr. Data type supported: S32 * @param[out] output Output tensor. Data type supported: Data type supported: S32/QASYMM8/QASYMM8_SIGNED * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and @@ -96,7 +96,7 @@ public: * @note The @p output type is S32 if @p gemm_info.type == GEMMLowpOutputStageType::NONE. It is QASYMM8/QASYMM8_SIGNED otherwise * * @param[in] a First input tensor info (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED. - * @param[in] b Second input tensor info (Matrix B). Data type supported: same as @p a + * @param[in] b Second input tensor info (Matrix B). Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL. * @param[in] c Third input tensor info (Matrix C). It can be a nullptr. Data type supported: S32 * @param[in] output Output tensor info. Data type supported: Data type supported: S32/QASYMM8/QASYMM8_SIGNED * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and diff --git a/arm_compute/runtime/NEON/functions/NELSTMLayer.h b/arm_compute/runtime/NEON/functions/NELSTMLayer.h index ae13d0c36f..e85e87b88e 100644 --- a/arm_compute/runtime/NEON/functions/NELSTMLayer.h +++ b/arm_compute/runtime/NEON/functions/NELSTMLayer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2019 ARM Limited. + * Copyright (c) 2018-2020 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -68,22 +68,23 @@ public: * @param[out] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input. * @param[out] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size]. * Data types supported: Same as @p input. - * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization: - * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. - * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. - * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input. - * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. - * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. - * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input - * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. - * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input. - * input_layer_norm_coefficients 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. - * forget_layer_norm_coefficients 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. - * cell_layer_norm_coefficients 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. - * output_layer_norm_coefficients 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * @param[in] lstm_params Weights tensors used in peephole optimization: + * input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. + * recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input. + * cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input + * projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * projection_bias (Optional) 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input. + * input_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * forget_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * cell_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * output_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo. * @param[in] cell_threshold The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip]. If set to 0.0 then clipping is disabled. - * @param[in] projection_threshold The clipping threshold for the output from the projection layer, such that values are bound within [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled. + * @param[in] projection_threshold The clipping threshold for the output from the projection layer, such that values are bound within [-proj_clip, proj_clip]. + * If set to 0.0 then clipping is disabled. */ void configure(const ITensor *input, const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights, @@ -112,22 +113,23 @@ public: * @param[in] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input. * @param[in] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size]. * Data types supported: Same as @p input. - * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization: - * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. - * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. - * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input. - * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. - * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. - * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input - * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. - * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input. - * input_layer_norm_coefficients 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. - * forget_layer_norm_coefficients 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. - * cell_layer_norm_coefficients 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. - * output_layer_norm_coefficients 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. + * @param[in] lstm_params Weights tensors used in peephole optimization: + * input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. + * recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input. + * cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input + * projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * projection_bias (Optional) 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input. + * input_layer_norm_weights (Optional) 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. + * forget_layer_norm_weights (Optional) 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. + * cell_layer_norm_weights (Optional) 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. + * output_layer_norm_weights (Optional) 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo. * @param[in] cell_threshold The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip]. If set to 0.0 then clipping is disabled. - * @param[in] projection_threshold The clipping threshold for the output from the projection layer, such that values are bound within [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled. + * @param[in] projection_threshold The clipping threshold for the output from the projection layer, such that values are bound within [-proj_clip, proj_clip]. + * If set to 0.0 then clipping is disabled. * * @return a status */ diff --git a/arm_compute/runtime/NEON/functions/NEQLSTMLayer.h b/arm_compute/runtime/NEON/functions/NEQLSTMLayer.h new file mode 100644 index 0000000000..a37909b775 --- /dev/null +++ b/arm_compute/runtime/NEON/functions/NEQLSTMLayer.h @@ -0,0 +1,332 @@ +/* + * Copyright (c) 2020 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_NEQLSTMLAYER_H +#define ARM_COMPUTE_NEQLSTMLAYER_H + +#include "arm_compute/core/NEON/kernels/NEArithmeticAdditionKernel.h" +#include "arm_compute/core/NEON/kernels/NEArithmeticSubtractionKernel.h" +#include "arm_compute/core/NEON/kernels/NEGEMMLowpReductionKernel.h" +#include "arm_compute/core/NEON/kernels/NEPixelWiseMultiplicationKernel.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h" +#include "arm_compute/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.h" +#include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h" +#include "arm_compute/runtime/NEON/functions/NETranspose.h" + +#include "arm_compute/runtime/common/LSTMParams.h" + +namespace arm_compute +{ +// Forward declarations +class ITensor; + +/** Basic function to run @ref NEQLSTMLayer + * + * This function calls the following NEON functions/kernels: + * + * -# @ref NEActivationLayer Activation functions (tanh and logistic) + * -# @ref NEArithmeticAdditionKernel Elementwise addition + * -# @ref NEArithmeticSubtractionKernel Elementwise subtraction + * -# @ref NEGEMMLowpMatrixMultiplyCore Quantized matrix multiplication core. Accumulators are 32-bit integers + * -# @ref NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint Convert 32-bit integers into QSYMM16 + * -# @ref NEGEMMLowpMatrixAReductionKernel For precomputing effective biases to use + * -# @ref NEPixelWiseMultiplicationKernel Elementwise multiplication + * -# @ref NETranspose Transpose function for reshaping the weights + * */ +class NEQLSTMLayer : public IFunction +{ +public: + /** Default constructor */ + NEQLSTMLayer(std::shared_ptr memory_manager = nullptr); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + NEQLSTMLayer(const NEQLSTMLayer &) = delete; + /** Default move constructor */ + NEQLSTMLayer(NEQLSTMLayer &&) = default; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + NEQLSTMLayer &operator=(const NEQLSTMLayer &) = delete; + /** Default move assignment operator */ + NEQLSTMLayer &operator=(NEQLSTMLayer &&) = default; + /** Initialize function's tensors. + * + * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: QASYMM8_SIGNED. + * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8. + * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8. + * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8. + * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8. + * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8. + * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8. + * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32. + * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32. + * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: S32. + * @param[in] cell_state_in 2D tensor with dimensions [output_size, batch_size]. Data type supported: QSYMM16. + * @param[in] output_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input. + * @param[out] cell_state_out Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size]. Data type supported: QSYMM16. + * @param[out] output_state_out Destination tensor. Output is a 2D tensor with dimensions [num_units, batch_size].Data types supported: Same as @p input. + * @param[in] lstm_params Weights tensors used in peephole, CIFG and layer normalization optimizations: + * input_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at input gate. + * forget_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at forget gate. + * cell_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at cell gate. + * output_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at output gate. + * hidden_state_zero The zero point of the hidden state. + * hidden_state_scale The scale of the hidden state. + * input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8. + * recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8. + * cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: QSYMM16. + * cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. + * cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. + * input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: S32. + * projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8. + * projection_bias (Optional) 1D weights tensor with dimensions [output_size]. S32. + * input_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. + * forget_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. + * cell_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. + * output_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. + * cell_threshold (Optional) The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip]. + * If set to 0.0 then clipping is disabled. + * projection_threshold (Optional) The clipping threshold for the output from the projection layer, such that values are bound within + * [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled. + */ + void configure(const ITensor *input, + const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights, + const ITensor *recurrent_to_forget_weights, const ITensor *recurrent_to_cell_weights, const ITensor *recurrent_to_output_weights, + const ITensor *forget_gate_bias, const ITensor *cell_bias, const ITensor *output_gate_bias, + const ITensor *cell_state_in, const ITensor *output_state_in, + ITensor *cell_state_out, ITensor *output_state_out, + const LSTMParams &lstm_params); + + /** Static function to check if given info will lead to a valid configuration of @ref NEQLSTMLayer + * + * @param[in] input Source tensor info. Input is a 2D tensor info with dimensions [input_size, batch_size]. Data types supported: QASYMM8_SIGNED. + * @param[in] input_to_forget_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8. + * @param[in] input_to_cell_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8. + * @param[in] input_to_output_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: QSYMM8. + * @param[in] recurrent_to_forget_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8. + * @param[in] recurrent_to_cell_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8. + * @param[in] recurrent_to_output_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: QSYMM8. + * @param[in] forget_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32. + * @param[in] cell_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32. + * @param[in] output_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: S32. + * @param[in] cell_state_in 2D tensor info with dimensions [num_units, batch_size]. Data type supported: QSYMM16. + * @param[in] output_state_in 2D tensor info with dimensions [output_size, batch_size]. Data type supported: Same as @p input. + * @param[out] cell_state_out Destination tensor info. Output is a 2D tensor info with dimensions [num_units, batch_size]. Data type supported: QSYMM16. + * @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. + * @param[in] lstm_params Weights tensors info used in peephole, CIFG and layer normalization optimizations: + * input_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at input gate. + * forget_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at forget gate. + * cell_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at cell gate. + * output_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at output gate. + * hidden_state_zero The zero point of the hidden state. + * hidden_state_scale The scale of the hidden state. + * input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: QSYMM8. + * recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8. + * cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: QSYMM16. + * cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. + * cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. + * input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: S32. + * projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: QSYMM8. + * projection_bias (Optional) 1D weights tensor with dimensions [output_size]. S32. + * input_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. + * forget_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. + * cell_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. + * output_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: QSYMM16. + * cell_threshold (Optional) The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip]. + * If set to 0.0 then clipping is disabled. + * projection_threshold (Optional) The clipping threshold for the output from the projection layer, such that values are bound within + * [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled. + * @return a status + */ + static Status validate(const ITensorInfo *input, + const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights, + const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights, + const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias, + const ITensorInfo *cell_state_in, const ITensorInfo *output_state_in, + const ITensorInfo *cell_state_out, const ITensorInfo *output_state_out, + const LSTMParams &lstm_params); + + // Inherited methods overridden: + void run() override; + void prepare() override; + +private: + /** Internal method to configure matrix multiplication plus output stage of each gate. + * + * @param[in] mm Matrix multiplication function to use. + * @param[in] outstage Output stage function to use. + * @param[in] gemmlowp_info GEMMLowp metadata to be used by the output stage. + * @param[in] mm_input Input tensor to matrix multiplication function. + * @param[in] mm_weights Weights tensor to matrix multiplication function. + * @param[in] bias Bias tensor to matrix multiplication function. + * @param[in] outstage_res Tensor to be used for storing the result of the output stage. + * @param[in] gemmlowp_scale Real multiplier to be used computing multiplier and shift for requantization. + * @param[in] mm_res_info Tensor info to be used to initialize matrix multiplication result tensor. + * @param[in] mm_res_info Tensor info to be used to initialize output stage result tensor. + * + */ + void configure_mm(NEGEMMLowpMatrixMultiplyCore &mm, NEGEMMLowpOutputStage &outstage, GEMMLowpOutputStageInfo &gemmlowp_info, + const ITensor *mm_input, const ITensor *mm_weights, const ITensor *bias, Tensor *mm_res, + Tensor *outstage_res, float gemmlowp_scale, + const TensorInfo &mm_res_info, const TensorInfo &outstage_tensor_info); + + MemoryGroup _memory_group{}; + + // Functions used + NETranspose _transpose_input_to_forget_weights{}; + NETranspose _transpose_input_to_cell_weights{}; + NETranspose _transpose_input_to_output_weights{}; + NETranspose _transpose_input_to_input_weights{}; + NETranspose _transpose_recurrent_to_forget_weights{}; + NETranspose _transpose_recurrent_to_cell_weights{}; + NETranspose _transpose_recurrent_to_output_weights{}; + NETranspose _transpose_recurrent_to_input_weights{}; + NETranspose _transpose_projection_weights{}; + NEGEMMLowpMatrixAReductionKernel _input_to_input_reduction{}; + NEGEMMLowpMatrixAReductionKernel _recurrent_to_input_reduction{}; + NEGEMMLowpMatrixAReductionKernel _input_to_forget_reduction{}; + NEGEMMLowpMatrixAReductionKernel _recurrent_to_forget_reduction{}; + NEGEMMLowpMatrixAReductionKernel _input_to_cell_reduction{}; + NEGEMMLowpMatrixAReductionKernel _recurrent_to_cell_reduction{}; + NEGEMMLowpMatrixAReductionKernel _input_to_output_reduction{}; + NEGEMMLowpMatrixAReductionKernel _recurrent_to_output_reduction{}; + NEGEMMLowpMatrixAReductionKernel _projection_reduction{}; + NEArithmeticAdditionKernel _projection_bias_add{}; + NEGEMMLowpMatrixMultiplyCore _mm_input_to_forget{}; + NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_forget{}; + NEPixelWiseMultiplicationKernel _pixelwise_mul_cell_to_forget{}; + NEGEMMLowpOutputStage _input_to_forget_outstage{}; + NEGEMMLowpOutputStage _recurrent_to_forget_outstage{}; + NEGEMMLowpOutputStage _cell_to_forget_outstage{}; + NEArithmeticAdditionKernel _accumulate_input_recurrent_forget{}; + NEArithmeticAdditionKernel _accumulate_cell_forget{}; + NEActivationLayer _forget_gate_sigmoid{}; + NEGEMMLowpMatrixMultiplyCore _mm_input_to_cell{}; + NEGEMMLowpOutputStage _input_to_cell_outstage{}; + NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_cell{}; + NEGEMMLowpOutputStage _recurrent_to_cell_outstage{}; + NEArithmeticAdditionKernel _accumulate_input_recurrent_modulation{}; + NEActivationLayer _cell_gate_tanh{}; + NEArithmeticSubtractionKernel _input_gate_sub{}; + NEGEMMLowpMatrixMultiplyCore _mm_input_to_input{}; + NEGEMMLowpOutputStage _input_to_input_outstage{}; + NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_input{}; + NEGEMMLowpOutputStage _recurrent_to_input_outstage{}; + NEArithmeticAdditionKernel _accumulate_input_recurrent_input{}; + NEPixelWiseMultiplicationKernel _pixelwise_mul_cell_to_input{}; + NEGEMMLowpOutputStage _cell_to_input_outstage{}; + NEArithmeticAdditionKernel _accumulate_cell_input{}; + NEActivationLayer _input_gate_tanh{}; + NEPixelWiseMultiplicationKernel _pixelwise_mul_forget_cell{}; + NEPixelWiseMultiplicationKernel _pixelwise_mul_input_cell{}; + NEArithmeticAdditionKernel _add_forget_cell{}; + NEActivationLayer _cell_clip{}; + NEGEMMLowpMatrixMultiplyCore _mm_input_to_output{}; + NEGEMMLowpOutputStage _input_to_output_outstage{}; + NEGEMMLowpMatrixMultiplyCore _mm_recurrent_to_output{}; + NEGEMMLowpOutputStage _recurrent_to_output_outstage{}; + NEArithmeticAdditionKernel _accumulate_input_recurrent_output{}; + NEPixelWiseMultiplicationKernel _pixelwise_mul_cell_to_output{}; + NEArithmeticAdditionKernel _accumulate_cell_to_output{}; + NEActivationLayer _output_gate_sigmoid{}; + NEActivationLayer _hidden_tanh{}; + NEPixelWiseMultiplicationKernel _pixelwise_mul_hidden{}; + NEGEMMLowpOutputStage _hidden_outstage{}; + NEGEMMLowpMatrixMultiplyCore _mm_projection{}; + NEGEMMLowpOutputStage _projection_outstage{}; + NEArithmeticAdditionKernel _accumulate_projection{}; + NEActivationLayer _projection_clip{}; + + // Tensor pointers + const ITensor *_input_to_input_weights + { + nullptr + }; + const ITensor *_recurrent_to_input_weights{ nullptr }; + const ITensor *_projection_bias{ nullptr }; + const ITensor *_input_to_forget_weights{ nullptr }; + const ITensor *_input_to_cell_weights{ nullptr }; + const ITensor *_input_to_output_weights{ nullptr }; + const ITensor *_recurrent_to_forget_weights{ nullptr }; + const ITensor *_recurrent_to_cell_weights{ nullptr }; + const ITensor *_recurrent_to_output_weights{ nullptr }; + const ITensor *_projection_weights{ nullptr }; + + // Temporary tensors + Tensor _input_to_forget_weights_transposed{ nullptr }; + Tensor _input_to_cell_weights_transposed{ nullptr }; + Tensor _input_to_output_weights_transposed{ nullptr }; + Tensor _input_to_input_weights_transposed{ nullptr }; + Tensor _recurrent_to_forget_weights_transposed{ nullptr }; + Tensor _recurrent_to_cell_weights_transposed{ nullptr }; + Tensor _recurrent_to_output_weights_transposed{ nullptr }; + Tensor _recurrent_to_input_weights_transposed{ nullptr }; + Tensor _projection_weights_transposed{ nullptr }; + Tensor _input_to_input_eff_bias{ nullptr }; + Tensor _recurrent_to_input_eff_bias{ nullptr }; + Tensor _input_to_forget_eff_bias{ nullptr }; + Tensor _recurrent_to_forget_eff_bias{ nullptr }; + Tensor _input_to_cell_eff_bias{ nullptr }; + Tensor _recurrent_to_cell_eff_bias{ nullptr }; + Tensor _input_to_output_eff_bias{ nullptr }; + Tensor _recurrent_to_output_eff_bias{ nullptr }; + Tensor _projection_reduction_res{ nullptr }; + Tensor _projection_eff_bias{ nullptr }; + Tensor _mm_input_to_forget_res{ nullptr }; + Tensor _mm_recurrent_to_forget_res{ nullptr }; + Tensor _mul_cell_to_forget_res{ nullptr }; + Tensor _input_to_forget_outstage_res{ nullptr }; + Tensor _cell_to_forget_outstage_res{ nullptr }; + Tensor _recurrent_to_forget_outstage_res{ nullptr }; + Tensor _forget_gate{ nullptr }; + Tensor _mm_input_to_cell_res{ nullptr }; + Tensor _input_to_cell_outstage_res{ nullptr }; + Tensor _mm_recurrent_to_cell_res{ nullptr }; + Tensor _recurrent_to_cell_outstage_res{ nullptr }; + Tensor _cell_gate{ nullptr }; + Tensor _mul_input_cell_res{ nullptr }; + Tensor _mm_input_to_input_res{ nullptr }; + Tensor _input_to_input_outstage_res{ nullptr }; + Tensor _mm_recurrent_to_input_res{ nullptr }; + Tensor _mul_cell_to_input_res{ nullptr }; + Tensor _cell_to_input_outstage_res{ nullptr }; + Tensor _recurrent_to_input_outstage_res{ nullptr }; + Tensor _input_gate{ nullptr }; + Tensor _mm_input_to_output_res{ nullptr }; + Tensor _input_to_output_outstage_res{ nullptr }; + Tensor _mm_recurrent_to_output_res{ nullptr }; + Tensor _mul_cell_to_output_res{ nullptr }; + Tensor _recurrent_to_output_outstage_res{ nullptr }; + Tensor _output_gate{ nullptr }; + Tensor _hidden_mul_res{ nullptr }; + Tensor _mm_projection_res{ nullptr }; + Tensor _projection_outstage_res{ nullptr }; + Tensor _ones{ nullptr }; + + bool _is_prepared{ false }; + bool _has_cifg{ false }; + bool _has_cell_clipping{ false }; + bool _has_projection{ false }; + bool _has_projection_clipping{ false }; + bool _has_peephole{ false }; +}; +} // namespace arm_compute +#endif /* ARM_COMPUTE_NEQLSTMLAYER_H */ diff --git a/arm_compute/runtime/common/LSTMParams.h b/arm_compute/runtime/common/LSTMParams.h index f16945730e..e21ddd7af1 100644 --- a/arm_compute/runtime/common/LSTMParams.h +++ b/arm_compute/runtime/common/LSTMParams.h @@ -54,10 +54,10 @@ public: _output_layer_norm_weights(nullptr), _cell_clip(0.f), _projection_clip(0.0f), - _input_gate_matmul_scale(0.0f), - _forget_gate_matmul_scale(0.0f), - _cell_gate_matmul_scale(0.0f), - _output_gate_matmul_scale(0.0f), + _input_intermediate_scale(0.0f), + _forget_intermediate_scale(0.0f), + _cell_intermediate_scale(0.0f), + _output_intermediate_scale(0.0f), _hidden_state_zero(0.0f), _hidden_state_scale(0), _has_peephole_opt(false), @@ -74,10 +74,10 @@ public: ~LSTMParams() = default; /** Set CIFG tensor parameters. * - * @param[in] input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data types supported: F16/F32. + * @param[in] input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data types supported: QSYMM8/F16/F32. * @param[in] recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input_to_input_weights. * @param[in] cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input_to_input_weights. - * @param[in] input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input_to_input_weights + * @param[in] input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input_to_input_weights, S32 when @p input_to_input_weights is QSYMM8 * * @return Reference to this LSTMParams object */ @@ -92,8 +92,8 @@ public: } /** Set projection tensor parameters. * - * @param[in] projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Data types supported: F16/F32. - * @param[in] projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p projection_weights. + * @param[in] projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Data types supported: QSYMM8/F16/F32. + * @param[in] projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p projection_weights, S32 when @p input_to_input_weights is QSYMM8. * * @return Reference to this LSTMParams object */ @@ -106,8 +106,8 @@ public: } /** Set peephole tensor parameters. * - * @param[in] cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Data types supported: F16/F32. - * @param[in] cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p cell_to_input_weights. + * @param[in] cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Data types supported: QSYMM16/F16/F32. + * @param[in] cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p cell_to_forget_weights. * * @return Reference to this LSTMParams object */ @@ -120,7 +120,7 @@ public: } /** Set layer normalization tensor parameters. * - * @param[in] input_layer_norm_weights 1D weights tensor with dimensions [num_units]. Data type supported: Data types supported: F16/F32. + * @param[in] input_layer_norm_weights 1D weights tensor with dimensions [num_units]. Data type supported: Data types supported: QSYMM16/F16/F32. * @param[in] forget_layer_norm_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input_layer_norm_weights. * @param[in] cell_layer_norm_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input_layer_norm_weights. * @param[in] output_layer_norm_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input_layer_norm_weights. @@ -164,19 +164,19 @@ public: /** Set scale of the intermediate results of matmul of each layer parameters. * - * @param[in] input_gate_matmul_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at input gate. - * @param[in] forget_gate_matmul_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at forget gate. - * @param[in] cell_gate_matmul_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at cell gate. - * @param[in] output_gate_matmul_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at output gate. + * @param[in] input_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at input gate. + * @param[in] forget_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at forget gate. + * @param[in] cell_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at cell gate. + * @param[in] output_intermediate_scale Scale of the intermediate result of matmul, i.e. input to layer normalization, at output gate. * * @return Reference to this LSTMParams object */ - LSTMParams &set_matmul_scale_params(float input_gate_matmul_scale, float forget_gate_matmul_scale, float cell_gate_matmul_scale, float output_gate_matmul_scale) + LSTMParams &set_matmul_scale_params(float input_intermediate_scale, float forget_intermediate_scale, float cell_intermediate_scale, float output_intermediate_scale) { - _input_gate_matmul_scale = input_gate_matmul_scale; - _forget_gate_matmul_scale = forget_gate_matmul_scale; - _cell_gate_matmul_scale = cell_gate_matmul_scale; - _output_gate_matmul_scale = output_gate_matmul_scale; + _input_intermediate_scale = input_intermediate_scale; + _forget_intermediate_scale = forget_intermediate_scale; + _cell_intermediate_scale = cell_intermediate_scale; + _output_intermediate_scale = output_intermediate_scale; return *this; } @@ -187,7 +187,7 @@ public: * * @return Reference to this LSTMParams object */ - LSTMParams &set_matmul_scale_params(int32_t hidden_state_zero, float hidden_state_scale) + LSTMParams &set_hidden_state_params(int32_t hidden_state_zero, float hidden_state_scale) { _hidden_state_zero = hidden_state_zero; _hidden_state_scale = hidden_state_scale; @@ -264,24 +264,24 @@ public: return _projection_clip; } - float input_gate_matmul_scale() const + float input_intermediate_scale() const { - return _input_gate_matmul_scale; + return _input_intermediate_scale; } - float forget_gate_matmul_scale() const + float forget_intermediate_scale() const { - return _forget_gate_matmul_scale; + return _forget_intermediate_scale; } - float cell_gate_matmul_scale() const + float cell_intermediate_scale() const { - return _cell_gate_matmul_scale; + return _cell_intermediate_scale; } - float output_gate_matmul_scale() const + float output_intermediate_scale() const { - return _output_gate_matmul_scale; + return _output_intermediate_scale; } int32_t hidden_state_zero() const @@ -329,10 +329,10 @@ private: const T *_output_layer_norm_weights; float _cell_clip; float _projection_clip; - float _input_gate_matmul_scale; - float _forget_gate_matmul_scale; - float _cell_gate_matmul_scale; - float _output_gate_matmul_scale; + float _input_intermediate_scale; + float _forget_intermediate_scale; + float _cell_intermediate_scale; + float _output_intermediate_scale; float _hidden_state_zero; int32_t _hidden_state_scale; bool _has_peephole_opt; diff --git a/src/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.cpp b/src/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.cpp index 3082ff25d7..10336e544a 100644 --- a/src/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.cpp +++ b/src/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 ARM Limited. + * Copyright (c) 2017-2020 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -723,7 +723,7 @@ namespace Status validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output) { ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::S8, DataType::U8); - ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL, DataType::S8, DataType::U8); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8, DataType::QSYMM8_PER_CHANNEL, DataType::S8, DataType::U8); ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32); TensorShape in0_shape = input0->tensor_shape(); diff --git a/src/core/NEON/kernels/NEGEMMLowpReductionKernel.cpp b/src/core/NEON/kernels/NEGEMMLowpReductionKernel.cpp index b7e862c81f..1acdb1efce 100644 --- a/src/core/NEON/kernels/NEGEMMLowpReductionKernel.cpp +++ b/src/core/NEON/kernels/NEGEMMLowpReductionKernel.cpp @@ -45,7 +45,7 @@ namespace Status validate_arguments_matrix_a_reduction(const ITensorInfo *input, const ITensorInfo *output) { ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); - ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8); if(output->total_size() > 0) { @@ -77,7 +77,7 @@ std::pair validate_and_configure_window_matrix_a_reduction(ITens Status validate_arguments_matrix_b_reduction(const ITensorInfo *input, const ITensorInfo *output) { ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); - ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8, DataType::QSYMM8_PER_CHANNEL); if(output->total_size() > 0) { @@ -287,6 +287,7 @@ void NEGEMMLowpMatrixAReductionKernel::run(const Window &window, const ThreadInf run_internal(window); break; case DataType::QASYMM8_SIGNED: + case DataType::QSYMM8: case DataType::QSYMM8_PER_CHANNEL: run_internal(window); break; @@ -535,6 +536,7 @@ void NEGEMMLowpMatrixBReductionKernel::run(const Window &window, const ThreadInf run_internal(window, info); break; case DataType::QASYMM8_SIGNED: + case DataType::QSYMM8: case DataType::QSYMM8_PER_CHANNEL: run_internal(window, info); break; diff --git a/src/runtime/CL/functions/CLLSTMLayer.cpp b/src/runtime/CL/functions/CLLSTMLayer.cpp index 793d5ca1a9..3a3917784b 100644 --- a/src/runtime/CL/functions/CLLSTMLayer.cpp +++ b/src/runtime/CL/functions/CLLSTMLayer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2019 ARM Limited. + * Copyright (c) 2018-2020 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -23,19 +23,17 @@ */ #include "arm_compute/runtime/CL/functions/CLLSTMLayer.h" -#include "arm_compute/core/PixelValue.h" #include "arm_compute/core/Utils.h" #include "arm_compute/core/Validate.h" +#include "arm_compute/core/utils/misc/InfoHelpers.h" #include "arm_compute/core/utils/misc/ShapeCalculator.h" #include "arm_compute/core/utils/quantization/AsymmHelpers.h" #include "arm_compute/runtime/CL/CLScheduler.h" -#include -#include -#include - -using namespace arm_compute; +namespace arm_compute +{ using namespace arm_compute::misc::shape_calculator; +using namespace arm_compute::utils::info_helpers; CLLSTMLayer::CLLSTMLayer(std::shared_ptr memory_manager) : _memory_group(std::move(memory_manager)), _fully_connected_input_gate(), _accum_input_gate1(), _subtract_input_gate(), _pixelwise_mul_input_gate(), _activation_input_gate(), @@ -71,22 +69,8 @@ void CLLSTMLayer::configure(const ICLTensor *input, _is_layer_norm_lstm = lstm_params.use_layer_norm(); // Set lstm parameters - LSTMParams lstm_params_info; - if(lstm_params.has_peephole_opt()) - { - lstm_params_info.set_peephole_params(lstm_params.cell_to_forget_weights()->info(), lstm_params.cell_to_output_weights()->info()); - } - if(lstm_params.has_projection()) - { - lstm_params_info.set_projection_params(lstm_params.projection_weights()->info(), - lstm_params.projection_bias() != nullptr ? lstm_params.projection_bias()->info() : nullptr); - } - if(!lstm_params.has_cifg_opt()) - { - const ITensorInfo *cell_to_input_weights_info = (lstm_params.has_peephole_opt()) ? lstm_params.cell_to_input_weights()->info() : nullptr; - lstm_params_info.set_cifg_params(lstm_params.input_to_input_weights()->info(), lstm_params.recurrent_to_input_weights()->info(), - cell_to_input_weights_info, lstm_params.input_gate_bias()->info()); - } + LSTMParams lstm_params_info{}; + build_lstm_params_tensor_info(lstm_params, &lstm_params_info); // Validate ARM_COMPUTE_ERROR_THROW_ON(CLLSTMLayer::validate(input->info(), input_to_forget_weights->info(), @@ -729,3 +713,4 @@ void CLLSTMLayer::prepare() _is_prepared = true; } } +} // namespace arm_compute diff --git a/src/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.cpp b/src/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.cpp index a6ebcacf29..f0fa61657b 100644 --- a/src/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.cpp +++ b/src/runtime/NEON/functions/NEGEMMLowpMatrixMultiplyCore.cpp @@ -268,7 +268,7 @@ void NEGEMMLowpMatrixMultiplyCore::configure(const ITensor *a, const ITensor *b, Status NEGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info) { ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED); - ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(b, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(b, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8, DataType::QSYMM8_PER_CHANNEL); ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32, DataType::QASYMM8, DataType::QASYMM8_SIGNED); ARM_COMPUTE_RETURN_ERROR_ON_MSG(c != nullptr && gemm_info.gemmlowp_output_stage().type == GEMMLowpOutputStageType::NONE, "Bias addition not supported in NEGEMMLowpMatrixMultiplyCore for output S32"); ARM_COMPUTE_RETURN_ERROR_ON_MSG((a)->dimension(0) != (b)->dimension(1), diff --git a/src/runtime/NEON/functions/NELSTMLayer.cpp b/src/runtime/NEON/functions/NELSTMLayer.cpp index ee2b2f4b28..aac63e7643 100644 --- a/src/runtime/NEON/functions/NELSTMLayer.cpp +++ b/src/runtime/NEON/functions/NELSTMLayer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2019 ARM Limited. + * Copyright (c) 2018-2020 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -23,19 +23,17 @@ */ #include "arm_compute/runtime/NEON/functions/NELSTMLayer.h" -#include "arm_compute/core/PixelValue.h" #include "arm_compute/core/Utils.h" #include "arm_compute/core/Validate.h" +#include "arm_compute/core/utils/misc/InfoHelpers.h" #include "arm_compute/core/utils/misc/ShapeCalculator.h" #include "arm_compute/core/utils/quantization/AsymmHelpers.h" #include "arm_compute/runtime/common/LSTMParams.h" -#include -#include -#include - -using namespace arm_compute; +namespace arm_compute +{ using namespace arm_compute::misc::shape_calculator; +using namespace arm_compute::utils::info_helpers; NELSTMLayer::NELSTMLayer(std::shared_ptr memory_manager) : _memory_group(std::move(memory_manager)), _fully_connected_input_gate(), _accum_input_gate1(), _subtract_input_gate(), _pixelwise_mul_input_gate(), _activation_input_gate(), @@ -71,22 +69,8 @@ void NELSTMLayer::configure(const ITensor *input, _is_layer_norm_lstm = lstm_params.use_layer_norm(); // Set lstm parameters - LSTMParams lstm_params_info; - if(lstm_params.has_peephole_opt()) - { - lstm_params_info.set_peephole_params(lstm_params.cell_to_forget_weights()->info(), lstm_params.cell_to_output_weights()->info()); - } - if(lstm_params.has_projection()) - { - lstm_params_info.set_projection_params(lstm_params.projection_weights()->info(), - lstm_params.projection_bias() != nullptr ? lstm_params.projection_bias()->info() : nullptr); - } - if(!lstm_params.has_cifg_opt()) - { - const ITensorInfo *cell_to_input_weights_info = (lstm_params.has_peephole_opt()) ? lstm_params.cell_to_input_weights()->info() : nullptr; - lstm_params_info.set_cifg_params(lstm_params.input_to_input_weights()->info(), lstm_params.recurrent_to_input_weights()->info(), - cell_to_input_weights_info, lstm_params.input_gate_bias()->info()); - } + LSTMParams lstm_params_info{}; + build_lstm_params_tensor_info(lstm_params, &lstm_params_info); // Validate ARM_COMPUTE_ERROR_THROW_ON(NELSTMLayer::validate(input->info(), input_to_forget_weights->info(), @@ -726,3 +710,4 @@ void NELSTMLayer::prepare() _is_prepared = true; } } +} // namespace arm_compute diff --git a/src/runtime/NEON/functions/NEQLSTMLayer.cpp b/src/runtime/NEON/functions/NEQLSTMLayer.cpp new file mode 100644 index 0000000000..3aa77b28ce --- /dev/null +++ b/src/runtime/NEON/functions/NEQLSTMLayer.cpp @@ -0,0 +1,850 @@ +/* + * Copyright (c) 2020 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/runtime/NEON/functions/NEQLSTMLayer.h" + +#include "arm_compute/core/KernelDescriptors.h" +#include "arm_compute/core/QuantizationInfo.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/utils/misc/InfoHelpers.h" +#include "arm_compute/core/utils/quantization/AsymmHelpers.h" +#include "arm_compute/runtime/NEON/NEScheduler.h" + +namespace arm_compute +{ +using namespace arm_compute::utils::info_helpers; +namespace +{ +Status validate_mm(GEMMLowpOutputStageInfo &gemmlowp_info, const ITensorInfo *mm_input, const ITensorInfo *mm_weights, const ITensorInfo *bias, + float gemmlowp_scale, const TensorInfo *mm_res_info, const TensorInfo *outstage_tensor_info) +{ + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyCore::validate(mm_input, mm_weights, nullptr, mm_res_info)); + ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(gemmlowp_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift)); + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOutputStage::validate(mm_res_info, bias, outstage_tensor_info, gemmlowp_info)); + return Status{}; +} +} // namespace + +NEQLSTMLayer::NEQLSTMLayer(std::shared_ptr memory_manager) +{ + _memory_group = MemoryGroup(std::move(memory_manager)); +} + +void NEQLSTMLayer::configure_mm(NEGEMMLowpMatrixMultiplyCore &mm, NEGEMMLowpOutputStage &outstage, GEMMLowpOutputStageInfo &gemmlowp_info, + const ITensor *mm_input, const ITensor *mm_weights, const ITensor *bias, + Tensor *mm_res, Tensor *outstage_res, float gemmlowp_scale, + const TensorInfo &mm_res_info, const TensorInfo &outstage_tensor_info) +{ + _memory_group.manage(mm_res); + _memory_group.manage(outstage_res); + + mm_res->allocator()->init(mm_res_info); + outstage_res->allocator()->init(outstage_tensor_info); + + // Configure matrix-multiplication + mm.configure(mm_input, mm_weights, nullptr, mm_res); + + // Configure output stage + quantization::calculate_quantized_multiplier(gemmlowp_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift); + outstage.configure(mm_res, bias, outstage_res, gemmlowp_info); + mm_res->allocator()->allocate(); +} + +void NEQLSTMLayer::configure(const ITensor *input, + const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights, + const ITensor *recurrent_to_forget_weights, const ITensor *recurrent_to_cell_weights, const ITensor *recurrent_to_output_weights, + const ITensor *forget_gate_bias, const ITensor *cell_bias, const ITensor *output_gate_bias, + const ITensor *cell_state_in, const ITensor *output_state_in, + ITensor *cell_state_out, ITensor *output_state_out, + const LSTMParams &lstm_params) +{ + ARM_COMPUTE_UNUSED(forget_gate_bias); + ARM_COMPUTE_UNUSED(cell_bias); + ARM_COMPUTE_UNUSED(output_gate_bias); + ARM_COMPUTE_ERROR_ON_NULLPTR(input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights, + recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights, + forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in, cell_state_out, output_state_out); + + // Set lstm parameters + LSTMParams lstm_params_info{}; + build_lstm_params_tensor_info(lstm_params, &lstm_params_info); + + // Validate + ARM_COMPUTE_ERROR_THROW_ON(NEQLSTMLayer::validate(input->info(), input_to_forget_weights->info(), input_to_cell_weights->info(), input_to_output_weights->info(), + recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(), + forget_gate_bias->info(), cell_bias->info(), output_gate_bias->info(), + cell_state_in->info(), output_state_in->info(), cell_state_out->info(), output_state_out->info(), lstm_params_info)); + + const int batch_size = input->info()->dimension(1); + const int num_units = input_to_output_weights->info()->dimension(1); + + const UniformQuantizationInfo qinput = input->info()->quantization_info().uniform(); + const UniformQuantizationInfo qcell_state_in = cell_state_in->info()->quantization_info().uniform(); + const UniformQuantizationInfo qoutput_state_in = output_state_in->info()->quantization_info().uniform(); + + _projection_bias = lstm_params.projection_bias(); + _input_to_forget_weights = input_to_forget_weights; + _input_to_cell_weights = input_to_cell_weights; + _input_to_output_weights = input_to_output_weights; + _recurrent_to_forget_weights = recurrent_to_forget_weights; + _recurrent_to_cell_weights = recurrent_to_cell_weights; + _recurrent_to_output_weights = recurrent_to_output_weights; + _projection_weights = lstm_params.projection_weights(); + + _has_cifg = lstm_params.has_cifg_opt(); + _has_projection = lstm_params.has_projection(); + _has_peephole = lstm_params.has_peephole_opt(); + + // Calculate and decompose effective scales for optimizing matmul calculation + const int32_t cell_shift = log2(qcell_state_in.scale); + + // Calculate quantized parameters for clipping. + int16_t quantized_cell_clip = 0; + if(lstm_params.cell_clip() > 0.0f) + { + quantized_cell_clip = quantize_qsymm16(lstm_params.cell_clip(), qcell_state_in); + } + _has_cell_clipping = quantized_cell_clip > 0; + + // Precompute effective bias for optimizing the matmul computations. + if(!_has_cifg) + { + _input_to_input_weights = lstm_params.input_to_input_weights(); + _recurrent_to_input_weights = lstm_params.recurrent_to_input_weights(); + + _input_to_input_reduction.configure(_input_to_input_weights, &_input_to_input_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)); + _recurrent_to_input_reduction.configure(_recurrent_to_input_weights, &_recurrent_to_input_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true)); + } + _input_to_forget_reduction.configure(input_to_forget_weights, &_input_to_forget_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)); + _recurrent_to_forget_reduction.configure(recurrent_to_forget_weights, &_recurrent_to_forget_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true)); + _input_to_cell_reduction.configure(input_to_cell_weights, &_input_to_cell_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)); + _recurrent_to_cell_reduction.configure(recurrent_to_cell_weights, &_recurrent_to_cell_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true)); + _input_to_output_reduction.configure(input_to_output_weights, &_input_to_output_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)); + _recurrent_to_output_reduction.configure(recurrent_to_output_weights, &_recurrent_to_output_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true)); + if(_projection_bias != nullptr) + { + _projection_reduction.configure(_projection_weights, &_projection_reduction_res, GEMMLowpReductionKernelInfo(num_units, false, lstm_params.hidden_state_zero(), true)); + _projection_bias_add.configure(_projection_bias, &_projection_reduction_res, &_projection_eff_bias, ConvertPolicy::SATURATE); + } + + // Pre-transpose weights to be used in GEMM. + _transpose_input_to_forget_weights.configure(input_to_forget_weights, &_input_to_forget_weights_transposed); + _transpose_input_to_cell_weights.configure(input_to_cell_weights, &_input_to_cell_weights_transposed); + _transpose_input_to_output_weights.configure(input_to_output_weights, &_input_to_output_weights_transposed); + _transpose_recurrent_to_forget_weights.configure(recurrent_to_forget_weights, &_recurrent_to_forget_weights_transposed); + _transpose_recurrent_to_cell_weights.configure(recurrent_to_cell_weights, &_recurrent_to_cell_weights_transposed); + _transpose_recurrent_to_output_weights.configure(recurrent_to_output_weights, &_recurrent_to_output_weights_transposed); + if(!_has_cifg) + { + _transpose_input_to_input_weights.configure(lstm_params.input_to_input_weights(), &_input_to_input_weights_transposed); + _transpose_recurrent_to_input_weights.configure(lstm_params.recurrent_to_input_weights(), &_recurrent_to_input_weights_transposed); + } + if(_has_projection) + { + _transpose_projection_weights.configure(_projection_weights, &_projection_weights_transposed); + } + + GEMMLowpOutputStageInfo gemmlowp_info; + gemmlowp_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; + gemmlowp_info.gemmlowp_min_bound = std::numeric_limits::lowest(); + gemmlowp_info.gemmlowp_max_bound = std::numeric_limits::max(); + gemmlowp_info.output_data_type = DataType::QSYMM16; + + const TensorInfo mm_out_info(TensorShape(num_units, batch_size), 1, DataType::S32); + // Forget gate. + const TensorInfo forget_gate_outstage_info(mm_out_info.tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0)); + const float input_to_forget_scale = input_to_forget_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.forget_intermediate_scale(); + configure_mm(_mm_input_to_forget, _input_to_forget_outstage, gemmlowp_info, + input, &_input_to_forget_weights_transposed, &_input_to_forget_eff_bias, + &_mm_input_to_forget_res, &_input_to_forget_outstage_res, input_to_forget_scale, + mm_out_info, forget_gate_outstage_info); + + const float recurrent_to_forget_scale = recurrent_to_forget_weights->info()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.forget_intermediate_scale(); + configure_mm(_mm_recurrent_to_forget, _recurrent_to_forget_outstage, gemmlowp_info, + output_state_in, &_recurrent_to_forget_weights_transposed, &_recurrent_to_forget_eff_bias, + &_mm_recurrent_to_forget_res, &_recurrent_to_forget_outstage_res, recurrent_to_forget_scale, + mm_out_info, forget_gate_outstage_info); + + _accumulate_input_recurrent_forget.configure(&_input_to_forget_outstage_res, &_recurrent_to_forget_outstage_res, &_recurrent_to_forget_outstage_res, ConvertPolicy::SATURATE); + _input_to_forget_outstage_res.allocator()->allocate(); + + if(_has_peephole) + { + _memory_group.manage(&_mul_cell_to_forget_res); + _pixelwise_mul_cell_to_forget.configure(cell_state_in, lstm_params.cell_to_forget_weights(), &_mul_cell_to_forget_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); + _cell_to_forget_outstage_res.allocator()->init(TensorInfo(_mul_cell_to_forget_res.info()->tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0))); + _memory_group.manage(&_cell_to_forget_outstage_res); + const float cell_to_forget_scale = std::pow(2, cell_shift) * lstm_params.cell_to_forget_weights()->info()->quantization_info().uniform().scale / lstm_params.forget_intermediate_scale(); + quantization::calculate_quantized_multiplier(cell_to_forget_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift); + _cell_to_forget_outstage.configure(&_mul_cell_to_forget_res, nullptr, &_cell_to_forget_outstage_res, gemmlowp_info); + _mul_cell_to_forget_res.allocator()->allocate(); + _accumulate_cell_forget.configure(&_recurrent_to_forget_outstage_res, &_cell_to_forget_outstage_res, &_recurrent_to_forget_outstage_res, ConvertPolicy::SATURATE); + _cell_to_forget_outstage_res.allocator()->allocate(); + } + + // Output quantization info of Sigmoid and Tanh activations + const QuantizationInfo sigmoid_tanh_outqinfo(1.f / 32768.f, 0); + + const TensorInfo forget_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); + _memory_group.manage(&_forget_gate); + _forget_gate.allocator()->init(forget_gate_info); + _forget_gate_sigmoid.configure(&_recurrent_to_forget_outstage_res, &_forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + _recurrent_to_forget_outstage_res.allocator()->allocate(); + + // Modulation gate. + const TensorInfo cell_outstage_info(mm_out_info.tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.cell_intermediate_scale(), 0)); + const float input_to_cell_scale = input_to_cell_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.cell_intermediate_scale(); + configure_mm(_mm_input_to_cell, _input_to_cell_outstage, gemmlowp_info, + input, &_input_to_cell_weights_transposed, &_input_to_cell_eff_bias, + &_mm_input_to_cell_res, &_input_to_cell_outstage_res, input_to_cell_scale, + mm_out_info, cell_outstage_info); + + const float recurrent_to_cell_scale = recurrent_to_cell_weights->info()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.cell_intermediate_scale(); + configure_mm(_mm_recurrent_to_cell, _recurrent_to_cell_outstage, gemmlowp_info, + output_state_in, &_recurrent_to_cell_weights_transposed, &_recurrent_to_cell_eff_bias, + &_mm_recurrent_to_cell_res, &_recurrent_to_cell_outstage_res, recurrent_to_cell_scale, + mm_out_info, cell_outstage_info); + + _accumulate_input_recurrent_modulation.configure(&_input_to_cell_outstage_res, &_recurrent_to_cell_outstage_res, &_recurrent_to_cell_outstage_res, ConvertPolicy::SATURATE); + _input_to_cell_outstage_res.allocator()->allocate(); + + const TensorInfo cell_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); + _memory_group.manage(&_cell_gate); + _cell_gate.allocator()->init(cell_gate_info); + _cell_gate_tanh.configure(&_recurrent_to_cell_outstage_res, &_cell_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)); + _recurrent_to_cell_outstage_res.allocator()->allocate(); + + // Input gate. + const TensorInfo input_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); + _input_gate.allocator()->init(input_gate_info); + _memory_group.manage(&_input_gate); + if(_has_cifg) + { + _ones.allocator()->init(*_forget_gate.info()); + _input_gate_sub.configure(&_ones, &_forget_gate, &_input_gate, ConvertPolicy::SATURATE); + _ones.allocator()->allocate(); + } + else + { + const TensorInfo input_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0)); + const float input_to_input_scale = _input_to_input_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.input_intermediate_scale(); + configure_mm(_mm_input_to_input, _input_to_input_outstage, gemmlowp_info, + input, &_input_to_input_weights_transposed, &_input_to_input_eff_bias, + &_mm_input_to_input_res, &_input_to_input_outstage_res, input_to_input_scale, + mm_out_info, input_outstage_info); + + const float recurrent_to_input_scale = _recurrent_to_input_weights->info()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.input_intermediate_scale(); + configure_mm(_mm_recurrent_to_input, _recurrent_to_input_outstage, gemmlowp_info, + input, &_recurrent_to_input_weights_transposed, &_recurrent_to_input_eff_bias, + &_mm_recurrent_to_input_res, &_recurrent_to_input_outstage_res, recurrent_to_input_scale, + mm_out_info, input_outstage_info); + _accumulate_input_recurrent_input.configure(&_input_to_input_outstage_res, &_recurrent_to_input_outstage_res, &_recurrent_to_input_outstage_res, ConvertPolicy::SATURATE); + _input_to_input_outstage_res.allocator()->allocate(); + + if(_has_peephole) + { + _memory_group.manage(&_mul_cell_to_input_res); + _pixelwise_mul_cell_to_input.configure(cell_state_in, lstm_params.cell_to_input_weights(), &_mul_cell_to_input_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); + const float cell_to_input_scale = std::pow(2, cell_shift) * lstm_params.cell_to_input_weights()->info()->quantization_info().uniform().scale / lstm_params.input_intermediate_scale(); + quantization::calculate_quantized_multiplier(cell_to_input_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift); + _cell_to_input_outstage_res.allocator()->init(TensorInfo(_mul_cell_to_input_res.info()->tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0))); + _memory_group.manage(&_cell_to_input_outstage_res); + _cell_to_input_outstage.configure(&_mul_cell_to_input_res, nullptr, &_cell_to_input_outstage_res, gemmlowp_info); + _mul_cell_to_input_res.allocator()->allocate(); + _accumulate_cell_input.configure(&_recurrent_to_input_outstage_res, &_cell_to_input_outstage_res, &_recurrent_to_input_outstage_res, ConvertPolicy::SATURATE); + _cell_to_input_outstage_res.allocator()->allocate(); + } + + _input_gate_tanh.configure(&_recurrent_to_input_outstage_res, &_input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)); + _recurrent_to_input_outstage_res.allocator()->allocate(); + } + // Cell. + // TODO(COMPMID-3395): Perform multiplication in the quantized domain in NEPixelWiseMultiplicationKernel + _pixelwise_mul_forget_cell.configure(&_forget_gate, cell_state_in, &_forget_gate, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); + const float cell_gate_scale = _cell_gate.info()->quantization_info().uniform().scale; + const float mul_input_cell_scale = cell_gate_scale * std::pow(2, 15 + cell_shift); + const TensorInfo mul_input_cell_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(mul_input_cell_scale, 0)); + _memory_group.manage(&_mul_input_cell_res); + _mul_input_cell_res.allocator()->init(mul_input_cell_info); + _pixelwise_mul_input_cell.configure(&_input_gate, &_cell_gate, &_mul_input_cell_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); + _cell_gate.allocator()->allocate(); + _add_forget_cell.configure(&_forget_gate, &_mul_input_cell_res, cell_state_out, ConvertPolicy::SATURATE); + _mul_input_cell_res.allocator()->allocate(); + _forget_gate.allocator()->allocate(); + if(_has_cell_clipping) + { + _cell_clip.configure(cell_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_cell_clip, quantized_cell_clip)); + } + // Output gate. + const TensorInfo output_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.output_intermediate_scale(), 0)); + const float input_to_output_scale = input_to_output_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.output_intermediate_scale(); + configure_mm(_mm_input_to_output, _input_to_output_outstage, gemmlowp_info, + input, &_input_to_output_weights_transposed, &_input_to_output_eff_bias, + &_mm_input_to_output_res, &_input_to_output_outstage_res, input_to_output_scale, + mm_out_info, output_outstage_info); + + const float recurrent_to_output_scale = recurrent_to_output_weights->info()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.output_intermediate_scale(); + configure_mm(_mm_recurrent_to_output, _recurrent_to_output_outstage, gemmlowp_info, + output_state_in, &_recurrent_to_output_weights_transposed, &_recurrent_to_output_eff_bias, + &_mm_recurrent_to_output_res, &_recurrent_to_output_outstage_res, recurrent_to_output_scale, + mm_out_info, output_outstage_info); + + _accumulate_input_recurrent_output.configure(&_recurrent_to_output_outstage_res, &_input_to_output_outstage_res, &_recurrent_to_output_outstage_res, ConvertPolicy::SATURATE); + _input_to_output_outstage_res.allocator()->allocate(); + + if(_has_peephole) + { + // TODO(COMPMID-3395): Perform multiplication in the quantized domain in NEPixelWiseMultiplicationKernel + // Here we are not using the output stage because all operations are done in float + // const float cell_to_output_scale = std::pow(2, cell_shift) * lstm_params.cell_to_output_weights()->info()->quantization_info().uniform().scale / lstm_params.output_intermediate_scale(); + // quantization::calculate_quantized_multiplier(cell_to_output_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift); + _memory_group.manage(&_mul_cell_to_output_res); + _pixelwise_mul_cell_to_output.configure(cell_state_out, lstm_params.cell_to_output_weights(), &_mul_cell_to_output_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); + _accumulate_cell_to_output.configure(&_recurrent_to_output_outstage_res, &_mul_cell_to_output_res, &_recurrent_to_output_outstage_res, ConvertPolicy::SATURATE); + _mul_cell_to_output_res.allocator()->allocate(); + } + + const TensorInfo output_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); + _memory_group.manage(&_output_gate); + _output_gate.allocator()->init(output_gate_info); + _output_gate_sigmoid.configure(&_recurrent_to_output_outstage_res, &_output_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + _recurrent_to_output_outstage_res.allocator()->allocate(); + + // Hidden. + _hidden_tanh.configure(cell_state_out, &_input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)); + // TODO(COMPMID-3395): Perform multiplication in the quantized domain in NEPixelWiseMultiplicationKernel + _memory_group.manage(&_hidden_mul_res); + const TensorInfo hidden_mul_res(_input_gate.info()->tensor_shape(), 1, DataType::S32); + _hidden_mul_res.allocator()->init(hidden_mul_res); + _pixelwise_mul_hidden.configure(&_output_gate, &_input_gate, &_hidden_mul_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); + _output_gate.allocator()->allocate(); + _input_gate.allocator()->allocate(); + const float hidden_state_scale = std::pow(2, -15) / lstm_params.hidden_state_scale() * std::pow(2, -15); + quantization::calculate_quantized_multiplier(hidden_state_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift); + gemmlowp_info.gemmlowp_offset = lstm_params.hidden_state_zero(); + gemmlowp_info.output_data_type = output_state_in->info()->data_type(); + _hidden_outstage.configure(&_hidden_mul_res, nullptr, output_state_out, gemmlowp_info); + _hidden_mul_res.allocator()->allocate(); + + // Projection. + if(_has_projection) + { + const TensorInfo projection_outstage_info(*output_state_out->info()); + const UniformQuantizationInfo qprojection = _projection_weights->info()->quantization_info().uniform(); + const float projection_scale = qprojection.scale * lstm_params.hidden_state_scale() / qoutput_state_in.scale; + gemmlowp_info.gemmlowp_offset = qoutput_state_in.offset; + gemmlowp_info.gemmlowp_min_bound = std::numeric_limits::lowest(); + gemmlowp_info.gemmlowp_max_bound = std::numeric_limits::max(); + gemmlowp_info.output_data_type = DataType::QASYMM8_SIGNED; + + configure_mm(_mm_projection, _projection_outstage, gemmlowp_info, + output_state_out, &_projection_weights_transposed, &_projection_eff_bias, + &_mm_projection_res, &_projection_outstage_res, projection_scale, + mm_out_info, projection_outstage_info); + + _accumulate_projection.configure(&_projection_outstage_res, output_state_out, output_state_out, ConvertPolicy::SATURATE); + _projection_outstage_res.allocator()->allocate(); + + int8_t quantized_projection_clip{ 0 }; + if(lstm_params.projection_clip() > 0.0f) + { + quantized_projection_clip = utility::clamp(lstm_params.projection_clip() / qprojection.scale, -128, 127); + } + + if(quantized_projection_clip > 0) + { + _projection_clip.configure(output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_projection_clip, quantized_projection_clip)); + _has_projection_clipping = true; + } + } +} + +Status NEQLSTMLayer::validate(const ITensorInfo *input, + const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights, + const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights, + const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias, + const ITensorInfo *cell_state_in, const ITensorInfo *output_state_in, + const ITensorInfo *cell_state_out, const ITensorInfo *output_state_out, + const LSTMParams &lstm_params) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights, recurrent_to_forget_weights, recurrent_to_cell_weights, + recurrent_to_output_weights, forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in, cell_state_out, output_state_out); + + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() != 2, "Input must have exactly 2 dimensions"); + + const unsigned int input_size = input->dimension(0); + const unsigned int batch_size = input->dimension(1); + const unsigned int num_units = input_to_output_weights->dimension(1); + const unsigned int output_size = recurrent_to_output_weights->dimension(0); + + ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->num_dimensions() != 2); + ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->dimension(0) != input_size); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_to_output_weights, input_to_forget_weights, input_to_cell_weights); + ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->num_dimensions() != 2); + ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->dimension(1) != num_units); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(recurrent_to_output_weights, recurrent_to_forget_weights, recurrent_to_cell_weights); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_to_forget_weights, 1, DataType::QSYMM8); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_to_forget_weights, input_to_cell_weights, input_to_output_weights, + recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights); + + ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->num_dimensions() != 1); + ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->dimension(0) != num_units); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(forget_gate_bias, cell_bias, output_gate_bias); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(forget_gate_bias, 1, DataType::S32); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(forget_gate_bias, cell_bias, output_gate_bias); + + ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->num_dimensions() != 2); + ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->dimension(0) != num_units); + ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->dimension(1) != batch_size); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(cell_state_in, 1, DataType::QSYMM16); + + ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() != 2); + ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->dimension(0) != output_size); + ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->dimension(1) != batch_size); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output_state_in); + + // Check whether peephole weights are all there or none + if(lstm_params.has_peephole_opt()) + { + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights()); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_forget_weights(), 1, DataType::QSYMM16); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->num_dimensions() != 1); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->dimension(0) != num_units); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights()); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights()); + + if(!lstm_params.has_cifg_opt()) + { + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_input_weights()); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_input_weights()); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_input_weights()); + } + } + + const UniformQuantizationInfo qinput = input->quantization_info().uniform(); + const UniformQuantizationInfo qcell_state_in = cell_state_in->quantization_info().uniform(); + const UniformQuantizationInfo qoutput_state_in = output_state_in->quantization_info().uniform(); + + // Calculate and decompose effective scales for optimizing matmul calculation + const int32_t cell_shift = log2(qcell_state_in.scale); + ARM_COMPUTE_RETURN_ERROR_ON(cell_shift > -9); + + // Calculate quantized parameters for clipping. + int16_t quantized_cell_clip = 0; + if(lstm_params.cell_clip() > 0.0f) + { + quantized_cell_clip = quantize_qsymm16(lstm_params.cell_clip(), qcell_state_in); + } + + // Precompute effective bias for optimizing the matmul computations. + const TensorInfo eff_bias_info(TensorShape(num_units), 1, DataType::S32); + if(!lstm_params.has_cifg_opt()) + { + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(lstm_params.input_to_input_weights(), &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true))); + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(lstm_params.recurrent_to_input_weights(), &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, + true))); + } + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(input_to_forget_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true))); + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(recurrent_to_forget_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true))); + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(input_to_cell_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true))); + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(recurrent_to_cell_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true))); + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(input_to_output_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true))); + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(recurrent_to_output_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true))); + if(lstm_params.projection_bias() != nullptr) + { + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(lstm_params.projection_weights(), &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, lstm_params.hidden_state_zero(), + true))); + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(lstm_params.projection_bias(), &eff_bias_info, &eff_bias_info, ConvertPolicy::SATURATE)); + } + + const TensorInfo input_weights_transposed(TensorShape(num_units, input_size), 1, input_to_forget_weights->data_type(), input_to_forget_weights->quantization_info()); + const TensorInfo recurrent_weights_transposed(TensorShape(num_units, output_size), 1, recurrent_to_forget_weights->data_type(), recurrent_to_forget_weights->quantization_info()); + + // Validate weights transpose + ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(input_to_forget_weights, &input_weights_transposed)); + ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(input_to_cell_weights, &input_weights_transposed)); + ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(input_to_output_weights, &input_weights_transposed)); + ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(recurrent_to_forget_weights, &recurrent_weights_transposed)); + ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(recurrent_to_cell_weights, &recurrent_weights_transposed)); + ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(recurrent_to_output_weights, &recurrent_weights_transposed)); + if(!lstm_params.has_cifg_opt()) + { + ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(lstm_params.input_to_input_weights(), &input_weights_transposed)); + ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(lstm_params.recurrent_to_input_weights(), &recurrent_weights_transposed)); + } + if(lstm_params.has_projection()) + { + ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(lstm_params.projection_weights(), &recurrent_weights_transposed)); + } + + GEMMLowpOutputStageInfo gemmlowp_info; + gemmlowp_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; + gemmlowp_info.gemmlowp_min_bound = std::numeric_limits::lowest(); + gemmlowp_info.gemmlowp_max_bound = std::numeric_limits::max(); + gemmlowp_info.output_data_type = DataType::QSYMM16; + + // Forget gate. + const TensorInfo forget_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0)); + const TensorInfo mm_out_info(TensorShape(num_units, batch_size), 1, DataType::S32); + const float input_to_forget_scale = input_to_forget_weights->quantization_info().uniform().scale * qinput.scale / lstm_params.forget_intermediate_scale(); + validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, input_to_forget_scale, &mm_out_info, &forget_outstage_info); + + const float recurrent_to_forget_scale = recurrent_to_forget_weights->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.forget_intermediate_scale(); + validate_mm(gemmlowp_info, input, &recurrent_weights_transposed, &eff_bias_info, recurrent_to_forget_scale, &mm_out_info, &forget_outstage_info); + + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&forget_outstage_info, &forget_outstage_info, &forget_outstage_info, ConvertPolicy::SATURATE)); + + if(lstm_params.has_peephole_opt()) + { + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_forget_weights(), 1, DataType::QSYMM16); + ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_forget_weights(), &mm_out_info, 1.f, ConvertPolicy::SATURATE, + RoundingPolicy::TO_ZERO)); + const float cell_to_forget_scale = std::pow(2, cell_shift) * lstm_params.cell_to_forget_weights()->quantization_info().uniform().scale / lstm_params.forget_intermediate_scale(); + ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(cell_to_forget_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift)); + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOutputStage::validate(&mm_out_info, nullptr, &forget_outstage_info, gemmlowp_info)); + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&forget_outstage_info, &forget_outstage_info, &forget_outstage_info, ConvertPolicy::SATURATE)); + } + + // Output quantization info of Sigmoid and Tanh activations + const QuantizationInfo sigmoid_tanh_outqinfo(1.f / 32768.f, 0); + + const TensorInfo forget_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); + ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(&forget_outstage_info, &forget_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); + + // Modulation gate. + const TensorInfo cell_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.cell_intermediate_scale(), 0)); + const float input_to_cell_scale = input_to_cell_weights->quantization_info().uniform().scale * qinput.scale / lstm_params.cell_intermediate_scale(); + validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, input_to_cell_scale, &mm_out_info, &cell_outstage_info); + + const float recurrent_to_cell_scale = recurrent_to_cell_weights->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.cell_intermediate_scale(); + validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, recurrent_to_cell_scale, &mm_out_info, &cell_outstage_info); + + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&cell_outstage_info, &cell_outstage_info, &cell_outstage_info, ConvertPolicy::SATURATE)); + + const TensorInfo cell_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); + ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(&cell_outstage_info, &cell_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f))); + + // Input gate. + const TensorInfo input_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); + if(lstm_params.has_cifg_opt()) + { + ARM_COMPUTE_RETURN_ERROR_ON_MSG(lstm_params.input_gate_bias() != nullptr, "Input gate bias must not be present when CIFG is used"); + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticSubtractionKernel::validate(&input_gate_info, &forget_gate_info, &forget_gate_info, ConvertPolicy::SATURATE)); + } + else + { + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights(), lstm_params.input_gate_bias()); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_to_forget_weights, lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights()); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_to_forget_weights, lstm_params.input_to_input_weights()); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(recurrent_to_forget_weights, lstm_params.recurrent_to_input_weights()); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(forget_gate_bias, lstm_params.input_gate_bias()); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(forget_gate_bias, lstm_params.input_gate_bias()); + + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyCore::validate(input, lstm_params.input_to_input_weights(), nullptr, &mm_out_info)); + const TensorInfo input_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0)); + const float input_to_input_scale = lstm_params.input_to_input_weights()->quantization_info().uniform().scale * qinput.scale / lstm_params.input_intermediate_scale(); + validate_mm(gemmlowp_info, input, lstm_params.input_to_input_weights(), &eff_bias_info, input_to_input_scale, &mm_out_info, &input_outstage_info); + + const float recurrent_to_input_scale = lstm_params.recurrent_to_input_weights()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.input_intermediate_scale(); + validate_mm(gemmlowp_info, input, lstm_params.recurrent_to_input_weights(), &eff_bias_info, recurrent_to_input_scale, &mm_out_info, &input_outstage_info); + + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&input_outstage_info, &input_outstage_info, &input_outstage_info, ConvertPolicy::SATURATE)); + + if(lstm_params.has_peephole_opt()) + { + ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_input_weights(), &input_outstage_info, 1.f, ConvertPolicy::SATURATE, + RoundingPolicy::TO_ZERO)); + const float cell_to_input_scale = std::pow(2, cell_shift) * lstm_params.cell_to_input_weights()->quantization_info().uniform().scale / lstm_params.input_intermediate_scale(); + ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(cell_to_input_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift)); + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOutputStage::validate(&input_outstage_info, &eff_bias_info, &input_outstage_info, gemmlowp_info)); + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&input_outstage_info, &input_outstage_info, &input_outstage_info, ConvertPolicy::SATURATE)); + } + + ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(&input_outstage_info, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f))); + } + // Cell. + ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&forget_gate_info, cell_state_in, &forget_gate_info, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); + ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&input_gate_info, cell_state_in, &cell_gate_info, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&forget_gate_info, &cell_gate_info, cell_state_out, ConvertPolicy::SATURATE)); + if(quantized_cell_clip > 0) + { + ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(cell_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_cell_clip, + quantized_cell_clip))); + } + // Output gate. + const TensorInfo output_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.output_intermediate_scale(), 0)); + const float input_to_output_scale = input_to_output_weights->quantization_info().uniform().scale * qinput.scale / lstm_params.output_intermediate_scale(); + validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, input_to_output_scale, &mm_out_info, &output_outstage_info); + + const float recurrent_to_output_scale = recurrent_to_output_weights->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.output_intermediate_scale(); + validate_mm(gemmlowp_info, output_state_in, &recurrent_weights_transposed, &eff_bias_info, recurrent_to_output_scale, &mm_out_info, &output_outstage_info); + + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&output_outstage_info, &output_outstage_info, &output_outstage_info, ConvertPolicy::SATURATE)); + if(lstm_params.has_peephole_opt()) + { + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_output_weights(), 1, DataType::QSYMM16); + // TODO(COMPMID-3395): Perform multiplication in the quantized domain in NEPixelWiseMultiplicationKernel + // Here we are not using the output stage because all operations are done in float + // const float cell_to_output_scale = std::pow(2, cell_shift) * lstm_params.cell_to_output_weights()->quantization_info().uniform().scale / lstm_params.output_intermediate_scale(); + // ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(cell_to_output_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift)); + ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(cell_state_out, lstm_params.cell_to_output_weights(), &output_outstage_info, 1.f, ConvertPolicy::SATURATE, + RoundingPolicy::TO_ZERO)); + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&output_outstage_info, &output_outstage_info, &output_outstage_info, ConvertPolicy::SATURATE)); + } + + const TensorInfo output_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); + ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(&output_outstage_info, &output_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); + + // Hidden. + ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(cell_state_out, &input_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f))); + const TensorInfo hidden_mul_res(TensorShape(num_units, batch_size), 1, DataType::S32); + ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&output_gate_info, &input_gate_info, &hidden_mul_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); + const float hidden_state_scale = std::pow(2, -15) / lstm_params.hidden_state_scale() * std::pow(2, -15); + ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(hidden_state_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift)); + gemmlowp_info.gemmlowp_offset = lstm_params.hidden_state_zero(); + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOutputStage::validate(&hidden_mul_res, nullptr, output_state_out, gemmlowp_info)); + + // Projection. + if(lstm_params.has_projection()) + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(recurrent_to_forget_weights, lstm_params.projection_weights()); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(forget_gate_bias, lstm_params.projection_bias()); + + const UniformQuantizationInfo qprojection = lstm_params.projection_weights()->quantization_info().uniform(); + const float projection_scale = qprojection.scale * lstm_params.hidden_state_scale() / qoutput_state_in.scale; + ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(projection_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift)); + gemmlowp_info.gemmlowp_offset = qoutput_state_in.offset; + gemmlowp_info.gemmlowp_min_bound = std::numeric_limits::lowest(); + gemmlowp_info.gemmlowp_max_bound = std::numeric_limits::max(); + gemmlowp_info.output_data_type = DataType::QASYMM8_SIGNED; + + const TensorInfo projection_outstage_info(*output_state_out); + validate_mm(gemmlowp_info, output_state_out, &recurrent_weights_transposed, &eff_bias_info, input_to_output_scale, &mm_out_info, &projection_outstage_info); + + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(output_state_out, output_state_out, output_state_out, ConvertPolicy::SATURATE)); + + int8_t quantized_projection_clip{ 0 }; + if(lstm_params.projection_clip() > 0.0f) + { + quantized_projection_clip = quantize_qasymm8_signed(lstm_params.projection_clip(), qprojection); + } + + if(quantized_projection_clip > 0) + { + ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_projection_clip, + quantized_projection_clip))); + } + } + + if(cell_state_out->total_size() > 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(cell_state_in, cell_state_out); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(cell_state_in, cell_state_out); + } + + if(output_state_out->total_size() > 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output_state_out); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output_state_in, output_state_out); + } + + return Status{}; +} + +void NEQLSTMLayer::run() +{ + prepare(); + + // Acquire all the temporaries + MemoryGroupResourceScope scope_mg(_memory_group); + + // Forget gate. + _mm_input_to_forget.run(); + _input_to_forget_outstage.run(); + + _mm_recurrent_to_forget.run(); + _recurrent_to_forget_outstage.run(); + NEScheduler::get().schedule(&_accumulate_input_recurrent_forget, Window::DimY); + + if(_has_peephole) + { + NEScheduler::get().schedule(&_pixelwise_mul_cell_to_forget, Window::DimY); + _cell_to_forget_outstage.run(); + NEScheduler::get().schedule(&_accumulate_cell_forget, Window::DimY); + } + + _forget_gate_sigmoid.run(); + + // Modulation gate. + _mm_input_to_cell.run(); + _input_to_cell_outstage.run(); + + _mm_recurrent_to_cell.run(); + _recurrent_to_cell_outstage.run(); + NEScheduler::get().schedule(&_accumulate_input_recurrent_modulation, Window::DimY); + + _cell_gate_tanh.run(); + + // Input gate + if(_has_cifg) + { + NEScheduler::get().schedule(&_input_gate_sub, Window::DimY); + } + else + { + _mm_input_to_input.run(); + _input_to_input_outstage.run(); + _mm_recurrent_to_input.run(); + _recurrent_to_input_outstage.run(); + NEScheduler::get().schedule(&_accumulate_input_recurrent_input, Window::DimY); + + if(_has_peephole) + { + NEScheduler::get().schedule(&_pixelwise_mul_cell_to_input, Window::DimY); + _cell_to_input_outstage.run(); + NEScheduler::get().schedule(&_accumulate_cell_input, Window::DimY); + } + + _input_gate_tanh.run(); + } + + // Cell. + NEScheduler::get().schedule(&_pixelwise_mul_forget_cell, Window::DimY); + NEScheduler::get().schedule(&_pixelwise_mul_input_cell, Window::DimY); + NEScheduler::get().schedule(&_add_forget_cell, Window::DimY); + if(_has_cell_clipping) + { + _cell_clip.run(); + } + + // Output gate. + _mm_input_to_output.run(); + _input_to_output_outstage.run(); + _mm_recurrent_to_output.run(); + _recurrent_to_output_outstage.run(); + NEScheduler::get().schedule(&_accumulate_input_recurrent_output, Window::DimY); + if(_has_peephole) + { + NEScheduler::get().schedule(&_pixelwise_mul_cell_to_output, Window::DimY); + NEScheduler::get().schedule(&_accumulate_cell_to_output, Window::DimY); + } + + _output_gate_sigmoid.run(); + + // Hidden. + _hidden_tanh.run(); + NEScheduler::get().schedule(&_pixelwise_mul_hidden, Window::DimY); + _hidden_outstage.run(); + + // Projection. + if(_has_projection) + { + _mm_projection.run(); + _projection_outstage.run(); + NEScheduler::get().schedule(&_accumulate_projection, Window::DimY); + if(_has_projection_clipping) + { + _projection_clip.run(); + } + } +} + +void NEQLSTMLayer::prepare() +{ + if(!_is_prepared) + { + // Pre-transpose weights to be used in GEMM. + _input_to_forget_weights_transposed.allocator()->allocate(); + _input_to_cell_weights_transposed.allocator()->allocate(); + _input_to_output_weights_transposed.allocator()->allocate(); + _recurrent_to_forget_weights_transposed.allocator()->allocate(); + _recurrent_to_cell_weights_transposed.allocator()->allocate(); + _recurrent_to_output_weights_transposed.allocator()->allocate(); + _transpose_input_to_forget_weights.run(); + _transpose_input_to_cell_weights.run(); + _transpose_input_to_output_weights.run(); + _transpose_recurrent_to_forget_weights.run(); + _transpose_recurrent_to_cell_weights.run(); + _transpose_recurrent_to_output_weights.run(); + + // Precompute effective biases + if(_has_cifg) + { + std::fill_n(reinterpret_cast(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 32767); + } + else + { + _input_to_input_eff_bias.allocator()->allocate(); + _recurrent_to_input_eff_bias.allocator()->allocate(); + NEScheduler::get().schedule(&_input_to_input_reduction, Window::DimY); + NEScheduler::get().schedule(&_recurrent_to_input_reduction, Window::DimY); + + _input_to_input_weights_transposed.allocator()->allocate(); + _recurrent_to_input_weights_transposed.allocator()->allocate(); + _transpose_input_to_input_weights.run(); + _transpose_recurrent_to_input_weights.run(); + _input_to_input_weights->mark_as_unused(); + _recurrent_to_input_weights->mark_as_unused(); + } + _input_to_forget_eff_bias.allocator()->allocate(); + _recurrent_to_forget_eff_bias.allocator()->allocate(); + _input_to_cell_eff_bias.allocator()->allocate(); + _recurrent_to_cell_eff_bias.allocator()->allocate(); + _input_to_output_eff_bias.allocator()->allocate(); + _recurrent_to_output_eff_bias.allocator()->allocate(); + NEScheduler::get().schedule(&_input_to_forget_reduction, Window::DimY); + NEScheduler::get().schedule(&_recurrent_to_forget_reduction, Window::DimY); + NEScheduler::get().schedule(&_input_to_cell_reduction, Window::DimY); + NEScheduler::get().schedule(&_recurrent_to_cell_reduction, Window::DimY); + NEScheduler::get().schedule(&_input_to_output_reduction, Window::DimY); + NEScheduler::get().schedule(&_recurrent_to_output_reduction, Window::DimY); + + if(_has_projection) + { + if(_projection_bias != nullptr) + { + _projection_eff_bias.allocator()->allocate(); + NEScheduler::get().schedule(&_projection_reduction, Window::DimY); + _projection_bias->mark_as_unused(); + } + + _projection_weights_transposed.allocator()->allocate(); + _transpose_projection_weights.run(); + _projection_weights->mark_as_unused(); + } + + // Mark weights as unused + _input_to_forget_weights->mark_as_unused(); + _input_to_cell_weights->mark_as_unused(); + _input_to_output_weights->mark_as_unused(); + _recurrent_to_forget_weights->mark_as_unused(); + _recurrent_to_cell_weights->mark_as_unused(); + _recurrent_to_output_weights->mark_as_unused(); + + _is_prepared = true; + } +} + +} // namespace arm_compute -- cgit v1.2.1