34 : NeonBaseWorkload<UnidirectionalSequenceLstmQueueDescriptor>(descriptor, info)
38 descriptor.m_Parameters,
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();
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();
51 TensorInfo inputInfo =
info.m_InputTensorInfos[0];
52 TensorInfo outputInfo =
info.m_OutputTensorInfos[2];
54 TensorShape inputLayerShape =
static_cast<IAclTensorHandle*
>(
m_Data.
m_Inputs[0])->GetShape();
55 TensorShape outputLayerShape =
static_cast<IAclTensorHandle*
>(
m_Data.
m_Outputs[2])->GetShape();
59 unsigned int inputSize = inputLayerShape[2];
60 unsigned int outputSize = outputLayerShape[2];
62 const TensorShape timeMajorShapeInput({maxTime, batchSize, inputSize});
63 const TensorShape timeMajorShapeOutput({maxTime, batchSize, outputSize});
70 std::unique_ptr<arm_compute::NEPermute> layer(
new arm_compute::NEPermute());
72 TensorInfo permuteOutInfo = inputInfo;
73 permuteOutInfo.SetShape(timeMajorShapeInput);
74 BuildArmComputeTensor(m_PermuteFirstOut, permuteOutInfo);
75 armcomputetensorutils::InitialiseArmComputeTensorEmpty(m_PermuteFirstOut);
78 layer->configure(&input, &m_PermuteFirstOut, arm_compute::PermutationVector(0U,2U,1U));
79 m_Permute1.reset(layer.release());
85 for (
unsigned int i = 0; i < maxTime; ++i)
87 arm_compute::Tensor splitter_out;
88 arm_compute::Tensor concat_in;
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);
97 armcomputetensorutils::InitialiseArmComputeTensorEmpty(splitter_out);
98 armcomputetensorutils::InitialiseArmComputeTensorEmpty(concat_in);
101 m_SplitterOutputsTensors.push_back(std::move(splitter_out));
102 m_ConcatInputsTensors.push_back(std::move(concat_in));
105 for (
unsigned int i = 0; i < maxTime; ++i)
108 m_SplitterOutputs.push_back(&m_SplitterOutputsTensors[i]);
109 m_ConcatInputs.push_back(&m_ConcatInputsTensors[i]);
115 unsigned int numberDimensions = 3;
116 unsigned int dimension = 0;
120 ViewsDescriptor splitterDesc(maxTime, numberDimensions);
121 unsigned int splitterDimSizes[3] = {1, batchSize, inputSize};
122 for (
unsigned int outputIdx = 0u; outputIdx < maxTime; ++outputIdx)
124 splitterDesc.SetViewOriginCoord(outputIdx, dimension, splitterDimSizes[dimension] * outputIdx);
125 for (
unsigned int dimIdx = 0u; dimIdx < numberDimensions; ++dimIdx)
127 splitterDesc.SetViewSize(outputIdx, dimIdx, splitterDimSizes[dimIdx]);
131 std::set<unsigned int> splitAxis =
ComputeSplitAxis(splitterDesc, timeMajorShapeInput);
133 std::unique_ptr<arm_compute::NESplit> split_layer(
new arm_compute::NESplit());
134 unsigned int aclAxisSplit = CalcAclAxis(splitterDesc.GetNumDimensions(),
138 split_layer->configure(&m_PermuteFirstOut, m_SplitterOutputs, aclAxisSplit);
141 split_layer->configure(&input, m_SplitterOutputs, aclAxisSplit);
144 split_layer->prepare();
145 m_Splitter.reset(split_layer.release());
151 arm_compute::LSTMParams<arm_compute::ITensor> lstm_param;
153 lstm_param.set_cell_clip_params(descriptor.m_Parameters.m_ClippingThresCell);
154 lstm_param.set_projection_clip_params(descriptor.m_Parameters.m_ClippingThresProj);
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);
161 lstm_param.set_hidden_state_params(descriptor.m_Parameters.m_HiddenStateZeroPoint,
162 descriptor.m_Parameters.m_HiddenStateScale);
164 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
167 m_InputToCellWeightsTensor = std::make_unique<arm_compute::Tensor>();
170 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
173 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
176 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::Tensor>();
179 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
182 m_ForgetGateBiasTensor = std::make_unique<arm_compute::Tensor>();
185 m_CellBiasTensor = std::make_unique<arm_compute::Tensor>();
188 m_OutputGateBiasTensor = std::make_unique<arm_compute::Tensor>();
194 m_InputToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
197 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
200 m_CellToInputWeightsTensor = std::make_unique<arm_compute::Tensor>();
206 m_InputGateBiasTensor = std::make_unique<arm_compute::Tensor>();
208 lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
209 m_RecurrentToInputWeightsTensor.get(),
211 m_InputGateBiasTensor.get());
216 m_ProjectionWeightsTensor = std::make_unique<arm_compute::Tensor>();
219 m_ProjectionBiasTensor = std::make_unique<arm_compute::Tensor>();
225 lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
231 m_CellToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>();
234 m_CellToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>();
237 lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
242 m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
248 m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
251 m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
254 m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>();
258 lstm_param.set_layer_normalization_params(inputNormWeightTensor,
259 m_ForgetLayerNormWeightsTensor.get(),
260 m_CellLayerNormWeightsTensor.get(),
261 m_OutputLayerNormWeightsTensor.get());
264 for (
unsigned int i = 0; i != maxTime; ++i)
268 arm_compute::ITensor* outputLSTM;
269 arm_compute::ITensor* inputLSTM;
277 TensorShape inputShape =
GetTensorShape(input.info()->tensor_shape(), 1U);
278 TensorShape outputShape =
GetTensorShape(output.info()->tensor_shape(), 1U);
280 TensorShape inputShapeShrink({inputShape[1], inputShape[2]});
281 TensorShape outputShapeShrink({outputShape[1], outputShape[2]});
283 auto acl_input_shape_shrink = BuildArmComputeTensorShape(inputShapeShrink);
284 auto acl_output_shape_shrink = BuildArmComputeTensorShape(outputShapeShrink);
286 input.info()->set_tensor_shape(acl_input_shape_shrink);
287 inputLSTM =
const_cast<arm_compute::ITensor*
>(&input);
289 output.info()->set_tensor_shape(acl_output_shape_shrink);
290 outputLSTM = &output;
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;
304 outputLSTM =
const_cast<arm_compute::ITensor*
>(m_ConcatInputs[i]);
309 inputLSTM = m_SplitterOutputs[i];
310 outputLSTM =
const_cast<arm_compute::ITensor*
>(m_ConcatInputs[i]);
313 std::unique_ptr<arm_compute::NEQLSTMLayer> lstm_layer(
new arm_compute::NEQLSTMLayer());
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(),
332 m_Layers.emplace_back(std::move(lstm_layer));
384 for (uint32_t i = 0; i < m_Layers.size(); ++i)
386 m_Layers[i]->prepare();
395 TensorShape shapeExpandTimeMajor({1, shape[0], shape[1]});
396 TensorShape shapeExpandBatchMajor({shape[0], 1, shape[1]});
400 for (
unsigned int i = 0; i < maxTime; ++i)
402 m_ConcatInputs[i]->info()->set_tensor_shape(BuildArmComputeTensorShape(shapeExpandTimeMajor));
406 for (
unsigned int inputIdx = 0u; inputIdx < maxTime; ++inputIdx)
408 concatDescriptor.SetViewOriginCoord(inputIdx, dimension, inputIdx);
409 concatDescriptor.SetConcatAxis(dimension);
411 m_Concat.reset(
new arm_compute::NEConcatenateLayer());
413 unsigned int aclAxisConcat = CalcAclAxis(concatDescriptor.GetNumDimensions(), concatDescriptor.GetConcatAxis());
416 TensorInfo concatOutputTensorInfo = outputInfo;
417 concatOutputTensorInfo.SetShape(timeMajorShapeOutput);
418 BuildArmComputeTensor(concat_out, concatOutputTensorInfo);
419 armcomputetensorutils::InitialiseArmComputeTensorEmpty(concat_out);
421 m_Concat->configure(m_ConcatInputs, &concat_out, aclAxisConcat);
425 m_Concat->configure(m_ConcatInputs, &output, aclAxisConcat);
436 output.info()->set_tensor_shape(BuildArmComputeTensorShape(shapeExpandBatchMajor));
440 output.info()->set_tensor_shape(BuildArmComputeTensorShape(shapeExpandTimeMajor));
450 std::unique_ptr<arm_compute::NEPermute> layer(
new arm_compute::NEPermute());
453 layer->configure(&concat_out, &output, arm_compute::PermutationVector(0U, 2U, 1U));
457 layer->configure(m_ConcatInputs[0], &output, arm_compute::PermutationVector(0U, 2U, 1U));
459 m_Permute2.reset(layer.release());