telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. 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 | |
| 37 | /// Create the network from a protobuf text file on disk |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 38 | armnn::INetworkPtr CreateNetworkFromTextFile(const char* graphFile); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 39 | |
| 40 | /// 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] | 41 | armnn::INetworkPtr CreateNetworkFromString(const std::string& protoText); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 42 | |
| 43 | /// 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] | 44 | BindingPointInfo GetNetworkInputBindingInfo(const std::string& name) const; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 45 | |
| 46 | /// 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] | 47 | BindingPointInfo GetNetworkOutputBindingInfo(const std::string& name) const; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 48 | |
| 49 | public: |
| 50 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 51 | OnnxParserImpl(); |
| 52 | ~OnnxParserImpl() = default; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 53 | |
| 54 | static ModelPtr LoadModelFromBinaryFile(const char * fileName); |
| 55 | static ModelPtr LoadModelFromTextFile(const char * fileName); |
| 56 | static ModelPtr LoadModelFromString(const std::string& inputString); |
| 57 | |
Ryan OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 58 | /// Retrieve inputs names |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 59 | static std::vector<std::string> GetInputs(ModelPtr& model); |
| 60 | |
Ryan OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 61 | /// Retrieve outputs names |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 62 | static std::vector<std::string> GetOutputs(ModelPtr& model); |
| 63 | |
Matthew Sloyan | ac001ee | 2021-02-03 10:43:04 +0000 | [diff] [blame] | 64 | /// Retrieve version in X.Y.Z form |
| 65 | static const std::string GetVersion(); |
| 66 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 67 | private: |
| 68 | |
| 69 | /// Parses a ModelProto loaded into memory from one of the other CreateNetwork* |
| 70 | armnn::INetworkPtr CreateNetworkFromModel(onnx::ModelProto& model); |
| 71 | |
Ryan OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 72 | /// Parse every node and make the connection between the resulting tensors |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 73 | void LoadGraph(); |
| 74 | |
| 75 | void SetupInfo(const google::protobuf::RepeatedPtrField<onnx::ValueInfoProto >* list); |
| 76 | |
| 77 | std::vector<armnn::TensorInfo> ComputeOutputInfo(std::vector<std::string> outNames, |
| 78 | const armnn::IConnectableLayer* layer, |
| 79 | std::vector<armnn::TensorShape> inputShapes); |
| 80 | |
| 81 | void DetectFullyConnected(); |
| 82 | |
| 83 | template <typename Location> |
| 84 | void GetInputAndParam(const onnx::NodeProto& node, |
| 85 | std::string* inputName, |
| 86 | std::string* constName, |
| 87 | const Location& location); |
| 88 | |
| 89 | template <typename Location> |
| 90 | void To1DTensor(const std::string &name, const Location& location); |
| 91 | |
| 92 | //Broadcast Preparation functions |
| 93 | std::pair<std::string, std::string> AddPrepareBroadcast(const std::string& input0, const std::string& input1); |
| 94 | void PrependForBroadcast(const std::string& outputName, const std::string& input0, const std::string& input1); |
| 95 | |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 96 | void AddConvLayerWithDepthwiseConv(const onnx::NodeProto& node, const armnn::Convolution2dDescriptor& convDesc); |
| 97 | void AddFullyConnected(const onnx::NodeProto& matmulNode, const onnx::NodeProto* addNode = nullptr); |
| 98 | void AddPoolingLayer(const onnx::NodeProto& nodeProto, armnn::Pooling2dDescriptor& desc); |
| 99 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 100 | void CreateConstantLayer(const std::string& tensorName, const std::string& layerName); |
| 101 | void CreateReshapeLayer(const std::string& inputName, |
| 102 | const std::string& outputName, |
| 103 | const std::string& layerName); |
| 104 | |
Tee Jung | 7ff9a60 | 2019-11-01 07:04:42 +0000 | [diff] [blame] | 105 | void ParseActivation(const onnx::NodeProto& nodeProto, const armnn::ActivationFunction func); |
Finn Williams | 7ee5d2c | 2020-03-27 11:11:50 +0000 | [diff] [blame] | 106 | void ParseClip(const onnx::NodeProto& nodeProto); |
Tee Jung | 7ff9a60 | 2019-11-01 07:04:42 +0000 | [diff] [blame] | 107 | void ParseSigmoid(const onnx::NodeProto& nodeProto); |
| 108 | void ParseTanh(const onnx::NodeProto& nodeProto); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 109 | void ParseRelu(const onnx::NodeProto& nodeProto); |
Tee Jung | 7ff9a60 | 2019-11-01 07:04:42 +0000 | [diff] [blame] | 110 | void ParseLeakyRelu(const onnx::NodeProto& nodeProto); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 111 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 112 | void ParseAdd(const onnx::NodeProto& nodeProto); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 113 | void ParseAveragePool(const onnx::NodeProto& nodeProto); |
| 114 | void ParseBatchNormalization(const onnx::NodeProto& node); |
| 115 | void ParseConstant(const onnx::NodeProto& nodeProto); |
| 116 | void ParseConv(const onnx::NodeProto& nodeProto); |
| 117 | void ParseFlatten(const onnx::NodeProto& node); |
| 118 | void ParseGlobalAveragePool(const onnx::NodeProto& node); |
| 119 | void ParseMaxPool(const onnx::NodeProto& nodeProto); |
| 120 | void ParseReshape(const onnx::NodeProto& nodeProto); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 121 | |
| 122 | void RegisterInputSlots(armnn::IConnectableLayer* layer, const std::vector<std::string>& tensorIndexes); |
| 123 | void RegisterOutputSlots(armnn::IConnectableLayer* layer, const std::vector<std::string>& tensorIndexes); |
| 124 | |
| 125 | void SetupInputLayers(); |
| 126 | void SetupOutputLayers(); |
| 127 | |
| 128 | void ResetParser(); |
| 129 | void Cleanup(); |
| 130 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 131 | std::pair<armnn::ConstTensor, std::unique_ptr<float[]>> |
| 132 | CreateConstTensor(const std::string name, |
| 133 | armnn::Optional<armnn::PermutationVector&> permutationVector = armnn::EmptyOptional()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 134 | |
| 135 | template <typename TypeList, typename Location> |
| 136 | void ValidateInputs(const onnx::NodeProto& node, |
| 137 | TypeList validInputs, |
| 138 | const Location& location); |
| 139 | |
| 140 | /// The network we're building. Gets cleared after it is passed to the user |
| 141 | armnn::INetworkPtr m_Network; |
| 142 | |
Ryan OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 143 | /// Ptr to the graph we're building the network from |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 144 | GraphPtr m_Graph; |
| 145 | |
Ryan OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 146 | /// Map of the information for every tensor |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 147 | struct OnnxTensor |
| 148 | { |
| 149 | std::unique_ptr<armnn::TensorInfo> m_info; |
| 150 | std::unique_ptr<const onnx::TensorProto> m_tensor; |
| 151 | onnx::TensorProto::DataType m_dtype; |
| 152 | |
| 153 | OnnxTensor() : m_info(nullptr), m_tensor(nullptr), m_dtype(onnx::TensorProto::FLOAT) { } |
| 154 | bool isConstant() { return m_tensor != nullptr; } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | std::unordered_map<std::string, OnnxTensor> m_TensorsInfo; |
| 158 | |
| 159 | /// map of onnx operation names to parsing member functions |
| 160 | static const std::map<std::string, OperationParsingFunction> m_ParserFunctions; |
| 161 | |
| 162 | /// A mapping of an output slot to each of the input slots it should be connected to |
| 163 | /// The outputSlot is from the layer that creates this tensor as one of its ouputs |
| 164 | /// The inputSlots are from the layers that use this tensor as one of their inputs |
| 165 | struct TensorSlots |
| 166 | { |
| 167 | armnn::IOutputSlot* outputSlot; |
| 168 | std::vector<armnn::IInputSlot*> inputSlots; |
| 169 | |
| 170 | TensorSlots() : outputSlot(nullptr) { } |
| 171 | }; |
Ryan OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 172 | /// 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] | 173 | std::unordered_map<std::string, TensorSlots> m_TensorConnections; |
| 174 | |
Ryan OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 175 | /// Map of the tensor names to their node and index in graph.node() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 176 | std::unordered_map<std::string, std::pair<const onnx::NodeProto*, int>> m_OutputsMap; |
| 177 | |
| 178 | /// Number of times a specific node (identified by his index number) was used as input |
| 179 | /// and list of the nodes it was fused with |
| 180 | struct UsageSummary |
| 181 | { |
| 182 | std::vector<size_t> fusedWithNodes; |
| 183 | size_t inputForNodes; |
| 184 | |
| 185 | UsageSummary() : fusedWithNodes({}), inputForNodes(0) { } |
| 186 | |
| 187 | }; |
| 188 | |
| 189 | std::vector<UsageSummary> m_OutputsFusedAndUsed; |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 190 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 191 | }; |
| 192 | } |