blob: 591447de210a1733ea0d4f517748122f24c4ff84 [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 *;
Jim Flynn11af3752019-03-19 17:22:29 +000025 using LstmDescriptorPtr = const armnnSerializer::LstmDescriptor *;
26 using LstmInputParamsPtr = const armnnSerializer::LstmInputParams *;
Jan Eilers5b01a892019-07-23 09:47:43 +010027 using QunatizedLstmInputParamsPtr = const armnnSerializer::QuantizedLstmInputParams *;
Kevin May43a799c2019-02-08 16:31:42 +000028 using TensorRawPtrVector = std::vector<TensorRawPtr>;
Derek Lamberti0028d1b2019-02-20 13:57:42 +000029 using LayerRawPtr = const armnnSerializer::LayerBase *;
30 using LayerBaseRawPtr = const armnnSerializer::LayerBase *;
Kevin May43a799c2019-02-08 16:31:42 +000031 using LayerBaseRawPtrVector = std::vector<LayerBaseRawPtr>;
32
33public:
34
Derek Lamberti2b183fb2019-02-18 16:36:57 +000035 /// Create an input network from binary file contents
36 armnn::INetworkPtr CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent) override;
Kevin May43a799c2019-02-08 16:31:42 +000037
Derek Lamberti2b183fb2019-02-18 16:36:57 +000038 /// Create an input network from a binary input stream
39 armnn::INetworkPtr CreateNetworkFromBinary(std::istream& binaryContent) override;
Kevin May43a799c2019-02-08 16:31:42 +000040
41 /// 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 +000042 BindingPointInfo GetNetworkInputBindingInfo(unsigned int layerId, const std::string& name) const override;
Kevin May43a799c2019-02-08 16:31:42 +000043
44 /// 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 +000045 BindingPointInfo GetNetworkOutputBindingInfo(unsigned int layerId, const std::string& name) const override;
Kevin May43a799c2019-02-08 16:31:42 +000046
Derek Lamberti0028d1b2019-02-20 13:57:42 +000047 Deserializer();
48 ~Deserializer() {}
Kevin May43a799c2019-02-08 16:31:42 +000049
50public:
51 // testable helpers
Kevin May43a799c2019-02-08 16:31:42 +000052 static GraphPtr LoadGraphFromBinary(const uint8_t* binaryContent, size_t len);
53 static TensorRawPtrVector GetInputs(const GraphPtr& graph, unsigned int layerIndex);
54 static TensorRawPtrVector GetOutputs(const GraphPtr& graph, unsigned int layerIndex);
Kevin May43a799c2019-02-08 16:31:42 +000055 static LayerBaseRawPtr GetBaseLayer(const GraphPtr& graphPtr, unsigned int layerIndex);
56 static int32_t GetBindingLayerInfo(const GraphPtr& graphPtr, unsigned int layerIndex);
Éanna Ó Catháin633f8592019-02-25 16:26:29 +000057 static std::string GetLayerName(const GraphPtr& graph, unsigned int index);
Nina Drozd57728782019-02-27 10:53:27 +000058 static armnn::Pooling2dDescriptor GetPoolingDescriptor(PoolingDescriptor pooling2dDescriptor,
59 unsigned int layerIndex);
60 static armnn::NormalizationDescriptor GetNormalizationDescriptor(
61 NormalizationDescriptorPtr normalizationDescriptor, unsigned int layerIndex);
Jim Flynn11af3752019-03-19 17:22:29 +000062 static armnn::LstmDescriptor GetLstmDescriptor(LstmDescriptorPtr lstmDescriptor);
63 static armnn::LstmInputParams GetLstmInputParams(LstmDescriptorPtr lstmDescriptor,
64 LstmInputParamsPtr lstmInputParams);
Saoirse Stewart263829c2019-02-19 15:54:14 +000065 static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo,
66 const std::vector<uint32_t> & targetDimsIn);
Kevin May43a799c2019-02-08 16:31:42 +000067
68private:
69 // No copying allowed until it is wanted and properly implemented
Derek Lamberti0028d1b2019-02-20 13:57:42 +000070 Deserializer(const Deserializer&) = delete;
71 Deserializer& operator=(const Deserializer&) = delete;
Kevin May43a799c2019-02-08 16:31:42 +000072
73 /// Create the network from an already loaded flatbuffers graph
Derek Lamberti8ddae332019-02-21 16:29:43 +000074 armnn::INetworkPtr CreateNetworkFromGraph(GraphPtr graph);
Kevin May43a799c2019-02-08 16:31:42 +000075
76 // signature for the parser functions
Derek Lamberti8ddae332019-02-21 16:29:43 +000077 using LayerParsingFunction = void(Deserializer::*)(GraphPtr graph, unsigned int layerIndex);
Kevin May43a799c2019-02-08 16:31:42 +000078
Derek Lamberti8ddae332019-02-21 16:29:43 +000079 void ParseUnsupportedLayer(GraphPtr graph, unsigned int layerIndex);
80 void ParseActivation(GraphPtr graph, unsigned int layerIndex);
81 void ParseAdd(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong6b4ed982019-02-26 17:24:13 +000082 void ParseBatchToSpaceNd(GraphPtr graph, unsigned int layerIndex);
ruoyan018e7fa232019-02-28 15:09:07 +000083 void ParseBatchNormalization(GraphPtr graph, unsigned int layerIndex);
Jim Flynn906f9462019-05-10 13:55:21 +010084 void ParseConcat(GraphPtr graph, unsigned int layerIndex);
Conor Kennedy76277882019-02-26 08:29:54 +000085 void ParseConstant(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +000086 void ParseConvolution2d(GraphPtr graph, unsigned int layerIndex);
87 void ParseDepthwiseConvolution2d(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowonge4294fd2019-03-28 09:56:53 +000088 void ParseDequantize(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong3e14a9d2019-03-18 12:37:06 +000089 void ParseDetectionPostProcess(GraphPtr graph, unsigned int layerIndex);
Éanna Ó Catháin58885892019-02-27 16:16:39 +000090 void ParseDivision(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong235cea52019-02-28 16:27:30 +000091 void ParseEqual(GraphPtr graph, unsigned int layerIndex);
Finn Williamsdd2ba7e2019-03-01 11:51:52 +000092 void ParseFloor(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +000093 void ParseFullyConnected(GraphPtr graph, unsigned int layerIndex);
Saoirse Stewarta1ed73a2019-03-04 13:40:12 +000094 void ParseGather(GraphPtr graph, unsigned int layerIndex);
Conor Kennedy79ffdf52019-03-01 14:24:54 +000095 void ParseGreater(GraphPtr graph, unsigned int layerIndex);
Narumol Prangnawarat495701f2019-03-07 17:31:34 +000096 void ParseL2Normalization(GraphPtr graph, unsigned int layerIndex);
Aron Virginas-Tar377351e2019-02-27 14:42:31 +000097 void ParseMaximum(GraphPtr graph, unsigned int layerIndex);
Sadik Armaganac97c8c2019-03-04 17:44:21 +000098 void ParseMean(GraphPtr graph, unsigned int layerIndex);
99 void ParseMinimum(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong1f886302019-04-05 13:37:19 +0100100 void ParseMerge(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +0000101 void ParseMultiplication(GraphPtr graph, unsigned int layerIndex);
Nina Drozd57728782019-02-27 10:53:27 +0000102 void ParseNormalization(GraphPtr graph, unsigned int layerIndex);
Jim Flynn11af3752019-03-19 17:22:29 +0000103 void ParseLstm(GraphPtr graph, unsigned int layerIndex);
Jan Eilers5b01a892019-07-23 09:47:43 +0100104 void ParseQuantizedLstm(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowongebb0f9c2019-03-01 12:14:06 +0000105 void ParsePad(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +0000106 void ParsePermute(GraphPtr graph, unsigned int layerIndex);
107 void ParsePooling2d(GraphPtr graph, unsigned int layerIndex);
Ellen Norris-Thompson51982472019-06-19 11:46:21 +0100108 void ParsePrelu(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti87acb272019-03-27 16:51:31 +0000109 void ParseQuantize(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +0000110 void ParseReshape(GraphPtr graph, unsigned int layerIndex);
FinnWilliamsArm6fb339a2019-06-28 15:07:10 +0100111 void ParseResize(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong6522cdc2019-03-01 16:14:13 +0000112 void ParseResizeBilinear(GraphPtr graph, unsigned int layerIndex);
Sadik Armagan8b42a382019-03-01 14:24:49 +0000113 void ParseRsqrt(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +0000114 void ParseSoftmax(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong45286992019-02-26 15:53:02 +0000115 void ParseSpaceToBatchNd(GraphPtr graph, unsigned int layerIndex);
Aron Virginas-Taraa067142019-06-11 16:01:44 +0100116 void ParseSpaceToDepth(GraphPtr graph, unsigned int layerIndex);
Jim Flynn18ce3382019-03-08 11:08:30 +0000117 void ParseSplitter(GraphPtr graph, unsigned int layerIndex);
Matthew Jacksonb5433ee2019-07-11 15:54:20 +0100118 void ParseStack(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowongb3485212019-03-04 12:35:39 +0000119 void ParseStridedSlice(GraphPtr graph, unsigned int layerIndex);
Conor Kennedyda1f9752019-03-01 14:37:12 +0000120 void ParseSubtraction(GraphPtr graph, unsigned int layerIndex);
Sadik Armaganeff363d2019-04-05 15:25:46 +0100121 void ParseSwitch(GraphPtr graph, unsigned int layerIndex);
Aron Virginas-Tarcb549302019-06-21 13:53:38 +0100122 void ParseTransposeConvolution2d(GraphPtr graph, unsigned int layerIndex);
Kevin May43a799c2019-02-08 16:31:42 +0000123
Derek Lamberti8ddae332019-02-21 16:29:43 +0000124 void RegisterInputSlots(GraphPtr graph, uint32_t layerIndex,
Kevin May43a799c2019-02-08 16:31:42 +0000125 armnn::IConnectableLayer* layer);
Derek Lamberti8ddae332019-02-21 16:29:43 +0000126 void RegisterOutputSlots(GraphPtr graph, uint32_t layerIndex,
Kevin May43a799c2019-02-08 16:31:42 +0000127 armnn::IConnectableLayer* layer);
Nattapat Chaimanowongaf000a92019-05-16 16:32:35 +0100128
129 // NOTE index here must be from flatbuffer object index property
130 void RegisterOutputSlotOfConnection(uint32_t sourceLayerIndex, uint32_t outputSlotIndex, armnn::IOutputSlot* slot);
131 void RegisterInputSlotOfConnection(uint32_t sourceLayerIndex, uint32_t outputSlotIndex, armnn::IInputSlot* slot);
132
Kevin May43a799c2019-02-08 16:31:42 +0000133 void ResetParser();
134
Derek Lamberti8ddae332019-02-21 16:29:43 +0000135 void SetupInputLayers(GraphPtr graphPtr);
136 void SetupOutputLayers(GraphPtr graphPtr);
Kevin May43a799c2019-02-08 16:31:42 +0000137
Nattapat Chaimanowongaf000a92019-05-16 16:32:35 +0100138 /// Helper to get the index of the layer in the flatbuffer vector from its index property
139 unsigned int GetLayerIndexInVector(GraphPtr graph, unsigned int index);
140
Kevin May43a799c2019-02-08 16:31:42 +0000141 /// The network we're building. Gets cleared after it is passed to the user
142 armnn::INetworkPtr m_Network;
Kevin May43a799c2019-02-08 16:31:42 +0000143 std::vector<LayerParsingFunction> m_ParserFunctions;
144
Derek Lamberti8ddae332019-02-21 16:29:43 +0000145 using NameToBindingInfo = std::pair<std::string, BindingPointInfo >;
146 std::vector<NameToBindingInfo> m_InputBindings;
147 std::vector<NameToBindingInfo> m_OutputBindings;
148
Nattapat Chaimanowongaf000a92019-05-16 16:32:35 +0100149 /// This struct describe connections for each layer
150 struct Connections
Kevin May43a799c2019-02-08 16:31:42 +0000151 {
Nattapat Chaimanowongaf000a92019-05-16 16:32:35 +0100152 // Maps output slot index (property in flatbuffer object) to IOutputSlot pointer
153 std::unordered_map<unsigned int, armnn::IOutputSlot*> outputSlots;
154
155 // Maps output slot index to IInputSlot pointer the output slot should be connected to
Nattapat Chaimanowongd469faf2019-03-04 17:10:40 +0000156 std::unordered_map<unsigned int, std::vector<armnn::IInputSlot*>> inputSlots;
Kevin May43a799c2019-02-08 16:31:42 +0000157 };
Nattapat Chaimanowongd469faf2019-03-04 17:10:40 +0000158
Nattapat Chaimanowongaf000a92019-05-16 16:32:35 +0100159 /// Maps layer index (index property in flatbuffer object) to Connections for each layer
160 std::unordered_map<unsigned int, Connections> m_GraphConnections;
Kevin May43a799c2019-02-08 16:31:42 +0000161};
162
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000163} //namespace armnnDeserializer