15 #include <arm_compute/runtime/CL/functions/CLLSTMLayer.h>
21 using namespace armcomputetensorutils;
25 const arm_compute::CLCompileContext& clCompileContext)
34 arm_compute::LSTMParams<arm_compute::ICLTensor> lstm_param;
37 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
38 BuildArmComputeTensor(*m_InputToForgetWeightsTensor,
m_Data.m_InputToForgetWeights->GetTensorInfo());
40 m_InputToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
41 BuildArmComputeTensor(*m_InputToCellWeightsTensor,
m_Data.m_InputToCellWeights->GetTensorInfo());
43 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
44 BuildArmComputeTensor(*m_InputToOutputWeightsTensor,
m_Data.m_InputToOutputWeights->GetTensorInfo());
46 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
47 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor,
m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
49 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
50 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor,
m_Data.m_RecurrentToCellWeights->GetTensorInfo());
52 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
53 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor,
m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
55 m_ForgetGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
56 BuildArmComputeTensor(*m_ForgetGateBiasTensor,
m_Data.m_ForgetGateBias->GetTensorInfo());
58 m_CellBiasTensor = std::make_unique<arm_compute::CLTensor>();
59 BuildArmComputeTensor(*m_CellBiasTensor,
m_Data.m_CellBias->GetTensorInfo());
61 m_OutputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
62 BuildArmComputeTensor(*m_OutputGateBiasTensor,
m_Data.m_OutputGateBias->GetTensorInfo());
65 if (!
m_Data.m_Parameters.m_CifgEnabled)
67 m_InputToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
68 BuildArmComputeTensor(*m_InputToInputWeightsTensor,
m_Data.m_InputToInputWeights->GetTensorInfo());
70 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
71 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor,
m_Data.m_RecurrentToInputWeights->GetTensorInfo());
73 m_CellToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
74 if (
m_Data.m_CellToInputWeights !=
nullptr)
76 BuildArmComputeTensor(*m_CellToInputWeightsTensor,
m_Data.m_CellToInputWeights->GetTensorInfo());
79 m_InputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
80 BuildArmComputeTensor(*m_InputGateBiasTensor,
m_Data.m_InputGateBias->GetTensorInfo());
82 lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
83 m_RecurrentToInputWeightsTensor.get(),
84 m_Data.m_CellToInputWeights !=
nullptr ? m_CellToInputWeightsTensor.get() :
nullptr,
85 m_InputGateBiasTensor.get());
88 if (
m_Data.m_Parameters.m_ProjectionEnabled)
90 m_ProjectionWeightsTensor = std::make_unique<arm_compute::CLTensor>();
91 BuildArmComputeTensor(*m_ProjectionWeightsTensor,
m_Data.m_ProjectionWeights->GetTensorInfo());
93 m_ProjectionBiasTensor = std::make_unique<arm_compute::CLTensor>();
94 if (
m_Data.m_ProjectionBias !=
nullptr)
96 BuildArmComputeTensor(*m_ProjectionBiasTensor,
m_Data.m_ProjectionBias->GetTensorInfo());
99 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
100 m_Data.m_ProjectionBias !=
nullptr ? m_ProjectionBiasTensor.get() :
nullptr);
103 if (
m_Data.m_Parameters.m_PeepholeEnabled)
105 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
106 BuildArmComputeTensor(*m_CellToForgetWeightsTensor,
m_Data.m_CellToForgetWeights->GetTensorInfo());
108 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
109 BuildArmComputeTensor(*m_CellToOutputWeightsTensor,
m_Data.m_CellToOutputWeights->GetTensorInfo());
111 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
114 if (
m_Data.m_Parameters.m_LayerNormEnabled)
116 m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
117 m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
118 m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
119 m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
121 if (!
m_Data.m_Parameters.m_CifgEnabled)
123 BuildArmComputeTensor(*m_InputLayerNormWeightsTensor,
m_Data.m_InputLayerNormWeights->GetTensorInfo());
125 BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor,
m_Data.m_ForgetLayerNormWeights->GetTensorInfo());
126 BuildArmComputeTensor(*m_CellLayerNormWeightsTensor,
m_Data.m_CellLayerNormWeights->GetTensorInfo());
127 BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor,
m_Data.m_OutputLayerNormWeights->GetTensorInfo());
129 lstm_param.set_layer_normalization_params(
m_Data.m_Parameters.m_CifgEnabled ?
nullptr :
130 m_InputLayerNormWeightsTensor.get(),
131 m_ForgetLayerNormWeightsTensor.get(),
132 m_CellLayerNormWeightsTensor.get(),
133 m_OutputLayerNormWeightsTensor.get());
146 const unsigned int batch_size = armnn::numeric_cast<unsigned int>(inputTensorInfo.
GetShape()[0]);
147 const unsigned int num_units = armnn::numeric_cast<unsigned int>(inputTensorInfo.
GetShape()[1]);
149 m_ScratchBuffer = std::make_unique<arm_compute::CLTensor>();
150 if (
m_Data.m_Parameters.m_CifgEnabled)
154 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
160 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2);
163 float cell_threshold =
m_Data.m_Parameters.m_ClippingThresCell;
164 float projection_threshold =
m_Data.m_Parameters.m_ClippingThresProj;
167 arm_compute::ActivationLayerInfo activationLayerInfo =
172 m_LstmLayer.configure(clCompileContext, &input, m_InputToForgetWeightsTensor.get(),
173 m_InputToCellWeightsTensor.get(), m_InputToOutputWeightsTensor.get(),
174 m_RecurrentToForgetWeightsTensor.get(), m_RecurrentToCellWeightsTensor.get(),
175 m_RecurrentToOutputWeightsTensor.get(), m_ForgetGateBiasTensor.get(),
176 m_CellBiasTensor.get(), m_OutputGateBiasTensor.get(), &output_state_in,
177 &cell_state_in, m_ScratchBuffer.get(), &output_state_out,
178 &cell_state_out, &output, lstm_param, activationLayerInfo,
179 cell_threshold, projection_threshold);
182 armcomputetensorutils::InitialiseArmComputeTensorEmpty(*m_ScratchBuffer);
194 if (!
m_Data.m_Parameters.m_CifgEnabled)
198 if (
m_Data.m_CellToInputWeights !=
nullptr)
205 if (
m_Data.m_Parameters.m_ProjectionEnabled)
208 if (
m_Data.m_ProjectionBias !=
nullptr)
214 if (
m_Data.m_Parameters.m_PeepholeEnabled)
220 if (
m_Data.m_Parameters.m_LayerNormEnabled)
222 if (!
m_Data.m_Parameters.m_CifgEnabled)
234 m_LstmLayer.prepare();
250 arm_compute::LSTMParams<arm_compute::ITensorInfo> lstm_params_info;
253 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
254 const arm_compute::TensorInfo aclOutputStateInInfo = BuildArmComputeTensorInfo(outputStateIn);
255 const arm_compute::TensorInfo aclCellStateInInfo = BuildArmComputeTensorInfo(cellStateIn);
256 const arm_compute::TensorInfo aclScratchBufferInfo = BuildArmComputeTensorInfo(scratchBuffer);
257 const arm_compute::TensorInfo aclOutputStateOutInfo = BuildArmComputeTensorInfo(outputStateOut);
258 const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut);
259 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
262 const arm_compute::TensorInfo aclInputToForgetWeightsInfo
264 const arm_compute::TensorInfo aclInputToCellWeightsInfo
266 const arm_compute::TensorInfo aclInputToOutputWeightsInfo
268 const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo
270 const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo
272 const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo
274 const arm_compute::TensorInfo aclForgetGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetForgetGateBias());
275 const arm_compute::TensorInfo aclCellBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetCellBias());
276 const arm_compute::TensorInfo aclOutputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetOutputGateBias());
278 arm_compute::TensorInfo aclInputToInputWeightsInfo;
279 arm_compute::TensorInfo aclRecurrentToInputWeightsInfo;
280 arm_compute::TensorInfo aclCellToInputWeightsInfo;
281 arm_compute::TensorInfo aclInputGateBiasInfo;
282 arm_compute::TensorInfo aclProjectionWeightsInfo;
283 arm_compute::TensorInfo aclProjectionBiasInfo;
284 arm_compute::TensorInfo aclCellToForgetWeightsInfo;
285 arm_compute::TensorInfo aclCellToOutputWeightsInfo;
286 arm_compute::TensorInfo aclInputLayerNormWeightsInfo;
287 arm_compute::TensorInfo aclForgetLayerNormWeightsInfo;
288 arm_compute::TensorInfo aclCellLayerNormWeightsInfo;
289 arm_compute::TensorInfo aclOutputLayerNormWeightsInfo;
300 aclInputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetInputGateBias());
301 lstm_params_info.set_cifg_params(&aclInputToInputWeightsInfo, &aclRecurrentToInputWeightsInfo,
303 &aclCellToInputWeightsInfo:
nullptr,
304 &aclInputGateBiasInfo);
313 aclProjectionBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetProjectionBias());
315 lstm_params_info.set_projection_params(&aclProjectionWeightsInfo,
317 &aclProjectionBiasInfo:
nullptr);
324 lstm_params_info.set_peephole_params(&aclCellToForgetWeightsInfo, &aclCellToOutputWeightsInfo);
331 arm_compute::ActivationLayerInfo activationLayerInfo =
347 lstm_params_info.set_layer_normalization_params(descriptor.
m_CifgEnabled ?
348 nullptr : &aclInputLayerNormWeightsInfo,
349 &aclForgetLayerNormWeightsInfo,
350 &aclCellLayerNormWeightsInfo,
351 &aclOutputLayerNormWeightsInfo);
354 return arm_compute::CLLSTMLayer::validate(&aclInputInfo, &aclInputToForgetWeightsInfo,
355 &aclInputToCellWeightsInfo,
356 &aclInputToOutputWeightsInfo,
357 &aclRecurrentToForgetWeightsInfo,
358 &aclRecurrentToCellWeightsInfo,
359 &aclRecurrentToOutputWeightsInfo,
360 &aclForgetGateBiasInfo,
362 &aclOutputGateBiasInfo,
363 &aclOutputStateInInfo, &aclCellStateInInfo,
364 &aclScratchBufferInfo, &aclOutputStateOutInfo,
365 &aclCellStateOutInfo, &aclOutputInfo,
366 lstm_params_info, activationLayerInfo,
367 cell_threshold, projection_threshold);
370 void ClLstmFloatWorkload::FreeUnusedTensors()
372 FreeTensorIfUnused(m_InputToInputWeightsTensor);
373 FreeTensorIfUnused(m_InputToForgetWeightsTensor);
374 FreeTensorIfUnused(m_InputToCellWeightsTensor);
375 FreeTensorIfUnused(m_InputToOutputWeightsTensor);
376 FreeTensorIfUnused(m_RecurrentToInputWeightsTensor);
377 FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor);
378 FreeTensorIfUnused(m_RecurrentToCellWeightsTensor);
379 FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor);
380 FreeTensorIfUnused(m_CellToInputWeightsTensor);
381 FreeTensorIfUnused(m_CellToForgetWeightsTensor);
382 FreeTensorIfUnused(m_CellToOutputWeightsTensor);
383 FreeTensorIfUnused(m_InputGateBiasTensor);
384 FreeTensorIfUnused(m_ForgetGateBiasTensor);
385 FreeTensorIfUnused(m_CellBiasTensor);
386 FreeTensorIfUnused(m_OutputGateBiasTensor);
387 FreeTensorIfUnused(m_ProjectionWeightsTensor);
388 FreeTensorIfUnused(m_ProjectionBiasTensor);
389 FreeTensorIfUnused(m_ScratchBuffer);
390 FreeTensorIfUnused(m_InputLayerNormWeightsTensor);
391 FreeTensorIfUnused(m_ForgetLayerNormWeightsTensor);
392 FreeTensorIfUnused(m_CellLayerNormWeightsTensor);
393 FreeTensorIfUnused(m_OutputLayerNormWeightsTensor);
429 void ClLstmFloatWorkload::Reconfigure()