Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [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/CLLSTMLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/PixelValue.h" |
| 27 | #include "arm_compute/core/Utils.h" |
| 28 | #include "arm_compute/core/Validate.h" |
| 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 30 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 31 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 32 | |
| 33 | #include <cmath> |
| 34 | #include <memory> |
| 35 | #include <tuple> |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | using namespace arm_compute::misc::shape_calculator; |
| 39 | |
| 40 | CLLSTMLayer::CLLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 41 | : _memory_group(std::move(memory_manager)), _fully_connected_input_gate(), _gemm_input_gate(), _transpose_input_gate(), _accum_input_gate1(), _accum_input_gate2(), _subtract_input_gate(), |
| 42 | _pixelwise_mul_input_gate(), _activation_input_gate(), _fully_connected_forget_gate(), _gemm_forget_gate(), _transpose_forget_gate(), _accum_forget_gate1(), _accum_forget_gate2(), |
| 43 | _pixelwise_mul_forget_gate(), _activation_forget_gate(), _fully_connected_cell_state(), _gemm_cell_state1(), _gemm_cell_state2(), _transpose_cell_state(), _accum_cell_state1(), _accum_cell_state2(), |
| 44 | _pixelwise_mul_cell_state1(), _activation_cell_state(), _cell_clip(), _pixelwise_mul_cell_state2(), _fully_connected_output(), _gemm_output(), _pixelwise_mul_output_state1(), _transpose_output(), |
| 45 | _accum_output1(), _accum_output2(), _activation_output(), _activation_output_state(), _pixelwise_mul_output_state2(), _fully_connected_output_state(), _gemm_output_state(), _accum_output_state(), |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 46 | _projection_clip(), _copy_cell_state(), _copy_output(), _concat_scratch_buffer(), _concat_inputs_forget_gate(), _concat_weights_forget_gate(), _concat_weights_input_gate(), _concat_weights_output(), |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 47 | _ones_memset_kernel(), _input_gate_out1(), _input_gate_out2(), _input_gate_out3(), _input_gate_out4(), _forget_gate_out1(), _forget_gate_out2(), _forget_gate_out3(), _forget_gate_out4(), |
| 48 | _forget_gate_out5(), _forget_gate_out6(), _cell_state_out1(), _cell_state_out2(), _cell_state_out3(), _cell_state_out4(), _cell_state_out5(), _output1(), _output2(), _output3(), _output4(), |
| 49 | _cell_state_activation(), _output_state1(), _ones(), _run_peephole_opt(false), _run_cifg_opt(false), _perform_cell_clipping(false), _has_projection_weights(false), _perform_projection_clipping(false), |
| 50 | _is_prepared(false) |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 51 | { |
| 52 | } |
| 53 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 54 | void CLLSTMLayer::configure(const ICLTensor *input, |
| 55 | const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights, |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 56 | const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights, |
| 57 | const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias, |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 58 | const ICLTensor *output_state_in, const ICLTensor *cell_state_in, |
| 59 | ICLTensor *scratch_buffer, ICLTensor *output_state_out, ICLTensor *cell_state_out, ICLTensor *output, |
| 60 | const LSTMParams<ICLTensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold) |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 61 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 62 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, |
| 63 | input_to_forget_weights, input_to_cell_weights, input_to_output_weights, |
| 64 | recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights, |
| 65 | forget_gate_bias, cell_bias, output_gate_bias, |
| 66 | output_state_in, cell_state_in, |
| 67 | scratch_buffer, output_state_out, cell_state_out, output); |
| 68 | |
| 69 | // Set lstm parameters |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 70 | LSTMParams<ITensorInfo> lstm_params_info; |
| 71 | if(lstm_params.has_peephole_opt()) |
| 72 | { |
Michalis Spyrou | 09daf4d | 2018-06-28 17:07:22 +0100 | [diff] [blame] | 73 | lstm_params_info.set_peephole_params(lstm_params.cell_to_forget_weights()->info(), lstm_params.cell_to_output_weights()->info()); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 74 | } |
| 75 | if(lstm_params.has_projection()) |
| 76 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 77 | lstm_params_info.set_projection_params(lstm_params.projection_weights()->info(), |
| 78 | lstm_params.projection_bias() != nullptr ? lstm_params.projection_bias()->info() : nullptr); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 79 | } |
| 80 | if(!lstm_params.has_cifg_opt()) |
| 81 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 82 | const ITensorInfo *cell_to_input_weights_info = (lstm_params.has_peephole_opt()) ? lstm_params.cell_to_input_weights()->info() : nullptr; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 83 | lstm_params_info.set_cifg_params(lstm_params.input_to_input_weights()->info(), lstm_params.recurrent_to_input_weights()->info(), |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 84 | cell_to_input_weights_info, lstm_params.input_gate_bias()->info()); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 85 | } |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 86 | |
| 87 | // Validate |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 88 | ARM_COMPUTE_ERROR_THROW_ON(CLLSTMLayer::validate(input->info(), input_to_forget_weights->info(), |
| 89 | input_to_cell_weights->info(), input_to_output_weights->info(), |
| 90 | recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(), |
| 91 | forget_gate_bias->info(), cell_bias->info(), output_gate_bias->info(), |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 92 | output_state_in->info(), cell_state_in->info(), |
| 93 | scratch_buffer->info(), output_state_out->info(), cell_state_out->info(), output->info(), |
| 94 | lstm_params_info, activation_info, cell_threshold, projection_threshold)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 95 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 96 | const TensorShape cell_state_shape = cell_state_in->info()->tensor_shape(); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 97 | // Configure block that calculates the forget gate |
| 98 | // forget_gate = Activation(input * input_to_forget_weights + output_state_in * recurrent_to_forget_weights + PixelWiseMul(cell_state, cell_to_forget_weights) + forget_gate_bias) |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 99 | // We optimize this as follows: |
| 100 | // forget_gate = Activation( (input,output_state_in) * (input_to_forget_weights,recurrent_to_forget_weights) + PixelWiseMul(cell_state, cell_to_forget_weights) + forget_gate_bias |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 101 | _forget_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 102 | _forget_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 103 | _forget_gate_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 104 | |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 105 | std::vector<const ICLTensor *> inputs_vector; |
| 106 | inputs_vector.emplace_back(input); |
| 107 | inputs_vector.emplace_back(output_state_in); |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 108 | const TensorShape concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, 0); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 109 | _forget_gate_out2.allocator()->init(TensorInfo(concat_shape, 1, input->info()->data_type())); |
| 110 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 111 | _memory_group.manage(&_forget_gate_out2); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 112 | _concat_inputs_forget_gate.configure(input, output_state_in, &_forget_gate_out2); |
| 113 | |
| 114 | std::vector<const ICLTensor *> weights_vector; |
| 115 | |
| 116 | weights_vector.emplace_back(input_to_forget_weights); |
| 117 | weights_vector.emplace_back(recurrent_to_forget_weights); |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 118 | const TensorShape weights_concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(weights_vector, 0); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 119 | _forget_gate_out6.allocator()->init(TensorInfo(weights_concat_shape, 1, input->info()->data_type())); |
| 120 | |
| 121 | _concat_weights_forget_gate.configure(input_to_forget_weights, recurrent_to_forget_weights, &_forget_gate_out6); |
| 122 | |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 123 | _memory_group.manage(&_forget_gate_out5); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 124 | _fully_connected_forget_gate.configure(&_forget_gate_out2, &_forget_gate_out6, forget_gate_bias, &_forget_gate_out5); |
| 125 | _memory_group.manage(&_forget_gate_out1); |
| 126 | _memory_group.manage(&_forget_gate_out3); |
| 127 | _forget_gate_out6.allocator()->allocate(); |
| 128 | |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 129 | CLTensor *forget_gate_out = &_forget_gate_out5; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 130 | if(lstm_params.has_peephole_opt()) |
| 131 | { |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 132 | _forget_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 133 | |
| 134 | _run_peephole_opt = true; |
| 135 | _memory_group.manage(&_forget_gate_out4); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 136 | _pixelwise_mul_forget_gate.configure(cell_state_in, lstm_params.cell_to_forget_weights(), &_forget_gate_out4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 137 | _accum_forget_gate2.configure(&_forget_gate_out5, &_forget_gate_out4, &_forget_gate_out3, ConvertPolicy::SATURATE); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 138 | _forget_gate_out4.allocator()->allocate(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 139 | _forget_gate_out5.allocator()->allocate(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 140 | forget_gate_out = &_forget_gate_out3; |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | _forget_gate_out3.allocator()->allocate(); |
| 145 | } |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 146 | _activation_forget_gate.configure(forget_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 147 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 148 | // Configure block that calculates the input gate |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 149 | // input_gate = Activation(input * input_to_input_weights + output_state * recurrent_to_input_weights + PixelWiseMul(cell_state, cell_to_input_weights) + input_gate_bias), without CIFG |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 150 | // input_gate = 1 - forget_gate, with CIFG |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 151 | // We optimize this as follows: |
| 152 | // input_gate = Activation((input,output_state) * (input_to_input_weights,recurrent_to_input_weights) + PixelWiseMul(cell_state, cell_to_input_weights) + input_gate_bias), without CIFG |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 153 | _input_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 154 | CLTensor *input_gate_out = &_input_gate_out1; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 155 | if(lstm_params.has_cifg_opt()) |
| 156 | { |
| 157 | _memory_group.manage(&_input_gate_out1); |
| 158 | _ones.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 159 | _ones_memset_kernel.configure(&_ones, PixelValue(1, _ones.info()->data_type())); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 160 | _subtract_input_gate.configure(ArithmeticOperation::SUB, &_ones, forget_gate_out, &_input_gate_out1, ConvertPolicy::SATURATE); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 161 | _ones.allocator()->allocate(); |
| 162 | _run_cifg_opt = true; |
| 163 | } |
| 164 | else |
| 165 | { |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 166 | _input_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 167 | _input_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 168 | |
| 169 | std::vector<const ICLTensor *> lstm_weights; |
| 170 | lstm_weights.emplace_back(lstm_params.input_to_input_weights()); |
| 171 | lstm_weights.emplace_back(lstm_params.recurrent_to_input_weights()); |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 172 | TensorShape lstm_weights_concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(lstm_weights, 0); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 173 | _input_gate_out2.allocator()->init(TensorInfo(lstm_weights_concat_shape, 1, input->info()->data_type())); |
| 174 | |
| 175 | _concat_weights_input_gate.configure(lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights(), &_input_gate_out2); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 176 | |
| 177 | _memory_group.manage(&_input_gate_out1); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 178 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 179 | _memory_group.manage(&_input_gate_out3); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 180 | _fully_connected_input_gate.configure(&_forget_gate_out2, &_input_gate_out2, lstm_params.input_gate_bias(), &_input_gate_out3); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 181 | _input_gate_out2.allocator()->allocate(); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 182 | |
| 183 | input_gate_out = &_input_gate_out3; |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 184 | if(_run_peephole_opt) |
| 185 | { |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 186 | _memory_group.manage(&_input_gate_out4); |
| 187 | _pixelwise_mul_input_gate.configure(cell_state_in, lstm_params.cell_to_input_weights(), &_input_gate_out4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); |
| 188 | _accum_input_gate2.configure(&_input_gate_out3, &_input_gate_out4, &_input_gate_out1, ConvertPolicy::SATURATE); |
| 189 | _input_gate_out3.allocator()->allocate(); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 190 | _input_gate_out4.allocator()->allocate(); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 191 | input_gate_out = &_input_gate_out1; |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 192 | } |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 193 | else |
| 194 | { |
| 195 | _input_gate_out1.allocator()->allocate(); |
| 196 | } |
| 197 | _activation_input_gate.configure(input_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 200 | // Configure block that calculates the cell state |
| 201 | // cell_state = Clip((PixelwiseMul(input_gate, Activation(input * input_to_cell_weights + output_state_in * recurrent_to_cell_weights + cell_bias)) + PixelwiseMul(forget_gate, cell_state)), cell_threshold) |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 202 | TensorShape cell_state1_shape = compute_transposed_shape(*recurrent_to_output_weights->info()); |
| 203 | _cell_state_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 204 | _cell_state_out2.allocator()->init(TensorInfo(cell_state1_shape, 1, input->info()->data_type())); |
| 205 | _cell_state_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 206 | _cell_state_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 207 | _cell_state_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 208 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 209 | _memory_group.manage(&_cell_state_out1); |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 210 | _fully_connected_cell_state.configure(input, input_to_cell_weights, cell_bias, &_cell_state_out1); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 211 | _memory_group.manage(&_cell_state_out2); |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 212 | _transpose_cell_state.configure(recurrent_to_cell_weights, &_cell_state_out2); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 213 | _memory_group.manage(&_cell_state_out3); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 214 | _gemm_cell_state1.configure(output_state_in, &_cell_state_out2, nullptr, &_cell_state_out3, 1.f, 0.f); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 215 | _cell_state_out2.allocator()->allocate(); |
| 216 | _memory_group.manage(&_cell_state_out4); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 217 | _accum_cell_state1.configure(ArithmeticOperation::ADD, &_cell_state_out1, &_cell_state_out3, &_cell_state_out4, ConvertPolicy::SATURATE); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 218 | _activation_cell_state.configure(&_cell_state_out4, nullptr, activation_info); |
| 219 | _memory_group.manage(&_cell_state_out5); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 220 | _pixelwise_mul_cell_state1.configure(&_cell_state_out4, input_gate_out, &_cell_state_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 221 | _cell_state_out4.allocator()->allocate(); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 222 | _pixelwise_mul_cell_state2.configure(forget_gate_out, cell_state_in, &_cell_state_out3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 223 | _accum_cell_state2.configure(ArithmeticOperation::ADD, &_cell_state_out5, &_cell_state_out3, &_cell_state_out1, ConvertPolicy::SATURATE); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 224 | _cell_state_out3.allocator()->allocate(); |
| 225 | _cell_state_out5.allocator()->allocate(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 226 | // Perform clipping |
| 227 | if(cell_threshold != 0.f) |
| 228 | { |
| 229 | _perform_cell_clipping = true; |
| 230 | _cell_clip.configure(&_cell_state_out1, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, cell_threshold)); |
| 231 | } |
| 232 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 233 | // Configure block that calculates the output |
| 234 | // output_state_out = Activation(input * input_to_output_weights + output_state_in * recurrent_to_output_weights + PixelWiseMul(cell_state, cell_to_output_weights) + output_gate_bias) |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 235 | // We optimize this as follows: |
| 236 | // output_state_out = Activation( (input,output_state_in) * (input_to_output_weights, recurrent_to_output_weights) + PixelWiseMul(cell_state, cell_to_output_weights) + output_gate_bias) |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 237 | _output1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 238 | _output4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 239 | std::vector<const ICLTensor *> in_out_weights; |
| 240 | in_out_weights.emplace_back(input_to_output_weights); |
| 241 | in_out_weights.emplace_back(recurrent_to_output_weights); |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 242 | TensorShape in_out_weights_concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(in_out_weights, 0); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 243 | _output2.allocator()->init(TensorInfo(in_out_weights_concat_shape, 1, input->info()->data_type())); |
| 244 | |
| 245 | _concat_weights_output.configure(input_to_output_weights, recurrent_to_output_weights, &_output2); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 246 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 247 | _memory_group.manage(&_output1); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 248 | _memory_group.manage(&_output4); |
| 249 | |
| 250 | _fully_connected_output.configure(&_forget_gate_out2, &_output2, output_gate_bias, &_output4); |
| 251 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 252 | _output2.allocator()->allocate(); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 253 | _forget_gate_out2.allocator()->allocate(); |
| 254 | |
| 255 | CLTensor *output_gate_out = &_output4; |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 256 | if(lstm_params.has_peephole_opt()) |
| 257 | { |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 258 | _output3.allocator()->init(TensorInfo(_cell_state_out1.info()->tensor_shape(), 1, input->info()->data_type())); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 259 | |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 260 | _memory_group.manage(&_output3); |
| 261 | _pixelwise_mul_output_state1.configure(&_cell_state_out1, lstm_params.cell_to_output_weights(), &_output3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); |
| 262 | _accum_output2.configure(&_output4, &_output3, &_output1, ConvertPolicy::SATURATE); |
| 263 | _output4.allocator()->allocate(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 264 | output_gate_out = &_output1; |
| 265 | |
| 266 | // Allocate intermediate buffers |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 267 | _output3.allocator()->allocate(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 268 | } |
| 269 | else |
| 270 | { |
| 271 | _output1.allocator()->allocate(); |
| 272 | } |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 273 | _activation_output.configure(output_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 274 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 275 | // Configure block that calculates the output state |
| 276 | /** lstm_res = PixelwiseMul(output, Activation(cell_state)) |
| 277 | * |
| 278 | * -- Clip(lstm_res * projection_weights + projection_bias, projection_threshold) , if there is a projection |
| 279 | * / |
| 280 | * output_state = -- |
| 281 | * \ |
| 282 | * -- lstm_res , otherwise |
| 283 | */ |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 284 | ICLTensor *output_state_out_tmp = lstm_params.has_projection() ? &_output_state1 : output_state_out; |
| 285 | _cell_state_activation.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 286 | _output_state1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 287 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 288 | _memory_group.manage(&_cell_state_activation); |
| 289 | _activation_output_state.configure(&_cell_state_out1, &_cell_state_activation, activation_info); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 290 | _pixelwise_mul_output_state2.configure(&_cell_state_activation, output_gate_out, output_state_out_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 291 | _cell_state_activation.allocator()->allocate(); |
| 292 | |
| 293 | if(lstm_params.has_projection()) |
| 294 | { |
| 295 | _has_projection_weights = true; |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 296 | _fully_connected_output_state.configure(output_state_out_tmp, lstm_params.projection_weights(), lstm_params.projection_bias(), output_state_out); |
| 297 | _output_state1.allocator()->allocate(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 298 | // Perform clipping |
| 299 | if(projection_threshold != 0.f) |
| 300 | { |
| 301 | _perform_projection_clipping = true; |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 302 | _projection_clip.configure(output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 303 | } |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | // Copy cell state and output |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 307 | _copy_cell_state.configure(&_cell_state_out1, cell_state_out); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 308 | _copy_output.configure(output_state_out, output); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 309 | |
| 310 | // Vector for holding the tensors to store in scratch buffer |
| 311 | std::vector<ICLTensor *> scratch_inputs; |
Georgios Pinitas | 0cc37c3 | 2018-11-14 15:54:26 +0000 | [diff] [blame] | 312 | if(!lstm_params.has_cifg_opt()) |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 313 | { |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 314 | scratch_inputs.emplace_back(input_gate_out); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 315 | } |
| 316 | scratch_inputs.emplace_back(&_cell_state_out1); |
| 317 | scratch_inputs.emplace_back(forget_gate_out); |
| 318 | scratch_inputs.emplace_back(output_gate_out); |
| 319 | _concat_scratch_buffer.configure(scratch_inputs, scratch_buffer); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 320 | input_gate_out->allocator()->allocate(); |
Michele Di Giorgio | dd2619a | 2018-11-05 16:46:09 +0000 | [diff] [blame] | 321 | _cell_state_out1.allocator()->allocate(); |
| 322 | forget_gate_out->allocator()->allocate(); |
| 323 | output_gate_out->allocator()->allocate(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 326 | Status CLLSTMLayer::validate(const ITensorInfo *input, |
| 327 | const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights, |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 328 | const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights, |
| 329 | const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias, |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 330 | const ITensorInfo *output_state_in, const ITensorInfo *cell_state_in, |
| 331 | const ITensorInfo *scratch_buffer, const ITensorInfo *output_state_out, const ITensorInfo *cell_state_out, const ITensorInfo *output, |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 332 | const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold) |
| 333 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 334 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, |
| 335 | input_to_forget_weights, input_to_cell_weights, input_to_output_weights, |
| 336 | recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights, |
| 337 | forget_gate_bias, cell_bias, output_gate_bias, |
| 338 | output_state_in, cell_state_in, |
| 339 | scratch_buffer, output_state_out, cell_state_out, output); |
| 340 | |
| 341 | // Check data types |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 342 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 343 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, |
| 344 | input_to_forget_weights, input_to_cell_weights, input_to_output_weights, |
| 345 | recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights, |
| 346 | forget_gate_bias, cell_bias, output_gate_bias, |
| 347 | output_state_in, cell_state_in, |
| 348 | scratch_buffer, output_state_out, cell_state_out, output); |
| 349 | |
| 350 | // Check dimensions |
Georgios Pinitas | 42447c1 | 2018-07-16 17:01:20 +0100 | [diff] [blame] | 351 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2); |
| 352 | ARM_COMPUTE_RETURN_ERROR_ON(input_to_forget_weights->num_dimensions() > 2); |
| 353 | ARM_COMPUTE_RETURN_ERROR_ON(input_to_cell_weights->num_dimensions() > 2); |
| 354 | ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->num_dimensions() > 2); |
| 355 | ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_forget_weights->num_dimensions() > 2); |
| 356 | ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_cell_weights->num_dimensions() > 2); |
| 357 | ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->num_dimensions() > 2); |
| 358 | ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->num_dimensions() > 1); |
| 359 | ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->num_dimensions() > 1); |
| 360 | ARM_COMPUTE_RETURN_ERROR_ON(output_gate_bias->num_dimensions() > 1); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 361 | ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() > 2); |
| 362 | ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->num_dimensions() > 2); |
Georgios Pinitas | 42447c1 | 2018-07-16 17:01:20 +0100 | [diff] [blame] | 363 | ARM_COMPUTE_RETURN_ERROR_ON(scratch_buffer->num_dimensions() > 2); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 364 | ARM_COMPUTE_RETURN_ERROR_ON(output_state_out->num_dimensions() > 2); |
| 365 | ARM_COMPUTE_RETURN_ERROR_ON(cell_state_out->num_dimensions() > 2); |
Georgios Pinitas | 42447c1 | 2018-07-16 17:01:20 +0100 | [diff] [blame] | 366 | ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 2); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 367 | ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->dimension(0) * 4 != scratch_buffer->dimension(0) |
| 368 | && cell_bias->dimension(0) * 3 != scratch_buffer->dimension(0)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 369 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 370 | const unsigned int num_batches = input->dimension(1); |
| 371 | const unsigned int num_cells = input_to_output_weights->dimension(1); |
| 372 | |
| 373 | // Check peephole optimization |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 374 | if(lstm_params.has_peephole_opt()) |
| 375 | { |
Michalis Spyrou | 09daf4d | 2018-06-28 17:07:22 +0100 | [diff] [blame] | 376 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_output_weights(), lstm_params.cell_to_forget_weights()); |
Georgios Pinitas | 42447c1 | 2018-07-16 17:01:20 +0100 | [diff] [blame] | 377 | ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->num_dimensions() > 1); |
| 378 | ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_output_weights()->num_dimensions() > 1); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | TensorShape units_out_transposed_shape = compute_transposed_shape(*recurrent_to_output_weights); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 382 | TensorShape num_units_transposed_shape = compute_transposed_shape(*forget_gate_bias); |
| 383 | const TensorInfo units_out_transposed_info = TensorInfo(units_out_transposed_shape, 1, input->data_type()); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 384 | const TensorInfo num_units_transposed_info = TensorInfo(num_units_transposed_shape, 1, input->data_type()); |
| 385 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 386 | TensorInfo input_gate = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type()); |
| 387 | TensorInfo forget_gate = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type()); |
| 388 | TensorInfo output_gate_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type()); |
| 389 | TensorInfo cell_state_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type()); |
| 390 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 391 | // Validate forget gate |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 392 | ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_forget_weights, forget_gate_bias, &forget_gate)); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 393 | |
| 394 | std::vector<const ITensorInfo *> inputs_vector; |
| 395 | inputs_vector.emplace_back(input); |
| 396 | inputs_vector.emplace_back(output_state_in); |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 397 | const TensorShape concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, 0); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 398 | TensorInfo forget_gate_concat = TensorInfo(concat_shape, 1, input->data_type()); |
| 399 | |
| 400 | ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenate2TensorsKernel::validate(input, output_state_in, &forget_gate_concat)); |
| 401 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 402 | if(lstm_params.has_peephole_opt()) |
| 403 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 404 | ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_forget_weights(), &forget_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN)); |
| 405 | ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 406 | } |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 407 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&forget_gate, &forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 408 | |
| 409 | // Validate input gate |
| 410 | if(!lstm_params.has_cifg_opt()) |
| 411 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 412 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(), |
| 413 | lstm_params.recurrent_to_input_weights(), |
| 414 | lstm_params.input_gate_bias()); |
Georgios Pinitas | 42447c1 | 2018-07-16 17:01:20 +0100 | [diff] [blame] | 415 | ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_to_input_weights()->num_dimensions() > 2); |
| 416 | ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.recurrent_to_input_weights()->num_dimensions() > 2); |
Georgios Pinitas | 42447c1 | 2018-07-16 17:01:20 +0100 | [diff] [blame] | 417 | ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_gate_bias()->num_dimensions() > 1); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 418 | |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 419 | std::vector<const ITensorInfo *> lstm_weights; |
| 420 | lstm_weights.emplace_back(lstm_params.input_to_input_weights()); |
| 421 | lstm_weights.emplace_back(lstm_params.recurrent_to_input_weights()); |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 422 | TensorShape lstm_weights_concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(lstm_weights, 0); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 423 | TensorInfo lstm_gate_concat = TensorInfo(lstm_weights_concat_shape, 1, input->data_type()); |
| 424 | ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenate2TensorsKernel::validate(lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights(), &lstm_gate_concat)); |
| 425 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 426 | ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, lstm_params.input_to_input_weights(), lstm_params.input_gate_bias(), &input_gate)); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 427 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 428 | if(lstm_params.has_peephole_opt()) |
| 429 | { |
| 430 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_input_weights()); |
| 431 | ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_input_weights()->num_dimensions() > 1); |
| 432 | ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_input_weights(), &input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN)); |
| 433 | ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&input_gate, &input_gate, &input_gate, ConvertPolicy::SATURATE)); |
| 434 | } |
| 435 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&input_gate, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 436 | } |
| 437 | else |
| 438 | { |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 439 | ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::SUB, &forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | // Validate cell state |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 443 | ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_cell_weights, cell_bias, &cell_state_tmp)); |
| 444 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(output_state_in, &units_out_transposed_info, nullptr, &cell_state_tmp, 1.f, 0.f, GEMMInfo())); |
| 445 | ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE)); |
| 446 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&cell_state_tmp, nullptr, activation_info)); |
| 447 | ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &input_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN)); |
| 448 | ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &forget_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN)); |
| 449 | ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 450 | if(cell_threshold != 0.f) |
| 451 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 452 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&cell_state_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, |
| 453 | cell_threshold))); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 454 | } |
| 455 | |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 456 | std::vector<const ITensorInfo *> in_out_weights; |
| 457 | in_out_weights.emplace_back(input_to_output_weights); |
| 458 | in_out_weights.emplace_back(recurrent_to_output_weights); |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 459 | TensorShape in_out_weights_concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(in_out_weights, 0); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 460 | TensorInfo in_out_gate_concat = TensorInfo(in_out_weights_concat_shape, 1, input->data_type()); |
| 461 | ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenate2TensorsKernel::validate(input_to_output_weights, recurrent_to_output_weights, &in_out_gate_concat)); |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 462 | // Validate output gate tmp |
| 463 | ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_output_weights, output_gate_bias, &output_gate_tmp)); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 464 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 465 | if(lstm_params.has_peephole_opt()) |
| 466 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 467 | ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&cell_state_tmp, lstm_params.cell_to_output_weights(), &output_gate_tmp, 1, ConvertPolicy::SATURATE, |
| 468 | RoundingPolicy::TO_NEAREST_EVEN)); |
| 469 | ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&output_gate_tmp, &output_gate_tmp, &output_gate_tmp, ConvertPolicy::SATURATE)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 470 | } |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 471 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&output_gate_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 472 | |
| 473 | // Validate output state |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 474 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&cell_state_tmp, &cell_state_tmp, activation_info)); |
| 475 | ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &output_gate_tmp, &output_gate_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 476 | if(lstm_params.has_projection()) |
| 477 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 478 | ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(&output_gate_tmp, lstm_params.projection_weights(), lstm_params.projection_bias(), output_state_out)); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 479 | if(projection_threshold != 0.f) |
| 480 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 481 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(output_state_out, output_state_out, |
| 482 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold))); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 486 | // Validate copy kernel |
| 487 | ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(&cell_state_tmp, cell_state_out)); |
| 488 | ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(output_state_out, output)); |
| 489 | |
| 490 | // Validate scratch concatenation |
| 491 | std::vector<ITensorInfo *> inputs_vector_info_raw; |
Georgios Pinitas | 0cc37c3 | 2018-11-14 15:54:26 +0000 | [diff] [blame] | 492 | if(!lstm_params.has_cifg_opt()) |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 493 | { |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 494 | inputs_vector_info_raw.push_back(&input_gate); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 495 | } |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 496 | inputs_vector_info_raw.push_back(&cell_state_tmp); |
| 497 | inputs_vector_info_raw.push_back(&forget_gate); |
| 498 | inputs_vector_info_raw.push_back(&output_gate_tmp); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 499 | |
| 500 | ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenateLayer::validate(inputs_vector_info_raw, scratch_buffer)); |
| 501 | return Status{}; |
| 502 | } |
| 503 | |
| 504 | void CLLSTMLayer::run() |
| 505 | { |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 506 | prepare(); |
| 507 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 508 | MemoryGroupResourceScope scope_mg(_memory_group); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 509 | |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 510 | CLScheduler::get().enqueue(_concat_inputs_forget_gate); |
| 511 | |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 512 | _fully_connected_forget_gate.run(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 513 | |
| 514 | if(_run_peephole_opt) |
| 515 | { |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 516 | CLScheduler::get().enqueue(_pixelwise_mul_forget_gate); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 517 | _accum_forget_gate2.run(); |
| 518 | } |
| 519 | CLScheduler::get().enqueue(_activation_forget_gate); |
| 520 | |
| 521 | if(_run_cifg_opt) |
| 522 | { |
Georgios Pinitas | dbfc2dc | 2019-04-02 12:51:21 +0100 | [diff] [blame] | 523 | CLScheduler::get().enqueue(_ones_memset_kernel); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 524 | CLScheduler::get().enqueue(_subtract_input_gate); |
| 525 | } |
| 526 | else |
| 527 | { |
| 528 | _fully_connected_input_gate.run(); |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 529 | |
Georgios Pinitas | 8bc745d | 2018-07-18 19:51:24 +0100 | [diff] [blame] | 530 | if(_run_peephole_opt) |
| 531 | { |
| 532 | CLScheduler::get().enqueue(_pixelwise_mul_input_gate); |
| 533 | _accum_input_gate2.run(); |
| 534 | } |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 535 | CLScheduler::get().enqueue(_activation_input_gate); |
| 536 | } |
| 537 | |
| 538 | _fully_connected_cell_state.run(); |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 539 | CLScheduler::get().enqueue(_transpose_cell_state); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 540 | _gemm_cell_state1.run(); |
| 541 | CLScheduler::get().enqueue(_accum_cell_state1); |
| 542 | CLScheduler::get().enqueue(_activation_cell_state); |
| 543 | CLScheduler::get().enqueue(_pixelwise_mul_cell_state1); |
| 544 | CLScheduler::get().enqueue(_pixelwise_mul_cell_state2); |
| 545 | CLScheduler::get().enqueue(_accum_cell_state2); |
| 546 | |
| 547 | if(_perform_cell_clipping) |
| 548 | { |
| 549 | CLScheduler::get().enqueue(_cell_clip); |
| 550 | } |
| 551 | |
| 552 | _fully_connected_output.run(); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 553 | |
| 554 | if(_run_peephole_opt) |
| 555 | { |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 556 | CLScheduler::get().enqueue(_pixelwise_mul_output_state1); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 557 | _accum_output2.run(); |
| 558 | } |
| 559 | CLScheduler::get().enqueue(_activation_output); |
| 560 | |
| 561 | CLScheduler::get().enqueue(_activation_output_state); |
Georgios Pinitas | 42a3172 | 2018-07-09 14:35:32 +0100 | [diff] [blame] | 562 | CLScheduler::get().enqueue(_pixelwise_mul_output_state2); |
Michalis Spyrou | bcedf51 | 2018-03-22 14:55:08 +0000 | [diff] [blame] | 563 | |
| 564 | if(_has_projection_weights) |
| 565 | { |
| 566 | _fully_connected_output_state.run(); |
| 567 | if(_perform_projection_clipping) |
| 568 | { |
| 569 | CLScheduler::get().enqueue(_projection_clip); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | CLScheduler::get().enqueue(_copy_cell_state); |
| 574 | CLScheduler::get().enqueue(_copy_output); |
| 575 | |
| 576 | _concat_scratch_buffer.run(); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 577 | } |
John Kesapides | cafec8f | 2019-02-19 15:53:59 +0000 | [diff] [blame] | 578 | |
| 579 | void CLLSTMLayer::prepare() |
| 580 | { |
| 581 | if(!_is_prepared) |
| 582 | { |
| 583 | CLScheduler::get().enqueue(_concat_weights_forget_gate); |
| 584 | if(!_run_cifg_opt) |
| 585 | { |
| 586 | CLScheduler::get().enqueue(_concat_weights_input_gate); |
| 587 | } |
| 588 | CLScheduler::get().enqueue(_concat_weights_output); |
| 589 | _is_prepared = true; |
| 590 | } |
| 591 | } |