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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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] == '.')
  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 layer_index = 0;
  86. int blob_index = 0;
  87. while (!feof(fp))
  88. {
  89. int nscan = 0;
  90. char layer_type[32];
  91. char layer_name[256];
  92. int bottom_count = 0;
  93. int top_count = 0;
  94. nscan = fscanf(fp, "%32s %256s %d %d", layer_type, layer_name, &bottom_count, &top_count);
  95. if (nscan != 4)
  96. {
  97. continue;
  98. }
  99. sanitize_name(layer_name);
  100. int typeindex = ncnn::layer_to_index(layer_type);
  101. fwrite(&typeindex, sizeof(int), 1, mp);
  102. fwrite(&bottom_count, sizeof(int), 1, mp);
  103. fwrite(&top_count, sizeof(int), 1, mp);
  104. fprintf(ip, "const int LAYER_%s = %d;\n", layer_name, layer_index);
  105. // layer->bottoms.resize(bottom_count);
  106. for (int i=0; i<bottom_count; i++)
  107. {
  108. char bottom_name[256];
  109. nscan = fscanf(fp, "%256s", bottom_name);
  110. if (nscan != 1)
  111. {
  112. continue;
  113. }
  114. sanitize_name(bottom_name);
  115. int bottom_blob_index = find_blob_index_by_name(bottom_name);
  116. fwrite(&bottom_blob_index, sizeof(int), 1, mp);
  117. }
  118. // layer->tops.resize(top_count);
  119. for (int i=0; i<top_count; i++)
  120. {
  121. char blob_name[256];
  122. nscan = fscanf(fp, "%256s", blob_name);
  123. if (nscan != 1)
  124. {
  125. continue;
  126. }
  127. sanitize_name(blob_name);
  128. blob_names[blob_index] = std::string(blob_name);
  129. fprintf(ip, "const int BLOB_%s = %d;\n", blob_name, blob_index);
  130. fwrite(&blob_index, sizeof(int), 1, mp);
  131. blob_index++;
  132. }
  133. // dump layer specific params
  134. // parse each key=value pair
  135. int id = 0;
  136. while (fscanf(fp, "%d=", &id) == 1)
  137. {
  138. fwrite(&id, sizeof(int), 1, mp);
  139. bool is_array = id <= -23300;
  140. if (is_array)
  141. {
  142. int len = 0;
  143. fscanf(fp, "%d", &len);
  144. fwrite(&len, sizeof(int), 1, mp);
  145. for (int j = 0; j < len; j++)
  146. {
  147. char vstr[16];
  148. fscanf(fp, ",%15[^,\n ]", vstr);
  149. bool is_float = vstr_is_float(vstr);
  150. if (is_float)
  151. {
  152. float vf;
  153. sscanf(vstr, "%f", &vf);
  154. fwrite(&vf, sizeof(float), 1, mp);
  155. }
  156. else
  157. {
  158. int v;
  159. sscanf(vstr, "%d", &v);
  160. fwrite(&v, sizeof(int), 1, mp);
  161. }
  162. }
  163. }
  164. else
  165. {
  166. char vstr[16];
  167. fscanf(fp, "%15s", vstr);
  168. bool is_float = vstr_is_float(vstr);
  169. if (is_float)
  170. {
  171. float vf;
  172. sscanf(vstr, "%f", &vf);
  173. fwrite(&vf, sizeof(float), 1, mp);
  174. }
  175. else
  176. {
  177. int v;
  178. sscanf(vstr, "%d", &v);
  179. fwrite(&v, sizeof(int), 1, mp);
  180. }
  181. }
  182. }
  183. int EOP = -233;
  184. fwrite(&EOP, sizeof(int), 1, mp);
  185. layer_names[layer_index] = std::string(layer_name);
  186. layer_index++;
  187. }
  188. fprintf(ip, "} // namespace %s_id\n", param_var.c_str());
  189. fprintf(ip, "#endif // NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  190. fclose(fp);
  191. fclose(mp);
  192. fclose(ip);
  193. return 0;
  194. }
  195. static int write_memcpp(const char* parambinpath, const char* modelpath, const char* memcpppath)
  196. {
  197. FILE* cppfp = fopen(memcpppath, "wb");
  198. // dump param
  199. std::string param_var = path_to_varname(parambinpath);
  200. std::string include_guard_var = path_to_varname(memcpppath);
  201. FILE* mp = fopen(parambinpath, "rb");
  202. fprintf(cppfp, "#ifndef NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  203. fprintf(cppfp, "#define NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  204. fprintf(cppfp, "static const unsigned char %s[] = {\n", param_var.c_str());
  205. int i = 0;
  206. while (!feof(mp))
  207. {
  208. int c = fgetc(mp);
  209. if (c == EOF)
  210. break;
  211. fprintf(cppfp, "0x%02x,", c);
  212. i++;
  213. if (i % 16 == 0)
  214. {
  215. fprintf(cppfp, "\n");
  216. }
  217. }
  218. fprintf(cppfp, "};\n");
  219. fclose(mp);
  220. // dump model
  221. std::string model_var = path_to_varname(modelpath);
  222. FILE* bp = fopen(modelpath, "rb");
  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. }