blob: 697e5cfaa7592df69ea8e0cc703b04217cce3380 [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#pragma once
6
7#include <armnn/ILayerVisitor.hpp>
8#include <armnn/LayerVisitorBase.hpp>
9#include <iostream>
10#include <Schema_generated.h>
11
12namespace armnnSerializer
13{
14
15class Serializer : public armnn::LayerVisitorBase<armnn::VisitorNoThrowPolicy>
16{
17public:
18 Serializer() {};
19 ~Serializer() {};
20
21 void VisitAdditionLayer(const armnn::IConnectableLayer* layer,
22 const char* name = nullptr) override;
23
24 void VisitInputLayer(const armnn::IConnectableLayer* layer,
25 armnn::LayerBindingId id,
26 const char* name = nullptr) override;
27
28 void VisitOutputLayer(const armnn::IConnectableLayer* layer,
29 armnn::LayerBindingId id,
30 const char* name = nullptr) override;
31
32 /// Serializes the network to ArmNN SerializedGraph.
33 /// @param [in] inNetwork The network to be serialized.
34 void Serialize(const armnn::INetwork& inNetwork);
35
36 /// Serializes the SerializedGraph to the stream.
37 /// @param [stream] the stream to save to
38 /// @return true if graph is Serialized to the Stream, false otherwise
39 bool SaveSerializedToStream(std::ostream& stream);
40
41private:
42
43 /// Creates the Input Slots and Output Slots and LayerBase for the layer.
44 flatbuffers::Offset<armnn::armnnSerializer::LayerBase> CreateLayerBase(
45 const armnn::IConnectableLayer* layer,
46 const armnn::armnnSerializer::LayerType layerType);
47
48 /// Creates the serializer AnyLayer for the layer and adds it to m_serializedLayers.
49 void CreateAnyLayer(const flatbuffers::Offset<void>& layer, const armnn::armnnSerializer::Layer serializerLayer);
50
51 /// Creates the serializer InputSlots for the layer.
52 std::vector<flatbuffers::Offset<armnn::armnnSerializer::InputSlot>> CreateInputSlots(
53 const armnn::IConnectableLayer* layer);
54
55 /// Creates the serializer OutputSlots for the layer.
56 std::vector<flatbuffers::Offset<armnn::armnnSerializer::OutputSlot>> CreateOutputSlots(
57 const armnn::IConnectableLayer* layer);
58
59 /// FlatBufferBuilder to create our layers' FlatBuffers.
60 flatbuffers::FlatBufferBuilder m_flatBufferBuilder;
61
62 /// AnyLayers required by the SerializedGraph.
63 std::vector<flatbuffers::Offset<armnn::armnnSerializer::AnyLayer>> m_serializedLayers;
64
65 /// Guids of all Input Layers required by the SerializedGraph.
66 std::vector<unsigned int> m_inputIds;
67
68 /// Guids of all Output Layers required by the SerializedGraph.
69 std::vector<unsigned int> m_outputIds;
70};
71
72} //namespace armnnSerializer