blob: c7b644c46ab1fa016dc02dfd386c68ba80b8f399 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5
arovir019e53a352018-08-31 15:26:35 +01006#include "ClLstmFloatWorkload.hpp"
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00007#include <cl/ClTensorHandle.hpp>
Colm Donelan0c479742021-12-10 12:43:54 +00008#include <armnn/backends/TensorHandle.hpp>
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00009#include <cl/ClLayerSupport.hpp>
10#include <aclCommon/ArmComputeTensorUtils.hpp>
Matthew Bentham14e46692018-09-20 15:35:30 +010011
Matthew Sloyan171214c2020-09-09 09:07:37 +010012#include <armnn/utility/NumericCast.hpp>
13
Matthew Bentham14e46692018-09-20 15:35:30 +010014#include <arm_compute/runtime/CL/functions/CLLSTMLayer.h>
15
16#include "ClWorkloadUtils.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +010017
18namespace armnn
19{
20using namespace armcomputetensorutils;
21
Sadik Armagane9444752020-12-02 11:28:58 +000022ClLstmFloatWorkload::ClLstmFloatWorkload(const LstmQueueDescriptor &descriptor,
23 const WorkloadInfo &info,
24 const arm_compute::CLCompileContext& clCompileContext)
telsoa01c577f2c2018-08-31 09:22:23 +010025 : FloatWorkload<LstmQueueDescriptor>(descriptor, info)
26{
Keith Davisbcd860a2021-08-05 14:20:33 +010027 // Report Profiling Details
28 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClLstmFloatWorkload_Construct",
29 descriptor.m_Parameters,
30 info,
31 this->GetGuid());
32
telsoa01c577f2c2018-08-31 09:22:23 +010033 arm_compute::LSTMParams<arm_compute::ICLTensor> lstm_param;
34
35 // Basic parameters
36 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
37 BuildArmComputeTensor(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights->GetTensorInfo());
38
39 m_InputToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
40 BuildArmComputeTensor(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights->GetTensorInfo());
41
42 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
43 BuildArmComputeTensor(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights->GetTensorInfo());
44
45 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
46 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
47
48 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
49 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights->GetTensorInfo());
50
51 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
52 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
53
54 m_ForgetGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
55 BuildArmComputeTensor(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias->GetTensorInfo());
56
57 m_CellBiasTensor = std::make_unique<arm_compute::CLTensor>();
58 BuildArmComputeTensor(*m_CellBiasTensor, m_Data.m_CellBias->GetTensorInfo());
59
60 m_OutputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
61 BuildArmComputeTensor(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias->GetTensorInfo());
62
63 // for future reference: check the AndroidNN API for the logic here
64 if (!m_Data.m_Parameters.m_CifgEnabled)
65 {
66 m_InputToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
67 BuildArmComputeTensor(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights->GetTensorInfo());
68
69 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
70 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights->GetTensorInfo());
71
72 m_CellToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
73 if (m_Data.m_CellToInputWeights != nullptr)
74 {
75 BuildArmComputeTensor(*m_CellToInputWeightsTensor, m_Data.m_CellToInputWeights->GetTensorInfo());
76 }
77
78 m_InputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
79 BuildArmComputeTensor(*m_InputGateBiasTensor, m_Data.m_InputGateBias->GetTensorInfo());
80
81 lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
82 m_RecurrentToInputWeightsTensor.get(),
83 m_Data.m_CellToInputWeights != nullptr ? m_CellToInputWeightsTensor.get() : nullptr,
84 m_InputGateBiasTensor.get());
85 }
86
87 if (m_Data.m_Parameters.m_ProjectionEnabled)
88 {
89 m_ProjectionWeightsTensor = std::make_unique<arm_compute::CLTensor>();
90 BuildArmComputeTensor(*m_ProjectionWeightsTensor, m_Data.m_ProjectionWeights->GetTensorInfo());
91
92 m_ProjectionBiasTensor = std::make_unique<arm_compute::CLTensor>();
93 if (m_Data.m_ProjectionBias != nullptr)
94 {
95 BuildArmComputeTensor(*m_ProjectionBiasTensor, m_Data.m_ProjectionBias->GetTensorInfo());
96 }
97
98 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
99 m_Data.m_ProjectionBias != nullptr ? m_ProjectionBiasTensor.get() : nullptr);
100 }
101
102 if (m_Data.m_Parameters.m_PeepholeEnabled)
103 {
104 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
105 BuildArmComputeTensor(*m_CellToForgetWeightsTensor, m_Data.m_CellToForgetWeights->GetTensorInfo());
106
107 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
108 BuildArmComputeTensor(*m_CellToOutputWeightsTensor, m_Data.m_CellToOutputWeights->GetTensorInfo());
109
110 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
111 }
112
Jan Eilersa2ec9092019-07-08 15:56:59 +0100113 if (m_Data.m_Parameters.m_LayerNormEnabled)
114 {
115 m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
116 m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
117 m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
118 m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
119
120 if (!m_Data.m_Parameters.m_CifgEnabled)
121 {
122 BuildArmComputeTensor(*m_InputLayerNormWeightsTensor, m_Data.m_InputLayerNormWeights->GetTensorInfo());
123 }
124 BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor, m_Data.m_ForgetLayerNormWeights->GetTensorInfo());
125 BuildArmComputeTensor(*m_CellLayerNormWeightsTensor, m_Data.m_CellLayerNormWeights->GetTensorInfo());
126 BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor, m_Data.m_OutputLayerNormWeights->GetTensorInfo());
127
128 lstm_param.set_layer_normalization_params(m_Data.m_Parameters.m_CifgEnabled ? nullptr :
129 m_InputLayerNormWeightsTensor.get(),
130 m_ForgetLayerNormWeightsTensor.get(),
131 m_CellLayerNormWeightsTensor.get(),
132 m_OutputLayerNormWeightsTensor.get());
133 }
134
telsoa01c577f2c2018-08-31 09:22:23 +0100135 const arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
136 const arm_compute::ICLTensor& output_state_in = static_cast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
Teresa Charlin57512af2020-07-29 12:06:40 +0100137 arm_compute::ICLTensor& cell_state_in = static_cast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
telsoa01c577f2c2018-08-31 09:22:23 +0100138
139 arm_compute::ICLTensor& output_state_out = static_cast<IClTensorHandle*>(m_Data.m_Outputs[1])->GetTensor();
140 arm_compute::ICLTensor& cell_state_out = static_cast<IClTensorHandle*>(m_Data.m_Outputs[2])->GetTensor();
141 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[3])->GetTensor();
142
143 // Get the batch_size and the num_units from the cellStateIn dimensions
144 const TensorInfo& inputTensorInfo = info.m_InputTensorInfos[2];
Matthew Sloyan171214c2020-09-09 09:07:37 +0100145 const unsigned int batch_size = armnn::numeric_cast<unsigned int>(inputTensorInfo.GetShape()[0]);
146 const unsigned int num_units = armnn::numeric_cast<unsigned int>(inputTensorInfo.GetShape()[1]);
telsoa01c577f2c2018-08-31 09:22:23 +0100147
148 m_ScratchBuffer = std::make_unique<arm_compute::CLTensor>();
149 if (m_Data.m_Parameters.m_CifgEnabled)
150 {
Matteo Martincigha65b7ae2018-11-14 12:39:55 +0000151 // 2D tensor with dimensions [num_units * 3, batch_size] with CIFG
152 armnn::TensorInfo scratchBuffer1({ batch_size, num_units * 3 }, DataType::Float32);
telsoa01c577f2c2018-08-31 09:22:23 +0100153 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
154 }
155 else
156 {
Matteo Martincigha65b7ae2018-11-14 12:39:55 +0000157 // scratch_buffer [num_units * 4, batch_size] without CIFG
158 armnn::TensorInfo scratchBuffer2({ batch_size, num_units * 4 }, DataType::Float32);
telsoa01c577f2c2018-08-31 09:22:23 +0100159 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2);
160 }
161
162 float cell_threshold = m_Data.m_Parameters.m_ClippingThresCell;
163 float projection_threshold = m_Data.m_Parameters.m_ClippingThresProj;
164
165 // for preparing the object for the class ActivationLayerInfo, we need to consider 5 situations
166 arm_compute::ActivationLayerInfo activationLayerInfo;
167 if (m_Data.m_Parameters.m_ActivationFunc == 0)
168 {
169 // no activation, do nothing
170 }
171 else if (m_Data.m_Parameters.m_ActivationFunc == 1)
172 {
173 activationLayerInfo = arm_compute::ActivationLayerInfo(
174 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
175 }
176 else if (m_Data.m_Parameters.m_ActivationFunc == 3)
177 {
178 activationLayerInfo = arm_compute::ActivationLayerInfo(
179 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
180 }
181 else if (m_Data.m_Parameters.m_ActivationFunc == 4)
182 {
183 activationLayerInfo = arm_compute::ActivationLayerInfo(
184 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
185 }
186 else if (m_Data.m_Parameters.m_ActivationFunc == 6)
187 {
188 activationLayerInfo = arm_compute::ActivationLayerInfo(
189 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
190 }
191 else
192 {
193 throw armnn::Exception("Wrong Type of Activation Function!");
194 }
195
Kevin May9f6862d2021-10-22 15:42:28 +0100196 {
197 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClLstmFloatWorkload_configure");
198 m_LstmLayer.configure(clCompileContext, &input, m_InputToForgetWeightsTensor.get(),
199 m_InputToCellWeightsTensor.get(), m_InputToOutputWeightsTensor.get(),
200 m_RecurrentToForgetWeightsTensor.get(), m_RecurrentToCellWeightsTensor.get(),
201 m_RecurrentToOutputWeightsTensor.get(), m_ForgetGateBiasTensor.get(),
202 m_CellBiasTensor.get(), m_OutputGateBiasTensor.get(), &output_state_in,
203 &cell_state_in, m_ScratchBuffer.get(), &output_state_out,
204 &cell_state_out, &output, lstm_param, activationLayerInfo,
205 cell_threshold, projection_threshold);
206 }
telsoa01c577f2c2018-08-31 09:22:23 +0100207
208 armcomputetensorutils::InitialiseArmComputeTensorEmpty(*m_ScratchBuffer);
209
Jan Eilersa2ec9092019-07-08 15:56:59 +0100210 InitializeArmComputeClTensorData(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights);
211 InitializeArmComputeClTensorData(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights);
212 InitializeArmComputeClTensorData(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights);
Matthew Bentham785df502018-09-21 10:29:58 +0100213 InitializeArmComputeClTensorData(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights);
Jan Eilersa2ec9092019-07-08 15:56:59 +0100214 InitializeArmComputeClTensorData(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights);
Matthew Bentham785df502018-09-21 10:29:58 +0100215 InitializeArmComputeClTensorData(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights);
Jan Eilersa2ec9092019-07-08 15:56:59 +0100216 InitializeArmComputeClTensorData(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias);
217 InitializeArmComputeClTensorData(*m_CellBiasTensor, m_Data.m_CellBias);
218 InitializeArmComputeClTensorData(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias);
telsoa01c577f2c2018-08-31 09:22:23 +0100219
220 if (!m_Data.m_Parameters.m_CifgEnabled)
221 {
Matthew Bentham785df502018-09-21 10:29:58 +0100222 InitializeArmComputeClTensorData(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights);
223 InitializeArmComputeClTensorData(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights);
telsoa01c577f2c2018-08-31 09:22:23 +0100224 if (m_Data.m_CellToInputWeights != nullptr)
225 {
Matthew Bentham785df502018-09-21 10:29:58 +0100226 InitializeArmComputeClTensorData(*m_CellToInputWeightsTensor, m_Data.m_CellToInputWeights);
telsoa01c577f2c2018-08-31 09:22:23 +0100227 }
Matthew Bentham785df502018-09-21 10:29:58 +0100228 InitializeArmComputeClTensorData(*m_InputGateBiasTensor, m_Data.m_InputGateBias);
telsoa01c577f2c2018-08-31 09:22:23 +0100229 }
230
231 if (m_Data.m_Parameters.m_ProjectionEnabled)
232 {
Matthew Bentham785df502018-09-21 10:29:58 +0100233 InitializeArmComputeClTensorData(*m_ProjectionWeightsTensor, m_Data.m_ProjectionWeights);
telsoa01c577f2c2018-08-31 09:22:23 +0100234 if (m_Data.m_ProjectionBias != nullptr)
235 {
Matthew Bentham785df502018-09-21 10:29:58 +0100236 InitializeArmComputeClTensorData(*m_ProjectionBiasTensor, m_Data.m_ProjectionBias);
telsoa01c577f2c2018-08-31 09:22:23 +0100237 }
238 }
239
240 if (m_Data.m_Parameters.m_PeepholeEnabled)
241 {
Matthew Bentham785df502018-09-21 10:29:58 +0100242 InitializeArmComputeClTensorData(*m_CellToForgetWeightsTensor, m_Data.m_CellToForgetWeights);
243 InitializeArmComputeClTensorData(*m_CellToOutputWeightsTensor, m_Data.m_CellToOutputWeights);
telsoa01c577f2c2018-08-31 09:22:23 +0100244 }
245
Jan Eilersa2ec9092019-07-08 15:56:59 +0100246 if (m_Data.m_Parameters.m_LayerNormEnabled)
247 {
248 if (!m_Data.m_Parameters.m_CifgEnabled)
249 {
250 InitializeArmComputeClTensorData(*m_InputLayerNormWeightsTensor, m_Data.m_InputLayerNormWeights);
251 }
252
253 InitializeArmComputeClTensorData(*m_ForgetLayerNormWeightsTensor, m_Data.m_ForgetLayerNormWeights);
254 InitializeArmComputeClTensorData(*m_CellLayerNormWeightsTensor, m_Data.m_CellLayerNormWeights);
255 InitializeArmComputeClTensorData(*m_OutputLayerNormWeightsTensor, m_Data.m_OutputLayerNormWeights);
256 }
257
telsoa01c577f2c2018-08-31 09:22:23 +0100258 // Force Compute Library to perform the necessary copying and reshaping, after which
259 // delete all the input tensors that will no longer be needed
260 m_LstmLayer.prepare();
261 FreeUnusedTensors();
262}
263
arovir019e53a352018-08-31 15:26:35 +0100264void ClLstmFloatWorkload::Execute() const
telsoa01c577f2c2018-08-31 09:22:23 +0100265{
Keith Davisbcd860a2021-08-05 14:20:33 +0100266 ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClLstmFloatWorkload_Execute", this->GetGuid());
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100267 RunClFunction(m_LstmLayer, CHECK_LOCATION());
telsoa01c577f2c2018-08-31 09:22:23 +0100268}
269
arovir019e53a352018-08-31 15:26:35 +0100270arm_compute::Status ClLstmFloatWorkloadValidate(const TensorInfo& input, const TensorInfo& outputStateIn,
271 const TensorInfo& cellStateIn, const TensorInfo& scratchBuffer,
272 const TensorInfo& outputStateOut, const TensorInfo& cellStateOut,
273 const TensorInfo& output, const LstmDescriptor& descriptor,
Jan Eilersd01a83c2019-07-03 18:20:40 +0100274 const LstmInputParamsInfo& paramsInfo)
telsoa01c577f2c2018-08-31 09:22:23 +0100275{
276 arm_compute::LSTMParams<arm_compute::ITensorInfo> lstm_params_info;
277
278 // The inputs and the outputs
279 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
280 const arm_compute::TensorInfo aclOutputStateInInfo = BuildArmComputeTensorInfo(outputStateIn);
281 const arm_compute::TensorInfo aclCellStateInInfo = BuildArmComputeTensorInfo(cellStateIn);
282 const arm_compute::TensorInfo aclScratchBufferInfo = BuildArmComputeTensorInfo(scratchBuffer);
283 const arm_compute::TensorInfo aclOutputStateOutInfo = BuildArmComputeTensorInfo(outputStateOut);
284 const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut);
285 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
286
287 // Basic parameters
Jan Eilersd01a83c2019-07-03 18:20:40 +0100288 const arm_compute::TensorInfo aclInputToForgetWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100289 = BuildArmComputeTensorInfo(paramsInfo.GetInputToForgetWeights());
Jan Eilersd01a83c2019-07-03 18:20:40 +0100290 const arm_compute::TensorInfo aclInputToCellWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100291 = BuildArmComputeTensorInfo(paramsInfo.GetInputToCellWeights());
Jan Eilersd01a83c2019-07-03 18:20:40 +0100292 const arm_compute::TensorInfo aclInputToOutputWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100293 = BuildArmComputeTensorInfo(paramsInfo.GetInputToOutputWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100294 const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100295 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToForgetWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100296 const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100297 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToCellWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100298 const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100299 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToOutputWeights());
300 const arm_compute::TensorInfo aclForgetGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetForgetGateBias());
301 const arm_compute::TensorInfo aclCellBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellBias());
302 const arm_compute::TensorInfo aclOutputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetOutputGateBias());
telsoa01c577f2c2018-08-31 09:22:23 +0100303
304 arm_compute::TensorInfo aclInputToInputWeightsInfo;
305 arm_compute::TensorInfo aclRecurrentToInputWeightsInfo;
306 arm_compute::TensorInfo aclCellToInputWeightsInfo;
307 arm_compute::TensorInfo aclInputGateBiasInfo;
308 arm_compute::TensorInfo aclProjectionWeightsInfo;
309 arm_compute::TensorInfo aclProjectionBiasInfo;
310 arm_compute::TensorInfo aclCellToForgetWeightsInfo;
311 arm_compute::TensorInfo aclCellToOutputWeightsInfo;
Jan Eilersa2ec9092019-07-08 15:56:59 +0100312 arm_compute::TensorInfo aclInputLayerNormWeightsInfo;
313 arm_compute::TensorInfo aclForgetLayerNormWeightsInfo;
314 arm_compute::TensorInfo aclCellLayerNormWeightsInfo;
315 arm_compute::TensorInfo aclOutputLayerNormWeightsInfo;
telsoa01c577f2c2018-08-31 09:22:23 +0100316
317 if (!descriptor.m_CifgEnabled)
318 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100319 aclInputToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputToInputWeights());
320 aclRecurrentToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToInputWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100321
Jan Eilersd01a83c2019-07-03 18:20:40 +0100322 if (paramsInfo.m_CellToInputWeights != nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +0100323 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100324 aclCellToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellToInputWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100325 }
Francis Murtaghbb590b42019-08-14 09:51:36 +0100326 aclInputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputGateBias());
telsoa01c577f2c2018-08-31 09:22:23 +0100327 lstm_params_info.set_cifg_params(&aclInputToInputWeightsInfo, &aclRecurrentToInputWeightsInfo,
Jan Eilersd01a83c2019-07-03 18:20:40 +0100328 paramsInfo.m_CellToInputWeights != nullptr ?
329 &aclCellToInputWeightsInfo: nullptr,
telsoa01c577f2c2018-08-31 09:22:23 +0100330 &aclInputGateBiasInfo);
331 }
332
333 if (descriptor.m_ProjectionEnabled)
334 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100335 aclProjectionWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetProjectionWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100336
Jan Eilersd01a83c2019-07-03 18:20:40 +0100337 if (paramsInfo.m_ProjectionBias != nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +0100338 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100339 aclProjectionBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputGateBias());
telsoa01c577f2c2018-08-31 09:22:23 +0100340 }
341 lstm_params_info.set_projection_params(&aclProjectionWeightsInfo,
Jan Eilersd01a83c2019-07-03 18:20:40 +0100342 paramsInfo.m_ProjectionBias != nullptr ?
343 &aclProjectionBiasInfo: nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100344 }
345
346 if (descriptor.m_PeepholeEnabled)
347 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100348 aclCellToForgetWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellToForgetWeights());
349 aclCellToOutputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellToOutputWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100350 lstm_params_info.set_peephole_params(&aclCellToForgetWeightsInfo, &aclCellToOutputWeightsInfo);
351 }
352
353 float cell_threshold = descriptor.m_ClippingThresCell;
354 float projection_threshold = descriptor.m_ClippingThresProj;
355
356 // for preparing the object for the class ActivationLayerInfo, we need to consider 5 situations
357 arm_compute::ActivationLayerInfo activationLayerInfo;
358 if (descriptor.m_ActivationFunc == 0)
359 {
360 // no activation, do nothing
361 }
362 else if (descriptor.m_ActivationFunc == 1)
363 {
364 activationLayerInfo = arm_compute::ActivationLayerInfo(
365 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
366 }
367 else if (descriptor.m_ActivationFunc == 3)
368 {
369 activationLayerInfo = arm_compute::ActivationLayerInfo(
370 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
371 }
372 else if (descriptor.m_ActivationFunc == 4)
373 {
374 activationLayerInfo = arm_compute::ActivationLayerInfo(
375 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
376 }
377 else if (descriptor.m_ActivationFunc == 6)
378 {
379 activationLayerInfo = arm_compute::ActivationLayerInfo(
380 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
381 }
382 else
383 {
384 throw armnn::Exception("Wrong Type of Activation Function!");
385 }
386
Jan Eilersa2ec9092019-07-08 15:56:59 +0100387 if (descriptor.m_LayerNormEnabled)
388 {
389 if (!descriptor.m_CifgEnabled)
390 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100391 aclInputLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputLayerNormWeights());
Jan Eilersa2ec9092019-07-08 15:56:59 +0100392 }
393
Francis Murtaghbb590b42019-08-14 09:51:36 +0100394 aclForgetLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetForgetLayerNormWeights());
Jan Eilersa2ec9092019-07-08 15:56:59 +0100395
Francis Murtaghbb590b42019-08-14 09:51:36 +0100396 aclCellLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellLayerNormWeights());
Jan Eilersa2ec9092019-07-08 15:56:59 +0100397
Francis Murtaghbb590b42019-08-14 09:51:36 +0100398 aclOutputLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetOutputLayerNormWeights());
Jan Eilersa2ec9092019-07-08 15:56:59 +0100399
400 lstm_params_info.set_layer_normalization_params(descriptor.m_CifgEnabled ?
401 nullptr : &aclInputLayerNormWeightsInfo,
402 &aclForgetLayerNormWeightsInfo,
403 &aclCellLayerNormWeightsInfo,
404 &aclOutputLayerNormWeightsInfo);
405 }
406
telsoa01c577f2c2018-08-31 09:22:23 +0100407 return arm_compute::CLLSTMLayer::validate(&aclInputInfo, &aclInputToForgetWeightsInfo,
408 &aclInputToCellWeightsInfo,
409 &aclInputToOutputWeightsInfo,
410 &aclRecurrentToForgetWeightsInfo,
411 &aclRecurrentToCellWeightsInfo,
412 &aclRecurrentToOutputWeightsInfo,
413 &aclForgetGateBiasInfo,
414 &aclCellBiasInfo,
415 &aclOutputGateBiasInfo,
416 &aclOutputStateInInfo, &aclCellStateInInfo,
417 &aclScratchBufferInfo, &aclOutputStateOutInfo,
418 &aclCellStateOutInfo, &aclOutputInfo,
419 lstm_params_info, activationLayerInfo,
420 cell_threshold, projection_threshold);
421}
422
arovir019e53a352018-08-31 15:26:35 +0100423void ClLstmFloatWorkload::FreeUnusedTensors()
telsoa01c577f2c2018-08-31 09:22:23 +0100424{
425 FreeTensorIfUnused(m_InputToInputWeightsTensor);
426 FreeTensorIfUnused(m_InputToForgetWeightsTensor);
427 FreeTensorIfUnused(m_InputToCellWeightsTensor);
428 FreeTensorIfUnused(m_InputToOutputWeightsTensor);
429 FreeTensorIfUnused(m_RecurrentToInputWeightsTensor);
430 FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor);
431 FreeTensorIfUnused(m_RecurrentToCellWeightsTensor);
432 FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor);
433 FreeTensorIfUnused(m_CellToInputWeightsTensor);
434 FreeTensorIfUnused(m_CellToForgetWeightsTensor);
435 FreeTensorIfUnused(m_CellToOutputWeightsTensor);
436 FreeTensorIfUnused(m_InputGateBiasTensor);
437 FreeTensorIfUnused(m_ForgetGateBiasTensor);
438 FreeTensorIfUnused(m_CellBiasTensor);
439 FreeTensorIfUnused(m_OutputGateBiasTensor);
440 FreeTensorIfUnused(m_ProjectionWeightsTensor);
441 FreeTensorIfUnused(m_ProjectionBiasTensor);
442 FreeTensorIfUnused(m_ScratchBuffer);
Jan Eilersa2ec9092019-07-08 15:56:59 +0100443 FreeTensorIfUnused(m_InputLayerNormWeightsTensor);
444 FreeTensorIfUnused(m_ForgetLayerNormWeightsTensor);
445 FreeTensorIfUnused(m_CellLayerNormWeightsTensor);
446 FreeTensorIfUnused(m_OutputLayerNormWeightsTensor);
telsoa01c577f2c2018-08-31 09:22:23 +0100447}
448
449} //namespace armnn