blob: 42b805794b38814790138f649daf35b0630a9e8c [file] [log] [blame]
Michalis Spyrou25f45a42018-08-08 12:53:05 +01001/*
Georgios Pinitas13a20802019-01-16 18:21:08 +00002 * Copyright (c) 2018-2019 ARM Limited.
Michalis Spyrou25f45a42018-08-08 12:53:05 +01003 *
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/NEON/functions/NELSTMLayer.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/common/LSTMParams.h"
32
33#include <cmath>
34#include <memory>
35#include <tuple>
36
37using namespace arm_compute;
38using namespace arm_compute::misc::shape_calculator;
39
40NELSTMLayer::NELSTMLayer(std::shared_ptr<IMemoryManager> memory_manager)
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 Kesapides917959c2019-02-04 12:37:29 +000046 _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(),
47 _input_gate_out1(), _input_gate_out2(), _input_gate_out3(), _input_gate_out4(), _forget_gate_out1(), _forget_gate_out2(), _forget_gate_out3(), _forget_gate_out4(), _forget_gate_out5(),
48 _forget_gate_out6(), _cell_state_out1(), _cell_state_out2(), _cell_state_out3(), _cell_state_out4(), _cell_state_out5(), _output1(), _output2(), _output3(), _output4(), _cell_state_activation(),
49 _output_state1(), _ones(), _run_peephole_opt(false), _run_cifg_opt(false), _perform_cell_clipping(false), _has_projection_weights(false), _perform_projection_clipping(false), _is_prepared(false)
Michalis Spyrou25f45a42018-08-08 12:53:05 +010050{
51}
52
53void NELSTMLayer::configure(const ITensor *input,
54 const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights,
55 const ITensor *recurrent_to_forget_weights, const ITensor *recurrent_to_cell_weights, const ITensor *recurrent_to_output_weights,
56 const ITensor *forget_gate_bias, const ITensor *cell_bias, const ITensor *output_gate_bias,
57 const ITensor *output_state_in, const ITensor *cell_state_in,
58 ITensor *scratch_buffer, ITensor *output_state_out, ITensor *cell_state_out, ITensor *output,
59 const LSTMParams<ITensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold)
60{
61 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
69 LSTMParams<ITensorInfo> lstm_params_info;
70 if(lstm_params.has_peephole_opt())
71 {
72 lstm_params_info.set_peephole_params(lstm_params.cell_to_forget_weights()->info(), lstm_params.cell_to_output_weights()->info());
73 }
74 if(lstm_params.has_projection())
75 {
76 lstm_params_info.set_projection_params(lstm_params.projection_weights()->info(),
77 lstm_params.projection_bias() != nullptr ? lstm_params.projection_bias()->info() : nullptr);
78 }
79 if(!lstm_params.has_cifg_opt())
80 {
81 const ITensorInfo *cell_to_input_weights_info = (lstm_params.has_peephole_opt()) ? lstm_params.cell_to_input_weights()->info() : nullptr;
82 lstm_params_info.set_cifg_params(lstm_params.input_to_input_weights()->info(), lstm_params.recurrent_to_input_weights()->info(),
83 cell_to_input_weights_info, lstm_params.input_gate_bias()->info());
84 }
85
86 // Validate
87 ARM_COMPUTE_ERROR_THROW_ON(NELSTMLayer::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(),
91 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));
94
Georgios Pinitasda953f22019-04-02 17:27:03 +010095 const TensorShape cell_state_shape = cell_state_in->info()->tensor_shape();
Michalis Spyrou25f45a42018-08-08 12:53:05 +010096
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 Kesapides917959c2019-02-04 12:37:29 +000099 // 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 Spyrou25f45a42018-08-08 12:53:05 +0100101 _forget_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100102 _forget_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
103 _forget_gate_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
104
John Kesapides917959c2019-02-04 12:37:29 +0000105 std::vector<const ITensor *> inputs_vector;
106 inputs_vector.emplace_back(input);
107 inputs_vector.emplace_back(output_state_in);
108
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100109 _memory_group.manage(&_forget_gate_out2);
Georgios Pinitas09f24972019-05-17 18:14:40 +0100110 _concat_inputs_forget_gate.configure(inputs_vector, &_forget_gate_out2, Window::DimX);
John Kesapides917959c2019-02-04 12:37:29 +0000111
112 std::vector<const ITensor *> weights_vector;
113
114 weights_vector.emplace_back(input_to_forget_weights);
115 weights_vector.emplace_back(recurrent_to_forget_weights);
116
Georgios Pinitas09f24972019-05-17 18:14:40 +0100117 _concat_weights_forget_gate.configure(weights_vector, &_forget_gate_out6, Window::DimX);
John Kesapides917959c2019-02-04 12:37:29 +0000118
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100119 _memory_group.manage(&_forget_gate_out5);
John Kesapides917959c2019-02-04 12:37:29 +0000120 _fully_connected_forget_gate.configure(&_forget_gate_out2, &_forget_gate_out6, forget_gate_bias, &_forget_gate_out5);
121 _memory_group.manage(&_forget_gate_out1);
122 _memory_group.manage(&_forget_gate_out3);
123 _forget_gate_out6.allocator()->allocate();
124
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100125 Tensor *forget_gate_out = &_forget_gate_out5;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100126 if(lstm_params.has_peephole_opt())
127 {
128 _forget_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
129
130 _run_peephole_opt = true;
131 _memory_group.manage(&_forget_gate_out4);
132 _pixelwise_mul_forget_gate.configure(cell_state_in, lstm_params.cell_to_forget_weights(), &_forget_gate_out4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
133 _accum_forget_gate2.configure(&_forget_gate_out5, &_forget_gate_out4, &_forget_gate_out3, ConvertPolicy::SATURATE);
134 _forget_gate_out4.allocator()->allocate();
135 _forget_gate_out5.allocator()->allocate();
136 forget_gate_out = &_forget_gate_out3;
137 }
138 else
139 {
140 _forget_gate_out3.allocator()->allocate();
141 }
Georgios Pinitas4f859822019-02-06 18:08:04 +0000142 _activation_forget_gate.configure(forget_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100143
144 // Configure block that calculates the input gate
145 // 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
146 // input_gate = 1 - forget_gate, with CIFG
John Kesapides917959c2019-02-04 12:37:29 +0000147 // We optimize this as follows:
148 // 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
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100149 _input_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas4f859822019-02-06 18:08:04 +0000150 Tensor *input_gate_out = &_input_gate_out1;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100151 if(lstm_params.has_cifg_opt())
152 {
153 _memory_group.manage(&_input_gate_out1);
154 _ones.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Georgios Pinitas4f859822019-02-06 18:08:04 +0000155 _subtract_input_gate.configure(&_ones, forget_gate_out, &_input_gate_out1, ConvertPolicy::SATURATE);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100156 _ones.allocator()->allocate();
157 _run_cifg_opt = true;
158 }
159 else
160 {
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100161 _input_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
162 _input_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
John Kesapides917959c2019-02-04 12:37:29 +0000163
164 std::vector<const ITensor *> lstm_weights;
165 lstm_weights.emplace_back(lstm_params.input_to_input_weights());
166 lstm_weights.emplace_back(lstm_params.recurrent_to_input_weights());
167
Georgios Pinitas09f24972019-05-17 18:14:40 +0100168 _concat_weights_input_gate.configure(lstm_weights, &_input_gate_out2, Window::DimX);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100169
170 _memory_group.manage(&_input_gate_out1);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100171 _memory_group.manage(&_input_gate_out4);
John Kesapides917959c2019-02-04 12:37:29 +0000172
173 _fully_connected_input_gate.configure(&_forget_gate_out2, &_input_gate_out2, lstm_params.input_gate_bias(), &_input_gate_out3);
174 _input_gate_out2.allocator()->allocate();
175 input_gate_out = &_input_gate_out3;
176
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100177 if(_run_peephole_opt)
178 {
John Kesapides917959c2019-02-04 12:37:29 +0000179 _memory_group.manage(&_input_gate_out4);
180 _pixelwise_mul_input_gate.configure(cell_state_in, lstm_params.cell_to_input_weights(), &_input_gate_out4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
181 _accum_input_gate2.configure(&_input_gate_out3, &_input_gate_out4, &_input_gate_out1, ConvertPolicy::SATURATE);
182 _input_gate_out3.allocator()->allocate();
Georgios Pinitas4f859822019-02-06 18:08:04 +0000183 _input_gate_out4.allocator()->allocate();
Georgios Pinitas4f859822019-02-06 18:08:04 +0000184 input_gate_out = &_input_gate_out1;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100185 }
Georgios Pinitas4f859822019-02-06 18:08:04 +0000186 else
187 {
188 _input_gate_out1.allocator()->allocate();
189 }
190 _activation_input_gate.configure(input_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100191 }
192
193 // Configure block that calculates the cell state
194 // 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)
195 TensorShape cell_state1_shape = compute_transposed_shape(*recurrent_to_output_weights->info());
196 _cell_state_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
197 _cell_state_out2.allocator()->init(TensorInfo(cell_state1_shape, 1, input->info()->data_type()));
198 _cell_state_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
199 _cell_state_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
200 _cell_state_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
201
202 _memory_group.manage(&_cell_state_out1);
203 _fully_connected_cell_state.configure(input, input_to_cell_weights, cell_bias, &_cell_state_out1);
204 _memory_group.manage(&_cell_state_out2);
205 _transpose_cell_state.configure(recurrent_to_cell_weights, &_cell_state_out2);
206 _memory_group.manage(&_cell_state_out3);
207 _gemm_cell_state1.configure(output_state_in, &_cell_state_out2, nullptr, &_cell_state_out3, 1.f, 0.f);
208 _cell_state_out2.allocator()->allocate();
209 _memory_group.manage(&_cell_state_out4);
210 _accum_cell_state1.configure(&_cell_state_out1, &_cell_state_out3, &_cell_state_out4, ConvertPolicy::SATURATE);
211 _activation_cell_state.configure(&_cell_state_out4, nullptr, activation_info);
212 _memory_group.manage(&_cell_state_out5);
Georgios Pinitas4f859822019-02-06 18:08:04 +0000213 _pixelwise_mul_cell_state1.configure(&_cell_state_out4, input_gate_out, &_cell_state_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100214 _cell_state_out4.allocator()->allocate();
Georgios Pinitas4f859822019-02-06 18:08:04 +0000215 _pixelwise_mul_cell_state2.configure(forget_gate_out, cell_state_in, &_cell_state_out3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100216 _accum_cell_state2.configure(&_cell_state_out5, &_cell_state_out3, &_cell_state_out1, ConvertPolicy::SATURATE);
217 _cell_state_out3.allocator()->allocate();
218 _cell_state_out5.allocator()->allocate();
219 // Perform clipping
220 if(cell_threshold != 0.f)
221 {
222 _perform_cell_clipping = true;
223 _cell_clip.configure(&_cell_state_out1, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, cell_threshold));
224 }
225
226 // Configure block that calculates the output
227 // 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 Kesapides917959c2019-02-04 12:37:29 +0000228 // We optimize this as follows:
229 // 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 Spyrou25f45a42018-08-08 12:53:05 +0100230 _output1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
John Kesapides917959c2019-02-04 12:37:29 +0000231 _output4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100232
John Kesapides917959c2019-02-04 12:37:29 +0000233 std::vector<const ITensor *> in_out_weights;
234 in_out_weights.emplace_back(input_to_output_weights);
235 in_out_weights.emplace_back(recurrent_to_output_weights);
236
Georgios Pinitas09f24972019-05-17 18:14:40 +0100237 _concat_weights_output.configure(in_out_weights, &_output2, Window::DimX);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100238 _memory_group.manage(&_output1);
John Kesapides917959c2019-02-04 12:37:29 +0000239 _memory_group.manage(&_output4);
240
241 _fully_connected_output.configure(&_forget_gate_out2, &_output2, output_gate_bias, &_output4);
242
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100243 _output2.allocator()->allocate();
John Kesapides917959c2019-02-04 12:37:29 +0000244 _forget_gate_out2.allocator()->allocate();
245
246 Tensor *output_gate_out = &_output4;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100247 if(lstm_params.has_peephole_opt())
248 {
John Kesapides917959c2019-02-04 12:37:29 +0000249 _output3.allocator()->init(TensorInfo(_cell_state_out1.info()->tensor_shape(), 1, input->info()->data_type()));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100250
John Kesapides917959c2019-02-04 12:37:29 +0000251 _memory_group.manage(&_output3);
252 _pixelwise_mul_output_state1.configure(&_cell_state_out1, lstm_params.cell_to_output_weights(), &_output3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
253 _accum_output2.configure(&_output4, &_output3, &_output1, ConvertPolicy::SATURATE);
254 _output4.allocator()->allocate();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100255 output_gate_out = &_output1;
256
257 // Allocate intermediate buffers
John Kesapides917959c2019-02-04 12:37:29 +0000258 _output3.allocator()->allocate();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100259 }
260 else
261 {
262 _output1.allocator()->allocate();
263 }
264 _activation_output.configure(output_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100265
266 // Configure block that calculates the output state
267 /** lstm_res = PixelwiseMul(output, Activation(cell_state))
268 *
269 * -- Clip(lstm_res * projection_weights + projection_bias, projection_threshold) , if there is a projection
270 * /
271 * output_state = --
272 * \
273 * -- lstm_res , otherwise
274 */
275 ITensor *output_state_out_tmp = lstm_params.has_projection() ? &_output_state1 : output_state_out;
276 _cell_state_activation.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
277 _output_state1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type()));
278
279 _memory_group.manage(&_cell_state_activation);
280 _activation_output_state.configure(&_cell_state_out1, &_cell_state_activation, activation_info);
281 _pixelwise_mul_output_state2.configure(&_cell_state_activation, output_gate_out, output_state_out_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO);
282 _cell_state_activation.allocator()->allocate();
Georgios Pinitas13a20802019-01-16 18:21:08 +0000283 output_gate_out->allocator()->allocate();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100284
285 if(lstm_params.has_projection())
286 {
287 _has_projection_weights = true;
288 _fully_connected_output_state.configure(output_state_out_tmp, lstm_params.projection_weights(), lstm_params.projection_bias(), output_state_out);
289 _output_state1.allocator()->allocate();
290 // Perform clipping
291 if(projection_threshold != 0.f)
292 {
293 _perform_projection_clipping = true;
294 _projection_clip.configure(output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold));
295 }
296 }
297
298 // Copy cell state and output
299 _copy_cell_state.configure(&_cell_state_out1, cell_state_out);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100300 _copy_output.configure(output_state_out, output);
301
302 // Vector for holding the tensors to store in scratch buffer
303 std::vector<ITensor *> scratch_inputs;
Georgios Pinitas0cc37c32018-11-14 15:54:26 +0000304 if(!lstm_params.has_cifg_opt())
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100305 {
Georgios Pinitas4f859822019-02-06 18:08:04 +0000306 scratch_inputs.emplace_back(input_gate_out);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100307 }
308 scratch_inputs.emplace_back(&_cell_state_out1);
309 scratch_inputs.emplace_back(forget_gate_out);
310 scratch_inputs.emplace_back(output_gate_out);
Georgios Pinitas09f24972019-05-17 18:14:40 +0100311 _concat_scratch_buffer.configure(scratch_inputs, scratch_buffer, Window::DimX);
Georgios Pinitas4f859822019-02-06 18:08:04 +0000312 input_gate_out->allocator()->allocate();
313 _cell_state_out1.allocator()->allocate();
314 forget_gate_out->allocator()->allocate();
315 output_gate_out->allocator()->allocate();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100316}
317
318Status NELSTMLayer::validate(const ITensorInfo *input,
319 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
320 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
321 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
322 const ITensorInfo *output_state_in, const ITensorInfo *cell_state_in,
323 const ITensorInfo *scratch_buffer, const ITensorInfo *output_state_out, const ITensorInfo *cell_state_out, const ITensorInfo *output,
324 const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold)
325{
326 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input,
327 input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
328 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
329 forget_gate_bias, cell_bias, output_gate_bias,
330 output_state_in, cell_state_in,
331 scratch_buffer, output_state_out, cell_state_out, output);
332
333 // Check data types
334 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
335 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input,
336 input_to_forget_weights, input_to_cell_weights, input_to_output_weights,
337 recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights,
338 forget_gate_bias, cell_bias, output_gate_bias,
339 output_state_in, cell_state_in,
340 scratch_buffer, output_state_out, cell_state_out, output);
341
342 // Check dimensions
343 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2);
344 ARM_COMPUTE_RETURN_ERROR_ON(input_to_forget_weights->num_dimensions() > 2);
345 ARM_COMPUTE_RETURN_ERROR_ON(input_to_cell_weights->num_dimensions() > 2);
346 ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->num_dimensions() > 2);
347 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_forget_weights->num_dimensions() > 2);
348 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_cell_weights->num_dimensions() > 2);
349 ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->num_dimensions() > 2);
350 ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->num_dimensions() > 1);
351 ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->num_dimensions() > 1);
352 ARM_COMPUTE_RETURN_ERROR_ON(output_gate_bias->num_dimensions() > 1);
353 ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() > 2);
354 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->num_dimensions() > 2);
355 ARM_COMPUTE_RETURN_ERROR_ON(scratch_buffer->num_dimensions() > 2);
356 ARM_COMPUTE_RETURN_ERROR_ON(output_state_out->num_dimensions() > 2);
357 ARM_COMPUTE_RETURN_ERROR_ON(cell_state_out->num_dimensions() > 2);
358 ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 2);
359 ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->dimension(0) * 4 != scratch_buffer->dimension(0)
360 && cell_bias->dimension(0) * 3 != scratch_buffer->dimension(0));
361
362 const unsigned int num_batches = input->dimension(1);
363 const unsigned int num_cells = input_to_output_weights->dimension(1);
364
365 // Check peephole optimization
366 if(lstm_params.has_peephole_opt())
367 {
368 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_output_weights(), lstm_params.cell_to_forget_weights());
369 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->num_dimensions() > 1);
370 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_output_weights()->num_dimensions() > 1);
371 }
372
373 TensorShape units_out_transposed_shape = compute_transposed_shape(*recurrent_to_output_weights);
374 TensorShape num_units_transposed_shape = compute_transposed_shape(*forget_gate_bias);
375 const TensorInfo units_out_transposed_info = TensorInfo(units_out_transposed_shape, 1, input->data_type());
376 const TensorInfo num_units_transposed_info = TensorInfo(num_units_transposed_shape, 1, input->data_type());
377
378 TensorInfo input_gate = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
379 TensorInfo forget_gate = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
380 TensorInfo output_gate_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
381 TensorInfo cell_state_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type());
382
John Kesapides917959c2019-02-04 12:37:29 +0000383 std::vector<const ITensorInfo *> inputs_vector;
384 inputs_vector.emplace_back(input);
385 inputs_vector.emplace_back(output_state_in);
Georgios Pinitas09f24972019-05-17 18:14:40 +0100386 const TensorShape concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, 0);
387 TensorInfo forget_gate_concat = TensorInfo(concat_shape, 1, input->data_type());
388 ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate(inputs_vector, &forget_gate_concat, Window::DimX));
John Kesapides917959c2019-02-04 12:37:29 +0000389
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100390 // Validate forget gate
391 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, input_to_forget_weights, forget_gate_bias, &forget_gate));
John Kesapides917959c2019-02-04 12:37:29 +0000392
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100393 if(lstm_params.has_peephole_opt())
394 {
395 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_forget_weights(), &forget_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
396 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE));
397 }
398 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&forget_gate, &forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
399
400 // Validate input gate
401 if(!lstm_params.has_cifg_opt())
402 {
403 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(),
404 lstm_params.recurrent_to_input_weights(),
405 lstm_params.input_gate_bias());
406 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_to_input_weights()->num_dimensions() > 2);
407 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.recurrent_to_input_weights()->num_dimensions() > 2);
408 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_gate_bias()->num_dimensions() > 1);
409
John Kesapides917959c2019-02-04 12:37:29 +0000410 std::vector<const ITensorInfo *> lstm_weights;
411 lstm_weights.emplace_back(lstm_params.input_to_input_weights());
412 lstm_weights.emplace_back(lstm_params.recurrent_to_input_weights());
Georgios Pinitas09f24972019-05-17 18:14:40 +0100413 TensorShape lstm_weights_concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(lstm_weights, 0);
414 TensorInfo lstm_gate_concat = TensorInfo(lstm_weights_concat_shape, 1, input->data_type());
415 ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate(lstm_weights, &lstm_gate_concat, Window::DimX));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100416 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, lstm_params.input_to_input_weights(), lstm_params.input_gate_bias(), &input_gate));
John Kesapides917959c2019-02-04 12:37:29 +0000417
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100418 if(lstm_params.has_peephole_opt())
419 {
420 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_input_weights());
421 ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_input_weights()->num_dimensions() > 1);
422 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_input_weights(), &input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
423 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&input_gate, &input_gate, &input_gate, ConvertPolicy::SATURATE));
424 }
425 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&input_gate, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
426 }
427 else
428 {
429 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticSubtractionKernel::validate(&forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE));
430 }
431
432 // Validate cell state
433 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, input_to_cell_weights, cell_bias, &cell_state_tmp));
434 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMM::validate(output_state_in, &units_out_transposed_info, nullptr, &cell_state_tmp, 1.f, 0.f, GEMMInfo()));
435 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE));
436 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&cell_state_tmp, nullptr, activation_info));
437 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &input_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
438 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &forget_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
439 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE));
440 if(cell_threshold != 0.f)
441 {
442 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&cell_state_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold,
443 cell_threshold)));
444 }
445
446 // Validate output gate tmp
John Kesapides917959c2019-02-04 12:37:29 +0000447 std::vector<const ITensorInfo *> in_out_weights;
448 in_out_weights.emplace_back(input_to_output_weights);
449 in_out_weights.emplace_back(recurrent_to_output_weights);
Georgios Pinitas09f24972019-05-17 18:14:40 +0100450 TensorShape in_out_weights_concat_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(in_out_weights, 0);
451 TensorInfo in_out_gate_concat = TensorInfo(in_out_weights_concat_shape, 1, input->data_type());
452 ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate(in_out_weights, &in_out_gate_concat, Window::DimX));
John Kesapides917959c2019-02-04 12:37:29 +0000453
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100454 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, input_to_output_weights, output_gate_bias, &output_gate_tmp));
John Kesapides917959c2019-02-04 12:37:29 +0000455
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100456 if(lstm_params.has_peephole_opt())
457 {
458 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, lstm_params.cell_to_output_weights(), &output_gate_tmp, 1, ConvertPolicy::SATURATE,
459 RoundingPolicy::TO_ZERO));
460 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&output_gate_tmp, &output_gate_tmp, &output_gate_tmp, ConvertPolicy::SATURATE));
461 }
462 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&output_gate_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)));
463
464 // Validate output state
465 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&cell_state_tmp, &cell_state_tmp, activation_info));
466 ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &output_gate_tmp, &output_gate_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO));
467 if(lstm_params.has_projection())
468 {
469 ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(&output_gate_tmp, lstm_params.projection_weights(), lstm_params.projection_bias(), output_state_out));
470 if(projection_threshold != 0.f)
471 {
472 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(output_state_out, output_state_out,
473 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold)));
474 }
475 }
476
477 // Validate copy kernel
478 ARM_COMPUTE_RETURN_ON_ERROR(NECopyKernel::validate(&cell_state_tmp, cell_state_out));
479 ARM_COMPUTE_RETURN_ON_ERROR(NECopyKernel::validate(output_state_out, output));
480
481 // Validate scratch concatenation
482 std::vector<ITensorInfo *> inputs_vector_info_raw;
Georgios Pinitas0cc37c32018-11-14 15:54:26 +0000483 if(!lstm_params.has_cifg_opt())
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100484 {
485 inputs_vector_info_raw.push_back(&input_gate);
486 }
487 inputs_vector_info_raw.push_back(&cell_state_tmp);
488 inputs_vector_info_raw.push_back(&forget_gate);
489 inputs_vector_info_raw.push_back(&output_gate_tmp);
490
Georgios Pinitas09f24972019-05-17 18:14:40 +0100491 ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate(inputs_vector_info_raw, scratch_buffer, Window::DimX));
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100492 return Status{};
493}
494
495void NELSTMLayer::run()
496{
John Kesapides917959c2019-02-04 12:37:29 +0000497 prepare();
498
Georgios Pinitasda953f22019-04-02 17:27:03 +0100499 MemoryGroupResourceScope scope_mg(_memory_group);
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100500
Michalis Spyrou2761c2f2019-03-22 13:06:08 +0000501 _concat_inputs_forget_gate.run();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100502 _fully_connected_forget_gate.run();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100503
504 if(_run_peephole_opt)
505 {
506 NEScheduler::get().schedule(&_pixelwise_mul_forget_gate, Window::DimY);
507 _accum_forget_gate2.run();
508 }
509 NEScheduler::get().schedule(&_activation_forget_gate, Window::DimY);
510
511 if(_run_cifg_opt)
512 {
513 if(_ones.info()->data_type() == DataType::F16)
514 {
515 std::fill_n(reinterpret_cast<half *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 1);
516 }
517 else
518 {
519 std::fill_n(reinterpret_cast<float *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 1);
520 }
521 NEScheduler::get().schedule(&_subtract_input_gate, Window::DimY);
522 }
523 else
524 {
525 _fully_connected_input_gate.run();
John Kesapides917959c2019-02-04 12:37:29 +0000526
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100527 if(_run_peephole_opt)
528 {
529 NEScheduler::get().schedule(&_pixelwise_mul_input_gate, Window::DimY);
530 _accum_input_gate2.run();
531 }
532 NEScheduler::get().schedule(&_activation_input_gate, Window::DimY);
533 }
534
535 _fully_connected_cell_state.run();
536 NEScheduler::get().schedule(&_transpose_cell_state, Window::DimY);
537 _gemm_cell_state1.run();
538 NEScheduler::get().schedule(&_accum_cell_state1, Window::DimY);
539 NEScheduler::get().schedule(&_activation_cell_state, Window::DimY);
540 NEScheduler::get().schedule(&_pixelwise_mul_cell_state1, Window::DimY);
541 NEScheduler::get().schedule(&_pixelwise_mul_cell_state2, Window::DimY);
542 NEScheduler::get().schedule(&_accum_cell_state2, Window::DimY);
543
544 if(_perform_cell_clipping)
545 {
546 NEScheduler::get().schedule(&_cell_clip, Window::DimY);
547 }
548
549 _fully_connected_output.run();
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100550 if(_run_peephole_opt)
551 {
552 NEScheduler::get().schedule(&_pixelwise_mul_output_state1, Window::DimY);
553 _accum_output2.run();
554 }
555 NEScheduler::get().schedule(&_activation_output, Window::DimY);
556
557 NEScheduler::get().schedule(&_activation_output_state, Window::DimY);
558 NEScheduler::get().schedule(&_pixelwise_mul_output_state2, Window::DimY);
559
560 if(_has_projection_weights)
561 {
562 _fully_connected_output_state.run();
563 if(_perform_projection_clipping)
564 {
565 NEScheduler::get().schedule(&_projection_clip, Window::DimY);
566 }
567 }
568
569 NEScheduler::get().schedule(&_copy_cell_state, Window::DimY);
570 NEScheduler::get().schedule(&_copy_output, Window::DimY);
571
572 _concat_scratch_buffer.run();
John Kesapides917959c2019-02-04 12:37:29 +0000573}
574
575void NELSTMLayer::prepare()
576{
577 if(!_is_prepared)
578 {
John Kesapides917959c2019-02-04 12:37:29 +0000579 _concat_weights_forget_gate.run();
580 if(!_run_cifg_opt)
581 {
582 _concat_weights_input_gate.run();
583 }
584 _concat_weights_output.run();
585 _is_prepared = true;
586 }
587}