Elffuss BraidHub

kikocis/ncnn public

master · 12.9 KiBRawPermalink
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 "canonicalize.h"
16
17#include <map>
18#include <string>
19#include <unordered_set>
20
21namespace pnnx {
22
23namespace onnx2pnnx {
24
25static bool string_starts_with(const std::string& s, const std::string& s2)
26{
27 return strncmp(s.c_str(), s2.c_str(), s2.size()) == 0;
28}
29
30static void canonicalize_subgraph_nodes(onnx::GraphProto* graph, const std::unordered_set<std::string>& initializers, const std::map<std::string, std::string>& input_output_remap)
31{
32 for (int i = 0; i < graph->node_size(); i++)
33 {
34 onnx::NodeProto* node = graph->mutable_node(i);
35
36 // canonicalize name
37 // node->set_name(std::string("op_") + std::to_string(i));
38
39 // canonicalize node input output
40 {
41 for (int j = 0; j < node->input_size(); j++)
42 {
43 const std::string& node_input = node->input(j);
44
45 // some input/output may have empty name, it causes trouble, skip it
46 if (node_input.empty())
47 continue;
48
49 // skip initializer
50 if (initializers.find(node_input) != initializers.end())
51 continue;
52
53 if (input_output_remap.find(node_input) != input_output_remap.end())
54 {
55 node->set_input(j, input_output_remap.at(node_input));
56 }
57 }
58 for (int j = 0; j < node->output_size(); j++)
59 {
60 const std::string& node_output = node->output(j);
61
62 // some input/output may have empty name, it causes trouble, skip it
63 if (node_output.empty())
64 continue;
65
66 if (input_output_remap.find(node_output) != input_output_remap.end())
67 {
68 node->set_output(j, input_output_remap.at(node_output));
69 }
70 }
71 }
72 }
73}
74
75void canonicalize(onnx::ModelProto& model)
76{
77 // collect initializers
78 std::unordered_set<std::string> initializers;
79 {
80 const onnx::GraphProto& graph = model.graph();
81 for (int i = 0; i < graph.initializer_size(); i++)
82 {
83 initializers.insert(graph.initializer(i).name());
84 }
85 }
86
87 onnx::GraphProto* graph = model.mutable_graph();
88
89 std::map<std::string, std::string> function_remap;
90
91 std::map<std::string, std::string> input_output_remap;
92 int input_output_index = 0;
93
94 // canonicalize graph input output
95 {
96 for (int i = 0; i < graph->input_size(); i++)
97 {
98 onnx::ValueInfoProto* input = graph->mutable_input(i);
99
100 std::string new_name = std::string("in") + std::to_string(i);
101
102 // fprintf(stderr, "%s -> %s\n", input->name().c_str(), new_name.c_str());
103 input_output_remap[input->name()] = new_name;
104 input->set_name(new_name);
105 }
106 for (int i = 0; i < graph->output_size(); i++)
107 {
108 onnx::ValueInfoProto* output = graph->mutable_output(i);
109
110 std::string new_name = std::string("out") + std::to_string(i);
111
112 // fprintf(stderr, "%s -> %s\n", output->name().c_str(), new_name.c_str());
113 input_output_remap[output->name()] = new_name;
114 output->set_name(new_name);
115 }
116 }
117
118 for (int i = 0; i < graph->node_size(); i++)
119 {
120 onnx::NodeProto* node = graph->mutable_node(i);
121
122 // simplify type
123 {
124 const std::string& op_type = node->op_type();
125
126 if (node->domain().empty())
127 {
128 // native onnx op
129 // Constant
130 node->set_name(op_type + "_" + std::to_string(i));
131 }
132 else if (string_starts_with(op_type, "aten_"))
133 {
134 // aten_view
135 node->set_name(op_type.substr(5) + "_" + std::to_string(i));
136 }
137 else if (string_starts_with(op_type, "_aten_"))
138 {
139 node->set_name(op_type.substr(6) + "_" + std::to_string(i));
140 }
141 else if (string_starts_with(op_type, "prims_"))
142 {
143 // prims_convert_element_type
144 node->set_name(op_type.substr(6) + "_" + std::to_string(i));
145 }
146 else if (string_starts_with(op_type, "torch_nn_modules_") && !string_starts_with(op_type, "torch_nn_modules_container_"))
147 {
148 // torch_nn_modules_conv_Conv2d _conv1_1
149 // torch_nn_modules_batchnorm_BatchNorm2d _bn1_1
150 // torch_nn_modules_pooling_MaxPool2d _maxpool_1_3
151 // torch_nn_modules_linear_Linear _fc_1
152
153 if (function_remap.find(op_type) != function_remap.end())
154 {
155 node->set_op_type(function_remap.at(op_type));
156 }
157 else
158 {
159 // torch_nn_modules_conv_Conv2d_xyz -> nn_Conv2d_i
160 char nn_type[256];
161 int nconsumed = 0;
162 sscanf(op_type.c_str() + sizeof("torch_nn_modules_") - 1, "%*[^_]_%255[^_]_%n", nn_type, &nconsumed);
163
164 std::string new_op_type = std::string("nn_") + nn_type + "_" + std::to_string(i);
165
166 function_remap[op_type] = new_op_type;
167
168 node->set_op_type(new_op_type);
169 }
170 node->set_name(node->op_type().substr(3));
171 }
172 else
173 {
174 // unknown module ?
175 fprintf(stderr, "unexpected op_type %s\n", op_type.c_str());
176 node->set_name(std::string("op_") + std::to_string(i));
177 }
178 }
179
180 // canonicalize name
181 // node->set_name(std::string("op_") + std::to_string(i));
182
183 // canonicalize node input output
184 {
185 for (int j = 0; j < node->input_size(); j++)
186 {
187 const std::string& node_input = node->input(j);
188
189 // some input/output may have empty name, it causes trouble, skip it
190 if (node_input.empty())
191 continue;
192
193 // skip initializer
194 if (initializers.find(node_input) != initializers.end())
195 continue;
196
197 if (input_output_remap.find(node_input) != input_output_remap.end())
198 {
199 node->set_input(j, input_output_remap.at(node_input));
200 }
201 else
202 {
203 std::string new_name = std::string("pnnx_") + std::to_string(input_output_index);
204
205 // fprintf(stderr, "%s -> pnnx_%s\n", node_input.c_str(), new_name.c_str());
206
207 input_output_remap[node_input] = new_name;
208 node->set_input(j, new_name);
209 input_output_index++;
210 }
211 }
212 for (int j = 0; j < node->output_size(); j++)
213 {
214 const std::string& node_output = node->output(j);
215
216 // some input/output may have empty name, it causes trouble, skip it
217 if (node_output.empty())
218 continue;
219
220 if (input_output_remap.find(node_output) != input_output_remap.end())
221 {
222 node->set_output(j, input_output_remap.at(node_output));
223 }
224 else
225 {
226 std::string new_name = std::string("pnnx_") + std::to_string(input_output_index);
227
228 // fprintf(stderr, "%s -> pnnx_%s\n", node_output.c_str(), new_name.c_str());
229
230 input_output_remap[node_output] = new_name;
231 node->set_output(j, new_name);
232 input_output_index++;
233 }
234 }
235 }
236
237 // canonicalize node input output in subgraph
238 for (int j = 0; j < node->attribute_size(); j++)
239 {
240 onnx::AttributeProto* attr = node->mutable_attribute(j);
241
242 if (attr->type() == onnx::AttributeProto::GRAPH)
243 {
244 onnx::GraphProto* sg = attr->mutable_g();
245
246 canonicalize_subgraph_nodes(sg, initializers, input_output_remap);
247 }
248 if (attr->type() == onnx::AttributeProto::GRAPHS)
249 {
250 for (int k = 0; k < attr->graphs().size(); k++)
251 {
252 onnx::GraphProto* sg = attr->mutable_graphs(k);
253
254 canonicalize_subgraph_nodes(sg, initializers, input_output_remap);
255 }
256 }
257 }
258 }
259
260 // canonicalize all functions
261 for (int i = 0; i < model.functions_size(); i++)
262 {
263 onnx::FunctionProto* function = model.mutable_functions(i);
264
265 if (function_remap.find(function->name()) != function_remap.end())
266 {
267 function->set_name(function_remap.at(function->name()));
268 }
269
270 if (!string_starts_with(function->name(), "nn_"))
271 continue;
272
273 // simplify function input
274 int function_input_index = 0;
275 int function_output_index = 0;
276 std::map<std::string, std::string> function_input_output_remap;
277 for (int j = 0; j < function->input_size(); j++)
278 {
279 const std::string& func_input = function->input(j);
280
281 if (initializers.find(func_input) == initializers.end())
282 {
283 // input tensor
284 std::string new_name = std::string("in") + std::to_string(function_input_index);
285 function_input_output_remap[func_input] = new_name;
286 function->set_input(j, new_name);
287 function_input_index++;
288 }
289 else
290 {
291 // weights
292 // layer2.0.bn1.running_mean
293 size_t last_dot = func_input.find_last_of('.');
294 if (last_dot != std::string::npos)
295 {
296 std::string new_name = func_input.substr(last_dot + 1);
297 function_input_output_remap[func_input] = new_name;
298 function->set_input(j, new_name);
299 }
300 }
301 }
302 for (int j = 0; j < function->output_size(); j++)
303 {
304 const std::string& func_output = function->output(j);
305
306 // output tensor
307 std::string new_name = std::string("out") + std::to_string(function_output_index);
308 function_input_output_remap[func_output] = new_name;
309 function->set_output(j, new_name);
310 function_output_index++;
311 }
312
313 for (int j = 0; j < function->node_size(); j++)
314 {
315 onnx::NodeProto* node = function->mutable_node(j);
316
317 for (int k = 0; k < node->input_size(); k++)
318 {
319 const std::string& input = node->input(k);
320
321 if (function_input_output_remap.find(input) != function_input_output_remap.end())
322 {
323 node->set_input(k, function_input_output_remap[input]);
324 }
325 }
326 for (int k = 0; k < node->output_size(); k++)
327 {
328 const std::string& output = node->output(k);
329
330 if (function_input_output_remap.find(output) != function_input_output_remap.end())
331 {
332 node->set_output(k, function_input_output_remap[output]);
333 }
334 }
335 }
336 }
337
338 // canonicalize all initializers
339 // for (int i = 0; i < graph->initializer_size(); i++)
340 // {
341 // onnx::TensorProto* initializer = graph->mutable_initializer(i);
342 //
343 // if (input_output_remap.find(initializer->name()) == input_output_remap.end())
344 // {
345 // // skip initializers inside module function
346 // continue;
347 // }
348 //
349 // initializer->set_name(input_output_remap.at(initializer->name()));
350 // }
351
352 // canonicalize all values
353 for (int i = 0; i < graph->value_info_size(); i++)
354 {
355 onnx::ValueInfoProto* value = graph->mutable_value_info(i);
356
357 if (input_output_remap.find(value->name()) == input_output_remap.end())
358 {
359 // skip values inside module function
360 continue;
361 }
362
363 value->set_name(input_output_remap.at(value->name()));
364 }
365}
366
367} // namespace onnx2pnnx
368
369} // namespace pnnx