17 using namespace armcomputetensorutils;
22 arm_compute::LSTMParams<arm_compute::ITensor> lstm_param;
25 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
26 BuildArmComputeTensor(*m_InputToForgetWeightsTensor,
m_Data.m_InputToForgetWeights->GetTensorInfo());
28 m_InputToCellWeightsTensor = std::make_unique<arm_compute::Tensor>();
29 BuildArmComputeTensor(*m_InputToCellWeightsTensor,
m_Data.m_InputToCellWeights->GetTensorInfo());
31 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
32 BuildArmComputeTensor(*m_InputToOutputWeightsTensor,
m_Data.m_InputToOutputWeights->GetTensorInfo());
34 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
35 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor,
m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
37 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::Tensor>();
38 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor,
m_Data.m_RecurrentToCellWeights->GetTensorInfo());
40 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
41 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor,
m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
43 m_ForgetGateBiasTensor = std::make_unique<arm_compute::Tensor>();
44 BuildArmComputeTensor(*m_ForgetGateBiasTensor,
m_Data.m_ForgetGateBias->GetTensorInfo());
46 m_CellBiasTensor = std::make_unique<arm_compute::Tensor>();
47 BuildArmComputeTensor(*m_CellBiasTensor,
m_Data.m_CellBias->GetTensorInfo());
49 m_OutputGateBiasTensor = std::make_unique<arm_compute::Tensor>();
50 BuildArmComputeTensor(*m_OutputGateBiasTensor,
m_Data.m_OutputGateBias->GetTensorInfo());
53 if (!
m_Data.m_Parameters.m_CifgEnabled)
55 m_InputToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
56 BuildArmComputeTensor(*m_InputToInputWeightsTensor,
m_Data.m_InputToInputWeights->GetTensorInfo());
58 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
59 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor,
m_Data.m_RecurrentToInputWeights->GetTensorInfo());
61 m_CellToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
62 if (
m_Data.m_CellToInputWeights !=
nullptr)
64 BuildArmComputeTensor(*m_CellToInputWeightsTensor,
m_Data.m_CellToInputWeights->GetTensorInfo());
67 m_InputGateBiasTensor = std::make_unique<arm_compute::Tensor>();
68 BuildArmComputeTensor(*m_InputGateBiasTensor,
m_Data.m_InputGateBias->GetTensorInfo());
70 lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
71 m_RecurrentToInputWeightsTensor.get(),
72 m_Data.m_CellToInputWeights !=
nullptr ? m_CellToInputWeightsTensor.get() :
nullptr,
73 m_InputGateBiasTensor.get());
76 if (
m_Data.m_Parameters.m_ProjectionEnabled)
78 m_ProjectionWeightsTensor = std::make_unique<arm_compute::Tensor>();
79 BuildArmComputeTensor(*m_ProjectionWeightsTensor,
m_Data.m_ProjectionWeights->GetTensorInfo());
81 m_ProjectionBiasTensor = std::make_unique<arm_compute::Tensor>();
82 if (
m_Data.m_ProjectionBias !=
nullptr)
84 BuildArmComputeTensor(*m_ProjectionBiasTensor,
m_Data.m_ProjectionBias->GetTensorInfo());
87 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
88 m_Data.m_ProjectionBias !=
nullptr ? m_ProjectionBiasTensor.get() :
nullptr);
91 if (
m_Data.m_Parameters.m_PeepholeEnabled)
93 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
94 BuildArmComputeTensor(*m_CellToForgetWeightsTensor,
m_Data.m_CellToForgetWeights->GetTensorInfo());
96 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
97 BuildArmComputeTensor(*m_CellToOutputWeightsTensor,
m_Data.m_CellToOutputWeights->GetTensorInfo());
99 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
102 if (
m_Data.m_Parameters.m_LayerNormEnabled)
104 m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
105 if (!
m_Data.m_Parameters.m_CifgEnabled)
107 BuildArmComputeTensor(*m_InputLayerNormWeightsTensor,
m_Data.m_InputLayerNormWeights->GetTensorInfo());
110 m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
111 BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor,
m_Data.m_ForgetLayerNormWeights->GetTensorInfo());
113 m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
114 BuildArmComputeTensor(*m_CellLayerNormWeightsTensor,
m_Data.m_CellLayerNormWeights->GetTensorInfo());
116 m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
117 BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor,
m_Data.m_OutputLayerNormWeights->GetTensorInfo());
119 lstm_param.set_layer_normalization_params(
m_Data.m_Parameters.m_CifgEnabled ?
120 nullptr : m_InputLayerNormWeightsTensor.get(),
121 m_ForgetLayerNormWeightsTensor.get(),
122 m_CellLayerNormWeightsTensor.get(),
123 m_OutputLayerNormWeightsTensor.get());
139 m_ScratchBuffer = std::make_unique<arm_compute::Tensor>();
140 if (
m_Data.m_Parameters.m_CifgEnabled)
144 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
150 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2);
153 float cell_threshold =
m_Data.m_Parameters.m_ClippingThresCell;
154 float projection_threshold =
m_Data.m_Parameters.m_ClippingThresProj;
157 arm_compute::ActivationLayerInfo activationLayerInfo;
158 if (
m_Data.m_Parameters.m_ActivationFunc == 0)
162 else if (
m_Data.m_Parameters.m_ActivationFunc == 1)
164 activationLayerInfo = arm_compute::ActivationLayerInfo(
165 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
167 else if (
m_Data.m_Parameters.m_ActivationFunc == 3)
169 activationLayerInfo = arm_compute::ActivationLayerInfo(
170 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
172 else if (
m_Data.m_Parameters.m_ActivationFunc == 4)
174 activationLayerInfo = arm_compute::ActivationLayerInfo(
175 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
177 else if (
m_Data.m_Parameters.m_ActivationFunc == 6)
179 activationLayerInfo = arm_compute::ActivationLayerInfo(
180 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
188 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);
196 armcomputetensorutils::InitialiseArmComputeTensorEmpty(*m_ScratchBuffer);
199 m_Data.m_InputToForgetWeights);
201 m_Data.m_InputToCellWeights);
203 m_Data.m_InputToOutputWeights);
205 m_Data.m_RecurrentToForgetWeights);
207 m_Data.m_RecurrentToCellWeights);
209 m_Data.m_RecurrentToOutputWeights);
217 if (!
m_Data.m_Parameters.m_CifgEnabled)
220 m_Data.m_InputToInputWeights);
222 m_Data.m_RecurrentToInputWeights);
223 if (
m_Data.m_CellToInputWeights !=
nullptr)
226 m_Data.m_CellToInputWeights);
232 if (
m_Data.m_Parameters.m_ProjectionEnabled)
235 m_Data.m_ProjectionWeights);
236 if (
m_Data.m_ProjectionBias !=
nullptr)
243 if (
m_Data.m_Parameters.m_PeepholeEnabled)
246 m_Data.m_CellToForgetWeights);
248 m_Data.m_CellToOutputWeights);
251 if (
m_Data.m_Parameters.m_LayerNormEnabled)
253 if (!
m_Data.m_Parameters.m_CifgEnabled)
264 m_LstmLayer.prepare();
283 arm_compute::LSTMParams<arm_compute::ITensorInfo> lstm_params_info;
286 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
287 const arm_compute::TensorInfo aclOutputStateInInfo = BuildArmComputeTensorInfo(outputStateIn);
288 const arm_compute::TensorInfo aclCellStateInInfo = BuildArmComputeTensorInfo(cellStateIn);
289 const arm_compute::TensorInfo aclScratchBufferInfo = BuildArmComputeTensorInfo(scratchBuffer);
290 const arm_compute::TensorInfo aclOutputStateOutInfo = BuildArmComputeTensorInfo(outputStateOut);
291 const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut);
292 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
295 const arm_compute::TensorInfo aclInputToForgetWeightsInfo
297 const arm_compute::TensorInfo aclInputToCellWeightsInfo
299 const arm_compute::TensorInfo aclInputToOutputWeightsInfo
301 const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo
303 const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo
305 const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo
307 const arm_compute::TensorInfo aclForgetGateBiasInfo
309 const arm_compute::TensorInfo aclCellBiasInfo
310 = BuildArmComputeTensorInfo(paramsInfo.
GetCellBias());
311 const arm_compute::TensorInfo aclOutputGateBiasInfo
314 arm_compute::TensorInfo aclInputToInputWeightsInfo;
315 arm_compute::TensorInfo aclRecurrentToInputWeightsInfo;
316 arm_compute::TensorInfo aclCellToInputWeightsInfo;
317 arm_compute::TensorInfo aclInputGateBiasInfo;
318 arm_compute::TensorInfo aclProjectionWeightsInfo;
319 arm_compute::TensorInfo aclProjectionBiasInfo;
320 arm_compute::TensorInfo aclCellToForgetWeightsInfo;
321 arm_compute::TensorInfo aclCellToOutputWeightsInfo;
323 arm_compute::TensorInfo aclInputLayerNormWeightsInfo;
324 arm_compute::TensorInfo aclForgetLayerNormWeightsInfo;
325 arm_compute::TensorInfo aclCellLayerNormWeightsInfo;
326 arm_compute::TensorInfo aclOutputLayerNormWeightsInfo;
337 aclInputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetInputGateBias());
339 lstm_params_info.set_cifg_params(&aclInputToInputWeightsInfo, &aclRecurrentToInputWeightsInfo,
341 &aclInputGateBiasInfo);
348 aclProjectionBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetProjectionBias());
352 lstm_params_info.set_projection_params(&aclProjectionWeightsInfo,
354 &aclProjectionBiasInfo :
nullptr);
362 lstm_params_info.set_peephole_params(&aclCellToForgetWeightsInfo, &aclCellToOutputWeightsInfo);
375 lstm_params_info.set_layer_normalization_params(descriptor.
m_CifgEnabled ?
376 nullptr : &aclInputLayerNormWeightsInfo,
377 &aclForgetLayerNormWeightsInfo,
378 &aclCellLayerNormWeightsInfo,
379 &aclOutputLayerNormWeightsInfo);
386 arm_compute::ActivationLayerInfo activationLayerInfo;
393 activationLayerInfo = arm_compute::ActivationLayerInfo(
394 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
397 activationLayerInfo = arm_compute::ActivationLayerInfo(
398 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
401 activationLayerInfo = arm_compute::ActivationLayerInfo(
402 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
405 activationLayerInfo = arm_compute::ActivationLayerInfo(
406 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
412 return arm_compute::NELSTMLayer::validate(&aclInputInfo,
413 &aclInputToForgetWeightsInfo,
414 &aclInputToCellWeightsInfo,
415 &aclInputToOutputWeightsInfo,
416 &aclRecurrentToForgetWeightsInfo,
417 &aclRecurrentToCellWeightsInfo,
418 &aclRecurrentToOutputWeightsInfo,
419 &aclForgetGateBiasInfo,
421 &aclOutputGateBiasInfo,
422 &aclOutputStateInInfo,
424 &aclScratchBufferInfo,
425 &aclOutputStateOutInfo,
426 &aclCellStateOutInfo,
431 projection_threshold);
434 void NeonLstmFloatWorkload::FreeUnusedTensors()
436 FreeTensorIfUnused(m_InputToInputWeightsTensor);
437 FreeTensorIfUnused(m_InputToForgetWeightsTensor);
438 FreeTensorIfUnused(m_InputToCellWeightsTensor);
439 FreeTensorIfUnused(m_InputToOutputWeightsTensor);
440 FreeTensorIfUnused(m_RecurrentToInputWeightsTensor);
441 FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor);
442 FreeTensorIfUnused(m_RecurrentToCellWeightsTensor);
443 FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor);
444 FreeTensorIfUnused(m_CellToInputWeightsTensor);
445 FreeTensorIfUnused(m_CellToForgetWeightsTensor);
446 FreeTensorIfUnused(m_CellToOutputWeightsTensor);
447 FreeTensorIfUnused(m_InputGateBiasTensor);
448 FreeTensorIfUnused(m_ForgetGateBiasTensor);
449 FreeTensorIfUnused(m_CellBiasTensor);
450 FreeTensorIfUnused(m_OutputGateBiasTensor);
451 FreeTensorIfUnused(m_ProjectionWeightsTensor);
452 FreeTensorIfUnused(m_ProjectionBiasTensor);
453 FreeTensorIfUnused(m_ScratchBuffer);
454 FreeTensorIfUnused(m_InputLayerNormWeightsTensor);
455 FreeTensorIfUnused(m_ForgetLayerNormWeightsTensor);
456 FreeTensorIfUnused(m_CellLayerNormWeightsTensor);
457 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) 2021 ARM Limited and Contributors.
std::vector< TensorInfo > m_InputTensorInfos
An LstmDescriptor for the LstmLayer.
bool m_PeepholeEnabled
Enable/disable peephole.
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.
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Contains information about inputs and outputs to a layer.
std::vector< ITensorHandle * > m_Inputs