blob: 908f20bfe5724e8dd67133cdf150ae58ead153da [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>
James Conroy1f58f032021-04-27 17:13:27 +01008#include <backendsCommon/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{
27 arm_compute::LSTMParams<arm_compute::ICLTensor> lstm_param;
28
29 // Basic parameters
30 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
31 BuildArmComputeTensor(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights->GetTensorInfo());
32
33 m_InputToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
34 BuildArmComputeTensor(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights->GetTensorInfo());
35
36 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
37 BuildArmComputeTensor(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights->GetTensorInfo());
38
39 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
40 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
41
42 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
43 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights->GetTensorInfo());
44
45 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
46 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
47
48 m_ForgetGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
49 BuildArmComputeTensor(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias->GetTensorInfo());
50
51 m_CellBiasTensor = std::make_unique<arm_compute::CLTensor>();
52 BuildArmComputeTensor(*m_CellBiasTensor, m_Data.m_CellBias->GetTensorInfo());
53
54 m_OutputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
55 BuildArmComputeTensor(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias->GetTensorInfo());
56
57 // for future reference: check the AndroidNN API for the logic here
58 if (!m_Data.m_Parameters.m_CifgEnabled)
59 {
60 m_InputToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
61 BuildArmComputeTensor(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights->GetTensorInfo());
62
63 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
64 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights->GetTensorInfo());
65
66 m_CellToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
67 if (m_Data.m_CellToInputWeights != nullptr)
68 {
69 BuildArmComputeTensor(*m_CellToInputWeightsTensor, m_Data.m_CellToInputWeights->GetTensorInfo());
70 }
71
72 m_InputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
73 BuildArmComputeTensor(*m_InputGateBiasTensor, m_Data.m_InputGateBias->GetTensorInfo());
74
75 lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
76 m_RecurrentToInputWeightsTensor.get(),
77 m_Data.m_CellToInputWeights != nullptr ? m_CellToInputWeightsTensor.get() : nullptr,
78 m_InputGateBiasTensor.get());
79 }
80
81 if (m_Data.m_Parameters.m_ProjectionEnabled)
82 {
83 m_ProjectionWeightsTensor = std::make_unique<arm_compute::CLTensor>();
84 BuildArmComputeTensor(*m_ProjectionWeightsTensor, m_Data.m_ProjectionWeights->GetTensorInfo());
85
86 m_ProjectionBiasTensor = std::make_unique<arm_compute::CLTensor>();
87 if (m_Data.m_ProjectionBias != nullptr)
88 {
89 BuildArmComputeTensor(*m_ProjectionBiasTensor, m_Data.m_ProjectionBias->GetTensorInfo());
90 }
91
92 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
93 m_Data.m_ProjectionBias != nullptr ? m_ProjectionBiasTensor.get() : nullptr);
94 }
95
96 if (m_Data.m_Parameters.m_PeepholeEnabled)
97 {
98 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
99 BuildArmComputeTensor(*m_CellToForgetWeightsTensor, m_Data.m_CellToForgetWeights->GetTensorInfo());
100
101 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
102 BuildArmComputeTensor(*m_CellToOutputWeightsTensor, m_Data.m_CellToOutputWeights->GetTensorInfo());
103
104 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
105 }
106
Jan Eilersa2ec9092019-07-08 15:56:59 +0100107 if (m_Data.m_Parameters.m_LayerNormEnabled)
108 {
109 m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
110 m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
111 m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
112 m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
113
114 if (!m_Data.m_Parameters.m_CifgEnabled)
115 {
116 BuildArmComputeTensor(*m_InputLayerNormWeightsTensor, m_Data.m_InputLayerNormWeights->GetTensorInfo());
117 }
118 BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor, m_Data.m_ForgetLayerNormWeights->GetTensorInfo());
119 BuildArmComputeTensor(*m_CellLayerNormWeightsTensor, m_Data.m_CellLayerNormWeights->GetTensorInfo());
120 BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor, m_Data.m_OutputLayerNormWeights->GetTensorInfo());
121
122 lstm_param.set_layer_normalization_params(m_Data.m_Parameters.m_CifgEnabled ? nullptr :
123 m_InputLayerNormWeightsTensor.get(),
124 m_ForgetLayerNormWeightsTensor.get(),
125 m_CellLayerNormWeightsTensor.get(),
126 m_OutputLayerNormWeightsTensor.get());
127 }
128
telsoa01c577f2c2018-08-31 09:22:23 +0100129 const arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
130 const arm_compute::ICLTensor& output_state_in = static_cast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
Teresa Charlin57512af2020-07-29 12:06:40 +0100131 arm_compute::ICLTensor& cell_state_in = static_cast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
telsoa01c577f2c2018-08-31 09:22:23 +0100132
133 arm_compute::ICLTensor& output_state_out = static_cast<IClTensorHandle*>(m_Data.m_Outputs[1])->GetTensor();
134 arm_compute::ICLTensor& cell_state_out = static_cast<IClTensorHandle*>(m_Data.m_Outputs[2])->GetTensor();
135 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[3])->GetTensor();
136
137 // Get the batch_size and the num_units from the cellStateIn dimensions
138 const TensorInfo& inputTensorInfo = info.m_InputTensorInfos[2];
Matthew Sloyan171214c2020-09-09 09:07:37 +0100139 const unsigned int batch_size = armnn::numeric_cast<unsigned int>(inputTensorInfo.GetShape()[0]);
140 const unsigned int num_units = armnn::numeric_cast<unsigned int>(inputTensorInfo.GetShape()[1]);
telsoa01c577f2c2018-08-31 09:22:23 +0100141
142 m_ScratchBuffer = std::make_unique<arm_compute::CLTensor>();
143 if (m_Data.m_Parameters.m_CifgEnabled)
144 {
Matteo Martincigha65b7ae2018-11-14 12:39:55 +0000145 // 2D tensor with dimensions [num_units * 3, batch_size] with CIFG
146 armnn::TensorInfo scratchBuffer1({ batch_size, num_units * 3 }, DataType::Float32);
telsoa01c577f2c2018-08-31 09:22:23 +0100147 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
148 }
149 else
150 {
Matteo Martincigha65b7ae2018-11-14 12:39:55 +0000151 // scratch_buffer [num_units * 4, batch_size] without CIFG
152 armnn::TensorInfo scratchBuffer2({ batch_size, num_units * 4 }, DataType::Float32);
telsoa01c577f2c2018-08-31 09:22:23 +0100153 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2);
154 }
155
156 float cell_threshold = m_Data.m_Parameters.m_ClippingThresCell;
157 float projection_threshold = m_Data.m_Parameters.m_ClippingThresProj;
158
159 // for preparing the object for the class ActivationLayerInfo, we need to consider 5 situations
160 arm_compute::ActivationLayerInfo activationLayerInfo;
161 if (m_Data.m_Parameters.m_ActivationFunc == 0)
162 {
163 // no activation, do nothing
164 }
165 else if (m_Data.m_Parameters.m_ActivationFunc == 1)
166 {
167 activationLayerInfo = arm_compute::ActivationLayerInfo(
168 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
169 }
170 else if (m_Data.m_Parameters.m_ActivationFunc == 3)
171 {
172 activationLayerInfo = arm_compute::ActivationLayerInfo(
173 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
174 }
175 else if (m_Data.m_Parameters.m_ActivationFunc == 4)
176 {
177 activationLayerInfo = arm_compute::ActivationLayerInfo(
178 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
179 }
180 else if (m_Data.m_Parameters.m_ActivationFunc == 6)
181 {
182 activationLayerInfo = arm_compute::ActivationLayerInfo(
183 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
184 }
185 else
186 {
187 throw armnn::Exception("Wrong Type of Activation Function!");
188 }
189
Sadik Armagane9444752020-12-02 11:28:58 +0000190 m_LstmLayer.configure(clCompileContext, &input, m_InputToForgetWeightsTensor.get(),
191 m_InputToCellWeightsTensor.get(), m_InputToOutputWeightsTensor.get(),
192 m_RecurrentToForgetWeightsTensor.get(), m_RecurrentToCellWeightsTensor.get(),
193 m_RecurrentToOutputWeightsTensor.get(), m_ForgetGateBiasTensor.get(),
194 m_CellBiasTensor.get(), m_OutputGateBiasTensor.get(), &output_state_in,
195 &cell_state_in, m_ScratchBuffer.get(), &output_state_out,
telsoa01c577f2c2018-08-31 09:22:23 +0100196 &cell_state_out, &output, lstm_param, activationLayerInfo,
197 cell_threshold, projection_threshold);
198
199 armcomputetensorutils::InitialiseArmComputeTensorEmpty(*m_ScratchBuffer);
200
Jan Eilersa2ec9092019-07-08 15:56:59 +0100201 InitializeArmComputeClTensorData(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights);
202 InitializeArmComputeClTensorData(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights);
203 InitializeArmComputeClTensorData(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights);
Matthew Bentham785df502018-09-21 10:29:58 +0100204 InitializeArmComputeClTensorData(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights);
Jan Eilersa2ec9092019-07-08 15:56:59 +0100205 InitializeArmComputeClTensorData(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights);
Matthew Bentham785df502018-09-21 10:29:58 +0100206 InitializeArmComputeClTensorData(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights);
Jan Eilersa2ec9092019-07-08 15:56:59 +0100207 InitializeArmComputeClTensorData(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias);
208 InitializeArmComputeClTensorData(*m_CellBiasTensor, m_Data.m_CellBias);
209 InitializeArmComputeClTensorData(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias);
telsoa01c577f2c2018-08-31 09:22:23 +0100210
211 if (!m_Data.m_Parameters.m_CifgEnabled)
212 {
Matthew Bentham785df502018-09-21 10:29:58 +0100213 InitializeArmComputeClTensorData(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights);
214 InitializeArmComputeClTensorData(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights);
telsoa01c577f2c2018-08-31 09:22:23 +0100215 if (m_Data.m_CellToInputWeights != nullptr)
216 {
Matthew Bentham785df502018-09-21 10:29:58 +0100217 InitializeArmComputeClTensorData(*m_CellToInputWeightsTensor, m_Data.m_CellToInputWeights);
telsoa01c577f2c2018-08-31 09:22:23 +0100218 }
Matthew Bentham785df502018-09-21 10:29:58 +0100219 InitializeArmComputeClTensorData(*m_InputGateBiasTensor, m_Data.m_InputGateBias);
telsoa01c577f2c2018-08-31 09:22:23 +0100220 }
221
222 if (m_Data.m_Parameters.m_ProjectionEnabled)
223 {
Matthew Bentham785df502018-09-21 10:29:58 +0100224 InitializeArmComputeClTensorData(*m_ProjectionWeightsTensor, m_Data.m_ProjectionWeights);
telsoa01c577f2c2018-08-31 09:22:23 +0100225 if (m_Data.m_ProjectionBias != nullptr)
226 {
Matthew Bentham785df502018-09-21 10:29:58 +0100227 InitializeArmComputeClTensorData(*m_ProjectionBiasTensor, m_Data.m_ProjectionBias);
telsoa01c577f2c2018-08-31 09:22:23 +0100228 }
229 }
230
231 if (m_Data.m_Parameters.m_PeepholeEnabled)
232 {
Matthew Bentham785df502018-09-21 10:29:58 +0100233 InitializeArmComputeClTensorData(*m_CellToForgetWeightsTensor, m_Data.m_CellToForgetWeights);
234 InitializeArmComputeClTensorData(*m_CellToOutputWeightsTensor, m_Data.m_CellToOutputWeights);
telsoa01c577f2c2018-08-31 09:22:23 +0100235 }
236
Jan Eilersa2ec9092019-07-08 15:56:59 +0100237 if (m_Data.m_Parameters.m_LayerNormEnabled)
238 {
239 if (!m_Data.m_Parameters.m_CifgEnabled)
240 {
241 InitializeArmComputeClTensorData(*m_InputLayerNormWeightsTensor, m_Data.m_InputLayerNormWeights);
242 }
243
244 InitializeArmComputeClTensorData(*m_ForgetLayerNormWeightsTensor, m_Data.m_ForgetLayerNormWeights);
245 InitializeArmComputeClTensorData(*m_CellLayerNormWeightsTensor, m_Data.m_CellLayerNormWeights);
246 InitializeArmComputeClTensorData(*m_OutputLayerNormWeightsTensor, m_Data.m_OutputLayerNormWeights);
247 }
248
telsoa01c577f2c2018-08-31 09:22:23 +0100249 // Force Compute Library to perform the necessary copying and reshaping, after which
250 // delete all the input tensors that will no longer be needed
251 m_LstmLayer.prepare();
252 FreeUnusedTensors();
253}
254
arovir019e53a352018-08-31 15:26:35 +0100255void ClLstmFloatWorkload::Execute() const
telsoa01c577f2c2018-08-31 09:22:23 +0100256{
Aron Virginas-Tar7bc8c9f2018-10-19 16:58:08 +0100257 ARMNN_SCOPED_PROFILING_EVENT_CL("ClLstmFloatWorkload_Execute");
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100258 RunClFunction(m_LstmLayer, CHECK_LOCATION());
telsoa01c577f2c2018-08-31 09:22:23 +0100259}
260
arovir019e53a352018-08-31 15:26:35 +0100261arm_compute::Status ClLstmFloatWorkloadValidate(const TensorInfo& input, const TensorInfo& outputStateIn,
262 const TensorInfo& cellStateIn, const TensorInfo& scratchBuffer,
263 const TensorInfo& outputStateOut, const TensorInfo& cellStateOut,
264 const TensorInfo& output, const LstmDescriptor& descriptor,
Jan Eilersd01a83c2019-07-03 18:20:40 +0100265 const LstmInputParamsInfo& paramsInfo)
telsoa01c577f2c2018-08-31 09:22:23 +0100266{
267 arm_compute::LSTMParams<arm_compute::ITensorInfo> lstm_params_info;
268
269 // The inputs and the outputs
270 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
271 const arm_compute::TensorInfo aclOutputStateInInfo = BuildArmComputeTensorInfo(outputStateIn);
272 const arm_compute::TensorInfo aclCellStateInInfo = BuildArmComputeTensorInfo(cellStateIn);
273 const arm_compute::TensorInfo aclScratchBufferInfo = BuildArmComputeTensorInfo(scratchBuffer);
274 const arm_compute::TensorInfo aclOutputStateOutInfo = BuildArmComputeTensorInfo(outputStateOut);
275 const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut);
276 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
277
278 // Basic parameters
Jan Eilersd01a83c2019-07-03 18:20:40 +0100279 const arm_compute::TensorInfo aclInputToForgetWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100280 = BuildArmComputeTensorInfo(paramsInfo.GetInputToForgetWeights());
Jan Eilersd01a83c2019-07-03 18:20:40 +0100281 const arm_compute::TensorInfo aclInputToCellWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100282 = BuildArmComputeTensorInfo(paramsInfo.GetInputToCellWeights());
Jan Eilersd01a83c2019-07-03 18:20:40 +0100283 const arm_compute::TensorInfo aclInputToOutputWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100284 = BuildArmComputeTensorInfo(paramsInfo.GetInputToOutputWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100285 const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100286 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToForgetWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100287 const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100288 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToCellWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100289 const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100290 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToOutputWeights());
291 const arm_compute::TensorInfo aclForgetGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetForgetGateBias());
292 const arm_compute::TensorInfo aclCellBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellBias());
293 const arm_compute::TensorInfo aclOutputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetOutputGateBias());
telsoa01c577f2c2018-08-31 09:22:23 +0100294
295 arm_compute::TensorInfo aclInputToInputWeightsInfo;
296 arm_compute::TensorInfo aclRecurrentToInputWeightsInfo;
297 arm_compute::TensorInfo aclCellToInputWeightsInfo;
298 arm_compute::TensorInfo aclInputGateBiasInfo;
299 arm_compute::TensorInfo aclProjectionWeightsInfo;
300 arm_compute::TensorInfo aclProjectionBiasInfo;
301 arm_compute::TensorInfo aclCellToForgetWeightsInfo;
302 arm_compute::TensorInfo aclCellToOutputWeightsInfo;
Jan Eilersa2ec9092019-07-08 15:56:59 +0100303 arm_compute::TensorInfo aclInputLayerNormWeightsInfo;
304 arm_compute::TensorInfo aclForgetLayerNormWeightsInfo;
305 arm_compute::TensorInfo aclCellLayerNormWeightsInfo;
306 arm_compute::TensorInfo aclOutputLayerNormWeightsInfo;
telsoa01c577f2c2018-08-31 09:22:23 +0100307
308 if (!descriptor.m_CifgEnabled)
309 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100310 aclInputToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputToInputWeights());
311 aclRecurrentToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToInputWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100312
Jan Eilersd01a83c2019-07-03 18:20:40 +0100313 if (paramsInfo.m_CellToInputWeights != nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +0100314 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100315 aclCellToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellToInputWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100316 }
Francis Murtaghbb590b42019-08-14 09:51:36 +0100317 aclInputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputGateBias());
telsoa01c577f2c2018-08-31 09:22:23 +0100318 lstm_params_info.set_cifg_params(&aclInputToInputWeightsInfo, &aclRecurrentToInputWeightsInfo,
Jan Eilersd01a83c2019-07-03 18:20:40 +0100319 paramsInfo.m_CellToInputWeights != nullptr ?
320 &aclCellToInputWeightsInfo: nullptr,
telsoa01c577f2c2018-08-31 09:22:23 +0100321 &aclInputGateBiasInfo);
322 }
323
324 if (descriptor.m_ProjectionEnabled)
325 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100326 aclProjectionWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetProjectionWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100327
Jan Eilersd01a83c2019-07-03 18:20:40 +0100328 if (paramsInfo.m_ProjectionBias != nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +0100329 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100330 aclProjectionBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputGateBias());
telsoa01c577f2c2018-08-31 09:22:23 +0100331 }
332 lstm_params_info.set_projection_params(&aclProjectionWeightsInfo,
Jan Eilersd01a83c2019-07-03 18:20:40 +0100333 paramsInfo.m_ProjectionBias != nullptr ?
334 &aclProjectionBiasInfo: nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100335 }
336
337 if (descriptor.m_PeepholeEnabled)
338 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100339 aclCellToForgetWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellToForgetWeights());
340 aclCellToOutputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellToOutputWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100341 lstm_params_info.set_peephole_params(&aclCellToForgetWeightsInfo, &aclCellToOutputWeightsInfo);
342 }
343
344 float cell_threshold = descriptor.m_ClippingThresCell;
345 float projection_threshold = descriptor.m_ClippingThresProj;
346
347 // for preparing the object for the class ActivationLayerInfo, we need to consider 5 situations
348 arm_compute::ActivationLayerInfo activationLayerInfo;
349 if (descriptor.m_ActivationFunc == 0)
350 {
351 // no activation, do nothing
352 }
353 else if (descriptor.m_ActivationFunc == 1)
354 {
355 activationLayerInfo = arm_compute::ActivationLayerInfo(
356 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
357 }
358 else if (descriptor.m_ActivationFunc == 3)
359 {
360 activationLayerInfo = arm_compute::ActivationLayerInfo(
361 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
362 }
363 else if (descriptor.m_ActivationFunc == 4)
364 {
365 activationLayerInfo = arm_compute::ActivationLayerInfo(
366 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
367 }
368 else if (descriptor.m_ActivationFunc == 6)
369 {
370 activationLayerInfo = arm_compute::ActivationLayerInfo(
371 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
372 }
373 else
374 {
375 throw armnn::Exception("Wrong Type of Activation Function!");
376 }
377
Jan Eilersa2ec9092019-07-08 15:56:59 +0100378 if (descriptor.m_LayerNormEnabled)
379 {
380 if (!descriptor.m_CifgEnabled)
381 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100382 aclInputLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputLayerNormWeights());
Jan Eilersa2ec9092019-07-08 15:56:59 +0100383 }
384
Francis Murtaghbb590b42019-08-14 09:51:36 +0100385 aclForgetLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetForgetLayerNormWeights());
Jan Eilersa2ec9092019-07-08 15:56:59 +0100386
Francis Murtaghbb590b42019-08-14 09:51:36 +0100387 aclCellLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellLayerNormWeights());
Jan Eilersa2ec9092019-07-08 15:56:59 +0100388
Francis Murtaghbb590b42019-08-14 09:51:36 +0100389 aclOutputLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetOutputLayerNormWeights());
Jan Eilersa2ec9092019-07-08 15:56:59 +0100390
391 lstm_params_info.set_layer_normalization_params(descriptor.m_CifgEnabled ?
392 nullptr : &aclInputLayerNormWeightsInfo,
393 &aclForgetLayerNormWeightsInfo,
394 &aclCellLayerNormWeightsInfo,
395 &aclOutputLayerNormWeightsInfo);
396 }
397
telsoa01c577f2c2018-08-31 09:22:23 +0100398 return arm_compute::CLLSTMLayer::validate(&aclInputInfo, &aclInputToForgetWeightsInfo,
399 &aclInputToCellWeightsInfo,
400 &aclInputToOutputWeightsInfo,
401 &aclRecurrentToForgetWeightsInfo,
402 &aclRecurrentToCellWeightsInfo,
403 &aclRecurrentToOutputWeightsInfo,
404 &aclForgetGateBiasInfo,
405 &aclCellBiasInfo,
406 &aclOutputGateBiasInfo,
407 &aclOutputStateInInfo, &aclCellStateInInfo,
408 &aclScratchBufferInfo, &aclOutputStateOutInfo,
409 &aclCellStateOutInfo, &aclOutputInfo,
410 lstm_params_info, activationLayerInfo,
411 cell_threshold, projection_threshold);
412}
413
arovir019e53a352018-08-31 15:26:35 +0100414void ClLstmFloatWorkload::FreeUnusedTensors()
telsoa01c577f2c2018-08-31 09:22:23 +0100415{
416 FreeTensorIfUnused(m_InputToInputWeightsTensor);
417 FreeTensorIfUnused(m_InputToForgetWeightsTensor);
418 FreeTensorIfUnused(m_InputToCellWeightsTensor);
419 FreeTensorIfUnused(m_InputToOutputWeightsTensor);
420 FreeTensorIfUnused(m_RecurrentToInputWeightsTensor);
421 FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor);
422 FreeTensorIfUnused(m_RecurrentToCellWeightsTensor);
423 FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor);
424 FreeTensorIfUnused(m_CellToInputWeightsTensor);
425 FreeTensorIfUnused(m_CellToForgetWeightsTensor);
426 FreeTensorIfUnused(m_CellToOutputWeightsTensor);
427 FreeTensorIfUnused(m_InputGateBiasTensor);
428 FreeTensorIfUnused(m_ForgetGateBiasTensor);
429 FreeTensorIfUnused(m_CellBiasTensor);
430 FreeTensorIfUnused(m_OutputGateBiasTensor);
431 FreeTensorIfUnused(m_ProjectionWeightsTensor);
432 FreeTensorIfUnused(m_ProjectionBiasTensor);
433 FreeTensorIfUnused(m_ScratchBuffer);
Jan Eilersa2ec9092019-07-08 15:56:59 +0100434 FreeTensorIfUnused(m_InputLayerNormWeightsTensor);
435 FreeTensorIfUnused(m_ForgetLayerNormWeightsTensor);
436 FreeTensorIfUnused(m_CellLayerNormWeightsTensor);
437 FreeTensorIfUnused(m_OutputLayerNormWeightsTensor);
telsoa01c577f2c2018-08-31 09:22:23 +0100438}
439
440} //namespace armnn