14 #include <arm_compute/runtime/CL/functions/CLLSTMLayer.h> 20 using namespace armcomputetensorutils;
24 const arm_compute::CLCompileContext& clCompileContext)
27 arm_compute::LSTMParams<arm_compute::ICLTensor> lstm_param;
30 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
31 BuildArmComputeTensor(*m_InputToForgetWeightsTensor,
m_Data.m_InputToForgetWeights->GetTensorInfo());
33 m_InputToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
34 BuildArmComputeTensor(*m_InputToCellWeightsTensor,
m_Data.m_InputToCellWeights->GetTensorInfo());
36 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
37 BuildArmComputeTensor(*m_InputToOutputWeightsTensor,
m_Data.m_InputToOutputWeights->GetTensorInfo());
39 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
40 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor,
m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
42 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
43 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor,
m_Data.m_RecurrentToCellWeights->GetTensorInfo());
45 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
46 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor,
m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
48 m_ForgetGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
49 BuildArmComputeTensor(*m_ForgetGateBiasTensor,
m_Data.m_ForgetGateBias->GetTensorInfo());
51 m_CellBiasTensor = std::make_unique<arm_compute::CLTensor>();
52 BuildArmComputeTensor(*m_CellBiasTensor,
m_Data.m_CellBias->GetTensorInfo());
54 m_OutputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
55 BuildArmComputeTensor(*m_OutputGateBiasTensor,
m_Data.m_OutputGateBias->GetTensorInfo());
58 if (!
m_Data.m_Parameters.m_CifgEnabled)
60 m_InputToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
61 BuildArmComputeTensor(*m_InputToInputWeightsTensor,
m_Data.m_InputToInputWeights->GetTensorInfo());
63 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
64 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor,
m_Data.m_RecurrentToInputWeights->GetTensorInfo());
66 m_CellToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
67 if (
m_Data.m_CellToInputWeights !=
nullptr)
69 BuildArmComputeTensor(*m_CellToInputWeightsTensor,
m_Data.m_CellToInputWeights->GetTensorInfo());
72 m_InputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
73 BuildArmComputeTensor(*m_InputGateBiasTensor,
m_Data.m_InputGateBias->GetTensorInfo());
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());
81 if (
m_Data.m_Parameters.m_ProjectionEnabled)
83 m_ProjectionWeightsTensor = std::make_unique<arm_compute::CLTensor>();
84 BuildArmComputeTensor(*m_ProjectionWeightsTensor,
m_Data.m_ProjectionWeights->GetTensorInfo());
86 m_ProjectionBiasTensor = std::make_unique<arm_compute::CLTensor>();
87 if (
m_Data.m_ProjectionBias !=
nullptr)
89 BuildArmComputeTensor(*m_ProjectionBiasTensor,
m_Data.m_ProjectionBias->GetTensorInfo());
92 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
93 m_Data.m_ProjectionBias !=
nullptr ? m_ProjectionBiasTensor.get() :
nullptr);
96 if (
m_Data.m_Parameters.m_PeepholeEnabled)
98 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
99 BuildArmComputeTensor(*m_CellToForgetWeightsTensor,
m_Data.m_CellToForgetWeights->GetTensorInfo());
101 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
102 BuildArmComputeTensor(*m_CellToOutputWeightsTensor,
m_Data.m_CellToOutputWeights->GetTensorInfo());
104 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
107 if (
m_Data.m_Parameters.m_LayerNormEnabled)
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>();
114 if (!
m_Data.m_Parameters.m_CifgEnabled)
116 BuildArmComputeTensor(*m_InputLayerNormWeightsTensor,
m_Data.m_InputLayerNormWeights->GetTensorInfo());
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());
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());
142 m_ScratchBuffer = std::make_unique<arm_compute::CLTensor>();
143 if (
m_Data.m_Parameters.m_CifgEnabled)
147 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
153 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2);
156 float cell_threshold =
m_Data.m_Parameters.m_ClippingThresCell;
157 float projection_threshold =
m_Data.m_Parameters.m_ClippingThresProj;
160 arm_compute::ActivationLayerInfo activationLayerInfo;
161 if (
m_Data.m_Parameters.m_ActivationFunc == 0)
165 else if (
m_Data.m_Parameters.m_ActivationFunc == 1)
167 activationLayerInfo = arm_compute::ActivationLayerInfo(
168 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
170 else if (
m_Data.m_Parameters.m_ActivationFunc == 3)
172 activationLayerInfo = arm_compute::ActivationLayerInfo(
173 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
175 else if (
m_Data.m_Parameters.m_ActivationFunc == 4)
177 activationLayerInfo = arm_compute::ActivationLayerInfo(
178 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
180 else if (
m_Data.m_Parameters.m_ActivationFunc == 6)
182 activationLayerInfo = arm_compute::ActivationLayerInfo(
183 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
190 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,
196 &cell_state_out, &output, lstm_param, activationLayerInfo,
197 cell_threshold, projection_threshold);
199 armcomputetensorutils::InitialiseArmComputeTensorEmpty(*m_ScratchBuffer);
211 if (!
m_Data.m_Parameters.m_CifgEnabled)
215 if (
m_Data.m_CellToInputWeights !=
nullptr)
222 if (
m_Data.m_Parameters.m_ProjectionEnabled)
225 if (
m_Data.m_ProjectionBias !=
nullptr)
231 if (
m_Data.m_Parameters.m_PeepholeEnabled)
237 if (
m_Data.m_Parameters.m_LayerNormEnabled)
239 if (!
m_Data.m_Parameters.m_CifgEnabled)
251 m_LstmLayer.prepare();
267 arm_compute::LSTMParams<arm_compute::ITensorInfo> lstm_params_info;
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);
279 const arm_compute::TensorInfo aclInputToForgetWeightsInfo
281 const arm_compute::TensorInfo aclInputToCellWeightsInfo
283 const arm_compute::TensorInfo aclInputToOutputWeightsInfo
285 const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo
287 const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo
289 const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo
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());
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;
303 arm_compute::TensorInfo aclInputLayerNormWeightsInfo;
304 arm_compute::TensorInfo aclForgetLayerNormWeightsInfo;
305 arm_compute::TensorInfo aclCellLayerNormWeightsInfo;
306 arm_compute::TensorInfo aclOutputLayerNormWeightsInfo;
317 aclInputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetInputGateBias());
318 lstm_params_info.set_cifg_params(&aclInputToInputWeightsInfo, &aclRecurrentToInputWeightsInfo,
320 &aclCellToInputWeightsInfo:
nullptr,
321 &aclInputGateBiasInfo);
330 aclProjectionBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetInputGateBias());
332 lstm_params_info.set_projection_params(&aclProjectionWeightsInfo,
334 &aclProjectionBiasInfo:
nullptr);
341 lstm_params_info.set_peephole_params(&aclCellToForgetWeightsInfo, &aclCellToOutputWeightsInfo);
348 arm_compute::ActivationLayerInfo activationLayerInfo;
355 activationLayerInfo = arm_compute::ActivationLayerInfo(
356 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
360 activationLayerInfo = arm_compute::ActivationLayerInfo(
361 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
365 activationLayerInfo = arm_compute::ActivationLayerInfo(
366 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
370 activationLayerInfo = arm_compute::ActivationLayerInfo(
371 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
391 lstm_params_info.set_layer_normalization_params(descriptor.
m_CifgEnabled ?
392 nullptr : &aclInputLayerNormWeightsInfo,
393 &aclForgetLayerNormWeightsInfo,
394 &aclCellLayerNormWeightsInfo,
395 &aclOutputLayerNormWeightsInfo);
398 return arm_compute::CLLSTMLayer::validate(&aclInputInfo, &aclInputToForgetWeightsInfo,
399 &aclInputToCellWeightsInfo,
400 &aclInputToOutputWeightsInfo,
401 &aclRecurrentToForgetWeightsInfo,
402 &aclRecurrentToCellWeightsInfo,
403 &aclRecurrentToOutputWeightsInfo,
404 &aclForgetGateBiasInfo,
406 &aclOutputGateBiasInfo,
407 &aclOutputStateInInfo, &aclCellStateInInfo,
408 &aclScratchBufferInfo, &aclOutputStateOutInfo,
409 &aclCellStateOutInfo, &aclOutputInfo,
410 lstm_params_info, activationLayerInfo,
411 cell_threshold, projection_threshold);
414 void ClLstmFloatWorkload::FreeUnusedTensors()
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);
434 FreeTensorIfUnused(m_InputLayerNormWeightsTensor);
435 FreeTensorIfUnused(m_ForgetLayerNormWeightsTensor);
436 FreeTensorIfUnused(m_CellLayerNormWeightsTensor);
437 FreeTensorIfUnused(m_OutputLayerNormWeightsTensor);
bool m_ProjectionEnabled
Enable/disable the projection layer.
ClLstmFloatWorkload(const LstmQueueDescriptor &descriptor, const WorkloadInfo &info, const arm_compute::CLCompileContext &clCompileContext)
const TensorShape & GetShape() const
float m_ClippingThresProj
Clipping threshold value for the projection.
#define ARMNN_SCOPED_PROFILING_EVENT_CL(name)
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
arm_compute::Status ClLstmFloatWorkloadValidate(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.
bool m_CifgEnabled
Enable/disable cifg (coupled input & forget gate).
std::vector< ITensorHandle * > m_Outputs
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.
void InitializeArmComputeClTensorData(arm_compute::CLTensor &clTensor, const ConstTensorHandle *handle)
std::vector< ITensorHandle * > m_Inputs
void Execute() const override