| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #ifndef PNNX_PASS_ONNX_H |
| 16 | #define PNNX_PASS_ONNX_H |
| 17 | |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | #include <unordered_map> |
| 21 | |
| 22 | namespace onnx { |
| 23 | class AttributeProto; |
| 24 | class FunctionProto; |
| 25 | class ModelProto; |
| 26 | class NodeProto; |
| 27 | class TensorProto; |
| 28 | class ValueInfoProto; |
| 29 | } // namespace onnx |
| 30 | |
| 31 | namespace pnnx { |
| 32 | |
| 33 | class Operator; |
| 34 | class Graph; |
| 35 | |
| 36 | namespace onnx2pnnx { |
| 37 | |
| 38 | class OnnxAttributeProxy |
| 39 | { |
| 40 | public: |
| 41 | OnnxAttributeProxy(const onnx::AttributeProto& _attr) |
| 42 | : attr(_attr) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | operator float() const |
| 47 | { |
| 48 | return value_f(); |
| 49 | } |
| 50 | operator int64_t() const |
| 51 | { |
| 52 | return value_i(); |
| 53 | } |
| 54 | operator std::string() const |
| 55 | { |
| 56 | return value_s(); |
| 57 | } |
| 58 | operator std::vector<float>() const |
| 59 | { |
| 60 | return value_fs(); |
| 61 | } |
| 62 | operator std::vector<int64_t>() const |
| 63 | { |
| 64 | return value_is(); |
| 65 | } |
| 66 | operator std::vector<std::string>() const |
| 67 | { |
| 68 | return value_ss(); |
| 69 | } |
| 70 | |
| 71 | float value_f() const; |
| 72 | int64_t value_i() const; |
| 73 | std::string value_s() const; |
| 74 | std::vector<float> value_fs() const; |
| 75 | std::vector<int64_t> value_is() const; |
| 76 | std::vector<std::string> value_ss() const; |
| 77 | |
| 78 | public: |
| 79 | const onnx::AttributeProto& attr; |
| 80 | }; |
| 81 | |
| 82 | class OnnxNodeProxy |
| 83 | { |
| 84 | public: |
| 85 | OnnxNodeProxy(const onnx::NodeProto& _node); |
| 86 | |
| 87 | bool has_attribute(const std::string& name) const; |
| 88 | const OnnxAttributeProxy attribute(const std::string& name) const; |
| 89 | |
| 90 | public: |
| 91 | const onnx::NodeProto& node; |
| 92 | |
| 93 | protected: |
| 94 | std::unordered_map<std::string, int> attributes; |
| 95 | }; |
| 96 | |
| 97 | class OnnxFunctionProxy |
| 98 | { |
| 99 | public: |
| 100 | OnnxFunctionProxy(const onnx::ModelProto& _model, const onnx::NodeProto& _caller, const onnx::FunctionProto& _function); |
| 101 | |
| 102 | bool has_typed_node(const std::string& type) const; |
| 103 | bool has_named_node(const std::string& name) const; |
| 104 | const OnnxNodeProxy typed_node(const std::string& type) const; |
| 105 | const OnnxNodeProxy named_node(const std::string& name) const; |
| 106 | |
| 107 | const OnnxNodeProxy find_producer(const std::string& name) const; |
| 108 | |
| 109 | bool has_initializer(const std::string& name) const; |
| 110 | const onnx::TensorProto& initializer(const std::string& name) const; |
| 111 | |
| 112 | public: |
| 113 | const onnx::ModelProto& model; |
| 114 | const onnx::NodeProto& caller; |
| 115 | const onnx::FunctionProto& function; |
| 116 | |
| 117 | protected: |
| 118 | std::unordered_map<std::string, int> typed_nodes; |
| 119 | std::unordered_map<std::string, int> named_nodes; |
| 120 | std::unordered_map<std::string, int> initializers; |
| 121 | }; |
| 122 | |
| 123 | class OnnxModelProxy |
| 124 | { |
| 125 | public: |
| 126 | OnnxModelProxy(const onnx::ModelProto& _model); |
| 127 | |
| 128 | bool has_node(const std::string& name) const; |
| 129 | const OnnxNodeProxy node(const std::string& name) const; |
| 130 | |
| 131 | bool has_function(const std::string& name, const std::string& caller) const; |
| 132 | const OnnxFunctionProxy function(const std::string& name, const std::string& caller) const; |
| 133 | |
| 134 | bool has_valueinfo(const std::string& name) const; |
| 135 | const onnx::ValueInfoProto& valueinfo(const std::string& name) const; |
| 136 | |
| 137 | bool has_initializer(const std::string& name) const; |
| 138 | const onnx::TensorProto& initializer(const std::string& name) const; |
| 139 | |
| 140 | public: |
| 141 | const onnx::ModelProto& model; |
| 142 | |
| 143 | protected: |
| 144 | std::unordered_map<std::string, int> nodes; |
| 145 | std::unordered_map<std::string, int> functions; |
| 146 | std::unordered_map<std::string, int> valueinfos; |
| 147 | std::unordered_map<std::string, int> initializers; |
| 148 | }; |
| 149 | |
| 150 | class FuseFunctionPass |
| 151 | { |
| 152 | public: |
| 153 | virtual ~FuseFunctionPass(); |
| 154 | |
| 155 | virtual const char* match_type_str() const = 0; |
| 156 | |
| 157 | virtual const char* type_str() const = 0; |
| 158 | |
| 159 | virtual void write(Operator* op, const OnnxFunctionProxy& function) const; |
| 160 | }; |
| 161 | |
| 162 | class FuseFunctionPassRegister |
| 163 | { |
| 164 | public: |
| 165 | FuseFunctionPassRegister(const FuseFunctionPass* pass); |
| 166 | ~FuseFunctionPassRegister(); |
| 167 | const FuseFunctionPass* pass; |
| 168 | }; |
| 169 | |
| 170 | const std::vector<const FuseFunctionPass*>& get_global_pnnx_fuse_function_passes(); |
| 171 | |
| 172 | #define REGISTER_GLOBAL_PNNX_FUSE_FUNCTION_PASS(CLASS) \ |
| 173 | static FuseFunctionPassRegister g_global_pnnx_fusefunctionpass_##CLASS##_register(new CLASS); |
| 174 | |
| 175 | } // namespace onnx2pnnx |
| 176 | |
| 177 | void pass_onnx(const onnx::ModelProto& model, Graph& pnnx_graph); |
| 178 | |
| 179 | } // namespace pnnx |
| 180 | |
| 181 | #endif // PNNX_PASS_ONNX_H |