diff options
-rw-r--r-- | arm_compute/runtime/CL/functions/CLLSTMLayer.h | 133 | ||||
-rw-r--r-- | arm_compute/runtime/NEON/NEFunctions.h | 1 | ||||
-rw-r--r-- | arm_compute/runtime/NEON/functions/NELSTMLayer.h | 210 | ||||
-rw-r--r-- | arm_compute/runtime/common/LSTMParams.h | 169 | ||||
-rw-r--r-- | src/runtime/NEON/functions/NELSTMLayer.cpp | 545 | ||||
-rw-r--r-- | tests/validation/NEON/LSTMLayer.cpp | 181 |
6 files changed, 1107 insertions, 132 deletions
diff --git a/arm_compute/runtime/CL/functions/CLLSTMLayer.h b/arm_compute/runtime/CL/functions/CLLSTMLayer.h index 6896a97e31..72e41a7aca 100644 --- a/arm_compute/runtime/CL/functions/CLLSTMLayer.h +++ b/arm_compute/runtime/CL/functions/CLLSTMLayer.h @@ -39,6 +39,7 @@ #include "arm_compute/runtime/CL/functions/CLGEMM.h" #include "arm_compute/runtime/CL/functions/CLWidthConcatenateLayer.h" #include "arm_compute/runtime/IMemoryManager.h" +#include "arm_compute/runtime/common/LSTMParams.h" #include <memory> @@ -46,138 +47,6 @@ namespace arm_compute { class ICLTensor; -template <typename T> -class LSTMParams -{ -public: - /** Constructor */ - LSTMParams() - : _input_to_input_weights(nullptr), _recurrent_to_input_weights(nullptr), _cell_to_input_weights(nullptr), _input_gate_bias(nullptr), _cell_to_forget_weights(nullptr), - _cell_to_output_weights(nullptr), _projection_weights(nullptr), _projection_bias(nullptr), _has_peephole_opt(false), _has_projection(false), _has_cifg_opt(true) - { - } - /** Prevent instances of this class from being copied (As this class contains pointers) */ - LSTMParams(const LSTMParams &) = delete; - /** Prevent instances of this class from being copied (As this class contains pointers) */ - LSTMParams &operator=(const LSTMParams &) = delete; - /** Default destructor */ - ~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] 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 - * - * @return Reference to this LSTMParams object - */ - LSTMParams &set_cifg_params(const T *input_to_input_weights, const T *recurrent_to_input_weights, const T *cell_to_input_weights, const T *input_gate_bias) - { - _input_to_input_weights = input_to_input_weights; - _recurrent_to_input_weights = recurrent_to_input_weights; - _cell_to_input_weights = cell_to_input_weights; - _input_gate_bias = input_gate_bias; - _has_cifg_opt = false; - return *this; - } - /** 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. - * - * @return Reference to this LSTMParams object - */ - LSTMParams &set_projection_params(const T *projection_weights, const T *projection_bias) - { - _projection_weights = projection_weights; - _projection_bias = projection_bias; - _has_projection = true; - return *this; - } - /** 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. - * - * @return Reference to this LSTMParams object - */ - LSTMParams &set_peephole_params(const T *cell_to_forget_weights, const T *cell_to_output_weights) - { - _cell_to_forget_weights = cell_to_forget_weights; - _cell_to_output_weights = cell_to_output_weights; - _has_peephole_opt = true; - return *this; - } - - const T *input_to_input_weights() const - { - return _input_to_input_weights; - } - - const T *recurrent_to_input_weights() const - { - return _recurrent_to_input_weights; - } - - const T *cell_to_input_weights() const - { - return _cell_to_input_weights; - } - - const T *input_gate_bias() const - { - return _input_gate_bias; - } - - const T *cell_to_forget_weights() const - { - return _cell_to_forget_weights; - } - - const T *cell_to_output_weights() const - { - return _cell_to_output_weights; - } - - const T *projection_weights() const - { - return _projection_weights; - } - - const T *projection_bias() const - { - return _projection_bias; - } - - bool has_peephole_opt() const - { - return _has_peephole_opt; - } - - bool has_projection() const - { - return _has_projection; - } - - bool has_cifg_opt() const - { - return _has_cifg_opt; - } - -private: - const T *_input_to_input_weights; - const T *_recurrent_to_input_weights; - const T *_cell_to_input_weights; - const T *_input_gate_bias; - const T *_cell_to_forget_weights; - const T *_cell_to_output_weights; - const T *_projection_weights; - const T *_projection_bias; - bool _has_peephole_opt; - bool _has_projection; - bool _has_cifg_opt; -}; - /** This function performs a single time step in a Long Short-Term Memory (LSTM) layer. * */ diff --git a/arm_compute/runtime/NEON/NEFunctions.h b/arm_compute/runtime/NEON/NEFunctions.h index e6d7114384..dabd78c256 100644 --- a/arm_compute/runtime/NEON/NEFunctions.h +++ b/arm_compute/runtime/NEON/NEFunctions.h @@ -82,6 +82,7 @@ #include "arm_compute/runtime/NEON/functions/NEIm2Col.h" #include "arm_compute/runtime/NEON/functions/NEIntegralImage.h" #include "arm_compute/runtime/NEON/functions/NEL2NormalizeLayer.h" +#include "arm_compute/runtime/NEON/functions/NELSTMLayer.h" #include "arm_compute/runtime/NEON/functions/NELaplacianPyramid.h" #include "arm_compute/runtime/NEON/functions/NELaplacianReconstruct.h" #include "arm_compute/runtime/NEON/functions/NELocallyConnectedLayer.h" diff --git a/arm_compute/runtime/NEON/functions/NELSTMLayer.h b/arm_compute/runtime/NEON/functions/NELSTMLayer.h new file mode 100644 index 0000000000..9c4ab2b068 --- /dev/null +++ b/arm_compute/runtime/NEON/functions/NELSTMLayer.h @@ -0,0 +1,210 @@ +/* + * Copyright (c) 2018 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_NELSTMLAYER_H__ +#define __ARM_COMPUTE_NELSTMLAYER_H__ + +#include "arm_compute/core/NEON/kernels/NEActivationLayerKernel.h" +#include "arm_compute/core/NEON/kernels/NEArithmeticAdditionKernel.h" +#include "arm_compute/core/NEON/kernels/NEArithmeticSubtractionKernel.h" +#include "arm_compute/core/NEON/kernels/NECopyKernel.h" +#include "arm_compute/core/NEON/kernels/NEPixelWiseMultiplicationKernel.h" + +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/NEON/INESimpleFunction.h" +#include "arm_compute/runtime/NEON/functions/NEArithmeticAddition.h" +#include "arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h" +#include "arm_compute/runtime/NEON/functions/NEGEMM.h" +#include "arm_compute/runtime/NEON/functions/NEWidthConcatenateLayer.h" +#include "arm_compute/runtime/common/LSTMParams.h" + +namespace arm_compute +{ +// Forward declarations +class ITensor; + +/** Basic function to run @ref NELSTMLayer */ +class NELSTMLayer : public IFunction +{ +public: + /** Default constructor */ + NELSTMLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); + /** Initialize function's tensors. + * + * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32. + * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. + * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. + * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. + * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. + * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input. + * @param[out] scratch_buffer 2D tensor with dimensions [num_units * 4, batch_size] with CIFG or [num_units * 3, batch_size] without CIGF. Data type supported: Same as @p input. + * @param[out] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. + * @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. + * @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. + */ + 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 *output_state_in, const ITensor *cell_state_in, + ITensor *scratch_buffer, ITensor *output_state_out, ITensor *cell_state_out, ITensor *output, + const LSTMParams<ITensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f); + + /** Static function to check if given info will lead to a valid configuration of @ref NELSTMLayer + * + * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32. + * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. + * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. + * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. + * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. + * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input. + * @param[in] scratch_buffer 2D tensor with dimensions [num_units * 4, batch_size] with CIFG or [num_units * 3, batch_size] without CIGF. Data type supported: Same as @p input. + * @param[in] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. + * @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. + * @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. + * + * @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 *output_state_in, const ITensorInfo *cell_state_in, + const ITensorInfo *scratch_buffer, const ITensorInfo *output_state_out, const ITensorInfo *cell_state_out, const ITensorInfo *output, + const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f); + + // Inherited methods overridden: + void run() override; + +private: + MemoryGroup _memory_group; + NEFullyConnectedLayer _fully_connected_input_gate; + NEGEMM _gemm_input_gate; + NETransposeKernel _transpose_input_gate; + NEArithmeticAdditionKernel _accum_input_gate1; + NEArithmeticAddition _accum_input_gate2; + NEArithmeticSubtractionKernel _subtract_input_gate; + NEPixelWiseMultiplicationKernel _pixelwise_mul_input_gate; + NEActivationLayerKernel _activation_input_gate; + NEFullyConnectedLayer _fully_connected_forget_gate; + NEGEMM _gemm_forget_gate; + NETransposeKernel _transpose_forget_gate; + NEArithmeticAdditionKernel _accum_forget_gate1; + NEArithmeticAddition _accum_forget_gate2; + NEPixelWiseMultiplicationKernel _pixelwise_mul_forget_gate; + NEActivationLayerKernel _activation_forget_gate; + NEFullyConnectedLayer _fully_connected_cell_state; + NEGEMM _gemm_cell_state1; + NEGEMM _gemm_cell_state2; + NETransposeKernel _transpose_cell_state; + NEArithmeticAdditionKernel _accum_cell_state1; + NEArithmeticAdditionKernel _accum_cell_state2; + NEPixelWiseMultiplicationKernel _pixelwise_mul_cell_state1; + NEActivationLayerKernel _activation_cell_state; + NEActivationLayerKernel _cell_clip; + NEPixelWiseMultiplicationKernel _pixelwise_mul_cell_state2; + NEFullyConnectedLayer _fully_connected_output; + NEGEMM _gemm_output; + NEPixelWiseMultiplicationKernel _pixelwise_mul_output_state1; + NETransposeKernel _transpose_output; + NEArithmeticAdditionKernel _accum_output1; + NEArithmeticAddition _accum_output2; + NEActivationLayerKernel _activation_output; + NEActivationLayerKernel _activation_output_state; + NEPixelWiseMultiplicationKernel _pixelwise_mul_output_state2; + NEFullyConnectedLayer _fully_connected_output_state; + NEGEMM _gemm_output_state; + NEArithmeticAdditionKernel _accum_output_state; + NEActivationLayerKernel _projection_clip; + NECopyKernel _copy_cell_state; + NECopyKernel _copy_output; + NEWidthConcatenateLayer _concat_scratch_buffer; + Tensor _input_gate_out1; + Tensor _input_gate_out2; + Tensor _input_gate_out3; + Tensor _input_gate_out4; + Tensor _input_gate_out5; + Tensor _forget_gate_out1; + Tensor _forget_gate_out2; + Tensor _forget_gate_out3; + Tensor _forget_gate_out4; + Tensor _forget_gate_out5; + Tensor _cell_state_out1; + Tensor _cell_state_out2; + Tensor _cell_state_out3; + Tensor _cell_state_out4; + Tensor _cell_state_out5; + Tensor _output1; + Tensor _output2; + Tensor _output3; + Tensor _output4; + Tensor _output5; + Tensor _cell_state_activation; + Tensor _output_state1; + Tensor _ones; + bool _run_peephole_opt; + bool _run_cifg_opt; + bool _perform_cell_clipping; + bool _has_projection_weights; + bool _perform_projection_clipping; +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_NELSTMLAYER_H__ */ diff --git a/arm_compute/runtime/common/LSTMParams.h b/arm_compute/runtime/common/LSTMParams.h new file mode 100644 index 0000000000..5b33e2e937 --- /dev/null +++ b/arm_compute/runtime/common/LSTMParams.h @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2018 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_LSTMPARAMS_H__ +#define __ARM_COMPUTE_LSTMPARAMS_H__ + +#include "arm_compute/core/IPyramid.h" +#include "arm_compute/core/PyramidInfo.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/Tensor.h" + +#include <cstddef> +#include <memory> + +namespace arm_compute +{ +template <typename T> +class LSTMParams +{ +public: + /** Constructor */ + LSTMParams() + : _input_to_input_weights(nullptr), _recurrent_to_input_weights(nullptr), _cell_to_input_weights(nullptr), _input_gate_bias(nullptr), _cell_to_forget_weights(nullptr), + _cell_to_output_weights(nullptr), _projection_weights(nullptr), _projection_bias(nullptr), _has_peephole_opt(false), _has_projection(false), _has_cifg_opt(true) + { + } + /** Prevent instances of this class from being copied (As this class contains pointers) */ + LSTMParams(const LSTMParams &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + LSTMParams &operator=(const LSTMParams &) = delete; + /** Default destructor */ + ~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] 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 + * + * @return Reference to this LSTMParams object + */ + LSTMParams &set_cifg_params(const T *input_to_input_weights, const T *recurrent_to_input_weights, const T *cell_to_input_weights, const T *input_gate_bias) + { + _input_to_input_weights = input_to_input_weights; + _recurrent_to_input_weights = recurrent_to_input_weights; + _cell_to_input_weights = cell_to_input_weights; + _input_gate_bias = input_gate_bias; + _has_cifg_opt = false; + return *this; + } + /** 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. + * + * @return Reference to this LSTMParams object + */ + LSTMParams &set_projection_params(const T *projection_weights, const T *projection_bias) + { + _projection_weights = projection_weights; + _projection_bias = projection_bias; + _has_projection = true; + return *this; + } + /** 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. + * + * @return Reference to this LSTMParams object + */ + LSTMParams &set_peephole_params(const T *cell_to_forget_weights, const T *cell_to_output_weights) + { + _cell_to_forget_weights = cell_to_forget_weights; + _cell_to_output_weights = cell_to_output_weights; + _has_peephole_opt = true; + return *this; + } + + const T *input_to_input_weights() const + { + return _input_to_input_weights; + } + + const T *recurrent_to_input_weights() const + { + return _recurrent_to_input_weights; + } + + const T *cell_to_input_weights() const + { + return _cell_to_input_weights; + } + + const T *input_gate_bias() const + { + return _input_gate_bias; + } + + const T *cell_to_forget_weights() const + { + return _cell_to_forget_weights; + } + + const T *cell_to_output_weights() const + { + return _cell_to_output_weights; + } + + const T *projection_weights() const + { + return _projection_weights; + } + + const T *projection_bias() const + { + return _projection_bias; + } + + bool has_peephole_opt() const + { + return _has_peephole_opt; + } + + bool has_projection() const + { + return _has_projection; + } + + bool has_cifg_opt() const + { + return _has_cifg_opt; + } + +private: + const T *_input_to_input_weights; + const T *_recurrent_to_input_weights; + const T *_cell_to_input_weights; + const T *_input_gate_bias; + const T *_cell_to_forget_weights; + const T *_cell_to_output_weights; + const T *_projection_weights; + const T *_projection_bias; + bool _has_peephole_opt; + bool _has_projection; + bool _has_cifg_opt; +}; +} +#endif /*__ARM_COMPUTE_LSTMPARAMS_H__ */ diff --git a/src/runtime/NEON/functions/NELSTMLayer.cpp b/src/runtime/NEON/functions/NELSTMLayer.cpp new file mode 100644 index 0000000000..934761a8ef --- /dev/null +++ b/src/runtime/NEON/functions/NELSTMLayer.cpp @@ -0,0 +1,545 @@ +/* + * Copyright (c) 2018 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/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/ShapeCalculator.h" +#include "arm_compute/core/utils/quantization/AsymmHelpers.h" +#include "arm_compute/runtime/common/LSTMParams.h" + +#include <cmath> +#include <memory> +#include <tuple> + +using namespace arm_compute; +using namespace arm_compute::misc::shape_calculator; + +NELSTMLayer::NELSTMLayer(std::shared_ptr<IMemoryManager> memory_manager) + : _memory_group(std::move(memory_manager)), _fully_connected_input_gate(), _gemm_input_gate(), _transpose_input_gate(), _accum_input_gate1(), _accum_input_gate2(), _subtract_input_gate(), + _pixelwise_mul_input_gate(), _activation_input_gate(), _fully_connected_forget_gate(), _gemm_forget_gate(), _transpose_forget_gate(), _accum_forget_gate1(), _accum_forget_gate2(), + _pixelwise_mul_forget_gate(), _activation_forget_gate(), _fully_connected_cell_state(), _gemm_cell_state1(), _gemm_cell_state2(), _transpose_cell_state(), _accum_cell_state1(), _accum_cell_state2(), + _pixelwise_mul_cell_state1(), _activation_cell_state(), _cell_clip(), _pixelwise_mul_cell_state2(), _fully_connected_output(), _gemm_output(), _pixelwise_mul_output_state1(), _transpose_output(), + _accum_output1(), _accum_output2(), _activation_output(), _activation_output_state(), _pixelwise_mul_output_state2(), _fully_connected_output_state(), _gemm_output_state(), _accum_output_state(), + _projection_clip(), _copy_cell_state(), _copy_output(), _concat_scratch_buffer(), _input_gate_out1(), _input_gate_out2(), _input_gate_out3(), _input_gate_out4(), _input_gate_out5(), + _forget_gate_out1(), _forget_gate_out2(), _forget_gate_out3(), _forget_gate_out4(), _forget_gate_out5(), _cell_state_out1(), _cell_state_out2(), _cell_state_out3(), _cell_state_out4(), + _cell_state_out5(), _output1(), _output2(), _output3(), _output4(), _output5(), _cell_state_activation(), _output_state1(), _ones(), _run_peephole_opt(false), _run_cifg_opt(false), + _perform_cell_clipping(false), _has_projection_weights(false), _perform_projection_clipping(false) +{ +} + +void NELSTMLayer::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 *output_state_in, const ITensor *cell_state_in, + ITensor *scratch_buffer, ITensor *output_state_out, ITensor *cell_state_out, ITensor *output, + const LSTMParams<ITensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold) +{ + 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, + output_state_in, cell_state_in, + scratch_buffer, output_state_out, cell_state_out, output); + + // Set lstm parameters + LSTMParams<ITensorInfo> 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()); + } + + // Validate + ARM_COMPUTE_ERROR_THROW_ON(NELSTMLayer::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(), + output_state_in->info(), cell_state_in->info(), + scratch_buffer->info(), output_state_out->info(), cell_state_out->info(), output->info(), + lstm_params_info, activation_info, cell_threshold, projection_threshold)); + + const TensorShape cell_state_shape = cell_state_in->info()->tensor_shape(); + + // Configure block that calculates the forget gate + // forget_gate = Activation(input * input_to_forget_weights + output_state_in * recurrent_to_forget_weights + PixelWiseMul(cell_state, cell_to_forget_weights) + forget_gate_bias) + TensorShape forget_gate1_shape = compute_transposed_shape(*recurrent_to_output_weights->info()); + _forget_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _forget_gate_out2.allocator()->init(TensorInfo(forget_gate1_shape, 1, input->info()->data_type())); + _forget_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _forget_gate_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + + _memory_group.manage(&_forget_gate_out1); + _fully_connected_forget_gate.configure(input, input_to_forget_weights, forget_gate_bias, &_forget_gate_out1); + _memory_group.manage(&_forget_gate_out2); + _transpose_forget_gate.configure(recurrent_to_forget_weights, &_forget_gate_out2); + _memory_group.manage(&_forget_gate_out3); + _gemm_forget_gate.configure(output_state_in, &_forget_gate_out2, nullptr, &_forget_gate_out3, 1.f, 0.f); + _forget_gate_out2.allocator()->allocate(); + _memory_group.manage(&_forget_gate_out5); + _accum_forget_gate1.configure(&_forget_gate_out1, &_forget_gate_out3, &_forget_gate_out5, ConvertPolicy::SATURATE); + Tensor *forget_gate_out = &_forget_gate_out5; + + if(lstm_params.has_peephole_opt()) + { + _forget_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + + _run_peephole_opt = true; + _memory_group.manage(&_forget_gate_out4); + _pixelwise_mul_forget_gate.configure(cell_state_in, lstm_params.cell_to_forget_weights(), &_forget_gate_out4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); + _accum_forget_gate2.configure(&_forget_gate_out5, &_forget_gate_out4, &_forget_gate_out3, ConvertPolicy::SATURATE); + _forget_gate_out4.allocator()->allocate(); + _forget_gate_out5.allocator()->allocate(); + forget_gate_out = &_forget_gate_out3; + } + else + { + _forget_gate_out3.allocator()->allocate(); + } + _activation_forget_gate.configure(forget_gate_out, &_forget_gate_out1, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + forget_gate_out->allocator()->allocate(); + + // Configure block that calculates the input gate + // input_gate = Activation(input * input_to_input_weights + output_state * recurrent_to_input_weights + PixelWiseMul(cell_state, cell_to_input_weights) + input_gate_bias), without CIFG + // input_gate = 1 - forget_gate, with CIFG + _input_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + if(lstm_params.has_cifg_opt()) + { + _memory_group.manage(&_input_gate_out1); + _ones.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _subtract_input_gate.configure(&_ones, &_forget_gate_out1, &_input_gate_out1, ConvertPolicy::SATURATE); + _ones.allocator()->allocate(); + _run_cifg_opt = true; + } + else + { + TensorShape input_gate_shape = compute_transposed_shape(*recurrent_to_output_weights->info()); + + _input_gate_out2.allocator()->init(TensorInfo(input_gate_shape, 1, input->info()->data_type())); + _input_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _input_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _input_gate_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + + _memory_group.manage(&_input_gate_out1); + _fully_connected_input_gate.configure(input, lstm_params.input_to_input_weights(), lstm_params.input_gate_bias(), &_input_gate_out1); + _memory_group.manage(&_input_gate_out2); + _transpose_input_gate.configure(lstm_params.recurrent_to_input_weights(), &_input_gate_out2); + _memory_group.manage(&_input_gate_out3); + _gemm_input_gate.configure(output_state_in, &_input_gate_out2, nullptr, &_input_gate_out3, 1.f, 0.f); + _input_gate_out2.allocator()->allocate(); + _memory_group.manage(&_input_gate_out4); + _accum_input_gate1.configure(&_input_gate_out1, &_input_gate_out3, &_input_gate_out4, ConvertPolicy::SATURATE); + if(_run_peephole_opt) + { + _memory_group.manage(&_input_gate_out5); + _pixelwise_mul_input_gate.configure(cell_state_in, lstm_params.cell_to_input_weights(), &_input_gate_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); + _accum_input_gate2.configure(&_input_gate_out4, &_input_gate_out5, &_input_gate_out1, ConvertPolicy::SATURATE); + _input_gate_out5.allocator()->allocate(); + } + _input_gate_out3.allocator()->allocate(); + _input_gate_out4.allocator()->allocate(); + _activation_input_gate.configure(&_input_gate_out1, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + } + + // Configure block that calculates the cell state + // cell_state = Clip((PixelwiseMul(input_gate, Activation(input * input_to_cell_weights + output_state_in * recurrent_to_cell_weights + cell_bias)) + PixelwiseMul(forget_gate, cell_state)), cell_threshold) + TensorShape cell_state1_shape = compute_transposed_shape(*recurrent_to_output_weights->info()); + _cell_state_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _cell_state_out2.allocator()->init(TensorInfo(cell_state1_shape, 1, input->info()->data_type())); + _cell_state_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _cell_state_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _cell_state_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + + _memory_group.manage(&_cell_state_out1); + _fully_connected_cell_state.configure(input, input_to_cell_weights, cell_bias, &_cell_state_out1); + _memory_group.manage(&_cell_state_out2); + _transpose_cell_state.configure(recurrent_to_cell_weights, &_cell_state_out2); + _memory_group.manage(&_cell_state_out3); + _gemm_cell_state1.configure(output_state_in, &_cell_state_out2, nullptr, &_cell_state_out3, 1.f, 0.f); + _cell_state_out2.allocator()->allocate(); + _memory_group.manage(&_cell_state_out4); + _accum_cell_state1.configure(&_cell_state_out1, &_cell_state_out3, &_cell_state_out4, ConvertPolicy::SATURATE); + _activation_cell_state.configure(&_cell_state_out4, nullptr, activation_info); + _memory_group.manage(&_cell_state_out5); + _pixelwise_mul_cell_state1.configure(&_cell_state_out4, &_input_gate_out1, &_cell_state_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); + _input_gate_out1.allocator()->allocate(); + _cell_state_out4.allocator()->allocate(); + _pixelwise_mul_cell_state2.configure(&_forget_gate_out1, cell_state_in, &_cell_state_out3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); + _forget_gate_out1.allocator()->allocate(); + _accum_cell_state2.configure(&_cell_state_out5, &_cell_state_out3, &_cell_state_out1, ConvertPolicy::SATURATE); + _cell_state_out3.allocator()->allocate(); + _cell_state_out5.allocator()->allocate(); + // Perform clipping + if(cell_threshold != 0.f) + { + _perform_cell_clipping = true; + _cell_clip.configure(&_cell_state_out1, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, cell_threshold)); + } + + // Configure block that calculates the output + // output_state_out = Activation(input * input_to_output_weights + output_state_in * recurrent_to_output_weights + PixelWiseMul(cell_state, cell_to_output_weights) + output_gate_bias) + TensorShape output1_shape = compute_transposed_shape(*recurrent_to_output_weights->info()); + _output1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _output2.allocator()->init(TensorInfo(output1_shape, 1, input->info()->data_type())); + _output3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _output5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + + _memory_group.manage(&_output1); + _fully_connected_output.configure(input, input_to_output_weights, output_gate_bias, &_output1); + _memory_group.manage(&_output2); + _transpose_output.configure(recurrent_to_output_weights, &_output2); + _memory_group.manage(&_output3); + _gemm_output.configure(output_state_in, &_output2, nullptr, &_output3, 1.f, 0.f); + _output2.allocator()->allocate(); + _memory_group.manage(&_output5); + _accum_output1.configure(&_output1, &_output3, &_output5, ConvertPolicy::SATURATE); + _output3.allocator()->allocate(); + Tensor *output_gate_out = &_output5; + if(lstm_params.has_peephole_opt()) + { + _output4.allocator()->init(TensorInfo(_cell_state_out1.info()->tensor_shape(), 1, input->info()->data_type())); + + _memory_group.manage(&_output4); + _pixelwise_mul_output_state1.configure(&_cell_state_out1, lstm_params.cell_to_output_weights(), &_output4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); + _accum_output2.configure(&_output5, &_output4, &_output1, ConvertPolicy::SATURATE); + _output5.allocator()->allocate(); + output_gate_out = &_output1; + + // Allocate intermediate buffers + _output4.allocator()->allocate(); + } + else + { + _output1.allocator()->allocate(); + } + _activation_output.configure(output_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + output_gate_out->allocator()->allocate(); + + // Configure block that calculates the output state + /** lstm_res = PixelwiseMul(output, Activation(cell_state)) + * + * -- Clip(lstm_res * projection_weights + projection_bias, projection_threshold) , if there is a projection + * / + * output_state = -- + * \ + * -- lstm_res , otherwise + */ + ITensor *output_state_out_tmp = lstm_params.has_projection() ? &_output_state1 : output_state_out; + _cell_state_activation.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _output_state1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + + _memory_group.manage(&_cell_state_activation); + _activation_output_state.configure(&_cell_state_out1, &_cell_state_activation, activation_info); + _pixelwise_mul_output_state2.configure(&_cell_state_activation, output_gate_out, output_state_out_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); + _cell_state_activation.allocator()->allocate(); + + if(lstm_params.has_projection()) + { + _has_projection_weights = true; + _fully_connected_output_state.configure(output_state_out_tmp, lstm_params.projection_weights(), lstm_params.projection_bias(), output_state_out); + _output_state1.allocator()->allocate(); + // Perform clipping + if(projection_threshold != 0.f) + { + _perform_projection_clipping = true; + _projection_clip.configure(output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold)); + } + } + + // Copy cell state and output + _copy_cell_state.configure(&_cell_state_out1, cell_state_out); + _cell_state_out1.allocator()->allocate(); + _copy_output.configure(output_state_out, output); + + // Vector for holding the tensors to store in scratch buffer + std::vector<ITensor *> scratch_inputs; + if(lstm_params.has_cifg_opt()) + { + scratch_inputs.emplace_back(&_input_gate_out1); + } + scratch_inputs.emplace_back(&_cell_state_out1); + scratch_inputs.emplace_back(forget_gate_out); + scratch_inputs.emplace_back(output_gate_out); + _concat_scratch_buffer.configure(scratch_inputs, scratch_buffer); +} + +Status NELSTMLayer::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 *output_state_in, const ITensorInfo *cell_state_in, + const ITensorInfo *scratch_buffer, const ITensorInfo *output_state_out, const ITensorInfo *cell_state_out, const ITensorInfo *output, + const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold) +{ + 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, + output_state_in, cell_state_in, + scratch_buffer, output_state_out, cell_state_out, output); + + // Check data types + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(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, + output_state_in, cell_state_in, + scratch_buffer, output_state_out, cell_state_out, output); + + // Check dimensions + ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(input_to_forget_weights->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(input_to_cell_weights->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_forget_weights->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_cell_weights->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->num_dimensions() > 1); + ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->num_dimensions() > 1); + ARM_COMPUTE_RETURN_ERROR_ON(output_gate_bias->num_dimensions() > 1); + ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(scratch_buffer->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(output_state_out->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(cell_state_out->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->dimension(0) * 4 != scratch_buffer->dimension(0) + && cell_bias->dimension(0) * 3 != scratch_buffer->dimension(0)); + + const unsigned int num_batches = input->dimension(1); + const unsigned int num_cells = input_to_output_weights->dimension(1); + + // Check peephole optimization + if(lstm_params.has_peephole_opt()) + { + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_output_weights(), lstm_params.cell_to_forget_weights()); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->num_dimensions() > 1); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_output_weights()->num_dimensions() > 1); + } + + TensorShape units_out_transposed_shape = compute_transposed_shape(*recurrent_to_output_weights); + TensorShape num_units_transposed_shape = compute_transposed_shape(*forget_gate_bias); + const TensorInfo units_out_transposed_info = TensorInfo(units_out_transposed_shape, 1, input->data_type()); + const TensorInfo num_units_transposed_info = TensorInfo(num_units_transposed_shape, 1, input->data_type()); + + TensorInfo input_gate = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type()); + TensorInfo forget_gate = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type()); + TensorInfo output_gate_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type()); + TensorInfo cell_state_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type()); + + // Validate forget gate + ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, input_to_forget_weights, forget_gate_bias, &forget_gate)); + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMM::validate(output_state_in, &units_out_transposed_info, nullptr, &forget_gate, 1.f, 0.f, GEMMInfo())); + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE)); + if(lstm_params.has_peephole_opt()) + { + ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_forget_weights(), &forget_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE)); + } + ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&forget_gate, &forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); + + // Validate input gate + if(!lstm_params.has_cifg_opt()) + { + 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(lstm_params.input_to_input_weights()->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.recurrent_to_input_weights()->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_gate_bias()->num_dimensions() > 1); + + ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, lstm_params.input_to_input_weights(), lstm_params.input_gate_bias(), &input_gate)); + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMM::validate(output_state_in, &units_out_transposed_info, nullptr, &input_gate, 1.f, 0.f, GEMMInfo())); + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&input_gate, &input_gate, &input_gate, ConvertPolicy::SATURATE)); + if(lstm_params.has_peephole_opt()) + { + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_input_weights()); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_input_weights()->num_dimensions() > 1); + ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_input_weights(), &input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&input_gate, &input_gate, &input_gate, ConvertPolicy::SATURATE)); + } + ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&input_gate, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); + } + else + { + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticSubtractionKernel::validate(&forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE)); + } + + // Validate cell state + ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, input_to_cell_weights, cell_bias, &cell_state_tmp)); + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMM::validate(output_state_in, &units_out_transposed_info, nullptr, &cell_state_tmp, 1.f, 0.f, GEMMInfo())); + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE)); + ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&cell_state_tmp, nullptr, activation_info)); + ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &input_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); + ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &forget_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE)); + if(cell_threshold != 0.f) + { + ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&cell_state_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, + cell_threshold))); + } + + // Validate output gate tmp + ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, input_to_output_weights, output_gate_bias, &output_gate_tmp)); + ARM_COMPUTE_RETURN_ON_ERROR(NEGEMM::validate(output_state_in, &units_out_transposed_info, nullptr, &output_gate_tmp, 1.f, 0.f, GEMMInfo())); + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&output_gate_tmp, &output_gate_tmp, &output_gate_tmp, ConvertPolicy::SATURATE)); + if(lstm_params.has_peephole_opt()) + { + ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, lstm_params.cell_to_output_weights(), &output_gate_tmp, 1, ConvertPolicy::SATURATE, + RoundingPolicy::TO_ZERO)); + ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&output_gate_tmp, &output_gate_tmp, &output_gate_tmp, ConvertPolicy::SATURATE)); + } + ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&output_gate_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); + + // Validate output state + ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&cell_state_tmp, &cell_state_tmp, activation_info)); + ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &output_gate_tmp, &output_gate_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); + if(lstm_params.has_projection()) + { + ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(&output_gate_tmp, lstm_params.projection_weights(), lstm_params.projection_bias(), output_state_out)); + if(projection_threshold != 0.f) + { + ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(output_state_out, output_state_out, + ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold))); + } + } + + // Validate copy kernel + ARM_COMPUTE_RETURN_ON_ERROR(NECopyKernel::validate(&cell_state_tmp, cell_state_out)); + ARM_COMPUTE_RETURN_ON_ERROR(NECopyKernel::validate(output_state_out, output)); + + // Validate scratch concatenation + std::vector<ITensorInfo *> inputs_vector_info_raw; + if(lstm_params.has_cifg_opt()) + { + inputs_vector_info_raw.push_back(&input_gate); + } + inputs_vector_info_raw.push_back(&cell_state_tmp); + inputs_vector_info_raw.push_back(&forget_gate); + inputs_vector_info_raw.push_back(&output_gate_tmp); + + ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayer::validate(inputs_vector_info_raw, scratch_buffer)); + return Status{}; +} + +void NELSTMLayer::run() +{ + _memory_group.acquire(); + + _fully_connected_forget_gate.run(); + NEScheduler::get().schedule(&_transpose_forget_gate, Window::DimY); + _gemm_forget_gate.run(); + NEScheduler::get().schedule(&_accum_forget_gate1, Window::DimY); + + if(_run_peephole_opt) + { + NEScheduler::get().schedule(&_pixelwise_mul_forget_gate, Window::DimY); + _accum_forget_gate2.run(); + } + NEScheduler::get().schedule(&_activation_forget_gate, Window::DimY); + + if(_run_cifg_opt) + { + if(_ones.info()->data_type() == DataType::F16) + { + std::fill_n(reinterpret_cast<half *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 1); + } + else + { + std::fill_n(reinterpret_cast<float *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 1); + } + NEScheduler::get().schedule(&_subtract_input_gate, Window::DimY); + } + else + { + _fully_connected_input_gate.run(); + NEScheduler::get().schedule(&_transpose_input_gate, Window::DimY); + _gemm_input_gate.run(); + NEScheduler::get().schedule(&_accum_input_gate1, Window::DimY); + if(_run_peephole_opt) + { + NEScheduler::get().schedule(&_pixelwise_mul_input_gate, Window::DimY); + _accum_input_gate2.run(); + } + NEScheduler::get().schedule(&_activation_input_gate, Window::DimY); + } + + _fully_connected_cell_state.run(); + NEScheduler::get().schedule(&_transpose_cell_state, Window::DimY); + _gemm_cell_state1.run(); + NEScheduler::get().schedule(&_accum_cell_state1, Window::DimY); + NEScheduler::get().schedule(&_activation_cell_state, Window::DimY); + NEScheduler::get().schedule(&_pixelwise_mul_cell_state1, Window::DimY); + NEScheduler::get().schedule(&_pixelwise_mul_cell_state2, Window::DimY); + NEScheduler::get().schedule(&_accum_cell_state2, Window::DimY); + + if(_perform_cell_clipping) + { + NEScheduler::get().schedule(&_cell_clip, Window::DimY); + } + + _fully_connected_output.run(); + NEScheduler::get().schedule(&_transpose_output, Window::DimY); + _gemm_output.run(); + NEScheduler::get().schedule(&_accum_output1, Window::DimY); + + if(_run_peephole_opt) + { + NEScheduler::get().schedule(&_pixelwise_mul_output_state1, Window::DimY); + _accum_output2.run(); + } + NEScheduler::get().schedule(&_activation_output, Window::DimY); + + NEScheduler::get().schedule(&_activation_output_state, Window::DimY); + NEScheduler::get().schedule(&_pixelwise_mul_output_state2, Window::DimY); + + if(_has_projection_weights) + { + _fully_connected_output_state.run(); + if(_perform_projection_clipping) + { + NEScheduler::get().schedule(&_projection_clip, Window::DimY); + } + } + + NEScheduler::get().schedule(&_copy_cell_state, Window::DimY); + NEScheduler::get().schedule(&_copy_output, Window::DimY); + + _concat_scratch_buffer.run(); + + _memory_group.release(); +}
\ No newline at end of file diff --git a/tests/validation/NEON/LSTMLayer.cpp b/tests/validation/NEON/LSTMLayer.cpp new file mode 100644 index 0000000000..3693d4f20d --- /dev/null +++ b/tests/validation/NEON/LSTMLayer.cpp @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2018 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/NELSTMLayer.h" +#include "tests/NEON/Accessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets/LSTMLayerDataset.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" +#include "tests/validation/fixtures/LSTMLayerFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +RelativeTolerance<float> tolerance_f32(0.001f); +RelativeTolerance<half> tolerance_f16(half(0.1)); +} // namespace + +TEST_SUITE(NEON) +TEST_SUITE(LSTMLayer) + +// *INDENT-OFF* +// clang-format off +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(zip(zip(zip(zip( + framework::dataset::make("InputInfo", { TensorInfo(TensorShape(8U, 2U), 1, DataType::U8), // Wrong data type + TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Wrong input size + TensorInfo(TensorShape(8U, 2U), 1, DataType::F32), // Wrong input weights size + TensorInfo(TensorShape(8U, 2U), 1, DataType::F32), // Wrong recurrent weights size + TensorInfo(TensorShape(8U, 2U), 1, DataType::F32), // Wrong cell bias size + TensorInfo(TensorShape(8U, 2U), 1, DataType::F32), // Wrong cell state size + TensorInfo(TensorShape(8U, 2U), 1, DataType::F32), // Wrong output size + TensorInfo(TensorShape(8U, 2U), 1, DataType::F32), // Wrong scratch size + }), + framework::dataset::make("InputWeightsInfo", { TensorInfo(TensorShape(8U, 16U), 1, DataType::F32), + TensorInfo(TensorShape(8U, 16U), 1, DataType::F32), + TensorInfo(TensorShape(27U, 11U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(8U, 16U), 1, DataType::F32), + TensorInfo(TensorShape(8U, 16U), 1, DataType::F32), + TensorInfo(TensorShape(8U, 16U), 1, DataType::F32), + TensorInfo(TensorShape(8U, 16U), 1, DataType::F32), + TensorInfo(TensorShape(8U, 16U), 1, DataType::F32), + })), + framework::dataset::make("RecurrentWeightsInfo", { TensorInfo(TensorShape(16U, 16U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 16U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 16U), 1, DataType::F32), + TensorInfo(TensorShape(25U, 11U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 16U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 16U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 16U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 16U), 1, DataType::F32), + })), + framework::dataset::make("CellBiasInfo", { TensorInfo(TensorShape(16U), 1, DataType::F32), + TensorInfo(TensorShape(16U), 1, DataType::F32), + TensorInfo(TensorShape(16U), 1, DataType::F32), + TensorInfo(TensorShape(16U), 1, DataType::F32), + TensorInfo(TensorShape(30U), 1, DataType::F32), + TensorInfo(TensorShape(16U), 1, DataType::F32), + TensorInfo(TensorShape(16U), 1, DataType::F32), + TensorInfo(TensorShape(16U), 1, DataType::F32), + })), + framework::dataset::make("ProjectionBiasInfo", { TensorInfo(TensorShape(16U), 1, DataType::F32), + TensorInfo(TensorShape(16U), 1, DataType::F32), + TensorInfo(TensorShape(16U), 1, DataType::F32), + TensorInfo(TensorShape(16U), 1, DataType::F32), + TensorInfo(TensorShape(16U), 1, DataType::F32), + TensorInfo(TensorShape(16U), 1, DataType::F32), + TensorInfo(TensorShape(16U), 1, DataType::F32), + TensorInfo(TensorShape(16U), 1, DataType::F32), + })), + framework::dataset::make("CellStateInfo", { TensorInfo(TensorShape(16U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(11U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32), + })), + framework::dataset::make("OutputInfo", { TensorInfo(TensorShape(16U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(11U, 13U), 1, DataType::F32), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32), + })), + framework::dataset::make("ScratchInfo", { TensorInfo(TensorShape(64U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(64U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(64U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(64U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(64U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(64U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(64U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(12U, 2U), 1, DataType::F32), + })), + framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), + ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), + ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), + ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), + ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), + ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), + ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), + ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), + })), + framework::dataset::make("Expected", { false, false, false, false, false, false, false, false })), + input_info, input_weights_info, recurrent_weights_info, cell_bias_info, projection_bias_info, cell_state_info, output_info, scratch_info, info, expected) +{ + LSTMParams<ITensorInfo> lstm_params_info; + lstm_params_info.set_peephole_params(&cell_bias_info, &cell_bias_info) + .set_projection_params(&recurrent_weights_info, &projection_bias_info) + .set_cifg_params(&input_weights_info, &recurrent_weights_info, &cell_bias_info, &cell_bias_info); + + ARM_COMPUTE_EXPECT(bool(NELSTMLayer::validate(&input_info.clone()->set_is_resizable(false), &input_weights_info.clone()->set_is_resizable(false), &input_weights_info.clone()->set_is_resizable(false), + &input_weights_info.clone()->set_is_resizable(false), &recurrent_weights_info.clone()->set_is_resizable(false), &recurrent_weights_info.clone()->set_is_resizable(false), + &recurrent_weights_info.clone()->set_is_resizable(false), &cell_bias_info.clone()->set_is_resizable(false), &cell_bias_info.clone()->set_is_resizable(false), + &cell_bias_info.clone()->set_is_resizable(false), + &output_info.clone()->set_is_resizable(false), &cell_state_info.clone()->set_is_resizable(false), + &scratch_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), &cell_state_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), + lstm_params_info, info, 0.05, 0.9)) == expected, framework::LogLevel::ERRORS); +} +// clang-format on +// *INDENT-ON* + +template <typename T> +using NELSTMLayerFixture = LSTMLayerValidationFixture<Tensor, Accessor, NELSTMLayer, LSTMParams<ITensor>, T>; + +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, NELSTMLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallLSTMLayerDataset(), framework::dataset::make("DataType", + DataType::F32)), + framework::dataset::make("ProjectionOpt", { true, false })), + framework::dataset::make("PeepholeOpt", { true, false }))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_f32); +} +TEST_SUITE_END() // FP32 + +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC +TEST_SUITE(FP16) +FIXTURE_DATA_TEST_CASE(RunSmall, NELSTMLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallLSTMLayerDataset(), framework::dataset::make("DataType", DataType::F16)), + framework::dataset::make("ProjectionOpt", { true, false })), + framework::dataset::make("PeepholeOpt", { true, false }))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_f16); +} +TEST_SUITE_END() // FP16 +#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ +TEST_SUITE_END() // LSTMLayer +TEST_SUITE_END() // NEON +} // namespace validation +} // namespace test +} // namespace arm_compute |