blob: 4a47dfb2cfa0bcf1802c15859774e17298cdfe32 [file] [log] [blame]
Michalis Spyrou25f45a42018-08-08 12:53:05 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Michalis Spyrou25f45a42018-08-08 12:53:05 +01003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_NELSTMLAYER_H
25#define ARM_COMPUTE_NELSTMLAYER_H
Michalis Spyrou25f45a42018-08-08 12:53:05 +010026
27#include "arm_compute/core/NEON/kernels/NEActivationLayerKernel.h"
Michalis Spyrou25f45a42018-08-08 12:53:05 +010028#include "arm_compute/core/NEON/kernels/NECopyKernel.h"
Michalis Spyrou25f45a42018-08-08 12:53:05 +010029
30#include "arm_compute/core/Types.h"
Michalis Spyrou25f45a42018-08-08 12:53:05 +010031#include "arm_compute/runtime/NEON/functions/NEArithmeticAddition.h"
Michalis Spyrou173ba9b2020-06-23 17:25:43 +010032#include "arm_compute/runtime/NEON/functions/NEArithmeticAddition.h"
33#include "arm_compute/runtime/NEON/functions/NEArithmeticSubtraction.h"
Georgios Pinitas09f24972019-05-17 18:14:40 +010034#include "arm_compute/runtime/NEON/functions/NEConcatenateLayer.h"
Michalis Spyrou25f45a42018-08-08 12:53:05 +010035#include "arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h"
36#include "arm_compute/runtime/NEON/functions/NEGEMM.h"
Michele Di Giorgio0cbfda62019-06-13 17:01:29 +010037#include "arm_compute/runtime/NEON/functions/NEMeanStdDevNormalizationLayer.h"
Michalis Spyrou6eb73452020-07-02 17:39:25 +010038#include "arm_compute/runtime/NEON/functions/NEPixelWiseMultiplication.h"
Michalis Spyrou25f45a42018-08-08 12:53:05 +010039#include "arm_compute/runtime/common/LSTMParams.h"
40
41namespace arm_compute
42{
43// Forward declarations
44class ITensor;
45
46/** Basic function to run @ref NELSTMLayer */
47class NELSTMLayer : public IFunction
48{
49public:
50 /** Default constructor */
51 NELSTMLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
52 /** Initialize function's tensors.
53 *
54 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32.
55 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
56 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
57 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
58 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
59 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
60 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
61 * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
62 * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
63 * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
64 * @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
65 * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
66 * @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.
67 * @param[out] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
68 * @param[out] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
69 * @param[out] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size].
70 * Data types supported: Same as @p input.
Michele Di Giorgio47a89902020-03-09 19:32:33 +000071 * @param[in] lstm_params Weights tensors used in peephole optimization:
72 * input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
73 * recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
74 * cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input.
75 * cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
76 * cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
77 * input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input
78 * projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
79 * projection_bias (Optional) 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input.
80 * input_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
81 * forget_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
82 * cell_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
83 * output_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
Michalis Spyrou25f45a42018-08-08 12:53:05 +010084 * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo.
85 * @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.
Michele Di Giorgio47a89902020-03-09 19:32:33 +000086 * @param[in] projection_threshold The clipping threshold for the output from the projection layer, such that values are bound within [-proj_clip, proj_clip].
87 * If set to 0.0 then clipping is disabled.
Michalis Spyrou25f45a42018-08-08 12:53:05 +010088 */
89 void configure(const ITensor *input,
90 const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights,
91 const ITensor *recurrent_to_forget_weights, const ITensor *recurrent_to_cell_weights, const ITensor *recurrent_to_output_weights,
92 const ITensor *forget_gate_bias, const ITensor *cell_bias, const ITensor *output_gate_bias,
93 const ITensor *output_state_in, const ITensor *cell_state_in,
94 ITensor *scratch_buffer, ITensor *output_state_out, ITensor *cell_state_out, ITensor *output,
95 const LSTMParams<ITensor> &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 NELSTMLayer
98 *
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].
115 * Data types supported: Same as @p input.
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000116 * @param[in] lstm_params Weights tensors used in peephole optimization:
117 * input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
118 * recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
119 * cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input.
120 * cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
121 * cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
122 * input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input
123 * projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
124 * projection_bias (Optional) 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input.
125 * input_layer_norm_weights (Optional) 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input.
126 * forget_layer_norm_weights (Optional) 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input.
127 * cell_layer_norm_weights (Optional) 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input.
128 * output_layer_norm_weights (Optional) 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input.
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100129 * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo.
130 * @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.
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000131 * @param[in] projection_threshold The clipping threshold for the output from the projection layer, such that values are bound within [-proj_clip, proj_clip].
132 * If set to 0.0 then clipping is disabled.
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100133 *
134 * @return a status
135 */
136 static Status validate(const ITensorInfo *input,
137 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
138 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
139 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
140 const ITensorInfo *output_state_in, const ITensorInfo *cell_state_in,
141 const ITensorInfo *scratch_buffer, const ITensorInfo *output_state_out, const ITensorInfo *cell_state_out, const ITensorInfo *output,
142 const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f);
143
144 // Inherited methods overridden:
145 void run() override;
John Kesapides917959c2019-02-04 12:37:29 +0000146 void prepare() override;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100147
148private:
Michalis Spyrou6eb73452020-07-02 17:39:25 +0100149 MemoryGroup _memory_group;
150 NEFullyConnectedLayer _fully_connected_input_gate;
151 NEArithmeticAddition _accum_input_gate1;
152 NEArithmeticSubtraction _subtract_input_gate;
153 NEPixelWiseMultiplication _pixelwise_mul_input_gate;
154 NEActivationLayer _activation_input_gate;
155 NEFullyConnectedLayer _fully_connected_forget_gate;
156 NEArithmeticAddition _accum_forget_gate1;
157 NEPixelWiseMultiplication _pixelwise_mul_forget_gate;
158 NEActivationLayer _activation_forget_gate;
159 NEFullyConnectedLayer _fully_connected_cell_state;
160 NEGEMM _gemm_cell_state1;
161 NETransposeKernel _transpose_cell_state;
162 NEArithmeticAddition _accum_cell_state1;
163 NEArithmeticAddition _accum_cell_state2;
164 NEPixelWiseMultiplication _pixelwise_mul_cell_state1;
165 NEActivationLayer _activation_cell_state;
166 NEActivationLayer _cell_clip;
167 NEPixelWiseMultiplication _pixelwise_mul_cell_state2;
168 NEFullyConnectedLayer _fully_connected_output;
169 NEPixelWiseMultiplication _pixelwise_mul_output_state1;
170 NEArithmeticAddition _accum_output1;
171 NEActivationLayer _activation_output;
172 NEActivationLayer _activation_output_state;
173 NEPixelWiseMultiplication _pixelwise_mul_output_state2;
174 NEFullyConnectedLayer _fully_connected_output_state;
175 NEActivationLayer _projection_clip;
176 NECopyKernel _copy_cell_state;
177 NECopyKernel _copy_output;
178 NEConcatenateLayer _concat_scratch_buffer;
179 NEConcatenateLayer _concat_inputs_forget_gate;
180 NEConcatenateLayer _concat_weights_forget_gate;
181 NEConcatenateLayer _concat_weights_input_gate;
182 NEConcatenateLayer _concat_weights_output;
183 NEMeanStdDevNormalizationLayer _mean_std_norm_input_gate;
184 NEPixelWiseMultiplication _pixelwise_mul_input_gate_coeff;
185 NEArithmeticAddition _accum_input_gate_bias;
186 NEMeanStdDevNormalizationLayer _mean_std_norm_forget_gate;
187 NEPixelWiseMultiplication _pixelwise_mul_forget_gate_coeff;
188 NEArithmeticAddition _accum_forget_gate_bias;
189 NEMeanStdDevNormalizationLayer _mean_std_norm_cell_gate;
190 NEPixelWiseMultiplication _pixelwise_mul_cell_gate_coeff;
191 NEArithmeticAddition _accum_cell_gate_bias;
192 NEMeanStdDevNormalizationLayer _mean_std_norm_output_gate;
193 NEPixelWiseMultiplication _pixelwise_mul_output_gate_coeff;
194 NEArithmeticAddition _accum_output_gate_bias;
195 Tensor _input_gate_out1;
196 Tensor _input_gate_out2;
197 Tensor _input_gate_out3;
198 Tensor _input_gate_out4;
199 Tensor _forget_gate_out1;
200 Tensor _forget_gate_out2;
201 Tensor _forget_gate_out3;
202 Tensor _forget_gate_out4;
203 Tensor _forget_gate_out5;
204 Tensor _forget_gate_out6;
205 Tensor _cell_state_out1;
206 Tensor _cell_state_out2;
207 Tensor _cell_state_out3;
208 Tensor _cell_state_out4;
209 Tensor _cell_state_out5;
210 Tensor _output1;
211 Tensor _output2;
212 Tensor _output3;
213 Tensor _output4;
214 Tensor _cell_state_activation;
215 Tensor _output_state1;
216 Tensor _ones;
217 Tensor _input_layer_norm_out1;
218 Tensor _input_layer_norm_out2;
219 Tensor _forget_layer_norm_out1;
220 Tensor _forget_layer_norm_out2;
221 Tensor _cell_layer_norm_out1;
222 Tensor _cell_layer_norm_out2;
223 Tensor _output_layer_norm_out1;
224 Tensor _output_layer_norm_out2;
225 bool _run_peephole_opt;
226 bool _run_cifg_opt;
227 bool _perform_cell_clipping;
228 bool _has_projection_weights;
229 bool _perform_projection_clipping;
230 bool _is_prepared;
231 bool _is_layer_norm_lstm;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100232};
233} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000234#endif /* ARM_COMPUTE_NELSTMLAYER_H */