blob: bed1fa9f5690712068ee1cda40f4965bacf45ab7 [file] [log] [blame]
Michalis Spyroubcedf512018-03-22 14:55:08 +00001/*
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"
30#include "arm_compute/core/CL/kernels/CLArithmeticAdditionKernel.h"
31#include "arm_compute/core/CL/kernels/CLArithmeticSubtractionKernel.h"
32#include "arm_compute/core/CL/kernels/CLCopyKernel.h"
33#include "arm_compute/core/CL/kernels/CLPixelWiseMultiplicationKernel.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/runtime/CL/CLMemoryGroup.h"
36#include "arm_compute/runtime/CL/CLTensor.h"
37#include "arm_compute/runtime/CL/functions/CLArithmeticAddition.h"
38#include "arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h"
39#include "arm_compute/runtime/CL/functions/CLGEMM.h"
40#include "arm_compute/runtime/CL/functions/CLWidthConcatenateLayer.h"
41#include "arm_compute/runtime/IMemoryManager.h"
42
43#include <memory>
44
45namespace arm_compute
46{
47class ICLTensor;
48
49template <typename T>
50class LSTMParams
51{
52public:
53 /** Constructor */
54 LSTMParams()
55 : _input_to_input_weights(nullptr), _recurrent_to_input_weights(nullptr), _cell_to_input_weights(nullptr), _input_gate_bias(nullptr), _cell_to_forget_weights(nullptr),
56 _cell_to_output_weights(nullptr), _projection_weights(nullptr), _projection_bias(nullptr), _has_peephole_opt(false), _has_projection(false), _has_cifg_opt(true)
57 {
58 }
59 /** Prevent instances of this class from being copied (As this class contains pointers) */
60 LSTMParams(const LSTMParams &) = delete;
61 /** Prevent instances of this class from being copied (As this class contains pointers) */
62 LSTMParams &operator=(const LSTMParams &) = delete;
63 /** Default destructor */
64 ~LSTMParams() = default;
65 /** Set CIFG tensor parameters.
66 *
67 * @param[in] input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data types supported: F16/F32.
68 * @param[in] recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input_to_input_weights.
69 * @param[in] cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input_to_input_weights.
70 * @param[in] input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input_to_input_weights
71 *
72 * @return Reference to this LSTMParams object
73 */
74 LSTMParams &set_cifg_params(const T *input_to_input_weights, const T *recurrent_to_input_weights, const T *cell_to_input_weights, const T *input_gate_bias)
75 {
76 _input_to_input_weights = input_to_input_weights;
77 _recurrent_to_input_weights = recurrent_to_input_weights;
78 _cell_to_input_weights = cell_to_input_weights;
79 _input_gate_bias = input_gate_bias;
80 _has_cifg_opt = false;
81 return *this;
82 }
83 /** Set projection tensor parameters.
84 *
85 * @param[in] projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Data types supported: F16/F32.
86 * @param[in] projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p projection_weights.
87 *
88 * @return Reference to this LSTMParams object
89 */
90 LSTMParams &set_projection_params(const T *projection_weights, const T *projection_bias)
91 {
92 _projection_weights = projection_weights;
93 _projection_bias = projection_bias;
94 _has_projection = true;
95 return *this;
96 }
97 /** Set peephole tensor parameters.
98 *
Michalis Spyrou09daf4d2018-06-28 17:07:22 +010099 * @param[in] cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Data types supported: F16/F32.
Michalis Spyroubcedf512018-03-22 14:55:08 +0000100 * @param[in] cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p cell_to_input_weights.
101 *
102 * @return Reference to this LSTMParams object
103 */
Michalis Spyrou09daf4d2018-06-28 17:07:22 +0100104 LSTMParams &set_peephole_params(const T *cell_to_forget_weights, const T *cell_to_output_weights)
Michalis Spyroubcedf512018-03-22 14:55:08 +0000105 {
Michalis Spyroubcedf512018-03-22 14:55:08 +0000106 _cell_to_forget_weights = cell_to_forget_weights;
107 _cell_to_output_weights = cell_to_output_weights;
108 _has_peephole_opt = true;
109 return *this;
110 }
111
112 const T *input_to_input_weights() const
113 {
114 return _input_to_input_weights;
115 }
116
117 const T *recurrent_to_input_weights() const
118 {
119 return _recurrent_to_input_weights;
120 }
121
122 const T *cell_to_input_weights() const
123 {
124 return _cell_to_input_weights;
125 }
126
127 const T *input_gate_bias() const
128 {
129 return _input_gate_bias;
130 }
131
132 const T *cell_to_forget_weights() const
133 {
134 return _cell_to_forget_weights;
135 }
136
137 const T *cell_to_output_weights() const
138 {
139 return _cell_to_output_weights;
140 }
141
142 const T *projection_weights() const
143 {
144 return _projection_weights;
145 }
146
147 const T *projection_bias() const
148 {
149 return _projection_bias;
150 }
151
152 bool has_peephole_opt() const
153 {
154 return _has_peephole_opt;
155 }
156
157 bool has_projection() const
158 {
159 return _has_projection;
160 }
161
162 bool has_cifg_opt() const
163 {
164 return _has_cifg_opt;
165 }
166
167private:
168 const T *_input_to_input_weights;
169 const T *_recurrent_to_input_weights;
170 const T *_cell_to_input_weights;
171 const T *_input_gate_bias;
172 const T *_cell_to_forget_weights;
173 const T *_cell_to_output_weights;
174 const T *_projection_weights;
175 const T *_projection_bias;
176 bool _has_peephole_opt;
177 bool _has_projection;
178 bool _has_cifg_opt;
179};
180
181/** This function performs a single time step in a Long Short-Term Memory (LSTM) layer.
182 *
183 */
184class CLLSTMLayer : public IFunction
185{
186public:
187 /** Default constructor */
188 CLLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
189 /** Initialize function's tensors.
190 *
191 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32.
192 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
193 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
194 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
195 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
196 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
197 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
198 * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
199 * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
200 * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
201 * @param[in, out] output_state 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
202 * @param[in, out] cell_state 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
203 * @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.
204 * @param[out] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size].
205 * Data types supported: Same as @p input.
206 * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization:
207 * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
208 * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
209 * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input.
210 * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
211 * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
212 * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input
213 * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
214 * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input.
215 * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo.
216 * @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.
217 * @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.
218 */
219 void configure(const ICLTensor *input, const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
220 const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
221 const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias, ICLTensor *output_state, ICLTensor *cell_state, ICLTensor *scratch_buffer, ICLTensor *output,
222 const LSTMParams<ICLTensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f);
223
224 /** Static function to check if given info will lead to a valid configuration of @ref CLLSTMLayer
225 *
226 * @param[in] input Source tensor info. Input is a 2D tensor info with dimensions [input_size, batch_size]. Data types supported: F16/F32.
227 * @param[in] input_to_forget_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: Same as @p input.
228 * @param[in] input_to_cell_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: Same as @p input.
229 * @param[in] input_to_output_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: Same as @p input.
230 * @param[in] recurrent_to_forget_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: Same as @p input.
231 * @param[in] recurrent_to_cell_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: Same as @p input.
232 * @param[in] recurrent_to_output_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: Same as @p input.
233 * @param[in] forget_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input.
234 * @param[in] cell_bias 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input.
235 * @param[in] output_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input.
236 * @param[in] output_state 2D weights tensor info with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
237 * @param[in] cell_state 2D tensor info with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
238 * @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.
239 * @param[in] output Destination tensor info. Output is a 2D tensor with dimensions [output_size, batch_size].
240 * Data types supported: Same as @p input.
241 * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization:
242 * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
243 * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
244 * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input.
245 * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
246 * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
247 * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input
248 * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
249 * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input.
250 * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo.
251 * @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.
252 * @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.
253 *
254 * @return a status
255 */
256 static Status validate(const ITensorInfo *input, const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
257 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
258 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
259 const ITensorInfo *output_state, const ITensorInfo *cell_state, const ITensorInfo *scratch_buffer, const ITensorInfo *output,
260 const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f);
261
262 // Inherited methods overridden:
263 void run() override;
264
265private:
266 CLMemoryGroup _memory_group;
267 CLFullyConnectedLayer _fully_connected_input_gate;
268 CLGEMM _gemm_input_gate1;
269 CLGEMM _gemm_input_gate2;
270 CLTransposeKernel _transpose_input_gate1;
271 CLTransposeKernel _transpose_input_gate2;
272 CLArithmeticAdditionKernel _accum_input_gate1;
273 CLArithmeticAddition _accum_input_gate2;
274 CLArithmeticSubtractionKernel _subtract_input_gate;
275 CLActivationLayerKernel _activation_input_gate;
276 CLFullyConnectedLayer _fully_connected_forget_gate;
277 CLGEMM _gemm_forget_gate1;
278 CLGEMM _gemm_forget_gate2;
279 CLTransposeKernel _transpose_forget_gate1;
280 CLTransposeKernel _transpose_forget_gate2;
281 CLArithmeticAdditionKernel _accum_forget_gate1;
282 CLArithmeticAddition _accum_forget_gate2;
283 CLActivationLayerKernel _activation_forget_gate;
284 CLFullyConnectedLayer _fully_connected_cell_state;
285 CLGEMM _gemm_cell_state1;
286 CLGEMM _gemm_cell_state2;
287 CLTransposeKernel _transpose_cell_state1;
288 CLArithmeticAdditionKernel _accum_cell_state1;
289 CLArithmeticAdditionKernel _accum_cell_state2;
290 CLPixelWiseMultiplicationKernel _pixelwise_mul_cell_state1;
291 CLActivationLayerKernel _activation_cell_state;
292 CLActivationLayerKernel _cell_clip;
293 CLPixelWiseMultiplicationKernel _pixelwise_mul_cell_state2;
294 CLFullyConnectedLayer _fully_connected_output;
295 CLGEMM _gemm_output1;
296 CLGEMM _gemm_output2;
297 CLTransposeKernel _transpose_output1;
298 CLTransposeKernel _transpose_output2;
299 CLArithmeticAdditionKernel _accum_output1;
300 CLArithmeticAddition _accum_output2;
301 CLActivationLayerKernel _activation_output;
302 CLActivationLayerKernel _activation_output_state;
303 CLPixelWiseMultiplicationKernel _pixelwise_mul_output_state;
304 CLFullyConnectedLayer _fully_connected_output_state;
305 CLGEMM _gemm_output_state;
306 CLArithmeticAdditionKernel _accum_output_state;
307 CLActivationLayerKernel _projection_clip;
308 CLCopyKernel _copy_cell_state;
309 CLCopyKernel _copy_output;
310 CLWidthConcatenateLayer _concat_scratch_buffer;
311 CLTensor _input_gate_out1;
312 CLTensor _input_gate_out2;
313 CLTensor _input_gate_out3;
314 CLTensor _input_gate_out4;
315 CLTensor _input_gate_out5;
316 CLTensor _input_gate_out6;
317 CLTensor _forget_gate_out1;
318 CLTensor _forget_gate_out2;
319 CLTensor _forget_gate_out3;
320 CLTensor _forget_gate_out4;
321 CLTensor _forget_gate_out5;
322 CLTensor _forget_gate_out6;
323 CLTensor _cell_state_out1;
324 CLTensor _cell_state_out2;
325 CLTensor _cell_state_out3;
326 CLTensor _cell_state_out4;
327 CLTensor _cell_state_out5;
328 CLTensor _output1;
329 CLTensor _output2;
330 CLTensor _output3;
331 CLTensor _output4;
332 CLTensor _output5;
333 CLTensor _output6;
334 CLTensor _cell_state_activation;
335 CLTensor _output_projection1;
336 CLTensor _ones;
337 bool _run_peephole_opt;
338 bool _run_cifg_opt;
339 bool _perform_cell_clipping;
340 bool _has_projection_weights;
341 bool _perform_projection_clipping;
342};
343}
344#endif /* __ARM_COMPUTE_CLLSTMLAYER_H__ */