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