Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 ARM Limited. |
| 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 | #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" |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | using namespace arm_compute::utils::info_helpers; |
| 37 | namespace |
| 38 | { |
| 39 | Status validate_mm(GEMMLowpOutputStageInfo &gemmlowp_info, const ITensorInfo *mm_input, const ITensorInfo *mm_weights, const ITensorInfo *bias, |
| 40 | float gemmlowp_scale, const TensorInfo *mm_res_info, const TensorInfo *outstage_tensor_info) |
| 41 | { |
| 42 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(mm_input, mm_weights, nullptr, mm_res_info)); |
| 43 | ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(gemmlowp_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift)); |
| 44 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(mm_res_info, bias, outstage_tensor_info, gemmlowp_info)); |
| 45 | return Status{}; |
| 46 | } |
| 47 | } // namespace |
| 48 | |
| 49 | CLQLSTMLayer::CLQLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager) |
| 50 | { |
| 51 | _memory_group = MemoryGroup(std::move(memory_manager)); |
| 52 | } |
| 53 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 54 | void CLQLSTMLayer::configure_mm(const CLCompileContext &compile_context, CLGEMMLowpMatrixMultiplyCore &mm, CLGEMMLowpOutputStage &outstage, GEMMLowpOutputStageInfo &gemmlowp_info, |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 55 | const ICLTensor *mm_input, const ICLTensor *mm_weights, const ICLTensor *bias, |
| 56 | CLTensor *mm_res, CLTensor *outstage_res, float gemmlowp_scale, |
| 57 | const TensorInfo &mm_res_info, const TensorInfo &outstage_tensor_info) |
| 58 | { |
| 59 | _memory_group.manage(mm_res); |
| 60 | _memory_group.manage(outstage_res); |
| 61 | |
| 62 | mm_res->allocator()->init(mm_res_info); |
| 63 | outstage_res->allocator()->init(outstage_tensor_info); |
| 64 | |
| 65 | // Configure matrix-multiplication |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 66 | mm.configure(compile_context, mm_input, mm_weights, nullptr, mm_res); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 67 | |
| 68 | // Configure output stage |
| 69 | quantization::calculate_quantized_multiplier(gemmlowp_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 70 | outstage.configure(compile_context, mm_res, bias, outstage_res, gemmlowp_info); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 71 | mm_res->allocator()->allocate(); |
| 72 | } |
| 73 | |
| 74 | void CLQLSTMLayer::configure(const ICLTensor *input, |
| 75 | const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights, |
| 76 | const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights, |
| 77 | const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias, |
| 78 | const ICLTensor *cell_state_in, const ICLTensor *output_state_in, |
| 79 | ICLTensor *cell_state_out, ICLTensor *output_state_out, |
| 80 | const LSTMParams<ICLTensor> &lstm_params) |
| 81 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 82 | configure(CLKernelLibrary::get().get_compile_context(), input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights, |
| 83 | recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights, forget_gate_bias, cell_bias, output_gate_bias, |
| 84 | cell_state_in, output_state_in, cell_state_out, output_state_out, lstm_params); |
| 85 | } |
| 86 | |
| 87 | void CLQLSTMLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input, |
| 88 | const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights, |
| 89 | const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights, |
| 90 | const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias, |
| 91 | const ICLTensor *cell_state_in, const ICLTensor *output_state_in, |
| 92 | ICLTensor *cell_state_out, ICLTensor *output_state_out, |
| 93 | const LSTMParams<ICLTensor> &lstm_params) |
| 94 | { |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 95 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights, |
| 96 | recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights, |
| 97 | forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in, cell_state_out, output_state_out); |
| 98 | |
| 99 | // Set lstm parameters |
| 100 | LSTMParams<ITensorInfo> lstm_params_info{}; |
| 101 | build_lstm_params_tensor_info(lstm_params, &lstm_params_info); |
| 102 | |
| 103 | // Validate |
| 104 | ARM_COMPUTE_ERROR_THROW_ON(CLQLSTMLayer::validate(input->info(), input_to_forget_weights->info(), input_to_cell_weights->info(), input_to_output_weights->info(), |
| 105 | recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(), |
| 106 | forget_gate_bias->info(), cell_bias->info(), output_gate_bias->info(), |
| 107 | cell_state_in->info(), output_state_in->info(), cell_state_out->info(), output_state_out->info(), lstm_params_info)); |
| 108 | |
| 109 | const int batch_size = input->info()->dimension(1); |
| 110 | const int num_units = input_to_output_weights->info()->dimension(1); |
| 111 | |
| 112 | const UniformQuantizationInfo qinput = input->info()->quantization_info().uniform(); |
| 113 | const UniformQuantizationInfo qcell_state_in = cell_state_in->info()->quantization_info().uniform(); |
| 114 | const UniformQuantizationInfo qoutput_state_in = output_state_in->info()->quantization_info().uniform(); |
| 115 | |
| 116 | _projection_bias = lstm_params.projection_bias(); |
| 117 | _input_to_forget_weights = input_to_forget_weights; |
| 118 | _input_to_cell_weights = input_to_cell_weights; |
| 119 | _input_to_output_weights = input_to_output_weights; |
| 120 | _recurrent_to_forget_weights = recurrent_to_forget_weights; |
| 121 | _recurrent_to_cell_weights = recurrent_to_cell_weights; |
| 122 | _recurrent_to_output_weights = recurrent_to_output_weights; |
| 123 | _projection_weights = lstm_params.projection_weights(); |
| 124 | |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 125 | // Layer normalization |
| 126 | _has_layer_norm = lstm_params.use_layer_norm(); |
| 127 | if(_has_layer_norm) |
| 128 | { |
| 129 | set_layer_norm_weight(lstm_params.forget_layer_norm_weights(), LayerNormGate::Forget); |
| 130 | set_layer_norm_weight(lstm_params.cell_layer_norm_weights(), LayerNormGate::Cell); |
| 131 | set_layer_norm_weight(lstm_params.input_layer_norm_weights(), LayerNormGate::Input); |
| 132 | set_layer_norm_weight(lstm_params.output_layer_norm_weights(), LayerNormGate::Output); |
| 133 | |
| 134 | set_layer_norm_bias(forget_gate_bias, LayerNormGate::Forget); |
| 135 | set_layer_norm_bias(cell_bias, LayerNormGate::Cell); |
| 136 | set_layer_norm_bias(lstm_params.input_gate_bias(), LayerNormGate::Input); |
| 137 | set_layer_norm_bias(output_gate_bias, LayerNormGate::Output); |
| 138 | } |
| 139 | |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 140 | _has_cifg = lstm_params.has_cifg_opt(); |
| 141 | _has_projection = lstm_params.has_projection(); |
| 142 | _has_peephole = lstm_params.has_peephole_opt(); |
| 143 | |
| 144 | // Calculate and decompose effective scales for optimizing matmul calculation |
| 145 | const int32_t cell_shift = log2(qcell_state_in.scale); |
| 146 | |
| 147 | // Calculate quantized parameters for clipping. |
| 148 | int16_t quantized_cell_clip = 0; |
| 149 | if(lstm_params.cell_clip() > 0.0f) |
| 150 | { |
| 151 | quantized_cell_clip = quantize_qsymm16(lstm_params.cell_clip(), qcell_state_in); |
| 152 | } |
| 153 | _has_cell_clipping = quantized_cell_clip > 0; |
| 154 | |
| 155 | // Precompute effective bias for optimizing the matmul computations. |
| 156 | if(!_has_cifg) |
| 157 | { |
| 158 | _input_to_input_weights = lstm_params.input_to_input_weights(); |
| 159 | _recurrent_to_input_weights = lstm_params.recurrent_to_input_weights(); |
| 160 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 161 | _input_to_input_reduction.configure(compile_context, _input_to_input_weights, &_input_to_input_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)); |
| 162 | _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 Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 163 | } |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 164 | _input_to_forget_reduction.configure(compile_context, input_to_forget_weights, &_input_to_forget_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)); |
| 165 | _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)); |
| 166 | _input_to_cell_reduction.configure(compile_context, input_to_cell_weights, &_input_to_cell_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)); |
| 167 | _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)); |
| 168 | _input_to_output_reduction.configure(compile_context, input_to_output_weights, &_input_to_output_eff_bias, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true)); |
| 169 | _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)); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 170 | if(_projection_bias != nullptr) |
| 171 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 172 | _projection_reduction.configure(compile_context, _projection_weights, &_projection_reduction_res, GEMMLowpReductionKernelInfo(num_units, false, lstm_params.hidden_state_zero(), true)); |
| 173 | _projection_bias_add.configure(compile_context, ArithmeticOperation::ADD, _projection_bias, &_projection_reduction_res, &_projection_eff_bias, ConvertPolicy::SATURATE); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Pre-transpose weights to be used in GEMM. |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 177 | _transpose_input_to_forget_weights.configure(compile_context, input_to_forget_weights, &_input_to_forget_weights_transposed); |
| 178 | _transpose_input_to_cell_weights.configure(compile_context, input_to_cell_weights, &_input_to_cell_weights_transposed); |
| 179 | _transpose_input_to_output_weights.configure(compile_context, input_to_output_weights, &_input_to_output_weights_transposed); |
| 180 | _transpose_recurrent_to_forget_weights.configure(compile_context, recurrent_to_forget_weights, &_recurrent_to_forget_weights_transposed); |
| 181 | _transpose_recurrent_to_cell_weights.configure(compile_context, recurrent_to_cell_weights, &_recurrent_to_cell_weights_transposed); |
| 182 | _transpose_recurrent_to_output_weights.configure(compile_context, recurrent_to_output_weights, &_recurrent_to_output_weights_transposed); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 183 | if(!_has_cifg) |
| 184 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 185 | _transpose_input_to_input_weights.configure(compile_context, lstm_params.input_to_input_weights(), &_input_to_input_weights_transposed); |
| 186 | _transpose_recurrent_to_input_weights.configure(compile_context, lstm_params.recurrent_to_input_weights(), &_recurrent_to_input_weights_transposed); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 187 | } |
| 188 | if(_has_projection) |
| 189 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 190 | _transpose_projection_weights.configure(compile_context, _projection_weights, &_projection_weights_transposed); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | GEMMLowpOutputStageInfo gemmlowp_info; |
| 194 | gemmlowp_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; |
| 195 | gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int16_t>::lowest(); |
| 196 | gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int16_t>::max(); |
| 197 | gemmlowp_info.output_data_type = DataType::QSYMM16; |
| 198 | |
| 199 | const TensorInfo mm_out_info(TensorShape(num_units, batch_size), 1, DataType::S32); |
| 200 | // Forget gate. |
| 201 | const TensorInfo forget_gate_outstage_info(mm_out_info.tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0)); |
| 202 | const float input_to_forget_scale = input_to_forget_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.forget_intermediate_scale(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 203 | configure_mm(compile_context, _mm_input_to_forget, _input_to_forget_outstage, gemmlowp_info, |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 204 | input, &_input_to_forget_weights_transposed, &_input_to_forget_eff_bias, |
| 205 | &_mm_input_to_forget_res, &_input_to_forget_outstage_res, input_to_forget_scale, |
| 206 | mm_out_info, forget_gate_outstage_info); |
| 207 | |
| 208 | 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 Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 209 | configure_mm(compile_context, _mm_recurrent_to_forget, _recurrent_to_forget_outstage, gemmlowp_info, |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 210 | output_state_in, &_recurrent_to_forget_weights_transposed, &_recurrent_to_forget_eff_bias, |
| 211 | &_mm_recurrent_to_forget_res, &_recurrent_to_forget_outstage_res, recurrent_to_forget_scale, |
| 212 | mm_out_info, forget_gate_outstage_info); |
| 213 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 214 | _accumulate_input_recurrent_forget.configure(compile_context, ArithmeticOperation::ADD, &_input_to_forget_outstage_res, &_recurrent_to_forget_outstage_res, &_recurrent_to_forget_outstage_res, |
| 215 | ConvertPolicy::SATURATE); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 216 | _input_to_forget_outstage_res.allocator()->allocate(); |
| 217 | |
| 218 | if(_has_peephole) |
| 219 | { |
| 220 | _memory_group.manage(&_mul_cell_to_forget_res); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 221 | _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 Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 222 | _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))); |
| 223 | _memory_group.manage(&_cell_to_forget_outstage_res); |
| 224 | 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(); |
| 225 | quantization::calculate_quantized_multiplier(cell_to_forget_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 226 | _cell_to_forget_outstage.configure(compile_context, &_mul_cell_to_forget_res, nullptr, &_cell_to_forget_outstage_res, gemmlowp_info); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 227 | _mul_cell_to_forget_res.allocator()->allocate(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 228 | _accumulate_cell_forget.configure(compile_context, ArithmeticOperation::ADD, &_recurrent_to_forget_outstage_res, &_cell_to_forget_outstage_res, &_recurrent_to_forget_outstage_res, |
| 229 | ConvertPolicy::SATURATE); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 230 | _cell_to_forget_outstage_res.allocator()->allocate(); |
| 231 | } |
| 232 | |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 233 | CLTensor *forget_activation_input = &_recurrent_to_forget_outstage_res; |
| 234 | |
| 235 | if(_has_layer_norm) |
| 236 | { |
| 237 | configure_layer_norm(LayerNormGate::Forget, &_recurrent_to_forget_outstage_res); |
| 238 | _recurrent_to_forget_outstage_res.allocator()->allocate(); |
| 239 | forget_activation_input = &get_layer_norm_output(LayerNormGate::Forget); |
| 240 | } |
| 241 | |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 242 | // Output quantization info of Sigmoid and Tanh activations |
| 243 | const QuantizationInfo sigmoid_tanh_outqinfo(1.f / 32768.f, 0); |
| 244 | |
| 245 | const TensorInfo forget_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); |
| 246 | _memory_group.manage(&_forget_gate); |
| 247 | _forget_gate.allocator()->init(forget_gate_info); |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 248 | _forget_gate_sigmoid.configure(compile_context, forget_activation_input, &_forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
| 249 | forget_activation_input->allocator()->allocate(); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 250 | |
| 251 | // Modulation gate. |
| 252 | const TensorInfo cell_outstage_info(mm_out_info.tensor_shape(), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.cell_intermediate_scale(), 0)); |
| 253 | const float input_to_cell_scale = input_to_cell_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.cell_intermediate_scale(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 254 | configure_mm(compile_context, _mm_input_to_cell, _input_to_cell_outstage, gemmlowp_info, |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 255 | input, &_input_to_cell_weights_transposed, &_input_to_cell_eff_bias, |
| 256 | &_mm_input_to_cell_res, &_input_to_cell_outstage_res, input_to_cell_scale, |
| 257 | mm_out_info, cell_outstage_info); |
| 258 | |
| 259 | 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 Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 260 | configure_mm(compile_context, _mm_recurrent_to_cell, _recurrent_to_cell_outstage, gemmlowp_info, |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 261 | output_state_in, &_recurrent_to_cell_weights_transposed, &_recurrent_to_cell_eff_bias, |
| 262 | &_mm_recurrent_to_cell_res, &_recurrent_to_cell_outstage_res, recurrent_to_cell_scale, |
| 263 | mm_out_info, cell_outstage_info); |
| 264 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 265 | _accumulate_input_recurrent_modulation.configure(compile_context, ArithmeticOperation::ADD, &_input_to_cell_outstage_res, &_recurrent_to_cell_outstage_res, &_recurrent_to_cell_outstage_res, |
| 266 | ConvertPolicy::SATURATE); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 267 | _input_to_cell_outstage_res.allocator()->allocate(); |
| 268 | |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 269 | CLTensor *cell_activation_input = &_recurrent_to_cell_outstage_res; |
| 270 | |
| 271 | if(_has_layer_norm) |
| 272 | { |
| 273 | configure_layer_norm(LayerNormGate::Cell, &_recurrent_to_cell_outstage_res); |
| 274 | _recurrent_to_cell_outstage_res.allocator()->allocate(); |
| 275 | cell_activation_input = &get_layer_norm_output(LayerNormGate::Cell); |
| 276 | } |
| 277 | |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 278 | const TensorInfo cell_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); |
| 279 | _memory_group.manage(&_cell_gate); |
| 280 | _cell_gate.allocator()->init(cell_gate_info); |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 281 | _cell_gate_tanh.configure(compile_context, cell_activation_input, &_cell_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)); |
| 282 | cell_activation_input->allocator()->allocate(); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 283 | |
| 284 | // Input gate. |
| 285 | const TensorInfo input_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); |
| 286 | _input_gate.allocator()->init(input_gate_info); |
| 287 | _memory_group.manage(&_input_gate); |
| 288 | if(_has_cifg) |
| 289 | { |
| 290 | _ones.allocator()->init(*_forget_gate.info()); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 291 | _input_gate_sub.configure(compile_context, ArithmeticOperation::SUB, &_ones, &_forget_gate, &_input_gate, ConvertPolicy::SATURATE); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 292 | _ones.allocator()->allocate(); |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | const TensorInfo input_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0)); |
| 297 | const float input_to_input_scale = _input_to_input_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.input_intermediate_scale(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 298 | configure_mm(compile_context, _mm_input_to_input, _input_to_input_outstage, gemmlowp_info, |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 299 | input, &_input_to_input_weights_transposed, &_input_to_input_eff_bias, |
| 300 | &_mm_input_to_input_res, &_input_to_input_outstage_res, input_to_input_scale, |
| 301 | mm_out_info, input_outstage_info); |
| 302 | |
| 303 | 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 Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 304 | configure_mm(compile_context, _mm_recurrent_to_input, _recurrent_to_input_outstage, gemmlowp_info, |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 305 | input, &_recurrent_to_input_weights_transposed, &_recurrent_to_input_eff_bias, |
| 306 | &_mm_recurrent_to_input_res, &_recurrent_to_input_outstage_res, recurrent_to_input_scale, |
| 307 | mm_out_info, input_outstage_info); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 308 | _accumulate_input_recurrent_input.configure(compile_context, ArithmeticOperation::ADD, &_input_to_input_outstage_res, &_recurrent_to_input_outstage_res, &_recurrent_to_input_outstage_res, |
| 309 | ConvertPolicy::SATURATE); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 310 | _input_to_input_outstage_res.allocator()->allocate(); |
| 311 | |
| 312 | if(_has_peephole) |
| 313 | { |
| 314 | _memory_group.manage(&_mul_cell_to_input_res); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 315 | _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 Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 316 | 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(); |
| 317 | quantization::calculate_quantized_multiplier(cell_to_input_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift); |
| 318 | _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))); |
| 319 | _memory_group.manage(&_cell_to_input_outstage_res); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 320 | _cell_to_input_outstage.configure(compile_context, &_mul_cell_to_input_res, nullptr, &_cell_to_input_outstage_res, gemmlowp_info); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 321 | _mul_cell_to_input_res.allocator()->allocate(); |
| 322 | _accumulate_cell_input.configure(ArithmeticOperation::ADD, &_recurrent_to_input_outstage_res, &_cell_to_input_outstage_res, &_recurrent_to_input_outstage_res, ConvertPolicy::SATURATE); |
| 323 | _cell_to_input_outstage_res.allocator()->allocate(); |
| 324 | } |
| 325 | |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 326 | CLTensor *input_activation_input = &_recurrent_to_input_outstage_res; |
| 327 | |
| 328 | if(_has_layer_norm) |
| 329 | { |
| 330 | configure_layer_norm(LayerNormGate::Input, &_recurrent_to_input_outstage_res); |
| 331 | _recurrent_to_input_outstage_res.allocator()->allocate(); |
| 332 | input_activation_input = &get_layer_norm_output(LayerNormGate::Input); |
| 333 | } |
| 334 | |
| 335 | _input_gate_tanh.configure(compile_context, input_activation_input, &_input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)); |
| 336 | input_activation_input->allocator()->allocate(); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 337 | } |
| 338 | // Cell. |
| 339 | // TODO(COMPMID-3396): Perform multiplication in the quantized domain in CLPixelWiseMultiplicationKernel |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 340 | _pixelwise_mul_forget_cell.configure(compile_context, &_forget_gate, cell_state_in, &_forget_gate, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 341 | const float cell_gate_scale = _cell_gate.info()->quantization_info().uniform().scale; |
| 342 | const float mul_input_cell_scale = cell_gate_scale * std::pow(2, 15 + cell_shift); |
| 343 | const TensorInfo mul_input_cell_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(mul_input_cell_scale, 0)); |
| 344 | _memory_group.manage(&_mul_input_cell_res); |
| 345 | _mul_input_cell_res.allocator()->init(mul_input_cell_info); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 346 | _pixelwise_mul_input_cell.configure(compile_context, &_input_gate, &_cell_gate, &_mul_input_cell_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 347 | _cell_gate.allocator()->allocate(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 348 | _add_forget_cell.configure(compile_context, ArithmeticOperation::ADD, &_forget_gate, &_mul_input_cell_res, cell_state_out, ConvertPolicy::SATURATE); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 349 | _mul_input_cell_res.allocator()->allocate(); |
| 350 | _forget_gate.allocator()->allocate(); |
| 351 | if(_has_cell_clipping) |
| 352 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 353 | _cell_clip.configure(compile_context, cell_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_cell_clip, quantized_cell_clip)); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 354 | } |
| 355 | // Output gate. |
| 356 | const TensorInfo output_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.output_intermediate_scale(), 0)); |
| 357 | const float input_to_output_scale = input_to_output_weights->info()->quantization_info().uniform().scale * qinput.scale / lstm_params.output_intermediate_scale(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 358 | configure_mm(compile_context, _mm_input_to_output, _input_to_output_outstage, gemmlowp_info, |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 359 | input, &_input_to_output_weights_transposed, &_input_to_output_eff_bias, |
| 360 | &_mm_input_to_output_res, &_input_to_output_outstage_res, input_to_output_scale, |
| 361 | mm_out_info, output_outstage_info); |
| 362 | |
| 363 | 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 Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 364 | configure_mm(compile_context, _mm_recurrent_to_output, _recurrent_to_output_outstage, gemmlowp_info, |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 365 | output_state_in, &_recurrent_to_output_weights_transposed, &_recurrent_to_output_eff_bias, |
| 366 | &_mm_recurrent_to_output_res, &_recurrent_to_output_outstage_res, recurrent_to_output_scale, |
| 367 | mm_out_info, output_outstage_info); |
| 368 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 369 | _accumulate_input_recurrent_output.configure(compile_context, ArithmeticOperation::ADD, &_recurrent_to_output_outstage_res, &_input_to_output_outstage_res, &_recurrent_to_output_outstage_res, |
| 370 | ConvertPolicy::SATURATE); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 371 | _input_to_output_outstage_res.allocator()->allocate(); |
| 372 | |
| 373 | if(_has_peephole) |
| 374 | { |
| 375 | // TODO(COMPMID-3396): Perform multiplication in the quantized domain in CLPixelWiseMultiplicationKernel |
| 376 | // Here we are not using the output stage because all operations are done in float |
| 377 | // 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(); |
| 378 | // quantization::calculate_quantized_multiplier(cell_to_output_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift); |
| 379 | _memory_group.manage(&_mul_cell_to_output_res); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 380 | _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); |
| 381 | _accumulate_cell_to_output.configure(compile_context, ArithmeticOperation::ADD, &_recurrent_to_output_outstage_res, &_mul_cell_to_output_res, &_recurrent_to_output_outstage_res, |
| 382 | ConvertPolicy::SATURATE); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 383 | _mul_cell_to_output_res.allocator()->allocate(); |
| 384 | } |
| 385 | |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 386 | CLTensor *output_activation_input = &_recurrent_to_output_outstage_res; |
| 387 | |
| 388 | if(_has_layer_norm) |
| 389 | { |
| 390 | configure_layer_norm(LayerNormGate::Output, &_recurrent_to_output_outstage_res); |
| 391 | _recurrent_to_output_outstage_res.allocator()->allocate(); |
| 392 | output_activation_input = &get_layer_norm_output(LayerNormGate::Output); |
| 393 | } |
| 394 | |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 395 | const TensorInfo output_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); |
| 396 | _memory_group.manage(&_output_gate); |
| 397 | _output_gate.allocator()->init(output_gate_info); |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 398 | _output_gate_sigmoid.configure(compile_context, output_activation_input, &_output_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
| 399 | output_activation_input->allocator()->allocate(); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 400 | |
| 401 | // Hidden. |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 402 | _hidden_tanh.configure(compile_context, cell_state_out, &_input_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 403 | // TODO(COMPMID-3396): Perform multiplication in the quantized domain in CLPixelWiseMultiplicationKernel |
| 404 | _memory_group.manage(&_hidden_mul_res); |
| 405 | const TensorInfo hidden_mul_res(_input_gate.info()->tensor_shape(), 1, DataType::S32); |
| 406 | _hidden_mul_res.allocator()->init(hidden_mul_res); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 407 | _pixelwise_mul_hidden.configure(compile_context, &_output_gate, &_input_gate, &_hidden_mul_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 408 | _output_gate.allocator()->allocate(); |
| 409 | _input_gate.allocator()->allocate(); |
| 410 | const float hidden_state_scale = std::pow(2, -15) / lstm_params.hidden_state_scale() * std::pow(2, -15); |
| 411 | quantization::calculate_quantized_multiplier(hidden_state_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift, /* ignore_epsilon */ true); |
| 412 | gemmlowp_info.gemmlowp_offset = lstm_params.hidden_state_zero(); |
| 413 | gemmlowp_info.output_data_type = output_state_in->info()->data_type(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 414 | _hidden_outstage.configure(compile_context, &_hidden_mul_res, nullptr, output_state_out, gemmlowp_info); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 415 | _hidden_mul_res.allocator()->allocate(); |
| 416 | |
| 417 | // Projection. |
| 418 | if(_has_projection) |
| 419 | { |
| 420 | const TensorInfo projection_outstage_info(*output_state_out->info()); |
| 421 | const UniformQuantizationInfo qprojection = _projection_weights->info()->quantization_info().uniform(); |
| 422 | const float projection_scale = qprojection.scale * lstm_params.hidden_state_scale() / qoutput_state_in.scale; |
| 423 | gemmlowp_info.gemmlowp_offset = qoutput_state_in.offset; |
| 424 | gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int8_t>::lowest(); |
| 425 | gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int8_t>::max(); |
| 426 | gemmlowp_info.output_data_type = DataType::QASYMM8_SIGNED; |
| 427 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 428 | configure_mm(compile_context, _mm_projection, _projection_outstage, gemmlowp_info, |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 429 | output_state_out, &_projection_weights_transposed, &_projection_eff_bias, |
| 430 | &_mm_projection_res, &_projection_outstage_res, projection_scale, |
| 431 | mm_out_info, projection_outstage_info); |
| 432 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 433 | _accumulate_projection.configure(compile_context, ArithmeticOperation::ADD, &_projection_outstage_res, output_state_out, output_state_out, ConvertPolicy::SATURATE); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 434 | _projection_outstage_res.allocator()->allocate(); |
| 435 | |
| 436 | int8_t quantized_projection_clip{ 0 }; |
| 437 | if(lstm_params.projection_clip() > 0.0f) |
| 438 | { |
| 439 | quantized_projection_clip = utility::clamp<int8_t>(lstm_params.projection_clip() / qprojection.scale, -128, 127); |
| 440 | } |
| 441 | |
| 442 | if(quantized_projection_clip > 0) |
| 443 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 444 | _projection_clip.configure(compile_context, output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_projection_clip, |
| 445 | quantized_projection_clip)); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 446 | _has_projection_clipping = true; |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | Status CLQLSTMLayer::validate(const ITensorInfo *input, |
| 452 | const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights, |
| 453 | const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights, |
| 454 | const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias, |
| 455 | const ITensorInfo *cell_state_in, const ITensorInfo *output_state_in, |
| 456 | const ITensorInfo *cell_state_out, const ITensorInfo *output_state_out, |
| 457 | const LSTMParams<ITensorInfo> &lstm_params) |
| 458 | { |
| 459 | 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, |
| 460 | recurrent_to_output_weights, forget_gate_bias, cell_bias, output_gate_bias, cell_state_in, output_state_in, cell_state_out, output_state_out); |
| 461 | |
| 462 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED); |
| 463 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() != 2, "Input must have exactly 2 dimensions"); |
| 464 | |
| 465 | const unsigned int input_size = input->dimension(0); |
| 466 | const unsigned int batch_size = input->dimension(1); |
| 467 | const unsigned int num_units = input_to_output_weights->dimension(1); |
| 468 | const unsigned int output_size = recurrent_to_output_weights->dimension(0); |
| 469 | |
| 470 | ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->num_dimensions() != 2); |
| 471 | ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->dimension(0) != input_size); |
| 472 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_to_output_weights, input_to_forget_weights, input_to_cell_weights); |
| 473 | ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->num_dimensions() != 2); |
| 474 | ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->dimension(1) != num_units); |
| 475 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(recurrent_to_output_weights, recurrent_to_forget_weights, recurrent_to_cell_weights); |
| 476 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_to_forget_weights, 1, DataType::QSYMM8); |
| 477 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_to_forget_weights, input_to_cell_weights, input_to_output_weights, |
| 478 | recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights); |
| 479 | |
| 480 | ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->num_dimensions() != 1); |
| 481 | ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->dimension(0) != num_units); |
| 482 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(forget_gate_bias, cell_bias, output_gate_bias); |
| 483 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(forget_gate_bias, 1, DataType::S32); |
| 484 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(forget_gate_bias, cell_bias, output_gate_bias); |
| 485 | |
| 486 | ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->num_dimensions() != 2); |
| 487 | ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->dimension(0) != num_units); |
| 488 | ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->dimension(1) != batch_size); |
| 489 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(cell_state_in, 1, DataType::QSYMM16); |
| 490 | |
| 491 | ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() != 2); |
| 492 | ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->dimension(0) != output_size); |
| 493 | ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->dimension(1) != batch_size); |
| 494 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output_state_in); |
| 495 | |
| 496 | // Check whether peephole weights are all there or none |
| 497 | if(lstm_params.has_peephole_opt()) |
| 498 | { |
| 499 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights()); |
| 500 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_forget_weights(), 1, DataType::QSYMM16); |
| 501 | ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->num_dimensions() != 1); |
| 502 | ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->dimension(0) != num_units); |
| 503 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights()); |
| 504 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_output_weights()); |
| 505 | |
| 506 | if(!lstm_params.has_cifg_opt()) |
| 507 | { |
| 508 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_input_weights()); |
| 509 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_input_weights()); |
| 510 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(lstm_params.cell_to_forget_weights(), lstm_params.cell_to_input_weights()); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | const UniformQuantizationInfo qinput = input->quantization_info().uniform(); |
| 515 | const UniformQuantizationInfo qcell_state_in = cell_state_in->quantization_info().uniform(); |
| 516 | const UniformQuantizationInfo qoutput_state_in = output_state_in->quantization_info().uniform(); |
| 517 | |
| 518 | // Calculate and decompose effective scales for optimizing matmul calculation |
| 519 | const int32_t cell_shift = log2(qcell_state_in.scale); |
| 520 | ARM_COMPUTE_RETURN_ERROR_ON(cell_shift > -9); |
| 521 | |
| 522 | // Calculate quantized parameters for clipping. |
| 523 | int16_t quantized_cell_clip = 0; |
| 524 | if(lstm_params.cell_clip() > 0.0f) |
| 525 | { |
| 526 | quantized_cell_clip = quantize_qsymm16(lstm_params.cell_clip(), qcell_state_in); |
| 527 | } |
| 528 | |
| 529 | // Precompute effective bias for optimizing the matmul computations. |
| 530 | const TensorInfo eff_bias_info(TensorShape(num_units), 1, DataType::S32); |
| 531 | if(!lstm_params.has_cifg_opt()) |
| 532 | { |
| 533 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(lstm_params.input_to_input_weights(), &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true))); |
| 534 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(lstm_params.recurrent_to_input_weights(), &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, |
| 535 | true))); |
| 536 | } |
| 537 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(input_to_forget_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true))); |
| 538 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(recurrent_to_forget_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true))); |
| 539 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(input_to_cell_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true))); |
| 540 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(recurrent_to_cell_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true))); |
| 541 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(input_to_output_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qinput.offset, true))); |
| 542 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(recurrent_to_output_weights, &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, -qoutput_state_in.offset, true))); |
| 543 | if(lstm_params.projection_bias() != nullptr) |
| 544 | { |
| 545 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixAReductionKernel::validate(lstm_params.projection_weights(), &eff_bias_info, GEMMLowpReductionKernelInfo(num_units, false, lstm_params.hidden_state_zero(), |
| 546 | true))); |
| 547 | ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::ADD, lstm_params.projection_bias(), &eff_bias_info, &eff_bias_info, ConvertPolicy::SATURATE)); |
| 548 | } |
| 549 | |
| 550 | const TensorInfo input_weights_transposed(TensorShape(num_units, input_size), 1, input_to_forget_weights->data_type(), input_to_forget_weights->quantization_info()); |
| 551 | const TensorInfo recurrent_weights_transposed(TensorShape(num_units, output_size), 1, recurrent_to_forget_weights->data_type(), recurrent_to_forget_weights->quantization_info()); |
| 552 | |
| 553 | // Validate weights transpose |
| 554 | ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(input_to_forget_weights, &input_weights_transposed)); |
| 555 | ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(input_to_cell_weights, &input_weights_transposed)); |
| 556 | ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(input_to_output_weights, &input_weights_transposed)); |
| 557 | ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(recurrent_to_forget_weights, &recurrent_weights_transposed)); |
| 558 | ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(recurrent_to_cell_weights, &recurrent_weights_transposed)); |
| 559 | ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(recurrent_to_output_weights, &recurrent_weights_transposed)); |
| 560 | if(!lstm_params.has_cifg_opt()) |
| 561 | { |
| 562 | ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(lstm_params.input_to_input_weights(), &input_weights_transposed)); |
| 563 | ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(lstm_params.recurrent_to_input_weights(), &recurrent_weights_transposed)); |
| 564 | } |
| 565 | if(lstm_params.has_projection()) |
| 566 | { |
| 567 | ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(lstm_params.projection_weights(), &recurrent_weights_transposed)); |
| 568 | } |
| 569 | |
| 570 | GEMMLowpOutputStageInfo gemmlowp_info; |
| 571 | gemmlowp_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; |
| 572 | gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int16_t>::lowest(); |
| 573 | gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int16_t>::max(); |
| 574 | gemmlowp_info.output_data_type = DataType::QSYMM16; |
| 575 | |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 576 | const bool has_layer_norm = lstm_params.use_layer_norm(); |
| 577 | |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 578 | // Forget gate. |
| 579 | const TensorInfo forget_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.forget_intermediate_scale(), 0)); |
| 580 | const TensorInfo mm_out_info(TensorShape(num_units, batch_size), 1, DataType::S32); |
| 581 | const float input_to_forget_scale = input_to_forget_weights->quantization_info().uniform().scale * qinput.scale / lstm_params.forget_intermediate_scale(); |
| 582 | validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, input_to_forget_scale, &mm_out_info, &forget_outstage_info); |
| 583 | |
| 584 | const float recurrent_to_forget_scale = recurrent_to_forget_weights->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.forget_intermediate_scale(); |
| 585 | validate_mm(gemmlowp_info, input, &recurrent_weights_transposed, &eff_bias_info, recurrent_to_forget_scale, &mm_out_info, &forget_outstage_info); |
| 586 | |
| 587 | ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::ADD, &forget_outstage_info, &forget_outstage_info, &forget_outstage_info, ConvertPolicy::SATURATE)); |
| 588 | |
| 589 | if(lstm_params.has_peephole_opt()) |
| 590 | { |
| 591 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_forget_weights(), 1, DataType::QSYMM16); |
| 592 | ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_forget_weights(), &mm_out_info, 1.f, ConvertPolicy::SATURATE, |
| 593 | RoundingPolicy::TO_ZERO)); |
| 594 | 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(); |
| 595 | ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(cell_to_forget_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift)); |
| 596 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&mm_out_info, nullptr, &forget_outstage_info, gemmlowp_info)); |
| 597 | ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::ADD, &forget_outstage_info, &forget_outstage_info, &forget_outstage_info, ConvertPolicy::SATURATE)); |
| 598 | } |
| 599 | |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 600 | if(has_layer_norm) |
| 601 | { |
| 602 | const ITensorInfo *w_info = lstm_params.forget_layer_norm_weights(); |
| 603 | const ITensorInfo *b_info = forget_gate_bias; |
| 604 | ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(forget_outstage_info, *w_info, *b_info)); |
| 605 | } |
| 606 | |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 607 | // Output quantization info of Sigmoid and Tanh activations |
| 608 | const QuantizationInfo sigmoid_tanh_outqinfo(1.f / 32768.f, 0); |
| 609 | |
| 610 | const TensorInfo forget_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); |
| 611 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&forget_outstage_info, &forget_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); |
| 612 | |
| 613 | // Modulation gate. |
| 614 | const TensorInfo cell_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.cell_intermediate_scale(), 0)); |
| 615 | const float input_to_cell_scale = input_to_cell_weights->quantization_info().uniform().scale * qinput.scale / lstm_params.cell_intermediate_scale(); |
| 616 | validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, input_to_cell_scale, &mm_out_info, &cell_outstage_info); |
| 617 | |
| 618 | const float recurrent_to_cell_scale = recurrent_to_cell_weights->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.cell_intermediate_scale(); |
| 619 | validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, recurrent_to_cell_scale, &mm_out_info, &cell_outstage_info); |
| 620 | |
| 621 | ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::ADD, &cell_outstage_info, &cell_outstage_info, &cell_outstage_info, ConvertPolicy::SATURATE)); |
| 622 | |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 623 | if(has_layer_norm) |
| 624 | { |
| 625 | const ITensorInfo *w_info = lstm_params.cell_layer_norm_weights(); |
| 626 | const ITensorInfo *b_info = cell_bias; |
| 627 | ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(cell_outstage_info, *w_info, *b_info)); |
| 628 | } |
| 629 | |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 630 | const TensorInfo cell_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); |
| 631 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&cell_outstage_info, &cell_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f))); |
| 632 | |
| 633 | // Input gate. |
| 634 | const TensorInfo input_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); |
| 635 | if(lstm_params.has_cifg_opt()) |
| 636 | { |
| 637 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(lstm_params.input_gate_bias() != nullptr, "Input gate bias must not be present when CIFG is used"); |
| 638 | ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::SUB, &input_gate_info, &forget_gate_info, &forget_gate_info, ConvertPolicy::SATURATE)); |
| 639 | } |
| 640 | else |
| 641 | { |
| 642 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights(), lstm_params.input_gate_bias()); |
| 643 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input_to_forget_weights, lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights()); |
| 644 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input_to_forget_weights, lstm_params.input_to_input_weights()); |
| 645 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(recurrent_to_forget_weights, lstm_params.recurrent_to_input_weights()); |
| 646 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(forget_gate_bias, lstm_params.input_gate_bias()); |
| 647 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(forget_gate_bias, lstm_params.input_gate_bias()); |
| 648 | |
| 649 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(input, lstm_params.input_to_input_weights(), nullptr, &mm_out_info)); |
| 650 | const TensorInfo input_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.input_intermediate_scale(), 0)); |
| 651 | const float input_to_input_scale = lstm_params.input_to_input_weights()->quantization_info().uniform().scale * qinput.scale / lstm_params.input_intermediate_scale(); |
| 652 | validate_mm(gemmlowp_info, input, lstm_params.input_to_input_weights(), &eff_bias_info, input_to_input_scale, &mm_out_info, &input_outstage_info); |
| 653 | |
| 654 | 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(); |
| 655 | validate_mm(gemmlowp_info, input, lstm_params.recurrent_to_input_weights(), &eff_bias_info, recurrent_to_input_scale, &mm_out_info, &input_outstage_info); |
| 656 | |
| 657 | ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::ADD, &input_outstage_info, &input_outstage_info, &input_outstage_info, ConvertPolicy::SATURATE)); |
| 658 | |
| 659 | if(lstm_params.has_peephole_opt()) |
| 660 | { |
| 661 | ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_input_weights(), &input_outstage_info, 1.f, ConvertPolicy::SATURATE, |
| 662 | RoundingPolicy::TO_ZERO)); |
| 663 | 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(); |
| 664 | ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(cell_to_input_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift)); |
| 665 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&input_outstage_info, &eff_bias_info, &input_outstage_info, gemmlowp_info)); |
| 666 | ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::ADD, &input_outstage_info, &input_outstage_info, &input_outstage_info, ConvertPolicy::SATURATE)); |
| 667 | } |
| 668 | |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 669 | if(has_layer_norm) |
| 670 | { |
| 671 | const ITensorInfo *w_info = lstm_params.input_layer_norm_weights(); |
| 672 | const ITensorInfo *b_info = lstm_params.input_gate_bias(); |
| 673 | ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(cell_outstage_info, *w_info, *b_info)); |
| 674 | } |
| 675 | |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 676 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&input_outstage_info, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f))); |
| 677 | } |
| 678 | // Cell. |
| 679 | ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&forget_gate_info, cell_state_in, &forget_gate_info, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); |
| 680 | ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&input_gate_info, cell_state_in, &cell_gate_info, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); |
| 681 | ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::ADD, &forget_gate_info, &cell_gate_info, cell_state_out, ConvertPolicy::SATURATE)); |
| 682 | if(quantized_cell_clip > 0) |
| 683 | { |
| 684 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(cell_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_cell_clip, |
| 685 | quantized_cell_clip))); |
| 686 | } |
| 687 | // Output gate. |
| 688 | const TensorInfo output_outstage_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, QuantizationInfo(lstm_params.output_intermediate_scale(), 0)); |
| 689 | const float input_to_output_scale = input_to_output_weights->quantization_info().uniform().scale * qinput.scale / lstm_params.output_intermediate_scale(); |
| 690 | validate_mm(gemmlowp_info, input, &input_weights_transposed, &eff_bias_info, input_to_output_scale, &mm_out_info, &output_outstage_info); |
| 691 | |
| 692 | const float recurrent_to_output_scale = recurrent_to_output_weights->quantization_info().uniform().scale * qoutput_state_in.scale / lstm_params.output_intermediate_scale(); |
| 693 | validate_mm(gemmlowp_info, output_state_in, &recurrent_weights_transposed, &eff_bias_info, recurrent_to_output_scale, &mm_out_info, &output_outstage_info); |
| 694 | |
| 695 | ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::ADD, &output_outstage_info, &output_outstage_info, &output_outstage_info, ConvertPolicy::SATURATE)); |
| 696 | if(lstm_params.has_peephole_opt()) |
| 697 | { |
| 698 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lstm_params.cell_to_output_weights(), 1, DataType::QSYMM16); |
| 699 | // TODO(COMPMID-3395): Perform multiplication in the quantized domain in NEPixelWiseMultiplicationKernel |
| 700 | // Here we are not using the output stage because all operations are done in float |
| 701 | // 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(); |
| 702 | // ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(cell_to_output_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift)); |
| 703 | ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(cell_state_out, lstm_params.cell_to_output_weights(), &output_outstage_info, 1.f, ConvertPolicy::SATURATE, |
| 704 | RoundingPolicy::TO_ZERO)); |
| 705 | ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::ADD, &output_outstage_info, &output_outstage_info, &output_outstage_info, ConvertPolicy::SATURATE)); |
| 706 | } |
| 707 | |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 708 | if(has_layer_norm) |
| 709 | { |
| 710 | const ITensorInfo *w_info = lstm_params.output_layer_norm_weights(); |
| 711 | const ITensorInfo *b_info = output_gate_bias; |
| 712 | ARM_COMPUTE_RETURN_ON_ERROR(validate_layer_norm(output_outstage_info, *w_info, *b_info)); |
| 713 | } |
| 714 | |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 715 | const TensorInfo output_gate_info(TensorShape(num_units, batch_size), 1, DataType::QSYMM16, sigmoid_tanh_outqinfo); |
| 716 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(&output_outstage_info, &output_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); |
| 717 | |
| 718 | // Hidden. |
| 719 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(cell_state_out, &input_gate_info, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f))); |
| 720 | const TensorInfo hidden_mul_res(TensorShape(num_units, batch_size), 1, DataType::S32); |
| 721 | ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&output_gate_info, &input_gate_info, &hidden_mul_res, 1.f, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); |
| 722 | const float hidden_state_scale = std::pow(2, -15) / lstm_params.hidden_state_scale() * std::pow(2, -15); |
| 723 | ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(hidden_state_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift, /* ignore_epsilon */ true)); |
| 724 | gemmlowp_info.gemmlowp_offset = lstm_params.hidden_state_zero(); |
| 725 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&hidden_mul_res, nullptr, output_state_out, gemmlowp_info)); |
| 726 | |
| 727 | // Projection. |
| 728 | if(lstm_params.has_projection()) |
| 729 | { |
| 730 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(recurrent_to_forget_weights, lstm_params.projection_weights()); |
| 731 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(forget_gate_bias, lstm_params.projection_bias()); |
| 732 | |
| 733 | const UniformQuantizationInfo qprojection = lstm_params.projection_weights()->quantization_info().uniform(); |
| 734 | const float projection_scale = qprojection.scale * lstm_params.hidden_state_scale() / qoutput_state_in.scale; |
| 735 | ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(projection_scale, &gemmlowp_info.gemmlowp_multiplier, &gemmlowp_info.gemmlowp_shift)); |
| 736 | gemmlowp_info.gemmlowp_offset = qoutput_state_in.offset; |
| 737 | gemmlowp_info.gemmlowp_min_bound = std::numeric_limits<int8_t>::lowest(); |
| 738 | gemmlowp_info.gemmlowp_max_bound = std::numeric_limits<int8_t>::max(); |
| 739 | gemmlowp_info.output_data_type = DataType::QASYMM8_SIGNED; |
| 740 | |
| 741 | const TensorInfo projection_outstage_info(*output_state_out); |
| 742 | validate_mm(gemmlowp_info, output_state_out, &recurrent_weights_transposed, &eff_bias_info, input_to_output_scale, &mm_out_info, &projection_outstage_info); |
| 743 | |
| 744 | ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::ADD, output_state_out, output_state_out, output_state_out, ConvertPolicy::SATURATE)); |
| 745 | |
| 746 | int8_t quantized_projection_clip{ 0 }; |
| 747 | if(lstm_params.projection_clip() > 0.0f) |
| 748 | { |
| 749 | quantized_projection_clip = quantize_qasymm8_signed(lstm_params.projection_clip(), qprojection); |
| 750 | } |
| 751 | |
| 752 | if(quantized_projection_clip > 0) |
| 753 | { |
| 754 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -quantized_projection_clip, |
| 755 | quantized_projection_clip))); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | if(cell_state_out->total_size() > 0) |
| 760 | { |
| 761 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(cell_state_in, cell_state_out); |
| 762 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(cell_state_in, cell_state_out); |
| 763 | } |
| 764 | |
| 765 | if(output_state_out->total_size() > 0) |
| 766 | { |
| 767 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output_state_out); |
| 768 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output_state_in, output_state_out); |
| 769 | } |
| 770 | |
| 771 | return Status{}; |
| 772 | } |
| 773 | |
| 774 | void CLQLSTMLayer::run() |
| 775 | { |
| 776 | prepare(); |
| 777 | |
| 778 | // Acquire all the temporaries |
| 779 | MemoryGroupResourceScope scope_mg(_memory_group); |
| 780 | |
| 781 | // Forget gate. |
| 782 | _mm_input_to_forget.run(); |
| 783 | _input_to_forget_outstage.run(); |
| 784 | |
| 785 | _mm_recurrent_to_forget.run(); |
| 786 | _recurrent_to_forget_outstage.run(); |
| 787 | CLScheduler::get().enqueue(_accumulate_input_recurrent_forget); |
| 788 | |
| 789 | if(_has_peephole) |
| 790 | { |
| 791 | CLScheduler::get().enqueue(_pixelwise_mul_cell_to_forget); |
| 792 | _cell_to_forget_outstage.run(); |
| 793 | CLScheduler::get().enqueue(_accumulate_cell_forget); |
| 794 | } |
| 795 | |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 796 | if(_has_layer_norm) |
| 797 | { |
| 798 | CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Forget)); |
| 799 | } |
| 800 | |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 801 | _forget_gate_sigmoid.run(); |
| 802 | |
| 803 | // Modulation gate. |
| 804 | _mm_input_to_cell.run(); |
| 805 | _input_to_cell_outstage.run(); |
| 806 | |
| 807 | _mm_recurrent_to_cell.run(); |
| 808 | _recurrent_to_cell_outstage.run(); |
| 809 | CLScheduler::get().enqueue(_accumulate_input_recurrent_modulation); |
| 810 | |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 811 | if(_has_layer_norm) |
| 812 | { |
| 813 | CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Cell)); |
| 814 | } |
| 815 | |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 816 | _cell_gate_tanh.run(); |
| 817 | |
| 818 | // Input gate |
| 819 | if(_has_cifg) |
| 820 | { |
| 821 | CLScheduler::get().enqueue(_input_gate_sub); |
| 822 | } |
| 823 | else |
| 824 | { |
| 825 | _mm_input_to_input.run(); |
| 826 | _input_to_input_outstage.run(); |
| 827 | _mm_recurrent_to_input.run(); |
| 828 | _recurrent_to_input_outstage.run(); |
| 829 | CLScheduler::get().enqueue(_accumulate_input_recurrent_input); |
| 830 | |
| 831 | if(_has_peephole) |
| 832 | { |
| 833 | CLScheduler::get().enqueue(_pixelwise_mul_cell_to_input); |
| 834 | _cell_to_input_outstage.run(); |
| 835 | CLScheduler::get().enqueue(_accumulate_cell_input); |
| 836 | } |
| 837 | |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 838 | if(_has_layer_norm) |
| 839 | { |
| 840 | CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Input)); |
| 841 | } |
| 842 | |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 843 | _input_gate_tanh.run(); |
| 844 | } |
| 845 | |
| 846 | // Cell. |
| 847 | CLScheduler::get().enqueue(_pixelwise_mul_forget_cell); |
| 848 | CLScheduler::get().enqueue(_pixelwise_mul_input_cell); |
| 849 | CLScheduler::get().enqueue(_add_forget_cell); |
| 850 | if(_has_cell_clipping) |
| 851 | { |
| 852 | _cell_clip.run(); |
| 853 | } |
| 854 | |
| 855 | // Output gate. |
| 856 | _mm_input_to_output.run(); |
| 857 | _input_to_output_outstage.run(); |
| 858 | _mm_recurrent_to_output.run(); |
| 859 | _recurrent_to_output_outstage.run(); |
| 860 | CLScheduler::get().enqueue(_accumulate_input_recurrent_output); |
| 861 | if(_has_peephole) |
| 862 | { |
| 863 | CLScheduler::get().enqueue(_pixelwise_mul_cell_to_output); |
| 864 | CLScheduler::get().enqueue(_accumulate_cell_to_output); |
| 865 | } |
| 866 | |
Sheri Zhang | 3a35398 | 2020-04-21 13:10:24 +0100 | [diff] [blame] | 867 | if(_has_layer_norm) |
| 868 | { |
| 869 | CLScheduler::get().enqueue(get_layer_norm(LayerNormGate::Output)); |
| 870 | } |
| 871 | |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 872 | _output_gate_sigmoid.run(); |
| 873 | |
| 874 | // Hidden. |
| 875 | _hidden_tanh.run(); |
| 876 | CLScheduler::get().enqueue(_pixelwise_mul_hidden); |
| 877 | _hidden_outstage.run(); |
| 878 | |
| 879 | // Projection. |
| 880 | if(_has_projection) |
| 881 | { |
| 882 | _mm_projection.run(); |
| 883 | _projection_outstage.run(); |
| 884 | CLScheduler::get().enqueue(_accumulate_projection); |
| 885 | if(_has_projection_clipping) |
| 886 | { |
| 887 | _projection_clip.run(); |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | void CLQLSTMLayer::prepare() |
| 893 | { |
| 894 | if(!_is_prepared) |
| 895 | { |
| 896 | // Pre-transpose weights to be used in GEMM. |
| 897 | _input_to_forget_weights_transposed.allocator()->allocate(); |
| 898 | _input_to_cell_weights_transposed.allocator()->allocate(); |
| 899 | _input_to_output_weights_transposed.allocator()->allocate(); |
| 900 | _recurrent_to_forget_weights_transposed.allocator()->allocate(); |
| 901 | _recurrent_to_cell_weights_transposed.allocator()->allocate(); |
| 902 | _recurrent_to_output_weights_transposed.allocator()->allocate(); |
| 903 | _transpose_input_to_forget_weights.run(); |
| 904 | _transpose_input_to_cell_weights.run(); |
| 905 | _transpose_input_to_output_weights.run(); |
| 906 | _transpose_recurrent_to_forget_weights.run(); |
| 907 | _transpose_recurrent_to_cell_weights.run(); |
| 908 | _transpose_recurrent_to_output_weights.run(); |
| 909 | |
| 910 | // Precompute effective biases |
| 911 | if(_has_cifg) |
| 912 | { |
| 913 | _ones.map(true); |
| 914 | std::fill_n(reinterpret_cast<int16_t *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 32767); |
| 915 | _ones.unmap(); |
| 916 | } |
| 917 | else |
| 918 | { |
| 919 | _input_to_input_eff_bias.allocator()->allocate(); |
| 920 | _recurrent_to_input_eff_bias.allocator()->allocate(); |
| 921 | CLScheduler::get().enqueue(_input_to_input_reduction); |
| 922 | CLScheduler::get().enqueue(_recurrent_to_input_reduction); |
| 923 | |
| 924 | _input_to_input_weights_transposed.allocator()->allocate(); |
| 925 | _recurrent_to_input_weights_transposed.allocator()->allocate(); |
| 926 | _transpose_input_to_input_weights.run(); |
| 927 | _transpose_recurrent_to_input_weights.run(); |
| 928 | _input_to_input_weights->mark_as_unused(); |
| 929 | _recurrent_to_input_weights->mark_as_unused(); |
| 930 | } |
| 931 | _input_to_forget_eff_bias.allocator()->allocate(); |
| 932 | _recurrent_to_forget_eff_bias.allocator()->allocate(); |
| 933 | _input_to_cell_eff_bias.allocator()->allocate(); |
| 934 | _recurrent_to_cell_eff_bias.allocator()->allocate(); |
| 935 | _input_to_output_eff_bias.allocator()->allocate(); |
| 936 | _recurrent_to_output_eff_bias.allocator()->allocate(); |
| 937 | CLScheduler::get().enqueue(_input_to_forget_reduction); |
| 938 | CLScheduler::get().enqueue(_recurrent_to_forget_reduction); |
| 939 | CLScheduler::get().enqueue(_input_to_cell_reduction); |
| 940 | CLScheduler::get().enqueue(_recurrent_to_cell_reduction); |
| 941 | CLScheduler::get().enqueue(_input_to_output_reduction); |
| 942 | CLScheduler::get().enqueue(_recurrent_to_output_reduction); |
| 943 | |
| 944 | if(_has_projection) |
| 945 | { |
| 946 | if(_projection_bias != nullptr) |
| 947 | { |
| 948 | _projection_eff_bias.allocator()->allocate(); |
| 949 | CLScheduler::get().enqueue(_projection_reduction); |
| 950 | _projection_bias->mark_as_unused(); |
| 951 | } |
| 952 | |
| 953 | _projection_weights_transposed.allocator()->allocate(); |
| 954 | _transpose_projection_weights.run(); |
| 955 | _projection_weights->mark_as_unused(); |
| 956 | } |
| 957 | |
| 958 | // Mark weights as unused |
| 959 | _input_to_forget_weights->mark_as_unused(); |
| 960 | _input_to_cell_weights->mark_as_unused(); |
| 961 | _input_to_output_weights->mark_as_unused(); |
| 962 | _recurrent_to_forget_weights->mark_as_unused(); |
| 963 | _recurrent_to_cell_weights->mark_as_unused(); |
| 964 | _recurrent_to_output_weights->mark_as_unused(); |
| 965 | |
| 966 | CLScheduler::get().queue().finish(); |
| 967 | _is_prepared = true; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | } // namespace arm_compute |