blob: 5c0f19a15cc27291f155c01246718a0763902191 [file] [log] [blame]
Michalis Spyrouba27e442019-05-28 10:04:57 +01001/*
Teresa Charlin562bee52021-04-13 17:44:15 +01002 * Copyright (c) 2019-2021 Arm Limited.
Michalis Spyrouba27e442019-05-28 10:04:57 +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 */
24#include "arm_compute/runtime/NEON/functions/NELSTMLayerQuantized.h"
25
26#include "arm_compute/core/Utils.h"
27#include "arm_compute/core/Validate.h"
28#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010029#include "src/core/NEON/kernels/NEConvertQuantizedSignednessKernel.h"
30#include "src/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
31#include "src/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h"
32#include "src/core/NEON/kernels/NEGEMMLowpOffsetContributionKernel.h"
33#include "src/core/NEON/kernels/NEGEMMLowpOffsetContributionOutputStageKernel.h"
34#include "src/core/NEON/kernels/NEGEMMLowpReductionKernel.h"
35#include "src/core/NEON/kernels/NEGEMMMatrixAdditionKernel.h"
36#include "src/core/NEON/kernels/NEGEMMMatrixMultiplyKernel.h"
37#include "src/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010038#include "src/core/helpers/AutoConfiguration.h"
Michalis Spyrouba27e442019-05-28 10:04:57 +010039
40#include <cmath>
41#include <memory>
42#include <tuple>
43
44namespace arm_compute
45{
46namespace
47{
48// Quantization info structures used in the LSTMQuantize layer
49const QuantizationInfo qasymm(1.f / 128.f, 128);
50const QuantizationInfo qsymm_3(8.f / 32768.f, 0); // qsymm16 with 3 integer bit
51const QuantizationInfo qsymm_4(16.f / 32768.f, 0); // qsymm16 with 4 integer bit
52const QuantizationInfo qsymm_0(1.f / 32768.f, 0); // qsymm16 with 0 integer bit
53} // namespace
Michalis Spyrouebcebf12020-10-21 00:04:14 +010054NELSTMLayerQuantized::~NELSTMLayerQuantized() = default;
Michalis Spyrouba27e442019-05-28 10:04:57 +010055
56NELSTMLayerQuantized::NELSTMLayerQuantized(std::shared_ptr<IMemoryManager> memory_manager)
57 : _memory_group(std::move(memory_manager)), _gemmlowp(), _output_stage(), _transpose_weights(), _concat_input_weights(), _concat_recurrent_weights(), _concat_weights(), _concat_inputs(),
58 _concat_bias(), _sigmoid_forget_gate(), _sigmoid_input_gate(), _sigmoid_output_gate(), _tanh_modulation_gate(), _tanh_output_state(), _add1(), _add2(), _mul1(), _mul2(), _mul3(),
59 _slice_input_tensor(), _slice_forget_tensor(), _slice_cell_tensor(), _slice_output_tensor(), _dequantize(), _quantize(), _input_to_input_weights(nullptr), _input_to_forget_weights(nullptr),
60 _input_to_cell_weights(nullptr), _input_to_output_weights(nullptr), _recurrent_to_input_weights(nullptr), _recurrent_to_forget_weights(nullptr), _recurrent_to_cell_weights(nullptr),
61 _recurrent_to_output_weights(nullptr), _input_gate_bias(nullptr), _forget_gate_bias(nullptr), _cell_bias(nullptr), _output_gate_bias(nullptr), _recurrent_weights(), _input_weights(), _weights(),
62 _input(), _weights_transposed(), _output_highp(), _output_lowp(), _bias(), _forget_gate_input(), _input_gate_input(), _output_gate_input(), _input_modulation_gate_input(), _forget_gate_output(),
63 _input_gate_output(), _output_gate_output(), _input_modulation_gate_output(), _cell_state1(), _cell_state2(), _output_state_tmp(), _output_state_out_symm(), _output_state_out_f32(),
64 _is_prepared(false)
65{
66}
67
68void NELSTMLayerQuantized::configure(const ITensor *input,
69 const ITensor *input_to_input_weights, const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights,
70 const ITensor *recurrent_to_input_weights, const ITensor *recurrent_to_forget_weights, const ITensor *recurrent_to_cell_weights, const ITensor *recurrent_to_output_weights,
71 const ITensor *input_gate_bias, const ITensor *forget_gate_bias, const ITensor *cell_bias, const ITensor *output_gate_bias,
72 ITensor *cell_state_in, const ITensor *output_state_in,
73 ITensor *cell_state_out, ITensor *output_state_out)
74{
75 ARM_COMPUTE_ERROR_ON_NULLPTR(input, input_to_input_weights, input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
76 recurrent_to_input_weights, recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
77 input_gate_bias, forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in, cell_state_out, output_state_out);
78
79 ARM_COMPUTE_ERROR_THROW_ON(NELSTMLayerQuantized::validate(input->info(), input_to_input_weights->info(), input_to_forget_weights->info(), input_to_cell_weights->info(),
80 input_to_output_weights->info(),
81 recurrent_to_input_weights->info(), recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(),
82 input_gate_bias->info(), forget_gate_bias->info(), cell_bias->info(), output_gate_bias->info(), cell_state_in->info(), output_state_in->info(), cell_state_out->info(), output_state_out->info()));
83
84 const int input_size = input->info()->dimension(0);
85 const int batch_size = input->info()->dimension(1);
86 const int output_size = input_to_input_weights->info()->dimension(1);
87
88 const QuantizationInfo qweights = input_to_input_weights->info()->quantization_info(); // Weights quantization
89
90 auto_init_if_empty(*cell_state_out->info(), TensorInfo(TensorShape(batch_size, output_size), 1, DataType::QSYMM16, qsymm_4));
91 auto_init_if_empty(*output_state_out->info(), TensorInfo(TensorShape(batch_size, output_size), 1, DataType::QASYMM8, qasymm));
92
93 _input_to_input_weights = input_to_input_weights;
94 _input_to_forget_weights = input_to_forget_weights;
95 _input_to_cell_weights = input_to_cell_weights;
96 _input_to_output_weights = input_to_output_weights;
97 _recurrent_to_input_weights = recurrent_to_input_weights;
98 _recurrent_to_forget_weights = recurrent_to_forget_weights;
99 _recurrent_to_cell_weights = recurrent_to_cell_weights;
100 _recurrent_to_output_weights = recurrent_to_output_weights;
101 _input_gate_bias = input_gate_bias;
102 _forget_gate_bias = forget_gate_bias;
103 _cell_bias = cell_bias;
104 _output_gate_bias = output_gate_bias;
105
106 // Weights concatenation
107 std::vector<const ITensor *> inputs_weights_vector{ input_to_input_weights, input_to_forget_weights, input_to_cell_weights, input_to_output_weights };
108 std::vector<const ITensor *> recurrent_weights_vector{ recurrent_to_input_weights, recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights };
109
110 _input_weights.allocator()->init(TensorInfo(TensorShape(input_size, 4 * output_size), 1, DataType::QASYMM8, qweights));
111 _concat_input_weights.configure(inputs_weights_vector, &_input_weights, Window::DimY);
112
113 _recurrent_weights.allocator()->init(TensorInfo(TensorShape(output_size, 4 * output_size), 1, DataType::QASYMM8, qweights));
114 _concat_recurrent_weights.configure(recurrent_weights_vector, &_recurrent_weights, Window::DimY);
115
116 std::vector<const ITensor *> weights_vector{ &_recurrent_weights, &_input_weights };
117 _weights.allocator()->init(TensorInfo(TensorShape(output_size + input_size, 4 * output_size), 1, DataType::QASYMM8, qweights));
118 _concat_weights.configure(weights_vector, &_weights, Window::DimX);
119 _transpose_weights.configure(&_weights, &_weights_transposed);
120
121 // Input concatenation
122 std::vector<const ITensor *> input_vector{ input, output_state_in };
123 _memory_group.manage(&_input);
124 _input.allocator()->init(TensorInfo(TensorShape(output_size + input_size, batch_size), 1, DataType::QASYMM8, qasymm));
125 _concat_inputs.configure(input_vector, &_input, Window::DimX);
126
127 // Bias concatenation
128 std::vector<const ITensor *> bias_vector{ input_gate_bias, forget_gate_bias, cell_bias, output_gate_bias };
129 _bias.allocator()->init(TensorInfo(TensorShape(4 * output_size), 1, DataType::S32));
130 _concat_bias.configure(bias_vector, &_bias, Window::DimX);
131
132 // Invert the offset for gemmlowp
133 _input.info()->set_quantization_info(QuantizationInfo(qasymm.uniform().scale, -qasymm.uniform().offset));
134 _weights_transposed.info()->set_quantization_info(QuantizationInfo(qweights.uniform().scale, -qweights.uniform().offset));
135
136 // Run gemmlowp
137 _memory_group.manage(&_output_highp);
138 _output_highp.allocator()->init(TensorInfo(TensorShape(4 * output_size, batch_size), 1, DataType::S32));
139 _gemmlowp.configure(&_input, &_weights_transposed, nullptr, &_output_highp);
140 _input.allocator()->allocate();
141
142 // Set the offset back
143 _input.info()->set_quantization_info(QuantizationInfo(qasymm.uniform().scale, qasymm.uniform().offset));
144 _weights_transposed.info()->set_quantization_info(QuantizationInfo(qweights.uniform().scale, qweights.uniform().offset));
145
146 // multiplier = (input_scale * weights_scale) / output_scale (2 ^ (-12))
147 _output_lowp.allocator()->init(TensorInfo(_output_highp.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_3));
148
149 const float multiplier = 4096.f * qasymm.uniform().scale * qweights.uniform().scale;
Michalis Spyroue7be8a02019-12-12 16:16:09 +0000150 int32_t output_multiplier = 0;
151 int32_t output_shift = 0;
Manuel Bottini07263982019-10-17 18:37:26 +0100152 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
Michalis Spyrouba27e442019-05-28 10:04:57 +0100153
154 _memory_group.manage(&_output_lowp);
155 _output_stage.configure(&_output_highp, &_bias, &_output_lowp, output_multiplier, output_shift);
156 _output_highp.allocator()->allocate();
157 _bias.allocator()->allocate();
158
159 // Get the gate tensors
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100160 if(batch_size > 1)
161 {
162 _memory_group.manage(&_input_gate_input);
163 _slice_input_tensor.configure(&_output_lowp, &_input_gate_input, { 0, 0 }, { output_size, batch_size });
164 _memory_group.manage(&_forget_gate_input);
165 _slice_forget_tensor.configure(&_output_lowp, &_forget_gate_input, { output_size, 0 }, { 2 * output_size, batch_size });
166 _memory_group.manage(&_input_modulation_gate_input);
167 _slice_cell_tensor.configure(&_output_lowp, &_input_modulation_gate_input, { 2 * output_size, 0 }, { 3 * output_size, batch_size });
168 _memory_group.manage(&_output_gate_input);
169 _slice_output_tensor.configure(&_output_lowp, &_output_gate_input, { 3 * output_size, 0 }, { 4 * output_size, batch_size });
170 _output_lowp.allocator()->allocate();
171 }
172 else
173 {
174 _memory_group.manage(&_input_gate_input);
175 _slice_input_tensor.configure(&_output_lowp, &_input_gate_input, { 0 }, { output_size });
176 _memory_group.manage(&_forget_gate_input);
177 _slice_forget_tensor.configure(&_output_lowp, &_forget_gate_input, { output_size }, { 2 * output_size });
178 _memory_group.manage(&_input_modulation_gate_input);
179 _slice_cell_tensor.configure(&_output_lowp, &_input_modulation_gate_input, { 2 * output_size }, { 3 * output_size });
180 _memory_group.manage(&_output_gate_input);
181 _slice_output_tensor.configure(&_output_lowp, &_output_gate_input, { 3 * output_size }, { 4 * output_size });
182 _output_lowp.allocator()->allocate();
183 }
Michalis Spyrouba27e442019-05-28 10:04:57 +0100184
185 // Forget gate
186 _memory_group.manage(&_forget_gate_output);
187 _forget_gate_output.allocator()->init(TensorInfo(_forget_gate_input.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
188 _sigmoid_forget_gate.configure(&_forget_gate_input, &_forget_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
189 _forget_gate_input.allocator()->allocate();
190
191 // Input gate
192 _memory_group.manage(&_input_gate_output);
193 _input_gate_output.allocator()->init(TensorInfo(_input_gate_input.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
194 _sigmoid_input_gate.configure(&_input_gate_input, &_input_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
195 _input_gate_input.allocator()->allocate();
196
197 // Input modulation gate equation
198 _memory_group.manage(&_input_modulation_gate_output);
199 _input_modulation_gate_output.allocator()->init(TensorInfo(_input_modulation_gate_input.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
200 _tanh_modulation_gate.configure(&_input_modulation_gate_input, &_input_modulation_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f));
201 _input_modulation_gate_input.allocator()->allocate();
202
203 // Output gate
204 _memory_group.manage(&_output_gate_output);
205 _output_gate_output.allocator()->init(TensorInfo(_output_gate_input.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
206 _sigmoid_output_gate.configure(&_output_gate_input, &_output_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
207 _output_gate_input.allocator()->allocate();
208
209 // Long term memory
210 _memory_group.manage(&_cell_state1);
211 _cell_state1.allocator()->init(TensorInfo(_forget_gate_output.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_4));
212 _mul1.configure(&_forget_gate_output, cell_state_in, &_cell_state1, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
213 _forget_gate_output.allocator()->allocate();
214
215 _memory_group.manage(&_cell_state2);
216 _cell_state2.allocator()->init(TensorInfo(_input_gate_output.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_4));
217 _mul2.configure(&_input_gate_output, &_input_modulation_gate_output, &_cell_state2, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
218 _input_modulation_gate_output.allocator()->allocate();
219 _input_gate_output.allocator()->allocate();
220
221 _add1.configure(&_cell_state1, &_cell_state2, cell_state_out, ConvertPolicy::SATURATE);
222 _cell_state1.allocator()->allocate();
223 _cell_state2.allocator()->allocate();
224
225 // Short term memory
226 _memory_group.manage(&_output_state_tmp);
227 _output_state_tmp.allocator()->init(TensorInfo(cell_state_out->info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
228 _tanh_output_state.configure(cell_state_out, &_output_state_tmp, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f));
229
230 _memory_group.manage(&_output_state_out_symm);
231 _output_state_out_symm.allocator()->init(TensorInfo(_output_gate_output.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
232 _mul3.configure(&_output_state_tmp, &_output_gate_output, &_output_state_out_symm, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
233 _output_gate_output.allocator()->allocate();
234 _output_state_tmp.allocator()->allocate();
235
236 // Requantize the output state from QSYMM16 to QASYMM8
237 _memory_group.manage(&_output_state_out_f32);
238 _output_state_out_f32.allocator()->init(TensorInfo(_output_state_out_symm.info()->tensor_shape(), 1, DataType::F32));
239 _dequantize.configure(&_output_state_out_symm, &_output_state_out_f32);
240 _output_state_out_symm.allocator()->allocate();
241
242 _quantize.configure(&_output_state_out_f32, output_state_out);
243 _output_state_out_f32.allocator()->allocate();
244}
245
246Status NELSTMLayerQuantized::validate(const ITensorInfo *input,
247 const ITensorInfo *input_to_input_weights, const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
248 const ITensorInfo *recurrent_to_input_weights, const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
249 const ITensorInfo *input_gate_bias, const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
250 const ITensorInfo *cell_state_in, const ITensorInfo *output_state_in,
251 const ITensorInfo *cell_state_out, const ITensorInfo *output_state_out)
252{
253 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, input_to_input_weights, input_to_forget_weights, input_to_cell_weights, input_to_output_weights, recurrent_to_input_weights,
254 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights, input_gate_bias, forget_gate_bias, cell_bias, output_gate_bias, cell_state_in,
255 output_state_in, cell_state_out, output_state_out);
256
257 const int input_size = input->dimension(0);
258 const int batch_size = input->dimension(1);
259 const int output_size = input_to_input_weights->dimension(1);
260
261 // Dimensionality checks
262 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2);
263 ARM_COMPUTE_RETURN_ERROR_ON(input_to_input_weights->num_dimensions() > 2);
264 ARM_COMPUTE_RETURN_ERROR_ON(input_gate_bias->num_dimensions() > 1);
265 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() > 2);
266
267 TensorInfo input_weights_info(input_to_input_weights->clone()->set_tensor_shape(TensorShape(input_size, output_size)).set_data_type(DataType::QASYMM8));
Manuel Bottini10c53f12019-07-17 16:11:53 +0100268 TensorInfo recurrent_weights_info(input_to_input_weights->clone()->set_tensor_shape(TensorShape(output_size, output_size)).set_data_type(DataType::QASYMM8));
Michalis Spyrouba27e442019-05-28 10:04:57 +0100269 TensorInfo bias_info(input_gate_bias->clone()->set_tensor_shape(TensorShape(output_size)).set_data_type(DataType::S32));
270 TensorInfo output_state_info(cell_state_in->clone()->set_tensor_shape(TensorShape(output_size, batch_size)).set_data_type(DataType::QASYMM8).set_quantization_info(qasymm));
271 TensorInfo cell_state_info(cell_state_in->clone()->set_tensor_shape(TensorShape(output_size, batch_size)).set_data_type(DataType::QSYMM16).set_quantization_info(qsymm_4));
272
273 // Shape checks
274 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&input_weights_info, input_to_input_weights, input_to_forget_weights, input_to_cell_weights, input_to_output_weights);
275 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&recurrent_weights_info, recurrent_to_input_weights, recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights);
276 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&bias_info, input_gate_bias, forget_gate_bias, cell_bias, output_gate_bias);
277 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&cell_state_info, cell_state_in);
278 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&output_state_info, output_state_in);
279
280 // Data type checks
281 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input_weights_info, input, input_to_input_weights, input_to_forget_weights, input_to_cell_weights, input_to_output_weights);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100282 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(recurrent_to_input_weights, recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights);
Michalis Spyrouba27e442019-05-28 10:04:57 +0100283 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&bias_info, input_gate_bias, forget_gate_bias, cell_bias, output_gate_bias);
284 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&cell_state_info, cell_state_in);
285 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&output_state_info, output_state_in);
286
287 // Quantization checks
Manuel Bottini10c53f12019-07-17 16:11:53 +0100288 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&input_weights_info, input_to_input_weights, input_to_forget_weights, input_to_cell_weights, input_to_output_weights);
289 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(recurrent_to_input_weights, recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights);
Michalis Spyrouba27e442019-05-28 10:04:57 +0100290 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&cell_state_info, cell_state_in);
291 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&output_state_info, output_state_in);
292
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100293 // Validate internal functions
294 // _concat_input_weights
295 std::vector<const ITensorInfo *> inputs_weights_vector;
296 inputs_weights_vector.emplace_back(input_to_input_weights);
297 inputs_weights_vector.emplace_back(input_to_forget_weights);
298 inputs_weights_vector.emplace_back(input_to_cell_weights);
299 inputs_weights_vector.emplace_back(input_to_output_weights);
300 const QuantizationInfo qweights = input_to_input_weights->quantization_info(); // Weights quantization
301 const TensorInfo input_weights(TensorShape(input_size, 4 * output_size), 1, DataType::QASYMM8, qweights);
302 ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate(inputs_weights_vector, &input_weights, Window::DimY));
303
304 // _concat_recurrent_weights
305 std::vector<const ITensorInfo *> recurrent_weights_vector;
306 recurrent_weights_vector.emplace_back(recurrent_to_input_weights);
307 recurrent_weights_vector.emplace_back(recurrent_to_forget_weights);
308 recurrent_weights_vector.emplace_back(recurrent_to_cell_weights);
309 recurrent_weights_vector.emplace_back(recurrent_to_output_weights);
310 const TensorInfo recurrent_weights(TensorShape(output_size, 4 * output_size), 1, DataType::QASYMM8, qweights);
311 ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate(recurrent_weights_vector, &recurrent_weights, Window::DimY));
312
313 // _concat_weights
314 std::vector<const ITensorInfo *> weights_vector;
315 weights_vector.emplace_back(&recurrent_weights);
316 weights_vector.emplace_back(&input_weights);
317 const TensorInfo weights(TensorShape(input_size + output_size, 4 * output_size), 1, DataType::QASYMM8, qweights);
318 ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate(weights_vector, &weights, Window::DimX));
319 // _transpose_weights
320 const TensorShape weights_transposed_shape(weights.tensor_shape()[1], weights.tensor_shape()[0]);
321 TensorInfo weights_transposed = weights.clone()->set_is_resizable(true).set_tensor_shape(weights_transposed_shape);
322 ARM_COMPUTE_RETURN_ON_ERROR(NETranspose::validate(&weights, &weights_transposed));
323
324 // _concat_inputs
325 std::vector<const ITensorInfo *> input_vector;
326 input_vector.emplace_back(input);
327 input_vector.emplace_back(output_state_in);
328 TensorInfo input_concatenated(TensorShape(output_size + input_size, batch_size), 1, DataType::QASYMM8, qasymm);
329 ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate(input_vector, &input_concatenated, Window::DimX));
330
331 // _concat_bias
332 std::vector<const ITensorInfo *> bias_vector;
333 bias_vector.emplace_back(input_gate_bias);
334 bias_vector.emplace_back(forget_gate_bias);
335 bias_vector.emplace_back(cell_bias);
336 bias_vector.emplace_back(output_gate_bias);
337
338 const TensorInfo bias_concatenated(TensorShape(4 * output_size), 1, DataType::S32);
339 ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate(bias_vector, &bias_concatenated, Window::DimX));
340
341 // Invert the offset for gemmlowp
342 input_concatenated.set_quantization_info(QuantizationInfo(qasymm.uniform().scale, -qasymm.uniform().offset));
343 weights_transposed.set_quantization_info(QuantizationInfo(qweights.uniform().scale, -qweights.uniform().offset));
344
345 // _gemmlowp
346 const TensorInfo output_highp(TensorShape(4 * output_size, batch_size), 1, DataType::S32);
347 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpMatrixMultiplyCore::validate(&input_concatenated, &weights_transposed, nullptr, &output_highp));
348
349 // Set the offset back
350 input_concatenated.set_quantization_info(QuantizationInfo(qasymm.uniform().scale, qasymm.uniform().offset));
351 weights_transposed.set_quantization_info(QuantizationInfo(qweights.uniform().scale, qweights.uniform().offset));
352
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100353 const TensorInfo output_lowp(output_highp.tensor_shape(), 1, DataType::QSYMM16, qsymm_3);
354
Manuel Bottini07263982019-10-17 18:37:26 +0100355 const float multiplier = 4096.f * qasymm.uniform().scale * qweights.uniform().scale;
Michalis Spyroue7be8a02019-12-12 16:16:09 +0000356 int32_t output_multiplier = 0;
357 int32_t output_shift = 0;
Manuel Bottini07263982019-10-17 18:37:26 +0100358 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift));
359
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100360 // _output_stage
361 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::validate(&output_highp, &bias_concatenated, &output_lowp));
362
363 TensorInfo input_gate_input;
364 TensorInfo forget_gate_input;
365 TensorInfo input_modulation_gate_input;
366 TensorInfo output_gate_input;
367
368 if(batch_size > 1)
369 {
370 // _slice_input_tensor
371 input_gate_input = TensorInfo(TensorShape(output_size, batch_size), 1, DataType::QSYMM16, qsymm_3);
372 ARM_COMPUTE_RETURN_ON_ERROR(NESlice::validate(&output_lowp, &input_gate_input, { 0, 0 }, { output_size, batch_size }));
373 // _slice_forget_tensor
374 forget_gate_input = TensorInfo(TensorShape(output_size, batch_size), 1, DataType::QSYMM16, qsymm_3);
375 ARM_COMPUTE_RETURN_ON_ERROR(NESlice::validate(&output_lowp, &forget_gate_input, { output_size, 0 }, { 2 * output_size, batch_size }));
376 // _slice_cell_tensor
377 input_modulation_gate_input = TensorInfo(TensorShape(output_size, batch_size), 1, DataType::QSYMM16, qsymm_3);
378 ARM_COMPUTE_RETURN_ON_ERROR(NESlice::validate(&output_lowp, &input_modulation_gate_input, { 2 * output_size, 0 }, { 3 * output_size, batch_size }));
379 // _slice_output_tensor
380 output_gate_input = TensorInfo(TensorShape(output_size, batch_size), 1, DataType::QSYMM16, qsymm_3);
381 ARM_COMPUTE_RETURN_ON_ERROR(NESlice::validate(&output_lowp, &output_gate_input, { 3 * output_size, 0 }, { 4 * output_size, batch_size }));
382 }
383 else
384 {
385 // _slice_input_tensor
386 input_gate_input = TensorInfo(TensorShape(output_size), 1, DataType::QSYMM16, qsymm_3);
387 ARM_COMPUTE_RETURN_ON_ERROR(NESlice::validate(&output_lowp, &input_gate_input, { 0 }, { output_size }));
388 // _slice_forget_tensor
389 forget_gate_input = TensorInfo(TensorShape(output_size), 1, DataType::QSYMM16, qsymm_3);
390 ARM_COMPUTE_RETURN_ON_ERROR(NESlice::validate(&output_lowp, &forget_gate_input, { output_size }, { 2 * output_size }));
391 // _slice_cell_tensor
392 input_modulation_gate_input = TensorInfo(TensorShape(output_size), 1, DataType::QSYMM16, qsymm_3);
393 ARM_COMPUTE_RETURN_ON_ERROR(NESlice::validate(&output_lowp, &input_modulation_gate_input, { 2 * output_size }, { 3 * output_size }));
394 // _slice_output_tensor
395 output_gate_input = TensorInfo(TensorShape(output_size), 1, DataType::QSYMM16, qsymm_3);
396 ARM_COMPUTE_RETURN_ON_ERROR(NESlice::validate(&output_lowp, &output_gate_input, { 3 * output_size }, { 4 * output_size }));
397 }
398
399 // _sigmoid_forget_gate
400 const TensorInfo forget_gate_output(forget_gate_input.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
401 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(&forget_gate_input, &forget_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
402 // _sigmoid_input_gate
403 const TensorInfo input_gate_output(input_gate_input.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
404 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(&input_gate_input, &input_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
405 // _tanh_modulation_gate
406 const TensorInfo input_modulation_gate_output(input_modulation_gate_input.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
407 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(&input_modulation_gate_input, &input_modulation_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f)));
408 // _sigmoid_output_gate
409 const TensorInfo output_gate_output(output_gate_input.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
410 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(&output_gate_input, &output_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
411
412 // _mul_forget_gate_cell_state
413 const TensorInfo cell_state_tmp1(forget_gate_output.tensor_shape(), 1, DataType::QSYMM16, qsymm_4);
414 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplication::validate(&forget_gate_output, cell_state_in, &cell_state_tmp1, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
415
416 // _mul_input_gate_input_mod_gate
417 const TensorInfo cell_state_tmp2(input_gate_output.tensor_shape(), 1, DataType::QSYMM16, qsymm_4);
418 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplication::validate(&input_gate_output, &input_modulation_gate_output, &cell_state_tmp2, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
419
420 // _add_cell_state_tmps
421 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&cell_state_tmp1, &cell_state_tmp2, cell_state_out, ConvertPolicy::SATURATE));
422
423 // _tanh_modulation_gate
424 const TensorInfo output_state_tmp(cell_state_out->tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
425 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(cell_state_out, &output_state_tmp, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f)));
426
427 // _mul_output_state_tmp_output_gate
428 const TensorInfo output_state_out_symm(output_gate_output.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
429 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplication::validate(&output_state_tmp, &output_gate_output, &output_state_out_symm, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
430
431 // _dequantize
432 const TensorInfo output_state_out_f32(output_state_out_symm.tensor_shape(), 1, DataType::F32);
433 ARM_COMPUTE_RETURN_ON_ERROR(NEDequantizationLayer::validate(&output_state_out_symm, &output_state_out_f32));
434
435 // _quantize
436 ARM_COMPUTE_RETURN_ON_ERROR(NEQuantizationLayer::validate(&output_state_out_f32, output_state_out));
437
Michalis Spyrouba27e442019-05-28 10:04:57 +0100438 if(cell_state_out->total_size() != 0)
439 {
440 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&cell_state_info, cell_state_out);
441 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&cell_state_info, cell_state_out);
442 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&cell_state_info, cell_state_out);
443 }
444
445 if(output_state_out->total_size() != 0)
446 {
447 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&output_state_info, output_state_out);
448 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&output_state_info, output_state_out);
449 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&output_state_info, output_state_out);
450 }
451
452 return Status{};
453}
454
455void NELSTMLayerQuantized::run()
456{
457 prepare();
458
459 // Acquire all the temporaries
460 MemoryGroupResourceScope scope_mg(_memory_group);
461
462 // Concat and transpose the input
463 _concat_inputs.run();
464
465 // Run gemmlowp
466 _gemmlowp.run();
467 _output_stage.run();
468
469 // Slice the results
470 _slice_input_tensor.run();
471 _slice_forget_tensor.run();
472 _slice_cell_tensor.run();
473 _slice_output_tensor.run();
474
475 // Gates
476 // Forget gate
477 _sigmoid_forget_gate.run();
478
479 // Input gate
480 _sigmoid_input_gate.run();
481
482 // Input modulation gate
483 _tanh_modulation_gate.run();
484
485 // Output gate
486 _sigmoid_output_gate.run();
487
488 // Cell state (long term memory)
489 _mul1.run();
490 _mul2.run();
491 _add1.run();
492
493 // Output state (short term memory)
494 _tanh_output_state.run();
495 _mul3.run();
496
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +0100497 // Requantize output state from QSYMM16 to QASYMM8
Michalis Spyrouba27e442019-05-28 10:04:57 +0100498 _dequantize.run();
499 _quantize.run();
500}
501
502void NELSTMLayerQuantized::prepare()
503{
504 if(!_is_prepared)
505 {
506 _input_weights.allocator()->allocate();
507 _concat_input_weights.run();
508
509 _input_to_input_weights->mark_as_unused();
510 _input_to_forget_weights->mark_as_unused();
511 _input_to_cell_weights->mark_as_unused();
512 _input_to_output_weights->mark_as_unused();
513
514 _recurrent_weights.allocator()->allocate();
515 _concat_recurrent_weights.run();
516 _recurrent_to_input_weights->mark_as_unused();
517 _recurrent_to_forget_weights->mark_as_unused();
518 _recurrent_to_cell_weights->mark_as_unused();
519 _recurrent_to_output_weights->mark_as_unused();
520
521 _weights.allocator()->allocate();
522 _concat_weights.run();
523
524 _input_weights.mark_as_unused();
525 _input_weights.allocator()->free();
526 _recurrent_weights.mark_as_unused();
527 _recurrent_weights.allocator()->free();
528
529 _weights_transposed.allocator()->allocate();
530 _transpose_weights.run();
531
532 _weights.mark_as_unused();
533 _weights.allocator()->free();
534
535 _bias.allocator()->allocate();
536 _concat_bias.run();
537 _input_gate_bias->mark_as_unused();
538 _forget_gate_bias->mark_as_unused();
539 _cell_bias->mark_as_unused();
540 _output_gate_bias->mark_as_unused();
541
542 _is_prepared = true;
543 }
544}
545
546} // namespace arm_compute