blob: 8bd47cbf8e86550eae9e8f991de473e445a7cad2 [file] [log] [blame]
Michalis Spyroubcedf512018-03-22 14:55:08 +00001/*
John Kesapidescafec8f2019-02-19 15:53:59 +00002 * Copyright (c) 2018-2019 ARM Limited.
Michalis Spyroubcedf512018-03-22 14:55:08 +00003 *
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 Spyroubcedf512018-03-22 14:55:08 +000030#include "arm_compute/core/CL/kernels/CLCopyKernel.h"
giuros01164a2722018-11-20 18:34:46 +000031#include "arm_compute/core/CL/kernels/CLElementwiseOperationKernel.h"
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +010032#include "arm_compute/core/CL/kernels/CLMemsetKernel.h"
Michalis Spyroubcedf512018-03-22 14:55:08 +000033#include "arm_compute/core/CL/kernels/CLPixelWiseMultiplicationKernel.h"
John Kesapidescafec8f2019-02-19 15:53:59 +000034#include "arm_compute/core/CL/kernels/CLWidthConcatenate2TensorsKernel.h"
Michalis Spyroubcedf512018-03-22 14:55:08 +000035#include "arm_compute/core/Types.h"
36#include "arm_compute/runtime/CL/CLMemoryGroup.h"
37#include "arm_compute/runtime/CL/CLTensor.h"
giuros01164a2722018-11-20 18:34:46 +000038#include "arm_compute/runtime/CL/functions/CLElementwiseOperations.h"
Michalis Spyroubcedf512018-03-22 14:55:08 +000039#include "arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h"
40#include "arm_compute/runtime/CL/functions/CLGEMM.h"
41#include "arm_compute/runtime/CL/functions/CLWidthConcatenateLayer.h"
42#include "arm_compute/runtime/IMemoryManager.h"
Michalis Spyrou25f45a42018-08-08 12:53:05 +010043#include "arm_compute/runtime/common/LSTMParams.h"
Michalis Spyroubcedf512018-03-22 14:55:08 +000044
45#include <memory>
46
47namespace arm_compute
48{
49class ICLTensor;
50
Michalis Spyroubcedf512018-03-22 14:55:08 +000051/** This function performs a single time step in a Long Short-Term Memory (LSTM) layer.
52 *
53 */
54class CLLSTMLayer : public IFunction
55{
56public:
57 /** Default constructor */
58 CLLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
59 /** Initialize function's tensors.
60 *
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010061 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32.
62 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
63 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
64 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
65 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
66 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
67 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
68 * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
69 * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
70 * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
71 * @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
72 * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
73 * @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.
74 * @param[out] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
75 * @param[out] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
76 * @param[out] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size].
77 * Data types supported: Same as @p input.
78 * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization:
79 * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
80 * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
81 * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input.
82 * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
83 * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
84 * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input
85 * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
86 * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input.
87 * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo.
88 * @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.
89 * @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 Spyroubcedf512018-03-22 14:55:08 +000090 */
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010091 void configure(const ICLTensor *input,
92 const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
Michalis Spyroubcedf512018-03-22 14:55:08 +000093 const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010094 const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
95 const ICLTensor *output_state_in, const ICLTensor *cell_state_in,
96 ICLTensor *scratch_buffer, ICLTensor *output_state_out, ICLTensor *cell_state_out, ICLTensor *output,
Michalis Spyroubcedf512018-03-22 14:55:08 +000097 const LSTMParams<ICLTensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f);
98
99 /** Static function to check if given info will lead to a valid configuration of @ref CLLSTMLayer
100 *
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100101 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32.
102 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
103 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
104 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
105 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
106 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
107 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
108 * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
109 * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
110 * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
111 * @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
112 * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
113 * @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.
114 * @param[in] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
115 * @param[in] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
116 * @param[in] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size].
Michalis Spyroubcedf512018-03-22 14:55:08 +0000117 * Data types supported: Same as @p input.
118 * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization:
119 * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
120 * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
121 * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input.
122 * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
123 * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
124 * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input
125 * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
126 * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input.
127 * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo.
128 * @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.
129 * @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.
130 *
131 * @return a status
132 */
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100133 static Status validate(const ITensorInfo *input,
134 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
Michalis Spyroubcedf512018-03-22 14:55:08 +0000135 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
136 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100137 const ITensorInfo *output_state_in, const ITensorInfo *cell_state_in,
138 const ITensorInfo *scratch_buffer, const ITensorInfo *output_state_out, const ITensorInfo *cell_state_out, const ITensorInfo *output,
Michalis Spyroubcedf512018-03-22 14:55:08 +0000139 const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f);
140
141 // Inherited methods overridden:
142 void run() override;
John Kesapidescafec8f2019-02-19 15:53:59 +0000143 void prepare() override;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000144
145private:
giuros01164a2722018-11-20 18:34:46 +0000146 CLMemoryGroup _memory_group;
147 CLFullyConnectedLayer _fully_connected_input_gate;
148 CLGEMM _gemm_input_gate;
149 CLTransposeKernel _transpose_input_gate;
150 CLSaturatedArithmeticOperationKernel _accum_input_gate1;
151 CLArithmeticAddition _accum_input_gate2;
152 CLSaturatedArithmeticOperationKernel _subtract_input_gate;
153 CLPixelWiseMultiplicationKernel _pixelwise_mul_input_gate;
154 CLActivationLayerKernel _activation_input_gate;
155 CLFullyConnectedLayer _fully_connected_forget_gate;
156 CLGEMM _gemm_forget_gate;
157 CLTransposeKernel _transpose_forget_gate;
158 CLSaturatedArithmeticOperationKernel _accum_forget_gate1;
159 CLArithmeticAddition _accum_forget_gate2;
160 CLPixelWiseMultiplicationKernel _pixelwise_mul_forget_gate;
161 CLActivationLayerKernel _activation_forget_gate;
162 CLFullyConnectedLayer _fully_connected_cell_state;
163 CLGEMM _gemm_cell_state1;
164 CLGEMM _gemm_cell_state2;
165 CLTransposeKernel _transpose_cell_state;
166 CLSaturatedArithmeticOperationKernel _accum_cell_state1;
167 CLSaturatedArithmeticOperationKernel _accum_cell_state2;
168 CLPixelWiseMultiplicationKernel _pixelwise_mul_cell_state1;
169 CLActivationLayerKernel _activation_cell_state;
170 CLActivationLayerKernel _cell_clip;
171 CLPixelWiseMultiplicationKernel _pixelwise_mul_cell_state2;
172 CLFullyConnectedLayer _fully_connected_output;
173 CLGEMM _gemm_output;
174 CLPixelWiseMultiplicationKernel _pixelwise_mul_output_state1;
175 CLTransposeKernel _transpose_output;
176 CLSaturatedArithmeticOperationKernel _accum_output1;
177 CLArithmeticAddition _accum_output2;
178 CLActivationLayerKernel _activation_output;
179 CLActivationLayerKernel _activation_output_state;
180 CLPixelWiseMultiplicationKernel _pixelwise_mul_output_state2;
181 CLFullyConnectedLayer _fully_connected_output_state;
182 CLGEMM _gemm_output_state;
183 CLSaturatedArithmeticOperationKernel _accum_output_state;
184 CLActivationLayerKernel _projection_clip;
185 CLCopyKernel _copy_cell_state;
186 CLCopyKernel _copy_output;
187 CLWidthConcatenateLayer _concat_scratch_buffer;
John Kesapidescafec8f2019-02-19 15:53:59 +0000188 CLWidthConcatenate2TensorsKernel _concat_inputs_forget_gate;
189 CLWidthConcatenate2TensorsKernel _concat_weights_forget_gate;
190 CLWidthConcatenate2TensorsKernel _concat_weights_input_gate;
191 CLWidthConcatenate2TensorsKernel _concat_weights_output;
Georgios Pinitasdbfc2dc2019-04-02 12:51:21 +0100192 CLMemsetKernel _ones_memset_kernel;
giuros01164a2722018-11-20 18:34:46 +0000193 CLTensor _input_gate_out1;
194 CLTensor _input_gate_out2;
195 CLTensor _input_gate_out3;
196 CLTensor _input_gate_out4;
giuros01164a2722018-11-20 18:34:46 +0000197 CLTensor _forget_gate_out1;
198 CLTensor _forget_gate_out2;
199 CLTensor _forget_gate_out3;
200 CLTensor _forget_gate_out4;
201 CLTensor _forget_gate_out5;
John Kesapidescafec8f2019-02-19 15:53:59 +0000202 CLTensor _forget_gate_out6;
giuros01164a2722018-11-20 18:34:46 +0000203 CLTensor _cell_state_out1;
204 CLTensor _cell_state_out2;
205 CLTensor _cell_state_out3;
206 CLTensor _cell_state_out4;
207 CLTensor _cell_state_out5;
208 CLTensor _output1;
209 CLTensor _output2;
210 CLTensor _output3;
211 CLTensor _output4;
giuros01164a2722018-11-20 18:34:46 +0000212 CLTensor _cell_state_activation;
213 CLTensor _output_state1;
214 CLTensor _ones;
215 bool _run_peephole_opt;
216 bool _run_cifg_opt;
217 bool _perform_cell_clipping;
218 bool _has_projection_weights;
219 bool _perform_projection_clipping;
John Kesapidescafec8f2019-02-19 15:53:59 +0000220 bool _is_prepared;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000221};
John Kesapidescafec8f2019-02-19 15:53:59 +0000222} // namespace arm_compute
Michalis Spyroubcedf512018-03-22 14:55:08 +0000223#endif /* __ARM_COMPUTE_CLLSTMLAYER_H__ */