14 #include <arm_compute/runtime/CL/functions/CLLSTMLayer.h> 20 using namespace armcomputetensorutils;
25 arm_compute::LSTMParams<arm_compute::ICLTensor> lstm_param;
28 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
29 BuildArmComputeTensor(*m_InputToForgetWeightsTensor,
m_Data.m_InputToForgetWeights->GetTensorInfo());
31 m_InputToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
32 BuildArmComputeTensor(*m_InputToCellWeightsTensor,
m_Data.m_InputToCellWeights->GetTensorInfo());
34 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
35 BuildArmComputeTensor(*m_InputToOutputWeightsTensor,
m_Data.m_InputToOutputWeights->GetTensorInfo());
37 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
38 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor,
m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
40 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
41 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor,
m_Data.m_RecurrentToCellWeights->GetTensorInfo());
43 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
44 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor,
m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
46 m_ForgetGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
47 BuildArmComputeTensor(*m_ForgetGateBiasTensor,
m_Data.m_ForgetGateBias->GetTensorInfo());
49 m_CellBiasTensor = std::make_unique<arm_compute::CLTensor>();
50 BuildArmComputeTensor(*m_CellBiasTensor,
m_Data.m_CellBias->GetTensorInfo());
52 m_OutputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
53 BuildArmComputeTensor(*m_OutputGateBiasTensor,
m_Data.m_OutputGateBias->GetTensorInfo());
56 if (!
m_Data.m_Parameters.m_CifgEnabled)
58 m_InputToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
59 BuildArmComputeTensor(*m_InputToInputWeightsTensor,
m_Data.m_InputToInputWeights->GetTensorInfo());
61 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
62 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor,
m_Data.m_RecurrentToInputWeights->GetTensorInfo());
64 m_CellToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
65 if (
m_Data.m_CellToInputWeights !=
nullptr)
67 BuildArmComputeTensor(*m_CellToInputWeightsTensor,
m_Data.m_CellToInputWeights->GetTensorInfo());
70 m_InputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
71 BuildArmComputeTensor(*m_InputGateBiasTensor,
m_Data.m_InputGateBias->GetTensorInfo());
73 lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
74 m_RecurrentToInputWeightsTensor.get(),
75 m_Data.m_CellToInputWeights !=
nullptr ? m_CellToInputWeightsTensor.get() :
nullptr,
76 m_InputGateBiasTensor.get());
79 if (
m_Data.m_Parameters.m_ProjectionEnabled)
81 m_ProjectionWeightsTensor = std::make_unique<arm_compute::CLTensor>();
82 BuildArmComputeTensor(*m_ProjectionWeightsTensor,
m_Data.m_ProjectionWeights->GetTensorInfo());
84 m_ProjectionBiasTensor = std::make_unique<arm_compute::CLTensor>();
85 if (
m_Data.m_ProjectionBias !=
nullptr)
87 BuildArmComputeTensor(*m_ProjectionBiasTensor,
m_Data.m_ProjectionBias->GetTensorInfo());
90 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
91 m_Data.m_ProjectionBias !=
nullptr ? m_ProjectionBiasTensor.get() :
nullptr);
94 if (
m_Data.m_Parameters.m_PeepholeEnabled)
96 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
97 BuildArmComputeTensor(*m_CellToForgetWeightsTensor,
m_Data.m_CellToForgetWeights->GetTensorInfo());
99 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
100 BuildArmComputeTensor(*m_CellToOutputWeightsTensor,
m_Data.m_CellToOutputWeights->GetTensorInfo());
102 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
105 if (
m_Data.m_Parameters.m_LayerNormEnabled)
107 m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
108 m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
109 m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
110 m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
112 if (!
m_Data.m_Parameters.m_CifgEnabled)
114 BuildArmComputeTensor(*m_InputLayerNormWeightsTensor,
m_Data.m_InputLayerNormWeights->GetTensorInfo());
116 BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor,
m_Data.m_ForgetLayerNormWeights->GetTensorInfo());
117 BuildArmComputeTensor(*m_CellLayerNormWeightsTensor,
m_Data.m_CellLayerNormWeights->GetTensorInfo());
118 BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor,
m_Data.m_OutputLayerNormWeights->GetTensorInfo());
120 lstm_param.set_layer_normalization_params(
m_Data.m_Parameters.m_CifgEnabled ?
nullptr :
121 m_InputLayerNormWeightsTensor.get(),
122 m_ForgetLayerNormWeightsTensor.get(),
123 m_CellLayerNormWeightsTensor.get(),
124 m_OutputLayerNormWeightsTensor.get());
140 m_ScratchBuffer = std::make_unique<arm_compute::CLTensor>();
141 if (
m_Data.m_Parameters.m_CifgEnabled)
145 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
151 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2);
154 float cell_threshold =
m_Data.m_Parameters.m_ClippingThresCell;
155 float projection_threshold =
m_Data.m_Parameters.m_ClippingThresProj;
158 arm_compute::ActivationLayerInfo activationLayerInfo;
159 if (
m_Data.m_Parameters.m_ActivationFunc == 0)
163 else if (
m_Data.m_Parameters.m_ActivationFunc == 1)
165 activationLayerInfo = arm_compute::ActivationLayerInfo(
166 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
168 else if (
m_Data.m_Parameters.m_ActivationFunc == 3)
170 activationLayerInfo = arm_compute::ActivationLayerInfo(
171 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
173 else if (
m_Data.m_Parameters.m_ActivationFunc == 4)
175 activationLayerInfo = arm_compute::ActivationLayerInfo(
176 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
178 else if (
m_Data.m_Parameters.m_ActivationFunc == 6)
180 activationLayerInfo = arm_compute::ActivationLayerInfo(
181 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);
208 if (!
m_Data.m_Parameters.m_CifgEnabled)
212 if (
m_Data.m_CellToInputWeights !=
nullptr)
219 if (
m_Data.m_Parameters.m_ProjectionEnabled)
222 if (
m_Data.m_ProjectionBias !=
nullptr)
228 if (
m_Data.m_Parameters.m_PeepholeEnabled)
234 if (
m_Data.m_Parameters.m_LayerNormEnabled)
236 if (!
m_Data.m_Parameters.m_CifgEnabled)
248 m_LstmLayer.prepare();
264 arm_compute::LSTMParams<arm_compute::ITensorInfo> lstm_params_info;
267 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
268 const arm_compute::TensorInfo aclOutputStateInInfo = BuildArmComputeTensorInfo(outputStateIn);
269 const arm_compute::TensorInfo aclCellStateInInfo = BuildArmComputeTensorInfo(cellStateIn);
270 const arm_compute::TensorInfo aclScratchBufferInfo = BuildArmComputeTensorInfo(scratchBuffer);
271 const arm_compute::TensorInfo aclOutputStateOutInfo = BuildArmComputeTensorInfo(outputStateOut);
272 const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut);
273 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
276 const arm_compute::TensorInfo aclInputToForgetWeightsInfo
278 const arm_compute::TensorInfo aclInputToCellWeightsInfo
280 const arm_compute::TensorInfo aclInputToOutputWeightsInfo
282 const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo
284 const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo
286 const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo
288 const arm_compute::TensorInfo aclForgetGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetForgetGateBias());
289 const arm_compute::TensorInfo aclCellBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetCellBias());
290 const arm_compute::TensorInfo aclOutputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetOutputGateBias());
292 arm_compute::TensorInfo aclInputToInputWeightsInfo;
293 arm_compute::TensorInfo aclRecurrentToInputWeightsInfo;
294 arm_compute::TensorInfo aclCellToInputWeightsInfo;
295 arm_compute::TensorInfo aclInputGateBiasInfo;
296 arm_compute::TensorInfo aclProjectionWeightsInfo;
297 arm_compute::TensorInfo aclProjectionBiasInfo;
298 arm_compute::TensorInfo aclCellToForgetWeightsInfo;
299 arm_compute::TensorInfo aclCellToOutputWeightsInfo;
300 arm_compute::TensorInfo aclInputLayerNormWeightsInfo;
301 arm_compute::TensorInfo aclForgetLayerNormWeightsInfo;
302 arm_compute::TensorInfo aclCellLayerNormWeightsInfo;
303 arm_compute::TensorInfo aclOutputLayerNormWeightsInfo;
314 aclInputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetInputGateBias());
315 lstm_params_info.set_cifg_params(&aclInputToInputWeightsInfo, &aclRecurrentToInputWeightsInfo,
317 &aclCellToInputWeightsInfo:
nullptr,
318 &aclInputGateBiasInfo);
327 aclProjectionBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetInputGateBias());
329 lstm_params_info.set_projection_params(&aclProjectionWeightsInfo,
331 &aclProjectionBiasInfo:
nullptr);
338 lstm_params_info.set_peephole_params(&aclCellToForgetWeightsInfo, &aclCellToOutputWeightsInfo);
345 arm_compute::ActivationLayerInfo activationLayerInfo;
352 activationLayerInfo = arm_compute::ActivationLayerInfo(
353 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
357 activationLayerInfo = arm_compute::ActivationLayerInfo(
358 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
362 activationLayerInfo = arm_compute::ActivationLayerInfo(
363 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
367 activationLayerInfo = arm_compute::ActivationLayerInfo(
368 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
388 lstm_params_info.set_layer_normalization_params(descriptor.
m_CifgEnabled ?
389 nullptr : &aclInputLayerNormWeightsInfo,
390 &aclForgetLayerNormWeightsInfo,
391 &aclCellLayerNormWeightsInfo,
392 &aclOutputLayerNormWeightsInfo);
395 return arm_compute::CLLSTMLayer::validate(&aclInputInfo, &aclInputToForgetWeightsInfo,
396 &aclInputToCellWeightsInfo,
397 &aclInputToOutputWeightsInfo,
398 &aclRecurrentToForgetWeightsInfo,
399 &aclRecurrentToCellWeightsInfo,
400 &aclRecurrentToOutputWeightsInfo,
401 &aclForgetGateBiasInfo,
403 &aclOutputGateBiasInfo,
404 &aclOutputStateInInfo, &aclCellStateInInfo,
405 &aclScratchBufferInfo, &aclOutputStateOutInfo,
406 &aclCellStateOutInfo, &aclOutputInfo,
407 lstm_params_info, activationLayerInfo,
408 cell_threshold, projection_threshold);
411 void ClLstmFloatWorkload::FreeUnusedTensors()
413 FreeTensorIfUnused(m_InputToInputWeightsTensor);
414 FreeTensorIfUnused(m_InputToForgetWeightsTensor);
415 FreeTensorIfUnused(m_InputToCellWeightsTensor);
416 FreeTensorIfUnused(m_InputToOutputWeightsTensor);
417 FreeTensorIfUnused(m_RecurrentToInputWeightsTensor);
418 FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor);
419 FreeTensorIfUnused(m_RecurrentToCellWeightsTensor);
420 FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor);
421 FreeTensorIfUnused(m_CellToInputWeightsTensor);
422 FreeTensorIfUnused(m_CellToForgetWeightsTensor);
423 FreeTensorIfUnused(m_CellToOutputWeightsTensor);
424 FreeTensorIfUnused(m_InputGateBiasTensor);
425 FreeTensorIfUnused(m_ForgetGateBiasTensor);
426 FreeTensorIfUnused(m_CellBiasTensor);
427 FreeTensorIfUnused(m_OutputGateBiasTensor);
428 FreeTensorIfUnused(m_ProjectionWeightsTensor);
429 FreeTensorIfUnused(m_ProjectionBiasTensor);
430 FreeTensorIfUnused(m_ScratchBuffer);
431 FreeTensorIfUnused(m_InputLayerNormWeightsTensor);
432 FreeTensorIfUnused(m_ForgetLayerNormWeightsTensor);
433 FreeTensorIfUnused(m_CellLayerNormWeightsTensor);
434 FreeTensorIfUnused(m_OutputLayerNormWeightsTensor);
bool m_ProjectionEnabled
Enable/disable the projection layer.
const TensorShape & GetShape() const
void InitializeArmComputeClTensorData(arm_compute::CLTensor &clTensor, const ConstCpuTensorHandle *handle)
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)
const QueueDescriptor m_Data
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)
ClLstmFloatWorkload(const LstmQueueDescriptor &descriptor, const WorkloadInfo &info)
Copyright (c) 2020 ARM Limited.
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.
std::vector< ITensorHandle * > m_Inputs
void Execute() const override