blob: d14c6102d5bd36c2c715dba331f08e1a454ae602 [file] [log] [blame]
Manuel Bottini10c53f12019-07-17 16:11:53 +01001/*
Georgios Pinitas856f66e2021-04-22 21:13:21 +01002 * Copyright (c) 2019-2021 Arm Limited.
Manuel Bottini10c53f12019-07-17 16:11:53 +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
25#include "arm_compute/runtime/CL/functions/CLLSTMLayerQuantized.h"
26
27#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/Validate.h"
29#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010030#include "src/core/CL/kernels/CLFillBorderKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/helpers/AutoConfiguration.h"
Manuel Bottini10c53f12019-07-17 16:11:53 +010032
ramelg016d891572021-09-29 10:05:09 +010033#include "src/common/utils/Log.h"
34
Manuel Bottini10c53f12019-07-17 16:11:53 +010035#include <memory>
Manuel Bottini10c53f12019-07-17 16:11:53 +010036
37namespace arm_compute
38{
39namespace
40{
41// Quantization info structures used in the LSTMQuantize layer
42const QuantizationInfo qasymm(1.f / 128.f, 128);
43const QuantizationInfo qsymm_3(8.f / 32768.f, 0); // qsymm16 with 3 integer bit
44const QuantizationInfo qsymm_4(16.f / 32768.f, 0); // qsymm16 with 4 integer bit
45const QuantizationInfo qsymm_0(1.f / 32768.f, 0); // qsymm16 with 0 integer bit
46} // namespace
47
48CLLSTMLayerQuantized::CLLSTMLayerQuantized(std::shared_ptr<IMemoryManager> memory_manager)
49 : _memory_group(std::move(memory_manager)), _gemmlowp(), _output_stage(), _transpose_weights(), _concat_input_weights(), _concat_recurrent_weights(), _concat_weights(), _concat_inputs(),
50 _concat_bias(), _sigmoid_forget_gate(), _sigmoid_input_gate(), _sigmoid_output_gate(), _tanh_modulation_gate(), _tanh_output_state(), _add_cell_state_tmps(), _add2(), _mul_forget_gate_cell_state(),
51 _mul_input_gate_input_mod_gate(), _mul_output_state_tmp_output_gate(), _slice_input_tensor(), _slice_forget_tensor(), _slice_cell_tensor(), _slice_output_tensor(), _dequantize(), _quantize(),
52 _input_to_input_weights(nullptr), _input_to_forget_weights(nullptr), _input_to_cell_weights(nullptr), _input_to_output_weights(nullptr), _recurrent_to_input_weights(nullptr),
53 _recurrent_to_forget_weights(nullptr), _recurrent_to_cell_weights(nullptr), _recurrent_to_output_weights(nullptr), _input_gate_bias(nullptr), _forget_gate_bias(nullptr), _cell_bias(nullptr),
54 _output_gate_bias(nullptr), _recurrent_weights(), _input_weights(), _weights(), _input(), _weights_transposed(), _output_highp(), _output_lowp(), _bias(), _forget_gate_input(), _input_gate_input(),
55 _output_gate_input(), _input_modulation_gate_input(), _forget_gate_output(), _input_gate_output(), _output_gate_output(), _input_modulation_gate_output(), _cell_state_tmp1(), _cell_state_tmp2(),
56 _output_state_tmp(), _output_state_out_symm(), _output_state_out_f32(), _is_prepared(false)
57{
58}
59
60void CLLSTMLayerQuantized::configure(const ICLTensor *input,
61 const ICLTensor *input_to_input_weights, const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
62 const ICLTensor *recurrent_to_input_weights, const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
63 const ICLTensor *input_gate_bias, const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
64 ICLTensor *cell_state_in, const ICLTensor *output_state_in,
65 ICLTensor *cell_state_out, ICLTensor *output_state_out)
66{
Manuel Bottini2b84be52020-04-08 10:15:51 +010067 configure(CLKernelLibrary::get().get_compile_context(), input, input_to_input_weights, input_to_forget_weights, input_to_cell_weights, input_to_output_weights, recurrent_to_input_weights,
68 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, output_state_in, cell_state_out,
69 output_state_out);
70}
71
72void CLLSTMLayerQuantized::configure(const CLCompileContext &compile_context, const ICLTensor *input,
73 const ICLTensor *input_to_input_weights, const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
74 const ICLTensor *recurrent_to_input_weights, const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
75 const ICLTensor *input_gate_bias, const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
76 ICLTensor *cell_state_in, const ICLTensor *output_state_in,
77 ICLTensor *cell_state_out, ICLTensor *output_state_out)
78{
Manuel Bottini10c53f12019-07-17 16:11:53 +010079 ARM_COMPUTE_ERROR_ON_NULLPTR(input, input_to_input_weights, input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
80 recurrent_to_input_weights, recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
81 input_gate_bias, forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in, cell_state_out, output_state_out);
82
ramelg016d891572021-09-29 10:05:09 +010083 ARM_COMPUTE_LOG_PARAMS(input, input_to_input_weights, input_to_forget_weights, input_to_cell_weights, input_to_output_weights, recurrent_to_input_weights,
84 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, output_state_in, cell_state_out,
85 output_state_out);
86
Manuel Bottini10c53f12019-07-17 16:11:53 +010087 ARM_COMPUTE_ERROR_THROW_ON(CLLSTMLayerQuantized::validate(input->info(), input_to_input_weights->info(), input_to_forget_weights->info(), input_to_cell_weights->info(),
88 input_to_output_weights->info(),
89 recurrent_to_input_weights->info(), recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(),
90 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()));
91
92 const int input_size = input->info()->dimension(0);
93 const int batch_size = input->info()->dimension(1);
94 const int output_size = input_to_input_weights->info()->dimension(1);
95
96 const QuantizationInfo qweights = input_to_input_weights->info()->quantization_info(); // Weights quantization
97
98 auto_init_if_empty(*cell_state_out->info(), TensorInfo(TensorShape(batch_size, output_size), 1, DataType::QSYMM16, qsymm_4));
99 auto_init_if_empty(*output_state_out->info(), TensorInfo(TensorShape(batch_size, output_size), 1, DataType::QASYMM8, qasymm));
100
101 _input_to_input_weights = input_to_input_weights;
102 _input_to_forget_weights = input_to_forget_weights;
103 _input_to_cell_weights = input_to_cell_weights;
104 _input_to_output_weights = input_to_output_weights;
105 _recurrent_to_input_weights = recurrent_to_input_weights;
106 _recurrent_to_forget_weights = recurrent_to_forget_weights;
107 _recurrent_to_cell_weights = recurrent_to_cell_weights;
108 _recurrent_to_output_weights = recurrent_to_output_weights;
109 _input_gate_bias = input_gate_bias;
110 _forget_gate_bias = forget_gate_bias;
111 _cell_bias = cell_bias;
112 _output_gate_bias = output_gate_bias;
113
114 // Weights concatenation
115 std::vector<const ICLTensor *> inputs_weights_vector;
116 inputs_weights_vector.emplace_back(input_to_input_weights);
117 inputs_weights_vector.emplace_back(input_to_forget_weights);
118 inputs_weights_vector.emplace_back(input_to_cell_weights);
119 inputs_weights_vector.emplace_back(input_to_output_weights);
120
121 std::vector<const ICLTensor *> recurrent_weights_vector;
122 recurrent_weights_vector.emplace_back(recurrent_to_input_weights);
123 recurrent_weights_vector.emplace_back(recurrent_to_forget_weights);
124 recurrent_weights_vector.emplace_back(recurrent_to_cell_weights);
125 recurrent_weights_vector.emplace_back(recurrent_to_output_weights);
126
127 _input_weights.allocator()->init(TensorInfo(TensorShape(input_size, 4 * output_size), 1, DataType::QASYMM8, qweights));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100128 _concat_input_weights.configure(compile_context, inputs_weights_vector, &_input_weights, Window::DimY);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100129
130 _recurrent_weights.allocator()->init(TensorInfo(TensorShape(output_size, 4 * output_size), 1, DataType::QASYMM8, qweights));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100131 _concat_recurrent_weights.configure(compile_context, recurrent_weights_vector, &_recurrent_weights, Window::DimY);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100132
133 std::vector<const ICLTensor *> weights_vector;
134 weights_vector.emplace_back(&_recurrent_weights);
135 weights_vector.emplace_back(&_input_weights);
136
137 _weights.allocator()->init(TensorInfo(TensorShape(output_size + input_size, 4 * output_size), 1, DataType::QASYMM8, qweights));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100138 _concat_weights.configure(compile_context, weights_vector, &_weights, Window::DimX);
139 _transpose_weights.configure(compile_context, &_weights, &_weights_transposed);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100140
141 // Input concatenation
142 std::vector<const ICLTensor *> input_vector;
143 input_vector.emplace_back(input);
144 input_vector.emplace_back(output_state_in);
145
146 _memory_group.manage(&_input);
147 _input.allocator()->init(TensorInfo(TensorShape(output_size + input_size, batch_size), 1, DataType::QASYMM8, qasymm));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100148 _concat_inputs.configure(compile_context, input_vector, &_input, Window::DimX);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100149
150 // Bias concatenation
151 std::vector<const ICLTensor *> bias_vector;
152 bias_vector.emplace_back(input_gate_bias);
153 bias_vector.emplace_back(forget_gate_bias);
154 bias_vector.emplace_back(cell_bias);
155 bias_vector.emplace_back(output_gate_bias);
156
157 _bias.allocator()->init(TensorInfo(TensorShape(4 * output_size), 1, DataType::S32));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100158 _concat_bias.configure(compile_context, bias_vector, &_bias, Window::DimX);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100159
160 // Invert the offset for gemmlowp
161 _input.info()->set_quantization_info(QuantizationInfo(qasymm.uniform().scale, -qasymm.uniform().offset));
162 _weights_transposed.info()->set_quantization_info(QuantizationInfo(qweights.uniform().scale, -qweights.uniform().offset));
163
164 // Run gemmlowp
165 _memory_group.manage(&_output_highp);
166 _output_highp.allocator()->init(TensorInfo(TensorShape(4 * output_size, batch_size), 1, DataType::S32));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100167 _gemmlowp.configure(compile_context, &_input, &_weights_transposed, nullptr, &_output_highp);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100168 _input.allocator()->allocate();
169
170 // Set the offset back
171 _input.info()->set_quantization_info(QuantizationInfo(qasymm.uniform().scale, qasymm.uniform().offset));
172 _weights_transposed.info()->set_quantization_info(QuantizationInfo(qweights.uniform().scale, qweights.uniform().offset));
173
174 // multiplier = (input_scale * weights_scale) / output_scale (2 ^ (-12))
175 _output_lowp.allocator()->init(TensorInfo(_output_highp.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_3));
176
177 const float multiplier = 4096.f * qasymm.uniform().scale * qweights.uniform().scale;
178 int output_multiplier = 0;
179 int output_shift = 0;
Manuel Bottini07263982019-10-17 18:37:26 +0100180 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100181
182 _memory_group.manage(&_output_lowp);
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100183
184 GEMMLowpOutputStageInfo info{};
185 info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
186 info.gemmlowp_multiplier = output_multiplier;
187 info.gemmlowp_shift = output_shift;
188 info.output_data_type = DataType::QSYMM16;
189 _output_stage.configure(compile_context, &_output_highp, &_bias, &_output_lowp, info);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100190 _output_highp.allocator()->allocate();
191 _bias.allocator()->allocate();
192
193 // Get the gate tensors
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100194 if(batch_size > 1)
195 {
196 _memory_group.manage(&_input_gate_input);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100197 _slice_input_tensor.configure(compile_context, &_output_lowp, &_input_gate_input, { 0, 0 }, { output_size, batch_size });
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100198 _memory_group.manage(&_forget_gate_input);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100199 _slice_forget_tensor.configure(compile_context, &_output_lowp, &_forget_gate_input, { output_size, 0 }, { 2 * output_size, batch_size });
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100200 _memory_group.manage(&_input_modulation_gate_input);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100201 _slice_cell_tensor.configure(compile_context, &_output_lowp, &_input_modulation_gate_input, { 2 * output_size, 0 }, { 3 * output_size, batch_size });
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100202 _memory_group.manage(&_output_gate_input);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100203 _slice_output_tensor.configure(compile_context, &_output_lowp, &_output_gate_input, { 3 * output_size, 0 }, { 4 * output_size, batch_size });
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100204 _output_lowp.allocator()->allocate();
205 }
206 else
207 {
208 _memory_group.manage(&_input_gate_input);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100209 _slice_input_tensor.configure(compile_context, &_output_lowp, &_input_gate_input, { 0 }, { output_size });
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100210 _memory_group.manage(&_forget_gate_input);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100211 _slice_forget_tensor.configure(compile_context, &_output_lowp, &_forget_gate_input, { output_size }, { 2 * output_size });
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100212 _memory_group.manage(&_input_modulation_gate_input);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100213 _slice_cell_tensor.configure(compile_context, &_output_lowp, &_input_modulation_gate_input, { 2 * output_size }, { 3 * output_size });
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100214 _memory_group.manage(&_output_gate_input);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100215 _slice_output_tensor.configure(compile_context, &_output_lowp, &_output_gate_input, { 3 * output_size }, { 4 * output_size });
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100216 _output_lowp.allocator()->allocate();
217 }
Manuel Bottini10c53f12019-07-17 16:11:53 +0100218
219 // Forget gate
220 _memory_group.manage(&_forget_gate_output);
221 _forget_gate_output.allocator()->init(TensorInfo(_forget_gate_input.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100222 _sigmoid_forget_gate.configure(compile_context, &_forget_gate_input, &_forget_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Manuel Bottini10c53f12019-07-17 16:11:53 +0100223 _forget_gate_input.allocator()->allocate();
224
225 // Input gate
226 _memory_group.manage(&_input_gate_output);
227 _input_gate_output.allocator()->init(TensorInfo(_input_gate_input.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100228 _sigmoid_input_gate.configure(compile_context, &_input_gate_input, &_input_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Manuel Bottini10c53f12019-07-17 16:11:53 +0100229 _input_gate_input.allocator()->allocate();
230
231 // Input modulation gate equation
232 _memory_group.manage(&_input_modulation_gate_output);
233 _input_modulation_gate_output.allocator()->init(TensorInfo(_input_modulation_gate_input.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100234 _tanh_modulation_gate.configure(compile_context, &_input_modulation_gate_input, &_input_modulation_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f));
Manuel Bottini10c53f12019-07-17 16:11:53 +0100235 _input_modulation_gate_input.allocator()->allocate();
236
237 // Output gate
238 _memory_group.manage(&_output_gate_output);
239 _output_gate_output.allocator()->init(TensorInfo(_output_gate_input.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100240 _sigmoid_output_gate.configure(compile_context, &_output_gate_input, &_output_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Manuel Bottini10c53f12019-07-17 16:11:53 +0100241 _output_gate_input.allocator()->allocate();
242
243 // Long term memory
244 _memory_group.manage(&_cell_state_tmp1);
245 _cell_state_tmp1.allocator()->init(TensorInfo(_forget_gate_output.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_4));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100246 _mul_forget_gate_cell_state.configure(compile_context, &_forget_gate_output, cell_state_in, &_cell_state_tmp1, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100247 _forget_gate_output.allocator()->allocate();
248
249 _memory_group.manage(&_cell_state_tmp2);
250 _cell_state_tmp2.allocator()->init(TensorInfo(_input_gate_output.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_4));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100251 _mul_input_gate_input_mod_gate.configure(compile_context, &_input_gate_output, &_input_modulation_gate_output, &_cell_state_tmp2, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100252 _input_modulation_gate_output.allocator()->allocate();
253 _input_gate_output.allocator()->allocate();
254
Manuel Bottini2b84be52020-04-08 10:15:51 +0100255 _add_cell_state_tmps.configure(compile_context, &_cell_state_tmp1, &_cell_state_tmp2, cell_state_out, ConvertPolicy::SATURATE);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100256 _cell_state_tmp1.allocator()->allocate();
257 _cell_state_tmp2.allocator()->allocate();
258
259 // Short term memory
260 _memory_group.manage(&_output_state_tmp);
261 _output_state_tmp.allocator()->init(TensorInfo(cell_state_out->info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100262 _tanh_output_state.configure(compile_context, cell_state_out, &_output_state_tmp, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f));
Manuel Bottini10c53f12019-07-17 16:11:53 +0100263
264 _memory_group.manage(&_output_state_out_symm);
265 _output_state_out_symm.allocator()->init(TensorInfo(_output_gate_output.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100266 _mul_output_state_tmp_output_gate.configure(compile_context, &_output_state_tmp, &_output_gate_output, &_output_state_out_symm, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100267 _output_gate_output.allocator()->allocate();
268 _output_state_tmp.allocator()->allocate();
269
270 // Requantize the output state from QSYMM16 to QASYMM8
271 _memory_group.manage(&_output_state_out_f32);
272 _output_state_out_f32.allocator()->init(TensorInfo(_output_state_out_symm.info()->tensor_shape(), 1, DataType::F32));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100273 _dequantize.configure(compile_context, &_output_state_out_symm, &_output_state_out_f32);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100274 _output_state_out_symm.allocator()->allocate();
275
Manuel Bottini2b84be52020-04-08 10:15:51 +0100276 _quantize.configure(compile_context, &_output_state_out_f32, output_state_out);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100277 _output_state_out_f32.allocator()->allocate();
278}
279
280Status CLLSTMLayerQuantized::validate(const ITensorInfo *input,
281 const ITensorInfo *input_to_input_weights, const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
282 const ITensorInfo *recurrent_to_input_weights, const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
283 const ITensorInfo *input_gate_bias, const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
284 const ITensorInfo *cell_state_in, const ITensorInfo *output_state_in,
285 const ITensorInfo *cell_state_out, const ITensorInfo *output_state_out)
286{
287 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,
288 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,
289 output_state_in, cell_state_out, output_state_out);
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100290 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::QASYMM8);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100291
292 const int input_size = input->dimension(0);
293 const int batch_size = input->dimension(1);
294 const int output_size = input_to_input_weights->dimension(1);
295
296 // Dimensionality checks
297 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2);
298 ARM_COMPUTE_RETURN_ERROR_ON(input_to_input_weights->num_dimensions() > 2);
299 ARM_COMPUTE_RETURN_ERROR_ON(input_gate_bias->num_dimensions() > 1);
300 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() > 2);
301
302 TensorInfo input_weights_info(input_to_input_weights->clone()->set_tensor_shape(TensorShape(input_size, output_size)).set_data_type(DataType::QASYMM8));
303 TensorInfo recurrent_weights_info(input_to_input_weights->clone()->set_tensor_shape(TensorShape(output_size, output_size)).set_data_type(DataType::QASYMM8));
304 TensorInfo bias_info(input_gate_bias->clone()->set_tensor_shape(TensorShape(output_size)).set_data_type(DataType::S32));
305 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));
306 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));
307
308 // Shape checks
309 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);
310 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);
311 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&bias_info, input_gate_bias, forget_gate_bias, cell_bias, output_gate_bias);
312 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&cell_state_info, cell_state_in);
313 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&output_state_info, output_state_in);
314
315 // Data type checks
316 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);
317 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&recurrent_weights_info, recurrent_to_input_weights, recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights);
318 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&bias_info, input_gate_bias, forget_gate_bias, cell_bias, output_gate_bias);
319 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&cell_state_info, cell_state_in);
320 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&output_state_info, output_state_in);
321
322 // Quantization checks
323 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input_to_input_weights, input_to_forget_weights, input_to_cell_weights, input_to_output_weights);
324 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(recurrent_to_input_weights, recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights);
325 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&cell_state_info, cell_state_in);
326 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&output_state_info, output_state_in);
327
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100328 // Validate internal functions
329 // _concat_input_weights
330 std::vector<const ITensorInfo *> inputs_weights_vector;
331 inputs_weights_vector.emplace_back(input_to_input_weights);
332 inputs_weights_vector.emplace_back(input_to_forget_weights);
333 inputs_weights_vector.emplace_back(input_to_cell_weights);
334 inputs_weights_vector.emplace_back(input_to_output_weights);
335 const QuantizationInfo qweights = input_to_input_weights->quantization_info(); // Weights quantization
336 const TensorInfo input_weights(TensorShape(input_size, 4 * output_size), 1, DataType::QASYMM8, qweights);
337 ARM_COMPUTE_RETURN_ON_ERROR(CLConcatenateLayer::validate(inputs_weights_vector, &input_weights, Window::DimY));
338
339 // _concat_recurrent_weights
340 std::vector<const ITensorInfo *> recurrent_weights_vector;
341 recurrent_weights_vector.emplace_back(recurrent_to_input_weights);
342 recurrent_weights_vector.emplace_back(recurrent_to_forget_weights);
343 recurrent_weights_vector.emplace_back(recurrent_to_cell_weights);
344 recurrent_weights_vector.emplace_back(recurrent_to_output_weights);
345 const TensorInfo recurrent_weights(TensorShape(output_size, 4 * output_size), 1, DataType::QASYMM8, qweights);
346 ARM_COMPUTE_RETURN_ON_ERROR(CLConcatenateLayer::validate(recurrent_weights_vector, &recurrent_weights, Window::DimY));
347
348 // _concat_weights
349 std::vector<const ITensorInfo *> weights_vector;
350 weights_vector.emplace_back(&recurrent_weights);
351 weights_vector.emplace_back(&input_weights);
352 const TensorInfo weights(TensorShape(input_size + output_size, 4 * output_size), 1, DataType::QASYMM8, qweights);
353 ARM_COMPUTE_RETURN_ON_ERROR(CLConcatenateLayer::validate(weights_vector, &weights, Window::DimX));
354 // _transpose_weights
355 const TensorShape weights_transposed_shape(weights.tensor_shape()[1], weights.tensor_shape()[0]);
356 TensorInfo weights_transposed = weights.clone()->set_is_resizable(true).set_tensor_shape(weights_transposed_shape);
357 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(&weights, &weights_transposed));
358
359 // _concat_inputs
360 std::vector<const ITensorInfo *> input_vector;
361 input_vector.emplace_back(input);
362 input_vector.emplace_back(output_state_in);
363 TensorInfo input_concatenated(TensorShape(output_size + input_size, batch_size), 1, DataType::QASYMM8, qasymm);
364 ARM_COMPUTE_RETURN_ON_ERROR(CLConcatenateLayer::validate(input_vector, &input_concatenated, Window::DimX));
365
366 // _concat_bias
367 std::vector<const ITensorInfo *> bias_vector;
368 bias_vector.emplace_back(input_gate_bias);
369 bias_vector.emplace_back(forget_gate_bias);
370 bias_vector.emplace_back(cell_bias);
371 bias_vector.emplace_back(output_gate_bias);
372
373 const TensorInfo bias_concatenated(TensorShape(4 * output_size), 1, DataType::S32);
374 ARM_COMPUTE_RETURN_ON_ERROR(CLConcatenateLayer::validate(bias_vector, &bias_concatenated, Window::DimX));
375
376 // Invert the offset for gemmlowp
377 input_concatenated.set_quantization_info(QuantizationInfo(qasymm.uniform().scale, -qasymm.uniform().offset));
378 weights_transposed.set_quantization_info(QuantizationInfo(qweights.uniform().scale, -qweights.uniform().offset));
379
380 // _gemmlowp
381 const TensorInfo output_highp(TensorShape(4 * output_size, batch_size), 1, DataType::S32);
382 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(&input_concatenated, &weights_transposed, nullptr, &output_highp));
383
384 // Set the offset back
385 input_concatenated.set_quantization_info(QuantizationInfo(qasymm.uniform().scale, qasymm.uniform().offset));
386 weights_transposed.set_quantization_info(QuantizationInfo(qweights.uniform().scale, qweights.uniform().offset));
387
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100388 const TensorInfo output_lowp(output_highp.tensor_shape(), 1, DataType::QSYMM16, qsymm_3);
389
Manuel Bottini07263982019-10-17 18:37:26 +0100390 const float multiplier = 4096.f * qasymm.uniform().scale * qweights.uniform().scale;
391 int output_multiplier = 0;
392 int output_shift = 0;
393 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift));
394
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100395 // _output_stage
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100396 GEMMLowpOutputStageInfo info{};
397 info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
398 info.gemmlowp_multiplier = output_multiplier;
399 info.gemmlowp_shift = output_shift;
400 info.output_data_type = DataType::QSYMM16;
401 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&output_highp, &bias_concatenated, &output_lowp, info));
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +0100402
403 TensorInfo input_gate_input;
404 TensorInfo forget_gate_input;
405 TensorInfo input_modulation_gate_input;
406 TensorInfo output_gate_input;
407
408 if(batch_size > 1)
409 {
410 // _slice_input_tensor
411 input_gate_input = TensorInfo(TensorShape(output_size, batch_size), 1, DataType::QSYMM16, qsymm_3);
412 ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &input_gate_input, { 0, 0 }, { output_size, batch_size }));
413 // _slice_forget_tensor
414 forget_gate_input = TensorInfo(TensorShape(output_size, batch_size), 1, DataType::QSYMM16, qsymm_3);
415 ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &forget_gate_input, { output_size, 0 }, { 2 * output_size, batch_size }));
416 // _slice_cell_tensor
417 input_modulation_gate_input = TensorInfo(TensorShape(output_size, batch_size), 1, DataType::QSYMM16, qsymm_3);
418 ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &input_modulation_gate_input, { 2 * output_size, 0 }, { 3 * output_size, batch_size }));
419 // _slice_output_tensor
420 output_gate_input = TensorInfo(TensorShape(output_size, batch_size), 1, DataType::QSYMM16, qsymm_3);
421 ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &output_gate_input, { 3 * output_size, 0 }, { 4 * output_size, batch_size }));
422 }
423 else
424 {
425 // _slice_input_tensor
426 input_gate_input = TensorInfo(TensorShape(output_size), 1, DataType::QSYMM16, qsymm_3);
427 ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &input_gate_input, { 0 }, { output_size }));
428 // _slice_forget_tensor
429 forget_gate_input = TensorInfo(TensorShape(output_size), 1, DataType::QSYMM16, qsymm_3);
430 ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &forget_gate_input, { output_size }, { 2 * output_size }));
431 // _slice_cell_tensor
432 input_modulation_gate_input = TensorInfo(TensorShape(output_size), 1, DataType::QSYMM16, qsymm_3);
433 ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &input_modulation_gate_input, { 2 * output_size }, { 3 * output_size }));
434 // _slice_output_tensor
435 output_gate_input = TensorInfo(TensorShape(output_size), 1, DataType::QSYMM16, qsymm_3);
436 ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &output_gate_input, { 3 * output_size }, { 4 * output_size }));
437 }
438
439 // _sigmoid_forget_gate
440 const TensorInfo forget_gate_output(forget_gate_input.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
441 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&forget_gate_input, &forget_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
442 // _sigmoid_input_gate
443 const TensorInfo input_gate_output(input_gate_input.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
444 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&input_gate_input, &input_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
445 // _tanh_modulation_gate
446 const TensorInfo input_modulation_gate_output(input_modulation_gate_input.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
447 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&input_modulation_gate_input, &input_modulation_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f)));
448 // _sigmoid_output_gate
449 const TensorInfo output_gate_output(output_gate_input.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
450 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&output_gate_input, &output_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
451
452 // _mul_forget_gate_cell_state
453 const TensorInfo cell_state_tmp1(forget_gate_output.tensor_shape(), 1, DataType::QSYMM16, qsymm_4);
454 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&forget_gate_output, cell_state_in, &cell_state_tmp1, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
455
456 // _mul_input_gate_input_mod_gate
457 const TensorInfo cell_state_tmp2(input_gate_output.tensor_shape(), 1, DataType::QSYMM16, qsymm_4);
458 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&input_gate_output, &input_modulation_gate_output, &cell_state_tmp2, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
459
460 // _add_cell_state_tmps
461 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&cell_state_tmp1, &cell_state_tmp2, cell_state_out, ConvertPolicy::SATURATE));
462
463 // _tanh_modulation_gate
464 const TensorInfo output_state_tmp(cell_state_out->tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
465 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(cell_state_out, &output_state_tmp, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f)));
466
467 // _mul_output_state_tmp_output_gate
468 const TensorInfo output_state_out_symm(output_gate_output.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
469 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&output_state_tmp, &output_gate_output, &output_state_out_symm, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
470
471 // _dequantize
472 const TensorInfo output_state_out_f32(output_state_out_symm.tensor_shape(), 1, DataType::F32);
473 ARM_COMPUTE_RETURN_ON_ERROR(CLDequantizationLayer::validate(&output_state_out_symm, &output_state_out_f32));
474
475 // _quantize
476 ARM_COMPUTE_RETURN_ON_ERROR(CLQuantizationLayer::validate(&output_state_out_f32, output_state_out));
477
Manuel Bottini10c53f12019-07-17 16:11:53 +0100478 if(cell_state_out->total_size() != 0)
479 {
480 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&cell_state_info, cell_state_out);
481 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&cell_state_info, cell_state_out);
482 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&cell_state_info, cell_state_out);
483 }
484
485 if(output_state_out->total_size() != 0)
486 {
487 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&output_state_info, output_state_out);
488 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&output_state_info, output_state_out);
489 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&output_state_info, output_state_out);
490 }
491
492 return Status{};
493}
494
495void CLLSTMLayerQuantized::run()
496{
497 prepare();
498
499 // Acquire all the temporaries
500 MemoryGroupResourceScope scope_mg(_memory_group);
501
502 // Concat and transpose the input
503 _concat_inputs.run();
504
505 // Run gemmlowp
506 _gemmlowp.run();
507 _output_stage.run();
508
509 // Slice the results
510 _slice_input_tensor.run();
511 _slice_forget_tensor.run();
512 _slice_cell_tensor.run();
513 _slice_output_tensor.run();
514
515 // Gates
516 // Forget gate
517 _sigmoid_forget_gate.run();
518
519 // Input gate
520 _sigmoid_input_gate.run();
521
522 // Input modulation gate
523 _tanh_modulation_gate.run();
524
525 // Output gate
526 _sigmoid_output_gate.run();
527
528 // Cell state (long term memory)
529 _mul_forget_gate_cell_state.run();
530 _mul_input_gate_input_mod_gate.run();
531 _add_cell_state_tmps.run();
532
533 // Output state (short term memory)
534 _tanh_output_state.run();
535 _mul_output_state_tmp_output_gate.run();
536
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +0100537 // Requantize output state from QSYMM16 to QASYMM8
Manuel Bottini10c53f12019-07-17 16:11:53 +0100538 _dequantize.run();
539 _quantize.run();
540}
541
542void CLLSTMLayerQuantized::prepare()
543{
544 if(!_is_prepared)
545 {
546 _input_weights.allocator()->allocate();
547 _concat_input_weights.run();
548
549 _input_to_input_weights->mark_as_unused();
550 _input_to_forget_weights->mark_as_unused();
551 _input_to_cell_weights->mark_as_unused();
552 _input_to_output_weights->mark_as_unused();
553
554 _recurrent_weights.allocator()->allocate();
555 _concat_recurrent_weights.run();
556 _recurrent_to_input_weights->mark_as_unused();
557 _recurrent_to_forget_weights->mark_as_unused();
558 _recurrent_to_cell_weights->mark_as_unused();
559 _recurrent_to_output_weights->mark_as_unused();
560
561 _weights.allocator()->allocate();
562 _concat_weights.run();
563
564 _input_weights.mark_as_unused();
565 _input_weights.allocator()->free();
566 _recurrent_weights.mark_as_unused();
567 _recurrent_weights.allocator()->free();
568
569 _weights_transposed.allocator()->allocate();
570 _transpose_weights.run();
571
572 _weights.mark_as_unused();
573 _weights.allocator()->free();
574
575 _bias.allocator()->allocate();
576 _concat_bias.run();
577 _input_gate_bias->mark_as_unused();
578 _forget_gate_bias->mark_as_unused();
579 _cell_bias->mark_as_unused();
580 _output_gate_bias->mark_as_unused();
581
582 _is_prepared = true;
583 }
584}
585
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +0100586} // namespace arm_compute