12 #include <arm_compute/runtime/CL/functions/CLLSTMLayer.h> 18 using namespace armcomputetensorutils;
23 arm_compute::LSTMParams<arm_compute::ICLTensor> lstm_param;
26 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
27 BuildArmComputeTensor(*m_InputToForgetWeightsTensor,
m_Data.m_InputToForgetWeights->GetTensorInfo());
29 m_InputToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
30 BuildArmComputeTensor(*m_InputToCellWeightsTensor,
m_Data.m_InputToCellWeights->GetTensorInfo());
32 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
33 BuildArmComputeTensor(*m_InputToOutputWeightsTensor,
m_Data.m_InputToOutputWeights->GetTensorInfo());
35 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
36 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor,
m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
38 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
39 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor,
m_Data.m_RecurrentToCellWeights->GetTensorInfo());
41 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
42 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor,
m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
44 m_ForgetGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
45 BuildArmComputeTensor(*m_ForgetGateBiasTensor,
m_Data.m_ForgetGateBias->GetTensorInfo());
47 m_CellBiasTensor = std::make_unique<arm_compute::CLTensor>();
48 BuildArmComputeTensor(*m_CellBiasTensor,
m_Data.m_CellBias->GetTensorInfo());
50 m_OutputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
51 BuildArmComputeTensor(*m_OutputGateBiasTensor,
m_Data.m_OutputGateBias->GetTensorInfo());
54 if (!
m_Data.m_Parameters.m_CifgEnabled)
56 m_InputToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
57 BuildArmComputeTensor(*m_InputToInputWeightsTensor,
m_Data.m_InputToInputWeights->GetTensorInfo());
59 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
60 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor,
m_Data.m_RecurrentToInputWeights->GetTensorInfo());
62 m_CellToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
63 if (
m_Data.m_CellToInputWeights !=
nullptr)
65 BuildArmComputeTensor(*m_CellToInputWeightsTensor,
m_Data.m_CellToInputWeights->GetTensorInfo());
68 m_InputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
69 BuildArmComputeTensor(*m_InputGateBiasTensor,
m_Data.m_InputGateBias->GetTensorInfo());
71 lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
72 m_RecurrentToInputWeightsTensor.get(),
73 m_Data.m_CellToInputWeights !=
nullptr ? m_CellToInputWeightsTensor.get() :
nullptr,
74 m_InputGateBiasTensor.get());
77 if (
m_Data.m_Parameters.m_ProjectionEnabled)
79 m_ProjectionWeightsTensor = std::make_unique<arm_compute::CLTensor>();
80 BuildArmComputeTensor(*m_ProjectionWeightsTensor,
m_Data.m_ProjectionWeights->GetTensorInfo());
82 m_ProjectionBiasTensor = std::make_unique<arm_compute::CLTensor>();
83 if (
m_Data.m_ProjectionBias !=
nullptr)
85 BuildArmComputeTensor(*m_ProjectionBiasTensor,
m_Data.m_ProjectionBias->GetTensorInfo());
88 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
89 m_Data.m_ProjectionBias !=
nullptr ? m_ProjectionBiasTensor.get() :
nullptr);
92 if (
m_Data.m_Parameters.m_PeepholeEnabled)
94 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
95 BuildArmComputeTensor(*m_CellToForgetWeightsTensor,
m_Data.m_CellToForgetWeights->GetTensorInfo());
97 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
98 BuildArmComputeTensor(*m_CellToOutputWeightsTensor,
m_Data.m_CellToOutputWeights->GetTensorInfo());
100 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
103 if (
m_Data.m_Parameters.m_LayerNormEnabled)
105 m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
106 m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
107 m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
108 m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
110 if (!
m_Data.m_Parameters.m_CifgEnabled)
112 BuildArmComputeTensor(*m_InputLayerNormWeightsTensor,
m_Data.m_InputLayerNormWeights->GetTensorInfo());
114 BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor,
m_Data.m_ForgetLayerNormWeights->GetTensorInfo());
115 BuildArmComputeTensor(*m_CellLayerNormWeightsTensor,
m_Data.m_CellLayerNormWeights->GetTensorInfo());
116 BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor,
m_Data.m_OutputLayerNormWeights->GetTensorInfo());
118 lstm_param.set_layer_normalization_params(
m_Data.m_Parameters.m_CifgEnabled ?
nullptr :
119 m_InputLayerNormWeightsTensor.get(),
120 m_ForgetLayerNormWeightsTensor.get(),
121 m_CellLayerNormWeightsTensor.get(),
122 m_OutputLayerNormWeightsTensor.get());
138 m_ScratchBuffer = std::make_unique<arm_compute::CLTensor>();
139 if (
m_Data.m_Parameters.m_CifgEnabled)
143 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
149 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2);
152 float cell_threshold =
m_Data.m_Parameters.m_ClippingThresCell;
153 float projection_threshold =
m_Data.m_Parameters.m_ClippingThresProj;
156 arm_compute::ActivationLayerInfo activationLayerInfo;
157 if (
m_Data.m_Parameters.m_ActivationFunc == 0)
161 else if (
m_Data.m_Parameters.m_ActivationFunc == 1)
163 activationLayerInfo = arm_compute::ActivationLayerInfo(
164 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
166 else if (
m_Data.m_Parameters.m_ActivationFunc == 3)
168 activationLayerInfo = arm_compute::ActivationLayerInfo(
169 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
171 else if (
m_Data.m_Parameters.m_ActivationFunc == 4)
173 activationLayerInfo = arm_compute::ActivationLayerInfo(
174 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
176 else if (
m_Data.m_Parameters.m_ActivationFunc == 6)
178 activationLayerInfo = arm_compute::ActivationLayerInfo(
179 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
186 m_LstmLayer.configure(&input, m_InputToForgetWeightsTensor.get(), m_InputToCellWeightsTensor.get(),
187 m_InputToOutputWeightsTensor.get(), m_RecurrentToForgetWeightsTensor.get(),
188 m_RecurrentToCellWeightsTensor.get(), m_RecurrentToOutputWeightsTensor.get(),
189 m_ForgetGateBiasTensor.get(), m_CellBiasTensor.get(), m_OutputGateBiasTensor.get(),
190 &output_state_in, &cell_state_in, m_ScratchBuffer.get(), &output_state_out,
191 &cell_state_out, &output, lstm_param, activationLayerInfo,
192 cell_threshold, projection_threshold);
194 armcomputetensorutils::InitialiseArmComputeTensorEmpty(*m_ScratchBuffer);
206 if (!
m_Data.m_Parameters.m_CifgEnabled)
210 if (
m_Data.m_CellToInputWeights !=
nullptr)
217 if (
m_Data.m_Parameters.m_ProjectionEnabled)
220 if (
m_Data.m_ProjectionBias !=
nullptr)
226 if (
m_Data.m_Parameters.m_PeepholeEnabled)
232 if (
m_Data.m_Parameters.m_LayerNormEnabled)
234 if (!
m_Data.m_Parameters.m_CifgEnabled)
246 m_LstmLayer.prepare();
262 arm_compute::LSTMParams<arm_compute::ITensorInfo> lstm_params_info;
265 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
266 const arm_compute::TensorInfo aclOutputStateInInfo = BuildArmComputeTensorInfo(outputStateIn);
267 const arm_compute::TensorInfo aclCellStateInInfo = BuildArmComputeTensorInfo(cellStateIn);
268 const arm_compute::TensorInfo aclScratchBufferInfo = BuildArmComputeTensorInfo(scratchBuffer);
269 const arm_compute::TensorInfo aclOutputStateOutInfo = BuildArmComputeTensorInfo(outputStateOut);
270 const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut);
271 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
274 const arm_compute::TensorInfo aclInputToForgetWeightsInfo
276 const arm_compute::TensorInfo aclInputToCellWeightsInfo
278 const arm_compute::TensorInfo aclInputToOutputWeightsInfo
280 const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo
282 const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo
284 const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo
286 const arm_compute::TensorInfo aclForgetGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetForgetGateBias());
287 const arm_compute::TensorInfo aclCellBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetCellBias());
288 const arm_compute::TensorInfo aclOutputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetOutputGateBias());
290 arm_compute::TensorInfo aclInputToInputWeightsInfo;
291 arm_compute::TensorInfo aclRecurrentToInputWeightsInfo;
292 arm_compute::TensorInfo aclCellToInputWeightsInfo;
293 arm_compute::TensorInfo aclInputGateBiasInfo;
294 arm_compute::TensorInfo aclProjectionWeightsInfo;
295 arm_compute::TensorInfo aclProjectionBiasInfo;
296 arm_compute::TensorInfo aclCellToForgetWeightsInfo;
297 arm_compute::TensorInfo aclCellToOutputWeightsInfo;
298 arm_compute::TensorInfo aclInputLayerNormWeightsInfo;
299 arm_compute::TensorInfo aclForgetLayerNormWeightsInfo;
300 arm_compute::TensorInfo aclCellLayerNormWeightsInfo;
301 arm_compute::TensorInfo aclOutputLayerNormWeightsInfo;
312 aclInputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetInputGateBias());
313 lstm_params_info.set_cifg_params(&aclInputToInputWeightsInfo, &aclRecurrentToInputWeightsInfo,
315 &aclCellToInputWeightsInfo:
nullptr,
316 &aclInputGateBiasInfo);
325 aclProjectionBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetInputGateBias());
327 lstm_params_info.set_projection_params(&aclProjectionWeightsInfo,
329 &aclProjectionBiasInfo:
nullptr);
336 lstm_params_info.set_peephole_params(&aclCellToForgetWeightsInfo, &aclCellToOutputWeightsInfo);
343 arm_compute::ActivationLayerInfo activationLayerInfo;
350 activationLayerInfo = arm_compute::ActivationLayerInfo(
351 arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
355 activationLayerInfo = arm_compute::ActivationLayerInfo(
356 arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
360 activationLayerInfo = arm_compute::ActivationLayerInfo(
361 arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
365 activationLayerInfo = arm_compute::ActivationLayerInfo(
366 arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
386 lstm_params_info.set_layer_normalization_params(descriptor.
m_CifgEnabled ?
387 nullptr : &aclInputLayerNormWeightsInfo,
388 &aclForgetLayerNormWeightsInfo,
389 &aclCellLayerNormWeightsInfo,
390 &aclOutputLayerNormWeightsInfo);
393 return arm_compute::CLLSTMLayer::validate(&aclInputInfo, &aclInputToForgetWeightsInfo,
394 &aclInputToCellWeightsInfo,
395 &aclInputToOutputWeightsInfo,
396 &aclRecurrentToForgetWeightsInfo,
397 &aclRecurrentToCellWeightsInfo,
398 &aclRecurrentToOutputWeightsInfo,
399 &aclForgetGateBiasInfo,
401 &aclOutputGateBiasInfo,
402 &aclOutputStateInInfo, &aclCellStateInInfo,
403 &aclScratchBufferInfo, &aclOutputStateOutInfo,
404 &aclCellStateOutInfo, &aclOutputInfo,
405 lstm_params_info, activationLayerInfo,
406 cell_threshold, projection_threshold);
409 void ClLstmFloatWorkload::FreeUnusedTensors()
411 FreeTensorIfUnused(m_InputToInputWeightsTensor);
412 FreeTensorIfUnused(m_InputToForgetWeightsTensor);
413 FreeTensorIfUnused(m_InputToCellWeightsTensor);
414 FreeTensorIfUnused(m_InputToOutputWeightsTensor);
415 FreeTensorIfUnused(m_RecurrentToInputWeightsTensor);
416 FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor);
417 FreeTensorIfUnused(m_RecurrentToCellWeightsTensor);
418 FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor);
419 FreeTensorIfUnused(m_CellToInputWeightsTensor);
420 FreeTensorIfUnused(m_CellToForgetWeightsTensor);
421 FreeTensorIfUnused(m_CellToOutputWeightsTensor);
422 FreeTensorIfUnused(m_InputGateBiasTensor);
423 FreeTensorIfUnused(m_ForgetGateBiasTensor);
424 FreeTensorIfUnused(m_CellBiasTensor);
425 FreeTensorIfUnused(m_OutputGateBiasTensor);
426 FreeTensorIfUnused(m_ProjectionWeightsTensor);
427 FreeTensorIfUnused(m_ProjectionBiasTensor);
428 FreeTensorIfUnused(m_ScratchBuffer);
429 FreeTensorIfUnused(m_InputLayerNormWeightsTensor);
430 FreeTensorIfUnused(m_ForgetLayerNormWeightsTensor);
431 FreeTensorIfUnused(m_CellLayerNormWeightsTensor);
432 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.
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
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.
Contains information about inputs and outputs to a layer.
std::vector< ITensorHandle * > m_Inputs
void Execute() const override