blob: eaf19d52b5087581320d1b4f06f3c129f426717f [file] [log] [blame]
Mike Kelly8c1701a2019-02-11 17:01:27 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "Serializer.hpp"
Saoirse Stewart3166c3e2019-02-18 15:24:53 +00007
8#include "SerializerUtils.hpp"
9
Mike Kelly8c1701a2019-02-11 17:01:27 +000010#include <armnn/ArmNN.hpp>
Saoirse Stewart3166c3e2019-02-18 15:24:53 +000011
Mike Kelly8c1701a2019-02-11 17:01:27 +000012#include <iostream>
Saoirse Stewart3166c3e2019-02-18 15:24:53 +000013
Matthew Bentham268509a2019-02-25 13:58:24 +000014#include <ArmnnSchema_generated.h>
Saoirse Stewart3166c3e2019-02-18 15:24:53 +000015
Mike Kelly8c1701a2019-02-11 17:01:27 +000016#include <flatbuffers/util.h>
17
18using namespace armnn;
19namespace fb = flatbuffers;
Derek Lamberti0028d1b2019-02-20 13:57:42 +000020namespace serializer = armnnSerializer;
Mike Kelly8c1701a2019-02-11 17:01:27 +000021
22namespace armnnSerializer
23{
24
Mike Kellyaf484012019-02-20 16:53:11 +000025serializer::ActivationFunction GetFlatBufferActivationFunction(armnn::ActivationFunction function)
26{
27 switch (function)
28 {
29 case armnn::ActivationFunction::Sigmoid:
30 return serializer::ActivationFunction::ActivationFunction_Sigmoid;
31 case armnn::ActivationFunction::TanH:
32 return serializer::ActivationFunction::ActivationFunction_TanH;
33 case armnn::ActivationFunction::Linear:
34 return serializer::ActivationFunction::ActivationFunction_Linear;
35 case armnn::ActivationFunction::ReLu:
36 return serializer::ActivationFunction::ActivationFunction_ReLu;
37 case armnn::ActivationFunction::BoundedReLu:
38 return serializer::ActivationFunction::ActivationFunction_BoundedReLu;
39 case armnn::ActivationFunction::LeakyReLu:
40 return serializer::ActivationFunction::ActivationFunction_LeakyReLu;
41 case armnn::ActivationFunction::Abs:
42 return serializer::ActivationFunction::ActivationFunction_Abs;
43 case armnn::ActivationFunction::Sqrt:
44 return serializer::ActivationFunction::ActivationFunction_Sqrt;
45 case armnn::ActivationFunction::Square:
46 return serializer::ActivationFunction::ActivationFunction_Square;
47 default:
48 return serializer::ActivationFunction::ActivationFunction_Sigmoid;
49 }
50}
51
Saoirse Stewartcb8a3212019-02-14 15:46:10 +000052uint32_t SerializerVisitor::GetSerializedId(unsigned int guid)
53{
54 std::pair<unsigned int, uint32_t> guidPair(guid, m_layerId);
55
56 if (m_guidMap.empty())
57 {
58 m_guidMap.insert(guidPair);
59 }
60 else if (m_guidMap.find(guid) == m_guidMap.end())
61 {
62 guidPair.second = ++m_layerId;
63 m_guidMap.insert(guidPair);
64 return m_layerId;
65 }
Saoirse Stewart30211042019-02-18 17:19:16 +000066 return m_guidMap[guid];
Saoirse Stewartcb8a3212019-02-14 15:46:10 +000067}
68
Mike Kelly8c1701a2019-02-11 17:01:27 +000069// Build FlatBuffer for Input Layer
Derek Lamberti0028d1b2019-02-20 13:57:42 +000070void SerializerVisitor::VisitInputLayer(const armnn::IConnectableLayer* layer, LayerBindingId id, const char* name)
Mike Kelly8c1701a2019-02-11 17:01:27 +000071{
72 // Create FlatBuffer BaseLayer
73 auto flatBufferInputBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Input);
74
75 // Create FlatBuffer BindableBaseLayer
76 auto flatBufferInputBindableBaseLayer = serializer::CreateBindableLayerBase(m_flatBufferBuilder,
77 flatBufferInputBaseLayer,
78 id);
Mike Kelly8c1701a2019-02-11 17:01:27 +000079 // Push layer Guid to outputIds.
Saoirse Stewartcb8a3212019-02-14 15:46:10 +000080 m_inputIds.push_back(GetSerializedId(layer->GetGuid()));
Mike Kelly8c1701a2019-02-11 17:01:27 +000081
82 // Create the FlatBuffer InputLayer
83 auto flatBufferInputLayer = serializer::CreateInputLayer(m_flatBufferBuilder, flatBufferInputBindableBaseLayer);
84
85 // Add the AnyLayer to the FlatBufferLayers
86 CreateAnyLayer(flatBufferInputLayer.o, serializer::Layer::Layer_InputLayer);
87}
88
89// Build FlatBuffer for Output Layer
Derek Lamberti0028d1b2019-02-20 13:57:42 +000090void SerializerVisitor::VisitOutputLayer(const armnn::IConnectableLayer* layer, LayerBindingId id, const char* name)
Mike Kelly8c1701a2019-02-11 17:01:27 +000091{
92 // Create FlatBuffer BaseLayer
93 auto flatBufferOutputBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Output);
94
95 // Create FlatBuffer BindableBaseLayer
96 auto flatBufferOutputBindableBaseLayer = serializer::CreateBindableLayerBase(m_flatBufferBuilder,
97 flatBufferOutputBaseLayer,
98 id);
99 // Push layer Guid to outputIds.
Saoirse Stewartcb8a3212019-02-14 15:46:10 +0000100 m_outputIds.push_back(GetSerializedId(layer->GetGuid()));
Mike Kelly8c1701a2019-02-11 17:01:27 +0000101
102 // Create the FlatBuffer OutputLayer
103 auto flatBufferOutputLayer = serializer::CreateOutputLayer(m_flatBufferBuilder, flatBufferOutputBindableBaseLayer);
104 // Add the AnyLayer to the FlatBufferLayers
105 CreateAnyLayer(flatBufferOutputLayer.o, serializer::Layer::Layer_OutputLayer);
106}
107
Mike Kellyaf484012019-02-20 16:53:11 +0000108// Build FlatBuffer for Activation Layer
109void SerializerVisitor::VisitActivationLayer(const armnn::IConnectableLayer* layer,
110 const armnn::ActivationDescriptor& descriptor,
111 const char* name)
112{
113 // Create FlatBuffer BaseLayer
114 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Activation);
115
116 // Create the FlatBuffer ActivationDescriptor
117 auto flatBufferDescriptor = CreateActivationDescriptor(m_flatBufferBuilder,
118 GetFlatBufferActivationFunction(descriptor.m_Function),
119 descriptor.m_A,
120 descriptor.m_B);
121
122 // Create the FlatBuffer ActivationLayer
123 auto flatBufferAdditionLayer = CreateActivationLayer(m_flatBufferBuilder,
124 flatBufferBaseLayer,
125 flatBufferDescriptor);
126
127 // Add the AnyLayer to the FlatBufferLayers
128 CreateAnyLayer(flatBufferAdditionLayer.o, serializer::Layer::Layer_ActivationLayer);
129}
130
Mike Kelly8c1701a2019-02-11 17:01:27 +0000131// Build FlatBuffer for Addition Layer
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000132void SerializerVisitor::VisitAdditionLayer(const armnn::IConnectableLayer* layer, const char* name)
Mike Kelly8c1701a2019-02-11 17:01:27 +0000133{
134 // Create FlatBuffer BaseLayer
135 auto flatBufferAdditionBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Addition);
136
137 // Create the FlatBuffer AdditionLayer
138 auto flatBufferAdditionLayer = serializer::CreateAdditionLayer(m_flatBufferBuilder, flatBufferAdditionBaseLayer);
139
140 // Add the AnyLayer to the FlatBufferLayers
141 CreateAnyLayer(flatBufferAdditionLayer.o, serializer::Layer::Layer_AdditionLayer);
142}
143
Nattapat Chaimanowong6b4ed982019-02-26 17:24:13 +0000144// Build FlatBuffer for BatchToSpaceNd Layer
145void SerializerVisitor::VisitBatchToSpaceNdLayer(const armnn::IConnectableLayer* layer,
146 const armnn::BatchToSpaceNdDescriptor& descriptor,
147 const char* name)
148{
149 // Create FlatBuffer BaseLayer
150 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_BatchToSpaceNd);
151
152 std::vector<unsigned int> crops;
153 crops.reserve(descriptor.m_Crops.size() * 2);
154 for (auto& crop : descriptor.m_Crops)
155 {
156 crops.push_back(crop.first);
157 crops.push_back(crop.second);
158 }
159
160 auto flatBufferDescriptor =
161 CreateBatchToSpaceNdDescriptor(m_flatBufferBuilder,
162 m_flatBufferBuilder.CreateVector(descriptor.m_BlockShape),
163 m_flatBufferBuilder.CreateVector(crops),
164 GetFlatBufferDataLayout(descriptor.m_DataLayout));
165
166 auto flatBufferLayer = serializer::CreateBatchToSpaceNdLayer(m_flatBufferBuilder,
167 flatBufferBaseLayer,
168 flatBufferDescriptor);
169
170 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_BatchToSpaceNdLayer);
171}
172
ruoyan018e7fa232019-02-28 15:09:07 +0000173void SerializerVisitor::VisitBatchNormalizationLayer(const armnn::IConnectableLayer* layer,
174 const armnn::BatchNormalizationDescriptor& batchNormDescriptor,
175 const armnn::ConstTensor& mean,
176 const armnn::ConstTensor& variance,
177 const armnn::ConstTensor& beta,
178 const armnn::ConstTensor& gamma,
179 const char* name)
180{
181 auto fbBatchNormalizationBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_BatchNormalization);
182 auto fbBatchNormalizationDescriptor = serializer::CreateBatchNormalizationDescriptor(
183 m_flatBufferBuilder,
184 batchNormDescriptor.m_Eps,
185 GetFlatBufferDataLayout(batchNormDescriptor.m_DataLayout));
186
187 auto fbMeanConstTensorInfo = CreateConstTensorInfo(mean);
188 auto fbVarianceConstTensorInfo = CreateConstTensorInfo(variance);
189 auto fbBetaConstTensorInfo = CreateConstTensorInfo(beta);
190 auto fbGammaConstTensorInfo = CreateConstTensorInfo(gamma);
191 auto fbBatchNormalizationLayer = serializer::CreateBatchNormalizationLayer(m_flatBufferBuilder,
192 fbBatchNormalizationBaseLayer,
193 fbBatchNormalizationDescriptor,
194 fbMeanConstTensorInfo,
195 fbVarianceConstTensorInfo,
196 fbBetaConstTensorInfo,
197 fbGammaConstTensorInfo);
198
199 CreateAnyLayer(fbBatchNormalizationLayer.o, serializer::Layer::Layer_BatchNormalizationLayer);
200}
201
Conor Kennedy76277882019-02-26 08:29:54 +0000202// Build FlatBuffer for Constant Layer
203void SerializerVisitor::VisitConstantLayer(const armnn::IConnectableLayer* layer,
204 const armnn::ConstTensor& input,
205 const char* name)
206{
207 // Create FlatBuffer BaseLayer
208 auto flatBufferConstantBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Constant);
209
210 auto flatBufferConstTensorInfo = CreateConstTensorInfo(input);
211
212 // Create the FlatBuffer ConstantLayer
213 auto flatBufferLayer = CreateConstantLayer(m_flatBufferBuilder,
214 flatBufferConstantBaseLayer,
215 flatBufferConstTensorInfo);
216
217 // Add the AnyLayer to the FlatBufferLayers
218 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_ConstantLayer);
219}
220
Mike Kellya0766c32019-02-19 17:22:07 +0000221// Build FlatBuffer for Convolution2dLayer
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000222void SerializerVisitor::VisitConvolution2dLayer(const armnn::IConnectableLayer* layer,
223 const armnn::Convolution2dDescriptor& descriptor,
224 const armnn::ConstTensor& weights,
225 const armnn::Optional<armnn::ConstTensor>& biases,
Mike Kellya0766c32019-02-19 17:22:07 +0000226 const char* name)
227{
228 // Create FlatBuffer BaseLayer
229 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Convolution2d);
230
231 auto flatBufferDescriptor = CreateConvolution2dDescriptor(m_flatBufferBuilder,
232 descriptor.m_PadLeft,
233 descriptor.m_PadRight,
234 descriptor.m_PadTop,
235 descriptor.m_PadBottom,
236 descriptor.m_StrideX,
237 descriptor.m_StrideY,
238 descriptor.m_BiasEnabled,
239 GetFlatBufferDataLayout(descriptor.m_DataLayout));
240 auto flatBufferWeightsConstTensorInfo = CreateConstTensorInfo(weights);
241 flatbuffers::Offset<serializer::ConstTensor> flatBufferBiasesConstTensorInfo;
242
243 if (biases.has_value())
244 {
245 flatBufferBiasesConstTensorInfo = CreateConstTensorInfo(biases.value());
246 }
247
248 // Create the FlatBuffer Convolution2dLayer
249 auto flatBufferLayer = CreateConvolution2dLayer(m_flatBufferBuilder,
250 flatBufferBaseLayer,
251 flatBufferDescriptor,
252 flatBufferWeightsConstTensorInfo,
253 flatBufferBiasesConstTensorInfo);
254
255 // Add the AnyLayer to the FlatBufferLayers
256 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_Convolution2dLayer);
257}
258
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000259void SerializerVisitor::VisitDepthwiseConvolution2dLayer(const armnn::IConnectableLayer* layer,
260 const armnn::DepthwiseConvolution2dDescriptor& descriptor,
261 const armnn::ConstTensor& weights,
262 const armnn::Optional<armnn::ConstTensor>& biases,
Aron Virginas-Tarc04125f2019-02-19 16:31:08 +0000263 const char* name)
264{
265 auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_DepthwiseConvolution2d);
266 auto fbDescriptor = CreateDepthwiseConvolution2dDescriptor(m_flatBufferBuilder,
267 descriptor.m_PadLeft,
268 descriptor.m_PadRight,
269 descriptor.m_PadTop,
270 descriptor.m_PadBottom,
271 descriptor.m_StrideX,
272 descriptor.m_StrideY,
273 descriptor.m_BiasEnabled,
274 GetFlatBufferDataLayout(descriptor.m_DataLayout));
275
276 flatbuffers::Offset<serializer::ConstTensor> fbWeightsConstTensorInfo = CreateConstTensorInfo(weights);
277 flatbuffers::Offset<serializer::ConstTensor> fbBiasesConstTensorInfo;
278 if (biases.has_value())
279 {
280 fbBiasesConstTensorInfo = CreateConstTensorInfo(biases.value());
281 }
282
283 auto flatBufferLayer = CreateDepthwiseConvolution2dLayer(m_flatBufferBuilder,
284 fbBaseLayer,
285 fbDescriptor,
286 fbWeightsConstTensorInfo,
287 fbBiasesConstTensorInfo);
288
289 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_DepthwiseConvolution2dLayer);
290}
291
Éanna Ó Catháin58885892019-02-27 16:16:39 +0000292void SerializerVisitor::VisitDivisionLayer(const armnn::IConnectableLayer* layer, const char* name)
293{
Aron Virginas-Tar0fe32452019-02-28 13:12:47 +0000294 auto fbDivisionBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Division);
295 auto fbDivisionLayer = serializer::CreateDivisionLayer(m_flatBufferBuilder, fbDivisionBaseLayer);
Éanna Ó Catháin58885892019-02-27 16:16:39 +0000296
Aron Virginas-Tar0fe32452019-02-28 13:12:47 +0000297 CreateAnyLayer(fbDivisionLayer.o, serializer::Layer::Layer_DivisionLayer);
298}
Éanna Ó Catháin58885892019-02-27 16:16:39 +0000299
Aron Virginas-Tar377351e2019-02-27 14:42:31 +0000300void SerializerVisitor::VisitEqualLayer(const armnn::IConnectableLayer* layer, const char* name)
301{
302 auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Equal);
303 auto fbEqualLayer = serializer::CreateEqualLayer(m_flatBufferBuilder, fbBaseLayer);
304
305 CreateAnyLayer(fbEqualLayer.o, serializer::Layer::Layer_EqualLayer);
306}
307
Finn Williamsdd2ba7e2019-03-01 11:51:52 +0000308void SerializerVisitor::VisitFloorLayer(const armnn::IConnectableLayer *layer, const char *name)
309{
310 auto flatBufferFloorBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Floor);
311 auto flatBufferFloorLayer = serializer::CreateFloorLayer(m_flatBufferBuilder, flatBufferFloorBaseLayer);
312
313 CreateAnyLayer(flatBufferFloorLayer.o, serializer::Layer::Layer_FloorLayer);
314}
315
Saoirse Stewarta1ed73a2019-03-04 13:40:12 +0000316void SerializerVisitor::VisitGatherLayer(const armnn::IConnectableLayer* layer, const char* name)
317{
Matteo Martincighf81edaa2019-03-04 14:34:30 +0000318 auto fbGatherBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Gather);
319 auto flatBufferLayer = serializer::CreateGatherLayer(m_flatBufferBuilder, fbGatherBaseLayer);
Saoirse Stewarta1ed73a2019-03-04 13:40:12 +0000320
321 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_GatherLayer);
322}
323
Conor Kennedy79ffdf52019-03-01 14:24:54 +0000324void SerializerVisitor::VisitGreaterLayer(const armnn::IConnectableLayer* layer, const char* name)
325{
326 auto fbGreaterBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Greater);
Sadik Armaganac97c8c2019-03-04 17:44:21 +0000327 auto fbGreaterLayer = serializer::CreateGreaterLayer(m_flatBufferBuilder, fbGreaterBaseLayer);
Conor Kennedy79ffdf52019-03-01 14:24:54 +0000328
329 CreateAnyLayer(fbGreaterLayer.o, serializer::Layer::Layer_GreaterLayer);
330}
331
Narumol Prangnawarat495701f2019-03-07 17:31:34 +0000332void SerializerVisitor::VisitL2NormalizationLayer(const armnn::IConnectableLayer* layer,
333 const armnn::L2NormalizationDescriptor& l2NormalizationDescriptor,
334 const char* name)
335{
336 // Create FlatBuffer BaseLayer
337 auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_L2Normalization);
338
339 // Create the FlatBuffer L2Normalization Descriptor
340 auto fbDescriptor = serializer::CreateL2NormalizationDescriptor(
341 m_flatBufferBuilder, GetFlatBufferDataLayout(l2NormalizationDescriptor.m_DataLayout));
342
343 // Create Flatuffer layer
344 auto fbLayer = serializer::CreateL2NormalizationLayer(m_flatBufferBuilder, fbBaseLayer, fbDescriptor);
345
346 CreateAnyLayer(fbLayer.o, serializer::Layer::Layer_L2NormalizationLayer);
347}
348
Sadik Armaganac97c8c2019-03-04 17:44:21 +0000349void SerializerVisitor::VisitMaximumLayer(const armnn::IConnectableLayer* layer, const char* name)
350{
351 auto fbMaximumBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Maximum);
352 auto fbMaximumLayer = serializer::CreateMaximumLayer(m_flatBufferBuilder, fbMaximumBaseLayer);
353
354 CreateAnyLayer(fbMaximumLayer.o, serializer::Layer::Layer_MaximumLayer);
355}
356
357void SerializerVisitor::VisitMeanLayer(const armnn::IConnectableLayer* layer,
358 const armnn::MeanDescriptor& descriptor,
359 const char* name)
360{
361 auto fbMeanBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Mean);
362 auto fbMeanDescriptor = serializer::CreateMeanDescriptor(m_flatBufferBuilder,
363 m_flatBufferBuilder.CreateVector(descriptor.m_Axis),
364 descriptor.m_KeepDims);
365
366 auto fbMeanLayer = serializer::CreateMeanLayer(m_flatBufferBuilder,
367 fbMeanBaseLayer,
368 fbMeanDescriptor);
369
370 CreateAnyLayer(fbMeanLayer.o, serializer::Layer::Layer_MeanLayer);
371}
372
373void SerializerVisitor::VisitMinimumLayer(const armnn::IConnectableLayer* layer, const char* name)
374{
375 auto fbMinimumBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Minimum);
376 auto fbMinimumLayer = serializer::CreateMinimumLayer(m_flatBufferBuilder, fbMinimumBaseLayer);
377
378 CreateAnyLayer(fbMinimumLayer.o, serializer::Layer::Layer_MinimumLayer);
379}
380
Jim Flynnac25a1b2019-02-28 10:40:49 +0000381void SerializerVisitor::VisitMergerLayer(const armnn::IConnectableLayer* layer,
382 const armnn::OriginsDescriptor& mergerDescriptor,
383 const char* name)
384{
385 auto flatBufferMergerBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Merger);
386
387 std::vector<flatbuffers::Offset<UintVector>> views;
388 for (unsigned int v = 0; v < mergerDescriptor.GetNumViews(); ++v)
389 {
390 const uint32_t* origin = mergerDescriptor.GetViewOrigin(v);
391 std::vector<uint32_t> origins;
392 for (unsigned int d = 0; d < mergerDescriptor.GetNumDimensions(); ++d)
393 {
394 origins.push_back(origin[d]);
395 }
396 auto view = m_flatBufferBuilder.CreateVector(origins);
397 auto uintVector = CreateUintVector(m_flatBufferBuilder, view);
398 views.push_back(uintVector);
399 }
400
401 auto flatBufferMergerDescriptor = CreateOriginsDescriptor(m_flatBufferBuilder,
402 mergerDescriptor.GetConcatAxis(),
403 mergerDescriptor.GetNumViews(),
404 mergerDescriptor.GetNumDimensions(),
405 m_flatBufferBuilder.CreateVector(views));
406
407 auto flatBufferLayer = CreateMergerLayer(m_flatBufferBuilder,
408 flatBufferMergerBaseLayer,
409 flatBufferMergerDescriptor);
410
411 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_MergerLayer);
412}
413
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000414void SerializerVisitor::VisitMultiplicationLayer(const armnn::IConnectableLayer* layer, const char* name)
Sadik Armagan5f450272019-02-12 14:31:45 +0000415{
Sadik Armaganac97c8c2019-03-04 17:44:21 +0000416 auto fbMultiplicationBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Multiplication);
417 auto fbMultiplicationLayer = serializer::CreateMultiplicationLayer(m_flatBufferBuilder,
418 fbMultiplicationBaseLayer);
Sadik Armagan5f450272019-02-12 14:31:45 +0000419
Sadik Armaganac97c8c2019-03-04 17:44:21 +0000420 CreateAnyLayer(fbMultiplicationLayer.o, serializer::Layer::Layer_MultiplicationLayer);
Sadik Armagan5f450272019-02-12 14:31:45 +0000421}
422
Nattapat Chaimanowongebb0f9c2019-03-01 12:14:06 +0000423void SerializerVisitor::VisitPadLayer(const armnn::IConnectableLayer* layer,
424 const armnn::PadDescriptor& padDescriptor,
425 const char* name)
426{
427 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Pad);
428
429 std::vector<unsigned int> padList;
430 for (auto& p: padDescriptor.m_PadList)
431 {
432 padList.push_back(p.first);
433 padList.push_back(p.second);
434 }
435
436 auto flatBufferPadDesc = serializer::CreatePadDescriptor(m_flatBufferBuilder,
437 m_flatBufferBuilder.CreateVector(padList));
438
439 auto flatBufferPadLayer = serializer::CreatePadLayer(m_flatBufferBuilder,
440 flatBufferBaseLayer,
441 flatBufferPadDesc);
442
443 CreateAnyLayer(flatBufferPadLayer.o, serializer::Layer::Layer_PadLayer);
444}
445
Nattapat Chaimanowong30b00202019-02-20 17:31:34 +0000446void SerializerVisitor::VisitPermuteLayer(const armnn::IConnectableLayer* layer,
447 const armnn::PermuteDescriptor& permuteDescriptor,
448 const char* name)
449{
450 // Create FlatBuffer BaseLayer
451 auto flatBufferPermuteBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Permute);
452
453 std::vector<unsigned int> dimMappings;
454 for (auto& v: permuteDescriptor.m_DimMappings)
455 {
456 dimMappings.push_back(v);
457 }
458
459 auto flatBufferPermuteDesc = serializer::CreatePermuteDescriptor(m_flatBufferBuilder,
460 m_flatBufferBuilder.CreateVector(dimMappings));
461
462 // Create the FlatBuffer PermuteLayer
463 auto flatBufferPermuteLayer = serializer::CreatePermuteLayer(m_flatBufferBuilder,
464 flatBufferPermuteBaseLayer,
465 flatBufferPermuteDesc);
466
467 // Add the AnyLayer to the FlatBufferLayers
468 CreateAnyLayer(flatBufferPermuteLayer.o, serializer::Layer::Layer_PermuteLayer);
469}
470
Saoirse Stewart263829c2019-02-19 15:54:14 +0000471// Build FlatBuffer for Reshape Layer
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000472void SerializerVisitor::VisitReshapeLayer(const armnn::IConnectableLayer* layer,
Saoirse Stewart263829c2019-02-19 15:54:14 +0000473 const armnn::ReshapeDescriptor& reshapeDescriptor,
474 const char* name)
475{
476 // Create FlatBuffer BaseLayer
477 auto flatBufferReshapeBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Reshape);
478
479 std::vector<unsigned int> targetShape;
480 for (unsigned int i =0; i < reshapeDescriptor.m_TargetShape.GetNumDimensions(); i++)
481 {
482 targetShape.push_back(reshapeDescriptor.m_TargetShape[i]);
483 }
484
485 auto flatBufferReshapeDesc = serializer::CreateReshapeDescriptor(m_flatBufferBuilder,
486 m_flatBufferBuilder.CreateVector(targetShape));
487
488 // Create the FlatBuffer ReshapeLayer
489 auto flatBufferReshapeLayer = serializer::CreateReshapeLayer(m_flatBufferBuilder, flatBufferReshapeBaseLayer,
490 flatBufferReshapeDesc);
491
492 // Add the AnyLayer to the FlatBufferLayers
493 CreateAnyLayer(flatBufferReshapeLayer.o, serializer::Layer::Layer_ReshapeLayer);
494}
495
Nattapat Chaimanowong6522cdc2019-03-01 16:14:13 +0000496void SerializerVisitor::VisitResizeBilinearLayer(const armnn::IConnectableLayer* layer,
497 const armnn::ResizeBilinearDescriptor& resizeDescriptor,
498 const char* name)
499{
500 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_ResizeBilinear);
501
502 auto flatBufferDescriptor =
503 CreateResizeBilinearDescriptor(m_flatBufferBuilder,
504 resizeDescriptor.m_TargetWidth,
505 resizeDescriptor.m_TargetHeight,
506 GetFlatBufferDataLayout(resizeDescriptor.m_DataLayout));
507
508 auto flatBufferLayer = serializer::CreateResizeBilinearLayer(m_flatBufferBuilder,
509 flatBufferBaseLayer,
510 flatBufferDescriptor);
511
512 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_ResizeBilinearLayer);
513}
514
Sadik Armagan8b42a382019-03-01 14:24:49 +0000515void SerializerVisitor::VisitRsqrtLayer(const armnn::IConnectableLayer* layer, const char* name)
516{
517 auto fbRsqrtBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Rsqrt);
518 auto fbRsqrtLayer = serializer::CreateRsqrtLayer(m_flatBufferBuilder, fbRsqrtBaseLayer);
519
520 CreateAnyLayer(fbRsqrtLayer.o, serializer::Layer::Layer_RsqrtLayer);
521}
522
Aron Virginas-Tarfc413c02019-02-13 15:41:52 +0000523// Build FlatBuffer for Softmax Layer
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000524void SerializerVisitor::VisitSoftmaxLayer(const armnn::IConnectableLayer* layer,
525 const armnn::SoftmaxDescriptor& softmaxDescriptor,
Aron Virginas-Tarfc413c02019-02-13 15:41:52 +0000526 const char* name)
527{
528 // Create FlatBuffer BaseLayer
529 auto flatBufferSoftmaxBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Softmax);
530
531 // Create the FlatBuffer SoftmaxDescriptor
532 auto flatBufferSoftmaxDesc =
533 serializer::CreateSoftmaxDescriptor(m_flatBufferBuilder, softmaxDescriptor.m_Beta);
534
535 // Create the FlatBuffer SoftmaxLayer
536 auto flatBufferSoftmaxLayer =
537 serializer::CreateSoftmaxLayer(m_flatBufferBuilder,
538 flatBufferSoftmaxBaseLayer,
539 flatBufferSoftmaxDesc);
540
541 CreateAnyLayer(flatBufferSoftmaxLayer.o, serializer::Layer::Layer_SoftmaxLayer);
542}
543
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000544void SerializerVisitor::VisitPooling2dLayer(const armnn::IConnectableLayer* layer,
545 const armnn::Pooling2dDescriptor& pooling2dDescriptor,
Saoirse Stewart3166c3e2019-02-18 15:24:53 +0000546 const char* name)
547{
548 auto fbPooling2dBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Pooling2d);
549 auto fbPooling2dDescriptor = serializer::CreatePooling2dDescriptor(
550 m_flatBufferBuilder,
551 GetFlatBufferPoolingAlgorithm(pooling2dDescriptor.m_PoolType),
552 pooling2dDescriptor.m_PadLeft,
553 pooling2dDescriptor.m_PadRight,
554 pooling2dDescriptor.m_PadTop,
555 pooling2dDescriptor.m_PadBottom,
556 pooling2dDescriptor.m_PoolWidth,
557 pooling2dDescriptor.m_PoolHeight,
558 pooling2dDescriptor.m_StrideX,
559 pooling2dDescriptor.m_StrideY,
560 GetFlatBufferOutputShapeRounding(pooling2dDescriptor.m_OutputShapeRounding),
561 GetFlatBufferPaddingMethod(pooling2dDescriptor.m_PaddingMethod),
562 GetFlatBufferDataLayout(pooling2dDescriptor.m_DataLayout));
563
564 auto fbPooling2dLayer = serializer::CreatePooling2dLayer(m_flatBufferBuilder,
565 fbPooling2dBaseLayer,
566 fbPooling2dDescriptor);
567
568 CreateAnyLayer(fbPooling2dLayer.o, serializer::Layer::Layer_Pooling2dLayer);
569}
570
Sadik Armagandbb0c0c2019-02-21 09:01:41 +0000571// Build FlatBuffer for FullyConnected Layer
572void SerializerVisitor::VisitFullyConnectedLayer(const armnn::IConnectableLayer* layer,
573 const armnn::FullyConnectedDescriptor& fullyConnectedDescriptor,
574 const armnn::ConstTensor& weights,
575 const armnn::Optional<armnn::ConstTensor>& biases,
576 const char* name)
577{
578 // Create FlatBuffer BaseLayer
579 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_FullyConnected);
580
581 // Create FlatBuffer FullyConnectedDescriptor
582 auto flatBufferDescriptor =
583 serializer::CreateFullyConnectedDescriptor(m_flatBufferBuilder,
584 fullyConnectedDescriptor.m_BiasEnabled,
585 fullyConnectedDescriptor.m_TransposeWeightMatrix);
586
587 // Create FlatBuffer weights data
588 auto flatBufferWeights = CreateConstTensorInfo(weights);
589
590 // Create FlatBuffer bias data
591 flatbuffers::Offset<serializer::ConstTensor> flatBufferBiases;
592 if (fullyConnectedDescriptor.m_BiasEnabled)
593 {
594 flatBufferBiases = CreateConstTensorInfo(biases.value());
595 }
596
597 // Create FlatBuffer FullyConnectedLayer
598 auto flatBufferLayer = serializer::CreateFullyConnectedLayer(m_flatBufferBuilder,
599 flatBufferBaseLayer,
600 flatBufferDescriptor,
601 flatBufferWeights,
602 flatBufferBiases);
603
604 // Add created FullyConnectedLayer to the FlatBufferLayers
605 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_FullyConnectedLayer);
606}
607
Nattapat Chaimanowong45286992019-02-26 15:53:02 +0000608// Build FlatBuffer for SpaceToBatchNd Layer
609void SerializerVisitor::VisitSpaceToBatchNdLayer(const armnn::IConnectableLayer* layer,
610 const armnn::SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
611 const char* name)
612{
613 // Create FlatBuffer BaseLayer
614 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_SpaceToBatchNd);
615
616 std::vector<unsigned int> padList;
617 padList.reserve(spaceToBatchNdDescriptor.m_PadList.size()*2);
618 for (auto& pad : spaceToBatchNdDescriptor.m_PadList)
619 {
620 padList.push_back(pad.first);
621 padList.push_back(pad.second);
622 }
623
624 auto flatBufferDescriptor =
625 CreateSpaceToBatchNdDescriptor(m_flatBufferBuilder,
626 m_flatBufferBuilder.CreateVector(spaceToBatchNdDescriptor.m_BlockShape),
627 m_flatBufferBuilder.CreateVector(padList),
628 GetFlatBufferDataLayout(spaceToBatchNdDescriptor.m_DataLayout));
629
630 auto flatBufferLayer = serializer::CreateSpaceToBatchNdLayer(m_flatBufferBuilder,
631 flatBufferBaseLayer,
632 flatBufferDescriptor);
633
634 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_SpaceToBatchNdLayer);
635}
636
Nina Drozd57728782019-02-27 10:53:27 +0000637void SerializerVisitor::VisitNormalizationLayer(const armnn::IConnectableLayer* layer,
638 const armnn::NormalizationDescriptor& descriptor,
639 const char* name)
640{
641 auto fbNormalizationBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Normalization);
642
643 auto fbNormalizationDescriptor = serializer::CreateNormalizationDescriptor(
644 m_flatBufferBuilder,
645 GetFlatBufferNormalizationAlgorithmChannel(descriptor.m_NormChannelType),
646 GetFlatBufferNormalizationAlgorithmMethod(descriptor.m_NormMethodType),
647 descriptor.m_NormSize,
648 descriptor.m_Alpha,
649 descriptor.m_Beta,
650 descriptor.m_K,
651 GetFlatBufferDataLayout(descriptor.m_DataLayout));
652
653 auto flatBufferLayer = serializer::CreateNormalizationLayer(m_flatBufferBuilder,
654 fbNormalizationBaseLayer,
655 fbNormalizationDescriptor);
656
657 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_NormalizationLayer);
658}
659
Nattapat Chaimanowongb3485212019-03-04 12:35:39 +0000660void SerializerVisitor::VisitStridedSliceLayer(const armnn::IConnectableLayer* layer,
661 const armnn::StridedSliceDescriptor& stridedSliceDescriptor,
662 const char* name)
663{
664 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_StridedSlice);
665
666 auto flatBufferDescriptor =
667 CreateStridedSliceDescriptor(m_flatBufferBuilder,
668 m_flatBufferBuilder.CreateVector(stridedSliceDescriptor.m_Begin),
669 m_flatBufferBuilder.CreateVector(stridedSliceDescriptor.m_End),
670 m_flatBufferBuilder.CreateVector(stridedSliceDescriptor.m_Stride),
671 stridedSliceDescriptor.m_BeginMask,
672 stridedSliceDescriptor.m_EndMask,
673 stridedSliceDescriptor.m_ShrinkAxisMask,
674 stridedSliceDescriptor.m_EllipsisMask,
675 stridedSliceDescriptor.m_NewAxisMask,
676 GetFlatBufferDataLayout(stridedSliceDescriptor.m_DataLayout));
677
678 auto flatBufferLayer = serializer::CreateStridedSliceLayer(m_flatBufferBuilder,
679 flatBufferBaseLayer,
680 flatBufferDescriptor);
681
682 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_StridedSliceLayer);
683}
684
Conor Kennedyda1f9752019-03-01 14:37:12 +0000685void SerializerVisitor::VisitSubtractionLayer(const armnn::IConnectableLayer* layer, const char* name)
686{
687 auto fbSubtractionBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Subtraction);
688 auto fbSubtractionLayer = serializer::CreateSubtractionLayer(m_flatBufferBuilder, fbSubtractionBaseLayer);
689
690 CreateAnyLayer(fbSubtractionLayer.o, serializer::Layer::Layer_SubtractionLayer);
691}
692
Sadik Armagandbb0c0c2019-02-21 09:01:41 +0000693fb::Offset<serializer::LayerBase> SerializerVisitor::CreateLayerBase(const IConnectableLayer* layer,
Nattapat Chaimanowongac9cadc2019-02-13 15:52:41 +0000694 const serializer::LayerType layerType)
Mike Kelly8c1701a2019-02-11 17:01:27 +0000695{
696 std::vector<fb::Offset<serializer::InputSlot>> inputSlots = CreateInputSlots(layer);
697 std::vector<fb::Offset<serializer::OutputSlot>> outputSlots = CreateOutputSlots(layer);
698
699 return serializer::CreateLayerBase(m_flatBufferBuilder,
Saoirse Stewartcb8a3212019-02-14 15:46:10 +0000700 GetSerializedId(layer->GetGuid()),
Mike Kelly8c1701a2019-02-11 17:01:27 +0000701 m_flatBufferBuilder.CreateString(layer->GetName()),
702 layerType,
703 m_flatBufferBuilder.CreateVector(inputSlots),
704 m_flatBufferBuilder.CreateVector(outputSlots));
705}
706
Nattapat Chaimanowongac9cadc2019-02-13 15:52:41 +0000707void SerializerVisitor::CreateAnyLayer(const flatbuffers::Offset<void>& layer, const serializer::Layer serializerLayer)
Mike Kelly8c1701a2019-02-11 17:01:27 +0000708{
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000709 auto anyLayer = armnnSerializer::CreateAnyLayer(m_flatBufferBuilder, serializerLayer, layer);
Mike Kelly8c1701a2019-02-11 17:01:27 +0000710 m_serializedLayers.push_back(anyLayer);
711}
712
Mike Kellya0766c32019-02-19 17:22:07 +0000713template <typename T>
714flatbuffers::Offset<flatbuffers::Vector<T>> SerializerVisitor::CreateDataVector(const void* memory, unsigned int size)
715{
716 const T* buffer = reinterpret_cast<const T*>(memory);
717 std::vector<T> vector(buffer, buffer + (size / sizeof(T)));
718 auto fbVector = m_flatBufferBuilder.CreateVector(vector);
719 return fbVector;
720}
721
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000722flatbuffers::Offset<serializer::ConstTensor>
723 SerializerVisitor::CreateConstTensorInfo(const armnn::ConstTensor& constTensor)
Mike Kellya0766c32019-02-19 17:22:07 +0000724{
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000725 armnn::TensorInfo tensorInfo = constTensor.GetInfo();
Mike Kellya0766c32019-02-19 17:22:07 +0000726
727 // Get the dimensions
728 std::vector<unsigned int> shape;
729
730 for(unsigned int dim = 0; dim < tensorInfo.GetShape().GetNumDimensions(); ++dim)
731 {
732 shape.push_back(tensorInfo.GetShape()[dim]);
733 }
734
735 // Create FlatBuffer TensorInfo
736 auto flatBufferTensorInfo = serializer::CreateTensorInfo(m_flatBufferBuilder,
737 m_flatBufferBuilder.CreateVector(shape),
738 GetFlatBufferDataType(tensorInfo.GetDataType()),
739 tensorInfo.GetQuantizationScale(),
740 tensorInfo.GetQuantizationOffset());
741 flatbuffers::Offset<void> fbPayload;
742
743 switch (tensorInfo.GetDataType())
744 {
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000745 case armnn::DataType::Float32:
746 case armnn::DataType::Signed32:
Mike Kellya0766c32019-02-19 17:22:07 +0000747 {
748 auto fbVector = CreateDataVector<int32_t>(constTensor.GetMemoryArea(), constTensor.GetNumBytes());
749 flatbuffers::Offset<serializer::IntData> flatBuffersData = serializer::CreateIntData(
750 m_flatBufferBuilder,
751 fbVector);
752 fbPayload = flatBuffersData.o;
753 break;
754 }
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000755 case armnn::DataType::Float16:
Mike Kellya0766c32019-02-19 17:22:07 +0000756 {
757 auto fbVector = CreateDataVector<int16_t>(constTensor.GetMemoryArea(), constTensor.GetNumBytes());
758 flatbuffers::Offset<serializer::ShortData> flatBuffersData = serializer::CreateShortData(
759 m_flatBufferBuilder,
760 fbVector);
761 fbPayload = flatBuffersData.o;
762 break;
763 }
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000764 case armnn::DataType::QuantisedAsymm8:
765 case armnn::DataType::Boolean:
Mike Kellya0766c32019-02-19 17:22:07 +0000766 default:
767 {
768 auto fbVector = CreateDataVector<int8_t>(constTensor.GetMemoryArea(), constTensor.GetNumBytes());
769 flatbuffers::Offset<serializer::ByteData> flatBuffersData = serializer::CreateByteData(
770 m_flatBufferBuilder,
771 fbVector);
772 fbPayload = flatBuffersData.o;
773 }
774 }
775 flatbuffers::Offset<serializer::ConstTensor> flatBufferConstTensor = serializer::CreateConstTensor(
776 m_flatBufferBuilder,
777 flatBufferTensorInfo,
778 GetFlatBufferConstTensorData(tensorInfo.GetDataType()),
779 fbPayload);
780 return flatBufferConstTensor;
781}
782
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000783std::vector<fb::Offset<serializer::InputSlot>>
784 SerializerVisitor::CreateInputSlots(const armnn::IConnectableLayer* layer)
Mike Kelly8c1701a2019-02-11 17:01:27 +0000785{
Mike Kellya0766c32019-02-19 17:22:07 +0000786 std::vector<fb::Offset<serializer::InputSlot>> inputSlots;
Mike Kelly8c1701a2019-02-11 17:01:27 +0000787
788 // Get the InputSlots
789 for (unsigned int slotIndex = 0; slotIndex<layer->GetNumInputSlots(); ++slotIndex)
790 {
791 const IInputSlot& inputSlot = layer->GetInputSlot(slotIndex);
792
793 // Get the Connection for the InputSlot
794 const IOutputSlot* connection = inputSlot.GetConnection();
795
796 // Create FlatBuffer Connection
Saoirse Stewartcb8a3212019-02-14 15:46:10 +0000797 serializer::Connection conn(GetSerializedId(inputSlot.GetConnection()->GetOwningLayerGuid()),
798 connection->CalculateIndexOnOwner());
Mike Kelly8c1701a2019-02-11 17:01:27 +0000799 // Create FlatBuffer InputSlot
800 inputSlots.push_back(serializer::CreateInputSlot(m_flatBufferBuilder, slotIndex, &conn));
801 }
802 return inputSlots;
803}
804
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000805std::vector<fb::Offset<serializer::OutputSlot>>
806 SerializerVisitor::CreateOutputSlots(const armnn::IConnectableLayer* layer)
Mike Kelly8c1701a2019-02-11 17:01:27 +0000807{
808 std::vector<fb::Offset<serializer::OutputSlot>> outputSlots;
809
810 // Get the OutputSlots
811 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
812 {
813 const IOutputSlot& outputSlot = layer->GetOutputSlot(slotIndex);
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000814 const armnn::TensorInfo& tensorInfo = outputSlot.GetTensorInfo();
Mike Kelly8c1701a2019-02-11 17:01:27 +0000815
816 // Get the dimensions
817 std::vector<unsigned int> shape;
818 for(unsigned int dim = 0; dim < tensorInfo.GetShape().GetNumDimensions(); ++dim)
819 {
820 shape.push_back(tensorInfo.GetShape()[dim]);
821 }
822
823 // Create FlatBuffer TensorInfo
824 auto flatBufferTensorInfo = serializer::CreateTensorInfo(m_flatBufferBuilder,
825 m_flatBufferBuilder.CreateVector(shape),
826 GetFlatBufferDataType(tensorInfo.GetDataType()),
827 tensorInfo.GetQuantizationScale(),
828 tensorInfo.GetQuantizationOffset());
829
830 // Create FlatBuffer Outputslot
831 outputSlots.push_back(serializer::CreateOutputSlot(m_flatBufferBuilder,
832 slotIndex,
833 flatBufferTensorInfo));
834 }
835 return outputSlots;
836}
837
Nattapat Chaimanowongac9cadc2019-02-13 15:52:41 +0000838
839ISerializer* ISerializer::CreateRaw()
840{
841 return new Serializer();
842}
843
844ISerializerPtr ISerializer::Create()
845{
846 return ISerializerPtr(CreateRaw(), &ISerializer::Destroy);
847}
848
849void ISerializer::Destroy(ISerializer* serializer)
850{
851 delete serializer;
852}
853
854void Serializer::Serialize(const INetwork& inNetwork)
855{
856 // Iterate through to network
857 inNetwork.Accept(m_SerializerVisitor);
858 flatbuffers::FlatBufferBuilder& fbBuilder = m_SerializerVisitor.GetFlatBufferBuilder();
859
860 // Create FlatBuffer SerializedGraph
861 auto serializedGraph = serializer::CreateSerializedGraph(
862 fbBuilder,
863 fbBuilder.CreateVector(m_SerializerVisitor.GetSerializedLayers()),
864 fbBuilder.CreateVector(m_SerializerVisitor.GetInputIds()),
865 fbBuilder.CreateVector(m_SerializerVisitor.GetOutputIds()));
866
867 // Serialize the graph
868 fbBuilder.Finish(serializedGraph);
869}
870
871bool Serializer::SaveSerializedToStream(std::ostream& stream)
872{
873 flatbuffers::FlatBufferBuilder& fbBuilder = m_SerializerVisitor.GetFlatBufferBuilder();
874
Nattapat Chaimanowong7b53b692019-02-12 14:38:31 +0000875 auto bytesToWrite = boost::numeric_cast<std::streamsize>(fbBuilder.GetSize());
876 stream.write(reinterpret_cast<const char*>(fbBuilder.GetBufferPointer()), bytesToWrite);
Nattapat Chaimanowongac9cadc2019-02-13 15:52:41 +0000877 return !stream.bad();
878}
879
Matteo Martincighec333912019-02-13 15:12:39 +0000880} // namespace armnnSerializer