blob: 20763c0c85da2441d530120187e12a53bc2c90c1 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
Matthew Sloyanca361232023-02-16 14:50:22 +00002// Copyright © 2017,2022-2023 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5#pragma once
6
7#include "armnnOnnxParser/IOnnxParser.hpp"
8#include "google/protobuf/repeated_field.h"
9#include <unordered_map>
10
11#include <onnx/onnx.pb.h>
12
13
14namespace armnn
15{
16class TensorInfo;
Tee Jung7ff9a602019-11-01 07:04:42 +000017enum class ActivationFunction;
telsoa01c577f2c2018-08-31 09:22:23 +010018}
19
20namespace armnnOnnxParser
21{
22
telsoa01c577f2c2018-08-31 09:22:23 +010023using ModelPtr = std::unique_ptr<onnx::ModelProto>;
24
Kevin Mayef33cb12021-01-29 14:24:57 +000025class OnnxParserImpl
telsoa01c577f2c2018-08-31 09:22:23 +010026{
27
Kevin Mayef33cb12021-01-29 14:24:57 +000028using OperationParsingFunction = void(OnnxParserImpl::*)(const onnx::NodeProto& NodeProto);
telsoa01c577f2c2018-08-31 09:22:23 +010029
30public:
31
32 using GraphPtr = std::unique_ptr<onnx::GraphProto>;
33
34 /// Create the network from a protobuf binary file on disk
Kevin Mayef33cb12021-01-29 14:24:57 +000035 armnn::INetworkPtr CreateNetworkFromBinaryFile(const char* graphFile);
telsoa01c577f2c2018-08-31 09:22:23 +010036
Narumol Prangnawarat1b11f322021-10-13 11:44:50 +010037 /// Create the network from a protobuf binary file on disk, with inputShapes specified
38 armnn::INetworkPtr CreateNetworkFromBinaryFile(const char* graphFile,
39 const std::map<std::string, armnn::TensorShape>& inputShapes);
40
Mike Kelly2ae32242022-11-25 13:55:24 +000041 /// Create the network from a protobuf binary
42 armnn::INetworkPtr CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent);
43
44 /// Create the network from a protobuf binary, with inputShapes specified
45 armnn::INetworkPtr CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent,
46 const std::map<std::string, armnn::TensorShape>& inputShapes);
47
telsoa01c577f2c2018-08-31 09:22:23 +010048 /// Create the network from a protobuf text file on disk
Kevin Mayef33cb12021-01-29 14:24:57 +000049 armnn::INetworkPtr CreateNetworkFromTextFile(const char* graphFile);
telsoa01c577f2c2018-08-31 09:22:23 +010050
Narumol Prangnawarat1b11f322021-10-13 11:44:50 +010051 /// Create the network from a protobuf text file on disk, with inputShapes specified
52 armnn::INetworkPtr CreateNetworkFromTextFile(const char* graphFile,
53 const std::map<std::string, armnn::TensorShape>& inputShapes);
54
telsoa01c577f2c2018-08-31 09:22:23 +010055 /// Create the network directly from protobuf text in a string. Useful for debugging/testing
Kevin Mayef33cb12021-01-29 14:24:57 +000056 armnn::INetworkPtr CreateNetworkFromString(const std::string& protoText);
telsoa01c577f2c2018-08-31 09:22:23 +010057
Narumol Prangnawarat1b11f322021-10-13 11:44:50 +010058 /// Create the network directly from protobuf text in a string, with inputShapes specified.
59 /// Useful for debugging/testing
60 armnn::INetworkPtr CreateNetworkFromString(const std::string& protoText,
61 const std::map<std::string, armnn::TensorShape>& inputShapes);
62
telsoa01c577f2c2018-08-31 09:22:23 +010063 /// Retrieve binding info (layer id and tensor info) for the network input identified by the given layer name
Kevin Mayef33cb12021-01-29 14:24:57 +000064 BindingPointInfo GetNetworkInputBindingInfo(const std::string& name) const;
telsoa01c577f2c2018-08-31 09:22:23 +010065
66 /// Retrieve binding info (layer id and tensor info) for the network output identified by the given layer name
Kevin Mayef33cb12021-01-29 14:24:57 +000067 BindingPointInfo GetNetworkOutputBindingInfo(const std::string& name) const;
telsoa01c577f2c2018-08-31 09:22:23 +010068
69public:
70
Kevin Mayef33cb12021-01-29 14:24:57 +000071 OnnxParserImpl();
72 ~OnnxParserImpl() = default;
telsoa01c577f2c2018-08-31 09:22:23 +010073
Mike Kelly2ae32242022-11-25 13:55:24 +000074 static ModelPtr LoadModelFromBinary(const std::vector<uint8_t>& binaryContent);
telsoa01c577f2c2018-08-31 09:22:23 +010075 static ModelPtr LoadModelFromBinaryFile(const char * fileName);
76 static ModelPtr LoadModelFromTextFile(const char * fileName);
77 static ModelPtr LoadModelFromString(const std::string& inputString);
78
Ryan OShea337c17f2020-02-21 12:33:17 +000079 /// Retrieve inputs names
telsoa01c577f2c2018-08-31 09:22:23 +010080 static std::vector<std::string> GetInputs(ModelPtr& model);
81
Ryan OShea337c17f2020-02-21 12:33:17 +000082 /// Retrieve outputs names
telsoa01c577f2c2018-08-31 09:22:23 +010083 static std::vector<std::string> GetOutputs(ModelPtr& model);
84
Matthew Sloyanac001ee2021-02-03 10:43:04 +000085 /// Retrieve version in X.Y.Z form
86 static const std::string GetVersion();
87
telsoa01c577f2c2018-08-31 09:22:23 +010088private:
89
90 /// Parses a ModelProto loaded into memory from one of the other CreateNetwork*
91 armnn::INetworkPtr CreateNetworkFromModel(onnx::ModelProto& model);
92
Ryan OShea337c17f2020-02-21 12:33:17 +000093 /// Parse every node and make the connection between the resulting tensors
telsoa01c577f2c2018-08-31 09:22:23 +010094 void LoadGraph();
95
96 void SetupInfo(const google::protobuf::RepeatedPtrField<onnx::ValueInfoProto >* list);
97
Narumol Prangnawarat452274c2021-09-23 16:12:19 +010098 std::vector<armnn::TensorInfo> ComputeOutputInfo(
99 std::vector<std::string> outNames,
100 const armnn::IConnectableLayer* layer,
101 std::vector<armnn::TensorShape> inputShapes,
102 const onnx::TensorProto::DataType& type = onnx::TensorProto::FLOAT);
telsoa01c577f2c2018-08-31 09:22:23 +0100103
104 void DetectFullyConnected();
105
106 template <typename Location>
107 void GetInputAndParam(const onnx::NodeProto& node,
108 std::string* inputName,
109 std::string* constName,
110 const Location& location);
111
112 template <typename Location>
113 void To1DTensor(const std::string &name, const Location& location);
114
115 //Broadcast Preparation functions
116 std::pair<std::string, std::string> AddPrepareBroadcast(const std::string& input0, const std::string& input1);
117 void PrependForBroadcast(const std::string& outputName, const std::string& input0, const std::string& input1);
118
Ryan OSheaed27ee72020-04-22 16:37:29 +0100119 void AddConvLayerWithDepthwiseConv(const onnx::NodeProto& node, const armnn::Convolution2dDescriptor& convDesc);
120 void AddFullyConnected(const onnx::NodeProto& matmulNode, const onnx::NodeProto* addNode = nullptr);
121 void AddPoolingLayer(const onnx::NodeProto& nodeProto, armnn::Pooling2dDescriptor& desc);
122
telsoa01c577f2c2018-08-31 09:22:23 +0100123 void CreateConstantLayer(const std::string& tensorName, const std::string& layerName);
Narumol Prangnawaratf10b15a2021-09-17 21:08:57 +0100124 void CreateInt64ConstantLayer(const std::string& tensorName, const std::string& layerName);
telsoa01c577f2c2018-08-31 09:22:23 +0100125 void CreateReshapeLayer(const std::string& inputName,
126 const std::string& outputName,
127 const std::string& layerName);
128
Tee Jung7ff9a602019-11-01 07:04:42 +0000129 void ParseActivation(const onnx::NodeProto& nodeProto, const armnn::ActivationFunction func);
Finn Williams7ee5d2c2020-03-27 11:11:50 +0000130 void ParseClip(const onnx::NodeProto& nodeProto);
Tee Jung7ff9a602019-11-01 07:04:42 +0000131 void ParseSigmoid(const onnx::NodeProto& nodeProto);
132 void ParseTanh(const onnx::NodeProto& nodeProto);
telsoa01c577f2c2018-08-31 09:22:23 +0100133 void ParseRelu(const onnx::NodeProto& nodeProto);
Tee Jung7ff9a602019-11-01 07:04:42 +0000134 void ParseLeakyRelu(const onnx::NodeProto& nodeProto);
telsoa01c577f2c2018-08-31 09:22:23 +0100135
telsoa01c577f2c2018-08-31 09:22:23 +0100136 void ParseAdd(const onnx::NodeProto& nodeProto);
Ryan OSheaed27ee72020-04-22 16:37:29 +0100137 void ParseAveragePool(const onnx::NodeProto& nodeProto);
138 void ParseBatchNormalization(const onnx::NodeProto& node);
Narumol Prangnawaratbc3bb622021-09-24 16:08:34 +0100139 void ParseConcat(const onnx::NodeProto& nodeProto);
Ryan OSheaed27ee72020-04-22 16:37:29 +0100140 void ParseConstant(const onnx::NodeProto& nodeProto);
141 void ParseConv(const onnx::NodeProto& nodeProto);
142 void ParseFlatten(const onnx::NodeProto& node);
Narumol Prangnawaratf10b15a2021-09-17 21:08:57 +0100143 void ParseGather(const onnx::NodeProto& node);
Narumol Prangnawarat1112b012021-09-30 12:10:50 +0100144 void ParseGemm(const onnx::NodeProto& node);
Ryan OSheaed27ee72020-04-22 16:37:29 +0100145 void ParseGlobalAveragePool(const onnx::NodeProto& node);
146 void ParseMaxPool(const onnx::NodeProto& nodeProto);
Narumol Prangnawaratcdc495e2021-09-16 18:13:39 +0100147 void ParseShape(const onnx::NodeProto& node);
Ryan OSheaed27ee72020-04-22 16:37:29 +0100148 void ParseReshape(const onnx::NodeProto& nodeProto);
Narumol Prangnawaratfe6aa2f2021-09-23 16:11:17 +0100149 void ParseUnsqueeze(const onnx::NodeProto& nodeProto);
telsoa01c577f2c2018-08-31 09:22:23 +0100150
Narumol Prangnawarat1112b012021-09-30 12:10:50 +0100151 void RegisterInputSlot(armnn::IConnectableLayer* layer,
152 const std::string& tensorId,
153 unsigned int slotIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100154 void RegisterInputSlots(armnn::IConnectableLayer* layer, const std::vector<std::string>& tensorIndexes);
155 void RegisterOutputSlots(armnn::IConnectableLayer* layer, const std::vector<std::string>& tensorIndexes);
156
157 void SetupInputLayers();
158 void SetupOutputLayers();
159
160 void ResetParser();
161 void Cleanup();
162
Jan Eilers53ef7952021-06-02 12:01:25 +0100163 std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
164 CreateConstTensor(const std::string name,
165 armnn::Optional<armnn::PermutationVector&> permutationVector = armnn::EmptyOptional());
telsoa01c577f2c2018-08-31 09:22:23 +0100166
Narumol Prangnawaratf10b15a2021-09-17 21:08:57 +0100167 std::pair<armnn::ConstTensor, std::unique_ptr<int32_t[]>>
168 CreateInt64ConstTensor(const std::string name,
169 armnn::Optional<armnn::PermutationVector&> permutationVector = armnn::EmptyOptional());
170
telsoa01c577f2c2018-08-31 09:22:23 +0100171 template <typename TypeList, typename Location>
172 void ValidateInputs(const onnx::NodeProto& node,
173 TypeList validInputs,
174 const Location& location);
175
176 /// The network we're building. Gets cleared after it is passed to the user
177 armnn::INetworkPtr m_Network;
178
Ryan OShea337c17f2020-02-21 12:33:17 +0000179 /// Ptr to the graph we're building the network from
telsoa01c577f2c2018-08-31 09:22:23 +0100180 GraphPtr m_Graph;
181
Ryan OShea337c17f2020-02-21 12:33:17 +0000182 /// Map of the information for every tensor
telsoa01c577f2c2018-08-31 09:22:23 +0100183 struct OnnxTensor
184 {
185 std::unique_ptr<armnn::TensorInfo> m_info;
186 std::unique_ptr<const onnx::TensorProto> m_tensor;
187 onnx::TensorProto::DataType m_dtype;
188
189 OnnxTensor() : m_info(nullptr), m_tensor(nullptr), m_dtype(onnx::TensorProto::FLOAT) { }
190 bool isConstant() { return m_tensor != nullptr; }
telsoa01c577f2c2018-08-31 09:22:23 +0100191 };
192
193 std::unordered_map<std::string, OnnxTensor> m_TensorsInfo;
194
195 /// map of onnx operation names to parsing member functions
196 static const std::map<std::string, OperationParsingFunction> m_ParserFunctions;
197
198 /// A mapping of an output slot to each of the input slots it should be connected to
Matthew Sloyanca361232023-02-16 14:50:22 +0000199 /// The outputSlot is from the layer that creates this tensor as one of its outputs
telsoa01c577f2c2018-08-31 09:22:23 +0100200 /// The inputSlots are from the layers that use this tensor as one of their inputs
201 struct TensorSlots
202 {
203 armnn::IOutputSlot* outputSlot;
204 std::vector<armnn::IInputSlot*> inputSlots;
205
206 TensorSlots() : outputSlot(nullptr) { }
207 };
Ryan OShea337c17f2020-02-21 12:33:17 +0000208 /// Map of the tensor names to their connections for the connections of the layers of the graph
telsoa01c577f2c2018-08-31 09:22:23 +0100209 std::unordered_map<std::string, TensorSlots> m_TensorConnections;
210
Ryan OShea337c17f2020-02-21 12:33:17 +0000211 /// Map of the tensor names to their node and index in graph.node()
telsoa01c577f2c2018-08-31 09:22:23 +0100212 std::unordered_map<std::string, std::pair<const onnx::NodeProto*, int>> m_OutputsMap;
213
Teresa Charlinbc148812021-12-13 15:29:10 +0000214 /// Number of times a specific node (identified by its index number) was used as input
telsoa01c577f2c2018-08-31 09:22:23 +0100215 /// and list of the nodes it was fused with
216 struct UsageSummary
217 {
218 std::vector<size_t> fusedWithNodes;
219 size_t inputForNodes;
220
221 UsageSummary() : fusedWithNodes({}), inputForNodes(0) { }
222
223 };
224
225 std::vector<UsageSummary> m_OutputsFusedAndUsed;
Ryan OSheaed27ee72020-04-22 16:37:29 +0100226
Narumol Prangnawarat1b11f322021-10-13 11:44:50 +0100227 std::map<std::string, armnn::TensorShape> m_InputShapes;
228
229 std::unordered_map<std::string, armnn::TensorInfo> m_InputInfos;
230
231 std::unordered_map<std::string, armnn::TensorInfo> m_OutputInfos;
232
telsoa01c577f2c2018-08-31 09:22:23 +0100233};
234}