blob: 4395a39060d5707c3bc8d763904d0586bc735bd4 [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"
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +010044
45namespace arm_compute
46{
47using namespace arm_compute::utils::info_helpers;
48namespace
49{
50Status validate_mm(GEMMLowpOutputStageInfo &gemmlowp_info, const ITensorInfo *mm_input, const ITensorInfo *mm_weights, const ITensorInfo *bias,
51 float gemmlowp_scale, const TensorInfo *mm_res_info, const TensorInfo *outstage_tensor_info)
52{
53 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(mm_input, mm_weights, nullptr, mm_res_info));
54 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(gemmlowp_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
55 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(mm_res_info, bias, outstage_tensor_info, gemmlowp_info));
56 return Status{};
57}
58} // namespace
59
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +010060Status CLQLSTMLayer::TensorCopyKernel::validate(const ITensorInfo &src, const ITensorInfo &dst)
61{
62 ARM_COMPUTE_RETURN_ERROR_ON(src.tensor_shape().num_dimensions() > max_dimension_supported);
63 ARM_COMPUTE_RETURN_ERROR_ON(dst.tensor_shape().num_dimensions() > max_dimension_supported);
64 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst);
65 ARM_COMPUTE_RETURN_ERROR_ON(dst.tensor_shape().y() != src.tensor_shape().y());
66 return Status{};
67}
68
69void CLQLSTMLayer::TensorCopyKernel::configure(ICLTensor &src, ICLTensor &dst)
70{
71 ARM_COMPUTE_ERROR_THROW_ON(CLQLSTMLayer::TensorCopyKernel::validate(*src.info(), *dst.info()));
72 _src = &src;
73 _dst = &dst;
74 _row_size = std::min(_src->info()->tensor_shape().x(), _dst->info()->tensor_shape().x());
75 _window = calculate_max_window(*_src->info(), Steps());
76}
77
78void CLQLSTMLayer::TensorCopyKernel::run()
79{
80 auto &q = CLScheduler::get().queue();
81
82 _src->map(q, true);
83 _dst->map(q, true);
84
85 Iterator input_iter{ _src, _window };
86 Iterator output_iter{ _dst, _window };
87
88 execute_window_loop(_window, [&](const Coordinates &)
89 {
90 memcpy(output_iter.ptr(), input_iter.ptr(), _row_size);
91 },
92 input_iter, output_iter);
93
94 _src->unmap(q);
95 _dst->unmap(q);
96}
97
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +010098CLQLSTMLayer::CLQLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager)
Georgios Pinitas40f51a62020-11-21 03:04:18 +000099 : _input_to_input_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
100 _recurrent_to_input_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
101 _input_to_forget_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
102 _recurrent_to_forget_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
103 _input_to_cell_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
104 _recurrent_to_cell_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
105 _input_to_output_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
106 _recurrent_to_output_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
107 _projection_reduction(std::make_unique<CLGEMMLowpMatrixAReductionKernel>()),
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100108 _layer_norms(),
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000109 _copy_output(std::make_unique<CLCopyKernel>())
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100110{
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100111 for(auto &norm : _layer_norms)
112 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000113 norm = std::make_unique<CLQLSTMLayerNormalizationKernel>();
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100114 }
115
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100116 _memory_group = MemoryGroup(std::move(memory_manager));
117}
118
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100119CLQLSTMLayer::~CLQLSTMLayer() = default;
120
121void CLQLSTMLayer::configure_layer_norm(LayerNormGate g, const ICLTensor *in)
122{
123 ARM_COMPUTE_ERROR_ON(!_has_layer_norm);
124
125 CLTensor *out = &get_layer_norm_output(g);
126 _memory_group.manage(out);
127 out->allocator()->init(*(in->info()));
128
129 get_layer_norm(g).configure(in, out, get_layer_norm_weight(g), get_layer_norm_bias(g));
130}
131
132Status CLQLSTMLayer::validate_layer_norm(const ITensorInfo &in, const ITensorInfo &weight, const ITensorInfo &bias)
133{
134 // Output quantization scale will be different, but ignored here
135 // since it will be configured at configure() stage.
136 const TensorInfo out
137 {
138 in
139 };
140 return CLQLSTMLayerNormalizationKernel::validate(&in, &out, &weight, &bias);
141}
142
Manuel Bottini2b84be52020-04-08 10:15:51 +0100143void CLQLSTMLayer::configure_mm(const CLCompileContext &compile_context, CLGEMMLowpMatrixMultiplyCore &mm, CLGEMMLowpOutputStage &outstage, GEMMLowpOutputStageInfo &gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100144 const ICLTensor *mm_input, const ICLTensor *mm_weights, const ICLTensor *bias,
145 CLTensor *mm_res, CLTensor *outstage_res, float gemmlowp_scale,
146 const TensorInfo &mm_res_info, const TensorInfo &outstage_tensor_info)
147{
148 _memory_group.manage(mm_res);
149 _memory_group.manage(outstage_res);
150
151 mm_res->allocator()->init(mm_res_info);
152 outstage_res->allocator()->init(outstage_tensor_info);
153
154 // Configure matrix-multiplication
Manuel Bottini2b84be52020-04-08 10:15:51 +0100155 mm.configure(compile_context, mm_input, mm_weights, nullptr, mm_res);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100156
157 // Configure output stage
158 quantization::calculate_quantized_multiplier(gemmlowp_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100159 outstage.configure(compile_context, mm_res, bias, outstage_res, gemmlowp_info);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100160 mm_res->allocator()->allocate();
161}
162
163void CLQLSTMLayer::configure(const ICLTensor *input,
164 const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
165 const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
166 const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100167 ICLTensor *cell_state_in, ICLTensor *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100168 ICLTensor *cell_state_out, ICLTensor *output_state_out, ICLTensor *output,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100169 const LSTMParams<ICLTensor> &lstm_params)
170{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100171 configure(CLKernelLibrary::get().get_compile_context(), input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
172 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 +0100173 cell_state_in, output_state_in, cell_state_out, output_state_out, output, lstm_params);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100174}
175
176void CLQLSTMLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input,
177 const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
178 const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
179 const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100180 ICLTensor *cell_state_in, ICLTensor *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100181 ICLTensor *cell_state_out, ICLTensor *output_state_out, ICLTensor *output,
Manuel Bottini2b84be52020-04-08 10:15:51 +0100182 const LSTMParams<ICLTensor> &lstm_params)
183{
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100184 ARM_COMPUTE_ERROR_ON_NULLPTR(input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
185 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100186 forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in,
187 cell_state_out, output_state_out, output);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100188
189 // Set lstm parameters
190 LSTMParams<ITensorInfo> lstm_params_info{};
191 build_lstm_params_tensor_info(lstm_params, &lstm_params_info);
192
193 // Validate
194 ARM_COMPUTE_ERROR_THROW_ON(CLQLSTMLayer::validate(input->info(), input_to_forget_weights->info(), input_to_cell_weights->info(), input_to_output_weights->info(),
195 recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(),
196 forget_gate_bias->info(), cell_bias->info(), output_gate_bias->info(),
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100197 cell_state_in->info(), output_state_in->info(), cell_state_out->info(), output_state_out->info(), output->info(),
198 lstm_params_info));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100199
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100200 const int batch_size = input->info()->dimension(1);
201 const int num_units = input_to_output_weights->info()->dimension(1);
202 const int output_size = output_state_out->info()->dimension(_out_state_output_size_dimension_idx);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100203
204 const UniformQuantizationInfo qinput = input->info()->quantization_info().uniform();
205 const UniformQuantizationInfo qcell_state_in = cell_state_in->info()->quantization_info().uniform();
206 const UniformQuantizationInfo qoutput_state_in = output_state_in->info()->quantization_info().uniform();
207
208 _projection_bias = lstm_params.projection_bias();
209 _input_to_forget_weights = input_to_forget_weights;
210 _input_to_cell_weights = input_to_cell_weights;
211 _input_to_output_weights = input_to_output_weights;
212 _recurrent_to_forget_weights = recurrent_to_forget_weights;
213 _recurrent_to_cell_weights = recurrent_to_cell_weights;
214 _recurrent_to_output_weights = recurrent_to_output_weights;
215 _projection_weights = lstm_params.projection_weights();
216
Sheri Zhang3a353982020-04-21 13:10:24 +0100217 // Layer normalization
218 _has_layer_norm = lstm_params.use_layer_norm();
219 if(_has_layer_norm)
220 {
221 set_layer_norm_weight(lstm_params.forget_layer_norm_weights(), LayerNormGate::Forget);
222 set_layer_norm_weight(lstm_params.cell_layer_norm_weights(), LayerNormGate::Cell);
223 set_layer_norm_weight(lstm_params.input_layer_norm_weights(), LayerNormGate::Input);
224 set_layer_norm_weight(lstm_params.output_layer_norm_weights(), LayerNormGate::Output);
225
226 set_layer_norm_bias(forget_gate_bias, LayerNormGate::Forget);
227 set_layer_norm_bias(cell_bias, LayerNormGate::Cell);
228 set_layer_norm_bias(lstm_params.input_gate_bias(), LayerNormGate::Input);
229 set_layer_norm_bias(output_gate_bias, LayerNormGate::Output);
230 }
231
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100232 _has_cifg = lstm_params.has_cifg_opt();
233 _has_projection = lstm_params.has_projection();
234 _has_peephole = lstm_params.has_peephole_opt();
235
236 // Calculate and decompose effective scales for optimizing matmul calculation
237 const int32_t cell_shift = log2(qcell_state_in.scale);
238
239 // Calculate quantized parameters for clipping.
240 int16_t quantized_cell_clip = 0;
241 if(lstm_params.cell_clip() > 0.0f)
242 {
243 quantized_cell_clip = quantize_qsymm16(lstm_params.cell_clip(), qcell_state_in);
244 }
245 _has_cell_clipping = quantized_cell_clip > 0;
246
247 // Precompute effective bias for optimizing the matmul computations.
248 if(!_has_cifg)
249 {
250 _input_to_input_weights = lstm_params.input_to_input_weights();
251 _recurrent_to_input_weights = lstm_params.recurrent_to_input_weights();
252
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100253 _input_to_input_reduction->configure(compile_context, _input_to_input_weights, &_input_to_input_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
254 _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 +0100255 }
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100256 _input_to_forget_reduction->configure(compile_context, input_to_forget_weights, &_input_to_forget_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
257 _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));
258 _input_to_cell_reduction->configure(compile_context, input_to_cell_weights, &_input_to_cell_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
259 _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));
260 _input_to_output_reduction->configure(compile_context, input_to_output_weights, &_input_to_output_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true));
261 _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 +0100262 if(_has_projection)
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100263 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100264 _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 +0100265 if(_projection_bias != nullptr)
266 {
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100267 _projection_bias_add.configure(compile_context, _projection_bias, &_projection_eff_bias, &_projection_eff_bias, ConvertPolicy::SATURATE);
Michele Di Giorgio11c562c2020-06-10 16:34:50 +0100268 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100269 }
270
271 // Pre-transpose weights to be used in GEMM.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100272 _transpose_input_to_forget_weights.configure(compile_context, input_to_forget_weights, &_input_to_forget_weights_transposed);
273 _transpose_input_to_cell_weights.configure(compile_context, input_to_cell_weights, &_input_to_cell_weights_transposed);
274 _transpose_input_to_output_weights.configure(compile_context, input_to_output_weights, &_input_to_output_weights_transposed);
275 _transpose_recurrent_to_forget_weights.configure(compile_context, recurrent_to_forget_weights, &_recurrent_to_forget_weights_transposed);
276 _transpose_recurrent_to_cell_weights.configure(compile_context, recurrent_to_cell_weights, &_recurrent_to_cell_weights_transposed);
277 _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 +0100278 if(!_has_cifg)
279 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100280 _transpose_input_to_input_weights.configure(compile_context, lstm_params.input_to_input_weights(), &_input_to_input_weights_transposed);
281 _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 +0100282 }
283 if(_has_projection)
284 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100285 _transpose_projection_weights.configure(compile_context, _projection_weights, &_projection_weights_transposed);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100286 }
287
288 GEMMLowpOutputStageInfo gemmlowp_info;
289 gemmlowp_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
290 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int16_t>::lowest();
291 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int16_t>::max();
292 gemmlowp_info.output_data_type = DataType::QSYMM16;
293
294 const TensorInfo mm_out_info(TensorShape(num_units, batch_size), 1, DataType::S32);
295 // Forget gate.
296 const TensorInfo forget_gate_outstage_info(mm_out_info.tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0));
297 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 +0100298 configure_mm(compile_context, _mm_input_to_forget, _input_to_forget_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100299 input, &_input_to_forget_weights_transposed, &_input_to_forget_eff_bias,
300 &_mm_input_to_forget_res, &_input_to_forget_outstage_res, input_to_forget_scale,
301 mm_out_info, forget_gate_outstage_info);
302
303 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 +0100304 configure_mm(compile_context, _mm_recurrent_to_forget, _recurrent_to_forget_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100305 output_state_in, &_recurrent_to_forget_weights_transposed, &_recurrent_to_forget_eff_bias,
306 &_mm_recurrent_to_forget_res, &_recurrent_to_forget_outstage_res, recurrent_to_forget_scale,
307 mm_out_info, forget_gate_outstage_info);
308
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100309 _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 +0100310 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100311 _input_to_forget_outstage_res.allocator()->allocate();
312
313 if(_has_peephole)
314 {
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100315 _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 +0100316 _memory_group.manage(&_mul_cell_to_forget_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100317 _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 +0100318 _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)));
319 _memory_group.manage(&_cell_to_forget_outstage_res);
320 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();
321 quantization::calculate_quantized_multiplier(cell_to_forget_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100322 _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 +0100323 _mul_cell_to_forget_res.allocator()->allocate();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100324 _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 +0100325 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100326 _cell_to_forget_outstage_res.allocator()->allocate();
327 }
328
Sheri Zhang3a353982020-04-21 13:10:24 +0100329 CLTensor *forget_activation_input = &_recurrent_to_forget_outstage_res;
330
331 if(_has_layer_norm)
332 {
333 configure_layer_norm(LayerNormGate::Forget, &_recurrent_to_forget_outstage_res);
334 _recurrent_to_forget_outstage_res.allocator()->allocate();
335 forget_activation_input = &get_layer_norm_output(LayerNormGate::Forget);
336 }
337
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100338 // Output quantization info of Sigmoid and Tanh activations
339 const QuantizationInfo sigmoid_tanh_outqinfo(1.f / 32768.f, 0);
340
341 const TensorInfo forget_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
342 _memory_group.manage(&_forget_gate);
343 _forget_gate.allocator()->init(forget_gate_info);
Sheri Zhang3a353982020-04-21 13:10:24 +0100344 _forget_gate_sigmoid.configure(compile_context, forget_activation_input, &_forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
345 forget_activation_input->allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100346
347 // Modulation gate.
348 const TensorInfo cell_outstage_info(mm_out_info.tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.cell_intermediate_scale(), 0));
349 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 +0100350 configure_mm(compile_context, _mm_input_to_cell, _input_to_cell_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100351 input, &_input_to_cell_weights_transposed, &_input_to_cell_eff_bias,
352 &_mm_input_to_cell_res, &_input_to_cell_outstage_res, input_to_cell_scale,
353 mm_out_info, cell_outstage_info);
354
355 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 +0100356 configure_mm(compile_context, _mm_recurrent_to_cell, _recurrent_to_cell_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100357 output_state_in, &_recurrent_to_cell_weights_transposed, &_recurrent_to_cell_eff_bias,
358 &_mm_recurrent_to_cell_res, &_recurrent_to_cell_outstage_res, recurrent_to_cell_scale,
359 mm_out_info, cell_outstage_info);
360
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100361 _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 +0100362 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100363 _input_to_cell_outstage_res.allocator()->allocate();
364
Sheri Zhang3a353982020-04-21 13:10:24 +0100365 CLTensor *cell_activation_input = &_recurrent_to_cell_outstage_res;
366
367 if(_has_layer_norm)
368 {
369 configure_layer_norm(LayerNormGate::Cell, &_recurrent_to_cell_outstage_res);
370 _recurrent_to_cell_outstage_res.allocator()->allocate();
371 cell_activation_input = &get_layer_norm_output(LayerNormGate::Cell);
372 }
373
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100374 const TensorInfo cell_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
375 _memory_group.manage(&_cell_gate);
376 _cell_gate.allocator()->init(cell_gate_info);
Sheri Zhang3a353982020-04-21 13:10:24 +0100377 _cell_gate_tanh.configure(compile_context, cell_activation_input, &_cell_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f));
378 cell_activation_input->allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100379
380 // Input gate.
381 const TensorInfo input_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
382 _input_gate.allocator()->init(input_gate_info);
383 _memory_group.manage(&_input_gate);
384 if(_has_cifg)
385 {
386 _ones.allocator()->init(*_forget_gate.info());
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100387 _input_gate_sub.configure(compile_context, &_ones, &_forget_gate, &_input_gate, ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100388 _ones.allocator()->allocate();
389 }
390 else
391 {
392 const TensorInfo input_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0));
393 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 +0100394 configure_mm(compile_context, _mm_input_to_input, _input_to_input_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100395 input, &_input_to_input_weights_transposed, &_input_to_input_eff_bias,
396 &_mm_input_to_input_res, &_input_to_input_outstage_res, input_to_input_scale,
397 mm_out_info, input_outstage_info);
398
399 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 +0100400 configure_mm(compile_context, _mm_recurrent_to_input, _recurrent_to_input_outstage, gemmlowp_info,
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100401 output_state_in, &_recurrent_to_input_weights_transposed, &_recurrent_to_input_eff_bias,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100402 &_mm_recurrent_to_input_res, &_recurrent_to_input_outstage_res, recurrent_to_input_scale,
403 mm_out_info, input_outstage_info);
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100404 _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 +0100405 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100406 _input_to_input_outstage_res.allocator()->allocate();
407
408 if(_has_peephole)
409 {
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100410 _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 +0100411 _memory_group.manage(&_mul_cell_to_input_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100412 _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 +0100413 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();
414 quantization::calculate_quantized_multiplier(cell_to_input_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
415 _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)));
416 _memory_group.manage(&_cell_to_input_outstage_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100417 _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 +0100418 _mul_cell_to_input_res.allocator()->allocate();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100419 _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 +0100420 _cell_to_input_outstage_res.allocator()->allocate();
421 }
422
Sheri Zhang3a353982020-04-21 13:10:24 +0100423 CLTensor *input_activation_input = &_recurrent_to_input_outstage_res;
424
425 if(_has_layer_norm)
426 {
427 configure_layer_norm(LayerNormGate::Input, &_recurrent_to_input_outstage_res);
428 _recurrent_to_input_outstage_res.allocator()->allocate();
429 input_activation_input = &get_layer_norm_output(LayerNormGate::Input);
430 }
431
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100432 _input_gate_sigmoid.configure(compile_context, input_activation_input, &_input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Sheri Zhang3a353982020-04-21 13:10:24 +0100433 input_activation_input->allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100434 }
435 // Cell.
Michalis Spyrou1009e872020-07-27 12:48:34 +0100436 // TODO(COMPMID-3396): Perform multiplication in the quantized domain in CLPixelWiseMultiplication
Manuel Bottini2b84be52020-04-08 10:15:51 +0100437 _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 +0100438 const float cell_gate_scale = _cell_gate.info()->quantization_info().uniform().scale;
439 const float mul_input_cell_scale = cell_gate_scale * std::pow(2, 15 + cell_shift);
440 const TensorInfo mul_input_cell_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(mul_input_cell_scale, 0));
441 _memory_group.manage(&_mul_input_cell_res);
442 _mul_input_cell_res.allocator()->init(mul_input_cell_info);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100443 _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 +0100444 _cell_gate.allocator()->allocate();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100445 _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 +0100446 _mul_input_cell_res.allocator()->allocate();
447 _forget_gate.allocator()->allocate();
448 if(_has_cell_clipping)
449 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100450 _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 +0100451 }
452 // Output gate.
453 const TensorInfo output_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.output_intermediate_scale(), 0));
454 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 +0100455 configure_mm(compile_context, _mm_input_to_output, _input_to_output_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100456 input, &_input_to_output_weights_transposed, &_input_to_output_eff_bias,
457 &_mm_input_to_output_res, &_input_to_output_outstage_res, input_to_output_scale,
458 mm_out_info, output_outstage_info);
459
460 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 +0100461 configure_mm(compile_context, _mm_recurrent_to_output, _recurrent_to_output_outstage, gemmlowp_info,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100462 output_state_in, &_recurrent_to_output_weights_transposed, &_recurrent_to_output_eff_bias,
463 &_mm_recurrent_to_output_res, &_recurrent_to_output_outstage_res, recurrent_to_output_scale,
464 mm_out_info, output_outstage_info);
465
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100466 _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 +0100467 ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100468 _input_to_output_outstage_res.allocator()->allocate();
469
470 if(_has_peephole)
471 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100472 // TODO(COMPMID-3396): Perform multiplication in the quantized domain in CLPixelWiseMultiplication
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100473 // Here we are not using the output stage because all operations are done in float
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100474 _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 +0100475 _memory_group.manage(&_mul_cell_to_output_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100476 _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 +0100477
478 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();
479 quantization::calculate_quantized_multiplier(cell_to_output_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift);
480 _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)));
481 _memory_group.manage(&_cell_to_output_outstage_res);
482 _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 +0100483 _mul_cell_to_output_res.allocator()->allocate();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100484
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100485 _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 +0100486 ConvertPolicy::SATURATE);
487 _cell_to_output_outstage_res.allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100488 }
489
Sheri Zhang3a353982020-04-21 13:10:24 +0100490 CLTensor *output_activation_input = &_recurrent_to_output_outstage_res;
491
492 if(_has_layer_norm)
493 {
494 configure_layer_norm(LayerNormGate::Output, &_recurrent_to_output_outstage_res);
495 _recurrent_to_output_outstage_res.allocator()->allocate();
496 output_activation_input = &get_layer_norm_output(LayerNormGate::Output);
497 }
498
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100499 const TensorInfo output_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
500 _memory_group.manage(&_output_gate);
501 _output_gate.allocator()->init(output_gate_info);
Sheri Zhang3a353982020-04-21 13:10:24 +0100502 _output_gate_sigmoid.configure(compile_context, output_activation_input, &_output_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
503 output_activation_input->allocator()->allocate();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100504
505 // Hidden.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100506 _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 +0100507 // TODO(COMPMID-3396): Perform multiplication in the quantized domain in CLPixelWiseMultiplication
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100508 _memory_group.manage(&_hidden_mul_res);
509 const TensorInfo hidden_mul_res(_input_gate.info()->tensor_shape(), 1, DataType::S32);
510 _hidden_mul_res.allocator()->init(hidden_mul_res);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100511 _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 +0100512 _output_gate.allocator()->allocate();
513 _input_gate.allocator()->allocate();
514 const float hidden_state_scale = std::pow(2, -15) / lstm_params.hidden_state_scale() * std::pow(2, -15);
515 quantization::calculate_quantized_multiplier(hidden_state_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift, /* ignore_epsilon */ true);
516 gemmlowp_info.gemmlowp_offset = lstm_params.hidden_state_zero();
517 gemmlowp_info.output_data_type = output_state_in->info()->data_type();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100518
519 _projection_tensor_copy_required = (num_units != output_size);
520 ICLTensor *hidden_gate_result = output_state_out;
521
522 _memory_group.manage(&_hidden_gate);
523
524 if(_projection_tensor_copy_required)
525 {
526 _hidden_gate.allocator()->init(*output_state_out->info());
527 _hidden_gate.info()->set_tensor_shape(_hidden_mul_res.info()->tensor_shape());
528 hidden_gate_result = &_hidden_gate;
529 }
530
531 _hidden_outstage.configure(compile_context, &_hidden_mul_res, nullptr, hidden_gate_result, gemmlowp_info);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100532 _hidden_mul_res.allocator()->allocate();
533
534 // Projection.
535 if(_has_projection)
536 {
537 const TensorInfo projection_outstage_info(*output_state_out->info());
538 const UniformQuantizationInfo qprojection = _projection_weights->info()->quantization_info().uniform();
539 const float projection_scale = qprojection.scale * lstm_params.hidden_state_scale() / qoutput_state_in.scale;
540 gemmlowp_info.gemmlowp_offset = qoutput_state_in.offset;
541 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int8_t>::lowest();
542 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int8_t>::max();
543 gemmlowp_info.output_data_type = DataType::QASYMM8_SIGNED;
544
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100545 TensorInfo projection_mm_out_info{ mm_out_info };
546 projection_mm_out_info.set_tensor_shape(TensorShape(output_size, batch_size));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100547
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100548 configure_mm(compile_context, _mm_projection, _projection_outstage, gemmlowp_info,
549 hidden_gate_result, &_projection_weights_transposed, &_projection_eff_bias,
550 &_mm_projection_res, &_projection_outstage_res, projection_scale,
551 projection_mm_out_info, projection_outstage_info);
552
553 ICLTensor *accumulate_destination = output_state_out;
554
555 if(_projection_tensor_copy_required)
556 {
557 _hidden_gate.allocator()->allocate();
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100558 _projection_accumulate_res.allocator()->init(*output_state_in->info());
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100559 _projection_accumulate_res.info()->set_tensor_shape(_projection_outstage_res.info()->tensor_shape());
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100560 _projection_output_to_accumulate_copy.configure(*output_state_in, _projection_accumulate_res);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100561 accumulate_destination = &_projection_accumulate_res;
562 }
563
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100564 _accumulate_projection.configure(compile_context, &_projection_outstage_res, accumulate_destination, accumulate_destination, ConvertPolicy::SATURATE);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100565 _projection_outstage_res.allocator()->allocate();
566
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100567 if(_projection_tensor_copy_required)
568 {
569 _projection_accumulate_to_output_copy.configure(_projection_accumulate_res, *output_state_out);
570 _projection_accumulate_res.allocator()->allocate();
571 }
572
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100573 int8_t quantized_projection_clip{ 0 };
574 if(lstm_params.projection_clip() > 0.0f)
575 {
576 quantized_projection_clip = utility::clamp<int8_t>(lstm_params.projection_clip() / qprojection.scale, -128, 127);
577 }
578
579 if(quantized_projection_clip > 0)
580 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100581 _projection_clip.configure(compile_context, output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_projection_clip,
582 quantized_projection_clip));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100583 _has_projection_clipping = true;
584 }
585 }
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100586 else
587 {
588 if(_projection_tensor_copy_required)
589 {
590 _hidden_to_output_copy.configure(_hidden_gate, *output_state_out);
591 _hidden_gate.allocator()->allocate();
592 }
593 }
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100594
595 // Copy output_state_out to output
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100596 _copy_output->configure(compile_context, output_state_out, output);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100597}
598
599Status CLQLSTMLayer::validate(const ITensorInfo *input,
600 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
601 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
602 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
603 const ITensorInfo *cell_state_in, const ITensorInfo *output_state_in,
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100604 const ITensorInfo *cell_state_out, const ITensorInfo *output_state_out, const ITensorInfo *output,
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100605 const LSTMParams<ITensorInfo> &lstm_params)
606{
607 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 +0100608 recurrent_to_output_weights, forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in,
609 cell_state_out, output_state_out, output);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100610
611 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED);
612 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() != 2, "Input must have exactly 2 dimensions");
613
614 const unsigned int input_size = input->dimension(0);
615 const unsigned int batch_size = input->dimension(1);
616 const unsigned int num_units = input_to_output_weights->dimension(1);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100617 const unsigned int output_size = output_state_out->dimension(_out_state_output_size_dimension_idx);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100618
619 ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->num_dimensions() != 2);
620 ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->dimension(0) != input_size);
621 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_to_output_weights, input_to_forget_weights, input_to_cell_weights);
622 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->num_dimensions() != 2);
623 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->dimension(1) != num_units);
624 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(recurrent_to_output_weights, recurrent_to_forget_weights, recurrent_to_cell_weights);
625 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_to_forget_weights, 1, DataType::QSYMM8);
626 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
627 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights);
628
629 ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->num_dimensions() != 1);
630 ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->dimension(0) != num_units);
631 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(forget_gate_bias, cell_bias, output_gate_bias);
632 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(forget_gate_bias, 1, DataType::S32);
633 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(forget_gate_bias, cell_bias, output_gate_bias);
634
635 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->num_dimensions() != 2);
636 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->dimension(0) != num_units);
637 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->dimension(1) != batch_size);
638 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(cell_state_in, 1, DataType::QSYMM16);
639
640 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() != 2);
641 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->dimension(0) != output_size);
642 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->dimension(1) != batch_size);
643 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output_state_in);
644
645 // Check whether peephole weights are all there or none
646 if(lstm_params.has_peephole_opt())
647 {
648 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights());
649 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_forget_weights(), 1, DataType::QSYMM16);
650 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->num_dimensions() != 1);
651 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->dimension(0) != num_units);
652 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights());
653 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights());
654
655 if(!lstm_params.has_cifg_opt())
656 {
657 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_input_weights());
658 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_input_weights());
659 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_input_weights());
660 }
661 }
662
663 const UniformQuantizationInfo qinput = input->quantization_info().uniform();
664 const UniformQuantizationInfo qcell_state_in = cell_state_in->quantization_info().uniform();
665 const UniformQuantizationInfo qoutput_state_in = output_state_in->quantization_info().uniform();
666
667 // Calculate and decompose effective scales for optimizing matmul calculation
668 const int32_t cell_shift = log2(qcell_state_in.scale);
669 ARM_COMPUTE_RETURN_ERROR_ON(cell_shift > -9);
670
671 // Calculate quantized parameters for clipping.
672 int16_t quantized_cell_clip = 0;
673 if(lstm_params.cell_clip() > 0.0f)
674 {
675 quantized_cell_clip = quantize_qsymm16(lstm_params.cell_clip(), qcell_state_in);
676 }
677
678 // Precompute effective bias for optimizing the matmul computations.
679 const TensorInfo eff_bias_info(TensorShape(num_units), 1, DataType::S32);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100680 const TensorInfo projection_eff_bias_info(TensorShape(output_size), 1, DataType::S32);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100681 if(!lstm_params.has_cifg_opt())
682 {
683 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(lstm_params.input_to_input_weights(), &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
684 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(lstm_params.recurrent_to_input_weights(), &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset,
685 true)));
686 }
687 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(input_to_forget_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
688 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(recurrent_to_forget_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true)));
689 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(input_to_cell_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
690 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(recurrent_to_cell_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true)));
691 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(input_to_output_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)));
692 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 +0100693 if(lstm_params.has_projection())
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100694 {
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100695 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(lstm_params.projection_weights(), &projection_eff_bias_info, GEMMLowpReductionKernelInfo(output_size, false,
696 lstm_params.hidden_state_zero(),
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100697 true)));
Michele Di Giorgio11c562c2020-06-10 16:34:50 +0100698 if(lstm_params.projection_bias() != nullptr)
699 {
700 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.projection_bias(), 1, DataType::S32);
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100701 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(lstm_params.projection_bias(), &projection_eff_bias_info,
702 &projection_eff_bias_info, ConvertPolicy::SATURATE));
Michele Di Giorgio11c562c2020-06-10 16:34:50 +0100703 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100704 }
705
706 const TensorInfo input_weights_transposed(TensorShape(num_units, input_size), 1, input_to_forget_weights->data_type(), input_to_forget_weights->quantization_info());
707 const TensorInfo recurrent_weights_transposed(TensorShape(num_units, output_size), 1, recurrent_to_forget_weights->data_type(), recurrent_to_forget_weights->quantization_info());
708
709 // Validate weights transpose
710 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(input_to_forget_weights, &input_weights_transposed));
711 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(input_to_cell_weights, &input_weights_transposed));
712 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(input_to_output_weights, &input_weights_transposed));
713 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(recurrent_to_forget_weights, &recurrent_weights_transposed));
714 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(recurrent_to_cell_weights, &recurrent_weights_transposed));
715 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(recurrent_to_output_weights, &recurrent_weights_transposed));
716 if(!lstm_params.has_cifg_opt())
717 {
718 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(lstm_params.input_to_input_weights(), &input_weights_transposed));
719 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(lstm_params.recurrent_to_input_weights(), &recurrent_weights_transposed));
720 }
721 if(lstm_params.has_projection())
722 {
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100723 const TensorInfo projection_weights_transposed(TensorShape(output_size, num_units), 1, lstm_params.projection_weights()->data_type(), lstm_params.projection_weights()->quantization_info());
724 ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(lstm_params.projection_weights(), &projection_weights_transposed));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100725 }
726
727 GEMMLowpOutputStageInfo gemmlowp_info;
728 gemmlowp_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
729 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int16_t>::lowest();
730 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int16_t>::max();
731 gemmlowp_info.output_data_type = DataType::QSYMM16;
732
Sheri Zhang3a353982020-04-21 13:10:24 +0100733 const bool has_layer_norm = lstm_params.use_layer_norm();
734
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100735 // Forget gate.
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100736 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.forget_intermediate_scale() == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100737 const TensorInfo forget_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0));
738 const TensorInfo mm_out_info(TensorShape(num_units, batch_size), 1, DataType::S32);
739 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 +0100740 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 +0100741
742 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 +0100743 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 +0100744
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100745 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 +0100746
747 if(lstm_params.has_peephole_opt())
748 {
749 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 +0100750 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(cell_state_in, lstm_params.cell_to_forget_weights(), &mm_out_info, 1.f, ConvertPolicy::SATURATE,
751 RoundingPolicy::TO_ZERO));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100752 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();
753 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(cell_to_forget_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
754 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&mm_out_info, nullptr, &forget_outstage_info, gemmlowp_info));
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100755 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 +0100756 }
757
Sheri Zhang3a353982020-04-21 13:10:24 +0100758 if(has_layer_norm)
759 {
760 const ITensorInfo *w_info = lstm_params.forget_layer_norm_weights();
761 const ITensorInfo *b_info = forget_gate_bias;
762 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(forget_outstage_info, *w_info, *b_info));
763 }
764
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100765 // Output quantization info of Sigmoid and Tanh activations
766 const QuantizationInfo sigmoid_tanh_outqinfo(1.f / 32768.f, 0);
767
768 const TensorInfo forget_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
769 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&forget_outstage_info, &forget_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
770
771 // Modulation gate.
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100772 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_intermediate_scale() == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100773 const TensorInfo cell_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.cell_intermediate_scale(), 0));
774 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 +0100775 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 +0100776
777 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 +0100778 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 +0100779
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100780 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 +0100781
Sheri Zhang3a353982020-04-21 13:10:24 +0100782 if(has_layer_norm)
783 {
784 const ITensorInfo *w_info = lstm_params.cell_layer_norm_weights();
785 const ITensorInfo *b_info = cell_bias;
786 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(cell_outstage_info, *w_info, *b_info));
787 }
788
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100789 const TensorInfo cell_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
790 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&cell_outstage_info, &cell_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)));
791
792 // Input gate.
793 const TensorInfo input_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
794 if(lstm_params.has_cifg_opt())
795 {
796 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 +0100797 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 +0100798 }
799 else
800 {
801 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights(), lstm_params.input_gate_bias());
802 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_to_forget_weights, lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights());
803 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_to_forget_weights, lstm_params.input_to_input_weights());
804 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(recurrent_to_forget_weights, lstm_params.recurrent_to_input_weights());
805 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(forget_gate_bias, lstm_params.input_gate_bias());
806 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(forget_gate_bias, lstm_params.input_gate_bias());
807
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100808 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_intermediate_scale() == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100809 const TensorInfo input_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0));
810 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 +0100811 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 +0100812
813 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 +0100814 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 +0100815
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100816 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 +0100817
818 if(lstm_params.has_peephole_opt())
819 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100820 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(cell_state_in, lstm_params.cell_to_input_weights(), &mm_out_info, 1.f, ConvertPolicy::SATURATE,
821 RoundingPolicy::TO_ZERO));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100822 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();
823 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 +0100824 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 +0100825 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 +0100826 }
827
Sheri Zhang3a353982020-04-21 13:10:24 +0100828 if(has_layer_norm)
829 {
830 const ITensorInfo *w_info = lstm_params.input_layer_norm_weights();
831 const ITensorInfo *b_info = lstm_params.input_gate_bias();
832 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(cell_outstage_info, *w_info, *b_info));
833 }
834
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100835 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 +0100836 }
837 // Cell.
Michalis Spyrou1009e872020-07-27 12:48:34 +0100838 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(&forget_gate_info, cell_state_in, &forget_gate_info, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
839 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 +0100840 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 +0100841 if(quantized_cell_clip > 0)
842 {
843 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(cell_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_cell_clip,
844 quantized_cell_clip)));
845 }
846 // Output gate.
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100847 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.output_intermediate_scale() == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100848 const TensorInfo output_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.output_intermediate_scale(), 0));
849 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 +0100850 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 +0100851
852 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 +0100853 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 +0100854
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100855 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 +0100856 if(lstm_params.has_peephole_opt())
857 {
858 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_output_weights(), 1, DataType::QSYMM16);
859 // TODO(COMPMID-3395): Perform multiplication in the quantized domain in NEPixelWiseMultiplicationKernel
860 // Here we are not using the output stage because all operations are done in float
861 // 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();
862 // 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 +0100863 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplication::validate(cell_state_out, lstm_params.cell_to_output_weights(), &output_outstage_info, 1.f, ConvertPolicy::SATURATE,
864 RoundingPolicy::TO_ZERO));
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100865 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 +0100866 }
867
Sheri Zhang3a353982020-04-21 13:10:24 +0100868 if(has_layer_norm)
869 {
870 const ITensorInfo *w_info = lstm_params.output_layer_norm_weights();
871 const ITensorInfo *b_info = output_gate_bias;
872 ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(output_outstage_info, *w_info, *b_info));
873 }
874
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100875 const TensorInfo output_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo);
876 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&output_outstage_info, &output_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
877
878 // Hidden.
879 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(cell_state_out, &input_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)));
880 const TensorInfo hidden_mul_res(TensorShape(num_units, batch_size), 1, DataType::S32);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100881 const TensorInfo hidden_out_info(TensorShape(num_units, batch_size), 1, DataType::QASYMM8_SIGNED);
882
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100883 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.hidden_state_scale() == 0);
Michalis Spyrou1009e872020-07-27 12:48:34 +0100884 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 +0100885 const float hidden_state_scale = std::pow(2, -15) / lstm_params.hidden_state_scale() * std::pow(2, -15);
886 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 +0100887 gemmlowp_info.gemmlowp_offset = lstm_params.hidden_state_zero();
888 gemmlowp_info.output_data_type = hidden_out_info.data_type();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100889 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&hidden_mul_res, nullptr, &hidden_out_info, gemmlowp_info));
890
891 const bool projection_tensor_copy_required = num_units != output_size;
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100892
893 // Projection.
894 if(lstm_params.has_projection())
895 {
896 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(recurrent_to_forget_weights, lstm_params.projection_weights());
Sang-Hoon Parkee4833d2020-05-20 09:13:32 +0100897 ARM_COMPUTE_RETURN_ERROR_ON(qoutput_state_in.scale == 0);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100898
899 const UniformQuantizationInfo qprojection = lstm_params.projection_weights()->quantization_info().uniform();
900 const float projection_scale = qprojection.scale * lstm_params.hidden_state_scale() / qoutput_state_in.scale;
901 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(projection_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift));
902 gemmlowp_info.gemmlowp_offset = qoutput_state_in.offset;
903 gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int8_t>::lowest();
904 gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int8_t>::max();
905 gemmlowp_info.output_data_type = DataType::QASYMM8_SIGNED;
906
907 const TensorInfo projection_outstage_info(*output_state_out);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100908 const TensorInfo projection_weights_transposed(TensorShape(output_size, num_units), 1, lstm_params.projection_weights()->data_type(), lstm_params.projection_weights()->quantization_info());
909
910 TensorInfo projection_mm_out_info{ mm_out_info };
911 projection_mm_out_info.set_tensor_shape(TensorShape(output_size, batch_size));
912
913 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,
914 &projection_outstage_info));
915
916 if(projection_tensor_copy_required)
917 {
Sang-Hoon Park840a72c2020-09-23 13:24:13 +0100918 ARM_COMPUTE_RETURN_ON_ERROR(CLQLSTMLayer::TensorCopyKernel::validate(*output_state_in, projection_outstage_info));
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100919 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100920
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100921 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 +0100922
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100923 if(projection_tensor_copy_required)
924 {
925 ARM_COMPUTE_RETURN_ON_ERROR(CLQLSTMLayer::TensorCopyKernel::validate(projection_outstage_info, *output_state_out));
926 }
927
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100928 int8_t quantized_projection_clip{ 0 };
929 if(lstm_params.projection_clip() > 0.0f)
930 {
931 quantized_projection_clip = quantize_qasymm8_signed(lstm_params.projection_clip(), qprojection);
932 }
933
934 if(quantized_projection_clip > 0)
935 {
936 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_projection_clip,
937 quantized_projection_clip)));
938 }
939 }
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100940 else
941 {
942 if(projection_tensor_copy_required)
943 {
944 ARM_COMPUTE_RETURN_ON_ERROR(CLQLSTMLayer::TensorCopyKernel::validate(hidden_out_info, *output_state_out));
945 }
946 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100947
948 if(cell_state_out->total_size() > 0)
949 {
950 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(cell_state_in, cell_state_out);
951 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(cell_state_in, cell_state_out);
952 }
953
954 if(output_state_out->total_size() > 0)
955 {
956 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output_state_out);
957 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output_state_in, output_state_out);
958 }
959
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +0100960 ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(output_state_out, output));
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100961 return Status{};
962}
963
964void CLQLSTMLayer::run()
965{
966 prepare();
967
968 // Acquire all the temporaries
969 MemoryGroupResourceScope scope_mg(_memory_group);
970
971 // Forget gate.
972 _mm_input_to_forget.run();
973 _input_to_forget_outstage.run();
974
975 _mm_recurrent_to_forget.run();
976 _recurrent_to_forget_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100977 _accumulate_input_recurrent_forget.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100978
979 if(_has_peephole)
980 {
Michalis Spyrou1009e872020-07-27 12:48:34 +0100981 _pixelwise_mul_cell_to_forget.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100982 _cell_to_forget_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100983 _accumulate_cell_forget.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100984 }
985
Sheri Zhang3a353982020-04-21 13:10:24 +0100986 if(_has_layer_norm)
987 {
988 CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Forget));
989 }
990
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100991 _forget_gate_sigmoid.run();
992
993 // Modulation gate.
994 _mm_input_to_cell.run();
995 _input_to_cell_outstage.run();
996
997 _mm_recurrent_to_cell.run();
998 _recurrent_to_cell_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100999 _accumulate_input_recurrent_modulation.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001000
Sheri Zhang3a353982020-04-21 13:10:24 +01001001 if(_has_layer_norm)
1002 {
1003 CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Cell));
1004 }
1005
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001006 _cell_gate_tanh.run();
1007
1008 // Input gate
1009 if(_has_cifg)
1010 {
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001011 _input_gate_sub.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001012 }
1013 else
1014 {
1015 _mm_input_to_input.run();
1016 _input_to_input_outstage.run();
1017 _mm_recurrent_to_input.run();
1018 _recurrent_to_input_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001019 _accumulate_input_recurrent_input.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001020
1021 if(_has_peephole)
1022 {
Michalis Spyrou1009e872020-07-27 12:48:34 +01001023 _pixelwise_mul_cell_to_input.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001024 _cell_to_input_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001025 _accumulate_cell_input.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001026 }
1027
Sheri Zhang3a353982020-04-21 13:10:24 +01001028 if(_has_layer_norm)
1029 {
1030 CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Input));
1031 }
1032
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001033 _input_gate_sigmoid.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001034 }
1035
1036 // Cell.
Michalis Spyrou1009e872020-07-27 12:48:34 +01001037 _pixelwise_mul_forget_cell.run();
1038 _pixelwise_mul_input_cell.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001039 _add_forget_cell.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001040 if(_has_cell_clipping)
1041 {
1042 _cell_clip.run();
1043 }
1044
1045 // Output gate.
1046 _mm_input_to_output.run();
1047 _input_to_output_outstage.run();
1048 _mm_recurrent_to_output.run();
1049 _recurrent_to_output_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001050 _accumulate_input_recurrent_output.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001051 if(_has_peephole)
1052 {
Michalis Spyrou1009e872020-07-27 12:48:34 +01001053 _pixelwise_mul_cell_to_output.run();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001054 _cell_to_output_outstage.run();
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001055 _accumulate_cell_to_output.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001056 }
1057
Sheri Zhang3a353982020-04-21 13:10:24 +01001058 if(_has_layer_norm)
1059 {
1060 CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Output));
1061 }
1062
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001063 _output_gate_sigmoid.run();
1064
1065 // Hidden.
1066 _hidden_tanh.run();
Michalis Spyrou1009e872020-07-27 12:48:34 +01001067 _pixelwise_mul_hidden.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001068 _hidden_outstage.run();
1069
1070 // Projection.
1071 if(_has_projection)
1072 {
1073 _mm_projection.run();
1074 _projection_outstage.run();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001075
1076 if(_projection_tensor_copy_required)
1077 {
1078 _projection_output_to_accumulate_copy.run();
1079 }
1080
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001081 _accumulate_projection.run();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001082
1083 if(_projection_tensor_copy_required)
1084 {
1085 _projection_accumulate_to_output_copy.run();
1086 }
1087
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001088 if(_has_projection_clipping)
1089 {
1090 _projection_clip.run();
1091 }
1092 }
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001093 else
1094 {
1095 if(_projection_tensor_copy_required)
1096 {
1097 _hidden_to_output_copy.run();
1098 }
1099 }
Michele Di Giorgiobeb2d452020-05-11 16:17:51 +01001100
1101 // Copy output_state_out to output
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +01001102 CLScheduler::get().enqueue(*_copy_output);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001103}
1104
1105void CLQLSTMLayer::prepare()
1106{
1107 if(!_is_prepared)
1108 {
1109 // Pre-transpose weights to be used in GEMM.
1110 _input_to_forget_weights_transposed.allocator()->allocate();
1111 _input_to_cell_weights_transposed.allocator()->allocate();
1112 _input_to_output_weights_transposed.allocator()->allocate();
1113 _recurrent_to_forget_weights_transposed.allocator()->allocate();
1114 _recurrent_to_cell_weights_transposed.allocator()->allocate();
1115 _recurrent_to_output_weights_transposed.allocator()->allocate();
1116 _transpose_input_to_forget_weights.run();
1117 _transpose_input_to_cell_weights.run();
1118 _transpose_input_to_output_weights.run();
1119 _transpose_recurrent_to_forget_weights.run();
1120 _transpose_recurrent_to_cell_weights.run();
1121 _transpose_recurrent_to_output_weights.run();
1122
1123 // Precompute effective biases
1124 if(_has_cifg)
1125 {
1126 _ones.map(true);
1127 std::fill_n(reinterpret_cast<int16_t *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 32767);
1128 _ones.unmap();
1129 }
1130 else
1131 {
1132 _input_to_input_eff_bias.allocator()->allocate();
1133 _recurrent_to_input_eff_bias.allocator()->allocate();
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +01001134 CLScheduler::get().enqueue(*_input_to_input_reduction);
1135 CLScheduler::get().enqueue(*_recurrent_to_input_reduction);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001136
1137 _input_to_input_weights_transposed.allocator()->allocate();
1138 _recurrent_to_input_weights_transposed.allocator()->allocate();
1139 _transpose_input_to_input_weights.run();
1140 _transpose_recurrent_to_input_weights.run();
1141 _input_to_input_weights->mark_as_unused();
1142 _recurrent_to_input_weights->mark_as_unused();
1143 }
1144 _input_to_forget_eff_bias.allocator()->allocate();
1145 _recurrent_to_forget_eff_bias.allocator()->allocate();
1146 _input_to_cell_eff_bias.allocator()->allocate();
1147 _recurrent_to_cell_eff_bias.allocator()->allocate();
1148 _input_to_output_eff_bias.allocator()->allocate();
1149 _recurrent_to_output_eff_bias.allocator()->allocate();
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +01001150 CLScheduler::get().enqueue(*_input_to_forget_reduction);
1151 CLScheduler::get().enqueue(*_recurrent_to_forget_reduction);
1152 CLScheduler::get().enqueue(*_input_to_cell_reduction);
1153 CLScheduler::get().enqueue(*_recurrent_to_cell_reduction);
1154 CLScheduler::get().enqueue(*_input_to_output_reduction);
1155 CLScheduler::get().enqueue(*_recurrent_to_output_reduction);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001156
1157 if(_has_projection)
1158 {
Michele Di Giorgio11c562c2020-06-10 16:34:50 +01001159 _projection_eff_bias.allocator()->allocate();
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +01001160 CLScheduler::get().enqueue(*_projection_reduction);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001161 if(_projection_bias != nullptr)
1162 {
Michalis Spyrouad7515d2020-07-24 00:02:23 +01001163 _projection_bias_add.run();
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001164 _projection_bias->mark_as_unused();
1165 }
1166
1167 _projection_weights_transposed.allocator()->allocate();
1168 _transpose_projection_weights.run();
1169 _projection_weights->mark_as_unused();
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +01001170
1171 if(!_projection_tensor_copy_required)
1172 {
1173 _hidden_gate.mark_as_unused();
1174 _projection_accumulate_res.mark_as_unused();
1175 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +01001176 }
1177
1178 // Mark weights as unused
1179 _input_to_forget_weights->mark_as_unused();
1180 _input_to_cell_weights->mark_as_unused();
1181 _input_to_output_weights->mark_as_unused();
1182 _recurrent_to_forget_weights->mark_as_unused();
1183 _recurrent_to_cell_weights->mark_as_unused();
1184 _recurrent_to_output_weights->mark_as_unused();
1185
1186 CLScheduler::get().queue().finish();
1187 _is_prepared = true;
1188 }
1189}
1190
1191} // namespace arm_compute