ArmNN
 21.02
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 SerializeComparisonLayer(const armnn::IConnectableLayer* layer,
130  const armnn::ComparisonDescriptor& descriptor,
131  const char* name = nullptr);
132 
133  void SerializeConcatLayer(const armnn::IConnectableLayer* layer,
134  const armnn::ConcatDescriptor& concatDescriptor,
135  const char* name = nullptr);
136 
137  void SerializeConstantLayer(const armnn::IConnectableLayer* layer,
138  const std::vector<armnn::ConstTensor>& contants,
139  const char* name = nullptr);
140 
141  void SerializeConvolution2dLayer(const armnn::IConnectableLayer* layer,
142  const armnn::Convolution2dDescriptor& descriptor,
143  const std::vector<armnn::ConstTensor>& contants,
144  const char* name = nullptr);
145 
146  void SerializeDepthToSpaceLayer(const armnn::IConnectableLayer* layer,
147  const armnn::DepthToSpaceDescriptor& descriptor,
148  const char* name = nullptr);
149 
150  void SerializeDepthwiseConvolution2dLayer(const armnn::IConnectableLayer* layer,
151  const armnn::DepthwiseConvolution2dDescriptor& descriptor,
152  const std::vector<armnn::ConstTensor>& constants,
153  const char* name = nullptr);
154 
155  void SerializeDequantizeLayer(const armnn::IConnectableLayer* layer,
156  const char* name = nullptr);
157 
158  void SerializeDetectionPostProcessLayer(const armnn::IConnectableLayer* layer,
159  const armnn::DetectionPostProcessDescriptor& descriptor,
160  const std::vector<armnn::ConstTensor>& constants,
161  const char* name = nullptr);
162 
163  void SerializeDivisionLayer(const armnn::IConnectableLayer* layer,
164  const char* name = nullptr);
165 
166  void SerializeElementwiseUnaryLayer(const armnn::IConnectableLayer* layer,
167  const armnn::ElementwiseUnaryDescriptor& descriptor,
168  const char* name = nullptr);
169 
170  ARMNN_DEPRECATED_MSG("Use VisitComparisonLayer instead")
171  void SerializeEqualLayer(const armnn::IConnectableLayer* layer, const char* name);
172 
173  void SerializeFillLayer(const armnn::IConnectableLayer* layer,
174  const armnn::FillDescriptor& fillDescriptor,
175  const char* name = nullptr);
176 
177  void SerializeFloorLayer(const armnn::IConnectableLayer *layer,
178  const char *name = nullptr);
179 
180  void SerializeFullyConnectedLayer(const armnn::IConnectableLayer* layer,
181  const armnn::FullyConnectedDescriptor& fullyConnectedDescriptor,
182  const std::vector<armnn::ConstTensor>& constants,
183  const char* name = nullptr);
184 
185  void SerializeGatherLayer(const armnn::IConnectableLayer* layer,
186  const armnn::GatherDescriptor& gatherDescriptor,
187  const char* name = nullptr);
188 
189  ARMNN_DEPRECATED_MSG("Use VisitComparisonLayer instead")
190  void SerializeGreaterLayer(const armnn::IConnectableLayer* layer, const char* name = nullptr);
191 
192  void SerializeInputLayer(const armnn::IConnectableLayer* layer,
194  const char* name = nullptr);
195 
196  void SerializeInstanceNormalizationLayer(const armnn::IConnectableLayer* layer,
197  const armnn::InstanceNormalizationDescriptor& instanceNormalizationDescriptor,
198  const char* name = nullptr);
199 
200  void SerializeL2NormalizationLayer(const armnn::IConnectableLayer* layer,
201  const armnn::L2NormalizationDescriptor& l2NormalizationDescriptor,
202  const char* name = nullptr);
203 
204  void SerializeLogicalBinaryLayer(const armnn::IConnectableLayer* layer,
205  const armnn::LogicalBinaryDescriptor& descriptor,
206  const char* name = nullptr);
207 
208  void SerializeLogSoftmaxLayer(const armnn::IConnectableLayer* layer,
209  const armnn::LogSoftmaxDescriptor& logSoftmaxDescriptor,
210  const char* name = nullptr);
211 
212  void SerializeLstmLayer(const armnn::IConnectableLayer* layer,
213  const armnn::LstmDescriptor& descriptor,
214  const std::vector<armnn::ConstTensor>& constants,
215  const char* name = nullptr);
216 
217  void SerializeMeanLayer(const armnn::IConnectableLayer* layer,
218  const armnn::MeanDescriptor& descriptor,
219  const char* name);
220 
221  void SerializeMinimumLayer(const armnn::IConnectableLayer* layer,
222  const char* name = nullptr);
223 
224  void SerializeMaximumLayer(const armnn::IConnectableLayer* layer,
225  const char* name = nullptr);
226 
227  void SerializeMergeLayer(const armnn::IConnectableLayer* layer,
228  const char* name = nullptr);
229 
230  ARMNN_DEPRECATED_MSG("Use VisitConcatLayer instead")
231  void SerializeMergerLayer(const armnn::IConnectableLayer* layer,
232  const armnn::MergerDescriptor& mergerDescriptor,
233  const char* name = nullptr);
234 
235  void SerializeMultiplicationLayer(const armnn::IConnectableLayer* layer,
236  const char* name = nullptr);
237 
238  void SerializeOutputLayer(const armnn::IConnectableLayer* layer,
240  const char* name = nullptr);
241 
242  void SerializePadLayer(const armnn::IConnectableLayer* layer,
243  const armnn::PadDescriptor& PadDescriptor,
244  const char* name = nullptr);
245 
246  void SerializePermuteLayer(const armnn::IConnectableLayer* layer,
247  const armnn::PermuteDescriptor& PermuteDescriptor,
248  const char* name = nullptr);
249 
250  void SerializePooling2dLayer(const armnn::IConnectableLayer* layer,
251  const armnn::Pooling2dDescriptor& pooling2dDescriptor,
252  const char* name = nullptr);
253 
254  void SerializePreluLayer(const armnn::IConnectableLayer* layer,
255  const char* name = nullptr);
256 
257  void SerializeQuantizeLayer(const armnn::IConnectableLayer* layer,
258  const char* name = nullptr);
259 
260  void SerializeQLstmLayer(const armnn::IConnectableLayer* layer,
261  const armnn::QLstmDescriptor& descriptor,
262  const std::vector<armnn::ConstTensor>& constants,
263  const char* name = nullptr);
264 
265  void SerializeQuantizedLstmLayer(const armnn::IConnectableLayer* layer,
266  const std::vector<armnn::ConstTensor>& constants,
267  const char* name = nullptr);
268 
269  void SerializeRankLayer(const armnn::IConnectableLayer* layer,
270  const char* name = nullptr);
271 
272  void SerializeReduceLayer(const armnn::IConnectableLayer* layer,
273  const armnn::ReduceDescriptor& reduceDescriptor,
274  const char* name = nullptr);
275 
276  void SerializeReshapeLayer(const armnn::IConnectableLayer* layer,
277  const armnn::ReshapeDescriptor& reshapeDescriptor,
278  const char* name = nullptr);
279 
280  void SerializeResizeLayer(const armnn::IConnectableLayer* layer,
281  const armnn::ResizeDescriptor& resizeDescriptor,
282  const char* name = nullptr);
283 
284  ARMNN_DEPRECATED_MSG("Use VisitResizeLayer instead")
285  void SerializeResizeBilinearLayer(const armnn::IConnectableLayer* layer,
286  const armnn::ResizeBilinearDescriptor& resizeDescriptor,
287  const char* name = nullptr);
288 
289  ARMNN_DEPRECATED_MSG("Use VisitElementwiseUnaryLayer instead")
290  void SerializeRsqrtLayer(const armnn::IConnectableLayer* layer,
291  const char* name = nullptr);
292 
293  void SerializeSliceLayer(const armnn::IConnectableLayer* layer,
294  const armnn::SliceDescriptor& sliceDescriptor,
295  const char* name = nullptr);
296 
297  void SerializeSoftmaxLayer(const armnn::IConnectableLayer* layer,
298  const armnn::SoftmaxDescriptor& softmaxDescriptor,
299  const char* name = nullptr);
300 
301  void SerializeSpaceToBatchNdLayer(const armnn::IConnectableLayer* layer,
302  const armnn::SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
303  const char* name = nullptr);
304 
305  void SerializeSpaceToDepthLayer(const armnn::IConnectableLayer* layer,
306  const armnn::SpaceToDepthDescriptor& spaceToDepthDescriptor,
307  const char* name = nullptr);
308 
309  void SerializeNormalizationLayer(const armnn::IConnectableLayer* layer,
310  const armnn::NormalizationDescriptor& normalizationDescriptor,
311  const char* name = nullptr);
312 
313  void SerializeSplitterLayer(const armnn::IConnectableLayer* layer,
314  const armnn::ViewsDescriptor& viewsDescriptor,
315  const char* name = nullptr);
316 
317  void SerializeStandInLayer(const armnn::IConnectableLayer* layer,
318  const armnn::StandInDescriptor& standInDescriptor,
319  const char* name = nullptr);
320 
321  void SerializeStackLayer(const armnn::IConnectableLayer* layer,
322  const armnn::StackDescriptor& stackDescriptor,
323  const char* name = nullptr);
324 
325  void SerializeStridedSliceLayer(const armnn::IConnectableLayer* layer,
326  const armnn::StridedSliceDescriptor& stridedSliceDescriptor,
327  const char* name = nullptr);
328 
329  void SerializeSubtractionLayer(const armnn::IConnectableLayer* layer,
330  const char* name = nullptr);
331 
332  void SerializeSwitchLayer(const armnn::IConnectableLayer* layer,
333  const char* name = nullptr);
334 
335  void SerializeTransposeConvolution2dLayer(const armnn::IConnectableLayer* layer,
336  const armnn::TransposeConvolution2dDescriptor& descriptor,
337  const std::vector<armnn::ConstTensor>& constants,
338  const char* = nullptr);
339 
340  void SerializeTransposeLayer(const armnn::IConnectableLayer* layer,
341  const armnn::TransposeDescriptor& descriptor,
342  const char* name = nullptr);
343 };
344 
345 
346 
348 {
349 public:
350  SerializerImpl() = default;
351  ~SerializerImpl() = default;
352 
353  /// Serializes the network to ArmNN SerializedGraph.
354  /// @param [in] inNetwork The network to be serialized.
355  void Serialize(const armnn::INetwork& inNetwork);
356 
357  /// Serializes the SerializedGraph to the stream.
358  /// @param [stream] the stream to save to
359  /// @return true if graph is Serialized to the Stream, false otherwise
360  bool SaveSerializedToStream(std::ostream& stream);
361 
362 private:
363 
364  /// Visitor to contruct serialized network
365  SerializerStrategy m_SerializerStrategy;
366 };
367 
368 } //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:210
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.