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

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