blob: 54df5a0a5ea20119c3ba8faa3a1331877cbde33b [file] [log] [blame]
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2020 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/CLCopyKernel.h"
34#include "src/core/CL/kernels/CLDepthConvertLayerKernel.h"
35#include "src/core/CL/kernels/CLFillBorderKernel.h"
36#include "src/core/CL/kernels/CLGEMMLowpMatrixMultiplyNativeKernel.h"
37#include "src/core/CL/kernels/CLGEMMLowpMatrixMultiplyReshapedOnlyRHSKernel.h"
38#include "src/core/CL/kernels/CLGEMMLowpOffsetContributionKernel.h"
39#include "src/core/CL/kernels/CLGEMMLowpOffsetContributionOutputStageKernel.h"
40#include "src/core/CL/kernels/CLGEMMLowpReductionKernel.h"
41#include "src/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.h"
42#include "src/core/CL/kernels/CLQLSTMLayerNormalizationKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010043#include "src/core/helpers/WindowHelpers.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010044#include "support/MemorySupport.h"
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +010045
46namespace arm_compute
47{
48using namespace arm_compute::utils::info_helpers;
49namespace
50{
51Status validate_mm(GEMMLowpOutputStageInfo &gemmlowp_info, const ITensorInfo *mm_input, const ITensorInfo *mm_weights, const ITensorInfo *bias,
52 float gemmlowp_scale, const TensorInfo *mm_res_info, const TensorInfo *outstage_tensor_info)
53{
54 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(mm_input, mm_weights, nullptr, mm_res_info));
55 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(gemmlowp_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
56 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(mm_res_info, bias, outstage_tensor_info, gemmlowp_info));
57 return Status{};
58}
59} // namespace
60
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +010061Status CLQLSTMLayer::TensorCopyKernel::validate(const ITensorInfo &src, const ITensorInfo &dst)
62{
63 ARM_COMPUTE_RETURN_ERROR_ON(src.tensor_shape().num_dimensions() > max_dimension_supported);
64 ARM_COMPUTE_RETURN_ERROR_ON(dst.tensor_shape().num_dimensions() > max_dimension_supported);
65 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst);
66 ARM_COMPUTE_RETURN_ERROR_ON(dst.tensor_shape().y() != src.tensor_shape().y());
67 return Status{};
68}
69
70void CLQLSTMLayer::TensorCopyKernel::configure(ICLTensor &src, ICLTensor &dst)
71{
72 ARM_COMPUTE_ERROR_THROW_ON(CLQLSTMLayer::TensorCopyKernel::validate(*src.info(), *dst.info()));
73 _src = &src;
74 _dst = &dst;
75 _row_size = std::min(_src->info()->tensor_shape().x(), _dst->info()->tensor_shape().x());
76 _window = calculate_max_window(*_src->info(), Steps());
77}
78
79void CLQLSTMLayer::TensorCopyKernel::run()
80{
81 auto &q = CLScheduler::get().queue();
82
83 _src->map(q, true);
84 _dst->map(q, true);
85
86 Iterator input_iter{ _src, _window };
87 Iterator output_iter{ _dst, _window };
88
89 execute_window_loop(_window, [&](const Coordinates &)
90 {
91 memcpy(output_iter.ptr(), input_iter.ptr(), _row_size);
92 },
93 input_iter, output_iter);
94
95 _src->unmap(q);
96 _dst->unmap(q);
97}
98
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +010099CLQLSTMLayer::CLQLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager)
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100100 : _input_to_input_reduction(support::cpp14::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
101 _recurrent_to_input_reduction(support::cpp14::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
102 _input_to_forget_reduction(support::cpp14::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
103 _recurrent_to_forget_reduction(support::cpp14::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
104 _input_to_cell_reduction(support::cpp14::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
105 _recurrent_to_cell_reduction(support::cpp14::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
106 _input_to_output_reduction(support::cpp14::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
107 _recurrent_to_output_reduction(support::cpp14::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
108 _projection_reduction(support::cpp14::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
109 _layer_norms(),
110 _copy_output(support::cpp14::make_unique<CLCopyKernel>())
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100111{
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100112 for(auto &norm : _layer_norms)
113 {
114 norm = support::cpp14::make_unique<CLQLSTMLayerNormalizationKernel>();
115 }
116
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100117 _memory_group = MemoryGroup(std::move(memory_manager));
118}
119
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100120CLQLSTMLayer::~CLQLSTMLayer() = default;
121
122void CLQLSTMLayer::configure_layer_norm(LayerNormGate g, const ICLTensor *in)
123{
124 ARM_COMPUTE_ERROR_ON(!_has_layer_norm);
125
126 CLTensor *out = &get_layer_norm_output(g);
127 _memory_group.manage(out);
128 out->allocator()->init(*(in->info()));
129
130 get_layer_norm(g).configure(in, out, get_layer_norm_weight(g), get_layer_norm_bias(g));
131}
132
133Status CLQLSTMLayer::validate_layer_norm(const ITensorInfo &in, const ITensorInfo &weight, const ITensorInfo &bias)
134{
135 // Output quantization scale will be different, but ignored here
136 // since it will be configured at configure() stage.
137 const TensorInfo out
138 {
139 in
140 };
141 return CLQLSTMLayerNormalizationKernel::validate(&in, &out, &weight, &bias);
142}
143
Manuel Bottini2b84be52020-04-08 10:15:51 +0100144void CLQLSTMLayer::configure_mm(const CLCompileContext &compile_context, CLGEMMLowpMatrixMultiplyCore &mm, CLGEMMLowpOutputStage &outstage, GEMMLowpOutputStageInfo &gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100145 const ICLTensor *mm_input, const ICLTensor *mm_weights, const ICLTensor *bias,
146 CLTensor *mm_res, CLTensor *outstage_res, float gemmlowp_scale,
147 const TensorInfo &mm_res_info, const TensorInfo &outstage_tensor_info)
148{
149 _memory_group.manage(mm_res);
150 _memory_group.manage(outstage_res);
151
152 mm_res->allocator()->init(mm_res_info);
153 outstage_res->allocator()->init(outstage_tensor_info);
154
155 // Configure matrix-multiplication
Manuel Bottini2b84be52020-04-08 10:15:51 +0100156 mm.configure(compile_context, mm_input, mm_weights, nullptr, mm_res);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100157
158 // Configure output stage
159 quantization::calculate_quantized_multiplier(gemmlowp_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100160 outstage.configure(compile_context, mm_res, bias, outstage_res, gemmlowp_info);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100161 mm_res->allocator()->allocate();
162}
163
164void CLQLSTMLayer::configure(const ICLTensor *input,
165 const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
166 const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
167 const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100168 ICLTensor *cell_state_in, ICLTensor *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100169 ICLTensor *cell_state_out, ICLTensor *output_state_out, ICLTensor *output,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100170 const LSTMParams<ICLTensor> &lstm_params)
171{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100172 configure(CLKernelLibrary::get().get_compile_context(), input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
173 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 +0100174 cell_state_in, output_state_in, cell_state_out, output_state_out, output, lstm_params);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100175}
176
177void CLQLSTMLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input,
178 const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
179 const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
180 const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100181 ICLTensor *cell_state_in, ICLTensor *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100182 ICLTensor *cell_state_out, ICLTensor *output_state_out, ICLTensor *output,
Manuel Bottini2b84be52020-04-08 10:15:51 +0100183 const LSTMParams<ICLTensor> &lstm_params)
184{
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100185 ARM_COMPUTE_ERROR_ON_NULLPTR(input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
186 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100187 forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in,
188 cell_state_out, output_state_out, output);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100189
190 // Set lstm parameters
191 LSTMParams<ITensorInfo> lstm_params_info{};
192 build_lstm_params_tensor_info(lstm_params, &lstm_params_info);
193
194 // Validate
195 ARM_COMPUTE_ERROR_THROW_ON(CLQLSTMLayer::validate(input->info(), input_to_forget_weights->info(), input_to_cell_weights->info(), input_to_output_weights->info(),
196 recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(),
197 forget_gate_bias->info(), cell_bias->info(), output_gate_bias->info(),
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100198 cell_state_in->info(), output_state_in->info(), cell_state_out->info(), output_state_out->info(), output->info(),
199 lstm_params_info));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100200
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100201 const int batch_size = input->info()->dimension(1);
202 const int num_units = input_to_output_weights->info()->dimension(1);
203 const int output_size = output_state_out->info()->dimension(_out_state_output_size_dimension_idx);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100204
205 const UniformQuantizationInfo qinput = input->info()->quantization_info().uniform();
206 const UniformQuantizationInfo qcell_state_in = cell_state_in->info()->quantization_info().uniform();
207 const UniformQuantizationInfo qoutput_state_in = output_state_in->info()->quantization_info().uniform();
208
209 _projection_bias = lstm_params.projection_bias();
210 _input_to_forget_weights = input_to_forget_weights;
211 _input_to_cell_weights = input_to_cell_weights;
212 _input_to_output_weights = input_to_output_weights;
213 _recurrent_to_forget_weights = recurrent_to_forget_weights;
214 _recurrent_to_cell_weights = recurrent_to_cell_weights;
215 _recurrent_to_output_weights = recurrent_to_output_weights;
216 _projection_weights = lstm_params.projection_weights();
217
Sheri Zhang3a353982020-04-21 13:10:24 +0100218 // Layer normalization
219 _has_layer_norm = lstm_params.use_layer_norm();
220 if(_has_layer_norm)
221 {
222 set_layer_norm_weight(lstm_params.forget_layer_norm_weights(), LayerNormGate::Forget);
223 set_layer_norm_weight(lstm_params.cell_layer_norm_weights(), LayerNormGate::Cell);
224 set_layer_norm_weight(lstm_params.input_layer_norm_weights(), LayerNormGate::Input);
225 set_layer_norm_weight(lstm_params.output_layer_norm_weights(), LayerNormGate::Output);
226
227 set_layer_norm_bias(forget_gate_bias, LayerNormGate::Forget);
228 set_layer_norm_bias(cell_bias, LayerNormGate::Cell);
229 set_layer_norm_bias(lstm_params.input_gate_bias(), LayerNormGate::Input);
230 set_layer_norm_bias(output_gate_bias, LayerNormGate::Output);
231 }
232
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100233 _has_cifg = lstm_params.has_cifg_opt();
234 _has_projection = lstm_params.has_projection();
235 _has_peephole = lstm_params.has_peephole_opt();
236
237 // Calculate and decompose effective scales for optimizing matmul calculation
238 const int32_t cell_shift = log2(qcell_state_in.scale);
239
240 // Calculate quantized parameters for clipping.
241 int16_t quantized_cell_clip = 0;
242 if(lstm_params.cell_clip() > 0.0f)
243 {
244 quantized_cell_clip = quantize_qsymm16(lstm_params.cell_clip(), qcell_state_in);
245 }
246 _has_cell_clipping = quantized_cell_clip > 0;
247
248 // Precompute effective bias for optimizing the matmul computations.
249 if(!_has_cifg)
250 {
251 _input_to_input_weights = lstm_params.input_to_input_weights();
252 _recurrent_to_input_weights = lstm_params.recurrent_to_input_weights();
253
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100254 _input_to_input_reduction->configure(compile_context, _input_to_input_weights, &_input_to_input_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
255 _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 +0100256 }
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100257 _input_to_forget_reduction->configure(compile_context, input_to_forget_weights, &_input_to_forget_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
258 _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));
259 _input_to_cell_reduction->configure(compile_context, input_to_cell_weights, &_input_to_cell_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
260 _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));
261 _input_to_output_reduction->configure(compile_context, input_to_output_weights, &_input_to_output_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
262 _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 +0100263 if(_has_projection)
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100264 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100265 _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 +0100266 if(_projection_bias != nullptr)
267 {
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100268 _projection_bias_add.configure(compile_context, _projection_bias, &_projection_eff_bias, &_projection_eff_bias, ConvertPolicy::SATURATE);
Michele Di Giorgio11c562c2020-06-10 16:34:50 +0100269 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100270 }
271
272 // Pre-transpose weights to be used in GEMM.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100273 _transpose_input_to_forget_weights.configure(compile_context, input_to_forget_weights, &_input_to_forget_weights_transposed);
274 _transpose_input_to_cell_weights.configure(compile_context, input_to_cell_weights, &_input_to_cell_weights_transposed);
275 _transpose_input_to_output_weights.configure(compile_context, input_to_output_weights, &_input_to_output_weights_transposed);
276 _transpose_recurrent_to_forget_weights.configure(compile_context, recurrent_to_forget_weights, &_recurrent_to_forget_weights_transposed);
277 _transpose_recurrent_to_cell_weights.configure(compile_context, recurrent_to_cell_weights, &_recurrent_to_cell_weights_transposed);
278 _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 +0100279 if(!_has_cifg)
280 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100281 _transpose_input_to_input_weights.configure(compile_context, lstm_params.input_to_input_weights(), &_input_to_input_weights_transposed);
282 _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 +0100283 }
284 if(_has_projection)
285 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100286 _transpose_projection_weights.configure(compile_context, _projection_weights, &_projection_weights_transposed);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100287 }
288
289 GEMMLowpOutputStageInfo gemmlowp_info;
290 gemmlowp_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
291 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int16_t>::lowest();
292 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int16_t>::max();
293 gemmlowp_info.output_data_type = DataType::QSYMM16;
294
295 const TensorInfo mm_out_info(TensorShape(num_units, batch_size), 1, DataType::S32);
296 // Forget gate.
297 const TensorInfo forget_gate_outstage_info(mm_out_info.tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0));
298 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 +0100299 configure_mm(compile_context, _mm_input_to_forget, _input_to_forget_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100300 input, &_input_to_forget_weights_transposed, &_input_to_forget_eff_bias,
301 &_mm_input_to_forget_res, &_input_to_forget_outstage_res, input_to_forget_scale,
302 mm_out_info, forget_gate_outstage_info);
303
304 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 +0100305 configure_mm(compile_context, _mm_recurrent_to_forget, _recurrent_to_forget_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100306 output_state_in, &_recurrent_to_forget_weights_transposed, &_recurrent_to_forget_eff_bias,
307 &_mm_recurrent_to_forget_res, &_recurrent_to_forget_outstage_res, recurrent_to_forget_scale,
308 mm_out_info, forget_gate_outstage_info);
309
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100310 _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 +0100311 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100312 _input_to_forget_outstage_res.allocator()->allocate();
313
314 if(_has_peephole)
315 {
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100316 _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 +0100317 _memory_group.manage(&_mul_cell_to_forget_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100318 _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 +0100319 _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)));
320 _memory_group.manage(&_cell_to_forget_outstage_res);
321 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();
322 quantization::calculate_quantized_multiplier(cell_to_forget_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100323 _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 +0100324 _mul_cell_to_forget_res.allocator()->allocate();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100325 _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 +0100326 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100327 _cell_to_forget_outstage_res.allocator()->allocate();
328 }
329
Sheri Zhang3a353982020-04-21 13:10:24 +0100330 CLTensor *forget_activation_input = &_recurrent_to_forget_outstage_res;
331
332 if(_has_layer_norm)
333 {
334 configure_layer_norm(LayerNormGate::Forget, &_recurrent_to_forget_outstage_res);
335 _recurrent_to_forget_outstage_res.allocator()->allocate();
336 forget_activation_input = &get_layer_norm_output(LayerNormGate::Forget);
337 }
338
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100339 // Output quantization info of Sigmoid and Tanh activations
340 const QuantizationInfo sigmoid_tanh_outqinfo(1.f / 32768.f, 0);
341
342 const TensorInfo forget_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
343 _memory_group.manage(&_forget_gate);
344 _forget_gate.allocator()->init(forget_gate_info);
Sheri Zhang3a353982020-04-21 13:10:24 +0100345 _forget_gate_sigmoid.configure(compile_context, forget_activation_input, &_forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
346 forget_activation_input->allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100347
348 // Modulation gate.
349 const TensorInfo cell_outstage_info(mm_out_info.tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.cell_intermediate_scale(), 0));
350 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 +0100351 configure_mm(compile_context, _mm_input_to_cell, _input_to_cell_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100352 input, &_input_to_cell_weights_transposed, &_input_to_cell_eff_bias,
353 &_mm_input_to_cell_res, &_input_to_cell_outstage_res, input_to_cell_scale,
354 mm_out_info, cell_outstage_info);
355
356 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 +0100357 configure_mm(compile_context, _mm_recurrent_to_cell, _recurrent_to_cell_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100358 output_state_in, &_recurrent_to_cell_weights_transposed, &_recurrent_to_cell_eff_bias,
359 &_mm_recurrent_to_cell_res, &_recurrent_to_cell_outstage_res, recurrent_to_cell_scale,
360 mm_out_info, cell_outstage_info);
361
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100362 _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 +0100363 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100364 _input_to_cell_outstage_res.allocator()->allocate();
365
Sheri Zhang3a353982020-04-21 13:10:24 +0100366 CLTensor *cell_activation_input = &_recurrent_to_cell_outstage_res;
367
368 if(_has_layer_norm)
369 {
370 configure_layer_norm(LayerNormGate::Cell, &_recurrent_to_cell_outstage_res);
371 _recurrent_to_cell_outstage_res.allocator()->allocate();
372 cell_activation_input = &get_layer_norm_output(LayerNormGate::Cell);
373 }
374
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100375 const TensorInfo cell_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
376 _memory_group.manage(&_cell_gate);
377 _cell_gate.allocator()->init(cell_gate_info);
Sheri Zhang3a353982020-04-21 13:10:24 +0100378 _cell_gate_tanh.configure(compile_context, cell_activation_input, &_cell_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f));
379 cell_activation_input->allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100380
381 // Input gate.
382 const TensorInfo input_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
383 _input_gate.allocator()->init(input_gate_info);
384 _memory_group.manage(&_input_gate);
385 if(_has_cifg)
386 {
387 _ones.allocator()->init(*_forget_gate.info());
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100388 _input_gate_sub.configure(compile_context, &_ones, &_forget_gate, &_input_gate, ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100389 _ones.allocator()->allocate();
390 }
391 else
392 {
393 const TensorInfo input_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0));
394 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 +0100395 configure_mm(compile_context, _mm_input_to_input, _input_to_input_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100396 input, &_input_to_input_weights_transposed, &_input_to_input_eff_bias,
397 &_mm_input_to_input_res, &_input_to_input_outstage_res, input_to_input_scale,
398 mm_out_info, input_outstage_info);
399
400 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 +0100401 configure_mm(compile_context, _mm_recurrent_to_input, _recurrent_to_input_outstage, gemmlowp_info,
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100402 output_state_in, &_recurrent_to_input_weights_transposed, &_recurrent_to_input_eff_bias,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100403 &_mm_recurrent_to_input_res, &_recurrent_to_input_outstage_res, recurrent_to_input_scale,
404 mm_out_info, input_outstage_info);
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100405 _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 +0100406 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100407 _input_to_input_outstage_res.allocator()->allocate();
408
409 if(_has_peephole)
410 {
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100411 _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 +0100412 _memory_group.manage(&_mul_cell_to_input_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100413 _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 +0100414 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();
415 quantization::calculate_quantized_multiplier(cell_to_input_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
416 _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)));
417 _memory_group.manage(&_cell_to_input_outstage_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100418 _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 +0100419 _mul_cell_to_input_res.allocator()->allocate();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100420 _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 +0100421 _cell_to_input_outstage_res.allocator()->allocate();
422 }
423
Sheri Zhang3a353982020-04-21 13:10:24 +0100424 CLTensor *input_activation_input = &_recurrent_to_input_outstage_res;
425
426 if(_has_layer_norm)
427 {
428 configure_layer_norm(LayerNormGate::Input, &_recurrent_to_input_outstage_res);
429 _recurrent_to_input_outstage_res.allocator()->allocate();
430 input_activation_input = &get_layer_norm_output(LayerNormGate::Input);
431 }
432
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100433 _input_gate_sigmoid.configure(compile_context, input_activation_input, &_input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Sheri Zhang3a353982020-04-21 13:10:24 +0100434 input_activation_input->allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100435 }
436 // Cell.
Michalis Spyrou1009e872020-07-27 12:48:34 +0100437 // TODO(COMPMID-3396): Perform multiplication in the quantized domain in CLPixelWiseMultiplication
Manuel Bottini2b84be52020-04-08 10:15:51 +0100438 _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 +0100439 const float cell_gate_scale = _cell_gate.info()->quantization_info().uniform().scale;
440 const float mul_input_cell_scale = cell_gate_scale * std::pow(2, 15 + cell_shift);
441 const TensorInfo mul_input_cell_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(mul_input_cell_scale, 0));
442 _memory_group.manage(&_mul_input_cell_res);
443 _mul_input_cell_res.allocator()->init(mul_input_cell_info);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100444 _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 +0100445 _cell_gate.allocator()->allocate();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100446 _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 +0100447 _mul_input_cell_res.allocator()->allocate();
448 _forget_gate.allocator()->allocate();
449 if(_has_cell_clipping)
450 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100451 _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 +0100452 }
453 // Output gate.
454 const TensorInfo output_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.output_intermediate_scale(), 0));
455 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 +0100456 configure_mm(compile_context, _mm_input_to_output, _input_to_output_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100457 input, &_input_to_output_weights_transposed, &_input_to_output_eff_bias,
458 &_mm_input_to_output_res, &_input_to_output_outstage_res, input_to_output_scale,
459 mm_out_info, output_outstage_info);
460
461 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 +0100462 configure_mm(compile_context, _mm_recurrent_to_output, _recurrent_to_output_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100463 output_state_in, &_recurrent_to_output_weights_transposed, &_recurrent_to_output_eff_bias,
464 &_mm_recurrent_to_output_res, &_recurrent_to_output_outstage_res, recurrent_to_output_scale,
465 mm_out_info, output_outstage_info);
466
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100467 _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 +0100468 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100469 _input_to_output_outstage_res.allocator()->allocate();
470
471 if(_has_peephole)
472 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100473 // TODO(COMPMID-3396): Perform multiplication in the quantized domain in CLPixelWiseMultiplication
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100474 // Here we are not using the output stage because all operations are done in float
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100475 _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 +0100476 _memory_group.manage(&_mul_cell_to_output_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100477 _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 +0100478
479 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();
480 quantization::calculate_quantized_multiplier(cell_to_output_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
481 _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)));
482 _memory_group.manage(&_cell_to_output_outstage_res);
483 _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 +0100484 _mul_cell_to_output_res.allocator()->allocate();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100485
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100486 _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 +0100487 ConvertPolicy::SATURATE);
488 _cell_to_output_outstage_res.allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100489 }
490
Sheri Zhang3a353982020-04-21 13:10:24 +0100491 CLTensor *output_activation_input = &_recurrent_to_output_outstage_res;
492
493 if(_has_layer_norm)
494 {
495 configure_layer_norm(LayerNormGate::Output, &_recurrent_to_output_outstage_res);
496 _recurrent_to_output_outstage_res.allocator()->allocate();
497 output_activation_input = &get_layer_norm_output(LayerNormGate::Output);
498 }
499
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100500 const TensorInfo output_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
501 _memory_group.manage(&_output_gate);
502 _output_gate.allocator()->init(output_gate_info);
Sheri Zhang3a353982020-04-21 13:10:24 +0100503 _output_gate_sigmoid.configure(compile_context, output_activation_input, &_output_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
504 output_activation_input->allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100505
506 // Hidden.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100507 _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 +0100508 // TODO(COMPMID-3396): Perform multiplication in the quantized domain in CLPixelWiseMultiplication
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100509 _memory_group.manage(&_hidden_mul_res);
510 const TensorInfo hidden_mul_res(_input_gate.info()->tensor_shape(), 1, DataType::S32);
511 _hidden_mul_res.allocator()->init(hidden_mul_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100512 _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 +0100513 _output_gate.allocator()->allocate();
514 _input_gate.allocator()->allocate();
515 const float hidden_state_scale = std::pow(2, -15) / lstm_params.hidden_state_scale() * std::pow(2, -15);
516 quantization::calculate_quantized_multiplier(hidden_state_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift, /* ignore_epsilon */ true);
517 gemmlowp_info.gemmlowp_offset = lstm_params.hidden_state_zero();
518 gemmlowp_info.output_data_type = output_state_in->info()->data_type();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100519
520 _projection_tensor_copy_required = (num_units != output_size);
521 ICLTensor *hidden_gate_result = output_state_out;
522
523 _memory_group.manage(&_hidden_gate);
524
525 if(_projection_tensor_copy_required)
526 {
527 _hidden_gate.allocator()->init(*output_state_out->info());
528 _hidden_gate.info()->set_tensor_shape(_hidden_mul_res.info()->tensor_shape());
529 hidden_gate_result = &_hidden_gate;
530 }
531
532 _hidden_outstage.configure(compile_context, &_hidden_mul_res, nullptr, hidden_gate_result, gemmlowp_info);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100533 _hidden_mul_res.allocator()->allocate();
534
535 // Projection.
536 if(_has_projection)
537 {
538 const TensorInfo projection_outstage_info(*output_state_out->info());
539 const UniformQuantizationInfo qprojection = _projection_weights->info()->quantization_info().uniform();
540 const float projection_scale = qprojection.scale * lstm_params.hidden_state_scale() / qoutput_state_in.scale;
541 gemmlowp_info.gemmlowp_offset = qoutput_state_in.offset;
542 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int8_t>::lowest();
543 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int8_t>::max();
544 gemmlowp_info.output_data_type = DataType::QASYMM8_SIGNED;
545
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100546 TensorInfo projection_mm_out_info{ mm_out_info };
547 projection_mm_out_info.set_tensor_shape(TensorShape(output_size, batch_size));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100548
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100549 configure_mm(compile_context, _mm_projection, _projection_outstage, gemmlowp_info,
550 hidden_gate_result, &_projection_weights_transposed, &_projection_eff_bias,
551 &_mm_projection_res, &_projection_outstage_res, projection_scale,
552 projection_mm_out_info, projection_outstage_info);
553
554 ICLTensor *accumulate_destination = output_state_out;
555
556 if(_projection_tensor_copy_required)
557 {
558 _hidden_gate.allocator()->allocate();
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100559 _projection_accumulate_res.allocator()->init(*output_state_in->info());
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100560 _projection_accumulate_res.info()->set_tensor_shape(_projection_outstage_res.info()->tensor_shape());
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100561 _projection_output_to_accumulate_copy.configure(*output_state_in, _projection_accumulate_res);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100562 accumulate_destination = &_projection_accumulate_res;
563 }
564
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100565 _accumulate_projection.configure(compile_context, &_projection_outstage_res, accumulate_destination, accumulate_destination, ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100566 _projection_outstage_res.allocator()->allocate();
567
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100568 if(_projection_tensor_copy_required)
569 {
570 _projection_accumulate_to_output_copy.configure(_projection_accumulate_res, *output_state_out);
571 _projection_accumulate_res.allocator()->allocate();
572 }
573
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100574 int8_t quantized_projection_clip{ 0 };
575 if(lstm_params.projection_clip() > 0.0f)
576 {
577 quantized_projection_clip = utility::clamp<int8_t>(lstm_params.projection_clip() / qprojection.scale, -128, 127);
578 }
579
580 if(quantized_projection_clip > 0)
581 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100582 _projection_clip.configure(compile_context, output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_projection_clip,
583 quantized_projection_clip));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100584 _has_projection_clipping = true;
585 }
586 }
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100587 else
588 {
589 if(_projection_tensor_copy_required)
590 {
591 _hidden_to_output_copy.configure(_hidden_gate, *output_state_out);
592 _hidden_gate.allocator()->allocate();
593 }
594 }
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100595
596 // Copy output_state_out to output
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100597 _copy_output->configure(compile_context, output_state_out, output);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100598}
599
600Status CLQLSTMLayer::validate(const ITensorInfo *input,
601 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
602 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
603 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
604 const ITensorInfo *cell_state_in, const ITensorInfo *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100605 const ITensorInfo *cell_state_out, const ITensorInfo *output_state_out, const ITensorInfo *output,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100606 const LSTMParams<ITensorInfo> &lstm_params)
607{
608 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 +0100609 recurrent_to_output_weights, forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in,
610 cell_state_out, output_state_out, output);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100611
612 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED);
613 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() != 2, "Input must have exactly 2 dimensions");
614
615 const unsigned int input_size = input->dimension(0);
616 const unsigned int batch_size = input->dimension(1);
617 const unsigned int num_units = input_to_output_weights->dimension(1);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100618 const unsigned int output_size = output_state_out->dimension(_out_state_output_size_dimension_idx);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100619
620 ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->num_dimensions() != 2);
621 ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->dimension(0) != input_size);
622 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_to_output_weights, input_to_forget_weights, input_to_cell_weights);
623 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->num_dimensions() != 2);
624 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->dimension(1) != num_units);
625 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(recurrent_to_output_weights, recurrent_to_forget_weights, recurrent_to_cell_weights);
626 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_to_forget_weights, 1, DataType::QSYMM8);
627 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
628 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights);
629
630 ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->num_dimensions() != 1);
631 ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->dimension(0) != num_units);
632 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(forget_gate_bias, cell_bias, output_gate_bias);
633 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(forget_gate_bias, 1, DataType::S32);
634 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(forget_gate_bias, cell_bias, output_gate_bias);
635
636 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->num_dimensions() != 2);
637 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->dimension(0) != num_units);
638 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->dimension(1) != batch_size);
639 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(cell_state_in, 1, DataType::QSYMM16);
640
641 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() != 2);
642 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->dimension(0) != output_size);
643 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->dimension(1) != batch_size);
644 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output_state_in);
645
646 // Check whether peephole weights are all there or none
647 if(lstm_params.has_peephole_opt())
648 {
649 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights());
650 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_forget_weights(), 1, DataType::QSYMM16);
651 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->num_dimensions() != 1);
652 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->dimension(0) != num_units);
653 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights());
654 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights());
655
656 if(!lstm_params.has_cifg_opt())
657 {
658 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_input_weights());
659 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_input_weights());
660 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_input_weights());
661 }
662 }
663
664 const UniformQuantizationInfo qinput = input->quantization_info().uniform();
665 const UniformQuantizationInfo qcell_state_in = cell_state_in->quantization_info().uniform();
666 const UniformQuantizationInfo qoutput_state_in = output_state_in->quantization_info().uniform();
667
668 // Calculate and decompose effective scales for optimizing matmul calculation
669 const int32_t cell_shift = log2(qcell_state_in.scale);
670 ARM_COMPUTE_RETURN_ERROR_ON(cell_shift > -9);
671
672 // Calculate quantized parameters for clipping.
673 int16_t quantized_cell_clip = 0;
674 if(lstm_params.cell_clip() > 0.0f)
675 {
676 quantized_cell_clip = quantize_qsymm16(lstm_params.cell_clip(), qcell_state_in);
677 }
678
679 // Precompute effective bias for optimizing the matmul computations.
680 const TensorInfo eff_bias_info(TensorShape(num_units), 1, DataType::S32);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100681 const TensorInfo projection_eff_bias_info(TensorShape(output_size), 1, DataType::S32);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100682 if(!lstm_params.has_cifg_opt())
683 {
684 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(lstm_params.input_to_input_weights(), &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
685 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(lstm_params.recurrent_to_input_weights(), &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset,
686 true)));
687 }
688 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(input_to_forget_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
689 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(recurrent_to_forget_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true)));
690 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(input_to_cell_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
691 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(recurrent_to_cell_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true)));
692 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(input_to_output_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
693 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 +0100694 if(lstm_params.has_projection())
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100695 {
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100696 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(lstm_params.projection_weights(), &projection_eff_bias_info, GEMMLowpReductionKernelInfo(output_size, false,
697 lstm_params.hidden_state_zero(),
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100698 true)));
Michele Di Giorgio11c562c2020-06-10 16:34:50 +0100699 if(lstm_params.projection_bias() != nullptr)
700 {
701 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.projection_bias(), 1, DataType::S32);
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100702 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(lstm_params.projection_bias(), &projection_eff_bias_info,
703 &projection_eff_bias_info, ConvertPolicy::SATURATE));
Michele Di Giorgio11c562c2020-06-10 16:34:50 +0100704 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100705 }
706
707 const TensorInfo input_weights_transposed(TensorShape(num_units, input_size), 1, input_to_forget_weights->data_type(), input_to_forget_weights->quantization_info());
708 const TensorInfo recurrent_weights_transposed(TensorShape(num_units, output_size), 1, recurrent_to_forget_weights->data_type(), recurrent_to_forget_weights->quantization_info());
709
710 // Validate weights transpose
711 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(input_to_forget_weights, &input_weights_transposed));
712 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(input_to_cell_weights, &input_weights_transposed));
713 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(input_to_output_weights, &input_weights_transposed));
714 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(recurrent_to_forget_weights, &recurrent_weights_transposed));
715 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(recurrent_to_cell_weights, &recurrent_weights_transposed));
716 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(recurrent_to_output_weights, &recurrent_weights_transposed));
717 if(!lstm_params.has_cifg_opt())
718 {
719 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(lstm_params.input_to_input_weights(), &input_weights_transposed));
720 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(lstm_params.recurrent_to_input_weights(), &recurrent_weights_transposed));
721 }
722 if(lstm_params.has_projection())
723 {
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100724 const TensorInfo projection_weights_transposed(TensorShape(output_size, num_units), 1, lstm_params.projection_weights()->data_type(), lstm_params.projection_weights()->quantization_info());
725 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(lstm_params.projection_weights(), &projection_weights_transposed));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100726 }
727
728 GEMMLowpOutputStageInfo gemmlowp_info;
729 gemmlowp_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
730 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int16_t>::lowest();
731 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int16_t>::max();
732 gemmlowp_info.output_data_type = DataType::QSYMM16;
733
Sheri Zhang3a353982020-04-21 13:10:24 +0100734 const bool has_layer_norm = lstm_params.use_layer_norm();
735
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100736 // Forget gate.
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100737 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.forget_intermediate_scale() == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100738 const TensorInfo forget_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0));
739 const TensorInfo mm_out_info(TensorShape(num_units, batch_size), 1, DataType::S32);
740 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 +0100741 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 +0100742
743 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 +0100744 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 +0100745
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100746 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 +0100747
748 if(lstm_params.has_peephole_opt())
749 {
750 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 +0100751 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(cell_state_in, lstm_params.cell_to_forget_weights(), &mm_out_info, 1.f, ConvertPolicy::SATURATE,
752 RoundingPolicy::TO_ZERO));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100753 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();
754 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(cell_to_forget_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
755 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&mm_out_info, nullptr, &forget_outstage_info, gemmlowp_info));
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100756 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 +0100757 }
758
Sheri Zhang3a353982020-04-21 13:10:24 +0100759 if(has_layer_norm)
760 {
761 const ITensorInfo *w_info = lstm_params.forget_layer_norm_weights();
762 const ITensorInfo *b_info = forget_gate_bias;
763 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(forget_outstage_info, *w_info, *b_info));
764 }
765
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100766 // Output quantization info of Sigmoid and Tanh activations
767 const QuantizationInfo sigmoid_tanh_outqinfo(1.f / 32768.f, 0);
768
769 const TensorInfo forget_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
770 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&forget_outstage_info, &forget_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
771
772 // Modulation gate.
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100773 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_intermediate_scale() == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100774 const TensorInfo cell_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.cell_intermediate_scale(), 0));
775 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 +0100776 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 +0100777
778 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 +0100779 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 +0100780
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100781 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 +0100782
Sheri Zhang3a353982020-04-21 13:10:24 +0100783 if(has_layer_norm)
784 {
785 const ITensorInfo *w_info = lstm_params.cell_layer_norm_weights();
786 const ITensorInfo *b_info = cell_bias;
787 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(cell_outstage_info, *w_info, *b_info));
788 }
789
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100790 const TensorInfo cell_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
791 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&cell_outstage_info, &cell_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)));
792
793 // Input gate.
794 const TensorInfo input_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
795 if(lstm_params.has_cifg_opt())
796 {
797 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 +0100798 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 +0100799 }
800 else
801 {
802 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights(), lstm_params.input_gate_bias());
803 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_to_forget_weights, lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights());
804 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_to_forget_weights, lstm_params.input_to_input_weights());
805 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(recurrent_to_forget_weights, lstm_params.recurrent_to_input_weights());
806 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(forget_gate_bias, lstm_params.input_gate_bias());
807 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(forget_gate_bias, lstm_params.input_gate_bias());
808
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100809 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_intermediate_scale() == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100810 const TensorInfo input_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0));
811 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 +0100812 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 +0100813
814 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 +0100815 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 +0100816
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100817 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 +0100818
819 if(lstm_params.has_peephole_opt())
820 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100821 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(cell_state_in, lstm_params.cell_to_input_weights(), &mm_out_info, 1.f, ConvertPolicy::SATURATE,
822 RoundingPolicy::TO_ZERO));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100823 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();
824 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 +0100825 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 +0100826 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 +0100827 }
828
Sheri Zhang3a353982020-04-21 13:10:24 +0100829 if(has_layer_norm)
830 {
831 const ITensorInfo *w_info = lstm_params.input_layer_norm_weights();
832 const ITensorInfo *b_info = lstm_params.input_gate_bias();
833 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(cell_outstage_info, *w_info, *b_info));
834 }
835
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100836 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 +0100837 }
838 // Cell.
Michalis Spyrou1009e872020-07-27 12:48:34 +0100839 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&forget_gate_info, cell_state_in, &forget_gate_info, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
840 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 +0100841 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 +0100842 if(quantized_cell_clip > 0)
843 {
844 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(cell_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_cell_clip,
845 quantized_cell_clip)));
846 }
847 // Output gate.
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100848 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.output_intermediate_scale() == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100849 const TensorInfo output_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.output_intermediate_scale(), 0));
850 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 +0100851 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 +0100852
853 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 +0100854 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 +0100855
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100856 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 +0100857 if(lstm_params.has_peephole_opt())
858 {
859 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_output_weights(), 1, DataType::QSYMM16);
860 // TODO(COMPMID-3395): Perform multiplication in the quantized domain in NEPixelWiseMultiplicationKernel
861 // Here we are not using the output stage because all operations are done in float
862 // 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();
863 // 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 +0100864 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(cell_state_out, lstm_params.cell_to_output_weights(), &output_outstage_info, 1.f, ConvertPolicy::SATURATE,
865 RoundingPolicy::TO_ZERO));
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100866 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 +0100867 }
868
Sheri Zhang3a353982020-04-21 13:10:24 +0100869 if(has_layer_norm)
870 {
871 const ITensorInfo *w_info = lstm_params.output_layer_norm_weights();
872 const ITensorInfo *b_info = output_gate_bias;
873 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(output_outstage_info, *w_info, *b_info));
874 }
875
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100876 const TensorInfo output_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
877 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&output_outstage_info, &output_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
878
879 // Hidden.
880 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(cell_state_out, &input_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)));
881 const TensorInfo hidden_mul_res(TensorShape(num_units, batch_size), 1, DataType::S32);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100882 const TensorInfo hidden_out_info(TensorShape(num_units, batch_size), 1, DataType::QASYMM8_SIGNED);
883
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100884 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.hidden_state_scale() == 0);
Michalis Spyrou1009e872020-07-27 12:48:34 +0100885 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 +0100886 const float hidden_state_scale = std::pow(2, -15) / lstm_params.hidden_state_scale() * std::pow(2, -15);
887 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 +0100888 gemmlowp_info.gemmlowp_offset = lstm_params.hidden_state_zero();
889 gemmlowp_info.output_data_type = hidden_out_info.data_type();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100890 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&hidden_mul_res, nullptr, &hidden_out_info, gemmlowp_info));
891
892 const bool projection_tensor_copy_required = num_units != output_size;
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100893
894 // Projection.
895 if(lstm_params.has_projection())
896 {
897 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(recurrent_to_forget_weights, lstm_params.projection_weights());
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100898 ARM_COMPUTE_RETURN_ERROR_ON(qoutput_state_in.scale == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100899
900 const UniformQuantizationInfo qprojection = lstm_params.projection_weights()->quantization_info().uniform();
901 const float projection_scale = qprojection.scale * lstm_params.hidden_state_scale() / qoutput_state_in.scale;
902 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(projection_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
903 gemmlowp_info.gemmlowp_offset = qoutput_state_in.offset;
904 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int8_t>::lowest();
905 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int8_t>::max();
906 gemmlowp_info.output_data_type = DataType::QASYMM8_SIGNED;
907
908 const TensorInfo projection_outstage_info(*output_state_out);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100909 const TensorInfo projection_weights_transposed(TensorShape(output_size, num_units), 1, lstm_params.projection_weights()->data_type(), lstm_params.projection_weights()->quantization_info());
910
911 TensorInfo projection_mm_out_info{ mm_out_info };
912 projection_mm_out_info.set_tensor_shape(TensorShape(output_size, batch_size));
913
914 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,
915 &projection_outstage_info));
916
917 if(projection_tensor_copy_required)
918 {
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100919 ARM_COMPUTE_RETURN_ON_ERROR(CLQLSTMLayer::TensorCopyKernel::validate(*output_state_in, projection_outstage_info));
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100920 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100921
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100922 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 +0100923
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100924 if(projection_tensor_copy_required)
925 {
926 ARM_COMPUTE_RETURN_ON_ERROR(CLQLSTMLayer::TensorCopyKernel::validate(projection_outstage_info, *output_state_out));
927 }
928
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100929 int8_t quantized_projection_clip{ 0 };
930 if(lstm_params.projection_clip() > 0.0f)
931 {
932 quantized_projection_clip = quantize_qasymm8_signed(lstm_params.projection_clip(), qprojection);
933 }
934
935 if(quantized_projection_clip > 0)
936 {
937 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_projection_clip,
938 quantized_projection_clip)));
939 }
940 }
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100941 else
942 {
943 if(projection_tensor_copy_required)
944 {
945 ARM_COMPUTE_RETURN_ON_ERROR(CLQLSTMLayer::TensorCopyKernel::validate(hidden_out_info, *output_state_out));
946 }
947 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100948
949 if(cell_state_out->total_size() > 0)
950 {
951 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(cell_state_in, cell_state_out);
952 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(cell_state_in, cell_state_out);
953 }
954
955 if(output_state_out->total_size() > 0)
956 {
957 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output_state_out);
958 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output_state_in, output_state_out);
959 }
960
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100961 ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(output_state_out, output));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100962 return Status{};
963}
964
965void CLQLSTMLayer::run()
966{
967 prepare();
968
969 // Acquire all the temporaries
970 MemoryGroupResourceScope scope_mg(_memory_group);
971
972 // Forget gate.
973 _mm_input_to_forget.run();
974 _input_to_forget_outstage.run();
975
976 _mm_recurrent_to_forget.run();
977 _recurrent_to_forget_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100978 _accumulate_input_recurrent_forget.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100979
980 if(_has_peephole)
981 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100982 _pixelwise_mul_cell_to_forget.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100983 _cell_to_forget_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100984 _accumulate_cell_forget.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100985 }
986
Sheri Zhang3a353982020-04-21 13:10:24 +0100987 if(_has_layer_norm)
988 {
989 CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Forget));
990 }
991
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100992 _forget_gate_sigmoid.run();
993
994 // Modulation gate.
995 _mm_input_to_cell.run();
996 _input_to_cell_outstage.run();
997
998 _mm_recurrent_to_cell.run();
999 _recurrent_to_cell_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001000 _accumulate_input_recurrent_modulation.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001001
Sheri Zhang3a353982020-04-21 13:10:24 +01001002 if(_has_layer_norm)
1003 {
1004 CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Cell));
1005 }
1006
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001007 _cell_gate_tanh.run();
1008
1009 // Input gate
1010 if(_has_cifg)
1011 {
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001012 _input_gate_sub.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001013 }
1014 else
1015 {
1016 _mm_input_to_input.run();
1017 _input_to_input_outstage.run();
1018 _mm_recurrent_to_input.run();
1019 _recurrent_to_input_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001020 _accumulate_input_recurrent_input.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001021
1022 if(_has_peephole)
1023 {
Michalis Spyrou1009e872020-07-27 12:48:34 +01001024 _pixelwise_mul_cell_to_input.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001025 _cell_to_input_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001026 _accumulate_cell_input.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001027 }
1028
Sheri Zhang3a353982020-04-21 13:10:24 +01001029 if(_has_layer_norm)
1030 {
1031 CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Input));
1032 }
1033
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001034 _input_gate_sigmoid.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001035 }
1036
1037 // Cell.
Michalis Spyrou1009e872020-07-27 12:48:34 +01001038 _pixelwise_mul_forget_cell.run();
1039 _pixelwise_mul_input_cell.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001040 _add_forget_cell.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001041 if(_has_cell_clipping)
1042 {
1043 _cell_clip.run();
1044 }
1045
1046 // Output gate.
1047 _mm_input_to_output.run();
1048 _input_to_output_outstage.run();
1049 _mm_recurrent_to_output.run();
1050 _recurrent_to_output_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001051 _accumulate_input_recurrent_output.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001052 if(_has_peephole)
1053 {
Michalis Spyrou1009e872020-07-27 12:48:34 +01001054 _pixelwise_mul_cell_to_output.run();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001055 _cell_to_output_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001056 _accumulate_cell_to_output.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001057 }
1058
Sheri Zhang3a353982020-04-21 13:10:24 +01001059 if(_has_layer_norm)
1060 {
1061 CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Output));
1062 }
1063
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001064 _output_gate_sigmoid.run();
1065
1066 // Hidden.
1067 _hidden_tanh.run();
Michalis Spyrou1009e872020-07-27 12:48:34 +01001068 _pixelwise_mul_hidden.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001069 _hidden_outstage.run();
1070
1071 // Projection.
1072 if(_has_projection)
1073 {
1074 _mm_projection.run();
1075 _projection_outstage.run();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001076
1077 if(_projection_tensor_copy_required)
1078 {
1079 _projection_output_to_accumulate_copy.run();
1080 }
1081
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001082 _accumulate_projection.run();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001083
1084 if(_projection_tensor_copy_required)
1085 {
1086 _projection_accumulate_to_output_copy.run();
1087 }
1088
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001089 if(_has_projection_clipping)
1090 {
1091 _projection_clip.run();
1092 }
1093 }
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001094 else
1095 {
1096 if(_projection_tensor_copy_required)
1097 {
1098 _hidden_to_output_copy.run();
1099 }
1100 }
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +01001101
1102 // Copy output_state_out to output
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +01001103 CLScheduler::get().enqueue(*_copy_output);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001104}
1105
1106void CLQLSTMLayer::prepare()
1107{
1108 if(!_is_prepared)
1109 {
1110 // Pre-transpose weights to be used in GEMM.
1111 _input_to_forget_weights_transposed.allocator()->allocate();
1112 _input_to_cell_weights_transposed.allocator()->allocate();
1113 _input_to_output_weights_transposed.allocator()->allocate();
1114 _recurrent_to_forget_weights_transposed.allocator()->allocate();
1115 _recurrent_to_cell_weights_transposed.allocator()->allocate();
1116 _recurrent_to_output_weights_transposed.allocator()->allocate();
1117 _transpose_input_to_forget_weights.run();
1118 _transpose_input_to_cell_weights.run();
1119 _transpose_input_to_output_weights.run();
1120 _transpose_recurrent_to_forget_weights.run();
1121 _transpose_recurrent_to_cell_weights.run();
1122 _transpose_recurrent_to_output_weights.run();
1123
1124 // Precompute effective biases
1125 if(_has_cifg)
1126 {
1127 _ones.map(true);
1128 std::fill_n(reinterpret_cast<int16_t *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 32767);
1129 _ones.unmap();
1130 }
1131 else
1132 {
1133 _input_to_input_eff_bias.allocator()->allocate();
1134 _recurrent_to_input_eff_bias.allocator()->allocate();
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +01001135 CLScheduler::get().enqueue(*_input_to_input_reduction);
1136 CLScheduler::get().enqueue(*_recurrent_to_input_reduction);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001137
1138 _input_to_input_weights_transposed.allocator()->allocate();
1139 _recurrent_to_input_weights_transposed.allocator()->allocate();
1140 _transpose_input_to_input_weights.run();
1141 _transpose_recurrent_to_input_weights.run();
1142 _input_to_input_weights->mark_as_unused();
1143 _recurrent_to_input_weights->mark_as_unused();
1144 }
1145 _input_to_forget_eff_bias.allocator()->allocate();
1146 _recurrent_to_forget_eff_bias.allocator()->allocate();
1147 _input_to_cell_eff_bias.allocator()->allocate();
1148 _recurrent_to_cell_eff_bias.allocator()->allocate();
1149 _input_to_output_eff_bias.allocator()->allocate();
1150 _recurrent_to_output_eff_bias.allocator()->allocate();
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +01001151 CLScheduler::get().enqueue(*_input_to_forget_reduction);
1152 CLScheduler::get().enqueue(*_recurrent_to_forget_reduction);
1153 CLScheduler::get().enqueue(*_input_to_cell_reduction);
1154 CLScheduler::get().enqueue(*_recurrent_to_cell_reduction);
1155 CLScheduler::get().enqueue(*_input_to_output_reduction);
1156 CLScheduler::get().enqueue(*_recurrent_to_output_reduction);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001157
1158 if(_has_projection)
1159 {
Michele Di Giorgio11c562c2020-06-10 16:34:50 +01001160 _projection_eff_bias.allocator()->allocate();
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +01001161 CLScheduler::get().enqueue(*_projection_reduction);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001162 if(_projection_bias != nullptr)
1163 {
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001164 _projection_bias_add.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001165 _projection_bias->mark_as_unused();
1166 }
1167
1168 _projection_weights_transposed.allocator()->allocate();
1169 _transpose_projection_weights.run();
1170 _projection_weights->mark_as_unused();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001171
1172 if(!_projection_tensor_copy_required)
1173 {
1174 _hidden_gate.mark_as_unused();
1175 _projection_accumulate_res.mark_as_unused();
1176 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001177 }
1178
1179 // Mark weights as unused
1180 _input_to_forget_weights->mark_as_unused();
1181 _input_to_cell_weights->mark_as_unused();
1182 _input_to_output_weights->mark_as_unused();
1183 _recurrent_to_forget_weights->mark_as_unused();
1184 _recurrent_to_cell_weights->mark_as_unused();
1185 _recurrent_to_output_weights->mark_as_unused();
1186
1187 CLScheduler::get().queue().finish();
1188 _is_prepared = true;
1189 }
1190}
1191
1192} // namespace arm_compute