telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
Matthew Sloyan | ca36123 | 2023-02-16 14:50:22 +0000 | [diff] [blame] | 2 | // Copyright © 2017,2022-2023 Arm Ltd and Contributors. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 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 | |
| 14 | namespace armnn |
| 15 | { |
| 16 | class TensorInfo; |
Tee Jung | 7ff9a60 | 2019-11-01 07:04:42 +0000 | [diff] [blame] | 17 | enum class ActivationFunction; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | namespace armnnOnnxParser |
| 21 | { |
| 22 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 23 | using ModelPtr = std::unique_ptr<onnx::ModelProto>; |
| 24 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 25 | class OnnxParserImpl |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 26 | { |
| 27 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 28 | using OperationParsingFunction = void(OnnxParserImpl::*)(const onnx::NodeProto& NodeProto); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 29 | |
| 30 | public: |
| 31 | |
| 32 | using GraphPtr = std::unique_ptr<onnx::GraphProto>; |
| 33 | |
| 34 | /// Create the network from a protobuf binary file on disk |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 35 | armnn::INetworkPtr CreateNetworkFromBinaryFile(const char* graphFile); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 36 | |
Narumol Prangnawarat | 1b11f32 | 2021-10-13 11:44:50 +0100 | [diff] [blame] | 37 | /// 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 Kelly | 2ae3224 | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 41 | /// 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 48 | /// Create the network from a protobuf text file on disk |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 49 | armnn::INetworkPtr CreateNetworkFromTextFile(const char* graphFile); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 50 | |
Narumol Prangnawarat | 1b11f32 | 2021-10-13 11:44:50 +0100 | [diff] [blame] | 51 | /// 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 55 | /// Create the network directly from protobuf text in a string. Useful for debugging/testing |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 56 | armnn::INetworkPtr CreateNetworkFromString(const std::string& protoText); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 57 | |
Narumol Prangnawarat | 1b11f32 | 2021-10-13 11:44:50 +0100 | [diff] [blame] | 58 | /// 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 63 | /// Retrieve binding info (layer id and tensor info) for the network input identified by the given layer name |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 64 | BindingPointInfo GetNetworkInputBindingInfo(const std::string& name) const; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 65 | |
| 66 | /// Retrieve binding info (layer id and tensor info) for the network output identified by the given layer name |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 67 | BindingPointInfo GetNetworkOutputBindingInfo(const std::string& name) const; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 68 | |
| 69 | public: |
| 70 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 71 | OnnxParserImpl(); |
| 72 | ~OnnxParserImpl() = default; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 73 | |
Mike Kelly | 2ae3224 | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 74 | static ModelPtr LoadModelFromBinary(const std::vector<uint8_t>& binaryContent); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 75 | static ModelPtr LoadModelFromBinaryFile(const char * fileName); |
| 76 | static ModelPtr LoadModelFromTextFile(const char * fileName); |
| 77 | static ModelPtr LoadModelFromString(const std::string& inputString); |
| 78 | |
Ryan OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 79 | /// Retrieve inputs names |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 80 | static std::vector<std::string> GetInputs(ModelPtr& model); |
| 81 | |
Ryan OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 82 | /// Retrieve outputs names |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 83 | static std::vector<std::string> GetOutputs(ModelPtr& model); |
| 84 | |
Matthew Sloyan | ac001ee | 2021-02-03 10:43:04 +0000 | [diff] [blame] | 85 | /// Retrieve version in X.Y.Z form |
| 86 | static const std::string GetVersion(); |
| 87 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 88 | private: |
| 89 | |
| 90 | /// Parses a ModelProto loaded into memory from one of the other CreateNetwork* |
| 91 | armnn::INetworkPtr CreateNetworkFromModel(onnx::ModelProto& model); |
| 92 | |
Ryan OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 93 | /// Parse every node and make the connection between the resulting tensors |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 94 | void LoadGraph(); |
| 95 | |
| 96 | void SetupInfo(const google::protobuf::RepeatedPtrField<onnx::ValueInfoProto >* list); |
| 97 | |
Narumol Prangnawarat | 452274c | 2021-09-23 16:12:19 +0100 | [diff] [blame] | 98 | 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); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 103 | |
| 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 OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 119 | 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 123 | void CreateConstantLayer(const std::string& tensorName, const std::string& layerName); |
Narumol Prangnawarat | f10b15a | 2021-09-17 21:08:57 +0100 | [diff] [blame] | 124 | void CreateInt64ConstantLayer(const std::string& tensorName, const std::string& layerName); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 125 | void CreateReshapeLayer(const std::string& inputName, |
| 126 | const std::string& outputName, |
| 127 | const std::string& layerName); |
| 128 | |
Tee Jung | 7ff9a60 | 2019-11-01 07:04:42 +0000 | [diff] [blame] | 129 | void ParseActivation(const onnx::NodeProto& nodeProto, const armnn::ActivationFunction func); |
Finn Williams | 7ee5d2c | 2020-03-27 11:11:50 +0000 | [diff] [blame] | 130 | void ParseClip(const onnx::NodeProto& nodeProto); |
Tee Jung | 7ff9a60 | 2019-11-01 07:04:42 +0000 | [diff] [blame] | 131 | void ParseSigmoid(const onnx::NodeProto& nodeProto); |
| 132 | void ParseTanh(const onnx::NodeProto& nodeProto); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 133 | void ParseRelu(const onnx::NodeProto& nodeProto); |
Tee Jung | 7ff9a60 | 2019-11-01 07:04:42 +0000 | [diff] [blame] | 134 | void ParseLeakyRelu(const onnx::NodeProto& nodeProto); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 135 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 136 | void ParseAdd(const onnx::NodeProto& nodeProto); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 137 | void ParseAveragePool(const onnx::NodeProto& nodeProto); |
| 138 | void ParseBatchNormalization(const onnx::NodeProto& node); |
Narumol Prangnawarat | bc3bb62 | 2021-09-24 16:08:34 +0100 | [diff] [blame] | 139 | void ParseConcat(const onnx::NodeProto& nodeProto); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 140 | void ParseConstant(const onnx::NodeProto& nodeProto); |
| 141 | void ParseConv(const onnx::NodeProto& nodeProto); |
| 142 | void ParseFlatten(const onnx::NodeProto& node); |
Narumol Prangnawarat | f10b15a | 2021-09-17 21:08:57 +0100 | [diff] [blame] | 143 | void ParseGather(const onnx::NodeProto& node); |
Narumol Prangnawarat | 1112b01 | 2021-09-30 12:10:50 +0100 | [diff] [blame] | 144 | void ParseGemm(const onnx::NodeProto& node); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 145 | void ParseGlobalAveragePool(const onnx::NodeProto& node); |
| 146 | void ParseMaxPool(const onnx::NodeProto& nodeProto); |
Narumol Prangnawarat | cdc495e | 2021-09-16 18:13:39 +0100 | [diff] [blame] | 147 | void ParseShape(const onnx::NodeProto& node); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 148 | void ParseReshape(const onnx::NodeProto& nodeProto); |
Narumol Prangnawarat | fe6aa2f | 2021-09-23 16:11:17 +0100 | [diff] [blame] | 149 | void ParseUnsqueeze(const onnx::NodeProto& nodeProto); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 150 | |
Narumol Prangnawarat | 1112b01 | 2021-09-30 12:10:50 +0100 | [diff] [blame] | 151 | void RegisterInputSlot(armnn::IConnectableLayer* layer, |
| 152 | const std::string& tensorId, |
| 153 | unsigned int slotIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 154 | 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 Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 163 | std::pair<armnn::ConstTensor, std::unique_ptr<float[]>> |
| 164 | CreateConstTensor(const std::string name, |
| 165 | armnn::Optional<armnn::PermutationVector&> permutationVector = armnn::EmptyOptional()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 166 | |
Narumol Prangnawarat | f10b15a | 2021-09-17 21:08:57 +0100 | [diff] [blame] | 167 | std::pair<armnn::ConstTensor, std::unique_ptr<int32_t[]>> |
| 168 | CreateInt64ConstTensor(const std::string name, |
| 169 | armnn::Optional<armnn::PermutationVector&> permutationVector = armnn::EmptyOptional()); |
| 170 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 171 | 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 OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 179 | /// Ptr to the graph we're building the network from |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 180 | GraphPtr m_Graph; |
| 181 | |
Ryan OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 182 | /// Map of the information for every tensor |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 183 | 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; } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 191 | }; |
| 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 Sloyan | ca36123 | 2023-02-16 14:50:22 +0000 | [diff] [blame] | 199 | /// The outputSlot is from the layer that creates this tensor as one of its outputs |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 200 | /// 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 OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 208 | /// Map of the tensor names to their connections for the connections of the layers of the graph |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 209 | std::unordered_map<std::string, TensorSlots> m_TensorConnections; |
| 210 | |
Ryan OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 211 | /// Map of the tensor names to their node and index in graph.node() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 212 | std::unordered_map<std::string, std::pair<const onnx::NodeProto*, int>> m_OutputsMap; |
| 213 | |
Teresa Charlin | bc14881 | 2021-12-13 15:29:10 +0000 | [diff] [blame] | 214 | /// Number of times a specific node (identified by its index number) was used as input |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 215 | /// 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 OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 226 | |
Narumol Prangnawarat | 1b11f32 | 2021-10-13 11:44:50 +0100 | [diff] [blame] | 227 | 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 233 | }; |
| 234 | } |