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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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 quantize_embed();
  112. int fuse_requantize();
  113. };
  114. NetQuantize::NetQuantize()
  115. : ModelWriter()
  116. {
  117. }
  118. int NetQuantize::quantize_convolution()
  119. {
  120. const int layer_count = static_cast<int>(layers.size());
  121. for (int i = 0; i < layer_count; i++)
  122. {
  123. // find convolution layer
  124. if (layers[i]->type != "Convolution")
  125. continue;
  126. // find convolution layer
  127. std::map<std::string, ncnn::Mat>::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  128. if (iter_data == blob_int8scale_table.end())
  129. continue;
  130. char key[256];
  131. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  132. std::map<std::string, ncnn::Mat>::iterator iter = weight_int8scale_table.find(key);
  133. if (iter == weight_int8scale_table.end())
  134. {
  135. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  136. return -1;
  137. }
  138. // Convolution - quantize weight from fp32 to int8
  139. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i];
  140. ncnn::Mat bottom_blob_int8_scales = iter_data->second;
  141. ncnn::Mat weight_data_int8_scales = iter->second;
  142. fprintf(stderr, "quantize_convolution %s\n", convolution->name.c_str());
  143. {
  144. const int maxk = convolution->kernel_w * convolution->kernel_h;
  145. const int num_input = convolution->weight_data_size / convolution->num_output / maxk;
  146. ncnn::Mat weight_data_r2 = convolution->weight_data.reshape(maxk, num_input, convolution->num_output);
  147. ncnn::Mat weight_data_int8;
  148. ncnn::Option opt_q = opt;
  149. opt_q.blob_allocator = convolution->weight_data.allocator;
  150. opt_q.use_packing_layout = false;
  151. ncnn::quantize_to_int8(weight_data_r2, weight_data_int8, weight_data_int8_scales, opt_q);
  152. if (weight_data_int8.empty())
  153. return -100;
  154. convolution->weight_data = weight_data_int8.reshape(convolution->weight_data_size);
  155. }
  156. convolution->int8_scale_term = 2;
  157. convolution->weight_data_int8_scales = weight_data_int8_scales;
  158. convolution->bottom_blob_int8_scales = bottom_blob_int8_scales;
  159. }
  160. return 0;
  161. }
  162. int NetQuantize::quantize_convolutiondepthwise()
  163. {
  164. const int layer_count = static_cast<int>(layers.size());
  165. for (int i = 0; i < layer_count; i++)
  166. {
  167. // find convolution layer
  168. if (layers[i]->type != "ConvolutionDepthWise")
  169. continue;
  170. // find convolutiondepthwise layer
  171. std::map<std::string, ncnn::Mat>::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  172. if (iter_data == blob_int8scale_table.end())
  173. continue;
  174. char key[256];
  175. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  176. std::map<std::string, ncnn::Mat>::iterator iter = weight_int8scale_table.find(key);
  177. if (iter == weight_int8scale_table.end())
  178. {
  179. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  180. return -1;
  181. }
  182. // Convolution - quantize weight from fp32 to int8
  183. ncnn::ConvolutionDepthWise* convdw = (ncnn::ConvolutionDepthWise*)layers[i];
  184. ncnn::Mat bottom_blob_int8_scales = iter_data->second;
  185. ncnn::Mat weight_data_int8_scales = iter->second;
  186. fprintf(stderr, "quantize_convolutiondepthwise %s\n", convdw->name.c_str());
  187. {
  188. ncnn::Mat int8_weight_data(convdw->weight_data_size, (size_t)1u);
  189. if (int8_weight_data.empty())
  190. return -100;
  191. const int weight_data_size_g = convdw->weight_data_size / convdw->group;
  192. for (int g = 0; g < convdw->group; g++)
  193. {
  194. ncnn::Option opt_q = opt;
  195. opt_q.blob_allocator = int8_weight_data.allocator;
  196. opt_q.use_packing_layout = false;
  197. const ncnn::Mat weight_data_g = convdw->weight_data.range(weight_data_size_g * g, weight_data_size_g);
  198. ncnn::Mat int8_weight_data_g = int8_weight_data.range(weight_data_size_g * g, weight_data_size_g);
  199. const ncnn::Mat weight_data_int8_scales_g = weight_data_int8_scales.range(g, 1);
  200. ncnn::quantize_to_int8(weight_data_g, int8_weight_data_g, weight_data_int8_scales_g, opt_q);
  201. }
  202. convdw->weight_data = int8_weight_data;
  203. }
  204. convdw->int8_scale_term = 1;
  205. convdw->weight_data_int8_scales = weight_data_int8_scales;
  206. convdw->bottom_blob_int8_scales = bottom_blob_int8_scales;
  207. }
  208. return 0;
  209. }
  210. int NetQuantize::quantize_innerproduct()
  211. {
  212. const int layer_count = static_cast<int>(layers.size());
  213. for (int i = 0; i < layer_count; i++)
  214. {
  215. // find convolution layer
  216. if (layers[i]->type != "InnerProduct")
  217. continue;
  218. // find InnerProduct layer
  219. std::map<std::string, ncnn::Mat>::iterator iter_data = blob_int8scale_table.find(layers[i]->name);
  220. if (iter_data == blob_int8scale_table.end())
  221. continue;
  222. char key[256];
  223. sprintf(key, "%s_param_0", layers[i]->name.c_str());
  224. std::map<std::string, ncnn::Mat>::iterator iter = weight_int8scale_table.find(key);
  225. if (iter == weight_int8scale_table.end())
  226. {
  227. fprintf(stderr, "this layer need to be quantized, but no scale param!\n");
  228. return -1;
  229. }
  230. // InnerProduct - quantize weight from fp32 to int8
  231. ncnn::InnerProduct* fc = (ncnn::InnerProduct*)layers[i];
  232. ncnn::Mat bottom_blob_int8_scales = iter_data->second;
  233. ncnn::Mat weight_data_int8_scales = iter->second;
  234. fprintf(stderr, "quantize_innerproduct %s\n", fc->name.c_str());
  235. {
  236. const int num_input = fc->weight_data_size / fc->num_output;
  237. ncnn::Mat weight_data_r2 = fc->weight_data.reshape(num_input, fc->num_output);
  238. ncnn::Mat weight_data_int8;
  239. ncnn::Option opt_q = opt;
  240. opt_q.use_packing_layout = false;
  241. ncnn::quantize_to_int8(weight_data_r2, weight_data_int8, weight_data_int8_scales, opt_q);
  242. if (weight_data_int8.empty())
  243. return -100;
  244. fc->weight_data = weight_data_int8.reshape(fc->weight_data_size);
  245. }
  246. fc->int8_scale_term = 2;
  247. fc->weight_data_int8_scales = weight_data_int8_scales;
  248. fc->bottom_blob_int8_scales = bottom_blob_int8_scales;
  249. }
  250. return 0;
  251. }
  252. int NetQuantize::quantize_rnn()
  253. {
  254. for (size_t i = 0; i < layers.size(); i++)
  255. {
  256. if (layers[i]->type != "RNN")
  257. continue;
  258. // RNN - quantize weight from fp32 to int8
  259. ncnn::RNN* rnn = (ncnn::RNN*)layers[i];
  260. fprintf(stderr, "quantize_rnn %s\n", rnn->name.c_str());
  261. // TODO move to ncnn2table
  262. const int num_directions = rnn->direction == 2 ? 2 : 1;
  263. const int size = rnn->weight_data_size / num_directions / rnn->num_output;
  264. ncnn::Mat weight_xc_data_int8_scales(rnn->num_output * num_directions);
  265. ncnn::Mat weight_hc_data_int8_scales(rnn->num_output * num_directions);
  266. for (int d = 0; d < num_directions; d++)
  267. {
  268. for (int q = 0; q < rnn->num_output; q++)
  269. {
  270. {
  271. const float* weight_xc_ptr = rnn->weight_xc_data.channel(d).row(q);
  272. float absmax = 0.f;
  273. for (int i = 0; i < size; i++)
  274. {
  275. absmax = std::max(absmax, (float)fabs(weight_xc_ptr[i]));
  276. }
  277. weight_xc_data_int8_scales[d * rnn->num_output + q] = 127 / absmax;
  278. }
  279. {
  280. const float* weight_hc_ptr = rnn->weight_hc_data.channel(d).row(q);
  281. float absmax = 0.f;
  282. for (int i = 0; i < size; i++)
  283. {
  284. absmax = std::max(absmax, (float)fabs(weight_hc_ptr[i]));
  285. }
  286. weight_hc_data_int8_scales[d * rnn->num_output + q] = 127 / absmax;
  287. }
  288. }
  289. }
  290. {
  291. ncnn::Mat weight_xc_data_r2 = rnn->weight_xc_data.reshape(size, rnn->num_output * num_directions);
  292. ncnn::Mat weight_xc_data_int8;
  293. ncnn::Option opt_q = opt;
  294. opt_q.blob_allocator = rnn->weight_xc_data.allocator;
  295. opt_q.use_packing_layout = false;
  296. ncnn::quantize_to_int8(weight_xc_data_r2, weight_xc_data_int8, weight_xc_data_int8_scales, opt_q);
  297. if (weight_xc_data_int8.empty())
  298. return -100;
  299. rnn->weight_xc_data = weight_xc_data_int8.reshape(size * rnn->num_output * num_directions);
  300. }
  301. {
  302. ncnn::Mat weight_hc_data_r2 = rnn->weight_hc_data.reshape(rnn->num_output, rnn->num_output * num_directions);
  303. ncnn::Mat weight_hc_data_int8;
  304. ncnn::Option opt_q = opt;
  305. opt_q.blob_allocator = rnn->weight_hc_data.allocator;
  306. opt_q.use_packing_layout = false;
  307. ncnn::quantize_to_int8(weight_hc_data_r2, weight_hc_data_int8, weight_hc_data_int8_scales, opt_q);
  308. if (weight_hc_data_int8.empty())
  309. return -100;
  310. rnn->weight_hc_data = weight_hc_data_int8.reshape(rnn->num_output * rnn->num_output * num_directions);
  311. }
  312. rnn->int8_scale_term = 2;
  313. rnn->weight_xc_data_int8_scales = weight_xc_data_int8_scales;
  314. rnn->weight_hc_data_int8_scales = weight_hc_data_int8_scales;
  315. }
  316. return 0;
  317. }
  318. int NetQuantize::quantize_lstm()
  319. {
  320. for (size_t i = 0; i < layers.size(); i++)
  321. {
  322. if (layers[i]->type != "LSTM")
  323. continue;
  324. // LSTM - quantize weight from fp32 to int8
  325. ncnn::LSTM* lstm = (ncnn::LSTM*)layers[i];
  326. fprintf(stderr, "quantize_lstm %s\n", lstm->name.c_str());
  327. // TODO move to ncnn2table
  328. const int num_directions = lstm->direction == 2 ? 2 : 1;
  329. const int size = lstm->weight_data_size / num_directions / lstm->hidden_size / 4;
  330. ncnn::Mat weight_xc_data_int8_scales(lstm->hidden_size * 4 * num_directions);
  331. ncnn::Mat weight_hc_data_int8_scales(lstm->hidden_size * 4 * num_directions);
  332. for (int d = 0; d < num_directions; d++)
  333. {
  334. for (int q = 0; q < lstm->hidden_size * 4; q++)
  335. {
  336. {
  337. const float* weight_xc_ptr = lstm->weight_xc_data.channel(d).row(q);
  338. float absmax = 0.f;
  339. for (int i = 0; i < size; i++)
  340. {
  341. absmax = std::max(absmax, (float)fabs(weight_xc_ptr[i]));
  342. }
  343. weight_xc_data_int8_scales[d * lstm->hidden_size * 4 + q] = 127 / absmax;
  344. }
  345. {
  346. const float* weight_hc_ptr = lstm->weight_hc_data.channel(d).row(q);
  347. float absmax = 0.f;
  348. for (int i = 0; i < size; i++)
  349. {
  350. absmax = std::max(absmax, (float)fabs(weight_hc_ptr[i]));
  351. }
  352. weight_hc_data_int8_scales[d * lstm->hidden_size * 4 + q] = 127 / absmax;
  353. }
  354. }
  355. }
  356. {
  357. ncnn::Mat weight_xc_data_r2 = lstm->weight_xc_data.reshape(size, lstm->hidden_size * 4 * num_directions);
  358. ncnn::Mat weight_xc_data_int8;
  359. ncnn::Option opt_q = opt;
  360. opt_q.blob_allocator = lstm->weight_xc_data.allocator;
  361. opt_q.use_packing_layout = false;
  362. ncnn::quantize_to_int8(weight_xc_data_r2, weight_xc_data_int8, weight_xc_data_int8_scales, opt_q);
  363. if (weight_xc_data_int8.empty())
  364. return -100;
  365. lstm->weight_xc_data = weight_xc_data_int8.reshape(size * lstm->hidden_size * 4 * num_directions);
  366. }
  367. {
  368. ncnn::Mat weight_hc_data_r2 = lstm->weight_hc_data.reshape(lstm->num_output, lstm->hidden_size * 4 * num_directions);
  369. ncnn::Mat weight_hc_data_int8;
  370. ncnn::Option opt_q = opt;
  371. opt_q.blob_allocator = lstm->weight_hc_data.allocator;
  372. opt_q.use_packing_layout = false;
  373. ncnn::quantize_to_int8(weight_hc_data_r2, weight_hc_data_int8, weight_hc_data_int8_scales, opt_q);
  374. if (weight_hc_data_int8.empty())
  375. return -100;
  376. lstm->weight_hc_data = weight_hc_data_int8.reshape(lstm->num_output * lstm->hidden_size * 4 * num_directions);
  377. }
  378. lstm->int8_scale_term = 2;
  379. lstm->weight_xc_data_int8_scales = weight_xc_data_int8_scales;
  380. lstm->weight_hc_data_int8_scales = weight_hc_data_int8_scales;
  381. }
  382. return 0;
  383. }
  384. int NetQuantize::quantize_gru()
  385. {
  386. for (size_t i = 0; i < layers.size(); i++)
  387. {
  388. if (layers[i]->type != "GRU")
  389. continue;
  390. // GRU - quantize weight from fp32 to int8
  391. ncnn::GRU* gru = (ncnn::GRU*)layers[i];
  392. fprintf(stderr, "quantize_gru %s\n", gru->name.c_str());
  393. // TODO move to ncnn2table
  394. const int num_directions = gru->direction == 2 ? 2 : 1;
  395. const int size = gru->weight_data_size / num_directions / gru->num_output / 3;
  396. ncnn::Mat weight_xc_data_int8_scales(gru->num_output * 3 * num_directions);
  397. ncnn::Mat weight_hc_data_int8_scales(gru->num_output * 3 * num_directions);
  398. for (int d = 0; d < num_directions; d++)
  399. {
  400. for (int q = 0; q < gru->num_output * 3; q++)
  401. {
  402. {
  403. const float* weight_xc_ptr = gru->weight_xc_data.channel(d).row(q);
  404. float absmax = 0.f;
  405. for (int i = 0; i < size; i++)
  406. {
  407. absmax = std::max(absmax, (float)fabs(weight_xc_ptr[i]));
  408. }
  409. weight_xc_data_int8_scales[d * gru->num_output * 3 + q] = 127 / absmax;
  410. }
  411. {
  412. const float* weight_hc_ptr = gru->weight_hc_data.channel(d).row(q);
  413. float absmax = 0.f;
  414. for (int i = 0; i < size; i++)
  415. {
  416. absmax = std::max(absmax, (float)fabs(weight_hc_ptr[i]));
  417. }
  418. weight_hc_data_int8_scales[d * gru->num_output * 3 + q] = 127 / absmax;
  419. }
  420. }
  421. }
  422. {
  423. ncnn::Mat weight_xc_data_r2 = gru->weight_xc_data.reshape(size, gru->num_output * 3 * num_directions);
  424. ncnn::Mat weight_xc_data_int8;
  425. ncnn::Option opt_q = opt;
  426. opt_q.blob_allocator = gru->weight_xc_data.allocator;
  427. opt_q.use_packing_layout = false;
  428. ncnn::quantize_to_int8(weight_xc_data_r2, weight_xc_data_int8, weight_xc_data_int8_scales, opt_q);
  429. if (weight_xc_data_int8.empty())
  430. return -100;
  431. gru->weight_xc_data = weight_xc_data_int8.reshape(size * gru->num_output * 3 * num_directions);
  432. }
  433. {
  434. ncnn::Mat weight_hc_data_r2 = gru->weight_hc_data.reshape(gru->num_output, gru->num_output * 3 * num_directions);
  435. ncnn::Mat weight_hc_data_int8;
  436. ncnn::Option opt_q = opt;
  437. opt_q.blob_allocator = gru->weight_hc_data.allocator;
  438. opt_q.use_packing_layout = false;
  439. ncnn::quantize_to_int8(weight_hc_data_r2, weight_hc_data_int8, weight_hc_data_int8_scales, opt_q);
  440. if (weight_hc_data_int8.empty())
  441. return -100;
  442. gru->weight_hc_data = weight_hc_data_int8.reshape(gru->num_output * gru->num_output * 3 * num_directions);
  443. }
  444. gru->int8_scale_term = 2;
  445. gru->weight_xc_data_int8_scales = weight_xc_data_int8_scales;
  446. gru->weight_hc_data_int8_scales = weight_hc_data_int8_scales;
  447. }
  448. return 0;
  449. }
  450. int NetQuantize::quantize_embed()
  451. {
  452. for (size_t i = 0; i < layers.size(); i++)
  453. {
  454. if (layers[i]->type != "Embed")
  455. continue;
  456. // Embed - quantize weight from fp32 to int8
  457. ncnn::Embed* embed = (ncnn::Embed*)layers[i];
  458. fprintf(stderr, "quantize_embed %s\n", embed->name.c_str());
  459. // TODO move to ncnn2table
  460. const int num_output = embed->num_output;
  461. const int input_dim = embed->input_dim;
  462. ncnn::Mat weight_data_int8_scales(1);
  463. {
  464. const float* ptr = embed->weight_data;
  465. float absmax = 0.f;
  466. for (int i = 0; i < embed->weight_data.w; i++)
  467. {
  468. absmax = std::max(absmax, (float)fabs(ptr[i]));
  469. }
  470. weight_data_int8_scales[0] = absmax == 0.f ? 1.f : 127 / absmax;
  471. }
  472. {
  473. ncnn::Mat weight_data_int8;
  474. ncnn::Option opt_q = opt;
  475. opt_q.blob_allocator = embed->weight_data.allocator;
  476. opt_q.use_packing_layout = false;
  477. ncnn::quantize_to_int8(embed->weight_data, weight_data_int8, weight_data_int8_scales, opt_q);
  478. if (weight_data_int8.empty())
  479. return -100;
  480. embed->weight_data = weight_data_int8;
  481. }
  482. embed->int8_scale_term = 2;
  483. embed->weight_data_int8_scale = weight_data_int8_scales[0];
  484. }
  485. return 0;
  486. }
  487. int NetQuantize::fuse_requantize()
  488. {
  489. const size_t layer_count = layers.size();
  490. for (size_t i = 0; i < layer_count; i++)
  491. {
  492. if (layers[i]->type != "Convolution" && layers[i]->type != "ConvolutionDepthWise")
  493. continue;
  494. // Convolution/ConvolutionDepthWise - Convolution/ConvolutionDepthWise
  495. int top_blob_index = layers[i]->tops[0];
  496. size_t j = i + 1;
  497. for (; j < layer_count; j++)
  498. {
  499. if (layers[j]->type != "Convolution" && layers[j]->type != "ConvolutionDepthWise")
  500. continue;
  501. if (layers[j]->bottoms.size() != 1)
  502. continue;
  503. if (layers[j]->bottoms[0] == top_blob_index)
  504. break;
  505. }
  506. if (j == layer_count)
  507. continue;
  508. // fuse requantize
  509. fprintf(stderr, "fuse_requantize %s %s\n", layers[i]->name.c_str(), layers[j]->name.c_str());
  510. if (layers[i]->type == "Convolution" && layers[j]->type == "Convolution")
  511. {
  512. ncnn::Convolution* convolution1 = (ncnn::Convolution*)layers[i];
  513. ncnn::Convolution* convolution2 = (ncnn::Convolution*)layers[j];
  514. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  515. continue;
  516. convolution1->int8_scale_term += 100;
  517. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  518. }
  519. if (layers[i]->type == "Convolution" && layers[j]->type == "ConvolutionDepthWise")
  520. {
  521. ncnn::Convolution* convolution1 = (ncnn::Convolution*)layers[i];
  522. ncnn::ConvolutionDepthWise* convolution2 = (ncnn::ConvolutionDepthWise*)layers[j];
  523. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  524. continue;
  525. convolution1->int8_scale_term += 100;
  526. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  527. }
  528. if (layers[i]->type == "ConvolutionDepthWise" && layers[j]->type == "Convolution")
  529. {
  530. ncnn::ConvolutionDepthWise* convolution1 = (ncnn::ConvolutionDepthWise*)layers[i];
  531. ncnn::Convolution* convolution2 = (ncnn::Convolution*)layers[j];
  532. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  533. continue;
  534. convolution1->int8_scale_term += 100;
  535. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  536. }
  537. if (layers[i]->type == "ConvolutionDepthWise" && layers[j]->type == "ConvolutionDepthWise")
  538. {
  539. ncnn::ConvolutionDepthWise* convolution1 = (ncnn::ConvolutionDepthWise*)layers[i];
  540. ncnn::ConvolutionDepthWise* convolution2 = (ncnn::ConvolutionDepthWise*)layers[j];
  541. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  542. continue;
  543. convolution1->int8_scale_term += 100;
  544. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  545. }
  546. }
  547. for (size_t i = 0; i < layer_count; i++)
  548. {
  549. if (layers[i]->type != "Convolution" && layers[i]->type != "ConvolutionDepthWise")
  550. continue;
  551. // Convolution/ConvolutionDepthWise - Split - Convolution/ConvolutionDepthWise
  552. int top_blob_index = layers[i]->tops[0];
  553. size_t j = i + 1;
  554. for (; j < layer_count; j++)
  555. {
  556. if (layers[j]->type != "Split")
  557. continue;
  558. if (layers[j]->bottoms.size() != 1)
  559. continue;
  560. if (layers[j]->bottoms[0] == top_blob_index)
  561. break;
  562. }
  563. if (j == layer_count)
  564. continue;
  565. ncnn::Split* split = (ncnn::Split*)layers[j];
  566. bool all_conv = true;
  567. for (size_t p = 0; p < split->tops.size(); p++)
  568. {
  569. int split_top_blob_index = split->tops[p];
  570. size_t k = j + 1;
  571. for (; k < layer_count; k++)
  572. {
  573. if (layers[k]->type != "Convolution" && layers[k]->type != "ConvolutionDepthWise")
  574. continue;
  575. if (layers[k]->bottoms.size() != 1)
  576. continue;
  577. if (layers[k]->bottoms[0] == split_top_blob_index)
  578. break;
  579. }
  580. if (k == layer_count)
  581. {
  582. all_conv = false;
  583. break;
  584. }
  585. if (layers[k]->type == "Convolution")
  586. {
  587. ncnn::Convolution* convolution = (ncnn::Convolution*)layers[k];
  588. if (convolution->weight_data.elemsize != 1u)
  589. {
  590. all_conv = false;
  591. break;
  592. }
  593. }
  594. if (layers[k]->type == "ConvolutionDepthWise")
  595. {
  596. ncnn::ConvolutionDepthWise* convolution = (ncnn::ConvolutionDepthWise*)layers[k];
  597. if (convolution->weight_data.elemsize != 1u)
  598. {
  599. all_conv = false;
  600. break;
  601. }
  602. }
  603. }
  604. if (!all_conv)
  605. continue;
  606. j = blobs[split->tops[0]].consumer;
  607. // fuse requantize
  608. fprintf(stderr, "fuse_requantize %s %s\n", layers[i]->name.c_str(), split->name.c_str());
  609. if (layers[i]->type == "Convolution" && layers[j]->type == "Convolution")
  610. {
  611. ncnn::Convolution* convolution1 = (ncnn::Convolution*)layers[i];
  612. ncnn::Convolution* convolution2 = (ncnn::Convolution*)layers[j];
  613. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  614. continue;
  615. convolution1->int8_scale_term += 100;
  616. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  617. }
  618. if (layers[i]->type == "Convolution" && layers[j]->type == "ConvolutionDepthWise")
  619. {
  620. ncnn::Convolution* convolution1 = (ncnn::Convolution*)layers[i];
  621. ncnn::ConvolutionDepthWise* convolution2 = (ncnn::ConvolutionDepthWise*)layers[j];
  622. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  623. continue;
  624. convolution1->int8_scale_term += 100;
  625. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  626. }
  627. if (layers[i]->type == "ConvolutionDepthWise" && layers[j]->type == "Convolution")
  628. {
  629. ncnn::ConvolutionDepthWise* convolution1 = (ncnn::ConvolutionDepthWise*)layers[i];
  630. ncnn::Convolution* convolution2 = (ncnn::Convolution*)layers[j];
  631. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  632. continue;
  633. convolution1->int8_scale_term += 100;
  634. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  635. }
  636. if (layers[i]->type == "ConvolutionDepthWise" && layers[j]->type == "ConvolutionDepthWise")
  637. {
  638. ncnn::ConvolutionDepthWise* convolution1 = (ncnn::ConvolutionDepthWise*)layers[i];
  639. ncnn::ConvolutionDepthWise* convolution2 = (ncnn::ConvolutionDepthWise*)layers[j];
  640. if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u)
  641. continue;
  642. convolution1->int8_scale_term += 100;
  643. convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales;
  644. }
  645. }
  646. return 0;
  647. }
  648. int main(int argc, char** argv)
  649. {
  650. if (argc != 5 && argc != 6)
  651. {
  652. fprintf(stderr, "usage: %s [inparam] [inbin] [outparam] [outbin] [calibration table]\n", argv[0]);
  653. return -1;
  654. }
  655. const char* inparam = argv[1];
  656. const char* inbin = argv[2];
  657. const char* outparam = argv[3];
  658. const char* outbin = argv[4];
  659. const char* int8scale_table_path = argc == 6 ? argv[5] : NULL;
  660. NetQuantize quantizer;
  661. // parse the calibration scale table
  662. if (int8scale_table_path)
  663. {
  664. bool s2 = read_int8scale_table(int8scale_table_path, quantizer.blob_int8scale_table, quantizer.weight_int8scale_table);
  665. if (!s2)
  666. {
  667. fprintf(stderr, "read_int8scale_table failed\n");
  668. return -1;
  669. }
  670. }
  671. quantizer.load_param(inparam);
  672. if (strcmp(inbin, "null") == 0)
  673. {
  674. DataReaderFromEmpty dr;
  675. quantizer.load_model(dr);
  676. quantizer.gen_random_weight = true;
  677. }
  678. else
  679. quantizer.load_model(inbin);
  680. quantizer.quantize_convolution();
  681. quantizer.quantize_convolutiondepthwise();
  682. quantizer.quantize_innerproduct();
  683. quantizer.quantize_rnn();
  684. quantizer.quantize_lstm();
  685. quantizer.quantize_gru();
  686. quantizer.quantize_embed();
  687. quantizer.fuse_requantize();
  688. quantizer.save(outparam, outbin);
  689. return 0;
  690. }