You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ncnn2mem.cpp 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 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. #include "layer.h"
  15. #include "layer_type.h"
  16. #include <cstddef>
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <string>
  21. #include <vector>
  22. static std::vector<std::string> layer_names;
  23. static std::vector<std::string> blob_names;
  24. static int find_blob_index_by_name(const char* name)
  25. {
  26. for (std::size_t i = 0; i < blob_names.size(); i++)
  27. {
  28. if (blob_names[i] == name)
  29. {
  30. return static_cast<int>(i);
  31. }
  32. }
  33. fprintf(stderr, "find_blob_index_by_name %s failed\n", name);
  34. return -1;
  35. }
  36. static void sanitize_name(char* name)
  37. {
  38. for (std::size_t i = 0; i < strlen(name); i++)
  39. {
  40. if (!isalnum(name[i]))
  41. {
  42. name[i] = '_';
  43. }
  44. }
  45. }
  46. static std::string path_to_varname(const char* path)
  47. {
  48. const char* lastslash = strrchr(path, '/');
  49. const char* name = lastslash == NULL ? path : lastslash + 1;
  50. std::string varname = name;
  51. sanitize_name((char*)varname.c_str());
  52. return varname;
  53. }
  54. static bool vstr_is_float(const char vstr[16])
  55. {
  56. // look ahead for determine isfloat
  57. for (int j = 0; j < 16; j++)
  58. {
  59. if (vstr[j] == '\0')
  60. break;
  61. if (vstr[j] == '.' || tolower(vstr[j]) == 'e')
  62. return true;
  63. }
  64. return false;
  65. }
  66. static float vstr_to_float(const char vstr[16])
  67. {
  68. double v = 0.0;
  69. const char* p = vstr;
  70. // sign
  71. bool sign = *p != '-';
  72. if (*p == '+' || *p == '-')
  73. {
  74. p++;
  75. }
  76. // digits before decimal point or exponent
  77. unsigned int v1 = 0;
  78. while (isdigit(*p))
  79. {
  80. v1 = v1 * 10 + (*p - '0');
  81. p++;
  82. }
  83. v = (double)v1;
  84. // digits after decimal point
  85. if (*p == '.')
  86. {
  87. p++;
  88. unsigned int pow10 = 1;
  89. unsigned int v2 = 0;
  90. while (isdigit(*p))
  91. {
  92. v2 = v2 * 10 + (*p - '0');
  93. pow10 *= 10;
  94. p++;
  95. }
  96. v += v2 / (double)pow10;
  97. }
  98. // exponent
  99. if (*p == 'e' || *p == 'E')
  100. {
  101. p++;
  102. // sign of exponent
  103. bool fact = *p != '-';
  104. if (*p == '+' || *p == '-')
  105. {
  106. p++;
  107. }
  108. // digits of exponent
  109. unsigned int expon = 0;
  110. while (isdigit(*p))
  111. {
  112. expon = expon * 10 + (*p - '0');
  113. p++;
  114. }
  115. double scale = 1.0;
  116. while (expon >= 8)
  117. {
  118. scale *= 1e8;
  119. expon -= 8;
  120. }
  121. while (expon > 0)
  122. {
  123. scale *= 10.0;
  124. expon -= 1;
  125. }
  126. v = fact ? v * scale : v / scale;
  127. }
  128. // fprintf(stderr, "v = %f\n", v);
  129. return sign ? (float)v : (float)-v;
  130. }
  131. static int dump_param(const char* parampath, const char* parambinpath, const char* idcpppath)
  132. {
  133. FILE* fp = fopen(parampath, "rb");
  134. if (!fp)
  135. {
  136. fprintf(stderr, "fopen %s failed\n", parampath);
  137. return -1;
  138. }
  139. FILE* mp = fopen(parambinpath, "wb");
  140. FILE* ip = fopen(idcpppath, "wb");
  141. std::string param_var = path_to_varname(parampath);
  142. std::string include_guard_var = path_to_varname(idcpppath);
  143. fprintf(ip, "#ifndef NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  144. fprintf(ip, "#define NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  145. fprintf(ip, "namespace %s_id {\n", param_var.c_str());
  146. int nscan = 0;
  147. int magic = 0;
  148. nscan = fscanf(fp, "%d", &magic);
  149. if (nscan != 1)
  150. {
  151. fprintf(stderr, "read magic failed %d\n", nscan);
  152. return -1;
  153. }
  154. fwrite(&magic, sizeof(int), 1, mp);
  155. int layer_count = 0;
  156. int blob_count = 0;
  157. nscan = fscanf(fp, "%d %d", &layer_count, &blob_count);
  158. if (nscan != 2)
  159. {
  160. fprintf(stderr, "read layer_count and blob_count failed %d\n", nscan);
  161. return -1;
  162. }
  163. fwrite(&layer_count, sizeof(int), 1, mp);
  164. fwrite(&blob_count, sizeof(int), 1, mp);
  165. layer_names.resize(layer_count);
  166. blob_names.resize(blob_count);
  167. std::vector<std::string> custom_layer_index;
  168. int blob_index = 0;
  169. for (int i = 0; i < layer_count; i++)
  170. {
  171. char layer_type[33];
  172. char layer_name[257];
  173. int bottom_count = 0;
  174. int top_count = 0;
  175. nscan = fscanf(fp, "%32s %256s %d %d", layer_type, layer_name, &bottom_count, &top_count);
  176. if (nscan != 4)
  177. {
  178. fprintf(stderr, "read layer params failed %d\n", nscan);
  179. return -1;
  180. }
  181. sanitize_name(layer_name);
  182. int typeindex = ncnn::layer_to_index(layer_type);
  183. if (typeindex == -1)
  184. {
  185. // lookup custom_layer_index
  186. for (size_t j = 0; j < custom_layer_index.size(); j++)
  187. {
  188. if (custom_layer_index[j] == layer_type)
  189. {
  190. typeindex = ncnn::LayerType::CustomBit | j;
  191. break;
  192. }
  193. }
  194. if (typeindex == -1)
  195. {
  196. // new custom layer type
  197. size_t j = custom_layer_index.size();
  198. custom_layer_index.push_back(layer_type);
  199. typeindex = ncnn::LayerType::CustomBit | j;
  200. }
  201. }
  202. fwrite(&typeindex, sizeof(int), 1, mp);
  203. fwrite(&bottom_count, sizeof(int), 1, mp);
  204. fwrite(&top_count, sizeof(int), 1, mp);
  205. fprintf(ip, "const int LAYER_%s = %d;\n", layer_name, i);
  206. // layer->bottoms.resize(bottom_count);
  207. for (int j = 0; j < bottom_count; j++)
  208. {
  209. char bottom_name[257];
  210. nscan = fscanf(fp, "%256s", bottom_name);
  211. if (nscan != 1)
  212. {
  213. fprintf(stderr, "read bottom_name failed %d\n", nscan);
  214. return -1;
  215. }
  216. sanitize_name(bottom_name);
  217. int bottom_blob_index = find_blob_index_by_name(bottom_name);
  218. fwrite(&bottom_blob_index, sizeof(int), 1, mp);
  219. }
  220. // layer->tops.resize(top_count);
  221. for (int j = 0; j < top_count; j++)
  222. {
  223. char blob_name[257];
  224. nscan = fscanf(fp, "%256s", blob_name);
  225. if (nscan != 1)
  226. {
  227. fprintf(stderr, "read blob_name failed %d\n", nscan);
  228. return -1;
  229. }
  230. sanitize_name(blob_name);
  231. blob_names[blob_index] = std::string(blob_name);
  232. fprintf(ip, "const int BLOB_%s = %d;\n", blob_name, blob_index);
  233. fwrite(&blob_index, sizeof(int), 1, mp);
  234. blob_index++;
  235. }
  236. // dump layer specific params
  237. // parse each key=value pair
  238. int id = 0;
  239. while (fscanf(fp, "%d=", &id) == 1)
  240. {
  241. fwrite(&id, sizeof(int), 1, mp);
  242. bool is_array = id <= -23300;
  243. if (is_array)
  244. {
  245. int len = 0;
  246. nscan = fscanf(fp, "%d", &len);
  247. if (nscan != 1)
  248. {
  249. fprintf(stderr, "read array length failed %d\n", nscan);
  250. return -1;
  251. }
  252. fwrite(&len, sizeof(int), 1, mp);
  253. for (int j = 0; j < len; j++)
  254. {
  255. char vstr[16];
  256. nscan = fscanf(fp, ",%15[^,\n ]", vstr);
  257. if (nscan != 1)
  258. {
  259. fprintf(stderr, "read array element failed %d\n", nscan);
  260. return -1;
  261. }
  262. bool is_float = vstr_is_float(vstr);
  263. if (is_float)
  264. {
  265. float vf = vstr_to_float(vstr);
  266. fwrite(&vf, sizeof(float), 1, mp);
  267. }
  268. else
  269. {
  270. int v;
  271. sscanf(vstr, "%d", &v);
  272. fwrite(&v, sizeof(int), 1, mp);
  273. }
  274. }
  275. }
  276. else
  277. {
  278. char vstr[16];
  279. nscan = fscanf(fp, "%15s", vstr);
  280. if (nscan != 1)
  281. {
  282. fprintf(stderr, "read value failed %d\n", nscan);
  283. return -1;
  284. }
  285. bool is_float = vstr_is_float(vstr);
  286. if (is_float)
  287. {
  288. float vf = vstr_to_float(vstr);
  289. fwrite(&vf, sizeof(float), 1, mp);
  290. }
  291. else
  292. {
  293. int v;
  294. sscanf(vstr, "%d", &v);
  295. fwrite(&v, sizeof(int), 1, mp);
  296. }
  297. }
  298. }
  299. int EOP = -233;
  300. fwrite(&EOP, sizeof(int), 1, mp);
  301. layer_names[i] = std::string(layer_name);
  302. }
  303. // dump custom layer index
  304. for (size_t j = 0; j < custom_layer_index.size(); j++)
  305. {
  306. const std::string& layer_type = custom_layer_index[j];
  307. int typeindex = ncnn::LayerType::CustomBit | j;
  308. fprintf(ip, "const int TYPEINDEX_%s = %d;\n", layer_type.c_str(), typeindex);
  309. fprintf(stderr, "net.register_custom_layer(%s_id::TYPEINDEX_%s, %s_layer_creator);\n", param_var.c_str(), layer_type.c_str(), layer_type.c_str());
  310. }
  311. fprintf(ip, "} // namespace %s_id\n", param_var.c_str());
  312. fprintf(ip, "#endif // NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  313. fclose(fp);
  314. fclose(mp);
  315. fclose(ip);
  316. return 0;
  317. }
  318. static int write_memcpp(const char* parambinpath, const char* modelpath, const char* memcpppath)
  319. {
  320. FILE* cppfp = fopen(memcpppath, "wb");
  321. // dump param
  322. std::string param_var = path_to_varname(parambinpath);
  323. std::string include_guard_var = path_to_varname(memcpppath);
  324. FILE* mp = fopen(parambinpath, "rb");
  325. if (!mp)
  326. {
  327. fprintf(stderr, "fopen %s failed\n", parambinpath);
  328. return -1;
  329. }
  330. fprintf(cppfp, "#ifndef NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  331. fprintf(cppfp, "#define NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  332. fprintf(cppfp, "\n#ifdef _MSC_VER\n__declspec(align(4))\n#else\n__attribute__((aligned(4)))\n#endif\n");
  333. fprintf(cppfp, "static const unsigned char %s[] = {\n", param_var.c_str());
  334. int i = 0;
  335. while (!feof(mp))
  336. {
  337. int c = fgetc(mp);
  338. if (c == EOF)
  339. break;
  340. fprintf(cppfp, "0x%02x,", c);
  341. i++;
  342. if (i % 16 == 0)
  343. {
  344. fprintf(cppfp, "\n");
  345. }
  346. }
  347. fprintf(cppfp, "};\n");
  348. fclose(mp);
  349. // dump model
  350. std::string model_var = path_to_varname(modelpath);
  351. FILE* bp = fopen(modelpath, "rb");
  352. if (!bp)
  353. {
  354. fprintf(stderr, "fopen %s failed\n", modelpath);
  355. return -1;
  356. }
  357. fprintf(cppfp, "\n#ifdef _MSC_VER\n__declspec(align(4))\n#else\n__attribute__((aligned(4)))\n#endif\n");
  358. fprintf(cppfp, "static const unsigned char %s[] = {\n", model_var.c_str());
  359. i = 0;
  360. while (!feof(bp))
  361. {
  362. int c = fgetc(bp);
  363. if (c == EOF)
  364. break;
  365. fprintf(cppfp, "0x%02x,", c);
  366. i++;
  367. if (i % 16 == 0)
  368. {
  369. fprintf(cppfp, "\n");
  370. }
  371. }
  372. fprintf(cppfp, "};\n");
  373. fprintf(cppfp, "#endif // NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  374. fclose(bp);
  375. fclose(cppfp);
  376. return 0;
  377. }
  378. int main(int argc, char** argv)
  379. {
  380. if (argc != 5)
  381. {
  382. fprintf(stderr, "Usage: %s [ncnnproto] [ncnnbin] [idcpppath] [memcpppath]\n", argv[0]);
  383. return -1;
  384. }
  385. const char* parampath = argv[1];
  386. const char* modelpath = argv[2];
  387. const char* idcpppath = argv[3];
  388. const char* memcpppath = argv[4];
  389. std::string parambinpath = std::string(parampath) + ".bin";
  390. dump_param(parampath, parambinpath.c_str(), idcpppath);
  391. write_memcpp(parambinpath.c_str(), modelpath, memcpppath);
  392. return 0;
  393. }