23 #include <fmt/format.h> 31 using namespace armnn;
40 const uint32_t VIRTUAL_LAYER_ID = std::numeric_limits<uint32_t>::max();
42 void CheckGraph(
const Deserializer::GraphPtr& graph,
43 unsigned int layersIndex,
46 if (graph->layers() ==
nullptr)
48 throw ParseException(fmt::format(
"{0} was called with invalid (null) graph. " 49 "Possible reason is that the graph is not yet loaded and Unpack(ed). " 55 else if (layersIndex >= graph->layers()->size())
57 throw ParseException(fmt::format(
"{0} was called with an invalid layers index. layers:{1} at {2}",
64 void CheckLayers(
const Deserializer::GraphPtr& graph,
65 unsigned int layersIndex,
66 unsigned int layerIndex,
69 if (graph->layers() ==
nullptr)
71 throw ParseException(fmt::format(
"{0} was called with invalid (null) graph. " 72 "Possible reason is that the graph is not yet loaded and Unpack(ed). " 78 else if (layersIndex >= graph->layers()->size())
80 throw ParseException(fmt::format(
"{0} was called with an invalid layers index. " 86 else if (layerIndex >= graph->layers()[layersIndex].size()
87 && layerIndex != VIRTUAL_LAYER_ID)
89 throw ParseException(fmt::format(
"{0} was called with an invalid layer index. " 90 "layers:{1} layer:{2} at {3}",
101 if (rawPtr ==
nullptr)
103 throw ParseException(fmt::format(
"{0} was called with a null tensor pointer. at {1}",
109 void CheckConstTensorPtr(Deserializer::ConstTensorRawPtr rawPtr,
112 if (rawPtr ==
nullptr)
114 throw ParseException(fmt::format(
"{0} was called with a null const tensor pointer. at {1}",
120 void CheckConstTensorSize(
const unsigned int constTensorSize,
121 const unsigned int tensorSize,
124 if (constTensorSize != tensorSize)
126 throw ParseException(fmt::format(
"{0} wrong number of components supplied to tensor. at:{1}",
132 #define CHECK_TENSOR_PTR(TENSOR_PTR) \ 133 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION()) 135 #define CHECK_CONST_TENSOR_SIZE(CONST_TENSOR_SIZE, TENSOR_SIZE) \ 136 CheckConstTensorSize(CONST_TENSOR_SIZE, TENSOR_SIZE, CHECK_LOCATION()) 138 #define CHECK_CONST_TENSOR_PTR(TENSOR_PTR) \ 139 CheckConstTensorPtr(TENSOR_PTR, CHECK_LOCATION()) 141 #define CHECK_LAYERS(GRAPH, LAYERS_INDEX, LAYER_INDEX) \ 142 CheckLayers(GRAPH, LAYERS_INDEX, LAYER_INDEX, CHECK_LOCATION()) 144 #define CHECK_GRAPH(GRAPH, LAYERS_INDEX) \ 145 CheckGraph(GRAPH, LAYERS_INDEX, CHECK_LOCATION()) 151 if (actualSize != expected.size())
156 for (
unsigned int i = 0u; i < actualSize; i++)
158 if (actual[i] != static_cast<unsigned int>(expected[i]))
167 Deserializer::Deserializer()
168 : m_Network(nullptr, nullptr),
170 m_ParserFunctions(Layer_MAX+1, &
Deserializer::ParseUnsupportedLayer)
173 m_ParserFunctions[Layer_AbsLayer] = &Deserializer::ParseAbs;
174 m_ParserFunctions[Layer_ActivationLayer] = &Deserializer::ParseActivation;
175 m_ParserFunctions[Layer_AdditionLayer] = &Deserializer::ParseAdd;
176 m_ParserFunctions[Layer_ArgMinMaxLayer] = &Deserializer::ParseArgMinMax;
177 m_ParserFunctions[Layer_BatchToSpaceNdLayer] = &Deserializer::ParseBatchToSpaceNd;
178 m_ParserFunctions[Layer_BatchNormalizationLayer] = &Deserializer::ParseBatchNormalization;
179 m_ParserFunctions[Layer_ComparisonLayer] = &Deserializer::ParseComparison;
180 m_ParserFunctions[Layer_ConcatLayer] = &Deserializer::ParseConcat;
181 m_ParserFunctions[Layer_ConstantLayer] = &Deserializer::ParseConstant;
182 m_ParserFunctions[Layer_Convolution2dLayer] = &Deserializer::ParseConvolution2d;
183 m_ParserFunctions[Layer_DepthToSpaceLayer] = &Deserializer::ParseDepthToSpace;
184 m_ParserFunctions[Layer_DepthwiseConvolution2dLayer] = &Deserializer::ParseDepthwiseConvolution2d;
185 m_ParserFunctions[Layer_DequantizeLayer] = &Deserializer::ParseDequantize;
186 m_ParserFunctions[Layer_DetectionPostProcessLayer] = &Deserializer::ParseDetectionPostProcess;
187 m_ParserFunctions[Layer_DivisionLayer] = &Deserializer::ParseDivision;
188 m_ParserFunctions[Layer_ElementwiseUnaryLayer] = &Deserializer::ParseElementwiseUnary;
189 m_ParserFunctions[Layer_EqualLayer] = &Deserializer::ParseEqual;
190 m_ParserFunctions[Layer_FullyConnectedLayer] = &Deserializer::ParseFullyConnected;
191 m_ParserFunctions[Layer_FillLayer] = &Deserializer::ParseFill;
192 m_ParserFunctions[Layer_FloorLayer] = &Deserializer::ParseFloor;
193 m_ParserFunctions[Layer_GatherLayer] = &Deserializer::ParseGather;
194 m_ParserFunctions[Layer_GreaterLayer] = &Deserializer::ParseGreater;
195 m_ParserFunctions[Layer_InstanceNormalizationLayer] = &Deserializer::ParseInstanceNormalization;
196 m_ParserFunctions[Layer_L2NormalizationLayer] = &Deserializer::ParseL2Normalization;
197 m_ParserFunctions[Layer_LogicalBinaryLayer] = &Deserializer::ParseLogicalBinary;
198 m_ParserFunctions[Layer_LogSoftmaxLayer] = &Deserializer::ParseLogSoftmax;
199 m_ParserFunctions[Layer_LstmLayer] = &Deserializer::ParseLstm;
200 m_ParserFunctions[Layer_MaximumLayer] = &Deserializer::ParseMaximum;
201 m_ParserFunctions[Layer_MeanLayer] = &Deserializer::ParseMean;
202 m_ParserFunctions[Layer_MinimumLayer] = &Deserializer::ParseMinimum;
203 m_ParserFunctions[Layer_MergeLayer] = &Deserializer::ParseMerge;
204 m_ParserFunctions[Layer_MergerLayer] = &Deserializer::ParseConcat;
205 m_ParserFunctions[Layer_MultiplicationLayer] = &Deserializer::ParseMultiplication;
206 m_ParserFunctions[Layer_NormalizationLayer] = &Deserializer::ParseNormalization;
207 m_ParserFunctions[Layer_PadLayer] = &Deserializer::ParsePad;
208 m_ParserFunctions[Layer_PermuteLayer] = &Deserializer::ParsePermute;
209 m_ParserFunctions[Layer_Pooling2dLayer] = &Deserializer::ParsePooling2d;
210 m_ParserFunctions[Layer_PreluLayer] = &Deserializer::ParsePrelu;
211 m_ParserFunctions[Layer_QLstmLayer] = &Deserializer::ParseQLstm;
212 m_ParserFunctions[Layer_QuantizeLayer] = &Deserializer::ParseQuantize;
213 m_ParserFunctions[Layer_QuantizedLstmLayer] = &Deserializer::ParseQuantizedLstm;
214 m_ParserFunctions[Layer_RankLayer] = &Deserializer::ParseRank;
215 m_ParserFunctions[Layer_ReshapeLayer] = &Deserializer::ParseReshape;
216 m_ParserFunctions[Layer_ResizeBilinearLayer] = &Deserializer::ParseResizeBilinear;
217 m_ParserFunctions[Layer_ResizeLayer] = &Deserializer::ParseResize;
218 m_ParserFunctions[Layer_RsqrtLayer] = &Deserializer::ParseRsqrt;
219 m_ParserFunctions[Layer_SliceLayer] = &Deserializer::ParseSlice;
220 m_ParserFunctions[Layer_SoftmaxLayer] = &Deserializer::ParseSoftmax;
221 m_ParserFunctions[Layer_SpaceToBatchNdLayer] = &Deserializer::ParseSpaceToBatchNd;
222 m_ParserFunctions[Layer_SpaceToDepthLayer] = &Deserializer::ParseSpaceToDepth;
223 m_ParserFunctions[Layer_SplitterLayer] = &Deserializer::ParseSplitter;
224 m_ParserFunctions[Layer_StackLayer] = &Deserializer::ParseStack;
225 m_ParserFunctions[Layer_StandInLayer] = &Deserializer::ParseStandIn;
226 m_ParserFunctions[Layer_StridedSliceLayer] = &Deserializer::ParseStridedSlice;
227 m_ParserFunctions[Layer_SubtractionLayer] = &Deserializer::ParseSubtraction;
228 m_ParserFunctions[Layer_SwitchLayer] = &Deserializer::ParseSwitch;
229 m_ParserFunctions[Layer_TransposeConvolution2dLayer] = &Deserializer::ParseTransposeConvolution2d;
230 m_ParserFunctions[Layer_TransposeLayer] = &Deserializer::ParseTranspose;
235 auto layerType = graphPtr->layers()->Get(layerIndex)->layer_type();
239 case Layer::Layer_AbsLayer:
240 return graphPtr->layers()->Get(layerIndex)->layer_as_AbsLayer()->base();
241 case Layer::Layer_ActivationLayer:
242 return graphPtr->layers()->Get(layerIndex)->layer_as_ActivationLayer()->base();
243 case Layer::Layer_AdditionLayer:
244 return graphPtr->layers()->Get(layerIndex)->layer_as_AdditionLayer()->base();
245 case Layer::Layer_ArgMinMaxLayer:
246 return graphPtr->layers()->Get(layerIndex)->layer_as_ArgMinMaxLayer()->base();
247 case Layer::Layer_BatchToSpaceNdLayer:
248 return graphPtr->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->base();
249 case Layer::Layer_BatchNormalizationLayer:
250 return graphPtr->layers()->Get(layerIndex)->layer_as_BatchNormalizationLayer()->base();
251 case Layer::Layer_ComparisonLayer:
252 return graphPtr->layers()->Get(layerIndex)->layer_as_ComparisonLayer()->base();
253 case Layer::Layer_ConcatLayer:
254 return graphPtr->layers()->Get(layerIndex)->layer_as_ConcatLayer()->base();
255 case Layer::Layer_ConstantLayer:
256 return graphPtr->layers()->Get(layerIndex)->layer_as_ConstantLayer()->base();
257 case Layer::Layer_Convolution2dLayer:
258 return graphPtr->layers()->Get(layerIndex)->layer_as_Convolution2dLayer()->base();
259 case Layer::Layer_DepthToSpaceLayer:
260 return graphPtr->layers()->Get(layerIndex)->layer_as_DepthToSpaceLayer()->base();
261 case Layer::Layer_DepthwiseConvolution2dLayer:
262 return graphPtr->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer()->base();
263 case Layer::Layer_DequantizeLayer:
264 return graphPtr->layers()->Get(layerIndex)->layer_as_DequantizeLayer()->base();
265 case Layer::Layer_DetectionPostProcessLayer:
266 return graphPtr->layers()->Get(layerIndex)->layer_as_DetectionPostProcessLayer()->base();
267 case Layer::Layer_DivisionLayer:
268 return graphPtr->layers()->Get(layerIndex)->layer_as_DivisionLayer()->base();
269 case Layer::Layer_EqualLayer:
270 return graphPtr->layers()->Get(layerIndex)->layer_as_EqualLayer()->base();
271 case Layer::Layer_ElementwiseUnaryLayer:
272 return graphPtr->layers()->Get(layerIndex)->layer_as_ElementwiseUnaryLayer()->base();
273 case Layer::Layer_FullyConnectedLayer:
274 return graphPtr->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer()->base();
275 case Layer::Layer_FillLayer:
276 return graphPtr->layers()->Get(layerIndex)->layer_as_FillLayer()->base();
277 case Layer::Layer_FloorLayer:
278 return graphPtr->layers()->Get(layerIndex)->layer_as_FloorLayer()->base();
279 case Layer::Layer_GatherLayer:
280 return graphPtr->layers()->Get(layerIndex)->layer_as_GatherLayer()->base();
281 case Layer::Layer_GreaterLayer:
282 return graphPtr->layers()->Get(layerIndex)->layer_as_GreaterLayer()->base();
283 case Layer::Layer_InputLayer:
284 return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->base();
285 case Layer::Layer_InstanceNormalizationLayer:
286 return graphPtr->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer()->base();
287 case Layer::Layer_L2NormalizationLayer:
288 return graphPtr->layers()->Get(layerIndex)->layer_as_L2NormalizationLayer()->base();
289 case Layer::Layer_LogicalBinaryLayer:
290 return graphPtr->layers()->Get(layerIndex)->layer_as_LogicalBinaryLayer()->base();
291 case Layer::Layer_LogSoftmaxLayer:
292 return graphPtr->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->base();
293 case Layer::Layer_LstmLayer:
294 return graphPtr->layers()->Get(layerIndex)->layer_as_LstmLayer()->base();
295 case Layer::Layer_MeanLayer:
296 return graphPtr->layers()->Get(layerIndex)->layer_as_MeanLayer()->base();
297 case Layer::Layer_MinimumLayer:
298 return graphPtr->layers()->Get(layerIndex)->layer_as_MinimumLayer()->base();
299 case Layer::Layer_MaximumLayer:
300 return graphPtr->layers()->Get(layerIndex)->layer_as_MaximumLayer()->base();
301 case Layer::Layer_MergeLayer:
302 return graphPtr->layers()->Get(layerIndex)->layer_as_MergeLayer()->base();
303 case Layer::Layer_MergerLayer:
304 return graphPtr->layers()->Get(layerIndex)->layer_as_MergerLayer()->base();
305 case Layer::Layer_MultiplicationLayer:
306 return graphPtr->layers()->Get(layerIndex)->layer_as_MultiplicationLayer()->base();
307 case Layer::Layer_NormalizationLayer:
308 return graphPtr->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->base();
309 case Layer::Layer_OutputLayer:
310 return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->base();
311 case Layer::Layer_PadLayer:
312 return graphPtr->layers()->Get(layerIndex)->layer_as_PadLayer()->base();
313 case Layer::Layer_PermuteLayer:
314 return graphPtr->layers()->Get(layerIndex)->layer_as_PermuteLayer()->base();
315 case Layer::Layer_Pooling2dLayer:
316 return graphPtr->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->base();
317 case Layer::Layer_PreluLayer:
318 return graphPtr->layers()->Get(layerIndex)->layer_as_PreluLayer()->base();
319 case Layer::Layer_QLstmLayer:
320 return graphPtr->layers()->Get(layerIndex)->layer_as_QLstmLayer()->base();
321 case Layer::Layer_QuantizeLayer:
322 return graphPtr->layers()->Get(layerIndex)->layer_as_QuantizeLayer()->base();
323 case Layer::Layer_QuantizedLstmLayer:
324 return graphPtr->layers()->Get(layerIndex)->layer_as_QuantizedLstmLayer()->base();
325 case Layer::Layer_RankLayer:
326 return graphPtr->layers()->Get(layerIndex)->layer_as_RankLayer()->base();
327 case Layer::Layer_ReshapeLayer:
328 return graphPtr->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->base();
329 case Layer::Layer_ResizeBilinearLayer:
330 return graphPtr->layers()->Get(layerIndex)->layer_as_ResizeBilinearLayer()->base();
331 case Layer::Layer_ResizeLayer:
332 return graphPtr->layers()->Get(layerIndex)->layer_as_ResizeLayer()->base();
333 case Layer::Layer_RsqrtLayer:
334 return graphPtr->layers()->Get(layerIndex)->layer_as_RsqrtLayer()->base();
335 case Layer::Layer_SliceLayer:
336 return graphPtr->layers()->Get(layerIndex)->layer_as_SliceLayer()->base();
337 case Layer::Layer_SoftmaxLayer:
338 return graphPtr->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->base();
339 case Layer::Layer_SpaceToBatchNdLayer:
340 return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->base();
341 case Layer::Layer_SpaceToDepthLayer:
342 return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->base();
343 case Layer::Layer_SplitterLayer:
344 return graphPtr->layers()->Get(layerIndex)->layer_as_SplitterLayer()->base();
345 case Layer::Layer_StackLayer:
346 return graphPtr->layers()->Get(layerIndex)->layer_as_StackLayer()->base();
347 case Layer::Layer_StandInLayer:
348 return graphPtr->layers()->Get(layerIndex)->layer_as_StandInLayer()->base();
349 case Layer::Layer_StridedSliceLayer:
350 return graphPtr->layers()->Get(layerIndex)->layer_as_StridedSliceLayer()->base();
351 case Layer::Layer_SubtractionLayer:
352 return graphPtr->layers()->Get(layerIndex)->layer_as_SubtractionLayer()->base();
353 case Layer::Layer_SwitchLayer:
354 return graphPtr->layers()->Get(layerIndex)->layer_as_SwitchLayer()->base();
355 case Layer::Layer_TransposeConvolution2dLayer:
356 return graphPtr->layers()->Get(layerIndex)->layer_as_TransposeConvolution2dLayer()->base();
357 case Layer::Layer_TransposeLayer:
358 return graphPtr->layers()->Get(layerIndex)->layer_as_TransposeLayer()->base();
359 case Layer::Layer_NONE:
361 throw ParseException(fmt::format(
"Layer type {} not recognized", layerType));
369 return layer->layerName()->str();
374 auto layerType = graphPtr->layers()->Get(layerIndex)->layer_type();
376 if (layerType == Layer::Layer_InputLayer)
378 return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->layerBindingId();
380 else if ( layerType == Layer::Layer_OutputLayer )
382 return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->layerBindingId();
391 case armnnSerializer::DataLayout::DataLayout_NHWC:
393 case armnnSerializer::DataLayout::DataLayout_NCHW:
403 case armnnSerializer::ActivationFunction_Sigmoid:
405 case armnnSerializer::ActivationFunction_TanH:
407 case armnnSerializer::ActivationFunction_Linear:
409 case armnnSerializer::ActivationFunction_ReLu:
411 case armnnSerializer::ActivationFunction_BoundedReLu:
413 case armnnSerializer::ActivationFunction_LeakyReLu:
415 case armnnSerializer::ActivationFunction_Abs:
417 case armnnSerializer::ActivationFunction_Sqrt:
419 case armnnSerializer::ActivationFunction_Square:
421 case armnnSerializer::ActivationFunction_Elu:
423 case armnnSerializer::ActivationFunction_HardSwish:
434 case armnnSerializer::ArgMinMaxFunction::ArgMinMaxFunction_Max:
436 case armnnSerializer::ArgMinMaxFunction::ArgMinMaxFunction_Min:
446 case armnnSerializer::ComparisonOperation::ComparisonOperation_Equal:
448 case armnnSerializer::ComparisonOperation::ComparisonOperation_Greater:
450 case armnnSerializer::ComparisonOperation::ComparisonOperation_GreaterOrEqual:
452 case armnnSerializer::ComparisonOperation::ComparisonOperation_Less:
454 case armnnSerializer::ComparisonOperation::ComparisonOperation_LessOrEqual:
456 case armnnSerializer::ComparisonOperation::ComparisonOperation_NotEqual:
466 case armnnSerializer::LogicalBinaryOperation::LogicalBinaryOperation_LogicalAnd:
468 case armnnSerializer::LogicalBinaryOperation::LogicalBinaryOperation_LogicalOr:
479 case armnnSerializer::UnaryOperation::UnaryOperation_Abs:
481 case armnnSerializer::UnaryOperation::UnaryOperation_Rsqrt:
483 case armnnSerializer::UnaryOperation::UnaryOperation_Sqrt:
485 case armnnSerializer::UnaryOperation::UnaryOperation_Exp:
487 case armnnSerializer::UnaryOperation::UnaryOperation_Neg:
489 case armnnSerializer::UnaryOperation::UnaryOperation_LogicalNot:
500 case armnnSerializer::ResizeMethod_NearestNeighbor:
502 case armnnSerializer::ResizeMethod_Bilinear:
514 switch (tensorPtr->dataType())
516 case DataType_QAsymmS8:
519 case DataType_QSymmS8:
522 case DataType_QuantisedAsymm8:
523 case DataType_QAsymmU8:
526 case DataType_QSymmS16:
527 case DataType_QuantisedSymm16:
530 case DataType_Signed32:
533 case DataType_Float32:
536 case DataType_Float16:
539 case DataType_Boolean:
545 throw ParseException(fmt::format(
"Unsupported data type {0} = {1}. {2}",
546 tensorPtr->dataType(),
547 EnumNameDataType(tensorPtr->dataType()),
552 if (tensorPtr->dimensionality() ==
static_cast<unsigned int>(Dimensionality::Scalar))
554 float quantizationScale = tensorPtr->quantizationScale();
555 int32_t quantizationOffset = tensorPtr->quantizationOffset();
563 auto dimensions = tensorPtr->dimensions();
564 unsigned int size = dimensions->size();
565 std::vector<unsigned int> outputDims(dimensions->begin(), dimensions->begin() + size);
567 auto quantizationScales = tensorPtr->quantizationScales();
569 if (quantizationScales)
571 unsigned int quantizationScalesSize = quantizationScales->size();
572 std::vector<float> scales(quantizationScales->begin(), quantizationScales->begin() + quantizationScalesSize);
573 unsigned int quantizationDim = tensorPtr->quantizationDim();
582 float quantizationScale = tensorPtr->quantizationScale();
583 int32_t quantizationOffset = tensorPtr->quantizationOffset();
599 switch (constTensorPtr->data_type())
601 case ConstTensorData_ByteData:
603 auto byteData = constTensorPtr->data_as_ByteData()->data();
607 case ConstTensorData_ShortData:
609 auto shortData = constTensorPtr->data_as_ShortData()->data();
613 case ConstTensorData_IntData:
615 auto intData = constTensorPtr->data_as_IntData()->data();
619 case ConstTensorData_LongData:
621 auto longData = constTensorPtr->data_as_LongData()->data();
628 throw ParseException(fmt::format(
"Unsupported data type {0} = {1}. {2}",
629 constTensorPtr->data_type(),
630 EnumNameConstTensorData(constTensorPtr->data_type()),
637 unsigned int layerIndex)
641 const auto& numInputs = layer->inputSlots()->size();
645 for (
unsigned int i=0; i<numInputs; ++i)
648 (layer->inputSlots()->Get(i)->connection()->sourceLayerIndex()));
649 result[i] =
GetBaseLayer(graphPtr, inputId)->outputSlots()->Get(0)->tensorInfo();
655 unsigned int layerIndex)
659 const auto& numOutputs = layer->outputSlots()->size();
663 for (
unsigned int i=0; i<numOutputs; ++i)
665 result[i] = layer->outputSlots()->Get(i)->tensorInfo();
670 void Deserializer::ParseUnsupportedLayer(
GraphPtr graph,
unsigned int layerIndex)
673 const auto layerName =
GetBaseLayer(graph, layerIndex)->layerName()->c_str();
674 throw ParseException(fmt::format(
"Layer not supported. layerIndex: {0} " 675 "layerName: {1} / {2}",
681 void Deserializer::ResetParser()
684 m_InputBindings.clear();
685 m_OutputBindings.clear();
707 return CreateNetworkFromGraph(graph);
713 std::vector<uint8_t> content((std::istreambuf_iterator<char>(binaryContent)), std::istreambuf_iterator<char>());
715 return CreateNetworkFromGraph(graph);
720 if (binaryContent ==
nullptr)
725 flatbuffers::Verifier verifier(binaryContent, len);
726 if (verifier.VerifyBuffer<SerializedGraph>() ==
false)
728 throw ParseException(fmt::format(
"Buffer doesn't conform to the expected Armnn " 729 "flatbuffers format. size:{0} {1}",
733 return GetSerializedGraph(binaryContent);
738 m_Network = INetwork::Create();
740 unsigned int layerIndex = 0;
741 for (AnyLayer
const* layer : *graph->layers())
743 if (layer->layer_type() != Layer_InputLayer &&
744 layer->layer_type() != Layer_OutputLayer)
747 auto& parserFunction = m_ParserFunctions[layer->layer_type()];
748 (this->*parserFunction)(graph, layerIndex);
753 SetupInputLayers(graph);
754 SetupOutputLayers(graph);
757 for (
auto&& graphIt : m_GraphConnections)
759 Connections& connections = graphIt.second;
760 for (
auto&& outputIt : connections.outputSlots)
762 const unsigned int outputSlotIndex = outputIt.first;
764 if (connections.inputSlots.find(outputSlotIndex) != connections.inputSlots.end())
766 for (
IInputSlot* inputSlot : connections.inputSlots[outputSlotIndex])
768 outputSlot->
Connect(*inputSlot);
774 return std::move(m_Network);
778 const std::string& name)
const 781 for (
auto inputBinding : m_InputBindings)
783 if (inputBinding.first == name)
785 return inputBinding.second;
788 throw ParseException(fmt::format(
"No input binding found for layer:{0} / {1}",
794 const std::string& name)
const 797 for (
auto outputBinding : m_OutputBindings)
799 if (outputBinding.first == name)
801 return outputBinding.second;
804 throw ParseException(fmt::format(
"No output binding found for layer:{0} / {1}",
809 unsigned int Deserializer::GetInputLayerInVector(
GraphPtr graph,
int targetId)
811 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
813 auto layer = graph->layers()->Get(i);
814 if (layer->layer_type() == Layer::Layer_InputLayer)
816 auto layerBindingId = layer->layer_as_InputLayer()->base()->layerBindingId();
817 if (layerBindingId == targetId)
823 throw ParseException(
"Input layer with given layerBindingId not found");
826 unsigned int Deserializer::GetOutputLayerInVector(
GraphPtr graph,
int targetId)
828 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
830 auto layer = graph->layers()->Get(i);
831 if (layer->layer_type() == Layer::Layer_OutputLayer)
833 auto layerBindingId = layer->layer_as_OutputLayer()->base()->layerBindingId();
834 if (layerBindingId == targetId)
840 throw ParseException(
"Output layer with given layerBindingId not found");
843 unsigned int Deserializer::GetLayerIndexInVector(
GraphPtr graph,
unsigned int targetIndex)
845 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
848 if (layer->index() == targetIndex)
856 Deserializer::FeatureVersions Deserializer::GetFeatureVersions(
GraphPtr graph)
858 Deserializer::FeatureVersions versions;
860 if (graph->featureVersions())
862 versions.m_BindingIdScheme = graph->featureVersions()->bindingIdsScheme();
868 void Deserializer::SetupInputLayers(
GraphPtr graph)
871 const unsigned int numInputs = graph->inputIds()->size();
872 m_InputBindings.clear();
873 m_InputBindings.reserve(numInputs);
875 for (
unsigned int i = 0; i < numInputs; i++)
877 unsigned int inputLayerIndex = 0xFFFFFFFF;
878 if (GetFeatureVersions(graph).m_BindingIdScheme == 0)
881 inputLayerIndex = GetLayerIndexInVector(graph, inputId);
885 const int inputId = graph->inputIds()->Get(i);
886 inputLayerIndex = GetInputLayerInVector(graph, inputId);
896 m_Network->AddInputLayer(bindingId, baseLayer->layerName()->c_str());
899 inputLayer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
900 RegisterOutputSlots(graph, inputLayerIndex, inputLayer);
903 m_InputBindings.push_back(std::make_pair(baseLayer->layerName()->c_str(), bindingInfo));
907 void Deserializer::SetupOutputLayers(
GraphPtr graph)
910 const unsigned int numOutputs = graph->outputIds()->size();
911 m_OutputBindings.clear();
912 m_OutputBindings.reserve(numOutputs);
914 for (
unsigned int i = 0; i < numOutputs; i++)
916 unsigned int outputLayerIndex = 0xFFFFFFFF;
917 if (GetFeatureVersions(graph).m_BindingIdScheme == 0)
919 const unsigned int outputId =
armnn::numeric_cast<
unsigned int>(graph->outputIds()->Get(i));
920 outputLayerIndex = GetLayerIndexInVector(graph, outputId);
924 const int outputId = graph->outputIds()->Get(i);
925 outputLayerIndex = GetOutputLayerInVector(graph, outputId);
935 m_Network->AddOutputLayer(bindingId, baseLayer->layerName()->c_str());
937 RegisterInputSlots(graph, outputLayerIndex, outputLayer);
939 unsigned int sourceLayerIndex =
940 GetLayerIndexInVector(graph, baseLayer->inputSlots()->Get(0)->connection()->sourceLayerIndex());
945 m_OutputBindings.push_back(std::make_pair(baseLayer->layerName()->c_str(), bindingInfo));
949 void Deserializer::RegisterOutputSlots(
GraphPtr graph,
958 throw ParseException(fmt::format(
"The number of outputslots ({0}) does not match the number expected ({1})" 959 " for layer index: {2} {3}",
960 baseLayer->outputSlots()->size(),
968 const unsigned int slotIndex = baseLayer->outputSlots()->Get(i)->index();
971 RegisterOutputSlotOfConnection(baseLayer->index(), slotIndex, outputSlot);
975 void Deserializer::RegisterInputSlots(
GraphPtr graph,
984 throw ParseException(fmt::format(
"The number of inputslots ({0}) does not match the number expected ({1})" 985 " for layer index:{2} {3}",
986 baseLayer->inputSlots()->size(),
994 auto fbInputSlot = baseLayer->inputSlots()->Get(i);
995 auto fbConnection = fbInputSlot->connection();
997 RegisterInputSlotOfConnection(fbConnection->sourceLayerIndex(), fbConnection->outputSlotIndex(), inputSlot);
1001 void Deserializer::RegisterInputSlotOfConnection(uint32_t sourceLayerIndex,
1002 uint32_t outputSlotIndex,
1005 if (m_GraphConnections.find(sourceLayerIndex) == m_GraphConnections.end())
1007 m_GraphConnections[sourceLayerIndex] = Connections();
1010 Connections& connections = m_GraphConnections[sourceLayerIndex];
1011 if (connections.inputSlots.find(outputSlotIndex) == connections.inputSlots.end())
1013 connections.inputSlots[outputSlotIndex] = {inputSlot};
1017 connections.inputSlots[outputSlotIndex].push_back(inputSlot);
1021 void Deserializer::RegisterOutputSlotOfConnection(uint32_t sourceLayerIndex,
1022 uint32_t outputSlotIndex,
1025 if (m_GraphConnections.find(sourceLayerIndex) == m_GraphConnections.end())
1027 m_GraphConnections[sourceLayerIndex] = Connections();
1030 Connections& connections = m_GraphConnections[sourceLayerIndex];
1031 if (connections.outputSlots.find(outputSlotIndex) != connections.outputSlots.end())
1036 connections.outputSlots[outputSlotIndex] = outputSlot;
1042 auto inputs =
GetInputs(graph, layerIndex);
1046 auto outputs =
GetOutputs(graph, layerIndex);
1052 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
1056 RegisterInputSlots(graph, layerIndex, layer);
1057 RegisterOutputSlots(graph, layerIndex, layer);
1060 void Deserializer::ParseActivation(
GraphPtr graph,
unsigned int layerIndex)
1063 auto inputs =
GetInputs(graph, layerIndex);
1067 auto outputs =
GetOutputs(graph, layerIndex);
1070 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ActivationLayer();
1072 auto serializerDescriptor = serializerLayer->descriptor();
1076 descriptor.
m_A = serializerDescriptor->a();
1077 descriptor.
m_B = serializerDescriptor->b();
1084 RegisterInputSlots(graph, layerIndex, layer);
1085 RegisterOutputSlots(graph, layerIndex, layer);
1088 void Deserializer::ParseAdd(
GraphPtr graph,
unsigned int layerIndex)
1091 auto inputs =
GetInputs(graph, layerIndex);
1095 auto outputs =
GetOutputs(graph, layerIndex);
1104 RegisterInputSlots(graph, layerIndex, layer);
1105 RegisterOutputSlots(graph, layerIndex, layer);
1108 void Deserializer::ParseArgMinMax(
GraphPtr graph,
unsigned int layerIndex)
1111 auto inputs =
GetInputs(graph, layerIndex);
1115 auto outputs =
GetOutputs(graph, layerIndex);
1118 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ArgMinMaxLayer();
1119 auto serializerDescriptor = serializerLayer->descriptor();
1123 descriptor.
m_Axis = serializerDescriptor->axis();
1125 IConnectableLayer* layer = m_Network->AddArgMinMaxLayer(descriptor, layerName.c_str());
1130 RegisterInputSlots(graph, layerIndex, layer);
1131 RegisterOutputSlots(graph, layerIndex, layer);
1134 void Deserializer::ParseBatchToSpaceNd(
GraphPtr graph,
unsigned int layerIndex)
1144 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->descriptor();
1145 auto flatBufferCrops = flatBufferDescriptor->crops();
1146 auto flatBufferBlockShape = flatBufferDescriptor->blockShape();
1148 if (flatBufferCrops->Length() % 2 != 0)
1153 std::vector<std::pair<unsigned int, unsigned int>> crops;
1154 crops.reserve(flatBufferCrops->Length() / 2);
1155 for (
unsigned int i = 0; i < flatBufferCrops->Length() - 1; i += 2)
1157 crops.emplace_back(flatBufferCrops->Get(i), flatBufferCrops->Get(i+1));
1163 std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end());
1167 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(descriptor, layerName.c_str());
1172 RegisterInputSlots(graph, layerIndex, layer);
1173 RegisterOutputSlots(graph, layerIndex, layer);
1176 void Deserializer::ParseBatchNormalization(
GraphPtr graph,
unsigned int layerIndex)
1180 auto inputs =
GetInputs(graph, layerIndex);
1183 auto outputs =
GetOutputs(graph, layerIndex);
1189 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_BatchNormalizationLayer();
1190 auto serializerDescriptor = serializerLayer->descriptor();
1193 descriptor.
m_Eps = serializerDescriptor->eps();
1207 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
1209 RegisterInputSlots(graph, layerIndex, layer);
1210 RegisterOutputSlots(graph, layerIndex, layer);
1213 void Deserializer::ParseConstant(
GraphPtr graph,
unsigned int layerIndex)
1218 auto outputs =
GetOutputs(graph, layerIndex);
1223 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ConstantLayer();
1224 auto serializerInput = serializerLayer->input();
1228 IConnectableLayer* layer = m_Network->AddConstantLayer(input, layerName.c_str());
1233 RegisterOutputSlots(graph, layerIndex, layer);
1236 void Deserializer::ParseConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
1239 auto inputs =
GetInputs(graph, layerIndex);
1243 auto outputs =
GetOutputs(graph, layerIndex);
1246 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_Convolution2dLayer();
1248 auto serializerDescriptor = serializerLayer->descriptor();
1251 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
1252 descriptor.
m_PadRight = serializerDescriptor->padRight();
1253 descriptor.
m_PadTop = serializerDescriptor->padTop();
1254 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
1255 descriptor.
m_StrideX = serializerDescriptor->strideX();
1256 descriptor.
m_StrideY = serializerDescriptor->strideY();;
1257 descriptor.
m_DilationX = serializerDescriptor->dilationX();
1258 descriptor.
m_DilationY = serializerDescriptor->dilationY();;
1259 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();;
1276 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1278 RegisterInputSlots(graph, layerIndex, layer);
1279 RegisterOutputSlots(graph, layerIndex, layer);
1282 void Deserializer::ParseDepthToSpace(
GraphPtr graph,
unsigned int layerIndex)
1286 auto inputs =
GetInputs(graph, layerIndex);
1289 auto outputs =
GetOutputs(graph, layerIndex);
1292 auto fbDescriptor = graph->layers()->Get(layerIndex)->layer_as_DepthToSpaceLayer()->descriptor();
1295 descriptor.
m_BlockSize = fbDescriptor->blockSize();
1299 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
1304 RegisterInputSlots(graph, layerIndex, layer);
1305 RegisterOutputSlots(graph, layerIndex, layer);
1308 void Deserializer::ParseDepthwiseConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
1311 auto inputs =
GetInputs(graph, layerIndex);
1315 auto outputs =
GetOutputs(graph, layerIndex);
1318 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer();
1320 auto serializerDescriptor = serializerLayer->descriptor();
1323 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
1324 descriptor.
m_PadRight = serializerDescriptor->padRight();
1325 descriptor.
m_PadTop = serializerDescriptor->padTop();
1326 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
1327 descriptor.
m_StrideX = serializerDescriptor->strideX();
1328 descriptor.
m_StrideY = serializerDescriptor->strideY();
1329 descriptor.
m_DilationX = serializerDescriptor->dilationX();
1330 descriptor.
m_DilationY = serializerDescriptor->dilationY();
1331 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();;
1343 IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(descriptor,
1349 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1351 RegisterInputSlots(graph, layerIndex, layer);
1352 RegisterOutputSlots(graph, layerIndex, layer);
1355 void Deserializer::ParseDetectionPostProcess(
GraphPtr graph,
unsigned int layerIndex)
1358 auto inputs =
GetInputs(graph, layerIndex);
1362 auto outputs =
GetOutputs(graph, layerIndex);
1365 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_DetectionPostProcessLayer();
1367 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1375 descriptor.
m_NumClasses = flatBufferDescriptor->numClasses();
1377 descriptor.
m_ScaleX = flatBufferDescriptor->scaleX();
1378 descriptor.
m_ScaleY = flatBufferDescriptor->scaleY();
1379 descriptor.
m_ScaleW = flatBufferDescriptor->scaleW();
1380 descriptor.
m_ScaleH = flatBufferDescriptor->scaleH();
1388 for (
unsigned int i = 0; i < 4; i++)
1390 layer->GetOutputSlot(i).SetTensorInfo(
ToTensorInfo(outputs[i]));
1393 RegisterInputSlots(graph, layerIndex, layer);
1394 RegisterOutputSlots(graph, layerIndex, layer);
1397 void Deserializer::ParseDivision(
GraphPtr graph,
unsigned int layerIndex)
1400 auto inputs =
GetInputs(graph, layerIndex);
1404 auto outputs =
GetOutputs(graph, layerIndex);
1413 RegisterInputSlots(graph, layerIndex, layer);
1414 RegisterOutputSlots(graph, layerIndex, layer);
1417 void Deserializer::ParseEqual(
GraphPtr graph,
unsigned int layerIndex)
1420 auto inputs =
GetInputs(graph, layerIndex);
1424 auto outputs =
GetOutputs(graph, layerIndex);
1429 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
1434 RegisterInputSlots(graph, layerIndex, layer);
1435 RegisterOutputSlots(graph, layerIndex, layer);
1438 void Deserializer::ParseFill(
GraphPtr graph,
unsigned int layerIndex)
1441 auto inputs =
GetInputs(graph, layerIndex);
1445 auto outputs =
GetOutputs(graph, layerIndex);
1450 IConnectableLayer* layer = m_Network->AddFillLayer(descriptor, layerName.c_str());
1455 RegisterInputSlots(graph, layerIndex, layer);
1456 RegisterOutputSlots(graph, layerIndex, layer);
1459 void Deserializer::ParseGreater(
GraphPtr graph,
unsigned int layerIndex)
1462 auto inputs =
GetInputs(graph, layerIndex);
1466 auto outputs =
GetOutputs(graph, layerIndex);
1471 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
1476 RegisterInputSlots(graph, layerIndex, layer);
1477 RegisterOutputSlots(graph, layerIndex, layer);
1480 void Deserializer::ParseInstanceNormalization(
GraphPtr graph,
unsigned int layerIndex)
1484 auto inputs =
GetInputs(graph, layerIndex);
1487 auto outputs =
GetOutputs(graph, layerIndex);
1490 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer();
1491 auto fbDescriptor = fbLayer->descriptor();
1494 descriptor.
m_Gamma = fbDescriptor->gamma();
1495 descriptor.
m_Beta = fbDescriptor->beta();
1496 descriptor.
m_Eps = fbDescriptor->eps();
1499 const std::string layerName =
GetLayerName(graph, layerIndex);
1502 IConnectableLayer* layer = m_Network->AddInstanceNormalizationLayer(descriptor, layerName.c_str());
1505 RegisterInputSlots(graph, layerIndex, layer);
1506 RegisterOutputSlots(graph, layerIndex, layer);
1509 void Deserializer::ParseL2Normalization(
GraphPtr graph,
unsigned int layerIndex)
1513 auto inputs =
GetInputs(graph, layerIndex);
1516 auto outputs =
GetOutputs(graph, layerIndex);
1520 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_L2NormalizationLayer();
1521 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1526 descriptor.
m_Eps = flatBufferDescriptor->eps();
1528 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(descriptor, layerName.c_str());
1531 RegisterInputSlots(graph, layerIndex, layer);
1532 RegisterOutputSlots(graph, layerIndex, layer);
1535 void Deserializer::ParseLogicalBinary(
GraphPtr graph,
unsigned int layerIndex)
1540 auto inputs =
GetInputs(graph, layerIndex);
1543 auto outputs =
GetOutputs(graph, layerIndex);
1546 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_LogicalBinaryLayer();
1547 auto fbDescriptor = fbLayer->descriptor();
1552 const std::string& layerName =
GetLayerName(graph, layerIndex);
1553 IConnectableLayer* layer = m_Network->AddLogicalBinaryLayer(descriptor, layerName.c_str());
1558 RegisterInputSlots(graph, layerIndex, layer);
1559 RegisterOutputSlots(graph, layerIndex, layer);
1562 void Deserializer::ParseLogSoftmax(
GraphPtr graph,
unsigned int layerIndex)
1573 descriptor.
m_Beta = graph->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->descriptor()->beta();
1574 descriptor.m_Axis = graph->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->descriptor()->axis();
1577 IConnectableLayer* layer = m_Network->AddLogSoftmaxLayer(descriptor, layerName.c_str());
1582 RegisterInputSlots(graph, layerIndex, layer);
1583 RegisterOutputSlots(graph, layerIndex, layer);
1586 void Deserializer::ParseMinimum(
GraphPtr graph,
unsigned int layerIndex)
1589 auto inputs =
GetInputs(graph, layerIndex);
1593 auto outputs =
GetOutputs(graph, layerIndex);
1602 RegisterInputSlots(graph, layerIndex, layer);
1603 RegisterOutputSlots(graph, layerIndex, layer);
1606 void Deserializer::ParseMaximum(
GraphPtr graph,
unsigned int layerIndex)
1609 auto inputs =
GetInputs(graph, layerIndex);
1613 auto outputs =
GetOutputs(graph, layerIndex);
1622 RegisterInputSlots(graph, layerIndex, layer);
1623 RegisterOutputSlots(graph, layerIndex, layer);
1627 unsigned int layerIndex)
1629 auto layerType = graph->layers()->Get(layerIndex)->layer_type();
1633 case Layer::Layer_ConcatLayer:
1634 return graph->layers()->Get(layerIndex)->layer_as_ConcatLayer()->descriptor();
1635 case Layer::Layer_MergerLayer:
1636 return graph->layers()->Get(layerIndex)->layer_as_MergerLayer()->descriptor();
1642 void Deserializer::ParseComparison(
GraphPtr graph,
unsigned int layerIndex)
1647 auto inputs =
GetInputs(graph, layerIndex);
1650 auto outputs =
GetOutputs(graph, layerIndex);
1653 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ComparisonLayer();
1654 auto fbDescriptor = fbLayer->descriptor();
1659 const std::string& layerName =
GetLayerName(graph, layerIndex);
1660 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
1665 RegisterInputSlots(graph, layerIndex, layer);
1666 RegisterOutputSlots(graph, layerIndex, layer);
1669 void Deserializer::ParseElementwiseUnary(
GraphPtr graph,
unsigned int layerIndex)
1674 auto inputs =
GetInputs(graph, layerIndex);
1677 auto outputs =
GetOutputs(graph, layerIndex);
1680 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ElementwiseUnaryLayer();
1681 auto fbDescriptor = fbLayer->descriptor();
1686 const std::string& layerName =
GetLayerName(graph, layerIndex);
1687 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
1692 RegisterInputSlots(graph, layerIndex, layer);
1693 RegisterOutputSlots(graph, layerIndex, layer);
1696 void Deserializer::ParseConcat(
GraphPtr graph,
unsigned int layerIndex)
1701 auto outputs =
GetOutputs(graph, layerIndex);
1706 unsigned int numViews = originsDescriptor->numViews();
1707 unsigned int numDimensions = originsDescriptor->numDimensions();
1710 auto inputs =
GetInputs(graph, layerIndex);
1714 auto originsPtr = originsDescriptor->viewOrigins();
1715 for (
unsigned int v = 0; v < numViews; ++v)
1717 auto originPtr = originsPtr->Get(v);
1718 for (
unsigned int d = 0; d < numDimensions; ++d)
1720 uint32_t value = originPtr->data()->Get(d);
1721 descriptor.SetViewOriginCoord(v, d, value);
1724 descriptor.SetConcatAxis(originsDescriptor->concatAxis());
1726 IConnectableLayer* layer = m_Network->AddConcatLayer(descriptor, layerName.c_str());
1730 RegisterInputSlots(graph, layerIndex, layer);
1731 RegisterOutputSlots(graph, layerIndex, layer);
1734 void Deserializer::ParseMultiplication(
GraphPtr graph,
unsigned int layerIndex)
1737 auto inputs =
GetInputs(graph, layerIndex);
1741 auto outputs =
GetOutputs(graph, layerIndex);
1745 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
1750 RegisterInputSlots(graph, layerIndex, layer);
1751 RegisterOutputSlots(graph, layerIndex, layer);
1754 void Deserializer::ParseFloor(
GraphPtr graph,
unsigned int layerIndex)
1759 auto inputs =
GetInputs(graph, layerIndex);
1762 auto outputs =
GetOutputs(graph, layerIndex);
1769 layer = m_Network->AddFloorLayer(layerName.c_str());
1774 RegisterInputSlots(graph, layerIndex, layer);
1775 RegisterOutputSlots(graph, layerIndex, layer);
1778 void Deserializer::ParseFullyConnected(
GraphPtr graph,
unsigned int layerIndex)
1781 auto inputs =
GetInputs(graph, layerIndex);
1785 auto outputs =
GetOutputs(graph, layerIndex);
1788 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer();
1790 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1793 fullyConnectedDescriptor.
m_BiasEnabled = flatBufferDescriptor->biasEnabled();
1800 if (flatBufferDescriptor->biasEnabled())
1805 layer = m_Network->AddFullyConnectedLayer(fullyConnectedDescriptor,
1811 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1813 RegisterInputSlots(graph, layerIndex, layer);
1814 RegisterOutputSlots(graph, layerIndex, layer);
1817 void Deserializer::ParsePad(
GraphPtr graph,
unsigned int layerIndex)
1827 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_PadLayer()->descriptor();
1828 auto flatBufferPadList = flatBufferDescriptor->padList();
1829 float padValue = flatBufferDescriptor->padValue();
1831 if (flatBufferPadList->Length() % 2 != 0)
1833 throw ParseException(fmt::format(
"The size of the pad list must be divisible by 2 {}",
1837 std::vector<std::pair<unsigned int, unsigned int>> padList;
1838 padList.reserve(flatBufferPadList->Length() / 2);
1839 for (
unsigned int i = 0; i < flatBufferPadList->Length() - 1; i += 2)
1841 padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1));
1847 IConnectableLayer* layer = m_Network->AddPadLayer(descriptor, layerName.c_str());
1852 RegisterInputSlots(graph, layerIndex, layer);
1853 RegisterOutputSlots(graph, layerIndex, layer);
1856 void Deserializer::ParsePermute(
GraphPtr graph,
unsigned int layerIndex)
1861 graph->layers()->Get(layerIndex)->layer_as_PermuteLayer()->descriptor()->dimMappings();
1863 auto inputs =
GetInputs(graph, layerIndex);
1866 auto outputs =
GetOutputs(graph, layerIndex);
1873 IConnectableLayer* layer = m_Network->AddPermuteLayer(descriptor, layerName.c_str());
1876 RegisterInputSlots(graph, layerIndex, layer);
1877 RegisterOutputSlots(graph, layerIndex, layer);
1881 unsigned int layerIndex)
1886 switch (pooling2dDesc->poolType())
1888 case PoolingAlgorithm_Average:
1893 case PoolingAlgorithm_Max:
1904 switch (pooling2dDesc->outputShapeRounding())
1906 case OutputShapeRounding_Floor:
1911 case OutputShapeRounding_Ceiling:
1922 switch (pooling2dDesc->paddingMethod())
1924 case PaddingMethod_Exclude:
1929 case PaddingMethod_IgnoreValue:
1940 switch (pooling2dDesc->dataLayout())
1942 case DataLayout_NCHW:
1947 case DataLayout_NHWC:
1959 desc.
m_PadLeft = pooling2dDesc->padLeft();
1961 desc.
m_PadTop = pooling2dDesc->padTop();
1962 desc.
m_StrideX = pooling2dDesc->strideX();
1963 desc.
m_StrideY = pooling2dDesc->strideY();
1970 void Deserializer::ParsePooling2d(
GraphPtr graph,
unsigned int layerIndex)
1974 auto pooling2dDes = graph->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->descriptor();
1975 auto inputs =
GetInputs(graph, layerIndex);
1978 auto outputs =
GetOutputs(graph, layerIndex);
1984 IConnectableLayer* layer = m_Network->AddPooling2dLayer(pooling2dDescriptor, layerName.c_str());
1987 RegisterInputSlots(graph, layerIndex, layer);
1988 RegisterOutputSlots(graph, layerIndex, layer);
1991 void Deserializer::ParseQuantize(
GraphPtr graph,
unsigned int layerIndex)
1995 auto inputs =
GetInputs(graph, layerIndex);
1998 auto outputs =
GetOutputs(graph, layerIndex);
2006 RegisterInputSlots(graph, layerIndex, layer);
2007 RegisterOutputSlots(graph, layerIndex, layer);
2011 const std::vector<uint32_t>& targetDimsIn)
2013 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2014 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2016 if (stretchDim != targetDimsIn.end())
2018 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2020 throw ParseException(fmt::format(
"At most one component of shape can be -1 {}",
2024 auto targetNumElements =
2026 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2028 auto stretchIndex =
static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2029 outputDims[stretchIndex] = inputTensorInfo.
GetNumElements() / targetNumElements;
2040 void Deserializer::ParseRank(
GraphPtr graph,
unsigned int layerIndex)
2056 RegisterInputSlots(graph, layerIndex, layer);
2057 RegisterOutputSlots(graph, layerIndex, layer);
2060 void Deserializer::ParseReshape(
GraphPtr graph,
unsigned int layerIndex)
2063 auto inputs =
GetInputs(graph, layerIndex);
2065 auto outputs =
GetOutputs(graph, layerIndex);
2071 const auto targetDims = graph->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->descriptor()->targetShape();
2072 std::vector<uint32_t> outputDims(targetDims->begin(), targetDims->begin() + targetDims->size());
2075 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
2077 const std::vector<uint32_t> expectedDims(outputs[0]->dimensions()->begin(),
2078 outputs[0]->dimensions()->begin() + outputs[0]->dimensions()->size());
2080 if (inputs.size() > 1 && !
CheckShape(reshapeOutputTensorShape, expectedDims))
2082 std::stringstream ss;
2083 ss <<
"New shape defined in reshape parameters " 2084 << reshapeOutputTensorShape
2085 <<
" does not equal output shape " 2086 << actualOutputTensorInfo.
GetShape()
2096 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2099 RegisterInputSlots(graph, layerIndex, layer);
2100 RegisterOutputSlots(graph, layerIndex, layer);
2103 void Deserializer::ParseResize(
GraphPtr graph,
unsigned int layerIndex)
2113 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_ResizeLayer()->descriptor();
2116 descriptor.
m_TargetWidth = flatBufferDescriptor->targetWidth();
2117 descriptor.
m_TargetHeight = flatBufferDescriptor->targetHeight();
2120 descriptor.
m_AlignCorners = flatBufferDescriptor->alignCorners();
2124 IConnectableLayer* layer = m_Network->AddResizeLayer(descriptor, layerName.c_str());
2129 RegisterInputSlots(graph, layerIndex, layer);
2130 RegisterOutputSlots(graph, layerIndex, layer);
2133 void Deserializer::ParseResizeBilinear(
GraphPtr graph,
unsigned int layerIndex)
2143 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_ResizeBilinearLayer()->descriptor();
2146 descriptor.
m_TargetWidth = flatBufferDescriptor->targetWidth();
2147 descriptor.
m_TargetHeight = flatBufferDescriptor->targetHeight();
2150 descriptor.
m_AlignCorners = flatBufferDescriptor->alignCorners();
2154 IConnectableLayer* layer = m_Network->AddResizeLayer(descriptor, layerName.c_str());
2159 RegisterInputSlots(graph, layerIndex, layer);
2160 RegisterOutputSlots(graph, layerIndex, layer);
2163 void Deserializer::ParseSoftmax(
GraphPtr graph,
unsigned int layerIndex)
2174 descriptor.
m_Beta = graph->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->descriptor()->beta();
2177 IConnectableLayer* layer = m_Network->AddSoftmaxLayer(descriptor, layerName.c_str());
2182 RegisterInputSlots(graph, layerIndex, layer);
2183 RegisterOutputSlots(graph, layerIndex, layer);
2186 void Deserializer::ParseSpaceToBatchNd(
GraphPtr graph,
unsigned int layerIndex)
2196 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->descriptor();
2197 auto flatBufferPadList = flatBufferDescriptor->padList();
2198 auto flatBufferBlockShape = flatBufferDescriptor->blockShape();
2200 if (flatBufferPadList->Length() % 2 != 0)
2202 throw ParseException(fmt::format(
"The size of the pad list must be divisible by 2 {}",
2206 std::vector<std::pair<unsigned int, unsigned int>> padList;
2207 padList.reserve(flatBufferPadList->Length() / 2);
2208 for (
unsigned int i = 0; i < flatBufferPadList->Length() - 1; i += 2)
2210 padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1));
2216 std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end());
2220 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(descriptor, layerName.c_str());
2225 RegisterInputSlots(graph, layerIndex, layer);
2226 RegisterOutputSlots(graph, layerIndex, layer);
2229 void Deserializer::ParseSpaceToDepth(
GraphPtr graph,
unsigned int layerIndex)
2239 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->descriptor();
2242 descriptor.
m_BlockSize = flatBufferDescriptor->blockSize();
2246 IConnectableLayer* layer = m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str());
2251 RegisterInputSlots(graph, layerIndex, layer);
2252 RegisterOutputSlots(graph, layerIndex, layer);
2257 unsigned int layerIndex)
2262 switch (normalizationDescriptor->normChannelType())
2264 case NormalizationAlgorithmChannel_Across:
2269 case NormalizationAlgorithmChannel_Within:
2280 switch (normalizationDescriptor->normMethodType())
2282 case NormalizationAlgorithmMethod_LocalBrightness:
2287 case NormalizationAlgorithmMethod_LocalContrast:
2298 switch (normalizationDescriptor->dataLayout())
2300 case DataLayout_NCHW:
2305 case DataLayout_NHWC:
2316 desc.
m_Alpha = normalizationDescriptor->alpha();
2317 desc.
m_Beta = normalizationDescriptor->beta();
2318 desc.
m_K = normalizationDescriptor->k();
2319 desc.
m_NormSize = normalizationDescriptor->normSize();
2324 void Deserializer::ParseNormalization(
GraphPtr graph,
unsigned int layerIndex)
2328 auto normalizationDes = graph->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->descriptor();
2341 IConnectableLayer* layer = m_Network->AddNormalizationLayer(normalizationDescriptor, layerName.c_str());
2344 RegisterInputSlots(graph, layerIndex, layer);
2345 RegisterOutputSlots(graph, layerIndex, layer);
2348 void Deserializer::ParseRsqrt(
GraphPtr graph,
unsigned int layerIndex)
2351 auto inputs =
GetInputs(graph, layerIndex);
2355 auto outputs =
GetOutputs(graph, layerIndex);
2361 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
2365 RegisterInputSlots(graph, layerIndex, layer);
2366 RegisterOutputSlots(graph, layerIndex, layer);
2369 void Deserializer::ParseSlice(
GraphPtr graph,
unsigned int layerIndex)
2373 auto inputs =
GetInputs(graph, layerIndex);
2376 auto outputs =
GetOutputs(graph, layerIndex);
2379 auto fbDescriptor = graph->layers()->Get(layerIndex)->layer_as_SliceLayer()->descriptor();
2381 auto fbBegin = fbDescriptor->begin();
2382 auto fbSize = fbDescriptor->size();
2384 if (fbBegin->Length() != fbSize->Length())
2386 throw ParseException(fmt::format(
"Begin and size descriptors must have the same length {}",
2391 descriptor.
m_Begin.insert(descriptor.
m_Begin.end(), fbBegin->begin(), fbBegin->end());
2392 descriptor.
m_Size.insert(descriptor.
m_Size.end(), fbSize->begin(), fbSize->end());
2395 IConnectableLayer* layer = m_Network->AddSliceLayer(descriptor, layerName.c_str());
2400 RegisterInputSlots(graph, layerIndex, layer);
2401 RegisterOutputSlots(graph, layerIndex, layer);
2404 void Deserializer::ParseStridedSlice(
GraphPtr graph,
unsigned int layerIndex)
2414 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_StridedSliceLayer()->descriptor();
2416 auto flatBufferBegin = flatBufferDescriptor->begin();
2417 auto flatBufferEnd = flatBufferDescriptor->end();
2418 auto flatBufferStride = flatBufferDescriptor->stride();
2420 if (!(flatBufferBegin->Length() == flatBufferEnd->Length() &&
2421 flatBufferBegin->Length() == flatBufferStride->Length()))
2423 throw ParseException(fmt::format(
"The size of the begin, end, and stride must be equal {}",
2427 std::vector<int> begin(flatBufferBegin->begin(), flatBufferBegin->end());
2428 std::vector<int> end(flatBufferEnd->begin(), flatBufferEnd->end());
2429 std::vector<int> stride(flatBufferStride->begin(), flatBufferStride->end());
2432 descriptor.m_BeginMask = flatBufferDescriptor->beginMask();
2433 descriptor.m_EndMask = flatBufferDescriptor->endMask();
2434 descriptor.m_ShrinkAxisMask = flatBufferDescriptor->shrinkAxisMask();
2435 descriptor.m_EllipsisMask = flatBufferDescriptor->ellipsisMask();
2436 descriptor.m_NewAxisMask = flatBufferDescriptor->newAxisMask();
2437 descriptor.m_DataLayout =
ToDataLayout(flatBufferDescriptor->dataLayout());
2440 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(descriptor, layerName.c_str());
2445 RegisterInputSlots(graph, layerIndex, layer);
2446 RegisterOutputSlots(graph, layerIndex, layer);
2449 void Deserializer::ParseSubtraction(
GraphPtr graph,
unsigned int layerIndex)
2452 auto inputs =
GetInputs(graph, layerIndex);
2456 auto outputs =
GetOutputs(graph, layerIndex);
2465 RegisterInputSlots(graph, layerIndex, layer);
2466 RegisterOutputSlots(graph, layerIndex, layer);
2469 void Deserializer::ParseGather(
GraphPtr graph,
unsigned int layerIndex)
2480 descriptor.
m_Axis = graph->layers()->Get(layerIndex)->layer_as_GatherLayer()->descriptor()->axis();
2483 IConnectableLayer* layer = m_Network->AddGatherLayer(descriptor, layerName.c_str());
2488 RegisterInputSlots(graph, layerIndex, layer);
2489 RegisterOutputSlots(graph, layerIndex, layer);
2492 void Deserializer::ParseMean(
GraphPtr graph,
unsigned int layerIndex)
2502 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_MeanLayer()->descriptor();
2503 auto flatBufferAxis = flatBufferDescriptor->axis();
2504 auto flatBufferKeepDims = flatBufferDescriptor->keepDims();
2507 descriptor.
m_Axis = std::vector<unsigned int>(flatBufferAxis->begin(), flatBufferAxis->end());
2511 IConnectableLayer* layer = m_Network->AddMeanLayer(descriptor, layerName.c_str());
2516 RegisterInputSlots(graph, layerIndex, layer);
2517 RegisterOutputSlots(graph, layerIndex, layer);
2520 void Deserializer::ParseSplitter(
GraphPtr graph,
unsigned int layerIndex)
2529 auto flatBufferViewsDescriptor = graph->layers()->Get(layerIndex)->layer_as_SplitterLayer()->descriptor();
2530 auto flatBufferViewSizes = flatBufferViewsDescriptor->viewSizes();
2531 auto flatBufferOriginsDescriptor = flatBufferViewsDescriptor->origins();
2532 auto flatBufferViewOrigins = flatBufferOriginsDescriptor->viewOrigins();
2533 uint32_t numViews = flatBufferOriginsDescriptor->numViews();
2534 uint32_t numDimensions = flatBufferOriginsDescriptor->numDimensions();
2541 for(
unsigned int vIdx = 0; vIdx < numViews; ++vIdx)
2543 for (
unsigned int dIdx = 0; dIdx < numDimensions; ++dIdx)
2545 viewsDescriptor.
SetViewSize(vIdx, dIdx, flatBufferViewSizes->Get(vIdx)->data()->Get(dIdx));
2546 viewsDescriptor.
SetViewOriginCoord(vIdx, dIdx, flatBufferViewOrigins->Get(vIdx)->data()->Get(dIdx));
2551 IConnectableLayer* layer = m_Network->AddSplitterLayer(viewsDescriptor, layerName.c_str());
2554 for(
unsigned int vIdx = 0; vIdx < numViews; ++vIdx)
2560 RegisterInputSlots(graph, layerIndex, layer);
2561 RegisterOutputSlots(graph, layerIndex, layer);
2579 void Deserializer::ParseLstm(
GraphPtr graph,
unsigned int layerIndex)
2583 auto inputs =
GetInputs(graph, layerIndex);
2586 auto outputs =
GetOutputs(graph, layerIndex);
2589 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_LstmLayer();
2591 auto flatBufferDescriptor = flatBufferLayer->descriptor();
2592 auto flatBufferInputParams = flatBufferLayer->inputParams();
2622 if (!lstmDescriptor.m_CifgEnabled)
2624 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
2625 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
2626 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
2627 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
2637 if (lstmDescriptor.m_ProjectionEnabled)
2639 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
2640 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
2648 if (lstmDescriptor.m_PeepholeEnabled)
2650 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
2651 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
2661 if (lstmDescriptor.m_LayerNormEnabled)
2663 if (!lstmDescriptor.m_CifgEnabled)
2665 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
2668 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
2669 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
2670 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
2677 IConnectableLayer* layer = m_Network->AddLstmLayer(lstmDescriptor, lstmInputParams, layerName.c_str());
2691 RegisterInputSlots(graph, layerIndex, layer);
2692 RegisterOutputSlots(graph, layerIndex, layer);
2704 desc.
m_CellClip = qLstmDescriptor->cellClip();
2718 void Deserializer::ParseQLstm(
GraphPtr graph,
unsigned int layerIndex)
2722 auto inputs =
GetInputs(graph, layerIndex);
2725 auto outputs =
GetOutputs(graph, layerIndex);
2728 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_QLstmLayer();
2730 auto flatBufferDescriptor = flatBufferLayer->descriptor();
2731 auto flatBufferInputParams = flatBufferLayer->inputParams();
2762 if (!qLstmDescriptor.m_CifgEnabled)
2764 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
2765 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
2766 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
2777 if (qLstmDescriptor.m_ProjectionEnabled)
2779 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
2780 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
2791 if (qLstmDescriptor.m_PeepholeEnabled)
2793 if (!qLstmDescriptor.m_CifgEnabled)
2795 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
2799 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
2800 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
2812 if (qLstmDescriptor.m_LayerNormEnabled)
2814 if (!qLstmDescriptor.m_CifgEnabled)
2816 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
2820 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
2821 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
2822 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
2829 IConnectableLayer* layer = m_Network->AddQLstmLayer(qLstmDescriptor, qLstmInputParams, layerName.c_str());
2840 RegisterInputSlots(graph, layerIndex, layer);
2841 RegisterOutputSlots(graph, layerIndex, layer);
2844 void Deserializer::ParseQuantizedLstm(
GraphPtr graph,
unsigned int layerIndex)
2848 auto inputs =
GetInputs(graph, layerIndex);
2851 auto outputs =
GetOutputs(graph, layerIndex);
2854 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_QuantizedLstmLayer();
2856 auto flatBufferInputParams = flatBufferLayer->inputParams();
2886 IConnectableLayer* layer = m_Network->AddQuantizedLstmLayer(lstmInputParams, layerName.c_str());
2894 RegisterInputSlots(graph, layerIndex, layer);
2895 RegisterOutputSlots(graph, layerIndex, layer);
2898 void Deserializer::ParseDequantize(
GraphPtr graph,
unsigned int layerIndex)
2908 const std::string layerName =
GetLayerName(graph, layerIndex);
2914 RegisterInputSlots(graph, layerIndex, layer);
2915 RegisterOutputSlots(graph, layerIndex, layer);
2918 void Deserializer::ParseMerge(
GraphPtr graph,
unsigned int layerIndex)
2928 const std::string layerName =
GetLayerName(graph, layerIndex);
2934 RegisterInputSlots(graph, layerIndex, layer);
2935 RegisterOutputSlots(graph, layerIndex, layer);
2938 void Deserializer::ParseSwitch(
GraphPtr graph,
unsigned int layerIndex)
2941 auto inputs =
GetInputs(graph, layerIndex);
2945 auto outputs =
GetOutputs(graph, layerIndex);
2957 RegisterInputSlots(graph, layerIndex, layer);
2958 RegisterOutputSlots(graph, layerIndex, layer);
2961 void Deserializer::ParsePrelu(
GraphPtr graph,
unsigned int layerIndex)
2964 auto inputs =
GetInputs(graph, layerIndex);
2968 auto outputs =
GetOutputs(graph, layerIndex);
2977 RegisterInputSlots(graph, layerIndex, layer);
2978 RegisterOutputSlots(graph, layerIndex, layer);
2981 void Deserializer::ParseTranspose(
GraphPtr graph,
unsigned int layerIndex)
2985 auto dimsMapping = graph->layers()->Get(layerIndex)->layer_as_TransposeLayer()->descriptor()->dimMappings();
2987 auto inputs =
GetInputs(graph, layerIndex);
2990 auto outputs =
GetOutputs(graph, layerIndex);
2997 IConnectableLayer* layer = m_Network->AddTransposeLayer(descriptor, layerName.c_str());
3000 RegisterInputSlots(graph, layerIndex, layer);
3001 RegisterOutputSlots(graph, layerIndex, layer);
3004 void Deserializer::ParseTransposeConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
3008 auto inputs =
GetInputs(graph, layerIndex);
3011 auto outputs =
GetOutputs(graph, layerIndex);
3014 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_TransposeConvolution2dLayer();
3016 auto serializerDescriptor = serializerLayer->descriptor();
3019 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
3020 descriptor.
m_PadRight = serializerDescriptor->padRight();
3021 descriptor.
m_PadTop = serializerDescriptor->padTop();
3022 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
3023 descriptor.
m_StrideX = serializerDescriptor->strideX();
3024 descriptor.
m_StrideY = serializerDescriptor->strideY();;
3025 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();;
3034 optionalBiases = armnn::MakeOptional<armnn::ConstTensor>(biases);
3037 IConnectableLayer* layer = m_Network->AddTransposeConvolution2dLayer(descriptor,
3043 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3045 RegisterInputSlots(graph, layerIndex, layer);
3046 RegisterOutputSlots(graph, layerIndex, layer);
3049 void Deserializer::ParseStack(
GraphPtr graph,
unsigned int layerIndex)
3052 auto inputs =
GetInputs(graph, layerIndex);
3054 auto outputs =
GetOutputs(graph, layerIndex);
3057 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_StackLayer()->descriptor();
3058 unsigned int axis = flatBufferDescriptor->axis();
3059 unsigned int numInputs = flatBufferDescriptor->numInputs();
3062 auto flatBufferInputShape = flatBufferDescriptor->inputShape();
3063 std::vector<uint32_t> vectorInputShape(flatBufferInputShape->begin(),
3064 flatBufferInputShape->begin() + flatBufferInputShape->size());
3066 TensorShape inputShape(static_cast<unsigned int>(vectorInputShape.size()), vectorInputShape.data());
3069 for (
unsigned int i=0; i<inputs.size(); ++i)
3072 if (descriptor.m_InputShape != inputShape)
3074 std::stringstream ss;
3075 ss <<
"Shape of input " 3079 <<
" does not equal defined input shape " 3080 << descriptor.m_InputShape
3088 IConnectableLayer* layer = m_Network->AddStackLayer(descriptor, layerName.c_str());
3093 RegisterInputSlots(graph, layerIndex, layer);
3094 RegisterOutputSlots(graph, layerIndex, layer);
3097 void Deserializer::ParseStandIn(
GraphPtr graph,
unsigned int layerIndex)
3101 auto inputs =
GetInputs(graph, layerIndex);
3102 auto outputs =
GetOutputs(graph, layerIndex);
3104 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_StandInLayer();
3105 auto fbDescriptor = fbLayer->descriptor();
3108 descriptor.
m_NumInputs = fbDescriptor->numInputs();
3114 const std::string layerName =
GetLayerName(graph, layerIndex);
3117 for (
unsigned int i = 0u; i < descriptor.
m_NumOutputs; ++i)
3123 RegisterInputSlots(graph, layerIndex, layer);
3124 RegisterOutputSlots(graph, layerIndex, layer);
armnn::ConstTensor ToConstTensor(Deserializer::ConstTensorRawPtr constTensorPtr)
static armnn::LstmDescriptor GetLstmDescriptor(LstmDescriptorPtr lstmDescriptor)
uint32_t m_PadBottom
Padding bottom value in the height dimension.
bool m_BiasEnabled
Enable/disable bias.
float m_Eps
Used to avoid dividing by zero.
virtual unsigned int GetNumOutputSlots() const =0
Returns the number of connectable output slots.
armnn::LogicalBinaryOperation ToLogicalBinaryOperation(armnnSerializer::LogicalBinaryOperation operation)
bool m_ProjectionEnabled
Enable/disable the projection layer.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
UnaryOperation m_Operation
Specifies the elementwiseUnary operation to execute.
A ViewsDescriptor for the SplitterLayer.
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
float m_ScaleW
Center size encoding scale weight.
#define CHECK_LAYERS(GRAPH, LAYERS_INDEX, LAYER_INDEX)
uint32_t m_PadBottom
Padding bottom value in the height dimension.
bool m_BiasEnabled
Enable/disable bias.
static GraphPtr LoadGraphFromBinary(const uint8_t *binaryContent, size_t len)
virtual unsigned int GetNumInputSlots() const =0
Returns the number of connectable input slots.
float m_K
Kappa value used for the across channel normalization equation.
A TransposeConvolution2dDescriptor for the TransposeConvolution2dLayer.
const TensorShape & GetShape() const
uint32_t m_PadBottom
Padding bottom value in the height dimension.
uint32_t m_PadLeft
Padding left value in the width dimension.
float m_ClippingThresProj
Clipping threshold value for the projection.
std::string AsString() const
static armnn::NormalizationDescriptor GetNormalizationDescriptor(NormalizationDescriptorPtr normalizationDescriptor, unsigned int layerIndex)
A ReshapeDescriptor for the ReshapeLayer.
static void Destroy(IDeserializer *parser)
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
A ComparisonDescriptor for the ComparisonLayer.
float m_ScaleX
Center size encoding scale x.
bool m_TransposeWeightMatrix
Enable/disable transpose weight matrix.
uint32_t m_PoolWidth
Pooling width value.
bool m_PeepholeEnabled
Enable/disable peephole.
#define CHECK_TENSOR_PTR(TENSOR_PTR)
A Convolution2dDescriptor for the Convolution2dLayer.
float m_Alpha
Alpha value for the normalization equation.
const armnnSerializer::TensorInfo * TensorRawPtr
const armnnSerializer::NormalizationDescriptor * NormalizationDescriptorPtr
uint32_t m_PadLeft
Padding left value in the width dimension.
float m_HiddenStateScale
Hidden State quantization scale.
bool m_BiasEnabled
Enable/disable bias.
float m_OutputIntermediateScale
Output intermediate quantization scale.
ResizeMethod m_Method
The Interpolation method to use (Bilinear, NearestNeighbor).
float m_Gamma
Gamma, the scale scalar value applied for the normalized tensor. Defaults to 1.0. ...
float m_Beta
Exponentiation value.
std::vector< unsigned int > m_Size
Size of the slice in each dimension.
The padding fields don't count and are ignored.
float m_Eps
Value to add to the variance. Used to avoid dividing by zero.
PaddingMethod m_PaddingMethod
The padding method to be used. (Exclude, IgnoreValue).
ArgMinMaxFunction m_Function
Specify if the function is to find Min or Max.
uint32_t m_DetectionsPerClass
Detections per classes, used in Regular NMS.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
void CheckLayers(Graph &graph)
static armnn::QLstmDescriptor GetQLstmDescriptor(QLstmDescriptorPtr qLstmDescriptorPtr)
static IDeserializerPtr Create()
uint32_t m_PadTop
Padding top value in the height dimension.
A LogicalBinaryDescriptor for the LogicalBinaryLayer.
uint32_t m_PadRight
Padding right value in the width dimension.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Copyright (c) 2020 ARM Limited.
void IgnoreUnused(Ts &&...)
armnn::INetworkPtr CreateNetworkFromBinary(const std::vector< uint8_t > &binaryContent) override
Create an input network from binary file contents.
uint32_t m_PadBottom
Padding bottom value in the height dimension.
#define CHECK_GRAPH(GRAPH, LAYERS_INDEX)
static std::string GetLayerName(const GraphPtr &graph, unsigned int index)
uint32_t m_DilationY
Dilation along y axis.
A SpaceToDepthDescriptor for the SpaceToDepthLayer.
std::vector< std::pair< unsigned int, unsigned int > > m_PadList
Specifies the padding values for the input dimension: heightPad{top, bottom} widthPad{left, right}.
uint32_t m_DilationY
Dilation factor value for height dimension.
LogicalBinaryOperation m_Operation
Specifies the logical operation to execute.
A BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
BindingPointInfo GetNetworkInputBindingInfo(unsigned int layerId, const std::string &name) const override
Retrieve binding info (layer id and tensor info) for the network input identified by the given layer ...
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
armnn::ComparisonOperation ToComparisonOperation(armnnSerializer::ComparisonOperation operation)
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
uint32_t m_NumOutputs
Number of output tensors.
NormalizationAlgorithmMethod m_NormMethodType
Normalization method algorithm to use (LocalBrightness, LocalContrast).
void SetShape(const TensorShape &newShape)
A ResizeDescriptor for the ResizeLayer.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
uint32_t m_MaxClassesPerDetection
Maximum numbers of classes per detection, used in Fast NMS.
std::vector< unsigned int > m_Axis
Values for the dimensions to reduce.
A StackDescriptor for the StackLayer.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
TensorShape m_TargetShape
Target shape value.
uint32_t m_PoolHeight
Pooling height value.
uint32_t m_PadTop
Padding top value in the height dimension.
uint32_t m_MaxDetections
Maximum numbers of detections.
A PadDescriptor for the PadLayer.
static int32_t GetBindingLayerInfo(const GraphPtr &graphPtr, unsigned int layerIndex)
const armnnSerializer::Pooling2dDescriptor * PoolingDescriptor
#define CHECK_CONST_TENSOR_SIZE(CONST_TENSOR_SIZE, TENSOR_SIZE)
armnn::TensorInfo ToTensorInfo(Deserializer::TensorRawPtr tensorPtr)
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
bool m_LayerNormEnabled
Enable/disable layer normalization.
armnn::DataLayout ToDataLayout(armnnSerializer::DataLayout dataLayout)
bool CheckShape(const armnn::TensorShape &actual, const std::vector< uint32_t > &expected)
float m_NmsIouThreshold
Intersection over union threshold.
An LstmDescriptor for the LstmLayer.
uint32_t m_PadRight
Padding right value in the width dimension.
uint32_t m_DilationX
Dilation factor value for width dimension.
uint32_t m_PadTop
Padding top value in the height dimension.
std::string FileLine() const
Status SetViewSize(uint32_t view, uint32_t coord, uint32_t value)
Set the size of the views.
#define ARMNN_ASSERT_MSG(COND, MSG)
std::vector< unsigned int > m_Begin
Beginning indices of the slice in each dimension.
bool m_KeepDims
Enable/disable keep dimensions. If true, then the reduced dimensions that are of length 1 are kept...
armnnSerializer::TensorInfo * TensorRawPtr
std::vector< unsigned int > m_BlockShape
Block shape values.
float m_Eps
Epsilon, small scalar value added to variance to avoid dividing by zero. Defaults to 1e-12f...
An output connection slot for a layer.
A L2NormalizationDescriptor for the L2NormalizationLayer.
An ArgMinMaxDescriptor for ArgMinMaxLayer.
An OriginsDescriptor for the ConcatLayer.
float m_ProjectionClip
Clipping threshold value for the projection.
static LayerBaseRawPtr GetBaseLayer(const GraphPtr &graphPtr, unsigned int layerIndex)
A FullyConnectedDescriptor for the FullyConnectedLayer.
bool m_BiasEnabled
Enable/disable bias.
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
float m_InputIntermediateScale
Input intermediate quantization scale.
uint32_t m_TargetWidth
Target width value.
A GatherDescriptor for the GatherLayer.
#define CHECK_VALID_SIZE(ACTUAL,...)
bool m_PeepholeEnabled
Enable/disable peephole.
uint32_t m_NumClasses
Number of classes.
#define CHECKED_NON_NEGATIVE(VALUE)
bool m_HalfPixelCenters
Half Pixel Centers.
std::unique_ptr< IDeserializer, void(*)(IDeserializer *parser)> IDeserializerPtr
armnn::ActivationFunction ToActivationFunction(armnnSerializer::ActivationFunction function)
uint32_t m_PadTop
Padding top value in the height dimension.
armnn::UnaryOperation ToUnaryOperation(armnnSerializer::UnaryOperation operation)
#define ARMNN_ASSERT(COND)
A StandInDescriptor for the StandIn layer.
A QLstmDescriptor for the QLstmLayer.
#define CHECK_CONST_TENSOR_PTR(TENSOR_PTR)
bool m_UseRegularNms
Use Regular NMS.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
BindingPointInfo GetNetworkOutputBindingInfo(unsigned int layerId, const std::string &name) const override
Retrieve binding info (layer id and tensor info) for the network output identified by the given layer...
std::vector< unsigned int > m_BlockShape
Block shape value.
An ActivationDescriptor for the ActivationLayer.
min(a, max(b, input)) ReLu1 & ReLu6.
std::vector< TensorRawPtr > TensorRawPtrVector
uint32_t m_TargetHeight
Target height value.
uint32_t m_ActivationFunc
The activation function to use.
A SliceDescriptor for the SliceLayer.
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
float m_ClippingThresCell
Clipping threshold value for the cell state.
unsigned int m_BlockSize
Scalar specifying the input block size. It must be >= 1.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
static TensorRawPtrVector GetOutputs(const GraphPtr &graph, unsigned int layerIndex)
float m_ForgetIntermediateScale
Forget intermediate quantization scale.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
float m_Beta
Beta, the offset scalar value applied for the normalized tensor. Defaults to 1.0. ...
armnn::ResizeMethod ToResizeMethod(armnnSerializer::ResizeMethod method)
float m_ScaleH
Center size encoding scale height.
ComparisonOperation m_Operation
Specifies the comparison operation to execute.
A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
NormalizationAlgorithmChannel m_NormChannelType
Normalization channel algorithm to use (Across, Within).
float m_CellClip
Clipping threshold value for the cell state.
float m_A
Alpha upper bound value used by the activation functions. (BoundedReLu, Linear, TanH, Elu).
uint32_t m_DilationX
Dilation along x axis.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
bool m_CifgEnabled
Enable/disable cifg (coupled input & forget gate).
armnn::ArgMinMaxFunction ToArgMinMaxFunction(armnnSerializer::ArgMinMaxFunction function)
uint32_t m_PadLeft
Padding left value in the width dimension.
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
bool m_AlignCorners
Aligned corners.
const armnnSerializer::SerializedGraph * GraphPtr
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
int32_t m_Axis
The axis in params to gather indices from.
A ElementwiseUnaryDescriptor for the ElementwiseUnaryLayer.
PoolingAlgorithm m_PoolType
The pooling algorithm to use (Max. Average, L2).
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
static IDeserializer * CreateRaw()
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
The padding fields count, but are ignored.
std::vector< std::pair< unsigned int, unsigned int > > m_Crops
The values to crop from the input dimension.
Base class for all ArmNN exceptions so that users can filter to just those.
const armnnSerializer::ConstTensor * ConstTensorRawPtr
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
bool m_ProjectionEnabled
Enable/disable the projection layer.
Jarret 2009: Local Contrast Normalization.
const armnnSerializer::LstmDescriptor * LstmDescriptorPtr
OutputShapeRounding m_OutputShapeRounding
The rounding method for the output shape. (Floor, Ceiling).
uint32_t m_NumInputs
Number of input tensors.
static armnn::Pooling2dDescriptor GetPoolingDescriptor(PoolingDescriptor pooling2dDescriptor, unsigned int layerIndex)
const armnnSerializer::QLstmDescriptor * QLstmDescriptorPtr
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
A MeanDescriptor for the MeanLayer.
static TensorRawPtrVector GetInputs(const GraphPtr &graph, unsigned int layerIndex)
bool m_LayerNormEnabled
Enable/disable layer normalization.
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
uint32_t m_PadRight
Padding right value in the width dimension.
A TransposeDescriptor for the TransposeLayer.
A StridedSliceDescriptor for the StridedSliceLayer.
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
int m_Axis
Axis to reduce across the input tensor.
float m_ScaleY
Center size encoding scale y.
float m_NmsScoreThreshold
NMS score threshold.
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
virtual int Connect(IInputSlot &destination)=0
Krichevsky 2012: Local Brightness Normalization.
A Pooling2dDescriptor for the Pooling2dLayer.
const armnnSerializer::LayerBase * LayerBaseRawPtr
A NormalizationDescriptor for the NormalizationLayer.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
An InstanceNormalizationDescriptor for InstanceNormalizationLayer.
float m_CellIntermediateScale
Cell intermediate quantization scale.
static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo &inputTensorInfo, const std::vector< uint32_t > &targetDimsIn)
float m_B
Beta lower bound value used by the activation functions. (BoundedReLu, Linear, TanH).
A SoftmaxDescriptor for the SoftmaxLayer.
float m_Beta
Beta value for the normalization equation.
const armnnSerializer::OriginsDescriptor * GetOriginsDescriptor(const armnnSerializer::SerializedGraph *graph, unsigned int layerIndex)
bool m_CifgEnabled
Enable/disable CIFG (coupled input & forget gate).
uint32_t m_NormSize
Depth radius value.
Status SetViewOriginCoord(uint32_t view, uint32_t coord, uint32_t value)
Set the view origin coordinates.
ActivationFunction m_Function
The activation function to use (Sigmoid, TanH, Linear, ReLu, BoundedReLu, SoftReLu, LeakyReLu, Abs, Sqrt, Square, Elu).
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
A FillDescriptor for the FillLayer.
A BatchNormalizationDescriptor for the BatchNormalizationLayer.
uint32_t m_PadLeft
Padding left value in the width dimension.
unsigned int GetNumElements() const
A PermuteDescriptor for the PermuteLayer.
uint32_t m_PadRight
Padding right value in the width dimension.
int32_t m_HiddenStateZeroPoint
Hidden State zero point.
std::vector< float > anchors({ 0.5f, 0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 1.0f, 1.0f, 0.5f, 10.5f, 1.0f, 1.0f, 0.5f, 10.5f, 1.0f, 1.0f, 0.5f, 100.5f, 1.0f, 1.0f })