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 --- .../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 ++-- 7 files changed, 437 insertions(+), 69 deletions(-) create mode 100644 arm_compute/runtime/NEON/functions/NEQLSTMLayer.h (limited to 'arm_compute') 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; -- cgit v1.2.1