2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include "Serializer.hpp"
7 #include <armnn/Descriptors.hpp>
8 #include <armnn/LstmParams.hpp>
9 #include <armnn/QuantizedLstmParams.hpp>
10 #include <armnn/utility/IgnoreUnused.hpp>
11 #include <armnn/utility/NumericCast.hpp>
15 #include <flatbuffers/util.h>
17 #include "SerializerUtils.hpp"
19 using namespace armnn;
20 namespace fb = flatbuffers;
21 namespace serializer = armnnSerializer;
23 namespace armnnSerializer
26 serializer::ActivationFunction GetFlatBufferActivationFunction(armnn::ActivationFunction function)
30 case armnn::ActivationFunction::Sigmoid:
31 return serializer::ActivationFunction::ActivationFunction_Sigmoid;
32 case armnn::ActivationFunction::TanH:
33 return serializer::ActivationFunction::ActivationFunction_TanH;
34 case armnn::ActivationFunction::Linear:
35 return serializer::ActivationFunction::ActivationFunction_Linear;
36 case armnn::ActivationFunction::ReLu:
37 return serializer::ActivationFunction::ActivationFunction_ReLu;
38 case armnn::ActivationFunction::BoundedReLu:
39 return serializer::ActivationFunction::ActivationFunction_BoundedReLu;
40 case armnn::ActivationFunction::LeakyReLu:
41 return serializer::ActivationFunction::ActivationFunction_LeakyReLu;
42 case armnn::ActivationFunction::Abs:
43 return serializer::ActivationFunction::ActivationFunction_Abs;
44 case armnn::ActivationFunction::Sqrt:
45 return serializer::ActivationFunction::ActivationFunction_Sqrt;
46 case armnn::ActivationFunction::Square:
47 return serializer::ActivationFunction::ActivationFunction_Square;
48 case armnn::ActivationFunction::Elu:
49 return serializer::ActivationFunction::ActivationFunction_Elu;
50 case armnn::ActivationFunction::HardSwish:
51 return serializer::ActivationFunction::ActivationFunction_HardSwish;
53 return serializer::ActivationFunction::ActivationFunction_Sigmoid;
57 serializer::ArgMinMaxFunction GetFlatBufferArgMinMaxFunction(armnn::ArgMinMaxFunction function)
61 case armnn::ArgMinMaxFunction::Max:
62 return serializer::ArgMinMaxFunction::ArgMinMaxFunction_Max;
63 case armnn::ArgMinMaxFunction::Min:
65 return serializer::ArgMinMaxFunction::ArgMinMaxFunction_Min;
69 uint32_t SerializerVisitor::GetSerializedId(armnn::LayerGuid guid)
71 if (m_guidMap.empty())
73 m_guidMap.insert(std::make_pair(guid, m_layerId));
75 else if (m_guidMap.find(guid) == m_guidMap.end())
78 m_guidMap.insert(std::make_pair(guid, m_layerId));
82 return m_guidMap[guid];
85 // Build FlatBuffer for Input Layer
86 void SerializerVisitor::VisitInputLayer(const armnn::IConnectableLayer* layer, LayerBindingId id, const char* name)
90 // Create FlatBuffer BaseLayer
91 auto flatBufferInputBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Input);
93 // Create FlatBuffer BindableBaseLayer
94 auto flatBufferInputBindableBaseLayer = serializer::CreateBindableLayerBase(m_flatBufferBuilder,
95 flatBufferInputBaseLayer,
97 // Push layer binding id to outputIds.
98 m_inputIds.push_back(id);
100 // Create the FlatBuffer InputLayer
101 auto flatBufferInputLayer = serializer::CreateInputLayer(m_flatBufferBuilder, flatBufferInputBindableBaseLayer);
103 // Add the AnyLayer to the FlatBufferLayers
104 CreateAnyLayer(flatBufferInputLayer.o, serializer::Layer::Layer_InputLayer);
107 // Build FlatBuffer for Output Layer
108 void SerializerVisitor::VisitOutputLayer(const armnn::IConnectableLayer* layer, LayerBindingId id, const char* name)
112 // Create FlatBuffer BaseLayer
113 auto flatBufferOutputBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Output);
115 // Create FlatBuffer BindableBaseLayer
116 auto flatBufferOutputBindableBaseLayer = serializer::CreateBindableLayerBase(m_flatBufferBuilder,
117 flatBufferOutputBaseLayer,
119 // Push layer binding id to outputIds.
120 m_outputIds.push_back(id);
122 // Create the FlatBuffer OutputLayer
123 auto flatBufferOutputLayer = serializer::CreateOutputLayer(m_flatBufferBuilder, flatBufferOutputBindableBaseLayer);
124 // Add the AnyLayer to the FlatBufferLayers
125 CreateAnyLayer(flatBufferOutputLayer.o, serializer::Layer::Layer_OutputLayer);
128 void SerializerVisitor::VisitAbsLayer(const armnn::IConnectableLayer* layer, const char* name)
131 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Abs);
132 auto flatBufferAbsLayer = serializer::CreateAbsLayer(m_flatBufferBuilder, flatBufferBaseLayer);
134 CreateAnyLayer(flatBufferAbsLayer.o, serializer::Layer::Layer_AbsLayer);
137 // Build FlatBuffer for Activation Layer
138 void SerializerVisitor::VisitActivationLayer(const armnn::IConnectableLayer* layer,
139 const armnn::ActivationDescriptor& descriptor,
144 // Create FlatBuffer BaseLayer
145 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Activation);
147 // Create the FlatBuffer ActivationDescriptor
148 auto flatBufferDescriptor = CreateActivationDescriptor(m_flatBufferBuilder,
149 GetFlatBufferActivationFunction(descriptor.m_Function),
153 // Create the FlatBuffer ActivationLayer
154 auto flatBufferAdditionLayer = CreateActivationLayer(m_flatBufferBuilder,
156 flatBufferDescriptor);
158 // Add the AnyLayer to the FlatBufferLayers
159 CreateAnyLayer(flatBufferAdditionLayer.o, serializer::Layer::Layer_ActivationLayer);
162 // Build FlatBuffer for Addition Layer
163 void SerializerVisitor::VisitAdditionLayer(const armnn::IConnectableLayer* layer, const char* name)
167 // Create FlatBuffer BaseLayer
168 auto flatBufferAdditionBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Addition);
170 // Create the FlatBuffer AdditionLayer
171 auto flatBufferAdditionLayer = serializer::CreateAdditionLayer(m_flatBufferBuilder, flatBufferAdditionBaseLayer);
173 // Add the AnyLayer to the FlatBufferLayers
174 CreateAnyLayer(flatBufferAdditionLayer.o, serializer::Layer::Layer_AdditionLayer);
177 // Build FlatBuffer for ArgMinMax Layer
178 void SerializerVisitor::VisitArgMinMaxLayer(const armnn::IConnectableLayer *layer,
179 const armnn::ArgMinMaxDescriptor& descriptor,
184 // Create FlatBuffer BaseLayer
185 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_ArgMinMax);
187 // Create FlatBuffer Descriptor
188 auto flatBufferDescriptor = CreateArgMinMaxDescriptor(m_flatBufferBuilder,
189 GetFlatBufferArgMinMaxFunction(descriptor.m_Function),
192 // Create FlatBuffer ArgMinMaxLayer
193 auto flatBufferLayer = CreateArgMinMaxLayer(m_flatBufferBuilder,
195 flatBufferDescriptor);
197 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_ArgMinMaxLayer);
200 // Build FlatBuffer for BatchToSpaceNd Layer
201 void SerializerVisitor::VisitBatchToSpaceNdLayer(const armnn::IConnectableLayer* layer,
202 const armnn::BatchToSpaceNdDescriptor& descriptor,
207 // Create FlatBuffer BaseLayer
208 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_BatchToSpaceNd);
210 std::vector<unsigned int> crops;
211 crops.reserve(descriptor.m_Crops.size() * 2);
212 for (auto& crop : descriptor.m_Crops)
214 crops.push_back(crop.first);
215 crops.push_back(crop.second);
218 auto flatBufferDescriptor =
219 CreateBatchToSpaceNdDescriptor(m_flatBufferBuilder,
220 m_flatBufferBuilder.CreateVector(descriptor.m_BlockShape),
221 m_flatBufferBuilder.CreateVector(crops),
222 GetFlatBufferDataLayout(descriptor.m_DataLayout));
224 auto flatBufferLayer = serializer::CreateBatchToSpaceNdLayer(m_flatBufferBuilder,
226 flatBufferDescriptor);
228 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_BatchToSpaceNdLayer);
231 void SerializerVisitor::VisitBatchNormalizationLayer(const armnn::IConnectableLayer* layer,
232 const armnn::BatchNormalizationDescriptor& batchNormDescriptor,
233 const armnn::ConstTensor& mean,
234 const armnn::ConstTensor& variance,
235 const armnn::ConstTensor& beta,
236 const armnn::ConstTensor& gamma,
241 auto fbBatchNormalizationBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_BatchNormalization);
242 auto fbBatchNormalizationDescriptor = serializer::CreateBatchNormalizationDescriptor(
244 batchNormDescriptor.m_Eps,
245 GetFlatBufferDataLayout(batchNormDescriptor.m_DataLayout));
247 auto fbMeanConstTensorInfo = CreateConstTensorInfo(mean);
248 auto fbVarianceConstTensorInfo = CreateConstTensorInfo(variance);
249 auto fbBetaConstTensorInfo = CreateConstTensorInfo(beta);
250 auto fbGammaConstTensorInfo = CreateConstTensorInfo(gamma);
251 auto fbBatchNormalizationLayer = serializer::CreateBatchNormalizationLayer(m_flatBufferBuilder,
252 fbBatchNormalizationBaseLayer,
253 fbBatchNormalizationDescriptor,
254 fbMeanConstTensorInfo,
255 fbVarianceConstTensorInfo,
256 fbBetaConstTensorInfo,
257 fbGammaConstTensorInfo);
259 CreateAnyLayer(fbBatchNormalizationLayer.o, serializer::Layer::Layer_BatchNormalizationLayer);
262 void SerializerVisitor::VisitComparisonLayer(const armnn::IConnectableLayer* layer,
263 const armnn::ComparisonDescriptor& descriptor,
268 auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Comparison);
269 auto fbDescriptor = serializer::CreateComparisonDescriptor(
271 GetFlatBufferComparisonOperation(descriptor.m_Operation));
273 auto fbLayer = serializer::CreateComparisonLayer(m_flatBufferBuilder, fbBaseLayer, fbDescriptor);
274 CreateAnyLayer(fbLayer.o, serializer::Layer::Layer_ComparisonLayer);
277 // Build FlatBuffer for Constant Layer
278 void SerializerVisitor::VisitConstantLayer(const armnn::IConnectableLayer* layer,
279 const armnn::ConstTensor& input,
284 // Create FlatBuffer BaseLayer
285 auto flatBufferConstantBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Constant);
287 auto flatBufferConstTensorInfo = CreateConstTensorInfo(input);
289 // Create the FlatBuffer ConstantLayer
290 auto flatBufferLayer = CreateConstantLayer(m_flatBufferBuilder,
291 flatBufferConstantBaseLayer,
292 flatBufferConstTensorInfo);
294 // Add the AnyLayer to the FlatBufferLayers
295 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_ConstantLayer);
298 // Build FlatBuffer for Convolution2dLayer
299 void SerializerVisitor::VisitConvolution2dLayer(const armnn::IConnectableLayer* layer,
300 const armnn::Convolution2dDescriptor& descriptor,
301 const armnn::ConstTensor& weights,
302 const armnn::Optional<armnn::ConstTensor>& biases,
307 // Create FlatBuffer BaseLayer
308 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Convolution2d);
310 auto flatBufferDescriptor = CreateConvolution2dDescriptor(m_flatBufferBuilder,
311 descriptor.m_PadLeft,
312 descriptor.m_PadRight,
314 descriptor.m_PadBottom,
315 descriptor.m_StrideX,
316 descriptor.m_StrideY,
317 descriptor.m_DilationX,
318 descriptor.m_DilationY,
319 descriptor.m_BiasEnabled,
320 GetFlatBufferDataLayout(descriptor.m_DataLayout));
321 auto flatBufferWeightsConstTensorInfo = CreateConstTensorInfo(weights);
322 flatbuffers::Offset<serializer::ConstTensor> flatBufferBiasesConstTensorInfo;
324 if (biases.has_value())
326 flatBufferBiasesConstTensorInfo = CreateConstTensorInfo(biases.value());
329 // Create the FlatBuffer Convolution2dLayer
330 auto flatBufferLayer = CreateConvolution2dLayer(m_flatBufferBuilder,
332 flatBufferDescriptor,
333 flatBufferWeightsConstTensorInfo,
334 flatBufferBiasesConstTensorInfo);
336 // Add the AnyLayer to the FlatBufferLayers
337 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_Convolution2dLayer);
340 void SerializerVisitor::VisitDepthToSpaceLayer(const armnn::IConnectableLayer* layer,
341 const armnn::DepthToSpaceDescriptor& descriptor,
346 auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_DepthToSpace);
347 auto fbDescriptor = CreateDepthToSpaceDescriptor(m_flatBufferBuilder,
348 descriptor.m_BlockSize,
349 GetFlatBufferDataLayout(descriptor.m_DataLayout));
351 auto fbLayer = serializer::CreateDepthToSpaceLayer(m_flatBufferBuilder, fbBaseLayer, fbDescriptor);
353 CreateAnyLayer(fbLayer.o, serializer::Layer::Layer_DepthToSpaceLayer);
356 void SerializerVisitor::VisitDepthwiseConvolution2dLayer(const armnn::IConnectableLayer* layer,
357 const armnn::DepthwiseConvolution2dDescriptor& descriptor,
358 const armnn::ConstTensor& weights,
359 const armnn::Optional<armnn::ConstTensor>& biases,
364 auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_DepthwiseConvolution2d);
365 auto fbDescriptor = CreateDepthwiseConvolution2dDescriptor(m_flatBufferBuilder,
366 descriptor.m_PadLeft,
367 descriptor.m_PadRight,
369 descriptor.m_PadBottom,
370 descriptor.m_StrideX,
371 descriptor.m_StrideY,
372 descriptor.m_DilationX,
373 descriptor.m_DilationY,
374 descriptor.m_BiasEnabled,
375 GetFlatBufferDataLayout(descriptor.m_DataLayout));
377 flatbuffers::Offset<serializer::ConstTensor> fbWeightsConstTensorInfo = CreateConstTensorInfo(weights);
378 flatbuffers::Offset<serializer::ConstTensor> fbBiasesConstTensorInfo;
379 if (biases.has_value())
381 fbBiasesConstTensorInfo = CreateConstTensorInfo(biases.value());
384 auto flatBufferLayer = CreateDepthwiseConvolution2dLayer(m_flatBufferBuilder,
387 fbWeightsConstTensorInfo,
388 fbBiasesConstTensorInfo);
390 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_DepthwiseConvolution2dLayer);
393 void SerializerVisitor::VisitDequantizeLayer(const armnn::IConnectableLayer* layer,
398 auto fbDequantizeBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Dequantize);
399 auto fbDequantizeLayer = serializer::CreateDequantizeLayer(m_flatBufferBuilder, fbDequantizeBaseLayer);
401 CreateAnyLayer(fbDequantizeLayer.o, serializer::Layer::Layer_DequantizeLayer);
404 void SerializerVisitor::VisitDetectionPostProcessLayer(const armnn::IConnectableLayer* layer,
405 const armnn::DetectionPostProcessDescriptor& descriptor,
406 const armnn::ConstTensor& anchors,
411 auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_DetectionPostProcess);
412 auto fbDescriptor = CreateDetectionPostProcessDescriptor(m_flatBufferBuilder,
413 descriptor.m_MaxDetections,
414 descriptor.m_MaxClassesPerDetection,
415 descriptor.m_DetectionsPerClass,
416 descriptor.m_NmsScoreThreshold,
417 descriptor.m_NmsIouThreshold,
418 descriptor.m_NumClasses,
419 descriptor.m_UseRegularNms,
423 descriptor.m_ScaleH);
425 flatbuffers::Offset<serializer::ConstTensor> fbAnchorsConstTensorInfo = CreateConstTensorInfo(anchors);
427 auto flatBufferLayer = CreateDetectionPostProcessLayer(m_flatBufferBuilder,
430 fbAnchorsConstTensorInfo);
432 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_DetectionPostProcessLayer);
435 void SerializerVisitor::VisitDivisionLayer(const armnn::IConnectableLayer* layer, const char* name)
439 auto fbDivisionBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Division);
440 auto fbDivisionLayer = serializer::CreateDivisionLayer(m_flatBufferBuilder, fbDivisionBaseLayer);
442 CreateAnyLayer(fbDivisionLayer.o, serializer::Layer::Layer_DivisionLayer);
445 void SerializerVisitor::VisitElementwiseUnaryLayer(const armnn::IConnectableLayer* layer,
446 const armnn::ElementwiseUnaryDescriptor& descriptor,
451 auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_ElementwiseUnary);
452 auto fbDescriptor = serializer::CreateElementwiseUnaryDescriptor(
454 GetFlatBufferUnaryOperation(descriptor.m_Operation));
456 auto fbLayer = serializer::CreateElementwiseUnaryLayer(m_flatBufferBuilder, fbBaseLayer, fbDescriptor);
457 CreateAnyLayer(fbLayer.o, serializer::Layer::Layer_ElementwiseUnaryLayer);
460 void SerializerVisitor::VisitEqualLayer(const armnn::IConnectableLayer* layer, const char* name)
464 auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Equal);
465 auto fbEqualLayer = serializer::CreateEqualLayer(m_flatBufferBuilder, fbBaseLayer);
467 CreateAnyLayer(fbEqualLayer.o, serializer::Layer::Layer_EqualLayer);
470 void SerializerVisitor::VisitFillLayer(const armnn::IConnectableLayer* layer,
471 const armnn::FillDescriptor& fillDescriptor,
476 auto fbFillBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Fill);
478 auto fbDescriptor = serializer::CreateFillDescriptor(m_flatBufferBuilder, fillDescriptor.m_Value);
480 auto fbFillLayer = serializer::CreateFillLayer(m_flatBufferBuilder, fbFillBaseLayer, fbDescriptor);
482 CreateAnyLayer(fbFillLayer.o, serializer::Layer::Layer_FillLayer);
485 void SerializerVisitor::VisitFloorLayer(const armnn::IConnectableLayer *layer, const char *name)
489 auto flatBufferFloorBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Floor);
490 auto flatBufferFloorLayer = serializer::CreateFloorLayer(m_flatBufferBuilder, flatBufferFloorBaseLayer);
492 CreateAnyLayer(flatBufferFloorLayer.o, serializer::Layer::Layer_FloorLayer);
495 void SerializerVisitor::VisitGatherLayer(const armnn::IConnectableLayer* layer,
498 armnn::GatherDescriptor gatherDescriptor{};
499 VisitGatherLayer(layer, gatherDescriptor, name);
502 void SerializerVisitor::VisitGatherLayer(const armnn::IConnectableLayer* layer,
503 const armnn::GatherDescriptor& gatherDescriptor,
508 auto fbGatherDescriptor = CreateGatherDescriptor(m_flatBufferBuilder,
509 gatherDescriptor.m_Axis);
510 auto fbGatherBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Gather);
511 auto flatBufferLayer = serializer::CreateGatherLayer(m_flatBufferBuilder, fbGatherBaseLayer, fbGatherDescriptor);
513 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_GatherLayer);
516 void SerializerVisitor::VisitGreaterLayer(const armnn::IConnectableLayer* layer, const char* name)
520 auto fbGreaterBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Greater);
521 auto fbGreaterLayer = serializer::CreateGreaterLayer(m_flatBufferBuilder, fbGreaterBaseLayer);
523 CreateAnyLayer(fbGreaterLayer.o, serializer::Layer::Layer_GreaterLayer);
526 void SerializerVisitor::VisitInstanceNormalizationLayer(
527 const armnn::IConnectableLayer* layer,
528 const armnn::InstanceNormalizationDescriptor& instanceNormalizationDescriptor,
533 auto fbDescriptor = serializer::CreateInstanceNormalizationDescriptor(
535 instanceNormalizationDescriptor.m_Gamma,
536 instanceNormalizationDescriptor.m_Beta,
537 instanceNormalizationDescriptor.m_Eps,
538 GetFlatBufferDataLayout(instanceNormalizationDescriptor.m_DataLayout));
540 auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_InstanceNormalization);
541 auto fbLayer = serializer::CreateInstanceNormalizationLayer(m_flatBufferBuilder, fbBaseLayer, fbDescriptor);
543 CreateAnyLayer(fbLayer.o, serializer::Layer::Layer_InstanceNormalizationLayer);
546 void SerializerVisitor::VisitL2NormalizationLayer(const armnn::IConnectableLayer* layer,
547 const armnn::L2NormalizationDescriptor& l2NormalizationDescriptor,
552 // Create FlatBuffer BaseLayer
553 auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_L2Normalization);
555 // Create the FlatBuffer L2Normalization Descriptor
556 auto fbDescriptor = serializer::CreateL2NormalizationDescriptor(
558 GetFlatBufferDataLayout(l2NormalizationDescriptor.m_DataLayout),
559 l2NormalizationDescriptor.m_Eps);
561 // Create FlatBuffer layer
562 auto fbLayer = serializer::CreateL2NormalizationLayer(m_flatBufferBuilder, fbBaseLayer, fbDescriptor);
564 CreateAnyLayer(fbLayer.o, serializer::Layer::Layer_L2NormalizationLayer);
567 void SerializerVisitor::VisitLogicalBinaryLayer(const armnn::IConnectableLayer* layer,
568 const armnn::LogicalBinaryDescriptor& descriptor,
573 auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_LogicalBinary);
574 auto fbDescriptor = serializer::CreateLogicalBinaryDescriptor(
576 GetFlatBufferLogicalBinaryOperation(descriptor.m_Operation));
578 auto fbLayer = serializer::CreateLogicalBinaryLayer(m_flatBufferBuilder, fbBaseLayer, fbDescriptor);
579 CreateAnyLayer(fbLayer.o, serializer::Layer::Layer_LogicalBinaryLayer);
582 void SerializerVisitor::VisitLogSoftmaxLayer(const armnn::IConnectableLayer* layer,
583 const armnn::LogSoftmaxDescriptor& logSoftmaxDescriptor,
588 // Create FlatBuffer BaseLayer
589 auto flatBufferLogSoftmaxBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_LogSoftmax);
591 // Create the FlatBuffer LogSoftmaxDescriptor
592 auto flatBufferLogSoftmaxDesc =
593 serializer::CreateLogSoftmaxDescriptor(m_flatBufferBuilder,
594 logSoftmaxDescriptor.m_Beta,
595 logSoftmaxDescriptor.m_Axis);
597 // Create the FlatBuffer LogSoftmaxLayer
598 auto flatBufferLogSoftmaxLayer =
599 serializer::CreateLogSoftmaxLayer(m_flatBufferBuilder,
600 flatBufferLogSoftmaxBaseLayer,
601 flatBufferLogSoftmaxDesc);
603 CreateAnyLayer(flatBufferLogSoftmaxLayer.o, serializer::Layer::Layer_LogSoftmaxLayer);
606 void SerializerVisitor::VisitLstmLayer(const armnn::IConnectableLayer* layer,
607 const armnn::LstmDescriptor& descriptor,
608 const armnn::LstmInputParams& params,
613 auto fbLstmBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Lstm);
615 auto fbLstmDescriptor = serializer::CreateLstmDescriptor(
617 descriptor.m_ActivationFunc,
618 descriptor.m_ClippingThresCell,
619 descriptor.m_ClippingThresProj,
620 descriptor.m_CifgEnabled,
621 descriptor.m_PeepholeEnabled,
622 descriptor.m_ProjectionEnabled,
623 descriptor.m_LayerNormEnabled);
625 // Get mandatory input parameters
626 auto inputToForgetWeights = CreateConstTensorInfo(*params.m_InputToForgetWeights);
627 auto inputToCellWeights = CreateConstTensorInfo(*params.m_InputToCellWeights);
628 auto inputToOutputWeights = CreateConstTensorInfo(*params.m_InputToOutputWeights);
629 auto recurrentToForgetWeights = CreateConstTensorInfo(*params.m_RecurrentToForgetWeights);
630 auto recurrentToCellWeights = CreateConstTensorInfo(*params.m_RecurrentToCellWeights);
631 auto recurrentToOutputWeights = CreateConstTensorInfo(*params.m_RecurrentToOutputWeights);
632 auto forgetGateBias = CreateConstTensorInfo(*params.m_ForgetGateBias);
633 auto cellBias = CreateConstTensorInfo(*params.m_CellBias);
634 auto outputGateBias = CreateConstTensorInfo(*params.m_OutputGateBias);
636 //Define optional parameters, these will be set depending on configuration in Lstm descriptor
637 flatbuffers::Offset<serializer::ConstTensor> inputToInputWeights;
638 flatbuffers::Offset<serializer::ConstTensor> recurrentToInputWeights;
639 flatbuffers::Offset<serializer::ConstTensor> cellToInputWeights;
640 flatbuffers::Offset<serializer::ConstTensor> inputGateBias;
641 flatbuffers::Offset<serializer::ConstTensor> projectionWeights;
642 flatbuffers::Offset<serializer::ConstTensor> projectionBias;
643 flatbuffers::Offset<serializer::ConstTensor> cellToForgetWeights;
644 flatbuffers::Offset<serializer::ConstTensor> cellToOutputWeights;
645 flatbuffers::Offset<serializer::ConstTensor> inputLayerNormWeights;
646 flatbuffers::Offset<serializer::ConstTensor> forgetLayerNormWeights;
647 flatbuffers::Offset<serializer::ConstTensor> cellLayerNormWeights;
648 flatbuffers::Offset<serializer::ConstTensor> outputLayerNormWeights;
650 if (!descriptor.m_CifgEnabled)
652 inputToInputWeights = CreateConstTensorInfo(*params.m_InputToInputWeights);
653 recurrentToInputWeights = CreateConstTensorInfo(*params.m_RecurrentToInputWeights);
654 cellToInputWeights = CreateConstTensorInfo(*params.m_CellToInputWeights);
655 inputGateBias = CreateConstTensorInfo(*params.m_InputGateBias);
658 if (descriptor.m_ProjectionEnabled)
660 projectionWeights = CreateConstTensorInfo(*params.m_ProjectionWeights);
661 projectionBias = CreateConstTensorInfo(*params.m_ProjectionBias);
664 if (descriptor.m_PeepholeEnabled)
666 cellToForgetWeights = CreateConstTensorInfo(*params.m_CellToForgetWeights);
667 cellToOutputWeights = CreateConstTensorInfo(*params.m_CellToOutputWeights);
670 if (descriptor.m_LayerNormEnabled)
672 if (!descriptor.m_CifgEnabled)
674 inputLayerNormWeights = CreateConstTensorInfo((*params.m_InputLayerNormWeights));
676 forgetLayerNormWeights = CreateConstTensorInfo(*params.m_ForgetLayerNormWeights);
677 cellLayerNormWeights = CreateConstTensorInfo(*params.m_CellLayerNormWeights);
678 outputLayerNormWeights = CreateConstTensorInfo(*params.m_OutputLayerNormWeights);
681 auto fbLstmParams = serializer::CreateLstmInputParams(
683 inputToForgetWeights,
685 inputToOutputWeights,
686 recurrentToForgetWeights,
687 recurrentToCellWeights,
688 recurrentToOutputWeights,
693 recurrentToInputWeights,
700 inputLayerNormWeights,
701 forgetLayerNormWeights,
702 cellLayerNormWeights,
703 outputLayerNormWeights);
705 auto fbLstmLayer = serializer::CreateLstmLayer(
711 CreateAnyLayer(fbLstmLayer.o, serializer::Layer::Layer_LstmLayer);
714 void SerializerVisitor::VisitMaximumLayer(const armnn::IConnectableLayer* layer, const char* name)
718 auto fbMaximumBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Maximum);
719 auto fbMaximumLayer = serializer::CreateMaximumLayer(m_flatBufferBuilder, fbMaximumBaseLayer);
721 CreateAnyLayer(fbMaximumLayer.o, serializer::Layer::Layer_MaximumLayer);
724 void SerializerVisitor::VisitMeanLayer(const armnn::IConnectableLayer* layer,
725 const armnn::MeanDescriptor& descriptor,
730 auto fbMeanBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Mean);
731 auto fbMeanDescriptor = serializer::CreateMeanDescriptor(m_flatBufferBuilder,
732 m_flatBufferBuilder.CreateVector(descriptor.m_Axis),
733 descriptor.m_KeepDims);
735 auto fbMeanLayer = serializer::CreateMeanLayer(m_flatBufferBuilder,
739 CreateAnyLayer(fbMeanLayer.o, serializer::Layer::Layer_MeanLayer);
742 void SerializerVisitor::VisitMinimumLayer(const armnn::IConnectableLayer* layer, const char* name)
746 auto fbMinimumBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Minimum);
747 auto fbMinimumLayer = serializer::CreateMinimumLayer(m_flatBufferBuilder, fbMinimumBaseLayer);
749 CreateAnyLayer(fbMinimumLayer.o, serializer::Layer::Layer_MinimumLayer);
752 void SerializerVisitor::VisitMergeLayer(const armnn::IConnectableLayer* layer, const char* name)
756 auto fbMergeBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Merge);
757 auto fbMergeLayer = serializer::CreateMergeLayer(m_flatBufferBuilder, fbMergeBaseLayer);
759 CreateAnyLayer(fbMergeLayer.o, serializer::Layer::Layer_MergeLayer);
762 void SerializerVisitor::VisitMergerLayer(const armnn::IConnectableLayer* layer,
763 const armnn::MergerDescriptor& mergerDescriptor,
766 VisitConcatLayer(layer, mergerDescriptor, name);
769 void SerializerVisitor::VisitConcatLayer(const armnn::IConnectableLayer* layer,
770 const armnn::ConcatDescriptor& concatDescriptor,
775 auto flatBufferConcatBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Concat);
777 std::vector<flatbuffers::Offset<UintVector>> views;
778 for (unsigned int v = 0; v < concatDescriptor.GetNumViews(); ++v)
780 const uint32_t* origin = concatDescriptor.GetViewOrigin(v);
781 std::vector<uint32_t> origins;
782 for (unsigned int d = 0; d < concatDescriptor.GetNumDimensions(); ++d)
784 origins.push_back(origin[d]);
786 auto view = m_flatBufferBuilder.CreateVector(origins);
787 auto uintVector = CreateUintVector(m_flatBufferBuilder, view);
788 views.push_back(uintVector);
791 auto flatBufferConcatDescriptor = CreateOriginsDescriptor(m_flatBufferBuilder,
792 concatDescriptor.GetConcatAxis(),
793 concatDescriptor.GetNumViews(),
794 concatDescriptor.GetNumDimensions(),
795 m_flatBufferBuilder.CreateVector(views));
797 auto flatBufferLayer = CreateConcatLayer(m_flatBufferBuilder,
798 flatBufferConcatBaseLayer,
799 flatBufferConcatDescriptor);
801 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_ConcatLayer);
804 void SerializerVisitor::VisitMultiplicationLayer(const armnn::IConnectableLayer* layer, const char* name)
808 auto fbMultiplicationBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Multiplication);
809 auto fbMultiplicationLayer = serializer::CreateMultiplicationLayer(m_flatBufferBuilder,
810 fbMultiplicationBaseLayer);
812 CreateAnyLayer(fbMultiplicationLayer.o, serializer::Layer::Layer_MultiplicationLayer);
815 void SerializerVisitor::VisitPadLayer(const armnn::IConnectableLayer* layer,
816 const armnn::PadDescriptor& padDescriptor,
821 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Pad);
823 std::vector<unsigned int> padList;
824 for (auto& p: padDescriptor.m_PadList)
826 padList.push_back(p.first);
827 padList.push_back(p.second);
830 auto flatBufferPadDesc = serializer::CreatePadDescriptor(m_flatBufferBuilder,
831 m_flatBufferBuilder.CreateVector(padList),
832 padDescriptor.m_PadValue);
834 auto flatBufferPadLayer = serializer::CreatePadLayer(m_flatBufferBuilder,
838 CreateAnyLayer(flatBufferPadLayer.o, serializer::Layer::Layer_PadLayer);
841 void SerializerVisitor::VisitPermuteLayer(const armnn::IConnectableLayer* layer,
842 const armnn::PermuteDescriptor& permuteDescriptor,
847 // Create FlatBuffer BaseLayer
848 auto flatBufferPermuteBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Permute);
850 std::vector<unsigned int> dimMappings;
851 for (unsigned int i=0; i<permuteDescriptor.m_DimMappings.GetSize(); ++i)
853 dimMappings.push_back(permuteDescriptor.m_DimMappings[i]);
856 auto flatBufferPermuteDesc = serializer::CreatePermuteDescriptor(m_flatBufferBuilder,
857 m_flatBufferBuilder.CreateVector(dimMappings));
859 // Create the FlatBuffer PermuteLayer
860 auto flatBufferPermuteLayer = serializer::CreatePermuteLayer(m_flatBufferBuilder,
861 flatBufferPermuteBaseLayer,
862 flatBufferPermuteDesc);
864 // Add the AnyLayer to the FlatBufferLayers
865 CreateAnyLayer(flatBufferPermuteLayer.o, serializer::Layer::Layer_PermuteLayer);
868 // Build FlatBuffer for Rank Layer
869 void SerializerVisitor::VisitRankLayer(const armnn::IConnectableLayer* layer,
873 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Rank);
874 auto flatBufferRankLayer = serializer::CreateRankLayer(m_flatBufferBuilder, flatBufferBaseLayer);
876 CreateAnyLayer(flatBufferRankLayer.o, serializer::Layer::Layer_RankLayer);
878 // Build FlatBuffer for Reshape Layer
879 void SerializerVisitor::VisitReshapeLayer(const armnn::IConnectableLayer* layer,
880 const armnn::ReshapeDescriptor& reshapeDescriptor,
885 // Create FlatBuffer BaseLayer
886 auto flatBufferReshapeBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Reshape);
888 std::vector<unsigned int> targetShape;
889 for (unsigned int i =0; i < reshapeDescriptor.m_TargetShape.GetNumDimensions(); i++)
891 targetShape.push_back(reshapeDescriptor.m_TargetShape[i]);
894 auto flatBufferReshapeDesc = serializer::CreateReshapeDescriptor(m_flatBufferBuilder,
895 m_flatBufferBuilder.CreateVector(targetShape));
897 // Create the FlatBuffer ReshapeLayer
898 auto flatBufferReshapeLayer = serializer::CreateReshapeLayer(m_flatBufferBuilder, flatBufferReshapeBaseLayer,
899 flatBufferReshapeDesc);
901 // Add the AnyLayer to the FlatBufferLayers
902 CreateAnyLayer(flatBufferReshapeLayer.o, serializer::Layer::Layer_ReshapeLayer);
905 void SerializerVisitor::VisitResizeBilinearLayer(const armnn::IConnectableLayer* layer,
906 const armnn::ResizeBilinearDescriptor& resizeDescriptor,
911 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_ResizeBilinear);
913 auto flatBufferDescriptor =
914 CreateResizeBilinearDescriptor(m_flatBufferBuilder,
915 resizeDescriptor.m_TargetWidth,
916 resizeDescriptor.m_TargetHeight,
917 GetFlatBufferDataLayout(resizeDescriptor.m_DataLayout),
918 resizeDescriptor.m_AlignCorners,
919 resizeDescriptor.m_HalfPixelCenters);
921 auto flatBufferLayer = serializer::CreateResizeBilinearLayer(m_flatBufferBuilder,
923 flatBufferDescriptor);
925 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_ResizeBilinearLayer);
928 void SerializerVisitor::VisitResizeLayer(const armnn::IConnectableLayer* layer,
929 const armnn::ResizeDescriptor& resizeDescriptor,
934 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Resize);
936 auto flatBufferDescriptor =
937 CreateResizeDescriptor(m_flatBufferBuilder,
938 resizeDescriptor.m_TargetHeight,
939 resizeDescriptor.m_TargetWidth,
940 GetFlatBufferResizeMethod(resizeDescriptor.m_Method),
941 GetFlatBufferDataLayout(resizeDescriptor.m_DataLayout),
942 resizeDescriptor.m_AlignCorners,
943 resizeDescriptor.m_HalfPixelCenters);
945 auto flatBufferLayer = serializer::CreateResizeLayer(m_flatBufferBuilder,
947 flatBufferDescriptor);
949 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_ResizeLayer);
952 void SerializerVisitor::VisitRsqrtLayer(const armnn::IConnectableLayer* layer, const char* name)
956 auto fbRsqrtBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Rsqrt);
957 auto fbRsqrtLayer = serializer::CreateRsqrtLayer(m_flatBufferBuilder, fbRsqrtBaseLayer);
959 CreateAnyLayer(fbRsqrtLayer.o, serializer::Layer::Layer_RsqrtLayer);
962 void SerializerVisitor::VisitSliceLayer(const armnn::IConnectableLayer* layer,
963 const armnn::SliceDescriptor& sliceDescriptor,
968 auto fbSliceBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Slice);
969 auto fbSliceDescriptor = CreateSliceDescriptor(m_flatBufferBuilder,
970 m_flatBufferBuilder.CreateVector(sliceDescriptor.m_Begin),
971 m_flatBufferBuilder.CreateVector(sliceDescriptor.m_Size));
973 auto fbSliceLayer = serializer::CreateSliceLayer(m_flatBufferBuilder, fbSliceBaseLayer, fbSliceDescriptor);
975 CreateAnyLayer(fbSliceLayer.o, serializer::Layer::Layer_SliceLayer);
978 // Build FlatBuffer for Softmax Layer
979 void SerializerVisitor::VisitSoftmaxLayer(const armnn::IConnectableLayer* layer,
980 const armnn::SoftmaxDescriptor& softmaxDescriptor,
985 // Create FlatBuffer BaseLayer
986 auto flatBufferSoftmaxBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Softmax);
988 // Create the FlatBuffer SoftmaxDescriptor
989 auto flatBufferSoftmaxDesc =
990 serializer::CreateSoftmaxDescriptor(m_flatBufferBuilder, softmaxDescriptor.m_Beta);
992 // Create the FlatBuffer SoftmaxLayer
993 auto flatBufferSoftmaxLayer =
994 serializer::CreateSoftmaxLayer(m_flatBufferBuilder,
995 flatBufferSoftmaxBaseLayer,
996 flatBufferSoftmaxDesc);
998 CreateAnyLayer(flatBufferSoftmaxLayer.o, serializer::Layer::Layer_SoftmaxLayer);
1001 void SerializerVisitor::VisitPooling2dLayer(const armnn::IConnectableLayer* layer,
1002 const armnn::Pooling2dDescriptor& pooling2dDescriptor,
1007 auto fbPooling2dBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Pooling2d);
1008 auto fbPooling2dDescriptor = serializer::CreatePooling2dDescriptor(
1009 m_flatBufferBuilder,
1010 GetFlatBufferPoolingAlgorithm(pooling2dDescriptor.m_PoolType),
1011 pooling2dDescriptor.m_PadLeft,
1012 pooling2dDescriptor.m_PadRight,
1013 pooling2dDescriptor.m_PadTop,
1014 pooling2dDescriptor.m_PadBottom,
1015 pooling2dDescriptor.m_PoolWidth,
1016 pooling2dDescriptor.m_PoolHeight,
1017 pooling2dDescriptor.m_StrideX,
1018 pooling2dDescriptor.m_StrideY,
1019 GetFlatBufferOutputShapeRounding(pooling2dDescriptor.m_OutputShapeRounding),
1020 GetFlatBufferPaddingMethod(pooling2dDescriptor.m_PaddingMethod),
1021 GetFlatBufferDataLayout(pooling2dDescriptor.m_DataLayout));
1023 auto fbPooling2dLayer = serializer::CreatePooling2dLayer(m_flatBufferBuilder,
1024 fbPooling2dBaseLayer,
1025 fbPooling2dDescriptor);
1027 CreateAnyLayer(fbPooling2dLayer.o, serializer::Layer::Layer_Pooling2dLayer);
1030 void SerializerVisitor::VisitPreluLayer(const armnn::IConnectableLayer* layer,
1035 // Create FlatBuffer BaseLayer
1036 auto flatBufferPreluBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Prelu);
1038 // Create the FlatBuffer AdditionLayer
1039 auto flatBufferPreluLayer = serializer::CreatePreluLayer(m_flatBufferBuilder, flatBufferPreluBaseLayer);
1041 // Add the AnyLayer to the FlatBufferLayers
1042 CreateAnyLayer(flatBufferPreluLayer.o, serializer::Layer::Layer_PreluLayer);
1045 void SerializerVisitor::VisitQuantizeLayer(const armnn::IConnectableLayer *layer, const char *name)
1049 auto fbQuantizeBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Quantize);
1050 auto fbQuantizeLayer = serializer::CreateQuantizeLayer(m_flatBufferBuilder,
1051 fbQuantizeBaseLayer);
1052 CreateAnyLayer(fbQuantizeLayer.o, serializer::Layer::Layer_QuantizeLayer);
1055 // Build FlatBuffer for FullyConnected Layer
1056 void SerializerVisitor::VisitFullyConnectedLayer(const armnn::IConnectableLayer* layer,
1057 const armnn::FullyConnectedDescriptor& fullyConnectedDescriptor,
1058 const armnn::ConstTensor& weights,
1059 const armnn::Optional<armnn::ConstTensor>& biases,
1064 // Create FlatBuffer BaseLayer
1065 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_FullyConnected);
1067 // Create FlatBuffer FullyConnectedDescriptor
1068 auto flatBufferDescriptor =
1069 serializer::CreateFullyConnectedDescriptor(m_flatBufferBuilder,
1070 fullyConnectedDescriptor.m_BiasEnabled,
1071 fullyConnectedDescriptor.m_TransposeWeightMatrix);
1073 // Create FlatBuffer weights data
1074 auto flatBufferWeights = CreateConstTensorInfo(weights);
1076 // Create FlatBuffer bias data
1077 flatbuffers::Offset<serializer::ConstTensor> flatBufferBiases;
1078 if (fullyConnectedDescriptor.m_BiasEnabled)
1080 flatBufferBiases = CreateConstTensorInfo(biases.value());
1083 // Create FlatBuffer FullyConnectedLayer
1084 auto flatBufferLayer = serializer::CreateFullyConnectedLayer(m_flatBufferBuilder,
1085 flatBufferBaseLayer,
1086 flatBufferDescriptor,
1090 // Add created FullyConnectedLayer to the FlatBufferLayers
1091 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_FullyConnectedLayer);
1094 // Build FlatBuffer for SpaceToBatchNd Layer
1095 void SerializerVisitor::VisitSpaceToBatchNdLayer(const armnn::IConnectableLayer* layer,
1096 const armnn::SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
1101 // Create FlatBuffer BaseLayer
1102 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_SpaceToBatchNd);
1104 std::vector<unsigned int> padList;
1105 padList.reserve(spaceToBatchNdDescriptor.m_PadList.size()*2);
1106 for (auto& pad : spaceToBatchNdDescriptor.m_PadList)
1108 padList.push_back(pad.first);
1109 padList.push_back(pad.second);
1112 auto flatBufferDescriptor =
1113 CreateSpaceToBatchNdDescriptor(m_flatBufferBuilder,
1114 m_flatBufferBuilder.CreateVector(spaceToBatchNdDescriptor.m_BlockShape),
1115 m_flatBufferBuilder.CreateVector(padList),
1116 GetFlatBufferDataLayout(spaceToBatchNdDescriptor.m_DataLayout));
1118 auto flatBufferLayer = serializer::CreateSpaceToBatchNdLayer(m_flatBufferBuilder,
1119 flatBufferBaseLayer,
1120 flatBufferDescriptor);
1122 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_SpaceToBatchNdLayer);
1125 // Build FlatBuffer for SpaceToDepthLayer
1126 void SerializerVisitor::VisitSpaceToDepthLayer(const armnn::IConnectableLayer* layer,
1127 const armnn::SpaceToDepthDescriptor& spaceToDepthDescriptor,
1132 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_SpaceToDepth);
1133 auto flatBufferDescriptor =
1134 CreateSpaceToDepthDescriptor(m_flatBufferBuilder,
1135 spaceToDepthDescriptor.m_BlockSize,
1136 GetFlatBufferDataLayout(spaceToDepthDescriptor.m_DataLayout));
1138 auto flatBufferLayer = serializer::CreateSpaceToDepthLayer(m_flatBufferBuilder,
1139 flatBufferBaseLayer,
1140 flatBufferDescriptor);
1142 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_SpaceToDepthLayer);
1145 // Build FlatBuffer for Splitter Layer
1146 void SerializerVisitor::VisitSplitterLayer(const armnn::IConnectableLayer* layer,
1147 const armnn::ViewsDescriptor& viewsDescriptor,
1152 // Create FlatBuffer ViewOrigins
1153 std::vector<flatbuffers::Offset<UintVector>> flatBufferViewOrigins;
1154 flatBufferViewOrigins.reserve(viewsDescriptor.GetNumViews());
1156 for(unsigned int vIdx = 0; vIdx < viewsDescriptor.GetNumViews(); ++vIdx)
1158 std::vector<uint32_t> viewOrigin;
1159 viewOrigin.reserve(viewsDescriptor.GetNumDimensions());
1162 for(unsigned int dIdx = 0; dIdx < viewsDescriptor.GetNumDimensions(); ++dIdx)
1164 viewOrigin.push_back(viewsDescriptor.GetViewOrigin(vIdx)[dIdx]);
1167 flatBufferViewOrigins.push_back(CreateUintVector(m_flatBufferBuilder,
1168 m_flatBufferBuilder.CreateVector(viewOrigin)));
1171 // Create FlatBuffer OriginsDescriptor
1172 auto flatBufferOriginDescriptor = CreateOriginsDescriptor(m_flatBufferBuilder,
1173 viewsDescriptor.GetOrigins().GetConcatAxis(),
1174 viewsDescriptor.GetOrigins().GetNumViews(),
1175 viewsDescriptor.GetOrigins().GetNumDimensions(),
1176 m_flatBufferBuilder.CreateVector(flatBufferViewOrigins));
1178 // Create FlatBuffer ViewOrigins
1179 std::vector<flatbuffers::Offset<UintVector>> flatBufferViewSizes;
1180 flatBufferViewSizes.reserve(viewsDescriptor.GetNumViews());
1182 for(unsigned int vIdx = 0; vIdx < viewsDescriptor.GetNumViews(); ++vIdx)
1184 std::vector<uint32_t> viewSize;
1185 viewSize.reserve(viewsDescriptor.GetNumDimensions());
1188 for(unsigned int dIdx = 0; dIdx < viewsDescriptor.GetNumDimensions(); ++dIdx)
1190 viewSize.push_back(viewsDescriptor.GetViewSizes(vIdx)[dIdx]);
1193 flatBufferViewSizes.push_back(CreateUintVector(m_flatBufferBuilder,
1194 m_flatBufferBuilder.CreateVector(viewSize)));
1197 // Create FlatBuffer ViewsDescriptor
1198 auto flatBufferViewsDescriptor = CreateViewsDescriptor(m_flatBufferBuilder,
1199 flatBufferOriginDescriptor,
1200 m_flatBufferBuilder.CreateVector(flatBufferViewSizes));
1202 // Create FlatBuffer BaseLayer
1203 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Splitter);
1205 auto flatBufferSplitterLayer = serializer::CreateSplitterLayer(m_flatBufferBuilder,
1206 flatBufferBaseLayer,
1207 flatBufferViewsDescriptor);
1209 CreateAnyLayer(flatBufferSplitterLayer.o, serializer::Layer::Layer_SplitterLayer);
1212 void SerializerVisitor::VisitNormalizationLayer(const armnn::IConnectableLayer* layer,
1213 const armnn::NormalizationDescriptor& descriptor,
1218 auto fbNormalizationBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Normalization);
1220 auto fbNormalizationDescriptor = serializer::CreateNormalizationDescriptor(
1221 m_flatBufferBuilder,
1222 GetFlatBufferNormalizationAlgorithmChannel(descriptor.m_NormChannelType),
1223 GetFlatBufferNormalizationAlgorithmMethod(descriptor.m_NormMethodType),
1224 descriptor.m_NormSize,
1228 GetFlatBufferDataLayout(descriptor.m_DataLayout));
1230 auto flatBufferLayer = serializer::CreateNormalizationLayer(m_flatBufferBuilder,
1231 fbNormalizationBaseLayer,
1232 fbNormalizationDescriptor);
1234 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_NormalizationLayer);
1237 void SerializerVisitor::VisitStackLayer(const armnn::IConnectableLayer* layer,
1238 const armnn::StackDescriptor& stackDescriptor,
1243 auto stackBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Stack);
1245 std::vector<unsigned int> inputShape;
1246 for (unsigned int i =0; i < stackDescriptor.m_InputShape.GetNumDimensions(); i++)
1248 inputShape.push_back(stackDescriptor.m_InputShape[i]);
1251 auto flatBufferStackDescriptor = CreateStackDescriptor(m_flatBufferBuilder,
1252 stackDescriptor.m_Axis,
1253 stackDescriptor.m_NumInputs,
1254 m_flatBufferBuilder.CreateVector(inputShape));
1256 auto stackLayer = serializer::CreateStackLayer(m_flatBufferBuilder, stackBaseLayer, flatBufferStackDescriptor);
1257 CreateAnyLayer(stackLayer.o, serializer::Layer::Layer_StackLayer);
1260 void SerializerVisitor::VisitStandInLayer(const armnn::IConnectableLayer *layer,
1261 const armnn::StandInDescriptor& standInDescriptor,
1266 auto fbDescriptor = serializer::CreateStandInDescriptor(m_flatBufferBuilder,
1267 standInDescriptor.m_NumInputs,
1268 standInDescriptor.m_NumOutputs);
1270 auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_StandIn);
1271 auto fbLayer = serializer::CreateStandInLayer(m_flatBufferBuilder, fbBaseLayer, fbDescriptor);
1273 CreateAnyLayer(fbLayer.o, serializer::Layer::Layer_StandInLayer);
1276 void SerializerVisitor::VisitStridedSliceLayer(const armnn::IConnectableLayer* layer,
1277 const armnn::StridedSliceDescriptor& stridedSliceDescriptor,
1282 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_StridedSlice);
1284 auto flatBufferDescriptor =
1285 CreateStridedSliceDescriptor(m_flatBufferBuilder,
1286 m_flatBufferBuilder.CreateVector(stridedSliceDescriptor.m_Begin),
1287 m_flatBufferBuilder.CreateVector(stridedSliceDescriptor.m_End),
1288 m_flatBufferBuilder.CreateVector(stridedSliceDescriptor.m_Stride),
1289 stridedSliceDescriptor.m_BeginMask,
1290 stridedSliceDescriptor.m_EndMask,
1291 stridedSliceDescriptor.m_ShrinkAxisMask,
1292 stridedSliceDescriptor.m_EllipsisMask,
1293 stridedSliceDescriptor.m_NewAxisMask,
1294 GetFlatBufferDataLayout(stridedSliceDescriptor.m_DataLayout));
1296 auto flatBufferLayer = serializer::CreateStridedSliceLayer(m_flatBufferBuilder,
1297 flatBufferBaseLayer,
1298 flatBufferDescriptor);
1300 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_StridedSliceLayer);
1303 void SerializerVisitor::VisitSubtractionLayer(const armnn::IConnectableLayer* layer, const char* name)
1307 auto fbSubtractionBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Subtraction);
1308 auto fbSubtractionLayer = serializer::CreateSubtractionLayer(m_flatBufferBuilder, fbSubtractionBaseLayer);
1310 CreateAnyLayer(fbSubtractionLayer.o, serializer::Layer::Layer_SubtractionLayer);
1313 void SerializerVisitor::VisitSwitchLayer(const armnn::IConnectableLayer* layer, const char* name)
1317 auto fbSwitchBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Switch);
1318 auto fbSwitchLayer = serializer::CreateSwitchLayer(m_flatBufferBuilder, fbSwitchBaseLayer);
1320 CreateAnyLayer(fbSwitchLayer.o, serializer::Layer::Layer_SwitchLayer);
1323 void SerializerVisitor::VisitTransposeConvolution2dLayer(
1324 const armnn::IConnectableLayer* layer,
1325 const armnn::TransposeConvolution2dDescriptor& descriptor,
1326 const armnn::ConstTensor& weights,
1327 const armnn::Optional<armnn::ConstTensor>& biases,
1332 auto fbBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Convolution2d);
1333 auto fbDescriptor = CreateTransposeConvolution2dDescriptor(m_flatBufferBuilder,
1334 descriptor.m_PadLeft,
1335 descriptor.m_PadRight,
1336 descriptor.m_PadTop,
1337 descriptor.m_PadBottom,
1338 descriptor.m_StrideX,
1339 descriptor.m_StrideY,
1340 descriptor.m_BiasEnabled,
1341 GetFlatBufferDataLayout(descriptor.m_DataLayout));
1344 auto fbWeightsConstTensorInfo = CreateConstTensorInfo(weights);
1345 flatbuffers::Offset<serializer::ConstTensor> fbBiasesConstTensorInfo;
1346 if (biases.has_value())
1348 fbBiasesConstTensorInfo = CreateConstTensorInfo(biases.value());
1351 auto fbLayer = CreateTransposeConvolution2dLayer(m_flatBufferBuilder,
1354 fbWeightsConstTensorInfo,
1355 fbBiasesConstTensorInfo);
1357 CreateAnyLayer(fbLayer.o, serializer::Layer::Layer_TransposeConvolution2dLayer);
1360 void SerializerVisitor::VisitTransposeLayer(const armnn::IConnectableLayer* layer,
1361 const armnn::TransposeDescriptor& descriptor,
1366 // Create FlatBuffer BaseLayer
1367 auto flatBufferBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_Transpose);
1369 std::vector<unsigned int> dimMappings;
1370 for (unsigned int i=0; i<descriptor.m_DimMappings.GetSize(); ++i)
1372 dimMappings.push_back(descriptor.m_DimMappings[i]);
1375 auto flatBufferDesc = serializer::CreateTransposeDescriptor(m_flatBufferBuilder,
1376 m_flatBufferBuilder.CreateVector(dimMappings));
1378 // Create the FlatBuffer TransposeLayer
1379 auto flatBufferLayer = serializer::CreateTransposeLayer(m_flatBufferBuilder,
1380 flatBufferBaseLayer,
1383 // Add the AnyLayer to the FlatBufferLayers
1384 CreateAnyLayer(flatBufferLayer.o, serializer::Layer::Layer_TransposeLayer);
1387 void SerializerVisitor::VisitQLstmLayer(const armnn::IConnectableLayer* layer,
1388 const armnn::QLstmDescriptor& descriptor,
1389 const armnn::LstmInputParams& params,
1394 auto fbQLstmBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_QLstm);
1396 auto fbQLstmDescriptor = serializer::CreateQLstmDescriptor(
1397 m_flatBufferBuilder,
1398 descriptor.m_CifgEnabled,
1399 descriptor.m_PeepholeEnabled,
1400 descriptor.m_ProjectionEnabled,
1401 descriptor.m_LayerNormEnabled,
1402 descriptor.m_CellClip,
1403 descriptor.m_ProjectionClip,
1404 descriptor.m_InputIntermediateScale,
1405 descriptor.m_ForgetIntermediateScale,
1406 descriptor.m_CellIntermediateScale,
1407 descriptor.m_OutputIntermediateScale,
1408 descriptor.m_HiddenStateZeroPoint,
1409 descriptor.m_HiddenStateScale
1413 auto inputToForgetWeights = CreateConstTensorInfo(*params.m_InputToForgetWeights);
1414 auto inputToCellWeights = CreateConstTensorInfo(*params.m_InputToCellWeights);
1415 auto inputToOutputWeights = CreateConstTensorInfo(*params.m_InputToOutputWeights);
1416 auto recurrentToForgetWeights = CreateConstTensorInfo(*params.m_RecurrentToForgetWeights);
1417 auto recurrentToCellWeights = CreateConstTensorInfo(*params.m_RecurrentToCellWeights);
1418 auto recurrentToOutputWeights = CreateConstTensorInfo(*params.m_RecurrentToOutputWeights);
1419 auto forgetGateBias = CreateConstTensorInfo(*params.m_ForgetGateBias);
1420 auto cellBias = CreateConstTensorInfo(*params.m_CellBias);
1421 auto outputGateBias = CreateConstTensorInfo(*params.m_OutputGateBias);
1424 flatbuffers::Offset<serializer::ConstTensor> inputToInputWeights;
1425 flatbuffers::Offset<serializer::ConstTensor> recurrentToInputWeights;
1426 flatbuffers::Offset<serializer::ConstTensor> inputGateBias;
1428 if (!descriptor.m_CifgEnabled)
1430 inputToInputWeights = CreateConstTensorInfo(*params.m_InputToInputWeights);
1431 recurrentToInputWeights = CreateConstTensorInfo(*params.m_RecurrentToInputWeights);
1432 inputGateBias = CreateConstTensorInfo(*params.m_InputGateBias);
1436 flatbuffers::Offset<serializer::ConstTensor> projectionWeights;
1437 flatbuffers::Offset<serializer::ConstTensor> projectionBias;
1439 if (descriptor.m_ProjectionEnabled)
1441 projectionWeights = CreateConstTensorInfo(*params.m_ProjectionWeights);
1442 projectionBias = CreateConstTensorInfo(*params.m_ProjectionBias);
1446 flatbuffers::Offset<serializer::ConstTensor> cellToInputWeights;
1447 flatbuffers::Offset<serializer::ConstTensor> cellToForgetWeights;
1448 flatbuffers::Offset<serializer::ConstTensor> cellToOutputWeights;
1450 if (descriptor.m_PeepholeEnabled)
1452 if (!descriptor.m_CifgEnabled)
1454 cellToInputWeights = CreateConstTensorInfo(*params.m_CellToInputWeights);
1457 cellToForgetWeights = CreateConstTensorInfo(*params.m_CellToForgetWeights);
1458 cellToOutputWeights = CreateConstTensorInfo(*params.m_CellToOutputWeights);
1462 flatbuffers::Offset<serializer::ConstTensor> inputLayerNormWeights;
1463 flatbuffers::Offset<serializer::ConstTensor> forgetLayerNormWeights;
1464 flatbuffers::Offset<serializer::ConstTensor> cellLayerNormWeights;
1465 flatbuffers::Offset<serializer::ConstTensor> outputLayerNormWeights;
1467 if (descriptor.m_LayerNormEnabled)
1469 if (!descriptor.m_CifgEnabled)
1471 inputLayerNormWeights = CreateConstTensorInfo((*params.m_InputLayerNormWeights));
1474 forgetLayerNormWeights = CreateConstTensorInfo(*params.m_ForgetLayerNormWeights);
1475 cellLayerNormWeights = CreateConstTensorInfo(*params.m_CellLayerNormWeights);
1476 outputLayerNormWeights = CreateConstTensorInfo(*params.m_OutputLayerNormWeights);
1479 auto fbQLstmParams = serializer::CreateQLstmInputParams(
1480 m_flatBufferBuilder,
1481 inputToForgetWeights,
1483 inputToOutputWeights,
1484 recurrentToForgetWeights,
1485 recurrentToCellWeights,
1486 recurrentToOutputWeights,
1490 inputToInputWeights,
1491 recurrentToInputWeights,
1496 cellToForgetWeights,
1497 cellToOutputWeights,
1498 inputLayerNormWeights,
1499 forgetLayerNormWeights,
1500 cellLayerNormWeights,
1501 outputLayerNormWeights);
1503 auto fbQLstmLayer = serializer::CreateQLstmLayer(
1504 m_flatBufferBuilder,
1509 CreateAnyLayer(fbQLstmLayer.o, serializer::Layer::Layer_QLstmLayer);
1512 void SerializerVisitor::VisitQuantizedLstmLayer(const armnn::IConnectableLayer* layer,
1513 const armnn::QuantizedLstmInputParams& params,
1518 auto fbQuantizedLstmBaseLayer = CreateLayerBase(layer, serializer::LayerType::LayerType_QuantizedLstm);
1520 // Get input parameters
1521 auto inputToInputWeights = CreateConstTensorInfo(params.GetInputToInputWeights());
1522 auto inputToForgetWeights = CreateConstTensorInfo(params.GetInputToForgetWeights());
1523 auto inputToCellWeights = CreateConstTensorInfo(params.GetInputToCellWeights());
1524 auto inputToOutputWeights = CreateConstTensorInfo(params.GetInputToOutputWeights());
1526 auto recurrentToInputWeights = CreateConstTensorInfo(params.GetRecurrentToInputWeights());
1527 auto recurrentToForgetWeights = CreateConstTensorInfo(params.GetRecurrentToForgetWeights());
1528 auto recurrentToCellWeights = CreateConstTensorInfo(params.GetRecurrentToCellWeights());
1529 auto recurrentToOutputWeights = CreateConstTensorInfo(params.GetRecurrentToOutputWeights());
1531 auto inputGateBias = CreateConstTensorInfo(params.GetInputGateBias());
1532 auto forgetGateBias = CreateConstTensorInfo(params.GetForgetGateBias());
1533 auto cellBias = CreateConstTensorInfo(params.GetCellBias());
1534 auto outputGateBias = CreateConstTensorInfo(params.GetOutputGateBias());
1536 auto fbQuantizedLstmParams = serializer::CreateQuantizedLstmInputParams(
1537 m_flatBufferBuilder,
1538 inputToInputWeights,
1539 inputToForgetWeights,
1541 inputToOutputWeights,
1542 recurrentToInputWeights,
1543 recurrentToForgetWeights,
1544 recurrentToCellWeights,
1545 recurrentToOutputWeights,
1551 auto fbQuantizedLstmLayer = serializer::CreateQuantizedLstmLayer(
1552 m_flatBufferBuilder,
1553 fbQuantizedLstmBaseLayer,
1554 fbQuantizedLstmParams);
1556 CreateAnyLayer(fbQuantizedLstmLayer.o, serializer::Layer::Layer_QuantizedLstmLayer);
1559 fb::Offset<serializer::LayerBase> SerializerVisitor::CreateLayerBase(const IConnectableLayer* layer,
1560 const serializer::LayerType layerType)
1563 uint32_t fbIndex = GetSerializedId(layer->GetGuid());
1565 std::vector<fb::Offset<serializer::InputSlot>> inputSlots = CreateInputSlots(layer);
1566 std::vector<fb::Offset<serializer::OutputSlot>> outputSlots = CreateOutputSlots(layer);
1568 return serializer::CreateLayerBase(m_flatBufferBuilder,
1570 m_flatBufferBuilder.CreateString(layer->GetName()),
1572 m_flatBufferBuilder.CreateVector(inputSlots),
1573 m_flatBufferBuilder.CreateVector(outputSlots));
1576 void SerializerVisitor::CreateAnyLayer(const flatbuffers::Offset<void>& layer, const serializer::Layer serializerLayer)
1579 auto anyLayer = armnnSerializer::CreateAnyLayer(m_flatBufferBuilder, serializerLayer, layer);
1580 m_serializedLayers.push_back(anyLayer);
1583 template <typename T>
1584 flatbuffers::Offset<flatbuffers::Vector<T>> SerializerVisitor::CreateDataVector(const void* memory, unsigned int size)
1586 const T* buffer = reinterpret_cast<const T*>(memory);
1587 std::vector<T> vector(buffer, buffer + (size / sizeof(T)));
1588 auto fbVector = m_flatBufferBuilder.CreateVector(vector);
1592 flatbuffers::Offset<TensorInfo> SerializerVisitor::CreateTensorInfo(const armnn::TensorInfo& tensorInfo)
1594 // Get the dimensions
1595 std::vector<unsigned int> shape;
1596 for(unsigned int dim = 0; dim < tensorInfo.GetShape().GetNumDimensions(); ++dim)
1598 shape.push_back(tensorInfo.GetShape()[dim]);
1601 if (tensorInfo.HasPerAxisQuantization())
1603 // Create FlatBuffer TensorInfo
1604 auto flatBufferTensorInfo =
1605 serializer::CreateTensorInfo(m_flatBufferBuilder,
1606 m_flatBufferBuilder.CreateVector(shape),
1607 GetFlatBufferDataType(tensorInfo.GetDataType()),
1608 tensorInfo.GetQuantizationScales()[0],
1609 tensorInfo.GetQuantizationOffset(),
1610 m_flatBufferBuilder.CreateVector(tensorInfo.GetQuantizationScales()),
1611 tensorInfo.GetQuantizationDim().value(),
1612 static_cast<unsigned int>
1613 (tensorInfo.GetShape().GetDimensionality()));
1614 return flatBufferTensorInfo;
1617 // Create FlatBuffer TensorInfo
1618 auto flatBufferTensorInfo = serializer::CreateTensorInfo(m_flatBufferBuilder,
1619 m_flatBufferBuilder.CreateVector(shape),
1620 GetFlatBufferDataType(tensorInfo.GetDataType()),
1621 tensorInfo.GetQuantizationScale(),
1622 tensorInfo.GetQuantizationOffset(),
1625 static_cast<unsigned int>
1626 (tensorInfo.GetShape().GetDimensionality()));
1627 return flatBufferTensorInfo;
1630 flatbuffers::Offset<serializer::ConstTensor>
1631 SerializerVisitor::CreateConstTensorInfo(const armnn::ConstTensor& constTensor)
1633 armnn::TensorInfo tensorInfo = constTensor.GetInfo();
1635 flatbuffers::Offset<void> fbPayload;
1637 switch (tensorInfo.GetDataType())
1639 case armnn::DataType::Float32:
1640 case armnn::DataType::Signed32:
1642 auto fbVector = CreateDataVector<int32_t>(constTensor.GetMemoryArea(), constTensor.GetNumBytes());
1643 flatbuffers::Offset<serializer::IntData> flatBuffersData = serializer::CreateIntData(
1644 m_flatBufferBuilder,
1646 fbPayload = flatBuffersData.o;
1649 case armnn::DataType::Float16:
1650 case armnn::DataType::BFloat16:
1651 case armnn::DataType::QSymmS16:
1653 auto fbVector = CreateDataVector<int16_t>(constTensor.GetMemoryArea(), constTensor.GetNumBytes());
1654 flatbuffers::Offset<serializer::ShortData> flatBuffersData = serializer::CreateShortData(
1655 m_flatBufferBuilder,
1657 fbPayload = flatBuffersData.o;
1660 case armnn::DataType::QSymmS8:
1661 case armnn::DataType::QAsymmS8:
1662 case armnn::DataType::QAsymmU8:
1663 case armnn::DataType::Boolean:
1666 auto fbVector = CreateDataVector<int8_t>(constTensor.GetMemoryArea(), constTensor.GetNumBytes());
1667 flatbuffers::Offset<serializer::ByteData> flatBuffersData = serializer::CreateByteData(
1668 m_flatBufferBuilder,
1670 fbPayload = flatBuffersData.o;
1673 flatbuffers::Offset<serializer::ConstTensor> flatBufferConstTensor = serializer::CreateConstTensor(
1674 m_flatBufferBuilder,
1675 CreateTensorInfo(tensorInfo),
1676 GetFlatBufferConstTensorData(tensorInfo.GetDataType()),
1678 return flatBufferConstTensor;
1681 flatbuffers::Offset<armnnSerializer::FeatureCompatibilityVersions> SerializerVisitor::GetVersionTable()
1683 flatbuffers::Offset<armnnSerializer::FeatureCompatibilityVersions> versionsTable =
1684 serializer::CreateFeatureCompatibilityVersions(
1685 m_flatBufferBuilder,
1686 1 // Binding ids scheme version
1688 return versionsTable;
1691 std::vector<fb::Offset<serializer::InputSlot>>
1692 SerializerVisitor::CreateInputSlots(const armnn::IConnectableLayer* layer)
1694 std::vector<fb::Offset<serializer::InputSlot>> inputSlots;
1696 // Get the InputSlots
1697 for (unsigned int slotIndex = 0; slotIndex<layer->GetNumInputSlots(); ++slotIndex)
1699 const IInputSlot& inputSlot = layer->GetInputSlot(slotIndex);
1701 // Get the Connection for the InputSlot
1702 const IOutputSlot* connection = inputSlot.GetConnection();
1704 // Create FlatBuffer Connection
1705 serializer::Connection conn(GetSerializedId(inputSlot.GetConnection()->GetOwningLayerGuid()),
1706 connection->CalculateIndexOnOwner());
1707 // Create FlatBuffer InputSlot
1708 inputSlots.push_back(serializer::CreateInputSlot(m_flatBufferBuilder, slotIndex, &conn));
1713 std::vector<fb::Offset<serializer::OutputSlot>>
1714 SerializerVisitor::CreateOutputSlots(const armnn::IConnectableLayer* layer)
1716 std::vector<fb::Offset<serializer::OutputSlot>> outputSlots;
1718 // Get the OutputSlots
1719 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
1721 const IOutputSlot& outputSlot = layer->GetOutputSlot(slotIndex);
1722 const armnn::TensorInfo& tensorInfo = outputSlot.GetTensorInfo();
1724 // Create FlatBuffer Outputslot
1725 outputSlots.push_back(serializer::CreateOutputSlot(m_flatBufferBuilder,
1727 CreateTensorInfo(tensorInfo)));
1733 ISerializer* ISerializer::CreateRaw()
1735 return new Serializer();
1738 ISerializerPtr ISerializer::Create()
1740 return ISerializerPtr(CreateRaw(), &ISerializer::Destroy);
1743 void ISerializer::Destroy(ISerializer* serializer)
1748 void Serializer::Serialize(const INetwork& inNetwork)
1750 // Iterate through to network
1751 inNetwork.Accept(m_SerializerVisitor);
1752 flatbuffers::FlatBufferBuilder& fbBuilder = m_SerializerVisitor.GetFlatBufferBuilder();
1754 // Create FlatBuffer SerializedGraph
1755 auto serializedGraph = serializer::CreateSerializedGraph(
1757 fbBuilder.CreateVector(m_SerializerVisitor.GetSerializedLayers()),
1758 fbBuilder.CreateVector(m_SerializerVisitor.GetInputIds()),
1759 fbBuilder.CreateVector(m_SerializerVisitor.GetOutputIds()),
1760 m_SerializerVisitor.GetVersionTable());
1762 // Serialize the graph
1763 fbBuilder.Finish(serializedGraph);
1766 bool Serializer::SaveSerializedToStream(std::ostream& stream)
1768 flatbuffers::FlatBufferBuilder& fbBuilder = m_SerializerVisitor.GetFlatBufferBuilder();
1770 auto bytesToWrite = armnn::numeric_cast<std::streamsize>(fbBuilder.GetSize());
1771 stream.write(reinterpret_cast<const char*>(fbBuilder.GetBufferPointer()), bytesToWrite);
1772 return !stream.bad();
1775 } // namespace armnnSerializer