1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2019-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust
25*c217d954SCole Faust #include "arm_compute/runtime/CL/functions/CLLSTMLayerQuantized.h"
26*c217d954SCole Faust
27*c217d954SCole Faust #include "arm_compute/core/Utils.h"
28*c217d954SCole Faust #include "arm_compute/core/Validate.h"
29*c217d954SCole Faust #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
30*c217d954SCole Faust #include "src/core/CL/kernels/CLFillBorderKernel.h"
31*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
32*c217d954SCole Faust
33*c217d954SCole Faust #include "src/common/utils/Log.h"
34*c217d954SCole Faust
35*c217d954SCole Faust #include <memory>
36*c217d954SCole Faust
37*c217d954SCole Faust namespace arm_compute
38*c217d954SCole Faust {
39*c217d954SCole Faust namespace
40*c217d954SCole Faust {
41*c217d954SCole Faust // Quantization info structures used in the LSTMQuantize layer
42*c217d954SCole Faust const QuantizationInfo qasymm(1.f / 128.f, 128);
43*c217d954SCole Faust const QuantizationInfo qsymm_3(8.f / 32768.f, 0); // qsymm16 with 3 integer bit
44*c217d954SCole Faust const QuantizationInfo qsymm_4(16.f / 32768.f, 0); // qsymm16 with 4 integer bit
45*c217d954SCole Faust const QuantizationInfo qsymm_0(1.f / 32768.f, 0); // qsymm16 with 0 integer bit
46*c217d954SCole Faust } // namespace
47*c217d954SCole Faust
CLLSTMLayerQuantized(std::shared_ptr<IMemoryManager> memory_manager)48*c217d954SCole Faust CLLSTMLayerQuantized::CLLSTMLayerQuantized(std::shared_ptr<IMemoryManager> memory_manager)
49*c217d954SCole Faust : _memory_group(std::move(memory_manager)), _gemmlowp(), _output_stage(), _transpose_weights(), _concat_input_weights(), _concat_recurrent_weights(), _concat_weights(), _concat_inputs(),
50*c217d954SCole Faust _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*c217d954SCole Faust _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*c217d954SCole Faust _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*c217d954SCole Faust _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*c217d954SCole Faust _output_gate_bias(nullptr), _recurrent_weights(), _input_weights(), _weights(), _input(), _weights_transposed(), _output_highp(), _output_lowp(), _bias(), _forget_gate_input(), _input_gate_input(),
55*c217d954SCole Faust _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*c217d954SCole Faust _output_state_tmp(), _output_state_out_symm(), _output_state_out_f32(), _is_prepared(false)
57*c217d954SCole Faust {
58*c217d954SCole Faust }
59*c217d954SCole Faust
configure(const ICLTensor * input,const ICLTensor * input_to_input_weights,const ICLTensor * input_to_forget_weights,const ICLTensor * input_to_cell_weights,const ICLTensor * input_to_output_weights,const ICLTensor * recurrent_to_input_weights,const ICLTensor * recurrent_to_forget_weights,const ICLTensor * recurrent_to_cell_weights,const ICLTensor * recurrent_to_output_weights,const ICLTensor * input_gate_bias,const ICLTensor * forget_gate_bias,const ICLTensor * cell_bias,const ICLTensor * output_gate_bias,ICLTensor * cell_state_in,const ICLTensor * output_state_in,ICLTensor * cell_state_out,ICLTensor * output_state_out)60*c217d954SCole Faust void CLLSTMLayerQuantized::configure(const ICLTensor *input,
61*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust const ICLTensor *input_gate_bias, const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
64*c217d954SCole Faust ICLTensor *cell_state_in, const ICLTensor *output_state_in,
65*c217d954SCole Faust ICLTensor *cell_state_out, ICLTensor *output_state_out)
66*c217d954SCole Faust {
67*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust output_state_out);
70*c217d954SCole Faust }
71*c217d954SCole Faust
configure(const CLCompileContext & compile_context,const ICLTensor * input,const ICLTensor * input_to_input_weights,const ICLTensor * input_to_forget_weights,const ICLTensor * input_to_cell_weights,const ICLTensor * input_to_output_weights,const ICLTensor * recurrent_to_input_weights,const ICLTensor * recurrent_to_forget_weights,const ICLTensor * recurrent_to_cell_weights,const ICLTensor * recurrent_to_output_weights,const ICLTensor * input_gate_bias,const ICLTensor * forget_gate_bias,const ICLTensor * cell_bias,const ICLTensor * output_gate_bias,ICLTensor * cell_state_in,const ICLTensor * output_state_in,ICLTensor * cell_state_out,ICLTensor * output_state_out)72*c217d954SCole Faust void CLLSTMLayerQuantized::configure(const CLCompileContext &compile_context, const ICLTensor *input,
73*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust const ICLTensor *input_gate_bias, const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
76*c217d954SCole Faust ICLTensor *cell_state_in, const ICLTensor *output_state_in,
77*c217d954SCole Faust ICLTensor *cell_state_out, ICLTensor *output_state_out)
78*c217d954SCole Faust {
79*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(input, input_to_input_weights, input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
80*c217d954SCole Faust recurrent_to_input_weights, recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
81*c217d954SCole Faust input_gate_bias, forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in, cell_state_out, output_state_out);
82*c217d954SCole Faust
83*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust output_state_out);
86*c217d954SCole Faust
87*c217d954SCole Faust 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*c217d954SCole Faust input_to_output_weights->info(),
89*c217d954SCole Faust recurrent_to_input_weights->info(), recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(),
90*c217d954SCole Faust 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*c217d954SCole Faust
92*c217d954SCole Faust const int input_size = input->info()->dimension(0);
93*c217d954SCole Faust const int batch_size = input->info()->dimension(1);
94*c217d954SCole Faust const int output_size = input_to_input_weights->info()->dimension(1);
95*c217d954SCole Faust
96*c217d954SCole Faust const QuantizationInfo qweights = input_to_input_weights->info()->quantization_info(); // Weights quantization
97*c217d954SCole Faust
98*c217d954SCole Faust auto_init_if_empty(*cell_state_out->info(), TensorInfo(TensorShape(batch_size, output_size), 1, DataType::QSYMM16, qsymm_4));
99*c217d954SCole Faust auto_init_if_empty(*output_state_out->info(), TensorInfo(TensorShape(batch_size, output_size), 1, DataType::QASYMM8, qasymm));
100*c217d954SCole Faust
101*c217d954SCole Faust _input_to_input_weights = input_to_input_weights;
102*c217d954SCole Faust _input_to_forget_weights = input_to_forget_weights;
103*c217d954SCole Faust _input_to_cell_weights = input_to_cell_weights;
104*c217d954SCole Faust _input_to_output_weights = input_to_output_weights;
105*c217d954SCole Faust _recurrent_to_input_weights = recurrent_to_input_weights;
106*c217d954SCole Faust _recurrent_to_forget_weights = recurrent_to_forget_weights;
107*c217d954SCole Faust _recurrent_to_cell_weights = recurrent_to_cell_weights;
108*c217d954SCole Faust _recurrent_to_output_weights = recurrent_to_output_weights;
109*c217d954SCole Faust _input_gate_bias = input_gate_bias;
110*c217d954SCole Faust _forget_gate_bias = forget_gate_bias;
111*c217d954SCole Faust _cell_bias = cell_bias;
112*c217d954SCole Faust _output_gate_bias = output_gate_bias;
113*c217d954SCole Faust
114*c217d954SCole Faust // Weights concatenation
115*c217d954SCole Faust std::vector<const ICLTensor *> inputs_weights_vector;
116*c217d954SCole Faust inputs_weights_vector.emplace_back(input_to_input_weights);
117*c217d954SCole Faust inputs_weights_vector.emplace_back(input_to_forget_weights);
118*c217d954SCole Faust inputs_weights_vector.emplace_back(input_to_cell_weights);
119*c217d954SCole Faust inputs_weights_vector.emplace_back(input_to_output_weights);
120*c217d954SCole Faust
121*c217d954SCole Faust std::vector<const ICLTensor *> recurrent_weights_vector;
122*c217d954SCole Faust recurrent_weights_vector.emplace_back(recurrent_to_input_weights);
123*c217d954SCole Faust recurrent_weights_vector.emplace_back(recurrent_to_forget_weights);
124*c217d954SCole Faust recurrent_weights_vector.emplace_back(recurrent_to_cell_weights);
125*c217d954SCole Faust recurrent_weights_vector.emplace_back(recurrent_to_output_weights);
126*c217d954SCole Faust
127*c217d954SCole Faust _input_weights.allocator()->init(TensorInfo(TensorShape(input_size, 4 * output_size), 1, DataType::QASYMM8, qweights));
128*c217d954SCole Faust _concat_input_weights.configure(compile_context, inputs_weights_vector, &_input_weights, Window::DimY);
129*c217d954SCole Faust
130*c217d954SCole Faust _recurrent_weights.allocator()->init(TensorInfo(TensorShape(output_size, 4 * output_size), 1, DataType::QASYMM8, qweights));
131*c217d954SCole Faust _concat_recurrent_weights.configure(compile_context, recurrent_weights_vector, &_recurrent_weights, Window::DimY);
132*c217d954SCole Faust
133*c217d954SCole Faust std::vector<const ICLTensor *> weights_vector;
134*c217d954SCole Faust weights_vector.emplace_back(&_recurrent_weights);
135*c217d954SCole Faust weights_vector.emplace_back(&_input_weights);
136*c217d954SCole Faust
137*c217d954SCole Faust _weights.allocator()->init(TensorInfo(TensorShape(output_size + input_size, 4 * output_size), 1, DataType::QASYMM8, qweights));
138*c217d954SCole Faust _concat_weights.configure(compile_context, weights_vector, &_weights, Window::DimX);
139*c217d954SCole Faust _transpose_weights.configure(compile_context, &_weights, &_weights_transposed);
140*c217d954SCole Faust
141*c217d954SCole Faust // Input concatenation
142*c217d954SCole Faust std::vector<const ICLTensor *> input_vector;
143*c217d954SCole Faust input_vector.emplace_back(input);
144*c217d954SCole Faust input_vector.emplace_back(output_state_in);
145*c217d954SCole Faust
146*c217d954SCole Faust _memory_group.manage(&_input);
147*c217d954SCole Faust _input.allocator()->init(TensorInfo(TensorShape(output_size + input_size, batch_size), 1, DataType::QASYMM8, qasymm));
148*c217d954SCole Faust _concat_inputs.configure(compile_context, input_vector, &_input, Window::DimX);
149*c217d954SCole Faust
150*c217d954SCole Faust // Bias concatenation
151*c217d954SCole Faust std::vector<const ICLTensor *> bias_vector;
152*c217d954SCole Faust bias_vector.emplace_back(input_gate_bias);
153*c217d954SCole Faust bias_vector.emplace_back(forget_gate_bias);
154*c217d954SCole Faust bias_vector.emplace_back(cell_bias);
155*c217d954SCole Faust bias_vector.emplace_back(output_gate_bias);
156*c217d954SCole Faust
157*c217d954SCole Faust _bias.allocator()->init(TensorInfo(TensorShape(4 * output_size), 1, DataType::S32));
158*c217d954SCole Faust _concat_bias.configure(compile_context, bias_vector, &_bias, Window::DimX);
159*c217d954SCole Faust
160*c217d954SCole Faust // Invert the offset for gemmlowp
161*c217d954SCole Faust _input.info()->set_quantization_info(QuantizationInfo(qasymm.uniform().scale, -qasymm.uniform().offset));
162*c217d954SCole Faust _weights_transposed.info()->set_quantization_info(QuantizationInfo(qweights.uniform().scale, -qweights.uniform().offset));
163*c217d954SCole Faust
164*c217d954SCole Faust // Run gemmlowp
165*c217d954SCole Faust _memory_group.manage(&_output_highp);
166*c217d954SCole Faust _output_highp.allocator()->init(TensorInfo(TensorShape(4 * output_size, batch_size), 1, DataType::S32));
167*c217d954SCole Faust _gemmlowp.configure(compile_context, &_input, &_weights_transposed, nullptr, &_output_highp);
168*c217d954SCole Faust _input.allocator()->allocate();
169*c217d954SCole Faust
170*c217d954SCole Faust // Set the offset back
171*c217d954SCole Faust _input.info()->set_quantization_info(QuantizationInfo(qasymm.uniform().scale, qasymm.uniform().offset));
172*c217d954SCole Faust _weights_transposed.info()->set_quantization_info(QuantizationInfo(qweights.uniform().scale, qweights.uniform().offset));
173*c217d954SCole Faust
174*c217d954SCole Faust // multiplier = (input_scale * weights_scale) / output_scale (2 ^ (-12))
175*c217d954SCole Faust _output_lowp.allocator()->init(TensorInfo(_output_highp.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_3));
176*c217d954SCole Faust
177*c217d954SCole Faust const float multiplier = 4096.f * qasymm.uniform().scale * qweights.uniform().scale;
178*c217d954SCole Faust int output_multiplier = 0;
179*c217d954SCole Faust int output_shift = 0;
180*c217d954SCole Faust quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
181*c217d954SCole Faust
182*c217d954SCole Faust _memory_group.manage(&_output_lowp);
183*c217d954SCole Faust
184*c217d954SCole Faust GEMMLowpOutputStageInfo info{};
185*c217d954SCole Faust info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
186*c217d954SCole Faust info.gemmlowp_multiplier = output_multiplier;
187*c217d954SCole Faust info.gemmlowp_shift = output_shift;
188*c217d954SCole Faust info.output_data_type = DataType::QSYMM16;
189*c217d954SCole Faust _output_stage.configure(compile_context, &_output_highp, &_bias, &_output_lowp, info);
190*c217d954SCole Faust _output_highp.allocator()->allocate();
191*c217d954SCole Faust _bias.allocator()->allocate();
192*c217d954SCole Faust
193*c217d954SCole Faust // Get the gate tensors
194*c217d954SCole Faust if(batch_size > 1)
195*c217d954SCole Faust {
196*c217d954SCole Faust _memory_group.manage(&_input_gate_input);
197*c217d954SCole Faust _slice_input_tensor.configure(compile_context, &_output_lowp, &_input_gate_input, { 0, 0 }, { output_size, batch_size });
198*c217d954SCole Faust _memory_group.manage(&_forget_gate_input);
199*c217d954SCole Faust _slice_forget_tensor.configure(compile_context, &_output_lowp, &_forget_gate_input, { output_size, 0 }, { 2 * output_size, batch_size });
200*c217d954SCole Faust _memory_group.manage(&_input_modulation_gate_input);
201*c217d954SCole Faust _slice_cell_tensor.configure(compile_context, &_output_lowp, &_input_modulation_gate_input, { 2 * output_size, 0 }, { 3 * output_size, batch_size });
202*c217d954SCole Faust _memory_group.manage(&_output_gate_input);
203*c217d954SCole Faust _slice_output_tensor.configure(compile_context, &_output_lowp, &_output_gate_input, { 3 * output_size, 0 }, { 4 * output_size, batch_size });
204*c217d954SCole Faust _output_lowp.allocator()->allocate();
205*c217d954SCole Faust }
206*c217d954SCole Faust else
207*c217d954SCole Faust {
208*c217d954SCole Faust _memory_group.manage(&_input_gate_input);
209*c217d954SCole Faust _slice_input_tensor.configure(compile_context, &_output_lowp, &_input_gate_input, { 0 }, { output_size });
210*c217d954SCole Faust _memory_group.manage(&_forget_gate_input);
211*c217d954SCole Faust _slice_forget_tensor.configure(compile_context, &_output_lowp, &_forget_gate_input, { output_size }, { 2 * output_size });
212*c217d954SCole Faust _memory_group.manage(&_input_modulation_gate_input);
213*c217d954SCole Faust _slice_cell_tensor.configure(compile_context, &_output_lowp, &_input_modulation_gate_input, { 2 * output_size }, { 3 * output_size });
214*c217d954SCole Faust _memory_group.manage(&_output_gate_input);
215*c217d954SCole Faust _slice_output_tensor.configure(compile_context, &_output_lowp, &_output_gate_input, { 3 * output_size }, { 4 * output_size });
216*c217d954SCole Faust _output_lowp.allocator()->allocate();
217*c217d954SCole Faust }
218*c217d954SCole Faust
219*c217d954SCole Faust // Forget gate
220*c217d954SCole Faust _memory_group.manage(&_forget_gate_output);
221*c217d954SCole Faust _forget_gate_output.allocator()->init(TensorInfo(_forget_gate_input.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
222*c217d954SCole Faust _sigmoid_forget_gate.configure(compile_context, &_forget_gate_input, &_forget_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
223*c217d954SCole Faust _forget_gate_input.allocator()->allocate();
224*c217d954SCole Faust
225*c217d954SCole Faust // Input gate
226*c217d954SCole Faust _memory_group.manage(&_input_gate_output);
227*c217d954SCole Faust _input_gate_output.allocator()->init(TensorInfo(_input_gate_input.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
228*c217d954SCole Faust _sigmoid_input_gate.configure(compile_context, &_input_gate_input, &_input_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
229*c217d954SCole Faust _input_gate_input.allocator()->allocate();
230*c217d954SCole Faust
231*c217d954SCole Faust // Input modulation gate equation
232*c217d954SCole Faust _memory_group.manage(&_input_modulation_gate_output);
233*c217d954SCole Faust _input_modulation_gate_output.allocator()->init(TensorInfo(_input_modulation_gate_input.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
234*c217d954SCole Faust _tanh_modulation_gate.configure(compile_context, &_input_modulation_gate_input, &_input_modulation_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f));
235*c217d954SCole Faust _input_modulation_gate_input.allocator()->allocate();
236*c217d954SCole Faust
237*c217d954SCole Faust // Output gate
238*c217d954SCole Faust _memory_group.manage(&_output_gate_output);
239*c217d954SCole Faust _output_gate_output.allocator()->init(TensorInfo(_output_gate_input.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
240*c217d954SCole Faust _sigmoid_output_gate.configure(compile_context, &_output_gate_input, &_output_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
241*c217d954SCole Faust _output_gate_input.allocator()->allocate();
242*c217d954SCole Faust
243*c217d954SCole Faust // Long term memory
244*c217d954SCole Faust _memory_group.manage(&_cell_state_tmp1);
245*c217d954SCole Faust _cell_state_tmp1.allocator()->init(TensorInfo(_forget_gate_output.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_4));
246*c217d954SCole Faust _mul_forget_gate_cell_state.configure(compile_context, &_forget_gate_output, cell_state_in, &_cell_state_tmp1, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
247*c217d954SCole Faust _forget_gate_output.allocator()->allocate();
248*c217d954SCole Faust
249*c217d954SCole Faust _memory_group.manage(&_cell_state_tmp2);
250*c217d954SCole Faust _cell_state_tmp2.allocator()->init(TensorInfo(_input_gate_output.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_4));
251*c217d954SCole Faust _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);
252*c217d954SCole Faust _input_modulation_gate_output.allocator()->allocate();
253*c217d954SCole Faust _input_gate_output.allocator()->allocate();
254*c217d954SCole Faust
255*c217d954SCole Faust _add_cell_state_tmps.configure(compile_context, &_cell_state_tmp1, &_cell_state_tmp2, cell_state_out, ConvertPolicy::SATURATE);
256*c217d954SCole Faust _cell_state_tmp1.allocator()->allocate();
257*c217d954SCole Faust _cell_state_tmp2.allocator()->allocate();
258*c217d954SCole Faust
259*c217d954SCole Faust // Short term memory
260*c217d954SCole Faust _memory_group.manage(&_output_state_tmp);
261*c217d954SCole Faust _output_state_tmp.allocator()->init(TensorInfo(cell_state_out->info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
262*c217d954SCole Faust _tanh_output_state.configure(compile_context, cell_state_out, &_output_state_tmp, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f));
263*c217d954SCole Faust
264*c217d954SCole Faust _memory_group.manage(&_output_state_out_symm);
265*c217d954SCole Faust _output_state_out_symm.allocator()->init(TensorInfo(_output_gate_output.info()->tensor_shape(), 1, DataType::QSYMM16, qsymm_0));
266*c217d954SCole Faust _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);
267*c217d954SCole Faust _output_gate_output.allocator()->allocate();
268*c217d954SCole Faust _output_state_tmp.allocator()->allocate();
269*c217d954SCole Faust
270*c217d954SCole Faust // Requantize the output state from QSYMM16 to QASYMM8
271*c217d954SCole Faust _memory_group.manage(&_output_state_out_f32);
272*c217d954SCole Faust _output_state_out_f32.allocator()->init(TensorInfo(_output_state_out_symm.info()->tensor_shape(), 1, DataType::F32));
273*c217d954SCole Faust _dequantize.configure(compile_context, &_output_state_out_symm, &_output_state_out_f32);
274*c217d954SCole Faust _output_state_out_symm.allocator()->allocate();
275*c217d954SCole Faust
276*c217d954SCole Faust _quantize.configure(compile_context, &_output_state_out_f32, output_state_out);
277*c217d954SCole Faust _output_state_out_f32.allocator()->allocate();
278*c217d954SCole Faust }
279*c217d954SCole Faust
validate(const ITensorInfo * input,const ITensorInfo * input_to_input_weights,const ITensorInfo * input_to_forget_weights,const ITensorInfo * input_to_cell_weights,const ITensorInfo * input_to_output_weights,const ITensorInfo * recurrent_to_input_weights,const ITensorInfo * recurrent_to_forget_weights,const ITensorInfo * recurrent_to_cell_weights,const ITensorInfo * recurrent_to_output_weights,const ITensorInfo * input_gate_bias,const ITensorInfo * forget_gate_bias,const ITensorInfo * cell_bias,const ITensorInfo * output_gate_bias,const ITensorInfo * cell_state_in,const ITensorInfo * output_state_in,const ITensorInfo * cell_state_out,const ITensorInfo * output_state_out)280*c217d954SCole Faust Status CLLSTMLayerQuantized::validate(const ITensorInfo *input,
281*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust const ITensorInfo *input_gate_bias, const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
284*c217d954SCole Faust const ITensorInfo *cell_state_in, const ITensorInfo *output_state_in,
285*c217d954SCole Faust const ITensorInfo *cell_state_out, const ITensorInfo *output_state_out)
286*c217d954SCole Faust {
287*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust output_state_in, cell_state_out, output_state_out);
290*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::QASYMM8);
291*c217d954SCole Faust
292*c217d954SCole Faust const int input_size = input->dimension(0);
293*c217d954SCole Faust const int batch_size = input->dimension(1);
294*c217d954SCole Faust const int output_size = input_to_input_weights->dimension(1);
295*c217d954SCole Faust
296*c217d954SCole Faust // Dimensionality checks
297*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2);
298*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(input_to_input_weights->num_dimensions() > 2);
299*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(input_gate_bias->num_dimensions() > 1);
300*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() > 2);
301*c217d954SCole Faust
302*c217d954SCole Faust TensorInfo input_weights_info(input_to_input_weights->clone()->set_tensor_shape(TensorShape(input_size, output_size)).set_data_type(DataType::QASYMM8));
303*c217d954SCole Faust TensorInfo recurrent_weights_info(input_to_input_weights->clone()->set_tensor_shape(TensorShape(output_size, output_size)).set_data_type(DataType::QASYMM8));
304*c217d954SCole Faust TensorInfo bias_info(input_gate_bias->clone()->set_tensor_shape(TensorShape(output_size)).set_data_type(DataType::S32));
305*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust
308*c217d954SCole Faust // Shape checks
309*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&bias_info, input_gate_bias, forget_gate_bias, cell_bias, output_gate_bias);
312*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&cell_state_info, cell_state_in);
313*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&output_state_info, output_state_in);
314*c217d954SCole Faust
315*c217d954SCole Faust // Data type checks
316*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&bias_info, input_gate_bias, forget_gate_bias, cell_bias, output_gate_bias);
319*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&cell_state_info, cell_state_in);
320*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&output_state_info, output_state_in);
321*c217d954SCole Faust
322*c217d954SCole Faust // Quantization checks
323*c217d954SCole Faust 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*c217d954SCole Faust 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*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&cell_state_info, cell_state_in);
326*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&output_state_info, output_state_in);
327*c217d954SCole Faust
328*c217d954SCole Faust // Validate internal functions
329*c217d954SCole Faust // _concat_input_weights
330*c217d954SCole Faust std::vector<const ITensorInfo *> inputs_weights_vector;
331*c217d954SCole Faust inputs_weights_vector.emplace_back(input_to_input_weights);
332*c217d954SCole Faust inputs_weights_vector.emplace_back(input_to_forget_weights);
333*c217d954SCole Faust inputs_weights_vector.emplace_back(input_to_cell_weights);
334*c217d954SCole Faust inputs_weights_vector.emplace_back(input_to_output_weights);
335*c217d954SCole Faust const QuantizationInfo qweights = input_to_input_weights->quantization_info(); // Weights quantization
336*c217d954SCole Faust const TensorInfo input_weights(TensorShape(input_size, 4 * output_size), 1, DataType::QASYMM8, qweights);
337*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLConcatenateLayer::validate(inputs_weights_vector, &input_weights, Window::DimY));
338*c217d954SCole Faust
339*c217d954SCole Faust // _concat_recurrent_weights
340*c217d954SCole Faust std::vector<const ITensorInfo *> recurrent_weights_vector;
341*c217d954SCole Faust recurrent_weights_vector.emplace_back(recurrent_to_input_weights);
342*c217d954SCole Faust recurrent_weights_vector.emplace_back(recurrent_to_forget_weights);
343*c217d954SCole Faust recurrent_weights_vector.emplace_back(recurrent_to_cell_weights);
344*c217d954SCole Faust recurrent_weights_vector.emplace_back(recurrent_to_output_weights);
345*c217d954SCole Faust const TensorInfo recurrent_weights(TensorShape(output_size, 4 * output_size), 1, DataType::QASYMM8, qweights);
346*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLConcatenateLayer::validate(recurrent_weights_vector, &recurrent_weights, Window::DimY));
347*c217d954SCole Faust
348*c217d954SCole Faust // _concat_weights
349*c217d954SCole Faust std::vector<const ITensorInfo *> weights_vector;
350*c217d954SCole Faust weights_vector.emplace_back(&recurrent_weights);
351*c217d954SCole Faust weights_vector.emplace_back(&input_weights);
352*c217d954SCole Faust const TensorInfo weights(TensorShape(input_size + output_size, 4 * output_size), 1, DataType::QASYMM8, qweights);
353*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLConcatenateLayer::validate(weights_vector, &weights, Window::DimX));
354*c217d954SCole Faust // _transpose_weights
355*c217d954SCole Faust const TensorShape weights_transposed_shape(weights.tensor_shape()[1], weights.tensor_shape()[0]);
356*c217d954SCole Faust TensorInfo weights_transposed = weights.clone()->set_is_resizable(true).set_tensor_shape(weights_transposed_shape);
357*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(&weights, &weights_transposed));
358*c217d954SCole Faust
359*c217d954SCole Faust // _concat_inputs
360*c217d954SCole Faust std::vector<const ITensorInfo *> input_vector;
361*c217d954SCole Faust input_vector.emplace_back(input);
362*c217d954SCole Faust input_vector.emplace_back(output_state_in);
363*c217d954SCole Faust TensorInfo input_concatenated(TensorShape(output_size + input_size, batch_size), 1, DataType::QASYMM8, qasymm);
364*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLConcatenateLayer::validate(input_vector, &input_concatenated, Window::DimX));
365*c217d954SCole Faust
366*c217d954SCole Faust // _concat_bias
367*c217d954SCole Faust std::vector<const ITensorInfo *> bias_vector;
368*c217d954SCole Faust bias_vector.emplace_back(input_gate_bias);
369*c217d954SCole Faust bias_vector.emplace_back(forget_gate_bias);
370*c217d954SCole Faust bias_vector.emplace_back(cell_bias);
371*c217d954SCole Faust bias_vector.emplace_back(output_gate_bias);
372*c217d954SCole Faust
373*c217d954SCole Faust const TensorInfo bias_concatenated(TensorShape(4 * output_size), 1, DataType::S32);
374*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLConcatenateLayer::validate(bias_vector, &bias_concatenated, Window::DimX));
375*c217d954SCole Faust
376*c217d954SCole Faust // Invert the offset for gemmlowp
377*c217d954SCole Faust input_concatenated.set_quantization_info(QuantizationInfo(qasymm.uniform().scale, -qasymm.uniform().offset));
378*c217d954SCole Faust weights_transposed.set_quantization_info(QuantizationInfo(qweights.uniform().scale, -qweights.uniform().offset));
379*c217d954SCole Faust
380*c217d954SCole Faust // _gemmlowp
381*c217d954SCole Faust const TensorInfo output_highp(TensorShape(4 * output_size, batch_size), 1, DataType::S32);
382*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(&input_concatenated, &weights_transposed, nullptr, &output_highp));
383*c217d954SCole Faust
384*c217d954SCole Faust // Set the offset back
385*c217d954SCole Faust input_concatenated.set_quantization_info(QuantizationInfo(qasymm.uniform().scale, qasymm.uniform().offset));
386*c217d954SCole Faust weights_transposed.set_quantization_info(QuantizationInfo(qweights.uniform().scale, qweights.uniform().offset));
387*c217d954SCole Faust
388*c217d954SCole Faust const TensorInfo output_lowp(output_highp.tensor_shape(), 1, DataType::QSYMM16, qsymm_3);
389*c217d954SCole Faust
390*c217d954SCole Faust const float multiplier = 4096.f * qasymm.uniform().scale * qweights.uniform().scale;
391*c217d954SCole Faust int output_multiplier = 0;
392*c217d954SCole Faust int output_shift = 0;
393*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift));
394*c217d954SCole Faust
395*c217d954SCole Faust // _output_stage
396*c217d954SCole Faust GEMMLowpOutputStageInfo info{};
397*c217d954SCole Faust info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
398*c217d954SCole Faust info.gemmlowp_multiplier = output_multiplier;
399*c217d954SCole Faust info.gemmlowp_shift = output_shift;
400*c217d954SCole Faust info.output_data_type = DataType::QSYMM16;
401*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&output_highp, &bias_concatenated, &output_lowp, info));
402*c217d954SCole Faust
403*c217d954SCole Faust TensorInfo input_gate_input;
404*c217d954SCole Faust TensorInfo forget_gate_input;
405*c217d954SCole Faust TensorInfo input_modulation_gate_input;
406*c217d954SCole Faust TensorInfo output_gate_input;
407*c217d954SCole Faust
408*c217d954SCole Faust if(batch_size > 1)
409*c217d954SCole Faust {
410*c217d954SCole Faust // _slice_input_tensor
411*c217d954SCole Faust input_gate_input = TensorInfo(TensorShape(output_size, batch_size), 1, DataType::QSYMM16, qsymm_3);
412*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &input_gate_input, { 0, 0 }, { output_size, batch_size }));
413*c217d954SCole Faust // _slice_forget_tensor
414*c217d954SCole Faust forget_gate_input = TensorInfo(TensorShape(output_size, batch_size), 1, DataType::QSYMM16, qsymm_3);
415*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &forget_gate_input, { output_size, 0 }, { 2 * output_size, batch_size }));
416*c217d954SCole Faust // _slice_cell_tensor
417*c217d954SCole Faust input_modulation_gate_input = TensorInfo(TensorShape(output_size, batch_size), 1, DataType::QSYMM16, qsymm_3);
418*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &input_modulation_gate_input, { 2 * output_size, 0 }, { 3 * output_size, batch_size }));
419*c217d954SCole Faust // _slice_output_tensor
420*c217d954SCole Faust output_gate_input = TensorInfo(TensorShape(output_size, batch_size), 1, DataType::QSYMM16, qsymm_3);
421*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &output_gate_input, { 3 * output_size, 0 }, { 4 * output_size, batch_size }));
422*c217d954SCole Faust }
423*c217d954SCole Faust else
424*c217d954SCole Faust {
425*c217d954SCole Faust // _slice_input_tensor
426*c217d954SCole Faust input_gate_input = TensorInfo(TensorShape(output_size), 1, DataType::QSYMM16, qsymm_3);
427*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &input_gate_input, { 0 }, { output_size }));
428*c217d954SCole Faust // _slice_forget_tensor
429*c217d954SCole Faust forget_gate_input = TensorInfo(TensorShape(output_size), 1, DataType::QSYMM16, qsymm_3);
430*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &forget_gate_input, { output_size }, { 2 * output_size }));
431*c217d954SCole Faust // _slice_cell_tensor
432*c217d954SCole Faust input_modulation_gate_input = TensorInfo(TensorShape(output_size), 1, DataType::QSYMM16, qsymm_3);
433*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &input_modulation_gate_input, { 2 * output_size }, { 3 * output_size }));
434*c217d954SCole Faust // _slice_output_tensor
435*c217d954SCole Faust output_gate_input = TensorInfo(TensorShape(output_size), 1, DataType::QSYMM16, qsymm_3);
436*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&output_lowp, &output_gate_input, { 3 * output_size }, { 4 * output_size }));
437*c217d954SCole Faust }
438*c217d954SCole Faust
439*c217d954SCole Faust // _sigmoid_forget_gate
440*c217d954SCole Faust const TensorInfo forget_gate_output(forget_gate_input.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
441*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&forget_gate_input, &forget_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
442*c217d954SCole Faust // _sigmoid_input_gate
443*c217d954SCole Faust const TensorInfo input_gate_output(input_gate_input.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
444*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&input_gate_input, &input_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
445*c217d954SCole Faust // _tanh_modulation_gate
446*c217d954SCole Faust const TensorInfo input_modulation_gate_output(input_modulation_gate_input.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
447*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&input_modulation_gate_input, &input_modulation_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f)));
448*c217d954SCole Faust // _sigmoid_output_gate
449*c217d954SCole Faust const TensorInfo output_gate_output(output_gate_input.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
450*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&output_gate_input, &output_gate_output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
451*c217d954SCole Faust
452*c217d954SCole Faust // _mul_forget_gate_cell_state
453*c217d954SCole Faust const TensorInfo cell_state_tmp1(forget_gate_output.tensor_shape(), 1, DataType::QSYMM16, qsymm_4);
454*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&forget_gate_output, cell_state_in, &cell_state_tmp1, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
455*c217d954SCole Faust
456*c217d954SCole Faust // _mul_input_gate_input_mod_gate
457*c217d954SCole Faust const TensorInfo cell_state_tmp2(input_gate_output.tensor_shape(), 1, DataType::QSYMM16, qsymm_4);
458*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&input_gate_output, &input_modulation_gate_output, &cell_state_tmp2, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
459*c217d954SCole Faust
460*c217d954SCole Faust // _add_cell_state_tmps
461*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&cell_state_tmp1, &cell_state_tmp2, cell_state_out, ConvertPolicy::SATURATE));
462*c217d954SCole Faust
463*c217d954SCole Faust // _tanh_modulation_gate
464*c217d954SCole Faust const TensorInfo output_state_tmp(cell_state_out->tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
465*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(cell_state_out, &output_state_tmp, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f)));
466*c217d954SCole Faust
467*c217d954SCole Faust // _mul_output_state_tmp_output_gate
468*c217d954SCole Faust const TensorInfo output_state_out_symm(output_gate_output.tensor_shape(), 1, DataType::QSYMM16, qsymm_0);
469*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&output_state_tmp, &output_gate_output, &output_state_out_symm, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
470*c217d954SCole Faust
471*c217d954SCole Faust // _dequantize
472*c217d954SCole Faust const TensorInfo output_state_out_f32(output_state_out_symm.tensor_shape(), 1, DataType::F32);
473*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLDequantizationLayer::validate(&output_state_out_symm, &output_state_out_f32));
474*c217d954SCole Faust
475*c217d954SCole Faust // _quantize
476*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLQuantizationLayer::validate(&output_state_out_f32, output_state_out));
477*c217d954SCole Faust
478*c217d954SCole Faust if(cell_state_out->total_size() != 0)
479*c217d954SCole Faust {
480*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&cell_state_info, cell_state_out);
481*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&cell_state_info, cell_state_out);
482*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&cell_state_info, cell_state_out);
483*c217d954SCole Faust }
484*c217d954SCole Faust
485*c217d954SCole Faust if(output_state_out->total_size() != 0)
486*c217d954SCole Faust {
487*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&output_state_info, output_state_out);
488*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&output_state_info, output_state_out);
489*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(&output_state_info, output_state_out);
490*c217d954SCole Faust }
491*c217d954SCole Faust
492*c217d954SCole Faust return Status{};
493*c217d954SCole Faust }
494*c217d954SCole Faust
run()495*c217d954SCole Faust void CLLSTMLayerQuantized::run()
496*c217d954SCole Faust {
497*c217d954SCole Faust prepare();
498*c217d954SCole Faust
499*c217d954SCole Faust // Acquire all the temporaries
500*c217d954SCole Faust MemoryGroupResourceScope scope_mg(_memory_group);
501*c217d954SCole Faust
502*c217d954SCole Faust // Concat and transpose the input
503*c217d954SCole Faust _concat_inputs.run();
504*c217d954SCole Faust
505*c217d954SCole Faust // Run gemmlowp
506*c217d954SCole Faust _gemmlowp.run();
507*c217d954SCole Faust _output_stage.run();
508*c217d954SCole Faust
509*c217d954SCole Faust // Slice the results
510*c217d954SCole Faust _slice_input_tensor.run();
511*c217d954SCole Faust _slice_forget_tensor.run();
512*c217d954SCole Faust _slice_cell_tensor.run();
513*c217d954SCole Faust _slice_output_tensor.run();
514*c217d954SCole Faust
515*c217d954SCole Faust // Gates
516*c217d954SCole Faust // Forget gate
517*c217d954SCole Faust _sigmoid_forget_gate.run();
518*c217d954SCole Faust
519*c217d954SCole Faust // Input gate
520*c217d954SCole Faust _sigmoid_input_gate.run();
521*c217d954SCole Faust
522*c217d954SCole Faust // Input modulation gate
523*c217d954SCole Faust _tanh_modulation_gate.run();
524*c217d954SCole Faust
525*c217d954SCole Faust // Output gate
526*c217d954SCole Faust _sigmoid_output_gate.run();
527*c217d954SCole Faust
528*c217d954SCole Faust // Cell state (long term memory)
529*c217d954SCole Faust _mul_forget_gate_cell_state.run();
530*c217d954SCole Faust _mul_input_gate_input_mod_gate.run();
531*c217d954SCole Faust _add_cell_state_tmps.run();
532*c217d954SCole Faust
533*c217d954SCole Faust // Output state (short term memory)
534*c217d954SCole Faust _tanh_output_state.run();
535*c217d954SCole Faust _mul_output_state_tmp_output_gate.run();
536*c217d954SCole Faust
537*c217d954SCole Faust // Requantize output state from QSYMM16 to QASYMM8
538*c217d954SCole Faust _dequantize.run();
539*c217d954SCole Faust _quantize.run();
540*c217d954SCole Faust }
541*c217d954SCole Faust
prepare()542*c217d954SCole Faust void CLLSTMLayerQuantized::prepare()
543*c217d954SCole Faust {
544*c217d954SCole Faust if(!_is_prepared)
545*c217d954SCole Faust {
546*c217d954SCole Faust _input_weights.allocator()->allocate();
547*c217d954SCole Faust _concat_input_weights.run();
548*c217d954SCole Faust
549*c217d954SCole Faust _input_to_input_weights->mark_as_unused();
550*c217d954SCole Faust _input_to_forget_weights->mark_as_unused();
551*c217d954SCole Faust _input_to_cell_weights->mark_as_unused();
552*c217d954SCole Faust _input_to_output_weights->mark_as_unused();
553*c217d954SCole Faust
554*c217d954SCole Faust _recurrent_weights.allocator()->allocate();
555*c217d954SCole Faust _concat_recurrent_weights.run();
556*c217d954SCole Faust _recurrent_to_input_weights->mark_as_unused();
557*c217d954SCole Faust _recurrent_to_forget_weights->mark_as_unused();
558*c217d954SCole Faust _recurrent_to_cell_weights->mark_as_unused();
559*c217d954SCole Faust _recurrent_to_output_weights->mark_as_unused();
560*c217d954SCole Faust
561*c217d954SCole Faust _weights.allocator()->allocate();
562*c217d954SCole Faust _concat_weights.run();
563*c217d954SCole Faust
564*c217d954SCole Faust _input_weights.mark_as_unused();
565*c217d954SCole Faust _input_weights.allocator()->free();
566*c217d954SCole Faust _recurrent_weights.mark_as_unused();
567*c217d954SCole Faust _recurrent_weights.allocator()->free();
568*c217d954SCole Faust
569*c217d954SCole Faust _weights_transposed.allocator()->allocate();
570*c217d954SCole Faust _transpose_weights.run();
571*c217d954SCole Faust
572*c217d954SCole Faust _weights.mark_as_unused();
573*c217d954SCole Faust _weights.allocator()->free();
574*c217d954SCole Faust
575*c217d954SCole Faust _bias.allocator()->allocate();
576*c217d954SCole Faust _concat_bias.run();
577*c217d954SCole Faust _input_gate_bias->mark_as_unused();
578*c217d954SCole Faust _forget_gate_bias->mark_as_unused();
579*c217d954SCole Faust _cell_bias->mark_as_unused();
580*c217d954SCole Faust _output_gate_bias->mark_as_unused();
581*c217d954SCole Faust
582*c217d954SCole Faust _is_prepared = true;
583*c217d954SCole Faust }
584*c217d954SCole Faust }
585*c217d954SCole Faust
586*c217d954SCole Faust } // namespace arm_compute
587