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