diff options
author | Michalis Spyrou <michalis.spyrou@arm.com> | 2018-03-22 14:55:08 +0000 |
---|---|---|
committer | Anthony Barbier <anthony.barbier@arm.com> | 2018-11-02 16:52:35 +0000 |
commit | bcedf513938fca9e33331bdef975f0488288bad4 (patch) | |
tree | c365f4387576a2b093368ce05f01ea253fe5570c | |
parent | a3221e6772dc371cf5de7e525bf5c22b58ad6d08 (diff) | |
download | ComputeLibrary-bcedf513938fca9e33331bdef975f0488288bad4.tar.gz |
COMPMID-993 Implement CL LSTM function
Change-Id: Iee4ad387c41dd8ccfe31b3044d797f2d7448e552
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/126655
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
-rw-r--r-- | arm_compute/runtime/CL/CLFunctions.h | 1 | ||||
-rw-r--r-- | arm_compute/runtime/CL/functions/CLLSTMLayer.h | 346 | ||||
-rw-r--r-- | src/runtime/CL/functions/CLLSTMLayer.cpp | 508 | ||||
-rw-r--r-- | tests/datasets/LSTMLayerDataset.h | 171 | ||||
-rw-r--r-- | tests/validation/CL/LSTMLayer.cpp | 177 | ||||
-rw-r--r-- | tests/validation/fixtures/LSTMLayerFixture.h | 404 |
6 files changed, 1607 insertions, 0 deletions
diff --git a/arm_compute/runtime/CL/CLFunctions.h b/arm_compute/runtime/CL/CLFunctions.h index a01e4a7978..fe90b0989f 100644 --- a/arm_compute/runtime/CL/CLFunctions.h +++ b/arm_compute/runtime/CL/CLFunctions.h @@ -79,6 +79,7 @@ #include "arm_compute/runtime/CL/functions/CLHistogram.h" #include "arm_compute/runtime/CL/functions/CLIntegralImage.h" #include "arm_compute/runtime/CL/functions/CLL2NormalizeLayer.h" +#include "arm_compute/runtime/CL/functions/CLLSTMLayer.h" #include "arm_compute/runtime/CL/functions/CLLaplacianPyramid.h" #include "arm_compute/runtime/CL/functions/CLLaplacianReconstruct.h" #include "arm_compute/runtime/CL/functions/CLLocallyConnectedLayer.h" diff --git a/arm_compute/runtime/CL/functions/CLLSTMLayer.h b/arm_compute/runtime/CL/functions/CLLSTMLayer.h new file mode 100644 index 0000000000..cf47f34290 --- /dev/null +++ b/arm_compute/runtime/CL/functions/CLLSTMLayer.h @@ -0,0 +1,346 @@ +/* + * 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_CLLSTMLAYER_H__ +#define __ARM_COMPUTE_CLLSTMLAYER_H__ + +#include "arm_compute/runtime/IFunction.h" + +#include "arm_compute/core/CL/kernels/CLActivationLayerKernel.h" +#include "arm_compute/core/CL/kernels/CLArithmeticAdditionKernel.h" +#include "arm_compute/core/CL/kernels/CLArithmeticSubtractionKernel.h" +#include "arm_compute/core/CL/kernels/CLCopyKernel.h" +#include "arm_compute/core/CL/kernels/CLPixelWiseMultiplicationKernel.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/CL/CLMemoryGroup.h" +#include "arm_compute/runtime/CL/CLTensor.h" +#include "arm_compute/runtime/CL/functions/CLArithmeticAddition.h" +#include "arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h" +#include "arm_compute/runtime/CL/functions/CLGEMM.h" +#include "arm_compute/runtime/CL/functions/CLWidthConcatenateLayer.h" +#include "arm_compute/runtime/IMemoryManager.h" + +#include <memory> + +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_input_weights 1D weights tensor with dimensions [num_units]. Data type supported: Data types supported: F16/F32. + * @param[in] cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p cell_to_input_weights. + * @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_input_weights, const T *cell_to_forget_weights, const T *cell_to_output_weights) + { + _cell_to_input_weights = cell_to_input_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. + * + */ +class CLLSTMLayer : public IFunction +{ +public: + /** Default constructor */ + CLLSTMLayer(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, out] output_state 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. + * @param[in, out] cell_state 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 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 ICLTensor *input, const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights, + const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights, + const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias, ICLTensor *output_state, ICLTensor *cell_state, ICLTensor *scratch_buffer, ICLTensor *output, + const LSTMParams<ICLTensor> &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 CLLSTMLayer + * + * @param[in] input Source tensor info. Input is a 2D tensor info with dimensions [input_size, batch_size]. Data types supported: F16/F32. + * @param[in] input_to_forget_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: Same as @p input. + * @param[in] input_to_cell_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: Same as @p input. + * @param[in] input_to_output_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: Same as @p input. + * @param[in] recurrent_to_forget_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * @param[in] recurrent_to_cell_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * @param[in] recurrent_to_output_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * @param[in] forget_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. + * @param[in] cell_bias 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. + * @param[in] output_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. + * @param[in] output_state 2D weights tensor info with dimensions [output_size, batch_size]. Data type supported: Same as @p input. + * @param[in] cell_state 2D tensor info with dimensions [num_units, batch_size]. Data type supported: Same as @p input. + * @param[in] scratch_buffer 2D tensor info 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 Destination tensor info. 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, const ITensorInfo *cell_state, const ITensorInfo *scratch_buffer, 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: + CLMemoryGroup _memory_group; + CLFullyConnectedLayer _fully_connected_input_gate; + CLGEMM _gemm_input_gate1; + CLGEMM _gemm_input_gate2; + CLTransposeKernel _transpose_input_gate1; + CLTransposeKernel _transpose_input_gate2; + CLArithmeticAdditionKernel _accum_input_gate1; + CLArithmeticAddition _accum_input_gate2; + CLArithmeticSubtractionKernel _subtract_input_gate; + CLActivationLayerKernel _activation_input_gate; + CLFullyConnectedLayer _fully_connected_forget_gate; + CLGEMM _gemm_forget_gate1; + CLGEMM _gemm_forget_gate2; + CLTransposeKernel _transpose_forget_gate1; + CLTransposeKernel _transpose_forget_gate2; + CLArithmeticAdditionKernel _accum_forget_gate1; + CLArithmeticAddition _accum_forget_gate2; + CLActivationLayerKernel _activation_forget_gate; + CLFullyConnectedLayer _fully_connected_cell_state; + CLGEMM _gemm_cell_state1; + CLGEMM _gemm_cell_state2; + CLTransposeKernel _transpose_cell_state1; + CLArithmeticAdditionKernel _accum_cell_state1; + CLArithmeticAdditionKernel _accum_cell_state2; + CLPixelWiseMultiplicationKernel _pixelwise_mul_cell_state1; + CLActivationLayerKernel _activation_cell_state; + CLActivationLayerKernel _cell_clip; + CLPixelWiseMultiplicationKernel _pixelwise_mul_cell_state2; + CLFullyConnectedLayer _fully_connected_output; + CLGEMM _gemm_output1; + CLGEMM _gemm_output2; + CLTransposeKernel _transpose_output1; + CLTransposeKernel _transpose_output2; + CLArithmeticAdditionKernel _accum_output1; + CLArithmeticAddition _accum_output2; + CLActivationLayerKernel _activation_output; + CLActivationLayerKernel _activation_output_state; + CLPixelWiseMultiplicationKernel _pixelwise_mul_output_state; + CLFullyConnectedLayer _fully_connected_output_state; + CLGEMM _gemm_output_state; + CLArithmeticAdditionKernel _accum_output_state; + CLActivationLayerKernel _projection_clip; + CLCopyKernel _copy_cell_state; + CLCopyKernel _copy_output; + CLWidthConcatenateLayer _concat_scratch_buffer; + CLTensor _input_gate_out1; + CLTensor _input_gate_out2; + CLTensor _input_gate_out3; + CLTensor _input_gate_out4; + CLTensor _input_gate_out5; + CLTensor _input_gate_out6; + CLTensor _forget_gate_out1; + CLTensor _forget_gate_out2; + CLTensor _forget_gate_out3; + CLTensor _forget_gate_out4; + CLTensor _forget_gate_out5; + CLTensor _forget_gate_out6; + CLTensor _cell_state_out1; + CLTensor _cell_state_out2; + CLTensor _cell_state_out3; + CLTensor _cell_state_out4; + CLTensor _cell_state_out5; + CLTensor _output1; + CLTensor _output2; + CLTensor _output3; + CLTensor _output4; + CLTensor _output5; + CLTensor _output6; + CLTensor _cell_state_activation; + CLTensor _output_projection1; + CLTensor _ones; + bool _run_peephole_opt; + bool _run_cifg_opt; + bool _perform_cell_clipping; + bool _has_projection_weights; + bool _perform_projection_clipping; +}; +} +#endif /* __ARM_COMPUTE_CLLSTMLAYER_H__ */ diff --git a/src/runtime/CL/functions/CLLSTMLayer.cpp b/src/runtime/CL/functions/CLLSTMLayer.cpp new file mode 100644 index 0000000000..930d311d1d --- /dev/null +++ b/src/runtime/CL/functions/CLLSTMLayer.cpp @@ -0,0 +1,508 @@ +/* + * 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/CL/functions/CLLSTMLayer.h" + +#include "arm_compute/core/PixelValue.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "arm_compute/core/utils/quantization/AsymmHelpers.h" +#include "arm_compute/runtime/CL/CLScheduler.h" + +#include <cmath> +#include <memory> +#include <tuple> + +using namespace arm_compute; +using namespace arm_compute::misc::shape_calculator; + +CLLSTMLayer::CLLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager) + : _memory_group(std::move(memory_manager)), _fully_connected_input_gate(), _gemm_input_gate1(), _gemm_input_gate2(), _transpose_input_gate1(), _transpose_input_gate2(), _accum_input_gate1(), + _accum_input_gate2(), _subtract_input_gate(), _activation_input_gate(), _fully_connected_forget_gate(), _gemm_forget_gate1(), _gemm_forget_gate2(), _transpose_forget_gate1(), + _transpose_forget_gate2(), _accum_forget_gate1(), _accum_forget_gate2(), _activation_forget_gate(), _fully_connected_cell_state(), _gemm_cell_state1(), _gemm_cell_state2(), _transpose_cell_state1(), + _accum_cell_state1(), _accum_cell_state2(), _pixelwise_mul_cell_state1(), _activation_cell_state(), _cell_clip(), _pixelwise_mul_cell_state2(), _fully_connected_output(), _gemm_output1(), + _gemm_output2(), _transpose_output1(), _transpose_output2(), _accum_output1(), _accum_output2(), _activation_output(), _activation_output_state(), _pixelwise_mul_output_state(), + _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(), _input_gate_out6(), _forget_gate_out1(), _forget_gate_out2(), _forget_gate_out3(), _forget_gate_out4(), _forget_gate_out5(), + _forget_gate_out6(), _cell_state_out1(), _cell_state_out2(), _cell_state_out3(), _cell_state_out4(), _cell_state_out5(), _output1(), _output2(), _output3(), _output4(), _output5(), _output6(), + _cell_state_activation(), _output_projection1(), _ones(), _run_peephole_opt(false), _run_cifg_opt(false), _perform_cell_clipping(false), _has_projection_weights(false), + _perform_projection_clipping(false) +{ +} + +void CLLSTMLayer::configure(const ICLTensor *input, const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights, + const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights, + const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias, + ICLTensor *output_state, ICLTensor *cell_state, ICLTensor *scratch_buffer, ICLTensor *output, const LSTMParams<ICLTensor> &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, cell_state); + LSTMParams<ITensorInfo> lstm_params_info; + if(lstm_params.has_peephole_opt()) + { + lstm_params_info.set_peephole_params(lstm_params.cell_to_input_weights()->info(), 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()->info()); + } + if(!lstm_params.has_cifg_opt()) + { + lstm_params_info.set_cifg_params(lstm_params.input_to_input_weights()->info(), lstm_params.recurrent_to_input_weights()->info(), + lstm_params.cell_to_input_weights()->info(), lstm_params.input_gate_bias()->info()); + } + ARM_COMPUTE_ERROR_THROW_ON(CLLSTMLayer::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->info(), cell_state->info(), scratch_buffer->info(), output->info(), lstm_params_info, + activation_info, cell_threshold, projection_threshold)); + + const TensorShape cell_state_shape = cell_state->info()->tensor_shape(); + + TensorShape forget_gate1_shape = compute_transposed_shape(*recurrent_to_output_weights->info()); + TensorShape forget_gate2_shape = compute_transposed_shape(*forget_gate_bias->info()); + TensorShape forget_gate3_shape{ 1, output_state->info()->dimension(1) }; + _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_out6.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + + // Configure block that calculates the forget gate + // forget_gate = Activation(input * input_to_forget_weights + output_state * recurrent_to_forget_weights + cell_state * cell_to_forget_weights + forget_gate_bias) + _memory_group.manage(&_forget_gate_out1); + _fully_connected_forget_gate.configure(input, input_to_forget_weights, forget_gate_bias, &_forget_gate_out1, true, false); + _memory_group.manage(&_forget_gate_out2); + _transpose_forget_gate1.configure(recurrent_to_forget_weights, &_forget_gate_out2); + _memory_group.manage(&_forget_gate_out3); + _gemm_forget_gate1.configure(output_state, &_forget_gate_out2, nullptr, &_forget_gate_out3, 1.f, 0.f); + _forget_gate_out2.allocator()->allocate(); + _memory_group.manage(&_forget_gate_out6); + _accum_forget_gate1.configure(&_forget_gate_out1, &_forget_gate_out3, &_forget_gate_out6, ConvertPolicy::SATURATE); + CLTensor *forget_gate_out = &_forget_gate_out6; + + if(lstm_params.has_peephole_opt()) + { + _forget_gate_out4.allocator()->init(TensorInfo(forget_gate2_shape, 1, input->info()->data_type())); + _forget_gate_out5.allocator()->init(TensorInfo(forget_gate3_shape, 1, input->info()->data_type())); + + _run_peephole_opt = true; + _memory_group.manage(&_forget_gate_out4); + _transpose_forget_gate2.configure(lstm_params.cell_to_forget_weights(), &_forget_gate_out4); + _memory_group.manage(&_forget_gate_out5); + _gemm_forget_gate2.configure(cell_state, &_forget_gate_out4, nullptr, &_forget_gate_out5, 1.f, 0.f); + _forget_gate_out4.allocator()->allocate(); + _accum_forget_gate2.configure(&_forget_gate_out6, &_forget_gate_out5, &_forget_gate_out3, ConvertPolicy::SATURATE); + _forget_gate_out5.allocator()->allocate(); + _forget_gate_out6.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(); + + TensorShape input_gate3_shape{ 1, output_state->info()->dimension(1) }; + _input_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _input_gate_out5.allocator()->init(TensorInfo(input_gate3_shape, 1, input->info()->data_type())); + + // Configure block that calculates the input gate + // input_gate = Activation(input * input_to_input_weights + output_state * recurrent_to_input_weights + cell_state * cell_to_input_weights + input_gate_bias), without CIFG + // input_gate = 1 - forget_gate, with CIFG + 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_gate1_shape = compute_transposed_shape(*recurrent_to_output_weights->info()); + TensorShape input_gate2_shape = compute_transposed_shape(*lstm_params.cell_to_input_weights()->info()); + + _input_gate_out2.allocator()->init(TensorInfo(input_gate1_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(input_gate2_shape, 1, input->info()->data_type())); + _input_gate_out6.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, true, false); + _memory_group.manage(&_input_gate_out2); + _transpose_input_gate1.configure(lstm_params.recurrent_to_input_weights(), &_input_gate_out2); + _memory_group.manage(&_input_gate_out3); + _gemm_input_gate1.configure(output_state, &_input_gate_out2, nullptr, &_input_gate_out3, 1.f, 0.f); + _input_gate_out2.allocator()->allocate(); + _memory_group.manage(&_input_gate_out4); + _transpose_input_gate2.configure(lstm_params.cell_to_input_weights(), &_input_gate_out4); + _memory_group.manage(&_input_gate_out5); + _gemm_input_gate2.configure(cell_state, &_input_gate_out4, nullptr, &_input_gate_out5, 1.f, 0.f); + _input_gate_out4.allocator()->allocate(); + _memory_group.manage(&_input_gate_out6); + _accum_input_gate1.configure(&_input_gate_out1, &_input_gate_out3, &_input_gate_out6, ConvertPolicy::SATURATE); + _input_gate_out3.allocator()->allocate(); + _accum_input_gate2.configure(&_input_gate_out6, &_input_gate_out5, &_input_gate_out1, ConvertPolicy::SATURATE); + _input_gate_out5.allocator()->allocate(); + _input_gate_out6.allocator()->allocate(); + _activation_input_gate.configure(&_input_gate_out1, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + } + + 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())); + + // Configure block that calculates the cell state + // cell_state = Clip((RixelwiseMul(input_gate, Activation(input * input_to_cell_weights + output_state * recurrent_to_cell_weights + cell_bias)) + PixelwiseMul(forget_gate, cell_state)), cell_threshold) + _memory_group.manage(&_cell_state_out1); + _fully_connected_cell_state.configure(input, input_to_cell_weights, cell_bias, &_cell_state_out1, true, false); + _memory_group.manage(&_cell_state_out2); + _transpose_cell_state1.configure(recurrent_to_cell_weights, &_cell_state_out2); + _memory_group.manage(&_cell_state_out3); + _gemm_cell_state1.configure(output_state, &_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_NEAREST_EVEN); + _input_gate_out1.allocator()->allocate(); + _cell_state_out4.allocator()->allocate(); + _pixelwise_mul_cell_state2.configure(&_forget_gate_out1, cell_state, &_cell_state_out3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + _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)); + } + + TensorShape output1_shape = compute_transposed_shape(*recurrent_to_output_weights->info()); + TensorShape output2_shape = compute_transposed_shape(*cell_bias->info()); + TensorShape output3_shape{ 1, output_state->info()->dimension(1) }; + _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())); + _output6.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + + // Configure block that calculates the output + // output_gate = Activation(input * input_to_output_weights + output_state * recurrent_to_output_weights + cell_state * cell_to_output_weights + output_gate_bias) + _memory_group.manage(&_output1); + _fully_connected_output.configure(input, input_to_output_weights, output_gate_bias, &_output1, true, false); + _memory_group.manage(&_output2); + _transpose_output1.configure(recurrent_to_output_weights, &_output2); + _memory_group.manage(&_output3); + _gemm_output1.configure(output_state, &_output2, nullptr, &_output3, 1.f, 0.f); + _output2.allocator()->allocate(); + _memory_group.manage(&_output6); + _accum_output1.configure(&_output1, &_output3, &_output6, ConvertPolicy::SATURATE); + _output3.allocator()->allocate(); + CLTensor *output_gate_out = &_output6; + if(lstm_params.has_peephole_opt()) + { + _output4.allocator()->init(TensorInfo(output2_shape, 1, input->info()->data_type())); + _output5.allocator()->init(TensorInfo(output3_shape, 1, input->info()->data_type())); + + _memory_group.manage(&_output4); + _transpose_output2.configure(lstm_params.cell_to_output_weights(), &_output4); + _memory_group.manage(&_output5); + _gemm_output2.configure(&_cell_state_out1, &_output4, nullptr, &_output5, 1.f, 0.f); + _accum_output2.configure(&_output6, &_output5, &_output1, ConvertPolicy::SATURATE); + _output6.allocator()->allocate(); + output_gate_out = &_output1; + + // Allocate intermediate buffers + _output4.allocator()->allocate(); + _output5.allocator()->allocate(); + } + else + { + _output1.allocator()->allocate(); + } + _activation_output.configure(output_gate_out, output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + output_gate_out->allocator()->allocate(); + + _cell_state_activation.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + + // 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 + */ + _memory_group.manage(&_cell_state_activation); + _activation_output_state.configure(&_cell_state_out1, &_cell_state_activation, activation_info); + _pixelwise_mul_output_state.configure(&_cell_state_activation, output, output_state, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + _cell_state_activation.allocator()->allocate(); + + if(lstm_params.has_projection()) + { + _has_projection_weights = true; + _output_projection1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _memory_group.manage(&_output_projection1); + _fully_connected_output_state.configure(output_state, lstm_params.projection_weights(), lstm_params.projection_bias(), &_output_projection1, true, false); + // Perform clipping + if(projection_threshold != 0.f) + { + _perform_projection_clipping = true; + _projection_clip.configure(&_output_projection1, output_state, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold)); + } + + // Allocate intermediate buffer + _output_projection1.allocator()->allocate(); + } + + // Copy cell state and output + _copy_cell_state.configure(&_cell_state_out1, cell_state); + _cell_state_out1.allocator()->allocate(); + _copy_output.configure(output_state, output); + + // Vector for holding the tensors to store in scratch buffer + std::vector<ICLTensor *> 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 CLLSTMLayer::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, const ITensorInfo *cell_state, const ITensorInfo *scratch_buffer, 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, cell_state); + 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, cell_state); + 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->num_dimensions() != 2); + ARM_COMPUTE_RETURN_ERROR_ON(cell_state->num_dimensions() != 2); + ARM_COMPUTE_RETURN_ERROR_ON(scratch_buffer->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)); + + if(lstm_params.has_peephole_opt()) + { + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_input_weights(), lstm_params.cell_to_output_weights(), lstm_params.cell_to_forget_weights()); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_input_weights()->num_dimensions() != 1); + 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 gemmv_shape{ 1, output_state->dimension(1) }; + 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 gemmv_shape_info = TensorInfo(gemmv_shape, 1, input->data_type()); + const TensorInfo num_units_transposed_info = TensorInfo(num_units_transposed_shape, 1, input->data_type()); + + // Validate forget gate + ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_forget_weights, forget_gate_bias, cell_state, true, false)); + ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(output_state, &units_out_transposed_info, nullptr, cell_state, 1.f, 0.f, GEMMInfo())); + ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAdditionKernel::validate(cell_state, cell_state, cell_state, ConvertPolicy::SATURATE)); + if(lstm_params.has_peephole_opt()) + { + ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(cell_state, &num_units_transposed_info, nullptr, &gemmv_shape_info, 1.f, 0.f, GEMMInfo())); + ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(cell_state, &gemmv_shape_info, cell_state, ConvertPolicy::SATURATE)); + } + ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(cell_state, cell_state, 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.cell_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.cell_to_input_weights()->num_dimensions() != 1); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_gate_bias()->num_dimensions() != 1); + ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, lstm_params.input_to_input_weights(), lstm_params.input_gate_bias(), cell_state, true, false)); + ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(cell_state, &num_units_transposed_info, nullptr, &gemmv_shape_info, 1.f, 0.f, GEMMInfo())); + ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(cell_state, &gemmv_shape_info, cell_state, ConvertPolicy::SATURATE)); + ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(cell_state, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); + } + else + { + ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticSubtractionKernel::validate(cell_state, cell_state, cell_state, ConvertPolicy::SATURATE)); + } + + // Validate cell state + ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_cell_weights, cell_bias, cell_state, true, false)); + ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(cell_state, nullptr, activation_info)); + ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(cell_state, cell_state, cell_state, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN)); + + if(cell_threshold != 0.f) + { + ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(cell_state, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, cell_threshold))); + } + + ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_output_weights, output_gate_bias, cell_state, true, false)); + if(lstm_params.has_peephole_opt()) + { + ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(cell_state, cell_state, cell_state, ConvertPolicy::SATURATE)); + } + ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(cell_state, output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); + + // Validate output state + ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(cell_state, cell_state, activation_info)); + ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(cell_state, output, output_state, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN)); + if(lstm_params.has_projection()) + { + ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(output_state, lstm_params.projection_weights(), lstm_params.projection_bias(), cell_state, true, false)); + if(projection_threshold != 0.f) + { + ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(cell_state, output_state, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, + projection_threshold))); + } + } + + std::vector<TensorInfo> inputs_vector_info; + if(lstm_params.has_cifg_opt()) + { + inputs_vector_info.emplace_back(*cell_state); + } + inputs_vector_info.emplace_back(*cell_state); + inputs_vector_info.emplace_back(*cell_state); + inputs_vector_info.emplace_back(*cell_state); + + std::vector<ITensorInfo *> inputs_vector_info_raw; + for(auto &input : inputs_vector_info) + { + inputs_vector_info_raw.emplace_back(&input); + } + + ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenateLayer::validate(inputs_vector_info_raw, scratch_buffer)); + return Status{}; +} + +void CLLSTMLayer::run() +{ + _memory_group.acquire(); + + _fully_connected_forget_gate.run(); + CLScheduler::get().enqueue(_transpose_forget_gate1); + _gemm_forget_gate1.run(); + CLScheduler::get().enqueue(_accum_forget_gate1); + + if(_run_peephole_opt) + { + CLScheduler::get().enqueue(_transpose_forget_gate2); + _gemm_forget_gate2.run(); + _accum_forget_gate2.run(); + } + CLScheduler::get().enqueue(_activation_forget_gate); + + if(_run_cifg_opt) + { + _ones.map(true); + std::fill_n(_ones.buffer(), _ones.info()->total_size(), 1); + _ones.unmap(); + CLScheduler::get().enqueue(_subtract_input_gate); + } + else + { + _fully_connected_input_gate.run(); + CLScheduler::get().enqueue(_transpose_input_gate1); + _gemm_input_gate1.run(); + CLScheduler::get().enqueue(_transpose_input_gate2); + _gemm_input_gate2.run(); + CLScheduler::get().enqueue(_accum_input_gate1); + _accum_input_gate2.run(); + CLScheduler::get().enqueue(_activation_input_gate); + } + + _fully_connected_cell_state.run(); + CLScheduler::get().enqueue(_transpose_cell_state1); + _gemm_cell_state1.run(); + CLScheduler::get().enqueue(_accum_cell_state1); + CLScheduler::get().enqueue(_activation_cell_state); + CLScheduler::get().enqueue(_pixelwise_mul_cell_state1); + CLScheduler::get().enqueue(_pixelwise_mul_cell_state2); + CLScheduler::get().enqueue(_accum_cell_state2); + + if(_perform_cell_clipping) + { + CLScheduler::get().enqueue(_cell_clip); + } + + _fully_connected_output.run(); + CLScheduler::get().enqueue(_transpose_output1); + _gemm_output1.run(); + CLScheduler::get().enqueue(_accum_output1); + CLScheduler::get().enqueue(_pixelwise_mul_output_state); + + if(_run_peephole_opt) + { + CLScheduler::get().enqueue(_transpose_output2); + _gemm_output2.run(); + _accum_output2.run(); + } + CLScheduler::get().enqueue(_activation_output); + + CLScheduler::get().enqueue(_activation_output_state); + CLScheduler::get().enqueue(_pixelwise_mul_output_state); + + if(_has_projection_weights) + { + _fully_connected_output_state.run(); + if(_perform_projection_clipping) + { + CLScheduler::get().enqueue(_projection_clip); + } + } + + CLScheduler::get().enqueue(_copy_cell_state); + CLScheduler::get().enqueue(_copy_output); + + _concat_scratch_buffer.run(); + + _memory_group.release(); +}
\ No newline at end of file diff --git a/tests/datasets/LSTMLayerDataset.h b/tests/datasets/LSTMLayerDataset.h new file mode 100644 index 0000000000..51802fd75b --- /dev/null +++ b/tests/datasets/LSTMLayerDataset.h @@ -0,0 +1,171 @@ +/* + * 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_TEST_LSTM_LAYER_DATASET +#define ARM_COMPUTE_TEST_LSTM_LAYER_DATASET + +#include "utils/TypePrinter.h" + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +namespace test +{ +namespace datasets +{ +class LSTMLayerDataset +{ +public: + using type = std::tuple<TensorShape, TensorShape, TensorShape, TensorShape, TensorShape, TensorShape, TensorShape, ActivationLayerInfo, float, float>; + + struct iterator + { + iterator(std::vector<TensorShape>::const_iterator src_it, + std::vector<TensorShape>::const_iterator input_weights_it, + std::vector<TensorShape>::const_iterator recurrent_weights_it, + std::vector<TensorShape>::const_iterator cells_bias_it, + std::vector<TensorShape>::const_iterator output_cell_it, + std::vector<TensorShape>::const_iterator dst_it, + std::vector<TensorShape>::const_iterator scratch_it, + std::vector<ActivationLayerInfo>::const_iterator infos_it, + std::vector<float>::const_iterator cell_threshold_it, + std::vector<float>::const_iterator projection_threshold_it) + : _src_it{ std::move(src_it) }, + _input_weights_it{ std::move(input_weights_it) }, + _recurrent_weights_it{ std::move(recurrent_weights_it) }, + _cells_bias_it{ std::move(cells_bias_it) }, + _output_cell_it{ std::move(output_cell_it) }, + _dst_it{ std::move(dst_it) }, + _scratch_it{ std::move(scratch_it) }, + _infos_it{ std::move(infos_it) }, + _cell_threshold_it{ std::move(cell_threshold_it) }, + _projection_threshold_it{ std::move(projection_threshold_it) } + { + } + + std::string description() const + { + std::stringstream description; + description << "In=" << *_src_it << ":"; + description << "InputWeights=" << *_input_weights_it << ":"; + description << "RecurrentWeights=" << *_recurrent_weights_it << ":"; + description << "Biases=" << *_cells_bias_it << ":"; + description << "Scratch=" << *_scratch_it << ":"; + description << "Out=" << *_dst_it; + return description.str(); + } + + LSTMLayerDataset::type operator*() const + { + return std::make_tuple(*_src_it, *_input_weights_it, *_recurrent_weights_it, *_cells_bias_it, *_output_cell_it, *_dst_it, *_scratch_it, *_infos_it, *_cell_threshold_it, *_projection_threshold_it); + } + + iterator &operator++() + { + ++_src_it; + ++_input_weights_it; + ++_recurrent_weights_it; + ++_cells_bias_it; + ++_output_cell_it; + ++_dst_it; + ++_scratch_it; + ++_infos_it; + ++_cell_threshold_it; + ++_projection_threshold_it; + + return *this; + } + + private: + std::vector<TensorShape>::const_iterator _src_it; + std::vector<TensorShape>::const_iterator _input_weights_it; + std::vector<TensorShape>::const_iterator _recurrent_weights_it; + std::vector<TensorShape>::const_iterator _cells_bias_it; + std::vector<TensorShape>::const_iterator _output_cell_it; + std::vector<TensorShape>::const_iterator _dst_it; + std::vector<TensorShape>::const_iterator _scratch_it; + std::vector<ActivationLayerInfo>::const_iterator _infos_it; + std::vector<float>::const_iterator _cell_threshold_it; + std::vector<float>::const_iterator _projection_threshold_it; + }; + + iterator begin() const + { + return iterator(_src_shapes.begin(), _input_weights_shapes.begin(), _recurrent_weights_shapes.begin(), _cell_bias_shapes.begin(), _output_cell_shapes.begin(), _dst_shapes.begin(), + _scratch_shapes.begin(), _infos.begin(), _cell_threshold.begin(), _projection_threshold.begin()); + } + + int size() const + { + return std::min(_src_shapes.size(), std::min(_input_weights_shapes.size(), std::min(_recurrent_weights_shapes.size(), std::min(_cell_bias_shapes.size(), std::min(_output_cell_shapes.size(), + std::min(_dst_shapes.size(), std::min(_scratch_shapes.size(), std::min(_cell_threshold.size(), std::min(_projection_threshold.size(), _infos.size()))))))))); + } + + void add_config(TensorShape src, TensorShape input_weights, TensorShape recurrent_weights, TensorShape cell_bias_weights, TensorShape output_cell_state, TensorShape dst, TensorShape scratch, + ActivationLayerInfo info, float cell_threshold, float projection_threshold) + { + _src_shapes.emplace_back(std::move(src)); + _input_weights_shapes.emplace_back(std::move(input_weights)); + _recurrent_weights_shapes.emplace_back(std::move(recurrent_weights)); + _cell_bias_shapes.emplace_back(std::move(cell_bias_weights)); + _output_cell_shapes.emplace_back(std::move(output_cell_state)); + _dst_shapes.emplace_back(std::move(dst)); + _scratch_shapes.emplace_back(std::move(scratch)); + _infos.emplace_back(std::move(info)); + _cell_threshold.emplace_back(std::move(cell_threshold)); + _projection_threshold.emplace_back(std::move(projection_threshold)); + } + +protected: + LSTMLayerDataset() = default; + LSTMLayerDataset(LSTMLayerDataset &&) = default; + +private: + std::vector<TensorShape> _src_shapes{}; + std::vector<TensorShape> _input_weights_shapes{}; + std::vector<TensorShape> _recurrent_weights_shapes{}; + std::vector<TensorShape> _cell_bias_shapes{}; + std::vector<TensorShape> _output_cell_shapes{}; + std::vector<TensorShape> _dst_shapes{}; + std::vector<TensorShape> _scratch_shapes{}; + std::vector<ActivationLayerInfo> _infos{}; + std::vector<float> _cell_threshold{}; + std::vector<float> _projection_threshold{}; +}; + +class SmallLSTMLayerDataset final : public LSTMLayerDataset +{ +public: + SmallLSTMLayerDataset() + { + add_config(TensorShape(8U, 2U), TensorShape(8U, 16U), TensorShape(16U, 16U), TensorShape(16U), TensorShape(16U, 2U), TensorShape(16U, 2U), TensorShape(64U, 2U), ActivationLayerInfo(), 0.05f, 0.93f); + add_config(TensorShape(8U, 2U), TensorShape(8U, 16U), TensorShape(16U, 16U), TensorShape(16U), TensorShape(16U, 2U), TensorShape(16U, 2U), TensorShape(48U, 2U), ActivationLayerInfo(), 0.05f, 0.93f); + } +}; + +} // namespace datasets +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_LSTM_LAYER_DATASET */ diff --git a/tests/validation/CL/LSTMLayer.cpp b/tests/validation/CL/LSTMLayer.cpp new file mode 100644 index 0000000000..bd43678844 --- /dev/null +++ b/tests/validation/CL/LSTMLayer.cpp @@ -0,0 +1,177 @@ +/* + * 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/CL/functions/CLLSTMLayer.h" +#include "tests/CL/CLAccessor.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(CL) +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, 0), // Wrong data type + TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32, 0), // Wrong input size + TensorInfo(TensorShape(8U, 2U), 1, DataType::F32, 0), // Wrong input weights size + TensorInfo(TensorShape(8U, 2U), 1, DataType::F32, 0), // Wrong recurrent weights size + TensorInfo(TensorShape(8U, 2U), 1, DataType::F32, 0), // Wrong cell bias size + TensorInfo(TensorShape(8U, 2U), 1, DataType::F32, 0), // Wrong cell state size + TensorInfo(TensorShape(8U, 2U), 1, DataType::F32, 0), // Wrong output size + TensorInfo(TensorShape(8U, 2U), 1, DataType::F32, 0), // Wrong scratch size + }), + framework::dataset::make("InputWeightsInfo", { TensorInfo(TensorShape(8U, 16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(8U, 16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(27U, 11U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(8U, 16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(8U, 16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(8U, 16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(8U, 16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(8U, 16U), 1, DataType::F32, 0), + })), + framework::dataset::make("RecurrentWeightsInfo", { TensorInfo(TensorShape(16U, 16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(25U, 11U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 16U), 1, DataType::F32, 0), + })), + framework::dataset::make("CellBiasInfo", { TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(30U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + })), + framework::dataset::make("ProjectionBiasInfo", { TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U), 1, DataType::F32, 0), + })), + framework::dataset::make("CellStateInfo", { TensorInfo(TensorShape(16U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(11U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32, 0), + })), + framework::dataset::make("OutputInfo", { TensorInfo(TensorShape(16U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(11U, 13U), 1, DataType::F32, 0), + TensorInfo(TensorShape(16U, 2U), 1, DataType::F32, 0), + })), + framework::dataset::make("ScratchInfo", { TensorInfo(TensorShape(64U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(64U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(64U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(64U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(64U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(64U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(64U, 2U), 1, DataType::F32, 0), + TensorInfo(TensorShape(12U, 2U), 1, DataType::F32, 0), + })), + 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, &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(CLLSTMLayer::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), lstm_params_info, info, 0.05, 0.9)) == expected, framework::LogLevel::ERRORS); +} +// clang-format on +// *INDENT-ON* + +template <typename T> +using CLLSTMLayerFixture = LSTMLayerValidationFixture<CLTensor, CLAccessor, CLLSTMLayer, LSTMParams<ICLTensor>, T>; + +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, CLLSTMLayerFixture<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(CLAccessor(_target), _reference, tolerance_f32); +} +TEST_SUITE_END() // FP32 + +TEST_SUITE(FP16) +FIXTURE_DATA_TEST_CASE(RunSmall, CLLSTMLayerFixture<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(CLAccessor(_target), _reference, tolerance_f16); +} +TEST_SUITE_END() // FP16 +TEST_SUITE_END() // LSTMLayer +TEST_SUITE_END() // CL +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/LSTMLayerFixture.h b/tests/validation/fixtures/LSTMLayerFixture.h new file mode 100644 index 0000000000..b7e43b3470 --- /dev/null +++ b/tests/validation/fixtures/LSTMLayerFixture.h @@ -0,0 +1,404 @@ +/* + * 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_TEST_LSTM_LAYER_FIXTURE +#define ARM_COMPUTE_TEST_LSTM_LAYER_FIXTURE + +#include "tests/Globals.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/reference/ActivationLayer.h" +#include "tests/validation/reference/ArithmeticAddition.h" +#include "tests/validation/reference/ArithmeticSubtraction.h" +#include "tests/validation/reference/FullyConnectedLayer.h" +#include "tests/validation/reference/GEMM.h" +#include "tests/validation/reference/PixelWiseMultiplication.h" +#include "tests/validation/reference/Transpose.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template <typename TensorType, typename AccessorType, typename FunctionType, typename FunctionParams, typename T> +class LSTMLayerValidationFixture : public framework::Fixture +{ +public: + template <typename...> + void setup(TensorShape input_shape, TensorShape input_weights_shape, TensorShape recurrent_weights_shape, TensorShape cell_bias_shape, TensorShape output_cell_shape, TensorShape output_shape, + TensorShape scratch_shape, ActivationLayerInfo info, float cell_threshold, float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt) + { + _target = compute_target(input_shape, input_weights_shape, recurrent_weights_shape, cell_bias_shape, output_cell_shape, output_shape, scratch_shape, info, cell_threshold, projection_threshold, + data_type, projection_opt, peephole_opt); + _reference = compute_reference(input_shape, input_weights_shape, recurrent_weights_shape, cell_bias_shape, output_cell_shape, output_shape, scratch_shape, info, cell_threshold, projection_threshold, + data_type, projection_opt, peephole_opt); + } + +protected: + template <typename U> + void fill(U &&tensor, int i) + { + std::uniform_real_distribution<> distribution(-1.0f, 1.0f); + library->fill(tensor, distribution, i); + } + template <typename U> + void fill_custom_val(U &&tensor, float num, int i) + { + std::uniform_real_distribution<> distribution(num, num); + library->fill(tensor, distribution, i); + } + TensorType compute_target(const TensorShape &input_shape, const TensorShape &input_weights_shape, const TensorShape &recurrent_weights_shape, const TensorShape &cell_bias_shape, + const TensorShape &output_cell_shape, const TensorShape &output_shape, const TensorShape &scratch_shape, ActivationLayerInfo info, float cell_threshold, + float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt) + { + // Create projection bias shape + TensorShape projection_bias_shape{}; + projection_bias_shape.set(0, output_shape.x()); + + // Create tensors + TensorType input = create_tensor<TensorType>(input_shape, data_type); + TensorType input_to_forget_w = create_tensor<TensorType>(input_weights_shape, data_type); + TensorType input_to_cell_w = create_tensor<TensorType>(input_weights_shape, data_type); + TensorType input_to_output_w = create_tensor<TensorType>(input_weights_shape, data_type); + TensorType recurrent_to_forget_w = create_tensor<TensorType>(recurrent_weights_shape, data_type); + TensorType recurrent_to_cell_w = create_tensor<TensorType>(recurrent_weights_shape, data_type); + TensorType recurrent_to_output_w = create_tensor<TensorType>(recurrent_weights_shape, data_type); + TensorType forget_gate_bias = create_tensor<TensorType>(cell_bias_shape, data_type); + TensorType cell_bias = create_tensor<TensorType>(cell_bias_shape, data_type); + TensorType output_gate_bias = create_tensor<TensorType>(cell_bias_shape, data_type); + TensorType output_state = create_tensor<TensorType>(output_shape, data_type); + TensorType cell_state = create_tensor<TensorType>(output_cell_shape, data_type); + TensorType scratch = create_tensor<TensorType>(scratch_shape, data_type); + TensorType output = create_tensor<TensorType>(output_shape, data_type); + TensorType input_to_input_w; + TensorType recurrent_to_input_w; + TensorType cell_to_input_w; + TensorType cell_to_forget_w; + TensorType input_gate_bias; + TensorType cell_to_output_w; + TensorType projection_w; + TensorType projection_bias; + + bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? true : false; + + FunctionParams lstm_params; + + if(!cifg_opt) + { + input_to_input_w = create_tensor<TensorType>(input_weights_shape, data_type); + recurrent_to_input_w = create_tensor<TensorType>(recurrent_weights_shape, data_type); + cell_to_input_w = create_tensor<TensorType>(cell_bias_shape, data_type); + input_gate_bias = create_tensor<TensorType>(cell_bias_shape, data_type); + lstm_params.set_cifg_params(&input_to_input_w, &recurrent_to_input_w, &cell_to_input_w, &input_gate_bias); + } + + if(peephole_opt) + { + if(cifg_opt) + { + cell_to_input_w = create_tensor<TensorType>(cell_bias_shape, data_type); + } + cell_to_forget_w = create_tensor<TensorType>(cell_bias_shape, data_type); + cell_to_output_w = create_tensor<TensorType>(cell_bias_shape, data_type); + lstm_params.set_peephole_params(&cell_to_input_w, &cell_to_forget_w, &cell_to_output_w); + } + + if(projection_opt) + { + projection_w = create_tensor<TensorType>(recurrent_weights_shape, data_type); + projection_bias = create_tensor<TensorType>(projection_bias_shape, data_type); + lstm_params.set_projection_params(&projection_w, &projection_bias); + } + + // Create and configure function + FunctionType lstm; + lstm.configure(&input, &input_to_forget_w, &input_to_cell_w, &input_to_output_w, &recurrent_to_forget_w, + &recurrent_to_cell_w, &recurrent_to_output_w, &forget_gate_bias, &cell_bias, &output_gate_bias, &output_state, &cell_state, + &scratch, &output, lstm_params, info, cell_threshold, projection_threshold); + + ARM_COMPUTE_EXPECT(input.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(input_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(input_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(input_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(recurrent_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(recurrent_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(recurrent_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(forget_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(cell_bias.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(output_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(output_state.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(cell_state.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(scratch.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(output.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + input.allocator()->allocate(); + input_to_forget_w.allocator()->allocate(); + input_to_cell_w.allocator()->allocate(); + input_to_output_w.allocator()->allocate(); + recurrent_to_forget_w.allocator()->allocate(); + recurrent_to_cell_w.allocator()->allocate(); + recurrent_to_output_w.allocator()->allocate(); + forget_gate_bias.allocator()->allocate(); + cell_bias.allocator()->allocate(); + output_gate_bias.allocator()->allocate(); + output_state.allocator()->allocate(); + cell_state.allocator()->allocate(); + scratch.allocator()->allocate(); + output.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!input.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!input_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!input_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!input_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!recurrent_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!recurrent_to_cell_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!recurrent_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!forget_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!cell_bias.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!output_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!output_state.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!cell_state.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!scratch.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!output.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill(AccessorType(input), 0); + fill(AccessorType(input_to_forget_w), 1); + fill(AccessorType(input_to_cell_w), 2); + fill(AccessorType(input_to_output_w), 3); + fill(AccessorType(recurrent_to_forget_w), 4); + fill(AccessorType(recurrent_to_cell_w), 5); + fill(AccessorType(recurrent_to_output_w), 6); + fill(AccessorType(forget_gate_bias), 7); + fill(AccessorType(cell_bias), 8); + fill(AccessorType(output_gate_bias), 9); + fill(AccessorType(output_state), 10); + fill(AccessorType(cell_state), 11); + fill(AccessorType(scratch), 12); + + if(!cifg_opt) + { + ARM_COMPUTE_EXPECT(input_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(recurrent_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(cell_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(input_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); + input_to_input_w.allocator()->allocate(); + recurrent_to_input_w.allocator()->allocate(); + cell_to_input_w.allocator()->allocate(); + input_gate_bias.allocator()->allocate(); + ARM_COMPUTE_EXPECT(!input_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!recurrent_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!cell_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!input_gate_bias.info()->is_resizable(), framework::LogLevel::ERRORS); + fill(AccessorType(input_to_input_w), 13); + fill(AccessorType(recurrent_to_input_w), 14); + fill(AccessorType(cell_to_input_w), 15); + fill(AccessorType(recurrent_to_input_w), 16); + fill(AccessorType(input_gate_bias), 17); + } + + if(peephole_opt) + { + if(cifg_opt) + { + ARM_COMPUTE_EXPECT(cell_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); + cell_to_input_w.allocator()->allocate(); + ARM_COMPUTE_EXPECT(!cell_to_input_w.info()->is_resizable(), framework::LogLevel::ERRORS); + fill(AccessorType(cell_to_input_w), 15); + } + ARM_COMPUTE_EXPECT(cell_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(cell_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); + cell_to_forget_w.allocator()->allocate(); + cell_to_output_w.allocator()->allocate(); + ARM_COMPUTE_EXPECT(!cell_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!cell_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); + fill(AccessorType(cell_to_output_w), 18); + } + + if(projection_opt) + { + ARM_COMPUTE_EXPECT(projection_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(projection_bias.info()->is_resizable(), framework::LogLevel::ERRORS); + + projection_w.allocator()->allocate(); + projection_bias.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!projection_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!projection_bias.info()->is_resizable(), framework::LogLevel::ERRORS); + + fill(AccessorType(projection_w), 19); + fill(AccessorType(projection_bias), 20); + } + + // Compute function + lstm.run(); + + return output; + } + + SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &input_weights_shape, const TensorShape &recurrent_weights_shape, const TensorShape &cell_bias_shape, + const TensorShape &output_cell_shape, const TensorShape &output_shape, const TensorShape &scratch_shape, ActivationLayerInfo info, float cell_threshold, + float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt) + { + // Create projection bias shape + TensorShape projection_bias_shape{}; + projection_bias_shape.set(0, output_shape.x()); + + TensorShape gemm_shape{ 1, output_shape.y() }; + SimpleTensor<T> gemm_out{ gemm_shape, data_type }; + + // Create reference + SimpleTensor<T> input{ input_shape, data_type }; + SimpleTensor<T> input_to_input_w{ input_weights_shape, data_type }; + SimpleTensor<T> input_to_forget_w{ input_weights_shape, data_type }; + SimpleTensor<T> input_to_cell_w{ input_weights_shape, data_type }; + SimpleTensor<T> input_to_output_w{ input_weights_shape, data_type }; + SimpleTensor<T> recurrent_to_input_w{ recurrent_weights_shape, data_type }; + SimpleTensor<T> recurrent_to_forget_w{ recurrent_weights_shape, data_type }; + SimpleTensor<T> recurrent_to_cell_w{ recurrent_weights_shape, data_type }; + SimpleTensor<T> recurrent_to_output_w{ recurrent_weights_shape, data_type }; + SimpleTensor<T> cell_to_input_w{ cell_bias_shape, data_type }; + SimpleTensor<T> cell_to_forget_w{ cell_bias_shape, data_type }; + SimpleTensor<T> cell_to_output_w{ cell_bias_shape, data_type }; + SimpleTensor<T> input_gate_bias{ cell_bias_shape, data_type }; + SimpleTensor<T> forget_gate_bias{ cell_bias_shape, data_type }; + SimpleTensor<T> cell_bias{ cell_bias_shape, data_type }; + SimpleTensor<T> output_gate_bias{ cell_bias_shape, data_type }; + SimpleTensor<T> projection_w{ recurrent_weights_shape, data_type }; + SimpleTensor<T> projection_bias{ projection_bias_shape, data_type }; + SimpleTensor<T> output_state{ output_shape, data_type }; + SimpleTensor<T> cell_state{ output_cell_shape, data_type }; + SimpleTensor<T> scratch{ scratch_shape, data_type }; + SimpleTensor<T> output{ output_shape, data_type }; + + // Fill reference + fill(input, 0); + fill(input_to_forget_w, 1); + fill(input_to_cell_w, 2); + fill(input_to_output_w, 3); + fill(recurrent_to_forget_w, 4); + fill(recurrent_to_cell_w, 5); + fill(recurrent_to_output_w, 6); + fill(forget_gate_bias, 7); + fill(cell_bias, 8); + fill(output_gate_bias, 9); + fill(output_state, 10); + fill(cell_state, 11); + fill(scratch, 12); + fill(input_to_input_w, 13); + fill(recurrent_to_input_w, 14); + fill(cell_to_input_w, 15); + fill(recurrent_to_input_w, 16); + fill(input_gate_bias, 17); + fill(cell_to_output_w, 18); + fill(projection_w, 19); + fill(projection_bias, 20); + + bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? true : false; + + // Compute forget_gate + SimpleTensor<T> fully_connected_forget = reference::fully_connected_layer(input, input_to_forget_w, forget_gate_bias, output_cell_shape); + SimpleTensor<T> transposed_weights = reference::transpose(recurrent_to_forget_w); + SimpleTensor<T> gemm = reference::gemm(output_state, transposed_weights, cell_state, 1.f, 0.f); + SimpleTensor<T> forget_gate = reference::arithmetic_addition(fully_connected_forget, gemm, data_type, ConvertPolicy::SATURATE); + + if(peephole_opt) + { + transposed_weights = reference::transpose(cell_to_forget_w); + gemm = reference::gemm(cell_state, transposed_weights, gemm_out, 1.f, 0.f); + forget_gate = reference::arithmetic_addition(forget_gate, gemm, data_type, ConvertPolicy::SATURATE); + } + + forget_gate = reference::activation_layer(forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + + // Compute input_gate + SimpleTensor<T> input_gate; + if(cifg_opt) + { + SimpleTensor<T> ones{ cell_bias_shape, data_type }; + fill_custom_val(ones, 1.f, 0); + input_gate = reference::arithmetic_subtraction<T, T, T>(ones, forget_gate, data_type, ConvertPolicy::SATURATE); + } + else + { + SimpleTensor<T> fully_connected_input = reference::fully_connected_layer(input, input_to_input_w, input_gate_bias, output_cell_shape); + transposed_weights = reference::transpose(recurrent_to_input_w); + gemm = reference::gemm(output_state, transposed_weights, cell_state, 1.f, 0.f); + input_gate = reference::arithmetic_addition(fully_connected_input, gemm, data_type, ConvertPolicy::SATURATE); + transposed_weights = reference::transpose(cell_to_input_w); + gemm = reference::gemm(cell_state, transposed_weights, gemm_out, 1.f, 0.f); + input_gate = reference::arithmetic_addition(input_gate, gemm, data_type, ConvertPolicy::SATURATE); + input_gate = reference::activation_layer(input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + } + + // Compute cell_state + SimpleTensor<T> fully_connected_cell_state = reference::fully_connected_layer(input, input_to_cell_w, cell_bias, output_cell_shape); + transposed_weights = reference::transpose(recurrent_to_cell_w); + gemm = reference::gemm(output_state, transposed_weights, cell_state, 1.f, 0.f); + SimpleTensor<T> pixelwise_mul = reference::pixel_wise_multiplication(cell_state, forget_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + cell_state = reference::arithmetic_addition(fully_connected_cell_state, gemm, data_type, ConvertPolicy::SATURATE); + cell_state = reference::activation_layer(cell_state, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + cell_state = reference::pixel_wise_multiplication(cell_state, input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + cell_state = reference::arithmetic_addition(cell_state, pixelwise_mul, data_type, ConvertPolicy::SATURATE); + if(cell_threshold != 0.f) + { + cell_state = reference::activation_layer(cell_state, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, cell_threshold)); + } + + // Compute output + SimpleTensor<T> fully_connected_output = reference::fully_connected_layer(input, input_to_output_w, output_gate_bias, output_cell_shape); + transposed_weights = reference::transpose(recurrent_to_output_w); + gemm = reference::gemm(output_state, transposed_weights, cell_state, 1.f, 0.f); + output = reference::arithmetic_addition(fully_connected_output, gemm, data_type, ConvertPolicy::SATURATE); + if(peephole_opt) + { + transposed_weights = reference::transpose(cell_to_output_w); + gemm = reference::gemm(cell_state, transposed_weights, gemm_out, 1.f, 0.f); + output = reference::arithmetic_addition(output, gemm, data_type, ConvertPolicy::SATURATE); + } + output = reference::activation_layer(output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + + // Compute output state + SimpleTensor<T> cell_state_activation = reference::activation_layer(cell_state, info); + output_state = reference::pixel_wise_multiplication(output, cell_state_activation, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + + if(projection_opt) + { + SimpleTensor<T> fully_connected_projection = reference::fully_connected_layer(output_state, projection_w, projection_bias, output_cell_shape); + if(projection_threshold != 0.f) + { + output_state = reference::activation_layer(fully_connected_projection, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold)); + } + } + return output_state; + } + + TensorType _target{}; + SimpleTensor<T> _reference{}; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_LSTM_LAYER_FIXTURE */ |