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