22 #include <boost/filesystem.hpp> 23 #include <boost/format.hpp> 24 #include <boost/numeric/conversion/cast.hpp> 32 using namespace armnn;
41 const uint32_t VIRTUAL_LAYER_ID = std::numeric_limits<uint32_t>::max();
43 void CheckGraph(
const Deserializer::GraphPtr& graph,
44 unsigned int layersIndex,
47 if (graph->layers() ==
nullptr)
51 boost::format(
"%1% was called with invalid (null) graph. " 52 "Possible reason is that the graph is not yet loaded and Unpack(ed). " 53 "layers:%2% at %3%") %
58 else if (layersIndex >= graph->layers()->size())
62 boost::format(
"%1% was called with an invalid layers index. " 63 "layers:%2% at %3%") %
70 void CheckLayers(
const Deserializer::GraphPtr& graph,
71 unsigned int layersIndex,
72 unsigned int layerIndex,
75 if (graph->layers() ==
nullptr)
79 boost::format(
"%1% was called with invalid (null) graph. " 80 "Possible reason is that the graph is not yet loaded and Unpack(ed). " 81 "layers:%2% at %3%") %
86 else if (layersIndex >= graph->layers()->size())
90 boost::format(
"%1% was called with an invalid layers index. " 91 "layers:%2% at %3%") %
96 else if (layerIndex >= graph->layers()[layersIndex].size()
97 && layerIndex != VIRTUAL_LAYER_ID)
101 boost::format(
"%1% was called with an invalid layer index. " 102 "layers:%2% layer:%3% at %4%") %
113 if (rawPtr ==
nullptr)
117 boost::format(
"%1% was called with a null tensor pointer. " 125 void CheckConstTensorPtr(Deserializer::ConstTensorRawPtr rawPtr,
128 if (rawPtr ==
nullptr)
130 throw ParseException(boost::str(boost::format(
"%1% was called with a null const tensor pointer. at %2%") %
136 void CheckConstTensorSize(
const unsigned int constTensorSize,
137 const unsigned int tensorSize,
140 if (constTensorSize != tensorSize)
142 throw ParseException(boost::str(boost::format(
"%1% wrong number of components supplied to tensor. at:%2%") %
148 #define CHECK_TENSOR_PTR(TENSOR_PTR) \ 149 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION()) 151 #define CHECK_CONST_TENSOR_SIZE(CONST_TENSOR_SIZE, TENSOR_SIZE) \ 152 CheckConstTensorSize(CONST_TENSOR_SIZE, TENSOR_SIZE, CHECK_LOCATION()) 154 #define CHECK_CONST_TENSOR_PTR(TENSOR_PTR) \ 155 CheckConstTensorPtr(TENSOR_PTR, CHECK_LOCATION()) 157 #define CHECK_LAYERS(GRAPH, LAYERS_INDEX, LAYER_INDEX) \ 158 CheckLayers(GRAPH, LAYERS_INDEX, LAYER_INDEX, CHECK_LOCATION()) 160 #define CHECK_GRAPH(GRAPH, LAYERS_INDEX) \ 161 CheckGraph(GRAPH, LAYERS_INDEX, CHECK_LOCATION()) 167 if (actualSize != expected.size())
172 for (
unsigned int i = 0u; i < actualSize; i++)
174 if (actual[i] != static_cast<unsigned int>(expected[i]))
183 Deserializer::Deserializer()
184 : m_Network(nullptr, nullptr),
186 m_ParserFunctions(Layer_MAX+1, &
Deserializer::ParseUnsupportedLayer)
189 m_ParserFunctions[Layer_AbsLayer] = &Deserializer::ParseAbs;
190 m_ParserFunctions[Layer_ActivationLayer] = &Deserializer::ParseActivation;
191 m_ParserFunctions[Layer_AdditionLayer] = &Deserializer::ParseAdd;
192 m_ParserFunctions[Layer_ArgMinMaxLayer] = &Deserializer::ParseArgMinMax;
193 m_ParserFunctions[Layer_BatchToSpaceNdLayer] = &Deserializer::ParseBatchToSpaceNd;
194 m_ParserFunctions[Layer_BatchNormalizationLayer] = &Deserializer::ParseBatchNormalization;
195 m_ParserFunctions[Layer_ComparisonLayer] = &Deserializer::ParseComparison;
196 m_ParserFunctions[Layer_ConcatLayer] = &Deserializer::ParseConcat;
197 m_ParserFunctions[Layer_ConstantLayer] = &Deserializer::ParseConstant;
198 m_ParserFunctions[Layer_Convolution2dLayer] = &Deserializer::ParseConvolution2d;
199 m_ParserFunctions[Layer_DepthToSpaceLayer] = &Deserializer::ParseDepthToSpace;
200 m_ParserFunctions[Layer_DepthwiseConvolution2dLayer] = &Deserializer::ParseDepthwiseConvolution2d;
201 m_ParserFunctions[Layer_DequantizeLayer] = &Deserializer::ParseDequantize;
202 m_ParserFunctions[Layer_DetectionPostProcessLayer] = &Deserializer::ParseDetectionPostProcess;
203 m_ParserFunctions[Layer_DivisionLayer] = &Deserializer::ParseDivision;
204 m_ParserFunctions[Layer_ElementwiseUnaryLayer] = &Deserializer::ParseElementwiseUnary;
205 m_ParserFunctions[Layer_EqualLayer] = &Deserializer::ParseEqual;
206 m_ParserFunctions[Layer_FullyConnectedLayer] = &Deserializer::ParseFullyConnected;
207 m_ParserFunctions[Layer_FloorLayer] = &Deserializer::ParseFloor;
208 m_ParserFunctions[Layer_GatherLayer] = &Deserializer::ParseGather;
209 m_ParserFunctions[Layer_GreaterLayer] = &Deserializer::ParseGreater;
210 m_ParserFunctions[Layer_InstanceNormalizationLayer] = &Deserializer::ParseInstanceNormalization;
211 m_ParserFunctions[Layer_L2NormalizationLayer] = &Deserializer::ParseL2Normalization;
212 m_ParserFunctions[Layer_LogSoftmaxLayer] = &Deserializer::ParseLogSoftmax;
213 m_ParserFunctions[Layer_LstmLayer] = &Deserializer::ParseLstm;
214 m_ParserFunctions[Layer_MaximumLayer] = &Deserializer::ParseMaximum;
215 m_ParserFunctions[Layer_MeanLayer] = &Deserializer::ParseMean;
216 m_ParserFunctions[Layer_MinimumLayer] = &Deserializer::ParseMinimum;
217 m_ParserFunctions[Layer_MergeLayer] = &Deserializer::ParseMerge;
218 m_ParserFunctions[Layer_MergerLayer] = &Deserializer::ParseConcat;
219 m_ParserFunctions[Layer_MultiplicationLayer] = &Deserializer::ParseMultiplication;
220 m_ParserFunctions[Layer_NormalizationLayer] = &Deserializer::ParseNormalization;
221 m_ParserFunctions[Layer_PadLayer] = &Deserializer::ParsePad;
222 m_ParserFunctions[Layer_PermuteLayer] = &Deserializer::ParsePermute;
223 m_ParserFunctions[Layer_Pooling2dLayer] = &Deserializer::ParsePooling2d;
224 m_ParserFunctions[Layer_PreluLayer] = &Deserializer::ParsePrelu;
225 m_ParserFunctions[Layer_QLstmLayer] = &Deserializer::ParseQLstm;
226 m_ParserFunctions[Layer_QuantizeLayer] = &Deserializer::ParseQuantize;
227 m_ParserFunctions[Layer_QuantizedLstmLayer] = &Deserializer::ParseQuantizedLstm;
228 m_ParserFunctions[Layer_ReshapeLayer] = &Deserializer::ParseReshape;
229 m_ParserFunctions[Layer_ResizeBilinearLayer] = &Deserializer::ParseResizeBilinear;
230 m_ParserFunctions[Layer_ResizeLayer] = &Deserializer::ParseResize;
231 m_ParserFunctions[Layer_RsqrtLayer] = &Deserializer::ParseRsqrt;
232 m_ParserFunctions[Layer_SliceLayer] = &Deserializer::ParseSlice;
233 m_ParserFunctions[Layer_SoftmaxLayer] = &Deserializer::ParseSoftmax;
234 m_ParserFunctions[Layer_SpaceToBatchNdLayer] = &Deserializer::ParseSpaceToBatchNd;
235 m_ParserFunctions[Layer_SpaceToDepthLayer] = &Deserializer::ParseSpaceToDepth;
236 m_ParserFunctions[Layer_SplitterLayer] = &Deserializer::ParseSplitter;
237 m_ParserFunctions[Layer_StackLayer] = &Deserializer::ParseStack;
238 m_ParserFunctions[Layer_StandInLayer] = &Deserializer::ParseStandIn;
239 m_ParserFunctions[Layer_StridedSliceLayer] = &Deserializer::ParseStridedSlice;
240 m_ParserFunctions[Layer_SubtractionLayer] = &Deserializer::ParseSubtraction;
241 m_ParserFunctions[Layer_SwitchLayer] = &Deserializer::ParseSwitch;
242 m_ParserFunctions[Layer_TransposeConvolution2dLayer] = &Deserializer::ParseTransposeConvolution2d;
243 m_ParserFunctions[Layer_TransposeLayer] = &Deserializer::ParseTranspose;
248 auto layerType = graphPtr->layers()->Get(layerIndex)->layer_type();
252 case Layer::Layer_AbsLayer:
253 return graphPtr->layers()->Get(layerIndex)->layer_as_AbsLayer()->base();
254 case Layer::Layer_ActivationLayer:
255 return graphPtr->layers()->Get(layerIndex)->layer_as_ActivationLayer()->base();
256 case Layer::Layer_AdditionLayer:
257 return graphPtr->layers()->Get(layerIndex)->layer_as_AdditionLayer()->base();
258 case Layer::Layer_ArgMinMaxLayer:
259 return graphPtr->layers()->Get(layerIndex)->layer_as_ArgMinMaxLayer()->base();
260 case Layer::Layer_BatchToSpaceNdLayer:
261 return graphPtr->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->base();
262 case Layer::Layer_BatchNormalizationLayer:
263 return graphPtr->layers()->Get(layerIndex)->layer_as_BatchNormalizationLayer()->base();
264 case Layer::Layer_ComparisonLayer:
265 return graphPtr->layers()->Get(layerIndex)->layer_as_ComparisonLayer()->base();
266 case Layer::Layer_ConcatLayer:
267 return graphPtr->layers()->Get(layerIndex)->layer_as_ConcatLayer()->base();
268 case Layer::Layer_ConstantLayer:
269 return graphPtr->layers()->Get(layerIndex)->layer_as_ConstantLayer()->base();
270 case Layer::Layer_Convolution2dLayer:
271 return graphPtr->layers()->Get(layerIndex)->layer_as_Convolution2dLayer()->base();
272 case Layer::Layer_DepthToSpaceLayer:
273 return graphPtr->layers()->Get(layerIndex)->layer_as_DepthToSpaceLayer()->base();
274 case Layer::Layer_DepthwiseConvolution2dLayer:
275 return graphPtr->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer()->base();
276 case Layer::Layer_DequantizeLayer:
277 return graphPtr->layers()->Get(layerIndex)->layer_as_DequantizeLayer()->base();
278 case Layer::Layer_DetectionPostProcessLayer:
279 return graphPtr->layers()->Get(layerIndex)->layer_as_DetectionPostProcessLayer()->base();
280 case Layer::Layer_DivisionLayer:
281 return graphPtr->layers()->Get(layerIndex)->layer_as_DivisionLayer()->base();
282 case Layer::Layer_EqualLayer:
283 return graphPtr->layers()->Get(layerIndex)->layer_as_EqualLayer()->base();
284 case Layer::Layer_FullyConnectedLayer:
285 return graphPtr->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer()->base();
286 case Layer::Layer_FloorLayer:
287 return graphPtr->layers()->Get(layerIndex)->layer_as_FloorLayer()->base();
288 case Layer::Layer_GatherLayer:
289 return graphPtr->layers()->Get(layerIndex)->layer_as_GatherLayer()->base();
290 case Layer::Layer_GreaterLayer:
291 return graphPtr->layers()->Get(layerIndex)->layer_as_GreaterLayer()->base();
292 case Layer::Layer_InputLayer:
293 return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->base();
294 case Layer::Layer_InstanceNormalizationLayer:
295 return graphPtr->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer()->base();
296 case Layer::Layer_L2NormalizationLayer:
297 return graphPtr->layers()->Get(layerIndex)->layer_as_L2NormalizationLayer()->base();
298 case Layer::Layer_LogSoftmaxLayer:
299 return graphPtr->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->base();
300 case Layer::Layer_LstmLayer:
301 return graphPtr->layers()->Get(layerIndex)->layer_as_LstmLayer()->base();
302 case Layer::Layer_MeanLayer:
303 return graphPtr->layers()->Get(layerIndex)->layer_as_MeanLayer()->base();
304 case Layer::Layer_MinimumLayer:
305 return graphPtr->layers()->Get(layerIndex)->layer_as_MinimumLayer()->base();
306 case Layer::Layer_MaximumLayer:
307 return graphPtr->layers()->Get(layerIndex)->layer_as_MaximumLayer()->base();
308 case Layer::Layer_MergeLayer:
309 return graphPtr->layers()->Get(layerIndex)->layer_as_MergeLayer()->base();
310 case Layer::Layer_MergerLayer:
311 return graphPtr->layers()->Get(layerIndex)->layer_as_MergerLayer()->base();
312 case Layer::Layer_MultiplicationLayer:
313 return graphPtr->layers()->Get(layerIndex)->layer_as_MultiplicationLayer()->base();
314 case Layer::Layer_NormalizationLayer:
315 return graphPtr->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->base();
316 case Layer::Layer_OutputLayer:
317 return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->base();
318 case Layer::Layer_PadLayer:
319 return graphPtr->layers()->Get(layerIndex)->layer_as_PadLayer()->base();
320 case Layer::Layer_PermuteLayer:
321 return graphPtr->layers()->Get(layerIndex)->layer_as_PermuteLayer()->base();
322 case Layer::Layer_Pooling2dLayer:
323 return graphPtr->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->base();
324 case Layer::Layer_PreluLayer:
325 return graphPtr->layers()->Get(layerIndex)->layer_as_PreluLayer()->base();
326 case Layer::Layer_QLstmLayer:
327 return graphPtr->layers()->Get(layerIndex)->layer_as_QLstmLayer()->base();
328 case Layer::Layer_QuantizeLayer:
329 return graphPtr->layers()->Get(layerIndex)->layer_as_QuantizeLayer()->base();
330 case Layer::Layer_QuantizedLstmLayer:
331 return graphPtr->layers()->Get(layerIndex)->layer_as_QuantizedLstmLayer()->base();
332 case Layer::Layer_ReshapeLayer:
333 return graphPtr->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->base();
334 case Layer::Layer_ResizeBilinearLayer:
335 return graphPtr->layers()->Get(layerIndex)->layer_as_ResizeBilinearLayer()->base();
336 case Layer::Layer_ResizeLayer:
337 return graphPtr->layers()->Get(layerIndex)->layer_as_ResizeLayer()->base();
338 case Layer::Layer_RsqrtLayer:
339 return graphPtr->layers()->Get(layerIndex)->layer_as_RsqrtLayer()->base();
340 case Layer::Layer_SliceLayer:
341 return graphPtr->layers()->Get(layerIndex)->layer_as_SliceLayer()->base();
342 case Layer::Layer_SoftmaxLayer:
343 return graphPtr->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->base();
344 case Layer::Layer_SpaceToBatchNdLayer:
345 return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->base();
346 case Layer::Layer_SpaceToDepthLayer:
347 return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->base();
348 case Layer::Layer_SplitterLayer:
349 return graphPtr->layers()->Get(layerIndex)->layer_as_SplitterLayer()->base();
350 case Layer::Layer_StackLayer:
351 return graphPtr->layers()->Get(layerIndex)->layer_as_StackLayer()->base();
352 case Layer::Layer_StandInLayer:
353 return graphPtr->layers()->Get(layerIndex)->layer_as_StandInLayer()->base();
354 case Layer::Layer_StridedSliceLayer:
355 return graphPtr->layers()->Get(layerIndex)->layer_as_StridedSliceLayer()->base();
356 case Layer::Layer_SubtractionLayer:
357 return graphPtr->layers()->Get(layerIndex)->layer_as_SubtractionLayer()->base();
358 case Layer::Layer_SwitchLayer:
359 return graphPtr->layers()->Get(layerIndex)->layer_as_SwitchLayer()->base();
360 case Layer::Layer_TransposeConvolution2dLayer:
361 return graphPtr->layers()->Get(layerIndex)->layer_as_TransposeConvolution2dLayer()->base();
362 case Layer::Layer_TransposeLayer:
363 return graphPtr->layers()->Get(layerIndex)->layer_as_TransposeLayer()->base();
364 case Layer::Layer_NONE:
367 boost::format(
"Layer type %1% not recognized") %
376 return layer->layerName()->str();
381 auto layerType = graphPtr->layers()->Get(layerIndex)->layer_type();
383 if (layerType == Layer::Layer_InputLayer)
385 return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->layerBindingId();
387 else if ( layerType == Layer::Layer_OutputLayer )
389 return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->layerBindingId();
398 case armnnSerializer::DataLayout::DataLayout_NHWC:
400 case armnnSerializer::DataLayout::DataLayout_NCHW:
410 case armnnSerializer::ActivationFunction_Sigmoid:
412 case armnnSerializer::ActivationFunction_TanH:
414 case armnnSerializer::ActivationFunction_Linear:
416 case armnnSerializer::ActivationFunction_ReLu:
418 case armnnSerializer::ActivationFunction_BoundedReLu:
420 case armnnSerializer::ActivationFunction_LeakyReLu:
422 case armnnSerializer::ActivationFunction_Abs:
424 case armnnSerializer::ActivationFunction_Sqrt:
426 case armnnSerializer::ActivationFunction_Square:
428 case armnnSerializer::ActivationFunction_Elu:
430 case armnnSerializer::ActivationFunction_HardSwish:
441 case armnnSerializer::ArgMinMaxFunction::ArgMinMaxFunction_Max:
443 case armnnSerializer::ArgMinMaxFunction::ArgMinMaxFunction_Min:
453 case armnnSerializer::ComparisonOperation::ComparisonOperation_Equal:
455 case armnnSerializer::ComparisonOperation::ComparisonOperation_Greater:
457 case armnnSerializer::ComparisonOperation::ComparisonOperation_GreaterOrEqual:
459 case armnnSerializer::ComparisonOperation::ComparisonOperation_Less:
461 case armnnSerializer::ComparisonOperation::ComparisonOperation_LessOrEqual:
463 case armnnSerializer::ComparisonOperation::ComparisonOperation_NotEqual:
473 case armnnSerializer::UnaryOperation::UnaryOperation_Abs:
475 case armnnSerializer::UnaryOperation::UnaryOperation_Rsqrt:
477 case armnnSerializer::UnaryOperation::UnaryOperation_Sqrt:
479 case armnnSerializer::UnaryOperation::UnaryOperation_Exp:
481 case armnnSerializer::UnaryOperation::UnaryOperation_Neg:
492 case armnnSerializer::ResizeMethod_NearestNeighbor:
494 case armnnSerializer::ResizeMethod_Bilinear:
506 switch (tensorPtr->dataType())
508 case DataType_QAsymmS8:
511 case DataType_QSymmS8:
514 case DataType_QuantisedAsymm8:
515 case DataType_QAsymmU8:
518 case DataType_QSymmS16:
519 case DataType_QuantisedSymm16:
522 case DataType_Signed32:
525 case DataType_Float32:
528 case DataType_Float16:
531 case DataType_Boolean:
539 boost::format(
"Unsupported data type %1% = %2%. %3%") %
540 tensorPtr->dataType() %
541 EnumNameDataType(tensorPtr->dataType()) %
547 auto dimensions = tensorPtr->dimensions();
548 unsigned int size = dimensions->size();
549 std::vector<unsigned int> outputDims(dimensions->begin(), dimensions->begin() + size);
551 auto quantizationScales = tensorPtr->quantizationScales();
553 if (quantizationScales)
555 unsigned int quantizationScalesSize = quantizationScales->size();
556 std::vector<float> scales(quantizationScales->begin(), quantizationScales->begin() + quantizationScalesSize);
557 unsigned int quantizationDim = tensorPtr->quantizationDim();
566 float quantizationScale = tensorPtr->quantizationScale();
567 int32_t quantizationOffset = tensorPtr->quantizationOffset();
583 switch (constTensorPtr->data_type())
585 case ConstTensorData_ByteData:
587 auto byteData = constTensorPtr->data_as_ByteData()->data();
591 case ConstTensorData_ShortData:
593 auto shortData = constTensorPtr->data_as_ShortData()->data();
597 case ConstTensorData_IntData:
599 auto intData = constTensorPtr->data_as_IntData()->data();
603 case ConstTensorData_LongData:
605 auto longData = constTensorPtr->data_as_LongData()->data();
613 boost::str(boost::format(
"Unsupported data type %1% = %2%. %3%") %
614 constTensorPtr->data_type() %
615 EnumNameConstTensorData(constTensorPtr->data_type()) %
622 unsigned int layerIndex)
626 const auto& numInputs = layer->inputSlots()->size();
630 for (
unsigned int i=0; i<numInputs; ++i)
633 (layer->inputSlots()->Get(i)->connection()->sourceLayerIndex()));
634 result[i] =
GetBaseLayer(graphPtr, inputId)->outputSlots()->Get(0)->tensorInfo();
640 unsigned int layerIndex)
644 const auto& numOutputs = layer->outputSlots()->size();
648 for (
unsigned int i=0; i<numOutputs; ++i)
650 result[i] = layer->outputSlots()->Get(i)->tensorInfo();
655 void Deserializer::ParseUnsupportedLayer(
GraphPtr graph,
unsigned int layerIndex)
658 const auto layerName =
GetBaseLayer(graph, layerIndex)->layerName()->c_str();
661 boost::format(
"Layer not supported. " 663 "layerName: %2% / %3%") %
669 void Deserializer::ResetParser()
672 m_InputBindings.clear();
673 m_OutputBindings.clear();
695 return CreateNetworkFromGraph(graph);
701 std::vector<uint8_t> content((std::istreambuf_iterator<char>(binaryContent)), std::istreambuf_iterator<char>());
703 return CreateNetworkFromGraph(graph);
708 if (binaryContent ==
nullptr)
713 flatbuffers::Verifier verifier(binaryContent, len);
714 if (verifier.VerifyBuffer<SerializedGraph>() ==
false)
717 boost::str(boost::format(
"Buffer doesn't conform to the expected Armnn " 718 "flatbuffers format. size:%1% %2%") %
722 return GetSerializedGraph(binaryContent);
727 m_Network = INetwork::Create();
729 unsigned int layerIndex = 0;
730 for (AnyLayer
const* layer : *graph->layers())
732 if (layer->layer_type() != Layer_InputLayer &&
733 layer->layer_type() != Layer_OutputLayer)
736 auto& parserFunction = m_ParserFunctions[layer->layer_type()];
737 (this->*parserFunction)(graph, layerIndex);
742 SetupInputLayers(graph);
743 SetupOutputLayers(graph);
746 for (
auto&& graphIt : m_GraphConnections)
748 Connections& connections = graphIt.second;
749 for (
auto&& outputIt : connections.outputSlots)
751 const unsigned int outputSlotIndex = outputIt.first;
753 if (connections.inputSlots.find(outputSlotIndex) != connections.inputSlots.end())
755 for (
IInputSlot* inputSlot : connections.inputSlots[outputSlotIndex])
757 outputSlot->
Connect(*inputSlot);
763 return std::move(m_Network);
767 const std::string& name)
const 770 for (
auto inputBinding : m_InputBindings)
772 if (inputBinding.first == name)
774 return inputBinding.second;
779 boost::format(
"No input binding found for layer:%1% / %2%") %
785 const std::string& name)
const 788 for (
auto outputBinding : m_OutputBindings)
790 if (outputBinding.first == name)
792 return outputBinding.second;
797 boost::format(
"No output binding found for layer:%1% / %2%") %
802 unsigned int Deserializer::GetInputLayerInVector(
GraphPtr graph,
int targetId)
804 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
806 auto layer = graph->layers()->Get(i);
807 if (layer->layer_type() == Layer::Layer_InputLayer)
809 auto layerBindingId = layer->layer_as_InputLayer()->base()->layerBindingId();
810 if (layerBindingId == targetId)
816 throw ParseException(
"Input layer with given layerBindingId not found");
819 unsigned int Deserializer::GetOutputLayerInVector(
GraphPtr graph,
int targetId)
821 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
823 auto layer = graph->layers()->Get(i);
824 if (layer->layer_type() == Layer::Layer_OutputLayer)
826 auto layerBindingId = layer->layer_as_OutputLayer()->base()->layerBindingId();
827 if (layerBindingId == targetId)
833 throw ParseException(
"Output layer with given layerBindingId not found");
836 unsigned int Deserializer::GetLayerIndexInVector(
GraphPtr graph,
unsigned int targetIndex)
838 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
841 if (layer->index() == targetIndex)
849 Deserializer::FeatureVersions Deserializer::GetFeatureVersions(
GraphPtr graph)
851 Deserializer::FeatureVersions versions;
853 if (graph->featureVersions())
855 versions.m_BindingIdScheme = graph->featureVersions()->bindingIdsScheme();
861 void Deserializer::SetupInputLayers(
GraphPtr graph)
864 const unsigned int numInputs = graph->inputIds()->size();
865 m_InputBindings.clear();
866 m_InputBindings.reserve(numInputs);
868 for (
unsigned int i = 0; i < numInputs; i++)
870 unsigned int inputLayerIndex = 0xFFFFFFFF;
871 if (GetFeatureVersions(graph).m_BindingIdScheme == 0)
874 inputLayerIndex = GetLayerIndexInVector(graph, inputId);
878 const int inputId = graph->inputIds()->Get(i);
879 inputLayerIndex = GetInputLayerInVector(graph, inputId);
889 m_Network->AddInputLayer(bindingId, baseLayer->layerName()->c_str());
892 inputLayer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
893 RegisterOutputSlots(graph, inputLayerIndex, inputLayer);
896 m_InputBindings.push_back(std::make_pair(baseLayer->layerName()->c_str(), bindingInfo));
900 void Deserializer::SetupOutputLayers(
GraphPtr graph)
903 const unsigned int numOutputs = graph->outputIds()->size();
904 m_OutputBindings.clear();
905 m_OutputBindings.reserve(numOutputs);
907 for (
unsigned int i = 0; i < numOutputs; i++)
909 unsigned int outputLayerIndex = 0xFFFFFFFF;
910 if (GetFeatureVersions(graph).m_BindingIdScheme == 0)
912 const unsigned int outputId =
boost::numeric_cast<
unsigned int>(graph->outputIds()->Get(i));
913 outputLayerIndex = GetLayerIndexInVector(graph, outputId);
917 const int outputId = graph->outputIds()->Get(i);
918 outputLayerIndex = GetOutputLayerInVector(graph, outputId);
928 m_Network->AddOutputLayer(bindingId, baseLayer->layerName()->c_str());
930 RegisterInputSlots(graph, outputLayerIndex, outputLayer);
932 unsigned int sourceLayerIndex =
933 GetLayerIndexInVector(graph, baseLayer->inputSlots()->Get(0)->connection()->sourceLayerIndex());
938 m_OutputBindings.push_back(std::make_pair(baseLayer->layerName()->c_str(), bindingInfo));
942 void Deserializer::RegisterOutputSlots(
GraphPtr graph,
952 boost::str(boost::format(
"The number of outputslots (%1%) does not match the number expected (%2%)" 953 " for layer index: %3% %4%") %
954 baseLayer->outputSlots()->size() %
962 const unsigned int slotIndex = baseLayer->outputSlots()->Get(i)->index();
965 RegisterOutputSlotOfConnection(baseLayer->index(), slotIndex, outputSlot);
969 void Deserializer::RegisterInputSlots(
GraphPtr graph,
979 boost::str(boost::format(
"The number of inputslots (%1%) does not match the number expected (%2%)" 980 " for layer index:%3% %4%") %
981 baseLayer->inputSlots()->size() %
989 auto fbInputSlot = baseLayer->inputSlots()->Get(i);
990 auto fbConnection = fbInputSlot->connection();
992 RegisterInputSlotOfConnection(fbConnection->sourceLayerIndex(), fbConnection->outputSlotIndex(), inputSlot);
996 void Deserializer::RegisterInputSlotOfConnection(uint32_t sourceLayerIndex,
997 uint32_t outputSlotIndex,
1000 if (m_GraphConnections.find(sourceLayerIndex) == m_GraphConnections.end())
1002 m_GraphConnections[sourceLayerIndex] = Connections();
1005 Connections& connections = m_GraphConnections[sourceLayerIndex];
1006 if (connections.inputSlots.find(outputSlotIndex) == connections.inputSlots.end())
1008 connections.inputSlots[outputSlotIndex] = {inputSlot};
1012 connections.inputSlots[outputSlotIndex].push_back(inputSlot);
1016 void Deserializer::RegisterOutputSlotOfConnection(uint32_t sourceLayerIndex,
1017 uint32_t outputSlotIndex,
1020 if (m_GraphConnections.find(sourceLayerIndex) == m_GraphConnections.end())
1022 m_GraphConnections[sourceLayerIndex] = Connections();
1025 Connections& connections = m_GraphConnections[sourceLayerIndex];
1026 if (connections.outputSlots.find(outputSlotIndex) != connections.outputSlots.end())
1031 connections.outputSlots[outputSlotIndex] = outputSlot;
1037 auto inputs =
GetInputs(graph, layerIndex);
1041 auto outputs =
GetOutputs(graph, layerIndex);
1047 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
1051 RegisterInputSlots(graph, layerIndex, layer);
1052 RegisterOutputSlots(graph, layerIndex, layer);
1055 void Deserializer::ParseActivation(
GraphPtr graph,
unsigned int layerIndex)
1058 auto inputs =
GetInputs(graph, layerIndex);
1062 auto outputs =
GetOutputs(graph, layerIndex);
1065 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ActivationLayer();
1067 auto serializerDescriptor = serializerLayer->descriptor();
1071 descriptor.
m_A = serializerDescriptor->a();
1072 descriptor.
m_B = serializerDescriptor->b();
1079 RegisterInputSlots(graph, layerIndex, layer);
1080 RegisterOutputSlots(graph, layerIndex, layer);
1083 void Deserializer::ParseAdd(
GraphPtr graph,
unsigned int layerIndex)
1086 auto inputs =
GetInputs(graph, layerIndex);
1090 auto outputs =
GetOutputs(graph, layerIndex);
1099 RegisterInputSlots(graph, layerIndex, layer);
1100 RegisterOutputSlots(graph, layerIndex, layer);
1103 void Deserializer::ParseArgMinMax(
GraphPtr graph,
unsigned int layerIndex)
1106 auto inputs =
GetInputs(graph, layerIndex);
1110 auto outputs =
GetOutputs(graph, layerIndex);
1113 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ArgMinMaxLayer();
1114 auto serializerDescriptor = serializerLayer->descriptor();
1118 descriptor.
m_Axis = serializerDescriptor->axis();
1120 IConnectableLayer* layer = m_Network->AddArgMinMaxLayer(descriptor, layerName.c_str());
1125 RegisterInputSlots(graph, layerIndex, layer);
1126 RegisterOutputSlots(graph, layerIndex, layer);
1129 void Deserializer::ParseBatchToSpaceNd(
GraphPtr graph,
unsigned int layerIndex)
1139 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->descriptor();
1140 auto flatBufferCrops = flatBufferDescriptor->crops();
1141 auto flatBufferBlockShape = flatBufferDescriptor->blockShape();
1143 if (flatBufferCrops->Length() % 2 != 0)
1146 boost::format(
"The size of crops must be divisible by 2 %1%") %
CHECK_LOCATION().AsString()));
1149 std::vector<std::pair<unsigned int, unsigned int>> crops;
1150 crops.reserve(flatBufferCrops->Length() / 2);
1151 for (
unsigned int i = 0; i < flatBufferCrops->Length() - 1; i += 2)
1153 crops.emplace_back(flatBufferCrops->Get(i), flatBufferCrops->Get(i+1));
1159 std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end());
1163 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(descriptor, layerName.c_str());
1168 RegisterInputSlots(graph, layerIndex, layer);
1169 RegisterOutputSlots(graph, layerIndex, layer);
1172 void Deserializer::ParseBatchNormalization(
GraphPtr graph,
unsigned int layerIndex)
1176 auto inputs =
GetInputs(graph, layerIndex);
1179 auto outputs =
GetOutputs(graph, layerIndex);
1185 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_BatchNormalizationLayer();
1186 auto serializerDescriptor = serializerLayer->descriptor();
1189 descriptor.
m_Eps = serializerDescriptor->eps();
1203 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
1205 RegisterInputSlots(graph, layerIndex, layer);
1206 RegisterOutputSlots(graph, layerIndex, layer);
1209 void Deserializer::ParseConstant(
GraphPtr graph,
unsigned int layerIndex)
1214 auto outputs =
GetOutputs(graph, layerIndex);
1219 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ConstantLayer();
1220 auto serializerInput = serializerLayer->input();
1224 IConnectableLayer* layer = m_Network->AddConstantLayer(input, layerName.c_str());
1229 RegisterOutputSlots(graph, layerIndex, layer);
1232 void Deserializer::ParseConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
1235 auto inputs =
GetInputs(graph, layerIndex);
1239 auto outputs =
GetOutputs(graph, layerIndex);
1242 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_Convolution2dLayer();
1244 auto serializerDescriptor = serializerLayer->descriptor();
1247 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
1248 descriptor.
m_PadRight = serializerDescriptor->padRight();
1249 descriptor.
m_PadTop = serializerDescriptor->padTop();
1250 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
1251 descriptor.
m_StrideX = serializerDescriptor->strideX();
1252 descriptor.
m_StrideY = serializerDescriptor->strideY();;
1253 descriptor.
m_DilationX = serializerDescriptor->dilationX();
1254 descriptor.
m_DilationY = serializerDescriptor->dilationY();;
1255 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();;
1272 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1274 RegisterInputSlots(graph, layerIndex, layer);
1275 RegisterOutputSlots(graph, layerIndex, layer);
1278 void Deserializer::ParseDepthToSpace(
GraphPtr graph,
unsigned int layerIndex)
1282 auto inputs =
GetInputs(graph, layerIndex);
1285 auto outputs =
GetOutputs(graph, layerIndex);
1288 auto fbDescriptor = graph->layers()->Get(layerIndex)->layer_as_DepthToSpaceLayer()->descriptor();
1291 descriptor.
m_BlockSize = fbDescriptor->blockSize();
1295 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
1300 RegisterInputSlots(graph, layerIndex, layer);
1301 RegisterOutputSlots(graph, layerIndex, layer);
1304 void Deserializer::ParseDepthwiseConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
1307 auto inputs =
GetInputs(graph, layerIndex);
1311 auto outputs =
GetOutputs(graph, layerIndex);
1314 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer();
1316 auto serializerDescriptor = serializerLayer->descriptor();
1319 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
1320 descriptor.
m_PadRight = serializerDescriptor->padRight();
1321 descriptor.
m_PadTop = serializerDescriptor->padTop();
1322 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
1323 descriptor.
m_StrideX = serializerDescriptor->strideX();
1324 descriptor.
m_StrideY = serializerDescriptor->strideY();
1325 descriptor.
m_DilationX = serializerDescriptor->dilationX();
1326 descriptor.
m_DilationY = serializerDescriptor->dilationY();
1327 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();;
1339 IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(descriptor,
1345 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1347 RegisterInputSlots(graph, layerIndex, layer);
1348 RegisterOutputSlots(graph, layerIndex, layer);
1351 void Deserializer::ParseDetectionPostProcess(
GraphPtr graph,
unsigned int layerIndex)
1354 auto inputs =
GetInputs(graph, layerIndex);
1358 auto outputs =
GetOutputs(graph, layerIndex);
1361 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_DetectionPostProcessLayer();
1363 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1371 descriptor.
m_NumClasses = flatBufferDescriptor->numClasses();
1373 descriptor.
m_ScaleX = flatBufferDescriptor->scaleX();
1374 descriptor.
m_ScaleY = flatBufferDescriptor->scaleY();
1375 descriptor.
m_ScaleW = flatBufferDescriptor->scaleW();
1376 descriptor.
m_ScaleH = flatBufferDescriptor->scaleH();
1384 for (
unsigned int i = 0; i < 4; i++)
1386 layer->GetOutputSlot(i).SetTensorInfo(
ToTensorInfo(outputs[i]));
1389 RegisterInputSlots(graph, layerIndex, layer);
1390 RegisterOutputSlots(graph, layerIndex, layer);
1393 void Deserializer::ParseDivision(
GraphPtr graph,
unsigned int layerIndex)
1396 auto inputs =
GetInputs(graph, layerIndex);
1400 auto outputs =
GetOutputs(graph, layerIndex);
1409 RegisterInputSlots(graph, layerIndex, layer);
1410 RegisterOutputSlots(graph, layerIndex, layer);
1413 void Deserializer::ParseEqual(
GraphPtr graph,
unsigned int layerIndex)
1416 auto inputs =
GetInputs(graph, layerIndex);
1420 auto outputs =
GetOutputs(graph, layerIndex);
1425 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
1430 RegisterInputSlots(graph, layerIndex, layer);
1431 RegisterOutputSlots(graph, layerIndex, layer);
1434 void Deserializer::ParseGreater(
GraphPtr graph,
unsigned int layerIndex)
1437 auto inputs =
GetInputs(graph, layerIndex);
1441 auto outputs =
GetOutputs(graph, layerIndex);
1446 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
1451 RegisterInputSlots(graph, layerIndex, layer);
1452 RegisterOutputSlots(graph, layerIndex, layer);
1455 void Deserializer::ParseInstanceNormalization(
GraphPtr graph,
unsigned int layerIndex)
1459 auto inputs =
GetInputs(graph, layerIndex);
1462 auto outputs =
GetOutputs(graph, layerIndex);
1465 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer();
1466 auto fbDescriptor = fbLayer->descriptor();
1469 descriptor.
m_Gamma = fbDescriptor->gamma();
1470 descriptor.
m_Beta = fbDescriptor->beta();
1471 descriptor.
m_Eps = fbDescriptor->eps();
1474 const std::string layerName =
GetLayerName(graph, layerIndex);
1477 IConnectableLayer* layer = m_Network->AddInstanceNormalizationLayer(descriptor, layerName.c_str());
1480 RegisterInputSlots(graph, layerIndex, layer);
1481 RegisterOutputSlots(graph, layerIndex, layer);
1484 void Deserializer::ParseL2Normalization(
GraphPtr graph,
unsigned int layerIndex)
1488 auto inputs =
GetInputs(graph, layerIndex);
1491 auto outputs =
GetOutputs(graph, layerIndex);
1495 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_L2NormalizationLayer();
1496 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1501 descriptor.
m_Eps = flatBufferDescriptor->eps();
1503 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(descriptor, layerName.c_str());
1506 RegisterInputSlots(graph, layerIndex, layer);
1507 RegisterOutputSlots(graph, layerIndex, layer);
1510 void Deserializer::ParseLogSoftmax(
GraphPtr graph,
unsigned int layerIndex)
1521 descriptor.
m_Beta = graph->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->descriptor()->beta();
1522 descriptor.m_Axis = graph->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->descriptor()->axis();
1525 IConnectableLayer* layer = m_Network->AddLogSoftmaxLayer(descriptor, layerName.c_str());
1530 RegisterInputSlots(graph, layerIndex, layer);
1531 RegisterOutputSlots(graph, layerIndex, layer);
1534 void Deserializer::ParseMinimum(
GraphPtr graph,
unsigned int layerIndex)
1537 auto inputs =
GetInputs(graph, layerIndex);
1541 auto outputs =
GetOutputs(graph, layerIndex);
1550 RegisterInputSlots(graph, layerIndex, layer);
1551 RegisterOutputSlots(graph, layerIndex, layer);
1554 void Deserializer::ParseMaximum(
GraphPtr graph,
unsigned int layerIndex)
1557 auto inputs =
GetInputs(graph, layerIndex);
1561 auto outputs =
GetOutputs(graph, layerIndex);
1570 RegisterInputSlots(graph, layerIndex, layer);
1571 RegisterOutputSlots(graph, layerIndex, layer);
1575 unsigned int layerIndex)
1577 auto layerType = graph->layers()->Get(layerIndex)->layer_type();
1581 case Layer::Layer_ConcatLayer:
1582 return graph->layers()->Get(layerIndex)->layer_as_ConcatLayer()->descriptor();
1583 case Layer::Layer_MergerLayer:
1584 return graph->layers()->Get(layerIndex)->layer_as_MergerLayer()->descriptor();
1590 void Deserializer::ParseComparison(
GraphPtr graph,
unsigned int layerIndex)
1595 auto inputs =
GetInputs(graph, layerIndex);
1598 auto outputs =
GetOutputs(graph, layerIndex);
1601 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ComparisonLayer();
1602 auto fbDescriptor = fbLayer->descriptor();
1607 const std::string& layerName =
GetLayerName(graph, layerIndex);
1608 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
1613 RegisterInputSlots(graph, layerIndex, layer);
1614 RegisterOutputSlots(graph, layerIndex, layer);
1617 void Deserializer::ParseElementwiseUnary(
GraphPtr graph,
unsigned int layerIndex)
1622 auto inputs =
GetInputs(graph, layerIndex);
1625 auto outputs =
GetOutputs(graph, layerIndex);
1628 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ElementwiseUnaryLayer();
1629 auto fbDescriptor = fbLayer->descriptor();
1634 const std::string& layerName =
GetLayerName(graph, layerIndex);
1635 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
1640 RegisterInputSlots(graph, layerIndex, layer);
1641 RegisterOutputSlots(graph, layerIndex, layer);
1644 void Deserializer::ParseConcat(
GraphPtr graph,
unsigned int layerIndex)
1649 auto outputs =
GetOutputs(graph, layerIndex);
1654 unsigned int numViews = originsDescriptor->numViews();
1655 unsigned int numDimensions = originsDescriptor->numDimensions();
1658 auto inputs =
GetInputs(graph, layerIndex);
1662 auto originsPtr = originsDescriptor->viewOrigins();
1663 for (
unsigned int v = 0; v < numViews; ++v)
1665 auto originPtr = originsPtr->Get(v);
1666 for (
unsigned int d = 0; d < numDimensions; ++d)
1668 uint32_t value = originPtr->data()->Get(d);
1669 descriptor.SetViewOriginCoord(v, d, value);
1672 descriptor.SetConcatAxis(originsDescriptor->concatAxis());
1674 IConnectableLayer* layer = m_Network->AddConcatLayer(descriptor, layerName.c_str());
1678 RegisterInputSlots(graph, layerIndex, layer);
1679 RegisterOutputSlots(graph, layerIndex, layer);
1682 void Deserializer::ParseMultiplication(
GraphPtr graph,
unsigned int layerIndex)
1685 auto inputs =
GetInputs(graph, layerIndex);
1689 auto outputs =
GetOutputs(graph, layerIndex);
1693 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
1698 RegisterInputSlots(graph, layerIndex, layer);
1699 RegisterOutputSlots(graph, layerIndex, layer);
1702 void Deserializer::ParseFloor(
GraphPtr graph,
unsigned int layerIndex)
1707 auto inputs =
GetInputs(graph, layerIndex);
1710 auto outputs =
GetOutputs(graph, layerIndex);
1717 layer = m_Network->AddFloorLayer(layerName.c_str());
1722 RegisterInputSlots(graph, layerIndex, layer);
1723 RegisterOutputSlots(graph, layerIndex, layer);
1726 void Deserializer::ParseFullyConnected(
GraphPtr graph,
unsigned int layerIndex)
1729 auto inputs =
GetInputs(graph, layerIndex);
1733 auto outputs =
GetOutputs(graph, layerIndex);
1736 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer();
1738 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1741 fullyConnectedDescriptor.
m_BiasEnabled = flatBufferDescriptor->biasEnabled();
1748 if (flatBufferDescriptor->biasEnabled())
1753 layer = m_Network->AddFullyConnectedLayer(fullyConnectedDescriptor,
1759 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1761 RegisterInputSlots(graph, layerIndex, layer);
1762 RegisterOutputSlots(graph, layerIndex, layer);
1765 void Deserializer::ParsePad(
GraphPtr graph,
unsigned int layerIndex)
1775 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_PadLayer()->descriptor();
1776 auto flatBufferPadList = flatBufferDescriptor->padList();
1777 float padValue = flatBufferDescriptor->padValue();
1779 if (flatBufferPadList->Length() % 2 != 0)
1782 boost::format(
"The size of the pad list must be divisible by 2 %1%") %
CHECK_LOCATION().AsString()));
1785 std::vector<std::pair<unsigned int, unsigned int>> padList;
1786 padList.reserve(flatBufferPadList->Length() / 2);
1787 for (
unsigned int i = 0; i < flatBufferPadList->Length() - 1; i += 2)
1789 padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1));
1795 IConnectableLayer* layer = m_Network->AddPadLayer(descriptor, layerName.c_str());
1800 RegisterInputSlots(graph, layerIndex, layer);
1801 RegisterOutputSlots(graph, layerIndex, layer);
1804 void Deserializer::ParsePermute(
GraphPtr graph,
unsigned int layerIndex)
1809 graph->layers()->Get(layerIndex)->layer_as_PermuteLayer()->descriptor()->dimMappings();
1811 auto inputs =
GetInputs(graph, layerIndex);
1814 auto outputs =
GetOutputs(graph, layerIndex);
1821 IConnectableLayer* layer = m_Network->AddPermuteLayer(descriptor, layerName.c_str());
1824 RegisterInputSlots(graph, layerIndex, layer);
1825 RegisterOutputSlots(graph, layerIndex, layer);
1829 unsigned int layerIndex)
1834 switch (pooling2dDesc->poolType())
1836 case PoolingAlgorithm_Average:
1841 case PoolingAlgorithm_Max:
1852 switch (pooling2dDesc->outputShapeRounding())
1854 case OutputShapeRounding_Floor:
1859 case OutputShapeRounding_Ceiling:
1870 switch (pooling2dDesc->paddingMethod())
1872 case PaddingMethod_Exclude:
1877 case PaddingMethod_IgnoreValue:
1888 switch (pooling2dDesc->dataLayout())
1890 case DataLayout_NCHW:
1895 case DataLayout_NHWC:
1907 desc.
m_PadLeft = pooling2dDesc->padLeft();
1909 desc.
m_PadTop = pooling2dDesc->padTop();
1910 desc.
m_StrideX = pooling2dDesc->strideX();
1911 desc.
m_StrideY = pooling2dDesc->strideY();
1918 void Deserializer::ParsePooling2d(
GraphPtr graph,
unsigned int layerIndex)
1922 auto pooling2dDes = graph->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->descriptor();
1923 auto inputs =
GetInputs(graph, layerIndex);
1926 auto outputs =
GetOutputs(graph, layerIndex);
1932 IConnectableLayer* layer = m_Network->AddPooling2dLayer(pooling2dDescriptor, layerName.c_str());
1935 RegisterInputSlots(graph, layerIndex, layer);
1936 RegisterOutputSlots(graph, layerIndex, layer);
1939 void Deserializer::ParseQuantize(
GraphPtr graph,
unsigned int layerIndex)
1943 auto inputs =
GetInputs(graph, layerIndex);
1946 auto outputs =
GetOutputs(graph, layerIndex);
1954 RegisterInputSlots(graph, layerIndex, layer);
1955 RegisterOutputSlots(graph, layerIndex, layer);
1959 const std::vector<uint32_t>& targetDimsIn)
1961 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
1962 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
1964 if (stretchDim != targetDimsIn.end())
1966 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
1969 boost::format(
"At most one component of shape can be -1 %1%") %
CHECK_LOCATION().AsString()));
1972 auto targetNumElements =
1974 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
1976 auto stretchIndex =
static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
1977 outputDims[stretchIndex] = inputTensorInfo.
GetNumElements() / targetNumElements;
1988 void Deserializer::ParseReshape(
GraphPtr graph,
unsigned int layerIndex)
1991 auto inputs =
GetInputs(graph, layerIndex);
1993 auto outputs =
GetOutputs(graph, layerIndex);
1999 const auto targetDims = graph->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->descriptor()->targetShape();
2000 std::vector<uint32_t> outputDims(targetDims->begin(), targetDims->begin() + targetDims->size());
2003 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
2005 const std::vector<uint32_t> expectedDims(outputs[0]->dimensions()->begin(),
2006 outputs[0]->dimensions()->begin() + outputs[0]->dimensions()->size());
2008 if (inputs.size() > 1 && !
CheckShape(reshapeOutputTensorShape, expectedDims))
2010 std::stringstream ss;
2011 ss <<
"New shape defined in reshape parameters " 2012 << reshapeOutputTensorShape
2013 <<
" does not equal output shape " 2014 << actualOutputTensorInfo.
GetShape()
2024 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2027 RegisterInputSlots(graph, layerIndex, layer);
2028 RegisterOutputSlots(graph, layerIndex, layer);
2031 void Deserializer::ParseResize(
GraphPtr graph,
unsigned int layerIndex)
2041 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_ResizeLayer()->descriptor();
2044 descriptor.
m_TargetWidth = flatBufferDescriptor->targetWidth();
2045 descriptor.
m_TargetHeight = flatBufferDescriptor->targetHeight();
2050 IConnectableLayer* layer = m_Network->AddResizeLayer(descriptor, layerName.c_str());
2055 RegisterInputSlots(graph, layerIndex, layer);
2056 RegisterOutputSlots(graph, layerIndex, layer);
2059 void Deserializer::ParseResizeBilinear(
GraphPtr graph,
unsigned int layerIndex)
2069 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_ResizeBilinearLayer()->descriptor();
2072 descriptor.
m_TargetWidth = flatBufferDescriptor->targetWidth();
2073 descriptor.
m_TargetHeight = flatBufferDescriptor->targetHeight();
2078 IConnectableLayer* layer = m_Network->AddResizeLayer(descriptor, layerName.c_str());
2083 RegisterInputSlots(graph, layerIndex, layer);
2084 RegisterOutputSlots(graph, layerIndex, layer);
2087 void Deserializer::ParseSoftmax(
GraphPtr graph,
unsigned int layerIndex)
2098 descriptor.
m_Beta = graph->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->descriptor()->beta();
2101 IConnectableLayer* layer = m_Network->AddSoftmaxLayer(descriptor, layerName.c_str());
2106 RegisterInputSlots(graph, layerIndex, layer);
2107 RegisterOutputSlots(graph, layerIndex, layer);
2110 void Deserializer::ParseSpaceToBatchNd(
GraphPtr graph,
unsigned int layerIndex)
2120 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->descriptor();
2121 auto flatBufferPadList = flatBufferDescriptor->padList();
2122 auto flatBufferBlockShape = flatBufferDescriptor->blockShape();
2124 if (flatBufferPadList->Length() % 2 != 0)
2127 boost::format(
"The size of the pad list must be divisible by 2 %1%") %
CHECK_LOCATION().AsString()));
2130 std::vector<std::pair<unsigned int, unsigned int>> padList;
2131 padList.reserve(flatBufferPadList->Length() / 2);
2132 for (
unsigned int i = 0; i < flatBufferPadList->Length() - 1; i += 2)
2134 padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1));
2140 std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end());
2144 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(descriptor, layerName.c_str());
2149 RegisterInputSlots(graph, layerIndex, layer);
2150 RegisterOutputSlots(graph, layerIndex, layer);
2153 void Deserializer::ParseSpaceToDepth(
GraphPtr graph,
unsigned int layerIndex)
2163 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->descriptor();
2166 descriptor.
m_BlockSize = flatBufferDescriptor->blockSize();
2170 IConnectableLayer* layer = m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str());
2175 RegisterInputSlots(graph, layerIndex, layer);
2176 RegisterOutputSlots(graph, layerIndex, layer);
2181 unsigned int layerIndex)
2186 switch (normalizationDescriptor->normChannelType())
2188 case NormalizationAlgorithmChannel_Across:
2193 case NormalizationAlgorithmChannel_Within:
2204 switch (normalizationDescriptor->normMethodType())
2206 case NormalizationAlgorithmMethod_LocalBrightness:
2211 case NormalizationAlgorithmMethod_LocalContrast:
2222 switch (normalizationDescriptor->dataLayout())
2224 case DataLayout_NCHW:
2229 case DataLayout_NHWC:
2240 desc.
m_Alpha = normalizationDescriptor->alpha();
2241 desc.
m_Beta = normalizationDescriptor->beta();
2242 desc.
m_K = normalizationDescriptor->k();
2243 desc.
m_NormSize = normalizationDescriptor->normSize();
2248 void Deserializer::ParseNormalization(
GraphPtr graph,
unsigned int layerIndex)
2252 auto normalizationDes = graph->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->descriptor();
2265 IConnectableLayer* layer = m_Network->AddNormalizationLayer(normalizationDescriptor, layerName.c_str());
2268 RegisterInputSlots(graph, layerIndex, layer);
2269 RegisterOutputSlots(graph, layerIndex, layer);
2272 void Deserializer::ParseRsqrt(
GraphPtr graph,
unsigned int layerIndex)
2275 auto inputs =
GetInputs(graph, layerIndex);
2279 auto outputs =
GetOutputs(graph, layerIndex);
2285 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
2289 RegisterInputSlots(graph, layerIndex, layer);
2290 RegisterOutputSlots(graph, layerIndex, layer);
2293 void Deserializer::ParseSlice(
GraphPtr graph,
unsigned int layerIndex)
2297 auto inputs =
GetInputs(graph, layerIndex);
2300 auto outputs =
GetOutputs(graph, layerIndex);
2303 auto fbDescriptor = graph->layers()->Get(layerIndex)->layer_as_SliceLayer()->descriptor();
2305 auto fbBegin = fbDescriptor->begin();
2306 auto fbSize = fbDescriptor->size();
2308 if (fbBegin->Length() != fbSize->Length())
2311 boost::format(
"Begin and size descriptors must have the same length %1%") %
CHECK_LOCATION().AsString()));
2315 descriptor.
m_Begin.insert(descriptor.
m_Begin.end(), fbBegin->begin(), fbBegin->end());
2316 descriptor.
m_Size.insert(descriptor.
m_Size.end(), fbSize->begin(), fbSize->end());
2319 IConnectableLayer* layer = m_Network->AddSliceLayer(descriptor, layerName.c_str());
2324 RegisterInputSlots(graph, layerIndex, layer);
2325 RegisterOutputSlots(graph, layerIndex, layer);
2328 void Deserializer::ParseStridedSlice(
GraphPtr graph,
unsigned int layerIndex)
2338 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_StridedSliceLayer()->descriptor();
2340 auto flatBufferBegin = flatBufferDescriptor->begin();
2341 auto flatBufferEnd = flatBufferDescriptor->end();
2342 auto flatBufferStride = flatBufferDescriptor->stride();
2344 if (!(flatBufferBegin->Length() == flatBufferEnd->Length() &&
2345 flatBufferBegin->Length() == flatBufferStride->Length()))
2348 boost::format(
"The size of the begin, end, and stride must be equal %1%") %
CHECK_LOCATION().AsString()));
2351 std::vector<int> begin(flatBufferBegin->begin(), flatBufferBegin->end());
2352 std::vector<int> end(flatBufferEnd->begin(), flatBufferEnd->end());
2353 std::vector<int> stride(flatBufferStride->begin(), flatBufferStride->end());
2356 descriptor.m_BeginMask = flatBufferDescriptor->beginMask();
2357 descriptor.m_EndMask = flatBufferDescriptor->endMask();
2358 descriptor.m_ShrinkAxisMask = flatBufferDescriptor->shrinkAxisMask();
2359 descriptor.m_EllipsisMask = flatBufferDescriptor->ellipsisMask();
2360 descriptor.m_NewAxisMask = flatBufferDescriptor->newAxisMask();
2361 descriptor.m_DataLayout =
ToDataLayout(flatBufferDescriptor->dataLayout());
2364 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(descriptor, layerName.c_str());
2369 RegisterInputSlots(graph, layerIndex, layer);
2370 RegisterOutputSlots(graph, layerIndex, layer);
2373 void Deserializer::ParseSubtraction(
GraphPtr graph,
unsigned int layerIndex)
2376 auto inputs =
GetInputs(graph, layerIndex);
2380 auto outputs =
GetOutputs(graph, layerIndex);
2389 RegisterInputSlots(graph, layerIndex, layer);
2390 RegisterOutputSlots(graph, layerIndex, layer);
2393 void Deserializer::ParseGather(
GraphPtr graph,
unsigned int layerIndex)
2409 RegisterInputSlots(graph, layerIndex, layer);
2410 RegisterOutputSlots(graph, layerIndex, layer);
2413 void Deserializer::ParseMean(
GraphPtr graph,
unsigned int layerIndex)
2423 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_MeanLayer()->descriptor();
2424 auto flatBufferAxis = flatBufferDescriptor->axis();
2425 auto flatBufferKeepDims = flatBufferDescriptor->keepDims();
2428 descriptor.
m_Axis = std::vector<unsigned int>(flatBufferAxis->begin(), flatBufferAxis->end());
2432 IConnectableLayer* layer = m_Network->AddMeanLayer(descriptor, layerName.c_str());
2437 RegisterInputSlots(graph, layerIndex, layer);
2438 RegisterOutputSlots(graph, layerIndex, layer);
2441 void Deserializer::ParseSplitter(
GraphPtr graph,
unsigned int layerIndex)
2450 auto flatBufferViewsDescriptor = graph->layers()->Get(layerIndex)->layer_as_SplitterLayer()->descriptor();
2451 auto flatBufferViewSizes = flatBufferViewsDescriptor->viewSizes();
2452 auto flatBufferOriginsDescriptor = flatBufferViewsDescriptor->origins();
2453 auto flatBufferViewOrigins = flatBufferOriginsDescriptor->viewOrigins();
2454 uint32_t numViews = flatBufferOriginsDescriptor->numViews();
2455 uint32_t numDimensions = flatBufferOriginsDescriptor->numDimensions();
2462 for(
unsigned int vIdx = 0; vIdx < numViews; ++vIdx)
2464 for (
unsigned int dIdx = 0; dIdx < numDimensions; ++dIdx)
2466 viewsDescriptor.
SetViewSize(vIdx, dIdx, flatBufferViewSizes->Get(vIdx)->data()->Get(dIdx));
2467 viewsDescriptor.
SetViewOriginCoord(vIdx, dIdx, flatBufferViewOrigins->Get(vIdx)->data()->Get(dIdx));
2472 IConnectableLayer* layer = m_Network->AddSplitterLayer(viewsDescriptor, layerName.c_str());
2475 for(
unsigned int vIdx = 0; vIdx < numViews; ++vIdx)
2481 RegisterInputSlots(graph, layerIndex, layer);
2482 RegisterOutputSlots(graph, layerIndex, layer);
2500 void Deserializer::ParseLstm(
GraphPtr graph,
unsigned int layerIndex)
2504 auto inputs =
GetInputs(graph, layerIndex);
2507 auto outputs =
GetOutputs(graph, layerIndex);
2510 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_LstmLayer();
2512 auto flatBufferDescriptor = flatBufferLayer->descriptor();
2513 auto flatBufferInputParams = flatBufferLayer->inputParams();
2543 if (!lstmDescriptor.m_CifgEnabled)
2545 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
2546 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
2547 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
2548 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
2558 if (lstmDescriptor.m_ProjectionEnabled)
2560 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
2561 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
2569 if (lstmDescriptor.m_PeepholeEnabled)
2571 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
2572 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
2582 if (lstmDescriptor.m_LayerNormEnabled)
2584 if (!lstmDescriptor.m_CifgEnabled)
2586 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
2589 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
2590 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
2591 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
2598 IConnectableLayer* layer = m_Network->AddLstmLayer(lstmDescriptor, lstmInputParams, layerName.c_str());
2612 RegisterInputSlots(graph, layerIndex, layer);
2613 RegisterOutputSlots(graph, layerIndex, layer);
2625 desc.
m_CellClip = qLstmDescriptor->cellClip();
2639 void Deserializer::ParseQLstm(
GraphPtr graph,
unsigned int layerIndex)
2643 auto inputs =
GetInputs(graph, layerIndex);
2646 auto outputs =
GetOutputs(graph, layerIndex);
2649 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_QLstmLayer();
2651 auto flatBufferDescriptor = flatBufferLayer->descriptor();
2652 auto flatBufferInputParams = flatBufferLayer->inputParams();
2683 if (!qLstmDescriptor.m_CifgEnabled)
2685 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
2686 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
2687 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
2698 if (qLstmDescriptor.m_ProjectionEnabled)
2700 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
2701 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
2712 if (qLstmDescriptor.m_PeepholeEnabled)
2714 if (!qLstmDescriptor.m_CifgEnabled)
2716 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
2720 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
2721 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
2733 if (qLstmDescriptor.m_LayerNormEnabled)
2735 if (!qLstmDescriptor.m_CifgEnabled)
2737 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
2741 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
2742 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
2743 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
2750 IConnectableLayer* layer = m_Network->AddQLstmLayer(qLstmDescriptor, qLstmInputParams, layerName.c_str());
2761 RegisterInputSlots(graph, layerIndex, layer);
2762 RegisterOutputSlots(graph, layerIndex, layer);
2765 void Deserializer::ParseQuantizedLstm(
GraphPtr graph,
unsigned int layerIndex)
2769 auto inputs =
GetInputs(graph, layerIndex);
2772 auto outputs =
GetOutputs(graph, layerIndex);
2775 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_QuantizedLstmLayer();
2777 auto flatBufferInputParams = flatBufferLayer->inputParams();
2807 IConnectableLayer* layer = m_Network->AddQuantizedLstmLayer(lstmInputParams, layerName.c_str());
2815 RegisterInputSlots(graph, layerIndex, layer);
2816 RegisterOutputSlots(graph, layerIndex, layer);
2819 void Deserializer::ParseDequantize(
GraphPtr graph,
unsigned int layerIndex)
2829 const std::string layerName =
GetLayerName(graph, layerIndex);
2835 RegisterInputSlots(graph, layerIndex, layer);
2836 RegisterOutputSlots(graph, layerIndex, layer);
2839 void Deserializer::ParseMerge(
GraphPtr graph,
unsigned int layerIndex)
2849 const std::string layerName =
GetLayerName(graph, layerIndex);
2855 RegisterInputSlots(graph, layerIndex, layer);
2856 RegisterOutputSlots(graph, layerIndex, layer);
2859 void Deserializer::ParseSwitch(
GraphPtr graph,
unsigned int layerIndex)
2862 auto inputs =
GetInputs(graph, layerIndex);
2866 auto outputs =
GetOutputs(graph, layerIndex);
2878 RegisterInputSlots(graph, layerIndex, layer);
2879 RegisterOutputSlots(graph, layerIndex, layer);
2882 void Deserializer::ParsePrelu(
GraphPtr graph,
unsigned int layerIndex)
2885 auto inputs =
GetInputs(graph, layerIndex);
2889 auto outputs =
GetOutputs(graph, layerIndex);
2898 RegisterInputSlots(graph, layerIndex, layer);
2899 RegisterOutputSlots(graph, layerIndex, layer);
2902 void Deserializer::ParseTranspose(
GraphPtr graph,
unsigned int layerIndex)
2906 auto dimsMapping = graph->layers()->Get(layerIndex)->layer_as_TransposeLayer()->descriptor()->dimMappings();
2908 auto inputs =
GetInputs(graph, layerIndex);
2911 auto outputs =
GetOutputs(graph, layerIndex);
2918 IConnectableLayer* layer = m_Network->AddTransposeLayer(descriptor, layerName.c_str());
2921 RegisterInputSlots(graph, layerIndex, layer);
2922 RegisterOutputSlots(graph, layerIndex, layer);
2925 void Deserializer::ParseTransposeConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
2929 auto inputs =
GetInputs(graph, layerIndex);
2932 auto outputs =
GetOutputs(graph, layerIndex);
2935 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_TransposeConvolution2dLayer();
2937 auto serializerDescriptor = serializerLayer->descriptor();
2940 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
2941 descriptor.
m_PadRight = serializerDescriptor->padRight();
2942 descriptor.
m_PadTop = serializerDescriptor->padTop();
2943 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
2944 descriptor.
m_StrideX = serializerDescriptor->strideX();
2945 descriptor.
m_StrideY = serializerDescriptor->strideY();;
2946 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();;
2955 optionalBiases = armnn::MakeOptional<armnn::ConstTensor>(biases);
2958 IConnectableLayer* layer = m_Network->AddTransposeConvolution2dLayer(descriptor,
2964 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2966 RegisterInputSlots(graph, layerIndex, layer);
2967 RegisterOutputSlots(graph, layerIndex, layer);
2970 void Deserializer::ParseStack(
GraphPtr graph,
unsigned int layerIndex)
2973 auto inputs =
GetInputs(graph, layerIndex);
2975 auto outputs =
GetOutputs(graph, layerIndex);
2978 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_StackLayer()->descriptor();
2979 unsigned int axis = flatBufferDescriptor->axis();
2980 unsigned int numInputs = flatBufferDescriptor->numInputs();
2983 auto flatBufferInputShape = flatBufferDescriptor->inputShape();
2984 std::vector<uint32_t> vectorInputShape(flatBufferInputShape->begin(),
2985 flatBufferInputShape->begin() + flatBufferInputShape->size());
2987 TensorShape inputShape(static_cast<unsigned int>(vectorInputShape.size()), vectorInputShape.data());
2990 for (
unsigned int i=0; i<inputs.size(); ++i)
2993 if (descriptor.m_InputShape != inputShape)
2995 std::stringstream ss;
2996 ss <<
"Shape of input " 3000 <<
" does not equal defined input shape " 3001 << descriptor.m_InputShape
3009 IConnectableLayer* layer = m_Network->AddStackLayer(descriptor, layerName.c_str());
3014 RegisterInputSlots(graph, layerIndex, layer);
3015 RegisterOutputSlots(graph, layerIndex, layer);
3018 void Deserializer::ParseStandIn(
GraphPtr graph,
unsigned int layerIndex)
3022 auto inputs =
GetInputs(graph, layerIndex);
3023 auto outputs =
GetOutputs(graph, layerIndex);
3025 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_StandInLayer();
3026 auto fbDescriptor = fbLayer->descriptor();
3029 descriptor.
m_NumInputs = fbDescriptor->numInputs();
3035 const std::string layerName =
GetLayerName(graph, layerIndex);
3038 for (
unsigned int i = 0u; i < descriptor.
m_NumOutputs; ++i)
3044 RegisterInputSlots(graph, layerIndex, layer);
3045 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.
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.
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.
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.
#define CHECK_VALID_SIZE(ACTUAL,...)
bool m_PeepholeEnabled
Enable/disable peephole.
uint32_t m_NumClasses
Number of classes.
#define CHECKED_NON_NEGATIVE(VALUE)
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)
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
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...
const armnnSerializer::SerializedGraph * GraphPtr
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
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
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.
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 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 })