Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef __ARM_COMPUTE_CLLSTMLAYER_H__ |
| 25 | #define __ARM_COMPUTE_CLLSTMLAYER_H__ |
| 26 | |
| 27 | #include "arm_compute/runtime/IFunction.h" |
| 28 | |
| 29 | #include "arm_compute/core/CL/kernels/CLActivationLayerKernel.h" |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/CL/kernels/CLCopyKernel.h" |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame^] | 31 | #include "arm_compute/core/CL/kernels/CLElementwiseOperationKernel.h" |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/CL/kernels/CLPixelWiseMultiplicationKernel.h" |
| 33 | #include "arm_compute/core/Types.h" |
| 34 | #include "arm_compute/runtime/CL/CLMemoryGroup.h" |
| 35 | #include "arm_compute/runtime/CL/CLTensor.h" |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame^] | 36 | #include "arm_compute/runtime/CL/functions/CLElementwiseOperations.h" |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 37 | #include "arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h" |
| 38 | #include "arm_compute/runtime/CL/functions/CLGEMM.h" |
| 39 | #include "arm_compute/runtime/CL/functions/CLWidthConcatenateLayer.h" |
| 40 | #include "arm_compute/runtime/IMemoryManager.h" |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 41 | #include "arm_compute/runtime/common/LSTMParams.h" |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 42 | |
| 43 | #include <memory> |
| 44 | |
| 45 | namespace arm_compute |
| 46 | { |
| 47 | class ICLTensor; |
| 48 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 49 | /** This function performs a single time step in a Long Short-Term Memory (LSTM) layer. |
| 50 | * |
| 51 | */ |
| 52 | class CLLSTMLayer : public IFunction |
| 53 | { |
| 54 | public: |
| 55 | /** Default constructor */ |
| 56 | CLLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
| 57 | /** Initialize function's tensors. |
| 58 | * |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 59 | * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32. |
| 60 | * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 61 | * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 62 | * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 63 | * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 64 | * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 65 | * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 66 | * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 67 | * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 68 | * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 69 | * @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. |
| 70 | * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input. |
| 71 | * @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. |
| 72 | * @param[out] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. |
| 73 | * @param[out] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input. |
| 74 | * @param[out] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size]. |
| 75 | * Data types supported: Same as @p input. |
| 76 | * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization: |
| 77 | * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 78 | * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 79 | * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input. |
| 80 | * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 81 | * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 82 | * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input |
| 83 | * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 84 | * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input. |
| 85 | * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo. |
| 86 | * @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. |
| 87 | * @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. |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 88 | */ |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 89 | void configure(const ICLTensor *input, |
| 90 | const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights, |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 91 | const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights, |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 92 | const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias, |
| 93 | const ICLTensor *output_state_in, const ICLTensor *cell_state_in, |
| 94 | ICLTensor *scratch_buffer, ICLTensor *output_state_out, ICLTensor *cell_state_out, ICLTensor *output, |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 95 | const LSTMParams<ICLTensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f); |
| 96 | |
| 97 | /** Static function to check if given info will lead to a valid configuration of @ref CLLSTMLayer |
| 98 | * |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 99 | * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32. |
| 100 | * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 101 | * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 102 | * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 103 | * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 104 | * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 105 | * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 106 | * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 107 | * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 108 | * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 109 | * @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. |
| 110 | * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input. |
| 111 | * @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. |
| 112 | * @param[in] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. |
| 113 | * @param[in] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input. |
| 114 | * @param[in] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size]. |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 115 | * Data types supported: Same as @p input. |
| 116 | * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization: |
| 117 | * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 118 | * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 119 | * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input. |
| 120 | * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 121 | * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 122 | * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input |
| 123 | * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 124 | * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input. |
| 125 | * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo. |
| 126 | * @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. |
| 127 | * @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. |
| 128 | * |
| 129 | * @return a status |
| 130 | */ |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 131 | static Status validate(const ITensorInfo *input, |
| 132 | const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights, |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 133 | const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights, |
| 134 | const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias, |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 135 | const ITensorInfo *output_state_in, const ITensorInfo *cell_state_in, |
| 136 | const ITensorInfo *scratch_buffer, const ITensorInfo *output_state_out, const ITensorInfo *cell_state_out, const ITensorInfo *output, |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 137 | const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f); |
| 138 | |
| 139 | // Inherited methods overridden: |
| 140 | void run() override; |
| 141 | |
| 142 | private: |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame^] | 143 | CLMemoryGroup _memory_group; |
| 144 | CLFullyConnectedLayer _fully_connected_input_gate; |
| 145 | CLGEMM _gemm_input_gate; |
| 146 | CLTransposeKernel _transpose_input_gate; |
| 147 | CLSaturatedArithmeticOperationKernel _accum_input_gate1; |
| 148 | CLArithmeticAddition _accum_input_gate2; |
| 149 | CLSaturatedArithmeticOperationKernel _subtract_input_gate; |
| 150 | CLPixelWiseMultiplicationKernel _pixelwise_mul_input_gate; |
| 151 | CLActivationLayerKernel _activation_input_gate; |
| 152 | CLFullyConnectedLayer _fully_connected_forget_gate; |
| 153 | CLGEMM _gemm_forget_gate; |
| 154 | CLTransposeKernel _transpose_forget_gate; |
| 155 | CLSaturatedArithmeticOperationKernel _accum_forget_gate1; |
| 156 | CLArithmeticAddition _accum_forget_gate2; |
| 157 | CLPixelWiseMultiplicationKernel _pixelwise_mul_forget_gate; |
| 158 | CLActivationLayerKernel _activation_forget_gate; |
| 159 | CLFullyConnectedLayer _fully_connected_cell_state; |
| 160 | CLGEMM _gemm_cell_state1; |
| 161 | CLGEMM _gemm_cell_state2; |
| 162 | CLTransposeKernel _transpose_cell_state; |
| 163 | CLSaturatedArithmeticOperationKernel _accum_cell_state1; |
| 164 | CLSaturatedArithmeticOperationKernel _accum_cell_state2; |
| 165 | CLPixelWiseMultiplicationKernel _pixelwise_mul_cell_state1; |
| 166 | CLActivationLayerKernel _activation_cell_state; |
| 167 | CLActivationLayerKernel _cell_clip; |
| 168 | CLPixelWiseMultiplicationKernel _pixelwise_mul_cell_state2; |
| 169 | CLFullyConnectedLayer _fully_connected_output; |
| 170 | CLGEMM _gemm_output; |
| 171 | CLPixelWiseMultiplicationKernel _pixelwise_mul_output_state1; |
| 172 | CLTransposeKernel _transpose_output; |
| 173 | CLSaturatedArithmeticOperationKernel _accum_output1; |
| 174 | CLArithmeticAddition _accum_output2; |
| 175 | CLActivationLayerKernel _activation_output; |
| 176 | CLActivationLayerKernel _activation_output_state; |
| 177 | CLPixelWiseMultiplicationKernel _pixelwise_mul_output_state2; |
| 178 | CLFullyConnectedLayer _fully_connected_output_state; |
| 179 | CLGEMM _gemm_output_state; |
| 180 | CLSaturatedArithmeticOperationKernel _accum_output_state; |
| 181 | CLActivationLayerKernel _projection_clip; |
| 182 | CLCopyKernel _copy_cell_state; |
| 183 | CLCopyKernel _copy_output; |
| 184 | CLWidthConcatenateLayer _concat_scratch_buffer; |
| 185 | CLTensor _input_gate_out1; |
| 186 | CLTensor _input_gate_out2; |
| 187 | CLTensor _input_gate_out3; |
| 188 | CLTensor _input_gate_out4; |
| 189 | CLTensor _input_gate_out5; |
| 190 | CLTensor _forget_gate_out1; |
| 191 | CLTensor _forget_gate_out2; |
| 192 | CLTensor _forget_gate_out3; |
| 193 | CLTensor _forget_gate_out4; |
| 194 | CLTensor _forget_gate_out5; |
| 195 | CLTensor _cell_state_out1; |
| 196 | CLTensor _cell_state_out2; |
| 197 | CLTensor _cell_state_out3; |
| 198 | CLTensor _cell_state_out4; |
| 199 | CLTensor _cell_state_out5; |
| 200 | CLTensor _output1; |
| 201 | CLTensor _output2; |
| 202 | CLTensor _output3; |
| 203 | CLTensor _output4; |
| 204 | CLTensor _output5; |
| 205 | CLTensor _cell_state_activation; |
| 206 | CLTensor _output_state1; |
| 207 | CLTensor _ones; |
| 208 | bool _run_peephole_opt; |
| 209 | bool _run_cifg_opt; |
| 210 | bool _perform_cell_clipping; |
| 211 | bool _has_projection_weights; |
| 212 | bool _perform_projection_clipping; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 213 | }; |
| 214 | } |
| 215 | #endif /* __ARM_COMPUTE_CLLSTMLAYER_H__ */ |