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

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