blob: fe9b45e054476172c02e90c4d0f087a626a9c321 [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>
8#include <backendsCommon/CpuTensorHandle.hpp>
9#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
arovir019e53a352018-08-31 15:26:35 +010022ClLstmFloatWorkload::ClLstmFloatWorkload(const LstmQueueDescriptor &descriptor, const WorkloadInfo &info)
telsoa01c577f2c2018-08-31 09:22:23 +010023 : FloatWorkload<LstmQueueDescriptor>(descriptor, info)
24{
25 arm_compute::LSTMParams<arm_compute::ICLTensor> lstm_param;
26
27 // Basic parameters
28 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
29 BuildArmComputeTensor(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights->GetTensorInfo());
30
31 m_InputToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
32 BuildArmComputeTensor(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights->GetTensorInfo());
33
34 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
35 BuildArmComputeTensor(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights->GetTensorInfo());
36
37 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
38 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
39
40 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
41 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights->GetTensorInfo());
42
43 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
44 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
45
46 m_ForgetGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
47 BuildArmComputeTensor(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias->GetTensorInfo());
48
49 m_CellBiasTensor = std::make_unique<arm_compute::CLTensor>();
50 BuildArmComputeTensor(*m_CellBiasTensor, m_Data.m_CellBias->GetTensorInfo());
51
52 m_OutputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
53 BuildArmComputeTensor(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias->GetTensorInfo());
54
55 // for future reference: check the AndroidNN API for the logic here
56 if (!m_Data.m_Parameters.m_CifgEnabled)
57 {
58 m_InputToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
59 BuildArmComputeTensor(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights->GetTensorInfo());
60
61 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
62 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights->GetTensorInfo());
63
64 m_CellToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
65 if (m_Data.m_CellToInputWeights != nullptr)
66 {
67 BuildArmComputeTensor(*m_CellToInputWeightsTensor, m_Data.m_CellToInputWeights->GetTensorInfo());
68 }
69
70 m_InputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
71 BuildArmComputeTensor(*m_InputGateBiasTensor, m_Data.m_InputGateBias->GetTensorInfo());
72
73 lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
74 m_RecurrentToInputWeightsTensor.get(),
75 m_Data.m_CellToInputWeights != nullptr ? m_CellToInputWeightsTensor.get() : nullptr,
76 m_InputGateBiasTensor.get());
77 }
78
79 if (m_Data.m_Parameters.m_ProjectionEnabled)
80 {
81 m_ProjectionWeightsTensor = std::make_unique<arm_compute::CLTensor>();
82 BuildArmComputeTensor(*m_ProjectionWeightsTensor, m_Data.m_ProjectionWeights->GetTensorInfo());
83
84 m_ProjectionBiasTensor = std::make_unique<arm_compute::CLTensor>();
85 if (m_Data.m_ProjectionBias != nullptr)
86 {
87 BuildArmComputeTensor(*m_ProjectionBiasTensor, m_Data.m_ProjectionBias->GetTensorInfo());
88 }
89
90 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
91 m_Data.m_ProjectionBias != nullptr ? m_ProjectionBiasTensor.get() : nullptr);
92 }
93
94 if (m_Data.m_Parameters.m_PeepholeEnabled)
95 {
96 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
97 BuildArmComputeTensor(*m_CellToForgetWeightsTensor, m_Data.m_CellToForgetWeights->GetTensorInfo());
98
99 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
100 BuildArmComputeTensor(*m_CellToOutputWeightsTensor, m_Data.m_CellToOutputWeights->GetTensorInfo());
101
102 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
103 }
104
Jan Eilersa2ec9092019-07-08 15:56:59 +0100105 if (m_Data.m_Parameters.m_LayerNormEnabled)
106 {
107 m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
108 m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
109 m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
110 m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
111
112 if (!m_Data.m_Parameters.m_CifgEnabled)
113 {
114 BuildArmComputeTensor(*m_InputLayerNormWeightsTensor, m_Data.m_InputLayerNormWeights->GetTensorInfo());
115 }
116 BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor, m_Data.m_ForgetLayerNormWeights->GetTensorInfo());
117 BuildArmComputeTensor(*m_CellLayerNormWeightsTensor, m_Data.m_CellLayerNormWeights->GetTensorInfo());
118 BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor, m_Data.m_OutputLayerNormWeights->GetTensorInfo());
119
120 lstm_param.set_layer_normalization_params(m_Data.m_Parameters.m_CifgEnabled ? nullptr :
121 m_InputLayerNormWeightsTensor.get(),
122 m_ForgetLayerNormWeightsTensor.get(),
123 m_CellLayerNormWeightsTensor.get(),
124 m_OutputLayerNormWeightsTensor.get());
125 }
126
telsoa01c577f2c2018-08-31 09:22:23 +0100127 const arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
128 const arm_compute::ICLTensor& output_state_in = static_cast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
Teresa Charlin57512af2020-07-29 12:06:40 +0100129 arm_compute::ICLTensor& cell_state_in = static_cast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
telsoa01c577f2c2018-08-31 09:22:23 +0100130
131 arm_compute::ICLTensor& output_state_out = static_cast<IClTensorHandle*>(m_Data.m_Outputs[1])->GetTensor();
132 arm_compute::ICLTensor& cell_state_out = static_cast<IClTensorHandle*>(m_Data.m_Outputs[2])->GetTensor();
133 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[3])->GetTensor();
134
135 // Get the batch_size and the num_units from the cellStateIn dimensions
136 const TensorInfo& inputTensorInfo = info.m_InputTensorInfos[2];
Matthew Sloyan171214c2020-09-09 09:07:37 +0100137 const unsigned int batch_size = armnn::numeric_cast<unsigned int>(inputTensorInfo.GetShape()[0]);
138 const unsigned int num_units = armnn::numeric_cast<unsigned int>(inputTensorInfo.GetShape()[1]);
telsoa01c577f2c2018-08-31 09:22:23 +0100139
140 m_ScratchBuffer = std::make_unique<arm_compute::CLTensor>();
141 if (m_Data.m_Parameters.m_CifgEnabled)
142 {
Matteo Martincigha65b7ae2018-11-14 12:39:55 +0000143 // 2D tensor with dimensions [num_units * 3, batch_size] with CIFG
144 armnn::TensorInfo scratchBuffer1({ batch_size, num_units * 3 }, DataType::Float32);
telsoa01c577f2c2018-08-31 09:22:23 +0100145 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
146 }
147 else
148 {
Matteo Martincigha65b7ae2018-11-14 12:39:55 +0000149 // scratch_buffer [num_units * 4, batch_size] without CIFG
150 armnn::TensorInfo scratchBuffer2({ batch_size, num_units * 4 }, DataType::Float32);
telsoa01c577f2c2018-08-31 09:22:23 +0100151 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2);
152 }
153
154 float cell_threshold = m_Data.m_Parameters.m_ClippingThresCell;
155 float projection_threshold = m_Data.m_Parameters.m_ClippingThresProj;
156
157 // for preparing the object for the class ActivationLayerInfo, we need to consider 5 situations
158 arm_compute::ActivationLayerInfo activationLayerInfo;
159 if (m_Data.m_Parameters.m_ActivationFunc == 0)
160 {
161 // no activation, do nothing
162 }
163 else if (m_Data.m_Parameters.m_ActivationFunc == 1)
164 {
165 activationLayerInfo = arm_compute::ActivationLayerInfo(
166 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
167 }
168 else if (m_Data.m_Parameters.m_ActivationFunc == 3)
169 {
170 activationLayerInfo = arm_compute::ActivationLayerInfo(
171 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
172 }
173 else if (m_Data.m_Parameters.m_ActivationFunc == 4)
174 {
175 activationLayerInfo = arm_compute::ActivationLayerInfo(
176 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
177 }
178 else if (m_Data.m_Parameters.m_ActivationFunc == 6)
179 {
180 activationLayerInfo = arm_compute::ActivationLayerInfo(
181 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
182 }
183 else
184 {
185 throw armnn::Exception("Wrong Type of Activation Function!");
186 }
187
telsoa01c577f2c2018-08-31 09:22:23 +0100188 m_LstmLayer.configure(&input, m_InputToForgetWeightsTensor.get(), m_InputToCellWeightsTensor.get(),
189 m_InputToOutputWeightsTensor.get(), m_RecurrentToForgetWeightsTensor.get(),
190 m_RecurrentToCellWeightsTensor.get(), m_RecurrentToOutputWeightsTensor.get(),
191 m_ForgetGateBiasTensor.get(), m_CellBiasTensor.get(), m_OutputGateBiasTensor.get(),
192 &output_state_in, &cell_state_in, m_ScratchBuffer.get(), &output_state_out,
193 &cell_state_out, &output, lstm_param, activationLayerInfo,
194 cell_threshold, projection_threshold);
195
196 armcomputetensorutils::InitialiseArmComputeTensorEmpty(*m_ScratchBuffer);
197
Jan Eilersa2ec9092019-07-08 15:56:59 +0100198 InitializeArmComputeClTensorData(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights);
199 InitializeArmComputeClTensorData(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights);
200 InitializeArmComputeClTensorData(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights);
Matthew Bentham785df502018-09-21 10:29:58 +0100201 InitializeArmComputeClTensorData(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights);
Jan Eilersa2ec9092019-07-08 15:56:59 +0100202 InitializeArmComputeClTensorData(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights);
Matthew Bentham785df502018-09-21 10:29:58 +0100203 InitializeArmComputeClTensorData(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights);
Jan Eilersa2ec9092019-07-08 15:56:59 +0100204 InitializeArmComputeClTensorData(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias);
205 InitializeArmComputeClTensorData(*m_CellBiasTensor, m_Data.m_CellBias);
206 InitializeArmComputeClTensorData(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias);
telsoa01c577f2c2018-08-31 09:22:23 +0100207
208 if (!m_Data.m_Parameters.m_CifgEnabled)
209 {
Matthew Bentham785df502018-09-21 10:29:58 +0100210 InitializeArmComputeClTensorData(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights);
211 InitializeArmComputeClTensorData(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights);
telsoa01c577f2c2018-08-31 09:22:23 +0100212 if (m_Data.m_CellToInputWeights != nullptr)
213 {
Matthew Bentham785df502018-09-21 10:29:58 +0100214 InitializeArmComputeClTensorData(*m_CellToInputWeightsTensor, m_Data.m_CellToInputWeights);
telsoa01c577f2c2018-08-31 09:22:23 +0100215 }
Matthew Bentham785df502018-09-21 10:29:58 +0100216 InitializeArmComputeClTensorData(*m_InputGateBiasTensor, m_Data.m_InputGateBias);
telsoa01c577f2c2018-08-31 09:22:23 +0100217 }
218
219 if (m_Data.m_Parameters.m_ProjectionEnabled)
220 {
Matthew Bentham785df502018-09-21 10:29:58 +0100221 InitializeArmComputeClTensorData(*m_ProjectionWeightsTensor, m_Data.m_ProjectionWeights);
telsoa01c577f2c2018-08-31 09:22:23 +0100222 if (m_Data.m_ProjectionBias != nullptr)
223 {
Matthew Bentham785df502018-09-21 10:29:58 +0100224 InitializeArmComputeClTensorData(*m_ProjectionBiasTensor, m_Data.m_ProjectionBias);
telsoa01c577f2c2018-08-31 09:22:23 +0100225 }
226 }
227
228 if (m_Data.m_Parameters.m_PeepholeEnabled)
229 {
Matthew Bentham785df502018-09-21 10:29:58 +0100230 InitializeArmComputeClTensorData(*m_CellToForgetWeightsTensor, m_Data.m_CellToForgetWeights);
231 InitializeArmComputeClTensorData(*m_CellToOutputWeightsTensor, m_Data.m_CellToOutputWeights);
telsoa01c577f2c2018-08-31 09:22:23 +0100232 }
233
Jan Eilersa2ec9092019-07-08 15:56:59 +0100234 if (m_Data.m_Parameters.m_LayerNormEnabled)
235 {
236 if (!m_Data.m_Parameters.m_CifgEnabled)
237 {
238 InitializeArmComputeClTensorData(*m_InputLayerNormWeightsTensor, m_Data.m_InputLayerNormWeights);
239 }
240
241 InitializeArmComputeClTensorData(*m_ForgetLayerNormWeightsTensor, m_Data.m_ForgetLayerNormWeights);
242 InitializeArmComputeClTensorData(*m_CellLayerNormWeightsTensor, m_Data.m_CellLayerNormWeights);
243 InitializeArmComputeClTensorData(*m_OutputLayerNormWeightsTensor, m_Data.m_OutputLayerNormWeights);
244 }
245
telsoa01c577f2c2018-08-31 09:22:23 +0100246 // Force Compute Library to perform the necessary copying and reshaping, after which
247 // delete all the input tensors that will no longer be needed
248 m_LstmLayer.prepare();
249 FreeUnusedTensors();
250}
251
arovir019e53a352018-08-31 15:26:35 +0100252void ClLstmFloatWorkload::Execute() const
telsoa01c577f2c2018-08-31 09:22:23 +0100253{
Aron Virginas-Tar7bc8c9f2018-10-19 16:58:08 +0100254 ARMNN_SCOPED_PROFILING_EVENT_CL("ClLstmFloatWorkload_Execute");
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100255 RunClFunction(m_LstmLayer, CHECK_LOCATION());
telsoa01c577f2c2018-08-31 09:22:23 +0100256}
257
arovir019e53a352018-08-31 15:26:35 +0100258arm_compute::Status ClLstmFloatWorkloadValidate(const TensorInfo& input, const TensorInfo& outputStateIn,
259 const TensorInfo& cellStateIn, const TensorInfo& scratchBuffer,
260 const TensorInfo& outputStateOut, const TensorInfo& cellStateOut,
261 const TensorInfo& output, const LstmDescriptor& descriptor,
Jan Eilersd01a83c2019-07-03 18:20:40 +0100262 const LstmInputParamsInfo& paramsInfo)
telsoa01c577f2c2018-08-31 09:22:23 +0100263{
264 arm_compute::LSTMParams<arm_compute::ITensorInfo> lstm_params_info;
265
266 // The inputs and the outputs
267 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
268 const arm_compute::TensorInfo aclOutputStateInInfo = BuildArmComputeTensorInfo(outputStateIn);
269 const arm_compute::TensorInfo aclCellStateInInfo = BuildArmComputeTensorInfo(cellStateIn);
270 const arm_compute::TensorInfo aclScratchBufferInfo = BuildArmComputeTensorInfo(scratchBuffer);
271 const arm_compute::TensorInfo aclOutputStateOutInfo = BuildArmComputeTensorInfo(outputStateOut);
272 const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut);
273 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
274
275 // Basic parameters
Jan Eilersd01a83c2019-07-03 18:20:40 +0100276 const arm_compute::TensorInfo aclInputToForgetWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100277 = BuildArmComputeTensorInfo(paramsInfo.GetInputToForgetWeights());
Jan Eilersd01a83c2019-07-03 18:20:40 +0100278 const arm_compute::TensorInfo aclInputToCellWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100279 = BuildArmComputeTensorInfo(paramsInfo.GetInputToCellWeights());
Jan Eilersd01a83c2019-07-03 18:20:40 +0100280 const arm_compute::TensorInfo aclInputToOutputWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100281 = BuildArmComputeTensorInfo(paramsInfo.GetInputToOutputWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100282 const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100283 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToForgetWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100284 const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100285 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToCellWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100286 const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +0100287 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToOutputWeights());
288 const arm_compute::TensorInfo aclForgetGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetForgetGateBias());
289 const arm_compute::TensorInfo aclCellBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellBias());
290 const arm_compute::TensorInfo aclOutputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetOutputGateBias());
telsoa01c577f2c2018-08-31 09:22:23 +0100291
292 arm_compute::TensorInfo aclInputToInputWeightsInfo;
293 arm_compute::TensorInfo aclRecurrentToInputWeightsInfo;
294 arm_compute::TensorInfo aclCellToInputWeightsInfo;
295 arm_compute::TensorInfo aclInputGateBiasInfo;
296 arm_compute::TensorInfo aclProjectionWeightsInfo;
297 arm_compute::TensorInfo aclProjectionBiasInfo;
298 arm_compute::TensorInfo aclCellToForgetWeightsInfo;
299 arm_compute::TensorInfo aclCellToOutputWeightsInfo;
Jan Eilersa2ec9092019-07-08 15:56:59 +0100300 arm_compute::TensorInfo aclInputLayerNormWeightsInfo;
301 arm_compute::TensorInfo aclForgetLayerNormWeightsInfo;
302 arm_compute::TensorInfo aclCellLayerNormWeightsInfo;
303 arm_compute::TensorInfo aclOutputLayerNormWeightsInfo;
telsoa01c577f2c2018-08-31 09:22:23 +0100304
305 if (!descriptor.m_CifgEnabled)
306 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100307 aclInputToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputToInputWeights());
308 aclRecurrentToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToInputWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100309
Jan Eilersd01a83c2019-07-03 18:20:40 +0100310 if (paramsInfo.m_CellToInputWeights != nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +0100311 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100312 aclCellToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellToInputWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100313 }
Francis Murtaghbb590b42019-08-14 09:51:36 +0100314 aclInputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputGateBias());
telsoa01c577f2c2018-08-31 09:22:23 +0100315 lstm_params_info.set_cifg_params(&aclInputToInputWeightsInfo, &aclRecurrentToInputWeightsInfo,
Jan Eilersd01a83c2019-07-03 18:20:40 +0100316 paramsInfo.m_CellToInputWeights != nullptr ?
317 &aclCellToInputWeightsInfo: nullptr,
telsoa01c577f2c2018-08-31 09:22:23 +0100318 &aclInputGateBiasInfo);
319 }
320
321 if (descriptor.m_ProjectionEnabled)
322 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100323 aclProjectionWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetProjectionWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100324
Jan Eilersd01a83c2019-07-03 18:20:40 +0100325 if (paramsInfo.m_ProjectionBias != nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +0100326 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100327 aclProjectionBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputGateBias());
telsoa01c577f2c2018-08-31 09:22:23 +0100328 }
329 lstm_params_info.set_projection_params(&aclProjectionWeightsInfo,
Jan Eilersd01a83c2019-07-03 18:20:40 +0100330 paramsInfo.m_ProjectionBias != nullptr ?
331 &aclProjectionBiasInfo: nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100332 }
333
334 if (descriptor.m_PeepholeEnabled)
335 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100336 aclCellToForgetWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellToForgetWeights());
337 aclCellToOutputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellToOutputWeights());
telsoa01c577f2c2018-08-31 09:22:23 +0100338 lstm_params_info.set_peephole_params(&aclCellToForgetWeightsInfo, &aclCellToOutputWeightsInfo);
339 }
340
341 float cell_threshold = descriptor.m_ClippingThresCell;
342 float projection_threshold = descriptor.m_ClippingThresProj;
343
344 // for preparing the object for the class ActivationLayerInfo, we need to consider 5 situations
345 arm_compute::ActivationLayerInfo activationLayerInfo;
346 if (descriptor.m_ActivationFunc == 0)
347 {
348 // no activation, do nothing
349 }
350 else if (descriptor.m_ActivationFunc == 1)
351 {
352 activationLayerInfo = arm_compute::ActivationLayerInfo(
353 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
354 }
355 else if (descriptor.m_ActivationFunc == 3)
356 {
357 activationLayerInfo = arm_compute::ActivationLayerInfo(
358 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
359 }
360 else if (descriptor.m_ActivationFunc == 4)
361 {
362 activationLayerInfo = arm_compute::ActivationLayerInfo(
363 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
364 }
365 else if (descriptor.m_ActivationFunc == 6)
366 {
367 activationLayerInfo = arm_compute::ActivationLayerInfo(
368 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
369 }
370 else
371 {
372 throw armnn::Exception("Wrong Type of Activation Function!");
373 }
374
Jan Eilersa2ec9092019-07-08 15:56:59 +0100375 if (descriptor.m_LayerNormEnabled)
376 {
377 if (!descriptor.m_CifgEnabled)
378 {
Francis Murtaghbb590b42019-08-14 09:51:36 +0100379 aclInputLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputLayerNormWeights());
Jan Eilersa2ec9092019-07-08 15:56:59 +0100380 }
381
Francis Murtaghbb590b42019-08-14 09:51:36 +0100382 aclForgetLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetForgetLayerNormWeights());
Jan Eilersa2ec9092019-07-08 15:56:59 +0100383
Francis Murtaghbb590b42019-08-14 09:51:36 +0100384 aclCellLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellLayerNormWeights());
Jan Eilersa2ec9092019-07-08 15:56:59 +0100385
Francis Murtaghbb590b42019-08-14 09:51:36 +0100386 aclOutputLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.GetOutputLayerNormWeights());
Jan Eilersa2ec9092019-07-08 15:56:59 +0100387
388 lstm_params_info.set_layer_normalization_params(descriptor.m_CifgEnabled ?
389 nullptr : &aclInputLayerNormWeightsInfo,
390 &aclForgetLayerNormWeightsInfo,
391 &aclCellLayerNormWeightsInfo,
392 &aclOutputLayerNormWeightsInfo);
393 }
394
telsoa01c577f2c2018-08-31 09:22:23 +0100395 return arm_compute::CLLSTMLayer::validate(&aclInputInfo, &aclInputToForgetWeightsInfo,
396 &aclInputToCellWeightsInfo,
397 &aclInputToOutputWeightsInfo,
398 &aclRecurrentToForgetWeightsInfo,
399 &aclRecurrentToCellWeightsInfo,
400 &aclRecurrentToOutputWeightsInfo,
401 &aclForgetGateBiasInfo,
402 &aclCellBiasInfo,
403 &aclOutputGateBiasInfo,
404 &aclOutputStateInInfo, &aclCellStateInInfo,
405 &aclScratchBufferInfo, &aclOutputStateOutInfo,
406 &aclCellStateOutInfo, &aclOutputInfo,
407 lstm_params_info, activationLayerInfo,
408 cell_threshold, projection_threshold);
409}
410
arovir019e53a352018-08-31 15:26:35 +0100411void ClLstmFloatWorkload::FreeUnusedTensors()
telsoa01c577f2c2018-08-31 09:22:23 +0100412{
413 FreeTensorIfUnused(m_InputToInputWeightsTensor);
414 FreeTensorIfUnused(m_InputToForgetWeightsTensor);
415 FreeTensorIfUnused(m_InputToCellWeightsTensor);
416 FreeTensorIfUnused(m_InputToOutputWeightsTensor);
417 FreeTensorIfUnused(m_RecurrentToInputWeightsTensor);
418 FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor);
419 FreeTensorIfUnused(m_RecurrentToCellWeightsTensor);
420 FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor);
421 FreeTensorIfUnused(m_CellToInputWeightsTensor);
422 FreeTensorIfUnused(m_CellToForgetWeightsTensor);
423 FreeTensorIfUnused(m_CellToOutputWeightsTensor);
424 FreeTensorIfUnused(m_InputGateBiasTensor);
425 FreeTensorIfUnused(m_ForgetGateBiasTensor);
426 FreeTensorIfUnused(m_CellBiasTensor);
427 FreeTensorIfUnused(m_OutputGateBiasTensor);
428 FreeTensorIfUnused(m_ProjectionWeightsTensor);
429 FreeTensorIfUnused(m_ProjectionBiasTensor);
430 FreeTensorIfUnused(m_ScratchBuffer);
Jan Eilersa2ec9092019-07-08 15:56:59 +0100431 FreeTensorIfUnused(m_InputLayerNormWeightsTensor);
432 FreeTensorIfUnused(m_ForgetLayerNormWeightsTensor);
433 FreeTensorIfUnused(m_CellLayerNormWeightsTensor);
434 FreeTensorIfUnused(m_OutputLayerNormWeightsTensor);
telsoa01c577f2c2018-08-31 09:22:23 +0100435}
436
437} //namespace armnn