24 #include <fmt/format.h>
32 using namespace armnn;
38 IDeserializer::IDeserializer() : pDeserializerImpl(new DeserializerImpl()){}
40 IDeserializer::~IDeserializer() =
default;
57 armnn::INetworkPtr IDeserializer::CreateNetworkFromBinary(
const std::vector<uint8_t> &binaryContent)
64 return pDeserializerImpl->CreateNetworkFromBinary(binaryContent);
67 BindingPointInfo IDeserializer::GetNetworkInputBindingInfo(
unsigned int layerId,
const std::string &name)
const
69 return pDeserializerImpl->GetNetworkInputBindingInfo(layerId, name);
72 BindingPointInfo IDeserializer::GetNetworkOutputBindingInfo(
unsigned int layerId,
const std::string &name)
const
74 return pDeserializerImpl->GetNetworkOutputBindingInfo(layerId, name);
80 const uint32_t VIRTUAL_LAYER_ID = std::numeric_limits<uint32_t>::max();
82 void CheckGraph(
const GraphPtr& graph,
83 unsigned int layersIndex,
86 if (graph->layers() ==
nullptr)
88 throw ParseException(fmt::format(
"{0} was called with invalid (null) graph. "
89 "Possible reason is that the graph is not yet loaded and Unpack(ed). "
95 else if (layersIndex >= graph->layers()->size())
97 throw ParseException(fmt::format(
"{0} was called with an invalid layers index. layers:{1} at {2}",
104 void CheckLayers(
const GraphPtr& graph,
105 unsigned int layersIndex,
106 unsigned int layerIndex,
109 if (graph->layers() ==
nullptr)
111 throw ParseException(fmt::format(
"{0} was called with invalid (null) graph. "
112 "Possible reason is that the graph is not yet loaded and Unpack(ed). "
118 else if (layersIndex >= graph->layers()->size())
120 throw ParseException(fmt::format(
"{0} was called with an invalid layers index. "
126 else if (layerIndex >= graph->layers()[layersIndex].size()
127 && layerIndex != VIRTUAL_LAYER_ID)
129 throw ParseException(fmt::format(
"{0} was called with an invalid layer index. "
130 "layers:{1} layer:{2} at {3}",
141 if (rawPtr ==
nullptr)
143 throw ParseException(fmt::format(
"{0} was called with a null tensor pointer. at {1}",
152 if (rawPtr ==
nullptr)
154 throw ParseException(fmt::format(
"{0} was called with a null const tensor pointer. at {1}",
160 void CheckConstTensorSize(
const unsigned int constTensorSize,
161 const unsigned int tensorSize,
164 if (constTensorSize != tensorSize)
166 throw ParseException(fmt::format(
"{0} wrong number of components supplied to tensor. at:{1}",
172 #define CHECK_TENSOR_PTR(TENSOR_PTR) \
173 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION())
175 #define CHECK_CONST_TENSOR_SIZE(CONST_TENSOR_SIZE, TENSOR_SIZE) \
176 CheckConstTensorSize(CONST_TENSOR_SIZE, TENSOR_SIZE, CHECK_LOCATION())
178 #define CHECK_CONST_TENSOR_PTR(TENSOR_PTR) \
179 CheckConstTensorPtr(TENSOR_PTR, CHECK_LOCATION())
181 #define CHECK_LAYERS(GRAPH, LAYERS_INDEX, LAYER_INDEX) \
182 CheckLayers(GRAPH, LAYERS_INDEX, LAYER_INDEX, CHECK_LOCATION())
184 #define CHECK_GRAPH(GRAPH, LAYERS_INDEX) \
185 CheckGraph(GRAPH, LAYERS_INDEX, CHECK_LOCATION())
191 if (actualSize != expected.size())
196 for (
unsigned int i = 0u; i < actualSize; i++)
198 if (actual[i] !=
static_cast<unsigned int>(expected[i]))
207 IDeserializer::DeserializerImpl::DeserializerImpl()
208 : m_Network(nullptr, nullptr),
213 m_ParserFunctions[Layer_AbsLayer] = &DeserializerImpl::ParseAbs;
214 m_ParserFunctions[Layer_ActivationLayer] = &DeserializerImpl::ParseActivation;
215 m_ParserFunctions[Layer_AdditionLayer] = &DeserializerImpl::ParseAdd;
216 m_ParserFunctions[Layer_ArgMinMaxLayer] = &DeserializerImpl::ParseArgMinMax;
217 m_ParserFunctions[Layer_BatchMatMulLayer] = &DeserializerImpl::ParseBatchMatMul;
218 m_ParserFunctions[Layer_BatchToSpaceNdLayer] = &DeserializerImpl::ParseBatchToSpaceNd;
219 m_ParserFunctions[Layer_BatchNormalizationLayer] = &DeserializerImpl::ParseBatchNormalization;
220 m_ParserFunctions[Layer_CastLayer] = &DeserializerImpl::ParseCast;
221 m_ParserFunctions[Layer_ChannelShuffleLayer] = &DeserializerImpl::ParseChannelShuffle;
222 m_ParserFunctions[Layer_ComparisonLayer] = &DeserializerImpl::ParseComparison;
223 m_ParserFunctions[Layer_ConcatLayer] = &DeserializerImpl::ParseConcat;
224 m_ParserFunctions[Layer_ConstantLayer] = &DeserializerImpl::ParseConstant;
225 m_ParserFunctions[Layer_Convolution2dLayer] = &DeserializerImpl::ParseConvolution2d;
226 m_ParserFunctions[Layer_Convolution3dLayer] = &DeserializerImpl::ParseConvolution3d;
227 m_ParserFunctions[Layer_DepthToSpaceLayer] = &DeserializerImpl::ParseDepthToSpace;
228 m_ParserFunctions[Layer_DepthwiseConvolution2dLayer] = &DeserializerImpl::ParseDepthwiseConvolution2d;
229 m_ParserFunctions[Layer_DequantizeLayer] = &DeserializerImpl::ParseDequantize;
230 m_ParserFunctions[Layer_DetectionPostProcessLayer] = &DeserializerImpl::ParseDetectionPostProcess;
231 m_ParserFunctions[Layer_DivisionLayer] = &DeserializerImpl::ParseDivision;
232 m_ParserFunctions[Layer_ElementwiseBinaryLayer] = &DeserializerImpl::ParseElementwiseBinary;
233 m_ParserFunctions[Layer_ElementwiseUnaryLayer] = &DeserializerImpl::ParseElementwiseUnary;
234 m_ParserFunctions[Layer_EqualLayer] = &DeserializerImpl::ParseEqual;
235 m_ParserFunctions[Layer_FullyConnectedLayer] = &DeserializerImpl::ParseFullyConnected;
236 m_ParserFunctions[Layer_FillLayer] = &DeserializerImpl::ParseFill;
237 m_ParserFunctions[Layer_FloorLayer] = &DeserializerImpl::ParseFloor;
238 m_ParserFunctions[Layer_GatherLayer] = &DeserializerImpl::ParseGather;
239 m_ParserFunctions[Layer_GatherNdLayer] = &DeserializerImpl::ParseGatherNd;
240 m_ParserFunctions[Layer_GreaterLayer] = &DeserializerImpl::ParseGreater;
241 m_ParserFunctions[Layer_InstanceNormalizationLayer] = &DeserializerImpl::ParseInstanceNormalization;
242 m_ParserFunctions[Layer_L2NormalizationLayer] = &DeserializerImpl::ParseL2Normalization;
243 m_ParserFunctions[Layer_LogicalBinaryLayer] = &DeserializerImpl::ParseLogicalBinary;
244 m_ParserFunctions[Layer_LogSoftmaxLayer] = &DeserializerImpl::ParseLogSoftmax;
245 m_ParserFunctions[Layer_LstmLayer] = &DeserializerImpl::ParseLstm;
246 m_ParserFunctions[Layer_MaximumLayer] = &DeserializerImpl::ParseMaximum;
247 m_ParserFunctions[Layer_MeanLayer] = &DeserializerImpl::ParseMean;
248 m_ParserFunctions[Layer_MinimumLayer] = &DeserializerImpl::ParseMinimum;
249 m_ParserFunctions[Layer_MergeLayer] = &DeserializerImpl::ParseMerge;
250 m_ParserFunctions[Layer_MergerLayer] = &DeserializerImpl::ParseConcat;
251 m_ParserFunctions[Layer_MultiplicationLayer] = &DeserializerImpl::ParseMultiplication;
252 m_ParserFunctions[Layer_NormalizationLayer] = &DeserializerImpl::ParseNormalization;
253 m_ParserFunctions[Layer_PadLayer] = &DeserializerImpl::ParsePad;
254 m_ParserFunctions[Layer_PermuteLayer] = &DeserializerImpl::ParsePermute;
255 m_ParserFunctions[Layer_Pooling2dLayer] = &DeserializerImpl::ParsePooling2d;
256 m_ParserFunctions[Layer_Pooling3dLayer] = &DeserializerImpl::ParsePooling3d;
257 m_ParserFunctions[Layer_PreluLayer] = &DeserializerImpl::ParsePrelu;
258 m_ParserFunctions[Layer_QLstmLayer] = &DeserializerImpl::ParseQLstm;
259 m_ParserFunctions[Layer_QuantizeLayer] = &DeserializerImpl::ParseQuantize;
260 m_ParserFunctions[Layer_QuantizedLstmLayer] = &DeserializerImpl::ParseQuantizedLstm;
261 m_ParserFunctions[Layer_RankLayer] = &DeserializerImpl::ParseRank;
262 m_ParserFunctions[Layer_ReduceLayer] = &DeserializerImpl::ParseReduce;
263 m_ParserFunctions[Layer_ReshapeLayer] = &DeserializerImpl::ParseReshape;
264 m_ParserFunctions[Layer_ResizeBilinearLayer] = &DeserializerImpl::ParseResizeBilinear;
265 m_ParserFunctions[Layer_ResizeLayer] = &DeserializerImpl::ParseResize;
266 m_ParserFunctions[Layer_ReverseV2Layer] = &DeserializerImpl::ParseReverseV2;
267 m_ParserFunctions[Layer_RsqrtLayer] = &DeserializerImpl::ParseRsqrt;
268 m_ParserFunctions[Layer_ShapeLayer] = &DeserializerImpl::ParseShape;
269 m_ParserFunctions[Layer_SliceLayer] = &DeserializerImpl::ParseSlice;
270 m_ParserFunctions[Layer_SoftmaxLayer] = &DeserializerImpl::ParseSoftmax;
271 m_ParserFunctions[Layer_SpaceToBatchNdLayer] = &DeserializerImpl::ParseSpaceToBatchNd;
272 m_ParserFunctions[Layer_SpaceToDepthLayer] = &DeserializerImpl::ParseSpaceToDepth;
273 m_ParserFunctions[Layer_SplitterLayer] = &DeserializerImpl::ParseSplitter;
274 m_ParserFunctions[Layer_StackLayer] = &DeserializerImpl::ParseStack;
275 m_ParserFunctions[Layer_StandInLayer] = &DeserializerImpl::ParseStandIn;
276 m_ParserFunctions[Layer_StridedSliceLayer] = &DeserializerImpl::ParseStridedSlice;
277 m_ParserFunctions[Layer_SubtractionLayer] = &DeserializerImpl::ParseSubtraction;
278 m_ParserFunctions[Layer_SwitchLayer] = &DeserializerImpl::ParseSwitch;
279 m_ParserFunctions[Layer_TileLayer] = &DeserializerImpl::ParseTile;
280 m_ParserFunctions[Layer_TransposeConvolution2dLayer] = &DeserializerImpl::ParseTransposeConvolution2d;
281 m_ParserFunctions[Layer_TransposeLayer] = &DeserializerImpl::ParseTranspose;
282 m_ParserFunctions[Layer_UnidirectionalSequenceLstmLayer] = &DeserializerImpl::ParseUnidirectionalSequenceLstm;
287 auto layerType = graphPtr->layers()->Get(layerIndex)->layer_type();
291 case Layer::Layer_AbsLayer:
292 return graphPtr->layers()->Get(layerIndex)->layer_as_AbsLayer()->base();
293 case Layer::Layer_ActivationLayer:
294 return graphPtr->layers()->Get(layerIndex)->layer_as_ActivationLayer()->base();
295 case Layer::Layer_AdditionLayer:
296 return graphPtr->layers()->Get(layerIndex)->layer_as_AdditionLayer()->base();
297 case Layer::Layer_ArgMinMaxLayer:
298 return graphPtr->layers()->Get(layerIndex)->layer_as_ArgMinMaxLayer()->base();
299 case Layer::Layer_BatchMatMulLayer:
300 return graphPtr->layers()->Get(layerIndex)->layer_as_BatchMatMulLayer()->base();
301 case Layer::Layer_BatchToSpaceNdLayer:
302 return graphPtr->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->base();
303 case Layer::Layer_BatchNormalizationLayer:
304 return graphPtr->layers()->Get(layerIndex)->layer_as_BatchNormalizationLayer()->base();
305 case Layer::Layer_CastLayer:
306 return graphPtr->layers()->Get(layerIndex)->layer_as_CastLayer()->base();
307 case Layer::Layer_ChannelShuffleLayer:
308 return graphPtr->layers()->Get(layerIndex)->layer_as_ChannelShuffleLayer()->base();
309 case Layer::Layer_ComparisonLayer:
310 return graphPtr->layers()->Get(layerIndex)->layer_as_ComparisonLayer()->base();
311 case Layer::Layer_ConcatLayer:
312 return graphPtr->layers()->Get(layerIndex)->layer_as_ConcatLayer()->base();
313 case Layer::Layer_ConstantLayer:
314 return graphPtr->layers()->Get(layerIndex)->layer_as_ConstantLayer()->base();
315 case Layer::Layer_Convolution2dLayer:
316 return graphPtr->layers()->Get(layerIndex)->layer_as_Convolution2dLayer()->base();
317 case Layer::Layer_Convolution3dLayer:
318 return graphPtr->layers()->Get(layerIndex)->layer_as_Convolution3dLayer()->base();
319 case Layer::Layer_DepthToSpaceLayer:
320 return graphPtr->layers()->Get(layerIndex)->layer_as_DepthToSpaceLayer()->base();
321 case Layer::Layer_DepthwiseConvolution2dLayer:
322 return graphPtr->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer()->base();
323 case Layer::Layer_DequantizeLayer:
324 return graphPtr->layers()->Get(layerIndex)->layer_as_DequantizeLayer()->base();
325 case Layer::Layer_DetectionPostProcessLayer:
326 return graphPtr->layers()->Get(layerIndex)->layer_as_DetectionPostProcessLayer()->base();
327 case Layer::Layer_DivisionLayer:
328 return graphPtr->layers()->Get(layerIndex)->layer_as_DivisionLayer()->base();
329 case Layer::Layer_EqualLayer:
330 return graphPtr->layers()->Get(layerIndex)->layer_as_EqualLayer()->base();
331 case Layer::Layer_ElementwiseBinaryLayer:
332 return graphPtr->layers()->Get(layerIndex)->layer_as_ElementwiseBinaryLayer()->base();
333 case Layer::Layer_ElementwiseUnaryLayer:
334 return graphPtr->layers()->Get(layerIndex)->layer_as_ElementwiseUnaryLayer()->base();
335 case Layer::Layer_FullyConnectedLayer:
336 return graphPtr->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer()->base();
337 case Layer::Layer_FillLayer:
338 return graphPtr->layers()->Get(layerIndex)->layer_as_FillLayer()->base();
339 case Layer::Layer_FloorLayer:
340 return graphPtr->layers()->Get(layerIndex)->layer_as_FloorLayer()->base();
341 case Layer::Layer_GatherLayer:
342 return graphPtr->layers()->Get(layerIndex)->layer_as_GatherLayer()->base();
343 case Layer::Layer_GatherNdLayer:
344 return graphPtr->layers()->Get(layerIndex)->layer_as_GatherNdLayer()->base();
345 case Layer::Layer_GreaterLayer:
346 return graphPtr->layers()->Get(layerIndex)->layer_as_GreaterLayer()->base();
347 case Layer::Layer_InputLayer:
348 return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->base();
349 case Layer::Layer_InstanceNormalizationLayer:
350 return graphPtr->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer()->base();
351 case Layer::Layer_L2NormalizationLayer:
352 return graphPtr->layers()->Get(layerIndex)->layer_as_L2NormalizationLayer()->base();
353 case Layer::Layer_LogicalBinaryLayer:
354 return graphPtr->layers()->Get(layerIndex)->layer_as_LogicalBinaryLayer()->base();
355 case Layer::Layer_LogSoftmaxLayer:
356 return graphPtr->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->base();
357 case Layer::Layer_LstmLayer:
358 return graphPtr->layers()->Get(layerIndex)->layer_as_LstmLayer()->base();
359 case Layer::Layer_MeanLayer:
360 return graphPtr->layers()->Get(layerIndex)->layer_as_MeanLayer()->base();
361 case Layer::Layer_MinimumLayer:
362 return graphPtr->layers()->Get(layerIndex)->layer_as_MinimumLayer()->base();
363 case Layer::Layer_MaximumLayer:
364 return graphPtr->layers()->Get(layerIndex)->layer_as_MaximumLayer()->base();
365 case Layer::Layer_MergeLayer:
366 return graphPtr->layers()->Get(layerIndex)->layer_as_MergeLayer()->base();
367 case Layer::Layer_MergerLayer:
368 return graphPtr->layers()->Get(layerIndex)->layer_as_MergerLayer()->base();
369 case Layer::Layer_MultiplicationLayer:
370 return graphPtr->layers()->Get(layerIndex)->layer_as_MultiplicationLayer()->base();
371 case Layer::Layer_NormalizationLayer:
372 return graphPtr->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->base();
373 case Layer::Layer_OutputLayer:
374 return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->base();
375 case Layer::Layer_PadLayer:
376 return graphPtr->layers()->Get(layerIndex)->layer_as_PadLayer()->base();
377 case Layer::Layer_PermuteLayer:
378 return graphPtr->layers()->Get(layerIndex)->layer_as_PermuteLayer()->base();
379 case Layer::Layer_Pooling2dLayer:
380 return graphPtr->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->base();
381 case Layer::Layer_Pooling3dLayer:
382 return graphPtr->layers()->Get(layerIndex)->layer_as_Pooling3dLayer()->base();
383 case Layer::Layer_PreluLayer:
384 return graphPtr->layers()->Get(layerIndex)->layer_as_PreluLayer()->base();
385 case Layer::Layer_QLstmLayer:
386 return graphPtr->layers()->Get(layerIndex)->layer_as_QLstmLayer()->base();
387 case Layer::Layer_QuantizeLayer:
388 return graphPtr->layers()->Get(layerIndex)->layer_as_QuantizeLayer()->base();
389 case Layer::Layer_QuantizedLstmLayer:
390 return graphPtr->layers()->Get(layerIndex)->layer_as_QuantizedLstmLayer()->base();
391 case Layer::Layer_RankLayer:
392 return graphPtr->layers()->Get(layerIndex)->layer_as_RankLayer()->base();
393 case Layer::Layer_ReduceLayer:
394 return graphPtr->layers()->Get(layerIndex)->layer_as_ReduceLayer()->base();
395 case Layer::Layer_ReshapeLayer:
396 return graphPtr->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->base();
397 case Layer::Layer_ResizeBilinearLayer:
398 return graphPtr->layers()->Get(layerIndex)->layer_as_ResizeBilinearLayer()->base();
399 case Layer::Layer_ResizeLayer:
400 return graphPtr->layers()->Get(layerIndex)->layer_as_ResizeLayer()->base();
401 case Layer::Layer_ReverseV2Layer:
402 return graphPtr->layers()->Get(layerIndex)->layer_as_ReverseV2Layer()->base();
403 case Layer::Layer_RsqrtLayer:
404 return graphPtr->layers()->Get(layerIndex)->layer_as_RsqrtLayer()->base();
405 case Layer::Layer_ShapeLayer:
406 return graphPtr->layers()->Get(layerIndex)->layer_as_ShapeLayer()->base();
407 case Layer::Layer_SliceLayer:
408 return graphPtr->layers()->Get(layerIndex)->layer_as_SliceLayer()->base();
409 case Layer::Layer_SoftmaxLayer:
410 return graphPtr->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->base();
411 case Layer::Layer_SpaceToBatchNdLayer:
412 return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->base();
413 case Layer::Layer_SpaceToDepthLayer:
414 return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->base();
415 case Layer::Layer_SplitterLayer:
416 return graphPtr->layers()->Get(layerIndex)->layer_as_SplitterLayer()->base();
417 case Layer::Layer_StackLayer:
418 return graphPtr->layers()->Get(layerIndex)->layer_as_StackLayer()->base();
419 case Layer::Layer_StandInLayer:
420 return graphPtr->layers()->Get(layerIndex)->layer_as_StandInLayer()->base();
421 case Layer::Layer_StridedSliceLayer:
422 return graphPtr->layers()->Get(layerIndex)->layer_as_StridedSliceLayer()->base();
423 case Layer::Layer_SubtractionLayer:
424 return graphPtr->layers()->Get(layerIndex)->layer_as_SubtractionLayer()->base();
425 case Layer::Layer_SwitchLayer:
426 return graphPtr->layers()->Get(layerIndex)->layer_as_SwitchLayer()->base();
427 case Layer::Layer_TileLayer:
428 return graphPtr->layers()->Get(layerIndex)->layer_as_TileLayer()->base();
429 case Layer::Layer_TransposeConvolution2dLayer:
430 return graphPtr->layers()->Get(layerIndex)->layer_as_TransposeConvolution2dLayer()->base();
431 case Layer::Layer_TransposeLayer:
432 return graphPtr->layers()->Get(layerIndex)->layer_as_TransposeLayer()->base();
433 case Layer::Layer_UnidirectionalSequenceLstmLayer:
434 return graphPtr->layers()->Get(layerIndex)->layer_as_UnidirectionalSequenceLstmLayer()->base();
435 case Layer::Layer_NONE:
437 throw ParseException(fmt::format(
"Layer type {} not recognized", layerType));
443 auto layer = GetBaseLayer(graph, index);
445 return layer->layerName()->str();
450 auto layerType = graphPtr->layers()->Get(layerIndex)->layer_type();
452 if (layerType == Layer::Layer_InputLayer)
454 return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->layerBindingId();
456 else if ( layerType == Layer::Layer_OutputLayer )
458 return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->layerBindingId();
467 case armnnSerializer::DataLayout::DataLayout_NHWC:
469 case armnnSerializer::DataLayout::DataLayout_NDHWC:
471 case armnnSerializer::DataLayout::DataLayout_NCDHW:
473 case armnnSerializer::DataLayout::DataLayout_NCHW:
483 case armnnSerializer::ActivationFunction_Sigmoid:
485 case armnnSerializer::ActivationFunction_TanH:
487 case armnnSerializer::ActivationFunction_Linear:
489 case armnnSerializer::ActivationFunction_ReLu:
491 case armnnSerializer::ActivationFunction_BoundedReLu:
493 case armnnSerializer::ActivationFunction_LeakyReLu:
495 case armnnSerializer::ActivationFunction_Abs:
497 case armnnSerializer::ActivationFunction_Sqrt:
499 case armnnSerializer::ActivationFunction_Square:
501 case armnnSerializer::ActivationFunction_Elu:
503 case armnnSerializer::ActivationFunction_HardSwish:
514 case armnnSerializer::ArgMinMaxFunction::ArgMinMaxFunction_Max:
516 case armnnSerializer::ArgMinMaxFunction::ArgMinMaxFunction_Min:
526 case armnnSerializer::ComparisonOperation::ComparisonOperation_Equal:
528 case armnnSerializer::ComparisonOperation::ComparisonOperation_Greater:
530 case armnnSerializer::ComparisonOperation::ComparisonOperation_GreaterOrEqual:
532 case armnnSerializer::ComparisonOperation::ComparisonOperation_Less:
534 case armnnSerializer::ComparisonOperation::ComparisonOperation_LessOrEqual:
536 case armnnSerializer::ComparisonOperation::ComparisonOperation_NotEqual:
546 case armnnSerializer::ReduceOperation::ReduceOperation_Sum:
548 case armnnSerializer::ReduceOperation::ReduceOperation_Max:
550 case armnnSerializer::ReduceOperation::ReduceOperation_Mean:
552 case armnnSerializer::ReduceOperation::ReduceOperation_Min:
554 case armnnSerializer::ReduceOperation::ReduceOperation_Prod:
565 case armnnSerializer::LogicalBinaryOperation::LogicalBinaryOperation_LogicalAnd:
567 case armnnSerializer::LogicalBinaryOperation::LogicalBinaryOperation_LogicalOr:
578 case armnnSerializer::BinaryOperation::BinaryOperation_Add:
580 case armnnSerializer::BinaryOperation::BinaryOperation_Div:
582 case armnnSerializer::BinaryOperation::BinaryOperation_Maximum:
584 case armnnSerializer::BinaryOperation::BinaryOperation_Minimum:
586 case armnnSerializer::BinaryOperation::BinaryOperation_Mul:
588 case armnnSerializer::BinaryOperation::BinaryOperation_Sub:
590 case armnnSerializer::BinaryOperation::BinaryOperation_SqDiff:
592 case armnnSerializer::BinaryOperation::BinaryOperation_Power:
603 case armnnSerializer::UnaryOperation::UnaryOperation_Abs:
605 case armnnSerializer::UnaryOperation::UnaryOperation_Ceil:
607 case armnnSerializer::UnaryOperation::UnaryOperation_Rsqrt:
609 case armnnSerializer::UnaryOperation::UnaryOperation_Sqrt:
611 case armnnSerializer::UnaryOperation::UnaryOperation_Exp:
613 case armnnSerializer::UnaryOperation::UnaryOperation_Neg:
615 case armnnSerializer::UnaryOperation::UnaryOperation_LogicalNot:
617 case armnnSerializer::UnaryOperation::UnaryOperation_Log:
619 case armnnSerializer::UnaryOperation::UnaryOperation_Sin:
630 case armnnSerializer::PaddingMode::PaddingMode_Reflect:
632 case armnnSerializer::PaddingMode::PaddingMode_Symmetric:
643 case armnnSerializer::ResizeMethod_NearestNeighbor:
645 case armnnSerializer::ResizeMethod_Bilinear:
657 switch (tensorPtr->dataType())
659 case DataType_QAsymmS8:
662 case DataType_QSymmS8:
665 case DataType_QuantisedAsymm8:
666 case DataType_QAsymmU8:
669 case DataType_QSymmS16:
670 case DataType_QuantisedSymm16:
673 case DataType_Signed32:
676 case DataType_Signed64:
679 case DataType_Float32:
682 case DataType_Float16:
685 case DataType_Boolean:
691 throw ParseException(fmt::format(
"Unsupported data type {0} = {1}. {2}",
692 tensorPtr->dataType(),
693 EnumNameDataType(tensorPtr->dataType()),
698 float quantizationScale = tensorPtr->quantizationScale();
699 int32_t quantizationOffset = tensorPtr->quantizationOffset();
701 if (tensorPtr->dimensionality() ==
static_cast<unsigned int>(Dimensionality::Scalar))
708 else if (tensorPtr->dimensionality() ==
static_cast<unsigned int>(Dimensionality::NotSpecified))
717 auto dimensions = tensorPtr->dimensions();
718 unsigned int size = dimensions->size();
719 std::vector<unsigned int> outputDims(dimensions->begin(), dimensions->begin() + size);
724 if (tensorPtr->dimensionSpecificity() !=
nullptr)
726 auto dimensionSpecificity = tensorPtr->dimensionSpecificity();
727 size = dimensionSpecificity->size();
728 for (
unsigned int i = 0; i < size; ++i)
730 dimensionsSpecificity[i] = dimensionSpecificity->Get(i);
734 TensorShape shape(size, outputDims.data(), dimensionsSpecificity);
736 auto quantizationScales = tensorPtr->quantizationScales();
737 if (quantizationScales)
739 unsigned int quantizationScalesSize = quantizationScales->size();
740 std::vector<float> scales(quantizationScales->begin(), quantizationScales->begin() + quantizationScalesSize);
741 unsigned int quantizationDim = tensorPtr->quantizationDim();
764 switch (constTensorPtr->data_type())
766 case ConstTensorData_ByteData:
768 auto byteData = constTensorPtr->data_as_ByteData()->data();
772 case ConstTensorData_ShortData:
774 auto shortData = constTensorPtr->data_as_ShortData()->data();
778 case ConstTensorData_IntData:
780 auto intData = constTensorPtr->data_as_IntData()->data();
784 case ConstTensorData_LongData:
786 auto longData = constTensorPtr->data_as_LongData()->data();
793 throw ParseException(fmt::format(
"Unsupported data type {0} = {1}. {2}",
794 constTensorPtr->data_type(),
795 EnumNameConstTensorData(constTensorPtr->data_type()),
804 auto layer = GetBaseLayer(graphPtr, layerIndex);
805 const auto& numInputs = layer->inputSlots()->size();
809 for (
unsigned int i=0; i<numInputs; ++i)
812 (layer->inputSlots()->Get(i)->connection()->sourceLayerIndex()));
813 result[i] = GetBaseLayer(graphPtr, inputId)->outputSlots()->Get(0)->tensorInfo();
821 auto layer = GetBaseLayer(graphPtr, layerIndex);
822 const auto& numOutputs = layer->outputSlots()->size();
826 for (
unsigned int i=0; i<numOutputs; ++i)
828 result[i] = layer->outputSlots()->Get(i)->tensorInfo();
833 void IDeserializer::DeserializerImpl::ParseUnsupportedLayer(
GraphPtr graph,
unsigned int layerIndex)
836 const auto layerName = GetBaseLayer(graph, layerIndex)->layerName()->c_str();
837 throw ParseException(fmt::format(
"Layer not supported. layerIndex: {0} "
838 "layerName: {1} / {2}",
844 void IDeserializer::DeserializerImpl::ResetParser()
847 m_InputBindings.clear();
848 m_OutputBindings.clear();
855 GraphPtr graph = LoadGraphFromBinary(binaryContent.data(), binaryContent.size());
856 return CreateNetworkFromGraph(graph);
862 if (binaryContent.fail()) {
866 binaryContent.seekg(0, std::ios::end);
867 const std::streamoff size = binaryContent.tellg();
868 std::vector<char> content(
static_cast<size_t>(size));
869 binaryContent.seekg(0);
870 binaryContent.read(content.data(),
static_cast<std::streamsize
>(size));
871 GraphPtr graph = LoadGraphFromBinary(
reinterpret_cast<uint8_t*
>(content.data()),
static_cast<size_t>(size));
872 return CreateNetworkFromGraph(graph);
877 if (binaryContent ==
nullptr)
882 flatbuffers::Verifier verifier(binaryContent, len);
883 if (verifier.VerifyBuffer<SerializedGraph>() ==
false)
885 throw ParseException(fmt::format(
"Buffer doesn't conform to the expected Armnn "
886 "flatbuffers format. size:{0} {1}",
890 return GetSerializedGraph(binaryContent);
895 m_Network = INetwork::Create();
897 unsigned int layerIndex = 0;
898 for (AnyLayer
const* layer : *graph->layers())
900 if (layer->layer_type() != Layer_InputLayer &&
901 layer->layer_type() != Layer_OutputLayer)
904 auto& parserFunction = m_ParserFunctions[layer->layer_type()];
905 (this->*parserFunction)(graph, layerIndex);
910 SetupInputLayers(graph);
911 SetupOutputLayers(graph);
914 for (
auto&& graphIt : m_GraphConnections)
916 Connections& connections = graphIt.second;
917 for (
auto&& outputIt : connections.outputSlots)
919 const unsigned int outputSlotIndex = outputIt.first;
921 if (connections.inputSlots.find(outputSlotIndex) != connections.inputSlots.end())
923 for (
IInputSlot* inputSlot : connections.inputSlots[outputSlotIndex])
925 outputSlot->
Connect(*inputSlot);
931 return std::move(m_Network);
935 const std::string& name)
const
938 for (
auto inputBinding : m_InputBindings)
940 if (inputBinding.first == name)
942 return inputBinding.second;
945 throw ParseException(fmt::format(
"No input binding found for layer:{0} / {1}",
951 const std::string& name)
const
954 for (
auto outputBinding : m_OutputBindings)
956 if (outputBinding.first == name)
958 return outputBinding.second;
961 throw ParseException(fmt::format(
"No output binding found for layer:{0} / {1}",
966 unsigned int IDeserializer::DeserializerImpl::GetInputLayerInVector(
GraphPtr graph,
int targetId)
968 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
970 auto layer = graph->layers()->Get(i);
971 if (layer->layer_type() == Layer::Layer_InputLayer)
973 auto layerBindingId = layer->layer_as_InputLayer()->base()->layerBindingId();
974 if (layerBindingId == targetId)
980 throw ParseException(
"Input layer with given layerBindingId not found");
983 unsigned int IDeserializer::DeserializerImpl::GetOutputLayerInVector(
GraphPtr graph,
int targetId)
985 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
987 auto layer = graph->layers()->Get(i);
988 if (layer->layer_type() == Layer::Layer_OutputLayer)
990 auto layerBindingId = layer->layer_as_OutputLayer()->base()->layerBindingId();
991 if (layerBindingId == targetId)
997 throw ParseException(
"Output layer with given layerBindingId not found");
1000 unsigned int IDeserializer::DeserializerImpl::GetLayerIndexInVector(
GraphPtr graph,
unsigned int targetIndex)
1002 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
1005 if (layer->index() == targetIndex)
1013 IDeserializer::DeserializerImpl::FeatureVersions IDeserializer::DeserializerImpl::GetFeatureVersions(
GraphPtr graph)
1015 IDeserializer::DeserializerImpl::FeatureVersions versions;
1017 if (graph->featureVersions())
1019 versions.m_BindingIdScheme = graph->featureVersions()->bindingIdsScheme();
1020 versions.m_WeightsLayoutScheme = graph->featureVersions()->weightsLayoutScheme();
1021 versions.m_ConstTensorsAsInputs = graph->featureVersions()->constantTensorsAsInputs();
1027 void IDeserializer::DeserializerImpl::SetupInputLayers(
GraphPtr graph)
1030 const unsigned int numInputs = graph->inputIds()->size();
1031 m_InputBindings.clear();
1032 m_InputBindings.reserve(numInputs);
1034 for (
unsigned int i = 0; i < numInputs; i++)
1036 unsigned int inputLayerIndex = 0xFFFFFFFF;
1037 if (GetFeatureVersions(graph).m_BindingIdScheme == 0)
1039 const unsigned int inputId = armnn::numeric_cast<unsigned int>(graph->inputIds()->Get(i));
1040 inputLayerIndex = GetLayerIndexInVector(graph, inputId);
1044 const int inputId = graph->inputIds()->Get(i);
1045 inputLayerIndex = GetInputLayerInVector(graph, inputId);
1051 LayerBindingId bindingId = GetBindingLayerInfo(graph, inputLayerIndex);
1055 m_Network->AddInputLayer(bindingId, baseLayer->layerName()->c_str());
1059 RegisterOutputSlots(graph, inputLayerIndex, inputLayer);
1062 m_InputBindings.push_back(std::make_pair(baseLayer->layerName()->c_str(), bindingInfo));
1066 void IDeserializer::DeserializerImpl::SetupOutputLayers(
GraphPtr graph)
1069 const unsigned int numOutputs = graph->outputIds()->size();
1070 m_OutputBindings.clear();
1071 m_OutputBindings.reserve(numOutputs);
1073 for (
unsigned int i = 0; i < numOutputs; i++)
1075 unsigned int outputLayerIndex = 0xFFFFFFFF;
1076 if (GetFeatureVersions(graph).m_BindingIdScheme == 0)
1078 const unsigned int outputId = armnn::numeric_cast<unsigned int>(graph->outputIds()->Get(i));
1079 outputLayerIndex = GetLayerIndexInVector(graph, outputId);
1083 const int outputId = graph->outputIds()->Get(i);
1084 outputLayerIndex = GetOutputLayerInVector(graph, outputId);
1090 LayerBindingId bindingId = GetBindingLayerInfo(graph, outputLayerIndex);
1094 m_Network->AddOutputLayer(bindingId, baseLayer->layerName()->c_str());
1096 RegisterInputSlots(graph, outputLayerIndex, outputLayer);
1097 unsigned int sourceLayerIndex =
1098 GetLayerIndexInVector(graph, baseLayer->inputSlots()->Get(0)->connection()->sourceLayerIndex());
1099 unsigned int outputSlotIndex =
1100 GetLayerIndexInVector(graph, baseLayer->inputSlots()->Get(0)->connection()->outputSlotIndex());
1101 LayerBaseRawPtr sourceBaseLayer = GetBaseLayer(graph, sourceLayerIndex);
1103 sourceBaseLayer->outputSlots()->Get(outputSlotIndex)->tensorInfo());
1105 m_OutputBindings.push_back(std::make_pair(baseLayer->layerName()->c_str(), bindingInfo));
1109 void IDeserializer::DeserializerImpl::RegisterOutputSlots(
GraphPtr graph,
1110 uint32_t layerIndex,
1118 throw ParseException(fmt::format(
"The number of outputslots ({0}) does not match the number expected ({1})"
1119 " for layer index: {2} {3}",
1120 baseLayer->outputSlots()->size(),
1128 const unsigned int slotIndex = baseLayer->outputSlots()->Get(i)->index();
1131 RegisterOutputSlotOfConnection(baseLayer->index(), slotIndex, outputSlot);
1135 void IDeserializer::DeserializerImpl::RegisterInputSlots(
GraphPtr graph,
1136 uint32_t layerIndex,
1138 std::vector<unsigned int> ignoreSlots)
1144 if (baseLayer->inputSlots()->size() != (layer->
GetNumInputSlots() - ignoreSlots.size()))
1146 throw ParseException(fmt::format(
"The number of inputslots ({0}) does not match the number expected ({1})"
1147 " for layer index:{2} {3}",
1148 baseLayer->inputSlots()->size(),
1157 if (std::find(ignoreSlots.begin(), ignoreSlots.end(), i) == ignoreSlots.end())
1159 auto fbInputSlot = baseLayer->inputSlots()->Get(i);
1160 auto fbConnection = fbInputSlot->connection();
1164 if (fbInputSlot->isOverridden())
1169 RegisterInputSlotOfConnection(fbConnection->sourceLayerIndex(), fbConnection->outputSlotIndex(), inputSlot);
1174 void IDeserializer::DeserializerImpl::RegisterInputSlotOfConnection(uint32_t sourceLayerIndex,
1175 uint32_t outputSlotIndex,
1178 if (m_GraphConnections.find(sourceLayerIndex) == m_GraphConnections.end())
1180 m_GraphConnections[sourceLayerIndex] = Connections();
1183 Connections& connections = m_GraphConnections[sourceLayerIndex];
1184 if (connections.inputSlots.find(outputSlotIndex) == connections.inputSlots.end())
1186 connections.inputSlots[outputSlotIndex] = {inputSlot};
1190 connections.inputSlots[outputSlotIndex].push_back(inputSlot);
1194 void IDeserializer::DeserializerImpl::RegisterOutputSlotOfConnection(uint32_t sourceLayerIndex,
1195 uint32_t outputSlotIndex,
1198 if (m_GraphConnections.find(sourceLayerIndex) == m_GraphConnections.end())
1200 m_GraphConnections[sourceLayerIndex] = Connections();
1203 Connections& connections = m_GraphConnections[sourceLayerIndex];
1204 if (connections.outputSlots.find(outputSlotIndex) != connections.outputSlots.end())
1209 connections.outputSlots[outputSlotIndex] = outputSlot;
1212 void IDeserializer::DeserializerImpl::ParseAbs(
GraphPtr graph,
unsigned int layerIndex)
1215 auto inputs = GetInputs(graph, layerIndex);
1219 auto outputs = GetOutputs(graph, layerIndex);
1222 auto layerName = GetLayerName(graph, layerIndex);
1225 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
1229 RegisterInputSlots(graph, layerIndex, layer);
1230 RegisterOutputSlots(graph, layerIndex, layer);
1233 void IDeserializer::DeserializerImpl::ParseActivation(
GraphPtr graph,
unsigned int layerIndex)
1236 auto inputs = GetInputs(graph, layerIndex);
1240 auto outputs = GetOutputs(graph, layerIndex);
1243 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ActivationLayer();
1244 auto layerName = GetLayerName(graph, layerIndex);
1245 auto serializerDescriptor = serializerLayer->descriptor();
1249 descriptor.
m_A = serializerDescriptor->a();
1250 descriptor.
m_B = serializerDescriptor->b();
1257 RegisterInputSlots(graph, layerIndex, layer);
1258 RegisterOutputSlots(graph, layerIndex, layer);
1261 void IDeserializer::DeserializerImpl::ParseAdd(
GraphPtr graph,
unsigned int layerIndex)
1264 auto inputs = GetInputs(graph, layerIndex);
1268 auto outputs = GetOutputs(graph, layerIndex);
1271 auto layerName = GetLayerName(graph, layerIndex);
1273 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(descriptor, layerName.c_str());
1278 RegisterInputSlots(graph, layerIndex, layer);
1279 RegisterOutputSlots(graph, layerIndex, layer);
1282 void IDeserializer::DeserializerImpl::ParseArgMinMax(
GraphPtr graph,
unsigned int layerIndex)
1285 auto inputs = GetInputs(graph, layerIndex);
1289 auto outputs = GetOutputs(graph, layerIndex);
1292 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ArgMinMaxLayer();
1293 auto serializerDescriptor = serializerLayer->descriptor();
1297 descriptor.
m_Axis = serializerDescriptor->axis();
1298 auto layerName = GetLayerName(graph, layerIndex);
1299 IConnectableLayer* layer = m_Network->AddArgMinMaxLayer(descriptor, layerName.c_str());
1304 RegisterInputSlots(graph, layerIndex, layer);
1305 RegisterOutputSlots(graph, layerIndex, layer);
1308 void IDeserializer::DeserializerImpl::ParseBatchMatMul(
GraphPtr graph,
unsigned int layerIndex)
1312 auto inputs = GetInputs(graph, layerIndex);
1316 auto outputs = GetOutputs(graph, layerIndex);
1319 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_BatchMatMulLayer();
1320 auto serializerDescriptor = serializerLayer->descriptor();
1323 serializerDescriptor->transposeY(),
1324 serializerDescriptor->adjointX(),
1325 serializerDescriptor->adjointY(),
1329 auto layerName = GetLayerName(graph, layerIndex);
1330 IConnectableLayer* layer = m_Network->AddBatchMatMulLayer(descriptor, layerName.c_str());
1335 RegisterInputSlots(graph, layerIndex, layer);
1336 RegisterOutputSlots(graph, layerIndex, layer);
1339 void IDeserializer::DeserializerImpl::ParseBatchToSpaceNd(
GraphPtr graph,
unsigned int layerIndex)
1349 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->descriptor();
1350 auto flatBufferCrops = flatBufferDescriptor->crops();
1351 auto flatBufferBlockShape = flatBufferDescriptor->blockShape();
1353 if (flatBufferCrops->size() % 2 != 0)
1358 std::vector<std::pair<unsigned int, unsigned int>> crops;
1359 crops.reserve(flatBufferCrops->size() / 2);
1360 for (
unsigned int i = 0; i < flatBufferCrops->size() - 1; i += 2)
1362 crops.emplace_back(flatBufferCrops->Get(i), flatBufferCrops->Get(i+1));
1368 std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end());
1371 auto layerName = GetLayerName(graph, layerIndex);
1372 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(descriptor, layerName.c_str());
1377 RegisterInputSlots(graph, layerIndex, layer);
1378 RegisterOutputSlots(graph, layerIndex, layer);
1381 void IDeserializer::DeserializerImpl::ParseBatchNormalization(
GraphPtr graph,
unsigned int layerIndex)
1385 auto inputs = GetInputs(graph, layerIndex);
1388 auto outputs = GetOutputs(graph, layerIndex);
1392 auto layerName = GetLayerName(graph, layerIndex);
1394 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_BatchNormalizationLayer();
1395 auto serializerDescriptor = serializerLayer->descriptor();
1398 descriptor.
m_Eps = serializerDescriptor->eps();
1414 RegisterInputSlots(graph, layerIndex, layer);
1415 RegisterOutputSlots(graph, layerIndex, layer);
1418 void IDeserializer::DeserializerImpl::ParseCast(
GraphPtr graph,
unsigned int layerIndex)
1428 auto layerName = GetLayerName(graph, layerIndex);
1435 RegisterInputSlots(graph, layerIndex, layer);
1436 RegisterOutputSlots(graph, layerIndex, layer);
1439 void IDeserializer::DeserializerImpl::ParseConstant(
GraphPtr graph,
unsigned int layerIndex)
1444 auto outputs = GetOutputs(graph, layerIndex);
1447 auto layerName = GetLayerName(graph, layerIndex);
1449 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ConstantLayer();
1450 auto serializerInput = serializerLayer->input();
1459 if (this->GetFeatureVersions(graph).m_WeightsLayoutScheme <= 0)
1465 std::unique_ptr<unsigned char[]> permuteBuffer(
new unsigned char[weightsInfo.
GetNumBytes()]);
1472 auto weightsShape = weightsInfo.
GetShape();
1476 weightsShape[2]*weightsShape[3]});
1481 layer = m_Network->AddConstantLayer(weightsPermuted, layerName.c_str());
1485 RegisterOutputSlots(graph, layerIndex, layer);
1491 layer = m_Network->AddConstantLayer(input, layerName.c_str());
1498 RegisterOutputSlots(graph, layerIndex, layer);
1501 void IDeserializer::DeserializerImpl::ParseConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
1504 auto inputs = GetInputs(graph, layerIndex);
1507 auto outputs = GetOutputs(graph, layerIndex);
1510 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_Convolution2dLayer();
1512 auto layerName = GetLayerName(graph, layerIndex);
1513 auto flatbufferDescriptor = flatBufferLayer->descriptor();
1516 descriptor.
m_PadLeft = flatbufferDescriptor->padLeft();
1517 descriptor.
m_PadRight = flatbufferDescriptor->padRight();
1518 descriptor.
m_PadTop = flatbufferDescriptor->padTop();
1519 descriptor.
m_PadBottom = flatbufferDescriptor->padBottom();
1520 descriptor.
m_StrideX = flatbufferDescriptor->strideX();
1521 descriptor.
m_StrideY = flatbufferDescriptor->strideY();;
1522 descriptor.
m_DilationX = flatbufferDescriptor->dilationX();
1523 descriptor.
m_DilationY = flatbufferDescriptor->dilationY();;
1524 descriptor.
m_BiasEnabled = flatbufferDescriptor->biasEnabled();;
1528 std::vector<unsigned int> ignoreSlots {};
1533 if (this->GetFeatureVersions(graph).m_ConstTensorsAsInputs <= 0)
1539 layer = m_Network->AddConvolution2dLayer(descriptor,
1543 auto weightsLayer = m_Network->AddConstantLayer(weightsTensor);
1544 weightsLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(1u));
1545 weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsTensor.
GetInfo());
1546 ignoreSlots.emplace_back(1u);
1551 auto biasLayer = m_Network->AddConstantLayer(biasTensor);
1552 biasLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(2u));
1553 biasLayer->GetOutputSlot(0).SetTensorInfo(biasTensor.
GetInfo());
1554 ignoreSlots.emplace_back(2u);
1559 layer = m_Network->AddConvolution2dLayer(descriptor,
1568 RegisterInputSlots(graph, layerIndex, layer, ignoreSlots);
1569 RegisterOutputSlots(graph, layerIndex, layer);
1572 void IDeserializer::DeserializerImpl::ParseConvolution3d(
GraphPtr graph,
unsigned int layerIndex)
1575 auto inputs = GetInputs(graph, layerIndex);
1578 auto outputs = GetOutputs(graph, layerIndex);
1581 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_Convolution3dLayer();
1582 auto layerName = GetLayerName(graph, layerIndex);
1583 auto serializerDescriptor = serializerLayer->descriptor();
1586 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
1587 descriptor.
m_PadRight = serializerDescriptor->padRight();
1588 descriptor.
m_PadTop = serializerDescriptor->padTop();
1589 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
1590 descriptor.
m_PadFront = serializerDescriptor->padFront();
1591 descriptor.
m_PadBack = serializerDescriptor->padBack();
1592 descriptor.
m_StrideX = serializerDescriptor->strideX();
1593 descriptor.
m_StrideY = serializerDescriptor->strideY();
1594 descriptor.
m_StrideZ = serializerDescriptor->strideZ();
1595 descriptor.
m_DilationX = serializerDescriptor->dilationX();
1596 descriptor.
m_DilationY = serializerDescriptor->dilationY();
1597 descriptor.
m_DilationZ = serializerDescriptor->dilationZ();
1598 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();
1604 IConnectableLayer* layer = m_Network->AddConvolution3dLayer(descriptor, layerName.c_str());
1609 RegisterInputSlots(graph, layerIndex, layer);
1610 RegisterOutputSlots(graph, layerIndex, layer);
1613 void IDeserializer::DeserializerImpl::ParseDepthToSpace(
GraphPtr graph,
unsigned int layerIndex)
1617 auto inputs = GetInputs(graph, layerIndex);
1620 auto outputs = GetOutputs(graph, layerIndex);
1623 auto fbDescriptor = graph->layers()->Get(layerIndex)->layer_as_DepthToSpaceLayer()->descriptor();
1626 descriptor.
m_BlockSize = fbDescriptor->blockSize();
1629 auto layerName = GetLayerName(graph, layerIndex);
1630 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
1635 RegisterInputSlots(graph, layerIndex, layer);
1636 RegisterOutputSlots(graph, layerIndex, layer);
1639 void IDeserializer::DeserializerImpl::ParseDepthwiseConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
1642 auto inputs = GetInputs(graph, layerIndex);
1645 auto outputs = GetOutputs(graph, layerIndex);
1648 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer();
1649 auto layerName = GetLayerName(graph, layerIndex);
1650 auto serializerDescriptor = serializerLayer->descriptor();
1653 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
1654 descriptor.
m_PadRight = serializerDescriptor->padRight();
1655 descriptor.
m_PadTop = serializerDescriptor->padTop();
1656 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
1657 descriptor.
m_StrideX = serializerDescriptor->strideX();
1658 descriptor.
m_StrideY = serializerDescriptor->strideY();
1659 descriptor.
m_DilationX = serializerDescriptor->dilationX();
1660 descriptor.
m_DilationY = serializerDescriptor->dilationY();
1661 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();
1665 std::vector<unsigned int> ignoreSlots {};
1669 if (this->GetFeatureVersions(graph).m_ConstTensorsAsInputs <= 0)
1676 ignoreSlots.emplace_back(1u);
1678 layer = m_Network->AddDepthwiseConvolution2dLayer(descriptor,
1685 ignoreSlots.emplace_back(2u);
1687 auto biasLayer = m_Network->AddConstantLayer(biases);
1688 biasLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(2u));
1689 biasLayer->GetOutputSlot(0).SetTensorInfo(biases.
GetInfo());
1692 if (this->GetFeatureVersions(graph).m_WeightsLayoutScheme <= 0)
1698 std::unique_ptr<unsigned char[]> permuteBuffer(
new unsigned char[weightsInfo.
GetNumBytes()]);
1705 auto weightsShape = weightsInfo.
GetShape();
1709 weightsShape[2]*weightsShape[3]});
1713 auto weightsLayer = m_Network->AddConstantLayer(weightsPermuted);
1714 weightsLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(1u));
1715 weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsPermuted.GetInfo());
1719 auto weightsLayer = m_Network->AddConstantLayer(weights);
1720 weightsLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(1u));
1721 weightsLayer->GetOutputSlot(0).SetTensorInfo(weights.
GetInfo());
1726 layer = m_Network->AddDepthwiseConvolution2dLayer(descriptor,
1735 RegisterInputSlots(graph, layerIndex, layer, ignoreSlots);
1736 RegisterOutputSlots(graph, layerIndex, layer);
1739 void IDeserializer::DeserializerImpl::ParseDetectionPostProcess(
GraphPtr graph,
unsigned int layerIndex)
1742 auto inputs = GetInputs(graph, layerIndex);
1746 auto outputs = GetOutputs(graph, layerIndex);
1749 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_DetectionPostProcessLayer();
1750 auto layerName = GetLayerName(graph, layerIndex);
1751 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1759 descriptor.
m_NumClasses = flatBufferDescriptor->numClasses();
1761 descriptor.
m_ScaleX = flatBufferDescriptor->scaleX();
1762 descriptor.
m_ScaleY = flatBufferDescriptor->scaleY();
1763 descriptor.
m_ScaleW = flatBufferDescriptor->scaleW();
1764 descriptor.
m_ScaleH = flatBufferDescriptor->scaleH();
1772 for (
unsigned int i = 0; i < 4; i++)
1777 RegisterInputSlots(graph, layerIndex, layer);
1778 RegisterOutputSlots(graph, layerIndex, layer);
1781 void IDeserializer::DeserializerImpl::ParseDivision(
GraphPtr graph,
unsigned int layerIndex)
1784 auto inputs = GetInputs(graph, layerIndex);
1788 auto outputs = GetOutputs(graph, layerIndex);
1791 auto layerName = GetLayerName(graph, layerIndex);
1793 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(descriptor, layerName.c_str());
1798 RegisterInputSlots(graph, layerIndex, layer);
1799 RegisterOutputSlots(graph, layerIndex, layer);
1802 void IDeserializer::DeserializerImpl::ParseEqual(
GraphPtr graph,
unsigned int layerIndex)
1805 auto inputs = GetInputs(graph, layerIndex);
1809 auto outputs = GetOutputs(graph, layerIndex);
1812 auto layerName = GetLayerName(graph, layerIndex);
1814 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
1819 RegisterInputSlots(graph, layerIndex, layer);
1820 RegisterOutputSlots(graph, layerIndex, layer);
1823 void IDeserializer::DeserializerImpl::ParseFill(
GraphPtr graph,
unsigned int layerIndex)
1826 auto inputs = GetInputs(graph, layerIndex);
1830 auto outputs = GetOutputs(graph, layerIndex);
1833 auto layerName = GetLayerName(graph, layerIndex);
1835 descriptor.
m_Value = graph->layers()->Get(layerIndex)->layer_as_FillLayer()->descriptor()->value();
1836 IConnectableLayer* layer = m_Network->AddFillLayer(descriptor, layerName.c_str());
1841 RegisterInputSlots(graph, layerIndex, layer);
1842 RegisterOutputSlots(graph, layerIndex, layer);
1845 void IDeserializer::DeserializerImpl::ParseGreater(
GraphPtr graph,
unsigned int layerIndex)
1848 auto inputs = GetInputs(graph, layerIndex);
1852 auto outputs = GetOutputs(graph, layerIndex);
1855 auto layerName = GetLayerName(graph, layerIndex);
1857 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
1862 RegisterInputSlots(graph, layerIndex, layer);
1863 RegisterOutputSlots(graph, layerIndex, layer);
1866 void IDeserializer::DeserializerImpl::ParseInstanceNormalization(
GraphPtr graph,
unsigned int layerIndex)
1870 auto inputs = GetInputs(graph, layerIndex);
1873 auto outputs = GetOutputs(graph, layerIndex);
1876 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer();
1877 auto fbDescriptor = fbLayer->descriptor();
1880 descriptor.
m_Gamma = fbDescriptor->gamma();
1881 descriptor.
m_Beta = fbDescriptor->beta();
1882 descriptor.
m_Eps = fbDescriptor->eps();
1885 const std::string layerName = GetLayerName(graph, layerIndex);
1888 IConnectableLayer* layer = m_Network->AddInstanceNormalizationLayer(descriptor, layerName.c_str());
1891 RegisterInputSlots(graph, layerIndex, layer);
1892 RegisterOutputSlots(graph, layerIndex, layer);
1895 void IDeserializer::DeserializerImpl::ParseL2Normalization(
GraphPtr graph,
unsigned int layerIndex)
1899 auto inputs = GetInputs(graph, layerIndex);
1902 auto outputs = GetOutputs(graph, layerIndex);
1906 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_L2NormalizationLayer();
1907 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1909 auto layerName = GetLayerName(graph, layerIndex);
1912 descriptor.
m_Eps = flatBufferDescriptor->eps();
1914 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(descriptor, layerName.c_str());
1917 RegisterInputSlots(graph, layerIndex, layer);
1918 RegisterOutputSlots(graph, layerIndex, layer);
1921 void IDeserializer::DeserializerImpl::ParseLogicalBinary(
GraphPtr graph,
unsigned int layerIndex)
1926 auto inputs = GetInputs(graph, layerIndex);
1929 auto outputs = GetOutputs(graph, layerIndex);
1932 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_LogicalBinaryLayer();
1933 auto fbDescriptor = fbLayer->descriptor();
1938 const std::string& layerName = GetLayerName(graph, layerIndex);
1939 IConnectableLayer* layer = m_Network->AddLogicalBinaryLayer(descriptor, layerName.c_str());
1944 RegisterInputSlots(graph, layerIndex, layer);
1945 RegisterOutputSlots(graph, layerIndex, layer);
1948 void IDeserializer::DeserializerImpl::ParseLogSoftmax(
GraphPtr graph,
unsigned int layerIndex)
1959 descriptor.
m_Beta = graph->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->descriptor()->beta();
1960 descriptor.
m_Axis = graph->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->descriptor()->axis();
1961 auto layerName = GetLayerName(graph, layerIndex);
1963 IConnectableLayer* layer = m_Network->AddLogSoftmaxLayer(descriptor, layerName.c_str());
1968 RegisterInputSlots(graph, layerIndex, layer);
1969 RegisterOutputSlots(graph, layerIndex, layer);
1972 void IDeserializer::DeserializerImpl::ParseMinimum(
GraphPtr graph,
unsigned int layerIndex)
1975 auto inputs = GetInputs(graph, layerIndex);
1979 auto outputs = GetOutputs(graph, layerIndex);
1982 auto layerName = GetLayerName(graph, layerIndex);
1984 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(descriptor, layerName.c_str());
1989 RegisterInputSlots(graph, layerIndex, layer);
1990 RegisterOutputSlots(graph, layerIndex, layer);
1993 void IDeserializer::DeserializerImpl::ParseMaximum(
GraphPtr graph,
unsigned int layerIndex)
1996 auto inputs = GetInputs(graph, layerIndex);
2000 auto outputs = GetOutputs(graph, layerIndex);
2003 auto layerName = GetLayerName(graph, layerIndex);
2005 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(descriptor, layerName.c_str());
2010 RegisterInputSlots(graph, layerIndex, layer);
2011 RegisterOutputSlots(graph, layerIndex, layer);
2015 unsigned int layerIndex)
2017 auto layerType = graph->layers()->Get(layerIndex)->layer_type();
2021 case Layer::Layer_ConcatLayer:
2022 return graph->layers()->Get(layerIndex)->layer_as_ConcatLayer()->descriptor();
2023 case Layer::Layer_MergerLayer:
2024 return graph->layers()->Get(layerIndex)->layer_as_MergerLayer()->descriptor();
2029 void IDeserializer::DeserializerImpl::ParseChannelShuffle(
GraphPtr graph,
unsigned int layerIndex)
2040 descriptor.
m_Axis = graph->layers()->Get(layerIndex)->layer_as_ChannelShuffleLayer()->descriptor()->axis();
2042 graph->layers()->Get(layerIndex)->layer_as_ChannelShuffleLayer()->descriptor()->numGroups();
2044 auto layerName = GetLayerName(graph, layerIndex);
2045 IConnectableLayer* layer = m_Network->AddChannelShuffleLayer(descriptor, layerName.c_str());
2050 RegisterInputSlots(graph, layerIndex, layer);
2051 RegisterOutputSlots(graph, layerIndex, layer);
2053 void IDeserializer::DeserializerImpl::ParseComparison(
GraphPtr graph,
unsigned int layerIndex)
2058 auto inputs = GetInputs(graph, layerIndex);
2061 auto outputs = GetOutputs(graph, layerIndex);
2064 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ComparisonLayer();
2065 auto fbDescriptor = fbLayer->descriptor();
2070 const std::string& layerName = GetLayerName(graph, layerIndex);
2071 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
2076 RegisterInputSlots(graph, layerIndex, layer);
2077 RegisterOutputSlots(graph, layerIndex, layer);
2080 void IDeserializer::DeserializerImpl::ParseElementwiseBinary(
GraphPtr graph,
unsigned int layerIndex)
2085 auto inputs = GetInputs(graph, layerIndex);
2088 auto outputs = GetOutputs(graph, layerIndex);
2091 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ElementwiseBinaryLayer();
2092 auto fbDescriptor = fbLayer->descriptor();
2097 const std::string& layerName = GetLayerName(graph, layerIndex);
2098 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(descriptor, layerName.c_str());
2103 RegisterInputSlots(graph, layerIndex, layer);
2104 RegisterOutputSlots(graph, layerIndex, layer);
2107 void IDeserializer::DeserializerImpl::ParseElementwiseUnary(
GraphPtr graph,
unsigned int layerIndex)
2112 auto inputs = GetInputs(graph, layerIndex);
2115 auto outputs = GetOutputs(graph, layerIndex);
2118 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ElementwiseUnaryLayer();
2119 auto fbDescriptor = fbLayer->descriptor();
2124 const std::string& layerName = GetLayerName(graph, layerIndex);
2125 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
2130 RegisterInputSlots(graph, layerIndex, layer);
2131 RegisterOutputSlots(graph, layerIndex, layer);
2134 void IDeserializer::DeserializerImpl::ParseConcat(
GraphPtr graph,
unsigned int layerIndex)
2139 auto outputs = GetOutputs(graph, layerIndex);
2142 auto layerName = GetLayerName(graph, layerIndex);
2144 unsigned int numViews = originsDescriptor->numViews();
2145 unsigned int numDimensions = originsDescriptor->numDimensions();
2148 auto inputs = GetInputs(graph, layerIndex);
2152 auto originsPtr = originsDescriptor->viewOrigins();
2153 for (
unsigned int v = 0; v < numViews; ++v)
2155 auto originPtr = originsPtr->Get(v);
2156 for (
unsigned int d = 0; d < numDimensions; ++d)
2158 uint32_t value = originPtr->data()->Get(d);
2159 descriptor.SetViewOriginCoord(v, d, value);
2162 descriptor.SetConcatAxis(originsDescriptor->concatAxis());
2164 IConnectableLayer* layer = m_Network->AddConcatLayer(descriptor, layerName.c_str());
2168 RegisterInputSlots(graph, layerIndex, layer);
2169 RegisterOutputSlots(graph, layerIndex, layer);
2172 void IDeserializer::DeserializerImpl::ParseMultiplication(
GraphPtr graph,
unsigned int layerIndex)
2175 auto inputs = GetInputs(graph, layerIndex);
2179 auto outputs = GetOutputs(graph, layerIndex);
2182 auto layerName = GetLayerName(graph, layerIndex);
2184 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(descriptor, layerName.c_str());
2189 RegisterInputSlots(graph, layerIndex, layer);
2190 RegisterOutputSlots(graph, layerIndex, layer);
2193 void IDeserializer::DeserializerImpl::ParseFloor(
GraphPtr graph,
unsigned int layerIndex)
2198 auto inputs = GetInputs(graph, layerIndex);
2201 auto outputs = GetOutputs(graph, layerIndex);
2204 auto layerName = GetLayerName(graph, layerIndex);
2208 layer = m_Network->AddFloorLayer(layerName.c_str());
2213 RegisterInputSlots(graph, layerIndex, layer);
2214 RegisterOutputSlots(graph, layerIndex, layer);
2217 void IDeserializer::DeserializerImpl::ParseFullyConnected(
GraphPtr graph,
unsigned int layerIndex)
2220 auto inputs = GetInputs(graph, layerIndex);
2223 auto outputs = GetOutputs(graph, layerIndex);
2226 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer();
2227 auto layerName = GetLayerName(graph, layerIndex);
2228 auto flatBufferDescriptor = flatBufferLayer->descriptor();
2231 fullyConnectedDescriptor.
m_BiasEnabled = flatBufferDescriptor->biasEnabled();
2233 fullyConnectedDescriptor.
m_ConstantWeights = flatBufferDescriptor->constantWeights();
2236 std::vector<unsigned int> ignoreSlots {};
2240 if (this->GetFeatureVersions(graph).m_ConstTensorsAsInputs <= 0)
2245 layer = m_Network->AddFullyConnectedLayer(fullyConnectedDescriptor,
2249 auto weightsLayer = m_Network->AddConstantLayer(weightsTensor);
2250 weightsLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(1u));
2251 weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsTensor.
GetInfo());
2252 ignoreSlots.emplace_back(1u);
2257 auto biasLayer = m_Network->AddConstantLayer(biasTensor);
2258 biasLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(2u));
2259 biasLayer->GetOutputSlot(0).SetTensorInfo(biasTensor.
GetInfo());
2260 ignoreSlots.emplace_back(2u);
2265 layer = m_Network->AddFullyConnectedLayer(fullyConnectedDescriptor,
2267 uint32_t numInputs = fullyConnectedDescriptor.
GetNumInputs();
2274 RegisterInputSlots(graph, layerIndex, layer, ignoreSlots);
2275 RegisterOutputSlots(graph, layerIndex, layer);
2278 void IDeserializer::DeserializerImpl::ParsePad(
GraphPtr graph,
unsigned int layerIndex)
2288 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_PadLayer()->descriptor();
2289 auto flatBufferPadList = flatBufferDescriptor->padList();
2290 auto paddingMode = flatBufferDescriptor->paddingMode();
2291 float padValue = flatBufferDescriptor->padValue();
2293 if (flatBufferPadList->size() % 2 != 0)
2295 throw ParseException(fmt::format(
"The size of the pad list must be divisible by 2 {}",
2299 std::vector<std::pair<unsigned int, unsigned int>> padList;
2300 padList.reserve(flatBufferPadList->size() / 2);
2301 for (
unsigned int i = 0; i < flatBufferPadList->size() - 1; i += 2)
2303 padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1));
2308 auto layerName = GetLayerName(graph, layerIndex);
2309 IConnectableLayer* layer = m_Network->AddPadLayer(descriptor, layerName.c_str());
2314 RegisterInputSlots(graph, layerIndex, layer);
2315 RegisterOutputSlots(graph, layerIndex, layer);
2318 void IDeserializer::DeserializerImpl::ParsePermute(
GraphPtr graph,
unsigned int layerIndex)
2323 graph->layers()->Get(layerIndex)->layer_as_PermuteLayer()->descriptor()->dimMappings();
2325 auto inputs = GetInputs(graph, layerIndex);
2328 auto outputs = GetOutputs(graph, layerIndex);
2332 auto layerName = GetLayerName(graph, layerIndex);
2335 IConnectableLayer* layer = m_Network->AddPermuteLayer(descriptor, layerName.c_str());
2338 RegisterInputSlots(graph, layerIndex, layer);
2339 RegisterOutputSlots(graph, layerIndex, layer);
2343 unsigned int layerIndex)
2348 switch (pooling2dDesc->poolType())
2350 case PoolingAlgorithm_Average:
2355 case PoolingAlgorithm_Max:
2360 case PoolingAlgorithm_L2:
2371 switch (pooling2dDesc->outputShapeRounding())
2373 case OutputShapeRounding_Floor:
2378 case OutputShapeRounding_Ceiling:
2389 switch (pooling2dDesc->paddingMethod())
2391 case PaddingMethod_Exclude:
2396 case PaddingMethod_IgnoreValue:
2407 switch (pooling2dDesc->dataLayout())
2409 case DataLayout_NCHW:
2414 case DataLayout_NHWC:
2426 desc.
m_PadLeft = pooling2dDesc->padLeft();
2428 desc.
m_PadTop = pooling2dDesc->padTop();
2429 desc.
m_StrideX = pooling2dDesc->strideX();
2430 desc.
m_StrideY = pooling2dDesc->strideY();
2438 unsigned int layerIndex)
2443 switch (pooling3dDesc->poolType())
2445 case PoolingAlgorithm_Average:
2450 case PoolingAlgorithm_Max:
2455 case PoolingAlgorithm_L2:
2466 switch (pooling3dDesc->outputShapeRounding())
2468 case OutputShapeRounding_Floor:
2473 case OutputShapeRounding_Ceiling:
2484 switch (pooling3dDesc->paddingMethod())
2486 case PaddingMethod_Exclude:
2491 case PaddingMethod_IgnoreValue:
2502 switch (pooling3dDesc->dataLayout())
2504 case DataLayout_NCDHW:
2509 case DataLayout_NDHWC:
2521 desc.
m_PadLeft = pooling3dDesc->padLeft();
2523 desc.
m_PadTop = pooling3dDesc->padTop();
2525 desc.
m_PadBack = pooling3dDesc->padBack();
2526 desc.
m_StrideX = pooling3dDesc->strideX();
2527 desc.
m_StrideY = pooling3dDesc->strideY();
2528 desc.
m_StrideZ = pooling3dDesc->strideZ();
2536 void IDeserializer::DeserializerImpl::ParsePooling2d(
GraphPtr graph,
unsigned int layerIndex)
2540 auto pooling2dDes = graph->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->descriptor();
2541 auto inputs = GetInputs(graph, layerIndex);
2544 auto outputs = GetOutputs(graph, layerIndex);
2548 auto pooling2dDescriptor = GetPooling2dDescriptor(pooling2dDes, layerIndex);
2549 auto layerName = GetLayerName(graph, layerIndex);
2550 IConnectableLayer* layer = m_Network->AddPooling2dLayer(pooling2dDescriptor, layerName.c_str());
2553 RegisterInputSlots(graph, layerIndex, layer);
2554 RegisterOutputSlots(graph, layerIndex, layer);
2557 void IDeserializer::DeserializerImpl::ParsePooling3d(
GraphPtr graph,
unsigned int layerIndex)
2561 auto pooling3dDes = graph->layers()->Get(layerIndex)->layer_as_Pooling3dLayer()->descriptor();
2562 auto inputs = GetInputs(graph, layerIndex);
2565 auto outputs = GetOutputs(graph, layerIndex);
2569 auto pooling3dDescriptor = GetPooling3dDescriptor(pooling3dDes, layerIndex);
2570 auto layerName = GetLayerName(graph, layerIndex);
2571 IConnectableLayer* layer = m_Network->AddPooling3dLayer(pooling3dDescriptor, layerName.c_str());
2574 RegisterInputSlots(graph, layerIndex, layer);
2575 RegisterOutputSlots(graph, layerIndex, layer);
2578 void IDeserializer::DeserializerImpl::ParseQuantize(
GraphPtr graph,
unsigned int layerIndex)
2582 auto inputs = GetInputs(graph, layerIndex);
2585 auto outputs = GetOutputs(graph, layerIndex);
2589 auto layerName = GetLayerName(graph, layerIndex);
2593 RegisterInputSlots(graph, layerIndex, layer);
2594 RegisterOutputSlots(graph, layerIndex, layer);
2598 const std::vector<uint32_t>& targetDimsIn)
2600 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2601 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2603 if (stretchDim != targetDimsIn.end())
2605 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2607 throw ParseException(fmt::format(
"At most one component of shape can be -1 {}",
2611 auto targetNumElements =
2612 armnn::numeric_cast<unsigned int>(
2613 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2615 auto stretchIndex =
static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2616 outputDims[stretchIndex] = inputTensorInfo.
GetNumElements() / targetNumElements;
2627 void IDeserializer::DeserializerImpl::ParseRank(
GraphPtr graph,
unsigned int layerIndex)
2637 auto layerName = GetLayerName(graph, layerIndex);
2643 RegisterInputSlots(graph, layerIndex, layer);
2644 RegisterOutputSlots(graph, layerIndex, layer);
2647 void IDeserializer::DeserializerImpl::ParseReduce(
GraphPtr graph,
unsigned int layerIndex)
2652 auto inputs = GetInputs(graph, layerIndex);
2655 auto outputs = GetOutputs(graph, layerIndex);
2658 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ReduceLayer();
2659 auto fbDescriptor = fbLayer->descriptor();
2660 auto flatBufferAxis = fbDescriptor->axis();
2663 descriptor.
m_KeepDims = fbDescriptor->keepDims();
2664 descriptor.
m_vAxis = std::vector<unsigned int>(flatBufferAxis->begin(), flatBufferAxis->end());
2667 const std::string& layerName = GetLayerName(graph, layerIndex);
2668 IConnectableLayer* layer = m_Network->AddReduceLayer(descriptor, layerName.c_str());
2673 RegisterInputSlots(graph, layerIndex, layer);
2674 RegisterOutputSlots(graph, layerIndex, layer);
2677 void IDeserializer::DeserializerImpl::ParseReshape(
GraphPtr graph,
unsigned int layerIndex)
2680 auto inputs = GetInputs(graph, layerIndex);
2682 auto outputs = GetOutputs(graph, layerIndex);
2688 const auto targetDims = graph->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->descriptor()->targetShape();
2689 std::vector<uint32_t> outputDims(targetDims->begin(), targetDims->begin() + targetDims->size());
2694 const std::vector<uint32_t> expectedDims(outputs[0]->dimensions()->begin(),
2695 outputs[0]->dimensions()->begin() + outputs[0]->dimensions()->size());
2697 if (inputs.size() > 1 && !
CheckShape(reshapeOutputTensorShape, expectedDims))
2699 std::stringstream ss;
2700 ss <<
"New shape defined in reshape parameters "
2701 << reshapeOutputTensorShape
2702 <<
" does not equal output shape "
2703 << actualOutputTensorInfo.
GetShape()
2712 auto layerName = GetLayerName(graph, layerIndex);
2713 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2716 RegisterInputSlots(graph, layerIndex, layer);
2717 RegisterOutputSlots(graph, layerIndex, layer);
2720 void IDeserializer::DeserializerImpl::ParseResize(
GraphPtr graph,
unsigned int layerIndex)
2730 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_ResizeLayer()->descriptor();
2733 descriptor.
m_TargetWidth = flatBufferDescriptor->targetWidth();
2734 descriptor.
m_TargetHeight = flatBufferDescriptor->targetHeight();
2737 descriptor.
m_AlignCorners = flatBufferDescriptor->alignCorners();
2740 auto layerName = GetLayerName(graph, layerIndex);
2741 IConnectableLayer* layer = m_Network->AddResizeLayer(descriptor, layerName.c_str());
2746 RegisterInputSlots(graph, layerIndex, layer);
2747 RegisterOutputSlots(graph, layerIndex, layer);
2750 void IDeserializer::DeserializerImpl::ParseReverseV2(
GraphPtr graph,
unsigned int layerIndex)
2760 auto layerName = GetLayerName(graph, layerIndex);
2766 RegisterInputSlots(graph, layerIndex, layer);
2767 RegisterOutputSlots(graph, layerIndex, layer);
2772 void IDeserializer::DeserializerImpl::ParseResizeBilinear(
GraphPtr graph,
unsigned int layerIndex)
2782 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_ResizeBilinearLayer()->descriptor();
2785 descriptor.
m_TargetWidth = flatBufferDescriptor->targetWidth();
2786 descriptor.
m_TargetHeight = flatBufferDescriptor->targetHeight();
2789 descriptor.
m_AlignCorners = flatBufferDescriptor->alignCorners();
2792 auto layerName = GetLayerName(graph, layerIndex);
2793 IConnectableLayer* layer = m_Network->AddResizeLayer(descriptor, layerName.c_str());
2798 RegisterInputSlots(graph, layerIndex, layer);
2799 RegisterOutputSlots(graph, layerIndex, layer);
2802 void IDeserializer::DeserializerImpl::ParseShape(
GraphPtr graph,
unsigned int layerIndex)
2812 auto layerName = GetLayerName(graph, layerIndex);
2818 RegisterInputSlots(graph, layerIndex, layer);
2819 RegisterOutputSlots(graph, layerIndex, layer);
2822 void IDeserializer::DeserializerImpl::ParseSoftmax(
GraphPtr graph,
unsigned int layerIndex)
2833 descriptor.
m_Beta = graph->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->descriptor()->beta();
2834 descriptor.
m_Axis = graph->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->descriptor()->axis();
2835 auto layerName = GetLayerName(graph, layerIndex);
2837 IConnectableLayer* layer = m_Network->AddSoftmaxLayer(descriptor, layerName.c_str());
2842 RegisterInputSlots(graph, layerIndex, layer);
2843 RegisterOutputSlots(graph, layerIndex, layer);
2846 void IDeserializer::DeserializerImpl::ParseSpaceToBatchNd(
GraphPtr graph,
unsigned int layerIndex)
2856 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->descriptor();
2857 auto flatBufferPadList = flatBufferDescriptor->padList();
2858 auto flatBufferBlockShape = flatBufferDescriptor->blockShape();
2860 if (flatBufferPadList->size() % 2 != 0)
2862 throw ParseException(fmt::format(
"The size of the pad list must be divisible by 2 {}",
2866 std::vector<std::pair<unsigned int, unsigned int>> padList;
2867 padList.reserve(flatBufferPadList->size() / 2);
2868 for (
unsigned int i = 0; i < flatBufferPadList->size() - 1; i += 2)
2870 padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1));
2876 std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end());
2879 auto layerName = GetLayerName(graph, layerIndex);
2880 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(descriptor, layerName.c_str());
2885 RegisterInputSlots(graph, layerIndex, layer);
2886 RegisterOutputSlots(graph, layerIndex, layer);
2889 void IDeserializer::DeserializerImpl::ParseSpaceToDepth(
GraphPtr graph,
unsigned int layerIndex)
2899 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->descriptor();
2902 descriptor.
m_BlockSize = flatBufferDescriptor->blockSize();
2905 auto layerName = GetLayerName(graph, layerIndex);
2906 IConnectableLayer* layer = m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str());
2911 RegisterInputSlots(graph, layerIndex, layer);
2912 RegisterOutputSlots(graph, layerIndex, layer);
2917 unsigned int layerIndex)
2922 switch (normalizationDescriptor->normChannelType())
2924 case NormalizationAlgorithmChannel_Across:
2929 case NormalizationAlgorithmChannel_Within:
2940 switch (normalizationDescriptor->normMethodType())
2942 case NormalizationAlgorithmMethod_LocalBrightness:
2947 case NormalizationAlgorithmMethod_LocalContrast:
2958 switch (normalizationDescriptor->dataLayout())
2960 case DataLayout_NCHW:
2965 case DataLayout_NHWC:
2976 desc.
m_Alpha = normalizationDescriptor->alpha();
2977 desc.
m_Beta = normalizationDescriptor->beta();
2978 desc.
m_K = normalizationDescriptor->k();
2979 desc.
m_NormSize = normalizationDescriptor->normSize();
2984 void IDeserializer::DeserializerImpl::ParseNormalization(
GraphPtr graph,
unsigned int layerIndex)
2988 auto normalizationDes = graph->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->descriptor();
2998 auto normalizationDescriptor = GetNormalizationDescriptor(normalizationDes, layerIndex);
2999 auto layerName = GetLayerName(graph, layerIndex);
3001 IConnectableLayer* layer = m_Network->AddNormalizationLayer(normalizationDescriptor, layerName.c_str());
3004 RegisterInputSlots(graph, layerIndex, layer);
3005 RegisterOutputSlots(graph, layerIndex, layer);
3008 void IDeserializer::DeserializerImpl::ParseRsqrt(
GraphPtr graph,
unsigned int layerIndex)
3011 auto inputs = GetInputs(graph, layerIndex);
3015 auto outputs = GetOutputs(graph, layerIndex);
3018 auto layerName = GetLayerName(graph, layerIndex);
3021 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
3025 RegisterInputSlots(graph, layerIndex, layer);
3026 RegisterOutputSlots(graph, layerIndex, layer);
3029 void IDeserializer::DeserializerImpl::ParseSlice(
GraphPtr graph,
unsigned int layerIndex)
3033 auto inputs = GetInputs(graph, layerIndex);
3036 auto outputs = GetOutputs(graph, layerIndex);
3039 auto fbDescriptor = graph->layers()->Get(layerIndex)->layer_as_SliceLayer()->descriptor();
3041 auto fbBegin = fbDescriptor->begin();
3042 auto fbSize = fbDescriptor->size();
3044 if (fbBegin->size() != fbSize->size())
3046 throw ParseException(fmt::format(
"Begin and size descriptors must have the same length {}",
3051 descriptor.
m_Begin.insert(descriptor.
m_Begin.end(), fbBegin->begin(), fbBegin->end());
3052 descriptor.
m_Size.insert(descriptor.
m_Size.end(), fbSize->begin(), fbSize->end());
3054 auto layerName = GetLayerName(graph, layerIndex);
3055 IConnectableLayer* layer = m_Network->AddSliceLayer(descriptor, layerName.c_str());
3060 RegisterInputSlots(graph, layerIndex, layer);
3061 RegisterOutputSlots(graph, layerIndex, layer);
3064 void IDeserializer::DeserializerImpl::ParseStridedSlice(
GraphPtr graph,
unsigned int layerIndex)
3074 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_StridedSliceLayer()->descriptor();
3076 auto flatBufferBegin = flatBufferDescriptor->begin();
3077 auto flatBufferEnd = flatBufferDescriptor->end();
3078 auto flatBufferStride = flatBufferDescriptor->stride();
3080 if (!(flatBufferBegin->size() == flatBufferEnd->size() &&
3081 flatBufferBegin->size() == flatBufferStride->size()))
3083 throw ParseException(fmt::format(
"The size of the begin, end, and stride must be equal {}",
3087 std::vector<int> begin(flatBufferBegin->begin(), flatBufferBegin->end());
3088 std::vector<int> end(flatBufferEnd->begin(), flatBufferEnd->end());
3089 std::vector<int> stride(flatBufferStride->begin(), flatBufferStride->end());
3092 descriptor.m_BeginMask = flatBufferDescriptor->beginMask();
3093 descriptor.m_EndMask = flatBufferDescriptor->endMask();
3094 descriptor.m_ShrinkAxisMask = flatBufferDescriptor->shrinkAxisMask();
3095 descriptor.m_EllipsisMask = flatBufferDescriptor->ellipsisMask();
3096 descriptor.m_NewAxisMask = flatBufferDescriptor->newAxisMask();
3097 descriptor.m_DataLayout =
ToDataLayout(flatBufferDescriptor->dataLayout());
3099 auto layerName = GetLayerName(graph, layerIndex);
3100 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(descriptor, layerName.c_str());
3105 RegisterInputSlots(graph, layerIndex, layer);
3106 RegisterOutputSlots(graph, layerIndex, layer);
3109 void IDeserializer::DeserializerImpl::ParseSubtraction(
GraphPtr graph,
unsigned int layerIndex)
3112 auto inputs = GetInputs(graph, layerIndex);
3116 auto outputs = GetOutputs(graph, layerIndex);
3119 auto layerName = GetLayerName(graph, layerIndex);
3121 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(descriptor, layerName.c_str());
3126 RegisterInputSlots(graph, layerIndex, layer);
3127 RegisterOutputSlots(graph, layerIndex, layer);
3130 void IDeserializer::DeserializerImpl::ParseGather(
GraphPtr graph,
unsigned int layerIndex)
3141 descriptor.
m_Axis = graph->layers()->Get(layerIndex)->layer_as_GatherLayer()->descriptor()->axis();
3143 auto layerName = GetLayerName(graph, layerIndex);
3144 IConnectableLayer* layer = m_Network->AddGatherLayer(descriptor, layerName.c_str());
3149 RegisterInputSlots(graph, layerIndex, layer);
3150 RegisterOutputSlots(graph, layerIndex, layer);
3153 void IDeserializer::DeserializerImpl::ParseGatherNd(
GraphPtr graph,
unsigned int layerIndex)
3163 auto layerName = GetLayerName(graph, layerIndex);
3169 RegisterInputSlots(graph, layerIndex, layer);
3170 RegisterOutputSlots(graph, layerIndex, layer);
3173 void IDeserializer::DeserializerImpl::ParseMean(
GraphPtr graph,
unsigned int layerIndex)
3183 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_MeanLayer()->descriptor();
3184 auto flatBufferAxis = flatBufferDescriptor->axis();
3185 auto flatBufferKeepDims = flatBufferDescriptor->keepDims();
3188 descriptor.
m_Axis = std::vector<unsigned int>(flatBufferAxis->begin(), flatBufferAxis->end());
3191 auto layerName = GetLayerName(graph, layerIndex);
3192 IConnectableLayer* layer = m_Network->AddMeanLayer(descriptor, layerName.c_str());
3197 RegisterInputSlots(graph, layerIndex, layer);
3198 RegisterOutputSlots(graph, layerIndex, layer);
3201 void IDeserializer::DeserializerImpl::ParseSplitter(
GraphPtr graph,
unsigned int layerIndex)
3210 auto flatBufferViewsDescriptor = graph->layers()->Get(layerIndex)->layer_as_SplitterLayer()->descriptor();
3211 auto flatBufferViewSizes = flatBufferViewsDescriptor->viewSizes();
3212 auto flatBufferOriginsDescriptor = flatBufferViewsDescriptor->origins();
3213 auto flatBufferViewOrigins = flatBufferOriginsDescriptor->viewOrigins();
3214 uint32_t numViews = flatBufferOriginsDescriptor->numViews();
3215 uint32_t numDimensions = flatBufferOriginsDescriptor->numDimensions();
3222 for(
unsigned int vIdx = 0; vIdx < numViews; ++vIdx)
3224 for (
unsigned int dIdx = 0; dIdx < numDimensions; ++dIdx)
3226 viewsDescriptor.SetViewSize(vIdx, dIdx, flatBufferViewSizes->Get(vIdx)->data()->Get(dIdx));
3227 viewsDescriptor.SetViewOriginCoord(vIdx, dIdx, flatBufferViewOrigins->Get(vIdx)->data()->Get(dIdx));
3231 auto layerName = GetLayerName(graph, layerIndex);
3232 IConnectableLayer* layer = m_Network->AddSplitterLayer(viewsDescriptor, layerName.c_str());
3235 for(
unsigned int vIdx = 0; vIdx < numViews; ++vIdx)
3241 RegisterInputSlots(graph, layerIndex, layer);
3242 RegisterOutputSlots(graph, layerIndex, layer);
3260 void IDeserializer::DeserializerImpl::ParseLstm(
GraphPtr graph,
unsigned int layerIndex)
3264 auto inputs = GetInputs(graph, layerIndex);
3267 auto outputs = GetOutputs(graph, layerIndex);
3270 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_LstmLayer();
3271 auto layerName = GetLayerName(graph, layerIndex);
3272 auto flatBufferDescriptor = flatBufferLayer->descriptor();
3273 auto flatBufferInputParams = flatBufferLayer->inputParams();
3275 auto lstmDescriptor = GetLstmDescriptor(flatBufferDescriptor);
3303 if (!lstmDescriptor.m_CifgEnabled)
3305 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
3306 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
3307 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
3308 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
3318 if (lstmDescriptor.m_ProjectionEnabled)
3320 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
3321 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
3329 if (lstmDescriptor.m_PeepholeEnabled)
3331 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
3332 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
3342 if (lstmDescriptor.m_LayerNormEnabled)
3344 if (!lstmDescriptor.m_CifgEnabled)
3346 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
3349 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
3350 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
3351 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
3358 IConnectableLayer* layer = m_Network->AddLstmLayer(lstmDescriptor, lstmInputParams, layerName.c_str());
3372 RegisterInputSlots(graph, layerIndex, layer);
3373 RegisterOutputSlots(graph, layerIndex, layer);
3385 desc.
m_CellClip = qLstmDescriptor->cellClip();
3399 void IDeserializer::DeserializerImpl::ParseQLstm(
GraphPtr graph,
unsigned int layerIndex)
3403 auto inputs = GetInputs(graph, layerIndex);
3406 auto outputs = GetOutputs(graph, layerIndex);
3409 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_QLstmLayer();
3410 auto layerName = GetLayerName(graph, layerIndex);
3411 auto flatBufferDescriptor = flatBufferLayer->descriptor();
3412 auto flatBufferInputParams = flatBufferLayer->inputParams();
3414 auto qLstmDescriptor = GetQLstmDescriptor(flatBufferDescriptor);
3443 if (!qLstmDescriptor.m_CifgEnabled)
3445 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
3446 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
3447 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
3458 if (qLstmDescriptor.m_ProjectionEnabled)
3460 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
3461 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
3472 if (qLstmDescriptor.m_PeepholeEnabled)
3474 if (!qLstmDescriptor.m_CifgEnabled)
3476 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
3480 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
3481 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
3493 if (qLstmDescriptor.m_LayerNormEnabled)
3495 if (!qLstmDescriptor.m_CifgEnabled)
3497 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
3501 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
3502 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
3503 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
3510 IConnectableLayer* layer = m_Network->AddQLstmLayer(qLstmDescriptor, qLstmInputParams, layerName.c_str());
3521 RegisterInputSlots(graph, layerIndex, layer);
3522 RegisterOutputSlots(graph, layerIndex, layer);
3525 void IDeserializer::DeserializerImpl::ParseQuantizedLstm(
GraphPtr graph,
unsigned int layerIndex)
3529 auto inputs = GetInputs(graph, layerIndex);
3532 auto outputs = GetOutputs(graph, layerIndex);
3535 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_QuantizedLstmLayer();
3536 auto layerName = GetLayerName(graph, layerIndex);
3537 auto flatBufferInputParams = flatBufferLayer->inputParams();
3567 IConnectableLayer* layer = m_Network->AddQuantizedLstmLayer(lstmInputParams, layerName.c_str());
3575 RegisterInputSlots(graph, layerIndex, layer);
3576 RegisterOutputSlots(graph, layerIndex, layer);
3579 void IDeserializer::DeserializerImpl::ParseDequantize(
GraphPtr graph,
unsigned int layerIndex)
3589 const std::string layerName = GetLayerName(graph, layerIndex);
3595 RegisterInputSlots(graph, layerIndex, layer);
3596 RegisterOutputSlots(graph, layerIndex, layer);
3599 void IDeserializer::DeserializerImpl::ParseMerge(
GraphPtr graph,
unsigned int layerIndex)
3609 const std::string layerName = GetLayerName(graph, layerIndex);
3615 RegisterInputSlots(graph, layerIndex, layer);
3616 RegisterOutputSlots(graph, layerIndex, layer);
3619 void IDeserializer::DeserializerImpl::ParseSwitch(
GraphPtr graph,
unsigned int layerIndex)
3622 auto inputs = GetInputs(graph, layerIndex);
3626 auto outputs = GetOutputs(graph, layerIndex);
3629 auto layerName = GetLayerName(graph, layerIndex);
3638 RegisterInputSlots(graph, layerIndex, layer);
3639 RegisterOutputSlots(graph, layerIndex, layer);
3642 void IDeserializer::DeserializerImpl::ParseTile(
GraphPtr graph,
unsigned int layerIndex)
3645 auto inputs = GetInputs(graph, layerIndex);
3649 auto outputs = GetOutputs(graph, layerIndex);
3652 auto TileLayer = graph->layers()->Get(layerIndex)->layer_as_TileLayer();
3653 auto layerName = GetLayerName(graph, layerIndex);
3654 auto flatBufferDescriptor =
TileLayer->descriptor();
3655 auto flatBufferMultiples = flatBufferDescriptor->m_Multiples();
3658 tileDescriptor.
m_Multiples = std::vector<unsigned int>(flatBufferMultiples->begin(), flatBufferMultiples->end());
3660 IConnectableLayer* layer = m_Network->AddTileLayer(tileDescriptor, layerName.c_str());
3665 RegisterInputSlots(graph, layerIndex, layer);
3666 RegisterOutputSlots(graph, layerIndex, layer);
3669 void IDeserializer::DeserializerImpl::ParsePrelu(
GraphPtr graph,
unsigned int layerIndex)
3672 auto inputs = GetInputs(graph, layerIndex);
3676 auto outputs = GetOutputs(graph, layerIndex);
3679 auto layerName = GetLayerName(graph, layerIndex);
3685 RegisterInputSlots(graph, layerIndex, layer);
3686 RegisterOutputSlots(graph, layerIndex, layer);
3689 void IDeserializer::DeserializerImpl::ParseTranspose(
GraphPtr graph,
unsigned int layerIndex)
3693 auto dimsMapping = graph->layers()->Get(layerIndex)->layer_as_TransposeLayer()->descriptor()->dimMappings();
3695 auto inputs = GetInputs(graph, layerIndex);
3698 auto outputs = GetOutputs(graph, layerIndex);
3702 auto layerName = GetLayerName(graph, layerIndex);
3705 IConnectableLayer* layer = m_Network->AddTransposeLayer(descriptor, layerName.c_str());
3708 RegisterInputSlots(graph, layerIndex, layer);
3709 RegisterOutputSlots(graph, layerIndex, layer);
3712 void IDeserializer::DeserializerImpl::ParseTransposeConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
3716 auto inputs = GetInputs(graph, layerIndex);
3719 auto outputs = GetOutputs(graph, layerIndex);
3722 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_TransposeConvolution2dLayer();
3723 auto layerName = GetLayerName(graph, layerIndex);
3724 auto serializerDescriptor = serializerLayer->descriptor();
3727 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
3728 descriptor.
m_PadRight = serializerDescriptor->padRight();
3729 descriptor.
m_PadTop = serializerDescriptor->padTop();
3730 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
3731 descriptor.
m_StrideX = serializerDescriptor->strideX();
3732 descriptor.
m_StrideY = serializerDescriptor->strideY();;
3733 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();;
3742 optionalBiases = armnn::MakeOptional<armnn::ConstTensor>(biases);
3745 IConnectableLayer* layer = m_Network->AddTransposeConvolution2dLayer(descriptor,
3753 RegisterInputSlots(graph, layerIndex, layer);
3754 RegisterOutputSlots(graph, layerIndex, layer);
3757 void IDeserializer::DeserializerImpl::ParseStack(
GraphPtr graph,
unsigned int layerIndex)
3760 auto inputs = GetInputs(graph, layerIndex);
3762 auto outputs = GetOutputs(graph, layerIndex);
3765 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_StackLayer()->descriptor();
3766 unsigned int axis = flatBufferDescriptor->axis();
3767 unsigned int numInputs = flatBufferDescriptor->numInputs();
3770 auto flatBufferInputShape = flatBufferDescriptor->inputShape();
3771 std::vector<uint32_t> vectorInputShape(flatBufferInputShape->begin(),
3772 flatBufferInputShape->begin() + flatBufferInputShape->size());
3774 TensorShape inputShape(
static_cast<unsigned int>(vectorInputShape.size()), vectorInputShape.data());
3777 for (
unsigned int i=0; i<inputs.size(); ++i)
3780 if (descriptor.m_InputShape != inputShape)
3782 std::stringstream ss;
3783 ss <<
"Shape of input "
3787 <<
" does not equal defined input shape "
3788 << descriptor.m_InputShape
3795 auto layerName = GetLayerName(graph, layerIndex);
3796 IConnectableLayer* layer = m_Network->AddStackLayer(descriptor, layerName.c_str());
3801 RegisterInputSlots(graph, layerIndex, layer);
3802 RegisterOutputSlots(graph, layerIndex, layer);
3805 void IDeserializer::DeserializerImpl::ParseStandIn(
GraphPtr graph,
unsigned int layerIndex)
3809 auto inputs = GetInputs(graph, layerIndex);
3810 auto outputs = GetOutputs(graph, layerIndex);
3812 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_StandInLayer();
3813 auto fbDescriptor = fbLayer->descriptor();
3816 descriptor.
m_NumInputs = fbDescriptor->numInputs();
3822 const std::string layerName = GetLayerName(graph, layerIndex);
3825 for (
unsigned int i = 0u; i < descriptor.
m_NumOutputs; ++i)
3831 RegisterInputSlots(graph, layerIndex, layer);
3832 RegisterOutputSlots(graph, layerIndex, layer);
3852 void IDeserializer::DeserializerImpl::ParseUnidirectionalSequenceLstm(
GraphPtr graph,
unsigned int layerIndex)
3856 auto inputs = GetInputs(graph, layerIndex);
3859 auto outputs = GetOutputs(graph, layerIndex);
3862 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_UnidirectionalSequenceLstmLayer();
3863 auto layerName = GetLayerName(graph, layerIndex);
3864 auto flatBufferDescriptor = flatBufferLayer->descriptor();
3865 auto flatBufferInputParams = flatBufferLayer->inputParams();
3867 auto descriptor = GetUnidirectionalSequenceLstmDescriptor(flatBufferDescriptor);
3895 if (!descriptor.m_CifgEnabled)
3897 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
3898 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
3899 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
3905 if (descriptor.m_PeepholeEnabled)
3907 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
3914 if (descriptor.m_ProjectionEnabled)
3916 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
3917 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
3925 if (descriptor.m_PeepholeEnabled)
3927 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
3928 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
3938 if (descriptor.m_LayerNormEnabled)
3940 if (!descriptor.m_CifgEnabled)
3942 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
3945 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
3946 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
3947 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
3954 IConnectableLayer* layer = m_Network->AddUnidirectionalSequenceLstmLayer(descriptor,
3967 RegisterInputSlots(graph, layerIndex, layer);
3968 RegisterOutputSlots(graph, layerIndex, layer);