14 #include <arm_compute/runtime/CL/functions/CLLSTMLayer.h> 20 using namespace armcomputetensorutils;
24 const arm_compute::CLCompileContext& clCompileContext)
33 arm_compute::LSTMParams<arm_compute::ICLTensor> lstm_param;
36 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
37 BuildArmComputeTensor(*m_InputToForgetWeightsTensor,
m_Data.m_InputToForgetWeights->GetTensorInfo());
39 m_InputToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
40 BuildArmComputeTensor(*m_InputToCellWeightsTensor,
m_Data.m_InputToCellWeights->GetTensorInfo());
42 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
43 BuildArmComputeTensor(*m_InputToOutputWeightsTensor,
m_Data.m_InputToOutputWeights->GetTensorInfo());
45 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
46 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor,
m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
48 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
49 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor,
m_Data.m_RecurrentToCellWeights->GetTensorInfo());
51 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
52 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor,
m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
54 m_ForgetGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
55 BuildArmComputeTensor(*m_ForgetGateBiasTensor,
m_Data.m_ForgetGateBias->GetTensorInfo());
57 m_CellBiasTensor = std::make_unique<arm_compute::CLTensor>();
58 BuildArmComputeTensor(*m_CellBiasTensor,
m_Data.m_CellBias->GetTensorInfo());
60 m_OutputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
61 BuildArmComputeTensor(*m_OutputGateBiasTensor,
m_Data.m_OutputGateBias->GetTensorInfo());
64 if (!
m_Data.m_Parameters.m_CifgEnabled)
66 m_InputToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
67 BuildArmComputeTensor(*m_InputToInputWeightsTensor,
m_Data.m_InputToInputWeights->GetTensorInfo());
69 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
70 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor,
m_Data.m_RecurrentToInputWeights->GetTensorInfo());
72 m_CellToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
73 if (
m_Data.m_CellToInputWeights !=
nullptr)
75 BuildArmComputeTensor(*m_CellToInputWeightsTensor,
m_Data.m_CellToInputWeights->GetTensorInfo());
78 m_InputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
79 BuildArmComputeTensor(*m_InputGateBiasTensor,
m_Data.m_InputGateBias->GetTensorInfo());
81 lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
82 m_RecurrentToInputWeightsTensor.get(),
83 m_Data.m_CellToInputWeights !=
nullptr ? m_CellToInputWeightsTensor.get() :
nullptr,
84 m_InputGateBiasTensor.get());
87 if (
m_Data.m_Parameters.m_ProjectionEnabled)
89 m_ProjectionWeightsTensor = std::make_unique<arm_compute::CLTensor>();
90 BuildArmComputeTensor(*m_ProjectionWeightsTensor,
m_Data.m_ProjectionWeights->GetTensorInfo());
92 m_ProjectionBiasTensor = std::make_unique<arm_compute::CLTensor>();
93 if (
m_Data.m_ProjectionBias !=
nullptr)
95 BuildArmComputeTensor(*m_ProjectionBiasTensor,
m_Data.m_ProjectionBias->GetTensorInfo());
98 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
99 m_Data.m_ProjectionBias !=
nullptr ? m_ProjectionBiasTensor.get() :
nullptr);
102 if (
m_Data.m_Parameters.m_PeepholeEnabled)
104 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
105 BuildArmComputeTensor(*m_CellToForgetWeightsTensor,
m_Data.m_CellToForgetWeights->GetTensorInfo());
107 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
108 BuildArmComputeTensor(*m_CellToOutputWeightsTensor,
m_Data.m_CellToOutputWeights->GetTensorInfo());
110 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
113 if (
m_Data.m_Parameters.m_LayerNormEnabled)
115 m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
116 m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
117 m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
118 m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
120 if (!
m_Data.m_Parameters.m_CifgEnabled)
122 BuildArmComputeTensor(*m_InputLayerNormWeightsTensor,
m_Data.m_InputLayerNormWeights->GetTensorInfo());
124 BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor,
m_Data.m_ForgetLayerNormWeights->GetTensorInfo());
125 BuildArmComputeTensor(*m_CellLayerNormWeightsTensor,
m_Data.m_CellLayerNormWeights->GetTensorInfo());
126 BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor,
m_Data.m_OutputLayerNormWeights->GetTensorInfo());
128 lstm_param.set_layer_normalization_params(
m_Data.m_Parameters.m_CifgEnabled ?
nullptr :
129 m_InputLayerNormWeightsTensor.get(),
130 m_ForgetLayerNormWeightsTensor.get(),
131 m_CellLayerNormWeightsTensor.get(),
132 m_OutputLayerNormWeightsTensor.get());
148 m_ScratchBuffer = std::make_unique<arm_compute::CLTensor>();
149 if (
m_Data.m_Parameters.m_CifgEnabled)
153 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
159 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2);
162 float cell_threshold =
m_Data.m_Parameters.m_ClippingThresCell;
163 float projection_threshold =
m_Data.m_Parameters.m_ClippingThresProj;
166 arm_compute::ActivationLayerInfo activationLayerInfo;
167 if (
m_Data.m_Parameters.m_ActivationFunc == 0)
171 else if (
m_Data.m_Parameters.m_ActivationFunc == 1)
173 activationLayerInfo = arm_compute::ActivationLayerInfo(
174 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
176 else if (
m_Data.m_Parameters.m_ActivationFunc == 3)
178 activationLayerInfo = arm_compute::ActivationLayerInfo(
179 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
181 else if (
m_Data.m_Parameters.m_ActivationFunc == 4)
183 activationLayerInfo = arm_compute::ActivationLayerInfo(
184 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
186 else if (
m_Data.m_Parameters.m_ActivationFunc == 6)
188 activationLayerInfo = arm_compute::ActivationLayerInfo(
189 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
198 m_LstmLayer.configure(clCompileContext, &input, m_InputToForgetWeightsTensor.get(),
199 m_InputToCellWeightsTensor.get(), m_InputToOutputWeightsTensor.get(),
200 m_RecurrentToForgetWeightsTensor.get(), m_RecurrentToCellWeightsTensor.get(),
201 m_RecurrentToOutputWeightsTensor.get(), m_ForgetGateBiasTensor.get(),
202 m_CellBiasTensor.get(), m_OutputGateBiasTensor.get(), &output_state_in,
203 &cell_state_in, m_ScratchBuffer.get(), &output_state_out,
204 &cell_state_out, &output, lstm_param, activationLayerInfo,
205 cell_threshold, projection_threshold);
208 armcomputetensorutils::InitialiseArmComputeTensorEmpty(*m_ScratchBuffer);
220 if (!
m_Data.m_Parameters.m_CifgEnabled)
224 if (
m_Data.m_CellToInputWeights !=
nullptr)
231 if (
m_Data.m_Parameters.m_ProjectionEnabled)
234 if (
m_Data.m_ProjectionBias !=
nullptr)
240 if (
m_Data.m_Parameters.m_PeepholeEnabled)
246 if (
m_Data.m_Parameters.m_LayerNormEnabled)
248 if (!
m_Data.m_Parameters.m_CifgEnabled)
260 m_LstmLayer.prepare();
276 arm_compute::LSTMParams<arm_compute::ITensorInfo> lstm_params_info;
279 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
280 const arm_compute::TensorInfo aclOutputStateInInfo = BuildArmComputeTensorInfo(outputStateIn);
281 const arm_compute::TensorInfo aclCellStateInInfo = BuildArmComputeTensorInfo(cellStateIn);
282 const arm_compute::TensorInfo aclScratchBufferInfo = BuildArmComputeTensorInfo(scratchBuffer);
283 const arm_compute::TensorInfo aclOutputStateOutInfo = BuildArmComputeTensorInfo(outputStateOut);
284 const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut);
285 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
288 const arm_compute::TensorInfo aclInputToForgetWeightsInfo
290 const arm_compute::TensorInfo aclInputToCellWeightsInfo
292 const arm_compute::TensorInfo aclInputToOutputWeightsInfo
294 const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo
296 const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo
298 const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo
300 const arm_compute::TensorInfo aclForgetGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetForgetGateBias());
301 const arm_compute::TensorInfo aclCellBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetCellBias());
302 const arm_compute::TensorInfo aclOutputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetOutputGateBias());
304 arm_compute::TensorInfo aclInputToInputWeightsInfo;
305 arm_compute::TensorInfo aclRecurrentToInputWeightsInfo;
306 arm_compute::TensorInfo aclCellToInputWeightsInfo;
307 arm_compute::TensorInfo aclInputGateBiasInfo;
308 arm_compute::TensorInfo aclProjectionWeightsInfo;
309 arm_compute::TensorInfo aclProjectionBiasInfo;
310 arm_compute::TensorInfo aclCellToForgetWeightsInfo;
311 arm_compute::TensorInfo aclCellToOutputWeightsInfo;
312 arm_compute::TensorInfo aclInputLayerNormWeightsInfo;
313 arm_compute::TensorInfo aclForgetLayerNormWeightsInfo;
314 arm_compute::TensorInfo aclCellLayerNormWeightsInfo;
315 arm_compute::TensorInfo aclOutputLayerNormWeightsInfo;
326 aclInputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetInputGateBias());
327 lstm_params_info.set_cifg_params(&aclInputToInputWeightsInfo, &aclRecurrentToInputWeightsInfo,
329 &aclCellToInputWeightsInfo:
nullptr,
330 &aclInputGateBiasInfo);
339 aclProjectionBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetInputGateBias());
341 lstm_params_info.set_projection_params(&aclProjectionWeightsInfo,
343 &aclProjectionBiasInfo:
nullptr);
350 lstm_params_info.set_peephole_params(&aclCellToForgetWeightsInfo, &aclCellToOutputWeightsInfo);
357 arm_compute::ActivationLayerInfo activationLayerInfo;
364 activationLayerInfo = arm_compute::ActivationLayerInfo(
365 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
369 activationLayerInfo = arm_compute::ActivationLayerInfo(
370 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
374 activationLayerInfo = arm_compute::ActivationLayerInfo(
375 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
379 activationLayerInfo = arm_compute::ActivationLayerInfo(
380 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
400 lstm_params_info.set_layer_normalization_params(descriptor.
m_CifgEnabled ?
401 nullptr : &aclInputLayerNormWeightsInfo,
402 &aclForgetLayerNormWeightsInfo,
403 &aclCellLayerNormWeightsInfo,
404 &aclOutputLayerNormWeightsInfo);
407 return arm_compute::CLLSTMLayer::validate(&aclInputInfo, &aclInputToForgetWeightsInfo,
408 &aclInputToCellWeightsInfo,
409 &aclInputToOutputWeightsInfo,
410 &aclRecurrentToForgetWeightsInfo,
411 &aclRecurrentToCellWeightsInfo,
412 &aclRecurrentToOutputWeightsInfo,
413 &aclForgetGateBiasInfo,
415 &aclOutputGateBiasInfo,
416 &aclOutputStateInInfo, &aclCellStateInInfo,
417 &aclScratchBufferInfo, &aclOutputStateOutInfo,
418 &aclCellStateOutInfo, &aclOutputInfo,
419 lstm_params_info, activationLayerInfo,
420 cell_threshold, projection_threshold);
423 void ClLstmFloatWorkload::FreeUnusedTensors()
425 FreeTensorIfUnused(m_InputToInputWeightsTensor);
426 FreeTensorIfUnused(m_InputToForgetWeightsTensor);
427 FreeTensorIfUnused(m_InputToCellWeightsTensor);
428 FreeTensorIfUnused(m_InputToOutputWeightsTensor);
429 FreeTensorIfUnused(m_RecurrentToInputWeightsTensor);
430 FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor);
431 FreeTensorIfUnused(m_RecurrentToCellWeightsTensor);
432 FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor);
433 FreeTensorIfUnused(m_CellToInputWeightsTensor);
434 FreeTensorIfUnused(m_CellToForgetWeightsTensor);
435 FreeTensorIfUnused(m_CellToOutputWeightsTensor);
436 FreeTensorIfUnused(m_InputGateBiasTensor);
437 FreeTensorIfUnused(m_ForgetGateBiasTensor);
438 FreeTensorIfUnused(m_CellBiasTensor);
439 FreeTensorIfUnused(m_OutputGateBiasTensor);
440 FreeTensorIfUnused(m_ProjectionWeightsTensor);
441 FreeTensorIfUnused(m_ProjectionBiasTensor);
442 FreeTensorIfUnused(m_ScratchBuffer);
443 FreeTensorIfUnused(m_InputLayerNormWeightsTensor);
444 FreeTensorIfUnused(m_ForgetLayerNormWeightsTensor);
445 FreeTensorIfUnused(m_CellLayerNormWeightsTensor);
446 FreeTensorIfUnused(m_OutputLayerNormWeightsTensor);
482 void ClLstmFloatWorkload::Reconfigure()
bool m_ProjectionEnabled
Enable/disable the projection layer.
#define ARMNN_SCOPED_PROFILING_EVENT_CL_GUID(name, guid)
void ReplaceOutputTensorHandle(ITensorHandle *tensorHandle, unsigned int slot) override
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.
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.
LayerDescriptor m_Parameters
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
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).
void ReplaceInputTensorHandle(ITensorHandle *tensorHandle, unsigned int slot) override
std::vector< ITensorHandle * > m_Outputs
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)
Contains information about TensorInfos of a layer.
void InitializeArmComputeClTensorData(arm_compute::CLTensor &clTensor, const ConstTensorHandle *handle)
std::vector< ITensorHandle * > m_Inputs
void Execute() const override