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