17 using namespace armcomputetensorutils;
28 arm_compute::LSTMParams<arm_compute::ITensor> lstm_param;
31 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
32 BuildArmComputeTensor(*m_InputToForgetWeightsTensor,
m_Data.m_InputToForgetWeights->GetTensorInfo());
34 m_InputToCellWeightsTensor = std::make_unique<arm_compute::Tensor>();
35 BuildArmComputeTensor(*m_InputToCellWeightsTensor,
m_Data.m_InputToCellWeights->GetTensorInfo());
37 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
38 BuildArmComputeTensor(*m_InputToOutputWeightsTensor,
m_Data.m_InputToOutputWeights->GetTensorInfo());
40 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
41 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor,
m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
43 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::Tensor>();
44 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor,
m_Data.m_RecurrentToCellWeights->GetTensorInfo());
46 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
47 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor,
m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
49 m_ForgetGateBiasTensor = std::make_unique<arm_compute::Tensor>();
50 BuildArmComputeTensor(*m_ForgetGateBiasTensor,
m_Data.m_ForgetGateBias->GetTensorInfo());
52 m_CellBiasTensor = std::make_unique<arm_compute::Tensor>();
53 BuildArmComputeTensor(*m_CellBiasTensor,
m_Data.m_CellBias->GetTensorInfo());
55 m_OutputGateBiasTensor = std::make_unique<arm_compute::Tensor>();
56 BuildArmComputeTensor(*m_OutputGateBiasTensor,
m_Data.m_OutputGateBias->GetTensorInfo());
59 if (!
m_Data.m_Parameters.m_CifgEnabled)
61 m_InputToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
62 BuildArmComputeTensor(*m_InputToInputWeightsTensor,
m_Data.m_InputToInputWeights->GetTensorInfo());
64 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
65 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor,
m_Data.m_RecurrentToInputWeights->GetTensorInfo());
67 m_CellToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
68 if (
m_Data.m_CellToInputWeights !=
nullptr)
70 BuildArmComputeTensor(*m_CellToInputWeightsTensor,
m_Data.m_CellToInputWeights->GetTensorInfo());
73 m_InputGateBiasTensor = std::make_unique<arm_compute::Tensor>();
74 BuildArmComputeTensor(*m_InputGateBiasTensor,
m_Data.m_InputGateBias->GetTensorInfo());
76 lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
77 m_RecurrentToInputWeightsTensor.get(),
78 m_Data.m_CellToInputWeights !=
nullptr ? m_CellToInputWeightsTensor.get() :
nullptr,
79 m_InputGateBiasTensor.get());
82 if (
m_Data.m_Parameters.m_ProjectionEnabled)
84 m_ProjectionWeightsTensor = std::make_unique<arm_compute::Tensor>();
85 BuildArmComputeTensor(*m_ProjectionWeightsTensor,
m_Data.m_ProjectionWeights->GetTensorInfo());
87 m_ProjectionBiasTensor = std::make_unique<arm_compute::Tensor>();
88 if (
m_Data.m_ProjectionBias !=
nullptr)
90 BuildArmComputeTensor(*m_ProjectionBiasTensor,
m_Data.m_ProjectionBias->GetTensorInfo());
93 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
94 m_Data.m_ProjectionBias !=
nullptr ? m_ProjectionBiasTensor.get() :
nullptr);
97 if (
m_Data.m_Parameters.m_PeepholeEnabled)
99 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
100 BuildArmComputeTensor(*m_CellToForgetWeightsTensor,
m_Data.m_CellToForgetWeights->GetTensorInfo());
102 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
103 BuildArmComputeTensor(*m_CellToOutputWeightsTensor,
m_Data.m_CellToOutputWeights->GetTensorInfo());
105 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
108 if (
m_Data.m_Parameters.m_LayerNormEnabled)
110 m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
111 if (!
m_Data.m_Parameters.m_CifgEnabled)
113 BuildArmComputeTensor(*m_InputLayerNormWeightsTensor,
m_Data.m_InputLayerNormWeights->GetTensorInfo());
116 m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
117 BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor,
m_Data.m_ForgetLayerNormWeights->GetTensorInfo());
119 m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
120 BuildArmComputeTensor(*m_CellLayerNormWeightsTensor,
m_Data.m_CellLayerNormWeights->GetTensorInfo());
122 m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
123 BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor,
m_Data.m_OutputLayerNormWeights->GetTensorInfo());
125 lstm_param.set_layer_normalization_params(
m_Data.m_Parameters.m_CifgEnabled ?
126 nullptr : m_InputLayerNormWeightsTensor.get(),
127 m_ForgetLayerNormWeightsTensor.get(),
128 m_CellLayerNormWeightsTensor.get(),
129 m_OutputLayerNormWeightsTensor.get());
145 m_ScratchBuffer = std::make_unique<arm_compute::Tensor>();
146 if (
m_Data.m_Parameters.m_CifgEnabled)
150 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
156 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2);
159 float cell_threshold =
m_Data.m_Parameters.m_ClippingThresCell;
160 float projection_threshold =
m_Data.m_Parameters.m_ClippingThresProj;
163 arm_compute::ActivationLayerInfo activationLayerInfo;
164 if (
m_Data.m_Parameters.m_ActivationFunc == 0)
168 else if (
m_Data.m_Parameters.m_ActivationFunc == 1)
170 activationLayerInfo = arm_compute::ActivationLayerInfo(
171 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
173 else if (
m_Data.m_Parameters.m_ActivationFunc == 3)
175 activationLayerInfo = arm_compute::ActivationLayerInfo(
176 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
178 else if (
m_Data.m_Parameters.m_ActivationFunc == 4)
180 activationLayerInfo = arm_compute::ActivationLayerInfo(
181 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
183 else if (
m_Data.m_Parameters.m_ActivationFunc == 6)
185 activationLayerInfo = arm_compute::ActivationLayerInfo(
186 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
194 m_LstmLayer.configure(&input, m_InputToForgetWeightsTensor.get(), m_InputToCellWeightsTensor.get(),
195 m_InputToOutputWeightsTensor.get(), m_RecurrentToForgetWeightsTensor.get(),
196 m_RecurrentToCellWeightsTensor.get(), m_RecurrentToOutputWeightsTensor.get(),
197 m_ForgetGateBiasTensor.get(), m_CellBiasTensor.get(), m_OutputGateBiasTensor.get(),
198 &output_state_in, &cell_state_in, m_ScratchBuffer.get(), &output_state_out,
199 &cell_state_out, &output, lstm_param, activationLayerInfo,
200 cell_threshold, projection_threshold);
202 armcomputetensorutils::InitialiseArmComputeTensorEmpty(*m_ScratchBuffer);
205 m_Data.m_InputToForgetWeights);
207 m_Data.m_InputToCellWeights);
209 m_Data.m_InputToOutputWeights);
211 m_Data.m_RecurrentToForgetWeights);
213 m_Data.m_RecurrentToCellWeights);
215 m_Data.m_RecurrentToOutputWeights);
223 if (!
m_Data.m_Parameters.m_CifgEnabled)
226 m_Data.m_InputToInputWeights);
228 m_Data.m_RecurrentToInputWeights);
229 if (
m_Data.m_CellToInputWeights !=
nullptr)
232 m_Data.m_CellToInputWeights);
238 if (
m_Data.m_Parameters.m_ProjectionEnabled)
241 m_Data.m_ProjectionWeights);
242 if (
m_Data.m_ProjectionBias !=
nullptr)
249 if (
m_Data.m_Parameters.m_PeepholeEnabled)
252 m_Data.m_CellToForgetWeights);
254 m_Data.m_CellToOutputWeights);
257 if (
m_Data.m_Parameters.m_LayerNormEnabled)
259 if (!
m_Data.m_Parameters.m_CifgEnabled)
270 m_LstmLayer.prepare();
290 arm_compute::LSTMParams<arm_compute::ITensorInfo> lstm_params_info;
293 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
294 const arm_compute::TensorInfo aclOutputStateInInfo = BuildArmComputeTensorInfo(outputStateIn);
295 const arm_compute::TensorInfo aclCellStateInInfo = BuildArmComputeTensorInfo(cellStateIn);
296 const arm_compute::TensorInfo aclScratchBufferInfo = BuildArmComputeTensorInfo(scratchBuffer);
297 const arm_compute::TensorInfo aclOutputStateOutInfo = BuildArmComputeTensorInfo(outputStateOut);
298 const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut);
299 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
302 const arm_compute::TensorInfo aclInputToForgetWeightsInfo
304 const arm_compute::TensorInfo aclInputToCellWeightsInfo
306 const arm_compute::TensorInfo aclInputToOutputWeightsInfo
308 const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo
310 const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo
312 const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo
314 const arm_compute::TensorInfo aclForgetGateBiasInfo
316 const arm_compute::TensorInfo aclCellBiasInfo
317 = BuildArmComputeTensorInfo(paramsInfo.
GetCellBias());
318 const arm_compute::TensorInfo aclOutputGateBiasInfo
321 arm_compute::TensorInfo aclInputToInputWeightsInfo;
322 arm_compute::TensorInfo aclRecurrentToInputWeightsInfo;
323 arm_compute::TensorInfo aclCellToInputWeightsInfo;
324 arm_compute::TensorInfo aclInputGateBiasInfo;
325 arm_compute::TensorInfo aclProjectionWeightsInfo;
326 arm_compute::TensorInfo aclProjectionBiasInfo;
327 arm_compute::TensorInfo aclCellToForgetWeightsInfo;
328 arm_compute::TensorInfo aclCellToOutputWeightsInfo;
330 arm_compute::TensorInfo aclInputLayerNormWeightsInfo;
331 arm_compute::TensorInfo aclForgetLayerNormWeightsInfo;
332 arm_compute::TensorInfo aclCellLayerNormWeightsInfo;
333 arm_compute::TensorInfo aclOutputLayerNormWeightsInfo;
344 aclInputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetInputGateBias());
346 lstm_params_info.set_cifg_params(&aclInputToInputWeightsInfo, &aclRecurrentToInputWeightsInfo,
348 &aclInputGateBiasInfo);
355 aclProjectionBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetProjectionBias());
359 lstm_params_info.set_projection_params(&aclProjectionWeightsInfo,
361 &aclProjectionBiasInfo :
nullptr);
369 lstm_params_info.set_peephole_params(&aclCellToForgetWeightsInfo, &aclCellToOutputWeightsInfo);
382 lstm_params_info.set_layer_normalization_params(descriptor.
m_CifgEnabled ?
383 nullptr : &aclInputLayerNormWeightsInfo,
384 &aclForgetLayerNormWeightsInfo,
385 &aclCellLayerNormWeightsInfo,
386 &aclOutputLayerNormWeightsInfo);
393 arm_compute::ActivationLayerInfo activationLayerInfo;
400 activationLayerInfo = arm_compute::ActivationLayerInfo(
401 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
404 activationLayerInfo = arm_compute::ActivationLayerInfo(
405 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
408 activationLayerInfo = arm_compute::ActivationLayerInfo(
409 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
412 activationLayerInfo = arm_compute::ActivationLayerInfo(
413 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
419 return arm_compute::NELSTMLayer::validate(&aclInputInfo,
420 &aclInputToForgetWeightsInfo,
421 &aclInputToCellWeightsInfo,
422 &aclInputToOutputWeightsInfo,
423 &aclRecurrentToForgetWeightsInfo,
424 &aclRecurrentToCellWeightsInfo,
425 &aclRecurrentToOutputWeightsInfo,
426 &aclForgetGateBiasInfo,
428 &aclOutputGateBiasInfo,
429 &aclOutputStateInInfo,
431 &aclScratchBufferInfo,
432 &aclOutputStateOutInfo,
433 &aclCellStateOutInfo,
438 projection_threshold);
441 void NeonLstmFloatWorkload::FreeUnusedTensors()
443 FreeTensorIfUnused(m_InputToInputWeightsTensor);
444 FreeTensorIfUnused(m_InputToForgetWeightsTensor);
445 FreeTensorIfUnused(m_InputToCellWeightsTensor);
446 FreeTensorIfUnused(m_InputToOutputWeightsTensor);
447 FreeTensorIfUnused(m_RecurrentToInputWeightsTensor);
448 FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor);
449 FreeTensorIfUnused(m_RecurrentToCellWeightsTensor);
450 FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor);
451 FreeTensorIfUnused(m_CellToInputWeightsTensor);
452 FreeTensorIfUnused(m_CellToForgetWeightsTensor);
453 FreeTensorIfUnused(m_CellToOutputWeightsTensor);
454 FreeTensorIfUnused(m_InputGateBiasTensor);
455 FreeTensorIfUnused(m_ForgetGateBiasTensor);
456 FreeTensorIfUnused(m_CellBiasTensor);
457 FreeTensorIfUnused(m_OutputGateBiasTensor);
458 FreeTensorIfUnused(m_ProjectionWeightsTensor);
459 FreeTensorIfUnused(m_ProjectionBiasTensor);
460 FreeTensorIfUnused(m_ScratchBuffer);
461 FreeTensorIfUnused(m_InputLayerNormWeightsTensor);
462 FreeTensorIfUnused(m_ForgetLayerNormWeightsTensor);
463 FreeTensorIfUnused(m_CellLayerNormWeightsTensor);
464 FreeTensorIfUnused(m_OutputLayerNormWeightsTensor);
bool m_ProjectionEnabled
Enable/disable the projection layer.
const TensorShape & GetShape() const
float m_ClippingThresProj
Clipping threshold value for the projection.
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.
LayerDescriptor m_Parameters
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.
profiling::ProfilingGuid GetGuid() const final
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.
#define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos, guid)
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)
void InitializeArmComputeTensorData(arm_compute::Tensor &tensor, const ConstTensorHandle *handle)
Contains information about TensorInfos of a layer.
std::vector< ITensorHandle * > m_Inputs
#define ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID(name, guid)