ArmNN
 21.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 <unordered_map>
14 
15 #include "ArmnnSchema_generated.h"
16 
17 #include <armnn/Types.hpp>
18 
19 namespace armnnSerializer
20 {
21 
23 {
24 public:
25  void ExecuteStrategy(const armnn::IConnectableLayer* layer,
26  const armnn::BaseDescriptor& descriptor,
27  const std::vector<armnn::ConstTensor>& constants,
28  const char* name,
29  const armnn::LayerBindingId id) override;
30 
31  SerializerStrategy() : m_layerId(0) {}
33 
34  flatbuffers::FlatBufferBuilder& GetFlatBufferBuilder()
35  {
36  return m_flatBufferBuilder;
37  }
38 
39  std::vector<int>& GetInputIds()
40  {
41  return m_inputIds;
42  }
43 
44  std::vector<int>& GetOutputIds()
45  {
46  return m_outputIds;
47  }
48 
49  std::vector<flatbuffers::Offset<armnnSerializer::AnyLayer>>& GetSerializedLayers()
50  {
51  return m_serializedLayers;
52  }
53 
54  flatbuffers::Offset<armnnSerializer::FeatureCompatibilityVersions> GetVersionTable();
55 
56 private:
57  /// Creates the Input Slots and Output Slots and LayerBase for the layer.
58  flatbuffers::Offset<armnnSerializer::LayerBase> CreateLayerBase(
59  const armnn::IConnectableLayer* layer,
60  const armnnSerializer::LayerType layerType);
61 
62  /// Creates the serializer AnyLayer for the layer and adds it to m_serializedLayers.
63  void CreateAnyLayer(const flatbuffers::Offset<void>& layer, const armnnSerializer::Layer serializerLayer);
64 
65  /// Creates the serializer ConstTensor for the armnn ConstTensor.
66  flatbuffers::Offset<armnnSerializer::ConstTensor> CreateConstTensorInfo(
67  const armnn::ConstTensor& constTensor);
68 
69  /// Creates the serializer TensorInfo for the armnn TensorInfo.
70  flatbuffers::Offset<TensorInfo> CreateTensorInfo(const armnn::TensorInfo& tensorInfo);
71 
72  template <typename T>
73  flatbuffers::Offset<flatbuffers::Vector<T>> CreateDataVector(const void* memory, unsigned int size);
74 
75  ///Function which maps Guid to an index
76  uint32_t GetSerializedId(armnn::LayerGuid guid);
77 
78  /// Creates the serializer InputSlots for the layer.
79  std::vector<flatbuffers::Offset<armnnSerializer::InputSlot>> CreateInputSlots(
80  const armnn::IConnectableLayer* layer);
81 
82  /// Creates the serializer OutputSlots for the layer.
83  std::vector<flatbuffers::Offset<armnnSerializer::OutputSlot>> CreateOutputSlots(
84  const armnn::IConnectableLayer* layer);
85 
86  /// FlatBufferBuilder to create our layers' FlatBuffers.
87  flatbuffers::FlatBufferBuilder m_flatBufferBuilder;
88 
89  /// AnyLayers required by the SerializedGraph.
90  std::vector<flatbuffers::Offset<armnnSerializer::AnyLayer>> m_serializedLayers;
91 
92  /// Vector of the binding ids of all Input Layers required by the SerializedGraph.
93  std::vector<int> m_inputIds;
94 
95  /// Vector of the binding ids of all Output Layers required by the SerializedGraph.
96  std::vector<int> m_outputIds;
97 
98  /// Mapped Guids of all Layers to match our index.
99  std::unordered_map<armnn::LayerGuid, uint32_t > m_guidMap;
100 
101  /// layer within our FlatBuffer index.
102  uint32_t m_layerId;
103 
104 private:
105  ARMNN_DEPRECATED_MSG("Use VisitElementwiseUnaryLayer instead")
106  void SerializeAbsLayer(const armnn::IConnectableLayer* layer,
107  const char* name = nullptr);
108 
109  void SerializeActivationLayer(const armnn::IConnectableLayer* layer,
110  const armnn::ActivationDescriptor& descriptor,
111  const char* name = nullptr);
112 
113  void SerializeAdditionLayer(const armnn::IConnectableLayer* layer,
114  const char* name = nullptr);
115 
116  void SerializeArgMinMaxLayer(const armnn::IConnectableLayer* layer,
117  const armnn::ArgMinMaxDescriptor& argMinMaxDescriptor,
118  const char* name = nullptr);
119 
120  void SerializeBatchToSpaceNdLayer(const armnn::IConnectableLayer* layer,
121  const armnn::BatchToSpaceNdDescriptor& descriptor,
122  const char* name = nullptr);
123 
124  void SerializeBatchNormalizationLayer(const armnn::IConnectableLayer* layer,
125  const armnn::BatchNormalizationDescriptor& BatchNormalizationDescriptor,
126  const std::vector<armnn::ConstTensor>& constants,
127  const char* name = nullptr);
128 
129  void SerializeCastLayer(const armnn::IConnectableLayer* layer,
130  const char* name = nullptr);
131 
132  void SerializeComparisonLayer(const armnn::IConnectableLayer* layer,
133  const armnn::ComparisonDescriptor& descriptor,
134  const char* name = nullptr);
135 
136  void SerializeConcatLayer(const armnn::IConnectableLayer* layer,
137  const armnn::ConcatDescriptor& concatDescriptor,
138  const char* name = nullptr);
139 
140  void SerializeConstantLayer(const armnn::IConnectableLayer* layer,
141  const std::vector<armnn::ConstTensor>& contants,
142  const char* name = nullptr);
143 
144  void SerializeConvolution2dLayer(const armnn::IConnectableLayer* layer,
145  const armnn::Convolution2dDescriptor& descriptor,
146  const std::vector<armnn::ConstTensor>& contants,
147  const char* name = nullptr);
148 
149  void SerializeDepthToSpaceLayer(const armnn::IConnectableLayer* layer,
150  const armnn::DepthToSpaceDescriptor& descriptor,
151  const char* name = nullptr);
152 
153  void SerializeDepthwiseConvolution2dLayer(const armnn::IConnectableLayer* layer,
154  const armnn::DepthwiseConvolution2dDescriptor& descriptor,
155  const std::vector<armnn::ConstTensor>& constants,
156  const char* name = nullptr);
157 
158  void SerializeDequantizeLayer(const armnn::IConnectableLayer* layer,
159  const char* name = nullptr);
160 
161  void SerializeDetectionPostProcessLayer(const armnn::IConnectableLayer* layer,
162  const armnn::DetectionPostProcessDescriptor& descriptor,
163  const std::vector<armnn::ConstTensor>& constants,
164  const char* name = nullptr);
165 
166  void SerializeDivisionLayer(const armnn::IConnectableLayer* layer,
167  const char* name = nullptr);
168 
169  void SerializeElementwiseUnaryLayer(const armnn::IConnectableLayer* layer,
170  const armnn::ElementwiseUnaryDescriptor& descriptor,
171  const char* name = nullptr);
172 
173  ARMNN_DEPRECATED_MSG("Use VisitComparisonLayer instead")
174  void SerializeEqualLayer(const armnn::IConnectableLayer* layer, const char* name);
175 
176  void SerializeFillLayer(const armnn::IConnectableLayer* layer,
177  const armnn::FillDescriptor& fillDescriptor,
178  const char* name = nullptr);
179 
180  void SerializeFloorLayer(const armnn::IConnectableLayer *layer,
181  const char *name = nullptr);
182 
183  void SerializeFullyConnectedLayer(const armnn::IConnectableLayer* layer,
184  const armnn::FullyConnectedDescriptor& fullyConnectedDescriptor,
185  const std::vector<armnn::ConstTensor>& constants,
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  ARMNN_DEPRECATED_MSG("Use VisitComparisonLayer instead")
193  void SerializeGreaterLayer(const armnn::IConnectableLayer* layer, 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  ARMNN_DEPRECATED_MSG("Use VisitConcatLayer instead")
234  void SerializeMergerLayer(const armnn::IConnectableLayer* layer,
235  const armnn::MergerDescriptor& mergerDescriptor,
236  const char* name = nullptr);
237 
238  void SerializeMultiplicationLayer(const armnn::IConnectableLayer* layer,
239  const char* name = nullptr);
240 
241  void SerializeOutputLayer(const armnn::IConnectableLayer* layer,
243  const char* name = nullptr);
244 
245  void SerializePadLayer(const armnn::IConnectableLayer* layer,
246  const armnn::PadDescriptor& PadDescriptor,
247  const char* name = nullptr);
248 
249  void SerializePermuteLayer(const armnn::IConnectableLayer* layer,
250  const armnn::PermuteDescriptor& PermuteDescriptor,
251  const char* name = nullptr);
252 
253  void SerializePooling2dLayer(const armnn::IConnectableLayer* layer,
254  const armnn::Pooling2dDescriptor& pooling2dDescriptor,
255  const char* name = nullptr);
256 
257  void SerializePreluLayer(const armnn::IConnectableLayer* layer,
258  const char* name = nullptr);
259 
260  void SerializeQuantizeLayer(const armnn::IConnectableLayer* layer,
261  const char* name = nullptr);
262 
263  void SerializeQLstmLayer(const armnn::IConnectableLayer* layer,
264  const armnn::QLstmDescriptor& descriptor,
265  const std::vector<armnn::ConstTensor>& constants,
266  const char* name = nullptr);
267 
268  void SerializeQuantizedLstmLayer(const armnn::IConnectableLayer* layer,
269  const std::vector<armnn::ConstTensor>& constants,
270  const char* name = nullptr);
271 
272  void SerializeRankLayer(const armnn::IConnectableLayer* layer,
273  const char* name = nullptr);
274 
275  void SerializeReduceLayer(const armnn::IConnectableLayer* layer,
276  const armnn::ReduceDescriptor& reduceDescriptor,
277  const char* name = nullptr);
278 
279  void SerializeReshapeLayer(const armnn::IConnectableLayer* layer,
280  const armnn::ReshapeDescriptor& reshapeDescriptor,
281  const char* name = nullptr);
282 
283  void SerializeResizeLayer(const armnn::IConnectableLayer* layer,
284  const armnn::ResizeDescriptor& resizeDescriptor,
285  const char* name = nullptr);
286 
287  ARMNN_DEPRECATED_MSG("Use VisitResizeLayer instead")
288  void SerializeResizeBilinearLayer(const armnn::IConnectableLayer* layer,
289  const armnn::ResizeBilinearDescriptor& resizeDescriptor,
290  const char* name = nullptr);
291 
292  ARMNN_DEPRECATED_MSG("Use VisitElementwiseUnaryLayer instead")
293  void SerializeRsqrtLayer(const armnn::IConnectableLayer* layer,
294  const char* name = nullptr);
295 
296  void SerializeSliceLayer(const armnn::IConnectableLayer* layer,
297  const armnn::SliceDescriptor& sliceDescriptor,
298  const char* name = nullptr);
299 
300  void SerializeSoftmaxLayer(const armnn::IConnectableLayer* layer,
301  const armnn::SoftmaxDescriptor& softmaxDescriptor,
302  const char* name = nullptr);
303 
304  void SerializeSpaceToBatchNdLayer(const armnn::IConnectableLayer* layer,
305  const armnn::SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
306  const char* name = nullptr);
307 
308  void SerializeSpaceToDepthLayer(const armnn::IConnectableLayer* layer,
309  const armnn::SpaceToDepthDescriptor& spaceToDepthDescriptor,
310  const char* name = nullptr);
311 
312  void SerializeNormalizationLayer(const armnn::IConnectableLayer* layer,
313  const armnn::NormalizationDescriptor& normalizationDescriptor,
314  const char* name = nullptr);
315 
316  void SerializeSplitterLayer(const armnn::IConnectableLayer* layer,
317  const armnn::ViewsDescriptor& viewsDescriptor,
318  const char* name = nullptr);
319 
320  void SerializeStandInLayer(const armnn::IConnectableLayer* layer,
321  const armnn::StandInDescriptor& standInDescriptor,
322  const char* name = nullptr);
323 
324  void SerializeStackLayer(const armnn::IConnectableLayer* layer,
325  const armnn::StackDescriptor& stackDescriptor,
326  const char* name = nullptr);
327 
328  void SerializeStridedSliceLayer(const armnn::IConnectableLayer* layer,
329  const armnn::StridedSliceDescriptor& stridedSliceDescriptor,
330  const char* name = nullptr);
331 
332  void SerializeSubtractionLayer(const armnn::IConnectableLayer* layer,
333  const char* name = nullptr);
334 
335  void SerializeSwitchLayer(const armnn::IConnectableLayer* layer,
336  const char* name = nullptr);
337 
338  void SerializeTransposeConvolution2dLayer(const armnn::IConnectableLayer* layer,
339  const armnn::TransposeConvolution2dDescriptor& descriptor,
340  const std::vector<armnn::ConstTensor>& constants,
341  const char* = nullptr);
342 
343  void SerializeTransposeLayer(const armnn::IConnectableLayer* layer,
344  const armnn::TransposeDescriptor& descriptor,
345  const char* name = nullptr);
346 };
347 
348 
349 
351 {
352 public:
353  SerializerImpl() = default;
354  ~SerializerImpl() = default;
355 
356  /// Serializes the network to ArmNN SerializedGraph.
357  /// @param [in] inNetwork The network to be serialized.
358  void Serialize(const armnn::INetwork& inNetwork);
359 
360  /// Serializes the SerializedGraph to the stream.
361  /// @param [stream] the stream to save to
362  /// @return true if graph is Serialized to the Stream, false otherwise
363  bool SaveSerializedToStream(std::ostream& stream);
364 
365 private:
366 
367  /// Visitor to contruct serialized network
368  SerializerStrategy m_SerializerStrategy;
369 };
370 
371 } //namespace armnnSerializer
A ViewsDescriptor for the SplitterLayer.
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:62
flatbuffers::FlatBufferBuilder & GetFlatBufferBuilder()
Definition: Serializer.hpp:34
A TransposeConvolution2dDescriptor for the TransposeConvolution2dLayer.
A ReshapeDescriptor for the ReshapeLayer.
A ComparisonDescriptor for the ComparisonLayer.
Definition: Descriptors.hpp:78
A Convolution2dDescriptor for the Convolution2dLayer.
Main network class which provides the interface for building up a neural network. ...
Definition: INetwork.hpp:178
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:243
A ResizeDescriptor for the ResizeLayer.
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:56
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:314
A GatherDescriptor for the GatherLayer.
A StandInDescriptor for the StandIn layer.
A QLstmDescriptor for the QLstmLayer.
std::vector< int > & GetInputIds()
Definition: Serializer.hpp:39
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:25
A SliceDescriptor for the SliceLayer.
A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
std::vector< int > & GetOutputIds()
Definition: Serializer.hpp:44
A ElementwiseUnaryDescriptor for the ElementwiseUnaryLayer.
Definition: Descriptors.hpp:98
A MeanDescriptor for the MeanLayer.
A TransposeDescriptor for the TransposeLayer.
A StridedSliceDescriptor for the StridedSliceLayer.
#define ARMNN_DEPRECATED_MSG(message)
Definition: Deprecated.hpp:43
A Pooling2dDescriptor for the Pooling2dLayer.
A NormalizationDescriptor for the NormalizationLayer.
std::vector< flatbuffers::Offset< armnnSerializer::AnyLayer > > & GetSerializedLayers()
Definition: Serializer.hpp:49
flatbuffers::Offset< armnnSerializer::FeatureCompatibilityVersions > GetVersionTable()
An InstanceNormalizationDescriptor for InstanceNormalizationLayer.
A ResizeBilinearDescriptor for the ResizeBilinearLayer.
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.