ArmNN
 22.05
Serializer.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 #pragma once
6 
8 #include <armnn/IStrategy.hpp>
10 
12 
13 #include <common/include/ProfilingGuid.hpp>
14 
15 #include <unordered_map>
16 
17 #include "ArmnnSchema_generated.h"
18 
19 #include <armnn/Types.hpp>
20 
21 namespace armnnSerializer
22 {
23 
25 {
26 public:
27  void ExecuteStrategy(const armnn::IConnectableLayer* layer,
28  const armnn::BaseDescriptor& descriptor,
29  const std::vector<armnn::ConstTensor>& constants,
30  const char* name,
31  const armnn::LayerBindingId id) override;
32 
33  SerializerStrategy() : m_layerId(0) {}
35 
36  flatbuffers::FlatBufferBuilder& GetFlatBufferBuilder()
37  {
38  return m_flatBufferBuilder;
39  }
40 
41  std::vector<int>& GetInputIds()
42  {
43  return m_inputIds;
44  }
45 
46  std::vector<int>& GetOutputIds()
47  {
48  return m_outputIds;
49  }
50 
51  std::vector<flatbuffers::Offset<armnnSerializer::AnyLayer>>& GetSerializedLayers()
52  {
53  return m_serializedLayers;
54  }
55 
56  flatbuffers::Offset<armnnSerializer::FeatureCompatibilityVersions> GetVersionTable();
57 
58 private:
59  /// Creates the Input Slots and Output Slots and LayerBase for the layer.
60  flatbuffers::Offset<armnnSerializer::LayerBase> CreateLayerBase(
61  const armnn::IConnectableLayer* layer,
62  const armnnSerializer::LayerType layerType);
63 
64  /// Creates the serializer AnyLayer for the layer and adds it to m_serializedLayers.
65  void CreateAnyLayer(const flatbuffers::Offset<void>& layer, const armnnSerializer::Layer serializerLayer);
66 
67  /// Creates the serializer ConstTensor for the armnn ConstTensor.
68  flatbuffers::Offset<armnnSerializer::ConstTensor> CreateConstTensorInfo(
69  const armnn::ConstTensor& constTensor);
70 
71  /// Creates the serializer TensorInfo for the armnn TensorInfo.
72  flatbuffers::Offset<TensorInfo> CreateTensorInfo(const armnn::TensorInfo& tensorInfo);
73 
74  template <typename T>
75  flatbuffers::Offset<flatbuffers::Vector<T>> CreateDataVector(const void* memory, unsigned int size);
76 
77  ///Function which maps Guid to an index
78  uint32_t GetSerializedId(LayerGuid guid);
79 
80  /// Creates the serializer InputSlots for the layer.
81  std::vector<flatbuffers::Offset<armnnSerializer::InputSlot>> CreateInputSlots(
82  const armnn::IConnectableLayer* layer);
83 
84  /// Creates the serializer OutputSlots for the layer.
85  std::vector<flatbuffers::Offset<armnnSerializer::OutputSlot>> CreateOutputSlots(
86  const armnn::IConnectableLayer* layer);
87 
88  /// FlatBufferBuilder to create our layers' FlatBuffers.
89  flatbuffers::FlatBufferBuilder m_flatBufferBuilder;
90 
91  /// AnyLayers required by the SerializedGraph.
92  std::vector<flatbuffers::Offset<armnnSerializer::AnyLayer>> m_serializedLayers;
93 
94  /// Vector of the binding ids of all Input Layers required by the SerializedGraph.
95  std::vector<int> m_inputIds;
96 
97  /// Vector of the binding ids of all Output Layers required by the SerializedGraph.
98  std::vector<int> m_outputIds;
99 
100  /// Mapped Guids of all Layers to match our index.
101  std::unordered_map<LayerGuid, uint32_t > m_guidMap;
102 
103  /// layer within our FlatBuffer index.
104  uint32_t m_layerId;
105 
106 private:
107  void SerializeActivationLayer(const armnn::IConnectableLayer* layer,
108  const armnn::ActivationDescriptor& descriptor,
109  const char* name = nullptr);
110 
111  void SerializeAdditionLayer(const armnn::IConnectableLayer* layer,
112  const char* name = nullptr);
113 
114  void SerializeArgMinMaxLayer(const armnn::IConnectableLayer* layer,
115  const armnn::ArgMinMaxDescriptor& argMinMaxDescriptor,
116  const char* name = nullptr);
117 
118  void SerializeBatchToSpaceNdLayer(const armnn::IConnectableLayer* layer,
119  const armnn::BatchToSpaceNdDescriptor& descriptor,
120  const char* name = nullptr);
121 
122  void SerializeBatchNormalizationLayer(const armnn::IConnectableLayer* layer,
123  const armnn::BatchNormalizationDescriptor& BatchNormalizationDescriptor,
124  const std::vector<armnn::ConstTensor>& constants,
125  const char* name = nullptr);
126 
127  void SerializeCastLayer(const armnn::IConnectableLayer* layer,
128  const char* name = nullptr);
129 
130  void SerializeChannelShuffleLayer(const armnn::IConnectableLayer* layer,
131  const armnn::ChannelShuffleDescriptor& descriptor,
132  const char* name = nullptr);
133 
134  void SerializeComparisonLayer(const armnn::IConnectableLayer* layer,
135  const armnn::ComparisonDescriptor& descriptor,
136  const char* name = nullptr);
137 
138  void SerializeConcatLayer(const armnn::IConnectableLayer* layer,
139  const armnn::ConcatDescriptor& concatDescriptor,
140  const char* name = nullptr);
141 
142  void SerializeConstantLayer(const armnn::IConnectableLayer* layer,
143  const std::vector<armnn::ConstTensor>& constants,
144  const char* name = nullptr);
145 
146  void SerializeConvolution2dLayer(const armnn::IConnectableLayer* layer,
147  const armnn::Convolution2dDescriptor& descriptor,
148  const char* name = nullptr);
149 
150  void SerializeConvolution3dLayer(const armnn::IConnectableLayer* layer,
151  const armnn::Convolution3dDescriptor& descriptor,
152  const char* name = nullptr);
153 
154  void SerializeDepthToSpaceLayer(const armnn::IConnectableLayer* layer,
155  const armnn::DepthToSpaceDescriptor& descriptor,
156  const char* name = nullptr);
157 
158  void SerializeDepthwiseConvolution2dLayer(const armnn::IConnectableLayer* layer,
159  const armnn::DepthwiseConvolution2dDescriptor& descriptor,
160  const char* name = nullptr);
161 
162  void SerializeDequantizeLayer(const armnn::IConnectableLayer* layer,
163  const char* name = nullptr);
164 
165  void SerializeDetectionPostProcessLayer(const armnn::IConnectableLayer* layer,
166  const armnn::DetectionPostProcessDescriptor& descriptor,
167  const std::vector<armnn::ConstTensor>& constants,
168  const char* name = nullptr);
169 
170  void SerializeDivisionLayer(const armnn::IConnectableLayer* layer,
171  const char* name = nullptr);
172 
173  void SerializeElementwiseUnaryLayer(const armnn::IConnectableLayer* layer,
174  const armnn::ElementwiseUnaryDescriptor& descriptor,
175  const char* name = nullptr);
176 
177  void SerializeFillLayer(const armnn::IConnectableLayer* layer,
178  const armnn::FillDescriptor& fillDescriptor,
179  const char* name = nullptr);
180 
181  void SerializeFloorLayer(const armnn::IConnectableLayer *layer,
182  const char *name = nullptr);
183 
184  void SerializeFullyConnectedLayer(const armnn::IConnectableLayer* layer,
185  const armnn::FullyConnectedDescriptor& fullyConnectedDescriptor,
186  const char* name = nullptr);
187 
188  void SerializeGatherLayer(const armnn::IConnectableLayer* layer,
189  const armnn::GatherDescriptor& gatherDescriptor,
190  const char* name = nullptr);
191 
192  void SerializeGatherNdLayer(const armnn::IConnectableLayer* layer,
193  const char* name = nullptr);
194 
195  void SerializeInputLayer(const armnn::IConnectableLayer* layer,
197  const char* name = nullptr);
198 
199  void SerializeInstanceNormalizationLayer(const armnn::IConnectableLayer* layer,
200  const armnn::InstanceNormalizationDescriptor& instanceNormalizationDescriptor,
201  const char* name = nullptr);
202 
203  void SerializeL2NormalizationLayer(const armnn::IConnectableLayer* layer,
204  const armnn::L2NormalizationDescriptor& l2NormalizationDescriptor,
205  const char* name = nullptr);
206 
207  void SerializeLogicalBinaryLayer(const armnn::IConnectableLayer* layer,
208  const armnn::LogicalBinaryDescriptor& descriptor,
209  const char* name = nullptr);
210 
211  void SerializeLogSoftmaxLayer(const armnn::IConnectableLayer* layer,
212  const armnn::LogSoftmaxDescriptor& logSoftmaxDescriptor,
213  const char* name = nullptr);
214 
215  void SerializeLstmLayer(const armnn::IConnectableLayer* layer,
216  const armnn::LstmDescriptor& descriptor,
217  const std::vector<armnn::ConstTensor>& constants,
218  const char* name = nullptr);
219 
220  void SerializeMeanLayer(const armnn::IConnectableLayer* layer,
221  const armnn::MeanDescriptor& descriptor,
222  const char* name);
223 
224  void SerializeMinimumLayer(const armnn::IConnectableLayer* layer,
225  const char* name = nullptr);
226 
227  void SerializeMaximumLayer(const armnn::IConnectableLayer* layer,
228  const char* name = nullptr);
229 
230  void SerializeMergeLayer(const armnn::IConnectableLayer* layer,
231  const char* name = nullptr);
232 
233  void SerializeMultiplicationLayer(const armnn::IConnectableLayer* layer,
234  const char* name = nullptr);
235 
236  void SerializeOutputLayer(const armnn::IConnectableLayer* layer,
238  const char* name = nullptr);
239 
240  void SerializePadLayer(const armnn::IConnectableLayer* layer,
241  const armnn::PadDescriptor& PadDescriptor,
242  const char* name = nullptr);
243 
244  void SerializePermuteLayer(const armnn::IConnectableLayer* layer,
245  const armnn::PermuteDescriptor& PermuteDescriptor,
246  const char* name = nullptr);
247 
248  void SerializePooling2dLayer(const armnn::IConnectableLayer* layer,
249  const armnn::Pooling2dDescriptor& pooling2dDescriptor,
250  const char* name = nullptr);
251 
252  void SerializePooling3dLayer(const armnn::IConnectableLayer* layer,
253  const armnn::Pooling3dDescriptor& pooling3dDescriptor,
254  const char* name = nullptr);
255 
256  void SerializePreluLayer(const armnn::IConnectableLayer* layer,
257  const char* name = nullptr);
258 
259  void SerializeQuantizeLayer(const armnn::IConnectableLayer* layer,
260  const char* name = nullptr);
261 
262  void SerializeQLstmLayer(const armnn::IConnectableLayer* layer,
263  const armnn::QLstmDescriptor& descriptor,
264  const std::vector<armnn::ConstTensor>& constants,
265  const char* name = nullptr);
266 
267  void SerializeQuantizedLstmLayer(const armnn::IConnectableLayer* layer,
268  const std::vector<armnn::ConstTensor>& constants,
269  const char* name = nullptr);
270 
271  void SerializeRankLayer(const armnn::IConnectableLayer* layer,
272  const char* name = nullptr);
273 
274  void SerializeReduceLayer(const armnn::IConnectableLayer* layer,
275  const armnn::ReduceDescriptor& reduceDescriptor,
276  const char* name = nullptr);
277 
278  void SerializeReshapeLayer(const armnn::IConnectableLayer* layer,
279  const armnn::ReshapeDescriptor& reshapeDescriptor,
280  const char* name = nullptr);
281 
282  void SerializeResizeLayer(const armnn::IConnectableLayer* layer,
283  const armnn::ResizeDescriptor& resizeDescriptor,
284  const char* name = nullptr);
285 
286  void SerializeSliceLayer(const armnn::IConnectableLayer* layer,
287  const armnn::SliceDescriptor& sliceDescriptor,
288  const char* name = nullptr);
289 
290  void SerializeSoftmaxLayer(const armnn::IConnectableLayer* layer,
291  const armnn::SoftmaxDescriptor& softmaxDescriptor,
292  const char* name = nullptr);
293 
294  void SerializeSpaceToBatchNdLayer(const armnn::IConnectableLayer* layer,
295  const armnn::SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
296  const char* name = nullptr);
297 
298  void SerializeSpaceToDepthLayer(const armnn::IConnectableLayer* layer,
299  const armnn::SpaceToDepthDescriptor& spaceToDepthDescriptor,
300  const char* name = nullptr);
301 
302  void SerializeNormalizationLayer(const armnn::IConnectableLayer* layer,
303  const armnn::NormalizationDescriptor& normalizationDescriptor,
304  const char* name = nullptr);
305 
306  void SerializeShapeLayer(const armnn::IConnectableLayer* layer,
307  const char* name = nullptr);
308 
309  void SerializeSplitterLayer(const armnn::IConnectableLayer* layer,
310  const armnn::ViewsDescriptor& viewsDescriptor,
311  const char* name = nullptr);
312 
313  void SerializeStandInLayer(const armnn::IConnectableLayer* layer,
314  const armnn::StandInDescriptor& standInDescriptor,
315  const char* name = nullptr);
316 
317  void SerializeStackLayer(const armnn::IConnectableLayer* layer,
318  const armnn::StackDescriptor& stackDescriptor,
319  const char* name = nullptr);
320 
321  void SerializeStridedSliceLayer(const armnn::IConnectableLayer* layer,
322  const armnn::StridedSliceDescriptor& stridedSliceDescriptor,
323  const char* name = nullptr);
324 
325  void SerializeSubtractionLayer(const armnn::IConnectableLayer* layer,
326  const char* name = nullptr);
327 
328  void SerializeSwitchLayer(const armnn::IConnectableLayer* layer,
329  const char* name = nullptr);
330 
331  void SerializeTransposeConvolution2dLayer(const armnn::IConnectableLayer* layer,
332  const armnn::TransposeConvolution2dDescriptor& descriptor,
333  const std::vector<armnn::ConstTensor>& constants,
334  const char* = nullptr);
335 
336  void SerializeTransposeLayer(const armnn::IConnectableLayer* layer,
337  const armnn::TransposeDescriptor& descriptor,
338  const char* name = nullptr);
339 
340  void SerializeUnidirectionalSequenceLstmLayer(const armnn::IConnectableLayer* layer,
342  const std::vector<armnn::ConstTensor>& constants,
343  const char* name = nullptr);
344 };
345 
346 
347 
349 {
350 public:
351  SerializerImpl() = default;
352  ~SerializerImpl() = default;
353 
354  /// Serializes the network to ArmNN SerializedGraph.
355  /// @param [in] inNetwork The network to be serialized.
356  void Serialize(const armnn::INetwork& inNetwork);
357 
358  /// Serializes the SerializedGraph to the stream.
359  /// @param [stream] the stream to save to
360  /// @return true if graph is Serialized to the Stream, false otherwise
361  bool SaveSerializedToStream(std::ostream& stream);
362 
363 private:
364 
365  /// Visitor to contruct serialized network
366  SerializerStrategy m_SerializerStrategy;
367 };
368 
369 } //namespace armnnSerializer
A ViewsDescriptor for the SplitterLayer.
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:66
flatbuffers::FlatBufferBuilder & GetFlatBufferBuilder()
Definition: Serializer.hpp:36
A TransposeConvolution2dDescriptor for the TransposeConvolution2dLayer.
A ReshapeDescriptor for the ReshapeLayer.
A ComparisonDescriptor for the ComparisonLayer.
Definition: Descriptors.hpp:89
A Convolution2dDescriptor for the Convolution2dLayer.
Main network class which provides the interface for building up a neural network. ...
Definition: INetwork.hpp:249
A LogicalBinaryDescriptor for the LogicalBinaryLayer.
A SpaceToDepthDescriptor for the SpaceToDepthLayer.
A BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.
void ExecuteStrategy(const armnn::IConnectableLayer *layer, const armnn::BaseDescriptor &descriptor, const std::vector< armnn::ConstTensor > &constants, const char *name, const armnn::LayerBindingId id) override
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:290
A ResizeBilinearDescriptor for the ResizeBilinearLayer.
Base class for all descriptors.
Definition: Descriptors.hpp:22
A StackDescriptor for the StackLayer.
A PadDescriptor for the PadLayer.
An LstmDescriptor for the LstmLayer.
A L2NormalizationDescriptor for the L2NormalizationLayer.
An ArgMinMaxDescriptor for ArgMinMaxLayer.
Definition: Descriptors.hpp:67
An OriginsDescriptor for the ConcatLayer.
A ReduceDescriptor for the REDUCE operators.
A FullyConnectedDescriptor for the FullyConnectedLayer.
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
A GatherDescriptor for the GatherLayer.
A StandInDescriptor for the StandIn layer.
A QLstmDescriptor for the QLstmLayer.
std::vector< int > & GetInputIds()
Definition: Serializer.hpp:41
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:36
A SliceDescriptor for the SliceLayer.
A Convolution3dDescriptor for the Convolution3dLayer.
A Pooling3dDescriptor for the Pooling3dLayer.
A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
arm::pipe::ProfilingGuid LayerGuid
Define LayerGuid type.
Definition: Types.hpp:26
std::vector< int > & GetOutputIds()
Definition: Serializer.hpp:46
A ElementwiseUnaryDescriptor for the ElementwiseUnaryLayer.
A MeanDescriptor for the MeanLayer.
A TransposeDescriptor for the TransposeLayer.
A StridedSliceDescriptor for the StridedSliceLayer.
A Pooling2dDescriptor for the Pooling2dLayer.
A NormalizationDescriptor for the NormalizationLayer.
std::vector< flatbuffers::Offset< armnnSerializer::AnyLayer > > & GetSerializedLayers()
Definition: Serializer.hpp:51
flatbuffers::Offset< armnnSerializer::FeatureCompatibilityVersions > GetVersionTable()
An InstanceNormalizationDescriptor for InstanceNormalizationLayer.
A ChannelShuffleDescriptor for the ChannelShuffle operator.
A SoftmaxDescriptor for the SoftmaxLayer.
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
A FillDescriptor for the FillLayer.
A BatchNormalizationDescriptor for the BatchNormalizationLayer.
A PermuteDescriptor for the PermuteLayer.