| 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 | #include "inline_containers.h" |
| 16 | |
| 17 | #include <map> |
| 18 | #include <string> |
| 19 | |
| 20 | namespace pnnx { |
| 21 | |
| 22 | namespace onnx2pnnx { |
| 23 | |
| 24 | static bool string_starts_with(const std::string& s, const std::string& s2) |
| 25 | { |
| 26 | return strncmp(s.c_str(), s2.c_str(), s2.size()) == 0; |
| 27 | } |
| 28 | |
| 29 | void inline_containers(onnx::ModelProto& model) |
| 30 | { |
| 31 | onnx::GraphProto* graph = model.mutable_graph(); |
| 32 | |
| 33 | for (int i = 0; i < graph->node_size(); i++) |
| 34 | { |
| 35 | onnx::NodeProto* node = graph->mutable_node(i); |
| 36 | |
| 37 | const std::string& op_type = node->op_type(); |
| 38 | |
| 39 | if (node->domain().empty()) |
| 40 | { |
| 41 | // native onnx op |
| 42 | |
| 43 | // Constant |
| 44 | // fprintf(stderr, " node = onnx %s\n", op_type.c_str()); |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | if (string_starts_with(op_type, "torch_nn_modules_") && !string_starts_with(op_type, "torch_nn_modules_container_")) |
| 49 | { |
| 50 | // torch_nn_modules_conv_Conv2d _conv1_1 |
| 51 | // torch_nn_modules_batchnorm_BatchNorm2d _bn1_1 |
| 52 | // torch_nn_modules_pooling_MaxPool2d _maxpool_1_3 |
| 53 | // torch_nn_modules_linear_Linear _fc_1 |
| 54 | |
| 55 | // std::vector<std::string> tokens = string_split(op_type, '_'); |
| 56 | |
| 57 | // fprintf(stderr, " node = nn.%s\n", tokens[4].c_str()); |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | if (string_starts_with(op_type, "aten_") || string_starts_with(op_type, "_aten_")) |
| 62 | { |
| 63 | // aten_view |
| 64 | |
| 65 | // std::vector<std::string> tokens = string_split(op_type, '_'); |
| 66 | |
| 67 | // fprintf(stderr, " node = aten::%s\n", tokens[1].c_str()); |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | if (string_starts_with(op_type, "prims_")) |
| 72 | { |
| 73 | // prims_convert_element_type |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | // find function |
| 78 | int function_index = -1; |
| 79 | for (int j = 0; j < model.functions_size(); j++) |
| 80 | { |
| 81 | const onnx::FunctionProto& function = model.functions(j); |
| 82 | if (function.name() == op_type) |
| 83 | { |
| 84 | function_index = j; |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (function_index == -1) |
| 90 | { |
| 91 | fprintf(stderr, "no such function with name %s\n", op_type.c_str()); |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | // ok, this is a function, inline it at node |
| 96 | // fprintf(stderr, "inline %s\n", op_type.c_str()); |
| 97 | |
| 98 | const onnx::FunctionProto& function = model.functions(function_index); |
| 99 | |
| 100 | // build function input and output name remap |
| 101 | std::map<std::string, std::string> input_output_remap; |
| 102 | { |
| 103 | for (int j = 0; j < node->input_size(); j++) |
| 104 | { |
| 105 | const std::string& node_input = node->input(j); |
| 106 | const std::string& func_input = function.input(j); |
| 107 | |
| 108 | input_output_remap[func_input] = node_input; |
| 109 | } |
| 110 | for (int j = 0; j < node->output_size(); j++) |
| 111 | { |
| 112 | const std::string& node_output = node->output(j); |
| 113 | const std::string& func_output = function.output(j); |
| 114 | |
| 115 | input_output_remap[func_output] = node_output; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // append function nodes to graph |
| 120 | { |
| 121 | graph->mutable_node()->Reserve(graph->node_size() + function.node_size()); |
| 122 | for (int j = 0; j < function.node_size(); j++) |
| 123 | { |
| 124 | onnx::NodeProto* inlined_node = graph->add_node(); |
| 125 | inlined_node->CopyFrom(function.node(j)); |
| 126 | |
| 127 | // prefix with caller node name |
| 128 | inlined_node->set_name(node->name() + "/" + inlined_node->name()); |
| 129 | |
| 130 | // reset input output |
| 131 | for (int j = 0; j < inlined_node->input_size(); j++) |
| 132 | { |
| 133 | const std::string& node_input = inlined_node->input(j); |
| 134 | if (input_output_remap.find(node_input) != input_output_remap.end()) |
| 135 | { |
| 136 | inlined_node->set_input(j, input_output_remap.at(node_input)); |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | // graph->add_value_info()->set_name(node->name() + "/" + node_input); |
| 141 | inlined_node->set_input(j, node->name() + "/" + node_input); |
| 142 | } |
| 143 | } |
| 144 | for (int j = 0; j < inlined_node->output_size(); j++) |
| 145 | { |
| 146 | const std::string& node_output = inlined_node->output(j); |
| 147 | if (input_output_remap.find(node_output) != input_output_remap.end()) |
| 148 | { |
| 149 | inlined_node->set_output(j, input_output_remap.at(node_output)); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | // graph->add_value_info()->set_name(node->name() + "/" + node_output); |
| 154 | inlined_node->set_output(j, node->name() + "/" + node_output); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // swap inlined function nodes to caller |
| 161 | { |
| 162 | // ..... cni ....... 0 1 2 3 4 |
| 163 | const int graph_node_size = graph->node_size(); |
| 164 | for (int j = 0; j < function.node_size(); j++) |
| 165 | { |
| 166 | for (int k = graph_node_size - 1; k > i; k--) |
| 167 | { |
| 168 | graph->mutable_node()->SwapElements(k, k - 1); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // ..... 0 1 2 3 4 cni ....... |
| 173 | for (int j = i + function.node_size(); j < graph_node_size - 1; j++) |
| 174 | { |
| 175 | graph->mutable_node()->SwapElements(j, j + 1); |
| 176 | } |
| 177 | |
| 178 | // ..... 0 1 2 3 4 ....... cni |
| 179 | graph->mutable_node()->RemoveLast(); |
| 180 | } |
| 181 | |
| 182 | // inlined node may be function |
| 183 | i -= 1; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | } // namespace onnx2pnnx |
| 188 | |
| 189 | } // namespace pnnx |