blob: d384400ed38d487da0b6d2688026c9ad8ff0dc19 [file] [log] [blame]
Michalis Spyroubcedf512018-03-22 14:55:08 +00001/*
2 * Copyright (c) 2018 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/runtime/CL/functions/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(),
48 _cell_state_out5(), _output1(), _output2(), _output3(), _output4(), _output5(), _cell_state_activation(), _output_projection1(), _ones(), _run_peephole_opt(false), _run_cifg_opt(false),
49 _perform_cell_clipping(false), _has_projection_weights(false), _perform_projection_clipping(false)
Michalis Spyroubcedf512018-03-22 14:55:08 +000050{
51}
52
53void CLLSTMLayer::configure(const ICLTensor *input, const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
54 const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
55 const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
56 ICLTensor *output_state, ICLTensor *cell_state, ICLTensor *scratch_buffer, ICLTensor *output, const LSTMParams<ICLTensor> &lstm_params, const ActivationLayerInfo &activation_info,
57 float cell_threshold, float projection_threshold)
58{
59 ARM_COMPUTE_ERROR_ON_NULLPTR(input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights, recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
60 forget_gate_bias, cell_bias, output_gate_bias, output_state, cell_state);
61 LSTMParams<ITensorInfo> lstm_params_info;
62 if(lstm_params.has_peephole_opt())
63 {
Michalis Spyrou09daf4d2018-06-28 17:07:22 +010064 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 +000065 }
66 if(lstm_params.has_projection())
67 {
68 lstm_params_info.set_projection_params(lstm_params.projection_weights()->info(), lstm_params.projection_bias()->info());
69 }
70 if(!lstm_params.has_cifg_opt())
71 {
72 lstm_params_info.set_cifg_params(lstm_params.input_to_input_weights()->info(), lstm_params.recurrent_to_input_weights()->info(),
73 lstm_params.cell_to_input_weights()->info(), lstm_params.input_gate_bias()->info());
74 }
75 ARM_COMPUTE_ERROR_THROW_ON(CLLSTMLayer::validate(input->info(), input_to_forget_weights->info(),
76 input_to_cell_weights->info(), input_to_output_weights->info(),
77 recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(),
78 forget_gate_bias->info(), cell_bias->info(), output_gate_bias->info(),
79 output_state->info(), cell_state->info(), scratch_buffer->info(), output->info(), lstm_params_info,
80 activation_info, cell_threshold, projection_threshold));
81
82 const TensorShape cell_state_shape = cell_state->info()->tensor_shape();
83
84 TensorShape forget_gate1_shape = compute_transposed_shape(*recurrent_to_output_weights->info());
Michalis Spyroubcedf512018-03-22 14:55:08 +000085 _forget_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
86 _forget_gate_out2.allocator()->init(TensorInfo(forget_gate1_shape, 1, input->info()->data_type()));
87 _forget_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas42a31722018-07-09 14:35:32 +010088 _forget_gate_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +000089
90 // Configure block that calculates the forget gate
Georgios Pinitas42a31722018-07-09 14:35:32 +010091 // forget_gate = Activation(input * input_to_forget_weights + output_state * recurrent_to_forget_weights + PixelWiseMul(cell_state, cell_to_forget_weights) + forget_gate_bias)
Michalis Spyroubcedf512018-03-22 14:55:08 +000092 _memory_group.manage(&_forget_gate_out1);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010093 _fully_connected_forget_gate.configure(input, input_to_forget_weights, forget_gate_bias, &_forget_gate_out1);
Michalis Spyroubcedf512018-03-22 14:55:08 +000094 _memory_group.manage(&_forget_gate_out2);
Georgios Pinitas42a31722018-07-09 14:35:32 +010095 _transpose_forget_gate.configure(recurrent_to_forget_weights, &_forget_gate_out2);
Michalis Spyroubcedf512018-03-22 14:55:08 +000096 _memory_group.manage(&_forget_gate_out3);
Georgios Pinitas42a31722018-07-09 14:35:32 +010097 _gemm_forget_gate.configure(output_state, &_forget_gate_out2, nullptr, &_forget_gate_out3, 1.f, 0.f);
Michalis Spyroubcedf512018-03-22 14:55:08 +000098 _forget_gate_out2.allocator()->allocate();
Georgios Pinitas42a31722018-07-09 14:35:32 +010099 _memory_group.manage(&_forget_gate_out5);
100 _accum_forget_gate1.configure(&_forget_gate_out1, &_forget_gate_out3, &_forget_gate_out5, ConvertPolicy::SATURATE);
101 CLTensor *forget_gate_out = &_forget_gate_out5;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000102
103 if(lstm_params.has_peephole_opt())
104 {
Georgios Pinitas42a31722018-07-09 14:35:32 +0100105 _forget_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000106
107 _run_peephole_opt = true;
108 _memory_group.manage(&_forget_gate_out4);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100109 _pixelwise_mul_forget_gate.configure(cell_state, lstm_params.cell_to_forget_weights(), &_forget_gate_out4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
110 _accum_forget_gate2.configure(&_forget_gate_out5, &_forget_gate_out4, &_forget_gate_out3, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000111 _forget_gate_out4.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000112 _forget_gate_out5.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000113 forget_gate_out = &_forget_gate_out3;
114 }
115 else
116 {
117 _forget_gate_out3.allocator()->allocate();
118 }
119 _activation_forget_gate.configure(forget_gate_out, &_forget_gate_out1, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
120 forget_gate_out->allocator()->allocate();
121
Michalis Spyroubcedf512018-03-22 14:55:08 +0000122 _input_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000123
124 // Configure block that calculates the input gate
Georgios Pinitas42a31722018-07-09 14:35:32 +0100125 // 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 +0000126 // input_gate = 1 - forget_gate, with CIFG
127 if(lstm_params.has_cifg_opt())
128 {
129 _memory_group.manage(&_input_gate_out1);
130 _ones.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
131 _subtract_input_gate.configure(&_ones, &_forget_gate_out1, &_input_gate_out1, ConvertPolicy::SATURATE);
132 _ones.allocator()->allocate();
133 _run_cifg_opt = true;
134 }
135 else
136 {
Georgios Pinitas42a31722018-07-09 14:35:32 +0100137 TensorShape input_gate_shape = compute_transposed_shape(*recurrent_to_output_weights->info());
Michalis Spyroubcedf512018-03-22 14:55:08 +0000138
Georgios Pinitas42a31722018-07-09 14:35:32 +0100139 _input_gate_out2.allocator()->init(TensorInfo(input_gate_shape, 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000140 _input_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas42a31722018-07-09 14:35:32 +0100141 _input_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
142 _input_gate_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000143
144 _memory_group.manage(&_input_gate_out1);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100145 _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 +0000146 _memory_group.manage(&_input_gate_out2);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100147 _transpose_input_gate.configure(lstm_params.recurrent_to_input_weights(), &_input_gate_out2);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000148 _memory_group.manage(&_input_gate_out3);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100149 _gemm_input_gate.configure(output_state, &_input_gate_out2, nullptr, &_input_gate_out3, 1.f, 0.f);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000150 _input_gate_out2.allocator()->allocate();
151 _memory_group.manage(&_input_gate_out4);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100152 _pixelwise_mul_input_gate.configure(cell_state, lstm_params.cell_to_input_weights(), &_input_gate_out4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000153 _memory_group.manage(&_input_gate_out5);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100154 _accum_input_gate1.configure(&_input_gate_out1, &_input_gate_out3, &_input_gate_out5, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000155 _input_gate_out3.allocator()->allocate();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100156 _accum_input_gate2.configure(&_input_gate_out5, &_input_gate_out4, &_input_gate_out1, ConvertPolicy::SATURATE);
157 _input_gate_out4.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000158 _input_gate_out5.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000159 _activation_input_gate.configure(&_input_gate_out1, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
160 }
161
162 TensorShape cell_state1_shape = compute_transposed_shape(*recurrent_to_output_weights->info());
163 _cell_state_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
164 _cell_state_out2.allocator()->init(TensorInfo(cell_state1_shape, 1, input->info()->data_type()));
165 _cell_state_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
166 _cell_state_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
167 _cell_state_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
168
169 // Configure block that calculates the cell state
Georgios Pinitas42a31722018-07-09 14:35:32 +0100170 // cell_state = Clip((PixelwiseMul(input_gate, Activation(input * input_to_cell_weights + output_state * recurrent_to_cell_weights + cell_bias)) + PixelwiseMul(forget_gate, cell_state)), cell_threshold)
Michalis Spyroubcedf512018-03-22 14:55:08 +0000171 _memory_group.manage(&_cell_state_out1);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100172 _fully_connected_cell_state.configure(input, input_to_cell_weights, cell_bias, &_cell_state_out1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000173 _memory_group.manage(&_cell_state_out2);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100174 _transpose_cell_state.configure(recurrent_to_cell_weights, &_cell_state_out2);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000175 _memory_group.manage(&_cell_state_out3);
176 _gemm_cell_state1.configure(output_state, &_cell_state_out2, nullptr, &_cell_state_out3, 1.f, 0.f);
177 _cell_state_out2.allocator()->allocate();
178 _memory_group.manage(&_cell_state_out4);
179 _accum_cell_state1.configure(&_cell_state_out1, &_cell_state_out3, &_cell_state_out4, ConvertPolicy::SATURATE);
180 _activation_cell_state.configure(&_cell_state_out4, nullptr, activation_info);
181 _memory_group.manage(&_cell_state_out5);
182 _pixelwise_mul_cell_state1.configure(&_cell_state_out4, &_input_gate_out1, &_cell_state_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
183 _input_gate_out1.allocator()->allocate();
184 _cell_state_out4.allocator()->allocate();
185 _pixelwise_mul_cell_state2.configure(&_forget_gate_out1, cell_state, &_cell_state_out3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
186 _forget_gate_out1.allocator()->allocate();
187 _accum_cell_state2.configure(&_cell_state_out5, &_cell_state_out3, &_cell_state_out1, ConvertPolicy::SATURATE);
188 _cell_state_out3.allocator()->allocate();
189 _cell_state_out5.allocator()->allocate();
190
191 // Perform clipping
192 if(cell_threshold != 0.f)
193 {
194 _perform_cell_clipping = true;
195 _cell_clip.configure(&_cell_state_out1, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, cell_threshold));
196 }
197
198 TensorShape output1_shape = compute_transposed_shape(*recurrent_to_output_weights->info());
Michalis Spyroubcedf512018-03-22 14:55:08 +0000199 _output1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
200 _output2.allocator()->init(TensorInfo(output1_shape, 1, input->info()->data_type()));
201 _output3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas42a31722018-07-09 14:35:32 +0100202 _output5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000203
204 // Configure block that calculates the output
Georgios Pinitas42a31722018-07-09 14:35:32 +0100205 // output_state = Activation(input * input_to_output_weights + output_state * recurrent_to_output_weights + PixelWiseMul(cell_state, cell_to_output_weights) + output_gate_bias)
Michalis Spyroubcedf512018-03-22 14:55:08 +0000206 _memory_group.manage(&_output1);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100207 _fully_connected_output.configure(input, input_to_output_weights, output_gate_bias, &_output1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000208 _memory_group.manage(&_output2);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100209 _transpose_output.configure(recurrent_to_output_weights, &_output2);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000210 _memory_group.manage(&_output3);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100211 _gemm_output.configure(output_state, &_output2, nullptr, &_output3, 1.f, 0.f);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000212 _output2.allocator()->allocate();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100213 _memory_group.manage(&_output5);
214 _accum_output1.configure(&_output1, &_output3, &_output5, ConvertPolicy::SATURATE);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000215 _output3.allocator()->allocate();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100216 CLTensor *output_gate_out = &_output5;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000217 if(lstm_params.has_peephole_opt())
218 {
Georgios Pinitas42a31722018-07-09 14:35:32 +0100219 _output4.allocator()->init(TensorInfo(_cell_state_out1.info()->tensor_shape(), 1, input->info()->data_type()));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000220
221 _memory_group.manage(&_output4);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100222 _pixelwise_mul_output_state1.configure(&_cell_state_out1, lstm_params.cell_to_output_weights(), &_output4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
223 _accum_output2.configure(&_output5, &_output4, &_output1, ConvertPolicy::SATURATE);
224 _output5.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000225 output_gate_out = &_output1;
226
227 // Allocate intermediate buffers
228 _output4.allocator()->allocate();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000229 }
230 else
231 {
232 _output1.allocator()->allocate();
233 }
234 _activation_output.configure(output_gate_out, output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
235 output_gate_out->allocator()->allocate();
236
237 _cell_state_activation.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
238
239 // Configure block that calculates the output state
240 /** lstm_res = PixelwiseMul(output, Activation(cell_state))
241 *
242 * -- Clip(lstm_res * projection_weights + projection_bias, projection_threshold) , if there is a projection
243 * /
244 * output_state = --
245 * \
246 * -- lstm_res , otherwise
247 */
248 _memory_group.manage(&_cell_state_activation);
249 _activation_output_state.configure(&_cell_state_out1, &_cell_state_activation, activation_info);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100250 _pixelwise_mul_output_state2.configure(&_cell_state_activation, output, output_state, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000251 _cell_state_activation.allocator()->allocate();
252
253 if(lstm_params.has_projection())
254 {
255 _has_projection_weights = true;
256 _output_projection1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
257 _memory_group.manage(&_output_projection1);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100258 _fully_connected_output_state.configure(output_state, lstm_params.projection_weights(), lstm_params.projection_bias(), &_output_projection1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000259 // Perform clipping
260 if(projection_threshold != 0.f)
261 {
262 _perform_projection_clipping = true;
263 _projection_clip.configure(&_output_projection1, output_state, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold));
264 }
265
266 // Allocate intermediate buffer
267 _output_projection1.allocator()->allocate();
268 }
269
270 // Copy cell state and output
271 _copy_cell_state.configure(&_cell_state_out1, cell_state);
272 _cell_state_out1.allocator()->allocate();
273 _copy_output.configure(output_state, output);
274
275 // Vector for holding the tensors to store in scratch buffer
276 std::vector<ICLTensor *> scratch_inputs;
277 if(lstm_params.has_cifg_opt())
278 {
279 scratch_inputs.emplace_back(&_input_gate_out1);
280 }
281 scratch_inputs.emplace_back(&_cell_state_out1);
282 scratch_inputs.emplace_back(forget_gate_out);
283 scratch_inputs.emplace_back(output_gate_out);
284 _concat_scratch_buffer.configure(scratch_inputs, scratch_buffer);
285}
286
287Status CLLSTMLayer::validate(const ITensorInfo *input, const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
288 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
289 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
290 const ITensorInfo *output_state, const ITensorInfo *cell_state, const ITensorInfo *scratch_buffer, const ITensorInfo *output,
291 const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold)
292{
293 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights, recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
294 forget_gate_bias, cell_bias, output_gate_bias, output_state, cell_state);
295 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
296 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, input_to_forget_weights, input_to_cell_weights, input_to_output_weights, recurrent_to_forget_weights, recurrent_to_cell_weights,
297 recurrent_to_output_weights, forget_gate_bias, cell_bias, output_gate_bias, output_state, cell_state);
Georgios Pinitas42447c12018-07-16 17:01:20 +0100298 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2);
299 ARM_COMPUTE_RETURN_ERROR_ON(input_to_forget_weights->num_dimensions() > 2);
300 ARM_COMPUTE_RETURN_ERROR_ON(input_to_cell_weights->num_dimensions() > 2);
301 ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->num_dimensions() > 2);
302 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_forget_weights->num_dimensions() > 2);
303 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_cell_weights->num_dimensions() > 2);
304 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->num_dimensions() > 2);
305 ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->num_dimensions() > 1);
306 ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->num_dimensions() > 1);
307 ARM_COMPUTE_RETURN_ERROR_ON(output_gate_bias->num_dimensions() > 1);
308 ARM_COMPUTE_RETURN_ERROR_ON(output_state->num_dimensions() > 2);
309 ARM_COMPUTE_RETURN_ERROR_ON(cell_state->num_dimensions() > 2);
310 ARM_COMPUTE_RETURN_ERROR_ON(scratch_buffer->num_dimensions() > 2);
311 ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 2);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000312 ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->dimension(0) * 4 != scratch_buffer->dimension(0) && cell_bias->dimension(0) * 3 != scratch_buffer->dimension(0));
313
314 if(lstm_params.has_peephole_opt())
315 {
Michalis Spyrou09daf4d2018-06-28 17:07:22 +0100316 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 +0100317 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->num_dimensions() > 1);
318 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_output_weights()->num_dimensions() > 1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000319 }
320
321 TensorShape units_out_transposed_shape = compute_transposed_shape(*recurrent_to_output_weights);
322 TensorShape gemmv_shape{ 1, output_state->dimension(1) };
323 TensorShape num_units_transposed_shape = compute_transposed_shape(*forget_gate_bias);
324 const TensorInfo units_out_transposed_info = TensorInfo(units_out_transposed_shape, 1, input->data_type());
325 const TensorInfo gemmv_shape_info = TensorInfo(gemmv_shape, 1, input->data_type());
326 const TensorInfo num_units_transposed_info = TensorInfo(num_units_transposed_shape, 1, input->data_type());
327
328 // Validate forget gate
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100329 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_forget_weights, forget_gate_bias, cell_state));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000330 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(output_state, &units_out_transposed_info, nullptr, cell_state, 1.f, 0.f, GEMMInfo()));
331 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAdditionKernel::validate(cell_state, cell_state, cell_state, ConvertPolicy::SATURATE));
332 if(lstm_params.has_peephole_opt())
333 {
334 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(cell_state, &num_units_transposed_info, nullptr, &gemmv_shape_info, 1.f, 0.f, GEMMInfo()));
335 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(cell_state, &gemmv_shape_info, cell_state, ConvertPolicy::SATURATE));
336 }
337 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(cell_state, cell_state, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
338
339 // Validate input gate
340 if(!lstm_params.has_cifg_opt())
341 {
342 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(), lstm_params.recurrent_to_input_weights(), lstm_params.cell_to_input_weights(), lstm_params.input_gate_bias());
Georgios Pinitas42447c12018-07-16 17:01:20 +0100343 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_to_input_weights()->num_dimensions() > 2);
344 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.recurrent_to_input_weights()->num_dimensions() > 2);
345 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_input_weights()->num_dimensions() > 1);
346 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_gate_bias()->num_dimensions() > 1);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100347 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, lstm_params.input_to_input_weights(), lstm_params.input_gate_bias(), cell_state));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000348 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(cell_state, &num_units_transposed_info, nullptr, &gemmv_shape_info, 1.f, 0.f, GEMMInfo()));
349 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(cell_state, &gemmv_shape_info, cell_state, ConvertPolicy::SATURATE));
350 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(cell_state, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
351 }
352 else
353 {
354 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticSubtractionKernel::validate(cell_state, cell_state, cell_state, ConvertPolicy::SATURATE));
355 }
356
357 // Validate cell state
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100358 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_cell_weights, cell_bias, cell_state));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000359 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(cell_state, nullptr, activation_info));
360 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(cell_state, cell_state, cell_state, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN));
361
362 if(cell_threshold != 0.f)
363 {
364 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(cell_state, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, cell_threshold)));
365 }
366
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100367 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(input, input_to_output_weights, output_gate_bias, cell_state));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000368 if(lstm_params.has_peephole_opt())
369 {
370 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAddition::validate(cell_state, cell_state, cell_state, ConvertPolicy::SATURATE));
371 }
372 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(cell_state, output, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
373
374 // Validate output state
375 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(cell_state, cell_state, activation_info));
376 ARM_COMPUTE_RETURN_ON_ERROR(CLPixelWiseMultiplicationKernel::validate(cell_state, output, output_state, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN));
377 if(lstm_params.has_projection())
378 {
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100379 ARM_COMPUTE_RETURN_ON_ERROR(CLFullyConnectedLayer::validate(output_state, lstm_params.projection_weights(), lstm_params.projection_bias(), cell_state));
Michalis Spyroubcedf512018-03-22 14:55:08 +0000380 if(projection_threshold != 0.f)
381 {
382 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayerKernel::validate(cell_state, output_state, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold,
383 projection_threshold)));
384 }
385 }
386
387 std::vector<TensorInfo> inputs_vector_info;
388 if(lstm_params.has_cifg_opt())
389 {
390 inputs_vector_info.emplace_back(*cell_state);
391 }
392 inputs_vector_info.emplace_back(*cell_state);
393 inputs_vector_info.emplace_back(*cell_state);
394 inputs_vector_info.emplace_back(*cell_state);
395
396 std::vector<ITensorInfo *> inputs_vector_info_raw;
397 for(auto &input : inputs_vector_info)
398 {
399 inputs_vector_info_raw.emplace_back(&input);
400 }
401
402 ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenateLayer::validate(inputs_vector_info_raw, scratch_buffer));
403 return Status{};
404}
405
406void CLLSTMLayer::run()
407{
408 _memory_group.acquire();
409
410 _fully_connected_forget_gate.run();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100411 CLScheduler::get().enqueue(_transpose_forget_gate);
412 _gemm_forget_gate.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000413 CLScheduler::get().enqueue(_accum_forget_gate1);
414
415 if(_run_peephole_opt)
416 {
Georgios Pinitas42a31722018-07-09 14:35:32 +0100417 CLScheduler::get().enqueue(_pixelwise_mul_forget_gate);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000418 _accum_forget_gate2.run();
419 }
420 CLScheduler::get().enqueue(_activation_forget_gate);
421
422 if(_run_cifg_opt)
423 {
424 _ones.map(true);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100425 if(_ones.info()->data_type() == DataType::F16)
426 {
427 std::fill_n(reinterpret_cast<half *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 1);
428 }
429 else
430 {
431 std::fill_n(reinterpret_cast<float *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 1);
432 }
Michalis Spyroubcedf512018-03-22 14:55:08 +0000433 _ones.unmap();
434 CLScheduler::get().enqueue(_subtract_input_gate);
435 }
436 else
437 {
438 _fully_connected_input_gate.run();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100439 CLScheduler::get().enqueue(_transpose_input_gate);
440 _gemm_input_gate.run();
441 CLScheduler::get().enqueue(_pixelwise_mul_input_gate);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000442 CLScheduler::get().enqueue(_accum_input_gate1);
443 _accum_input_gate2.run();
444 CLScheduler::get().enqueue(_activation_input_gate);
445 }
446
447 _fully_connected_cell_state.run();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100448 CLScheduler::get().enqueue(_transpose_cell_state);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000449 _gemm_cell_state1.run();
450 CLScheduler::get().enqueue(_accum_cell_state1);
451 CLScheduler::get().enqueue(_activation_cell_state);
452 CLScheduler::get().enqueue(_pixelwise_mul_cell_state1);
453 CLScheduler::get().enqueue(_pixelwise_mul_cell_state2);
454 CLScheduler::get().enqueue(_accum_cell_state2);
455
456 if(_perform_cell_clipping)
457 {
458 CLScheduler::get().enqueue(_cell_clip);
459 }
460
461 _fully_connected_output.run();
Georgios Pinitas42a31722018-07-09 14:35:32 +0100462 CLScheduler::get().enqueue(_transpose_output);
463 _gemm_output.run();
Michalis Spyroubcedf512018-03-22 14:55:08 +0000464 CLScheduler::get().enqueue(_accum_output1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000465
466 if(_run_peephole_opt)
467 {
Georgios Pinitas42a31722018-07-09 14:35:32 +0100468 CLScheduler::get().enqueue(_pixelwise_mul_output_state1);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000469 _accum_output2.run();
470 }
471 CLScheduler::get().enqueue(_activation_output);
472
473 CLScheduler::get().enqueue(_activation_output_state);
Georgios Pinitas42a31722018-07-09 14:35:32 +0100474 CLScheduler::get().enqueue(_pixelwise_mul_output_state2);
Michalis Spyroubcedf512018-03-22 14:55:08 +0000475
476 if(_has_projection_weights)
477 {
478 _fully_connected_output_state.run();
479 if(_perform_projection_clipping)
480 {
481 CLScheduler::get().enqueue(_projection_clip);
482 }
483 }
484
485 CLScheduler::get().enqueue(_copy_cell_state);
486 CLScheduler::get().enqueue(_copy_output);
487
488 _concat_scratch_buffer.run();
489
490 _memory_group.release();
491}