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