ArmNN
 21.08
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(armnn::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<armnn::LayerGuid, uint32_t > m_guidMap;
102 
103  /// layer within our FlatBuffer index.
104  uint32_t m_layerId;
105 
106 private:
107  ARMNN_DEPRECATED_MSG("Use VisitElementwiseUnaryLayer instead")
108  void SerializeAbsLayer(const armnn::IConnectableLayer* layer,
109  const char* name = nullptr);
110 
111  void SerializeActivationLayer(const armnn::IConnectableLayer* layer,
112  const armnn::ActivationDescriptor& descriptor,
113  const char* name = nullptr);
114 
115  void SerializeAdditionLayer(const armnn::IConnectableLayer* layer,
116  const char* name = nullptr);
117 
118  void SerializeArgMinMaxLayer(const armnn::IConnectableLayer* layer,
119  const armnn::ArgMinMaxDescriptor& argMinMaxDescriptor,
120  const char* name = nullptr);
121 
122  void SerializeBatchToSpaceNdLayer(const armnn::IConnectableLayer* layer,
123  const armnn::BatchToSpaceNdDescriptor& descriptor,
124  const char* name = nullptr);
125 
126  void SerializeBatchNormalizationLayer(const armnn::IConnectableLayer* layer,
127  const armnn::BatchNormalizationDescriptor& BatchNormalizationDescriptor,
128  const std::vector<armnn::ConstTensor>& constants,
129  const char* name = nullptr);
130 
131  void SerializeCastLayer(const armnn::IConnectableLayer* layer,
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>& contants,
144  const char* name = nullptr);
145 
146  void SerializeConvolution2dLayer(const armnn::IConnectableLayer* layer,
147  const armnn::Convolution2dDescriptor& descriptor,
148  const std::vector<armnn::ConstTensor>& contants,
149  const char* name = nullptr);
150 
151  void SerializeDepthToSpaceLayer(const armnn::IConnectableLayer* layer,
152  const armnn::DepthToSpaceDescriptor& descriptor,
153  const char* name = nullptr);
154 
155  void SerializeDepthwiseConvolution2dLayer(const armnn::IConnectableLayer* layer,
156  const armnn::DepthwiseConvolution2dDescriptor& descriptor,
157  const std::vector<armnn::ConstTensor>& constants,
158  const char* name = nullptr);
159 
160  void SerializeDequantizeLayer(const armnn::IConnectableLayer* layer,
161  const char* name = nullptr);
162 
163  void SerializeDetectionPostProcessLayer(const armnn::IConnectableLayer* layer,
164  const armnn::DetectionPostProcessDescriptor& descriptor,
165  const std::vector<armnn::ConstTensor>& constants,
166  const char* name = nullptr);
167 
168  void SerializeDivisionLayer(const armnn::IConnectableLayer* layer,
169  const char* name = nullptr);
170 
171  void SerializeElementwiseUnaryLayer(const armnn::IConnectableLayer* layer,
172  const armnn::ElementwiseUnaryDescriptor& descriptor,
173  const char* name = nullptr);
174 
175  ARMNN_DEPRECATED_MSG("Use VisitComparisonLayer instead")
176  void SerializeEqualLayer(const armnn::IConnectableLayer* layer, const char* name);
177 
178  void SerializeFillLayer(const armnn::IConnectableLayer* layer,
179  const armnn::FillDescriptor& fillDescriptor,
180  const char* name = nullptr);
181 
182  void SerializeFloorLayer(const armnn::IConnectableLayer *layer,
183  const char *name = nullptr);
184 
185  void SerializeFullyConnectedLayer(const armnn::IConnectableLayer* layer,
186  const armnn::FullyConnectedDescriptor& fullyConnectedDescriptor,
187  const char* name = nullptr);
188 
189  void SerializeGatherLayer(const armnn::IConnectableLayer* layer,
190  const armnn::GatherDescriptor& gatherDescriptor,
191  const char* name = nullptr);
192 
193  ARMNN_DEPRECATED_MSG("Use VisitComparisonLayer instead")
194  void SerializeGreaterLayer(const armnn::IConnectableLayer* layer, const char* name = nullptr);
195 
196  void SerializeInputLayer(const armnn::IConnectableLayer* layer,
198  const char* name = nullptr);
199 
200  void SerializeInstanceNormalizationLayer(const armnn::IConnectableLayer* layer,
201  const armnn::InstanceNormalizationDescriptor& instanceNormalizationDescriptor,
202  const char* name = nullptr);
203 
204  void SerializeL2NormalizationLayer(const armnn::IConnectableLayer* layer,
205  const armnn::L2NormalizationDescriptor& l2NormalizationDescriptor,
206  const char* name = nullptr);
207 
208  void SerializeLogicalBinaryLayer(const armnn::IConnectableLayer* layer,
209  const armnn::LogicalBinaryDescriptor& descriptor,
210  const char* name = nullptr);
211 
212  void SerializeLogSoftmaxLayer(const armnn::IConnectableLayer* layer,
213  const armnn::LogSoftmaxDescriptor& logSoftmaxDescriptor,
214  const char* name = nullptr);
215 
216  void SerializeLstmLayer(const armnn::IConnectableLayer* layer,
217  const armnn::LstmDescriptor& descriptor,
218  const std::vector<armnn::ConstTensor>& constants,
219  const char* name = nullptr);
220 
221  void SerializeMeanLayer(const armnn::IConnectableLayer* layer,
222  const armnn::MeanDescriptor& descriptor,
223  const char* name);
224 
225  void SerializeMinimumLayer(const armnn::IConnectableLayer* layer,
226  const char* name = nullptr);
227 
228  void SerializeMaximumLayer(const armnn::IConnectableLayer* layer,
229  const char* name = nullptr);
230 
231  void SerializeMergeLayer(const armnn::IConnectableLayer* layer,
232  const char* name = nullptr);
233 
234  ARMNN_DEPRECATED_MSG("Use VisitConcatLayer instead")
235  void SerializeMergerLayer(const armnn::IConnectableLayer* layer,
236  const armnn::MergerDescriptor& mergerDescriptor,
237  const char* name = nullptr);
238 
239  void SerializeMultiplicationLayer(const armnn::IConnectableLayer* layer,
240  const char* name = nullptr);
241 
242  void SerializeOutputLayer(const armnn::IConnectableLayer* layer,
244  const char* name = nullptr);
245 
246  void SerializePadLayer(const armnn::IConnectableLayer* layer,
247  const armnn::PadDescriptor& PadDescriptor,
248  const char* name = nullptr);
249 
250  void SerializePermuteLayer(const armnn::IConnectableLayer* layer,
251  const armnn::PermuteDescriptor& PermuteDescriptor,
252  const char* name = nullptr);
253 
254  void SerializePooling2dLayer(const armnn::IConnectableLayer* layer,
255  const armnn::Pooling2dDescriptor& pooling2dDescriptor,
256  const char* name = nullptr);
257 
258  void SerializePreluLayer(const armnn::IConnectableLayer* layer,
259  const char* name = nullptr);
260 
261  void SerializeQuantizeLayer(const armnn::IConnectableLayer* layer,
262  const char* name = nullptr);
263 
264  void SerializeQLstmLayer(const armnn::IConnectableLayer* layer,
265  const armnn::QLstmDescriptor& descriptor,
266  const std::vector<armnn::ConstTensor>& constants,
267  const char* name = nullptr);
268 
269  void SerializeQuantizedLstmLayer(const armnn::IConnectableLayer* layer,
270  const std::vector<armnn::ConstTensor>& constants,
271  const char* name = nullptr);
272 
273  void SerializeRankLayer(const armnn::IConnectableLayer* layer,
274  const char* name = nullptr);
275 
276  void SerializeReduceLayer(const armnn::IConnectableLayer* layer,
277  const armnn::ReduceDescriptor& reduceDescriptor,
278  const char* name = nullptr);
279 
280  void SerializeReshapeLayer(const armnn::IConnectableLayer* layer,
281  const armnn::ReshapeDescriptor& reshapeDescriptor,
282  const char* name = nullptr);
283 
284  void SerializeResizeLayer(const armnn::IConnectableLayer* layer,
285  const armnn::ResizeDescriptor& resizeDescriptor,
286  const char* name = nullptr);
287 
288  ARMNN_DEPRECATED_MSG("Use VisitResizeLayer instead")
289  void SerializeResizeBilinearLayer(const armnn::IConnectableLayer* layer,
290  const armnn::ResizeBilinearDescriptor& resizeDescriptor,
291  const char* name = nullptr);
292 
293  ARMNN_DEPRECATED_MSG("Use VisitElementwiseUnaryLayer instead")
294  void SerializeRsqrtLayer(const armnn::IConnectableLayer* layer,
295  const char* name = nullptr);
296 
297  void SerializeSliceLayer(const armnn::IConnectableLayer* layer,
298  const armnn::SliceDescriptor& sliceDescriptor,
299  const char* name = nullptr);
300 
301  void SerializeSoftmaxLayer(const armnn::IConnectableLayer* layer,
302  const armnn::SoftmaxDescriptor& softmaxDescriptor,
303  const char* name = nullptr);
304 
305  void SerializeSpaceToBatchNdLayer(const armnn::IConnectableLayer* layer,
306  const armnn::SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
307  const char* name = nullptr);
308 
309  void SerializeSpaceToDepthLayer(const armnn::IConnectableLayer* layer,
310  const armnn::SpaceToDepthDescriptor& spaceToDepthDescriptor,
311  const char* name = nullptr);
312 
313  void SerializeNormalizationLayer(const armnn::IConnectableLayer* layer,
314  const armnn::NormalizationDescriptor& normalizationDescriptor,
315  const char* name = nullptr);
316 
317  void SerializeShapeLayer(const armnn::IConnectableLayer* layer,
318  const char* name = nullptr);
319 
320  void SerializeSplitterLayer(const armnn::IConnectableLayer* layer,
321  const armnn::ViewsDescriptor& viewsDescriptor,
322  const char* name = nullptr);
323 
324  void SerializeStandInLayer(const armnn::IConnectableLayer* layer,
325  const armnn::StandInDescriptor& standInDescriptor,
326  const char* name = nullptr);
327 
328  void SerializeStackLayer(const armnn::IConnectableLayer* layer,
329  const armnn::StackDescriptor& stackDescriptor,
330  const char* name = nullptr);
331 
332  void SerializeStridedSliceLayer(const armnn::IConnectableLayer* layer,
333  const armnn::StridedSliceDescriptor& stridedSliceDescriptor,
334  const char* name = nullptr);
335 
336  void SerializeSubtractionLayer(const armnn::IConnectableLayer* layer,
337  const char* name = nullptr);
338 
339  void SerializeSwitchLayer(const armnn::IConnectableLayer* layer,
340  const char* name = nullptr);
341 
342  void SerializeTransposeConvolution2dLayer(const armnn::IConnectableLayer* layer,
343  const armnn::TransposeConvolution2dDescriptor& descriptor,
344  const std::vector<armnn::ConstTensor>& constants,
345  const char* = nullptr);
346 
347  void SerializeTransposeLayer(const armnn::IConnectableLayer* layer,
348  const armnn::TransposeDescriptor& descriptor,
349  const char* name = nullptr);
350 
351  void SerializeUnidirectionalSequenceLstmLayer(const armnn::IConnectableLayer* layer,
353  const std::vector<armnn::ConstTensor>& constants,
354  const char* name = nullptr);
355 };
356 
357 
358 
360 {
361 public:
362  SerializerImpl() = default;
363  ~SerializerImpl() = default;
364 
365  /// Serializes the network to ArmNN SerializedGraph.
366  /// @param [in] inNetwork The network to be serialized.
367  void Serialize(const armnn::INetwork& inNetwork);
368 
369  /// Serializes the SerializedGraph to the stream.
370  /// @param [stream] the stream to save to
371  /// @return true if graph is Serialized to the Stream, false otherwise
372  bool SaveSerializedToStream(std::ostream& stream);
373 
374 private:
375 
376  /// Visitor to contruct serialized network
377  SerializerStrategy m_SerializerStrategy;
378 };
379 
380 } //namespace armnnSerializer
A ViewsDescriptor for the SplitterLayer.
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:61
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:78
A Convolution2dDescriptor for the Convolution2dLayer.
Main network class which provides the interface for building up a neural network. ...
Definition: INetwork.hpp:177
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:244
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: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:25
A SliceDescriptor for the SliceLayer.
A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
std::vector< int > & GetOutputIds()
Definition: Serializer.hpp:46
A ElementwiseUnaryDescriptor for the ElementwiseUnaryLayer.
Definition: Descriptors.hpp:98
profiling::ProfilingGuid LayerGuid
Define LayerGuid type.
Definition: Types.hpp:313
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:51
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.