ArmNN
 20.02
Deserializer.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "armnn/INetwork.hpp"
10 #include <ArmnnSchema_generated.h>
11 
12 #include <unordered_map>
13 
14 namespace armnnDeserializer
15 {
17 {
18 public:
19  // Shorthands for deserializer types
20  using ConstTensorRawPtr = const armnnSerializer::ConstTensor *;
21  using GraphPtr = const armnnSerializer::SerializedGraph *;
22  using TensorRawPtr = const armnnSerializer::TensorInfo *;
23  using PoolingDescriptor = const armnnSerializer::Pooling2dDescriptor *;
24  using NormalizationDescriptorPtr = const armnnSerializer::NormalizationDescriptor *;
25  using LstmDescriptorPtr = const armnnSerializer::LstmDescriptor *;
26  using LstmInputParamsPtr = const armnnSerializer::LstmInputParams *;
27  using QunatizedLstmInputParamsPtr = const armnnSerializer::QuantizedLstmInputParams *;
28  using TensorRawPtrVector = std::vector<TensorRawPtr>;
29  using LayerRawPtr = const armnnSerializer::LayerBase *;
30  using LayerBaseRawPtr = const armnnSerializer::LayerBase *;
31  using LayerBaseRawPtrVector = std::vector<LayerBaseRawPtr>;
32 
33 public:
34 
35  /// Create an input network from binary file contents
36  armnn::INetworkPtr CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent) override;
37 
38  /// Create an input network from a binary input stream
39  armnn::INetworkPtr CreateNetworkFromBinary(std::istream& binaryContent) override;
40 
41  /// Retrieve binding info (layer id and tensor info) for the network input identified by the given layer name
42  BindingPointInfo GetNetworkInputBindingInfo(unsigned int layerId, const std::string& name) const override;
43 
44  /// Retrieve binding info (layer id and tensor info) for the network output identified by the given layer name
45  BindingPointInfo GetNetworkOutputBindingInfo(unsigned int layerId, const std::string& name) const override;
46 
47  Deserializer();
49 
50 public:
51  // testable helpers
52  static GraphPtr LoadGraphFromBinary(const uint8_t* binaryContent, size_t len);
53  static TensorRawPtrVector GetInputs(const GraphPtr& graph, unsigned int layerIndex);
54  static TensorRawPtrVector GetOutputs(const GraphPtr& graph, unsigned int layerIndex);
55  static LayerBaseRawPtr GetBaseLayer(const GraphPtr& graphPtr, unsigned int layerIndex);
56  static int32_t GetBindingLayerInfo(const GraphPtr& graphPtr, unsigned int layerIndex);
57  static std::string GetLayerName(const GraphPtr& graph, unsigned int index);
59  unsigned int layerIndex);
61  NormalizationDescriptorPtr normalizationDescriptor, unsigned int layerIndex);
64  LstmInputParamsPtr lstmInputParams);
65  static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo,
66  const std::vector<uint32_t> & targetDimsIn);
67 
68 private:
69  // No copying allowed until it is wanted and properly implemented
70  Deserializer(const Deserializer&) = delete;
71  Deserializer& operator=(const Deserializer&) = delete;
72 
73  /// Create the network from an already loaded flatbuffers graph
74  armnn::INetworkPtr CreateNetworkFromGraph(GraphPtr graph);
75 
76  // signature for the parser functions
77  using LayerParsingFunction = void(Deserializer::*)(GraphPtr graph, unsigned int layerIndex);
78 
79  void ParseUnsupportedLayer(GraphPtr graph, unsigned int layerIndex);
80  void ParseAbs(GraphPtr graph, unsigned int layerIndex);
81  void ParseActivation(GraphPtr graph, unsigned int layerIndex);
82  void ParseAdd(GraphPtr graph, unsigned int layerIndex);
83  void ParseArgMinMax(GraphPtr graph, unsigned int layerIndex);
84  void ParseBatchToSpaceNd(GraphPtr graph, unsigned int layerIndex);
85  void ParseBatchNormalization(GraphPtr graph, unsigned int layerIndex);
86  void ParseComparison(GraphPtr graph, unsigned int layerIndex);
87  void ParseConcat(GraphPtr graph, unsigned int layerIndex);
88  void ParseConstant(GraphPtr graph, unsigned int layerIndex);
89  void ParseConvolution2d(GraphPtr graph, unsigned int layerIndex);
90  void ParseDepthToSpace(GraphPtr graph, unsigned int layerIndex);
91  void ParseDepthwiseConvolution2d(GraphPtr graph, unsigned int layerIndex);
92  void ParseDequantize(GraphPtr graph, unsigned int layerIndex);
93  void ParseDetectionPostProcess(GraphPtr graph, unsigned int layerIndex);
94  void ParseDivision(GraphPtr graph, unsigned int layerIndex);
95  void ParseElementwiseUnary(GraphPtr graph, unsigned int layerIndex);
96  void ParseEqual(GraphPtr graph, unsigned int layerIndex);
97  void ParseFloor(GraphPtr graph, unsigned int layerIndex);
98  void ParseFullyConnected(GraphPtr graph, unsigned int layerIndex);
99  void ParseGather(GraphPtr graph, unsigned int layerIndex);
100  void ParseGreater(GraphPtr graph, unsigned int layerIndex);
101  void ParseInstanceNormalization(GraphPtr graph, unsigned int layerIndex);
102  void ParseL2Normalization(GraphPtr graph, unsigned int layerIndex);
103  void ParseLogSoftmax(GraphPtr graph, unsigned int layerIndex);
104  void ParseMaximum(GraphPtr graph, unsigned int layerIndex);
105  void ParseMean(GraphPtr graph, unsigned int layerIndex);
106  void ParseMinimum(GraphPtr graph, unsigned int layerIndex);
107  void ParseMerge(GraphPtr graph, unsigned int layerIndex);
108  void ParseMultiplication(GraphPtr graph, unsigned int layerIndex);
109  void ParseNormalization(GraphPtr graph, unsigned int layerIndex);
110  void ParseLstm(GraphPtr graph, unsigned int layerIndex);
111  void ParseQuantizedLstm(GraphPtr graph, unsigned int layerIndex);
112  void ParsePad(GraphPtr graph, unsigned int layerIndex);
113  void ParsePermute(GraphPtr graph, unsigned int layerIndex);
114  void ParsePooling2d(GraphPtr graph, unsigned int layerIndex);
115  void ParsePrelu(GraphPtr graph, unsigned int layerIndex);
116  void ParseQuantize(GraphPtr graph, unsigned int layerIndex);
117  void ParseReshape(GraphPtr graph, unsigned int layerIndex);
118  void ParseResize(GraphPtr graph, unsigned int layerIndex);
119  void ParseResizeBilinear(GraphPtr graph, unsigned int layerIndex);
120  void ParseRsqrt(GraphPtr graph, unsigned int layerIndex);
121  void ParseSlice(GraphPtr graph, unsigned int layerIndex);
122  void ParseSoftmax(GraphPtr graph, unsigned int layerIndex);
123  void ParseSpaceToBatchNd(GraphPtr graph, unsigned int layerIndex);
124  void ParseSpaceToDepth(GraphPtr graph, unsigned int layerIndex);
125  void ParseSplitter(GraphPtr graph, unsigned int layerIndex);
126  void ParseStack(GraphPtr graph, unsigned int layerIndex);
127  void ParseStandIn(GraphPtr graph, unsigned int layerIndex);
128  void ParseStridedSlice(GraphPtr graph, unsigned int layerIndex);
129  void ParseSubtraction(GraphPtr graph, unsigned int layerIndex);
130  void ParseSwitch(GraphPtr graph, unsigned int layerIndex);
131  void ParseTranspose(GraphPtr graph, unsigned int layerIndex);
132  void ParseTransposeConvolution2d(GraphPtr graph, unsigned int layerIndex);
133 
134  void RegisterInputSlots(GraphPtr graph, uint32_t layerIndex,
135  armnn::IConnectableLayer* layer);
136  void RegisterOutputSlots(GraphPtr graph, uint32_t layerIndex,
137  armnn::IConnectableLayer* layer);
138 
139  // NOTE index here must be from flatbuffer object index property
140  void RegisterOutputSlotOfConnection(uint32_t sourceLayerIndex, uint32_t outputSlotIndex, armnn::IOutputSlot* slot);
141  void RegisterInputSlotOfConnection(uint32_t sourceLayerIndex, uint32_t outputSlotIndex, armnn::IInputSlot* slot);
142 
143  void ResetParser();
144 
145  void SetupInputLayers(GraphPtr graphPtr);
146  void SetupOutputLayers(GraphPtr graphPtr);
147 
148  /// Helper to get the index of the layer in the flatbuffer vector from its bindingId property
149  unsigned int GetInputLayerInVector(GraphPtr graph, int targetId);
150  unsigned int GetOutputLayerInVector(GraphPtr graph, int targetId);
151 
152  /// Helper to get the index of the layer in the flatbuffer vector from its index property
153  unsigned int GetLayerIndexInVector(GraphPtr graph, unsigned int index);
154 
155  struct FeatureVersions
156  {
157  // Default values to zero for backward compatibility
158  unsigned int m_BindingIdScheme = 0;
159  };
160 
161  FeatureVersions GetFeatureVersions(GraphPtr graph);
162 
163  /// The network we're building. Gets cleared after it is passed to the user
164  armnn::INetworkPtr m_Network;
165  std::vector<LayerParsingFunction> m_ParserFunctions;
166 
167  using NameToBindingInfo = std::pair<std::string, BindingPointInfo >;
168  std::vector<NameToBindingInfo> m_InputBindings;
169  std::vector<NameToBindingInfo> m_OutputBindings;
170 
171  /// This struct describe connections for each layer
172  struct Connections
173  {
174  // Maps output slot index (property in flatbuffer object) to IOutputSlot pointer
175  std::unordered_map<unsigned int, armnn::IOutputSlot*> outputSlots;
176 
177  // Maps output slot index to IInputSlot pointer the output slot should be connected to
178  std::unordered_map<unsigned int, std::vector<armnn::IInputSlot*>> inputSlots;
179  };
180 
181  /// Maps layer index (index property in flatbuffer object) to Connections for each layer
182  std::unordered_map<unsigned int, Connections> m_GraphConnections;
183 };
184 
185 } // namespace armnnDeserializer
const armnnSerializer::LstmInputParams * LstmInputParamsPtr
static armnn::LstmDescriptor GetLstmDescriptor(LstmDescriptorPtr lstmDescriptor)
const armnnSerializer::QuantizedLstmInputParams * QunatizedLstmInputParamsPtr
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:61
std::vector< LayerBaseRawPtr > LayerBaseRawPtrVector
static GraphPtr LoadGraphFromBinary(const uint8_t *binaryContent, size_t len)
static armnn::NormalizationDescriptor GetNormalizationDescriptor(NormalizationDescriptorPtr normalizationDescriptor, unsigned int layerIndex)
const armnnSerializer::TensorInfo * TensorRawPtr
const armnnSerializer::NormalizationDescriptor * NormalizationDescriptorPtr
armnn::INetworkPtr CreateNetworkFromBinary(const std::vector< uint8_t > &binaryContent) override
Create an input network from binary file contents.
static std::string GetLayerName(const GraphPtr &graph, unsigned int index)
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 ...
static int32_t GetBindingLayerInfo(const GraphPtr &graphPtr, unsigned int layerIndex)
const armnnSerializer::Pooling2dDescriptor * PoolingDescriptor
An LstmDescriptor for the LstmLayer.
An output connection slot for a layer.
Definition: INetwork.hpp:37
static LayerBaseRawPtr GetBaseLayer(const GraphPtr &graphPtr, unsigned int layerIndex)
const armnnSerializer::LayerBase * LayerRawPtr
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< TensorRawPtr > TensorRawPtrVector
static TensorRawPtrVector GetOutputs(const GraphPtr &graph, unsigned int layerIndex)
const armnnSerializer::SerializedGraph * GraphPtr
const armnnSerializer::ConstTensor * ConstTensorRawPtr
const armnnSerializer::LstmDescriptor * LstmDescriptorPtr
static armnn::Pooling2dDescriptor GetPoolingDescriptor(PoolingDescriptor pooling2dDescriptor, unsigned int layerIndex)
static armnn::LstmInputParams GetLstmInputParams(LstmDescriptorPtr lstmDescriptor, LstmInputParamsPtr lstmInputParams)
static TensorRawPtrVector GetInputs(const GraphPtr &graph, unsigned int layerIndex)
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:101
A Pooling2dDescriptor for the Pooling2dLayer.
const armnnSerializer::LayerBase * LayerBaseRawPtr
A NormalizationDescriptor for the NormalizationLayer.
static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo &inputTensorInfo, const std::vector< uint32_t > &targetDimsIn)
An input connection slot for a layer.
Definition: INetwork.hpp:24