Elffuss BraidHub

kikocis/ncnn public

master · 11.4 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 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 <stdio.h>
16#include <stdint.h>
17#include <string.h>
18
19#include <algorithm>
20#include <string>
21#include <vector>
22
23#include "ir.h"
24#include "pass_level2.h"
25#include "pass_level3.h"
26#include "pass_level4.h"
27#include "pass_level5.h"
28
29#if BUILD_TORCH2PNNX
30#include "load_torchscript.h"
31#endif
32#if BUILD_ONNX2PNNX
33#include "load_onnx.h"
34#endif
35
36#include "pass_ncnn.h"
37#include "save_ncnn.h"
38
39#if BUILD_PNNX2ONNX
40#include "save_onnx.h"
41#endif
42
43static std::string get_basename(const std::string& path)
44{
45 std::string dirpath;
46 std::string filename;
47
48 size_t dirpos = path.find_last_of("/\\");
49 if (dirpos != std::string::npos)
50 {
51 dirpath = path.substr(0, dirpos + 1);
52 filename = path.substr(dirpos + 1);
53 }
54 else
55 {
56 filename = path;
57 }
58
59 std::string base = filename.substr(0, filename.find_last_of('.'));
60 // sanitize -
61 std::replace(base.begin(), base.end(), '-', '_');
62 return dirpath + base;
63}
64
65static void parse_string_list(char* s, std::vector<std::string>& list)
66{
67 list.clear();
68
69 char* pch = strtok(s, ",");
70 while (pch != NULL)
71 {
72 list.push_back(std::string(pch));
73
74 pch = strtok(NULL, ",");
75 }
76}
77
78static void print_string_list(const std::vector<std::string>& list)
79{
80 for (size_t i = 0; i < list.size(); i++)
81 {
82 fprintf(stderr, "%s", list[i].c_str());
83 if (i + 1 != list.size())
84 fprintf(stderr, ",");
85 }
86}
87
88static void parse_shape_list(char* s, std::vector<std::vector<int64_t> >& shapes, std::vector<std::string>& types)
89{
90 shapes.clear();
91 types.clear();
92
93 char* pch = strtok(s, "[]");
94 while (pch != NULL)
95 {
96 // assign user data type
97 if (!types.empty() && (pch[0] == 'b' || pch[0] == 'f' || pch[0] == 'i' || pch[0] == 'u' || pch[0] == 'c'))
98 {
99 char type[32];
100 int nscan = sscanf(pch, "%31[^,]", type);
101 if (nscan == 1)
102 {
103 types[types.size() - 1] = std::string(type);
104 }
105 }
106
107 // parse a,b,c
108 int v;
109 int nconsumed = 0;
110 int nscan = sscanf(pch, "%d%n", &v, &nconsumed);
111 if (nscan == 1)
112 {
113 // ok we get shape
114 pch += nconsumed;
115
116 std::vector<int64_t> s;
117 s.push_back(v);
118
119 nscan = sscanf(pch, ",%d%n", &v, &nconsumed);
120 while (nscan == 1)
121 {
122 pch += nconsumed;
123
124 s.push_back(v);
125
126 nscan = sscanf(pch, ",%d%n", &v, &nconsumed);
127 }
128
129 // shape end
130 shapes.push_back(s);
131 types.push_back("f32");
132 }
133
134 pch = strtok(NULL, "[]");
135 }
136}
137
138static void print_shape_list(const std::vector<std::vector<int64_t> >& shapes, const std::vector<std::string>& types)
139{
140 for (size_t i = 0; i < shapes.size(); i++)
141 {
142 const std::vector<int64_t>& s = shapes[i];
143 const std::string& t = types[i];
144 fprintf(stderr, "[");
145 for (size_t j = 0; j < s.size(); j++)
146 {
147 fprintf(stderr, "%ld", s[j]);
148 if (j != s.size() - 1)
149 fprintf(stderr, ",");
150 }
151 fprintf(stderr, "]");
152 fprintf(stderr, "%s", t.c_str());
153 if (i != shapes.size() - 1)
154 fprintf(stderr, ",");
155 }
156}
157
158static bool model_file_maybe_torchscript(const std::string& path)
159{
160 FILE* fp = fopen(path.c_str(), "rb");
161 if (!fp)
162 {
163 fprintf(stderr, "open failed %s\n", path.c_str());
164 return false;
165 }
166
167 uint32_t signature = 0;
168 fread((char*)&signature, sizeof(signature), 1, fp);
169
170 fclose(fp);
171
172 // torchscript is a zip
173 return signature == 0x04034b50;
174}
175
176static void show_usage()
177{
178 fprintf(stderr, "Usage: pnnx [model.pt] [(key=value)...]\n");
179 fprintf(stderr, " pnnxparam=model.pnnx.param\n");
180 fprintf(stderr, " pnnxbin=model.pnnx.bin\n");
181 fprintf(stderr, " pnnxpy=model_pnnx.py\n");
182 fprintf(stderr, " pnnxonnx=model.pnnx.onnx\n");
183 fprintf(stderr, " ncnnparam=model.ncnn.param\n");
184 fprintf(stderr, " ncnnbin=model.ncnn.bin\n");
185 fprintf(stderr, " ncnnpy=model_ncnn.py\n");
186 fprintf(stderr, " fp16=1\n");
187 fprintf(stderr, " optlevel=2\n");
188 fprintf(stderr, " device=cpu/gpu\n");
189 fprintf(stderr, " inputshape=[1,3,224,224],...\n");
190 fprintf(stderr, " inputshape2=[1,3,320,320],...\n");
191#if _WIN32
192 fprintf(stderr, " customop=C:\\Users\\nihui\\AppData\\Local\\torch_extensions\\torch_extensions\\Cache\\fused\\fused.dll,...\n");
193#else
194 fprintf(stderr, " customop=/home/nihui/.cache/torch_extensions/fused/fused.so,...\n");
195#endif
196 fprintf(stderr, " moduleop=models.common.Focus,models.yolo.Detect,...\n");
197 fprintf(stderr, "Sample usage: pnnx mobilenet_v2.pt inputshape=[1,3,224,224]\n");
198 fprintf(stderr, " pnnx yolov5s.pt inputshape=[1,3,640,640]f32 inputshape2=[1,3,320,320]f32 device=gpu moduleop=models.common.Focus,models.yolo.Detect\n");
199}
200
201int main(int argc, char** argv)
202{
203 if (argc < 2)
204 {
205 show_usage();
206 return -1;
207 }
208
209 for (int i = 1; i < argc; i++)
210 {
211 if (argv[i][0] == '-')
212 {
213 show_usage();
214 return -1;
215 }
216 }
217
218 std::string ptpath = std::string(argv[1]);
219
220 std::string ptbase = get_basename(ptpath);
221
222 std::string pnnxparampath = ptbase + ".pnnx.param";
223 std::string pnnxbinpath = ptbase + ".pnnx.bin";
224 std::string pnnxpypath = ptbase + "_pnnx.py";
225 std::string pnnxonnxpath = ptbase + ".pnnx.onnx";
226 std::string ncnnparampath = ptbase + ".ncnn.param";
227 std::string ncnnbinpath = ptbase + ".ncnn.bin";
228 std::string ncnnpypath = ptbase + "_ncnn.py";
229 int fp16 = 1;
230 int optlevel = 2;
231 std::string device = "cpu";
232 std::vector<std::vector<int64_t> > input_shapes;
233 std::vector<std::string> input_types;
234 std::vector<std::vector<int64_t> > input_shapes2;
235 std::vector<std::string> input_types2;
236 std::vector<std::string> customop_modules;
237 std::vector<std::string> module_operators;
238
239 for (int i = 2; i < argc; i++)
240 {
241 // key=value
242 char* kv = argv[i];
243
244 char* eqs = strchr(kv, '=');
245 if (eqs == NULL)
246 {
247 fprintf(stderr, "unrecognized arg %s\n", kv);
248 continue;
249 }
250
251 // split k v
252 eqs[0] = '\0';
253 const char* key = kv;
254 char* value = eqs + 1;
255
256 if (strcmp(key, "pnnxparam") == 0)
257 pnnxparampath = std::string(value);
258 if (strcmp(key, "pnnxbin") == 0)
259 pnnxbinpath = std::string(value);
260 if (strcmp(key, "pnnxpy") == 0)
261 pnnxpypath = std::string(value);
262 if (strcmp(key, "pnnxonnx") == 0)
263 pnnxonnxpath = std::string(value);
264 if (strcmp(key, "ncnnparam") == 0)
265 ncnnparampath = std::string(value);
266 if (strcmp(key, "ncnnbin") == 0)
267 ncnnbinpath = std::string(value);
268 if (strcmp(key, "ncnnpy") == 0)
269 ncnnpypath = std::string(value);
270 if (strcmp(key, "fp16") == 0)
271 fp16 = atoi(value);
272 if (strcmp(key, "optlevel") == 0)
273 optlevel = atoi(value);
274 if (strcmp(key, "device") == 0)
275 device = value;
276 if (strcmp(key, "inputshape") == 0)
277 parse_shape_list(value, input_shapes, input_types);
278 if (strcmp(key, "inputshape2") == 0)
279 parse_shape_list(value, input_shapes2, input_types2);
280 if (strcmp(key, "customop") == 0)
281 parse_string_list(value, customop_modules);
282 if (strcmp(key, "moduleop") == 0)
283 parse_string_list(value, module_operators);
284 }
285
286 // print options
287 {
288 fprintf(stderr, "pnnxparam = %s\n", pnnxparampath.c_str());
289 fprintf(stderr, "pnnxbin = %s\n", pnnxbinpath.c_str());
290 fprintf(stderr, "pnnxpy = %s\n", pnnxpypath.c_str());
291 fprintf(stderr, "pnnxonnx = %s\n", pnnxonnxpath.c_str());
292 fprintf(stderr, "ncnnparam = %s\n", ncnnparampath.c_str());
293 fprintf(stderr, "ncnnbin = %s\n", ncnnbinpath.c_str());
294 fprintf(stderr, "ncnnpy = %s\n", ncnnpypath.c_str());
295 fprintf(stderr, "fp16 = %d\n", fp16);
296 fprintf(stderr, "optlevel = %d\n", optlevel);
297 fprintf(stderr, "device = %s\n", device.c_str());
298 fprintf(stderr, "inputshape = ");
299 print_shape_list(input_shapes, input_types);
300 fprintf(stderr, "\n");
301 fprintf(stderr, "inputshape2 = ");
302 print_shape_list(input_shapes2, input_types2);
303 fprintf(stderr, "\n");
304 fprintf(stderr, "customop = ");
305 print_string_list(customop_modules);
306 fprintf(stderr, "\n");
307 fprintf(stderr, "moduleop = ");
308 print_string_list(module_operators);
309 fprintf(stderr, "\n");
310 }
311
312 std::set<std::string> foldable_constants;
313 std::string foldable_constants_zippath = ptbase + ".foldable_constants.zip";
314
315 pnnx::Graph pnnx_graph;
316#if BUILD_ONNX2PNNX
317 if (!model_file_maybe_torchscript(ptpath))
318 {
319 load_onnx(ptpath.c_str(), pnnx_graph,
320 input_shapes, input_types,
321 input_shapes2, input_types2);
322 }
323 else
324#endif
325 {
326 load_torchscript(ptpath, pnnx_graph,
327 device, input_shapes, input_types,
328 input_shapes2, input_types2,
329 customop_modules, module_operators,
330 foldable_constants_zippath, foldable_constants);
331 }
332
333 fprintf(stderr, "############# pass_level2\n");
334
335 pnnx::pass_level2(pnnx_graph);
336
337 pnnx_graph.save("debug.param", "debug.bin");
338
339 if (optlevel >= 1)
340 {
341 fprintf(stderr, "############# pass_level3\n");
342
343 pnnx::pass_level3(pnnx_graph, foldable_constants, foldable_constants_zippath);
344
345 fprintf(stderr, "############# pass_level4\n");
346
347 pnnx::pass_level4(pnnx_graph);
348 }
349
350 pnnx_graph.save("debug2.param", "debug2.bin");
351
352 if (optlevel >= 2)
353 {
354 fprintf(stderr, "############# pass_level5\n");
355
356 pnnx::pass_level5(pnnx_graph, foldable_constants, foldable_constants_zippath);
357 }
358
359 // delete foldable_constants_zippath
360 remove(foldable_constants_zippath.c_str());
361
362 pnnx_graph.save(pnnxparampath, pnnxbinpath);
363
364 pnnx_graph.python(pnnxpypath, pnnxbinpath);
365
366#if BUILD_PNNX2ONNX
367 pnnx::save_onnx(pnnx_graph, pnnxonnxpath.c_str(), fp16);
368#else
369 fprintf(stderr, "pnnx build without onnx-zero support, skip saving onnx\n");
370#endif
371
372 // if (optlevel >= 2)
373 {
374 fprintf(stderr, "############# pass_ncnn\n");
375
376 pnnx::pass_ncnn(pnnx_graph, module_operators);
377
378 pnnx::save_ncnn(pnnx_graph, ncnnparampath, ncnnbinpath, ncnnpypath, fp16);
379 }
380
381 // pnnx::Graph pnnx_graph2;
382
383 // pnnx_graph2.load("pnnx.param", "pnnx.bin");
384 // pnnx_graph2.save("pnnx2.param", "pnnx2.bin");
385
386 return 0;
387}