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.

ncnn2int8.cpp 28 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. // BUG1989 is pleased to support the open source community by supporting ncnn available.
  2. //
  3. // Copyright (C) 2019 BUG1989. 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. #ifdef _MSC_VER
  15. #define _CRT_SECURE_NO_DEPRECATE
  16. #endif
  17. #include <cstdio>
  18. #include <cstring>
  19. #include <map>
  20. #include <set>
  21. #include <vector>
  22. // ncnn public header
  23. #include "datareader.h"
  24. #include "layer.h"
  25. #include "layer_type.h"
  26. #include "net.h"
  27. // ncnn private header
  28. #include "../modelwriter.h"
  29. class DataReaderFromEmpty : public ncnn::DataReader
  30. {
  31. public:
  32. virtual int scan(const char* format, void* p) const
  33. {
  34. return 0;
  35. }
  36. virtual size_t read(void* buf, size_t size) const
  37. {
  38. memset(buf, 0, size);
  39. return size;
  40. }
  41. };
  42. static bool read_int8scale_table(const char* filepath, std::map<std::string, ncnn::Mat>& blob_int8scale_table, std::map<std::string, ncnn::Mat>& weight_int8scale_table)
  43. {
  44. blob_int8scale_table.clear();
  45. weight_int8scale_table.clear();
  46. FILE* fp = fopen(filepath, "rb");
  47. if (!fp)
  48. {
  49. fprintf(stderr, "Open %s failed.\n", filepath);
  50. return false;
  51. }
  52. std::string key_str;
  53. std::vector<float> scales;
  54. std::vector<char> line(10240000);
  55. char* pch = NULL;
  56. size_t len = 0;
  57. while (!feof(fp))
  58. {
  59. char* s = fgets(line.data(), (int)line.size(), fp);
  60. if (!s)
  61. break;
  62. float scale = 1.f;
  63. char key[256];
  64. line[strcspn(line.data(), "\r\n")] = 0;
  65. pch = strtok(line.data(), " ");
  66. if (pch == NULL) break;
  67. bool is_key = true;
  68. while (pch != NULL)
  69. {
  70. if (is_key)
  71. {
  72. sscanf(pch, "%255s", key);
  73. key_str = key;
  74. is_key = false;
  75. }
  76. else
  77. {
  78. sscanf(pch, "%f", &scale);
  79. scales.push_back(scale);
  80. }
  81. pch = strtok(NULL, " ");
  82. }
  83. // XYZ_param_N pattern
  84. if (strstr(key_str.c_str(), "_param_"))
  85. {
  86. weight_int8scale_table[key_str] = ncnn::Mat((int)scales.size(), (void*)scales.data()).clone();
  87. }
  88. else
  89. {
  90. blob_int8scale_table[key_str] = ncnn::Mat((int)scales.size(), (void*)scales.data()).clone();
  91. }
  92. key_str.clear();
  93. scales.clear();
  94. }
  95. fclose(fp);
  96. return true;
  97. }
  98. class NetQuantize : public ModelWriter
  99. {
  100. public:
  101. NetQuantize();
  102. std::map<std::string, ncnn::Mat> blob_int8scale_table;
  103. std::map<std::string, ncnn::Mat> weight_int8scale_table;
  104. public:
  105. int quantize_convolution();
  106. int quantize_convolutiondepthwise();
  107. int quantize_innerproduct();
  108. int quantize_rnn();
  109. int quantize_lstm();
  110. int quantize_gru();
  111. int fuse_requantize();
  112. };
  113. NetQuantize::NetQuantize()
  114. : ModelWriter()
  115. {
  116. }
  117. int NetQuantize::quantize_convolution()
  118. {
  119. const int layer_count = static_cast<int>(layers.size());
  120. for (int i = 0; i < layer_count; i++)
  121. {
  122. // find convolution layer
  123. if (layers[i]->type != "Convolution")
  124. continue;
  125. // find convolution layer
  126. std::map<std::string, ncnn::Mat>::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  127. if (iter_data == blob_int8scale_table.end())
  128. continue;
  129. char key[256];
  130. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  131. std::map<std::string, ncnn::Mat>::iterator iter = weight_int8scale_table.find(key);
  132. if (iter == weight_int8scale_table.end())
  133. {
  134. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  135. return -1;
  136. }
  137. // Convolution - quantize weight from fp32 to int8
  138. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  139. ncnn::Mat bottom_blob_int8_scales = iter_data->second;
  140. ncnn::Mat weight_data_int8_scales = iter->second;
  141. fprintf(stderr, "quantize_convolution %s\n", convolution->name.c_str());
  142. {
  143. const int maxk = convolution->kernel_w * convolution->kernel_h;
  144. const int num_input = convolution->weight_data_size / convolution->num_output / maxk;
  145. ncnn::Mat weight_data_r2 = convolution->weight_data.reshape(maxk, num_input, convolution->num_output);
  146. ncnn::Mat weight_data_int8;
  147. ncnn::Option opt_q = opt;
  148. opt_q.blob_allocator = convolution->weight_data.allocator;
  149. opt_q.use_packing_layout = false;
  150. ncnn::quantize_to_int8(weight_data_r2, weight_data_int8, weight_data_int8_scales, opt_q);
  151. if (weight_data_int8.empty())
  152. return -100;
  153. convolution->weight_data = weight_data_int8.reshape(convolution->weight_data_size);
  154. }
  155. convolution->int8_scale_term = 2;
  156. convolution->weight_data_int8_scales = weight_data_int8_scales;
  157. convolution->bottom_blob_int8_scales = bottom_blob_int8_scales;
  158. }
  159. return 0;
  160. }
  161. int NetQuantize::quantize_convolutiondepthwise()
  162. {
  163. const int layer_count = static_cast<int>(layers.size());
  164. for (int i = 0; i < layer_count; i++)
  165. {
  166. // find convolution layer
  167. if (layers[i]->type != "ConvolutionDepthWise")
  168. continue;
  169. // find convolutiondepthwise layer
  170. std::map<std::string, ncnn::Mat>::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  171. if (iter_data == blob_int8scale_table.end())
  172. continue;
  173. char key[256];
  174. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  175. std::map<std::string, ncnn::Mat>::iterator iter = weight_int8scale_table.find(key);
  176. if (iter == weight_int8scale_table.end())
  177. {
  178. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  179. return -1;
  180. }
  181. // Convolution - quantize weight from fp32 to int8
  182. ncnn::ConvolutionDepthWise* convdw = (ncnn::ConvolutionDepthWise*)layers[i];
  183. ncnn::Mat bottom_blob_int8_scales = iter_data->second;
  184. ncnn::Mat weight_data_int8_scales = iter->second;
  185. fprintf(stderr, "quantize_convolutiondepthwise %s\n", convdw->name.c_str());
  186. {
  187. ncnn::Mat int8_weight_data(convdw->weight_data_size, (size_t)1u);
  188. if (int8_weight_data.empty())
  189. return -100;
  190. const int weight_data_size_g = convdw->weight_data_size / convdw->group;
  191. for (int g = 0; g < convdw->group; g++)
  192. {
  193. ncnn::Option opt_q = opt;
  194. opt_q.blob_allocator = int8_weight_data.allocator;
  195. opt_q.use_packing_layout = false;
  196. const ncnn::Mat weight_data_g = convdw->weight_data.range(weight_data_size_g * g, weight_data_size_g);
  197. ncnn::Mat int8_weight_data_g = int8_weight_data.range(weight_data_size_g * g, weight_data_size_g);
  198. const ncnn::Mat weight_data_int8_scales_g = weight_data_int8_scales.range(g, 1);
  199. ncnn::quantize_to_int8(weight_data_g, int8_weight_data_g, weight_data_int8_scales_g, opt_q);
  200. }
  201. convdw->weight_data = int8_weight_data;
  202. }
  203. convdw->int8_scale_term = 1;
  204. convdw->weight_data_int8_scales = weight_data_int8_scales;
  205. convdw->bottom_blob_int8_scales = bottom_blob_int8_scales;
  206. }
  207. return 0;
  208. }
  209. int NetQuantize::quantize_innerproduct()
  210. {
  211. const int layer_count = static_cast<int>(layers.size());
  212. for (int i = 0; i < layer_count; i++)
  213. {
  214. // find convolution layer
  215. if (layers[i]->type != "InnerProduct")
  216. continue;
  217. // find InnerProduct layer
  218. std::map<std::string, ncnn::Mat>::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  219. if (iter_data == blob_int8scale_table.end())
  220. continue;
  221. char key[256];
  222. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  223. std::map<std::string, ncnn::Mat>::iterator iter = weight_int8scale_table.find(key);
  224. if (iter == weight_int8scale_table.end())
  225. {
  226. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  227. return -1;
  228. }
  229. // InnerProduct - quantize weight from fp32 to int8
  230. ncnn::InnerProduct* fc = (ncnn::InnerProduct*)layers[i];
  231. ncnn::Mat bottom_blob_int8_scales = iter_data->second;
  232. ncnn::Mat weight_data_int8_scales = iter->second;
  233. fprintf(stderr, "quantize_innerproduct %s\n", fc->name.c_str());
  234. {
  235. const int num_input = fc->weight_data_size / fc->num_output;
  236. ncnn::Mat weight_data_r2 = fc->weight_data.reshape(num_input, fc->num_output);
  237. ncnn::Mat weight_data_int8;
  238. ncnn::Option opt_q = opt;
  239. opt_q.use_packing_layout = false;
  240. ncnn::quantize_to_int8(weight_data_r2, weight_data_int8, weight_data_int8_scales, opt_q);
  241. if (weight_data_int8.empty())
  242. return -100;
  243. fc->weight_data = weight_data_int8.reshape(fc->weight_data_size);
  244. }
  245. fc->int8_scale_term = 2;
  246. fc->weight_data_int8_scales = weight_data_int8_scales;
  247. fc->bottom_blob_int8_scales = bottom_blob_int8_scales;
  248. }
  249. return 0;
  250. }
  251. int NetQuantize::quantize_rnn()
  252. {
  253. for (size_t i = 0; i < layers.size(); i++)
  254. {
  255. if (layers[i]->type != "RNN")
  256. continue;
  257. // RNN - quantize weight from fp32 to int8
  258. ncnn::RNN* rnn = (ncnn::RNN*)layers[i];
  259. fprintf(stderr, "quantize_rnn %s\n", rnn->name.c_str());
  260. // TODO move to ncnn2table
  261. const int num_directions = rnn->direction == 2 ? 2 : 1;
  262. const int size = rnn->weight_data_size / num_directions / rnn->num_output;
  263. ncnn::Mat weight_xc_data_int8_scales(rnn->num_output * num_directions);
  264. ncnn::Mat weight_hc_data_int8_scales(rnn->num_output * num_directions);
  265. for (int d = 0; d < num_directions; d++)
  266. {
  267. for (int q = 0; q < rnn->num_output; q++)
  268. {
  269. {
  270. const float* weight_xc_ptr = rnn->weight_xc_data.channel(d).row(q);
  271. float absmax = 0.f;
  272. for (int i = 0; i < size; i++)
  273. {
  274. absmax = std::max(absmax, (float)fabs(weight_xc_ptr[i]));
  275. }
  276. weight_xc_data_int8_scales[d * rnn->num_output + q] = 127 / absmax;
  277. }
  278. {
  279. const float* weight_hc_ptr = rnn->weight_hc_data.channel(d).row(q);
  280. float absmax = 0.f;
  281. for (int i = 0; i < size; i++)
  282. {
  283. absmax = std::max(absmax, (float)fabs(weight_hc_ptr[i]));
  284. }
  285. weight_hc_data_int8_scales[d * rnn->num_output + q] = 127 / absmax;
  286. }
  287. }
  288. }
  289. {
  290. ncnn::Mat weight_xc_data_r2 = rnn->weight_xc_data.reshape(size, rnn->num_output * num_directions);
  291. ncnn::Mat weight_xc_data_int8;
  292. ncnn::Option opt_q = opt;
  293. opt_q.blob_allocator = rnn->weight_xc_data.allocator;
  294. opt_q.use_packing_layout = false;
  295. ncnn::quantize_to_int8(weight_xc_data_r2, weight_xc_data_int8, weight_xc_data_int8_scales, opt_q);
  296. if (weight_xc_data_int8.empty())
  297. return -100;
  298. rnn->weight_xc_data = weight_xc_data_int8.reshape(size * rnn->num_output * num_directions);
  299. }
  300. {
  301. ncnn::Mat weight_hc_data_r2 = rnn->weight_hc_data.reshape(rnn->num_output, rnn->num_output * num_directions);
  302. ncnn::Mat weight_hc_data_int8;
  303. ncnn::Option opt_q = opt;
  304. opt_q.blob_allocator = rnn->weight_hc_data.allocator;
  305. opt_q.use_packing_layout = false;
  306. ncnn::quantize_to_int8(weight_hc_data_r2, weight_hc_data_int8, weight_hc_data_int8_scales, opt_q);
  307. if (weight_hc_data_int8.empty())
  308. return -100;
  309. rnn->weight_hc_data = weight_hc_data_int8.reshape(rnn->num_output * rnn->num_output * num_directions);
  310. }
  311. rnn->int8_scale_term = 2;
  312. rnn->weight_xc_data_int8_scales = weight_xc_data_int8_scales;
  313. rnn->weight_hc_data_int8_scales = weight_hc_data_int8_scales;
  314. }
  315. return 0;
  316. }
  317. int NetQuantize::quantize_lstm()
  318. {
  319. for (size_t i = 0; i < layers.size(); i++)
  320. {
  321. if (layers[i]->type != "LSTM")
  322. continue;
  323. // LSTM - quantize weight from fp32 to int8
  324. ncnn::LSTM* lstm = (ncnn::LSTM*)layers[i];
  325. fprintf(stderr, "quantize_lstm %s\n", lstm->name.c_str());
  326. // TODO move to ncnn2table
  327. const int num_directions = lstm->direction == 2 ? 2 : 1;
  328. const int size = lstm->weight_data_size / num_directions / lstm->hidden_size / 4;
  329. ncnn::Mat weight_xc_data_int8_scales(lstm->hidden_size * 4 * num_directions);
  330. ncnn::Mat weight_hc_data_int8_scales(lstm->hidden_size * 4 * num_directions);
  331. for (int d = 0; d < num_directions; d++)
  332. {
  333. for (int q = 0; q < lstm->hidden_size * 4; q++)
  334. {
  335. {
  336. const float* weight_xc_ptr = lstm->weight_xc_data.channel(d).row(q);
  337. float absmax = 0.f;
  338. for (int i = 0; i < size; i++)
  339. {
  340. absmax = std::max(absmax, (float)fabs(weight_xc_ptr[i]));
  341. }
  342. weight_xc_data_int8_scales[d * lstm->hidden_size * 4 + q] = 127 / absmax;
  343. }
  344. {
  345. const float* weight_hc_ptr = lstm->weight_hc_data.channel(d).row(q);
  346. float absmax = 0.f;
  347. for (int i = 0; i < size; i++)
  348. {
  349. absmax = std::max(absmax, (float)fabs(weight_hc_ptr[i]));
  350. }
  351. weight_hc_data_int8_scales[d * lstm->hidden_size * 4 + q] = 127 / absmax;
  352. }
  353. }
  354. }
  355. {
  356. ncnn::Mat weight_xc_data_r2 = lstm->weight_xc_data.reshape(size, lstm->hidden_size * 4 * num_directions);
  357. ncnn::Mat weight_xc_data_int8;
  358. ncnn::Option opt_q = opt;
  359. opt_q.blob_allocator = lstm->weight_xc_data.allocator;
  360. opt_q.use_packing_layout = false;
  361. ncnn::quantize_to_int8(weight_xc_data_r2, weight_xc_data_int8, weight_xc_data_int8_scales, opt_q);
  362. if (weight_xc_data_int8.empty())
  363. return -100;
  364. lstm->weight_xc_data = weight_xc_data_int8.reshape(size * lstm->hidden_size * 4 * num_directions);
  365. }
  366. {
  367. ncnn::Mat weight_hc_data_r2 = lstm->weight_hc_data.reshape(lstm->num_output, lstm->hidden_size * 4 * num_directions);
  368. ncnn::Mat weight_hc_data_int8;
  369. ncnn::Option opt_q = opt;
  370. opt_q.blob_allocator = lstm->weight_hc_data.allocator;
  371. opt_q.use_packing_layout = false;
  372. ncnn::quantize_to_int8(weight_hc_data_r2, weight_hc_data_int8, weight_hc_data_int8_scales, opt_q);
  373. if (weight_hc_data_int8.empty())
  374. return -100;
  375. lstm->weight_hc_data = weight_hc_data_int8.reshape(lstm->num_output * lstm->hidden_size * 4 * num_directions);
  376. }
  377. lstm->int8_scale_term = 2;
  378. lstm->weight_xc_data_int8_scales = weight_xc_data_int8_scales;
  379. lstm->weight_hc_data_int8_scales = weight_hc_data_int8_scales;
  380. }
  381. return 0;
  382. }
  383. int NetQuantize::quantize_gru()
  384. {
  385. for (size_t i = 0; i < layers.size(); i++)
  386. {
  387. if (layers[i]->type != "GRU")
  388. continue;
  389. // GRU - quantize weight from fp32 to int8
  390. ncnn::GRU* gru = (ncnn::GRU*)layers[i];
  391. fprintf(stderr, "quantize_gru %s\n", gru->name.c_str());
  392. // TODO move to ncnn2table
  393. const int num_directions = gru->direction == 2 ? 2 : 1;
  394. const int size = gru->weight_data_size / num_directions / gru->num_output / 3;
  395. ncnn::Mat weight_xc_data_int8_scales(gru->num_output * 3 * num_directions);
  396. ncnn::Mat weight_hc_data_int8_scales(gru->num_output * 3 * num_directions);
  397. for (int d = 0; d < num_directions; d++)
  398. {
  399. for (int q = 0; q < gru->num_output * 3; q++)
  400. {
  401. {
  402. const float* weight_xc_ptr = gru->weight_xc_data.channel(d).row(q);
  403. float absmax = 0.f;
  404. for (int i = 0; i < size; i++)
  405. {
  406. absmax = std::max(absmax, (float)fabs(weight_xc_ptr[i]));
  407. }
  408. weight_xc_data_int8_scales[d * gru->num_output * 3 + q] = 127 / absmax;
  409. }
  410. {
  411. const float* weight_hc_ptr = gru->weight_hc_data.channel(d).row(q);
  412. float absmax = 0.f;
  413. for (int i = 0; i < size; i++)
  414. {
  415. absmax = std::max(absmax, (float)fabs(weight_hc_ptr[i]));
  416. }
  417. weight_hc_data_int8_scales[d * gru->num_output * 3 + q] = 127 / absmax;
  418. }
  419. }
  420. }
  421. {
  422. ncnn::Mat weight_xc_data_r2 = gru->weight_xc_data.reshape(size, gru->num_output * 3 * num_directions);
  423. ncnn::Mat weight_xc_data_int8;
  424. ncnn::Option opt_q = opt;
  425. opt_q.blob_allocator = gru->weight_xc_data.allocator;
  426. opt_q.use_packing_layout = false;
  427. ncnn::quantize_to_int8(weight_xc_data_r2, weight_xc_data_int8, weight_xc_data_int8_scales, opt_q);
  428. if (weight_xc_data_int8.empty())
  429. return -100;
  430. gru->weight_xc_data = weight_xc_data_int8.reshape(size * gru->num_output * 3 * num_directions);
  431. }
  432. {
  433. ncnn::Mat weight_hc_data_r2 = gru->weight_hc_data.reshape(gru->num_output, gru->num_output * 3 * num_directions);
  434. ncnn::Mat weight_hc_data_int8;
  435. ncnn::Option opt_q = opt;
  436. opt_q.blob_allocator = gru->weight_hc_data.allocator;
  437. opt_q.use_packing_layout = false;
  438. ncnn::quantize_to_int8(weight_hc_data_r2, weight_hc_data_int8, weight_hc_data_int8_scales, opt_q);
  439. if (weight_hc_data_int8.empty())
  440. return -100;
  441. gru->weight_hc_data = weight_hc_data_int8.reshape(gru->num_output * gru->num_output * 3 * num_directions);
  442. }
  443. gru->int8_scale_term = 2;
  444. gru->weight_xc_data_int8_scales = weight_xc_data_int8_scales;
  445. gru->weight_hc_data_int8_scales = weight_hc_data_int8_scales;
  446. }
  447. return 0;
  448. }
  449. int NetQuantize::fuse_requantize()
  450. {
  451. const size_t layer_count = layers.size();
  452. for (size_t i = 0; i < layer_count; i++)
  453. {
  454. if (layers[i]->type != "Convolution" && layers[i]->type != "ConvolutionDepthWise")
  455. continue;
  456. // Convolution/ConvolutionDepthWise - Convolution/ConvolutionDepthWise
  457. int top_blob_index = layers[i]->tops[0];
  458. size_t j = i + 1;
  459. for (; j < layer_count; j++)
  460. {
  461. if (layers[j]->type != "Convolution" && layers[j]->type != "ConvolutionDepthWise")
  462. continue;
  463. if (layers[j]->bottoms.size() != 1)
  464. continue;
  465. if (layers[j]->bottoms[0] == top_blob_index)
  466. break;
  467. }
  468. if (j == layer_count)
  469. continue;
  470. // fuse requantize
  471. fprintf(stderr, "fuse_requantize %s %s\n", layers[i]->name.c_str(), layers[j]->name.c_str());
  472. if (layers[i]->type == "Convolution" && layers[j]->type == "Convolution")
  473. {
  474. ncnn::Convolution* convolution1 = (ncnn::Convolution*)layers[i];
  475. ncnn::Convolution* convolution2 = (ncnn::Convolution*)layers[j];
  476. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  477. continue;
  478. convolution1->int8_scale_term += 100;
  479. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  480. }
  481. if (layers[i]->type == "Convolution" && layers[j]->type == "ConvolutionDepthWise")
  482. {
  483. ncnn::Convolution* convolution1 = (ncnn::Convolution*)layers[i];
  484. ncnn::ConvolutionDepthWise* convolution2 = (ncnn::ConvolutionDepthWise*)layers[j];
  485. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  486. continue;
  487. convolution1->int8_scale_term += 100;
  488. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  489. }
  490. if (layers[i]->type == "ConvolutionDepthWise" && layers[j]->type == "Convolution")
  491. {
  492. ncnn::ConvolutionDepthWise* convolution1 = (ncnn::ConvolutionDepthWise*)layers[i];
  493. ncnn::Convolution* convolution2 = (ncnn::Convolution*)layers[j];
  494. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  495. continue;
  496. convolution1->int8_scale_term += 100;
  497. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  498. }
  499. if (layers[i]->type == "ConvolutionDepthWise" && layers[j]->type == "ConvolutionDepthWise")
  500. {
  501. ncnn::ConvolutionDepthWise* convolution1 = (ncnn::ConvolutionDepthWise*)layers[i];
  502. ncnn::ConvolutionDepthWise* convolution2 = (ncnn::ConvolutionDepthWise*)layers[j];
  503. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  504. continue;
  505. convolution1->int8_scale_term += 100;
  506. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  507. }
  508. }
  509. for (size_t i = 0; i < layer_count; i++)
  510. {
  511. if (layers[i]->type != "Convolution" && layers[i]->type != "ConvolutionDepthWise")
  512. continue;
  513. // Convolution/ConvolutionDepthWise - Split - Convolution/ConvolutionDepthWise
  514. int top_blob_index = layers[i]->tops[0];
  515. size_t j = i + 1;
  516. for (; j < layer_count; j++)
  517. {
  518. if (layers[j]->type != "Split")
  519. continue;
  520. if (layers[j]->bottoms.size() != 1)
  521. continue;
  522. if (layers[j]->bottoms[0] == top_blob_index)
  523. break;
  524. }
  525. if (j == layer_count)
  526. continue;
  527. ncnn::Split* split = (ncnn::Split*)layers[j];
  528. bool all_conv = true;
  529. for (size_t p = 0; p < split->tops.size(); p++)
  530. {
  531. int split_top_blob_index = split->tops[p];
  532. size_t k = j + 1;
  533. for (; k < layer_count; k++)
  534. {
  535. if (layers[k]->type != "Convolution" && layers[k]->type != "ConvolutionDepthWise")
  536. continue;
  537. if (layers[k]->bottoms.size() != 1)
  538. continue;
  539. if (layers[k]->bottoms[0] == split_top_blob_index)
  540. break;
  541. }
  542. if (k == layer_count)
  543. {
  544. all_conv = false;
  545. break;
  546. }
  547. if (layers[k]->type == "Convolution")
  548. {
  549. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[k];
  550. if (convolution->weight_data.elemsize != 1u)
  551. {
  552. all_conv = false;
  553. break;
  554. }
  555. }
  556. if (layers[k]->type == "ConvolutionDepthWise")
  557. {
  558. ncnn::ConvolutionDepthWise* convolution = (ncnn::ConvolutionDepthWise*)layers[k];
  559. if (convolution->weight_data.elemsize != 1u)
  560. {
  561. all_conv = false;
  562. break;
  563. }
  564. }
  565. }
  566. if (!all_conv)
  567. continue;
  568. j = blobs[split->tops[0]].consumer;
  569. // fuse requantize
  570. fprintf(stderr, "fuse_requantize %s %s\n", layers[i]->name.c_str(), split->name.c_str());
  571. if (layers[i]->type == "Convolution" && layers[j]->type == "Convolution")
  572. {
  573. ncnn::Convolution* convolution1 = (ncnn::Convolution*)layers[i];
  574. ncnn::Convolution* convolution2 = (ncnn::Convolution*)layers[j];
  575. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  576. continue;
  577. convolution1->int8_scale_term += 100;
  578. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  579. }
  580. if (layers[i]->type == "Convolution" && layers[j]->type == "ConvolutionDepthWise")
  581. {
  582. ncnn::Convolution* convolution1 = (ncnn::Convolution*)layers[i];
  583. ncnn::ConvolutionDepthWise* convolution2 = (ncnn::ConvolutionDepthWise*)layers[j];
  584. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  585. continue;
  586. convolution1->int8_scale_term += 100;
  587. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  588. }
  589. if (layers[i]->type == "ConvolutionDepthWise" && layers[j]->type == "Convolution")
  590. {
  591. ncnn::ConvolutionDepthWise* convolution1 = (ncnn::ConvolutionDepthWise*)layers[i];
  592. ncnn::Convolution* convolution2 = (ncnn::Convolution*)layers[j];
  593. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  594. continue;
  595. convolution1->int8_scale_term += 100;
  596. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  597. }
  598. if (layers[i]->type == "ConvolutionDepthWise" && layers[j]->type == "ConvolutionDepthWise")
  599. {
  600. ncnn::ConvolutionDepthWise* convolution1 = (ncnn::ConvolutionDepthWise*)layers[i];
  601. ncnn::ConvolutionDepthWise* convolution2 = (ncnn::ConvolutionDepthWise*)layers[j];
  602. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  603. continue;
  604. convolution1->int8_scale_term += 100;
  605. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  606. }
  607. }
  608. return 0;
  609. }
  610. int main(int argc, char** argv)
  611. {
  612. if (argc != 5 && argc != 6)
  613. {
  614. fprintf(stderr, "usage: %s [inparam] [inbin] [outparam] [outbin] [calibration table]\n", argv[0]);
  615. return -1;
  616. }
  617. const char* inparam = argv[1];
  618. const char* inbin = argv[2];
  619. const char* outparam = argv[3];
  620. const char* outbin = argv[4];
  621. const char* int8scale_table_path = argc == 6 ? argv[5] : NULL;
  622. NetQuantize quantizer;
  623. // parse the calibration scale table
  624. if (int8scale_table_path)
  625. {
  626. bool s2 = read_int8scale_table(int8scale_table_path, quantizer.blob_int8scale_table, quantizer.weight_int8scale_table);
  627. if (!s2)
  628. {
  629. fprintf(stderr, "read_int8scale_table failed\n");
  630. return -1;
  631. }
  632. }
  633. quantizer.load_param(inparam);
  634. if (strcmp(inbin, "null") == 0)
  635. {
  636. DataReaderFromEmpty dr;
  637. quantizer.load_model(dr);
  638. quantizer.gen_random_weight = true;
  639. }
  640. else
  641. quantizer.load_model(inbin);
  642. quantizer.quantize_convolution();
  643. quantizer.quantize_convolutiondepthwise();
  644. quantizer.quantize_innerproduct();
  645. quantizer.quantize_rnn();
  646. quantizer.quantize_lstm();
  647. quantizer.quantize_gru();
  648. quantizer.fuse_requantize();
  649. quantizer.save(outparam, outbin);
  650. return 0;
  651. }