blob: 67e6e849e8333edcc27b0abd89f5aa7883ac8184 [file] [log] [blame]
Kevin May43a799c2019-02-08 16:31:42 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include "armnn/INetwork.hpp"
Derek Lamberti0028d1b2019-02-20 13:57:42 +00009#include "armnnDeserializer/IDeserializer.hpp"
Matthew Bentham268509a2019-02-25 13:58:24 +000010#include <ArmnnSchema_generated.h>
Kevin May43a799c2019-02-08 16:31:42 +000011
Nattapat Chaimanowongd469faf2019-03-04 17:10:40 +000012#include <unordered_map>
13
Derek Lamberti0028d1b2019-02-20 13:57:42 +000014namespace armnnDeserializer
Kevin May43a799c2019-02-08 16:31:42 +000015{
Derek Lamberti0028d1b2019-02-20 13:57:42 +000016class Deserializer : public IDeserializer
Kevin May43a799c2019-02-08 16:31:42 +000017{
18public:
19 // Shorthands for deserializer types
Derek Lamberti0028d1b2019-02-20 13:57:42 +000020 using ConstTensorRawPtr = const armnnSerializer::ConstTensor *;
21 using GraphPtr = const armnnSerializer::SerializedGraph *;
22 using TensorRawPtr = const armnnSerializer::TensorInfo *;
23 using PoolingDescriptor = const armnnSerializer::Pooling2dDescriptor *;
Nina Drozd57728782019-02-27 10:53:27 +000024 using NormalizationDescriptorPtr = const armnnSerializer::NormalizationDescriptor *;
Kevin May43a799c2019-02-08 16:31:42 +000025 using TensorRawPtrVector = std::vector<TensorRawPtr>;
Derek Lamberti0028d1b2019-02-20 13:57:42 +000026 using LayerRawPtr = const armnnSerializer::LayerBase *;
27 using LayerBaseRawPtr = const armnnSerializer::LayerBase *;
Kevin May43a799c2019-02-08 16:31:42 +000028 using LayerBaseRawPtrVector = std::vector<LayerBaseRawPtr>;
29
30public:
31
Derek Lamberti2b183fb2019-02-18 16:36:57 +000032 /// Create an input network from binary file contents
33 armnn::INetworkPtr CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent) override;
Kevin May43a799c2019-02-08 16:31:42 +000034
Derek Lamberti2b183fb2019-02-18 16:36:57 +000035 /// Create an input network from a binary input stream
36 armnn::INetworkPtr CreateNetworkFromBinary(std::istream& binaryContent) override;
Kevin May43a799c2019-02-08 16:31:42 +000037
38 /// Retrieve binding info (layer id and tensor info) for the network input identified by the given layer name
Derek Lamberti2b183fb2019-02-18 16:36:57 +000039 BindingPointInfo GetNetworkInputBindingInfo(unsigned int layerId, const std::string& name) const override;
Kevin May43a799c2019-02-08 16:31:42 +000040
41 /// Retrieve binding info (layer id and tensor info) for the network output identified by the given layer name
Derek Lamberti2b183fb2019-02-18 16:36:57 +000042 BindingPointInfo GetNetworkOutputBindingInfo(unsigned int layerId, const std::string& name) const override;
Kevin May43a799c2019-02-08 16:31:42 +000043
Derek Lamberti0028d1b2019-02-20 13:57:42 +000044 Deserializer();
45 ~Deserializer() {}
Kevin May43a799c2019-02-08 16:31:42 +000046
47public:
48 // testable helpers
Kevin May43a799c2019-02-08 16:31:42 +000049 static GraphPtr LoadGraphFromBinary(const uint8_t* binaryContent, size_t len);
50 static TensorRawPtrVector GetInputs(const GraphPtr& graph, unsigned int layerIndex);
51 static TensorRawPtrVector GetOutputs(const GraphPtr& graph, unsigned int layerIndex);
52 static LayerBaseRawPtrVector GetGraphInputs(const GraphPtr& graphPtr);
53 static LayerBaseRawPtrVector GetGraphOutputs(const GraphPtr& graphPtr);
54 static LayerBaseRawPtr GetBaseLayer(const GraphPtr& graphPtr, unsigned int layerIndex);
55 static int32_t GetBindingLayerInfo(const GraphPtr& graphPtr, unsigned int layerIndex);
Éanna Ó Catháin633f8592019-02-25 16:26:29 +000056 static std::string GetLayerName(const GraphPtr& graph, unsigned int index);
Nina Drozd57728782019-02-27 10:53:27 +000057 static armnn::Pooling2dDescriptor GetPoolingDescriptor(PoolingDescriptor pooling2dDescriptor,
58 unsigned int layerIndex);
59 static armnn::NormalizationDescriptor GetNormalizationDescriptor(
60 NormalizationDescriptorPtr normalizationDescriptor, unsigned int layerIndex);
Saoirse Stewart263829c2019-02-19 15:54:14 +000061 static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo,
62 const std::vector<uint32_t> & targetDimsIn);
Kevin May43a799c2019-02-08 16:31:42 +000063
64private:
65 // No copying allowed until it is wanted and properly implemented
Derek Lamberti0028d1b2019-02-20 13:57:42 +000066 Deserializer(const Deserializer&) = delete;
67 Deserializer& operator=(const Deserializer&) = delete;
Kevin May43a799c2019-02-08 16:31:42 +000068
69 /// Create the network from an already loaded flatbuffers graph
Derek Lamberti8ddae332019-02-21 16:29:43 +000070 armnn::INetworkPtr CreateNetworkFromGraph(GraphPtr graph);
Kevin May43a799c2019-02-08 16:31:42 +000071
72 // signature for the parser functions
Derek Lamberti8ddae332019-02-21 16:29:43 +000073 using LayerParsingFunction = void(Deserializer::*)(GraphPtr graph, unsigned int layerIndex);
Kevin May43a799c2019-02-08 16:31:42 +000074
Derek Lamberti8ddae332019-02-21 16:29:43 +000075 void ParseUnsupportedLayer(GraphPtr graph, unsigned int layerIndex);
76 void ParseActivation(GraphPtr graph, unsigned int layerIndex);
77 void ParseAdd(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong6b4ed982019-02-26 17:24:13 +000078 void ParseBatchToSpaceNd(GraphPtr graph, unsigned int layerIndex);
ruoyan018e7fa232019-02-28 15:09:07 +000079 void ParseBatchNormalization(GraphPtr graph, unsigned int layerIndex);
Conor Kennedy76277882019-02-26 08:29:54 +000080 void ParseConstant(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +000081 void ParseConvolution2d(GraphPtr graph, unsigned int layerIndex);
82 void ParseDepthwiseConvolution2d(GraphPtr graph, unsigned int layerIndex);
Éanna Ó Catháin58885892019-02-27 16:16:39 +000083 void ParseDivision(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong235cea52019-02-28 16:27:30 +000084 void ParseEqual(GraphPtr graph, unsigned int layerIndex);
Finn Williamsdd2ba7e2019-03-01 11:51:52 +000085 void ParseFloor(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +000086 void ParseFullyConnected(GraphPtr graph, unsigned int layerIndex);
Saoirse Stewarta1ed73a2019-03-04 13:40:12 +000087 void ParseGather(GraphPtr graph, unsigned int layerIndex);
Conor Kennedy79ffdf52019-03-01 14:24:54 +000088 void ParseGreater(GraphPtr graph, unsigned int layerIndex);
Narumol Prangnawarat495701f2019-03-07 17:31:34 +000089 void ParseL2Normalization(GraphPtr graph, unsigned int layerIndex);
Aron Virginas-Tar377351e2019-02-27 14:42:31 +000090 void ParseMaximum(GraphPtr graph, unsigned int layerIndex);
Sadik Armaganac97c8c2019-03-04 17:44:21 +000091 void ParseMean(GraphPtr graph, unsigned int layerIndex);
92 void ParseMinimum(GraphPtr graph, unsigned int layerIndex);
Jim Flynnac25a1b2019-02-28 10:40:49 +000093 void ParseMerger(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +000094 void ParseMultiplication(GraphPtr graph, unsigned int layerIndex);
Nina Drozd57728782019-02-27 10:53:27 +000095 void ParseNormalization(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowongebb0f9c2019-03-01 12:14:06 +000096 void ParsePad(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +000097 void ParsePermute(GraphPtr graph, unsigned int layerIndex);
98 void ParsePooling2d(GraphPtr graph, unsigned int layerIndex);
99 void ParseReshape(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong6522cdc2019-03-01 16:14:13 +0000100 void ParseResizeBilinear(GraphPtr graph, unsigned int layerIndex);
Sadik Armagan8b42a382019-03-01 14:24:49 +0000101 void ParseRsqrt(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +0000102 void ParseSoftmax(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong45286992019-02-26 15:53:02 +0000103 void ParseSpaceToBatchNd(GraphPtr graph, unsigned int layerIndex);
Jim Flynn18ce3382019-03-08 11:08:30 +0000104 void ParseSplitter(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowongb3485212019-03-04 12:35:39 +0000105 void ParseStridedSlice(GraphPtr graph, unsigned int layerIndex);
Conor Kennedyda1f9752019-03-01 14:37:12 +0000106 void ParseSubtraction(GraphPtr graph, unsigned int layerIndex);
Kevin May43a799c2019-02-08 16:31:42 +0000107
Nattapat Chaimanowongd469faf2019-03-04 17:10:40 +0000108 void RegisterOutputSlotOfConnection(uint32_t sourceLayerIndex, armnn::IOutputSlot* slot);
109 void RegisterInputSlotOfConnection(uint32_t sourceLayerIndex, uint32_t outputSlotIndex, armnn::IInputSlot* slot);
Derek Lamberti8ddae332019-02-21 16:29:43 +0000110 void RegisterInputSlots(GraphPtr graph, uint32_t layerIndex,
Kevin May43a799c2019-02-08 16:31:42 +0000111 armnn::IConnectableLayer* layer);
Derek Lamberti8ddae332019-02-21 16:29:43 +0000112 void RegisterOutputSlots(GraphPtr graph, uint32_t layerIndex,
Kevin May43a799c2019-02-08 16:31:42 +0000113 armnn::IConnectableLayer* layer);
114 void ResetParser();
115
Derek Lamberti8ddae332019-02-21 16:29:43 +0000116 void SetupInputLayers(GraphPtr graphPtr);
117 void SetupOutputLayers(GraphPtr graphPtr);
Kevin May43a799c2019-02-08 16:31:42 +0000118
119 /// The network we're building. Gets cleared after it is passed to the user
120 armnn::INetworkPtr m_Network;
Kevin May43a799c2019-02-08 16:31:42 +0000121 std::vector<LayerParsingFunction> m_ParserFunctions;
122
Derek Lamberti8ddae332019-02-21 16:29:43 +0000123 using NameToBindingInfo = std::pair<std::string, BindingPointInfo >;
124 std::vector<NameToBindingInfo> m_InputBindings;
125 std::vector<NameToBindingInfo> m_OutputBindings;
126
Kevin May43a799c2019-02-08 16:31:42 +0000127 /// A mapping of an output slot to each of the input slots it should be connected to
Nattapat Chaimanowongd469faf2019-03-04 17:10:40 +0000128 struct SlotsMap
Kevin May43a799c2019-02-08 16:31:42 +0000129 {
Nattapat Chaimanowongd469faf2019-03-04 17:10:40 +0000130 std::vector<armnn::IOutputSlot*> outputSlots;
131 std::unordered_map<unsigned int, std::vector<armnn::IInputSlot*>> inputSlots;
Kevin May43a799c2019-02-08 16:31:42 +0000132 };
Nattapat Chaimanowongd469faf2019-03-04 17:10:40 +0000133
134 typedef std::vector<SlotsMap> Connections;
135 std::vector<Connections> m_GraphConnections;
Kevin May43a799c2019-02-08 16:31:42 +0000136};
137
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000138} //namespace armnnDeserializer