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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 int dump_param(const char* parampath, const char* parambinpath, const char* idcpppath)
  67. {
  68. FILE* fp = fopen(parampath, "rb");
  69. if (!fp)
  70. {
  71. fprintf(stderr, "fopen %s failed\n", parampath);
  72. return -1;
  73. }
  74. FILE* mp = fopen(parambinpath, "wb");
  75. FILE* ip = fopen(idcpppath, "wb");
  76. std::string param_var = path_to_varname(parampath);
  77. std::string include_guard_var = path_to_varname(idcpppath);
  78. fprintf(ip, "#ifndef NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  79. fprintf(ip, "#define NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  80. fprintf(ip, "namespace %s_id {\n", param_var.c_str());
  81. int nscan = 0;
  82. int magic = 0;
  83. nscan = fscanf(fp, "%d", &magic);
  84. if (nscan != 1)
  85. {
  86. fprintf(stderr, "read magic failed %d\n", nscan);
  87. return -1;
  88. }
  89. fwrite(&magic, sizeof(int), 1, mp);
  90. int layer_count = 0;
  91. int blob_count = 0;
  92. nscan = fscanf(fp, "%d %d", &layer_count, &blob_count);
  93. if (nscan != 2)
  94. {
  95. fprintf(stderr, "read layer_count and blob_count failed %d\n", nscan);
  96. return -1;
  97. }
  98. fwrite(&layer_count, sizeof(int), 1, mp);
  99. fwrite(&blob_count, sizeof(int), 1, mp);
  100. layer_names.resize(layer_count);
  101. blob_names.resize(blob_count);
  102. std::vector<std::string> custom_layer_index;
  103. int blob_index = 0;
  104. for (int i = 0; i < layer_count; i++)
  105. {
  106. char layer_type[33];
  107. char layer_name[257];
  108. int bottom_count = 0;
  109. int top_count = 0;
  110. nscan = fscanf(fp, "%32s %256s %d %d", layer_type, layer_name, &bottom_count, &top_count);
  111. if (nscan != 4)
  112. {
  113. fprintf(stderr, "read layer params failed %d\n", nscan);
  114. return -1;
  115. }
  116. sanitize_name(layer_name);
  117. int typeindex = ncnn::layer_to_index(layer_type);
  118. if (typeindex == -1)
  119. {
  120. // lookup custom_layer_index
  121. for (size_t j = 0; j < custom_layer_index.size(); j++)
  122. {
  123. if (custom_layer_index[j] == layer_type)
  124. {
  125. typeindex = ncnn::LayerType::CustomBit | j;
  126. break;
  127. }
  128. }
  129. if (typeindex == -1)
  130. {
  131. // new custom layer type
  132. size_t j = custom_layer_index.size();
  133. custom_layer_index.push_back(layer_type);
  134. typeindex = ncnn::LayerType::CustomBit | j;
  135. }
  136. }
  137. fwrite(&typeindex, sizeof(int), 1, mp);
  138. fwrite(&bottom_count, sizeof(int), 1, mp);
  139. fwrite(&top_count, sizeof(int), 1, mp);
  140. fprintf(ip, "const int LAYER_%s = %d;\n", layer_name, i);
  141. // layer->bottoms.resize(bottom_count);
  142. for (int j = 0; j < bottom_count; j++)
  143. {
  144. char bottom_name[257];
  145. nscan = fscanf(fp, "%256s", bottom_name);
  146. if (nscan != 1)
  147. {
  148. fprintf(stderr, "read bottom_name failed %d\n", nscan);
  149. return -1;
  150. }
  151. sanitize_name(bottom_name);
  152. int bottom_blob_index = find_blob_index_by_name(bottom_name);
  153. fwrite(&bottom_blob_index, sizeof(int), 1, mp);
  154. }
  155. // layer->tops.resize(top_count);
  156. for (int j = 0; j < top_count; j++)
  157. {
  158. char blob_name[257];
  159. nscan = fscanf(fp, "%256s", blob_name);
  160. if (nscan != 1)
  161. {
  162. fprintf(stderr, "read blob_name failed %d\n", nscan);
  163. return -1;
  164. }
  165. sanitize_name(blob_name);
  166. blob_names[blob_index] = std::string(blob_name);
  167. fprintf(ip, "const int BLOB_%s = %d;\n", blob_name, blob_index);
  168. fwrite(&blob_index, sizeof(int), 1, mp);
  169. blob_index++;
  170. }
  171. // dump layer specific params
  172. // parse each key=value pair
  173. int id = 0;
  174. while (fscanf(fp, "%d=", &id) == 1)
  175. {
  176. fwrite(&id, sizeof(int), 1, mp);
  177. bool is_array = id <= -23300;
  178. if (is_array)
  179. {
  180. int len = 0;
  181. nscan = fscanf(fp, "%d", &len);
  182. if (nscan != 1)
  183. {
  184. fprintf(stderr, "read array length failed %d\n", nscan);
  185. return -1;
  186. }
  187. fwrite(&len, sizeof(int), 1, mp);
  188. for (int j = 0; j < len; j++)
  189. {
  190. char vstr[16];
  191. nscan = fscanf(fp, ",%15[^,\n ]", vstr);
  192. if (nscan != 1)
  193. {
  194. fprintf(stderr, "read array element failed %d\n", nscan);
  195. return -1;
  196. }
  197. bool is_float = vstr_is_float(vstr);
  198. if (is_float)
  199. {
  200. float vf;
  201. sscanf(vstr, "%f", &vf);
  202. fwrite(&vf, sizeof(float), 1, mp);
  203. }
  204. else
  205. {
  206. int v;
  207. sscanf(vstr, "%d", &v);
  208. fwrite(&v, sizeof(int), 1, mp);
  209. }
  210. }
  211. }
  212. else
  213. {
  214. char vstr[16];
  215. nscan = fscanf(fp, "%15s", vstr);
  216. if (nscan != 1)
  217. {
  218. fprintf(stderr, "read value failed %d\n", nscan);
  219. return -1;
  220. }
  221. bool is_float = vstr_is_float(vstr);
  222. if (is_float)
  223. {
  224. float vf;
  225. sscanf(vstr, "%f", &vf);
  226. fwrite(&vf, sizeof(float), 1, mp);
  227. }
  228. else
  229. {
  230. int v;
  231. sscanf(vstr, "%d", &v);
  232. fwrite(&v, sizeof(int), 1, mp);
  233. }
  234. }
  235. }
  236. int EOP = -233;
  237. fwrite(&EOP, sizeof(int), 1, mp);
  238. layer_names[i] = std::string(layer_name);
  239. }
  240. // dump custom layer index
  241. for (size_t j = 0; j < custom_layer_index.size(); j++)
  242. {
  243. const std::string& layer_type = custom_layer_index[j];
  244. int typeindex = ncnn::LayerType::CustomBit | j;
  245. fprintf(ip, "const int TYPEINDEX_%s = %d;\n", layer_type.c_str(), typeindex);
  246. 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());
  247. }
  248. fprintf(ip, "} // namespace %s_id\n", param_var.c_str());
  249. fprintf(ip, "#endif // NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  250. fclose(fp);
  251. fclose(mp);
  252. fclose(ip);
  253. return 0;
  254. }
  255. static int write_memcpp(const char* parambinpath, const char* modelpath, const char* memcpppath)
  256. {
  257. FILE* cppfp = fopen(memcpppath, "wb");
  258. // dump param
  259. std::string param_var = path_to_varname(parambinpath);
  260. std::string include_guard_var = path_to_varname(memcpppath);
  261. FILE* mp = fopen(parambinpath, "rb");
  262. if (!mp)
  263. {
  264. fprintf(stderr, "fopen %s failed\n", parambinpath);
  265. return -1;
  266. }
  267. fprintf(cppfp, "#ifndef NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  268. fprintf(cppfp, "#define NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  269. fprintf(cppfp, "\n#ifdef _MSC_VER\n__declspec(align(4))\n#else\n__attribute__((aligned(4)))\n#endif\n");
  270. fprintf(cppfp, "static const unsigned char %s[] = {\n", param_var.c_str());
  271. int i = 0;
  272. while (!feof(mp))
  273. {
  274. int c = fgetc(mp);
  275. if (c == EOF)
  276. break;
  277. fprintf(cppfp, "0x%02x,", c);
  278. i++;
  279. if (i % 16 == 0)
  280. {
  281. fprintf(cppfp, "\n");
  282. }
  283. }
  284. fprintf(cppfp, "};\n");
  285. fclose(mp);
  286. // dump model
  287. std::string model_var = path_to_varname(modelpath);
  288. FILE* bp = fopen(modelpath, "rb");
  289. if (!bp)
  290. {
  291. fprintf(stderr, "fopen %s failed\n", modelpath);
  292. return -1;
  293. }
  294. fprintf(cppfp, "\n#ifdef _MSC_VER\n__declspec(align(4))\n#else\n__attribute__((aligned(4)))\n#endif\n");
  295. fprintf(cppfp, "static const unsigned char %s[] = {\n", model_var.c_str());
  296. i = 0;
  297. while (!feof(bp))
  298. {
  299. int c = fgetc(bp);
  300. if (c == EOF)
  301. break;
  302. fprintf(cppfp, "0x%02x,", c);
  303. i++;
  304. if (i % 16 == 0)
  305. {
  306. fprintf(cppfp, "\n");
  307. }
  308. }
  309. fprintf(cppfp, "};\n");
  310. fprintf(cppfp, "#endif // NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  311. fclose(bp);
  312. fclose(cppfp);
  313. return 0;
  314. }
  315. int main(int argc, char** argv)
  316. {
  317. if (argc != 5)
  318. {
  319. fprintf(stderr, "Usage: %s [ncnnproto] [ncnnbin] [idcpppath] [memcpppath]\n", argv[0]);
  320. return -1;
  321. }
  322. const char* parampath = argv[1];
  323. const char* modelpath = argv[2];
  324. const char* idcpppath = argv[3];
  325. const char* memcpppath = argv[4];
  326. std::string parambinpath = std::string(parampath) + ".bin";
  327. dump_param(parampath, parambinpath.c_str(), idcpppath);
  328. write_memcpp(parambinpath.c_str(), modelpath, memcpppath);
  329. return 0;
  330. }