blob: cf47f34290a81a84d165f8573471bbb5f632ad77 [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 *
99 * @param[in] cell_to_input_weights 1D weights tensor with dimensions [num_units]. Data type supported: Data types supported: F16/F32.
100 * @param[in] cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p cell_to_input_weights.
101 * @param[in] cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p cell_to_input_weights.
102 *
103 * @return Reference to this LSTMParams object
104 */
105 LSTMParams &set_peephole_params(const T *cell_to_input_weights, const T *cell_to_forget_weights, const T *cell_to_output_weights)
106 {
107 _cell_to_input_weights = cell_to_input_weights;
108 _cell_to_forget_weights = cell_to_forget_weights;
109 _cell_to_output_weights = cell_to_output_weights;
110 _has_peephole_opt = true;
111 return *this;
112 }
113
114 const T *input_to_input_weights() const
115 {
116 return _input_to_input_weights;
117 }
118
119 const T *recurrent_to_input_weights() const
120 {
121 return _recurrent_to_input_weights;
122 }
123
124 const T *cell_to_input_weights() const
125 {
126 return _cell_to_input_weights;
127 }
128
129 const T *input_gate_bias() const
130 {
131 return _input_gate_bias;
132 }
133
134 const T *cell_to_forget_weights() const
135 {
136 return _cell_to_forget_weights;
137 }
138
139 const T *cell_to_output_weights() const
140 {
141 return _cell_to_output_weights;
142 }
143
144 const T *projection_weights() const
145 {
146 return _projection_weights;
147 }
148
149 const T *projection_bias() const
150 {
151 return _projection_bias;
152 }
153
154 bool has_peephole_opt() const
155 {
156 return _has_peephole_opt;
157 }
158
159 bool has_projection() const
160 {
161 return _has_projection;
162 }
163
164 bool has_cifg_opt() const
165 {
166 return _has_cifg_opt;
167 }
168
169private:
170 const T *_input_to_input_weights;
171 const T *_recurrent_to_input_weights;
172 const T *_cell_to_input_weights;
173 const T *_input_gate_bias;
174 const T *_cell_to_forget_weights;
175 const T *_cell_to_output_weights;
176 const T *_projection_weights;
177 const T *_projection_bias;
178 bool _has_peephole_opt;
179 bool _has_projection;
180 bool _has_cifg_opt;
181};
182
183/** This function performs a single time step in a Long Short-Term Memory (LSTM) layer.
184 *
185 */
186class CLLSTMLayer : public IFunction
187{
188public:
189 /** Default constructor */
190 CLLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
191 /** Initialize function's tensors.
192 *
193 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32.
194 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
195 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
196 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
197 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
198 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
199 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
200 * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
201 * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
202 * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
203 * @param[in, out] output_state 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
204 * @param[in, out] cell_state 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
205 * @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.
206 * @param[out] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size].
207 * Data types supported: Same as @p input.
208 * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization:
209 * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
210 * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
211 * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input.
212 * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
213 * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
214 * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input
215 * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
216 * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input.
217 * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo.
218 * @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.
219 * @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.
220 */
221 void configure(const ICLTensor *input, const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
222 const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
223 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,
224 const LSTMParams<ICLTensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f);
225
226 /** Static function to check if given info will lead to a valid configuration of @ref CLLSTMLayer
227 *
228 * @param[in] input Source tensor info. Input is a 2D tensor info with dimensions [input_size, batch_size]. Data types supported: F16/F32.
229 * @param[in] input_to_forget_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: Same as @p input.
230 * @param[in] input_to_cell_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: Same as @p input.
231 * @param[in] input_to_output_weights 2D weights tensor info with dimensions [input_size, num_units]. Data type supported: Same as @p input.
232 * @param[in] recurrent_to_forget_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: Same as @p input.
233 * @param[in] recurrent_to_cell_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: Same as @p input.
234 * @param[in] recurrent_to_output_weights 2D weights tensor info with dimensions [output_size, num_units]. Data type supported: Same as @p input.
235 * @param[in] forget_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input.
236 * @param[in] cell_bias 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input.
237 * @param[in] output_gate_bias 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input.
238 * @param[in] output_state 2D weights tensor info with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
239 * @param[in] cell_state 2D tensor info with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
240 * @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.
241 * @param[in] output Destination tensor info. Output is a 2D tensor with dimensions [output_size, batch_size].
242 * Data types supported: Same as @p input.
243 * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization:
244 * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
245 * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
246 * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input.
247 * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
248 * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
249 * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input
250 * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
251 * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input.
252 * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo.
253 * @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.
254 * @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.
255 *
256 * @return a status
257 */
258 static Status validate(const ITensorInfo *input, const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
259 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
260 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
261 const ITensorInfo *output_state, const ITensorInfo *cell_state, const ITensorInfo *scratch_buffer, const ITensorInfo *output,
262 const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f);
263
264 // Inherited methods overridden:
265 void run() override;
266
267private:
268 CLMemoryGroup _memory_group;
269 CLFullyConnectedLayer _fully_connected_input_gate;
270 CLGEMM _gemm_input_gate1;
271 CLGEMM _gemm_input_gate2;
272 CLTransposeKernel _transpose_input_gate1;
273 CLTransposeKernel _transpose_input_gate2;
274 CLArithmeticAdditionKernel _accum_input_gate1;
275 CLArithmeticAddition _accum_input_gate2;
276 CLArithmeticSubtractionKernel _subtract_input_gate;
277 CLActivationLayerKernel _activation_input_gate;
278 CLFullyConnectedLayer _fully_connected_forget_gate;
279 CLGEMM _gemm_forget_gate1;
280 CLGEMM _gemm_forget_gate2;
281 CLTransposeKernel _transpose_forget_gate1;
282 CLTransposeKernel _transpose_forget_gate2;
283 CLArithmeticAdditionKernel _accum_forget_gate1;
284 CLArithmeticAddition _accum_forget_gate2;
285 CLActivationLayerKernel _activation_forget_gate;
286 CLFullyConnectedLayer _fully_connected_cell_state;
287 CLGEMM _gemm_cell_state1;
288 CLGEMM _gemm_cell_state2;
289 CLTransposeKernel _transpose_cell_state1;
290 CLArithmeticAdditionKernel _accum_cell_state1;
291 CLArithmeticAdditionKernel _accum_cell_state2;
292 CLPixelWiseMultiplicationKernel _pixelwise_mul_cell_state1;
293 CLActivationLayerKernel _activation_cell_state;
294 CLActivationLayerKernel _cell_clip;
295 CLPixelWiseMultiplicationKernel _pixelwise_mul_cell_state2;
296 CLFullyConnectedLayer _fully_connected_output;
297 CLGEMM _gemm_output1;
298 CLGEMM _gemm_output2;
299 CLTransposeKernel _transpose_output1;
300 CLTransposeKernel _transpose_output2;
301 CLArithmeticAdditionKernel _accum_output1;
302 CLArithmeticAddition _accum_output2;
303 CLActivationLayerKernel _activation_output;
304 CLActivationLayerKernel _activation_output_state;
305 CLPixelWiseMultiplicationKernel _pixelwise_mul_output_state;
306 CLFullyConnectedLayer _fully_connected_output_state;
307 CLGEMM _gemm_output_state;
308 CLArithmeticAdditionKernel _accum_output_state;
309 CLActivationLayerKernel _projection_clip;
310 CLCopyKernel _copy_cell_state;
311 CLCopyKernel _copy_output;
312 CLWidthConcatenateLayer _concat_scratch_buffer;
313 CLTensor _input_gate_out1;
314 CLTensor _input_gate_out2;
315 CLTensor _input_gate_out3;
316 CLTensor _input_gate_out4;
317 CLTensor _input_gate_out5;
318 CLTensor _input_gate_out6;
319 CLTensor _forget_gate_out1;
320 CLTensor _forget_gate_out2;
321 CLTensor _forget_gate_out3;
322 CLTensor _forget_gate_out4;
323 CLTensor _forget_gate_out5;
324 CLTensor _forget_gate_out6;
325 CLTensor _cell_state_out1;
326 CLTensor _cell_state_out2;
327 CLTensor _cell_state_out3;
328 CLTensor _cell_state_out4;
329 CLTensor _cell_state_out5;
330 CLTensor _output1;
331 CLTensor _output2;
332 CLTensor _output3;
333 CLTensor _output4;
334 CLTensor _output5;
335 CLTensor _output6;
336 CLTensor _cell_state_activation;
337 CLTensor _output_projection1;
338 CLTensor _ones;
339 bool _run_peephole_opt;
340 bool _run_cifg_opt;
341 bool _perform_cell_clipping;
342 bool _has_projection_weights;
343 bool _perform_projection_clipping;
344};
345}
346#endif /* __ARM_COMPUTE_CLLSTMLAYER_H__ */