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 12 kB

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