22 #include <boost/format.hpp> 23 #include <boost/numeric/conversion/cast.hpp> 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)
50 boost::format(
"%1% was called with invalid (null) graph. " 51 "Possible reason is that the graph is not yet loaded and Unpack(ed). " 52 "layers:%2% at %3%") %
57 else if (layersIndex >= graph->layers()->size())
61 boost::format(
"%1% was called with an invalid layers index. " 62 "layers:%2% at %3%") %
69 void CheckLayers(
const Deserializer::GraphPtr& graph,
70 unsigned int layersIndex,
71 unsigned int layerIndex,
74 if (graph->layers() ==
nullptr)
78 boost::format(
"%1% was called with invalid (null) graph. " 79 "Possible reason is that the graph is not yet loaded and Unpack(ed). " 80 "layers:%2% at %3%") %
85 else if (layersIndex >= graph->layers()->size())
89 boost::format(
"%1% was called with an invalid layers index. " 90 "layers:%2% at %3%") %
95 else if (layerIndex >= graph->layers()[layersIndex].size()
96 && layerIndex != VIRTUAL_LAYER_ID)
100 boost::format(
"%1% was called with an invalid layer index. " 101 "layers:%2% layer:%3% at %4%") %
112 if (rawPtr ==
nullptr)
116 boost::format(
"%1% was called with a null tensor pointer. " 124 void CheckConstTensorPtr(Deserializer::ConstTensorRawPtr rawPtr,
127 if (rawPtr ==
nullptr)
129 throw ParseException(boost::str(boost::format(
"%1% was called with a null const tensor pointer. at %2%") %
135 void CheckConstTensorSize(
const unsigned int constTensorSize,
136 const unsigned int tensorSize,
139 if (constTensorSize != tensorSize)
141 throw ParseException(boost::str(boost::format(
"%1% wrong number of components supplied to tensor. at:%2%") %
147 #define CHECK_TENSOR_PTR(TENSOR_PTR) \ 148 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION()) 150 #define CHECK_CONST_TENSOR_SIZE(CONST_TENSOR_SIZE, TENSOR_SIZE) \ 151 CheckConstTensorSize(CONST_TENSOR_SIZE, TENSOR_SIZE, CHECK_LOCATION()) 153 #define CHECK_CONST_TENSOR_PTR(TENSOR_PTR) \ 154 CheckConstTensorPtr(TENSOR_PTR, CHECK_LOCATION()) 156 #define CHECK_LAYERS(GRAPH, LAYERS_INDEX, LAYER_INDEX) \ 157 CheckLayers(GRAPH, LAYERS_INDEX, LAYER_INDEX, CHECK_LOCATION()) 159 #define CHECK_GRAPH(GRAPH, LAYERS_INDEX) \ 160 CheckGraph(GRAPH, LAYERS_INDEX, CHECK_LOCATION()) 166 if (actualSize != expected.size())
171 for (
unsigned int i = 0u; i < actualSize; i++)
173 if (actual[i] != static_cast<unsigned int>(expected[i]))
182 Deserializer::Deserializer()
183 : m_Network(nullptr, nullptr),
185 m_ParserFunctions(Layer_MAX+1, &
Deserializer::ParseUnsupportedLayer)
188 m_ParserFunctions[Layer_AbsLayer] = &Deserializer::ParseAbs;
189 m_ParserFunctions[Layer_ActivationLayer] = &Deserializer::ParseActivation;
190 m_ParserFunctions[Layer_AdditionLayer] = &Deserializer::ParseAdd;
191 m_ParserFunctions[Layer_ArgMinMaxLayer] = &Deserializer::ParseArgMinMax;
192 m_ParserFunctions[Layer_BatchToSpaceNdLayer] = &Deserializer::ParseBatchToSpaceNd;
193 m_ParserFunctions[Layer_BatchNormalizationLayer] = &Deserializer::ParseBatchNormalization;
194 m_ParserFunctions[Layer_ComparisonLayer] = &Deserializer::ParseComparison;
195 m_ParserFunctions[Layer_ConcatLayer] = &Deserializer::ParseConcat;
196 m_ParserFunctions[Layer_ConstantLayer] = &Deserializer::ParseConstant;
197 m_ParserFunctions[Layer_Convolution2dLayer] = &Deserializer::ParseConvolution2d;
198 m_ParserFunctions[Layer_DepthToSpaceLayer] = &Deserializer::ParseDepthToSpace;
199 m_ParserFunctions[Layer_DepthwiseConvolution2dLayer] = &Deserializer::ParseDepthwiseConvolution2d;
200 m_ParserFunctions[Layer_DequantizeLayer] = &Deserializer::ParseDequantize;
201 m_ParserFunctions[Layer_DetectionPostProcessLayer] = &Deserializer::ParseDetectionPostProcess;
202 m_ParserFunctions[Layer_DivisionLayer] = &Deserializer::ParseDivision;
203 m_ParserFunctions[Layer_ElementwiseUnaryLayer] = &Deserializer::ParseElementwiseUnary;
204 m_ParserFunctions[Layer_EqualLayer] = &Deserializer::ParseEqual;
205 m_ParserFunctions[Layer_FullyConnectedLayer] = &Deserializer::ParseFullyConnected;
206 m_ParserFunctions[Layer_FillLayer] = &Deserializer::ParseFill;
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_RankLayer] = &Deserializer::ParseRank;
229 m_ParserFunctions[Layer_ReshapeLayer] = &Deserializer::ParseReshape;
230 m_ParserFunctions[Layer_ResizeBilinearLayer] = &Deserializer::ParseResizeBilinear;
231 m_ParserFunctions[Layer_ResizeLayer] = &Deserializer::ParseResize;
232 m_ParserFunctions[Layer_RsqrtLayer] = &Deserializer::ParseRsqrt;
233 m_ParserFunctions[Layer_SliceLayer] = &Deserializer::ParseSlice;
234 m_ParserFunctions[Layer_SoftmaxLayer] = &Deserializer::ParseSoftmax;
235 m_ParserFunctions[Layer_SpaceToBatchNdLayer] = &Deserializer::ParseSpaceToBatchNd;
236 m_ParserFunctions[Layer_SpaceToDepthLayer] = &Deserializer::ParseSpaceToDepth;
237 m_ParserFunctions[Layer_SplitterLayer] = &Deserializer::ParseSplitter;
238 m_ParserFunctions[Layer_StackLayer] = &Deserializer::ParseStack;
239 m_ParserFunctions[Layer_StandInLayer] = &Deserializer::ParseStandIn;
240 m_ParserFunctions[Layer_StridedSliceLayer] = &Deserializer::ParseStridedSlice;
241 m_ParserFunctions[Layer_SubtractionLayer] = &Deserializer::ParseSubtraction;
242 m_ParserFunctions[Layer_SwitchLayer] = &Deserializer::ParseSwitch;
243 m_ParserFunctions[Layer_TransposeConvolution2dLayer] = &Deserializer::ParseTransposeConvolution2d;
244 m_ParserFunctions[Layer_TransposeLayer] = &Deserializer::ParseTranspose;
249 auto layerType = graphPtr->layers()->Get(layerIndex)->layer_type();
253 case Layer::Layer_AbsLayer:
254 return graphPtr->layers()->Get(layerIndex)->layer_as_AbsLayer()->base();
255 case Layer::Layer_ActivationLayer:
256 return graphPtr->layers()->Get(layerIndex)->layer_as_ActivationLayer()->base();
257 case Layer::Layer_AdditionLayer:
258 return graphPtr->layers()->Get(layerIndex)->layer_as_AdditionLayer()->base();
259 case Layer::Layer_ArgMinMaxLayer:
260 return graphPtr->layers()->Get(layerIndex)->layer_as_ArgMinMaxLayer()->base();
261 case Layer::Layer_BatchToSpaceNdLayer:
262 return graphPtr->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->base();
263 case Layer::Layer_BatchNormalizationLayer:
264 return graphPtr->layers()->Get(layerIndex)->layer_as_BatchNormalizationLayer()->base();
265 case Layer::Layer_ComparisonLayer:
266 return graphPtr->layers()->Get(layerIndex)->layer_as_ComparisonLayer()->base();
267 case Layer::Layer_ConcatLayer:
268 return graphPtr->layers()->Get(layerIndex)->layer_as_ConcatLayer()->base();
269 case Layer::Layer_ConstantLayer:
270 return graphPtr->layers()->Get(layerIndex)->layer_as_ConstantLayer()->base();
271 case Layer::Layer_Convolution2dLayer:
272 return graphPtr->layers()->Get(layerIndex)->layer_as_Convolution2dLayer()->base();
273 case Layer::Layer_DepthToSpaceLayer:
274 return graphPtr->layers()->Get(layerIndex)->layer_as_DepthToSpaceLayer()->base();
275 case Layer::Layer_DepthwiseConvolution2dLayer:
276 return graphPtr->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer()->base();
277 case Layer::Layer_DequantizeLayer:
278 return graphPtr->layers()->Get(layerIndex)->layer_as_DequantizeLayer()->base();
279 case Layer::Layer_DetectionPostProcessLayer:
280 return graphPtr->layers()->Get(layerIndex)->layer_as_DetectionPostProcessLayer()->base();
281 case Layer::Layer_DivisionLayer:
282 return graphPtr->layers()->Get(layerIndex)->layer_as_DivisionLayer()->base();
283 case Layer::Layer_EqualLayer:
284 return graphPtr->layers()->Get(layerIndex)->layer_as_EqualLayer()->base();
285 case Layer::Layer_FullyConnectedLayer:
286 return graphPtr->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer()->base();
287 case Layer::Layer_FillLayer:
288 return graphPtr->layers()->Get(layerIndex)->layer_as_FillLayer()->base();
289 case Layer::Layer_FloorLayer:
290 return graphPtr->layers()->Get(layerIndex)->layer_as_FloorLayer()->base();
291 case Layer::Layer_GatherLayer:
292 return graphPtr->layers()->Get(layerIndex)->layer_as_GatherLayer()->base();
293 case Layer::Layer_GreaterLayer:
294 return graphPtr->layers()->Get(layerIndex)->layer_as_GreaterLayer()->base();
295 case Layer::Layer_InputLayer:
296 return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->base();
297 case Layer::Layer_InstanceNormalizationLayer:
298 return graphPtr->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer()->base();
299 case Layer::Layer_L2NormalizationLayer:
300 return graphPtr->layers()->Get(layerIndex)->layer_as_L2NormalizationLayer()->base();
301 case Layer::Layer_LogSoftmaxLayer:
302 return graphPtr->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->base();
303 case Layer::Layer_LstmLayer:
304 return graphPtr->layers()->Get(layerIndex)->layer_as_LstmLayer()->base();
305 case Layer::Layer_MeanLayer:
306 return graphPtr->layers()->Get(layerIndex)->layer_as_MeanLayer()->base();
307 case Layer::Layer_MinimumLayer:
308 return graphPtr->layers()->Get(layerIndex)->layer_as_MinimumLayer()->base();
309 case Layer::Layer_MaximumLayer:
310 return graphPtr->layers()->Get(layerIndex)->layer_as_MaximumLayer()->base();
311 case Layer::Layer_MergeLayer:
312 return graphPtr->layers()->Get(layerIndex)->layer_as_MergeLayer()->base();
313 case Layer::Layer_MergerLayer:
314 return graphPtr->layers()->Get(layerIndex)->layer_as_MergerLayer()->base();
315 case Layer::Layer_MultiplicationLayer:
316 return graphPtr->layers()->Get(layerIndex)->layer_as_MultiplicationLayer()->base();
317 case Layer::Layer_NormalizationLayer:
318 return graphPtr->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->base();
319 case Layer::Layer_OutputLayer:
320 return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->base();
321 case Layer::Layer_PadLayer:
322 return graphPtr->layers()->Get(layerIndex)->layer_as_PadLayer()->base();
323 case Layer::Layer_PermuteLayer:
324 return graphPtr->layers()->Get(layerIndex)->layer_as_PermuteLayer()->base();
325 case Layer::Layer_Pooling2dLayer:
326 return graphPtr->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->base();
327 case Layer::Layer_PreluLayer:
328 return graphPtr->layers()->Get(layerIndex)->layer_as_PreluLayer()->base();
329 case Layer::Layer_QLstmLayer:
330 return graphPtr->layers()->Get(layerIndex)->layer_as_QLstmLayer()->base();
331 case Layer::Layer_QuantizeLayer:
332 return graphPtr->layers()->Get(layerIndex)->layer_as_QuantizeLayer()->base();
333 case Layer::Layer_QuantizedLstmLayer:
334 return graphPtr->layers()->Get(layerIndex)->layer_as_QuantizedLstmLayer()->base();
335 case Layer::Layer_RankLayer:
336 return graphPtr->layers()->Get(layerIndex)->layer_as_RankLayer()->base();
337 case Layer::Layer_ReshapeLayer:
338 return graphPtr->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->base();
339 case Layer::Layer_ResizeBilinearLayer:
340 return graphPtr->layers()->Get(layerIndex)->layer_as_ResizeBilinearLayer()->base();
341 case Layer::Layer_ResizeLayer:
342 return graphPtr->layers()->Get(layerIndex)->layer_as_ResizeLayer()->base();
343 case Layer::Layer_RsqrtLayer:
344 return graphPtr->layers()->Get(layerIndex)->layer_as_RsqrtLayer()->base();
345 case Layer::Layer_SliceLayer:
346 return graphPtr->layers()->Get(layerIndex)->layer_as_SliceLayer()->base();
347 case Layer::Layer_SoftmaxLayer:
348 return graphPtr->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->base();
349 case Layer::Layer_SpaceToBatchNdLayer:
350 return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->base();
351 case Layer::Layer_SpaceToDepthLayer:
352 return graphPtr->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->base();
353 case Layer::Layer_SplitterLayer:
354 return graphPtr->layers()->Get(layerIndex)->layer_as_SplitterLayer()->base();
355 case Layer::Layer_StackLayer:
356 return graphPtr->layers()->Get(layerIndex)->layer_as_StackLayer()->base();
357 case Layer::Layer_StandInLayer:
358 return graphPtr->layers()->Get(layerIndex)->layer_as_StandInLayer()->base();
359 case Layer::Layer_StridedSliceLayer:
360 return graphPtr->layers()->Get(layerIndex)->layer_as_StridedSliceLayer()->base();
361 case Layer::Layer_SubtractionLayer:
362 return graphPtr->layers()->Get(layerIndex)->layer_as_SubtractionLayer()->base();
363 case Layer::Layer_SwitchLayer:
364 return graphPtr->layers()->Get(layerIndex)->layer_as_SwitchLayer()->base();
365 case Layer::Layer_TransposeConvolution2dLayer:
366 return graphPtr->layers()->Get(layerIndex)->layer_as_TransposeConvolution2dLayer()->base();
367 case Layer::Layer_TransposeLayer:
368 return graphPtr->layers()->Get(layerIndex)->layer_as_TransposeLayer()->base();
369 case Layer::Layer_NONE:
372 boost::format(
"Layer type %1% not recognized") %
381 return layer->layerName()->str();
386 auto layerType = graphPtr->layers()->Get(layerIndex)->layer_type();
388 if (layerType == Layer::Layer_InputLayer)
390 return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->layerBindingId();
392 else if ( layerType == Layer::Layer_OutputLayer )
394 return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->layerBindingId();
403 case armnnSerializer::DataLayout::DataLayout_NHWC:
405 case armnnSerializer::DataLayout::DataLayout_NCHW:
415 case armnnSerializer::ActivationFunction_Sigmoid:
417 case armnnSerializer::ActivationFunction_TanH:
419 case armnnSerializer::ActivationFunction_Linear:
421 case armnnSerializer::ActivationFunction_ReLu:
423 case armnnSerializer::ActivationFunction_BoundedReLu:
425 case armnnSerializer::ActivationFunction_LeakyReLu:
427 case armnnSerializer::ActivationFunction_Abs:
429 case armnnSerializer::ActivationFunction_Sqrt:
431 case armnnSerializer::ActivationFunction_Square:
433 case armnnSerializer::ActivationFunction_Elu:
435 case armnnSerializer::ActivationFunction_HardSwish:
446 case armnnSerializer::ArgMinMaxFunction::ArgMinMaxFunction_Max:
448 case armnnSerializer::ArgMinMaxFunction::ArgMinMaxFunction_Min:
458 case armnnSerializer::ComparisonOperation::ComparisonOperation_Equal:
460 case armnnSerializer::ComparisonOperation::ComparisonOperation_Greater:
462 case armnnSerializer::ComparisonOperation::ComparisonOperation_GreaterOrEqual:
464 case armnnSerializer::ComparisonOperation::ComparisonOperation_Less:
466 case armnnSerializer::ComparisonOperation::ComparisonOperation_LessOrEqual:
468 case armnnSerializer::ComparisonOperation::ComparisonOperation_NotEqual:
478 case armnnSerializer::UnaryOperation::UnaryOperation_Abs:
480 case armnnSerializer::UnaryOperation::UnaryOperation_Rsqrt:
482 case armnnSerializer::UnaryOperation::UnaryOperation_Sqrt:
484 case armnnSerializer::UnaryOperation::UnaryOperation_Exp:
486 case armnnSerializer::UnaryOperation::UnaryOperation_Neg:
497 case armnnSerializer::ResizeMethod_NearestNeighbor:
499 case armnnSerializer::ResizeMethod_Bilinear:
511 switch (tensorPtr->dataType())
513 case DataType_QAsymmS8:
516 case DataType_QSymmS8:
519 case DataType_QuantisedAsymm8:
520 case DataType_QAsymmU8:
523 case DataType_QSymmS16:
524 case DataType_QuantisedSymm16:
527 case DataType_Signed32:
530 case DataType_Float32:
533 case DataType_Float16:
536 case DataType_Boolean:
544 boost::format(
"Unsupported data type %1% = %2%. %3%") %
545 tensorPtr->dataType() %
546 EnumNameDataType(tensorPtr->dataType()) %
551 if (tensorPtr->dimensionality() ==
static_cast<unsigned int>(Dimensionality::Scalar))
553 float quantizationScale = tensorPtr->quantizationScale();
554 int32_t quantizationOffset = tensorPtr->quantizationOffset();
562 auto dimensions = tensorPtr->dimensions();
563 unsigned int size = dimensions->size();
564 std::vector<unsigned int> outputDims(dimensions->begin(), dimensions->begin() + size);
566 auto quantizationScales = tensorPtr->quantizationScales();
568 if (quantizationScales)
570 unsigned int quantizationScalesSize = quantizationScales->size();
571 std::vector<float> scales(quantizationScales->begin(), quantizationScales->begin() + quantizationScalesSize);
572 unsigned int quantizationDim = tensorPtr->quantizationDim();
581 float quantizationScale = tensorPtr->quantizationScale();
582 int32_t quantizationOffset = tensorPtr->quantizationOffset();
598 switch (constTensorPtr->data_type())
600 case ConstTensorData_ByteData:
602 auto byteData = constTensorPtr->data_as_ByteData()->data();
606 case ConstTensorData_ShortData:
608 auto shortData = constTensorPtr->data_as_ShortData()->data();
612 case ConstTensorData_IntData:
614 auto intData = constTensorPtr->data_as_IntData()->data();
618 case ConstTensorData_LongData:
620 auto longData = constTensorPtr->data_as_LongData()->data();
628 boost::str(boost::format(
"Unsupported data type %1% = %2%. %3%") %
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();
676 boost::format(
"Layer not supported. " 678 "layerName: %2% / %3%") %
684 void Deserializer::ResetParser()
687 m_InputBindings.clear();
688 m_OutputBindings.clear();
710 return CreateNetworkFromGraph(graph);
716 std::vector<uint8_t> content((std::istreambuf_iterator<char>(binaryContent)), std::istreambuf_iterator<char>());
718 return CreateNetworkFromGraph(graph);
723 if (binaryContent ==
nullptr)
728 flatbuffers::Verifier verifier(binaryContent, len);
729 if (verifier.VerifyBuffer<SerializedGraph>() ==
false)
732 boost::str(boost::format(
"Buffer doesn't conform to the expected Armnn " 733 "flatbuffers format. size:%1% %2%") %
737 return GetSerializedGraph(binaryContent);
742 m_Network = INetwork::Create();
744 unsigned int layerIndex = 0;
745 for (AnyLayer
const* layer : *graph->layers())
747 if (layer->layer_type() != Layer_InputLayer &&
748 layer->layer_type() != Layer_OutputLayer)
751 auto& parserFunction = m_ParserFunctions[layer->layer_type()];
752 (this->*parserFunction)(graph, layerIndex);
757 SetupInputLayers(graph);
758 SetupOutputLayers(graph);
761 for (
auto&& graphIt : m_GraphConnections)
763 Connections& connections = graphIt.second;
764 for (
auto&& outputIt : connections.outputSlots)
766 const unsigned int outputSlotIndex = outputIt.first;
768 if (connections.inputSlots.find(outputSlotIndex) != connections.inputSlots.end())
770 for (
IInputSlot* inputSlot : connections.inputSlots[outputSlotIndex])
772 outputSlot->
Connect(*inputSlot);
778 return std::move(m_Network);
782 const std::string& name)
const 785 for (
auto inputBinding : m_InputBindings)
787 if (inputBinding.first == name)
789 return inputBinding.second;
794 boost::format(
"No input binding found for layer:%1% / %2%") %
800 const std::string& name)
const 803 for (
auto outputBinding : m_OutputBindings)
805 if (outputBinding.first == name)
807 return outputBinding.second;
812 boost::format(
"No output binding found for layer:%1% / %2%") %
817 unsigned int Deserializer::GetInputLayerInVector(
GraphPtr graph,
int targetId)
819 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
821 auto layer = graph->layers()->Get(i);
822 if (layer->layer_type() == Layer::Layer_InputLayer)
824 auto layerBindingId = layer->layer_as_InputLayer()->base()->layerBindingId();
825 if (layerBindingId == targetId)
831 throw ParseException(
"Input layer with given layerBindingId not found");
834 unsigned int Deserializer::GetOutputLayerInVector(
GraphPtr graph,
int targetId)
836 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
838 auto layer = graph->layers()->Get(i);
839 if (layer->layer_type() == Layer::Layer_OutputLayer)
841 auto layerBindingId = layer->layer_as_OutputLayer()->base()->layerBindingId();
842 if (layerBindingId == targetId)
848 throw ParseException(
"Output layer with given layerBindingId not found");
851 unsigned int Deserializer::GetLayerIndexInVector(
GraphPtr graph,
unsigned int targetIndex)
853 for (
unsigned int i = 0; i < graph->layers()->size(); i++)
856 if (layer->index() == targetIndex)
864 Deserializer::FeatureVersions Deserializer::GetFeatureVersions(
GraphPtr graph)
866 Deserializer::FeatureVersions versions;
868 if (graph->featureVersions())
870 versions.m_BindingIdScheme = graph->featureVersions()->bindingIdsScheme();
876 void Deserializer::SetupInputLayers(
GraphPtr graph)
879 const unsigned int numInputs = graph->inputIds()->size();
880 m_InputBindings.clear();
881 m_InputBindings.reserve(numInputs);
883 for (
unsigned int i = 0; i < numInputs; i++)
885 unsigned int inputLayerIndex = 0xFFFFFFFF;
886 if (GetFeatureVersions(graph).m_BindingIdScheme == 0)
889 inputLayerIndex = GetLayerIndexInVector(graph, inputId);
893 const int inputId = graph->inputIds()->Get(i);
894 inputLayerIndex = GetInputLayerInVector(graph, inputId);
904 m_Network->AddInputLayer(bindingId, baseLayer->layerName()->c_str());
907 inputLayer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
908 RegisterOutputSlots(graph, inputLayerIndex, inputLayer);
911 m_InputBindings.push_back(std::make_pair(baseLayer->layerName()->c_str(), bindingInfo));
915 void Deserializer::SetupOutputLayers(
GraphPtr graph)
918 const unsigned int numOutputs = graph->outputIds()->size();
919 m_OutputBindings.clear();
920 m_OutputBindings.reserve(numOutputs);
922 for (
unsigned int i = 0; i < numOutputs; i++)
924 unsigned int outputLayerIndex = 0xFFFFFFFF;
925 if (GetFeatureVersions(graph).m_BindingIdScheme == 0)
927 const unsigned int outputId =
boost::numeric_cast<
unsigned int>(graph->outputIds()->Get(i));
928 outputLayerIndex = GetLayerIndexInVector(graph, outputId);
932 const int outputId = graph->outputIds()->Get(i);
933 outputLayerIndex = GetOutputLayerInVector(graph, outputId);
943 m_Network->AddOutputLayer(bindingId, baseLayer->layerName()->c_str());
945 RegisterInputSlots(graph, outputLayerIndex, outputLayer);
947 unsigned int sourceLayerIndex =
948 GetLayerIndexInVector(graph, baseLayer->inputSlots()->Get(0)->connection()->sourceLayerIndex());
953 m_OutputBindings.push_back(std::make_pair(baseLayer->layerName()->c_str(), bindingInfo));
957 void Deserializer::RegisterOutputSlots(
GraphPtr graph,
967 boost::str(boost::format(
"The number of outputslots (%1%) does not match the number expected (%2%)" 968 " for layer index: %3% %4%") %
969 baseLayer->outputSlots()->size() %
977 const unsigned int slotIndex = baseLayer->outputSlots()->Get(i)->index();
980 RegisterOutputSlotOfConnection(baseLayer->index(), slotIndex, outputSlot);
984 void Deserializer::RegisterInputSlots(
GraphPtr graph,
994 boost::str(boost::format(
"The number of inputslots (%1%) does not match the number expected (%2%)" 995 " for layer index:%3% %4%") %
996 baseLayer->inputSlots()->size() %
1004 auto fbInputSlot = baseLayer->inputSlots()->Get(i);
1005 auto fbConnection = fbInputSlot->connection();
1007 RegisterInputSlotOfConnection(fbConnection->sourceLayerIndex(), fbConnection->outputSlotIndex(), inputSlot);
1011 void Deserializer::RegisterInputSlotOfConnection(uint32_t sourceLayerIndex,
1012 uint32_t outputSlotIndex,
1015 if (m_GraphConnections.find(sourceLayerIndex) == m_GraphConnections.end())
1017 m_GraphConnections[sourceLayerIndex] = Connections();
1020 Connections& connections = m_GraphConnections[sourceLayerIndex];
1021 if (connections.inputSlots.find(outputSlotIndex) == connections.inputSlots.end())
1023 connections.inputSlots[outputSlotIndex] = {inputSlot};
1027 connections.inputSlots[outputSlotIndex].push_back(inputSlot);
1031 void Deserializer::RegisterOutputSlotOfConnection(uint32_t sourceLayerIndex,
1032 uint32_t outputSlotIndex,
1035 if (m_GraphConnections.find(sourceLayerIndex) == m_GraphConnections.end())
1037 m_GraphConnections[sourceLayerIndex] = Connections();
1040 Connections& connections = m_GraphConnections[sourceLayerIndex];
1041 if (connections.outputSlots.find(outputSlotIndex) != connections.outputSlots.end())
1046 connections.outputSlots[outputSlotIndex] = outputSlot;
1052 auto inputs =
GetInputs(graph, layerIndex);
1056 auto outputs =
GetOutputs(graph, layerIndex);
1062 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
1066 RegisterInputSlots(graph, layerIndex, layer);
1067 RegisterOutputSlots(graph, layerIndex, layer);
1070 void Deserializer::ParseActivation(
GraphPtr graph,
unsigned int layerIndex)
1073 auto inputs =
GetInputs(graph, layerIndex);
1077 auto outputs =
GetOutputs(graph, layerIndex);
1080 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ActivationLayer();
1082 auto serializerDescriptor = serializerLayer->descriptor();
1086 descriptor.
m_A = serializerDescriptor->a();
1087 descriptor.
m_B = serializerDescriptor->b();
1094 RegisterInputSlots(graph, layerIndex, layer);
1095 RegisterOutputSlots(graph, layerIndex, layer);
1098 void Deserializer::ParseAdd(
GraphPtr graph,
unsigned int layerIndex)
1101 auto inputs =
GetInputs(graph, layerIndex);
1105 auto outputs =
GetOutputs(graph, layerIndex);
1114 RegisterInputSlots(graph, layerIndex, layer);
1115 RegisterOutputSlots(graph, layerIndex, layer);
1118 void Deserializer::ParseArgMinMax(
GraphPtr graph,
unsigned int layerIndex)
1121 auto inputs =
GetInputs(graph, layerIndex);
1125 auto outputs =
GetOutputs(graph, layerIndex);
1128 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ArgMinMaxLayer();
1129 auto serializerDescriptor = serializerLayer->descriptor();
1133 descriptor.
m_Axis = serializerDescriptor->axis();
1135 IConnectableLayer* layer = m_Network->AddArgMinMaxLayer(descriptor, layerName.c_str());
1140 RegisterInputSlots(graph, layerIndex, layer);
1141 RegisterOutputSlots(graph, layerIndex, layer);
1144 void Deserializer::ParseBatchToSpaceNd(
GraphPtr graph,
unsigned int layerIndex)
1154 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_BatchToSpaceNdLayer()->descriptor();
1155 auto flatBufferCrops = flatBufferDescriptor->crops();
1156 auto flatBufferBlockShape = flatBufferDescriptor->blockShape();
1158 if (flatBufferCrops->Length() % 2 != 0)
1161 boost::format(
"The size of crops must be divisible by 2 %1%") %
CHECK_LOCATION().AsString()));
1164 std::vector<std::pair<unsigned int, unsigned int>> crops;
1165 crops.reserve(flatBufferCrops->Length() / 2);
1166 for (
unsigned int i = 0; i < flatBufferCrops->Length() - 1; i += 2)
1168 crops.emplace_back(flatBufferCrops->Get(i), flatBufferCrops->Get(i+1));
1174 std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end());
1178 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(descriptor, layerName.c_str());
1183 RegisterInputSlots(graph, layerIndex, layer);
1184 RegisterOutputSlots(graph, layerIndex, layer);
1187 void Deserializer::ParseBatchNormalization(
GraphPtr graph,
unsigned int layerIndex)
1191 auto inputs =
GetInputs(graph, layerIndex);
1194 auto outputs =
GetOutputs(graph, layerIndex);
1200 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_BatchNormalizationLayer();
1201 auto serializerDescriptor = serializerLayer->descriptor();
1204 descriptor.
m_Eps = serializerDescriptor->eps();
1218 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
1220 RegisterInputSlots(graph, layerIndex, layer);
1221 RegisterOutputSlots(graph, layerIndex, layer);
1224 void Deserializer::ParseConstant(
GraphPtr graph,
unsigned int layerIndex)
1229 auto outputs =
GetOutputs(graph, layerIndex);
1234 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_ConstantLayer();
1235 auto serializerInput = serializerLayer->input();
1239 IConnectableLayer* layer = m_Network->AddConstantLayer(input, layerName.c_str());
1244 RegisterOutputSlots(graph, layerIndex, layer);
1247 void Deserializer::ParseConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
1250 auto inputs =
GetInputs(graph, layerIndex);
1254 auto outputs =
GetOutputs(graph, layerIndex);
1257 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_Convolution2dLayer();
1259 auto serializerDescriptor = serializerLayer->descriptor();
1262 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
1263 descriptor.
m_PadRight = serializerDescriptor->padRight();
1264 descriptor.
m_PadTop = serializerDescriptor->padTop();
1265 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
1266 descriptor.
m_StrideX = serializerDescriptor->strideX();
1267 descriptor.
m_StrideY = serializerDescriptor->strideY();;
1268 descriptor.
m_DilationX = serializerDescriptor->dilationX();
1269 descriptor.
m_DilationY = serializerDescriptor->dilationY();;
1270 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();;
1287 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1289 RegisterInputSlots(graph, layerIndex, layer);
1290 RegisterOutputSlots(graph, layerIndex, layer);
1293 void Deserializer::ParseDepthToSpace(
GraphPtr graph,
unsigned int layerIndex)
1297 auto inputs =
GetInputs(graph, layerIndex);
1300 auto outputs =
GetOutputs(graph, layerIndex);
1303 auto fbDescriptor = graph->layers()->Get(layerIndex)->layer_as_DepthToSpaceLayer()->descriptor();
1306 descriptor.
m_BlockSize = fbDescriptor->blockSize();
1310 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
1315 RegisterInputSlots(graph, layerIndex, layer);
1316 RegisterOutputSlots(graph, layerIndex, layer);
1319 void Deserializer::ParseDepthwiseConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
1322 auto inputs =
GetInputs(graph, layerIndex);
1326 auto outputs =
GetOutputs(graph, layerIndex);
1329 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer();
1331 auto serializerDescriptor = serializerLayer->descriptor();
1334 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
1335 descriptor.
m_PadRight = serializerDescriptor->padRight();
1336 descriptor.
m_PadTop = serializerDescriptor->padTop();
1337 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
1338 descriptor.
m_StrideX = serializerDescriptor->strideX();
1339 descriptor.
m_StrideY = serializerDescriptor->strideY();
1340 descriptor.
m_DilationX = serializerDescriptor->dilationX();
1341 descriptor.
m_DilationY = serializerDescriptor->dilationY();
1342 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();;
1354 IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(descriptor,
1360 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1362 RegisterInputSlots(graph, layerIndex, layer);
1363 RegisterOutputSlots(graph, layerIndex, layer);
1366 void Deserializer::ParseDetectionPostProcess(
GraphPtr graph,
unsigned int layerIndex)
1369 auto inputs =
GetInputs(graph, layerIndex);
1373 auto outputs =
GetOutputs(graph, layerIndex);
1376 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_DetectionPostProcessLayer();
1378 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1386 descriptor.
m_NumClasses = flatBufferDescriptor->numClasses();
1388 descriptor.
m_ScaleX = flatBufferDescriptor->scaleX();
1389 descriptor.
m_ScaleY = flatBufferDescriptor->scaleY();
1390 descriptor.
m_ScaleW = flatBufferDescriptor->scaleW();
1391 descriptor.
m_ScaleH = flatBufferDescriptor->scaleH();
1399 for (
unsigned int i = 0; i < 4; i++)
1401 layer->GetOutputSlot(i).SetTensorInfo(
ToTensorInfo(outputs[i]));
1404 RegisterInputSlots(graph, layerIndex, layer);
1405 RegisterOutputSlots(graph, layerIndex, layer);
1408 void Deserializer::ParseDivision(
GraphPtr graph,
unsigned int layerIndex)
1411 auto inputs =
GetInputs(graph, layerIndex);
1415 auto outputs =
GetOutputs(graph, layerIndex);
1424 RegisterInputSlots(graph, layerIndex, layer);
1425 RegisterOutputSlots(graph, layerIndex, layer);
1428 void Deserializer::ParseEqual(
GraphPtr graph,
unsigned int layerIndex)
1431 auto inputs =
GetInputs(graph, layerIndex);
1435 auto outputs =
GetOutputs(graph, layerIndex);
1440 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
1445 RegisterInputSlots(graph, layerIndex, layer);
1446 RegisterOutputSlots(graph, layerIndex, layer);
1449 void Deserializer::ParseFill(
GraphPtr graph,
unsigned int layerIndex)
1452 auto inputs =
GetInputs(graph, layerIndex);
1456 auto outputs =
GetOutputs(graph, layerIndex);
1461 IConnectableLayer* layer = m_Network->AddFillLayer(descriptor, layerName.c_str());
1466 RegisterInputSlots(graph, layerIndex, layer);
1467 RegisterOutputSlots(graph, layerIndex, layer);
1470 void Deserializer::ParseGreater(
GraphPtr graph,
unsigned int layerIndex)
1473 auto inputs =
GetInputs(graph, layerIndex);
1477 auto outputs =
GetOutputs(graph, layerIndex);
1482 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
1487 RegisterInputSlots(graph, layerIndex, layer);
1488 RegisterOutputSlots(graph, layerIndex, layer);
1491 void Deserializer::ParseInstanceNormalization(
GraphPtr graph,
unsigned int layerIndex)
1495 auto inputs =
GetInputs(graph, layerIndex);
1498 auto outputs =
GetOutputs(graph, layerIndex);
1501 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_InstanceNormalizationLayer();
1502 auto fbDescriptor = fbLayer->descriptor();
1505 descriptor.
m_Gamma = fbDescriptor->gamma();
1506 descriptor.
m_Beta = fbDescriptor->beta();
1507 descriptor.
m_Eps = fbDescriptor->eps();
1510 const std::string layerName =
GetLayerName(graph, layerIndex);
1513 IConnectableLayer* layer = m_Network->AddInstanceNormalizationLayer(descriptor, layerName.c_str());
1516 RegisterInputSlots(graph, layerIndex, layer);
1517 RegisterOutputSlots(graph, layerIndex, layer);
1520 void Deserializer::ParseL2Normalization(
GraphPtr graph,
unsigned int layerIndex)
1524 auto inputs =
GetInputs(graph, layerIndex);
1527 auto outputs =
GetOutputs(graph, layerIndex);
1531 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_L2NormalizationLayer();
1532 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1537 descriptor.
m_Eps = flatBufferDescriptor->eps();
1539 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(descriptor, layerName.c_str());
1542 RegisterInputSlots(graph, layerIndex, layer);
1543 RegisterOutputSlots(graph, layerIndex, layer);
1546 void Deserializer::ParseLogSoftmax(
GraphPtr graph,
unsigned int layerIndex)
1557 descriptor.
m_Beta = graph->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->descriptor()->beta();
1558 descriptor.m_Axis = graph->layers()->Get(layerIndex)->layer_as_LogSoftmaxLayer()->descriptor()->axis();
1561 IConnectableLayer* layer = m_Network->AddLogSoftmaxLayer(descriptor, layerName.c_str());
1566 RegisterInputSlots(graph, layerIndex, layer);
1567 RegisterOutputSlots(graph, layerIndex, layer);
1570 void Deserializer::ParseMinimum(
GraphPtr graph,
unsigned int layerIndex)
1573 auto inputs =
GetInputs(graph, layerIndex);
1577 auto outputs =
GetOutputs(graph, layerIndex);
1586 RegisterInputSlots(graph, layerIndex, layer);
1587 RegisterOutputSlots(graph, layerIndex, layer);
1590 void Deserializer::ParseMaximum(
GraphPtr graph,
unsigned int layerIndex)
1593 auto inputs =
GetInputs(graph, layerIndex);
1597 auto outputs =
GetOutputs(graph, layerIndex);
1606 RegisterInputSlots(graph, layerIndex, layer);
1607 RegisterOutputSlots(graph, layerIndex, layer);
1611 unsigned int layerIndex)
1613 auto layerType = graph->layers()->Get(layerIndex)->layer_type();
1617 case Layer::Layer_ConcatLayer:
1618 return graph->layers()->Get(layerIndex)->layer_as_ConcatLayer()->descriptor();
1619 case Layer::Layer_MergerLayer:
1620 return graph->layers()->Get(layerIndex)->layer_as_MergerLayer()->descriptor();
1626 void Deserializer::ParseComparison(
GraphPtr graph,
unsigned int layerIndex)
1631 auto inputs =
GetInputs(graph, layerIndex);
1634 auto outputs =
GetOutputs(graph, layerIndex);
1637 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ComparisonLayer();
1638 auto fbDescriptor = fbLayer->descriptor();
1643 const std::string& layerName =
GetLayerName(graph, layerIndex);
1644 IConnectableLayer* layer = m_Network->AddComparisonLayer(descriptor, layerName.c_str());
1649 RegisterInputSlots(graph, layerIndex, layer);
1650 RegisterOutputSlots(graph, layerIndex, layer);
1653 void Deserializer::ParseElementwiseUnary(
GraphPtr graph,
unsigned int layerIndex)
1658 auto inputs =
GetInputs(graph, layerIndex);
1661 auto outputs =
GetOutputs(graph, layerIndex);
1664 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_ElementwiseUnaryLayer();
1665 auto fbDescriptor = fbLayer->descriptor();
1670 const std::string& layerName =
GetLayerName(graph, layerIndex);
1671 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
1676 RegisterInputSlots(graph, layerIndex, layer);
1677 RegisterOutputSlots(graph, layerIndex, layer);
1680 void Deserializer::ParseConcat(
GraphPtr graph,
unsigned int layerIndex)
1685 auto outputs =
GetOutputs(graph, layerIndex);
1690 unsigned int numViews = originsDescriptor->numViews();
1691 unsigned int numDimensions = originsDescriptor->numDimensions();
1694 auto inputs =
GetInputs(graph, layerIndex);
1698 auto originsPtr = originsDescriptor->viewOrigins();
1699 for (
unsigned int v = 0; v < numViews; ++v)
1701 auto originPtr = originsPtr->Get(v);
1702 for (
unsigned int d = 0; d < numDimensions; ++d)
1704 uint32_t value = originPtr->data()->Get(d);
1705 descriptor.SetViewOriginCoord(v, d, value);
1708 descriptor.SetConcatAxis(originsDescriptor->concatAxis());
1710 IConnectableLayer* layer = m_Network->AddConcatLayer(descriptor, layerName.c_str());
1714 RegisterInputSlots(graph, layerIndex, layer);
1715 RegisterOutputSlots(graph, layerIndex, layer);
1718 void Deserializer::ParseMultiplication(
GraphPtr graph,
unsigned int layerIndex)
1721 auto inputs =
GetInputs(graph, layerIndex);
1725 auto outputs =
GetOutputs(graph, layerIndex);
1729 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
1734 RegisterInputSlots(graph, layerIndex, layer);
1735 RegisterOutputSlots(graph, layerIndex, layer);
1738 void Deserializer::ParseFloor(
GraphPtr graph,
unsigned int layerIndex)
1743 auto inputs =
GetInputs(graph, layerIndex);
1746 auto outputs =
GetOutputs(graph, layerIndex);
1753 layer = m_Network->AddFloorLayer(layerName.c_str());
1758 RegisterInputSlots(graph, layerIndex, layer);
1759 RegisterOutputSlots(graph, layerIndex, layer);
1762 void Deserializer::ParseFullyConnected(
GraphPtr graph,
unsigned int layerIndex)
1765 auto inputs =
GetInputs(graph, layerIndex);
1769 auto outputs =
GetOutputs(graph, layerIndex);
1772 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer();
1774 auto flatBufferDescriptor = flatBufferLayer->descriptor();
1777 fullyConnectedDescriptor.
m_BiasEnabled = flatBufferDescriptor->biasEnabled();
1784 if (flatBufferDescriptor->biasEnabled())
1789 layer = m_Network->AddFullyConnectedLayer(fullyConnectedDescriptor,
1795 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1797 RegisterInputSlots(graph, layerIndex, layer);
1798 RegisterOutputSlots(graph, layerIndex, layer);
1801 void Deserializer::ParsePad(
GraphPtr graph,
unsigned int layerIndex)
1811 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_PadLayer()->descriptor();
1812 auto flatBufferPadList = flatBufferDescriptor->padList();
1813 float padValue = flatBufferDescriptor->padValue();
1815 if (flatBufferPadList->Length() % 2 != 0)
1818 boost::format(
"The size of the pad list must be divisible by 2 %1%") %
CHECK_LOCATION().AsString()));
1821 std::vector<std::pair<unsigned int, unsigned int>> padList;
1822 padList.reserve(flatBufferPadList->Length() / 2);
1823 for (
unsigned int i = 0; i < flatBufferPadList->Length() - 1; i += 2)
1825 padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1));
1831 IConnectableLayer* layer = m_Network->AddPadLayer(descriptor, layerName.c_str());
1836 RegisterInputSlots(graph, layerIndex, layer);
1837 RegisterOutputSlots(graph, layerIndex, layer);
1840 void Deserializer::ParsePermute(
GraphPtr graph,
unsigned int layerIndex)
1845 graph->layers()->Get(layerIndex)->layer_as_PermuteLayer()->descriptor()->dimMappings();
1847 auto inputs =
GetInputs(graph, layerIndex);
1850 auto outputs =
GetOutputs(graph, layerIndex);
1857 IConnectableLayer* layer = m_Network->AddPermuteLayer(descriptor, layerName.c_str());
1860 RegisterInputSlots(graph, layerIndex, layer);
1861 RegisterOutputSlots(graph, layerIndex, layer);
1865 unsigned int layerIndex)
1870 switch (pooling2dDesc->poolType())
1872 case PoolingAlgorithm_Average:
1877 case PoolingAlgorithm_Max:
1888 switch (pooling2dDesc->outputShapeRounding())
1890 case OutputShapeRounding_Floor:
1895 case OutputShapeRounding_Ceiling:
1906 switch (pooling2dDesc->paddingMethod())
1908 case PaddingMethod_Exclude:
1913 case PaddingMethod_IgnoreValue:
1924 switch (pooling2dDesc->dataLayout())
1926 case DataLayout_NCHW:
1931 case DataLayout_NHWC:
1943 desc.
m_PadLeft = pooling2dDesc->padLeft();
1945 desc.
m_PadTop = pooling2dDesc->padTop();
1946 desc.
m_StrideX = pooling2dDesc->strideX();
1947 desc.
m_StrideY = pooling2dDesc->strideY();
1954 void Deserializer::ParsePooling2d(
GraphPtr graph,
unsigned int layerIndex)
1958 auto pooling2dDes = graph->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->descriptor();
1959 auto inputs =
GetInputs(graph, layerIndex);
1962 auto outputs =
GetOutputs(graph, layerIndex);
1968 IConnectableLayer* layer = m_Network->AddPooling2dLayer(pooling2dDescriptor, layerName.c_str());
1971 RegisterInputSlots(graph, layerIndex, layer);
1972 RegisterOutputSlots(graph, layerIndex, layer);
1975 void Deserializer::ParseQuantize(
GraphPtr graph,
unsigned int layerIndex)
1979 auto inputs =
GetInputs(graph, layerIndex);
1982 auto outputs =
GetOutputs(graph, layerIndex);
1990 RegisterInputSlots(graph, layerIndex, layer);
1991 RegisterOutputSlots(graph, layerIndex, layer);
1995 const std::vector<uint32_t>& targetDimsIn)
1997 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
1998 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2000 if (stretchDim != targetDimsIn.end())
2002 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2005 boost::format(
"At most one component of shape can be -1 %1%") %
CHECK_LOCATION().AsString()));
2008 auto targetNumElements =
2010 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2012 auto stretchIndex =
static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2013 outputDims[stretchIndex] = inputTensorInfo.
GetNumElements() / targetNumElements;
2024 void Deserializer::ParseRank(
GraphPtr graph,
unsigned int layerIndex)
2040 RegisterInputSlots(graph, layerIndex, layer);
2041 RegisterOutputSlots(graph, layerIndex, layer);
2044 void Deserializer::ParseReshape(
GraphPtr graph,
unsigned int layerIndex)
2047 auto inputs =
GetInputs(graph, layerIndex);
2049 auto outputs =
GetOutputs(graph, layerIndex);
2055 const auto targetDims = graph->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->descriptor()->targetShape();
2056 std::vector<uint32_t> outputDims(targetDims->begin(), targetDims->begin() + targetDims->size());
2059 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
2061 const std::vector<uint32_t> expectedDims(outputs[0]->dimensions()->begin(),
2062 outputs[0]->dimensions()->begin() + outputs[0]->dimensions()->size());
2064 if (inputs.size() > 1 && !
CheckShape(reshapeOutputTensorShape, expectedDims))
2066 std::stringstream ss;
2067 ss <<
"New shape defined in reshape parameters " 2068 << reshapeOutputTensorShape
2069 <<
" does not equal output shape " 2070 << actualOutputTensorInfo.
GetShape()
2080 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2083 RegisterInputSlots(graph, layerIndex, layer);
2084 RegisterOutputSlots(graph, layerIndex, layer);
2087 void Deserializer::ParseResize(
GraphPtr graph,
unsigned int layerIndex)
2097 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_ResizeLayer()->descriptor();
2100 descriptor.
m_TargetWidth = flatBufferDescriptor->targetWidth();
2101 descriptor.
m_TargetHeight = flatBufferDescriptor->targetHeight();
2104 descriptor.
m_AlignCorners = flatBufferDescriptor->alignCorners();
2108 IConnectableLayer* layer = m_Network->AddResizeLayer(descriptor, layerName.c_str());
2113 RegisterInputSlots(graph, layerIndex, layer);
2114 RegisterOutputSlots(graph, layerIndex, layer);
2117 void Deserializer::ParseResizeBilinear(
GraphPtr graph,
unsigned int layerIndex)
2127 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_ResizeBilinearLayer()->descriptor();
2130 descriptor.
m_TargetWidth = flatBufferDescriptor->targetWidth();
2131 descriptor.
m_TargetHeight = flatBufferDescriptor->targetHeight();
2134 descriptor.
m_AlignCorners = flatBufferDescriptor->alignCorners();
2138 IConnectableLayer* layer = m_Network->AddResizeLayer(descriptor, layerName.c_str());
2143 RegisterInputSlots(graph, layerIndex, layer);
2144 RegisterOutputSlots(graph, layerIndex, layer);
2147 void Deserializer::ParseSoftmax(
GraphPtr graph,
unsigned int layerIndex)
2158 descriptor.
m_Beta = graph->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->descriptor()->beta();
2161 IConnectableLayer* layer = m_Network->AddSoftmaxLayer(descriptor, layerName.c_str());
2166 RegisterInputSlots(graph, layerIndex, layer);
2167 RegisterOutputSlots(graph, layerIndex, layer);
2170 void Deserializer::ParseSpaceToBatchNd(
GraphPtr graph,
unsigned int layerIndex)
2180 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToBatchNdLayer()->descriptor();
2181 auto flatBufferPadList = flatBufferDescriptor->padList();
2182 auto flatBufferBlockShape = flatBufferDescriptor->blockShape();
2184 if (flatBufferPadList->Length() % 2 != 0)
2187 boost::format(
"The size of the pad list must be divisible by 2 %1%") %
CHECK_LOCATION().AsString()));
2190 std::vector<std::pair<unsigned int, unsigned int>> padList;
2191 padList.reserve(flatBufferPadList->Length() / 2);
2192 for (
unsigned int i = 0; i < flatBufferPadList->Length() - 1; i += 2)
2194 padList.emplace_back(flatBufferPadList->Get(i), flatBufferPadList->Get(i+1));
2200 std::vector<unsigned int>(flatBufferBlockShape->begin(), flatBufferBlockShape->end());
2204 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(descriptor, layerName.c_str());
2209 RegisterInputSlots(graph, layerIndex, layer);
2210 RegisterOutputSlots(graph, layerIndex, layer);
2213 void Deserializer::ParseSpaceToDepth(
GraphPtr graph,
unsigned int layerIndex)
2223 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_SpaceToDepthLayer()->descriptor();
2226 descriptor.
m_BlockSize = flatBufferDescriptor->blockSize();
2230 IConnectableLayer* layer = m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str());
2235 RegisterInputSlots(graph, layerIndex, layer);
2236 RegisterOutputSlots(graph, layerIndex, layer);
2241 unsigned int layerIndex)
2246 switch (normalizationDescriptor->normChannelType())
2248 case NormalizationAlgorithmChannel_Across:
2253 case NormalizationAlgorithmChannel_Within:
2264 switch (normalizationDescriptor->normMethodType())
2266 case NormalizationAlgorithmMethod_LocalBrightness:
2271 case NormalizationAlgorithmMethod_LocalContrast:
2282 switch (normalizationDescriptor->dataLayout())
2284 case DataLayout_NCHW:
2289 case DataLayout_NHWC:
2300 desc.
m_Alpha = normalizationDescriptor->alpha();
2301 desc.
m_Beta = normalizationDescriptor->beta();
2302 desc.
m_K = normalizationDescriptor->k();
2303 desc.
m_NormSize = normalizationDescriptor->normSize();
2308 void Deserializer::ParseNormalization(
GraphPtr graph,
unsigned int layerIndex)
2312 auto normalizationDes = graph->layers()->Get(layerIndex)->layer_as_NormalizationLayer()->descriptor();
2325 IConnectableLayer* layer = m_Network->AddNormalizationLayer(normalizationDescriptor, layerName.c_str());
2328 RegisterInputSlots(graph, layerIndex, layer);
2329 RegisterOutputSlots(graph, layerIndex, layer);
2332 void Deserializer::ParseRsqrt(
GraphPtr graph,
unsigned int layerIndex)
2335 auto inputs =
GetInputs(graph, layerIndex);
2339 auto outputs =
GetOutputs(graph, layerIndex);
2345 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
2349 RegisterInputSlots(graph, layerIndex, layer);
2350 RegisterOutputSlots(graph, layerIndex, layer);
2353 void Deserializer::ParseSlice(
GraphPtr graph,
unsigned int layerIndex)
2357 auto inputs =
GetInputs(graph, layerIndex);
2360 auto outputs =
GetOutputs(graph, layerIndex);
2363 auto fbDescriptor = graph->layers()->Get(layerIndex)->layer_as_SliceLayer()->descriptor();
2365 auto fbBegin = fbDescriptor->begin();
2366 auto fbSize = fbDescriptor->size();
2368 if (fbBegin->Length() != fbSize->Length())
2371 boost::format(
"Begin and size descriptors must have the same length %1%") %
CHECK_LOCATION().AsString()));
2375 descriptor.
m_Begin.insert(descriptor.
m_Begin.end(), fbBegin->begin(), fbBegin->end());
2376 descriptor.
m_Size.insert(descriptor.
m_Size.end(), fbSize->begin(), fbSize->end());
2379 IConnectableLayer* layer = m_Network->AddSliceLayer(descriptor, layerName.c_str());
2384 RegisterInputSlots(graph, layerIndex, layer);
2385 RegisterOutputSlots(graph, layerIndex, layer);
2388 void Deserializer::ParseStridedSlice(
GraphPtr graph,
unsigned int layerIndex)
2398 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_StridedSliceLayer()->descriptor();
2400 auto flatBufferBegin = flatBufferDescriptor->begin();
2401 auto flatBufferEnd = flatBufferDescriptor->end();
2402 auto flatBufferStride = flatBufferDescriptor->stride();
2404 if (!(flatBufferBegin->Length() == flatBufferEnd->Length() &&
2405 flatBufferBegin->Length() == flatBufferStride->Length()))
2408 boost::format(
"The size of the begin, end, and stride must be equal %1%") %
CHECK_LOCATION().AsString()));
2411 std::vector<int> begin(flatBufferBegin->begin(), flatBufferBegin->end());
2412 std::vector<int> end(flatBufferEnd->begin(), flatBufferEnd->end());
2413 std::vector<int> stride(flatBufferStride->begin(), flatBufferStride->end());
2416 descriptor.m_BeginMask = flatBufferDescriptor->beginMask();
2417 descriptor.m_EndMask = flatBufferDescriptor->endMask();
2418 descriptor.m_ShrinkAxisMask = flatBufferDescriptor->shrinkAxisMask();
2419 descriptor.m_EllipsisMask = flatBufferDescriptor->ellipsisMask();
2420 descriptor.m_NewAxisMask = flatBufferDescriptor->newAxisMask();
2421 descriptor.m_DataLayout =
ToDataLayout(flatBufferDescriptor->dataLayout());
2424 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(descriptor, layerName.c_str());
2429 RegisterInputSlots(graph, layerIndex, layer);
2430 RegisterOutputSlots(graph, layerIndex, layer);
2433 void Deserializer::ParseSubtraction(
GraphPtr graph,
unsigned int layerIndex)
2436 auto inputs =
GetInputs(graph, layerIndex);
2440 auto outputs =
GetOutputs(graph, layerIndex);
2449 RegisterInputSlots(graph, layerIndex, layer);
2450 RegisterOutputSlots(graph, layerIndex, layer);
2453 void Deserializer::ParseGather(
GraphPtr graph,
unsigned int layerIndex)
2464 descriptor.
m_Axis = graph->layers()->Get(layerIndex)->layer_as_GatherLayer()->descriptor()->axis();
2467 IConnectableLayer* layer = m_Network->AddGatherLayer(descriptor, layerName.c_str());
2472 RegisterInputSlots(graph, layerIndex, layer);
2473 RegisterOutputSlots(graph, layerIndex, layer);
2476 void Deserializer::ParseMean(
GraphPtr graph,
unsigned int layerIndex)
2486 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_MeanLayer()->descriptor();
2487 auto flatBufferAxis = flatBufferDescriptor->axis();
2488 auto flatBufferKeepDims = flatBufferDescriptor->keepDims();
2491 descriptor.
m_Axis = std::vector<unsigned int>(flatBufferAxis->begin(), flatBufferAxis->end());
2495 IConnectableLayer* layer = m_Network->AddMeanLayer(descriptor, layerName.c_str());
2500 RegisterInputSlots(graph, layerIndex, layer);
2501 RegisterOutputSlots(graph, layerIndex, layer);
2504 void Deserializer::ParseSplitter(
GraphPtr graph,
unsigned int layerIndex)
2513 auto flatBufferViewsDescriptor = graph->layers()->Get(layerIndex)->layer_as_SplitterLayer()->descriptor();
2514 auto flatBufferViewSizes = flatBufferViewsDescriptor->viewSizes();
2515 auto flatBufferOriginsDescriptor = flatBufferViewsDescriptor->origins();
2516 auto flatBufferViewOrigins = flatBufferOriginsDescriptor->viewOrigins();
2517 uint32_t numViews = flatBufferOriginsDescriptor->numViews();
2518 uint32_t numDimensions = flatBufferOriginsDescriptor->numDimensions();
2525 for(
unsigned int vIdx = 0; vIdx < numViews; ++vIdx)
2527 for (
unsigned int dIdx = 0; dIdx < numDimensions; ++dIdx)
2529 viewsDescriptor.
SetViewSize(vIdx, dIdx, flatBufferViewSizes->Get(vIdx)->data()->Get(dIdx));
2530 viewsDescriptor.
SetViewOriginCoord(vIdx, dIdx, flatBufferViewOrigins->Get(vIdx)->data()->Get(dIdx));
2535 IConnectableLayer* layer = m_Network->AddSplitterLayer(viewsDescriptor, layerName.c_str());
2538 for(
unsigned int vIdx = 0; vIdx < numViews; ++vIdx)
2544 RegisterInputSlots(graph, layerIndex, layer);
2545 RegisterOutputSlots(graph, layerIndex, layer);
2563 void Deserializer::ParseLstm(
GraphPtr graph,
unsigned int layerIndex)
2567 auto inputs =
GetInputs(graph, layerIndex);
2570 auto outputs =
GetOutputs(graph, layerIndex);
2573 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_LstmLayer();
2575 auto flatBufferDescriptor = flatBufferLayer->descriptor();
2576 auto flatBufferInputParams = flatBufferLayer->inputParams();
2606 if (!lstmDescriptor.m_CifgEnabled)
2608 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
2609 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
2610 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
2611 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
2621 if (lstmDescriptor.m_ProjectionEnabled)
2623 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
2624 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
2632 if (lstmDescriptor.m_PeepholeEnabled)
2634 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
2635 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
2645 if (lstmDescriptor.m_LayerNormEnabled)
2647 if (!lstmDescriptor.m_CifgEnabled)
2649 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
2652 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
2653 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
2654 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
2661 IConnectableLayer* layer = m_Network->AddLstmLayer(lstmDescriptor, lstmInputParams, layerName.c_str());
2675 RegisterInputSlots(graph, layerIndex, layer);
2676 RegisterOutputSlots(graph, layerIndex, layer);
2688 desc.
m_CellClip = qLstmDescriptor->cellClip();
2702 void Deserializer::ParseQLstm(
GraphPtr graph,
unsigned int layerIndex)
2706 auto inputs =
GetInputs(graph, layerIndex);
2709 auto outputs =
GetOutputs(graph, layerIndex);
2712 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_QLstmLayer();
2714 auto flatBufferDescriptor = flatBufferLayer->descriptor();
2715 auto flatBufferInputParams = flatBufferLayer->inputParams();
2746 if (!qLstmDescriptor.m_CifgEnabled)
2748 inputToInputWeights =
ToConstTensor(flatBufferInputParams->inputToInputWeights());
2749 recurrentToInputWeights =
ToConstTensor(flatBufferInputParams->recurrentToInputWeights());
2750 inputGateBias =
ToConstTensor(flatBufferInputParams->inputGateBias());
2761 if (qLstmDescriptor.m_ProjectionEnabled)
2763 projectionWeights =
ToConstTensor(flatBufferInputParams->projectionWeights());
2764 projectionBias =
ToConstTensor(flatBufferInputParams->projectionBias());
2775 if (qLstmDescriptor.m_PeepholeEnabled)
2777 if (!qLstmDescriptor.m_CifgEnabled)
2779 cellToInputWeights =
ToConstTensor(flatBufferInputParams->cellToInputWeights());
2783 cellToForgetWeights =
ToConstTensor(flatBufferInputParams->cellToForgetWeights());
2784 cellToOutputWeights =
ToConstTensor(flatBufferInputParams->cellToOutputWeights());
2796 if (qLstmDescriptor.m_LayerNormEnabled)
2798 if (!qLstmDescriptor.m_CifgEnabled)
2800 inputLayerNormWeights =
ToConstTensor(flatBufferInputParams->inputLayerNormWeights());
2804 forgetLayerNormWeights =
ToConstTensor(flatBufferInputParams->forgetLayerNormWeights());
2805 cellLayerNormWeights =
ToConstTensor(flatBufferInputParams->cellLayerNormWeights());
2806 outputLayerNormWeights =
ToConstTensor(flatBufferInputParams->outputLayerNormWeights());
2813 IConnectableLayer* layer = m_Network->AddQLstmLayer(qLstmDescriptor, qLstmInputParams, layerName.c_str());
2824 RegisterInputSlots(graph, layerIndex, layer);
2825 RegisterOutputSlots(graph, layerIndex, layer);
2828 void Deserializer::ParseQuantizedLstm(
GraphPtr graph,
unsigned int layerIndex)
2832 auto inputs =
GetInputs(graph, layerIndex);
2835 auto outputs =
GetOutputs(graph, layerIndex);
2838 auto flatBufferLayer = graph->layers()->Get(layerIndex)->layer_as_QuantizedLstmLayer();
2840 auto flatBufferInputParams = flatBufferLayer->inputParams();
2870 IConnectableLayer* layer = m_Network->AddQuantizedLstmLayer(lstmInputParams, layerName.c_str());
2878 RegisterInputSlots(graph, layerIndex, layer);
2879 RegisterOutputSlots(graph, layerIndex, layer);
2882 void Deserializer::ParseDequantize(
GraphPtr graph,
unsigned int layerIndex)
2892 const std::string layerName =
GetLayerName(graph, layerIndex);
2898 RegisterInputSlots(graph, layerIndex, layer);
2899 RegisterOutputSlots(graph, layerIndex, layer);
2902 void Deserializer::ParseMerge(
GraphPtr graph,
unsigned int layerIndex)
2912 const std::string layerName =
GetLayerName(graph, layerIndex);
2918 RegisterInputSlots(graph, layerIndex, layer);
2919 RegisterOutputSlots(graph, layerIndex, layer);
2922 void Deserializer::ParseSwitch(
GraphPtr graph,
unsigned int layerIndex)
2925 auto inputs =
GetInputs(graph, layerIndex);
2929 auto outputs =
GetOutputs(graph, layerIndex);
2941 RegisterInputSlots(graph, layerIndex, layer);
2942 RegisterOutputSlots(graph, layerIndex, layer);
2945 void Deserializer::ParsePrelu(
GraphPtr graph,
unsigned int layerIndex)
2948 auto inputs =
GetInputs(graph, layerIndex);
2952 auto outputs =
GetOutputs(graph, layerIndex);
2961 RegisterInputSlots(graph, layerIndex, layer);
2962 RegisterOutputSlots(graph, layerIndex, layer);
2965 void Deserializer::ParseTranspose(
GraphPtr graph,
unsigned int layerIndex)
2969 auto dimsMapping = graph->layers()->Get(layerIndex)->layer_as_TransposeLayer()->descriptor()->dimMappings();
2971 auto inputs =
GetInputs(graph, layerIndex);
2974 auto outputs =
GetOutputs(graph, layerIndex);
2981 IConnectableLayer* layer = m_Network->AddTransposeLayer(descriptor, layerName.c_str());
2984 RegisterInputSlots(graph, layerIndex, layer);
2985 RegisterOutputSlots(graph, layerIndex, layer);
2988 void Deserializer::ParseTransposeConvolution2d(
GraphPtr graph,
unsigned int layerIndex)
2992 auto inputs =
GetInputs(graph, layerIndex);
2995 auto outputs =
GetOutputs(graph, layerIndex);
2998 auto serializerLayer = graph->layers()->Get(layerIndex)->layer_as_TransposeConvolution2dLayer();
3000 auto serializerDescriptor = serializerLayer->descriptor();
3003 descriptor.
m_PadLeft = serializerDescriptor->padLeft();
3004 descriptor.
m_PadRight = serializerDescriptor->padRight();
3005 descriptor.
m_PadTop = serializerDescriptor->padTop();
3006 descriptor.
m_PadBottom = serializerDescriptor->padBottom();
3007 descriptor.
m_StrideX = serializerDescriptor->strideX();
3008 descriptor.
m_StrideY = serializerDescriptor->strideY();;
3009 descriptor.
m_BiasEnabled = serializerDescriptor->biasEnabled();;
3018 optionalBiases = armnn::MakeOptional<armnn::ConstTensor>(biases);
3021 IConnectableLayer* layer = m_Network->AddTransposeConvolution2dLayer(descriptor,
3027 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3029 RegisterInputSlots(graph, layerIndex, layer);
3030 RegisterOutputSlots(graph, layerIndex, layer);
3033 void Deserializer::ParseStack(
GraphPtr graph,
unsigned int layerIndex)
3036 auto inputs =
GetInputs(graph, layerIndex);
3038 auto outputs =
GetOutputs(graph, layerIndex);
3041 auto flatBufferDescriptor = graph->layers()->Get(layerIndex)->layer_as_StackLayer()->descriptor();
3042 unsigned int axis = flatBufferDescriptor->axis();
3043 unsigned int numInputs = flatBufferDescriptor->numInputs();
3046 auto flatBufferInputShape = flatBufferDescriptor->inputShape();
3047 std::vector<uint32_t> vectorInputShape(flatBufferInputShape->begin(),
3048 flatBufferInputShape->begin() + flatBufferInputShape->size());
3050 TensorShape inputShape(static_cast<unsigned int>(vectorInputShape.size()), vectorInputShape.data());
3053 for (
unsigned int i=0; i<inputs.size(); ++i)
3056 if (descriptor.m_InputShape != inputShape)
3058 std::stringstream ss;
3059 ss <<
"Shape of input " 3063 <<
" does not equal defined input shape " 3064 << descriptor.m_InputShape
3072 IConnectableLayer* layer = m_Network->AddStackLayer(descriptor, layerName.c_str());
3077 RegisterInputSlots(graph, layerIndex, layer);
3078 RegisterOutputSlots(graph, layerIndex, layer);
3081 void Deserializer::ParseStandIn(
GraphPtr graph,
unsigned int layerIndex)
3085 auto inputs =
GetInputs(graph, layerIndex);
3086 auto outputs =
GetOutputs(graph, layerIndex);
3088 auto fbLayer = graph->layers()->Get(layerIndex)->layer_as_StandInLayer();
3089 auto fbDescriptor = fbLayer->descriptor();
3092 descriptor.
m_NumInputs = fbDescriptor->numInputs();
3098 const std::string layerName =
GetLayerName(graph, layerIndex);
3101 for (
unsigned int i = 0u; i < descriptor.
m_NumOutputs; ++i)
3107 RegisterInputSlots(graph, layerIndex, layer);
3108 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.
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)
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...
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.
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 })