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