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