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:
505 case armnnSerializer::ActivationFunction_Gelu:
516 case armnnSerializer::ArgMinMaxFunction::ArgMinMaxFunction_Max:
518 case armnnSerializer::ArgMinMaxFunction::ArgMinMaxFunction_Min:
528 case armnnSerializer::ComparisonOperation::ComparisonOperation_Equal:
530 case armnnSerializer::ComparisonOperation::ComparisonOperation_Greater:
532 case armnnSerializer::ComparisonOperation::ComparisonOperation_GreaterOrEqual:
534 case armnnSerializer::ComparisonOperation::ComparisonOperation_Less:
536 case armnnSerializer::ComparisonOperation::ComparisonOperation_LessOrEqual:
538 case armnnSerializer::ComparisonOperation::ComparisonOperation_NotEqual:
548 case armnnSerializer::ReduceOperation::ReduceOperation_Sum:
550 case armnnSerializer::ReduceOperation::ReduceOperation_Max:
552 case armnnSerializer::ReduceOperation::ReduceOperation_Mean:
554 case armnnSerializer::ReduceOperation::ReduceOperation_Min:
556 case armnnSerializer::ReduceOperation::ReduceOperation_Prod:
567 case armnnSerializer::LogicalBinaryOperation::LogicalBinaryOperation_LogicalAnd:
569 case armnnSerializer::LogicalBinaryOperation::LogicalBinaryOperation_LogicalOr:
580 case armnnSerializer::BinaryOperation::BinaryOperation_Add:
582 case armnnSerializer::BinaryOperation::BinaryOperation_Div:
584 case armnnSerializer::BinaryOperation::BinaryOperation_Maximum:
586 case armnnSerializer::BinaryOperation::BinaryOperation_Minimum:
588 case armnnSerializer::BinaryOperation::BinaryOperation_Mul:
590 case armnnSerializer::BinaryOperation::BinaryOperation_Sub:
592 case armnnSerializer::BinaryOperation::BinaryOperation_SqDiff:
594 case armnnSerializer::BinaryOperation::BinaryOperation_Power:
605 case armnnSerializer::UnaryOperation::UnaryOperation_Abs:
607 case armnnSerializer::UnaryOperation::UnaryOperation_Ceil:
609 case armnnSerializer::UnaryOperation::UnaryOperation_Rsqrt:
611 case armnnSerializer::UnaryOperation::UnaryOperation_Sqrt:
613 case armnnSerializer::UnaryOperation::UnaryOperation_Exp:
615 case armnnSerializer::UnaryOperation::UnaryOperation_Neg:
617 case armnnSerializer::UnaryOperation::UnaryOperation_LogicalNot:
619 case armnnSerializer::UnaryOperation::UnaryOperation_Log:
621 case armnnSerializer::UnaryOperation::UnaryOperation_Sin:
632 case armnnSerializer::PaddingMode::PaddingMode_Reflect:
634 case armnnSerializer::PaddingMode::PaddingMode_Symmetric:
645 case armnnSerializer::ResizeMethod_NearestNeighbor:
647 case armnnSerializer::ResizeMethod_Bilinear:
659 switch (tensorPtr->dataType())
661 case DataType_QAsymmS8:
664 case DataType_QSymmS8:
667 case DataType_QuantisedAsymm8:
668 case DataType_QAsymmU8:
671 case DataType_QSymmS16:
672 case DataType_QuantisedSymm16:
675 case DataType_Signed32:
678 case DataType_Signed64:
681 case DataType_Float32:
684 case DataType_Float16:
687 case DataType_Boolean:
693 throw ParseException(fmt::format(
"Unsupported data type {0} = {1}. {2}",
694 tensorPtr->dataType(),
695 EnumNameDataType(tensorPtr->dataType()),
700 float quantizationScale = tensorPtr->quantizationScale();
701 int32_t quantizationOffset = tensorPtr->quantizationOffset();
703 if (tensorPtr->dimensionality() ==
static_cast<unsigned int>(Dimensionality::Scalar))
710 else if (tensorPtr->dimensionality() ==
static_cast<unsigned int>(Dimensionality::NotSpecified))
719 auto dimensions = tensorPtr->dimensions();
720 unsigned int size = dimensions->size();
721 std::vector<unsigned int> outputDims(dimensions->begin(), dimensions->begin() + size);
726 if (tensorPtr->dimensionSpecificity() !=
nullptr)
728 auto dimensionSpecificity = tensorPtr->dimensionSpecificity();
729 size = dimensionSpecificity->size();
730 for (
unsigned int i = 0; i < size; ++i)
732 dimensionsSpecificity[i] = dimensionSpecificity->Get(i);
736 TensorShape shape(size, outputDims.data(), dimensionsSpecificity);
738 auto quantizationScales = tensorPtr->quantizationScales();
739 if (quantizationScales)
741 unsigned int quantizationScalesSize = quantizationScales->size();
742 std::vector<float> scales(quantizationScales->begin(), quantizationScales->begin() + quantizationScalesSize);
743 unsigned int quantizationDim = tensorPtr->quantizationDim();
766 switch (constTensorPtr->data_type())
768 case ConstTensorData_ByteData:
770 auto byteData = constTensorPtr->data_as_ByteData()->data();
774 case ConstTensorData_ShortData:
776 auto shortData = constTensorPtr->data_as_ShortData()->data();
780 case ConstTensorData_IntData:
782 auto intData = constTensorPtr->data_as_IntData()->data();
786 case ConstTensorData_LongData:
788 auto longData = constTensorPtr->data_as_LongData()->data();
795 throw ParseException(fmt::format(
"Unsupported data type {0} = {1}. {2}",
796 constTensorPtr->data_type(),
797 EnumNameConstTensorData(constTensorPtr->data_type()),
806 auto layer = GetBaseLayer(graphPtr, layerIndex);
807 const auto& numInputs = layer->inputSlots()->size();
811 for (
unsigned int i=0; i<numInputs; ++i)
814 (layer->inputSlots()->Get(i)->connection()->sourceLayerIndex()));
815 result[i] = GetBaseLayer(graphPtr, inputId)->outputSlots()->Get(0)->tensorInfo();
823 auto layer = GetBaseLayer(graphPtr, layerIndex);
824 const auto& numOutputs = layer->outputSlots()->size();
828 for (
unsigned int i=0; i<numOutputs; ++i)
830 result[i] = layer->outputSlots()->Get(i)->tensorInfo();
835 void IDeserializer::DeserializerImpl::ParseUnsupportedLayer(
GraphPtr graph,
unsigned int layerIndex)
838 const auto layerName = GetBaseLayer(graph, layerIndex)->layerName()->c_str();
839 throw ParseException(fmt::format(
"Layer not supported. layerIndex: {0} "
840 "layerName: {1} / {2}",
846 void IDeserializer::DeserializerImpl::ResetParser()
849 m_InputBindings.clear();
850 m_OutputBindings.clear();
857 GraphPtr graph = LoadGraphFromBinary(binaryContent.data(), binaryContent.size());
858 return CreateNetworkFromGraph(graph);
864 if (binaryContent.fail()) {
868 binaryContent.seekg(0, std::ios::end);
869 const std::streamoff size = binaryContent.tellg();
870 std::vector<char> content(
static_cast<size_t>(size));
871 binaryContent.seekg(0);
872 binaryContent.read(content.data(),
static_cast<std::streamsize
>(size));
873 GraphPtr graph = LoadGraphFromBinary(
reinterpret_cast<uint8_t*
>(content.data()),
static_cast<size_t>(size));
874 return CreateNetworkFromGraph(graph);
879 if (binaryContent ==
nullptr)
884 flatbuffers::Verifier verifier(binaryContent, len);
885 if (verifier.VerifyBuffer<SerializedGraph>() ==
false)
887 throw ParseException(fmt::format(
"Buffer doesn't conform to the expected Armnn "
888 "flatbuffers format. size:{0} {1}",
892 return GetSerializedGraph(binaryContent);
897 if (graph ==
nullptr)
901 m_Network = INetwork::Create();
902 unsigned int layerIndex = 0;
903 for (AnyLayer
const* layer : *graph->layers())
905 if (layer->layer_type() != Layer_InputLayer &&
906 layer->layer_type() != Layer_OutputLayer)
909 auto& parserFunction = m_ParserFunctions[layer->layer_type()];
910 (this->*parserFunction)(graph, layerIndex);
915 SetupInputLayers(graph);
916 SetupOutputLayers(graph);
919 for (
auto&& graphIt : m_GraphConnections)
921 Connections& connections = graphIt.second;
922 for (
auto&& outputIt : connections.outputSlots)
924 const unsigned int outputSlotIndex = outputIt.first;
926 if (connections.inputSlots.find(outputSlotIndex) != connections.inputSlots.end())
928 for (
IInputSlot* inputSlot : connections.inputSlots[outputSlotIndex])
930 outputSlot->
Connect(*inputSlot);
936 return std::move(m_Network);
940 const std::string& name)
const
943 for (
auto inputBinding : m_InputBindings)
945 if (inputBinding.first == name)
947 return inputBinding.second;
950 throw ParseException(fmt::format(
"No input binding found for layer:{0} / {1}",
956 const std::string& name)
const
959 for (
auto outputBinding : m_OutputBindings)
961 if (outputBinding.first == name)
963 return outputBinding.second;
966 throw ParseException(fmt::format(
"No output binding found for layer:{0} / {1}",
971 unsigned int IDeserializer::DeserializerImpl::GetInputLayerInVector(
GraphPtr graph,
int targetId)
973 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
975 auto layer = graph->layers()->Get(i);
976 if (layer->layer_type() == Layer::Layer_InputLayer)
978 auto layerBindingId = layer->layer_as_InputLayer()->base()->layerBindingId();
979 if (layerBindingId == targetId)
985 throw ParseException(
"Input layer with given layerBindingId not found");
988 unsigned int IDeserializer::DeserializerImpl::GetOutputLayerInVector(
GraphPtr graph,
int targetId)
990 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
992 auto layer = graph->layers()->Get(i);
993 if (layer->layer_type() == Layer::Layer_OutputLayer)
995 auto layerBindingId = layer->layer_as_OutputLayer()->base()->layerBindingId();
996 if (layerBindingId == targetId)
1002 throw ParseException(
"Output layer with given layerBindingId not found");
1005 unsigned int IDeserializer::DeserializerImpl::GetLayerIndexInVector(
GraphPtr graph,
unsigned int targetIndex)
1007 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
1010 if (layer->index() == targetIndex)
1018 IDeserializer::DeserializerImpl::FeatureVersions IDeserializer::DeserializerImpl::GetFeatureVersions(
GraphPtr graph)
1020 IDeserializer::DeserializerImpl::FeatureVersions versions;
1022 if (graph->featureVersions())
1024 versions.m_BindingIdScheme = graph->featureVersions()->bindingIdsScheme();
1025 versions.m_WeightsLayoutScheme = graph->featureVersions()->weightsLayoutScheme();
1026 versions.m_ConstTensorsAsInputs = graph->featureVersions()->constantTensorsAsInputs();
1032 void IDeserializer::DeserializerImpl::SetupInputLayers(
GraphPtr graph)
1035 const unsigned int numInputs = graph->inputIds()->size();
1036 m_InputBindings.clear();
1037 m_InputBindings.reserve(numInputs);
1039 for (
unsigned int i = 0; i < numInputs; i++)
1041 unsigned int inputLayerIndex = 0xFFFFFFFF;
1042 if (GetFeatureVersions(graph).m_BindingIdScheme == 0)
1044 const unsigned int inputId = armnn::numeric_cast<unsigned int>(graph->inputIds()->Get(i));
1045 inputLayerIndex = GetLayerIndexInVector(graph, inputId);
1049 const int inputId = graph->inputIds()->Get(i);
1050 inputLayerIndex = GetInputLayerInVector(graph, inputId);
1056 LayerBindingId bindingId = GetBindingLayerInfo(graph, inputLayerIndex);
1057 if (baseLayer->layerName()->c_str() ==
nullptr)
1059 throw ParseException(fmt::format(
"Input with layer index [{0}] has no name", inputLayerIndex));
1063 m_Network->AddInputLayer(bindingId, baseLayer->layerName()->c_str());
1067 RegisterOutputSlots(graph, inputLayerIndex, inputLayer);
1070 m_InputBindings.push_back(std::make_pair(baseLayer->layerName()->c_str(), bindingInfo));
1074 void IDeserializer::DeserializerImpl::SetupOutputLayers(
GraphPtr graph)
1077 const unsigned int numOutputs = graph->outputIds()->size();
1078 m_OutputBindings.clear();
1079 m_OutputBindings.reserve(numOutputs);
1081 for (
unsigned int i = 0; i < numOutputs; i++)
1083 unsigned int outputLayerIndex = 0xFFFFFFFF;
1084 if (GetFeatureVersions(graph).m_BindingIdScheme == 0)
1086 const unsigned int outputId = armnn::numeric_cast<unsigned int>(graph->outputIds()->Get(i));
1087 outputLayerIndex = GetLayerIndexInVector(graph, outputId);
1091 const int outputId = graph->outputIds()->Get(i);
1092 outputLayerIndex = GetOutputLayerInVector(graph, outputId);
1098 LayerBindingId bindingId = GetBindingLayerInfo(graph, outputLayerIndex);
1099 if (baseLayer->layerName()->c_str() ==
nullptr)
1101 throw ParseException(fmt::format(
"Output with layer index [{0}] has no name", outputLayerIndex));
1105 m_Network->AddOutputLayer(bindingId, baseLayer->layerName()->c_str());
1107 RegisterInputSlots(graph, outputLayerIndex, outputLayer);
1108 unsigned int sourceLayerIndex =
1109 GetLayerIndexInVector(graph, baseLayer->inputSlots()->Get(0)->connection()->sourceLayerIndex());
1110 unsigned int outputSlotIndex =
1111 GetLayerIndexInVector(graph, baseLayer->inputSlots()->Get(0)->connection()->outputSlotIndex());
1112 LayerBaseRawPtr sourceBaseLayer = GetBaseLayer(graph, sourceLayerIndex);
1114 sourceBaseLayer->outputSlots()->Get(outputSlotIndex)->tensorInfo());
1116 m_OutputBindings.push_back(std::make_pair(baseLayer->layerName()->c_str(), bindingInfo));
1120 void IDeserializer::DeserializerImpl::RegisterOutputSlots(
GraphPtr graph,
1121 uint32_t layerIndex,
1124 if (layer ==
nullptr)
1127 "RegisterOutputSlots: pointer to layer with index [{0}] is null", layerIndex));
1133 throw ParseException(fmt::format(
"The number of outputslots ({0}) does not match the number expected ({1})"
1134 " for layer index: {2} {3}",
1135 baseLayer->outputSlots()->size(),
1143 const unsigned int slotIndex = baseLayer->outputSlots()->Get(i)->index();
1146 RegisterOutputSlotOfConnection(baseLayer->index(), slotIndex, outputSlot);
1150 void IDeserializer::DeserializerImpl::RegisterInputSlots(
GraphPtr graph,
1151 uint32_t layerIndex,
1153 std::vector<unsigned int> ignoreSlots)
1155 if (layer ==
nullptr)
1158 "RegisterInputSlots: pointer to layer with index [{0}] is null", layerIndex));
1163 if (baseLayer->inputSlots()->size() != (layer->
GetNumInputSlots() - ignoreSlots.size()))
1165 throw ParseException(fmt::format(
"The number of inputslots ({0}) does not match the number expected ({1})"
1166 " for layer index:{2} {3}",
1167 baseLayer->inputSlots()->size(),
1176 if (std::find(ignoreSlots.begin(), ignoreSlots.end(), i) == ignoreSlots.end())
1178 auto fbInputSlot = baseLayer->inputSlots()->Get(i);
1179 auto fbConnection = fbInputSlot->connection();
1183 if (fbInputSlot->isOverridden())
1188 RegisterInputSlotOfConnection(fbConnection->sourceLayerIndex(), fbConnection->outputSlotIndex(), inputSlot);
1193 void IDeserializer::DeserializerImpl::RegisterInputSlotOfConnection(uint32_t sourceLayerIndex,
1194 uint32_t outputSlotIndex,
1197 if (m_GraphConnections.find(sourceLayerIndex) == m_GraphConnections.end())
1199 m_GraphConnections[sourceLayerIndex] = Connections();
1202 Connections& connections = m_GraphConnections[sourceLayerIndex];
1203 if (connections.inputSlots.find(outputSlotIndex) == connections.inputSlots.end())
1205 connections.inputSlots[outputSlotIndex] = {inputSlot};
1209 connections.inputSlots[outputSlotIndex].push_back(inputSlot);
1213 void IDeserializer::DeserializerImpl::RegisterOutputSlotOfConnection(uint32_t sourceLayerIndex,
1214 uint32_t outputSlotIndex,
1217 if (m_GraphConnections.find(sourceLayerIndex) == m_GraphConnections.end())
1219 m_GraphConnections[sourceLayerIndex] = Connections();
1222 Connections& connections = m_GraphConnections[sourceLayerIndex];
1223 if (connections.outputSlots.find(outputSlotIndex) != connections.outputSlots.end())
1228 connections.outputSlots[outputSlotIndex] = outputSlot;
1231 void IDeserializer::DeserializerImpl::ParseAbs(
GraphPtr graph,
unsigned int layerIndex)
1234 auto inputs = GetInputs(graph, layerIndex);
1238 auto outputs = GetOutputs(graph, layerIndex);
1241 auto layerName = GetLayerName(graph, layerIndex);
1244 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
1248 RegisterInputSlots(graph, layerIndex, layer);
1249 RegisterOutputSlots(graph, layerIndex, layer);
1252 void IDeserializer::DeserializerImpl::ParseActivation(
GraphPtr graph,
unsigned int layerIndex)
1255 auto inputs = GetInputs(graph, layerIndex);
1259 auto outputs = GetOutputs(graph, layerIndex);
1262 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ActivationLayer();
1263 auto layerName = GetLayerName(graph, layerIndex);
1264 auto serializerDescriptor = serializerLayer->descriptor();
1268 descriptor.
m_A = serializerDescriptor->a();
1269 descriptor.
m_B = serializerDescriptor->b();
1276 RegisterInputSlots(graph, layerIndex, layer);
1277 RegisterOutputSlots(graph, layerIndex, layer);
1280 void IDeserializer::DeserializerImpl::ParseAdd(
GraphPtr graph,
unsigned int layerIndex)
1283 auto inputs = GetInputs(graph, layerIndex);
1287 auto outputs = GetOutputs(graph, layerIndex);
1290 auto layerName = GetLayerName(graph, layerIndex);
1292 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(descriptor, layerName.c_str());
1297 RegisterInputSlots(graph, layerIndex, layer);
1298 RegisterOutputSlots(graph, layerIndex, layer);
1301 void IDeserializer::DeserializerImpl::ParseArgMinMax(
GraphPtr graph,
unsigned int layerIndex)
1304 auto inputs = GetInputs(graph, layerIndex);
1308 auto outputs = GetOutputs(graph, layerIndex);
1311 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ArgMinMaxLayer();
1312 auto serializerDescriptor = serializerLayer->descriptor();
1316 descriptor.
m_Axis = serializerDescriptor->axis();
1317 auto layerName = GetLayerName(graph, layerIndex);
1318 IConnectableLayer* layer = m_Network->AddArgMinMaxLayer(descriptor, layerName.c_str());
1323 RegisterInputSlots(graph, layerIndex, layer);
1324 RegisterOutputSlots(graph, layerIndex, layer);
1327 void IDeserializer::DeserializerImpl::ParseBatchMatMul(
GraphPtr graph,
unsigned int layerIndex)
1331 auto inputs = GetInputs(graph, layerIndex);
1335 auto outputs = GetOutputs(graph, layerIndex);
1338 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_BatchMatMulLayer();
1339 auto serializerDescriptor = serializerLayer->descriptor();
1342 serializerDescriptor->transposeY(),
1343 serializerDescriptor->adjointX(),
1344 serializerDescriptor->adjointY(),
1348 auto layerName = GetLayerName(graph, layerIndex);
1349 IConnectableLayer* layer = m_Network->AddBatchMatMulLayer(descriptor, layerName.c_str());
1354 RegisterInputSlots(graph, layerIndex, layer);
1355 RegisterOutputSlots(graph, layerIndex, layer);
1358 void IDeserializer::DeserializerImpl::ParseBatchToSpaceNd(
GraphPtr graph,
unsigned int layerIndex)
1368 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->descriptor();
1369 auto flatBufferCrops = flatBufferDescriptor->crops();
1370 auto flatBufferBlockShape = flatBufferDescriptor->blockShape();
1372 if (flatBufferCrops->size() % 2 != 0)
1377 std::vector<std::pair<unsigned int, unsigned int>> crops;
1378 crops.reserve(flatBufferCrops->size() / 2);
1379 for (
unsigned int i = 0; i < flatBufferCrops->size() - 1; i += 2)
1381 crops.emplace_back(flatBufferCrops->Get(i), flatBufferCrops->Get(i+1));
1387 std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end());
1390 auto layerName = GetLayerName(graph, layerIndex);
1391 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(descriptor, layerName.c_str());
1396 RegisterInputSlots(graph, layerIndex, layer);
1397 RegisterOutputSlots(graph, layerIndex, layer);
1400 void IDeserializer::DeserializerImpl::ParseBatchNormalization(
GraphPtr graph,
unsigned int layerIndex)
1404 auto inputs = GetInputs(graph, layerIndex);
1407 auto outputs = GetOutputs(graph, layerIndex);
1411 auto layerName = GetLayerName(graph, layerIndex);
1413 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_BatchNormalizationLayer();
1414 auto serializerDescriptor = serializerLayer->descriptor();
1417 descriptor.
m_Eps = serializerDescriptor->eps();
1433 RegisterInputSlots(graph, layerIndex, layer);
1434 RegisterOutputSlots(graph, layerIndex, layer);
1437 void IDeserializer::DeserializerImpl::ParseCast(
GraphPtr graph,
unsigned int layerIndex)
1447 auto layerName = GetLayerName(graph, layerIndex);
1454 RegisterInputSlots(graph, layerIndex, layer);
1455 RegisterOutputSlots(graph, layerIndex, layer);
1458 void IDeserializer::DeserializerImpl::ParseConstant(
GraphPtr graph,
unsigned int layerIndex)
1463 auto outputs = GetOutputs(graph, layerIndex);
1466 auto layerName = GetLayerName(graph, layerIndex);
1468 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ConstantLayer();
1469 auto serializerInput = serializerLayer->input();
1478 if (this->GetFeatureVersions(graph).m_WeightsLayoutScheme <= 0)
1484 std::unique_ptr<unsigned char[]> permuteBuffer(
new unsigned char[weightsInfo.
GetNumBytes()]);
1491 auto weightsShape = weightsInfo.
GetShape();
1495 weightsShape[2]*weightsShape[3]});
1500 layer = m_Network->AddConstantLayer(weightsPermuted, layerName.c_str());
1504 RegisterOutputSlots(graph, layerIndex, layer);
1510 layer = m_Network->AddConstantLayer(input, layerName.c_str());
1517 RegisterOutputSlots(graph, layerIndex, layer);
1520 void IDeserializer::DeserializerImpl::ParseConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
1523 auto inputs = GetInputs(graph, layerIndex);
1526 auto outputs = GetOutputs(graph, layerIndex);
1529 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_Convolution2dLayer();
1531 auto layerName = GetLayerName(graph, layerIndex);
1532 auto flatbufferDescriptor = flatBufferLayer->descriptor();
1535 descriptor.
m_PadLeft = flatbufferDescriptor->padLeft();
1536 descriptor.
m_PadRight = flatbufferDescriptor->padRight();
1537 descriptor.
m_PadTop = flatbufferDescriptor->padTop();
1538 descriptor.
m_PadBottom = flatbufferDescriptor->padBottom();
1539 descriptor.
m_StrideX = flatbufferDescriptor->strideX();
1540 descriptor.
m_StrideY = flatbufferDescriptor->strideY();;
1541 descriptor.
m_DilationX = flatbufferDescriptor->dilationX();
1542 descriptor.
m_DilationY = flatbufferDescriptor->dilationY();;
1543 descriptor.
m_BiasEnabled = flatbufferDescriptor->biasEnabled();;
1547 std::vector<unsigned int> ignoreSlots {};
1552 if (this->GetFeatureVersions(graph).m_ConstTensorsAsInputs <= 0)
1558 layer = m_Network->AddConvolution2dLayer(descriptor,
1562 auto weightsLayer = m_Network->AddConstantLayer(weightsTensor);
1563 weightsLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(1u));
1564 weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsTensor.
GetInfo());
1565 ignoreSlots.emplace_back(1u);
1570 auto biasLayer = m_Network->AddConstantLayer(biasTensor);
1571 biasLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(2u));
1572 biasLayer->GetOutputSlot(0).SetTensorInfo(biasTensor.
GetInfo());
1573 ignoreSlots.emplace_back(2u);
1578 layer = m_Network->AddConvolution2dLayer(descriptor,
1587 RegisterInputSlots(graph, layerIndex, layer, ignoreSlots);
1588 RegisterOutputSlots(graph, layerIndex, layer);
1591 void IDeserializer::DeserializerImpl::ParseConvolution3d(
GraphPtr graph,
unsigned int layerIndex)
1594 auto inputs = GetInputs(graph, layerIndex);
1597 auto outputs = GetOutputs(graph, layerIndex);
1600 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_Convolution3dLayer();
1601 auto layerName = GetLayerName(graph, layerIndex);
1602 auto serializerDescriptor = serializerLayer->descriptor();
1605 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
1606 descriptor.
m_PadRight = serializerDescriptor->padRight();
1607 descriptor.
m_PadTop = serializerDescriptor->padTop();
1608 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
1609 descriptor.
m_PadFront = serializerDescriptor->padFront();
1610 descriptor.
m_PadBack = serializerDescriptor->padBack();
1611 descriptor.
m_StrideX = serializerDescriptor->strideX();
1612 descriptor.
m_StrideY = serializerDescriptor->strideY();
1613 descriptor.
m_StrideZ = serializerDescriptor->strideZ();
1614 descriptor.
m_DilationX = serializerDescriptor->dilationX();
1615 descriptor.
m_DilationY = serializerDescriptor->dilationY();
1616 descriptor.
m_DilationZ = serializerDescriptor->dilationZ();
1617 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();
1623 IConnectableLayer* layer = m_Network->AddConvolution3dLayer(descriptor, layerName.c_str());
1628 RegisterInputSlots(graph, layerIndex, layer);
1629 RegisterOutputSlots(graph, layerIndex, layer);
1632 void IDeserializer::DeserializerImpl::ParseDepthToSpace(
GraphPtr graph,
unsigned int layerIndex)
1636 auto inputs = GetInputs(graph, layerIndex);
1639 auto outputs = GetOutputs(graph, layerIndex);
1642 auto fbDescriptor = graph->layers()->Get(layerIndex)->layer_as_DepthToSpaceLayer()->descriptor();
1645 descriptor.
m_BlockSize = fbDescriptor->blockSize();
1648 auto layerName = GetLayerName(graph, layerIndex);
1649 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
1654 RegisterInputSlots(graph, layerIndex, layer);
1655 RegisterOutputSlots(graph, layerIndex, layer);
1658 void IDeserializer::DeserializerImpl::ParseDepthwiseConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
1661 auto inputs = GetInputs(graph, layerIndex);
1664 auto outputs = GetOutputs(graph, layerIndex);
1667 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer();
1668 auto layerName = GetLayerName(graph, layerIndex);
1669 auto serializerDescriptor = serializerLayer->descriptor();
1672 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
1673 descriptor.
m_PadRight = serializerDescriptor->padRight();
1674 descriptor.
m_PadTop = serializerDescriptor->padTop();
1675 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
1676 descriptor.
m_StrideX = serializerDescriptor->strideX();
1677 descriptor.
m_StrideY = serializerDescriptor->strideY();
1678 descriptor.
m_DilationX = serializerDescriptor->dilationX();
1679 descriptor.
m_DilationY = serializerDescriptor->dilationY();
1680 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();
1684 std::vector<unsigned int> ignoreSlots {};
1688 if (this->GetFeatureVersions(graph).m_ConstTensorsAsInputs <= 0)
1695 ignoreSlots.emplace_back(1u);
1697 layer = m_Network->AddDepthwiseConvolution2dLayer(descriptor,
1704 ignoreSlots.emplace_back(2u);
1706 auto biasLayer = m_Network->AddConstantLayer(biases);
1707 biasLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(2u));
1708 biasLayer->GetOutputSlot(0).SetTensorInfo(biases.
GetInfo());
1711 if (this->GetFeatureVersions(graph).m_WeightsLayoutScheme <= 0)
1717 std::unique_ptr<unsigned char[]> permuteBuffer(
new unsigned char[weightsInfo.
GetNumBytes()]);
1724 auto weightsShape = weightsInfo.
GetShape();
1728 weightsShape[2]*weightsShape[3]});
1732 auto weightsLayer = m_Network->AddConstantLayer(weightsPermuted);
1733 weightsLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(1u));
1734 weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsPermuted.GetInfo());
1738 auto weightsLayer = m_Network->AddConstantLayer(weights);
1739 weightsLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(1u));
1740 weightsLayer->GetOutputSlot(0).SetTensorInfo(weights.
GetInfo());
1745 layer = m_Network->AddDepthwiseConvolution2dLayer(descriptor,
1754 RegisterInputSlots(graph, layerIndex, layer, ignoreSlots);
1755 RegisterOutputSlots(graph, layerIndex, layer);
1758 void IDeserializer::DeserializerImpl::ParseDetectionPostProcess(
GraphPtr graph,
unsigned int layerIndex)
1761 auto inputs = GetInputs(graph, layerIndex);
1765 auto outputs = GetOutputs(graph, layerIndex);
1768 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_DetectionPostProcessLayer();
1769 auto layerName = GetLayerName(graph, layerIndex);
1770 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1778 descriptor.
m_NumClasses = flatBufferDescriptor->numClasses();
1780 descriptor.
m_ScaleX = flatBufferDescriptor->scaleX();
1781 descriptor.
m_ScaleY = flatBufferDescriptor->scaleY();
1782 descriptor.
m_ScaleW = flatBufferDescriptor->scaleW();
1783 descriptor.
m_ScaleH = flatBufferDescriptor->scaleH();
1791 for (
unsigned int i = 0; i < 4; i++)
1796 RegisterInputSlots(graph, layerIndex, layer);
1797 RegisterOutputSlots(graph, layerIndex, layer);
1800 void IDeserializer::DeserializerImpl::ParseDivision(
GraphPtr graph,
unsigned int layerIndex)
1803 auto inputs = GetInputs(graph, layerIndex);
1807 auto outputs = GetOutputs(graph, layerIndex);
1810 auto layerName = GetLayerName(graph, layerIndex);
1812 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(descriptor, layerName.c_str());
1817 RegisterInputSlots(graph, layerIndex, layer);
1818 RegisterOutputSlots(graph, layerIndex, layer);
1821 void IDeserializer::DeserializerImpl::ParseEqual(
GraphPtr graph,
unsigned int layerIndex)
1824 auto inputs = GetInputs(graph, layerIndex);
1828 auto outputs = GetOutputs(graph, layerIndex);
1831 auto layerName = GetLayerName(graph, layerIndex);
1833 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
1838 RegisterInputSlots(graph, layerIndex, layer);
1839 RegisterOutputSlots(graph, layerIndex, layer);
1842 void IDeserializer::DeserializerImpl::ParseFill(
GraphPtr graph,
unsigned int layerIndex)
1845 auto inputs = GetInputs(graph, layerIndex);
1849 auto outputs = GetOutputs(graph, layerIndex);
1852 auto layerName = GetLayerName(graph, layerIndex);
1854 descriptor.
m_Value = graph->layers()->Get(layerIndex)->layer_as_FillLayer()->descriptor()->value();
1855 IConnectableLayer* layer = m_Network->AddFillLayer(descriptor, layerName.c_str());
1860 RegisterInputSlots(graph, layerIndex, layer);
1861 RegisterOutputSlots(graph, layerIndex, layer);
1864 void IDeserializer::DeserializerImpl::ParseGreater(
GraphPtr graph,
unsigned int layerIndex)
1867 auto inputs = GetInputs(graph, layerIndex);
1871 auto outputs = GetOutputs(graph, layerIndex);
1874 auto layerName = GetLayerName(graph, layerIndex);
1876 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
1881 RegisterInputSlots(graph, layerIndex, layer);
1882 RegisterOutputSlots(graph, layerIndex, layer);
1885 void IDeserializer::DeserializerImpl::ParseInstanceNormalization(
GraphPtr graph,
unsigned int layerIndex)
1889 auto inputs = GetInputs(graph, layerIndex);
1892 auto outputs = GetOutputs(graph, layerIndex);
1895 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer();
1896 auto fbDescriptor = fbLayer->descriptor();
1899 descriptor.
m_Gamma = fbDescriptor->gamma();
1900 descriptor.
m_Beta = fbDescriptor->beta();
1901 descriptor.
m_Eps = fbDescriptor->eps();
1904 const std::string layerName = GetLayerName(graph, layerIndex);
1907 IConnectableLayer* layer = m_Network->AddInstanceNormalizationLayer(descriptor, layerName.c_str());
1910 RegisterInputSlots(graph, layerIndex, layer);
1911 RegisterOutputSlots(graph, layerIndex, layer);
1914 void IDeserializer::DeserializerImpl::ParseL2Normalization(
GraphPtr graph,
unsigned int layerIndex)
1918 auto inputs = GetInputs(graph, layerIndex);
1921 auto outputs = GetOutputs(graph, layerIndex);
1925 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_L2NormalizationLayer();
1926 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1928 auto layerName = GetLayerName(graph, layerIndex);
1931 descriptor.
m_Eps = flatBufferDescriptor->eps();
1933 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(descriptor, layerName.c_str());
1936 RegisterInputSlots(graph, layerIndex, layer);
1937 RegisterOutputSlots(graph, layerIndex, layer);
1940 void IDeserializer::DeserializerImpl::ParseLogicalBinary(
GraphPtr graph,
unsigned int layerIndex)
1945 auto inputs = GetInputs(graph, layerIndex);
1948 auto outputs = GetOutputs(graph, layerIndex);
1951 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_LogicalBinaryLayer();
1952 auto fbDescriptor = fbLayer->descriptor();
1957 const std::string& layerName = GetLayerName(graph, layerIndex);
1958 IConnectableLayer* layer = m_Network->AddLogicalBinaryLayer(descriptor, layerName.c_str());
1963 RegisterInputSlots(graph, layerIndex, layer);
1964 RegisterOutputSlots(graph, layerIndex, layer);
1967 void IDeserializer::DeserializerImpl::ParseLogSoftmax(
GraphPtr graph,
unsigned int layerIndex)
1978 descriptor.
m_Beta = graph->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->descriptor()->beta();
1979 descriptor.
m_Axis = graph->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->descriptor()->axis();
1980 auto layerName = GetLayerName(graph, layerIndex);
1982 IConnectableLayer* layer = m_Network->AddLogSoftmaxLayer(descriptor, layerName.c_str());
1987 RegisterInputSlots(graph, layerIndex, layer);
1988 RegisterOutputSlots(graph, layerIndex, layer);
1991 void IDeserializer::DeserializerImpl::ParseMinimum(
GraphPtr graph,
unsigned int layerIndex)
1994 auto inputs = GetInputs(graph, layerIndex);
1998 auto outputs = GetOutputs(graph, layerIndex);
2001 auto layerName = GetLayerName(graph, layerIndex);
2003 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(descriptor, layerName.c_str());
2008 RegisterInputSlots(graph, layerIndex, layer);
2009 RegisterOutputSlots(graph, layerIndex, layer);
2012 void IDeserializer::DeserializerImpl::ParseMaximum(
GraphPtr graph,
unsigned int layerIndex)
2015 auto inputs = GetInputs(graph, layerIndex);
2019 auto outputs = GetOutputs(graph, layerIndex);
2022 auto layerName = GetLayerName(graph, layerIndex);
2024 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(descriptor, layerName.c_str());
2029 RegisterInputSlots(graph, layerIndex, layer);
2030 RegisterOutputSlots(graph, layerIndex, layer);
2034 unsigned int layerIndex)
2036 auto layerType = graph->layers()->Get(layerIndex)->layer_type();
2040 case Layer::Layer_ConcatLayer:
2041 return graph->layers()->Get(layerIndex)->layer_as_ConcatLayer()->descriptor();
2042 case Layer::Layer_MergerLayer:
2043 return graph->layers()->Get(layerIndex)->layer_as_MergerLayer()->descriptor();
2048 void IDeserializer::DeserializerImpl::ParseChannelShuffle(
GraphPtr graph,
unsigned int layerIndex)
2059 descriptor.
m_Axis = graph->layers()->Get(layerIndex)->layer_as_ChannelShuffleLayer()->descriptor()->axis();
2061 graph->layers()->Get(layerIndex)->layer_as_ChannelShuffleLayer()->descriptor()->numGroups();
2063 auto layerName = GetLayerName(graph, layerIndex);
2064 IConnectableLayer* layer = m_Network->AddChannelShuffleLayer(descriptor, layerName.c_str());
2069 RegisterInputSlots(graph, layerIndex, layer);
2070 RegisterOutputSlots(graph, layerIndex, layer);
2072 void IDeserializer::DeserializerImpl::ParseComparison(
GraphPtr graph,
unsigned int layerIndex)
2077 auto inputs = GetInputs(graph, layerIndex);
2080 auto outputs = GetOutputs(graph, layerIndex);
2083 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ComparisonLayer();
2084 auto fbDescriptor = fbLayer->descriptor();
2089 const std::string& layerName = GetLayerName(graph, layerIndex);
2090 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
2095 RegisterInputSlots(graph, layerIndex, layer);
2096 RegisterOutputSlots(graph, layerIndex, layer);
2099 void IDeserializer::DeserializerImpl::ParseElementwiseBinary(
GraphPtr graph,
unsigned int layerIndex)
2104 auto inputs = GetInputs(graph, layerIndex);
2107 auto outputs = GetOutputs(graph, layerIndex);
2110 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ElementwiseBinaryLayer();
2111 auto fbDescriptor = fbLayer->descriptor();
2116 const std::string& layerName = GetLayerName(graph, layerIndex);
2117 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(descriptor, layerName.c_str());
2122 RegisterInputSlots(graph, layerIndex, layer);
2123 RegisterOutputSlots(graph, layerIndex, layer);
2126 void IDeserializer::DeserializerImpl::ParseElementwiseUnary(
GraphPtr graph,
unsigned int layerIndex)
2131 auto inputs = GetInputs(graph, layerIndex);
2134 auto outputs = GetOutputs(graph, layerIndex);
2137 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ElementwiseUnaryLayer();
2138 auto fbDescriptor = fbLayer->descriptor();
2143 const std::string& layerName = GetLayerName(graph, layerIndex);
2144 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
2149 RegisterInputSlots(graph, layerIndex, layer);
2150 RegisterOutputSlots(graph, layerIndex, layer);
2153 void IDeserializer::DeserializerImpl::ParseConcat(
GraphPtr graph,
unsigned int layerIndex)
2158 auto outputs = GetOutputs(graph, layerIndex);
2161 auto layerName = GetLayerName(graph, layerIndex);
2163 unsigned int numViews = originsDescriptor->numViews();
2164 unsigned int numDimensions = originsDescriptor->numDimensions();
2167 auto inputs = GetInputs(graph, layerIndex);
2171 auto originsPtr = originsDescriptor->viewOrigins();
2172 for (
unsigned int v = 0; v < numViews; ++v)
2174 auto originPtr = originsPtr->Get(v);
2175 for (
unsigned int d = 0; d < numDimensions; ++d)
2177 uint32_t value = originPtr->data()->Get(d);
2178 descriptor.SetViewOriginCoord(v, d, value);
2181 descriptor.SetConcatAxis(originsDescriptor->concatAxis());
2183 IConnectableLayer* layer = m_Network->AddConcatLayer(descriptor, layerName.c_str());
2187 RegisterInputSlots(graph, layerIndex, layer);
2188 RegisterOutputSlots(graph, layerIndex, layer);
2191 void IDeserializer::DeserializerImpl::ParseMultiplication(
GraphPtr graph,
unsigned int layerIndex)
2194 auto inputs = GetInputs(graph, layerIndex);
2198 auto outputs = GetOutputs(graph, layerIndex);
2201 auto layerName = GetLayerName(graph, layerIndex);
2203 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(descriptor, layerName.c_str());
2208 RegisterInputSlots(graph, layerIndex, layer);
2209 RegisterOutputSlots(graph, layerIndex, layer);
2212 void IDeserializer::DeserializerImpl::ParseFloor(
GraphPtr graph,
unsigned int layerIndex)
2217 auto inputs = GetInputs(graph, layerIndex);
2220 auto outputs = GetOutputs(graph, layerIndex);
2223 auto layerName = GetLayerName(graph, layerIndex);
2227 layer = m_Network->AddFloorLayer(layerName.c_str());
2232 RegisterInputSlots(graph, layerIndex, layer);
2233 RegisterOutputSlots(graph, layerIndex, layer);
2236 void IDeserializer::DeserializerImpl::ParseFullyConnected(
GraphPtr graph,
unsigned int layerIndex)
2239 auto inputs = GetInputs(graph, layerIndex);
2242 auto outputs = GetOutputs(graph, layerIndex);
2245 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer();
2246 auto layerName = GetLayerName(graph, layerIndex);
2247 auto flatBufferDescriptor = flatBufferLayer->descriptor();
2250 fullyConnectedDescriptor.
m_BiasEnabled = flatBufferDescriptor->biasEnabled();
2252 fullyConnectedDescriptor.
m_ConstantWeights = flatBufferDescriptor->constantWeights();
2255 std::vector<unsigned int> ignoreSlots {};
2259 if (this->GetFeatureVersions(graph).m_ConstTensorsAsInputs <= 0)
2264 layer = m_Network->AddFullyConnectedLayer(fullyConnectedDescriptor,
2268 auto weightsLayer = m_Network->AddConstantLayer(weightsTensor);
2269 weightsLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(1u));
2270 weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsTensor.
GetInfo());
2271 ignoreSlots.emplace_back(1u);
2276 auto biasLayer = m_Network->AddConstantLayer(biasTensor);
2277 biasLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(2u));
2278 biasLayer->GetOutputSlot(0).SetTensorInfo(biasTensor.
GetInfo());
2279 ignoreSlots.emplace_back(2u);
2284 layer = m_Network->AddFullyConnectedLayer(fullyConnectedDescriptor,
2286 uint32_t numInputs = fullyConnectedDescriptor.
GetNumInputs();
2293 RegisterInputSlots(graph, layerIndex, layer, ignoreSlots);
2294 RegisterOutputSlots(graph, layerIndex, layer);
2297 void IDeserializer::DeserializerImpl::ParsePad(
GraphPtr graph,
unsigned int layerIndex)
2307 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_PadLayer()->descriptor();
2308 auto flatBufferPadList = flatBufferDescriptor->padList();
2309 auto paddingMode = flatBufferDescriptor->paddingMode();
2310 float padValue = flatBufferDescriptor->padValue();
2312 if (flatBufferPadList->size() % 2 != 0)
2314 throw ParseException(fmt::format(
"The size of the pad list must be divisible by 2 {}",
2318 std::vector<std::pair<unsigned int, unsigned int>> padList;
2319 padList.reserve(flatBufferPadList->size() / 2);
2320 for (
unsigned int i = 0; i < flatBufferPadList->size() - 1; i += 2)
2322 padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1));
2327 auto layerName = GetLayerName(graph, layerIndex);
2328 IConnectableLayer* layer = m_Network->AddPadLayer(descriptor, layerName.c_str());
2333 RegisterInputSlots(graph, layerIndex, layer);
2334 RegisterOutputSlots(graph, layerIndex, layer);
2337 void IDeserializer::DeserializerImpl::ParsePermute(
GraphPtr graph,
unsigned int layerIndex)
2342 graph->layers()->Get(layerIndex)->layer_as_PermuteLayer()->descriptor()->dimMappings();
2344 auto inputs = GetInputs(graph, layerIndex);
2347 auto outputs = GetOutputs(graph, layerIndex);
2351 auto layerName = GetLayerName(graph, layerIndex);
2354 IConnectableLayer* layer = m_Network->AddPermuteLayer(descriptor, layerName.c_str());
2357 RegisterInputSlots(graph, layerIndex, layer);
2358 RegisterOutputSlots(graph, layerIndex, layer);
2362 unsigned int layerIndex)
2367 switch (pooling2dDesc->poolType())
2369 case PoolingAlgorithm_Average:
2374 case PoolingAlgorithm_Max:
2379 case PoolingAlgorithm_L2:
2390 switch (pooling2dDesc->outputShapeRounding())
2392 case OutputShapeRounding_Floor:
2397 case OutputShapeRounding_Ceiling:
2408 switch (pooling2dDesc->paddingMethod())
2410 case PaddingMethod_Exclude:
2415 case PaddingMethod_IgnoreValue:
2426 switch (pooling2dDesc->dataLayout())
2428 case DataLayout_NCHW:
2433 case DataLayout_NHWC:
2445 desc.
m_PadLeft = pooling2dDesc->padLeft();
2447 desc.
m_PadTop = pooling2dDesc->padTop();
2448 desc.
m_StrideX = pooling2dDesc->strideX();
2449 desc.
m_StrideY = pooling2dDesc->strideY();
2457 unsigned int layerIndex)
2462 switch (pooling3dDesc->poolType())
2464 case PoolingAlgorithm_Average:
2469 case PoolingAlgorithm_Max:
2474 case PoolingAlgorithm_L2:
2485 switch (pooling3dDesc->outputShapeRounding())
2487 case OutputShapeRounding_Floor:
2492 case OutputShapeRounding_Ceiling:
2503 switch (pooling3dDesc->paddingMethod())
2505 case PaddingMethod_Exclude:
2510 case PaddingMethod_IgnoreValue:
2521 switch (pooling3dDesc->dataLayout())
2523 case DataLayout_NCDHW:
2528 case DataLayout_NDHWC:
2540 desc.
m_PadLeft = pooling3dDesc->padLeft();
2542 desc.
m_PadTop = pooling3dDesc->padTop();
2544 desc.
m_PadBack = pooling3dDesc->padBack();
2545 desc.
m_StrideX = pooling3dDesc->strideX();
2546 desc.
m_StrideY = pooling3dDesc->strideY();
2547 desc.
m_StrideZ = pooling3dDesc->strideZ();
2555 void IDeserializer::DeserializerImpl::ParsePooling2d(
GraphPtr graph,
unsigned int layerIndex)
2559 auto pooling2dDes = graph->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->descriptor();
2560 auto inputs = GetInputs(graph, layerIndex);
2563 auto outputs = GetOutputs(graph, layerIndex);
2567 auto pooling2dDescriptor = GetPooling2dDescriptor(pooling2dDes, layerIndex);
2568 auto layerName = GetLayerName(graph, layerIndex);
2569 IConnectableLayer* layer = m_Network->AddPooling2dLayer(pooling2dDescriptor, layerName.c_str());
2572 RegisterInputSlots(graph, layerIndex, layer);
2573 RegisterOutputSlots(graph, layerIndex, layer);
2576 void IDeserializer::DeserializerImpl::ParsePooling3d(
GraphPtr graph,
unsigned int layerIndex)
2580 auto pooling3dDes = graph->layers()->Get(layerIndex)->layer_as_Pooling3dLayer()->descriptor();
2581 auto inputs = GetInputs(graph, layerIndex);
2584 auto outputs = GetOutputs(graph, layerIndex);
2588 auto pooling3dDescriptor = GetPooling3dDescriptor(pooling3dDes, layerIndex);
2589 auto layerName = GetLayerName(graph, layerIndex);
2590 IConnectableLayer* layer = m_Network->AddPooling3dLayer(pooling3dDescriptor, layerName.c_str());
2593 RegisterInputSlots(graph, layerIndex, layer);
2594 RegisterOutputSlots(graph, layerIndex, layer);
2597 void IDeserializer::DeserializerImpl::ParseQuantize(
GraphPtr graph,
unsigned int layerIndex)
2601 auto inputs = GetInputs(graph, layerIndex);
2604 auto outputs = GetOutputs(graph, layerIndex);
2608 auto layerName = GetLayerName(graph, layerIndex);
2612 RegisterInputSlots(graph, layerIndex, layer);
2613 RegisterOutputSlots(graph, layerIndex, layer);
2617 const std::vector<uint32_t>& targetDimsIn)
2619 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2620 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2622 if (stretchDim != targetDimsIn.end())
2624 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2626 throw ParseException(fmt::format(
"At most one component of shape can be -1 {}",
2630 auto targetNumElements =
2631 armnn::numeric_cast<unsigned int>(
2632 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2634 auto stretchIndex =
static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2635 if (targetNumElements == 0)
2639 outputDims[stretchIndex] = 0;
2644 fmt::format(
"Input to reshape is a tensor with elements, but the requested shape has 0. {}",
2650 outputDims[stretchIndex] = inputTensorInfo.
GetNumElements() / targetNumElements;
2662 void IDeserializer::DeserializerImpl::ParseRank(
GraphPtr graph,
unsigned int layerIndex)
2672 auto layerName = GetLayerName(graph, layerIndex);
2678 RegisterInputSlots(graph, layerIndex, layer);
2679 RegisterOutputSlots(graph, layerIndex, layer);
2682 void IDeserializer::DeserializerImpl::ParseReduce(
GraphPtr graph,
unsigned int layerIndex)
2687 auto inputs = GetInputs(graph, layerIndex);
2690 auto outputs = GetOutputs(graph, layerIndex);
2693 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ReduceLayer();
2694 auto fbDescriptor = fbLayer->descriptor();
2695 auto flatBufferAxis = fbDescriptor->axis();
2698 descriptor.
m_KeepDims = fbDescriptor->keepDims();
2699 descriptor.
m_vAxis = std::vector<unsigned int>(flatBufferAxis->begin(), flatBufferAxis->end());
2702 const std::string& layerName = GetLayerName(graph, layerIndex);
2703 IConnectableLayer* layer = m_Network->AddReduceLayer(descriptor, layerName.c_str());
2708 RegisterInputSlots(graph, layerIndex, layer);
2709 RegisterOutputSlots(graph, layerIndex, layer);
2712 void IDeserializer::DeserializerImpl::ParseReshape(
GraphPtr graph,
unsigned int layerIndex)
2715 auto inputs = GetInputs(graph, layerIndex);
2717 auto outputs = GetOutputs(graph, layerIndex);
2723 const auto targetDims = graph->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->descriptor()->targetShape();
2724 std::vector<uint32_t> outputDims(targetDims->begin(), targetDims->begin() + targetDims->size());
2729 const std::vector<uint32_t> expectedDims(outputs[0]->dimensions()->begin(),
2730 outputs[0]->dimensions()->begin() + outputs[0]->dimensions()->size());
2732 if (inputs.size() > 1 && !
CheckShape(reshapeOutputTensorShape, expectedDims))
2734 std::stringstream ss;
2735 ss <<
"New shape defined in reshape parameters "
2736 << reshapeOutputTensorShape
2737 <<
" does not equal output shape "
2738 << actualOutputTensorInfo.
GetShape()
2747 auto layerName = GetLayerName(graph, layerIndex);
2748 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2751 RegisterInputSlots(graph, layerIndex, layer);
2752 RegisterOutputSlots(graph, layerIndex, layer);
2755 void IDeserializer::DeserializerImpl::ParseResize(
GraphPtr graph,
unsigned int layerIndex)
2765 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_ResizeLayer()->descriptor();
2768 descriptor.
m_TargetWidth = flatBufferDescriptor->targetWidth();
2769 descriptor.
m_TargetHeight = flatBufferDescriptor->targetHeight();
2772 descriptor.
m_AlignCorners = flatBufferDescriptor->alignCorners();
2775 auto layerName = GetLayerName(graph, layerIndex);
2776 IConnectableLayer* layer = m_Network->AddResizeLayer(descriptor, layerName.c_str());
2781 RegisterInputSlots(graph, layerIndex, layer);
2782 RegisterOutputSlots(graph, layerIndex, layer);
2785 void IDeserializer::DeserializerImpl::ParseReverseV2(
GraphPtr graph,
unsigned int layerIndex)
2795 auto layerName = GetLayerName(graph, layerIndex);
2801 RegisterInputSlots(graph, layerIndex, layer);
2802 RegisterOutputSlots(graph, layerIndex, layer);
2807 void IDeserializer::DeserializerImpl::ParseResizeBilinear(
GraphPtr graph,
unsigned int layerIndex)
2817 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_ResizeBilinearLayer()->descriptor();
2820 descriptor.
m_TargetWidth = flatBufferDescriptor->targetWidth();
2821 descriptor.
m_TargetHeight = flatBufferDescriptor->targetHeight();
2824 descriptor.
m_AlignCorners = flatBufferDescriptor->alignCorners();
2827 auto layerName = GetLayerName(graph, layerIndex);
2828 IConnectableLayer* layer = m_Network->AddResizeLayer(descriptor, layerName.c_str());
2833 RegisterInputSlots(graph, layerIndex, layer);
2834 RegisterOutputSlots(graph, layerIndex, layer);
2837 void IDeserializer::DeserializerImpl::ParseShape(
GraphPtr graph,
unsigned int layerIndex)
2847 auto layerName = GetLayerName(graph, layerIndex);
2853 RegisterInputSlots(graph, layerIndex, layer);
2854 RegisterOutputSlots(graph, layerIndex, layer);
2857 void IDeserializer::DeserializerImpl::ParseSoftmax(
GraphPtr graph,
unsigned int layerIndex)
2868 descriptor.
m_Beta = graph->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->descriptor()->beta();
2869 descriptor.
m_Axis = graph->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->descriptor()->axis();
2870 auto layerName = GetLayerName(graph, layerIndex);
2872 IConnectableLayer* layer = m_Network->AddSoftmaxLayer(descriptor, layerName.c_str());
2877 RegisterInputSlots(graph, layerIndex, layer);
2878 RegisterOutputSlots(graph, layerIndex, layer);
2881 void IDeserializer::DeserializerImpl::ParseSpaceToBatchNd(
GraphPtr graph,
unsigned int layerIndex)
2891 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->descriptor();
2892 auto flatBufferPadList = flatBufferDescriptor->padList();
2893 auto flatBufferBlockShape = flatBufferDescriptor->blockShape();
2895 if (flatBufferPadList->size() % 2 != 0)
2897 throw ParseException(fmt::format(
"The size of the pad list must be divisible by 2 {}",
2901 std::vector<std::pair<unsigned int, unsigned int>> padList;
2902 padList.reserve(flatBufferPadList->size() / 2);
2903 for (
unsigned int i = 0; i < flatBufferPadList->size() - 1; i += 2)
2905 padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1));
2911 std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end());
2914 auto layerName = GetLayerName(graph, layerIndex);
2915 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(descriptor, layerName.c_str());
2920 RegisterInputSlots(graph, layerIndex, layer);
2921 RegisterOutputSlots(graph, layerIndex, layer);
2924 void IDeserializer::DeserializerImpl::ParseSpaceToDepth(
GraphPtr graph,
unsigned int layerIndex)
2934 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->descriptor();
2937 descriptor.
m_BlockSize = flatBufferDescriptor->blockSize();
2940 auto layerName = GetLayerName(graph, layerIndex);
2941 IConnectableLayer* layer = m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str());
2946 RegisterInputSlots(graph, layerIndex, layer);
2947 RegisterOutputSlots(graph, layerIndex, layer);
2952 unsigned int layerIndex)
2957 switch (normalizationDescriptor->normChannelType())
2959 case NormalizationAlgorithmChannel_Across:
2964 case NormalizationAlgorithmChannel_Within:
2975 switch (normalizationDescriptor->normMethodType())
2977 case NormalizationAlgorithmMethod_LocalBrightness:
2982 case NormalizationAlgorithmMethod_LocalContrast:
2993 switch (normalizationDescriptor->dataLayout())
2995 case DataLayout_NCHW:
3000 case DataLayout_NHWC:
3011 desc.
m_Alpha = normalizationDescriptor->alpha();
3012 desc.
m_Beta = normalizationDescriptor->beta();
3013 desc.
m_K = normalizationDescriptor->k();
3014 desc.
m_NormSize = normalizationDescriptor->normSize();
3019 void IDeserializer::DeserializerImpl::ParseNormalization(
GraphPtr graph,
unsigned int layerIndex)
3023 auto normalizationDes = graph->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->descriptor();
3033 auto normalizationDescriptor = GetNormalizationDescriptor(normalizationDes, layerIndex);
3034 auto layerName = GetLayerName(graph, layerIndex);
3036 IConnectableLayer* layer = m_Network->AddNormalizationLayer(normalizationDescriptor, layerName.c_str());
3039 RegisterInputSlots(graph, layerIndex, layer);
3040 RegisterOutputSlots(graph, layerIndex, layer);
3043 void IDeserializer::DeserializerImpl::ParseRsqrt(
GraphPtr graph,
unsigned int layerIndex)
3046 auto inputs = GetInputs(graph, layerIndex);
3050 auto outputs = GetOutputs(graph, layerIndex);
3053 auto layerName = GetLayerName(graph, layerIndex);
3056 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
3060 RegisterInputSlots(graph, layerIndex, layer);
3061 RegisterOutputSlots(graph, layerIndex, layer);
3064 void IDeserializer::DeserializerImpl::ParseSlice(
GraphPtr graph,
unsigned int layerIndex)
3068 auto inputs = GetInputs(graph, layerIndex);
3071 auto outputs = GetOutputs(graph, layerIndex);
3074 auto fbDescriptor = graph->layers()->Get(layerIndex)->layer_as_SliceLayer()->descriptor();
3076 auto fbBegin = fbDescriptor->begin();
3077 auto fbSize = fbDescriptor->size();
3079 if (fbBegin->size() != fbSize->size())
3081 throw ParseException(fmt::format(
"Begin and size descriptors must have the same length {}",
3086 descriptor.
m_Begin.insert(descriptor.
m_Begin.end(), fbBegin->begin(), fbBegin->end());
3087 descriptor.
m_Size.insert(descriptor.
m_Size.end(), fbSize->begin(), fbSize->end());
3089 auto layerName = GetLayerName(graph, layerIndex);
3090 IConnectableLayer* layer = m_Network->AddSliceLayer(descriptor, layerName.c_str());
3095 RegisterInputSlots(graph, layerIndex, layer);
3096 RegisterOutputSlots(graph, layerIndex, layer);
3099 void IDeserializer::DeserializerImpl::ParseStridedSlice(
GraphPtr graph,
unsigned int layerIndex)
3109 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_StridedSliceLayer()->descriptor();
3111 auto flatBufferBegin = flatBufferDescriptor->begin();
3112 auto flatBufferEnd = flatBufferDescriptor->end();
3113 auto flatBufferStride = flatBufferDescriptor->stride();
3115 if (!(flatBufferBegin->size() == flatBufferEnd->size() &&
3116 flatBufferBegin->size() == flatBufferStride->size()))
3118 throw ParseException(fmt::format(
"The size of the begin, end, and stride must be equal {}",
3122 std::vector<int> begin(flatBufferBegin->begin(), flatBufferBegin->end());
3123 std::vector<int> end(flatBufferEnd->begin(), flatBufferEnd->end());
3124 std::vector<int> stride(flatBufferStride->begin(), flatBufferStride->end());
3127 descriptor.m_BeginMask = flatBufferDescriptor->beginMask();
3128 descriptor.m_EndMask = flatBufferDescriptor->endMask();
3129 descriptor.m_ShrinkAxisMask = flatBufferDescriptor->shrinkAxisMask();
3130 descriptor.m_EllipsisMask = flatBufferDescriptor->ellipsisMask();
3131 descriptor.m_NewAxisMask = flatBufferDescriptor->newAxisMask();
3132 descriptor.m_DataLayout =
ToDataLayout(flatBufferDescriptor->dataLayout());
3134 auto layerName = GetLayerName(graph, layerIndex);
3135 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(descriptor, layerName.c_str());
3140 RegisterInputSlots(graph, layerIndex, layer);
3141 RegisterOutputSlots(graph, layerIndex, layer);
3144 void IDeserializer::DeserializerImpl::ParseSubtraction(
GraphPtr graph,
unsigned int layerIndex)
3147 auto inputs = GetInputs(graph, layerIndex);
3151 auto outputs = GetOutputs(graph, layerIndex);
3154 auto layerName = GetLayerName(graph, layerIndex);
3156 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(descriptor, layerName.c_str());
3161 RegisterInputSlots(graph, layerIndex, layer);
3162 RegisterOutputSlots(graph, layerIndex, layer);
3165 void IDeserializer::DeserializerImpl::ParseGather(
GraphPtr graph,
unsigned int layerIndex)
3176 descriptor.
m_Axis = graph->layers()->Get(layerIndex)->layer_as_GatherLayer()->descriptor()->axis();
3178 auto layerName = GetLayerName(graph, layerIndex);
3179 IConnectableLayer* layer = m_Network->AddGatherLayer(descriptor, layerName.c_str());
3184 RegisterInputSlots(graph, layerIndex, layer);
3185 RegisterOutputSlots(graph, layerIndex, layer);
3188 void IDeserializer::DeserializerImpl::ParseGatherNd(
GraphPtr graph,
unsigned int layerIndex)
3198 auto layerName = GetLayerName(graph, layerIndex);
3204 RegisterInputSlots(graph, layerIndex, layer);
3205 RegisterOutputSlots(graph, layerIndex, layer);
3208 void IDeserializer::DeserializerImpl::ParseMean(
GraphPtr graph,
unsigned int layerIndex)
3218 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_MeanLayer()->descriptor();
3219 auto flatBufferAxis = flatBufferDescriptor->axis();
3220 auto flatBufferKeepDims = flatBufferDescriptor->keepDims();
3223 descriptor.
m_Axis = std::vector<unsigned int>(flatBufferAxis->begin(), flatBufferAxis->end());
3226 auto layerName = GetLayerName(graph, layerIndex);
3227 IConnectableLayer* layer = m_Network->AddMeanLayer(descriptor, layerName.c_str());
3232 RegisterInputSlots(graph, layerIndex, layer);
3233 RegisterOutputSlots(graph, layerIndex, layer);
3236 void IDeserializer::DeserializerImpl::ParseSplitter(
GraphPtr graph,
unsigned int layerIndex)
3245 auto flatBufferViewsDescriptor = graph->layers()->Get(layerIndex)->layer_as_SplitterLayer()->descriptor();
3246 auto flatBufferViewSizes = flatBufferViewsDescriptor->viewSizes();
3247 auto flatBufferOriginsDescriptor = flatBufferViewsDescriptor->origins();
3248 auto flatBufferViewOrigins = flatBufferOriginsDescriptor->viewOrigins();
3249 uint32_t numViews = flatBufferOriginsDescriptor->numViews();
3250 uint32_t numDimensions = flatBufferOriginsDescriptor->numDimensions();
3257 for(
unsigned int vIdx = 0; vIdx < numViews; ++vIdx)
3259 for (
unsigned int dIdx = 0; dIdx < numDimensions; ++dIdx)
3261 viewsDescriptor.SetViewSize(vIdx, dIdx, flatBufferViewSizes->Get(vIdx)->data()->Get(dIdx));
3262 viewsDescriptor.SetViewOriginCoord(vIdx, dIdx, flatBufferViewOrigins->Get(vIdx)->data()->Get(dIdx));
3266 if (flatBufferViewsDescriptor->hasAxis())
3268 viewsDescriptor.SetAxis(flatBufferViewsDescriptor->axis());
3271 auto layerName = GetLayerName(graph, layerIndex);
3272 IConnectableLayer* layer = m_Network->AddSplitterLayer(viewsDescriptor, layerName.c_str());
3275 for(
unsigned int vIdx = 0; vIdx < numViews; ++vIdx)
3281 RegisterInputSlots(graph, layerIndex, layer);
3282 RegisterOutputSlots(graph, layerIndex, layer);
3300 void IDeserializer::DeserializerImpl::ParseLstm(
GraphPtr graph,
unsigned int layerIndex)
3304 auto inputs = GetInputs(graph, layerIndex);
3307 auto outputs = GetOutputs(graph, layerIndex);
3310 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_LstmLayer();
3311 auto layerName = GetLayerName(graph, layerIndex);
3312 auto flatBufferDescriptor = flatBufferLayer->descriptor();
3313 auto flatBufferInputParams = flatBufferLayer->inputParams();
3315 auto lstmDescriptor = GetLstmDescriptor(flatBufferDescriptor);
3343 if (!lstmDescriptor.m_CifgEnabled)
3345 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
3346 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
3347 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
3348 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
3358 if (lstmDescriptor.m_ProjectionEnabled)
3360 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
3361 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
3369 if (lstmDescriptor.m_PeepholeEnabled)
3371 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
3372 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
3382 if (lstmDescriptor.m_LayerNormEnabled)
3384 if (!lstmDescriptor.m_CifgEnabled)
3386 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
3389 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
3390 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
3391 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
3398 IConnectableLayer* layer = m_Network->AddLstmLayer(lstmDescriptor, lstmInputParams, layerName.c_str());
3412 RegisterInputSlots(graph, layerIndex, layer);
3413 RegisterOutputSlots(graph, layerIndex, layer);
3425 desc.
m_CellClip = qLstmDescriptor->cellClip();
3439 void IDeserializer::DeserializerImpl::ParseQLstm(
GraphPtr graph,
unsigned int layerIndex)
3443 auto inputs = GetInputs(graph, layerIndex);
3446 auto outputs = GetOutputs(graph, layerIndex);
3449 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_QLstmLayer();
3450 auto layerName = GetLayerName(graph, layerIndex);
3451 auto flatBufferDescriptor = flatBufferLayer->descriptor();
3452 auto flatBufferInputParams = flatBufferLayer->inputParams();
3454 auto qLstmDescriptor = GetQLstmDescriptor(flatBufferDescriptor);
3483 if (!qLstmDescriptor.m_CifgEnabled)
3485 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
3486 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
3487 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
3498 if (qLstmDescriptor.m_ProjectionEnabled)
3500 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
3501 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
3512 if (qLstmDescriptor.m_PeepholeEnabled)
3514 if (!qLstmDescriptor.m_CifgEnabled)
3516 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
3520 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
3521 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
3533 if (qLstmDescriptor.m_LayerNormEnabled)
3535 if (!qLstmDescriptor.m_CifgEnabled)
3537 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
3541 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
3542 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
3543 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
3550 IConnectableLayer* layer = m_Network->AddQLstmLayer(qLstmDescriptor, qLstmInputParams, layerName.c_str());
3561 RegisterInputSlots(graph, layerIndex, layer);
3562 RegisterOutputSlots(graph, layerIndex, layer);
3565 void IDeserializer::DeserializerImpl::ParseQuantizedLstm(
GraphPtr graph,
unsigned int layerIndex)
3569 auto inputs = GetInputs(graph, layerIndex);
3572 auto outputs = GetOutputs(graph, layerIndex);
3575 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_QuantizedLstmLayer();
3576 auto layerName = GetLayerName(graph, layerIndex);
3577 auto flatBufferInputParams = flatBufferLayer->inputParams();
3607 IConnectableLayer* layer = m_Network->AddQuantizedLstmLayer(lstmInputParams, layerName.c_str());
3615 RegisterInputSlots(graph, layerIndex, layer);
3616 RegisterOutputSlots(graph, layerIndex, layer);
3619 void IDeserializer::DeserializerImpl::ParseDequantize(
GraphPtr graph,
unsigned int layerIndex)
3629 const std::string layerName = GetLayerName(graph, layerIndex);
3635 RegisterInputSlots(graph, layerIndex, layer);
3636 RegisterOutputSlots(graph, layerIndex, layer);
3639 void IDeserializer::DeserializerImpl::ParseMerge(
GraphPtr graph,
unsigned int layerIndex)
3649 const std::string layerName = GetLayerName(graph, layerIndex);
3655 RegisterInputSlots(graph, layerIndex, layer);
3656 RegisterOutputSlots(graph, layerIndex, layer);
3659 void IDeserializer::DeserializerImpl::ParseSwitch(
GraphPtr graph,
unsigned int layerIndex)
3662 auto inputs = GetInputs(graph, layerIndex);
3666 auto outputs = GetOutputs(graph, layerIndex);
3669 auto layerName = GetLayerName(graph, layerIndex);
3678 RegisterInputSlots(graph, layerIndex, layer);
3679 RegisterOutputSlots(graph, layerIndex, layer);
3682 void IDeserializer::DeserializerImpl::ParseTile(
GraphPtr graph,
unsigned int layerIndex)
3685 auto inputs = GetInputs(graph, layerIndex);
3689 auto outputs = GetOutputs(graph, layerIndex);
3692 auto TileLayer = graph->layers()->Get(layerIndex)->layer_as_TileLayer();
3693 auto layerName = GetLayerName(graph, layerIndex);
3694 auto flatBufferDescriptor =
TileLayer->descriptor();
3695 auto flatBufferMultiples = flatBufferDescriptor->m_Multiples();
3698 tileDescriptor.
m_Multiples = std::vector<unsigned int>(flatBufferMultiples->begin(), flatBufferMultiples->end());
3700 IConnectableLayer* layer = m_Network->AddTileLayer(tileDescriptor, layerName.c_str());
3705 RegisterInputSlots(graph, layerIndex, layer);
3706 RegisterOutputSlots(graph, layerIndex, layer);
3709 void IDeserializer::DeserializerImpl::ParsePrelu(
GraphPtr graph,
unsigned int layerIndex)
3712 auto inputs = GetInputs(graph, layerIndex);
3716 auto outputs = GetOutputs(graph, layerIndex);
3719 auto layerName = GetLayerName(graph, layerIndex);
3725 RegisterInputSlots(graph, layerIndex, layer);
3726 RegisterOutputSlots(graph, layerIndex, layer);
3729 void IDeserializer::DeserializerImpl::ParseTranspose(
GraphPtr graph,
unsigned int layerIndex)
3733 auto dimsMapping = graph->layers()->Get(layerIndex)->layer_as_TransposeLayer()->descriptor()->dimMappings();
3735 auto inputs = GetInputs(graph, layerIndex);
3738 auto outputs = GetOutputs(graph, layerIndex);
3742 auto layerName = GetLayerName(graph, layerIndex);
3745 IConnectableLayer* layer = m_Network->AddTransposeLayer(descriptor, layerName.c_str());
3748 RegisterInputSlots(graph, layerIndex, layer);
3749 RegisterOutputSlots(graph, layerIndex, layer);
3752 void IDeserializer::DeserializerImpl::ParseTransposeConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
3756 auto inputs = GetInputs(graph, layerIndex);
3759 auto outputs = GetOutputs(graph, layerIndex);
3762 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_TransposeConvolution2dLayer();
3763 auto layerName = GetLayerName(graph, layerIndex);
3764 auto serializerDescriptor = serializerLayer->descriptor();
3767 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
3768 descriptor.
m_PadRight = serializerDescriptor->padRight();
3769 descriptor.
m_PadTop = serializerDescriptor->padTop();
3770 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
3771 descriptor.
m_StrideX = serializerDescriptor->strideX();
3772 descriptor.
m_StrideY = serializerDescriptor->strideY();;
3773 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();;
3782 optionalBiases = armnn::MakeOptional<armnn::ConstTensor>(biases);
3785 IConnectableLayer* layer = m_Network->AddTransposeConvolution2dLayer(descriptor,
3793 RegisterInputSlots(graph, layerIndex, layer);
3794 RegisterOutputSlots(graph, layerIndex, layer);
3797 void IDeserializer::DeserializerImpl::ParseStack(
GraphPtr graph,
unsigned int layerIndex)
3800 auto inputs = GetInputs(graph, layerIndex);
3802 auto outputs = GetOutputs(graph, layerIndex);
3805 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_StackLayer()->descriptor();
3806 unsigned int axis = flatBufferDescriptor->axis();
3807 unsigned int numInputs = flatBufferDescriptor->numInputs();
3810 auto flatBufferInputShape = flatBufferDescriptor->inputShape();
3811 std::vector<uint32_t> vectorInputShape(flatBufferInputShape->begin(),
3812 flatBufferInputShape->begin() + flatBufferInputShape->size());
3814 TensorShape inputShape(
static_cast<unsigned int>(vectorInputShape.size()), vectorInputShape.data());
3817 for (
unsigned int i=0; i<inputs.size(); ++i)
3820 if (descriptor.m_InputShape != inputShape)
3822 std::stringstream ss;
3823 ss <<
"Shape of input "
3827 <<
" does not equal defined input shape "
3828 << descriptor.m_InputShape
3835 auto layerName = GetLayerName(graph, layerIndex);
3836 IConnectableLayer* layer = m_Network->AddStackLayer(descriptor, layerName.c_str());
3841 RegisterInputSlots(graph, layerIndex, layer);
3842 RegisterOutputSlots(graph, layerIndex, layer);
3845 void IDeserializer::DeserializerImpl::ParseStandIn(
GraphPtr graph,
unsigned int layerIndex)
3849 auto inputs = GetInputs(graph, layerIndex);
3850 auto outputs = GetOutputs(graph, layerIndex);
3852 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_StandInLayer();
3853 auto fbDescriptor = fbLayer->descriptor();
3856 descriptor.
m_NumInputs = fbDescriptor->numInputs();
3862 const std::string layerName = GetLayerName(graph, layerIndex);
3865 for (
unsigned int i = 0u; i < descriptor.
m_NumOutputs; ++i)
3871 RegisterInputSlots(graph, layerIndex, layer);
3872 RegisterOutputSlots(graph, layerIndex, layer);
3892 void IDeserializer::DeserializerImpl::ParseUnidirectionalSequenceLstm(
GraphPtr graph,
unsigned int layerIndex)
3896 auto inputs = GetInputs(graph, layerIndex);
3899 auto outputs = GetOutputs(graph, layerIndex);
3902 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_UnidirectionalSequenceLstmLayer();
3903 auto layerName = GetLayerName(graph, layerIndex);
3904 auto flatBufferDescriptor = flatBufferLayer->descriptor();
3905 auto flatBufferInputParams = flatBufferLayer->inputParams();
3907 auto descriptor = GetUnidirectionalSequenceLstmDescriptor(flatBufferDescriptor);
3935 if (!descriptor.m_CifgEnabled)
3937 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
3938 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
3939 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
3945 if (descriptor.m_PeepholeEnabled)
3947 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
3954 if (descriptor.m_ProjectionEnabled)
3956 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
3957 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
3965 if (descriptor.m_PeepholeEnabled)
3967 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
3968 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
3978 if (descriptor.m_LayerNormEnabled)
3980 if (!descriptor.m_CifgEnabled)
3982 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
3985 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
3986 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
3987 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
3994 IConnectableLayer* layer = m_Network->AddUnidirectionalSequenceLstmLayer(descriptor,
4007 RegisterInputSlots(graph, layerIndex, layer);
4008 RegisterOutputSlots(graph, layerIndex, layer);