blob: e7a0e5765e61d0a78bb87cce7a766b98d047e631 [file] [log] [blame]
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001/*
Sheri Zhang7e20e292021-02-02 11:49:34 +00002 * Copyright (c) 2020-2021 Arm Limited.
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/runtime/CL/functions/CLQLSTMLayer.h"
25
26#include "arm_compute/core/KernelDescriptors.h"
27#include "arm_compute/core/QuantizationInfo.h"
28#include "arm_compute/core/Utils.h"
29#include "arm_compute/core/Validate.h"
30#include "arm_compute/core/utils/misc/InfoHelpers.h"
31#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
32#include "arm_compute/runtime/CL/CLScheduler.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010033#include "src/core/CL/kernels/CLDepthConvertLayerKernel.h"
34#include "src/core/CL/kernels/CLFillBorderKernel.h"
35#include "src/core/CL/kernels/CLGEMMLowpMatrixMultiplyNativeKernel.h"
36#include "src/core/CL/kernels/CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel.h"
37#include "src/core/CL/kernels/CLGEMMLowpOffsetContributionKernel.h"
38#include "src/core/CL/kernels/CLGEMMLowpOffsetContributionOutputStageKernel.h"
39#include "src/core/CL/kernels/CLGEMMLowpReductionKernel.h"
40#include "src/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.h"
41#include "src/core/CL/kernels/CLQLSTMLayerNormalizationKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010042#include "src/core/helpers/WindowHelpers.h"
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +010043
44namespace arm_compute
45{
46using namespace arm_compute::utils::info_helpers;
47namespace
48{
49Status validate_mm(GEMMLowpOutputStageInfo &gemmlowp_info, const ITensorInfo *mm_input, const ITensorInfo *mm_weights, const ITensorInfo *bias,
50 float gemmlowp_scale, const TensorInfo *mm_res_info, const TensorInfo *outstage_tensor_info)
51{
52 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(mm_input, mm_weights, nullptr, mm_res_info));
53 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(gemmlowp_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
54 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(mm_res_info, bias, outstage_tensor_info, gemmlowp_info));
55 return Status{};
56}
57} // namespace
58
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +010059Status CLQLSTMLayer::TensorCopyKernel::validate(const ITensorInfo &src, const ITensorInfo &dst)
60{
61 ARM_COMPUTE_RETURN_ERROR_ON(src.tensor_shape().num_dimensions() > max_dimension_supported);
62 ARM_COMPUTE_RETURN_ERROR_ON(dst.tensor_shape().num_dimensions() > max_dimension_supported);
63 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst);
64 ARM_COMPUTE_RETURN_ERROR_ON(dst.tensor_shape().y() != src.tensor_shape().y());
65 return Status{};
66}
67
68void CLQLSTMLayer::TensorCopyKernel::configure(ICLTensor &src, ICLTensor &dst)
69{
70 ARM_COMPUTE_ERROR_THROW_ON(CLQLSTMLayer::TensorCopyKernel::validate(*src.info(), *dst.info()));
71 _src = &src;
72 _dst = &dst;
73 _row_size = std::min(_src->info()->tensor_shape().x(), _dst->info()->tensor_shape().x());
74 _window = calculate_max_window(*_src->info(), Steps());
75}
76
77void CLQLSTMLayer::TensorCopyKernel::run()
78{
79 auto &q = CLScheduler::get().queue();
80
81 _src->map(q, true);
82 _dst->map(q, true);
83
84 Iterator input_iter{ _src, _window };
85 Iterator output_iter{ _dst, _window };
86
87 execute_window_loop(_window, [&](const Coordinates &)
88 {
89 memcpy(output_iter.ptr(), input_iter.ptr(), _row_size);
90 },
91 input_iter, output_iter);
92
93 _src->unmap(q);
94 _dst->unmap(q);
95}
96
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +010097CLQLSTMLayer::CLQLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager)
Georgios Pinitas40f51a62020-11-21 03:04:18 +000098 : _input_to_input_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
99 _recurrent_to_input_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
100 _input_to_forget_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
101 _recurrent_to_forget_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
102 _input_to_cell_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
103 _recurrent_to_cell_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
104 _input_to_output_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
105 _recurrent_to_output_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
106 _projection_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100107 _layer_norms(),
Sheri Zhang7e20e292021-02-02 11:49:34 +0000108 _copy_output()
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100109{
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100110 for(auto &norm : _layer_norms)
111 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000112 norm = std::make_unique<CLQLSTMLayerNormalizationKernel>();
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100113 }
114
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100115 _memory_group = MemoryGroup(std::move(memory_manager));
116}
117
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100118CLQLSTMLayer::~CLQLSTMLayer() = default;
119
120void CLQLSTMLayer::configure_layer_norm(LayerNormGate g, const ICLTensor *in)
121{
122 ARM_COMPUTE_ERROR_ON(!_has_layer_norm);
123
124 CLTensor *out = &get_layer_norm_output(g);
125 _memory_group.manage(out);
126 out->allocator()->init(*(in->info()));
127
128 get_layer_norm(g).configure(in, out, get_layer_norm_weight(g), get_layer_norm_bias(g));
129}
130
131Status CLQLSTMLayer::validate_layer_norm(const ITensorInfo &in, const ITensorInfo &weight, const ITensorInfo &bias)
132{
133 // Output quantization scale will be different, but ignored here
134 // since it will be configured at configure() stage.
135 const TensorInfo out
136 {
137 in
138 };
139 return CLQLSTMLayerNormalizationKernel::validate(&in, &out, &weight, &bias);
140}
141
Manuel Bottini2b84be52020-04-08 10:15:51 +0100142void CLQLSTMLayer::configure_mm(const CLCompileContext &compile_context, CLGEMMLowpMatrixMultiplyCore &mm, CLGEMMLowpOutputStage &outstage, GEMMLowpOutputStageInfo &gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100143 const ICLTensor *mm_input, const ICLTensor *mm_weights, const ICLTensor *bias,
144 CLTensor *mm_res, CLTensor *outstage_res, float gemmlowp_scale,
145 const TensorInfo &mm_res_info, const TensorInfo &outstage_tensor_info)
146{
147 _memory_group.manage(mm_res);
148 _memory_group.manage(outstage_res);
149
150 mm_res->allocator()->init(mm_res_info);
151 outstage_res->allocator()->init(outstage_tensor_info);
152
153 // Configure matrix-multiplication
Manuel Bottini2b84be52020-04-08 10:15:51 +0100154 mm.configure(compile_context, mm_input, mm_weights, nullptr, mm_res);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100155
156 // Configure output stage
157 quantization::calculate_quantized_multiplier(gemmlowp_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100158 outstage.configure(compile_context, mm_res, bias, outstage_res, gemmlowp_info);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100159 mm_res->allocator()->allocate();
160}
161
162void CLQLSTMLayer::configure(const ICLTensor *input,
163 const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
164 const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
165 const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100166 ICLTensor *cell_state_in, ICLTensor *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100167 ICLTensor *cell_state_out, ICLTensor *output_state_out, ICLTensor *output,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100168 const LSTMParams<ICLTensor> &lstm_params)
169{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100170 configure(CLKernelLibrary::get().get_compile_context(), input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
171 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights, forget_gate_bias, cell_bias, output_gate_bias,
Michalis Spyroue6bd70c2020-05-21 15:10:25 +0100172 cell_state_in, output_state_in, cell_state_out, output_state_out, output, lstm_params);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100173}
174
175void CLQLSTMLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input,
176 const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
177 const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
178 const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100179 ICLTensor *cell_state_in, ICLTensor *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100180 ICLTensor *cell_state_out, ICLTensor *output_state_out, ICLTensor *output,
Manuel Bottini2b84be52020-04-08 10:15:51 +0100181 const LSTMParams<ICLTensor> &lstm_params)
182{
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100183 ARM_COMPUTE_ERROR_ON_NULLPTR(input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
184 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100185 forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in,
186 cell_state_out, output_state_out, output);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100187
188 // Set lstm parameters
189 LSTMParams<ITensorInfo> lstm_params_info{};
190 build_lstm_params_tensor_info(lstm_params, &lstm_params_info);
191
192 // Validate
193 ARM_COMPUTE_ERROR_THROW_ON(CLQLSTMLayer::validate(input->info(), input_to_forget_weights->info(), input_to_cell_weights->info(), input_to_output_weights->info(),
194 recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(),
195 forget_gate_bias->info(), cell_bias->info(), output_gate_bias->info(),
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100196 cell_state_in->info(), output_state_in->info(), cell_state_out->info(), output_state_out->info(), output->info(),
197 lstm_params_info));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100198
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100199 const int batch_size = input->info()->dimension(1);
200 const int num_units = input_to_output_weights->info()->dimension(1);
201 const int output_size = output_state_out->info()->dimension(_out_state_output_size_dimension_idx);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100202
203 const UniformQuantizationInfo qinput = input->info()->quantization_info().uniform();
204 const UniformQuantizationInfo qcell_state_in = cell_state_in->info()->quantization_info().uniform();
205 const UniformQuantizationInfo qoutput_state_in = output_state_in->info()->quantization_info().uniform();
206
207 _projection_bias = lstm_params.projection_bias();
208 _input_to_forget_weights = input_to_forget_weights;
209 _input_to_cell_weights = input_to_cell_weights;
210 _input_to_output_weights = input_to_output_weights;
211 _recurrent_to_forget_weights = recurrent_to_forget_weights;
212 _recurrent_to_cell_weights = recurrent_to_cell_weights;
213 _recurrent_to_output_weights = recurrent_to_output_weights;
214 _projection_weights = lstm_params.projection_weights();
215
Sheri Zhang3a353982020-04-21 13:10:24 +0100216 // Layer normalization
217 _has_layer_norm = lstm_params.use_layer_norm();
218 if(_has_layer_norm)
219 {
220 set_layer_norm_weight(lstm_params.forget_layer_norm_weights(), LayerNormGate::Forget);
221 set_layer_norm_weight(lstm_params.cell_layer_norm_weights(), LayerNormGate::Cell);
222 set_layer_norm_weight(lstm_params.input_layer_norm_weights(), LayerNormGate::Input);
223 set_layer_norm_weight(lstm_params.output_layer_norm_weights(), LayerNormGate::Output);
224
225 set_layer_norm_bias(forget_gate_bias, LayerNormGate::Forget);
226 set_layer_norm_bias(cell_bias, LayerNormGate::Cell);
227 set_layer_norm_bias(lstm_params.input_gate_bias(), LayerNormGate::Input);
228 set_layer_norm_bias(output_gate_bias, LayerNormGate::Output);
229 }
230
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100231 _has_cifg = lstm_params.has_cifg_opt();
232 _has_projection = lstm_params.has_projection();
233 _has_peephole = lstm_params.has_peephole_opt();
234
235 // Calculate and decompose effective scales for optimizing matmul calculation
236 const int32_t cell_shift = log2(qcell_state_in.scale);
237
238 // Calculate quantized parameters for clipping.
239 int16_t quantized_cell_clip = 0;
240 if(lstm_params.cell_clip() > 0.0f)
241 {
242 quantized_cell_clip = quantize_qsymm16(lstm_params.cell_clip(), qcell_state_in);
243 }
244 _has_cell_clipping = quantized_cell_clip > 0;
245
246 // Precompute effective bias for optimizing the matmul computations.
247 if(!_has_cifg)
248 {
249 _input_to_input_weights = lstm_params.input_to_input_weights();
250 _recurrent_to_input_weights = lstm_params.recurrent_to_input_weights();
251
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100252 _input_to_input_reduction->configure(compile_context, _input_to_input_weights, &_input_to_input_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
253 _recurrent_to_input_reduction->configure(compile_context, _recurrent_to_input_weights, &_recurrent_to_input_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100254 }
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100255 _input_to_forget_reduction->configure(compile_context, input_to_forget_weights, &_input_to_forget_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
256 _recurrent_to_forget_reduction->configure(compile_context, recurrent_to_forget_weights, &_recurrent_to_forget_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true));
257 _input_to_cell_reduction->configure(compile_context, input_to_cell_weights, &_input_to_cell_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
258 _recurrent_to_cell_reduction->configure(compile_context, recurrent_to_cell_weights, &_recurrent_to_cell_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true));
259 _input_to_output_reduction->configure(compile_context, input_to_output_weights, &_input_to_output_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
260 _recurrent_to_output_reduction->configure(compile_context, recurrent_to_output_weights, &_recurrent_to_output_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true));
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100261 if(_has_projection)
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100262 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100263 _projection_reduction->configure(compile_context, _projection_weights, &_projection_eff_bias, GEMMLowpReductionKernelInfo(output_size, false, lstm_params.hidden_state_zero(), true));
Michele Di Giorgio11c562c2020-06-10 16:34:50 +0100264 if(_projection_bias != nullptr)
265 {
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100266 _projection_bias_add.configure(compile_context, _projection_bias, &_projection_eff_bias, &_projection_eff_bias, ConvertPolicy::SATURATE);
Michele Di Giorgio11c562c2020-06-10 16:34:50 +0100267 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100268 }
269
270 // Pre-transpose weights to be used in GEMM.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100271 _transpose_input_to_forget_weights.configure(compile_context, input_to_forget_weights, &_input_to_forget_weights_transposed);
272 _transpose_input_to_cell_weights.configure(compile_context, input_to_cell_weights, &_input_to_cell_weights_transposed);
273 _transpose_input_to_output_weights.configure(compile_context, input_to_output_weights, &_input_to_output_weights_transposed);
274 _transpose_recurrent_to_forget_weights.configure(compile_context, recurrent_to_forget_weights, &_recurrent_to_forget_weights_transposed);
275 _transpose_recurrent_to_cell_weights.configure(compile_context, recurrent_to_cell_weights, &_recurrent_to_cell_weights_transposed);
276 _transpose_recurrent_to_output_weights.configure(compile_context, recurrent_to_output_weights, &_recurrent_to_output_weights_transposed);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100277 if(!_has_cifg)
278 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100279 _transpose_input_to_input_weights.configure(compile_context, lstm_params.input_to_input_weights(), &_input_to_input_weights_transposed);
280 _transpose_recurrent_to_input_weights.configure(compile_context, lstm_params.recurrent_to_input_weights(), &_recurrent_to_input_weights_transposed);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100281 }
282 if(_has_projection)
283 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100284 _transpose_projection_weights.configure(compile_context, _projection_weights, &_projection_weights_transposed);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100285 }
286
287 GEMMLowpOutputStageInfo gemmlowp_info;
288 gemmlowp_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
289 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int16_t>::lowest();
290 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int16_t>::max();
291 gemmlowp_info.output_data_type = DataType::QSYMM16;
292
293 const TensorInfo mm_out_info(TensorShape(num_units, batch_size), 1, DataType::S32);
294 // Forget gate.
295 const TensorInfo forget_gate_outstage_info(mm_out_info.tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0));
296 const float input_to_forget_scale = input_to_forget_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.forget_intermediate_scale();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100297 configure_mm(compile_context, _mm_input_to_forget, _input_to_forget_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100298 input, &_input_to_forget_weights_transposed, &_input_to_forget_eff_bias,
299 &_mm_input_to_forget_res, &_input_to_forget_outstage_res, input_to_forget_scale,
300 mm_out_info, forget_gate_outstage_info);
301
302 const float recurrent_to_forget_scale = recurrent_to_forget_weights->info()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.forget_intermediate_scale();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100303 configure_mm(compile_context, _mm_recurrent_to_forget, _recurrent_to_forget_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100304 output_state_in, &_recurrent_to_forget_weights_transposed, &_recurrent_to_forget_eff_bias,
305 &_mm_recurrent_to_forget_res, &_recurrent_to_forget_outstage_res, recurrent_to_forget_scale,
306 mm_out_info, forget_gate_outstage_info);
307
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100308 _accumulate_input_recurrent_forget.configure(compile_context, &_input_to_forget_outstage_res, &_recurrent_to_forget_outstage_res, &_recurrent_to_forget_outstage_res,
Manuel Bottini2b84be52020-04-08 10:15:51 +0100309 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100310 _input_to_forget_outstage_res.allocator()->allocate();
311
312 if(_has_peephole)
313 {
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100314 _mul_cell_to_forget_res.allocator()->init(TensorInfo(cell_state_in->info()->tensor_shape(), 1, DataType::S32));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100315 _memory_group.manage(&_mul_cell_to_forget_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100316 _pixelwise_mul_cell_to_forget.configure(compile_context, cell_state_in, lstm_params.cell_to_forget_weights(), &_mul_cell_to_forget_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100317 _cell_to_forget_outstage_res.allocator()->init(TensorInfo(_mul_cell_to_forget_res.info()->tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0)));
318 _memory_group.manage(&_cell_to_forget_outstage_res);
319 const float cell_to_forget_scale = std::pow(2, cell_shift) * lstm_params.cell_to_forget_weights()->info()->quantization_info().uniform().scale / lstm_params.forget_intermediate_scale();
320 quantization::calculate_quantized_multiplier(cell_to_forget_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100321 _cell_to_forget_outstage.configure(compile_context, &_mul_cell_to_forget_res, nullptr, &_cell_to_forget_outstage_res, gemmlowp_info);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100322 _mul_cell_to_forget_res.allocator()->allocate();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100323 _accumulate_cell_forget.configure(compile_context, &_recurrent_to_forget_outstage_res, &_cell_to_forget_outstage_res, &_recurrent_to_forget_outstage_res,
Manuel Bottini2b84be52020-04-08 10:15:51 +0100324 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100325 _cell_to_forget_outstage_res.allocator()->allocate();
326 }
327
Sheri Zhang3a353982020-04-21 13:10:24 +0100328 CLTensor *forget_activation_input = &_recurrent_to_forget_outstage_res;
329
330 if(_has_layer_norm)
331 {
332 configure_layer_norm(LayerNormGate::Forget, &_recurrent_to_forget_outstage_res);
333 _recurrent_to_forget_outstage_res.allocator()->allocate();
334 forget_activation_input = &get_layer_norm_output(LayerNormGate::Forget);
335 }
336
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100337 // Output quantization info of Sigmoid and Tanh activations
338 const QuantizationInfo sigmoid_tanh_outqinfo(1.f / 32768.f, 0);
339
340 const TensorInfo forget_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
341 _memory_group.manage(&_forget_gate);
342 _forget_gate.allocator()->init(forget_gate_info);
Sheri Zhang3a353982020-04-21 13:10:24 +0100343 _forget_gate_sigmoid.configure(compile_context, forget_activation_input, &_forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
344 forget_activation_input->allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100345
346 // Modulation gate.
347 const TensorInfo cell_outstage_info(mm_out_info.tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.cell_intermediate_scale(), 0));
348 const float input_to_cell_scale = input_to_cell_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.cell_intermediate_scale();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100349 configure_mm(compile_context, _mm_input_to_cell, _input_to_cell_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100350 input, &_input_to_cell_weights_transposed, &_input_to_cell_eff_bias,
351 &_mm_input_to_cell_res, &_input_to_cell_outstage_res, input_to_cell_scale,
352 mm_out_info, cell_outstage_info);
353
354 const float recurrent_to_cell_scale = recurrent_to_cell_weights->info()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.cell_intermediate_scale();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100355 configure_mm(compile_context, _mm_recurrent_to_cell, _recurrent_to_cell_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100356 output_state_in, &_recurrent_to_cell_weights_transposed, &_recurrent_to_cell_eff_bias,
357 &_mm_recurrent_to_cell_res, &_recurrent_to_cell_outstage_res, recurrent_to_cell_scale,
358 mm_out_info, cell_outstage_info);
359
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100360 _accumulate_input_recurrent_modulation.configure(compile_context, &_input_to_cell_outstage_res, &_recurrent_to_cell_outstage_res, &_recurrent_to_cell_outstage_res,
Manuel Bottini2b84be52020-04-08 10:15:51 +0100361 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100362 _input_to_cell_outstage_res.allocator()->allocate();
363
Sheri Zhang3a353982020-04-21 13:10:24 +0100364 CLTensor *cell_activation_input = &_recurrent_to_cell_outstage_res;
365
366 if(_has_layer_norm)
367 {
368 configure_layer_norm(LayerNormGate::Cell, &_recurrent_to_cell_outstage_res);
369 _recurrent_to_cell_outstage_res.allocator()->allocate();
370 cell_activation_input = &get_layer_norm_output(LayerNormGate::Cell);
371 }
372
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100373 const TensorInfo cell_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
374 _memory_group.manage(&_cell_gate);
375 _cell_gate.allocator()->init(cell_gate_info);
Sheri Zhang3a353982020-04-21 13:10:24 +0100376 _cell_gate_tanh.configure(compile_context, cell_activation_input, &_cell_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f));
377 cell_activation_input->allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100378
379 // Input gate.
380 const TensorInfo input_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
381 _input_gate.allocator()->init(input_gate_info);
382 _memory_group.manage(&_input_gate);
383 if(_has_cifg)
384 {
385 _ones.allocator()->init(*_forget_gate.info());
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100386 _input_gate_sub.configure(compile_context, &_ones, &_forget_gate, &_input_gate, ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100387 _ones.allocator()->allocate();
388 }
389 else
390 {
391 const TensorInfo input_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0));
392 const float input_to_input_scale = _input_to_input_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.input_intermediate_scale();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100393 configure_mm(compile_context, _mm_input_to_input, _input_to_input_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100394 input, &_input_to_input_weights_transposed, &_input_to_input_eff_bias,
395 &_mm_input_to_input_res, &_input_to_input_outstage_res, input_to_input_scale,
396 mm_out_info, input_outstage_info);
397
398 const float recurrent_to_input_scale = _recurrent_to_input_weights->info()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.input_intermediate_scale();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100399 configure_mm(compile_context, _mm_recurrent_to_input, _recurrent_to_input_outstage, gemmlowp_info,
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100400 output_state_in, &_recurrent_to_input_weights_transposed, &_recurrent_to_input_eff_bias,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100401 &_mm_recurrent_to_input_res, &_recurrent_to_input_outstage_res, recurrent_to_input_scale,
402 mm_out_info, input_outstage_info);
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100403 _accumulate_input_recurrent_input.configure(compile_context, &_input_to_input_outstage_res, &_recurrent_to_input_outstage_res, &_recurrent_to_input_outstage_res,
Manuel Bottini2b84be52020-04-08 10:15:51 +0100404 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100405 _input_to_input_outstage_res.allocator()->allocate();
406
407 if(_has_peephole)
408 {
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100409 _mul_cell_to_input_res.allocator()->init(TensorInfo(cell_state_in->info()->tensor_shape(), 1, DataType::S32));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100410 _memory_group.manage(&_mul_cell_to_input_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100411 _pixelwise_mul_cell_to_input.configure(compile_context, cell_state_in, lstm_params.cell_to_input_weights(), &_mul_cell_to_input_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100412 const float cell_to_input_scale = std::pow(2, cell_shift) * lstm_params.cell_to_input_weights()->info()->quantization_info().uniform().scale / lstm_params.input_intermediate_scale();
413 quantization::calculate_quantized_multiplier(cell_to_input_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
414 _cell_to_input_outstage_res.allocator()->init(TensorInfo(_mul_cell_to_input_res.info()->tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0)));
415 _memory_group.manage(&_cell_to_input_outstage_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100416 _cell_to_input_outstage.configure(compile_context, &_mul_cell_to_input_res, nullptr, &_cell_to_input_outstage_res, gemmlowp_info);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100417 _mul_cell_to_input_res.allocator()->allocate();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100418 _accumulate_cell_input.configure(&_recurrent_to_input_outstage_res, &_cell_to_input_outstage_res, &_recurrent_to_input_outstage_res, ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100419 _cell_to_input_outstage_res.allocator()->allocate();
420 }
421
Sheri Zhang3a353982020-04-21 13:10:24 +0100422 CLTensor *input_activation_input = &_recurrent_to_input_outstage_res;
423
424 if(_has_layer_norm)
425 {
426 configure_layer_norm(LayerNormGate::Input, &_recurrent_to_input_outstage_res);
427 _recurrent_to_input_outstage_res.allocator()->allocate();
428 input_activation_input = &get_layer_norm_output(LayerNormGate::Input);
429 }
430
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100431 _input_gate_sigmoid.configure(compile_context, input_activation_input, &_input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Sheri Zhang3a353982020-04-21 13:10:24 +0100432 input_activation_input->allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100433 }
434 // Cell.
Michalis Spyrou1009e872020-07-27 12:48:34 +0100435 // TODO(COMPMID-3396): Perform multiplication in the quantized domain in CLPixelWiseMultiplication
Manuel Bottini2b84be52020-04-08 10:15:51 +0100436 _pixelwise_mul_forget_cell.configure(compile_context, &_forget_gate, cell_state_in, &_forget_gate, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100437 const float cell_gate_scale = _cell_gate.info()->quantization_info().uniform().scale;
438 const float mul_input_cell_scale = cell_gate_scale * std::pow(2, 15 + cell_shift);
439 const TensorInfo mul_input_cell_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(mul_input_cell_scale, 0));
440 _memory_group.manage(&_mul_input_cell_res);
441 _mul_input_cell_res.allocator()->init(mul_input_cell_info);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100442 _pixelwise_mul_input_cell.configure(compile_context, &_input_gate, &_cell_gate, &_mul_input_cell_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100443 _cell_gate.allocator()->allocate();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100444 _add_forget_cell.configure(compile_context, &_forget_gate, &_mul_input_cell_res, cell_state_out, ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100445 _mul_input_cell_res.allocator()->allocate();
446 _forget_gate.allocator()->allocate();
447 if(_has_cell_clipping)
448 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100449 _cell_clip.configure(compile_context, cell_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_cell_clip, quantized_cell_clip));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100450 }
451 // Output gate.
452 const TensorInfo output_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.output_intermediate_scale(), 0));
453 const float input_to_output_scale = input_to_output_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.output_intermediate_scale();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100454 configure_mm(compile_context, _mm_input_to_output, _input_to_output_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100455 input, &_input_to_output_weights_transposed, &_input_to_output_eff_bias,
456 &_mm_input_to_output_res, &_input_to_output_outstage_res, input_to_output_scale,
457 mm_out_info, output_outstage_info);
458
459 const float recurrent_to_output_scale = recurrent_to_output_weights->info()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.output_intermediate_scale();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100460 configure_mm(compile_context, _mm_recurrent_to_output, _recurrent_to_output_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100461 output_state_in, &_recurrent_to_output_weights_transposed, &_recurrent_to_output_eff_bias,
462 &_mm_recurrent_to_output_res, &_recurrent_to_output_outstage_res, recurrent_to_output_scale,
463 mm_out_info, output_outstage_info);
464
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100465 _accumulate_input_recurrent_output.configure(compile_context, &_recurrent_to_output_outstage_res, &_input_to_output_outstage_res, &_recurrent_to_output_outstage_res,
Manuel Bottini2b84be52020-04-08 10:15:51 +0100466 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100467 _input_to_output_outstage_res.allocator()->allocate();
468
469 if(_has_peephole)
470 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100471 // TODO(COMPMID-3396): Perform multiplication in the quantized domain in CLPixelWiseMultiplication
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100472 // Here we are not using the output stage because all operations are done in float
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100473 _mul_cell_to_output_res.allocator()->init(TensorInfo(cell_state_out->info()->tensor_shape(), 1, DataType::S32));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100474 _memory_group.manage(&_mul_cell_to_output_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100475 _pixelwise_mul_cell_to_output.configure(compile_context, cell_state_out, lstm_params.cell_to_output_weights(), &_mul_cell_to_output_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100476
477 const float cell_to_output_scale = std::pow(2, cell_shift) * lstm_params.cell_to_output_weights()->info()->quantization_info().uniform().scale / lstm_params.output_intermediate_scale();
478 quantization::calculate_quantized_multiplier(cell_to_output_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
479 _cell_to_output_outstage_res.allocator()->init(TensorInfo(_mul_cell_to_output_res.info()->tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.output_intermediate_scale(), 0)));
480 _memory_group.manage(&_cell_to_output_outstage_res);
481 _cell_to_output_outstage.configure(compile_context, &_mul_cell_to_output_res, nullptr, &_cell_to_output_outstage_res, gemmlowp_info);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100482 _mul_cell_to_output_res.allocator()->allocate();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100483
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100484 _accumulate_cell_to_output.configure(compile_context, &_recurrent_to_output_outstage_res, &_cell_to_output_outstage_res, &_recurrent_to_output_outstage_res,
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100485 ConvertPolicy::SATURATE);
486 _cell_to_output_outstage_res.allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100487 }
488
Sheri Zhang3a353982020-04-21 13:10:24 +0100489 CLTensor *output_activation_input = &_recurrent_to_output_outstage_res;
490
491 if(_has_layer_norm)
492 {
493 configure_layer_norm(LayerNormGate::Output, &_recurrent_to_output_outstage_res);
494 _recurrent_to_output_outstage_res.allocator()->allocate();
495 output_activation_input = &get_layer_norm_output(LayerNormGate::Output);
496 }
497
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100498 const TensorInfo output_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
499 _memory_group.manage(&_output_gate);
500 _output_gate.allocator()->init(output_gate_info);
Sheri Zhang3a353982020-04-21 13:10:24 +0100501 _output_gate_sigmoid.configure(compile_context, output_activation_input, &_output_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
502 output_activation_input->allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100503
504 // Hidden.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100505 _hidden_tanh.configure(compile_context, cell_state_out, &_input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100506 // TODO(COMPMID-3396): Perform multiplication in the quantized domain in CLPixelWiseMultiplication
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100507 _memory_group.manage(&_hidden_mul_res);
508 const TensorInfo hidden_mul_res(_input_gate.info()->tensor_shape(), 1, DataType::S32);
509 _hidden_mul_res.allocator()->init(hidden_mul_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100510 _pixelwise_mul_hidden.configure(compile_context, &_output_gate, &_input_gate, &_hidden_mul_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100511 _output_gate.allocator()->allocate();
512 _input_gate.allocator()->allocate();
513 const float hidden_state_scale = std::pow(2, -15) / lstm_params.hidden_state_scale() * std::pow(2, -15);
514 quantization::calculate_quantized_multiplier(hidden_state_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift, /* ignore_epsilon */ true);
515 gemmlowp_info.gemmlowp_offset = lstm_params.hidden_state_zero();
516 gemmlowp_info.output_data_type = output_state_in->info()->data_type();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100517
518 _projection_tensor_copy_required = (num_units != output_size);
519 ICLTensor *hidden_gate_result = output_state_out;
520
521 _memory_group.manage(&_hidden_gate);
522
523 if(_projection_tensor_copy_required)
524 {
525 _hidden_gate.allocator()->init(*output_state_out->info());
526 _hidden_gate.info()->set_tensor_shape(_hidden_mul_res.info()->tensor_shape());
527 hidden_gate_result = &_hidden_gate;
528 }
529
530 _hidden_outstage.configure(compile_context, &_hidden_mul_res, nullptr, hidden_gate_result, gemmlowp_info);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100531 _hidden_mul_res.allocator()->allocate();
532
533 // Projection.
534 if(_has_projection)
535 {
536 const TensorInfo projection_outstage_info(*output_state_out->info());
537 const UniformQuantizationInfo qprojection = _projection_weights->info()->quantization_info().uniform();
538 const float projection_scale = qprojection.scale * lstm_params.hidden_state_scale() / qoutput_state_in.scale;
539 gemmlowp_info.gemmlowp_offset = qoutput_state_in.offset;
540 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int8_t>::lowest();
541 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int8_t>::max();
542 gemmlowp_info.output_data_type = DataType::QASYMM8_SIGNED;
543
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100544 TensorInfo projection_mm_out_info{ mm_out_info };
545 projection_mm_out_info.set_tensor_shape(TensorShape(output_size, batch_size));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100546
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100547 configure_mm(compile_context, _mm_projection, _projection_outstage, gemmlowp_info,
548 hidden_gate_result, &_projection_weights_transposed, &_projection_eff_bias,
549 &_mm_projection_res, &_projection_outstage_res, projection_scale,
550 projection_mm_out_info, projection_outstage_info);
551
552 ICLTensor *accumulate_destination = output_state_out;
553
554 if(_projection_tensor_copy_required)
555 {
556 _hidden_gate.allocator()->allocate();
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100557 _projection_accumulate_res.allocator()->init(*output_state_in->info());
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100558 _projection_accumulate_res.info()->set_tensor_shape(_projection_outstage_res.info()->tensor_shape());
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100559 _projection_output_to_accumulate_copy.configure(*output_state_in, _projection_accumulate_res);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100560 accumulate_destination = &_projection_accumulate_res;
561 }
562
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100563 _accumulate_projection.configure(compile_context, &_projection_outstage_res, accumulate_destination, accumulate_destination, ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100564 _projection_outstage_res.allocator()->allocate();
565
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100566 if(_projection_tensor_copy_required)
567 {
568 _projection_accumulate_to_output_copy.configure(_projection_accumulate_res, *output_state_out);
569 _projection_accumulate_res.allocator()->allocate();
570 }
571
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100572 int8_t quantized_projection_clip{ 0 };
573 if(lstm_params.projection_clip() > 0.0f)
574 {
575 quantized_projection_clip = utility::clamp<int8_t>(lstm_params.projection_clip() / qprojection.scale, -128, 127);
576 }
577
578 if(quantized_projection_clip > 0)
579 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100580 _projection_clip.configure(compile_context, output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_projection_clip,
581 quantized_projection_clip));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100582 _has_projection_clipping = true;
583 }
584 }
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100585 else
586 {
587 if(_projection_tensor_copy_required)
588 {
589 _hidden_to_output_copy.configure(_hidden_gate, *output_state_out);
590 _hidden_gate.allocator()->allocate();
591 }
592 }
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100593
594 // Copy output_state_out to output
Sheri Zhang7e20e292021-02-02 11:49:34 +0000595 _copy_output.configure(compile_context, output_state_out, output);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100596}
597
598Status CLQLSTMLayer::validate(const ITensorInfo *input,
599 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
600 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
601 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
602 const ITensorInfo *cell_state_in, const ITensorInfo *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100603 const ITensorInfo *cell_state_out, const ITensorInfo *output_state_out, const ITensorInfo *output,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100604 const LSTMParams<ITensorInfo> &lstm_params)
605{
606 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights, recurrent_to_forget_weights, recurrent_to_cell_weights,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100607 recurrent_to_output_weights, forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in,
608 cell_state_out, output_state_out, output);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100609
610 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED);
611 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() != 2, "Input must have exactly 2 dimensions");
612
613 const unsigned int input_size = input->dimension(0);
614 const unsigned int batch_size = input->dimension(1);
615 const unsigned int num_units = input_to_output_weights->dimension(1);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100616 const unsigned int output_size = output_state_out->dimension(_out_state_output_size_dimension_idx);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100617
618 ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->num_dimensions() != 2);
619 ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->dimension(0) != input_size);
620 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_to_output_weights, input_to_forget_weights, input_to_cell_weights);
621 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->num_dimensions() != 2);
622 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->dimension(1) != num_units);
623 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(recurrent_to_output_weights, recurrent_to_forget_weights, recurrent_to_cell_weights);
624 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_to_forget_weights, 1, DataType::QSYMM8);
625 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
626 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights);
627
628 ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->num_dimensions() != 1);
629 ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->dimension(0) != num_units);
630 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(forget_gate_bias, cell_bias, output_gate_bias);
631 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(forget_gate_bias, 1, DataType::S32);
632 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(forget_gate_bias, cell_bias, output_gate_bias);
633
634 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->num_dimensions() != 2);
635 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->dimension(0) != num_units);
636 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->dimension(1) != batch_size);
637 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(cell_state_in, 1, DataType::QSYMM16);
638
639 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() != 2);
640 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->dimension(0) != output_size);
641 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->dimension(1) != batch_size);
642 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output_state_in);
643
644 // Check whether peephole weights are all there or none
645 if(lstm_params.has_peephole_opt())
646 {
647 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights());
648 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_forget_weights(), 1, DataType::QSYMM16);
649 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->num_dimensions() != 1);
650 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->dimension(0) != num_units);
651 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights());
652 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights());
653
654 if(!lstm_params.has_cifg_opt())
655 {
656 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_input_weights());
657 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_input_weights());
658 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_input_weights());
659 }
660 }
661
662 const UniformQuantizationInfo qinput = input->quantization_info().uniform();
663 const UniformQuantizationInfo qcell_state_in = cell_state_in->quantization_info().uniform();
664 const UniformQuantizationInfo qoutput_state_in = output_state_in->quantization_info().uniform();
665
666 // Calculate and decompose effective scales for optimizing matmul calculation
667 const int32_t cell_shift = log2(qcell_state_in.scale);
668 ARM_COMPUTE_RETURN_ERROR_ON(cell_shift > -9);
669
670 // Calculate quantized parameters for clipping.
671 int16_t quantized_cell_clip = 0;
672 if(lstm_params.cell_clip() > 0.0f)
673 {
674 quantized_cell_clip = quantize_qsymm16(lstm_params.cell_clip(), qcell_state_in);
675 }
676
677 // Precompute effective bias for optimizing the matmul computations.
678 const TensorInfo eff_bias_info(TensorShape(num_units), 1, DataType::S32);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100679 const TensorInfo projection_eff_bias_info(TensorShape(output_size), 1, DataType::S32);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100680 if(!lstm_params.has_cifg_opt())
681 {
682 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(lstm_params.input_to_input_weights(), &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
683 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(lstm_params.recurrent_to_input_weights(), &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset,
684 true)));
685 }
686 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(input_to_forget_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
687 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(recurrent_to_forget_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true)));
688 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(input_to_cell_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
689 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(recurrent_to_cell_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true)));
690 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(input_to_output_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
691 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(recurrent_to_output_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true)));
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100692 if(lstm_params.has_projection())
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100693 {
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100694 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(lstm_params.projection_weights(), &projection_eff_bias_info, GEMMLowpReductionKernelInfo(output_size, false,
695 lstm_params.hidden_state_zero(),
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100696 true)));
Michele Di Giorgio11c562c2020-06-10 16:34:50 +0100697 if(lstm_params.projection_bias() != nullptr)
698 {
699 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.projection_bias(), 1, DataType::S32);
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100700 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(lstm_params.projection_bias(), &projection_eff_bias_info,
701 &projection_eff_bias_info, ConvertPolicy::SATURATE));
Michele Di Giorgio11c562c2020-06-10 16:34:50 +0100702 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100703 }
704
705 const TensorInfo input_weights_transposed(TensorShape(num_units, input_size), 1, input_to_forget_weights->data_type(), input_to_forget_weights->quantization_info());
706 const TensorInfo recurrent_weights_transposed(TensorShape(num_units, output_size), 1, recurrent_to_forget_weights->data_type(), recurrent_to_forget_weights->quantization_info());
707
708 // Validate weights transpose
709 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(input_to_forget_weights, &input_weights_transposed));
710 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(input_to_cell_weights, &input_weights_transposed));
711 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(input_to_output_weights, &input_weights_transposed));
712 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(recurrent_to_forget_weights, &recurrent_weights_transposed));
713 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(recurrent_to_cell_weights, &recurrent_weights_transposed));
714 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(recurrent_to_output_weights, &recurrent_weights_transposed));
715 if(!lstm_params.has_cifg_opt())
716 {
717 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(lstm_params.input_to_input_weights(), &input_weights_transposed));
718 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(lstm_params.recurrent_to_input_weights(), &recurrent_weights_transposed));
719 }
720 if(lstm_params.has_projection())
721 {
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100722 const TensorInfo projection_weights_transposed(TensorShape(output_size, num_units), 1, lstm_params.projection_weights()->data_type(), lstm_params.projection_weights()->quantization_info());
723 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(lstm_params.projection_weights(), &projection_weights_transposed));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100724 }
725
726 GEMMLowpOutputStageInfo gemmlowp_info;
727 gemmlowp_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
728 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int16_t>::lowest();
729 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int16_t>::max();
730 gemmlowp_info.output_data_type = DataType::QSYMM16;
731
Sheri Zhang3a353982020-04-21 13:10:24 +0100732 const bool has_layer_norm = lstm_params.use_layer_norm();
733
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100734 // Forget gate.
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100735 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.forget_intermediate_scale() == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100736 const TensorInfo forget_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0));
737 const TensorInfo mm_out_info(TensorShape(num_units, batch_size), 1, DataType::S32);
738 const float input_to_forget_scale = input_to_forget_weights->quantization_info().uniform().scale * qinput.scale / lstm_params.forget_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100739 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, input_to_forget_scale, &mm_out_info, &forget_outstage_info));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100740
741 const float recurrent_to_forget_scale = recurrent_to_forget_weights->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.forget_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100742 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, output_state_in, &recurrent_weights_transposed, &eff_bias_info, recurrent_to_forget_scale, &mm_out_info, &forget_outstage_info));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100743
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100744 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&forget_outstage_info, &forget_outstage_info, &forget_outstage_info, ConvertPolicy::SATURATE));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100745
746 if(lstm_params.has_peephole_opt())
747 {
748 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_forget_weights(), 1, DataType::QSYMM16);
Michalis Spyrou1009e872020-07-27 12:48:34 +0100749 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(cell_state_in, lstm_params.cell_to_forget_weights(), &mm_out_info, 1.f, ConvertPolicy::SATURATE,
750 RoundingPolicy::TO_ZERO));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100751 const float cell_to_forget_scale = std::pow(2, cell_shift) * lstm_params.cell_to_forget_weights()->quantization_info().uniform().scale / lstm_params.forget_intermediate_scale();
752 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(cell_to_forget_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
753 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&mm_out_info, nullptr, &forget_outstage_info, gemmlowp_info));
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100754 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&forget_outstage_info, &forget_outstage_info, &forget_outstage_info, ConvertPolicy::SATURATE));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100755 }
756
Sheri Zhang3a353982020-04-21 13:10:24 +0100757 if(has_layer_norm)
758 {
759 const ITensorInfo *w_info = lstm_params.forget_layer_norm_weights();
760 const ITensorInfo *b_info = forget_gate_bias;
761 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(forget_outstage_info, *w_info, *b_info));
762 }
763
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100764 // Output quantization info of Sigmoid and Tanh activations
765 const QuantizationInfo sigmoid_tanh_outqinfo(1.f / 32768.f, 0);
766
767 const TensorInfo forget_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
768 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&forget_outstage_info, &forget_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
769
770 // Modulation gate.
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100771 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_intermediate_scale() == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100772 const TensorInfo cell_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.cell_intermediate_scale(), 0));
773 const float input_to_cell_scale = input_to_cell_weights->quantization_info().uniform().scale * qinput.scale / lstm_params.cell_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100774 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, input_to_cell_scale, &mm_out_info, &cell_outstage_info));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100775
776 const float recurrent_to_cell_scale = recurrent_to_cell_weights->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.cell_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100777 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, output_state_in, &input_weights_transposed, &eff_bias_info, recurrent_to_cell_scale, &mm_out_info, &cell_outstage_info));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100778
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100779 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&cell_outstage_info, &cell_outstage_info, &cell_outstage_info, ConvertPolicy::SATURATE));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100780
Sheri Zhang3a353982020-04-21 13:10:24 +0100781 if(has_layer_norm)
782 {
783 const ITensorInfo *w_info = lstm_params.cell_layer_norm_weights();
784 const ITensorInfo *b_info = cell_bias;
785 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(cell_outstage_info, *w_info, *b_info));
786 }
787
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100788 const TensorInfo cell_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
789 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&cell_outstage_info, &cell_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)));
790
791 // Input gate.
792 const TensorInfo input_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
793 if(lstm_params.has_cifg_opt())
794 {
795 ARM_COMPUTE_RETURN_ERROR_ON_MSG(lstm_params.input_gate_bias() != nullptr, "Input gate bias must not be present when CIFG is used");
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100796 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticSubtraction::validate(&input_gate_info, &forget_gate_info, &forget_gate_info, ConvertPolicy::SATURATE));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100797 }
798 else
799 {
800 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights(), lstm_params.input_gate_bias());
801 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_to_forget_weights, lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights());
802 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_to_forget_weights, lstm_params.input_to_input_weights());
803 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(recurrent_to_forget_weights, lstm_params.recurrent_to_input_weights());
804 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(forget_gate_bias, lstm_params.input_gate_bias());
805 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(forget_gate_bias, lstm_params.input_gate_bias());
806
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100807 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_intermediate_scale() == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100808 const TensorInfo input_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0));
809 const float input_to_input_scale = lstm_params.input_to_input_weights()->quantization_info().uniform().scale * qinput.scale / lstm_params.input_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100810 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, input_to_input_scale, &mm_out_info, &input_outstage_info));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100811
812 const float recurrent_to_input_scale = lstm_params.recurrent_to_input_weights()->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.input_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100813 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, output_state_in, &recurrent_weights_transposed, &eff_bias_info, recurrent_to_input_scale, &mm_out_info, &input_outstage_info));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100814
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100815 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&input_outstage_info, &input_outstage_info, &input_outstage_info, ConvertPolicy::SATURATE));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100816
817 if(lstm_params.has_peephole_opt())
818 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100819 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(cell_state_in, lstm_params.cell_to_input_weights(), &mm_out_info, 1.f, ConvertPolicy::SATURATE,
820 RoundingPolicy::TO_ZERO));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100821 const float cell_to_input_scale = std::pow(2, cell_shift) * lstm_params.cell_to_input_weights()->quantization_info().uniform().scale / lstm_params.input_intermediate_scale();
822 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(cell_to_input_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100823 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&mm_out_info, &eff_bias_info, &input_outstage_info, gemmlowp_info));
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100824 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&input_outstage_info, &input_outstage_info, &input_outstage_info, ConvertPolicy::SATURATE));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100825 }
826
Sheri Zhang3a353982020-04-21 13:10:24 +0100827 if(has_layer_norm)
828 {
829 const ITensorInfo *w_info = lstm_params.input_layer_norm_weights();
830 const ITensorInfo *b_info = lstm_params.input_gate_bias();
831 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(cell_outstage_info, *w_info, *b_info));
832 }
833
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100834 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&input_outstage_info, &input_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC, 1.f, 1.f)));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100835 }
836 // Cell.
Michalis Spyrou1009e872020-07-27 12:48:34 +0100837 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&forget_gate_info, cell_state_in, &forget_gate_info, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
838 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&input_gate_info, cell_state_in, &cell_gate_info, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100839 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&forget_gate_info, &cell_gate_info, cell_state_out, ConvertPolicy::SATURATE));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100840 if(quantized_cell_clip > 0)
841 {
842 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(cell_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_cell_clip,
843 quantized_cell_clip)));
844 }
845 // Output gate.
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100846 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.output_intermediate_scale() == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100847 const TensorInfo output_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.output_intermediate_scale(), 0));
848 const float input_to_output_scale = input_to_output_weights->quantization_info().uniform().scale * qinput.scale / lstm_params.output_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100849 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, input_to_output_scale, &mm_out_info, &output_outstage_info));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100850
851 const float recurrent_to_output_scale = recurrent_to_output_weights->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.output_intermediate_scale();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100852 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, output_state_in, &recurrent_weights_transposed, &eff_bias_info, recurrent_to_output_scale, &mm_out_info, &output_outstage_info));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100853
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100854 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&output_outstage_info, &output_outstage_info, &output_outstage_info, ConvertPolicy::SATURATE));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100855 if(lstm_params.has_peephole_opt())
856 {
857 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_output_weights(), 1, DataType::QSYMM16);
858 // TODO(COMPMID-3395): Perform multiplication in the quantized domain in NEPixelWiseMultiplicationKernel
859 // Here we are not using the output stage because all operations are done in float
860 // const float cell_to_output_scale = std::pow(2, cell_shift) * lstm_params.cell_to_output_weights()->quantization_info().uniform().scale / lstm_params.output_intermediate_scale();
861 // ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(cell_to_output_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
Michalis Spyrou1009e872020-07-27 12:48:34 +0100862 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(cell_state_out, lstm_params.cell_to_output_weights(), &output_outstage_info, 1.f, ConvertPolicy::SATURATE,
863 RoundingPolicy::TO_ZERO));
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100864 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&output_outstage_info, &output_outstage_info, &output_outstage_info, ConvertPolicy::SATURATE));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100865 }
866
Sheri Zhang3a353982020-04-21 13:10:24 +0100867 if(has_layer_norm)
868 {
869 const ITensorInfo *w_info = lstm_params.output_layer_norm_weights();
870 const ITensorInfo *b_info = output_gate_bias;
871 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(output_outstage_info, *w_info, *b_info));
872 }
873
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100874 const TensorInfo output_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
875 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&output_outstage_info, &output_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
876
877 // Hidden.
878 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(cell_state_out, &input_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)));
879 const TensorInfo hidden_mul_res(TensorShape(num_units, batch_size), 1, DataType::S32);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100880 const TensorInfo hidden_out_info(TensorShape(num_units, batch_size), 1, DataType::QASYMM8_SIGNED);
881
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100882 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.hidden_state_scale() == 0);
Michalis Spyrou1009e872020-07-27 12:48:34 +0100883 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&output_gate_info, &input_gate_info, &hidden_mul_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100884 const float hidden_state_scale = std::pow(2, -15) / lstm_params.hidden_state_scale() * std::pow(2, -15);
885 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(hidden_state_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift, /* ignore_epsilon */ true));
Sang-Hoon Park9f893752020-10-20 15:33:31 +0100886 gemmlowp_info.gemmlowp_offset = lstm_params.hidden_state_zero();
887 gemmlowp_info.output_data_type = hidden_out_info.data_type();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100888 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&hidden_mul_res, nullptr, &hidden_out_info, gemmlowp_info));
889
890 const bool projection_tensor_copy_required = num_units != output_size;
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100891
892 // Projection.
893 if(lstm_params.has_projection())
894 {
895 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(recurrent_to_forget_weights, lstm_params.projection_weights());
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100896 ARM_COMPUTE_RETURN_ERROR_ON(qoutput_state_in.scale == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100897
898 const UniformQuantizationInfo qprojection = lstm_params.projection_weights()->quantization_info().uniform();
899 const float projection_scale = qprojection.scale * lstm_params.hidden_state_scale() / qoutput_state_in.scale;
900 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(projection_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
901 gemmlowp_info.gemmlowp_offset = qoutput_state_in.offset;
902 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int8_t>::lowest();
903 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int8_t>::max();
904 gemmlowp_info.output_data_type = DataType::QASYMM8_SIGNED;
905
906 const TensorInfo projection_outstage_info(*output_state_out);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100907 const TensorInfo projection_weights_transposed(TensorShape(output_size, num_units), 1, lstm_params.projection_weights()->data_type(), lstm_params.projection_weights()->quantization_info());
908
909 TensorInfo projection_mm_out_info{ mm_out_info };
910 projection_mm_out_info.set_tensor_shape(TensorShape(output_size, batch_size));
911
912 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemmlowp_info, &hidden_out_info, &projection_weights_transposed, &projection_eff_bias_info, projection_scale, &projection_mm_out_info,
913 &projection_outstage_info));
914
915 if(projection_tensor_copy_required)
916 {
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100917 ARM_COMPUTE_RETURN_ON_ERROR(CLQLSTMLayer::TensorCopyKernel::validate(*output_state_in, projection_outstage_info));
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100918 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100919
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100920 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(output_state_out, output_state_out, output_state_out, ConvertPolicy::SATURATE));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100921
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100922 if(projection_tensor_copy_required)
923 {
924 ARM_COMPUTE_RETURN_ON_ERROR(CLQLSTMLayer::TensorCopyKernel::validate(projection_outstage_info, *output_state_out));
925 }
926
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100927 int8_t quantized_projection_clip{ 0 };
928 if(lstm_params.projection_clip() > 0.0f)
929 {
930 quantized_projection_clip = quantize_qasymm8_signed(lstm_params.projection_clip(), qprojection);
931 }
932
933 if(quantized_projection_clip > 0)
934 {
935 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_projection_clip,
936 quantized_projection_clip)));
937 }
938 }
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100939 else
940 {
941 if(projection_tensor_copy_required)
942 {
943 ARM_COMPUTE_RETURN_ON_ERROR(CLQLSTMLayer::TensorCopyKernel::validate(hidden_out_info, *output_state_out));
944 }
945 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100946
947 if(cell_state_out->total_size() > 0)
948 {
949 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(cell_state_in, cell_state_out);
950 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(cell_state_in, cell_state_out);
951 }
952
953 if(output_state_out->total_size() > 0)
954 {
955 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output_state_out);
956 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output_state_in, output_state_out);
957 }
958
Sheri Zhang7e20e292021-02-02 11:49:34 +0000959 ARM_COMPUTE_RETURN_ON_ERROR(CLCopy::validate(output_state_out, output));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100960 return Status{};
961}
962
963void CLQLSTMLayer::run()
964{
965 prepare();
966
967 // Acquire all the temporaries
968 MemoryGroupResourceScope scope_mg(_memory_group);
969
970 // Forget gate.
971 _mm_input_to_forget.run();
972 _input_to_forget_outstage.run();
973
974 _mm_recurrent_to_forget.run();
975 _recurrent_to_forget_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100976 _accumulate_input_recurrent_forget.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100977
978 if(_has_peephole)
979 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100980 _pixelwise_mul_cell_to_forget.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100981 _cell_to_forget_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100982 _accumulate_cell_forget.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100983 }
984
Sheri Zhang3a353982020-04-21 13:10:24 +0100985 if(_has_layer_norm)
986 {
987 CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Forget));
988 }
989
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100990 _forget_gate_sigmoid.run();
991
992 // Modulation gate.
993 _mm_input_to_cell.run();
994 _input_to_cell_outstage.run();
995
996 _mm_recurrent_to_cell.run();
997 _recurrent_to_cell_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100998 _accumulate_input_recurrent_modulation.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100999
Sheri Zhang3a353982020-04-21 13:10:24 +01001000 if(_has_layer_norm)
1001 {
1002 CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Cell));
1003 }
1004
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001005 _cell_gate_tanh.run();
1006
1007 // Input gate
1008 if(_has_cifg)
1009 {
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001010 _input_gate_sub.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001011 }
1012 else
1013 {
1014 _mm_input_to_input.run();
1015 _input_to_input_outstage.run();
1016 _mm_recurrent_to_input.run();
1017 _recurrent_to_input_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001018 _accumulate_input_recurrent_input.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001019
1020 if(_has_peephole)
1021 {
Michalis Spyrou1009e872020-07-27 12:48:34 +01001022 _pixelwise_mul_cell_to_input.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001023 _cell_to_input_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001024 _accumulate_cell_input.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001025 }
1026
Sheri Zhang3a353982020-04-21 13:10:24 +01001027 if(_has_layer_norm)
1028 {
1029 CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Input));
1030 }
1031
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001032 _input_gate_sigmoid.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001033 }
1034
1035 // Cell.
Michalis Spyrou1009e872020-07-27 12:48:34 +01001036 _pixelwise_mul_forget_cell.run();
1037 _pixelwise_mul_input_cell.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001038 _add_forget_cell.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001039 if(_has_cell_clipping)
1040 {
1041 _cell_clip.run();
1042 }
1043
1044 // Output gate.
1045 _mm_input_to_output.run();
1046 _input_to_output_outstage.run();
1047 _mm_recurrent_to_output.run();
1048 _recurrent_to_output_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001049 _accumulate_input_recurrent_output.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001050 if(_has_peephole)
1051 {
Michalis Spyrou1009e872020-07-27 12:48:34 +01001052 _pixelwise_mul_cell_to_output.run();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001053 _cell_to_output_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001054 _accumulate_cell_to_output.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001055 }
1056
Sheri Zhang3a353982020-04-21 13:10:24 +01001057 if(_has_layer_norm)
1058 {
1059 CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Output));
1060 }
1061
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001062 _output_gate_sigmoid.run();
1063
1064 // Hidden.
1065 _hidden_tanh.run();
Michalis Spyrou1009e872020-07-27 12:48:34 +01001066 _pixelwise_mul_hidden.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001067 _hidden_outstage.run();
1068
1069 // Projection.
1070 if(_has_projection)
1071 {
1072 _mm_projection.run();
1073 _projection_outstage.run();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001074
1075 if(_projection_tensor_copy_required)
1076 {
1077 _projection_output_to_accumulate_copy.run();
1078 }
1079
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001080 _accumulate_projection.run();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001081
1082 if(_projection_tensor_copy_required)
1083 {
1084 _projection_accumulate_to_output_copy.run();
1085 }
1086
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001087 if(_has_projection_clipping)
1088 {
1089 _projection_clip.run();
1090 }
1091 }
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001092 else
1093 {
1094 if(_projection_tensor_copy_required)
1095 {
1096 _hidden_to_output_copy.run();
1097 }
1098 }
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +01001099
1100 // Copy output_state_out to output
Sheri Zhang7e20e292021-02-02 11:49:34 +00001101 _copy_output.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001102}
1103
1104void CLQLSTMLayer::prepare()
1105{
1106 if(!_is_prepared)
1107 {
1108 // Pre-transpose weights to be used in GEMM.
1109 _input_to_forget_weights_transposed.allocator()->allocate();
1110 _input_to_cell_weights_transposed.allocator()->allocate();
1111 _input_to_output_weights_transposed.allocator()->allocate();
1112 _recurrent_to_forget_weights_transposed.allocator()->allocate();
1113 _recurrent_to_cell_weights_transposed.allocator()->allocate();
1114 _recurrent_to_output_weights_transposed.allocator()->allocate();
1115 _transpose_input_to_forget_weights.run();
1116 _transpose_input_to_cell_weights.run();
1117 _transpose_input_to_output_weights.run();
1118 _transpose_recurrent_to_forget_weights.run();
1119 _transpose_recurrent_to_cell_weights.run();
1120 _transpose_recurrent_to_output_weights.run();
1121
1122 // Precompute effective biases
1123 if(_has_cifg)
1124 {
1125 _ones.map(true);
1126 std::fill_n(reinterpret_cast<int16_t *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 32767);
1127 _ones.unmap();
1128 }
1129 else
1130 {
1131 _input_to_input_eff_bias.allocator()->allocate();
1132 _recurrent_to_input_eff_bias.allocator()->allocate();
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +01001133 CLScheduler::get().enqueue(*_input_to_input_reduction);
1134 CLScheduler::get().enqueue(*_recurrent_to_input_reduction);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001135
1136 _input_to_input_weights_transposed.allocator()->allocate();
1137 _recurrent_to_input_weights_transposed.allocator()->allocate();
1138 _transpose_input_to_input_weights.run();
1139 _transpose_recurrent_to_input_weights.run();
1140 _input_to_input_weights->mark_as_unused();
1141 _recurrent_to_input_weights->mark_as_unused();
1142 }
1143 _input_to_forget_eff_bias.allocator()->allocate();
1144 _recurrent_to_forget_eff_bias.allocator()->allocate();
1145 _input_to_cell_eff_bias.allocator()->allocate();
1146 _recurrent_to_cell_eff_bias.allocator()->allocate();
1147 _input_to_output_eff_bias.allocator()->allocate();
1148 _recurrent_to_output_eff_bias.allocator()->allocate();
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +01001149 CLScheduler::get().enqueue(*_input_to_forget_reduction);
1150 CLScheduler::get().enqueue(*_recurrent_to_forget_reduction);
1151 CLScheduler::get().enqueue(*_input_to_cell_reduction);
1152 CLScheduler::get().enqueue(*_recurrent_to_cell_reduction);
1153 CLScheduler::get().enqueue(*_input_to_output_reduction);
1154 CLScheduler::get().enqueue(*_recurrent_to_output_reduction);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001155
1156 if(_has_projection)
1157 {
Michele Di Giorgio11c562c2020-06-10 16:34:50 +01001158 _projection_eff_bias.allocator()->allocate();
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +01001159 CLScheduler::get().enqueue(*_projection_reduction);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001160 if(_projection_bias != nullptr)
1161 {
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001162 _projection_bias_add.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001163 _projection_bias->mark_as_unused();
1164 }
1165
1166 _projection_weights_transposed.allocator()->allocate();
1167 _transpose_projection_weights.run();
1168 _projection_weights->mark_as_unused();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001169
1170 if(!_projection_tensor_copy_required)
1171 {
1172 _hidden_gate.mark_as_unused();
1173 _projection_accumulate_res.mark_as_unused();
1174 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001175 }
1176
1177 // Mark weights as unused
1178 _input_to_forget_weights->mark_as_unused();
1179 _input_to_cell_weights->mark_as_unused();
1180 _input_to_output_weights->mark_as_unused();
1181 _recurrent_to_forget_weights->mark_as_unused();
1182 _recurrent_to_cell_weights->mark_as_unused();
1183 _recurrent_to_output_weights->mark_as_unused();
1184
1185 CLScheduler::get().queue().finish();
1186 _is_prepared = true;
1187 }
1188}
1189
1190} // namespace arm_compute