blob: f01b1b898f1abaaf4b25b4a9437b7a29777c1491 [file] [log] [blame]
Michalis Spyroubcedf512018-03-22 14:55:08 +00001/*
Georgios Pinitas4f859822019-02-06 18:08:04 +00002 * Copyright (c) 2018-2019 ARM Limited.
Michalis Spyroubcedf512018-03-22 14:55:08 +00003 *
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
37using namespace arm_compute;
38using namespace arm_compute::misc::shape_calculator;
39
40CLLSTMLayer::CLLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager)
Georgios Pinitas42a31722018-07-09 14:35:32 +010041 : _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(),
46 _projection_clip(), _copy_cell_state(), _copy_output(), _concat_scratch_buffer(), _input_gate_out1(), _input_gate_out2(), _input_gate_out3(), _input_gate_out4(), _input_gate_out5(),
47 _forget_gate_out1(), _forget_gate_out2(), _forget_gate_out3(), _forget_gate_out4(), _forget_gate_out5(), _cell_state_out1(), _cell_state_out2(), _cell_state_out3(), _cell_state_out4(),
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010048 _cell_state_out5(), _output1(), _output2(), _output3(), _output4(), _output5(), _cell_state_activation(), _output_state1(), _ones(), _run_peephole_opt(false), _run_cifg_opt(false),
Georgios Pinitas42a31722018-07-09 14:35:32 +010049 _perform_cell_clipping(false), _has_projection_weights(false), _perform_projection_clipping(false)
Michalis Spyroubcedf512018-03-22 14:55:08 +000050{
51}
52
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010053void CLLSTMLayer::configure(const ICLTensor *input,
54 const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
Michalis Spyroubcedf512018-03-22 14:55:08 +000055 const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
56 const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010057 const ICLTensor *output_state_in, const ICLTensor *cell_state_in,
58 ICLTensor *scratch_buffer, ICLTensor *output_state_out, ICLTensor *cell_state_out, ICLTensor *output,
59 const LSTMParams<ICLTensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold)
Michalis Spyroubcedf512018-03-22 14:55:08 +000060{
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010061 ARM_COMPUTE_ERROR_ON_NULLPTR(input,
62 input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
63 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
64 forget_gate_bias, cell_bias, output_gate_bias,
65 output_state_in, cell_state_in,
66 scratch_buffer, output_state_out, cell_state_out, output);
67
68 // Set lstm parameters
Michalis Spyroubcedf512018-03-22 14:55:08 +000069 LSTMParams<ITensorInfo> lstm_params_info;
70 if(lstm_params.has_peephole_opt())
71 {
Michalis Spyrou09daf4d2018-06-28 17:07:22 +010072 lstm_params_info.set_peephole_params(lstm_params.cell_to_forget_weights()->info(), lstm_params.cell_to_output_weights()->info());
Michalis Spyroubcedf512018-03-22 14:55:08 +000073 }
74 if(lstm_params.has_projection())
75 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010076 lstm_params_info.set_projection_params(lstm_params.projection_weights()->info(),
77 lstm_params.projection_bias() != nullptr ? lstm_params.projection_bias()->info() : nullptr);
Michalis Spyroubcedf512018-03-22 14:55:08 +000078 }
79 if(!lstm_params.has_cifg_opt())
80 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010081 const ITensorInfo *cell_to_input_weights_info = (lstm_params.has_peephole_opt()) ? lstm_params.cell_to_input_weights()->info() : nullptr;
Michalis Spyroubcedf512018-03-22 14:55:08 +000082 lstm_params_info.set_cifg_params(lstm_params.input_to_input_weights()->info(), lstm_params.recurrent_to_input_weights()->info(),
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010083 cell_to_input_weights_info, lstm_params.input_gate_bias()->info());
Michalis Spyroubcedf512018-03-22 14:55:08 +000084 }
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010085
86 // Validate
Michalis Spyroubcedf512018-03-22 14:55:08 +000087 ARM_COMPUTE_ERROR_THROW_ON(CLLSTMLayer::validate(input->info(), input_to_forget_weights->info(),
88 input_to_cell_weights->info(), input_to_output_weights->info(),
89 recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(),
90 forget_gate_bias->info(), cell_bias->info(), output_gate_bias->info(),
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010091 output_state_in->info(), cell_state_in->info(),
92 scratch_buffer->info(), output_state_out->info(), cell_state_out->info(), output->info(),
93 lstm_params_info, activation_info, cell_threshold, projection_threshold));
Michalis Spyroubcedf512018-03-22 14:55:08 +000094
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010095 const TensorShape cell_state_shape = cell_state_in->info()->tensor_shape();
Michalis Spyroubcedf512018-03-22 14:55:08 +000096
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010097 // 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)
Michalis Spyroubcedf512018-03-22 14:55:08 +000099 TensorShape forget_gate1_shape = compute_transposed_shape(*recurrent_to_output_weights->info());
Michalis Spyroubcedf512018-03-22 14:55:08 +0000100 _forget_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
101 _forget_gate_out2.allocator()->init(TensorInfo(forget_gate1_shape, 1, input->info()->data_type()));
102 _forget_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas42a31722018-07-09 14:35:32 +0100103 _forget_gate_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000104
Michalis Spyroubcedf512018-03-22 14:55:08 +0000105 _memory_group.manage(&_forget_gate_out1);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100106 _fully_connected_forget_gate.configure(input, input_to_forget_weights, forget_gate_bias, &_forget_gate_out1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000107 _memory_group.manage(&_forget_gate_out2);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100108 _transpose_forget_gate.configure(recurrent_to_forget_weights, &_forget_gate_out2);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000109 _memory_group.manage(&_forget_gate_out3);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100110 _gemm_forget_gate.configure(output_state_in, &_forget_gate_out2, nullptr, &_forget_gate_out3, 1.f, 0.f);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000111 _forget_gate_out2.allocator()->allocate();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100112 _memory_group.manage(&_forget_gate_out5);
giuros01164a2722018-11-20 18:34:46 +0000113 _accum_forget_gate1.configure(ArithmeticOperation::ADD, &_forget_gate_out1, &_forget_gate_out3, &_forget_gate_out5, ConvertPolicy::SATURATE);
Georgios Pinitas4f859822019-02-06 18:08:04 +0000114 _forget_gate_out1.allocator()->allocate();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100115 CLTensor *forget_gate_out = &_forget_gate_out5;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000116 if(lstm_params.has_peephole_opt())
117 {
Georgios Pinitas42a31722018-07-09 14:35:32 +0100118 _forget_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000119
120 _run_peephole_opt = true;
121 _memory_group.manage(&_forget_gate_out4);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100122 _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 Pinitas42a31722018-07-09 14:35:32 +0100123 _accum_forget_gate2.configure(&_forget_gate_out5, &_forget_gate_out4, &_forget_gate_out3, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000124 _forget_gate_out4.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000125 _forget_gate_out5.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000126 forget_gate_out = &_forget_gate_out3;
127 }
128 else
129 {
130 _forget_gate_out3.allocator()->allocate();
131 }
Georgios Pinitas4f859822019-02-06 18:08:04 +0000132 _activation_forget_gate.configure(forget_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000133
Michalis Spyroubcedf512018-03-22 14:55:08 +0000134 // Configure block that calculates the input gate
Georgios Pinitas42a31722018-07-09 14:35:32 +0100135 // 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 Spyroubcedf512018-03-22 14:55:08 +0000136 // input_gate = 1 - forget_gate, with CIFG
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100137 _input_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas4f859822019-02-06 18:08:04 +0000138 CLTensor *input_gate_out = &_input_gate_out1;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000139 if(lstm_params.has_cifg_opt())
140 {
141 _memory_group.manage(&_input_gate_out1);
142 _ones.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas4f859822019-02-06 18:08:04 +0000143 _subtract_input_gate.configure(ArithmeticOperation::SUB, &_ones, forget_gate_out, &_input_gate_out1, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000144 _ones.allocator()->allocate();
145 _run_cifg_opt = true;
146 }
147 else
148 {
Georgios Pinitas42a31722018-07-09 14:35:32 +0100149 TensorShape input_gate_shape = compute_transposed_shape(*recurrent_to_output_weights->info());
Michalis Spyroubcedf512018-03-22 14:55:08 +0000150
Georgios Pinitas42a31722018-07-09 14:35:32 +0100151 _input_gate_out2.allocator()->init(TensorInfo(input_gate_shape, 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000152 _input_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas42a31722018-07-09 14:35:32 +0100153 _input_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
154 _input_gate_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000155
156 _memory_group.manage(&_input_gate_out1);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100157 _fully_connected_input_gate.configure(input, lstm_params.input_to_input_weights(), lstm_params.input_gate_bias(), &_input_gate_out1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000158 _memory_group.manage(&_input_gate_out2);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100159 _transpose_input_gate.configure(lstm_params.recurrent_to_input_weights(), &_input_gate_out2);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000160 _memory_group.manage(&_input_gate_out3);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100161 _gemm_input_gate.configure(output_state_in, &_input_gate_out2, nullptr, &_input_gate_out3, 1.f, 0.f);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000162 _input_gate_out2.allocator()->allocate();
163 _memory_group.manage(&_input_gate_out4);
giuros01164a2722018-11-20 18:34:46 +0000164 _accum_input_gate1.configure(ArithmeticOperation::ADD, &_input_gate_out1, &_input_gate_out3, &_input_gate_out4, ConvertPolicy::SATURATE);
Georgios Pinitas4f859822019-02-06 18:08:04 +0000165 _input_gate_out3.allocator()->allocate();
166 input_gate_out = &_input_gate_out4;
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100167 if(_run_peephole_opt)
168 {
169 _memory_group.manage(&_input_gate_out5);
170 _pixelwise_mul_input_gate.configure(cell_state_in, lstm_params.cell_to_input_weights(), &_input_gate_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
171 _accum_input_gate2.configure(&_input_gate_out4, &_input_gate_out5, &_input_gate_out1, ConvertPolicy::SATURATE);
Georgios Pinitas4f859822019-02-06 18:08:04 +0000172 _input_gate_out4.allocator()->allocate();
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100173 _input_gate_out5.allocator()->allocate();
Georgios Pinitas4f859822019-02-06 18:08:04 +0000174 input_gate_out = &_input_gate_out1;
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100175 }
Georgios Pinitas4f859822019-02-06 18:08:04 +0000176 else
177 {
178 _input_gate_out1.allocator()->allocate();
179 }
180 _activation_input_gate.configure(input_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000181 }
182
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100183 // Configure block that calculates the cell state
184 // 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 Spyroubcedf512018-03-22 14:55:08 +0000185 TensorShape cell_state1_shape = compute_transposed_shape(*recurrent_to_output_weights->info());
186 _cell_state_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
187 _cell_state_out2.allocator()->init(TensorInfo(cell_state1_shape, 1, input->info()->data_type()));
188 _cell_state_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
189 _cell_state_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
190 _cell_state_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
191
Michalis Spyroubcedf512018-03-22 14:55:08 +0000192 _memory_group.manage(&_cell_state_out1);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100193 _fully_connected_cell_state.configure(input, input_to_cell_weights, cell_bias, &_cell_state_out1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000194 _memory_group.manage(&_cell_state_out2);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100195 _transpose_cell_state.configure(recurrent_to_cell_weights, &_cell_state_out2);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000196 _memory_group.manage(&_cell_state_out3);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100197 _gemm_cell_state1.configure(output_state_in, &_cell_state_out2, nullptr, &_cell_state_out3, 1.f, 0.f);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000198 _cell_state_out2.allocator()->allocate();
199 _memory_group.manage(&_cell_state_out4);
giuros01164a2722018-11-20 18:34:46 +0000200 _accum_cell_state1.configure(ArithmeticOperation::ADD, &_cell_state_out1, &_cell_state_out3, &_cell_state_out4, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000201 _activation_cell_state.configure(&_cell_state_out4, nullptr, activation_info);
202 _memory_group.manage(&_cell_state_out5);
Georgios Pinitas4f859822019-02-06 18:08:04 +0000203 _pixelwise_mul_cell_state1.configure(&_cell_state_out4, input_gate_out, &_cell_state_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000204 _cell_state_out4.allocator()->allocate();
Georgios Pinitas4f859822019-02-06 18:08:04 +0000205 _pixelwise_mul_cell_state2.configure(forget_gate_out, cell_state_in, &_cell_state_out3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
giuros01164a2722018-11-20 18:34:46 +0000206 _accum_cell_state2.configure(ArithmeticOperation::ADD, &_cell_state_out5, &_cell_state_out3, &_cell_state_out1, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000207 _cell_state_out3.allocator()->allocate();
208 _cell_state_out5.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000209 // Perform clipping
210 if(cell_threshold != 0.f)
211 {
212 _perform_cell_clipping = true;
213 _cell_clip.configure(&_cell_state_out1, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, cell_threshold));
214 }
215
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100216 // Configure block that calculates the output
217 // 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)
Michalis Spyroubcedf512018-03-22 14:55:08 +0000218 TensorShape output1_shape = compute_transposed_shape(*recurrent_to_output_weights->info());
Michalis Spyroubcedf512018-03-22 14:55:08 +0000219 _output1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
220 _output2.allocator()->init(TensorInfo(output1_shape, 1, input->info()->data_type()));
221 _output3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas42a31722018-07-09 14:35:32 +0100222 _output5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000223
Michalis Spyroubcedf512018-03-22 14:55:08 +0000224 _memory_group.manage(&_output1);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100225 _fully_connected_output.configure(input, input_to_output_weights, output_gate_bias, &_output1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000226 _memory_group.manage(&_output2);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100227 _transpose_output.configure(recurrent_to_output_weights, &_output2);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000228 _memory_group.manage(&_output3);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100229 _gemm_output.configure(output_state_in, &_output2, nullptr, &_output3, 1.f, 0.f);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000230 _output2.allocator()->allocate();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100231 _memory_group.manage(&_output5);
giuros01164a2722018-11-20 18:34:46 +0000232 _accum_output1.configure(ArithmeticOperation::ADD, &_output1, &_output3, &_output5, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000233 _output3.allocator()->allocate();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100234 CLTensor *output_gate_out = &_output5;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000235 if(lstm_params.has_peephole_opt())
236 {
Georgios Pinitas42a31722018-07-09 14:35:32 +0100237 _output4.allocator()->init(TensorInfo(_cell_state_out1.info()->tensor_shape(), 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000238
239 _memory_group.manage(&_output4);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100240 _pixelwise_mul_output_state1.configure(&_cell_state_out1, lstm_params.cell_to_output_weights(), &_output4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
241 _accum_output2.configure(&_output5, &_output4, &_output1, ConvertPolicy::SATURATE);
242 _output5.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000243 output_gate_out = &_output1;
244
245 // Allocate intermediate buffers
246 _output4.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000247 }
248 else
249 {
250 _output1.allocator()->allocate();
251 }
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100252 _activation_output.configure(output_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000253
Michalis Spyroubcedf512018-03-22 14:55:08 +0000254 // Configure block that calculates the output state
255 /** lstm_res = PixelwiseMul(output, Activation(cell_state))
256 *
257 * -- Clip(lstm_res * projection_weights + projection_bias, projection_threshold) , if there is a projection
258 * /
259 * output_state = --
260 * \
261 * -- lstm_res , otherwise
262 */
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100263 ICLTensor *output_state_out_tmp = lstm_params.has_projection() ? &_output_state1 : output_state_out;
264 _cell_state_activation.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
265 _output_state1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
266
Michalis Spyroubcedf512018-03-22 14:55:08 +0000267 _memory_group.manage(&_cell_state_activation);
268 _activation_output_state.configure(&_cell_state_out1, &_cell_state_activation, activation_info);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100269 _pixelwise_mul_output_state2.configure(&_cell_state_activation, output_gate_out, output_state_out_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000270 _cell_state_activation.allocator()->allocate();
271
272 if(lstm_params.has_projection())
273 {
274 _has_projection_weights = true;
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100275 _fully_connected_output_state.configure(output_state_out_tmp, lstm_params.projection_weights(), lstm_params.projection_bias(), output_state_out);
276 _output_state1.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000277 // Perform clipping
278 if(projection_threshold != 0.f)
279 {
280 _perform_projection_clipping = true;
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100281 _projection_clip.configure(output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000282 }
Michalis Spyroubcedf512018-03-22 14:55:08 +0000283 }
284
285 // Copy cell state and output
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100286 _copy_cell_state.configure(&_cell_state_out1, cell_state_out);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100287 _copy_output.configure(output_state_out, output);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000288
289 // Vector for holding the tensors to store in scratch buffer
290 std::vector<ICLTensor *> scratch_inputs;
Georgios Pinitas0cc37c32018-11-14 15:54:26 +0000291 if(!lstm_params.has_cifg_opt())
Michalis Spyroubcedf512018-03-22 14:55:08 +0000292 {
Georgios Pinitas4f859822019-02-06 18:08:04 +0000293 scratch_inputs.emplace_back(input_gate_out);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000294 }
295 scratch_inputs.emplace_back(&_cell_state_out1);
296 scratch_inputs.emplace_back(forget_gate_out);
297 scratch_inputs.emplace_back(output_gate_out);
298 _concat_scratch_buffer.configure(scratch_inputs, scratch_buffer);
Georgios Pinitas4f859822019-02-06 18:08:04 +0000299 input_gate_out->allocator()->allocate();
Michele Di Giorgiodd2619a2018-11-05 16:46:09 +0000300 _cell_state_out1.allocator()->allocate();
301 forget_gate_out->allocator()->allocate();
302 output_gate_out->allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000303}
304
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100305Status CLLSTMLayer::validate(const ITensorInfo *input,
306 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
Michalis Spyroubcedf512018-03-22 14:55:08 +0000307 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
308 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100309 const ITensorInfo *output_state_in, const ITensorInfo *cell_state_in,
310 const ITensorInfo *scratch_buffer, const ITensorInfo *output_state_out, const ITensorInfo *cell_state_out, const ITensorInfo *output,
Michalis Spyroubcedf512018-03-22 14:55:08 +0000311 const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold)
312{
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100313 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input,
314 input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
315 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
316 forget_gate_bias, cell_bias, output_gate_bias,
317 output_state_in, cell_state_in,
318 scratch_buffer, output_state_out, cell_state_out, output);
319
320 // Check data types
Michalis Spyroubcedf512018-03-22 14:55:08 +0000321 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100322 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input,
323 input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
324 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
325 forget_gate_bias, cell_bias, output_gate_bias,
326 output_state_in, cell_state_in,
327 scratch_buffer, output_state_out, cell_state_out, output);
328
329 // Check dimensions
Georgios Pinitas42447c12018-07-16 17:01:20 +0100330 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2);
331 ARM_COMPUTE_RETURN_ERROR_ON(input_to_forget_weights->num_dimensions() > 2);
332 ARM_COMPUTE_RETURN_ERROR_ON(input_to_cell_weights->num_dimensions() > 2);
333 ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->num_dimensions() > 2);
334 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_forget_weights->num_dimensions() > 2);
335 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_cell_weights->num_dimensions() > 2);
336 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->num_dimensions() > 2);
337 ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->num_dimensions() > 1);
338 ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->num_dimensions() > 1);
339 ARM_COMPUTE_RETURN_ERROR_ON(output_gate_bias->num_dimensions() > 1);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100340 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() > 2);
341 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->num_dimensions() > 2);
Georgios Pinitas42447c12018-07-16 17:01:20 +0100342 ARM_COMPUTE_RETURN_ERROR_ON(scratch_buffer->num_dimensions() > 2);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100343 ARM_COMPUTE_RETURN_ERROR_ON(output_state_out->num_dimensions() > 2);
344 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_out->num_dimensions() > 2);
Georgios Pinitas42447c12018-07-16 17:01:20 +0100345 ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 2);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100346 ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->dimension(0) * 4 != scratch_buffer->dimension(0)
347 && cell_bias->dimension(0) * 3 != scratch_buffer->dimension(0));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000348
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100349 const unsigned int num_batches = input->dimension(1);
350 const unsigned int num_cells = input_to_output_weights->dimension(1);
351
352 // Check peephole optimization
Michalis Spyroubcedf512018-03-22 14:55:08 +0000353 if(lstm_params.has_peephole_opt())
354 {
Michalis Spyrou09daf4d2018-06-28 17:07:22 +0100355 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_output_weights(), lstm_params.cell_to_forget_weights());
Georgios Pinitas42447c12018-07-16 17:01:20 +0100356 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->num_dimensions() > 1);
357 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_output_weights()->num_dimensions() > 1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000358 }
359
360 TensorShape units_out_transposed_shape = compute_transposed_shape(*recurrent_to_output_weights);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000361 TensorShape num_units_transposed_shape = compute_transposed_shape(*forget_gate_bias);
362 const TensorInfo units_out_transposed_info = TensorInfo(units_out_transposed_shape, 1, input->data_type());
Michalis Spyroubcedf512018-03-22 14:55:08 +0000363 const TensorInfo num_units_transposed_info = TensorInfo(num_units_transposed_shape, 1, input->data_type());
364
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100365 TensorInfo input_gate = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
366 TensorInfo forget_gate = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
367 TensorInfo output_gate_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
368 TensorInfo cell_state_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
369
Michalis Spyroubcedf512018-03-22 14:55:08 +0000370 // Validate forget gate
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100371 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_forget_weights, forget_gate_bias, &forget_gate));
372 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(output_state_in, &units_out_transposed_info, nullptr, &forget_gate, 1.f, 0.f, GEMMInfo()));
giuros01164a2722018-11-20 18:34:46 +0000373 ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::ADD, &forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000374 if(lstm_params.has_peephole_opt())
375 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100376 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));
377 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000378 }
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100379 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&forget_gate, &forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000380
381 // Validate input gate
382 if(!lstm_params.has_cifg_opt())
383 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100384 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(),
385 lstm_params.recurrent_to_input_weights(),
386 lstm_params.input_gate_bias());
Georgios Pinitas42447c12018-07-16 17:01:20 +0100387 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_to_input_weights()->num_dimensions() > 2);
388 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.recurrent_to_input_weights()->num_dimensions() > 2);
Georgios Pinitas42447c12018-07-16 17:01:20 +0100389 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_gate_bias()->num_dimensions() > 1);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100390
391 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, lstm_params.input_to_input_weights(), lstm_params.input_gate_bias(), &input_gate));
392 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(output_state_in, &units_out_transposed_info, nullptr, &input_gate, 1.f, 0.f, GEMMInfo()));
393 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&input_gate, &input_gate, &input_gate, ConvertPolicy::SATURATE));
394 if(lstm_params.has_peephole_opt())
395 {
396 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_input_weights());
397 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_input_weights()->num_dimensions() > 1);
398 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));
399 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&input_gate, &input_gate, &input_gate, ConvertPolicy::SATURATE));
400 }
401 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&input_gate, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000402 }
403 else
404 {
giuros01164a2722018-11-20 18:34:46 +0000405 ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::SUB, &forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000406 }
407
408 // Validate cell state
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100409 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_cell_weights, cell_bias, &cell_state_tmp));
410 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(output_state_in, &units_out_transposed_info, nullptr, &cell_state_tmp, 1.f, 0.f, GEMMInfo()));
411 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE));
412 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&cell_state_tmp, nullptr, activation_info));
413 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &input_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN));
414 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &forget_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN));
415 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000416 if(cell_threshold != 0.f)
417 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100418 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&cell_state_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold,
419 cell_threshold)));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000420 }
421
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100422 // Validate output gate tmp
423 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_output_weights, output_gate_bias, &output_gate_tmp));
424 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(output_state_in, &units_out_transposed_info, nullptr, &output_gate_tmp, 1.f, 0.f, GEMMInfo()));
425 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&output_gate_tmp, &output_gate_tmp, &output_gate_tmp, ConvertPolicy::SATURATE));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000426 if(lstm_params.has_peephole_opt())
427 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100428 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&cell_state_tmp, lstm_params.cell_to_output_weights(), &output_gate_tmp, 1, ConvertPolicy::SATURATE,
429 RoundingPolicy::TO_NEAREST_EVEN));
430 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(&output_gate_tmp, &output_gate_tmp, &output_gate_tmp, ConvertPolicy::SATURATE));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000431 }
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100432 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&output_gate_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000433
434 // Validate output state
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100435 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(&cell_state_tmp, &cell_state_tmp, activation_info));
436 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &output_gate_tmp, &output_gate_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000437 if(lstm_params.has_projection())
438 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100439 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(&output_gate_tmp, lstm_params.projection_weights(), lstm_params.projection_bias(), output_state_out));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000440 if(projection_threshold != 0.f)
441 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100442 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(output_state_out, output_state_out,
443 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold)));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000444 }
445 }
446
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100447 // Validate copy kernel
448 ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(&cell_state_tmp, cell_state_out));
449 ARM_COMPUTE_RETURN_ON_ERROR(CLCopyKernel::validate(output_state_out, output));
450
451 // Validate scratch concatenation
452 std::vector<ITensorInfo *> inputs_vector_info_raw;
Georgios Pinitas0cc37c32018-11-14 15:54:26 +0000453 if(!lstm_params.has_cifg_opt())
Michalis Spyroubcedf512018-03-22 14:55:08 +0000454 {
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100455 inputs_vector_info_raw.push_back(&input_gate);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000456 }
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100457 inputs_vector_info_raw.push_back(&cell_state_tmp);
458 inputs_vector_info_raw.push_back(&forget_gate);
459 inputs_vector_info_raw.push_back(&output_gate_tmp);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000460
461 ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenateLayer::validate(inputs_vector_info_raw, scratch_buffer));
462 return Status{};
463}
464
465void CLLSTMLayer::run()
466{
467 _memory_group.acquire();
468
469 _fully_connected_forget_gate.run();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100470 CLScheduler::get().enqueue(_transpose_forget_gate);
471 _gemm_forget_gate.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000472 CLScheduler::get().enqueue(_accum_forget_gate1);
473
474 if(_run_peephole_opt)
475 {
Georgios Pinitas42a31722018-07-09 14:35:32 +0100476 CLScheduler::get().enqueue(_pixelwise_mul_forget_gate);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000477 _accum_forget_gate2.run();
478 }
479 CLScheduler::get().enqueue(_activation_forget_gate);
480
481 if(_run_cifg_opt)
482 {
483 _ones.map(true);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100484 if(_ones.info()->data_type() == DataType::F16)
485 {
486 std::fill_n(reinterpret_cast<half *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 1);
487 }
488 else
489 {
490 std::fill_n(reinterpret_cast<float *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 1);
491 }
Michalis Spyroubcedf512018-03-22 14:55:08 +0000492 _ones.unmap();
493 CLScheduler::get().enqueue(_subtract_input_gate);
494 }
495 else
496 {
497 _fully_connected_input_gate.run();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100498 CLScheduler::get().enqueue(_transpose_input_gate);
499 _gemm_input_gate.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000500 CLScheduler::get().enqueue(_accum_input_gate1);
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100501 if(_run_peephole_opt)
502 {
503 CLScheduler::get().enqueue(_pixelwise_mul_input_gate);
504 _accum_input_gate2.run();
505 }
Michalis Spyroubcedf512018-03-22 14:55:08 +0000506 CLScheduler::get().enqueue(_activation_input_gate);
507 }
508
509 _fully_connected_cell_state.run();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100510 CLScheduler::get().enqueue(_transpose_cell_state);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000511 _gemm_cell_state1.run();
512 CLScheduler::get().enqueue(_accum_cell_state1);
513 CLScheduler::get().enqueue(_activation_cell_state);
514 CLScheduler::get().enqueue(_pixelwise_mul_cell_state1);
515 CLScheduler::get().enqueue(_pixelwise_mul_cell_state2);
516 CLScheduler::get().enqueue(_accum_cell_state2);
517
518 if(_perform_cell_clipping)
519 {
520 CLScheduler::get().enqueue(_cell_clip);
521 }
522
523 _fully_connected_output.run();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100524 CLScheduler::get().enqueue(_transpose_output);
525 _gemm_output.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000526 CLScheduler::get().enqueue(_accum_output1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000527
528 if(_run_peephole_opt)
529 {
Georgios Pinitas42a31722018-07-09 14:35:32 +0100530 CLScheduler::get().enqueue(_pixelwise_mul_output_state1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000531 _accum_output2.run();
532 }
533 CLScheduler::get().enqueue(_activation_output);
534
535 CLScheduler::get().enqueue(_activation_output_state);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100536 CLScheduler::get().enqueue(_pixelwise_mul_output_state2);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000537
538 if(_has_projection_weights)
539 {
540 _fully_connected_output_state.run();
541 if(_perform_projection_clipping)
542 {
543 CLScheduler::get().enqueue(_projection_clip);
544 }
545 }
546
547 CLScheduler::get().enqueue(_copy_cell_state);
548 CLScheduler::get().enqueue(_copy_output);
549
550 _concat_scratch_buffer.run();
551
552 _memory_group.release();
giuros01164a2722018-11-20 18:34:46 +0000553}