blob: 2f14ab9022f3d0b009c3644ac816ae3a4325a571 [file] [log] [blame]
arovir019e53a352018-08-31 15:26:35 +01001//
Teresa Charlin588cbdf2022-01-19 15:55:37 +00002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
arovir019e53a352018-08-31 15:26:35 +01004//
5
6#include "NeonLstmFloatWorkload.hpp"
Les Bellde9011b2018-10-03 10:37:52 +01007#include "NeonWorkloadUtils.hpp"
8
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00009#include "aclCommon/ArmComputeTensorUtils.hpp"
Matteo Martincighe5b8eb92019-11-28 15:45:42 +000010
Matthew Sloyan171214c2020-09-09 09:07:37 +010011#include <armnn/utility/NumericCast.hpp>
12
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000013#include "neon/NeonTensorHandle.hpp"
arovir019e53a352018-08-31 15:26:35 +010014
15namespace armnn
16{
Les Bellde9011b2018-10-03 10:37:52 +010017using namespace armcomputetensorutils;
18
19NeonLstmFloatWorkload::NeonLstmFloatWorkload(const LstmQueueDescriptor &descriptor, const WorkloadInfo &info)
arovir019e53a352018-08-31 15:26:35 +010020 : FloatWorkload<LstmQueueDescriptor>(descriptor, info)
21{
Keith Davis2d0679f2021-08-05 11:35:00 +010022 // Report Profiling Details
23 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonLstmFloatWorkload_Construct",
24 descriptor.m_Parameters,
25 info,
26 this->GetGuid());
27
Les Bellde9011b2018-10-03 10:37:52 +010028 arm_compute::LSTMParams<arm_compute::ITensor> lstm_param;
29
30 // Basic parameters
31 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
32 BuildArmComputeTensor(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights->GetTensorInfo());
33
34 m_InputToCellWeightsTensor = std::make_unique<arm_compute::Tensor>();
35 BuildArmComputeTensor(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights->GetTensorInfo());
36
37 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
38 BuildArmComputeTensor(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights->GetTensorInfo());
39
40 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
41 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
42
43 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::Tensor>();
44 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights->GetTensorInfo());
45
46 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
47 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
48
49 m_ForgetGateBiasTensor = std::make_unique<arm_compute::Tensor>();
50 BuildArmComputeTensor(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias->GetTensorInfo());
51
52 m_CellBiasTensor = std::make_unique<arm_compute::Tensor>();
53 BuildArmComputeTensor(*m_CellBiasTensor, m_Data.m_CellBias->GetTensorInfo());
54
55 m_OutputGateBiasTensor = std::make_unique<arm_compute::Tensor>();
56 BuildArmComputeTensor(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias->GetTensorInfo());
57
58 // for future reference: check the AndroidNN API for the logic here
59 if (!m_Data.m_Parameters.m_CifgEnabled)
60 {
61 m_InputToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
62 BuildArmComputeTensor(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights->GetTensorInfo());
63
64 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
65 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights->GetTensorInfo());
66
67 m_CellToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
68 if (m_Data.m_CellToInputWeights != nullptr)
69 {
70 BuildArmComputeTensor(*m_CellToInputWeightsTensor, m_Data.m_CellToInputWeights->GetTensorInfo());
71 }
72
73 m_InputGateBiasTensor = std::make_unique<arm_compute::Tensor>();
74 BuildArmComputeTensor(*m_InputGateBiasTensor, m_Data.m_InputGateBias->GetTensorInfo());
75
76 lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
77 m_RecurrentToInputWeightsTensor.get(),
78 m_Data.m_CellToInputWeights != nullptr ? m_CellToInputWeightsTensor.get() : nullptr,
79 m_InputGateBiasTensor.get());
80 }
81
82 if (m_Data.m_Parameters.m_ProjectionEnabled)
83 {
84 m_ProjectionWeightsTensor = std::make_unique<arm_compute::Tensor>();
85 BuildArmComputeTensor(*m_ProjectionWeightsTensor, m_Data.m_ProjectionWeights->GetTensorInfo());
86
87 m_ProjectionBiasTensor = std::make_unique<arm_compute::Tensor>();
88 if (m_Data.m_ProjectionBias != nullptr)
89 {
90 BuildArmComputeTensor(*m_ProjectionBiasTensor, m_Data.m_ProjectionBias->GetTensorInfo());
91 }
92
93 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
94 m_Data.m_ProjectionBias != nullptr ? m_ProjectionBiasTensor.get() : nullptr);
95 }
96
97 if (m_Data.m_Parameters.m_PeepholeEnabled)
98 {
99 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
100 BuildArmComputeTensor(*m_CellToForgetWeightsTensor, m_Data.m_CellToForgetWeights->GetTensorInfo());
101
102 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
103 BuildArmComputeTensor(*m_CellToOutputWeightsTensor, m_Data.m_CellToOutputWeights->GetTensorInfo());
104
105 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
106 }
107
Jan Eilersad5293a2019-07-08 09:57:55 +0100108 if (m_Data.m_Parameters.m_LayerNormEnabled)
109 {
110 m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
111 if (!m_Data.m_Parameters.m_CifgEnabled)
112 {
113 BuildArmComputeTensor(*m_InputLayerNormWeightsTensor, m_Data.m_InputLayerNormWeights->GetTensorInfo());
114 }
115
116 m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
117 BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor, m_Data.m_ForgetLayerNormWeights->GetTensorInfo());
118
119 m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
120 BuildArmComputeTensor(*m_CellLayerNormWeightsTensor, m_Data.m_CellLayerNormWeights->GetTensorInfo());
121
122 m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
123 BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor, m_Data.m_OutputLayerNormWeights->GetTensorInfo());
124
125 lstm_param.set_layer_normalization_params(m_Data.m_Parameters.m_CifgEnabled ?
126 nullptr : m_InputLayerNormWeightsTensor.get(),
127 m_ForgetLayerNormWeightsTensor.get(),
128 m_CellLayerNormWeightsTensor.get(),
129 m_OutputLayerNormWeightsTensor.get());
130 }
131
Derek Lambertic81855f2019-06-13 17:34:19 +0100132 const arm_compute::ITensor& input = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
133 const arm_compute::ITensor& output_state_in = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
134 const arm_compute::ITensor& cell_state_in = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
Les Bellde9011b2018-10-03 10:37:52 +0100135
Derek Lambertic81855f2019-06-13 17:34:19 +0100136 arm_compute::ITensor& output_state_out = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[1])->GetTensor();
137 arm_compute::ITensor& cell_state_out = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[2])->GetTensor();
138 arm_compute::ITensor& output = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[3])->GetTensor();
Les Bellde9011b2018-10-03 10:37:52 +0100139
140 // Get the batch_size and the num_units from the cellStateIn dimensions
141 const TensorInfo& inputTensorInfo = info.m_InputTensorInfos[2];
Matthew Sloyan171214c2020-09-09 09:07:37 +0100142 const unsigned int batch_size = armnn::numeric_cast<unsigned int>(inputTensorInfo.GetShape()[0]);
143 const unsigned int num_units = armnn::numeric_cast<unsigned int>(inputTensorInfo.GetShape()[1]);
Les Bellde9011b2018-10-03 10:37:52 +0100144
145 m_ScratchBuffer = std::make_unique<arm_compute::Tensor>();
146 if (m_Data.m_Parameters.m_CifgEnabled)
147 {
Jan Eilersad5293a2019-07-08 09:57:55 +0100148 // 2D tensor with dimensions [num_units * 3, batch_size] with CIFG
Matteo Martincigha65b7ae2018-11-14 12:39:55 +0000149 armnn::TensorInfo scratchBuffer1({ batch_size, num_units * 3 }, DataType::Float32);
Les Bellde9011b2018-10-03 10:37:52 +0100150 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
151 }
152 else
153 {
Jan Eilersad5293a2019-07-08 09:57:55 +0100154 // scratch_buffer [num_units * 4, batch_size] without CIFG
Matteo Martincigha65b7ae2018-11-14 12:39:55 +0000155 armnn::TensorInfo scratchBuffer2({ batch_size, num_units * 4 }, DataType::Float32);
Les Bellde9011b2018-10-03 10:37:52 +0100156 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2);
157 }
158
159 float cell_threshold = m_Data.m_Parameters.m_ClippingThresCell;
160 float projection_threshold = m_Data.m_Parameters.m_ClippingThresProj;
161
162 // for preparing the object for the class ActivationLayerInfo, we need to consider 5 situations
163 arm_compute::ActivationLayerInfo activationLayerInfo;
164 if (m_Data.m_Parameters.m_ActivationFunc == 0)
165 {
166 // no activation, do nothing
167 }
168 else if (m_Data.m_Parameters.m_ActivationFunc == 1)
169 {
170 activationLayerInfo = arm_compute::ActivationLayerInfo(
171 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
172 }
173 else if (m_Data.m_Parameters.m_ActivationFunc == 3)
174 {
175 activationLayerInfo = arm_compute::ActivationLayerInfo(
176 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
177 }
178 else if (m_Data.m_Parameters.m_ActivationFunc == 4)
179 {
180 activationLayerInfo = arm_compute::ActivationLayerInfo(
181 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
182 }
183 else if (m_Data.m_Parameters.m_ActivationFunc == 6)
184 {
185 activationLayerInfo = arm_compute::ActivationLayerInfo(
186 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
187 }
188 else
189 {
190 throw armnn::Exception("Wrong Type of Activation Function!");
191 }
192
193
194 m_LstmLayer.configure(&input, m_InputToForgetWeightsTensor.get(), m_InputToCellWeightsTensor.get(),
195 m_InputToOutputWeightsTensor.get(), m_RecurrentToForgetWeightsTensor.get(),
196 m_RecurrentToCellWeightsTensor.get(), m_RecurrentToOutputWeightsTensor.get(),
197 m_ForgetGateBiasTensor.get(), m_CellBiasTensor.get(), m_OutputGateBiasTensor.get(),
198 &output_state_in, &cell_state_in, m_ScratchBuffer.get(), &output_state_out,
199 &cell_state_out, &output, lstm_param, activationLayerInfo,
200 cell_threshold, projection_threshold);
201
202 armcomputetensorutils::InitialiseArmComputeTensorEmpty(*m_ScratchBuffer);
203
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +0100204 InitializeArmComputeTensorData(*m_InputToForgetWeightsTensor,
205 m_Data.m_InputToForgetWeights);
206 InitializeArmComputeTensorData(*m_InputToCellWeightsTensor,
207 m_Data.m_InputToCellWeights);
208 InitializeArmComputeTensorData(*m_InputToOutputWeightsTensor,
209 m_Data.m_InputToOutputWeights);
210 InitializeArmComputeTensorData(*m_RecurrentToForgetWeightsTensor,
211 m_Data.m_RecurrentToForgetWeights);
212 InitializeArmComputeTensorData(*m_RecurrentToCellWeightsTensor,
213 m_Data.m_RecurrentToCellWeights);
214 InitializeArmComputeTensorData(*m_RecurrentToOutputWeightsTensor,
215 m_Data.m_RecurrentToOutputWeights);
216 InitializeArmComputeTensorData(*m_ForgetGateBiasTensor,
217 m_Data.m_ForgetGateBias);
218 InitializeArmComputeTensorData(*m_CellBiasTensor,
219 m_Data.m_CellBias);
220 InitializeArmComputeTensorData(*m_OutputGateBiasTensor,
221 m_Data.m_OutputGateBias);
Les Bellde9011b2018-10-03 10:37:52 +0100222
223 if (!m_Data.m_Parameters.m_CifgEnabled)
224 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +0100225 InitializeArmComputeTensorData(*m_InputToInputWeightsTensor,
226 m_Data.m_InputToInputWeights);
227 InitializeArmComputeTensorData(*m_RecurrentToInputWeightsTensor,
228 m_Data.m_RecurrentToInputWeights);
Les Bellde9011b2018-10-03 10:37:52 +0100229 if (m_Data.m_CellToInputWeights != nullptr)
230 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +0100231 InitializeArmComputeTensorData(*m_CellToInputWeightsTensor,
232 m_Data.m_CellToInputWeights);
Les Bellde9011b2018-10-03 10:37:52 +0100233 }
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +0100234 InitializeArmComputeTensorData(*m_InputGateBiasTensor,
235 m_Data.m_InputGateBias);
Les Bellde9011b2018-10-03 10:37:52 +0100236 }
237
238 if (m_Data.m_Parameters.m_ProjectionEnabled)
239 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +0100240 InitializeArmComputeTensorData(*m_ProjectionWeightsTensor,
241 m_Data.m_ProjectionWeights);
Les Bellde9011b2018-10-03 10:37:52 +0100242 if (m_Data.m_ProjectionBias != nullptr)
243 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +0100244 InitializeArmComputeTensorData(*m_ProjectionBiasTensor,
245 m_Data.m_ProjectionBias);
Les Bellde9011b2018-10-03 10:37:52 +0100246 }
247 }
248
249 if (m_Data.m_Parameters.m_PeepholeEnabled)
250 {
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +0100251 InitializeArmComputeTensorData(*m_CellToForgetWeightsTensor,
252 m_Data.m_CellToForgetWeights);
253 InitializeArmComputeTensorData(*m_CellToOutputWeightsTensor,
254 m_Data.m_CellToOutputWeights);
Les Bellde9011b2018-10-03 10:37:52 +0100255 }
256
Jan Eilersad5293a2019-07-08 09:57:55 +0100257 if (m_Data.m_Parameters.m_LayerNormEnabled)
258 {
259 if (!m_Data.m_Parameters.m_CifgEnabled)
260 {
261 InitializeArmComputeTensorData(*m_InputLayerNormWeightsTensor, m_Data.m_InputLayerNormWeights);
262 }
263 InitializeArmComputeTensorData(*m_ForgetLayerNormWeightsTensor, m_Data.m_ForgetLayerNormWeights);
264 InitializeArmComputeTensorData(*m_CellLayerNormWeightsTensor, m_Data.m_CellLayerNormWeights);
265 InitializeArmComputeTensorData(*m_OutputLayerNormWeightsTensor, m_Data.m_OutputLayerNormWeights);
266 }
267
Les Bellde9011b2018-10-03 10:37:52 +0100268 // Force Compute Library to perform the necessary copying and reshaping, after which
269 // delete all the input tensors that will no longer be needed
270 m_LstmLayer.prepare();
271 FreeUnusedTensors();
arovir019e53a352018-08-31 15:26:35 +0100272}
273
274void NeonLstmFloatWorkload::Execute() const
275{
Keith Davis2d0679f2021-08-05 11:35:00 +0100276 ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonLstmFloatWorkload_Execute", this->GetGuid());
Les Bellde9011b2018-10-03 10:37:52 +0100277 m_LstmLayer.run();
arovir019e53a352018-08-31 15:26:35 +0100278}
279
Les Bellde9011b2018-10-03 10:37:52 +0100280arm_compute::Status NeonLstmFloatWorkloadValidate(const TensorInfo& input,
281 const TensorInfo& outputStateIn,
282 const TensorInfo& cellStateIn,
283 const TensorInfo& scratchBuffer,
284 const TensorInfo& outputStateOut,
285 const TensorInfo& cellStateOut,
286 const TensorInfo& output,
287 const LstmDescriptor& descriptor,
Jan Eilersad5293a2019-07-08 09:57:55 +0100288 const LstmInputParamsInfo& paramsInfo)
Les Bellde9011b2018-10-03 10:37:52 +0100289{
290 arm_compute::LSTMParams<arm_compute::ITensorInfo> lstm_params_info;
291
Jan Eilersad5293a2019-07-08 09:57:55 +0100292 // The inputs and outputs
Les Bellde9011b2018-10-03 10:37:52 +0100293 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
294 const arm_compute::TensorInfo aclOutputStateInInfo = BuildArmComputeTensorInfo(outputStateIn);
295 const arm_compute::TensorInfo aclCellStateInInfo = BuildArmComputeTensorInfo(cellStateIn);
296 const arm_compute::TensorInfo aclScratchBufferInfo = BuildArmComputeTensorInfo(scratchBuffer);
297 const arm_compute::TensorInfo aclOutputStateOutInfo = BuildArmComputeTensorInfo(outputStateOut);
298 const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut);
299 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
300
301 // Basic parameters
Jan Eilersad5293a2019-07-08 09:57:55 +0100302 const arm_compute::TensorInfo aclInputToForgetWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100303 = BuildArmComputeTensorInfo(paramsInfo.GetInputToForgetWeights());
Jan Eilersad5293a2019-07-08 09:57:55 +0100304 const arm_compute::TensorInfo aclInputToCellWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100305 = BuildArmComputeTensorInfo(paramsInfo.GetInputToCellWeights());
Jan Eilersad5293a2019-07-08 09:57:55 +0100306 const arm_compute::TensorInfo aclInputToOutputWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100307 = BuildArmComputeTensorInfo(paramsInfo.GetInputToOutputWeights());
Les Bellde9011b2018-10-03 10:37:52 +0100308 const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100309 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToForgetWeights());
Les Bellde9011b2018-10-03 10:37:52 +0100310 const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100311 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToCellWeights());
Les Bellde9011b2018-10-03 10:37:52 +0100312 const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100313 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToOutputWeights());
Jan Eilersad5293a2019-07-08 09:57:55 +0100314 const arm_compute::TensorInfo aclForgetGateBiasInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100315 = BuildArmComputeTensorInfo(paramsInfo.GetForgetGateBias());
Jan Eilersad5293a2019-07-08 09:57:55 +0100316 const arm_compute::TensorInfo aclCellBiasInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100317 = BuildArmComputeTensorInfo(paramsInfo.GetCellBias());
Jan Eilersad5293a2019-07-08 09:57:55 +0100318 const arm_compute::TensorInfo aclOutputGateBiasInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100319 = BuildArmComputeTensorInfo(paramsInfo.GetOutputGateBias());
Les Bellde9011b2018-10-03 10:37:52 +0100320
321 arm_compute::TensorInfo aclInputToInputWeightsInfo;
322 arm_compute::TensorInfo aclRecurrentToInputWeightsInfo;
323 arm_compute::TensorInfo aclCellToInputWeightsInfo;
324 arm_compute::TensorInfo aclInputGateBiasInfo;
325 arm_compute::TensorInfo aclProjectionWeightsInfo;
326 arm_compute::TensorInfo aclProjectionBiasInfo;
327 arm_compute::TensorInfo aclCellToForgetWeightsInfo;
328 arm_compute::TensorInfo aclCellToOutputWeightsInfo;
329
Jan Eilersad5293a2019-07-08 09:57:55 +0100330 arm_compute::TensorInfo aclInputLayerNormWeightsInfo;
331 arm_compute::TensorInfo aclForgetLayerNormWeightsInfo;
332 arm_compute::TensorInfo aclCellLayerNormWeightsInfo;
333 arm_compute::TensorInfo aclOutputLayerNormWeightsInfo;
334
335
Les Bellde9011b2018-10-03 10:37:52 +0100336 if (!descriptor.m_CifgEnabled)
337 {
Jan Eilersad5293a2019-07-08 09:57:55 +0100338 if (descriptor.m_PeepholeEnabled)
Les Bellde9011b2018-10-03 10:37:52 +0100339 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100340 aclCellToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellToInputWeights());
Les Bellde9011b2018-10-03 10:37:52 +0100341 }
Francis Murtaghbb590b42019-08-14 09:51:36 +0100342 aclInputToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputToInputWeights());
343 aclRecurrentToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToInputWeights());
344 aclInputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputGateBias());
Jan Eilersad5293a2019-07-08 09:57:55 +0100345
Les Bellde9011b2018-10-03 10:37:52 +0100346 lstm_params_info.set_cifg_params(&aclInputToInputWeightsInfo, &aclRecurrentToInputWeightsInfo,
Jan Eilersad5293a2019-07-08 09:57:55 +0100347 descriptor.m_PeepholeEnabled ? &aclCellToInputWeightsInfo : nullptr,
Les Bellde9011b2018-10-03 10:37:52 +0100348 &aclInputGateBiasInfo);
349 }
350
351 if (descriptor.m_ProjectionEnabled)
352 {
Jan Eilersad5293a2019-07-08 09:57:55 +0100353 if (paramsInfo.m_ProjectionBias != nullptr)
Les Bellde9011b2018-10-03 10:37:52 +0100354 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100355 aclProjectionBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetProjectionBias());
Les Bellde9011b2018-10-03 10:37:52 +0100356 }
Francis Murtaghbb590b42019-08-14 09:51:36 +0100357 aclProjectionWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetProjectionWeights());
Jan Eilersad5293a2019-07-08 09:57:55 +0100358
Les Bellde9011b2018-10-03 10:37:52 +0100359 lstm_params_info.set_projection_params(&aclProjectionWeightsInfo,
Jan Eilersad5293a2019-07-08 09:57:55 +0100360 paramsInfo.m_ProjectionBias != nullptr ?
361 &aclProjectionBiasInfo : nullptr);
Les Bellde9011b2018-10-03 10:37:52 +0100362 }
363
364 if (descriptor.m_PeepholeEnabled)
365 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100366 aclCellToForgetWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellToForgetWeights());
367 aclCellToOutputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellToOutputWeights());
Jan Eilersad5293a2019-07-08 09:57:55 +0100368
Les Bellde9011b2018-10-03 10:37:52 +0100369 lstm_params_info.set_peephole_params(&aclCellToForgetWeightsInfo, &aclCellToOutputWeightsInfo);
370 }
371
Jan Eilersad5293a2019-07-08 09:57:55 +0100372 if (descriptor.m_LayerNormEnabled)
373 {
374 if (!descriptor.m_CifgEnabled)
375 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100376 aclInputLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputLayerNormWeights());
Jan Eilersad5293a2019-07-08 09:57:55 +0100377 }
Francis Murtaghbb590b42019-08-14 09:51:36 +0100378 aclForgetLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetForgetLayerNormWeights());
379 aclCellLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellLayerNormWeights());
380 aclOutputLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetOutputLayerNormWeights());
Jan Eilersad5293a2019-07-08 09:57:55 +0100381
382 lstm_params_info.set_layer_normalization_params(descriptor.m_CifgEnabled ?
383 nullptr : &aclInputLayerNormWeightsInfo,
384 &aclForgetLayerNormWeightsInfo,
385 &aclCellLayerNormWeightsInfo,
386 &aclOutputLayerNormWeightsInfo);
387 }
388
Les Bellde9011b2018-10-03 10:37:52 +0100389 float cell_threshold = descriptor.m_ClippingThresCell;
390 float projection_threshold = descriptor.m_ClippingThresProj;
391
392 // for preparing the object for the class ActivationLayerInfo, we need to consider 5 situations
393 arm_compute::ActivationLayerInfo activationLayerInfo;
394 switch (descriptor.m_ActivationFunc)
395 {
396 case 0:
397 // no activation, do nothing
398 break;
399 case 1:
400 activationLayerInfo = arm_compute::ActivationLayerInfo(
401 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
402 break;
403 case 3:
404 activationLayerInfo = arm_compute::ActivationLayerInfo(
405 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
406 break;
407 case 4:
408 activationLayerInfo = arm_compute::ActivationLayerInfo(
409 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
410 break;
411 case 6:
412 activationLayerInfo = arm_compute::ActivationLayerInfo(
413 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
414 break;
415 default:
416 throw armnn::Exception("Wrong Type of Activation Function!");
417 }
418
419 return arm_compute::NELSTMLayer::validate(&aclInputInfo,
420 &aclInputToForgetWeightsInfo,
421 &aclInputToCellWeightsInfo,
422 &aclInputToOutputWeightsInfo,
423 &aclRecurrentToForgetWeightsInfo,
424 &aclRecurrentToCellWeightsInfo,
425 &aclRecurrentToOutputWeightsInfo,
426 &aclForgetGateBiasInfo,
427 &aclCellBiasInfo,
428 &aclOutputGateBiasInfo,
429 &aclOutputStateInInfo,
430 &aclCellStateInInfo,
431 &aclScratchBufferInfo,
432 &aclOutputStateOutInfo,
433 &aclCellStateOutInfo,
434 &aclOutputInfo,
435 lstm_params_info,
436 activationLayerInfo,
437 cell_threshold,
438 projection_threshold);
439}
440
441void NeonLstmFloatWorkload::FreeUnusedTensors()
442{
443 FreeTensorIfUnused(m_InputToInputWeightsTensor);
444 FreeTensorIfUnused(m_InputToForgetWeightsTensor);
445 FreeTensorIfUnused(m_InputToCellWeightsTensor);
446 FreeTensorIfUnused(m_InputToOutputWeightsTensor);
447 FreeTensorIfUnused(m_RecurrentToInputWeightsTensor);
448 FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor);
449 FreeTensorIfUnused(m_RecurrentToCellWeightsTensor);
450 FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor);
451 FreeTensorIfUnused(m_CellToInputWeightsTensor);
452 FreeTensorIfUnused(m_CellToForgetWeightsTensor);
453 FreeTensorIfUnused(m_CellToOutputWeightsTensor);
454 FreeTensorIfUnused(m_InputGateBiasTensor);
455 FreeTensorIfUnused(m_ForgetGateBiasTensor);
456 FreeTensorIfUnused(m_CellBiasTensor);
457 FreeTensorIfUnused(m_OutputGateBiasTensor);
458 FreeTensorIfUnused(m_ProjectionWeightsTensor);
459 FreeTensorIfUnused(m_ProjectionBiasTensor);
460 FreeTensorIfUnused(m_ScratchBuffer);
Jan Eilersad5293a2019-07-08 09:57:55 +0100461 FreeTensorIfUnused(m_InputLayerNormWeightsTensor);
462 FreeTensorIfUnused(m_ForgetLayerNormWeightsTensor);
463 FreeTensorIfUnused(m_CellLayerNormWeightsTensor);
464 FreeTensorIfUnused(m_OutputLayerNormWeightsTensor);
Les Bellde9011b2018-10-03 10:37:52 +0100465}
466
Kevin May998a2082022-03-02 12:11:31 +0000467void NeonLstmFloatWorkload::ReplaceInputTensorHandle(ITensorHandle* tensorHandle, unsigned int slot)
468{
469 ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot];
470 this->m_Data.m_Inputs[slot] = tensorHandle;
471 try
472 {
473 Reconfigure();
474 }
475 catch(armnn::UnimplementedException& e)
476 {
477 // Cannot reconfigure, revert the slot back and throw the exception.
478 this->m_Data.m_Inputs[slot] = backupHandle;
479 throw e;
480 }
481}
482
483// Replace output tensor handle with the given TensorHandle
484void NeonLstmFloatWorkload::ReplaceOutputTensorHandle(ITensorHandle* tensorHandle, unsigned int slot)
485{
486 ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot];
487 this->m_Data.m_Inputs[slot] = tensorHandle;
488 try
489 {
490 Reconfigure();
491 }
492 catch(armnn::UnimplementedException& e)
493 {
494 // Cannot reconfigure, revert the slot back and throw the exception.
495 this->m_Data.m_Inputs[slot] = backupHandle;
496 throw e;
497 }
498}
499
500void NeonLstmFloatWorkload::Reconfigure()
501{
502 throw armnn::UnimplementedException("Reconfigure not implemented for this workload");
503}
504
Les Bellde9011b2018-10-03 10:37:52 +0100505} //namespace armnn