From 39438b427b293c6d2e7066c68d3c3d3cb6d98a15 Mon Sep 17 00:00:00 2001 From: Michele Di Giorgio Date: Tue, 4 Jun 2019 12:41:45 +0100 Subject: COMPMID-2342: Add layer normalization support in CLLSTMLayer Change-Id: I25d974aa94e69c5f79a0bd99d5869a351d6d954d Signed-off-by: Michele Di Giorgio Reviewed-on: https://review.mlplatform.org/c/1324 Reviewed-by: Manuel Bottini Comments-Addressed: Arm Jenkins Tested-by: Arm Jenkins Reviewed-by: Michalis Spyrou --- arm_compute/runtime/CL/functions/CLLSTMLayer.h | 126 +++++++++------- arm_compute/runtime/common/LSTMParams.h | 54 ++++++- src/runtime/CL/functions/CLLSTMLayer.cpp | 192 +++++++++++++++++++++---- tests/validation/CL/LSTMLayer.cpp | 15 +- tests/validation/NEON/LSTMLayer.cpp | 15 +- tests/validation/fixtures/LSTMLayerFixture.h | 131 +++++++++++++++-- 6 files changed, 426 insertions(+), 107 deletions(-) diff --git a/arm_compute/runtime/CL/functions/CLLSTMLayer.h b/arm_compute/runtime/CL/functions/CLLSTMLayer.h index 3add152878..44b3baf81b 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/CLElementwiseOperations.h" #include "arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h" #include "arm_compute/runtime/CL/functions/CLGEMM.h" +#include "arm_compute/runtime/CL/functions/CLMeanStdDevNormalizationLayer.h" #include "arm_compute/runtime/IMemoryManager.h" #include "arm_compute/runtime/common/LSTMParams.h" @@ -76,17 +77,22 @@ public: * @param[out] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size]. * Data types supported: Same as @p input. * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization: - * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. - * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. - * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input. - * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. - * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. - * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input - * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. - * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input. + * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. + * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input. + * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input + * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input. + * input_layer_norm_coefficients 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * forget_layer_norm_coefficients 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * cell_layer_norm_coefficients 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. + * output_layer_norm_coefficients 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo. - * @param[in] cell_threshold The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip]. If set to 0.0 then clipping is disabled. - * @param[in] projection_threshold The clipping threshold for the output from the projection layer, such that values are bound within [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled. + * @param[in] cell_threshold The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip]. If set to 0.0f 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.0f 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, @@ -98,35 +104,40 @@ public: /** Static function to check if given info will lead to a valid configuration of @ref CLLSTMLayer * - * @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] input Source tensor info. 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 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_in 2D weights tensor info with dimensions [output_size, batch_size]. Data type supported: Same as @p input. + * @param[in] cell_state_in 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_state_out 2D weights tensor info with dimensions [output_size, batch_size]. Data type supported: Same as @p input. + * @param[in] cell_state_out 2D tensor info with dimensions [num_units, batch_size]. 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 info used in peephole optimization: + * input_to_input_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: Same as @p input. + * recurrent_to_input_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * cell_to_input_weights 1D weights tensor info with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input. + * cell_to_forget_weights 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. + * cell_to_output_weights 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. + * input_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input + * projection_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: Same as @p input. + * projection_bias 1D weights tensor info with dimensions [output_size]. Data type supported: Same as @p input. + * input_layer_norm_coefficients 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. + * forget_layer_norm_coefficients 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. + * cell_layer_norm_coefficients 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. + * output_layer_norm_coefficients 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo. - * @param[in] cell_threshold The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip]. If set to 0.0 then clipping is disabled. - * @param[in] projection_threshold The clipping threshold for the output from the projection layer, such that values are bound within [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled. + * @param[in] cell_threshold The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip]. If set to 0.0f 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.0f then clipping is disabled. * * @return a status */ @@ -145,23 +156,16 @@ public: private: CLMemoryGroup _memory_group; CLFullyConnectedLayer _fully_connected_input_gate; - CLGEMM _gemm_input_gate; - CLTransposeKernel _transpose_input_gate; - CLSaturatedArithmeticOperationKernel _accum_input_gate1; - CLArithmeticAddition _accum_input_gate2; + CLArithmeticAddition _accum_input_gate1; CLSaturatedArithmeticOperationKernel _subtract_input_gate; CLPixelWiseMultiplicationKernel _pixelwise_mul_input_gate; CLActivationLayerKernel _activation_input_gate; CLFullyConnectedLayer _fully_connected_forget_gate; - CLGEMM _gemm_forget_gate; - CLTransposeKernel _transpose_forget_gate; - CLSaturatedArithmeticOperationKernel _accum_forget_gate1; - CLArithmeticAddition _accum_forget_gate2; + CLArithmeticAddition _accum_forget_gate1; CLPixelWiseMultiplicationKernel _pixelwise_mul_forget_gate; CLActivationLayerKernel _activation_forget_gate; CLFullyConnectedLayer _fully_connected_cell_state; CLGEMM _gemm_cell_state1; - CLGEMM _gemm_cell_state2; CLTransposeKernel _transpose_cell_state; CLSaturatedArithmeticOperationKernel _accum_cell_state1; CLSaturatedArithmeticOperationKernel _accum_cell_state2; @@ -170,17 +174,12 @@ private: CLActivationLayerKernel _cell_clip; CLPixelWiseMultiplicationKernel _pixelwise_mul_cell_state2; CLFullyConnectedLayer _fully_connected_output; - CLGEMM _gemm_output; CLPixelWiseMultiplicationKernel _pixelwise_mul_output_state1; - CLTransposeKernel _transpose_output; - CLSaturatedArithmeticOperationKernel _accum_output1; - CLArithmeticAddition _accum_output2; + CLArithmeticAddition _accum_output1; CLActivationLayerKernel _activation_output; CLActivationLayerKernel _activation_output_state; CLPixelWiseMultiplicationKernel _pixelwise_mul_output_state2; CLFullyConnectedLayer _fully_connected_output_state; - CLGEMM _gemm_output_state; - CLSaturatedArithmeticOperationKernel _accum_output_state; CLActivationLayerKernel _projection_clip; CLCopyKernel _copy_cell_state; CLCopyKernel _copy_output; @@ -190,6 +189,18 @@ private: CLWidthConcatenate2TensorsKernel _concat_weights_input_gate; CLWidthConcatenate2TensorsKernel _concat_weights_output; CLMemsetKernel _ones_memset_kernel; + CLMeanStdDevNormalizationLayer _mean_std_norm_input_gate; + CLPixelWiseMultiplicationKernel _pixelwise_mul_input_gate_coeff; + CLSaturatedArithmeticOperationKernel _accum_input_gate_bias; + CLMeanStdDevNormalizationLayer _mean_std_norm_forget_gate; + CLPixelWiseMultiplicationKernel _pixelwise_mul_forget_gate_coeff; + CLSaturatedArithmeticOperationKernel _accum_forget_gate_bias; + CLMeanStdDevNormalizationLayer _mean_std_norm_cell_gate; + CLPixelWiseMultiplicationKernel _pixelwise_mul_cell_gate_coeff; + CLSaturatedArithmeticOperationKernel _accum_cell_gate_bias; + CLMeanStdDevNormalizationLayer _mean_std_norm_output_gate; + CLPixelWiseMultiplicationKernel _pixelwise_mul_output_gate_coeff; + CLSaturatedArithmeticOperationKernel _accum_output_gate_bias; CLTensor _input_gate_out1; CLTensor _input_gate_out2; CLTensor _input_gate_out3; @@ -212,12 +223,21 @@ private: CLTensor _cell_state_activation; CLTensor _output_state1; CLTensor _ones; + CLTensor _input_layer_norm_out1; + CLTensor _input_layer_norm_out2; + CLTensor _forget_layer_norm_out1; + CLTensor _forget_layer_norm_out2; + CLTensor _cell_layer_norm_out1; + CLTensor _cell_layer_norm_out2; + CLTensor _output_layer_norm_out1; + CLTensor _output_layer_norm_out2; bool _run_peephole_opt; bool _run_cifg_opt; bool _perform_cell_clipping; bool _has_projection_weights; bool _perform_projection_clipping; bool _is_prepared; + bool _is_layer_norm_lstm; }; } // namespace arm_compute #endif /* __ARM_COMPUTE_CLLSTMLAYER_H__ */ diff --git a/arm_compute/runtime/common/LSTMParams.h b/arm_compute/runtime/common/LSTMParams.h index 5b33e2e937..6979f90721 100644 --- a/arm_compute/runtime/common/LSTMParams.h +++ b/arm_compute/runtime/common/LSTMParams.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -41,7 +41,8 @@ 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) + _cell_to_output_weights(nullptr), _projection_weights(nullptr), _projection_bias(nullptr), _input_layer_norm_weights(nullptr), _forget_layer_norm_weights(nullptr), _cell_layer_norm_weights(nullptr), + _output_layer_norm_weights(nullptr), _has_peephole_opt(false), _has_projection(false), _has_cifg_opt(true), _use_layer_norm(false) { } /** Prevent instances of this class from being copied (As this class contains pointers) */ @@ -96,6 +97,25 @@ public: _has_peephole_opt = true; return *this; } + /** Set layer normalization tensor parameters. + * + * @param[in] input_layer_norm_weights 1D weights tensor with dimensions [num_units]. Data type supported: Data types supported: F16/F32. + * @param[in] forget_layer_norm_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input_layer_norm_weights. + * @param[in] cell_layer_norm_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input_layer_norm_weights. + * @param[in] output_layer_norm_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input_layer_norm_weights. + * + * @return Reference to this LSTMParams object + */ + LSTMParams &set_layer_normalization_params(const T *input_layer_norm_weights, const T *forget_layer_norm_weights, + const T *cell_layer_norm_weights, const T *output_layer_norm_weights) + { + _input_layer_norm_weights = input_layer_norm_weights; + _forget_layer_norm_weights = forget_layer_norm_weights; + _cell_layer_norm_weights = cell_layer_norm_weights; + _output_layer_norm_weights = output_layer_norm_weights; + _use_layer_norm = true; + return *this; + } const T *input_to_input_weights() const { @@ -137,6 +157,26 @@ public: return _projection_bias; } + const T *input_layer_norm_weights() const + { + return _input_layer_norm_weights; + } + + const T *forget_layer_norm_weights() const + { + return _forget_layer_norm_weights; + } + + const T *cell_layer_norm_weights() const + { + return _cell_layer_norm_weights; + } + + const T *output_layer_norm_weights() const + { + return _output_layer_norm_weights; + } + bool has_peephole_opt() const { return _has_peephole_opt; @@ -152,6 +192,11 @@ public: return _has_cifg_opt; } + bool use_layer_norm() const + { + return _use_layer_norm; + } + private: const T *_input_to_input_weights; const T *_recurrent_to_input_weights; @@ -161,9 +206,14 @@ private: const T *_cell_to_output_weights; const T *_projection_weights; const T *_projection_bias; + const T *_input_layer_norm_weights; + const T *_forget_layer_norm_weights; + const T *_cell_layer_norm_weights; + const T *_output_layer_norm_weights; bool _has_peephole_opt; bool _has_projection; bool _has_cifg_opt; + bool _use_layer_norm; }; } #endif /*__ARM_COMPUTE_LSTMPARAMS_H__ */ diff --git a/src/runtime/CL/functions/CLLSTMLayer.cpp b/src/runtime/CL/functions/CLLSTMLayer.cpp index 85a81a8cd4..793d5ca1a9 100644 --- a/src/runtime/CL/functions/CLLSTMLayer.cpp +++ b/src/runtime/CL/functions/CLLSTMLayer.cpp @@ -38,16 +38,18 @@ using namespace arm_compute; using namespace arm_compute::misc::shape_calculator; CLLSTMLayer::CLLSTMLayer(std::shared_ptr 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(), _concat_inputs_forget_gate(), _concat_weights_forget_gate(), _concat_weights_input_gate(), _concat_weights_output(), - _ones_memset_kernel(), _input_gate_out1(), _input_gate_out2(), _input_gate_out3(), _input_gate_out4(), _forget_gate_out1(), _forget_gate_out2(), _forget_gate_out3(), _forget_gate_out4(), + : _memory_group(std::move(memory_manager)), _fully_connected_input_gate(), _accum_input_gate1(), _subtract_input_gate(), _pixelwise_mul_input_gate(), _activation_input_gate(), + _fully_connected_forget_gate(), _accum_forget_gate1(), _pixelwise_mul_forget_gate(), _activation_forget_gate(), _fully_connected_cell_state(), _gemm_cell_state1(), _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(), + _pixelwise_mul_output_state1(), _accum_output1(), _activation_output(), _activation_output_state(), _pixelwise_mul_output_state2(), _fully_connected_output_state(), _projection_clip(), + _copy_cell_state(), _copy_output(), _concat_scratch_buffer(), _concat_inputs_forget_gate(), _concat_weights_forget_gate(), _concat_weights_input_gate(), _concat_weights_output(), + _ones_memset_kernel(), _mean_std_norm_input_gate(), _pixelwise_mul_input_gate_coeff(), _accum_input_gate_bias(), _mean_std_norm_forget_gate(), _pixelwise_mul_forget_gate_coeff(), + _accum_forget_gate_bias(), _mean_std_norm_cell_gate(), _pixelwise_mul_cell_gate_coeff(), _accum_cell_gate_bias(), _mean_std_norm_output_gate(), _pixelwise_mul_output_gate_coeff(), + _accum_output_gate_bias(), _input_gate_out1(), _input_gate_out2(), _input_gate_out3(), _input_gate_out4(), _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(), - _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), - _is_prepared(false) + _cell_state_activation(), _output_state1(), _ones(), _input_layer_norm_out1(), _input_layer_norm_out2(), _forget_layer_norm_out1(), _forget_layer_norm_out2(), _cell_layer_norm_out1(), + _cell_layer_norm_out2(), _output_layer_norm_out1(), _output_layer_norm_out2(), _run_peephole_opt(false), _run_cifg_opt(false), _perform_cell_clipping(false), _has_projection_weights(false), + _perform_projection_clipping(false), _is_prepared(false), _is_layer_norm_lstm(false) { } @@ -66,6 +68,8 @@ void CLLSTMLayer::configure(const ICLTensor *input, output_state_in, cell_state_in, scratch_buffer, output_state_out, cell_state_out, output); + _is_layer_norm_lstm = lstm_params.use_layer_norm(); + // Set lstm parameters LSTMParams lstm_params_info; if(lstm_params.has_peephole_opt()) @@ -121,7 +125,7 @@ void CLLSTMLayer::configure(const ICLTensor *input, _concat_weights_forget_gate.configure(input_to_forget_weights, recurrent_to_forget_weights, &_forget_gate_out6); _memory_group.manage(&_forget_gate_out5); - _fully_connected_forget_gate.configure(&_forget_gate_out2, &_forget_gate_out6, forget_gate_bias, &_forget_gate_out5); + _fully_connected_forget_gate.configure(&_forget_gate_out2, &_forget_gate_out6, (_is_layer_norm_lstm) ? nullptr : forget_gate_bias, &_forget_gate_out5); _memory_group.manage(&_forget_gate_out1); _memory_group.manage(&_forget_gate_out3); _forget_gate_out6.allocator()->allocate(); @@ -134,7 +138,7 @@ void CLLSTMLayer::configure(const ICLTensor *input, _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_NEAREST_EVEN); - _accum_forget_gate2.configure(&_forget_gate_out5, &_forget_gate_out4, &_forget_gate_out3, ConvertPolicy::SATURATE); + _accum_forget_gate1.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; @@ -143,6 +147,20 @@ void CLLSTMLayer::configure(const ICLTensor *input, { _forget_gate_out3.allocator()->allocate(); } + if(_is_layer_norm_lstm) + { + _forget_layer_norm_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _forget_layer_norm_out2.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _memory_group.manage(&_forget_layer_norm_out1); + _memory_group.manage(&_forget_layer_norm_out2); + _mean_std_norm_forget_gate.configure(forget_gate_out); + _pixelwise_mul_forget_gate_coeff.configure(forget_gate_out, lstm_params.forget_layer_norm_weights(), &_forget_layer_norm_out1, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + // forget_gate_out is going to be reassigned, so allocate the tensor that it was assigned to before + forget_gate_out->allocator()->allocate(); + _accum_forget_gate_bias.configure(ArithmeticOperation::ADD, &_forget_layer_norm_out1, forget_gate_bias, &_forget_layer_norm_out2, ConvertPolicy::SATURATE); + _forget_layer_norm_out1.allocator()->allocate(); + forget_gate_out = &_forget_layer_norm_out2; + } _activation_forget_gate.configure(forget_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); // Configure block that calculates the input gate @@ -177,7 +195,7 @@ void CLLSTMLayer::configure(const ICLTensor *input, _memory_group.manage(&_input_gate_out1); _memory_group.manage(&_input_gate_out3); - _fully_connected_input_gate.configure(&_forget_gate_out2, &_input_gate_out2, lstm_params.input_gate_bias(), &_input_gate_out3); + _fully_connected_input_gate.configure(&_forget_gate_out2, &_input_gate_out2, (_is_layer_norm_lstm) ? nullptr : lstm_params.input_gate_bias(), &_input_gate_out3); _input_gate_out2.allocator()->allocate(); input_gate_out = &_input_gate_out3; @@ -185,7 +203,7 @@ void CLLSTMLayer::configure(const ICLTensor *input, { _memory_group.manage(&_input_gate_out4); _pixelwise_mul_input_gate.configure(cell_state_in, lstm_params.cell_to_input_weights(), &_input_gate_out4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); - _accum_input_gate2.configure(&_input_gate_out3, &_input_gate_out4, &_input_gate_out1, ConvertPolicy::SATURATE); + _accum_input_gate1.configure(&_input_gate_out3, &_input_gate_out4, &_input_gate_out1, ConvertPolicy::SATURATE); _input_gate_out3.allocator()->allocate(); _input_gate_out4.allocator()->allocate(); input_gate_out = &_input_gate_out1; @@ -194,6 +212,21 @@ void CLLSTMLayer::configure(const ICLTensor *input, { _input_gate_out1.allocator()->allocate(); } + + if(_is_layer_norm_lstm) + { + _input_layer_norm_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _input_layer_norm_out2.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _memory_group.manage(&_input_layer_norm_out1); + _memory_group.manage(&_input_layer_norm_out2); + _mean_std_norm_input_gate.configure(input_gate_out); + _pixelwise_mul_input_gate_coeff.configure(input_gate_out, lstm_params.input_layer_norm_weights(), &_input_layer_norm_out1, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + // input_gate_out is going to be reassigned, so allocate the tensor that it was assigned to before + input_gate_out->allocator()->allocate(); + _accum_input_gate_bias.configure(ArithmeticOperation::ADD, &_input_layer_norm_out1, lstm_params.input_gate_bias(), &_input_layer_norm_out2, ConvertPolicy::SATURATE); + _input_layer_norm_out1.allocator()->allocate(); + input_gate_out = &_input_layer_norm_out2; + } _activation_input_gate.configure(input_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); } @@ -207,7 +240,7 @@ void CLLSTMLayer::configure(const ICLTensor *input, _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); + _fully_connected_cell_state.configure(input, input_to_cell_weights, (_is_layer_norm_lstm) ? nullptr : 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); @@ -215,10 +248,25 @@ void CLLSTMLayer::configure(const ICLTensor *input, _cell_state_out2.allocator()->allocate(); _memory_group.manage(&_cell_state_out4); _accum_cell_state1.configure(ArithmeticOperation::ADD, &_cell_state_out1, &_cell_state_out3, &_cell_state_out4, ConvertPolicy::SATURATE); - _activation_cell_state.configure(&_cell_state_out4, nullptr, activation_info); + CLTensor *cell_state_out_ptr = &_cell_state_out4; + if(_is_layer_norm_lstm) + { + _cell_layer_norm_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _cell_layer_norm_out2.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _memory_group.manage(&_cell_layer_norm_out1); + _memory_group.manage(&_cell_layer_norm_out2); + _mean_std_norm_cell_gate.configure(cell_state_out_ptr); + _pixelwise_mul_cell_gate_coeff.configure(cell_state_out_ptr, lstm_params.cell_layer_norm_weights(), &_cell_layer_norm_out1, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + // cell_state_out_ptr is going to be reassigned, so allocate the tensor that it was assigned to before + cell_state_out_ptr->allocator()->allocate(); + _accum_cell_gate_bias.configure(ArithmeticOperation::ADD, &_cell_layer_norm_out1, cell_bias, &_cell_layer_norm_out2, ConvertPolicy::SATURATE); + _cell_layer_norm_out1.allocator()->allocate(); + cell_state_out_ptr = &_cell_layer_norm_out2; + } + _activation_cell_state.configure(cell_state_out_ptr, nullptr, activation_info); _memory_group.manage(&_cell_state_out5); - _pixelwise_mul_cell_state1.configure(&_cell_state_out4, input_gate_out, &_cell_state_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); - _cell_state_out4.allocator()->allocate(); + _pixelwise_mul_cell_state1.configure(cell_state_out_ptr, input_gate_out, &_cell_state_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + cell_state_out_ptr->allocator()->allocate(); _pixelwise_mul_cell_state2.configure(forget_gate_out, cell_state_in, &_cell_state_out3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); _accum_cell_state2.configure(ArithmeticOperation::ADD, &_cell_state_out5, &_cell_state_out3, &_cell_state_out1, ConvertPolicy::SATURATE); _cell_state_out3.allocator()->allocate(); @@ -247,7 +295,7 @@ void CLLSTMLayer::configure(const ICLTensor *input, _memory_group.manage(&_output1); _memory_group.manage(&_output4); - _fully_connected_output.configure(&_forget_gate_out2, &_output2, output_gate_bias, &_output4); + _fully_connected_output.configure(&_forget_gate_out2, &_output2, (_is_layer_norm_lstm) ? nullptr : output_gate_bias, &_output4); _output2.allocator()->allocate(); _forget_gate_out2.allocator()->allocate(); @@ -259,7 +307,7 @@ void CLLSTMLayer::configure(const ICLTensor *input, _memory_group.manage(&_output3); _pixelwise_mul_output_state1.configure(&_cell_state_out1, lstm_params.cell_to_output_weights(), &_output3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); - _accum_output2.configure(&_output4, &_output3, &_output1, ConvertPolicy::SATURATE); + _accum_output1.configure(&_output4, &_output3, &_output1, ConvertPolicy::SATURATE); _output4.allocator()->allocate(); output_gate_out = &_output1; @@ -270,6 +318,20 @@ void CLLSTMLayer::configure(const ICLTensor *input, { _output1.allocator()->allocate(); } + if(_is_layer_norm_lstm) + { + _output_layer_norm_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _output_layer_norm_out2.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + _memory_group.manage(&_output_layer_norm_out1); + _memory_group.manage(&_output_layer_norm_out2); + _mean_std_norm_output_gate.configure(output_gate_out); + _pixelwise_mul_output_gate_coeff.configure(output_gate_out, lstm_params.output_layer_norm_weights(), &_output_layer_norm_out1, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + // output_gate_out is going to be reassigned, so allocate the tensor that it was assigned to before + output_gate_out->allocator()->allocate(); + _accum_output_gate_bias.configure(ArithmeticOperation::ADD, &_output_layer_norm_out1, output_gate_bias, &_output_layer_norm_out2, ConvertPolicy::SATURATE); + _output_layer_norm_out1.allocator()->allocate(); + output_gate_out = &_output_layer_norm_out2; + } _activation_output.configure(output_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); // Configure block that calculates the output state @@ -370,6 +432,31 @@ Status CLLSTMLayer::validate(const ITensorInfo *input, const unsigned int num_batches = input->dimension(1); const unsigned int num_cells = input_to_output_weights->dimension(1); + if(lstm_params.use_layer_norm()) + { + // If CIFG is used, input layer normalization weights tensor is omitted + if(lstm_params.has_cifg_opt()) + { + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_layer_norm_weights() != nullptr); + } + else + { + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_layer_norm_weights()); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_layer_norm_weights()->num_dimensions() > 1); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_layer_norm_weights()->dimension(0) != num_batches); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, lstm_params.input_layer_norm_weights()); + } + + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.forget_layer_norm_weights(), lstm_params.cell_layer_norm_weights(), lstm_params.output_layer_norm_weights()); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, lstm_params.forget_layer_norm_weights(), lstm_params.cell_layer_norm_weights(), lstm_params.output_layer_norm_weights()); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.forget_layer_norm_weights()->num_dimensions() > 1); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_layer_norm_weights()->num_dimensions() > 1); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.output_layer_norm_weights()->num_dimensions() > 1); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.forget_layer_norm_weights()->dimension(0) != num_batches); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_layer_norm_weights()->dimension(0) != num_batches); + ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.output_layer_norm_weights()->dimension(0) != num_batches); + } + // Check peephole optimization if(lstm_params.has_peephole_opt()) { @@ -389,7 +476,7 @@ Status CLLSTMLayer::validate(const ITensorInfo *input, TensorInfo cell_state_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type()); // Validate forget gate - ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_forget_weights, forget_gate_bias, &forget_gate)); + ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_forget_weights, (lstm_params.use_layer_norm()) ? nullptr : forget_gate_bias, &forget_gate)); std::vector inputs_vector; inputs_vector.emplace_back(input); @@ -404,6 +491,13 @@ Status CLLSTMLayer::validate(const ITensorInfo *input, ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_forget_weights(), &forget_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN)); ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE)); } + if(lstm_params.use_layer_norm()) + { + ARM_COMPUTE_RETURN_ON_ERROR(CLMeanStdDevNormalizationLayer::validate(&forget_gate)); + ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&forget_gate, lstm_params.forget_layer_norm_weights(), &forget_gate, 1, ConvertPolicy::SATURATE, + RoundingPolicy::TO_NEAREST_EVEN)); + ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&forget_gate, forget_gate_bias, &forget_gate, ConvertPolicy::SATURATE)); + } ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&forget_gate, &forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); // Validate input gate @@ -423,7 +517,7 @@ Status CLLSTMLayer::validate(const ITensorInfo *input, TensorInfo lstm_gate_concat = TensorInfo(lstm_weights_concat_shape, 1, input->data_type()); ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenate2TensorsKernel::validate(lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights(), &lstm_gate_concat)); - ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, lstm_params.input_to_input_weights(), lstm_params.input_gate_bias(), &input_gate)); + ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, lstm_params.input_to_input_weights(), (lstm_params.use_layer_norm()) ? nullptr : lstm_params.input_gate_bias(), &input_gate)); if(lstm_params.has_peephole_opt()) { @@ -432,6 +526,13 @@ Status CLLSTMLayer::validate(const ITensorInfo *input, ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_input_weights(), &input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN)); ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&input_gate, &input_gate, &input_gate, ConvertPolicy::SATURATE)); } + + if(lstm_params.use_layer_norm()) + { + ARM_COMPUTE_RETURN_ON_ERROR(CLMeanStdDevNormalizationLayer::validate(&input_gate)); + ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&input_gate, lstm_params.input_layer_norm_weights(), &input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN)); + ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&input_gate, lstm_params.input_gate_bias(), &input_gate, ConvertPolicy::SATURATE)); + } ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&input_gate, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); } else @@ -440,9 +541,16 @@ Status CLLSTMLayer::validate(const ITensorInfo *input, } // Validate cell state - ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_cell_weights, cell_bias, &cell_state_tmp)); + ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_cell_weights, (lstm_params.use_layer_norm()) ? nullptr : cell_bias, &cell_state_tmp)); ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(output_state_in, &units_out_transposed_info, nullptr, &cell_state_tmp, 1.f, 0.f, GEMMInfo())); ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE)); + if(lstm_params.use_layer_norm()) + { + ARM_COMPUTE_RETURN_ON_ERROR(CLMeanStdDevNormalizationLayer::validate(&cell_state_tmp)); + ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&cell_state_tmp, lstm_params.cell_layer_norm_weights(), &cell_state_tmp, 1, ConvertPolicy::SATURATE, + RoundingPolicy::TO_NEAREST_EVEN)); + ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&cell_state_tmp, cell_bias, &cell_state_tmp, ConvertPolicy::SATURATE)); + } ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&cell_state_tmp, nullptr, activation_info)); ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &input_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN)); ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &forget_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN)); @@ -460,7 +568,7 @@ Status CLLSTMLayer::validate(const ITensorInfo *input, TensorInfo in_out_gate_concat = TensorInfo(in_out_weights_concat_shape, 1, input->data_type()); ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenate2TensorsKernel::validate(input_to_output_weights, recurrent_to_output_weights, &in_out_gate_concat)); // Validate output gate tmp - ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_output_weights, output_gate_bias, &output_gate_tmp)); + ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_output_weights, (lstm_params.use_layer_norm()) ? nullptr : output_gate_bias, &output_gate_tmp)); if(lstm_params.has_peephole_opt()) { @@ -468,6 +576,13 @@ Status CLLSTMLayer::validate(const ITensorInfo *input, RoundingPolicy::TO_NEAREST_EVEN)); ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&output_gate_tmp, &output_gate_tmp, &output_gate_tmp, ConvertPolicy::SATURATE)); } + if(lstm_params.use_layer_norm()) + { + ARM_COMPUTE_RETURN_ON_ERROR(CLMeanStdDevNormalizationLayer::validate(&output_gate_tmp)); + ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&output_gate_tmp, lstm_params.output_layer_norm_weights(), &output_gate_tmp, 1, ConvertPolicy::SATURATE, + RoundingPolicy::TO_NEAREST_EVEN)); + ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&output_gate_tmp, output_gate_bias, &output_gate_tmp, ConvertPolicy::SATURATE)); + } ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&output_gate_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); // Validate output state @@ -514,7 +629,13 @@ void CLLSTMLayer::run() if(_run_peephole_opt) { CLScheduler::get().enqueue(_pixelwise_mul_forget_gate); - _accum_forget_gate2.run(); + _accum_forget_gate1.run(); + } + if(_is_layer_norm_lstm) + { + _mean_std_norm_forget_gate.run(); + CLScheduler::get().enqueue(_pixelwise_mul_forget_gate_coeff); + CLScheduler::get().enqueue(_accum_forget_gate_bias); } CLScheduler::get().enqueue(_activation_forget_gate); @@ -530,7 +651,14 @@ void CLLSTMLayer::run() if(_run_peephole_opt) { CLScheduler::get().enqueue(_pixelwise_mul_input_gate); - _accum_input_gate2.run(); + _accum_input_gate1.run(); + } + + if(_is_layer_norm_lstm) + { + _mean_std_norm_input_gate.run(); + CLScheduler::get().enqueue(_pixelwise_mul_input_gate_coeff); + CLScheduler::get().enqueue(_accum_input_gate_bias); } CLScheduler::get().enqueue(_activation_input_gate); } @@ -539,6 +667,12 @@ void CLLSTMLayer::run() CLScheduler::get().enqueue(_transpose_cell_state); _gemm_cell_state1.run(); CLScheduler::get().enqueue(_accum_cell_state1); + if(_is_layer_norm_lstm) + { + _mean_std_norm_cell_gate.run(); + CLScheduler::get().enqueue(_pixelwise_mul_cell_gate_coeff); + CLScheduler::get().enqueue(_accum_cell_gate_bias); + } CLScheduler::get().enqueue(_activation_cell_state); CLScheduler::get().enqueue(_pixelwise_mul_cell_state1); CLScheduler::get().enqueue(_pixelwise_mul_cell_state2); @@ -554,7 +688,13 @@ void CLLSTMLayer::run() if(_run_peephole_opt) { CLScheduler::get().enqueue(_pixelwise_mul_output_state1); - _accum_output2.run(); + _accum_output1.run(); + } + if(_is_layer_norm_lstm) + { + _mean_std_norm_output_gate.run(); + CLScheduler::get().enqueue(_pixelwise_mul_output_gate_coeff); + CLScheduler::get().enqueue(_accum_output_gate_bias); } CLScheduler::get().enqueue(_activation_output); diff --git a/tests/validation/CL/LSTMLayer.cpp b/tests/validation/CL/LSTMLayer.cpp index 71a9383d93..69ac61dcf4 100644 --- a/tests/validation/CL/LSTMLayer.cpp +++ b/tests/validation/CL/LSTMLayer.cpp @@ -153,10 +153,11 @@ template using CLLSTMLayerFixture = LSTMLayerValidationFixture, T>; TEST_SUITE(FP32) -FIXTURE_DATA_TEST_CASE(RunSmall, CLLSTMLayerFixture, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallLSTMLayerDataset(), framework::dataset::make("DataType", +FIXTURE_DATA_TEST_CASE(RunSmall, CLLSTMLayerFixture, framework::DatasetMode::ALL, combine(combine(combine(combine(datasets::SmallLSTMLayerDataset(), framework::dataset::make("DataType", DataType::F32)), - framework::dataset::make("ProjectionOpt", { true, false })), - framework::dataset::make("PeepholeOpt", { true, false }))) + framework::dataset::make("ProjectionOpt", { true, false })), + framework::dataset::make("PeepholeOpt", { true, false })), + framework::dataset::make("UseLayerNorm", { true, false }))) { // Validate output validate(CLAccessor(_target), _reference, tolerance_f32); @@ -165,9 +166,11 @@ FIXTURE_DATA_TEST_CASE(RunSmall, CLLSTMLayerFixture, framework::DatasetMo TEST_SUITE_END() // FP32 TEST_SUITE(FP16) -FIXTURE_DATA_TEST_CASE(RunSmall, CLLSTMLayerFixture, 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 }))) +FIXTURE_DATA_TEST_CASE(RunSmall, CLLSTMLayerFixture, framework::DatasetMode::ALL, combine(combine(combine(combine(datasets::SmallLSTMLayerDataset(), framework::dataset::make("DataType", + DataType::F16)), + framework::dataset::make("ProjectionOpt", { true, false })), + framework::dataset::make("PeepholeOpt", { true, false })), + framework::dataset::make("UseLayerNorm", { true, false }))) { // Validate output validate(CLAccessor(_target), _reference, tolerance_f16); diff --git a/tests/validation/NEON/LSTMLayer.cpp b/tests/validation/NEON/LSTMLayer.cpp index b27dfae8fa..c503972ba9 100644 --- a/tests/validation/NEON/LSTMLayer.cpp +++ b/tests/validation/NEON/LSTMLayer.cpp @@ -153,10 +153,11 @@ template using NELSTMLayerFixture = LSTMLayerValidationFixture, T>; TEST_SUITE(FP32) -FIXTURE_DATA_TEST_CASE(RunSmall, NELSTMLayerFixture, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallLSTMLayerDataset(), framework::dataset::make("DataType", +FIXTURE_DATA_TEST_CASE(RunSmall, NELSTMLayerFixture, framework::DatasetMode::ALL, combine(combine(combine(combine(datasets::SmallLSTMLayerDataset(), framework::dataset::make("DataType", DataType::F32)), - framework::dataset::make("ProjectionOpt", { true, false })), - framework::dataset::make("PeepholeOpt", { true, false }))) + framework::dataset::make("ProjectionOpt", { true, false })), + framework::dataset::make("PeepholeOpt", { true, false })), + framework::dataset::make("UseLayerNorm", { false }))) { // Validate output validate(Accessor(_target), _reference, tolerance_f32); @@ -166,9 +167,11 @@ TEST_SUITE_END() // FP32 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC TEST_SUITE(FP16) -FIXTURE_DATA_TEST_CASE(RunSmall, NELSTMLayerFixture, 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 }))) +FIXTURE_DATA_TEST_CASE(RunSmall, NELSTMLayerFixture, framework::DatasetMode::ALL, combine(combine(combine(combine(datasets::SmallLSTMLayerDataset(), framework::dataset::make("DataType", + DataType::F16)), + framework::dataset::make("ProjectionOpt", { true, false })), + framework::dataset::make("PeepholeOpt", { true, false })), + framework::dataset::make("UseLayerNorm", { false }))) { // Validate output validate(Accessor(_target), _reference, tolerance_f16); diff --git a/tests/validation/fixtures/LSTMLayerFixture.h b/tests/validation/fixtures/LSTMLayerFixture.h index 2cf83b8b3d..9260686d56 100644 --- a/tests/validation/fixtures/LSTMLayerFixture.h +++ b/tests/validation/fixtures/LSTMLayerFixture.h @@ -32,6 +32,7 @@ #include "tests/validation/reference/ConcatenateLayer.h" #include "tests/validation/reference/FullyConnectedLayer.h" #include "tests/validation/reference/GEMM.h" +#include "tests/validation/reference/MeanStdDevNormalizationLayer.h" #include "tests/validation/reference/PixelWiseMultiplication.h" #include "tests/validation/reference/Transpose.h" @@ -47,12 +48,13 @@ class LSTMLayerValidationFixture : public framework::Fixture public: template 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) + TensorShape scratch_shape, ActivationLayerInfo info, float cell_threshold, float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt, + bool use_layer_norm) { _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); + data_type, projection_opt, peephole_opt, use_layer_norm); _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); + data_type, projection_opt, peephole_opt, use_layer_norm); } protected: @@ -70,7 +72,7 @@ protected: } 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) + float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt, bool use_layer_norm) { const unsigned int num_cells = input_weights_shape.y(); const unsigned int num_outputs = recurrent_weights_shape.x(); @@ -100,6 +102,10 @@ protected: TensorType cell_to_output_w; TensorType projection_w; TensorType projection_bias; + TensorType input_layer_norm_w; + TensorType forget_layer_norm_w; + TensorType cell_layer_norm_w; + TensorType output_layer_norm_w; bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? false : true; @@ -131,6 +137,22 @@ protected: lstm_params.set_projection_params(&projection_w, &projection_bias); } + if(use_layer_norm) + { + forget_layer_norm_w = create_tensor(TensorShape(num_cells), data_type); + cell_layer_norm_w = create_tensor(TensorShape(num_cells), data_type); + output_layer_norm_w = create_tensor(TensorShape(num_cells), data_type); + if(!cifg_opt) + { + input_layer_norm_w = create_tensor(TensorShape(num_cells), data_type); + lstm_params.set_layer_normalization_params(&input_layer_norm_w, &forget_layer_norm_w, &cell_layer_norm_w, &output_layer_norm_w); + } + else + { + lstm_params.set_layer_normalization_params(nullptr, &forget_layer_norm_w, &cell_layer_norm_w, &output_layer_norm_w); + } + } + // 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, @@ -257,6 +279,35 @@ protected: fill(AccessorType(projection_bias), 21); } + if(use_layer_norm) + { + if(!cifg_opt) + { + ARM_COMPUTE_EXPECT(input_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); + + input_layer_norm_w.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!input_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); + + fill(AccessorType(input_layer_norm_w), 22); + } + ARM_COMPUTE_EXPECT(forget_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(cell_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(output_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); + + forget_layer_norm_w.allocator()->allocate(); + cell_layer_norm_w.allocator()->allocate(); + output_layer_norm_w.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!forget_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!cell_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!output_layer_norm_w.info()->is_resizable(), framework::LogLevel::ERRORS); + + fill(AccessorType(forget_layer_norm_w), 23); + fill(AccessorType(cell_layer_norm_w), 24); + fill(AccessorType(output_layer_norm_w), 25); + } + // Compute function lstm.run(); @@ -266,7 +317,7 @@ protected: SimpleTensor 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) + float projection_threshold, DataType data_type, bool projection_opt, bool peephole_opt, bool use_layer_norm) { const unsigned int num_cells = input_weights_shape.y(); const unsigned int num_outputs = recurrent_weights_shape.x(); @@ -306,6 +357,8 @@ protected: SimpleTensor cell_state_out{ output_cell_shape, data_type }; SimpleTensor output{ output_shape, data_type }; + bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? false : true; + // Fill reference fill(input, 0); fill(input_to_forget_w, 1); @@ -314,9 +367,18 @@ protected: 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); + if(use_layer_norm) + { + fill_custom_val(forget_gate_bias, 0.f, 7); + fill_custom_val(cell_bias, 0.f, 8); + fill_custom_val(output_gate_bias, 0.f, 9); + } + else + { + fill(forget_gate_bias, 7); + fill(cell_bias, 8); + fill(output_gate_bias, 9); + } fill(output_state_in, 10); fill(cell_state_in, 11); fill(scratch, 12); @@ -324,14 +386,19 @@ protected: fill(recurrent_to_input_w, 14); fill(cell_to_input_w, 15); fill(recurrent_to_input_w, 16); - fill(input_gate_bias, 17); + if(!cifg_opt && use_layer_norm) + { + fill_custom_val(input_gate_bias, 0.f, 17); + } + else + { + fill(input_gate_bias, 17); + } fill(cell_to_forget_w, 18); fill(cell_to_output_w, 19); fill(projection_w, 20); fill(projection_bias, 21); - bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? false : true; - // Compute forget_gate SimpleTensor fully_connected_forget = reference::fully_connected_layer(input, input_to_forget_w, forget_gate_bias, output_cell_shape); SimpleTensor transposed_weights = reference::transpose(recurrent_to_forget_w); @@ -344,6 +411,15 @@ protected: forget_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, forget_gate, pixelwise_mul_forget_gate, data_type, ConvertPolicy::SATURATE); } + if(use_layer_norm) + { + SimpleTensor forget_layer_norm_w{ cell_bias_shape, data_type }; + fill(forget_layer_norm_w, 23); + forget_gate = reference::mean_std_normalization_layer(forget_gate); + forget_gate = reference::pixel_wise_multiplication(forget_gate, forget_layer_norm_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + fill(forget_gate_bias, 7); + forget_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, forget_gate, forget_gate_bias, data_type, ConvertPolicy::SATURATE); + } forget_gate = reference::activation_layer(forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); // Compute input_gate @@ -365,6 +441,15 @@ protected: SimpleTensor pixelwise_mul_input_gate = reference::pixel_wise_multiplication(cell_state_in, cell_to_input_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); input_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, input_gate, pixelwise_mul_input_gate, data_type, ConvertPolicy::SATURATE); } + if(use_layer_norm) + { + SimpleTensor input_layer_norm_w{ cell_bias_shape, data_type }; + fill(input_layer_norm_w, 22); + input_gate = reference::mean_std_normalization_layer(input_gate); + input_gate = reference::pixel_wise_multiplication(input_gate, input_layer_norm_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + fill(input_gate_bias, 17); + input_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, input_gate, input_gate_bias, data_type, ConvertPolicy::SATURATE); + } input_gate = reference::activation_layer(input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); } @@ -374,9 +459,18 @@ protected: gemm = reference::gemm(output_state_in, transposed_weights, cell_state_out, 1.f, 0.f); SimpleTensor pixelwise_mul = reference::pixel_wise_multiplication(cell_state_in, forget_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); cell_state_out = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, fully_connected_cell_state, gemm, data_type, ConvertPolicy::SATURATE); - cell_state_out = reference::activation_layer(cell_state_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); - cell_state_out = reference::pixel_wise_multiplication(cell_state_out, input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); - cell_state_out = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, cell_state_out, pixelwise_mul, data_type, ConvertPolicy::SATURATE); + if(use_layer_norm) + { + SimpleTensor cell_layer_norm_w{ cell_bias_shape, data_type }; + fill(cell_layer_norm_w, 24); + cell_state_out = reference::mean_std_normalization_layer(cell_state_out); + cell_state_out = reference::pixel_wise_multiplication(cell_state_out, cell_layer_norm_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + fill(cell_bias, 8); + cell_state_out = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, cell_state_out, cell_bias, data_type, ConvertPolicy::SATURATE); + } + cell_state_out = reference::activation_layer(cell_state_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + cell_state_out = reference::pixel_wise_multiplication(cell_state_out, input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + cell_state_out = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, cell_state_out, pixelwise_mul, data_type, ConvertPolicy::SATURATE); if(cell_threshold != 0.f) { cell_state_out = reference::activation_layer(cell_state_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, cell_threshold)); @@ -392,6 +486,15 @@ protected: pixelwise_mul = reference::pixel_wise_multiplication(cell_state_out, cell_to_output_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); output = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, output, pixelwise_mul, data_type, ConvertPolicy::SATURATE); } + if(use_layer_norm) + { + SimpleTensor output_layer_norm_w{ cell_bias_shape, data_type }; + fill(output_layer_norm_w, 25); + output = reference::mean_std_normalization_layer(output); + output = reference::pixel_wise_multiplication(output, output_layer_norm_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + fill(output_gate_bias, 9); + output = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, output, output_gate_bias, data_type, ConvertPolicy::SATURATE); + } output = reference::activation_layer(output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); // Compute output state -- cgit v1.2.1