blob: d388f501d4f07f653324f952f03b90df28a312c2 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. 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
37 /// Create the network from a protobuf text file on disk
Kevin Mayef33cb12021-01-29 14:24:57 +000038 armnn::INetworkPtr CreateNetworkFromTextFile(const char* graphFile);
telsoa01c577f2c2018-08-31 09:22:23 +010039
40 /// Create the network directly from protobuf text in a string. Useful for debugging/testing
Kevin Mayef33cb12021-01-29 14:24:57 +000041 armnn::INetworkPtr CreateNetworkFromString(const std::string& protoText);
telsoa01c577f2c2018-08-31 09:22:23 +010042
43 /// 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 +000044 BindingPointInfo GetNetworkInputBindingInfo(const std::string& name) const;
telsoa01c577f2c2018-08-31 09:22:23 +010045
46 /// 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 +000047 BindingPointInfo GetNetworkOutputBindingInfo(const std::string& name) const;
telsoa01c577f2c2018-08-31 09:22:23 +010048
49public:
50
Kevin Mayef33cb12021-01-29 14:24:57 +000051 OnnxParserImpl();
52 ~OnnxParserImpl() = default;
telsoa01c577f2c2018-08-31 09:22:23 +010053
54 static ModelPtr LoadModelFromBinaryFile(const char * fileName);
55 static ModelPtr LoadModelFromTextFile(const char * fileName);
56 static ModelPtr LoadModelFromString(const std::string& inputString);
57
Ryan OShea337c17f2020-02-21 12:33:17 +000058 /// Retrieve inputs names
telsoa01c577f2c2018-08-31 09:22:23 +010059 static std::vector<std::string> GetInputs(ModelPtr& model);
60
Ryan OShea337c17f2020-02-21 12:33:17 +000061 /// Retrieve outputs names
telsoa01c577f2c2018-08-31 09:22:23 +010062 static std::vector<std::string> GetOutputs(ModelPtr& model);
63
Matthew Sloyanac001ee2021-02-03 10:43:04 +000064 /// Retrieve version in X.Y.Z form
65 static const std::string GetVersion();
66
telsoa01c577f2c2018-08-31 09:22:23 +010067private:
68
69 /// Parses a ModelProto loaded into memory from one of the other CreateNetwork*
70 armnn::INetworkPtr CreateNetworkFromModel(onnx::ModelProto& model);
71
Ryan OShea337c17f2020-02-21 12:33:17 +000072 /// Parse every node and make the connection between the resulting tensors
telsoa01c577f2c2018-08-31 09:22:23 +010073 void LoadGraph();
74
75 void SetupInfo(const google::protobuf::RepeatedPtrField<onnx::ValueInfoProto >* list);
76
Narumol Prangnawarat452274c2021-09-23 16:12:19 +010077 std::vector<armnn::TensorInfo> ComputeOutputInfo(
78 std::vector<std::string> outNames,
79 const armnn::IConnectableLayer* layer,
80 std::vector<armnn::TensorShape> inputShapes,
81 const onnx::TensorProto::DataType& type = onnx::TensorProto::FLOAT);
telsoa01c577f2c2018-08-31 09:22:23 +010082
83 void DetectFullyConnected();
84
85 template <typename Location>
86 void GetInputAndParam(const onnx::NodeProto& node,
87 std::string* inputName,
88 std::string* constName,
89 const Location& location);
90
91 template <typename Location>
92 void To1DTensor(const std::string &name, const Location& location);
93
94 //Broadcast Preparation functions
95 std::pair<std::string, std::string> AddPrepareBroadcast(const std::string& input0, const std::string& input1);
96 void PrependForBroadcast(const std::string& outputName, const std::string& input0, const std::string& input1);
97
Ryan OSheaed27ee72020-04-22 16:37:29 +010098 void AddConvLayerWithDepthwiseConv(const onnx::NodeProto& node, const armnn::Convolution2dDescriptor& convDesc);
99 void AddFullyConnected(const onnx::NodeProto& matmulNode, const onnx::NodeProto* addNode = nullptr);
100 void AddPoolingLayer(const onnx::NodeProto& nodeProto, armnn::Pooling2dDescriptor& desc);
101
telsoa01c577f2c2018-08-31 09:22:23 +0100102 void CreateConstantLayer(const std::string& tensorName, const std::string& layerName);
Narumol Prangnawaratf10b15a2021-09-17 21:08:57 +0100103 void CreateInt64ConstantLayer(const std::string& tensorName, const std::string& layerName);
telsoa01c577f2c2018-08-31 09:22:23 +0100104 void CreateReshapeLayer(const std::string& inputName,
105 const std::string& outputName,
106 const std::string& layerName);
107
Tee Jung7ff9a602019-11-01 07:04:42 +0000108 void ParseActivation(const onnx::NodeProto& nodeProto, const armnn::ActivationFunction func);
Finn Williams7ee5d2c2020-03-27 11:11:50 +0000109 void ParseClip(const onnx::NodeProto& nodeProto);
Tee Jung7ff9a602019-11-01 07:04:42 +0000110 void ParseSigmoid(const onnx::NodeProto& nodeProto);
111 void ParseTanh(const onnx::NodeProto& nodeProto);
telsoa01c577f2c2018-08-31 09:22:23 +0100112 void ParseRelu(const onnx::NodeProto& nodeProto);
Tee Jung7ff9a602019-11-01 07:04:42 +0000113 void ParseLeakyRelu(const onnx::NodeProto& nodeProto);
telsoa01c577f2c2018-08-31 09:22:23 +0100114
telsoa01c577f2c2018-08-31 09:22:23 +0100115 void ParseAdd(const onnx::NodeProto& nodeProto);
Ryan OSheaed27ee72020-04-22 16:37:29 +0100116 void ParseAveragePool(const onnx::NodeProto& nodeProto);
117 void ParseBatchNormalization(const onnx::NodeProto& node);
Narumol Prangnawaratbc3bb622021-09-24 16:08:34 +0100118 void ParseConcat(const onnx::NodeProto& nodeProto);
Ryan OSheaed27ee72020-04-22 16:37:29 +0100119 void ParseConstant(const onnx::NodeProto& nodeProto);
120 void ParseConv(const onnx::NodeProto& nodeProto);
121 void ParseFlatten(const onnx::NodeProto& node);
Narumol Prangnawaratf10b15a2021-09-17 21:08:57 +0100122 void ParseGather(const onnx::NodeProto& node);
Ryan OSheaed27ee72020-04-22 16:37:29 +0100123 void ParseGlobalAveragePool(const onnx::NodeProto& node);
124 void ParseMaxPool(const onnx::NodeProto& nodeProto);
Narumol Prangnawaratcdc495e2021-09-16 18:13:39 +0100125 void ParseShape(const onnx::NodeProto& node);
Ryan OSheaed27ee72020-04-22 16:37:29 +0100126 void ParseReshape(const onnx::NodeProto& nodeProto);
Narumol Prangnawaratfe6aa2f2021-09-23 16:11:17 +0100127 void ParseUnsqueeze(const onnx::NodeProto& nodeProto);
telsoa01c577f2c2018-08-31 09:22:23 +0100128
129 void RegisterInputSlots(armnn::IConnectableLayer* layer, const std::vector<std::string>& tensorIndexes);
130 void RegisterOutputSlots(armnn::IConnectableLayer* layer, const std::vector<std::string>& tensorIndexes);
131
132 void SetupInputLayers();
133 void SetupOutputLayers();
134
135 void ResetParser();
136 void Cleanup();
137
Jan Eilers53ef7952021-06-02 12:01:25 +0100138 std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
139 CreateConstTensor(const std::string name,
140 armnn::Optional<armnn::PermutationVector&> permutationVector = armnn::EmptyOptional());
telsoa01c577f2c2018-08-31 09:22:23 +0100141
Narumol Prangnawaratf10b15a2021-09-17 21:08:57 +0100142 std::pair<armnn::ConstTensor, std::unique_ptr<int32_t[]>>
143 CreateInt64ConstTensor(const std::string name,
144 armnn::Optional<armnn::PermutationVector&> permutationVector = armnn::EmptyOptional());
145
telsoa01c577f2c2018-08-31 09:22:23 +0100146 template <typename TypeList, typename Location>
147 void ValidateInputs(const onnx::NodeProto& node,
148 TypeList validInputs,
149 const Location& location);
150
151 /// The network we're building. Gets cleared after it is passed to the user
152 armnn::INetworkPtr m_Network;
153
Ryan OShea337c17f2020-02-21 12:33:17 +0000154 /// Ptr to the graph we're building the network from
telsoa01c577f2c2018-08-31 09:22:23 +0100155 GraphPtr m_Graph;
156
Ryan OShea337c17f2020-02-21 12:33:17 +0000157 /// Map of the information for every tensor
telsoa01c577f2c2018-08-31 09:22:23 +0100158 struct OnnxTensor
159 {
160 std::unique_ptr<armnn::TensorInfo> m_info;
161 std::unique_ptr<const onnx::TensorProto> m_tensor;
162 onnx::TensorProto::DataType m_dtype;
163
164 OnnxTensor() : m_info(nullptr), m_tensor(nullptr), m_dtype(onnx::TensorProto::FLOAT) { }
165 bool isConstant() { return m_tensor != nullptr; }
telsoa01c577f2c2018-08-31 09:22:23 +0100166 };
167
168 std::unordered_map<std::string, OnnxTensor> m_TensorsInfo;
169
170 /// map of onnx operation names to parsing member functions
171 static const std::map<std::string, OperationParsingFunction> m_ParserFunctions;
172
173 /// A mapping of an output slot to each of the input slots it should be connected to
174 /// The outputSlot is from the layer that creates this tensor as one of its ouputs
175 /// The inputSlots are from the layers that use this tensor as one of their inputs
176 struct TensorSlots
177 {
178 armnn::IOutputSlot* outputSlot;
179 std::vector<armnn::IInputSlot*> inputSlots;
180
181 TensorSlots() : outputSlot(nullptr) { }
182 };
Ryan OShea337c17f2020-02-21 12:33:17 +0000183 /// Map of the tensor names to their connections for the connections of the layers of the graph
telsoa01c577f2c2018-08-31 09:22:23 +0100184 std::unordered_map<std::string, TensorSlots> m_TensorConnections;
185
Ryan OShea337c17f2020-02-21 12:33:17 +0000186 /// Map of the tensor names to their node and index in graph.node()
telsoa01c577f2c2018-08-31 09:22:23 +0100187 std::unordered_map<std::string, std::pair<const onnx::NodeProto*, int>> m_OutputsMap;
188
189 /// Number of times a specific node (identified by his index number) was used as input
190 /// and list of the nodes it was fused with
191 struct UsageSummary
192 {
193 std::vector<size_t> fusedWithNodes;
194 size_t inputForNodes;
195
196 UsageSummary() : fusedWithNodes({}), inputForNodes(0) { }
197
198 };
199
200 std::vector<UsageSummary> m_OutputsFusedAndUsed;
Ryan OSheaed27ee72020-04-22 16:37:29 +0100201
telsoa01c577f2c2018-08-31 09:22:23 +0100202};
203}