blob: a22c669ca700afed478c77df8044bc941cc31d1e [file] [log] [blame]
Michele Di Giorgio47a89902020-03-09 19:32:33 +00001/*
2 * Copyright (c) 2020 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#include "arm_compute/runtime/NEON/functions/NEQLSTMLayer.h"
25
26#include "arm_compute/core/KernelDescriptors.h"
27#include "arm_compute/core/QuantizationInfo.h"
28#include "arm_compute/core/Utils.h"
29#include "arm_compute/core/Validate.h"
30#include "arm_compute/core/utils/misc/InfoHelpers.h"
31#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
32#include "arm_compute/runtime/NEON/NEScheduler.h"
33
34namespace arm_compute
35{
36using namespace arm_compute::utils::info_helpers;
37namespace
38{
39Status validate_mm(GEMMLowpOutputStageInfo &gemmlowp_info, const ITensorInfo *mm_input, const ITensorInfo *mm_weights, const ITensorInfo *bias,
40 float gemmlowp_scale, const TensorInfo *mm_res_info, const TensorInfo *outstage_tensor_info)
41{
42 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyCore::validate(mm_input, mm_weights, nullptr, mm_res_info));
43 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(gemmlowp_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
44 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOutputStage::validate(mm_res_info, bias, outstage_tensor_info, gemmlowp_info));
45 return Status{};
46}
47} // namespace
48
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +010049Status NEQLSTMLayer::TensorCopyKernel::validate(const ITensorInfo &src, const ITensorInfo &dst)
50{
51 ARM_COMPUTE_RETURN_ERROR_ON(src.tensor_shape().num_dimensions() > max_dimension_supported);
52 ARM_COMPUTE_RETURN_ERROR_ON(dst.tensor_shape().num_dimensions() > max_dimension_supported);
53 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst);
54 ARM_COMPUTE_RETURN_ERROR_ON(dst.tensor_shape().y() != src.tensor_shape().y());
55 return Status{};
56}
57
58void NEQLSTMLayer::TensorCopyKernel::configure(ITensor &src, ITensor &dst)
59{
60 ARM_COMPUTE_ERROR_THROW_ON(NEQLSTMLayer::TensorCopyKernel::validate(*src.info(), *dst.info()));
61 _src = &src;
62 _dst = &dst;
63 _row_size = std::min(_src->info()->tensor_shape().x(), _dst->info()->tensor_shape().x());
64 _window = calculate_max_window(*_src->info(), Steps());
65}
66
67void NEQLSTMLayer::TensorCopyKernel::run()
68{
69 Iterator input_iter{ _src, _window };
70 Iterator output_iter{ _dst, _window };
71
72 execute_window_loop(_window, [&](const Coordinates &)
73 {
74 memcpy(output_iter.ptr(), input_iter.ptr(), _row_size);
75 },
76 input_iter, output_iter);
77}
78
Michele Di Giorgio47a89902020-03-09 19:32:33 +000079NEQLSTMLayer::NEQLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager)
80{
81 _memory_group = MemoryGroup(std::move(memory_manager));
82}
83
84void NEQLSTMLayer::configure_mm(NEGEMMLowpMatrixMultiplyCore &mm, NEGEMMLowpOutputStage &outstage, GEMMLowpOutputStageInfo &gemmlowp_info,
85 const ITensor *mm_input, const ITensor *mm_weights, const ITensor *bias,
86 Tensor *mm_res, Tensor *outstage_res, float gemmlowp_scale,
87 const TensorInfo &mm_res_info, const TensorInfo &outstage_tensor_info)
88{
89 _memory_group.manage(mm_res);
90 _memory_group.manage(outstage_res);
91
92 mm_res->allocator()->init(mm_res_info);
93 outstage_res->allocator()->init(outstage_tensor_info);
94
95 // Configure matrix-multiplication
96 mm.configure(mm_input, mm_weights, nullptr, mm_res);
97
98 // Configure output stage
99 quantization::calculate_quantized_multiplier(gemmlowp_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
100 outstage.configure(mm_res, bias, outstage_res, gemmlowp_info);
101 mm_res->allocator()->allocate();
102}
103
104void NEQLSTMLayer::configure(const ITensor *input,
105 const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights,
106 const ITensor *recurrent_to_forget_weights, const ITensor *recurrent_to_cell_weights, const ITensor *recurrent_to_output_weights,
107 const ITensor *forget_gate_bias, const ITensor *cell_bias, const ITensor *output_gate_bias,
108 const ITensor *cell_state_in, const ITensor *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100109 ITensor *cell_state_out, ITensor *output_state_out, ITensor *output,
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000110 const LSTMParams<ITensor> &lstm_params)
111{
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000112 ARM_COMPUTE_ERROR_ON_NULLPTR(input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
113 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
114 forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in, cell_state_out, output_state_out);
115
116 // Set lstm parameters
117 LSTMParams<ITensorInfo> lstm_params_info{};
118 build_lstm_params_tensor_info(lstm_params, &lstm_params_info);
119
120 // Validate
121 ARM_COMPUTE_ERROR_THROW_ON(NEQLSTMLayer::validate(input->info(), input_to_forget_weights->info(), input_to_cell_weights->info(), input_to_output_weights->info(),
122 recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(),
123 forget_gate_bias->info(), cell_bias->info(), output_gate_bias->info(),
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100124 cell_state_in->info(), output_state_in->info(), cell_state_out->info(), output_state_out->info(), output->info(),
125 lstm_params_info));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000126
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100127 const int batch_size = input->info()->dimension(1);
128 const int num_units = input_to_output_weights->info()->dimension(1);
129 const int output_size = output_state_out->info()->dimension(_out_state_output_size_dimension_idx);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000130
131 const UniformQuantizationInfo qinput = input->info()->quantization_info().uniform();
132 const UniformQuantizationInfo qcell_state_in = cell_state_in->info()->quantization_info().uniform();
133 const UniformQuantizationInfo qoutput_state_in = output_state_in->info()->quantization_info().uniform();
134
135 _projection_bias = lstm_params.projection_bias();
136 _input_to_forget_weights = input_to_forget_weights;
137 _input_to_cell_weights = input_to_cell_weights;
138 _input_to_output_weights = input_to_output_weights;
139 _recurrent_to_forget_weights = recurrent_to_forget_weights;
140 _recurrent_to_cell_weights = recurrent_to_cell_weights;
141 _recurrent_to_output_weights = recurrent_to_output_weights;
142 _projection_weights = lstm_params.projection_weights();
143
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100144 // Layer normalization
145 _has_layer_norm = lstm_params.use_layer_norm();
146 if(_has_layer_norm)
147 {
148 set_layer_norm_weight(lstm_params.forget_layer_norm_weights(), LayerNormGate::Forget);
149 set_layer_norm_weight(lstm_params.cell_layer_norm_weights(), LayerNormGate::Cell);
150 set_layer_norm_weight(lstm_params.input_layer_norm_weights(), LayerNormGate::Input);
151 set_layer_norm_weight(lstm_params.output_layer_norm_weights(), LayerNormGate::Output);
152
153 set_layer_norm_bias(forget_gate_bias, LayerNormGate::Forget);
154 set_layer_norm_bias(cell_bias, LayerNormGate::Cell);
155 set_layer_norm_bias(lstm_params.input_gate_bias(), LayerNormGate::Input);
156 set_layer_norm_bias(output_gate_bias, LayerNormGate::Output);
157 }
158
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000159 _has_cifg = lstm_params.has_cifg_opt();
160 _has_projection = lstm_params.has_projection();
161 _has_peephole = lstm_params.has_peephole_opt();
162
163 // Calculate and decompose effective scales for optimizing matmul calculation
164 const int32_t cell_shift = log2(qcell_state_in.scale);
165
166 // Calculate quantized parameters for clipping.
167 int16_t quantized_cell_clip = 0;
168 if(lstm_params.cell_clip() > 0.0f)
169 {
170 quantized_cell_clip = quantize_qsymm16(lstm_params.cell_clip(), qcell_state_in);
171 }
172 _has_cell_clipping = quantized_cell_clip > 0;
173
174 // Precompute effective bias for optimizing the matmul computations.
175 if(!_has_cifg)
176 {
177 _input_to_input_weights = lstm_params.input_to_input_weights();
178 _recurrent_to_input_weights = lstm_params.recurrent_to_input_weights();
179
180 _input_to_input_reduction.configure(_input_to_input_weights, &_input_to_input_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
181 _recurrent_to_input_reduction.configure(_recurrent_to_input_weights, &_recurrent_to_input_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true));
182 }
183 _input_to_forget_reduction.configure(input_to_forget_weights, &_input_to_forget_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
184 _recurrent_to_forget_reduction.configure(recurrent_to_forget_weights, &_recurrent_to_forget_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true));
185 _input_to_cell_reduction.configure(input_to_cell_weights, &_input_to_cell_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
186 _recurrent_to_cell_reduction.configure(recurrent_to_cell_weights, &_recurrent_to_cell_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true));
187 _input_to_output_reduction.configure(input_to_output_weights, &_input_to_output_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
188 _recurrent_to_output_reduction.configure(recurrent_to_output_weights, &_recurrent_to_output_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true));
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100189 if(_has_projection)
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000190 {
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100191 _projection_reduction.configure(_projection_weights, &_projection_eff_bias, GEMMLowpReductionKernelInfo(output_size, false, lstm_params.hidden_state_zero(), true));
Michele Di Giorgio11c562c2020-06-10 16:34:50 +0100192 if(_projection_bias != nullptr)
193 {
Michele Di Giorgio19023832020-06-17 16:08:10 +0000194 _projection_bias_add.configure(_projection_bias, &_projection_eff_bias, &_projection_eff_bias, ConvertPolicy::SATURATE);
Michele Di Giorgio11c562c2020-06-10 16:34:50 +0100195 }
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000196 }
197
198 // Pre-transpose weights to be used in GEMM.
199 _transpose_input_to_forget_weights.configure(input_to_forget_weights, &_input_to_forget_weights_transposed);
200 _transpose_input_to_cell_weights.configure(input_to_cell_weights, &_input_to_cell_weights_transposed);
201 _transpose_input_to_output_weights.configure(input_to_output_weights, &_input_to_output_weights_transposed);
202 _transpose_recurrent_to_forget_weights.configure(recurrent_to_forget_weights, &_recurrent_to_forget_weights_transposed);
203 _transpose_recurrent_to_cell_weights.configure(recurrent_to_cell_weights, &_recurrent_to_cell_weights_transposed);
204 _transpose_recurrent_to_output_weights.configure(recurrent_to_output_weights, &_recurrent_to_output_weights_transposed);
205 if(!_has_cifg)
206 {
207 _transpose_input_to_input_weights.configure(lstm_params.input_to_input_weights(), &_input_to_input_weights_transposed);
208 _transpose_recurrent_to_input_weights.configure(lstm_params.recurrent_to_input_weights(), &_recurrent_to_input_weights_transposed);
209 }
210 if(_has_projection)
211 {
212 _transpose_projection_weights.configure(_projection_weights, &_projection_weights_transposed);
213 }
214
215 GEMMLowpOutputStageInfo gemmlowp_info;
216 gemmlowp_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
217 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int16_t>::lowest();
218 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int16_t>::max();
219 gemmlowp_info.output_data_type = DataType::QSYMM16;
220
221 const TensorInfo mm_out_info(TensorShape(num_units, batch_size), 1, DataType::S32);
222 // Forget gate.
223 const TensorInfo forget_gate_outstage_info(mm_out_info.tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0));
224 const float input_to_forget_scale = input_to_forget_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.forget_intermediate_scale();
225 configure_mm(_mm_input_to_forget, _input_to_forget_outstage, gemmlowp_info,
226 input, &_input_to_forget_weights_transposed, &_input_to_forget_eff_bias,
227 &_mm_input_to_forget_res, &_input_to_forget_outstage_res, input_to_forget_scale,
228 mm_out_info, forget_gate_outstage_info);
229
230 const float recurrent_to_forget_scale = recurrent_to_forget_weights->info()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.forget_intermediate_scale();
231 configure_mm(_mm_recurrent_to_forget, _recurrent_to_forget_outstage, gemmlowp_info,
232 output_state_in, &_recurrent_to_forget_weights_transposed, &_recurrent_to_forget_eff_bias,
233 &_mm_recurrent_to_forget_res, &_recurrent_to_forget_outstage_res, recurrent_to_forget_scale,
234 mm_out_info, forget_gate_outstage_info);
235
236 _accumulate_input_recurrent_forget.configure(&_input_to_forget_outstage_res, &_recurrent_to_forget_outstage_res, &_recurrent_to_forget_outstage_res, ConvertPolicy::SATURATE);
237 _input_to_forget_outstage_res.allocator()->allocate();
238
239 if(_has_peephole)
240 {
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100241 _mul_cell_to_forget_res.allocator()->init(TensorInfo(cell_state_in->info()->tensor_shape(), 1, DataType::S32));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000242 _memory_group.manage(&_mul_cell_to_forget_res);
243 _pixelwise_mul_cell_to_forget.configure(cell_state_in, lstm_params.cell_to_forget_weights(), &_mul_cell_to_forget_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
244 _cell_to_forget_outstage_res.allocator()->init(TensorInfo(_mul_cell_to_forget_res.info()->tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0)));
245 _memory_group.manage(&_cell_to_forget_outstage_res);
246 const float cell_to_forget_scale = std::pow(2, cell_shift) * lstm_params.cell_to_forget_weights()->info()->quantization_info().uniform().scale / lstm_params.forget_intermediate_scale();
247 quantization::calculate_quantized_multiplier(cell_to_forget_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
248 _cell_to_forget_outstage.configure(&_mul_cell_to_forget_res, nullptr, &_cell_to_forget_outstage_res, gemmlowp_info);
249 _mul_cell_to_forget_res.allocator()->allocate();
250 _accumulate_cell_forget.configure(&_recurrent_to_forget_outstage_res, &_cell_to_forget_outstage_res, &_recurrent_to_forget_outstage_res, ConvertPolicy::SATURATE);
251 _cell_to_forget_outstage_res.allocator()->allocate();
252 }
253
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100254 Tensor *forget_activation_input = &_recurrent_to_forget_outstage_res;
255
256 if(_has_layer_norm)
257 {
258 configure_layer_norm(LayerNormGate::Forget, forget_activation_input);
259 forget_activation_input->allocator()->allocate();
260 forget_activation_input = &get_layer_norm_output(LayerNormGate::Forget);
261 }
262
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000263 // Output quantization info of Sigmoid and Tanh activations
264 const QuantizationInfo sigmoid_tanh_outqinfo(1.f / 32768.f, 0);
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100265 const TensorInfo forget_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000266
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000267 _memory_group.manage(&_forget_gate);
268 _forget_gate.allocator()->init(forget_gate_info);
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100269 _forget_gate_sigmoid.configure(forget_activation_input, &_forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
270 forget_activation_input->allocator()->allocate();
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000271
272 // Modulation gate.
273 const TensorInfo cell_outstage_info(mm_out_info.tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.cell_intermediate_scale(), 0));
274 const float input_to_cell_scale = input_to_cell_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.cell_intermediate_scale();
275 configure_mm(_mm_input_to_cell, _input_to_cell_outstage, gemmlowp_info,
276 input, &_input_to_cell_weights_transposed, &_input_to_cell_eff_bias,
277 &_mm_input_to_cell_res, &_input_to_cell_outstage_res, input_to_cell_scale,
278 mm_out_info, cell_outstage_info);
279
280 const float recurrent_to_cell_scale = recurrent_to_cell_weights->info()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.cell_intermediate_scale();
281 configure_mm(_mm_recurrent_to_cell, _recurrent_to_cell_outstage, gemmlowp_info,
282 output_state_in, &_recurrent_to_cell_weights_transposed, &_recurrent_to_cell_eff_bias,
283 &_mm_recurrent_to_cell_res, &_recurrent_to_cell_outstage_res, recurrent_to_cell_scale,
284 mm_out_info, cell_outstage_info);
285
286 _accumulate_input_recurrent_modulation.configure(&_input_to_cell_outstage_res, &_recurrent_to_cell_outstage_res, &_recurrent_to_cell_outstage_res, ConvertPolicy::SATURATE);
287 _input_to_cell_outstage_res.allocator()->allocate();
288
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100289 Tensor *cell_activation_input = &_recurrent_to_cell_outstage_res;
290
291 if(_has_layer_norm)
292 {
293 configure_layer_norm(LayerNormGate::Cell, cell_activation_input);
294 cell_activation_input->allocator()->allocate();
295 cell_activation_input = &get_layer_norm_output(LayerNormGate::Cell);
296 }
297
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000298 const TensorInfo cell_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100299
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000300 _memory_group.manage(&_cell_gate);
301 _cell_gate.allocator()->init(cell_gate_info);
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100302 _cell_gate_tanh.configure(cell_activation_input, &_cell_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f));
303 cell_activation_input->allocator()->allocate();
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000304
305 // Input gate.
306 const TensorInfo input_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
307 _input_gate.allocator()->init(input_gate_info);
308 _memory_group.manage(&_input_gate);
309 if(_has_cifg)
310 {
311 _ones.allocator()->init(*_forget_gate.info());
312 _input_gate_sub.configure(&_ones, &_forget_gate, &_input_gate, ConvertPolicy::SATURATE);
313 _ones.allocator()->allocate();
314 }
315 else
316 {
317 const TensorInfo input_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0));
318 const float input_to_input_scale = _input_to_input_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.input_intermediate_scale();
319 configure_mm(_mm_input_to_input, _input_to_input_outstage, gemmlowp_info,
320 input, &_input_to_input_weights_transposed, &_input_to_input_eff_bias,
321 &_mm_input_to_input_res, &_input_to_input_outstage_res, input_to_input_scale,
322 mm_out_info, input_outstage_info);
323
324 const float recurrent_to_input_scale = _recurrent_to_input_weights->info()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.input_intermediate_scale();
325 configure_mm(_mm_recurrent_to_input, _recurrent_to_input_outstage, gemmlowp_info,
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100326 output_state_in, &_recurrent_to_input_weights_transposed, &_recurrent_to_input_eff_bias,
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000327 &_mm_recurrent_to_input_res, &_recurrent_to_input_outstage_res, recurrent_to_input_scale,
328 mm_out_info, input_outstage_info);
329 _accumulate_input_recurrent_input.configure(&_input_to_input_outstage_res, &_recurrent_to_input_outstage_res, &_recurrent_to_input_outstage_res, ConvertPolicy::SATURATE);
330 _input_to_input_outstage_res.allocator()->allocate();
331
332 if(_has_peephole)
333 {
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100334 _mul_cell_to_input_res.allocator()->init(TensorInfo(cell_state_in->info()->tensor_shape(), 1, DataType::S32));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000335 _memory_group.manage(&_mul_cell_to_input_res);
336 _pixelwise_mul_cell_to_input.configure(cell_state_in, lstm_params.cell_to_input_weights(), &_mul_cell_to_input_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
337 const float cell_to_input_scale = std::pow(2, cell_shift) * lstm_params.cell_to_input_weights()->info()->quantization_info().uniform().scale / lstm_params.input_intermediate_scale();
338 quantization::calculate_quantized_multiplier(cell_to_input_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
339 _cell_to_input_outstage_res.allocator()->init(TensorInfo(_mul_cell_to_input_res.info()->tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0)));
340 _memory_group.manage(&_cell_to_input_outstage_res);
341 _cell_to_input_outstage.configure(&_mul_cell_to_input_res, nullptr, &_cell_to_input_outstage_res, gemmlowp_info);
342 _mul_cell_to_input_res.allocator()->allocate();
343 _accumulate_cell_input.configure(&_recurrent_to_input_outstage_res, &_cell_to_input_outstage_res, &_recurrent_to_input_outstage_res, ConvertPolicy::SATURATE);
344 _cell_to_input_outstage_res.allocator()->allocate();
345 }
346
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100347 Tensor *input_activation_input = &_recurrent_to_input_outstage_res;
348
349 if(_has_layer_norm)
350 {
351 configure_layer_norm(LayerNormGate::Input, input_activation_input);
352 input_activation_input->allocator()->allocate();
353 input_activation_input = &get_layer_norm_output(LayerNormGate::Input);
354 }
355
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100356 _input_gate_sigmoid.configure(input_activation_input, &_input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100357 input_activation_input->allocator()->allocate();
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000358 }
359 // Cell.
360 // TODO(COMPMID-3395): Perform multiplication in the quantized domain in NEPixelWiseMultiplicationKernel
361 _pixelwise_mul_forget_cell.configure(&_forget_gate, cell_state_in, &_forget_gate, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
362 const float cell_gate_scale = _cell_gate.info()->quantization_info().uniform().scale;
363 const float mul_input_cell_scale = cell_gate_scale * std::pow(2, 15 + cell_shift);
364 const TensorInfo mul_input_cell_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(mul_input_cell_scale, 0));
365 _memory_group.manage(&_mul_input_cell_res);
366 _mul_input_cell_res.allocator()->init(mul_input_cell_info);
367 _pixelwise_mul_input_cell.configure(&_input_gate, &_cell_gate, &_mul_input_cell_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
368 _cell_gate.allocator()->allocate();
369 _add_forget_cell.configure(&_forget_gate, &_mul_input_cell_res, cell_state_out, ConvertPolicy::SATURATE);
370 _mul_input_cell_res.allocator()->allocate();
371 _forget_gate.allocator()->allocate();
372 if(_has_cell_clipping)
373 {
374 _cell_clip.configure(cell_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_cell_clip, quantized_cell_clip));
375 }
376 // Output gate.
377 const TensorInfo output_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.output_intermediate_scale(), 0));
378 const float input_to_output_scale = input_to_output_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.output_intermediate_scale();
379 configure_mm(_mm_input_to_output, _input_to_output_outstage, gemmlowp_info,
380 input, &_input_to_output_weights_transposed, &_input_to_output_eff_bias,
381 &_mm_input_to_output_res, &_input_to_output_outstage_res, input_to_output_scale,
382 mm_out_info, output_outstage_info);
383
384 const float recurrent_to_output_scale = recurrent_to_output_weights->info()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.output_intermediate_scale();
385 configure_mm(_mm_recurrent_to_output, _recurrent_to_output_outstage, gemmlowp_info,
386 output_state_in, &_recurrent_to_output_weights_transposed, &_recurrent_to_output_eff_bias,
387 &_mm_recurrent_to_output_res, &_recurrent_to_output_outstage_res, recurrent_to_output_scale,
388 mm_out_info, output_outstage_info);
389
390 _accumulate_input_recurrent_output.configure(&_recurrent_to_output_outstage_res, &_input_to_output_outstage_res, &_recurrent_to_output_outstage_res, ConvertPolicy::SATURATE);
391 _input_to_output_outstage_res.allocator()->allocate();
392
393 if(_has_peephole)
394 {
395 // TODO(COMPMID-3395): Perform multiplication in the quantized domain in NEPixelWiseMultiplicationKernel
396 // Here we are not using the output stage because all operations are done in float
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100397 _mul_cell_to_output_res.allocator()->init(TensorInfo(cell_state_out->info()->tensor_shape(), 1, DataType::S32));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000398 _memory_group.manage(&_mul_cell_to_output_res);
399 _pixelwise_mul_cell_to_output.configure(cell_state_out, lstm_params.cell_to_output_weights(), &_mul_cell_to_output_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100400
401 const float cell_to_output_scale = std::pow(2, cell_shift) * lstm_params.cell_to_output_weights()->info()->quantization_info().uniform().scale / lstm_params.output_intermediate_scale();
402 quantization::calculate_quantized_multiplier(cell_to_output_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
403 _cell_to_output_outstage_res.allocator()->init(TensorInfo(_mul_cell_to_output_res.info()->tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.output_intermediate_scale(), 0)));
404 _memory_group.manage(&_cell_to_output_outstage_res);
405 _cell_to_output_outstage.configure(&_mul_cell_to_output_res, nullptr, &_cell_to_output_outstage_res, gemmlowp_info);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000406 _mul_cell_to_output_res.allocator()->allocate();
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100407
408 _accumulate_cell_to_output.configure(&_recurrent_to_output_outstage_res, &_cell_to_output_outstage_res, &_recurrent_to_output_outstage_res, ConvertPolicy::SATURATE);
409 _cell_to_output_outstage_res.allocator()->allocate();
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000410 }
411
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100412 Tensor *output_activation_input = &_recurrent_to_output_outstage_res;
413
414 if(_has_layer_norm)
415 {
416 configure_layer_norm(LayerNormGate::Output, output_activation_input);
417 output_activation_input->allocator()->allocate();
418 output_activation_input = &get_layer_norm_output(LayerNormGate::Output);
419 }
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000420 const TensorInfo output_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100421
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000422 _memory_group.manage(&_output_gate);
423 _output_gate.allocator()->init(output_gate_info);
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100424 _output_gate_sigmoid.configure(output_activation_input, &_output_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
425 output_activation_input->allocator()->allocate();
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000426
427 // Hidden.
428 _hidden_tanh.configure(cell_state_out, &_input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f));
429 // TODO(COMPMID-3395): Perform multiplication in the quantized domain in NEPixelWiseMultiplicationKernel
430 _memory_group.manage(&_hidden_mul_res);
431 const TensorInfo hidden_mul_res(_input_gate.info()->tensor_shape(), 1, DataType::S32);
432 _hidden_mul_res.allocator()->init(hidden_mul_res);
433 _pixelwise_mul_hidden.configure(&_output_gate, &_input_gate, &_hidden_mul_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
434 _output_gate.allocator()->allocate();
435 _input_gate.allocator()->allocate();
436 const float hidden_state_scale = std::pow(2, -15) / lstm_params.hidden_state_scale() * std::pow(2, -15);
Sang-Hoon Park30b46a62020-04-18 01:40:57 +0100437 quantization::calculate_quantized_multiplier(hidden_state_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift, /* ignore_epsilon */ true);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000438 gemmlowp_info.gemmlowp_offset = lstm_params.hidden_state_zero();
439 gemmlowp_info.output_data_type = output_state_in->info()->data_type();
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100440
441 _projection_tensor_copy_required = (num_units != output_size);
442 ITensor *hidden_gate_result = output_state_out;
443
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100444 _memory_group.manage(&_hidden_gate);
445
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100446 if(_projection_tensor_copy_required)
447 {
448 _hidden_gate.allocator()->init(*output_state_out->info());
449 _hidden_gate.info()->set_tensor_shape(_hidden_mul_res.info()->tensor_shape());
450 hidden_gate_result = &_hidden_gate;
451 }
452
453 _hidden_outstage.configure(&_hidden_mul_res, nullptr, hidden_gate_result, gemmlowp_info);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000454 _hidden_mul_res.allocator()->allocate();
455
456 // Projection.
457 if(_has_projection)
458 {
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100459 const TensorInfo projection_outstage_info(*output_state_out->info());
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000460 const UniformQuantizationInfo qprojection = _projection_weights->info()->quantization_info().uniform();
461 const float projection_scale = qprojection.scale * lstm_params.hidden_state_scale() / qoutput_state_in.scale;
462 gemmlowp_info.gemmlowp_offset = qoutput_state_in.offset;
463 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int8_t>::lowest();
464 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int8_t>::max();
465 gemmlowp_info.output_data_type = DataType::QASYMM8_SIGNED;
466
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100467 TensorInfo projection_mm_out_info{ mm_out_info };
468 projection_mm_out_info.set_tensor_shape(TensorShape(output_size, batch_size));
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100469
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000470 configure_mm(_mm_projection, _projection_outstage, gemmlowp_info,
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100471 hidden_gate_result, &_projection_weights_transposed, &_projection_eff_bias,
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000472 &_mm_projection_res, &_projection_outstage_res, projection_scale,
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100473 projection_mm_out_info, projection_outstage_info);
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100474
475 ITensor *accumulate_destination = output_state_out;
476
477 if(_projection_tensor_copy_required)
478 {
479 _hidden_gate.allocator()->allocate();
480 _projection_accumulate_res.allocator()->init(*output_state_out->info());
481 _projection_accumulate_res.info()->set_tensor_shape(_projection_outstage_res.info()->tensor_shape());
482 _projection_output_to_accumulate_copy.configure(*output_state_out, _projection_accumulate_res);
483 accumulate_destination = &_projection_accumulate_res;
484 }
485
486 _accumulate_projection.configure(&_projection_outstage_res, accumulate_destination, accumulate_destination, ConvertPolicy::SATURATE);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000487 _projection_outstage_res.allocator()->allocate();
488
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100489 if(_projection_tensor_copy_required)
490 {
491 _projection_accumulate_to_output_copy.configure(_projection_accumulate_res, *output_state_out);
492 _projection_accumulate_res.allocator()->allocate();
493 }
494
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000495 int8_t quantized_projection_clip{ 0 };
496 if(lstm_params.projection_clip() > 0.0f)
497 {
498 quantized_projection_clip = utility::clamp<int8_t>(lstm_params.projection_clip() / qprojection.scale, -128, 127);
499 }
500
501 if(quantized_projection_clip > 0)
502 {
503 _projection_clip.configure(output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_projection_clip, quantized_projection_clip));
504 _has_projection_clipping = true;
505 }
506 }
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100507 else
508 {
509 if(_projection_tensor_copy_required)
510 {
511 _hidden_to_output_copy.configure(_hidden_gate, *output_state_out);
512 _hidden_gate.allocator()->allocate();
513 }
514 }
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100515
516 // Copy output_state_out to output
517 _copy_output.configure(output_state_out, output);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000518}
519
520Status NEQLSTMLayer::validate(const ITensorInfo *input,
521 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
522 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
523 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
524 const ITensorInfo *cell_state_in, const ITensorInfo *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100525 const ITensorInfo *cell_state_out, const ITensorInfo *output_state_out, const ITensorInfo *output,
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000526 const LSTMParams<ITensorInfo> &lstm_params)
527{
528 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights, recurrent_to_forget_weights, recurrent_to_cell_weights,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100529 recurrent_to_output_weights, forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in,
530 cell_state_out, output_state_out, output);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000531
532 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED);
533 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() != 2, "Input must have exactly 2 dimensions");
534
535 const unsigned int input_size = input->dimension(0);
536 const unsigned int batch_size = input->dimension(1);
537 const unsigned int num_units = input_to_output_weights->dimension(1);
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100538 const unsigned int output_size = output_state_out->dimension(_out_state_output_size_dimension_idx);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000539
540 ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->num_dimensions() != 2);
541 ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->dimension(0) != input_size);
542 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_to_output_weights, input_to_forget_weights, input_to_cell_weights);
543 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->num_dimensions() != 2);
544 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->dimension(1) != num_units);
545 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(recurrent_to_output_weights, recurrent_to_forget_weights, recurrent_to_cell_weights);
546 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_to_forget_weights, 1, DataType::QSYMM8);
547 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
548 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights);
549
550 ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->num_dimensions() != 1);
551 ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->dimension(0) != num_units);
552 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(forget_gate_bias, cell_bias, output_gate_bias);
553 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(forget_gate_bias, 1, DataType::S32);
554 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(forget_gate_bias, cell_bias, output_gate_bias);
555
556 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->num_dimensions() != 2);
557 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->dimension(0) != num_units);
558 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->dimension(1) != batch_size);
559 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(cell_state_in, 1, DataType::QSYMM16);
560
561 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() != 2);
562 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->dimension(0) != output_size);
563 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->dimension(1) != batch_size);
564 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output_state_in);
565
566 // Check whether peephole weights are all there or none
567 if(lstm_params.has_peephole_opt())
568 {
569 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights());
570 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_forget_weights(), 1, DataType::QSYMM16);
571 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->num_dimensions() != 1);
572 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->dimension(0) != num_units);
573 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights());
574 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights());
575
576 if(!lstm_params.has_cifg_opt())
577 {
578 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_input_weights());
579 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_input_weights());
580 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_input_weights());
581 }
582 }
583
584 const UniformQuantizationInfo qinput = input->quantization_info().uniform();
585 const UniformQuantizationInfo qcell_state_in = cell_state_in->quantization_info().uniform();
586 const UniformQuantizationInfo qoutput_state_in = output_state_in->quantization_info().uniform();
587
588 // Calculate and decompose effective scales for optimizing matmul calculation
589 const int32_t cell_shift = log2(qcell_state_in.scale);
590 ARM_COMPUTE_RETURN_ERROR_ON(cell_shift > -9);
591
592 // Calculate quantized parameters for clipping.
593 int16_t quantized_cell_clip = 0;
594 if(lstm_params.cell_clip() > 0.0f)
595 {
596 quantized_cell_clip = quantize_qsymm16(lstm_params.cell_clip(), qcell_state_in);
597 }
598
599 // Precompute effective bias for optimizing the matmul computations.
600 const TensorInfo eff_bias_info(TensorShape(num_units), 1, DataType::S32);
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100601 const TensorInfo projection_eff_bias_info(TensorShape(output_size), 1, DataType::S32);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000602 if(!lstm_params.has_cifg_opt())
603 {
604 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(lstm_params.input_to_input_weights(), &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
605 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(lstm_params.recurrent_to_input_weights(), &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset,
606 true)));
607 }
608 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(input_to_forget_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
609 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(recurrent_to_forget_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true)));
610 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(input_to_cell_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
611 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(recurrent_to_cell_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true)));
612 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(input_to_output_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
613 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(recurrent_to_output_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true)));
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100614 if(lstm_params.has_projection())
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000615 {
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100616 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixAReductionKernel::validate(lstm_params.projection_weights(), &projection_eff_bias_info, GEMMLowpReductionKernelInfo(output_size, false,
617 lstm_params.hidden_state_zero(),
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000618 true)));
Michele Di Giorgio11c562c2020-06-10 16:34:50 +0100619 if(lstm_params.projection_bias() != nullptr)
620 {
621 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.projection_bias(), 1, DataType::S32);
622 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(lstm_params.projection_bias(), &projection_eff_bias_info, &projection_eff_bias_info, ConvertPolicy::SATURATE));
623 }
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000624 }
625
626 const TensorInfo input_weights_transposed(TensorShape(num_units, input_size), 1, input_to_forget_weights->data_type(), input_to_forget_weights->quantization_info());
627 const TensorInfo recurrent_weights_transposed(TensorShape(num_units, output_size), 1, recurrent_to_forget_weights->data_type(), recurrent_to_forget_weights->quantization_info());
628
629 // Validate weights transpose
630 ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(input_to_forget_weights, &input_weights_transposed));
631 ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(input_to_cell_weights, &input_weights_transposed));
632 ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(input_to_output_weights, &input_weights_transposed));
633 ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(recurrent_to_forget_weights, &recurrent_weights_transposed));
634 ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(recurrent_to_cell_weights, &recurrent_weights_transposed));
635 ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(recurrent_to_output_weights, &recurrent_weights_transposed));
636 if(!lstm_params.has_cifg_opt())
637 {
638 ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(lstm_params.input_to_input_weights(), &input_weights_transposed));
639 ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(lstm_params.recurrent_to_input_weights(), &recurrent_weights_transposed));
640 }
641 if(lstm_params.has_projection())
642 {
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100643 const TensorInfo projection_weights_transposed(TensorShape(output_size, num_units), 1, lstm_params.projection_weights()->data_type(), lstm_params.projection_weights()->quantization_info());
644 ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(lstm_params.projection_weights(), &projection_weights_transposed));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000645 }
646
647 GEMMLowpOutputStageInfo gemmlowp_info;
648 gemmlowp_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
649 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int16_t>::lowest();
650 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int16_t>::max();
651 gemmlowp_info.output_data_type = DataType::QSYMM16;
652
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100653 const bool has_layer_norm = lstm_params.use_layer_norm();
654
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000655 // Forget gate.
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100656 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.forget_intermediate_scale() == 0);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000657 const TensorInfo forget_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0));
658 const TensorInfo mm_out_info(TensorShape(num_units, batch_size), 1, DataType::S32);
659 const float input_to_forget_scale = input_to_forget_weights->quantization_info().uniform().scale * qinput.scale / lstm_params.forget_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100660 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, input_to_forget_scale, &mm_out_info, &forget_outstage_info));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000661
662 const float recurrent_to_forget_scale = recurrent_to_forget_weights->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.forget_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100663 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, output_state_in, &recurrent_weights_transposed, &eff_bias_info, recurrent_to_forget_scale, &mm_out_info, &forget_outstage_info));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000664
665 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&forget_outstage_info, &forget_outstage_info, &forget_outstage_info, ConvertPolicy::SATURATE));
666
667 if(lstm_params.has_peephole_opt())
668 {
669 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_forget_weights(), 1, DataType::QSYMM16);
670 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_forget_weights(), &mm_out_info, 1.f, ConvertPolicy::SATURATE,
671 RoundingPolicy::TO_ZERO));
672 const float cell_to_forget_scale = std::pow(2, cell_shift) * lstm_params.cell_to_forget_weights()->quantization_info().uniform().scale / lstm_params.forget_intermediate_scale();
673 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(cell_to_forget_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
674 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOutputStage::validate(&mm_out_info, nullptr, &forget_outstage_info, gemmlowp_info));
675 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&forget_outstage_info, &forget_outstage_info, &forget_outstage_info, ConvertPolicy::SATURATE));
676 }
677
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100678 if(has_layer_norm)
679 {
680 const ITensorInfo *w_info = lstm_params.forget_layer_norm_weights();
681 const ITensorInfo *b_info = forget_gate_bias;
682 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(forget_outstage_info, *w_info, *b_info));
683 }
684
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000685 // Output quantization info of Sigmoid and Tanh activations
686 const QuantizationInfo sigmoid_tanh_outqinfo(1.f / 32768.f, 0);
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100687 const TensorInfo forget_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000688
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000689 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(&forget_outstage_info, &forget_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
690
691 // Modulation gate.
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100692 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_intermediate_scale() == 0);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000693 const TensorInfo cell_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.cell_intermediate_scale(), 0));
694 const float input_to_cell_scale = input_to_cell_weights->quantization_info().uniform().scale * qinput.scale / lstm_params.cell_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100695 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, input_to_cell_scale, &mm_out_info, &cell_outstage_info));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000696
697 const float recurrent_to_cell_scale = recurrent_to_cell_weights->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.cell_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100698 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, output_state_in, &recurrent_weights_transposed, &eff_bias_info, recurrent_to_cell_scale, &mm_out_info, &cell_outstage_info));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000699
700 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&cell_outstage_info, &cell_outstage_info, &cell_outstage_info, ConvertPolicy::SATURATE));
701
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100702 if(has_layer_norm)
703 {
704 const ITensorInfo *w_info = lstm_params.cell_layer_norm_weights();
705 const ITensorInfo *b_info = cell_bias;
706 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(cell_outstage_info, *w_info, *b_info));
707 }
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000708 const TensorInfo cell_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100709
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000710 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(&cell_outstage_info, &cell_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)));
711
712 // Input gate.
713 const TensorInfo input_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
714 if(lstm_params.has_cifg_opt())
715 {
716 ARM_COMPUTE_RETURN_ERROR_ON_MSG(lstm_params.input_gate_bias() != nullptr, "Input gate bias must not be present when CIFG is used");
717 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticSubtractionKernel::validate(&input_gate_info, &forget_gate_info, &forget_gate_info, ConvertPolicy::SATURATE));
718 }
719 else
720 {
721 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights(), lstm_params.input_gate_bias());
722 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_to_forget_weights, lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights());
723 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_to_forget_weights, lstm_params.input_to_input_weights());
724 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(recurrent_to_forget_weights, lstm_params.recurrent_to_input_weights());
725 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(forget_gate_bias, lstm_params.input_gate_bias());
726 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(forget_gate_bias, lstm_params.input_gate_bias());
727
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100728 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_intermediate_scale() == 0);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000729 const TensorInfo input_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0));
730 const float input_to_input_scale = lstm_params.input_to_input_weights()->quantization_info().uniform().scale * qinput.scale / lstm_params.input_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100731 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, input_to_input_scale, &mm_out_info, &input_outstage_info));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000732
733 const float recurrent_to_input_scale = lstm_params.recurrent_to_input_weights()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.input_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100734 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, output_state_in, &recurrent_weights_transposed, &eff_bias_info, recurrent_to_input_scale, &mm_out_info, &input_outstage_info));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000735
736 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&input_outstage_info, &input_outstage_info, &input_outstage_info, ConvertPolicy::SATURATE));
737
738 if(lstm_params.has_peephole_opt())
739 {
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100740 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_input_weights(), &mm_out_info, 1.f, ConvertPolicy::SATURATE,
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000741 RoundingPolicy::TO_ZERO));
742 const float cell_to_input_scale = std::pow(2, cell_shift) * lstm_params.cell_to_input_weights()->quantization_info().uniform().scale / lstm_params.input_intermediate_scale();
743 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(cell_to_input_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100744 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOutputStage::validate(&mm_out_info, &eff_bias_info, &input_outstage_info, gemmlowp_info));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000745 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&input_outstage_info, &input_outstage_info, &input_outstage_info, ConvertPolicy::SATURATE));
746 }
747
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100748 if(has_layer_norm)
749 {
750 const ITensorInfo *w_info = lstm_params.input_layer_norm_weights();
751 const ITensorInfo *b_info = lstm_params.input_gate_bias();
752 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(input_outstage_info, *w_info, *b_info));
753 }
754
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100755 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(&input_outstage_info, &input_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000756 }
757 // Cell.
758 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&forget_gate_info, cell_state_in, &forget_gate_info, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
759 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&input_gate_info, cell_state_in, &cell_gate_info, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
760 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&forget_gate_info, &cell_gate_info, cell_state_out, ConvertPolicy::SATURATE));
761 if(quantized_cell_clip > 0)
762 {
763 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(cell_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_cell_clip,
764 quantized_cell_clip)));
765 }
766 // Output gate.
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100767 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.output_intermediate_scale() == 0);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000768 const TensorInfo output_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.output_intermediate_scale(), 0));
769 const float input_to_output_scale = input_to_output_weights->quantization_info().uniform().scale * qinput.scale / lstm_params.output_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100770 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, input_to_output_scale, &mm_out_info, &output_outstage_info));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000771
772 const float recurrent_to_output_scale = recurrent_to_output_weights->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.output_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100773 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, output_state_in, &recurrent_weights_transposed, &eff_bias_info, recurrent_to_output_scale, &mm_out_info, &output_outstage_info));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000774
775 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&output_outstage_info, &output_outstage_info, &output_outstage_info, ConvertPolicy::SATURATE));
776 if(lstm_params.has_peephole_opt())
777 {
778 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_output_weights(), 1, DataType::QSYMM16);
779 // TODO(COMPMID-3395): Perform multiplication in the quantized domain in NEPixelWiseMultiplicationKernel
780 // Here we are not using the output stage because all operations are done in float
781 // const float cell_to_output_scale = std::pow(2, cell_shift) * lstm_params.cell_to_output_weights()->quantization_info().uniform().scale / lstm_params.output_intermediate_scale();
782 // ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(cell_to_output_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
783 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(cell_state_out, lstm_params.cell_to_output_weights(), &output_outstage_info, 1.f, ConvertPolicy::SATURATE,
784 RoundingPolicy::TO_ZERO));
785 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(&output_outstage_info, &output_outstage_info, &output_outstage_info, ConvertPolicy::SATURATE));
786 }
787
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100788 if(has_layer_norm)
789 {
790 const ITensorInfo *w_info = lstm_params.output_layer_norm_weights();
791 const ITensorInfo *b_info = output_gate_bias;
792 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(output_outstage_info, *w_info, *b_info));
793 }
794
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000795 const TensorInfo output_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
796 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(&output_outstage_info, &output_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
797
798 // Hidden.
799 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(cell_state_out, &input_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)));
800 const TensorInfo hidden_mul_res(TensorShape(num_units, batch_size), 1, DataType::S32);
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100801 const TensorInfo hidden_out_info(TensorShape(num_units, batch_size), 1, DataType::QASYMM8_SIGNED);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000802 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&output_gate_info, &input_gate_info, &hidden_mul_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100803
804 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.hidden_state_scale() == 0);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000805 const float hidden_state_scale = std::pow(2, -15) / lstm_params.hidden_state_scale() * std::pow(2, -15);
Sang-Hoon Park30b46a62020-04-18 01:40:57 +0100806 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(hidden_state_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift, /* ignore_epsilon */ true));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000807 gemmlowp_info.gemmlowp_offset = lstm_params.hidden_state_zero();
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100808 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpOutputStage::validate(&hidden_mul_res, nullptr, &hidden_out_info, gemmlowp_info));
809
810 const bool projection_tensor_copy_required = num_units != output_size;
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000811
812 // Projection.
813 if(lstm_params.has_projection())
814 {
815 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(recurrent_to_forget_weights, lstm_params.projection_weights());
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100816 ARM_COMPUTE_RETURN_ERROR_ON(qoutput_state_in.scale == 0);
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000817
818 const UniformQuantizationInfo qprojection = lstm_params.projection_weights()->quantization_info().uniform();
819 const float projection_scale = qprojection.scale * lstm_params.hidden_state_scale() / qoutput_state_in.scale;
820 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(projection_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
821 gemmlowp_info.gemmlowp_offset = qoutput_state_in.offset;
822 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int8_t>::lowest();
823 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int8_t>::max();
824 gemmlowp_info.output_data_type = DataType::QASYMM8_SIGNED;
825
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100826 const TensorInfo projection_outstage_info(*output_state_out);
827 const TensorInfo projection_weights_transposed(TensorShape(output_size, num_units), 1, lstm_params.projection_weights()->data_type(), lstm_params.projection_weights()->quantization_info());
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100828
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100829 TensorInfo projection_mm_out_info{ mm_out_info };
830 projection_mm_out_info.set_tensor_shape(TensorShape(output_size, batch_size));
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100831
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100832 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, &hidden_out_info, &projection_weights_transposed, &projection_eff_bias_info, projection_scale, &projection_mm_out_info,
833 &projection_outstage_info));
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100834
835 if(projection_tensor_copy_required)
836 {
837 ARM_COMPUTE_RETURN_ON_ERROR(NEQLSTMLayer::TensorCopyKernel::validate(*output_state_out, projection_outstage_info));
838 }
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000839
840 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(output_state_out, output_state_out, output_state_out, ConvertPolicy::SATURATE));
841
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100842 if(projection_tensor_copy_required)
843 {
844 ARM_COMPUTE_RETURN_ON_ERROR(NEQLSTMLayer::TensorCopyKernel::validate(projection_outstage_info, *output_state_out));
845 }
846
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000847 int8_t quantized_projection_clip{ 0 };
848 if(lstm_params.projection_clip() > 0.0f)
849 {
850 quantized_projection_clip = quantize_qasymm8_signed(lstm_params.projection_clip(), qprojection);
851 }
852
853 if(quantized_projection_clip > 0)
854 {
855 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_projection_clip,
856 quantized_projection_clip)));
857 }
858 }
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100859 else
860 {
861 if(projection_tensor_copy_required)
862 {
863 ARM_COMPUTE_RETURN_ON_ERROR(NEQLSTMLayer::TensorCopyKernel::validate(hidden_out_info, *output_state_out));
864 }
865 }
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000866
867 if(cell_state_out->total_size() > 0)
868 {
869 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(cell_state_in, cell_state_out);
870 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(cell_state_in, cell_state_out);
871 }
872
873 if(output_state_out->total_size() > 0)
874 {
875 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output_state_out);
876 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output_state_in, output_state_out);
877 }
878
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100879 ARM_COMPUTE_RETURN_ON_ERROR(NECopyKernel::validate(output_state_out, output));
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000880 return Status{};
881}
882
883void NEQLSTMLayer::run()
884{
885 prepare();
886
887 // Acquire all the temporaries
888 MemoryGroupResourceScope scope_mg(_memory_group);
889
890 // Forget gate.
891 _mm_input_to_forget.run();
892 _input_to_forget_outstage.run();
893
894 _mm_recurrent_to_forget.run();
895 _recurrent_to_forget_outstage.run();
896 NEScheduler::get().schedule(&_accumulate_input_recurrent_forget, Window::DimY);
897
898 if(_has_peephole)
899 {
900 NEScheduler::get().schedule(&_pixelwise_mul_cell_to_forget, Window::DimY);
901 _cell_to_forget_outstage.run();
902 NEScheduler::get().schedule(&_accumulate_cell_forget, Window::DimY);
903 }
904
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100905 if(_has_layer_norm)
906 {
907 NEScheduler::get().schedule(&get_layer_norm(LayerNormGate::Forget), Window::DimY);
908 }
909
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000910 _forget_gate_sigmoid.run();
911
912 // Modulation gate.
913 _mm_input_to_cell.run();
914 _input_to_cell_outstage.run();
915
916 _mm_recurrent_to_cell.run();
917 _recurrent_to_cell_outstage.run();
918 NEScheduler::get().schedule(&_accumulate_input_recurrent_modulation, Window::DimY);
919
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100920 if(_has_layer_norm)
921 {
922 NEScheduler::get().schedule(&get_layer_norm(LayerNormGate::Cell), Window::DimY);
923 }
924
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000925 _cell_gate_tanh.run();
926
927 // Input gate
928 if(_has_cifg)
929 {
930 NEScheduler::get().schedule(&_input_gate_sub, Window::DimY);
931 }
932 else
933 {
934 _mm_input_to_input.run();
935 _input_to_input_outstage.run();
936 _mm_recurrent_to_input.run();
937 _recurrent_to_input_outstage.run();
938 NEScheduler::get().schedule(&_accumulate_input_recurrent_input, Window::DimY);
939
940 if(_has_peephole)
941 {
942 NEScheduler::get().schedule(&_pixelwise_mul_cell_to_input, Window::DimY);
943 _cell_to_input_outstage.run();
944 NEScheduler::get().schedule(&_accumulate_cell_input, Window::DimY);
945 }
946
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100947 if(_has_layer_norm)
948 {
949 NEScheduler::get().schedule(&get_layer_norm(LayerNormGate::Input), Window::DimY);
950 }
951
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100952 _input_gate_sigmoid.run();
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000953 }
954
955 // Cell.
956 NEScheduler::get().schedule(&_pixelwise_mul_forget_cell, Window::DimY);
957 NEScheduler::get().schedule(&_pixelwise_mul_input_cell, Window::DimY);
958 NEScheduler::get().schedule(&_add_forget_cell, Window::DimY);
959 if(_has_cell_clipping)
960 {
961 _cell_clip.run();
962 }
963
964 // Output gate.
965 _mm_input_to_output.run();
966 _input_to_output_outstage.run();
967 _mm_recurrent_to_output.run();
968 _recurrent_to_output_outstage.run();
969 NEScheduler::get().schedule(&_accumulate_input_recurrent_output, Window::DimY);
970 if(_has_peephole)
971 {
972 NEScheduler::get().schedule(&_pixelwise_mul_cell_to_output, Window::DimY);
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100973 _cell_to_output_outstage.run();
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000974 NEScheduler::get().schedule(&_accumulate_cell_to_output, Window::DimY);
975 }
976
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100977 if(_has_layer_norm)
978 {
979 NEScheduler::get().schedule(&get_layer_norm(LayerNormGate::Output), Window::DimY);
980 }
981
Michele Di Giorgio47a89902020-03-09 19:32:33 +0000982 _output_gate_sigmoid.run();
983
984 // Hidden.
985 _hidden_tanh.run();
986 NEScheduler::get().schedule(&_pixelwise_mul_hidden, Window::DimY);
987 _hidden_outstage.run();
988
989 // Projection.
990 if(_has_projection)
991 {
992 _mm_projection.run();
993 _projection_outstage.run();
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +0100994
995 if(_projection_tensor_copy_required)
996 {
997 _projection_output_to_accumulate_copy.run();
998 }
999
Michele Di Giorgio47a89902020-03-09 19:32:33 +00001000 NEScheduler::get().schedule(&_accumulate_projection, Window::DimY);
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +01001001
1002 if(_projection_tensor_copy_required)
1003 {
1004 _projection_accumulate_to_output_copy.run();
1005 }
1006
Michele Di Giorgio47a89902020-03-09 19:32:33 +00001007 if(_has_projection_clipping)
1008 {
1009 _projection_clip.run();
1010 }
1011 }
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +01001012 else
1013 {
1014 if(_projection_tensor_copy_required)
1015 {
1016 _hidden_to_output_copy.run();
1017 }
1018 }
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +01001019
1020 // Copy output_state_out to output
1021 NEScheduler::get().schedule(&_copy_output, Window::DimY);
Michele Di Giorgio47a89902020-03-09 19:32:33 +00001022}
1023
1024void NEQLSTMLayer::prepare()
1025{
1026 if(!_is_prepared)
1027 {
1028 // Pre-transpose weights to be used in GEMM.
1029 _input_to_forget_weights_transposed.allocator()->allocate();
1030 _input_to_cell_weights_transposed.allocator()->allocate();
1031 _input_to_output_weights_transposed.allocator()->allocate();
1032 _recurrent_to_forget_weights_transposed.allocator()->allocate();
1033 _recurrent_to_cell_weights_transposed.allocator()->allocate();
1034 _recurrent_to_output_weights_transposed.allocator()->allocate();
1035 _transpose_input_to_forget_weights.run();
1036 _transpose_input_to_cell_weights.run();
1037 _transpose_input_to_output_weights.run();
1038 _transpose_recurrent_to_forget_weights.run();
1039 _transpose_recurrent_to_cell_weights.run();
1040 _transpose_recurrent_to_output_weights.run();
1041
1042 // Precompute effective biases
1043 if(_has_cifg)
1044 {
1045 std::fill_n(reinterpret_cast<int16_t *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 32767);
1046 }
1047 else
1048 {
1049 _input_to_input_eff_bias.allocator()->allocate();
1050 _recurrent_to_input_eff_bias.allocator()->allocate();
1051 NEScheduler::get().schedule(&_input_to_input_reduction, Window::DimY);
1052 NEScheduler::get().schedule(&_recurrent_to_input_reduction, Window::DimY);
1053
1054 _input_to_input_weights_transposed.allocator()->allocate();
1055 _recurrent_to_input_weights_transposed.allocator()->allocate();
1056 _transpose_input_to_input_weights.run();
1057 _transpose_recurrent_to_input_weights.run();
1058 _input_to_input_weights->mark_as_unused();
1059 _recurrent_to_input_weights->mark_as_unused();
1060 }
1061 _input_to_forget_eff_bias.allocator()->allocate();
1062 _recurrent_to_forget_eff_bias.allocator()->allocate();
1063 _input_to_cell_eff_bias.allocator()->allocate();
1064 _recurrent_to_cell_eff_bias.allocator()->allocate();
1065 _input_to_output_eff_bias.allocator()->allocate();
1066 _recurrent_to_output_eff_bias.allocator()->allocate();
1067 NEScheduler::get().schedule(&_input_to_forget_reduction, Window::DimY);
1068 NEScheduler::get().schedule(&_recurrent_to_forget_reduction, Window::DimY);
1069 NEScheduler::get().schedule(&_input_to_cell_reduction, Window::DimY);
1070 NEScheduler::get().schedule(&_recurrent_to_cell_reduction, Window::DimY);
1071 NEScheduler::get().schedule(&_input_to_output_reduction, Window::DimY);
1072 NEScheduler::get().schedule(&_recurrent_to_output_reduction, Window::DimY);
1073
1074 if(_has_projection)
1075 {
Michele Di Giorgio11c562c2020-06-10 16:34:50 +01001076 _projection_eff_bias.allocator()->allocate();
1077 NEScheduler::get().schedule(&_projection_reduction, Window::DimY);
Michele Di Giorgio47a89902020-03-09 19:32:33 +00001078 if(_projection_bias != nullptr)
1079 {
Michele Di Giorgio11c562c2020-06-10 16:34:50 +01001080 NEScheduler::get().schedule(&_projection_bias_add, Window::DimY);
Michele Di Giorgio47a89902020-03-09 19:32:33 +00001081 _projection_bias->mark_as_unused();
1082 }
1083
1084 _projection_weights_transposed.allocator()->allocate();
1085 _transpose_projection_weights.run();
1086 _projection_weights->mark_as_unused();
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +01001087
1088 if(!_projection_tensor_copy_required)
1089 {
1090 _hidden_gate.mark_as_unused();
Sang-Hoon Parkd5c020a2020-05-06 21:01:19 +01001091 _projection_accumulate_res.mark_as_unused();
1092 }
Michele Di Giorgio47a89902020-03-09 19:32:33 +00001093 }
1094
1095 // Mark weights as unused
1096 _input_to_forget_weights->mark_as_unused();
1097 _input_to_cell_weights->mark_as_unused();
1098 _input_to_output_weights->mark_as_unused();
1099 _recurrent_to_forget_weights->mark_as_unused();
1100 _recurrent_to_cell_weights->mark_as_unused();
1101 _recurrent_to_output_weights->mark_as_unused();
1102
1103 _is_prepared = true;
1104 }
1105}
1106
1107} // namespace arm_compute