34 : NeonBaseWorkload<UnidirectionalSequenceLstmQueueDescriptor>(descriptor, info)
35{
36
38 descriptor.m_Parameters,
39 info,
40 GetGuid());
41
42
43 const arm_compute::ITensor& input = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
44 arm_compute::ITensor& outputStateIn = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
45 const arm_compute::ITensor& cellStateIn = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
46
47 arm_compute::ITensor& outputStateOut = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
48 arm_compute::ITensor& cellStateOut = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[1])->GetTensor();
49 arm_compute::ITensor& output = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[2])->GetTensor();
50
51 TensorInfo inputInfo =
info.m_InputTensorInfos[0];
52 TensorInfo outputInfo =
info.m_OutputTensorInfos[2];
53
54 TensorShape inputLayerShape = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetShape();
55 TensorShape outputLayerShape = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[2])->GetShape();
56
57 unsigned int maxTime = m_Data.m_Parameters.m_TimeMajor ? inputLayerShape[0] : inputLayerShape[1];
58 unsigned int batchSize = m_Data.m_Parameters.m_TimeMajor ? inputLayerShape[1] : inputLayerShape[0];
59 unsigned int inputSize = inputLayerShape[2];
60 unsigned int outputSize = outputLayerShape[2];
61
62 const TensorShape timeMajorShapeInput({maxTime, batchSize, inputSize});
63 const TensorShape timeMajorShapeOutput({maxTime, batchSize, outputSize});
64
65
66
67
68 if (!m_Data.m_Parameters.m_TimeMajor)
69 {
70 std::unique_ptr<arm_compute::NEPermute> layer(new arm_compute::NEPermute());
71
72 TensorInfo permuteOutInfo = inputInfo;
73 permuteOutInfo.SetShape(timeMajorShapeInput);
74 BuildArmComputeTensor(m_PermuteFirstOut, permuteOutInfo);
75 armcomputetensorutils::InitialiseArmComputeTensorEmpty(m_PermuteFirstOut);
76
77
78 layer->configure(&input, &m_PermuteFirstOut, arm_compute::PermutationVector(0U,2U,1U));
79 m_Permute1.reset(layer.release());
80 }
81
82
83
84
85 for (unsigned int i = 0; i < maxTime; ++i)
86 {
87 arm_compute::Tensor splitter_out;
88 arm_compute::Tensor concat_in;
89
90 auto splitterTensorInfo = inputInfo;
91 auto concatTensorInfo = outputInfo;
92 splitterTensorInfo.SetShape({batchSize, inputSize});
93 concatTensorInfo.SetShape({batchSize, outputSize});
94 BuildArmComputeTensor(splitter_out, splitterTensorInfo);
95 BuildArmComputeTensor(concat_in, concatTensorInfo);
96
97 armcomputetensorutils::InitialiseArmComputeTensorEmpty(splitter_out);
98 armcomputetensorutils::InitialiseArmComputeTensorEmpty(concat_in);
99
100
101 m_SplitterOutputsTensors.push_back(std::move(splitter_out));
102 m_ConcatInputsTensors.push_back(std::move(concat_in));
103 }
104
105 for (unsigned int i = 0; i < maxTime; ++i)
106 {
107
108 m_SplitterOutputs.push_back(&m_SplitterOutputsTensors[i]);
109 m_ConcatInputs.push_back(&m_ConcatInputsTensors[i]);
110 }
111
112
113
114
115 unsigned int numberDimensions = 3;
116 unsigned int dimension = 0;
117
118 if (maxTime != 1)
119 {
120 ViewsDescriptor splitterDesc(maxTime, numberDimensions);
121 unsigned int splitterDimSizes[3] = {1, batchSize, inputSize};
122 for (unsigned int outputIdx = 0u; outputIdx < maxTime; ++outputIdx)
123 {
124 splitterDesc.SetViewOriginCoord(outputIdx, dimension, splitterDimSizes[dimension] * outputIdx);
125 for (unsigned int dimIdx = 0u; dimIdx < numberDimensions; ++dimIdx)
126 {
127 splitterDesc.SetViewSize(outputIdx, dimIdx, splitterDimSizes[dimIdx]);
128 }
129 }
130
131 std::set<unsigned int> splitAxis =
ComputeSplitAxis(splitterDesc, timeMajorShapeInput);
132
133 std::unique_ptr<arm_compute::NESplit> split_layer(new arm_compute::NESplit());
134 unsigned int aclAxisSplit = CalcAclAxis(splitterDesc.GetNumDimensions(),
135 *splitAxis.begin());
136 if (!m_Data.m_Parameters.m_TimeMajor)
137 {
138 split_layer->configure(&m_PermuteFirstOut, m_SplitterOutputs, aclAxisSplit);
139 } else
140 {
141 split_layer->configure(&input, m_SplitterOutputs, aclAxisSplit);
142 }
143
144 split_layer->prepare();
145 m_Splitter.reset(split_layer.release());
146 }
147
148
149
150
151 arm_compute::LSTMParams<arm_compute::ITensor> lstm_param;
152
153 lstm_param.set_cell_clip_params(descriptor.m_Parameters.m_ClippingThresCell);
154 lstm_param.set_projection_clip_params(descriptor.m_Parameters.m_ClippingThresProj);
155
156 lstm_param.set_matmul_scale_params(descriptor.m_Parameters.m_InputIntermediateScale,
157 descriptor.m_Parameters.m_ForgetIntermediateScale,
158 descriptor.m_Parameters.m_CellIntermediateScale,
159 descriptor.m_Parameters.m_OutputIntermediateScale);
160
161 lstm_param.set_hidden_state_params(descriptor.m_Parameters.m_HiddenStateZeroPoint,
162 descriptor.m_Parameters.m_HiddenStateScale);
163
164 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
165 BuildArmComputeTensor(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights->GetTensorInfo());
166
167 m_InputToCellWeightsTensor = std::make_unique<arm_compute::Tensor>();
168 BuildArmComputeTensor(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights->GetTensorInfo());
169
170 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
171 BuildArmComputeTensor(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights->GetTensorInfo());
172
173 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
174 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
175
176 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::Tensor>();
177 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights->GetTensorInfo());
178
179 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
180 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
181
182 m_ForgetGateBiasTensor = std::make_unique<arm_compute::Tensor>();
183 BuildArmComputeTensor(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias->GetTensorInfo());
184
185 m_CellBiasTensor = std::make_unique<arm_compute::Tensor>();
186 BuildArmComputeTensor(*m_CellBiasTensor, m_Data.m_CellBias->GetTensorInfo());
187
188 m_OutputGateBiasTensor = std::make_unique<arm_compute::Tensor>();
189 BuildArmComputeTensor(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias->GetTensorInfo());
190
191
192 if (!m_Data.m_Parameters.m_CifgEnabled)
193 {
194 m_InputToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
195 BuildArmComputeTensor(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights->GetTensorInfo());
196
197 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
198 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights->GetTensorInfo());
199
200 m_CellToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
201 if (m_Data.m_CellToInputWeights != nullptr)
202 {
203 BuildArmComputeTensor(*m_CellToInputWeightsTensor, m_Data.m_CellToInputWeights->GetTensorInfo());
204 }
205
206 m_InputGateBiasTensor = std::make_unique<arm_compute::Tensor>();
207 BuildArmComputeTensor(*m_InputGateBiasTensor, m_Data.m_InputGateBias->GetTensorInfo());
208 lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
209 m_RecurrentToInputWeightsTensor.get(),
210 m_Data.m_CellToInputWeights ? m_CellToInputWeightsTensor.get() : nullptr,
211 m_InputGateBiasTensor.get());
212 }
213
214 if (m_Data.m_Parameters.m_ProjectionEnabled)
215 {
216 m_ProjectionWeightsTensor = std::make_unique<arm_compute::Tensor>();
217 BuildArmComputeTensor(*m_ProjectionWeightsTensor, m_Data.m_ProjectionWeights->GetTensorInfo());
218
219 m_ProjectionBiasTensor = std::make_unique<arm_compute::Tensor>();
220 if (m_Data.m_ProjectionBias != nullptr)
221 {
222 BuildArmComputeTensor(*m_ProjectionBiasTensor, m_Data.m_ProjectionBias->GetTensorInfo());
223 }
224
225 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
226 m_Data.m_ProjectionBias ? m_ProjectionBiasTensor.get() : nullptr);
227 }
228
229 if (m_Data.m_Parameters.m_PeepholeEnabled)
230 {
231 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
232 BuildArmComputeTensor(*m_CellToForgetWeightsTensor, m_Data.m_CellToForgetWeights->GetTensorInfo());
233
234 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
235 BuildArmComputeTensor(*m_CellToOutputWeightsTensor, m_Data.m_CellToOutputWeights->GetTensorInfo());
236
237 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
238 }
239
240 if (m_Data.m_Parameters.m_LayerNormEnabled)
241 {
242 m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
243 if (!m_Data.m_Parameters.m_CifgEnabled)
244 {
245 BuildArmComputeTensor(*m_InputLayerNormWeightsTensor, m_Data.m_InputLayerNormWeights->GetTensorInfo());
246 }
247
248 m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
249 BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor, m_Data.m_ForgetLayerNormWeights->GetTensorInfo());
250
251 m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
252 BuildArmComputeTensor(*m_CellLayerNormWeightsTensor, m_Data.m_CellLayerNormWeights->GetTensorInfo());
253
254 m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
255 BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor, m_Data.m_OutputLayerNormWeights->GetTensorInfo());
256
257 auto inputNormWeightTensor = m_Data.m_Parameters.m_CifgEnabled ? nullptr : m_InputLayerNormWeightsTensor.get();
258 lstm_param.set_layer_normalization_params(inputNormWeightTensor,
259 m_ForgetLayerNormWeightsTensor.get(),
260 m_CellLayerNormWeightsTensor.get(),
261 m_OutputLayerNormWeightsTensor.get());
262 }
263
264 for (unsigned int i = 0; i != maxTime; ++i)
265 {
266
267
268 arm_compute::ITensor* outputLSTM;
269 arm_compute::ITensor* inputLSTM;
270
271
272
273
274
275 if (maxTime == 1 && m_Data.m_Parameters.m_TimeMajor)
276 {
277 TensorShape inputShape =
GetTensorShape(input.info()->tensor_shape(), 1U);
278 TensorShape outputShape =
GetTensorShape(output.info()->tensor_shape(), 1U);
279
280 TensorShape inputShapeShrink({inputShape[1], inputShape[2]});
281 TensorShape outputShapeShrink({outputShape[1], outputShape[2]});
282
283 auto acl_input_shape_shrink = BuildArmComputeTensorShape(inputShapeShrink);
284 auto acl_output_shape_shrink = BuildArmComputeTensorShape(outputShapeShrink);
285
286 input.info()->set_tensor_shape(acl_input_shape_shrink);
287 inputLSTM = const_cast<arm_compute::ITensor*>(&input);
288
289 output.info()->set_tensor_shape(acl_output_shape_shrink);
290 outputLSTM = &output;
291 }
292
293
294
295
296 else if (maxTime == 1 && !m_Data.m_Parameters.m_TimeMajor)
297 {
298 TensorShape inputShape =
GetTensorShape(m_PermuteFirstOut.info()->tensor_shape(), 1U);
299 TensorShape inputShapeShrink({inputShape[1], inputShape[2]});
300 auto acl_input_shape_shrink = BuildArmComputeTensorShape(inputShapeShrink);
301 m_PermuteFirstOut.info()->set_tensor_shape(acl_input_shape_shrink);
302 inputLSTM = &m_PermuteFirstOut;
303
304 outputLSTM = const_cast<arm_compute::ITensor*>(m_ConcatInputs[i]);
305 }
306
307 else
308 {
309 inputLSTM = m_SplitterOutputs[i];
310 outputLSTM = const_cast<arm_compute::ITensor*>(m_ConcatInputs[i]);
311 }
312
313 std::unique_ptr<arm_compute::NEQLSTMLayer> lstm_layer(new arm_compute::NEQLSTMLayer());
314
315 lstm_layer->configure(inputLSTM,
316 m_InputToForgetWeightsTensor.get(),
317 m_InputToCellWeightsTensor.get(),
318 m_InputToOutputWeightsTensor.get(),
319 m_RecurrentToForgetWeightsTensor.get(),
320 m_RecurrentToCellWeightsTensor.get(),
321 m_RecurrentToOutputWeightsTensor.get(),
322 m_ForgetGateBiasTensor.get(),
323 m_CellBiasTensor.get(),
324 m_OutputGateBiasTensor.get(),
325 &cellStateIn,
326 &outputStateIn,
327 &cellStateOut,
328 &outputStateOut,
329 outputLSTM,
330 lstm_param);
331
332 m_Layers.emplace_back(std::move(lstm_layer));
333 }
334
344
345 if (!m_Data.m_Parameters.m_CifgEnabled)
346 {
349 if (m_Data.m_CellToInputWeights != nullptr)
350 {
352 }
354 }
355
356 if (m_Data.m_Parameters.m_ProjectionEnabled)
357 {
359 if (m_Data.m_ProjectionBias != nullptr)
360 {
362 }
363 }
364
365 if (m_Data.m_Parameters.m_PeepholeEnabled)
366 {
369 }
370
371 if (m_Data.m_Parameters.m_LayerNormEnabled)
372 {
373 if (!m_Data.m_Parameters.m_CifgEnabled)
374 {
376 }
380 }
381
382
383
384 for (uint32_t i = 0; i < m_Layers.size(); ++i)
385 {
386 m_Layers[i]->prepare();
387 }
388
389
390
391
392
393
395 TensorShape shapeExpandTimeMajor({1, shape[0], shape[1]});
396 TensorShape shapeExpandBatchMajor({shape[0], 1, shape[1]});
397
398 if (maxTime != 1)
399 {
400 for (unsigned int i = 0; i < maxTime; ++i)
401 {
402 m_ConcatInputs[i]->info()->set_tensor_shape(BuildArmComputeTensorShape(shapeExpandTimeMajor));
403 }
405
406 for (unsigned int inputIdx = 0u; inputIdx < maxTime; ++inputIdx)
407 {
408 concatDescriptor.SetViewOriginCoord(inputIdx, dimension, inputIdx);
409 concatDescriptor.SetConcatAxis(dimension);
410 }
411 m_Concat.reset(new arm_compute::NEConcatenateLayer());
412
413 unsigned int aclAxisConcat = CalcAclAxis(concatDescriptor.GetNumDimensions(), concatDescriptor.GetConcatAxis());
414 if (!m_Data.m_Parameters.m_TimeMajor)
415 {
416 TensorInfo concatOutputTensorInfo = outputInfo;
417 concatOutputTensorInfo.SetShape(timeMajorShapeOutput);
418 BuildArmComputeTensor(concat_out, concatOutputTensorInfo);
419 armcomputetensorutils::InitialiseArmComputeTensorEmpty(concat_out);
420
421 m_Concat->configure(m_ConcatInputs, &concat_out, aclAxisConcat);
422 }
423 else
424 {
425 m_Concat->configure(m_ConcatInputs, &output, aclAxisConcat);
426 }
427
428 m_Concat->prepare();
429 }
430
431
432 else
433 {
434 if (!m_Data.m_Parameters.m_TimeMajor)
435 {
436 output.info()->set_tensor_shape(BuildArmComputeTensorShape(shapeExpandBatchMajor));
437 }
438 else
439 {
440 output.info()->set_tensor_shape(BuildArmComputeTensorShape(shapeExpandTimeMajor));
441 }
442 }
443
444
445
446
447 if (!m_Data.m_Parameters.m_TimeMajor)
448 {
449
450 std::unique_ptr<arm_compute::NEPermute> layer(new arm_compute::NEPermute());
451 if (maxTime != 1)
452 {
453 layer->configure(&concat_out, &output, arm_compute::PermutationVector(0U, 2U, 1U));
454 }
455 else
456 {
457 layer->configure(m_ConcatInputs[0], &output, arm_compute::PermutationVector(0U, 2U, 1U));
458 }
459 m_Permute2.reset(layer.release());
460 }
461
462 FreeUnusedTensors();
463}
#define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos, guid)
std::set< unsigned int > ComputeSplitAxis(const armnn::SplitterDescriptor &desc, const TensorShape &input)
Calculates the axis values for split operation.
OriginsDescriptor ConcatDescriptor
void InitializeArmComputeTensorData(arm_compute::Tensor &tensor, TensorInfo tensorInfo, const ITensorHandle *handle)
armnn::TensorShape GetTensorShape(unsigned int numberOfBatches, unsigned int numberOfChannels, unsigned int height, unsigned int width, const armnn::DataLayout dataLayout)