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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 <cstddef>
  16. #include <ctype.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <string>
  20. #include <vector>
  21. static std::vector<std::string> layer_names;
  22. static std::vector<std::string> blob_names;
  23. static int find_blob_index_by_name(const char* name)
  24. {
  25. for (std::size_t i = 0; i < blob_names.size(); i++)
  26. {
  27. if (blob_names[i] == name)
  28. {
  29. return static_cast<int>(i);
  30. }
  31. }
  32. fprintf(stderr, "find_blob_index_by_name %s failed\n", name);
  33. return -1;
  34. }
  35. static void sanitize_name(char* name)
  36. {
  37. for (std::size_t i = 0; i < strlen(name); i++)
  38. {
  39. if (!isalnum(name[i]))
  40. {
  41. name[i] = '_';
  42. }
  43. }
  44. }
  45. static std::string path_to_varname(const char* path)
  46. {
  47. const char* lastslash = strrchr(path, '/');
  48. const char* name = lastslash == NULL ? path : lastslash + 1;
  49. std::string varname = name;
  50. sanitize_name((char*)varname.c_str());
  51. return varname;
  52. }
  53. static bool vstr_is_float(const char vstr[16])
  54. {
  55. // look ahead for determine isfloat
  56. for (int j = 0; j < 16; j++)
  57. {
  58. if (vstr[j] == '\0')
  59. break;
  60. if (vstr[j] == '.' || tolower(vstr[j]) == 'e')
  61. return true;
  62. }
  63. return false;
  64. }
  65. static int dump_param(const char* parampath, const char* parambinpath, const char* idcpppath)
  66. {
  67. FILE* fp = fopen(parampath, "rb");
  68. if (!fp)
  69. {
  70. fprintf(stderr, "fopen %s failed\n", parampath);
  71. return -1;
  72. }
  73. FILE* mp = fopen(parambinpath, "wb");
  74. FILE* ip = fopen(idcpppath, "wb");
  75. std::string param_var = path_to_varname(parampath);
  76. std::string include_guard_var = path_to_varname(idcpppath);
  77. fprintf(ip, "#ifndef NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  78. fprintf(ip, "#define NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  79. fprintf(ip, "namespace %s_id {\n", param_var.c_str());
  80. int nscan = 0;
  81. int magic = 0;
  82. nscan = fscanf(fp, "%d", &magic);
  83. if (nscan != 1)
  84. {
  85. fprintf(stderr, "read magic failed %d\n", nscan);
  86. return -1;
  87. }
  88. fwrite(&magic, sizeof(int), 1, mp);
  89. int layer_count = 0;
  90. int blob_count = 0;
  91. nscan = fscanf(fp, "%d %d", &layer_count, &blob_count);
  92. if (nscan != 2)
  93. {
  94. fprintf(stderr, "read layer_count and blob_count failed %d\n", nscan);
  95. return -1;
  96. }
  97. fwrite(&layer_count, sizeof(int), 1, mp);
  98. fwrite(&blob_count, sizeof(int), 1, mp);
  99. layer_names.resize(layer_count);
  100. blob_names.resize(blob_count);
  101. int blob_index = 0;
  102. for (int i = 0; i < layer_count; i++)
  103. {
  104. char layer_type[33];
  105. char layer_name[257];
  106. int bottom_count = 0;
  107. int top_count = 0;
  108. nscan = fscanf(fp, "%32s %256s %d %d", layer_type, layer_name, &bottom_count, &top_count);
  109. if (nscan != 4)
  110. {
  111. fprintf(stderr, "read layer params failed %d\n", nscan);
  112. return -1;
  113. }
  114. sanitize_name(layer_name);
  115. int typeindex = ncnn::layer_to_index(layer_type);
  116. fwrite(&typeindex, sizeof(int), 1, mp);
  117. fwrite(&bottom_count, sizeof(int), 1, mp);
  118. fwrite(&top_count, sizeof(int), 1, mp);
  119. fprintf(ip, "const int LAYER_%s = %d;\n", layer_name, i);
  120. // layer->bottoms.resize(bottom_count);
  121. for (int j = 0; j < bottom_count; j++)
  122. {
  123. char bottom_name[257];
  124. nscan = fscanf(fp, "%256s", bottom_name);
  125. if (nscan != 1)
  126. {
  127. fprintf(stderr, "read bottom_name failed %d\n", nscan);
  128. return -1;
  129. }
  130. sanitize_name(bottom_name);
  131. int bottom_blob_index = find_blob_index_by_name(bottom_name);
  132. fwrite(&bottom_blob_index, sizeof(int), 1, mp);
  133. }
  134. // layer->tops.resize(top_count);
  135. for (int j = 0; j < top_count; j++)
  136. {
  137. char blob_name[257];
  138. nscan = fscanf(fp, "%256s", blob_name);
  139. if (nscan != 1)
  140. {
  141. fprintf(stderr, "read blob_name failed %d\n", nscan);
  142. return -1;
  143. }
  144. sanitize_name(blob_name);
  145. blob_names[blob_index] = std::string(blob_name);
  146. fprintf(ip, "const int BLOB_%s = %d;\n", blob_name, blob_index);
  147. fwrite(&blob_index, sizeof(int), 1, mp);
  148. blob_index++;
  149. }
  150. // dump layer specific params
  151. // parse each key=value pair
  152. int id = 0;
  153. while (fscanf(fp, "%d=", &id) == 1)
  154. {
  155. fwrite(&id, sizeof(int), 1, mp);
  156. bool is_array = id <= -23300;
  157. if (is_array)
  158. {
  159. int len = 0;
  160. nscan = fscanf(fp, "%d", &len);
  161. if (nscan != 1)
  162. {
  163. fprintf(stderr, "read array length failed %d\n", nscan);
  164. return -1;
  165. }
  166. fwrite(&len, sizeof(int), 1, mp);
  167. for (int j = 0; j < len; j++)
  168. {
  169. char vstr[16];
  170. nscan = fscanf(fp, ",%15[^,\n ]", vstr);
  171. if (nscan != 1)
  172. {
  173. fprintf(stderr, "read array element failed %d\n", nscan);
  174. return -1;
  175. }
  176. bool is_float = vstr_is_float(vstr);
  177. if (is_float)
  178. {
  179. float vf;
  180. sscanf(vstr, "%f", &vf);
  181. fwrite(&vf, sizeof(float), 1, mp);
  182. }
  183. else
  184. {
  185. int v;
  186. sscanf(vstr, "%d", &v);
  187. fwrite(&v, sizeof(int), 1, mp);
  188. }
  189. }
  190. }
  191. else
  192. {
  193. char vstr[16];
  194. nscan = fscanf(fp, "%15s", vstr);
  195. if (nscan != 1)
  196. {
  197. fprintf(stderr, "read value failed %d\n", nscan);
  198. return -1;
  199. }
  200. bool is_float = vstr_is_float(vstr);
  201. if (is_float)
  202. {
  203. float vf;
  204. sscanf(vstr, "%f", &vf);
  205. fwrite(&vf, sizeof(float), 1, mp);
  206. }
  207. else
  208. {
  209. int v;
  210. sscanf(vstr, "%d", &v);
  211. fwrite(&v, sizeof(int), 1, mp);
  212. }
  213. }
  214. }
  215. int EOP = -233;
  216. fwrite(&EOP, sizeof(int), 1, mp);
  217. layer_names[i] = std::string(layer_name);
  218. }
  219. fprintf(ip, "} // namespace %s_id\n", param_var.c_str());
  220. fprintf(ip, "#endif // NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  221. fclose(fp);
  222. fclose(mp);
  223. fclose(ip);
  224. return 0;
  225. }
  226. static int write_memcpp(const char* parambinpath, const char* modelpath, const char* memcpppath)
  227. {
  228. FILE* cppfp = fopen(memcpppath, "wb");
  229. // dump param
  230. std::string param_var = path_to_varname(parambinpath);
  231. std::string include_guard_var = path_to_varname(memcpppath);
  232. FILE* mp = fopen(parambinpath, "rb");
  233. if (!mp)
  234. {
  235. fprintf(stderr, "fopen %s failed\n", parambinpath);
  236. return -1;
  237. }
  238. fprintf(cppfp, "#ifndef NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  239. fprintf(cppfp, "#define NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  240. fprintf(cppfp, "\n#ifdef _MSC_VER\n__declspec(align(4))\n#else\n__attribute__((aligned(4)))\n#endif\n");
  241. fprintf(cppfp, "static const unsigned char %s[] = {\n", param_var.c_str());
  242. int i = 0;
  243. while (!feof(mp))
  244. {
  245. int c = fgetc(mp);
  246. if (c == EOF)
  247. break;
  248. fprintf(cppfp, "0x%02x,", c);
  249. i++;
  250. if (i % 16 == 0)
  251. {
  252. fprintf(cppfp, "\n");
  253. }
  254. }
  255. fprintf(cppfp, "};\n");
  256. fclose(mp);
  257. // dump model
  258. std::string model_var = path_to_varname(modelpath);
  259. FILE* bp = fopen(modelpath, "rb");
  260. if (!bp)
  261. {
  262. fprintf(stderr, "fopen %s failed\n", modelpath);
  263. return -1;
  264. }
  265. fprintf(cppfp, "\n#ifdef _MSC_VER\n__declspec(align(4))\n#else\n__attribute__((aligned(4)))\n#endif\n");
  266. fprintf(cppfp, "static const unsigned char %s[] = {\n", model_var.c_str());
  267. i = 0;
  268. while (!feof(bp))
  269. {
  270. int c = fgetc(bp);
  271. if (c == EOF)
  272. break;
  273. fprintf(cppfp, "0x%02x,", c);
  274. i++;
  275. if (i % 16 == 0)
  276. {
  277. fprintf(cppfp, "\n");
  278. }
  279. }
  280. fprintf(cppfp, "};\n");
  281. fprintf(cppfp, "#endif // NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  282. fclose(bp);
  283. fclose(cppfp);
  284. return 0;
  285. }
  286. int main(int argc, char** argv)
  287. {
  288. if (argc != 5)
  289. {
  290. fprintf(stderr, "Usage: %s [ncnnproto] [ncnnbin] [idcpppath] [memcpppath]\n", argv[0]);
  291. return -1;
  292. }
  293. const char* parampath = argv[1];
  294. const char* modelpath = argv[2];
  295. const char* idcpppath = argv[3];
  296. const char* memcpppath = argv[4];
  297. std::string parambinpath = std::string(parampath) + ".bin";
  298. dump_param(parampath, parambinpath.c_str(), idcpppath);
  299. write_memcpp(parambinpath.c_str(), modelpath, memcpppath);
  300. return 0;
  301. }