Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 1 | // |
| 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> |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 9 | |
| 10 | #include <armnnSerializer/ISerializer.hpp> |
| 11 | |
Saoirse Stewart | cb8a321 | 2019-02-14 15:46:10 +0000 | [diff] [blame] | 12 | #include <unordered_map> |
| 13 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 14 | #include <Schema_generated.h> |
| 15 | |
| 16 | namespace armnnSerializer |
| 17 | { |
| 18 | |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 19 | class SerializerVisitor : public armnn::LayerVisitorBase<armnn::VisitorNoThrowPolicy> |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 20 | { |
| 21 | public: |
Saoirse Stewart | cb8a321 | 2019-02-14 15:46:10 +0000 | [diff] [blame] | 22 | SerializerVisitor() : m_layerId(0) {}; |
Matteo Martincigh | ec33391 | 2019-02-13 15:12:39 +0000 | [diff] [blame] | 23 | ~SerializerVisitor() {} |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 24 | |
| 25 | flatbuffers::FlatBufferBuilder& GetFlatBufferBuilder() |
| 26 | { |
| 27 | return m_flatBufferBuilder; |
| 28 | } |
| 29 | |
| 30 | std::vector<unsigned int>& GetInputIds() |
| 31 | { |
| 32 | return m_inputIds; |
| 33 | } |
| 34 | |
| 35 | std::vector<unsigned int>& GetOutputIds() |
| 36 | { |
| 37 | return m_outputIds; |
| 38 | } |
| 39 | |
| 40 | std::vector<flatbuffers::Offset<armnn::armnnSerializer::AnyLayer>>& GetSerializedLayers() |
| 41 | { |
| 42 | return m_serializedLayers; |
| 43 | } |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 44 | |
| 45 | void VisitAdditionLayer(const armnn::IConnectableLayer* layer, |
| 46 | const char* name = nullptr) override; |
| 47 | |
| 48 | void VisitInputLayer(const armnn::IConnectableLayer* layer, |
| 49 | armnn::LayerBindingId id, |
| 50 | const char* name = nullptr) override; |
| 51 | |
| 52 | void VisitOutputLayer(const armnn::IConnectableLayer* layer, |
| 53 | armnn::LayerBindingId id, |
| 54 | const char* name = nullptr) override; |
| 55 | |
Sadik Armagan | 5f45027 | 2019-02-12 14:31:45 +0000 | [diff] [blame] | 56 | void VisitMultiplicationLayer(const armnn::IConnectableLayer* layer, |
| 57 | const char* name = nullptr) override; |
| 58 | |
Aron Virginas-Tar | fc413c0 | 2019-02-13 15:41:52 +0000 | [diff] [blame] | 59 | void VisitSoftmaxLayer(const armnn::IConnectableLayer* layer, |
| 60 | const armnn::SoftmaxDescriptor& softmaxDescriptor, |
| 61 | const char* name = nullptr) override; |
| 62 | |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 63 | void VisitPooling2dLayer(const armnn::IConnectableLayer* layer, |
| 64 | const armnn::Pooling2dDescriptor& pooling2dDescriptor, |
| 65 | const char* name = nullptr) override; |
| 66 | |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame^] | 67 | void VisitReshapeLayer(const armnn::IConnectableLayer* layer, |
| 68 | const armnn::ReshapeDescriptor& reshapeDescriptor, |
| 69 | const char* name = nullptr) override; |
| 70 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 71 | private: |
| 72 | |
| 73 | /// Creates the Input Slots and Output Slots and LayerBase for the layer. |
| 74 | flatbuffers::Offset<armnn::armnnSerializer::LayerBase> CreateLayerBase( |
| 75 | const armnn::IConnectableLayer* layer, |
| 76 | const armnn::armnnSerializer::LayerType layerType); |
| 77 | |
| 78 | /// Creates the serializer AnyLayer for the layer and adds it to m_serializedLayers. |
| 79 | void CreateAnyLayer(const flatbuffers::Offset<void>& layer, const armnn::armnnSerializer::Layer serializerLayer); |
| 80 | |
Saoirse Stewart | cb8a321 | 2019-02-14 15:46:10 +0000 | [diff] [blame] | 81 | ///Function which maps Guid to an index |
| 82 | uint32_t GetSerializedId(unsigned int guid); |
| 83 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 84 | /// Creates the serializer InputSlots for the layer. |
| 85 | std::vector<flatbuffers::Offset<armnn::armnnSerializer::InputSlot>> CreateInputSlots( |
| 86 | const armnn::IConnectableLayer* layer); |
| 87 | |
| 88 | /// Creates the serializer OutputSlots for the layer. |
| 89 | std::vector<flatbuffers::Offset<armnn::armnnSerializer::OutputSlot>> CreateOutputSlots( |
| 90 | const armnn::IConnectableLayer* layer); |
| 91 | |
| 92 | /// FlatBufferBuilder to create our layers' FlatBuffers. |
| 93 | flatbuffers::FlatBufferBuilder m_flatBufferBuilder; |
| 94 | |
| 95 | /// AnyLayers required by the SerializedGraph. |
| 96 | std::vector<flatbuffers::Offset<armnn::armnnSerializer::AnyLayer>> m_serializedLayers; |
| 97 | |
| 98 | /// Guids of all Input Layers required by the SerializedGraph. |
| 99 | std::vector<unsigned int> m_inputIds; |
| 100 | |
| 101 | /// Guids of all Output Layers required by the SerializedGraph. |
| 102 | std::vector<unsigned int> m_outputIds; |
Saoirse Stewart | cb8a321 | 2019-02-14 15:46:10 +0000 | [diff] [blame] | 103 | |
| 104 | /// Mapped Guids of all Layers to match our index. |
| 105 | std::unordered_map<unsigned int, uint32_t > m_guidMap; |
| 106 | |
| 107 | /// layer within our FlatBuffer index. |
| 108 | uint32_t m_layerId; |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 111 | class Serializer : public ISerializer |
| 112 | { |
| 113 | public: |
Matteo Martincigh | ec33391 | 2019-02-13 15:12:39 +0000 | [diff] [blame] | 114 | Serializer() {} |
| 115 | ~Serializer() {} |
Nattapat Chaimanowong | ac9cadc | 2019-02-13 15:52:41 +0000 | [diff] [blame] | 116 | |
| 117 | /// Serializes the network to ArmNN SerializedGraph. |
| 118 | /// @param [in] inNetwork The network to be serialized. |
| 119 | void Serialize(const armnn::INetwork& inNetwork) override; |
| 120 | |
| 121 | /// Serializes the SerializedGraph to the stream. |
| 122 | /// @param [stream] the stream to save to |
| 123 | /// @return true if graph is Serialized to the Stream, false otherwise |
| 124 | bool SaveSerializedToStream(std::ostream& stream) override; |
| 125 | |
| 126 | private: |
| 127 | |
| 128 | /// Visitor to contruct serialized network |
| 129 | SerializerVisitor m_SerializerVisitor; |
| 130 | }; |
| 131 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 132 | } //namespace armnnSerializer |