blob: bd01a35431065c9c74dffcf7189684d0ab521ffc [file] [log] [blame]
Kevin May43a799c2019-02-08 16:31:42 +00001//
Finn Williams2605b232020-06-10 15:53:46 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
Kevin May43a799c2019-02-08 16:31:42 +00003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Finn Williams85d36712021-01-26 22:30:06 +00008#include <armnn/INetwork.hpp>
9#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{
Kevin May43a799c2019-02-08 16:31:42 +000016
Finn Williams85d36712021-01-26 22:30:06 +000017// Shorthands for deserializer types
18using ConstTensorRawPtr = const armnnSerializer::ConstTensor *;
19using GraphPtr = const armnnSerializer::SerializedGraph *;
20using TensorRawPtr = const armnnSerializer::TensorInfo *;
Tamas Nyirid998a1c2021-11-05 14:55:33 +000021using Pooling2dDescriptor = const armnnSerializer::Pooling2dDescriptor *;
22using Pooling3dDescriptor = const armnnSerializer::Pooling3dDescriptor *;
Finn Williams85d36712021-01-26 22:30:06 +000023using NormalizationDescriptorPtr = const armnnSerializer::NormalizationDescriptor *;
24using LstmDescriptorPtr = const armnnSerializer::LstmDescriptor *;
25using LstmInputParamsPtr = const armnnSerializer::LstmInputParams *;
26using QLstmDescriptorPtr = const armnnSerializer::QLstmDescriptor *;
27using QunatizedLstmInputParamsPtr = const armnnSerializer::QuantizedLstmInputParams *;
28using TensorRawPtrVector = std::vector<TensorRawPtr>;
29using LayerRawPtr = const armnnSerializer::LayerBase *;
30using LayerBaseRawPtr = const armnnSerializer::LayerBase *;
31using LayerBaseRawPtrVector = std::vector<LayerBaseRawPtr>;
Narumol Prangnawarata0162e12021-07-23 14:47:49 +010032using UnidirectionalSequenceLstmDescriptorPtr = const armnnSerializer::UnidirectionalSequenceLstmDescriptor *;
Finn Williams85d36712021-01-26 22:30:06 +000033
34class IDeserializer::DeserializerImpl
35{
Kevin May43a799c2019-02-08 16:31:42 +000036public:
37
Derek Lamberti2b183fb2019-02-18 16:36:57 +000038 /// Create an input network from binary file contents
Finn Williams85d36712021-01-26 22:30:06 +000039 armnn::INetworkPtr CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent);
Kevin May43a799c2019-02-08 16:31:42 +000040
Derek Lamberti2b183fb2019-02-18 16:36:57 +000041 /// Create an input network from a binary input stream
Finn Williams85d36712021-01-26 22:30:06 +000042 armnn::INetworkPtr CreateNetworkFromBinary(std::istream& binaryContent);
Kevin May43a799c2019-02-08 16:31:42 +000043
44 /// Retrieve binding info (layer id and tensor info) for the network input identified by the given layer name
Finn Williams85d36712021-01-26 22:30:06 +000045 BindingPointInfo GetNetworkInputBindingInfo(unsigned int layerId, const std::string& name) const;
Kevin May43a799c2019-02-08 16:31:42 +000046
47 /// Retrieve binding info (layer id and tensor info) for the network output identified by the given layer name
Finn Williams85d36712021-01-26 22:30:06 +000048 BindingPointInfo GetNetworkOutputBindingInfo(unsigned int layerId, const std::string& name) const;
Kevin May43a799c2019-02-08 16:31:42 +000049
Finn Williams85d36712021-01-26 22:30:06 +000050 DeserializerImpl();
51 ~DeserializerImpl() = default;
Kevin May43a799c2019-02-08 16:31:42 +000052
Finn Williams85d36712021-01-26 22:30:06 +000053 // No copying allowed until it is wanted and properly implemented
54 DeserializerImpl(const DeserializerImpl&) = delete;
55 DeserializerImpl& operator=(const DeserializerImpl&) = delete;
56
Kevin May43a799c2019-02-08 16:31:42 +000057 // testable helpers
Kevin May43a799c2019-02-08 16:31:42 +000058 static GraphPtr LoadGraphFromBinary(const uint8_t* binaryContent, size_t len);
59 static TensorRawPtrVector GetInputs(const GraphPtr& graph, unsigned int layerIndex);
60 static TensorRawPtrVector GetOutputs(const GraphPtr& graph, unsigned int layerIndex);
Kevin May43a799c2019-02-08 16:31:42 +000061 static LayerBaseRawPtr GetBaseLayer(const GraphPtr& graphPtr, unsigned int layerIndex);
62 static int32_t GetBindingLayerInfo(const GraphPtr& graphPtr, unsigned int layerIndex);
Éanna Ó Catháin633f8592019-02-25 16:26:29 +000063 static std::string GetLayerName(const GraphPtr& graph, unsigned int index);
Tamas Nyirid998a1c2021-11-05 14:55:33 +000064 static armnn::Pooling2dDescriptor GetPooling2dDescriptor(Pooling2dDescriptor pooling2dDescriptor,
65 unsigned int layerIndex);
66 static armnn::Pooling3dDescriptor GetPooling3dDescriptor(Pooling3dDescriptor pooling3dDescriptor,
Nina Drozd57728782019-02-27 10:53:27 +000067 unsigned int layerIndex);
68 static armnn::NormalizationDescriptor GetNormalizationDescriptor(
69 NormalizationDescriptorPtr normalizationDescriptor, unsigned int layerIndex);
Jim Flynn11af3752019-03-19 17:22:29 +000070 static armnn::LstmDescriptor GetLstmDescriptor(LstmDescriptorPtr lstmDescriptor);
71 static armnn::LstmInputParams GetLstmInputParams(LstmDescriptorPtr lstmDescriptor,
72 LstmInputParamsPtr lstmInputParams);
James Conroy8d333182020-05-13 10:27:58 +010073 static armnn::QLstmDescriptor GetQLstmDescriptor(QLstmDescriptorPtr qLstmDescriptorPtr);
Narumol Prangnawarata0162e12021-07-23 14:47:49 +010074 static armnn::UnidirectionalSequenceLstmDescriptor GetUnidirectionalSequenceLstmDescriptor(
75 UnidirectionalSequenceLstmDescriptorPtr descriptor);
Saoirse Stewart263829c2019-02-19 15:54:14 +000076 static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo,
77 const std::vector<uint32_t> & targetDimsIn);
Kevin May43a799c2019-02-08 16:31:42 +000078
79private:
Kevin May43a799c2019-02-08 16:31:42 +000080 /// Create the network from an already loaded flatbuffers graph
Derek Lamberti8ddae332019-02-21 16:29:43 +000081 armnn::INetworkPtr CreateNetworkFromGraph(GraphPtr graph);
Kevin May43a799c2019-02-08 16:31:42 +000082
83 // signature for the parser functions
Finn Williams85d36712021-01-26 22:30:06 +000084 using LayerParsingFunction = void(DeserializerImpl::*)(GraphPtr graph, unsigned int layerIndex);
Kevin May43a799c2019-02-08 16:31:42 +000085
Derek Lamberti8ddae332019-02-21 16:29:43 +000086 void ParseUnsupportedLayer(GraphPtr graph, unsigned int layerIndex);
FinnWilliamsArm4ffcc8f2019-09-05 14:34:20 +010087 void ParseAbs(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +000088 void ParseActivation(GraphPtr graph, unsigned int layerIndex);
89 void ParseAdd(GraphPtr graph, unsigned int layerIndex);
Narumol Prangnawarat0cfcf232019-09-09 17:16:24 +010090 void ParseArgMinMax(GraphPtr graph, unsigned int layerIndex);
Samuel Yapa04f4a12022-08-19 11:14:38 +010091 void ParseBatchMatMul(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong6b4ed982019-02-26 17:24:13 +000092 void ParseBatchToSpaceNd(GraphPtr graph, unsigned int layerIndex);
ruoyan018e7fa232019-02-28 15:09:07 +000093 void ParseBatchNormalization(GraphPtr graph, unsigned int layerIndex);
mathad01b392e982021-04-07 12:07:30 +010094 void ParseCast(GraphPtr graph, unsigned int layerIndex);
Simon Obute51f67772021-09-03 15:50:13 +010095 void ParseChannelShuffle(GraphPtr graph, unsigned int layerIndex);
Aron Virginas-Tare80ebd12019-10-17 16:11:54 +010096 void ParseComparison(GraphPtr graph, unsigned int layerIndex);
Jim Flynn906f9462019-05-10 13:55:21 +010097 void ParseConcat(GraphPtr graph, unsigned int layerIndex);
Conor Kennedy76277882019-02-26 08:29:54 +000098 void ParseConstant(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +000099 void ParseConvolution2d(GraphPtr graph, unsigned int layerIndex);
Matthew Sloyanb63a3112021-09-08 13:05:51 +0100100 void ParseConvolution3d(GraphPtr graph, unsigned int layerIndex);
Aron Virginas-Tarda9d2d32019-09-20 10:42:02 +0100101 void ParseDepthToSpace(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +0000102 void ParseDepthwiseConvolution2d(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowonge4294fd2019-03-28 09:56:53 +0000103 void ParseDequantize(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong3e14a9d2019-03-18 12:37:06 +0000104 void ParseDetectionPostProcess(GraphPtr graph, unsigned int layerIndex);
Éanna Ó Catháin58885892019-02-27 16:16:39 +0000105 void ParseDivision(GraphPtr graph, unsigned int layerIndex);
josh minor4a3c6102020-01-06 16:40:46 -0600106 void ParseElementwiseUnary(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong235cea52019-02-28 16:27:30 +0000107 void ParseEqual(GraphPtr graph, unsigned int layerIndex);
Keith Davis300ad562020-06-04 16:34:23 +0100108 void ParseFill(GraphPtr graph, unsigned int layerIndex);
Finn Williamsdd2ba7e2019-03-01 11:51:52 +0000109 void ParseFloor(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +0000110 void ParseFullyConnected(GraphPtr graph, unsigned int layerIndex);
Saoirse Stewarta1ed73a2019-03-04 13:40:12 +0000111 void ParseGather(GraphPtr graph, unsigned int layerIndex);
Teresa Charlin6966bfa2022-04-25 17:14:50 +0100112 void ParseGatherNd(GraphPtr graph, unsigned int layerIndex);
Conor Kennedy79ffdf52019-03-01 14:24:54 +0000113 void ParseGreater(GraphPtr graph, unsigned int layerIndex);
Aron Virginas-Tar781ced92019-10-03 11:15:39 +0100114 void ParseInstanceNormalization(GraphPtr graph, unsigned int layerIndex);
Narumol Prangnawarat495701f2019-03-07 17:31:34 +0000115 void ParseL2Normalization(GraphPtr graph, unsigned int layerIndex);
James Conroyaba90cd2020-11-06 16:28:18 +0000116 void ParseLogicalBinary(GraphPtr graph, unsigned int layerIndex);
Sadik Armagan26257852019-10-14 13:00:47 +0100117 void ParseLogSoftmax(GraphPtr graph, unsigned int layerIndex);
Aron Virginas-Tar377351e2019-02-27 14:42:31 +0000118 void ParseMaximum(GraphPtr graph, unsigned int layerIndex);
Sadik Armaganac97c8c2019-03-04 17:44:21 +0000119 void ParseMean(GraphPtr graph, unsigned int layerIndex);
120 void ParseMinimum(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong1f886302019-04-05 13:37:19 +0100121 void ParseMerge(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +0000122 void ParseMultiplication(GraphPtr graph, unsigned int layerIndex);
Nina Drozd57728782019-02-27 10:53:27 +0000123 void ParseNormalization(GraphPtr graph, unsigned int layerIndex);
Jim Flynn11af3752019-03-19 17:22:29 +0000124 void ParseLstm(GraphPtr graph, unsigned int layerIndex);
Jan Eilers5b01a892019-07-23 09:47:43 +0100125 void ParseQuantizedLstm(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowongebb0f9c2019-03-01 12:14:06 +0000126 void ParsePad(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +0000127 void ParsePermute(GraphPtr graph, unsigned int layerIndex);
128 void ParsePooling2d(GraphPtr graph, unsigned int layerIndex);
Tamas Nyirid998a1c2021-11-05 14:55:33 +0000129 void ParsePooling3d(GraphPtr graph, unsigned int layerIndex);
Ellen Norris-Thompson51982472019-06-19 11:46:21 +0100130 void ParsePrelu(GraphPtr graph, unsigned int layerIndex);
James Conroy8d333182020-05-13 10:27:58 +0100131 void ParseQLstm(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti87acb272019-03-27 16:51:31 +0000132 void ParseQuantize(GraphPtr graph, unsigned int layerIndex);
Finn Williams2605b232020-06-10 15:53:46 +0100133 void ParseRank(GraphPtr graph, unsigned int layerIndex);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +0000134 void ParseReduce(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +0000135 void ParseReshape(GraphPtr graph, unsigned int layerIndex);
FinnWilliamsArm6fb339a2019-06-28 15:07:10 +0100136 void ParseResize(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong6522cdc2019-03-01 16:14:13 +0000137 void ParseResizeBilinear(GraphPtr graph, unsigned int layerIndex);
Sadik Armagan8b42a382019-03-01 14:24:49 +0000138 void ParseRsqrt(GraphPtr graph, unsigned int layerIndex);
Keith Davis3ae3f972021-05-21 16:33:48 +0100139 void ParseShape(GraphPtr graph, unsigned int layerIndex);
Aron Virginas-Tar2fda80b2019-09-18 13:36:52 +0100140 void ParseSlice(GraphPtr graph, unsigned int layerIndex);
Derek Lamberti8ddae332019-02-21 16:29:43 +0000141 void ParseSoftmax(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowong45286992019-02-26 15:53:02 +0000142 void ParseSpaceToBatchNd(GraphPtr graph, unsigned int layerIndex);
Aron Virginas-Taraa067142019-06-11 16:01:44 +0100143 void ParseSpaceToDepth(GraphPtr graph, unsigned int layerIndex);
Jim Flynn18ce3382019-03-08 11:08:30 +0000144 void ParseSplitter(GraphPtr graph, unsigned int layerIndex);
Matthew Jacksonb5433ee2019-07-11 15:54:20 +0100145 void ParseStack(GraphPtr graph, unsigned int layerIndex);
Aron Virginas-Tar85121a22019-10-23 10:41:35 +0100146 void ParseStandIn(GraphPtr graph, unsigned int layerIndex);
Nattapat Chaimanowongb3485212019-03-04 12:35:39 +0000147 void ParseStridedSlice(GraphPtr graph, unsigned int layerIndex);
Conor Kennedyda1f9752019-03-01 14:37:12 +0000148 void ParseSubtraction(GraphPtr graph, unsigned int layerIndex);
Sadik Armaganeff363d2019-04-05 15:25:46 +0100149 void ParseSwitch(GraphPtr graph, unsigned int layerIndex);
Mike Kellyc9ea45a2020-02-28 18:11:58 +0000150 void ParseTranspose(GraphPtr graph, unsigned int layerIndex);
Aron Virginas-Tarcb549302019-06-21 13:53:38 +0100151 void ParseTransposeConvolution2d(GraphPtr graph, unsigned int layerIndex);
Narumol Prangnawarata0162e12021-07-23 14:47:49 +0100152 void ParseUnidirectionalSequenceLstm(GraphPtr graph, unsigned int layerIndex);
Kevin May43a799c2019-02-08 16:31:42 +0000153
Matthew Sloyan81beae32021-07-13 19:46:11 +0100154 void RegisterInputSlots(GraphPtr graph,
155 uint32_t layerIndex,
156 armnn::IConnectableLayer* layer,
157 std::vector<unsigned int> ignoreSlots = {});
158 void RegisterOutputSlots(GraphPtr graph,
159 uint32_t layerIndex,
Kevin May43a799c2019-02-08 16:31:42 +0000160 armnn::IConnectableLayer* layer);
Nattapat Chaimanowongaf000a92019-05-16 16:32:35 +0100161
162 // NOTE index here must be from flatbuffer object index property
163 void RegisterOutputSlotOfConnection(uint32_t sourceLayerIndex, uint32_t outputSlotIndex, armnn::IOutputSlot* slot);
164 void RegisterInputSlotOfConnection(uint32_t sourceLayerIndex, uint32_t outputSlotIndex, armnn::IInputSlot* slot);
165
Kevin May43a799c2019-02-08 16:31:42 +0000166 void ResetParser();
167
Derek Lamberti8ddae332019-02-21 16:29:43 +0000168 void SetupInputLayers(GraphPtr graphPtr);
169 void SetupOutputLayers(GraphPtr graphPtr);
Kevin May43a799c2019-02-08 16:31:42 +0000170
Tee Jungaa920c52019-11-05 10:48:25 +0000171 /// Helper to get the index of the layer in the flatbuffer vector from its bindingId property
172 unsigned int GetInputLayerInVector(GraphPtr graph, int targetId);
173 unsigned int GetOutputLayerInVector(GraphPtr graph, int targetId);
174
Nattapat Chaimanowongaf000a92019-05-16 16:32:35 +0100175 /// Helper to get the index of the layer in the flatbuffer vector from its index property
176 unsigned int GetLayerIndexInVector(GraphPtr graph, unsigned int index);
177
Tee Jungaa920c52019-11-05 10:48:25 +0000178 struct FeatureVersions
179 {
180 // Default values to zero for backward compatibility
181 unsigned int m_BindingIdScheme = 0;
Jan Eilers53ef7952021-06-02 12:01:25 +0100182
183 // Default values to zero for backward compatibility
184 unsigned int m_WeightsLayoutScheme = 0;
Matthew Sloyan81beae32021-07-13 19:46:11 +0100185
186 // Default values to zero for backward compatibility
187 unsigned int m_ConstTensorsAsInputs = 0;
Tee Jungaa920c52019-11-05 10:48:25 +0000188 };
189
190 FeatureVersions GetFeatureVersions(GraphPtr graph);
191
Kevin May43a799c2019-02-08 16:31:42 +0000192 /// The network we're building. Gets cleared after it is passed to the user
193 armnn::INetworkPtr m_Network;
Kevin May43a799c2019-02-08 16:31:42 +0000194 std::vector<LayerParsingFunction> m_ParserFunctions;
195
Derek Lamberti8ddae332019-02-21 16:29:43 +0000196 using NameToBindingInfo = std::pair<std::string, BindingPointInfo >;
197 std::vector<NameToBindingInfo> m_InputBindings;
198 std::vector<NameToBindingInfo> m_OutputBindings;
199
Nattapat Chaimanowongaf000a92019-05-16 16:32:35 +0100200 /// This struct describe connections for each layer
201 struct Connections
Kevin May43a799c2019-02-08 16:31:42 +0000202 {
Nattapat Chaimanowongaf000a92019-05-16 16:32:35 +0100203 // Maps output slot index (property in flatbuffer object) to IOutputSlot pointer
204 std::unordered_map<unsigned int, armnn::IOutputSlot*> outputSlots;
205
206 // Maps output slot index to IInputSlot pointer the output slot should be connected to
Nattapat Chaimanowongd469faf2019-03-04 17:10:40 +0000207 std::unordered_map<unsigned int, std::vector<armnn::IInputSlot*>> inputSlots;
Kevin May43a799c2019-02-08 16:31:42 +0000208 };
Nattapat Chaimanowongd469faf2019-03-04 17:10:40 +0000209
Nattapat Chaimanowongaf000a92019-05-16 16:32:35 +0100210 /// Maps layer index (index property in flatbuffer object) to Connections for each layer
211 std::unordered_map<unsigned int, Connections> m_GraphConnections;
Kevin May43a799c2019-02-08 16:31:42 +0000212};
213
Finn Williams85d36712021-01-26 22:30:06 +0000214} // namespace armnnDeserializer