18 using namespace armcomputetensorutils;
29 arm_compute::LSTMParams<arm_compute::ITensor> lstm_param;
32 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
33 BuildArmComputeTensor(*m_InputToForgetWeightsTensor,
m_Data.m_InputToForgetWeights->GetTensorInfo());
35 m_InputToCellWeightsTensor = std::make_unique<arm_compute::Tensor>();
36 BuildArmComputeTensor(*m_InputToCellWeightsTensor,
m_Data.m_InputToCellWeights->GetTensorInfo());
38 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
39 BuildArmComputeTensor(*m_InputToOutputWeightsTensor,
m_Data.m_InputToOutputWeights->GetTensorInfo());
41 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
42 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor,
m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
44 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::Tensor>();
45 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor,
m_Data.m_RecurrentToCellWeights->GetTensorInfo());
47 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
48 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor,
m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
50 m_ForgetGateBiasTensor = std::make_unique<arm_compute::Tensor>();
51 BuildArmComputeTensor(*m_ForgetGateBiasTensor,
m_Data.m_ForgetGateBias->GetTensorInfo());
53 m_CellBiasTensor = std::make_unique<arm_compute::Tensor>();
54 BuildArmComputeTensor(*m_CellBiasTensor,
m_Data.m_CellBias->GetTensorInfo());
56 m_OutputGateBiasTensor = std::make_unique<arm_compute::Tensor>();
57 BuildArmComputeTensor(*m_OutputGateBiasTensor,
m_Data.m_OutputGateBias->GetTensorInfo());
60 if (!
m_Data.m_Parameters.m_CifgEnabled)
62 m_InputToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
63 BuildArmComputeTensor(*m_InputToInputWeightsTensor,
m_Data.m_InputToInputWeights->GetTensorInfo());
65 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
66 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor,
m_Data.m_RecurrentToInputWeights->GetTensorInfo());
68 m_CellToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
69 if (
m_Data.m_CellToInputWeights !=
nullptr)
71 BuildArmComputeTensor(*m_CellToInputWeightsTensor,
m_Data.m_CellToInputWeights->GetTensorInfo());
74 m_InputGateBiasTensor = std::make_unique<arm_compute::Tensor>();
75 BuildArmComputeTensor(*m_InputGateBiasTensor,
m_Data.m_InputGateBias->GetTensorInfo());
77 lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
78 m_RecurrentToInputWeightsTensor.get(),
79 m_Data.m_CellToInputWeights !=
nullptr ? m_CellToInputWeightsTensor.get() :
nullptr,
80 m_InputGateBiasTensor.get());
83 if (
m_Data.m_Parameters.m_ProjectionEnabled)
85 m_ProjectionWeightsTensor = std::make_unique<arm_compute::Tensor>();
86 BuildArmComputeTensor(*m_ProjectionWeightsTensor,
m_Data.m_ProjectionWeights->GetTensorInfo());
88 m_ProjectionBiasTensor = std::make_unique<arm_compute::Tensor>();
89 if (
m_Data.m_ProjectionBias !=
nullptr)
91 BuildArmComputeTensor(*m_ProjectionBiasTensor,
m_Data.m_ProjectionBias->GetTensorInfo());
94 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
95 m_Data.m_ProjectionBias !=
nullptr ? m_ProjectionBiasTensor.get() :
nullptr);
98 if (
m_Data.m_Parameters.m_PeepholeEnabled)
100 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
101 BuildArmComputeTensor(*m_CellToForgetWeightsTensor,
m_Data.m_CellToForgetWeights->GetTensorInfo());
103 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
104 BuildArmComputeTensor(*m_CellToOutputWeightsTensor,
m_Data.m_CellToOutputWeights->GetTensorInfo());
106 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
109 if (
m_Data.m_Parameters.m_LayerNormEnabled)
111 m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
112 if (!
m_Data.m_Parameters.m_CifgEnabled)
114 BuildArmComputeTensor(*m_InputLayerNormWeightsTensor,
m_Data.m_InputLayerNormWeights->GetTensorInfo());
117 m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
118 BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor,
m_Data.m_ForgetLayerNormWeights->GetTensorInfo());
120 m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
121 BuildArmComputeTensor(*m_CellLayerNormWeightsTensor,
m_Data.m_CellLayerNormWeights->GetTensorInfo());
123 m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
124 BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor,
m_Data.m_OutputLayerNormWeights->GetTensorInfo());
126 lstm_param.set_layer_normalization_params(
m_Data.m_Parameters.m_CifgEnabled ?
127 nullptr : m_InputLayerNormWeightsTensor.get(),
128 m_ForgetLayerNormWeightsTensor.get(),
129 m_CellLayerNormWeightsTensor.get(),
130 m_OutputLayerNormWeightsTensor.get());
143 const unsigned int batch_size = armnn::numeric_cast<unsigned int>(inputTensorInfo.
GetShape()[0]);
144 const unsigned int num_units = armnn::numeric_cast<unsigned int>(inputTensorInfo.
GetShape()[1]);
146 m_ScratchBuffer = std::make_unique<arm_compute::Tensor>();
147 if (
m_Data.m_Parameters.m_CifgEnabled)
151 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
157 BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2);
160 float cell_threshold =
m_Data.m_Parameters.m_ClippingThresCell;
161 float projection_threshold =
m_Data.m_Parameters.m_ClippingThresProj;
164 arm_compute::ActivationLayerInfo activationLayerInfo =
167 m_LstmLayer.configure(&input, m_InputToForgetWeightsTensor.get(), m_InputToCellWeightsTensor.get(),
168 m_InputToOutputWeightsTensor.get(), m_RecurrentToForgetWeightsTensor.get(),
169 m_RecurrentToCellWeightsTensor.get(), m_RecurrentToOutputWeightsTensor.get(),
170 m_ForgetGateBiasTensor.get(), m_CellBiasTensor.get(), m_OutputGateBiasTensor.get(),
171 &output_state_in, &cell_state_in, m_ScratchBuffer.get(), &output_state_out,
172 &cell_state_out, &output, lstm_param, activationLayerInfo,
173 cell_threshold, projection_threshold);
175 armcomputetensorutils::InitialiseArmComputeTensorEmpty(*m_ScratchBuffer);
178 m_Data.m_InputToForgetWeights);
180 m_Data.m_InputToCellWeights);
182 m_Data.m_InputToOutputWeights);
184 m_Data.m_RecurrentToForgetWeights);
186 m_Data.m_RecurrentToCellWeights);
188 m_Data.m_RecurrentToOutputWeights);
196 if (!
m_Data.m_Parameters.m_CifgEnabled)
199 m_Data.m_InputToInputWeights);
201 m_Data.m_RecurrentToInputWeights);
202 if (
m_Data.m_CellToInputWeights !=
nullptr)
205 m_Data.m_CellToInputWeights);
211 if (
m_Data.m_Parameters.m_ProjectionEnabled)
214 m_Data.m_ProjectionWeights);
215 if (
m_Data.m_ProjectionBias !=
nullptr)
222 if (
m_Data.m_Parameters.m_PeepholeEnabled)
225 m_Data.m_CellToForgetWeights);
227 m_Data.m_CellToOutputWeights);
230 if (
m_Data.m_Parameters.m_LayerNormEnabled)
232 if (!
m_Data.m_Parameters.m_CifgEnabled)
243 m_LstmLayer.prepare();
263 arm_compute::LSTMParams<arm_compute::ITensorInfo> lstm_params_info;
266 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
267 const arm_compute::TensorInfo aclOutputStateInInfo = BuildArmComputeTensorInfo(outputStateIn);
268 const arm_compute::TensorInfo aclCellStateInInfo = BuildArmComputeTensorInfo(cellStateIn);
269 const arm_compute::TensorInfo aclScratchBufferInfo = BuildArmComputeTensorInfo(scratchBuffer);
270 const arm_compute::TensorInfo aclOutputStateOutInfo = BuildArmComputeTensorInfo(outputStateOut);
271 const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut);
272 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
275 const arm_compute::TensorInfo aclInputToForgetWeightsInfo
277 const arm_compute::TensorInfo aclInputToCellWeightsInfo
279 const arm_compute::TensorInfo aclInputToOutputWeightsInfo
281 const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo
283 const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo
285 const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo
287 const arm_compute::TensorInfo aclForgetGateBiasInfo
289 const arm_compute::TensorInfo aclCellBiasInfo
290 = BuildArmComputeTensorInfo(paramsInfo.
GetCellBias());
291 const arm_compute::TensorInfo aclOutputGateBiasInfo
294 arm_compute::TensorInfo aclInputToInputWeightsInfo;
295 arm_compute::TensorInfo aclRecurrentToInputWeightsInfo;
296 arm_compute::TensorInfo aclCellToInputWeightsInfo;
297 arm_compute::TensorInfo aclInputGateBiasInfo;
298 arm_compute::TensorInfo aclProjectionWeightsInfo;
299 arm_compute::TensorInfo aclProjectionBiasInfo;
300 arm_compute::TensorInfo aclCellToForgetWeightsInfo;
301 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());
319 lstm_params_info.set_cifg_params(&aclInputToInputWeightsInfo, &aclRecurrentToInputWeightsInfo,
321 &aclInputGateBiasInfo);
328 aclProjectionBiasInfo = BuildArmComputeTensorInfo(paramsInfo.
GetProjectionBias());
332 lstm_params_info.set_projection_params(&aclProjectionWeightsInfo,
334 &aclProjectionBiasInfo :
nullptr);
342 lstm_params_info.set_peephole_params(&aclCellToForgetWeightsInfo, &aclCellToOutputWeightsInfo);
355 lstm_params_info.set_layer_normalization_params(descriptor.
m_CifgEnabled ?
356 nullptr : &aclInputLayerNormWeightsInfo,
357 &aclForgetLayerNormWeightsInfo,
358 &aclCellLayerNormWeightsInfo,
359 &aclOutputLayerNormWeightsInfo);
366 arm_compute::ActivationLayerInfo activationLayerInfo =
369 return arm_compute::NELSTMLayer::validate(&aclInputInfo,
370 &aclInputToForgetWeightsInfo,
371 &aclInputToCellWeightsInfo,
372 &aclInputToOutputWeightsInfo,
373 &aclRecurrentToForgetWeightsInfo,
374 &aclRecurrentToCellWeightsInfo,
375 &aclRecurrentToOutputWeightsInfo,
376 &aclForgetGateBiasInfo,
378 &aclOutputGateBiasInfo,
379 &aclOutputStateInInfo,
381 &aclScratchBufferInfo,
382 &aclOutputStateOutInfo,
383 &aclCellStateOutInfo,
388 projection_threshold);
391 void NeonLstmFloatWorkload::FreeUnusedTensors()
393 FreeTensorIfUnused(m_InputToInputWeightsTensor);
394 FreeTensorIfUnused(m_InputToForgetWeightsTensor);
395 FreeTensorIfUnused(m_InputToCellWeightsTensor);
396 FreeTensorIfUnused(m_InputToOutputWeightsTensor);
397 FreeTensorIfUnused(m_RecurrentToInputWeightsTensor);
398 FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor);
399 FreeTensorIfUnused(m_RecurrentToCellWeightsTensor);
400 FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor);
401 FreeTensorIfUnused(m_CellToInputWeightsTensor);
402 FreeTensorIfUnused(m_CellToForgetWeightsTensor);
403 FreeTensorIfUnused(m_CellToOutputWeightsTensor);
404 FreeTensorIfUnused(m_InputGateBiasTensor);
405 FreeTensorIfUnused(m_ForgetGateBiasTensor);
406 FreeTensorIfUnused(m_CellBiasTensor);
407 FreeTensorIfUnused(m_OutputGateBiasTensor);
408 FreeTensorIfUnused(m_ProjectionWeightsTensor);
409 FreeTensorIfUnused(m_ProjectionBiasTensor);
410 FreeTensorIfUnused(m_ScratchBuffer);
411 FreeTensorIfUnused(m_InputLayerNormWeightsTensor);
412 FreeTensorIfUnused(m_ForgetLayerNormWeightsTensor);
413 FreeTensorIfUnused(m_CellLayerNormWeightsTensor);
414 FreeTensorIfUnused(m_OutputLayerNormWeightsTensor);
450 void NeonLstmFloatWorkload::Reconfigure()