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

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