15 using namespace armcomputetensorutils;
20 arm_compute::LSTMParams<arm_compute::ITensor> lstm_param;
23 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
24 BuildArmComputeTensor(*m_InputToForgetWeightsTensor,
m_Data.m_InputToForgetWeights->GetTensorInfo());
26 m_InputToCellWeightsTensor = std::make_unique<arm_compute::Tensor>();
27 BuildArmComputeTensor(*m_InputToCellWeightsTensor,
m_Data.m_InputToCellWeights->GetTensorInfo());
29 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
30 BuildArmComputeTensor(*m_InputToOutputWeightsTensor,
m_Data.m_InputToOutputWeights->GetTensorInfo());
32 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
33 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor,
m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
35 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::Tensor>();
36 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor,
m_Data.m_RecurrentToCellWeights->GetTensorInfo());
38 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
39 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor,
m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
41 m_ForgetGateBiasTensor = std::make_unique<arm_compute::Tensor>();
42 BuildArmComputeTensor(*m_ForgetGateBiasTensor,
m_Data.m_ForgetGateBias->GetTensorInfo());
44 m_CellBiasTensor = std::make_unique<arm_compute::Tensor>();
45 BuildArmComputeTensor(*m_CellBiasTensor,
m_Data.m_CellBias->GetTensorInfo());
47 m_OutputGateBiasTensor = std::make_unique<arm_compute::Tensor>();
48 BuildArmComputeTensor(*m_OutputGateBiasTensor,
m_Data.m_OutputGateBias->GetTensorInfo());
51 if (!
m_Data.m_Parameters.m_CifgEnabled)
53 m_InputToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
54 BuildArmComputeTensor(*m_InputToInputWeightsTensor,
m_Data.m_InputToInputWeights->GetTensorInfo());
56 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
57 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor,
m_Data.m_RecurrentToInputWeights->GetTensorInfo());
59 m_CellToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
60 if (
m_Data.m_CellToInputWeights !=
nullptr)
62 BuildArmComputeTensor(*m_CellToInputWeightsTensor,
m_Data.m_CellToInputWeights->GetTensorInfo());
65 m_InputGateBiasTensor = std::make_unique<arm_compute::Tensor>();
66 BuildArmComputeTensor(*m_InputGateBiasTensor,
m_Data.m_InputGateBias->GetTensorInfo());
68 lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
69 m_RecurrentToInputWeightsTensor.get(),
70 m_Data.m_CellToInputWeights !=
nullptr ? m_CellToInputWeightsTensor.get() :
nullptr,
71 m_InputGateBiasTensor.get());
74 if (
m_Data.m_Parameters.m_ProjectionEnabled)
76 m_ProjectionWeightsTensor = std::make_unique<arm_compute::Tensor>();
77 BuildArmComputeTensor(*m_ProjectionWeightsTensor,
m_Data.m_ProjectionWeights->GetTensorInfo());
79 m_ProjectionBiasTensor = std::make_unique<arm_compute::Tensor>();
80 if (
m_Data.m_ProjectionBias !=
nullptr)
82 BuildArmComputeTensor(*m_ProjectionBiasTensor,
m_Data.m_ProjectionBias->GetTensorInfo());
85 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
86 m_Data.m_ProjectionBias !=
nullptr ? m_ProjectionBiasTensor.get() :
nullptr);
89 if (
m_Data.m_Parameters.m_PeepholeEnabled)
91 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
92 BuildArmComputeTensor(*m_CellToForgetWeightsTensor,
m_Data.m_CellToForgetWeights->GetTensorInfo());
94 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
95 BuildArmComputeTensor(*m_CellToOutputWeightsTensor,
m_Data.m_CellToOutputWeights->GetTensorInfo());
97 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
100 if (
m_Data.m_Parameters.m_LayerNormEnabled)
102 m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
103 if (!
m_Data.m_Parameters.m_CifgEnabled)
105 BuildArmComputeTensor(*m_InputLayerNormWeightsTensor,
m_Data.m_InputLayerNormWeights->GetTensorInfo());
108 m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
109 BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor,
m_Data.m_ForgetLayerNormWeights->GetTensorInfo());
111 m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
112 BuildArmComputeTensor(*m_CellLayerNormWeightsTensor,
m_Data.m_CellLayerNormWeights->GetTensorInfo());
114 m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
115 BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor,
m_Data.m_OutputLayerNormWeights->GetTensorInfo());
117 lstm_param.set_layer_normalization_params(
m_Data.m_Parameters.m_CifgEnabled ?
118 nullptr : m_InputLayerNormWeightsTensor.get(),
119 m_ForgetLayerNormWeightsTensor.get(),
120 m_CellLayerNormWeightsTensor.get(),
121 m_OutputLayerNormWeightsTensor.get());
137 m_ScratchBuffer = std::make_unique<arm_compute::Tensor>();
138 if (
m_Data.m_Parameters.m_CifgEnabled)
142 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
148 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2);
151 float cell_threshold =
m_Data.m_Parameters.m_ClippingThresCell;
152 float projection_threshold =
m_Data.m_Parameters.m_ClippingThresProj;
155 arm_compute::ActivationLayerInfo activationLayerInfo;
156 if (
m_Data.m_Parameters.m_ActivationFunc == 0)
160 else if (
m_Data.m_Parameters.m_ActivationFunc == 1)
162 activationLayerInfo = arm_compute::ActivationLayerInfo(
163 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
165 else if (
m_Data.m_Parameters.m_ActivationFunc == 3)
167 activationLayerInfo = arm_compute::ActivationLayerInfo(
168 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
170 else if (
m_Data.m_Parameters.m_ActivationFunc == 4)
172 activationLayerInfo = arm_compute::ActivationLayerInfo(
173 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
175 else if (
m_Data.m_Parameters.m_ActivationFunc == 6)
177 activationLayerInfo = arm_compute::ActivationLayerInfo(
178 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
186 m_LstmLayer.configure(&input, m_InputToForgetWeightsTensor.get(), m_InputToCellWeightsTensor.get(),
187 m_InputToOutputWeightsTensor.get(), m_RecurrentToForgetWeightsTensor.get(),
188 m_RecurrentToCellWeightsTensor.get(), m_RecurrentToOutputWeightsTensor.get(),
189 m_ForgetGateBiasTensor.get(), m_CellBiasTensor.get(), m_OutputGateBiasTensor.get(),
190 &output_state_in, &cell_state_in, m_ScratchBuffer.get(), &output_state_out,
191 &cell_state_out, &output, lstm_param, activationLayerInfo,
192 cell_threshold, projection_threshold);
194 armcomputetensorutils::InitialiseArmComputeTensorEmpty(*m_ScratchBuffer);
197 m_Data.m_InputToForgetWeights);
199 m_Data.m_InputToCellWeights);
201 m_Data.m_InputToOutputWeights);
203 m_Data.m_RecurrentToForgetWeights);
205 m_Data.m_RecurrentToCellWeights);
207 m_Data.m_RecurrentToOutputWeights);
215 if (!
m_Data.m_Parameters.m_CifgEnabled)
218 m_Data.m_InputToInputWeights);
220 m_Data.m_RecurrentToInputWeights);
221 if (
m_Data.m_CellToInputWeights !=
nullptr)
224 m_Data.m_CellToInputWeights);
230 if (
m_Data.m_Parameters.m_ProjectionEnabled)
233 m_Data.m_ProjectionWeights);
234 if (
m_Data.m_ProjectionBias !=
nullptr)
241 if (
m_Data.m_Parameters.m_PeepholeEnabled)
244 m_Data.m_CellToForgetWeights);
246 m_Data.m_CellToOutputWeights);
249 if (
m_Data.m_Parameters.m_LayerNormEnabled)
251 if (!
m_Data.m_Parameters.m_CifgEnabled)
262 m_LstmLayer.prepare();
281 arm_compute::LSTMParams<arm_compute::ITensorInfo> lstm_params_info;
284 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
285 const arm_compute::TensorInfo aclOutputStateInInfo = BuildArmComputeTensorInfo(outputStateIn);
286 const arm_compute::TensorInfo aclCellStateInInfo = BuildArmComputeTensorInfo(cellStateIn);
287 const arm_compute::TensorInfo aclScratchBufferInfo = BuildArmComputeTensorInfo(scratchBuffer);
288 const arm_compute::TensorInfo aclOutputStateOutInfo = BuildArmComputeTensorInfo(outputStateOut);
289 const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut);
290 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
293 const arm_compute::TensorInfo aclInputToForgetWeightsInfo
295 const arm_compute::TensorInfo aclInputToCellWeightsInfo
297 const arm_compute::TensorInfo aclInputToOutputWeightsInfo
299 const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo
301 const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo
303 const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo
305 const arm_compute::TensorInfo aclForgetGateBiasInfo
307 const arm_compute::TensorInfo aclCellBiasInfo
308 = BuildArmComputeTensorInfo(paramsInfo.
GetCellBias());
309 const arm_compute::TensorInfo aclOutputGateBiasInfo
312 arm_compute::TensorInfo aclInputToInputWeightsInfo;
313 arm_compute::TensorInfo aclRecurrentToInputWeightsInfo;
314 arm_compute::TensorInfo aclCellToInputWeightsInfo;
315 arm_compute::TensorInfo aclInputGateBiasInfo;
316 arm_compute::TensorInfo aclProjectionWeightsInfo;
317 arm_compute::TensorInfo aclProjectionBiasInfo;
318 arm_compute::TensorInfo aclCellToForgetWeightsInfo;
319 arm_compute::TensorInfo aclCellToOutputWeightsInfo;
321 arm_compute::TensorInfo aclInputLayerNormWeightsInfo;
322 arm_compute::TensorInfo aclForgetLayerNormWeightsInfo;
323 arm_compute::TensorInfo aclCellLayerNormWeightsInfo;
324 arm_compute::TensorInfo aclOutputLayerNormWeightsInfo;
335 aclInputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetInputGateBias());
337 lstm_params_info.set_cifg_params(&aclInputToInputWeightsInfo, &aclRecurrentToInputWeightsInfo,
339 &aclInputGateBiasInfo);
346 aclProjectionBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetProjectionBias());
350 lstm_params_info.set_projection_params(&aclProjectionWeightsInfo,
352 &aclProjectionBiasInfo :
nullptr);
360 lstm_params_info.set_peephole_params(&aclCellToForgetWeightsInfo, &aclCellToOutputWeightsInfo);
373 lstm_params_info.set_layer_normalization_params(descriptor.
m_CifgEnabled ?
374 nullptr : &aclInputLayerNormWeightsInfo,
375 &aclForgetLayerNormWeightsInfo,
376 &aclCellLayerNormWeightsInfo,
377 &aclOutputLayerNormWeightsInfo);
384 arm_compute::ActivationLayerInfo activationLayerInfo;
391 activationLayerInfo = arm_compute::ActivationLayerInfo(
392 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
395 activationLayerInfo = arm_compute::ActivationLayerInfo(
396 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
399 activationLayerInfo = arm_compute::ActivationLayerInfo(
400 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
403 activationLayerInfo = arm_compute::ActivationLayerInfo(
404 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
410 return arm_compute::NELSTMLayer::validate(&aclInputInfo,
411 &aclInputToForgetWeightsInfo,
412 &aclInputToCellWeightsInfo,
413 &aclInputToOutputWeightsInfo,
414 &aclRecurrentToForgetWeightsInfo,
415 &aclRecurrentToCellWeightsInfo,
416 &aclRecurrentToOutputWeightsInfo,
417 &aclForgetGateBiasInfo,
419 &aclOutputGateBiasInfo,
420 &aclOutputStateInInfo,
422 &aclScratchBufferInfo,
423 &aclOutputStateOutInfo,
424 &aclCellStateOutInfo,
429 projection_threshold);
432 void NeonLstmFloatWorkload::FreeUnusedTensors()
434 FreeTensorIfUnused(m_InputToInputWeightsTensor);
435 FreeTensorIfUnused(m_InputToForgetWeightsTensor);
436 FreeTensorIfUnused(m_InputToCellWeightsTensor);
437 FreeTensorIfUnused(m_InputToOutputWeightsTensor);
438 FreeTensorIfUnused(m_RecurrentToInputWeightsTensor);
439 FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor);
440 FreeTensorIfUnused(m_RecurrentToCellWeightsTensor);
441 FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor);
442 FreeTensorIfUnused(m_CellToInputWeightsTensor);
443 FreeTensorIfUnused(m_CellToForgetWeightsTensor);
444 FreeTensorIfUnused(m_CellToOutputWeightsTensor);
445 FreeTensorIfUnused(m_InputGateBiasTensor);
446 FreeTensorIfUnused(m_ForgetGateBiasTensor);
447 FreeTensorIfUnused(m_CellBiasTensor);
448 FreeTensorIfUnused(m_OutputGateBiasTensor);
449 FreeTensorIfUnused(m_ProjectionWeightsTensor);
450 FreeTensorIfUnused(m_ProjectionBiasTensor);
451 FreeTensorIfUnused(m_ScratchBuffer);
452 FreeTensorIfUnused(m_InputLayerNormWeightsTensor);
453 FreeTensorIfUnused(m_ForgetLayerNormWeightsTensor);
454 FreeTensorIfUnused(m_CellLayerNormWeightsTensor);
455 FreeTensorIfUnused(m_OutputLayerNormWeightsTensor);
bool m_ProjectionEnabled
Enable/disable the projection layer.
const TensorShape & GetShape() const
float m_ClippingThresProj
Clipping threshold value for the projection.
const QueueDescriptor m_Data
virtual void Execute() const override
arm_compute::Status NeonLstmFloatWorkloadValidate(const TensorInfo &input, const TensorInfo &outputStateIn, const TensorInfo &cellStateIn, const TensorInfo &scratchBuffer, const TensorInfo &outputStateOut, const TensorInfo &cellStateOut, const TensorInfo &output, const LstmDescriptor &descriptor, const LstmInputParamsInfo ¶msInfo)
Copyright (c) 2020 ARM Limited.
std::vector< TensorInfo > m_InputTensorInfos
An LstmDescriptor for the LstmLayer.
bool m_PeepholeEnabled
Enable/disable peephole.
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
uint32_t m_ActivationFunc
The activation function to use.
float m_ClippingThresCell
Clipping threshold value for the cell state.
void InitializeArmComputeTensorData(arm_compute::Tensor &tensor, const ConstCpuTensorHandle *handle)
bool m_CifgEnabled
Enable/disable cifg (coupled input & forget gate).
std::vector< ITensorHandle * > m_Outputs
NeonLstmFloatWorkload(const LstmQueueDescriptor &descriptor, const WorkloadInfo &info)
Base class for all ArmNN exceptions so that users can filter to just those.
bool m_LayerNormEnabled
Enable/disable layer normalization.
Contains information about inputs and outputs to a layer.
std::vector< ITensorHandle * > m_Inputs