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_ElementwiseUnaryLayer] = &DeserializerImpl::ParseElementwiseUnary;
233 m_ParserFunctions[Layer_EqualLayer] = &DeserializerImpl::ParseEqual;
234 m_ParserFunctions[Layer_FullyConnectedLayer] = &DeserializerImpl::ParseFullyConnected;
235 m_ParserFunctions[Layer_FillLayer] = &DeserializerImpl::ParseFill;
236 m_ParserFunctions[Layer_FloorLayer] = &DeserializerImpl::ParseFloor;
237 m_ParserFunctions[Layer_GatherLayer] = &DeserializerImpl::ParseGather;
238 m_ParserFunctions[Layer_GatherNdLayer] = &DeserializerImpl::ParseGatherNd;
239 m_ParserFunctions[Layer_GreaterLayer] = &DeserializerImpl::ParseGreater;
240 m_ParserFunctions[Layer_InstanceNormalizationLayer] = &DeserializerImpl::ParseInstanceNormalization;
241 m_ParserFunctions[Layer_L2NormalizationLayer] = &DeserializerImpl::ParseL2Normalization;
242 m_ParserFunctions[Layer_LogicalBinaryLayer] = &DeserializerImpl::ParseLogicalBinary;
243 m_ParserFunctions[Layer_LogSoftmaxLayer] = &DeserializerImpl::ParseLogSoftmax;
244 m_ParserFunctions[Layer_LstmLayer] = &DeserializerImpl::ParseLstm;
245 m_ParserFunctions[Layer_MaximumLayer] = &DeserializerImpl::ParseMaximum;
246 m_ParserFunctions[Layer_MeanLayer] = &DeserializerImpl::ParseMean;
247 m_ParserFunctions[Layer_MinimumLayer] = &DeserializerImpl::ParseMinimum;
248 m_ParserFunctions[Layer_MergeLayer] = &DeserializerImpl::ParseMerge;
249 m_ParserFunctions[Layer_MergerLayer] = &DeserializerImpl::ParseConcat;
250 m_ParserFunctions[Layer_MultiplicationLayer] = &DeserializerImpl::ParseMultiplication;
251 m_ParserFunctions[Layer_NormalizationLayer] = &DeserializerImpl::ParseNormalization;
252 m_ParserFunctions[Layer_PadLayer] = &DeserializerImpl::ParsePad;
253 m_ParserFunctions[Layer_PermuteLayer] = &DeserializerImpl::ParsePermute;
254 m_ParserFunctions[Layer_Pooling2dLayer] = &DeserializerImpl::ParsePooling2d;
255 m_ParserFunctions[Layer_Pooling3dLayer] = &DeserializerImpl::ParsePooling3d;
256 m_ParserFunctions[Layer_PreluLayer] = &DeserializerImpl::ParsePrelu;
257 m_ParserFunctions[Layer_QLstmLayer] = &DeserializerImpl::ParseQLstm;
258 m_ParserFunctions[Layer_QuantizeLayer] = &DeserializerImpl::ParseQuantize;
259 m_ParserFunctions[Layer_QuantizedLstmLayer] = &DeserializerImpl::ParseQuantizedLstm;
260 m_ParserFunctions[Layer_RankLayer] = &DeserializerImpl::ParseRank;
261 m_ParserFunctions[Layer_ReduceLayer] = &DeserializerImpl::ParseReduce;
262 m_ParserFunctions[Layer_ReshapeLayer] = &DeserializerImpl::ParseReshape;
263 m_ParserFunctions[Layer_ResizeBilinearLayer] = &DeserializerImpl::ParseResizeBilinear;
264 m_ParserFunctions[Layer_ResizeLayer] = &DeserializerImpl::ParseResize;
265 m_ParserFunctions[Layer_RsqrtLayer] = &DeserializerImpl::ParseRsqrt;
266 m_ParserFunctions[Layer_ShapeLayer] = &DeserializerImpl::ParseShape;
267 m_ParserFunctions[Layer_SliceLayer] = &DeserializerImpl::ParseSlice;
268 m_ParserFunctions[Layer_SoftmaxLayer] = &DeserializerImpl::ParseSoftmax;
269 m_ParserFunctions[Layer_SpaceToBatchNdLayer] = &DeserializerImpl::ParseSpaceToBatchNd;
270 m_ParserFunctions[Layer_SpaceToDepthLayer] = &DeserializerImpl::ParseSpaceToDepth;
271 m_ParserFunctions[Layer_SplitterLayer] = &DeserializerImpl::ParseSplitter;
272 m_ParserFunctions[Layer_StackLayer] = &DeserializerImpl::ParseStack;
273 m_ParserFunctions[Layer_StandInLayer] = &DeserializerImpl::ParseStandIn;
274 m_ParserFunctions[Layer_StridedSliceLayer] = &DeserializerImpl::ParseStridedSlice;
275 m_ParserFunctions[Layer_SubtractionLayer] = &DeserializerImpl::ParseSubtraction;
276 m_ParserFunctions[Layer_SwitchLayer] = &DeserializerImpl::ParseSwitch;
277 m_ParserFunctions[Layer_TransposeConvolution2dLayer] = &DeserializerImpl::ParseTransposeConvolution2d;
278 m_ParserFunctions[Layer_TransposeLayer] = &DeserializerImpl::ParseTranspose;
279 m_ParserFunctions[Layer_UnidirectionalSequenceLstmLayer] = &DeserializerImpl::ParseUnidirectionalSequenceLstm;
284 auto layerType = graphPtr->layers()->Get(layerIndex)->layer_type();
288 case Layer::Layer_AbsLayer:
289 return graphPtr->layers()->Get(layerIndex)->layer_as_AbsLayer()->base();
290 case Layer::Layer_ActivationLayer:
291 return graphPtr->layers()->Get(layerIndex)->layer_as_ActivationLayer()->base();
292 case Layer::Layer_AdditionLayer:
293 return graphPtr->layers()->Get(layerIndex)->layer_as_AdditionLayer()->base();
294 case Layer::Layer_ArgMinMaxLayer:
295 return graphPtr->layers()->Get(layerIndex)->layer_as_ArgMinMaxLayer()->base();
296 case Layer::Layer_BatchMatMulLayer:
297 return graphPtr->layers()->Get(layerIndex)->layer_as_BatchMatMulLayer()->base();
298 case Layer::Layer_BatchToSpaceNdLayer:
299 return graphPtr->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->base();
300 case Layer::Layer_BatchNormalizationLayer:
301 return graphPtr->layers()->Get(layerIndex)->layer_as_BatchNormalizationLayer()->base();
302 case Layer::Layer_CastLayer:
303 return graphPtr->layers()->Get(layerIndex)->layer_as_CastLayer()->base();
304 case Layer::Layer_ChannelShuffleLayer:
305 return graphPtr->layers()->Get(layerIndex)->layer_as_ChannelShuffleLayer()->base();
306 case Layer::Layer_ComparisonLayer:
307 return graphPtr->layers()->Get(layerIndex)->layer_as_ComparisonLayer()->base();
308 case Layer::Layer_ConcatLayer:
309 return graphPtr->layers()->Get(layerIndex)->layer_as_ConcatLayer()->base();
310 case Layer::Layer_ConstantLayer:
311 return graphPtr->layers()->Get(layerIndex)->layer_as_ConstantLayer()->base();
312 case Layer::Layer_Convolution2dLayer:
313 return graphPtr->layers()->Get(layerIndex)->layer_as_Convolution2dLayer()->base();
314 case Layer::Layer_Convolution3dLayer:
315 return graphPtr->layers()->Get(layerIndex)->layer_as_Convolution3dLayer()->base();
316 case Layer::Layer_DepthToSpaceLayer:
317 return graphPtr->layers()->Get(layerIndex)->layer_as_DepthToSpaceLayer()->base();
318 case Layer::Layer_DepthwiseConvolution2dLayer:
319 return graphPtr->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer()->base();
320 case Layer::Layer_DequantizeLayer:
321 return graphPtr->layers()->Get(layerIndex)->layer_as_DequantizeLayer()->base();
322 case Layer::Layer_DetectionPostProcessLayer:
323 return graphPtr->layers()->Get(layerIndex)->layer_as_DetectionPostProcessLayer()->base();
324 case Layer::Layer_DivisionLayer:
325 return graphPtr->layers()->Get(layerIndex)->layer_as_DivisionLayer()->base();
326 case Layer::Layer_EqualLayer:
327 return graphPtr->layers()->Get(layerIndex)->layer_as_EqualLayer()->base();
328 case Layer::Layer_ElementwiseUnaryLayer:
329 return graphPtr->layers()->Get(layerIndex)->layer_as_ElementwiseUnaryLayer()->base();
330 case Layer::Layer_FullyConnectedLayer:
331 return graphPtr->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer()->base();
332 case Layer::Layer_FillLayer:
333 return graphPtr->layers()->Get(layerIndex)->layer_as_FillLayer()->base();
334 case Layer::Layer_FloorLayer:
335 return graphPtr->layers()->Get(layerIndex)->layer_as_FloorLayer()->base();
336 case Layer::Layer_GatherLayer:
337 return graphPtr->layers()->Get(layerIndex)->layer_as_GatherLayer()->base();
338 case Layer::Layer_GatherNdLayer:
339 return graphPtr->layers()->Get(layerIndex)->layer_as_GatherNdLayer()->base();
340 case Layer::Layer_GreaterLayer:
341 return graphPtr->layers()->Get(layerIndex)->layer_as_GreaterLayer()->base();
342 case Layer::Layer_InputLayer:
343 return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->base();
344 case Layer::Layer_InstanceNormalizationLayer:
345 return graphPtr->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer()->base();
346 case Layer::Layer_L2NormalizationLayer:
347 return graphPtr->layers()->Get(layerIndex)->layer_as_L2NormalizationLayer()->base();
348 case Layer::Layer_LogicalBinaryLayer:
349 return graphPtr->layers()->Get(layerIndex)->layer_as_LogicalBinaryLayer()->base();
350 case Layer::Layer_LogSoftmaxLayer:
351 return graphPtr->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->base();
352 case Layer::Layer_LstmLayer:
353 return graphPtr->layers()->Get(layerIndex)->layer_as_LstmLayer()->base();
354 case Layer::Layer_MeanLayer:
355 return graphPtr->layers()->Get(layerIndex)->layer_as_MeanLayer()->base();
356 case Layer::Layer_MinimumLayer:
357 return graphPtr->layers()->Get(layerIndex)->layer_as_MinimumLayer()->base();
358 case Layer::Layer_MaximumLayer:
359 return graphPtr->layers()->Get(layerIndex)->layer_as_MaximumLayer()->base();
360 case Layer::Layer_MergeLayer:
361 return graphPtr->layers()->Get(layerIndex)->layer_as_MergeLayer()->base();
362 case Layer::Layer_MergerLayer:
363 return graphPtr->layers()->Get(layerIndex)->layer_as_MergerLayer()->base();
364 case Layer::Layer_MultiplicationLayer:
365 return graphPtr->layers()->Get(layerIndex)->layer_as_MultiplicationLayer()->base();
366 case Layer::Layer_NormalizationLayer:
367 return graphPtr->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->base();
368 case Layer::Layer_OutputLayer:
369 return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->base();
370 case Layer::Layer_PadLayer:
371 return graphPtr->layers()->Get(layerIndex)->layer_as_PadLayer()->base();
372 case Layer::Layer_PermuteLayer:
373 return graphPtr->layers()->Get(layerIndex)->layer_as_PermuteLayer()->base();
374 case Layer::Layer_Pooling2dLayer:
375 return graphPtr->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->base();
376 case Layer::Layer_Pooling3dLayer:
377 return graphPtr->layers()->Get(layerIndex)->layer_as_Pooling3dLayer()->base();
378 case Layer::Layer_PreluLayer:
379 return graphPtr->layers()->Get(layerIndex)->layer_as_PreluLayer()->base();
380 case Layer::Layer_QLstmLayer:
381 return graphPtr->layers()->Get(layerIndex)->layer_as_QLstmLayer()->base();
382 case Layer::Layer_QuantizeLayer:
383 return graphPtr->layers()->Get(layerIndex)->layer_as_QuantizeLayer()->base();
384 case Layer::Layer_QuantizedLstmLayer:
385 return graphPtr->layers()->Get(layerIndex)->layer_as_QuantizedLstmLayer()->base();
386 case Layer::Layer_RankLayer:
387 return graphPtr->layers()->Get(layerIndex)->layer_as_RankLayer()->base();
388 case Layer::Layer_ReduceLayer:
389 return graphPtr->layers()->Get(layerIndex)->layer_as_ReduceLayer()->base();
390 case Layer::Layer_ReshapeLayer:
391 return graphPtr->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->base();
392 case Layer::Layer_ResizeBilinearLayer:
393 return graphPtr->layers()->Get(layerIndex)->layer_as_ResizeBilinearLayer()->base();
394 case Layer::Layer_ResizeLayer:
395 return graphPtr->layers()->Get(layerIndex)->layer_as_ResizeLayer()->base();
396 case Layer::Layer_RsqrtLayer:
397 return graphPtr->layers()->Get(layerIndex)->layer_as_RsqrtLayer()->base();
398 case Layer::Layer_ShapeLayer:
399 return graphPtr->layers()->Get(layerIndex)->layer_as_ShapeLayer()->base();
400 case Layer::Layer_SliceLayer:
401 return graphPtr->layers()->Get(layerIndex)->layer_as_SliceLayer()->base();
402 case Layer::Layer_SoftmaxLayer:
403 return graphPtr->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->base();
404 case Layer::Layer_SpaceToBatchNdLayer:
405 return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->base();
406 case Layer::Layer_SpaceToDepthLayer:
407 return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->base();
408 case Layer::Layer_SplitterLayer:
409 return graphPtr->layers()->Get(layerIndex)->layer_as_SplitterLayer()->base();
410 case Layer::Layer_StackLayer:
411 return graphPtr->layers()->Get(layerIndex)->layer_as_StackLayer()->base();
412 case Layer::Layer_StandInLayer:
413 return graphPtr->layers()->Get(layerIndex)->layer_as_StandInLayer()->base();
414 case Layer::Layer_StridedSliceLayer:
415 return graphPtr->layers()->Get(layerIndex)->layer_as_StridedSliceLayer()->base();
416 case Layer::Layer_SubtractionLayer:
417 return graphPtr->layers()->Get(layerIndex)->layer_as_SubtractionLayer()->base();
418 case Layer::Layer_SwitchLayer:
419 return graphPtr->layers()->Get(layerIndex)->layer_as_SwitchLayer()->base();
420 case Layer::Layer_TransposeConvolution2dLayer:
421 return graphPtr->layers()->Get(layerIndex)->layer_as_TransposeConvolution2dLayer()->base();
422 case Layer::Layer_TransposeLayer:
423 return graphPtr->layers()->Get(layerIndex)->layer_as_TransposeLayer()->base();
424 case Layer::Layer_UnidirectionalSequenceLstmLayer:
425 return graphPtr->layers()->Get(layerIndex)->layer_as_UnidirectionalSequenceLstmLayer()->base();
426 case Layer::Layer_NONE:
428 throw ParseException(fmt::format(
"Layer type {} not recognized", layerType));
434 auto layer = GetBaseLayer(graph, index);
436 return layer->layerName()->str();
441 auto layerType = graphPtr->layers()->Get(layerIndex)->layer_type();
443 if (layerType == Layer::Layer_InputLayer)
445 return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->layerBindingId();
447 else if ( layerType == Layer::Layer_OutputLayer )
449 return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->layerBindingId();
458 case armnnSerializer::DataLayout::DataLayout_NHWC:
460 case armnnSerializer::DataLayout::DataLayout_NDHWC:
462 case armnnSerializer::DataLayout::DataLayout_NCDHW:
464 case armnnSerializer::DataLayout::DataLayout_NCHW:
474 case armnnSerializer::ActivationFunction_Sigmoid:
476 case armnnSerializer::ActivationFunction_TanH:
478 case armnnSerializer::ActivationFunction_Linear:
480 case armnnSerializer::ActivationFunction_ReLu:
482 case armnnSerializer::ActivationFunction_BoundedReLu:
484 case armnnSerializer::ActivationFunction_LeakyReLu:
486 case armnnSerializer::ActivationFunction_Abs:
488 case armnnSerializer::ActivationFunction_Sqrt:
490 case armnnSerializer::ActivationFunction_Square:
492 case armnnSerializer::ActivationFunction_Elu:
494 case armnnSerializer::ActivationFunction_HardSwish:
505 case armnnSerializer::ArgMinMaxFunction::ArgMinMaxFunction_Max:
507 case armnnSerializer::ArgMinMaxFunction::ArgMinMaxFunction_Min:
517 case armnnSerializer::ComparisonOperation::ComparisonOperation_Equal:
519 case armnnSerializer::ComparisonOperation::ComparisonOperation_Greater:
521 case armnnSerializer::ComparisonOperation::ComparisonOperation_GreaterOrEqual:
523 case armnnSerializer::ComparisonOperation::ComparisonOperation_Less:
525 case armnnSerializer::ComparisonOperation::ComparisonOperation_LessOrEqual:
527 case armnnSerializer::ComparisonOperation::ComparisonOperation_NotEqual:
537 case armnnSerializer::ReduceOperation::ReduceOperation_Sum:
539 case armnnSerializer::ReduceOperation::ReduceOperation_Max:
541 case armnnSerializer::ReduceOperation::ReduceOperation_Mean:
543 case armnnSerializer::ReduceOperation::ReduceOperation_Min:
545 case armnnSerializer::ReduceOperation::ReduceOperation_Prod:
556 case armnnSerializer::LogicalBinaryOperation::LogicalBinaryOperation_LogicalAnd:
558 case armnnSerializer::LogicalBinaryOperation::LogicalBinaryOperation_LogicalOr:
569 case armnnSerializer::UnaryOperation::UnaryOperation_Abs:
571 case armnnSerializer::UnaryOperation::UnaryOperation_Rsqrt:
573 case armnnSerializer::UnaryOperation::UnaryOperation_Sqrt:
575 case armnnSerializer::UnaryOperation::UnaryOperation_Exp:
577 case armnnSerializer::UnaryOperation::UnaryOperation_Neg:
579 case armnnSerializer::UnaryOperation::UnaryOperation_LogicalNot:
581 case armnnSerializer::UnaryOperation::UnaryOperation_Log:
583 case armnnSerializer::UnaryOperation::UnaryOperation_Sin:
594 case armnnSerializer::PaddingMode::PaddingMode_Reflect:
596 case armnnSerializer::PaddingMode::PaddingMode_Symmetric:
607 case armnnSerializer::ResizeMethod_NearestNeighbor:
609 case armnnSerializer::ResizeMethod_Bilinear:
621 switch (tensorPtr->dataType())
623 case DataType_QAsymmS8:
626 case DataType_QSymmS8:
629 case DataType_QuantisedAsymm8:
630 case DataType_QAsymmU8:
633 case DataType_QSymmS16:
634 case DataType_QuantisedSymm16:
637 case DataType_Signed32:
640 case DataType_Signed64:
643 case DataType_Float32:
646 case DataType_Float16:
649 case DataType_Boolean:
655 throw ParseException(fmt::format(
"Unsupported data type {0} = {1}. {2}",
656 tensorPtr->dataType(),
657 EnumNameDataType(tensorPtr->dataType()),
662 float quantizationScale = tensorPtr->quantizationScale();
663 int32_t quantizationOffset = tensorPtr->quantizationOffset();
665 if (tensorPtr->dimensionality() ==
static_cast<unsigned int>(Dimensionality::Scalar))
672 else if (tensorPtr->dimensionality() ==
static_cast<unsigned int>(Dimensionality::NotSpecified))
681 auto dimensions = tensorPtr->dimensions();
682 unsigned int size = dimensions->size();
683 std::vector<unsigned int> outputDims(dimensions->begin(), dimensions->begin() + size);
688 if (tensorPtr->dimensionSpecificity() !=
nullptr)
690 auto dimensionSpecificity = tensorPtr->dimensionSpecificity();
691 size = dimensionSpecificity->size();
692 for (
unsigned int i = 0; i < size; ++i)
694 dimensionsSpecificity[i] = dimensionSpecificity->Get(i);
698 TensorShape shape(size, outputDims.data(), dimensionsSpecificity);
700 auto quantizationScales = tensorPtr->quantizationScales();
701 if (quantizationScales)
703 unsigned int quantizationScalesSize = quantizationScales->size();
704 std::vector<float> scales(quantizationScales->begin(), quantizationScales->begin() + quantizationScalesSize);
705 unsigned int quantizationDim = tensorPtr->quantizationDim();
728 switch (constTensorPtr->data_type())
730 case ConstTensorData_ByteData:
732 auto byteData = constTensorPtr->data_as_ByteData()->data();
736 case ConstTensorData_ShortData:
738 auto shortData = constTensorPtr->data_as_ShortData()->data();
742 case ConstTensorData_IntData:
744 auto intData = constTensorPtr->data_as_IntData()->data();
748 case ConstTensorData_LongData:
750 auto longData = constTensorPtr->data_as_LongData()->data();
757 throw ParseException(fmt::format(
"Unsupported data type {0} = {1}. {2}",
758 constTensorPtr->data_type(),
759 EnumNameConstTensorData(constTensorPtr->data_type()),
768 auto layer = GetBaseLayer(graphPtr, layerIndex);
769 const auto& numInputs = layer->inputSlots()->size();
773 for (
unsigned int i=0; i<numInputs; ++i)
776 (layer->inputSlots()->Get(i)->connection()->sourceLayerIndex()));
777 result[i] = GetBaseLayer(graphPtr, inputId)->outputSlots()->Get(0)->tensorInfo();
785 auto layer = GetBaseLayer(graphPtr, layerIndex);
786 const auto& numOutputs = layer->outputSlots()->size();
790 for (
unsigned int i=0; i<numOutputs; ++i)
792 result[i] = layer->outputSlots()->Get(i)->tensorInfo();
797 void IDeserializer::DeserializerImpl::ParseUnsupportedLayer(
GraphPtr graph,
unsigned int layerIndex)
800 const auto layerName = GetBaseLayer(graph, layerIndex)->layerName()->c_str();
801 throw ParseException(fmt::format(
"Layer not supported. layerIndex: {0} "
802 "layerName: {1} / {2}",
808 void IDeserializer::DeserializerImpl::ResetParser()
811 m_InputBindings.clear();
812 m_OutputBindings.clear();
819 GraphPtr graph = LoadGraphFromBinary(binaryContent.data(), binaryContent.size());
820 return CreateNetworkFromGraph(graph);
826 if (binaryContent.fail()) {
830 binaryContent.seekg(0, std::ios::end);
831 const std::streamoff size = binaryContent.tellg();
832 std::vector<char> content(
static_cast<size_t>(size));
833 binaryContent.seekg(0);
834 binaryContent.read(content.data(),
static_cast<std::streamsize
>(size));
835 GraphPtr graph = LoadGraphFromBinary(
reinterpret_cast<uint8_t*
>(content.data()),
static_cast<size_t>(size));
836 return CreateNetworkFromGraph(graph);
841 if (binaryContent ==
nullptr)
846 flatbuffers::Verifier verifier(binaryContent, len);
847 if (verifier.VerifyBuffer<SerializedGraph>() ==
false)
849 throw ParseException(fmt::format(
"Buffer doesn't conform to the expected Armnn "
850 "flatbuffers format. size:{0} {1}",
854 return GetSerializedGraph(binaryContent);
859 m_Network = INetwork::Create();
861 unsigned int layerIndex = 0;
862 for (AnyLayer
const* layer : *graph->layers())
864 if (layer->layer_type() != Layer_InputLayer &&
865 layer->layer_type() != Layer_OutputLayer)
868 auto& parserFunction = m_ParserFunctions[layer->layer_type()];
869 (this->*parserFunction)(graph, layerIndex);
874 SetupInputLayers(graph);
875 SetupOutputLayers(graph);
878 for (
auto&& graphIt : m_GraphConnections)
880 Connections& connections = graphIt.second;
881 for (
auto&& outputIt : connections.outputSlots)
883 const unsigned int outputSlotIndex = outputIt.first;
885 if (connections.inputSlots.find(outputSlotIndex) != connections.inputSlots.end())
887 for (
IInputSlot* inputSlot : connections.inputSlots[outputSlotIndex])
889 outputSlot->
Connect(*inputSlot);
895 return std::move(m_Network);
899 const std::string& name)
const
902 for (
auto inputBinding : m_InputBindings)
904 if (inputBinding.first == name)
906 return inputBinding.second;
909 throw ParseException(fmt::format(
"No input binding found for layer:{0} / {1}",
915 const std::string& name)
const
918 for (
auto outputBinding : m_OutputBindings)
920 if (outputBinding.first == name)
922 return outputBinding.second;
925 throw ParseException(fmt::format(
"No output binding found for layer:{0} / {1}",
930 unsigned int IDeserializer::DeserializerImpl::GetInputLayerInVector(
GraphPtr graph,
int targetId)
932 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
934 auto layer = graph->layers()->Get(i);
935 if (layer->layer_type() == Layer::Layer_InputLayer)
937 auto layerBindingId = layer->layer_as_InputLayer()->base()->layerBindingId();
938 if (layerBindingId == targetId)
944 throw ParseException(
"Input layer with given layerBindingId not found");
947 unsigned int IDeserializer::DeserializerImpl::GetOutputLayerInVector(
GraphPtr graph,
int targetId)
949 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
951 auto layer = graph->layers()->Get(i);
952 if (layer->layer_type() == Layer::Layer_OutputLayer)
954 auto layerBindingId = layer->layer_as_OutputLayer()->base()->layerBindingId();
955 if (layerBindingId == targetId)
961 throw ParseException(
"Output layer with given layerBindingId not found");
964 unsigned int IDeserializer::DeserializerImpl::GetLayerIndexInVector(
GraphPtr graph,
unsigned int targetIndex)
966 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
969 if (layer->index() == targetIndex)
977 IDeserializer::DeserializerImpl::FeatureVersions IDeserializer::DeserializerImpl::GetFeatureVersions(
GraphPtr graph)
979 IDeserializer::DeserializerImpl::FeatureVersions versions;
981 if (graph->featureVersions())
983 versions.m_BindingIdScheme = graph->featureVersions()->bindingIdsScheme();
984 versions.m_WeightsLayoutScheme = graph->featureVersions()->weightsLayoutScheme();
985 versions.m_ConstTensorsAsInputs = graph->featureVersions()->constantTensorsAsInputs();
991 void IDeserializer::DeserializerImpl::SetupInputLayers(
GraphPtr graph)
994 const unsigned int numInputs = graph->inputIds()->size();
995 m_InputBindings.clear();
996 m_InputBindings.reserve(numInputs);
998 for (
unsigned int i = 0; i < numInputs; i++)
1000 unsigned int inputLayerIndex = 0xFFFFFFFF;
1001 if (GetFeatureVersions(graph).m_BindingIdScheme == 0)
1003 const unsigned int inputId = armnn::numeric_cast<unsigned int>(graph->inputIds()->Get(i));
1004 inputLayerIndex = GetLayerIndexInVector(graph, inputId);
1008 const int inputId = graph->inputIds()->Get(i);
1009 inputLayerIndex = GetInputLayerInVector(graph, inputId);
1015 LayerBindingId bindingId = GetBindingLayerInfo(graph, inputLayerIndex);
1019 m_Network->AddInputLayer(bindingId, baseLayer->layerName()->c_str());
1023 RegisterOutputSlots(graph, inputLayerIndex, inputLayer);
1026 m_InputBindings.push_back(std::make_pair(baseLayer->layerName()->c_str(), bindingInfo));
1030 void IDeserializer::DeserializerImpl::SetupOutputLayers(
GraphPtr graph)
1033 const unsigned int numOutputs = graph->outputIds()->size();
1034 m_OutputBindings.clear();
1035 m_OutputBindings.reserve(numOutputs);
1037 for (
unsigned int i = 0; i < numOutputs; i++)
1039 unsigned int outputLayerIndex = 0xFFFFFFFF;
1040 if (GetFeatureVersions(graph).m_BindingIdScheme == 0)
1042 const unsigned int outputId = armnn::numeric_cast<unsigned int>(graph->outputIds()->Get(i));
1043 outputLayerIndex = GetLayerIndexInVector(graph, outputId);
1047 const int outputId = graph->outputIds()->Get(i);
1048 outputLayerIndex = GetOutputLayerInVector(graph, outputId);
1054 LayerBindingId bindingId = GetBindingLayerInfo(graph, outputLayerIndex);
1058 m_Network->AddOutputLayer(bindingId, baseLayer->layerName()->c_str());
1060 RegisterInputSlots(graph, outputLayerIndex, outputLayer);
1061 unsigned int sourceLayerIndex =
1062 GetLayerIndexInVector(graph, baseLayer->inputSlots()->Get(0)->connection()->sourceLayerIndex());
1063 unsigned int outputSlotIndex =
1064 GetLayerIndexInVector(graph, baseLayer->inputSlots()->Get(0)->connection()->outputSlotIndex());
1065 LayerBaseRawPtr sourceBaseLayer = GetBaseLayer(graph, sourceLayerIndex);
1067 sourceBaseLayer->outputSlots()->Get(outputSlotIndex)->tensorInfo());
1069 m_OutputBindings.push_back(std::make_pair(baseLayer->layerName()->c_str(), bindingInfo));
1073 void IDeserializer::DeserializerImpl::RegisterOutputSlots(
GraphPtr graph,
1074 uint32_t layerIndex,
1082 throw ParseException(fmt::format(
"The number of outputslots ({0}) does not match the number expected ({1})"
1083 " for layer index: {2} {3}",
1084 baseLayer->outputSlots()->size(),
1092 const unsigned int slotIndex = baseLayer->outputSlots()->Get(i)->index();
1095 RegisterOutputSlotOfConnection(baseLayer->index(), slotIndex, outputSlot);
1099 void IDeserializer::DeserializerImpl::RegisterInputSlots(
GraphPtr graph,
1100 uint32_t layerIndex,
1102 std::vector<unsigned int> ignoreSlots)
1108 if (baseLayer->inputSlots()->size() != (layer->
GetNumInputSlots() - ignoreSlots.size()))
1110 throw ParseException(fmt::format(
"The number of inputslots ({0}) does not match the number expected ({1})"
1111 " for layer index:{2} {3}",
1112 baseLayer->inputSlots()->size(),
1121 if (std::find(ignoreSlots.begin(), ignoreSlots.end(), i) == ignoreSlots.end())
1123 auto fbInputSlot = baseLayer->inputSlots()->Get(i);
1124 auto fbConnection = fbInputSlot->connection();
1126 RegisterInputSlotOfConnection(fbConnection->sourceLayerIndex(), fbConnection->outputSlotIndex(), inputSlot);
1131 void IDeserializer::DeserializerImpl::RegisterInputSlotOfConnection(uint32_t sourceLayerIndex,
1132 uint32_t outputSlotIndex,
1135 if (m_GraphConnections.find(sourceLayerIndex) == m_GraphConnections.end())
1137 m_GraphConnections[sourceLayerIndex] = Connections();
1140 Connections& connections = m_GraphConnections[sourceLayerIndex];
1141 if (connections.inputSlots.find(outputSlotIndex) == connections.inputSlots.end())
1143 connections.inputSlots[outputSlotIndex] = {inputSlot};
1147 connections.inputSlots[outputSlotIndex].push_back(inputSlot);
1151 void IDeserializer::DeserializerImpl::RegisterOutputSlotOfConnection(uint32_t sourceLayerIndex,
1152 uint32_t outputSlotIndex,
1155 if (m_GraphConnections.find(sourceLayerIndex) == m_GraphConnections.end())
1157 m_GraphConnections[sourceLayerIndex] = Connections();
1160 Connections& connections = m_GraphConnections[sourceLayerIndex];
1161 if (connections.outputSlots.find(outputSlotIndex) != connections.outputSlots.end())
1166 connections.outputSlots[outputSlotIndex] = outputSlot;
1169 void IDeserializer::DeserializerImpl::ParseAbs(
GraphPtr graph,
unsigned int layerIndex)
1172 auto inputs = GetInputs(graph, layerIndex);
1176 auto outputs = GetOutputs(graph, layerIndex);
1179 auto layerName = GetLayerName(graph, layerIndex);
1182 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
1186 RegisterInputSlots(graph, layerIndex, layer);
1187 RegisterOutputSlots(graph, layerIndex, layer);
1190 void IDeserializer::DeserializerImpl::ParseActivation(
GraphPtr graph,
unsigned int layerIndex)
1193 auto inputs = GetInputs(graph, layerIndex);
1197 auto outputs = GetOutputs(graph, layerIndex);
1200 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ActivationLayer();
1201 auto layerName = GetLayerName(graph, layerIndex);
1202 auto serializerDescriptor = serializerLayer->descriptor();
1206 descriptor.
m_A = serializerDescriptor->a();
1207 descriptor.
m_B = serializerDescriptor->b();
1214 RegisterInputSlots(graph, layerIndex, layer);
1215 RegisterOutputSlots(graph, layerIndex, layer);
1218 void IDeserializer::DeserializerImpl::ParseAdd(
GraphPtr graph,
unsigned int layerIndex)
1221 auto inputs = GetInputs(graph, layerIndex);
1225 auto outputs = GetOutputs(graph, layerIndex);
1228 auto layerName = GetLayerName(graph, layerIndex);
1234 RegisterInputSlots(graph, layerIndex, layer);
1235 RegisterOutputSlots(graph, layerIndex, layer);
1238 void IDeserializer::DeserializerImpl::ParseArgMinMax(
GraphPtr graph,
unsigned int layerIndex)
1241 auto inputs = GetInputs(graph, layerIndex);
1245 auto outputs = GetOutputs(graph, layerIndex);
1248 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ArgMinMaxLayer();
1249 auto serializerDescriptor = serializerLayer->descriptor();
1253 descriptor.
m_Axis = serializerDescriptor->axis();
1254 auto layerName = GetLayerName(graph, layerIndex);
1255 IConnectableLayer* layer = m_Network->AddArgMinMaxLayer(descriptor, layerName.c_str());
1260 RegisterInputSlots(graph, layerIndex, layer);
1261 RegisterOutputSlots(graph, layerIndex, layer);
1264 void IDeserializer::DeserializerImpl::ParseBatchMatMul(
GraphPtr graph,
unsigned int layerIndex)
1268 auto inputs = GetInputs(graph, layerIndex);
1272 auto outputs = GetOutputs(graph, layerIndex);
1275 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_BatchMatMulLayer();
1276 auto serializerDescriptor = serializerLayer->descriptor();
1279 serializerDescriptor->transposeY(),
1280 serializerDescriptor->adjointX(),
1281 serializerDescriptor->adjointY(),
1285 auto layerName = GetLayerName(graph, layerIndex);
1286 IConnectableLayer* layer = m_Network->AddBatchMatMulLayer(descriptor, layerName.c_str());
1291 RegisterInputSlots(graph, layerIndex, layer);
1292 RegisterOutputSlots(graph, layerIndex, layer);
1295 void IDeserializer::DeserializerImpl::ParseBatchToSpaceNd(
GraphPtr graph,
unsigned int layerIndex)
1305 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->descriptor();
1306 auto flatBufferCrops = flatBufferDescriptor->crops();
1307 auto flatBufferBlockShape = flatBufferDescriptor->blockShape();
1309 if (flatBufferCrops->size() % 2 != 0)
1314 std::vector<std::pair<unsigned int, unsigned int>> crops;
1315 crops.reserve(flatBufferCrops->size() / 2);
1316 for (
unsigned int i = 0; i < flatBufferCrops->size() - 1; i += 2)
1318 crops.emplace_back(flatBufferCrops->Get(i), flatBufferCrops->Get(i+1));
1324 std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end());
1327 auto layerName = GetLayerName(graph, layerIndex);
1328 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(descriptor, layerName.c_str());
1333 RegisterInputSlots(graph, layerIndex, layer);
1334 RegisterOutputSlots(graph, layerIndex, layer);
1337 void IDeserializer::DeserializerImpl::ParseBatchNormalization(
GraphPtr graph,
unsigned int layerIndex)
1341 auto inputs = GetInputs(graph, layerIndex);
1344 auto outputs = GetOutputs(graph, layerIndex);
1348 auto layerName = GetLayerName(graph, layerIndex);
1350 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_BatchNormalizationLayer();
1351 auto serializerDescriptor = serializerLayer->descriptor();
1354 descriptor.
m_Eps = serializerDescriptor->eps();
1370 RegisterInputSlots(graph, layerIndex, layer);
1371 RegisterOutputSlots(graph, layerIndex, layer);
1374 void IDeserializer::DeserializerImpl::ParseCast(
GraphPtr graph,
unsigned int layerIndex)
1384 auto layerName = GetLayerName(graph, layerIndex);
1391 RegisterInputSlots(graph, layerIndex, layer);
1392 RegisterOutputSlots(graph, layerIndex, layer);
1395 void IDeserializer::DeserializerImpl::ParseConstant(
GraphPtr graph,
unsigned int layerIndex)
1400 auto outputs = GetOutputs(graph, layerIndex);
1403 auto layerName = GetLayerName(graph, layerIndex);
1405 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ConstantLayer();
1406 auto serializerInput = serializerLayer->input();
1415 if (this->GetFeatureVersions(graph).m_WeightsLayoutScheme <= 0)
1421 std::unique_ptr<unsigned char[]> permuteBuffer(
new unsigned char[weightsInfo.
GetNumBytes()]);
1428 auto weightsShape = weightsInfo.
GetShape();
1432 weightsShape[2]*weightsShape[3]});
1437 layer = m_Network->AddConstantLayer(weightsPermuted, layerName.c_str());
1441 RegisterOutputSlots(graph, layerIndex, layer);
1447 layer = m_Network->AddConstantLayer(input, layerName.c_str());
1454 RegisterOutputSlots(graph, layerIndex, layer);
1457 void IDeserializer::DeserializerImpl::ParseConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
1460 auto inputs = GetInputs(graph, layerIndex);
1463 auto outputs = GetOutputs(graph, layerIndex);
1466 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_Convolution2dLayer();
1468 auto layerName = GetLayerName(graph, layerIndex);
1469 auto flatbufferDescriptor = flatBufferLayer->descriptor();
1472 descriptor.
m_PadLeft = flatbufferDescriptor->padLeft();
1473 descriptor.
m_PadRight = flatbufferDescriptor->padRight();
1474 descriptor.
m_PadTop = flatbufferDescriptor->padTop();
1475 descriptor.
m_PadBottom = flatbufferDescriptor->padBottom();
1476 descriptor.
m_StrideX = flatbufferDescriptor->strideX();
1477 descriptor.
m_StrideY = flatbufferDescriptor->strideY();;
1478 descriptor.
m_DilationX = flatbufferDescriptor->dilationX();
1479 descriptor.
m_DilationY = flatbufferDescriptor->dilationY();;
1480 descriptor.
m_BiasEnabled = flatbufferDescriptor->biasEnabled();;
1484 std::vector<unsigned int> ignoreSlots {};
1489 if (this->GetFeatureVersions(graph).m_ConstTensorsAsInputs <= 0)
1495 layer = m_Network->AddConvolution2dLayer(descriptor,
1499 auto weightsLayer = m_Network->AddConstantLayer(weightsTensor);
1500 weightsLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(1u));
1501 weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsTensor.
GetInfo());
1502 ignoreSlots.emplace_back(1u);
1507 auto biasLayer = m_Network->AddConstantLayer(biasTensor);
1508 biasLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(2u));
1509 biasLayer->GetOutputSlot(0).SetTensorInfo(biasTensor.
GetInfo());
1510 ignoreSlots.emplace_back(2u);
1515 layer = m_Network->AddConvolution2dLayer(descriptor,
1524 RegisterInputSlots(graph, layerIndex, layer, ignoreSlots);
1525 RegisterOutputSlots(graph, layerIndex, layer);
1528 void IDeserializer::DeserializerImpl::ParseConvolution3d(
GraphPtr graph,
unsigned int layerIndex)
1531 auto inputs = GetInputs(graph, layerIndex);
1534 auto outputs = GetOutputs(graph, layerIndex);
1537 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_Convolution3dLayer();
1538 auto layerName = GetLayerName(graph, layerIndex);
1539 auto serializerDescriptor = serializerLayer->descriptor();
1542 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
1543 descriptor.
m_PadRight = serializerDescriptor->padRight();
1544 descriptor.
m_PadTop = serializerDescriptor->padTop();
1545 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
1546 descriptor.
m_PadFront = serializerDescriptor->padFront();
1547 descriptor.
m_PadBack = serializerDescriptor->padBack();
1548 descriptor.
m_StrideX = serializerDescriptor->strideX();
1549 descriptor.
m_StrideY = serializerDescriptor->strideY();
1550 descriptor.
m_StrideZ = serializerDescriptor->strideZ();
1551 descriptor.
m_DilationX = serializerDescriptor->dilationX();
1552 descriptor.
m_DilationY = serializerDescriptor->dilationY();
1553 descriptor.
m_DilationZ = serializerDescriptor->dilationZ();
1554 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();
1560 IConnectableLayer* layer = m_Network->AddConvolution3dLayer(descriptor, layerName.c_str());
1565 RegisterInputSlots(graph, layerIndex, layer);
1566 RegisterOutputSlots(graph, layerIndex, layer);
1569 void IDeserializer::DeserializerImpl::ParseDepthToSpace(
GraphPtr graph,
unsigned int layerIndex)
1573 auto inputs = GetInputs(graph, layerIndex);
1576 auto outputs = GetOutputs(graph, layerIndex);
1579 auto fbDescriptor = graph->layers()->Get(layerIndex)->layer_as_DepthToSpaceLayer()->descriptor();
1582 descriptor.
m_BlockSize = fbDescriptor->blockSize();
1585 auto layerName = GetLayerName(graph, layerIndex);
1586 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
1591 RegisterInputSlots(graph, layerIndex, layer);
1592 RegisterOutputSlots(graph, layerIndex, layer);
1595 void IDeserializer::DeserializerImpl::ParseDepthwiseConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
1598 auto inputs = GetInputs(graph, layerIndex);
1601 auto outputs = GetOutputs(graph, layerIndex);
1604 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer();
1605 auto layerName = GetLayerName(graph, layerIndex);
1606 auto serializerDescriptor = serializerLayer->descriptor();
1609 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
1610 descriptor.
m_PadRight = serializerDescriptor->padRight();
1611 descriptor.
m_PadTop = serializerDescriptor->padTop();
1612 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
1613 descriptor.
m_StrideX = serializerDescriptor->strideX();
1614 descriptor.
m_StrideY = serializerDescriptor->strideY();
1615 descriptor.
m_DilationX = serializerDescriptor->dilationX();
1616 descriptor.
m_DilationY = serializerDescriptor->dilationY();
1617 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();
1621 std::vector<unsigned int> ignoreSlots {};
1625 if (this->GetFeatureVersions(graph).m_ConstTensorsAsInputs <= 0)
1632 ignoreSlots.emplace_back(1u);
1634 layer = m_Network->AddDepthwiseConvolution2dLayer(descriptor,
1641 ignoreSlots.emplace_back(2u);
1643 auto biasLayer = m_Network->AddConstantLayer(biases);
1644 biasLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(2u));
1645 biasLayer->GetOutputSlot(0).SetTensorInfo(biases.
GetInfo());
1648 if (this->GetFeatureVersions(graph).m_WeightsLayoutScheme <= 0)
1654 std::unique_ptr<unsigned char[]> permuteBuffer(
new unsigned char[weightsInfo.
GetNumBytes()]);
1661 auto weightsShape = weightsInfo.
GetShape();
1665 weightsShape[2]*weightsShape[3]});
1669 auto weightsLayer = m_Network->AddConstantLayer(weightsPermuted);
1670 weightsLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(1u));
1671 weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsPermuted.GetInfo());
1675 auto weightsLayer = m_Network->AddConstantLayer(weights);
1676 weightsLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(1u));
1677 weightsLayer->GetOutputSlot(0).SetTensorInfo(weights.
GetInfo());
1682 layer = m_Network->AddDepthwiseConvolution2dLayer(descriptor,
1691 RegisterInputSlots(graph, layerIndex, layer, ignoreSlots);
1692 RegisterOutputSlots(graph, layerIndex, layer);
1695 void IDeserializer::DeserializerImpl::ParseDetectionPostProcess(
GraphPtr graph,
unsigned int layerIndex)
1698 auto inputs = GetInputs(graph, layerIndex);
1702 auto outputs = GetOutputs(graph, layerIndex);
1705 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_DetectionPostProcessLayer();
1706 auto layerName = GetLayerName(graph, layerIndex);
1707 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1715 descriptor.
m_NumClasses = flatBufferDescriptor->numClasses();
1717 descriptor.
m_ScaleX = flatBufferDescriptor->scaleX();
1718 descriptor.
m_ScaleY = flatBufferDescriptor->scaleY();
1719 descriptor.
m_ScaleW = flatBufferDescriptor->scaleW();
1720 descriptor.
m_ScaleH = flatBufferDescriptor->scaleH();
1728 for (
unsigned int i = 0; i < 4; i++)
1733 RegisterInputSlots(graph, layerIndex, layer);
1734 RegisterOutputSlots(graph, layerIndex, layer);
1737 void IDeserializer::DeserializerImpl::ParseDivision(
GraphPtr graph,
unsigned int layerIndex)
1740 auto inputs = GetInputs(graph, layerIndex);
1744 auto outputs = GetOutputs(graph, layerIndex);
1747 auto layerName = GetLayerName(graph, layerIndex);
1753 RegisterInputSlots(graph, layerIndex, layer);
1754 RegisterOutputSlots(graph, layerIndex, layer);
1757 void IDeserializer::DeserializerImpl::ParseEqual(
GraphPtr graph,
unsigned int layerIndex)
1760 auto inputs = GetInputs(graph, layerIndex);
1764 auto outputs = GetOutputs(graph, layerIndex);
1767 auto layerName = GetLayerName(graph, layerIndex);
1769 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
1774 RegisterInputSlots(graph, layerIndex, layer);
1775 RegisterOutputSlots(graph, layerIndex, layer);
1778 void IDeserializer::DeserializerImpl::ParseFill(
GraphPtr graph,
unsigned int layerIndex)
1781 auto inputs = GetInputs(graph, layerIndex);
1785 auto outputs = GetOutputs(graph, layerIndex);
1788 auto layerName = GetLayerName(graph, layerIndex);
1790 descriptor.
m_Value = graph->layers()->Get(layerIndex)->layer_as_FillLayer()->descriptor()->value();
1791 IConnectableLayer* layer = m_Network->AddFillLayer(descriptor, layerName.c_str());
1796 RegisterInputSlots(graph, layerIndex, layer);
1797 RegisterOutputSlots(graph, layerIndex, layer);
1800 void IDeserializer::DeserializerImpl::ParseGreater(
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->AddComparisonLayer(descriptor, layerName.c_str());
1817 RegisterInputSlots(graph, layerIndex, layer);
1818 RegisterOutputSlots(graph, layerIndex, layer);
1821 void IDeserializer::DeserializerImpl::ParseInstanceNormalization(
GraphPtr graph,
unsigned int layerIndex)
1825 auto inputs = GetInputs(graph, layerIndex);
1828 auto outputs = GetOutputs(graph, layerIndex);
1831 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer();
1832 auto fbDescriptor = fbLayer->descriptor();
1835 descriptor.
m_Gamma = fbDescriptor->gamma();
1836 descriptor.
m_Beta = fbDescriptor->beta();
1837 descriptor.
m_Eps = fbDescriptor->eps();
1840 const std::string layerName = GetLayerName(graph, layerIndex);
1843 IConnectableLayer* layer = m_Network->AddInstanceNormalizationLayer(descriptor, layerName.c_str());
1846 RegisterInputSlots(graph, layerIndex, layer);
1847 RegisterOutputSlots(graph, layerIndex, layer);
1850 void IDeserializer::DeserializerImpl::ParseL2Normalization(
GraphPtr graph,
unsigned int layerIndex)
1854 auto inputs = GetInputs(graph, layerIndex);
1857 auto outputs = GetOutputs(graph, layerIndex);
1861 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_L2NormalizationLayer();
1862 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1864 auto layerName = GetLayerName(graph, layerIndex);
1867 descriptor.
m_Eps = flatBufferDescriptor->eps();
1869 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(descriptor, layerName.c_str());
1872 RegisterInputSlots(graph, layerIndex, layer);
1873 RegisterOutputSlots(graph, layerIndex, layer);
1876 void IDeserializer::DeserializerImpl::ParseLogicalBinary(
GraphPtr graph,
unsigned int layerIndex)
1881 auto inputs = GetInputs(graph, layerIndex);
1884 auto outputs = GetOutputs(graph, layerIndex);
1887 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_LogicalBinaryLayer();
1888 auto fbDescriptor = fbLayer->descriptor();
1893 const std::string& layerName = GetLayerName(graph, layerIndex);
1894 IConnectableLayer* layer = m_Network->AddLogicalBinaryLayer(descriptor, layerName.c_str());
1899 RegisterInputSlots(graph, layerIndex, layer);
1900 RegisterOutputSlots(graph, layerIndex, layer);
1903 void IDeserializer::DeserializerImpl::ParseLogSoftmax(
GraphPtr graph,
unsigned int layerIndex)
1914 descriptor.
m_Beta = graph->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->descriptor()->beta();
1915 descriptor.
m_Axis = graph->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->descriptor()->axis();
1916 auto layerName = GetLayerName(graph, layerIndex);
1918 IConnectableLayer* layer = m_Network->AddLogSoftmaxLayer(descriptor, layerName.c_str());
1923 RegisterInputSlots(graph, layerIndex, layer);
1924 RegisterOutputSlots(graph, layerIndex, layer);
1927 void IDeserializer::DeserializerImpl::ParseMinimum(
GraphPtr graph,
unsigned int layerIndex)
1930 auto inputs = GetInputs(graph, layerIndex);
1934 auto outputs = GetOutputs(graph, layerIndex);
1937 auto layerName = GetLayerName(graph, layerIndex);
1943 RegisterInputSlots(graph, layerIndex, layer);
1944 RegisterOutputSlots(graph, layerIndex, layer);
1947 void IDeserializer::DeserializerImpl::ParseMaximum(
GraphPtr graph,
unsigned int layerIndex)
1950 auto inputs = GetInputs(graph, layerIndex);
1954 auto outputs = GetOutputs(graph, layerIndex);
1957 auto layerName = GetLayerName(graph, layerIndex);
1963 RegisterInputSlots(graph, layerIndex, layer);
1964 RegisterOutputSlots(graph, layerIndex, layer);
1968 unsigned int layerIndex)
1970 auto layerType = graph->layers()->Get(layerIndex)->layer_type();
1974 case Layer::Layer_ConcatLayer:
1975 return graph->layers()->Get(layerIndex)->layer_as_ConcatLayer()->descriptor();
1976 case Layer::Layer_MergerLayer:
1977 return graph->layers()->Get(layerIndex)->layer_as_MergerLayer()->descriptor();
1982 void IDeserializer::DeserializerImpl::ParseChannelShuffle(
GraphPtr graph,
unsigned int layerIndex)
1993 descriptor.
m_Axis = graph->layers()->Get(layerIndex)->layer_as_ChannelShuffleLayer()->descriptor()->axis();
1995 graph->layers()->Get(layerIndex)->layer_as_ChannelShuffleLayer()->descriptor()->numGroups();
1997 auto layerName = GetLayerName(graph, layerIndex);
1998 IConnectableLayer* layer = m_Network->AddChannelShuffleLayer(descriptor, layerName.c_str());
2003 RegisterInputSlots(graph, layerIndex, layer);
2004 RegisterOutputSlots(graph, layerIndex, layer);
2006 void IDeserializer::DeserializerImpl::ParseComparison(
GraphPtr graph,
unsigned int layerIndex)
2011 auto inputs = GetInputs(graph, layerIndex);
2014 auto outputs = GetOutputs(graph, layerIndex);
2017 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ComparisonLayer();
2018 auto fbDescriptor = fbLayer->descriptor();
2023 const std::string& layerName = GetLayerName(graph, layerIndex);
2024 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
2029 RegisterInputSlots(graph, layerIndex, layer);
2030 RegisterOutputSlots(graph, layerIndex, layer);
2033 void IDeserializer::DeserializerImpl::ParseElementwiseUnary(
GraphPtr graph,
unsigned int layerIndex)
2038 auto inputs = GetInputs(graph, layerIndex);
2041 auto outputs = GetOutputs(graph, layerIndex);
2044 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ElementwiseUnaryLayer();
2045 auto fbDescriptor = fbLayer->descriptor();
2050 const std::string& layerName = GetLayerName(graph, layerIndex);
2051 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
2056 RegisterInputSlots(graph, layerIndex, layer);
2057 RegisterOutputSlots(graph, layerIndex, layer);
2060 void IDeserializer::DeserializerImpl::ParseConcat(
GraphPtr graph,
unsigned int layerIndex)
2065 auto outputs = GetOutputs(graph, layerIndex);
2068 auto layerName = GetLayerName(graph, layerIndex);
2070 unsigned int numViews = originsDescriptor->numViews();
2071 unsigned int numDimensions = originsDescriptor->numDimensions();
2074 auto inputs = GetInputs(graph, layerIndex);
2078 auto originsPtr = originsDescriptor->viewOrigins();
2079 for (
unsigned int v = 0; v < numViews; ++v)
2081 auto originPtr = originsPtr->Get(v);
2082 for (
unsigned int d = 0; d < numDimensions; ++d)
2084 uint32_t value = originPtr->data()->Get(d);
2085 descriptor.SetViewOriginCoord(v, d, value);
2088 descriptor.SetConcatAxis(originsDescriptor->concatAxis());
2090 IConnectableLayer* layer = m_Network->AddConcatLayer(descriptor, layerName.c_str());
2094 RegisterInputSlots(graph, layerIndex, layer);
2095 RegisterOutputSlots(graph, layerIndex, layer);
2098 void IDeserializer::DeserializerImpl::ParseMultiplication(
GraphPtr graph,
unsigned int layerIndex)
2101 auto inputs = GetInputs(graph, layerIndex);
2105 auto outputs = GetOutputs(graph, layerIndex);
2108 auto layerName = GetLayerName(graph, layerIndex);
2109 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
2114 RegisterInputSlots(graph, layerIndex, layer);
2115 RegisterOutputSlots(graph, layerIndex, layer);
2118 void IDeserializer::DeserializerImpl::ParseFloor(
GraphPtr graph,
unsigned int layerIndex)
2123 auto inputs = GetInputs(graph, layerIndex);
2126 auto outputs = GetOutputs(graph, layerIndex);
2129 auto layerName = GetLayerName(graph, layerIndex);
2133 layer = m_Network->AddFloorLayer(layerName.c_str());
2138 RegisterInputSlots(graph, layerIndex, layer);
2139 RegisterOutputSlots(graph, layerIndex, layer);
2142 void IDeserializer::DeserializerImpl::ParseFullyConnected(
GraphPtr graph,
unsigned int layerIndex)
2145 auto inputs = GetInputs(graph, layerIndex);
2148 auto outputs = GetOutputs(graph, layerIndex);
2151 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer();
2152 auto layerName = GetLayerName(graph, layerIndex);
2153 auto flatBufferDescriptor = flatBufferLayer->descriptor();
2156 fullyConnectedDescriptor.
m_BiasEnabled = flatBufferDescriptor->biasEnabled();
2158 fullyConnectedDescriptor.
m_ConstantWeights = flatBufferDescriptor->constantWeights();
2161 std::vector<unsigned int> ignoreSlots {};
2165 if (this->GetFeatureVersions(graph).m_ConstTensorsAsInputs <= 0)
2170 layer = m_Network->AddFullyConnectedLayer(fullyConnectedDescriptor,
2174 auto weightsLayer = m_Network->AddConstantLayer(weightsTensor);
2175 weightsLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(1u));
2176 weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsTensor.
GetInfo());
2177 ignoreSlots.emplace_back(1u);
2182 auto biasLayer = m_Network->AddConstantLayer(biasTensor);
2183 biasLayer->GetOutputSlot(0).Connect(layer->
GetInputSlot(2u));
2184 biasLayer->GetOutputSlot(0).SetTensorInfo(biasTensor.
GetInfo());
2185 ignoreSlots.emplace_back(2u);
2190 layer = m_Network->AddFullyConnectedLayer(fullyConnectedDescriptor,
2192 uint32_t numInputs = fullyConnectedDescriptor.
GetNumInputs();
2199 RegisterInputSlots(graph, layerIndex, layer, ignoreSlots);
2200 RegisterOutputSlots(graph, layerIndex, layer);
2203 void IDeserializer::DeserializerImpl::ParsePad(
GraphPtr graph,
unsigned int layerIndex)
2213 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_PadLayer()->descriptor();
2214 auto flatBufferPadList = flatBufferDescriptor->padList();
2215 auto paddingMode = flatBufferDescriptor->paddingMode();
2216 float padValue = flatBufferDescriptor->padValue();
2218 if (flatBufferPadList->size() % 2 != 0)
2220 throw ParseException(fmt::format(
"The size of the pad list must be divisible by 2 {}",
2224 std::vector<std::pair<unsigned int, unsigned int>> padList;
2225 padList.reserve(flatBufferPadList->size() / 2);
2226 for (
unsigned int i = 0; i < flatBufferPadList->size() - 1; i += 2)
2228 padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1));
2233 auto layerName = GetLayerName(graph, layerIndex);
2234 IConnectableLayer* layer = m_Network->AddPadLayer(descriptor, layerName.c_str());
2239 RegisterInputSlots(graph, layerIndex, layer);
2240 RegisterOutputSlots(graph, layerIndex, layer);
2243 void IDeserializer::DeserializerImpl::ParsePermute(
GraphPtr graph,
unsigned int layerIndex)
2248 graph->layers()->Get(layerIndex)->layer_as_PermuteLayer()->descriptor()->dimMappings();
2250 auto inputs = GetInputs(graph, layerIndex);
2253 auto outputs = GetOutputs(graph, layerIndex);
2257 auto layerName = GetLayerName(graph, layerIndex);
2260 IConnectableLayer* layer = m_Network->AddPermuteLayer(descriptor, layerName.c_str());
2263 RegisterInputSlots(graph, layerIndex, layer);
2264 RegisterOutputSlots(graph, layerIndex, layer);
2268 unsigned int layerIndex)
2273 switch (pooling2dDesc->poolType())
2275 case PoolingAlgorithm_Average:
2280 case PoolingAlgorithm_Max:
2285 case PoolingAlgorithm_L2:
2296 switch (pooling2dDesc->outputShapeRounding())
2298 case OutputShapeRounding_Floor:
2303 case OutputShapeRounding_Ceiling:
2314 switch (pooling2dDesc->paddingMethod())
2316 case PaddingMethod_Exclude:
2321 case PaddingMethod_IgnoreValue:
2332 switch (pooling2dDesc->dataLayout())
2334 case DataLayout_NCHW:
2339 case DataLayout_NHWC:
2351 desc.
m_PadLeft = pooling2dDesc->padLeft();
2353 desc.
m_PadTop = pooling2dDesc->padTop();
2354 desc.
m_StrideX = pooling2dDesc->strideX();
2355 desc.
m_StrideY = pooling2dDesc->strideY();
2363 unsigned int layerIndex)
2368 switch (pooling3dDesc->poolType())
2370 case PoolingAlgorithm_Average:
2375 case PoolingAlgorithm_Max:
2380 case PoolingAlgorithm_L2:
2391 switch (pooling3dDesc->outputShapeRounding())
2393 case OutputShapeRounding_Floor:
2398 case OutputShapeRounding_Ceiling:
2409 switch (pooling3dDesc->paddingMethod())
2411 case PaddingMethod_Exclude:
2416 case PaddingMethod_IgnoreValue:
2427 switch (pooling3dDesc->dataLayout())
2429 case DataLayout_NCDHW:
2434 case DataLayout_NDHWC:
2446 desc.
m_PadLeft = pooling3dDesc->padLeft();
2448 desc.
m_PadTop = pooling3dDesc->padTop();
2450 desc.
m_PadBack = pooling3dDesc->padBack();
2451 desc.
m_StrideX = pooling3dDesc->strideX();
2452 desc.
m_StrideY = pooling3dDesc->strideY();
2453 desc.
m_StrideZ = pooling3dDesc->strideZ();
2461 void IDeserializer::DeserializerImpl::ParsePooling2d(
GraphPtr graph,
unsigned int layerIndex)
2465 auto pooling2dDes = graph->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->descriptor();
2466 auto inputs = GetInputs(graph, layerIndex);
2469 auto outputs = GetOutputs(graph, layerIndex);
2473 auto pooling2dDescriptor = GetPooling2dDescriptor(pooling2dDes, layerIndex);
2474 auto layerName = GetLayerName(graph, layerIndex);
2475 IConnectableLayer* layer = m_Network->AddPooling2dLayer(pooling2dDescriptor, layerName.c_str());
2478 RegisterInputSlots(graph, layerIndex, layer);
2479 RegisterOutputSlots(graph, layerIndex, layer);
2482 void IDeserializer::DeserializerImpl::ParsePooling3d(
GraphPtr graph,
unsigned int layerIndex)
2486 auto pooling3dDes = graph->layers()->Get(layerIndex)->layer_as_Pooling3dLayer()->descriptor();
2487 auto inputs = GetInputs(graph, layerIndex);
2490 auto outputs = GetOutputs(graph, layerIndex);
2494 auto pooling3dDescriptor = GetPooling3dDescriptor(pooling3dDes, layerIndex);
2495 auto layerName = GetLayerName(graph, layerIndex);
2496 IConnectableLayer* layer = m_Network->AddPooling3dLayer(pooling3dDescriptor, layerName.c_str());
2499 RegisterInputSlots(graph, layerIndex, layer);
2500 RegisterOutputSlots(graph, layerIndex, layer);
2503 void IDeserializer::DeserializerImpl::ParseQuantize(
GraphPtr graph,
unsigned int layerIndex)
2507 auto inputs = GetInputs(graph, layerIndex);
2510 auto outputs = GetOutputs(graph, layerIndex);
2514 auto layerName = GetLayerName(graph, layerIndex);
2518 RegisterInputSlots(graph, layerIndex, layer);
2519 RegisterOutputSlots(graph, layerIndex, layer);
2523 const std::vector<uint32_t>& targetDimsIn)
2525 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2526 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2528 if (stretchDim != targetDimsIn.end())
2530 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2532 throw ParseException(fmt::format(
"At most one component of shape can be -1 {}",
2536 auto targetNumElements =
2537 armnn::numeric_cast<unsigned int>(
2538 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2540 auto stretchIndex =
static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2541 outputDims[stretchIndex] = inputTensorInfo.
GetNumElements() / targetNumElements;
2552 void IDeserializer::DeserializerImpl::ParseRank(
GraphPtr graph,
unsigned int layerIndex)
2562 auto layerName = GetLayerName(graph, layerIndex);
2568 RegisterInputSlots(graph, layerIndex, layer);
2569 RegisterOutputSlots(graph, layerIndex, layer);
2572 void IDeserializer::DeserializerImpl::ParseReduce(
GraphPtr graph,
unsigned int layerIndex)
2577 auto inputs = GetInputs(graph, layerIndex);
2580 auto outputs = GetOutputs(graph, layerIndex);
2583 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ReduceLayer();
2584 auto fbDescriptor = fbLayer->descriptor();
2585 auto flatBufferAxis = fbDescriptor->axis();
2588 descriptor.
m_KeepDims = fbDescriptor->keepDims();
2589 descriptor.
m_vAxis = std::vector<unsigned int>(flatBufferAxis->begin(), flatBufferAxis->end());
2592 const std::string& layerName = GetLayerName(graph, layerIndex);
2593 IConnectableLayer* layer = m_Network->AddReduceLayer(descriptor, layerName.c_str());
2598 RegisterInputSlots(graph, layerIndex, layer);
2599 RegisterOutputSlots(graph, layerIndex, layer);
2602 void IDeserializer::DeserializerImpl::ParseReshape(
GraphPtr graph,
unsigned int layerIndex)
2605 auto inputs = GetInputs(graph, layerIndex);
2607 auto outputs = GetOutputs(graph, layerIndex);
2613 const auto targetDims = graph->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->descriptor()->targetShape();
2614 std::vector<uint32_t> outputDims(targetDims->begin(), targetDims->begin() + targetDims->size());
2619 const std::vector<uint32_t> expectedDims(outputs[0]->dimensions()->begin(),
2620 outputs[0]->dimensions()->begin() + outputs[0]->dimensions()->size());
2622 if (inputs.size() > 1 && !
CheckShape(reshapeOutputTensorShape, expectedDims))
2624 std::stringstream ss;
2625 ss <<
"New shape defined in reshape parameters "
2626 << reshapeOutputTensorShape
2627 <<
" does not equal output shape "
2628 << actualOutputTensorInfo.
GetShape()
2637 auto layerName = GetLayerName(graph, layerIndex);
2638 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2641 RegisterInputSlots(graph, layerIndex, layer);
2642 RegisterOutputSlots(graph, layerIndex, layer);
2645 void IDeserializer::DeserializerImpl::ParseResize(
GraphPtr graph,
unsigned int layerIndex)
2655 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_ResizeLayer()->descriptor();
2658 descriptor.
m_TargetWidth = flatBufferDescriptor->targetWidth();
2659 descriptor.
m_TargetHeight = flatBufferDescriptor->targetHeight();
2662 descriptor.
m_AlignCorners = flatBufferDescriptor->alignCorners();
2665 auto layerName = GetLayerName(graph, layerIndex);
2666 IConnectableLayer* layer = m_Network->AddResizeLayer(descriptor, layerName.c_str());
2671 RegisterInputSlots(graph, layerIndex, layer);
2672 RegisterOutputSlots(graph, layerIndex, layer);
2678 void IDeserializer::DeserializerImpl::ParseResizeBilinear(
GraphPtr graph,
unsigned int layerIndex)
2688 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_ResizeBilinearLayer()->descriptor();
2691 descriptor.
m_TargetWidth = flatBufferDescriptor->targetWidth();
2692 descriptor.
m_TargetHeight = flatBufferDescriptor->targetHeight();
2695 descriptor.
m_AlignCorners = flatBufferDescriptor->alignCorners();
2698 auto layerName = GetLayerName(graph, layerIndex);
2699 IConnectableLayer* layer = m_Network->AddResizeLayer(descriptor, layerName.c_str());
2704 RegisterInputSlots(graph, layerIndex, layer);
2705 RegisterOutputSlots(graph, layerIndex, layer);
2708 void IDeserializer::DeserializerImpl::ParseShape(
GraphPtr graph,
unsigned int layerIndex)
2718 auto layerName = GetLayerName(graph, layerIndex);
2724 RegisterInputSlots(graph, layerIndex, layer);
2725 RegisterOutputSlots(graph, layerIndex, layer);
2728 void IDeserializer::DeserializerImpl::ParseSoftmax(
GraphPtr graph,
unsigned int layerIndex)
2739 descriptor.
m_Beta = graph->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->descriptor()->beta();
2740 descriptor.
m_Axis = graph->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->descriptor()->axis();
2741 auto layerName = GetLayerName(graph, layerIndex);
2743 IConnectableLayer* layer = m_Network->AddSoftmaxLayer(descriptor, layerName.c_str());
2748 RegisterInputSlots(graph, layerIndex, layer);
2749 RegisterOutputSlots(graph, layerIndex, layer);
2752 void IDeserializer::DeserializerImpl::ParseSpaceToBatchNd(
GraphPtr graph,
unsigned int layerIndex)
2762 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->descriptor();
2763 auto flatBufferPadList = flatBufferDescriptor->padList();
2764 auto flatBufferBlockShape = flatBufferDescriptor->blockShape();
2766 if (flatBufferPadList->size() % 2 != 0)
2768 throw ParseException(fmt::format(
"The size of the pad list must be divisible by 2 {}",
2772 std::vector<std::pair<unsigned int, unsigned int>> padList;
2773 padList.reserve(flatBufferPadList->size() / 2);
2774 for (
unsigned int i = 0; i < flatBufferPadList->size() - 1; i += 2)
2776 padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1));
2782 std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end());
2785 auto layerName = GetLayerName(graph, layerIndex);
2786 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(descriptor, layerName.c_str());
2791 RegisterInputSlots(graph, layerIndex, layer);
2792 RegisterOutputSlots(graph, layerIndex, layer);
2795 void IDeserializer::DeserializerImpl::ParseSpaceToDepth(
GraphPtr graph,
unsigned int layerIndex)
2805 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->descriptor();
2808 descriptor.
m_BlockSize = flatBufferDescriptor->blockSize();
2811 auto layerName = GetLayerName(graph, layerIndex);
2812 IConnectableLayer* layer = m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str());
2817 RegisterInputSlots(graph, layerIndex, layer);
2818 RegisterOutputSlots(graph, layerIndex, layer);
2823 unsigned int layerIndex)
2828 switch (normalizationDescriptor->normChannelType())
2830 case NormalizationAlgorithmChannel_Across:
2835 case NormalizationAlgorithmChannel_Within:
2846 switch (normalizationDescriptor->normMethodType())
2848 case NormalizationAlgorithmMethod_LocalBrightness:
2853 case NormalizationAlgorithmMethod_LocalContrast:
2864 switch (normalizationDescriptor->dataLayout())
2866 case DataLayout_NCHW:
2871 case DataLayout_NHWC:
2882 desc.
m_Alpha = normalizationDescriptor->alpha();
2883 desc.
m_Beta = normalizationDescriptor->beta();
2884 desc.
m_K = normalizationDescriptor->k();
2885 desc.
m_NormSize = normalizationDescriptor->normSize();
2890 void IDeserializer::DeserializerImpl::ParseNormalization(
GraphPtr graph,
unsigned int layerIndex)
2894 auto normalizationDes = graph->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->descriptor();
2904 auto normalizationDescriptor = GetNormalizationDescriptor(normalizationDes, layerIndex);
2905 auto layerName = GetLayerName(graph, layerIndex);
2907 IConnectableLayer* layer = m_Network->AddNormalizationLayer(normalizationDescriptor, layerName.c_str());
2910 RegisterInputSlots(graph, layerIndex, layer);
2911 RegisterOutputSlots(graph, layerIndex, layer);
2914 void IDeserializer::DeserializerImpl::ParseRsqrt(
GraphPtr graph,
unsigned int layerIndex)
2917 auto inputs = GetInputs(graph, layerIndex);
2921 auto outputs = GetOutputs(graph, layerIndex);
2924 auto layerName = GetLayerName(graph, layerIndex);
2927 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
2931 RegisterInputSlots(graph, layerIndex, layer);
2932 RegisterOutputSlots(graph, layerIndex, layer);
2935 void IDeserializer::DeserializerImpl::ParseSlice(
GraphPtr graph,
unsigned int layerIndex)
2939 auto inputs = GetInputs(graph, layerIndex);
2942 auto outputs = GetOutputs(graph, layerIndex);
2945 auto fbDescriptor = graph->layers()->Get(layerIndex)->layer_as_SliceLayer()->descriptor();
2947 auto fbBegin = fbDescriptor->begin();
2948 auto fbSize = fbDescriptor->size();
2950 if (fbBegin->size() != fbSize->size())
2952 throw ParseException(fmt::format(
"Begin and size descriptors must have the same length {}",
2957 descriptor.
m_Begin.insert(descriptor.
m_Begin.end(), fbBegin->begin(), fbBegin->end());
2958 descriptor.
m_Size.insert(descriptor.
m_Size.end(), fbSize->begin(), fbSize->end());
2960 auto layerName = GetLayerName(graph, layerIndex);
2961 IConnectableLayer* layer = m_Network->AddSliceLayer(descriptor, layerName.c_str());
2966 RegisterInputSlots(graph, layerIndex, layer);
2967 RegisterOutputSlots(graph, layerIndex, layer);
2970 void IDeserializer::DeserializerImpl::ParseStridedSlice(
GraphPtr graph,
unsigned int layerIndex)
2980 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_StridedSliceLayer()->descriptor();
2982 auto flatBufferBegin = flatBufferDescriptor->begin();
2983 auto flatBufferEnd = flatBufferDescriptor->end();
2984 auto flatBufferStride = flatBufferDescriptor->stride();
2986 if (!(flatBufferBegin->size() == flatBufferEnd->size() &&
2987 flatBufferBegin->size() == flatBufferStride->size()))
2989 throw ParseException(fmt::format(
"The size of the begin, end, and stride must be equal {}",
2993 std::vector<int> begin(flatBufferBegin->begin(), flatBufferBegin->end());
2994 std::vector<int> end(flatBufferEnd->begin(), flatBufferEnd->end());
2995 std::vector<int> stride(flatBufferStride->begin(), flatBufferStride->end());
2998 descriptor.m_BeginMask = flatBufferDescriptor->beginMask();
2999 descriptor.m_EndMask = flatBufferDescriptor->endMask();
3000 descriptor.m_ShrinkAxisMask = flatBufferDescriptor->shrinkAxisMask();
3001 descriptor.m_EllipsisMask = flatBufferDescriptor->ellipsisMask();
3002 descriptor.m_NewAxisMask = flatBufferDescriptor->newAxisMask();
3003 descriptor.m_DataLayout =
ToDataLayout(flatBufferDescriptor->dataLayout());
3005 auto layerName = GetLayerName(graph, layerIndex);
3006 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(descriptor, layerName.c_str());
3011 RegisterInputSlots(graph, layerIndex, layer);
3012 RegisterOutputSlots(graph, layerIndex, layer);
3015 void IDeserializer::DeserializerImpl::ParseSubtraction(
GraphPtr graph,
unsigned int layerIndex)
3018 auto inputs = GetInputs(graph, layerIndex);
3022 auto outputs = GetOutputs(graph, layerIndex);
3025 auto layerName = GetLayerName(graph, layerIndex);
3031 RegisterInputSlots(graph, layerIndex, layer);
3032 RegisterOutputSlots(graph, layerIndex, layer);
3035 void IDeserializer::DeserializerImpl::ParseGather(
GraphPtr graph,
unsigned int layerIndex)
3046 descriptor.
m_Axis = graph->layers()->Get(layerIndex)->layer_as_GatherLayer()->descriptor()->axis();
3048 auto layerName = GetLayerName(graph, layerIndex);
3049 IConnectableLayer* layer = m_Network->AddGatherLayer(descriptor, layerName.c_str());
3054 RegisterInputSlots(graph, layerIndex, layer);
3055 RegisterOutputSlots(graph, layerIndex, layer);
3058 void IDeserializer::DeserializerImpl::ParseGatherNd(
GraphPtr graph,
unsigned int layerIndex)
3068 auto layerName = GetLayerName(graph, layerIndex);
3074 RegisterInputSlots(graph, layerIndex, layer);
3075 RegisterOutputSlots(graph, layerIndex, layer);
3078 void IDeserializer::DeserializerImpl::ParseMean(
GraphPtr graph,
unsigned int layerIndex)
3088 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_MeanLayer()->descriptor();
3089 auto flatBufferAxis = flatBufferDescriptor->axis();
3090 auto flatBufferKeepDims = flatBufferDescriptor->keepDims();
3093 descriptor.
m_Axis = std::vector<unsigned int>(flatBufferAxis->begin(), flatBufferAxis->end());
3096 auto layerName = GetLayerName(graph, layerIndex);
3097 IConnectableLayer* layer = m_Network->AddMeanLayer(descriptor, layerName.c_str());
3102 RegisterInputSlots(graph, layerIndex, layer);
3103 RegisterOutputSlots(graph, layerIndex, layer);
3106 void IDeserializer::DeserializerImpl::ParseSplitter(
GraphPtr graph,
unsigned int layerIndex)
3115 auto flatBufferViewsDescriptor = graph->layers()->Get(layerIndex)->layer_as_SplitterLayer()->descriptor();
3116 auto flatBufferViewSizes = flatBufferViewsDescriptor->viewSizes();
3117 auto flatBufferOriginsDescriptor = flatBufferViewsDescriptor->origins();
3118 auto flatBufferViewOrigins = flatBufferOriginsDescriptor->viewOrigins();
3119 uint32_t numViews = flatBufferOriginsDescriptor->numViews();
3120 uint32_t numDimensions = flatBufferOriginsDescriptor->numDimensions();
3127 for(
unsigned int vIdx = 0; vIdx < numViews; ++vIdx)
3129 for (
unsigned int dIdx = 0; dIdx < numDimensions; ++dIdx)
3131 viewsDescriptor.SetViewSize(vIdx, dIdx, flatBufferViewSizes->Get(vIdx)->data()->Get(dIdx));
3132 viewsDescriptor.SetViewOriginCoord(vIdx, dIdx, flatBufferViewOrigins->Get(vIdx)->data()->Get(dIdx));
3136 auto layerName = GetLayerName(graph, layerIndex);
3137 IConnectableLayer* layer = m_Network->AddSplitterLayer(viewsDescriptor, layerName.c_str());
3140 for(
unsigned int vIdx = 0; vIdx < numViews; ++vIdx)
3146 RegisterInputSlots(graph, layerIndex, layer);
3147 RegisterOutputSlots(graph, layerIndex, layer);
3165 void IDeserializer::DeserializerImpl::ParseLstm(
GraphPtr graph,
unsigned int layerIndex)
3169 auto inputs = GetInputs(graph, layerIndex);
3172 auto outputs = GetOutputs(graph, layerIndex);
3175 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_LstmLayer();
3176 auto layerName = GetLayerName(graph, layerIndex);
3177 auto flatBufferDescriptor = flatBufferLayer->descriptor();
3178 auto flatBufferInputParams = flatBufferLayer->inputParams();
3180 auto lstmDescriptor = GetLstmDescriptor(flatBufferDescriptor);
3208 if (!lstmDescriptor.m_CifgEnabled)
3210 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
3211 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
3212 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
3213 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
3223 if (lstmDescriptor.m_ProjectionEnabled)
3225 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
3226 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
3234 if (lstmDescriptor.m_PeepholeEnabled)
3236 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
3237 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
3247 if (lstmDescriptor.m_LayerNormEnabled)
3249 if (!lstmDescriptor.m_CifgEnabled)
3251 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
3254 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
3255 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
3256 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
3263 IConnectableLayer* layer = m_Network->AddLstmLayer(lstmDescriptor, lstmInputParams, layerName.c_str());
3277 RegisterInputSlots(graph, layerIndex, layer);
3278 RegisterOutputSlots(graph, layerIndex, layer);
3290 desc.
m_CellClip = qLstmDescriptor->cellClip();
3304 void IDeserializer::DeserializerImpl::ParseQLstm(
GraphPtr graph,
unsigned int layerIndex)
3308 auto inputs = GetInputs(graph, layerIndex);
3311 auto outputs = GetOutputs(graph, layerIndex);
3314 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_QLstmLayer();
3315 auto layerName = GetLayerName(graph, layerIndex);
3316 auto flatBufferDescriptor = flatBufferLayer->descriptor();
3317 auto flatBufferInputParams = flatBufferLayer->inputParams();
3319 auto qLstmDescriptor = GetQLstmDescriptor(flatBufferDescriptor);
3348 if (!qLstmDescriptor.m_CifgEnabled)
3350 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
3351 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
3352 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
3363 if (qLstmDescriptor.m_ProjectionEnabled)
3365 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
3366 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
3377 if (qLstmDescriptor.m_PeepholeEnabled)
3379 if (!qLstmDescriptor.m_CifgEnabled)
3381 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
3385 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
3386 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
3398 if (qLstmDescriptor.m_LayerNormEnabled)
3400 if (!qLstmDescriptor.m_CifgEnabled)
3402 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
3406 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
3407 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
3408 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
3415 IConnectableLayer* layer = m_Network->AddQLstmLayer(qLstmDescriptor, qLstmInputParams, layerName.c_str());
3426 RegisterInputSlots(graph, layerIndex, layer);
3427 RegisterOutputSlots(graph, layerIndex, layer);
3430 void IDeserializer::DeserializerImpl::ParseQuantizedLstm(
GraphPtr graph,
unsigned int layerIndex)
3434 auto inputs = GetInputs(graph, layerIndex);
3437 auto outputs = GetOutputs(graph, layerIndex);
3440 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_QuantizedLstmLayer();
3441 auto layerName = GetLayerName(graph, layerIndex);
3442 auto flatBufferInputParams = flatBufferLayer->inputParams();
3472 IConnectableLayer* layer = m_Network->AddQuantizedLstmLayer(lstmInputParams, layerName.c_str());
3480 RegisterInputSlots(graph, layerIndex, layer);
3481 RegisterOutputSlots(graph, layerIndex, layer);
3484 void IDeserializer::DeserializerImpl::ParseDequantize(
GraphPtr graph,
unsigned int layerIndex)
3494 const std::string layerName = GetLayerName(graph, layerIndex);
3500 RegisterInputSlots(graph, layerIndex, layer);
3501 RegisterOutputSlots(graph, layerIndex, layer);
3504 void IDeserializer::DeserializerImpl::ParseMerge(
GraphPtr graph,
unsigned int layerIndex)
3514 const std::string layerName = GetLayerName(graph, layerIndex);
3520 RegisterInputSlots(graph, layerIndex, layer);
3521 RegisterOutputSlots(graph, layerIndex, layer);
3524 void IDeserializer::DeserializerImpl::ParseSwitch(
GraphPtr graph,
unsigned int layerIndex)
3527 auto inputs = GetInputs(graph, layerIndex);
3531 auto outputs = GetOutputs(graph, layerIndex);
3534 auto layerName = GetLayerName(graph, layerIndex);
3543 RegisterInputSlots(graph, layerIndex, layer);
3544 RegisterOutputSlots(graph, layerIndex, layer);
3547 void IDeserializer::DeserializerImpl::ParsePrelu(
GraphPtr graph,
unsigned int layerIndex)
3550 auto inputs = GetInputs(graph, layerIndex);
3554 auto outputs = GetOutputs(graph, layerIndex);
3557 auto layerName = GetLayerName(graph, layerIndex);
3563 RegisterInputSlots(graph, layerIndex, layer);
3564 RegisterOutputSlots(graph, layerIndex, layer);
3567 void IDeserializer::DeserializerImpl::ParseTranspose(
GraphPtr graph,
unsigned int layerIndex)
3571 auto dimsMapping = graph->layers()->Get(layerIndex)->layer_as_TransposeLayer()->descriptor()->dimMappings();
3573 auto inputs = GetInputs(graph, layerIndex);
3576 auto outputs = GetOutputs(graph, layerIndex);
3580 auto layerName = GetLayerName(graph, layerIndex);
3583 IConnectableLayer* layer = m_Network->AddTransposeLayer(descriptor, layerName.c_str());
3586 RegisterInputSlots(graph, layerIndex, layer);
3587 RegisterOutputSlots(graph, layerIndex, layer);
3590 void IDeserializer::DeserializerImpl::ParseTransposeConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
3594 auto inputs = GetInputs(graph, layerIndex);
3597 auto outputs = GetOutputs(graph, layerIndex);
3600 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_TransposeConvolution2dLayer();
3601 auto layerName = GetLayerName(graph, layerIndex);
3602 auto serializerDescriptor = serializerLayer->descriptor();
3605 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
3606 descriptor.
m_PadRight = serializerDescriptor->padRight();
3607 descriptor.
m_PadTop = serializerDescriptor->padTop();
3608 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
3609 descriptor.
m_StrideX = serializerDescriptor->strideX();
3610 descriptor.
m_StrideY = serializerDescriptor->strideY();;
3611 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();;
3620 optionalBiases = armnn::MakeOptional<armnn::ConstTensor>(biases);
3623 IConnectableLayer* layer = m_Network->AddTransposeConvolution2dLayer(descriptor,
3631 RegisterInputSlots(graph, layerIndex, layer);
3632 RegisterOutputSlots(graph, layerIndex, layer);
3635 void IDeserializer::DeserializerImpl::ParseStack(
GraphPtr graph,
unsigned int layerIndex)
3638 auto inputs = GetInputs(graph, layerIndex);
3640 auto outputs = GetOutputs(graph, layerIndex);
3643 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_StackLayer()->descriptor();
3644 unsigned int axis = flatBufferDescriptor->axis();
3645 unsigned int numInputs = flatBufferDescriptor->numInputs();
3648 auto flatBufferInputShape = flatBufferDescriptor->inputShape();
3649 std::vector<uint32_t> vectorInputShape(flatBufferInputShape->begin(),
3650 flatBufferInputShape->begin() + flatBufferInputShape->size());
3652 TensorShape inputShape(
static_cast<unsigned int>(vectorInputShape.size()), vectorInputShape.data());
3655 for (
unsigned int i=0; i<inputs.size(); ++i)
3658 if (descriptor.m_InputShape != inputShape)
3660 std::stringstream ss;
3661 ss <<
"Shape of input "
3665 <<
" does not equal defined input shape "
3666 << descriptor.m_InputShape
3673 auto layerName = GetLayerName(graph, layerIndex);
3674 IConnectableLayer* layer = m_Network->AddStackLayer(descriptor, layerName.c_str());
3679 RegisterInputSlots(graph, layerIndex, layer);
3680 RegisterOutputSlots(graph, layerIndex, layer);
3683 void IDeserializer::DeserializerImpl::ParseStandIn(
GraphPtr graph,
unsigned int layerIndex)
3687 auto inputs = GetInputs(graph, layerIndex);
3688 auto outputs = GetOutputs(graph, layerIndex);
3690 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_StandInLayer();
3691 auto fbDescriptor = fbLayer->descriptor();
3694 descriptor.
m_NumInputs = fbDescriptor->numInputs();
3700 const std::string layerName = GetLayerName(graph, layerIndex);
3703 for (
unsigned int i = 0u; i < descriptor.
m_NumOutputs; ++i)
3709 RegisterInputSlots(graph, layerIndex, layer);
3710 RegisterOutputSlots(graph, layerIndex, layer);
3730 void IDeserializer::DeserializerImpl::ParseUnidirectionalSequenceLstm(
GraphPtr graph,
unsigned int layerIndex)
3734 auto inputs = GetInputs(graph, layerIndex);
3737 auto outputs = GetOutputs(graph, layerIndex);
3740 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_UnidirectionalSequenceLstmLayer();
3741 auto layerName = GetLayerName(graph, layerIndex);
3742 auto flatBufferDescriptor = flatBufferLayer->descriptor();
3743 auto flatBufferInputParams = flatBufferLayer->inputParams();
3745 auto descriptor = GetUnidirectionalSequenceLstmDescriptor(flatBufferDescriptor);
3773 if (!descriptor.m_CifgEnabled)
3775 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
3776 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
3777 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
3783 if (descriptor.m_PeepholeEnabled)
3785 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
3792 if (descriptor.m_ProjectionEnabled)
3794 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
3795 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
3803 if (descriptor.m_PeepholeEnabled)
3805 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
3806 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
3816 if (descriptor.m_LayerNormEnabled)
3818 if (!descriptor.m_CifgEnabled)
3820 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
3823 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
3824 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
3825 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
3832 IConnectableLayer* layer = m_Network->AddUnidirectionalSequenceLstmLayer(descriptor,
3845 RegisterInputSlots(graph, layerIndex, layer);
3846 RegisterOutputSlots(graph, layerIndex, layer);