| 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 quantize_embed(); |
| 137 | int quantize_gemm(); |
| 138 | |
| 139 | int fuse_requantize(); |
| 140 | }; |
| 141 | |
| 142 | NetQuantize::NetQuantize() |
| 143 | : ModelWriter() |
| 144 | { |
| 145 | } |
| 146 | |
| 147 | int NetQuantize::quantize_convolution() |
| 148 | { |
| 149 | const int layer_count = static_cast<int>(layers.size()); |
| 150 | for (int i = 0; i < layer_count; i++) |
| 151 | { |
| 152 | // find convolution layer |
| 153 | if (layers[i]->type != "Convolution") |
| 154 | continue; |
| 155 | |
| 156 | // find convolution layer |
| 157 | std::map<std::string, ncnn::Mat>::iterator iter_data = blob_int8scale_table.find(layers[i]->name); |
| 158 | if (iter_data == blob_int8scale_table.end()) |
| 159 | continue; |
| 160 | |
| 161 | char key[256]; |
| 162 | sprintf(key, "%s_param_0", layers[i]->name.c_str()); |
| 163 | |
| 164 | std::map<std::string, ncnn::Mat>::iterator iter = weight_int8scale_table.find(key); |
| 165 | if (iter == weight_int8scale_table.end()) |
| 166 | { |
| 167 | fprintf(stderr, "this layer need to be quantized, but no scale param!\n"); |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | // Convolution - quantize weight from fp32 to int8 |
| 172 | ncnn::Convolution* convolution = (ncnn::Convolution*)layers[i]; |
| 173 | |
| 174 | ncnn::Mat bottom_blob_int8_scales = iter_data->second; |
| 175 | ncnn::Mat weight_data_int8_scales = iter->second; |
| 176 | |
| 177 | fprintf(stderr, "quantize_convolution %s\n", convolution->name.c_str()); |
| 178 | |
| 179 | { |
| 180 | const int maxk = convolution->kernel_w * convolution->kernel_h; |
| 181 | const int num_input = convolution->weight_data_size / convolution->num_output / maxk; |
| 182 | |
| 183 | ncnn::Mat weight_data_r2 = convolution->weight_data.reshape(maxk, num_input, convolution->num_output); |
| 184 | |
| 185 | ncnn::Mat weight_data_int8; |
| 186 | |
| 187 | ncnn::Option opt_q = opt; |
| 188 | opt_q.blob_allocator = convolution->weight_data.allocator; |
| 189 | opt_q.use_packing_layout = false; |
| 190 | ncnn::quantize_to_int8(weight_data_r2, weight_data_int8, weight_data_int8_scales, opt_q); |
| 191 | if (weight_data_int8.empty()) |
| 192 | return -100; |
| 193 | |
| 194 | convolution->weight_data = weight_data_int8.reshape(convolution->weight_data_size); |
| 195 | } |
| 196 | |
| 197 | convolution->int8_scale_term = 2; |
| 198 | convolution->weight_data_int8_scales = weight_data_int8_scales; |
| 199 | convolution->bottom_blob_int8_scales = bottom_blob_int8_scales; |
| 200 | } |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | int NetQuantize::quantize_convolutiondepthwise() |
| 206 | { |
| 207 | const int layer_count = static_cast<int>(layers.size()); |
| 208 | for (int i = 0; i < layer_count; i++) |
| 209 | { |
| 210 | // find convolution layer |
| 211 | if (layers[i]->type != "ConvolutionDepthWise") |
| 212 | continue; |
| 213 | |
| 214 | // find convolutiondepthwise layer |
| 215 | std::map<std::string, ncnn::Mat>::iterator iter_data = blob_int8scale_table.find(layers[i]->name); |
| 216 | if (iter_data == blob_int8scale_table.end()) |
| 217 | continue; |
| 218 | |
| 219 | char key[256]; |
| 220 | sprintf(key, "%s_param_0", layers[i]->name.c_str()); |
| 221 | |
| 222 | std::map<std::string, ncnn::Mat>::iterator iter = weight_int8scale_table.find(key); |
| 223 | if (iter == weight_int8scale_table.end()) |
| 224 | { |
| 225 | fprintf(stderr, "this layer need to be quantized, but no scale param!\n"); |
| 226 | return -1; |
| 227 | } |
| 228 | |
| 229 | // Convolution - quantize weight from fp32 to int8 |
| 230 | ncnn::ConvolutionDepthWise* convdw = (ncnn::ConvolutionDepthWise*)layers[i]; |
| 231 | |
| 232 | ncnn::Mat bottom_blob_int8_scales = iter_data->second; |
| 233 | ncnn::Mat weight_data_int8_scales = iter->second; |
| 234 | |
| 235 | fprintf(stderr, "quantize_convolutiondepthwise %s\n", convdw->name.c_str()); |
| 236 | |
| 237 | { |
| 238 | ncnn::Mat int8_weight_data(convdw->weight_data_size, (size_t)1u); |
| 239 | if (int8_weight_data.empty()) |
| 240 | return -100; |
| 241 | |
| 242 | const int weight_data_size_g = convdw->weight_data_size / convdw->group; |
| 243 | |
| 244 | for (int g = 0; g < convdw->group; g++) |
| 245 | { |
| 246 | ncnn::Option opt_q = opt; |
| 247 | opt_q.blob_allocator = int8_weight_data.allocator; |
| 248 | opt_q.use_packing_layout = false; |
| 249 | |
| 250 | const ncnn::Mat weight_data_g = convdw->weight_data.range(weight_data_size_g * g, weight_data_size_g); |
| 251 | ncnn::Mat int8_weight_data_g = int8_weight_data.range(weight_data_size_g * g, weight_data_size_g); |
| 252 | const ncnn::Mat weight_data_int8_scales_g = weight_data_int8_scales.range(g, 1); |
| 253 | ncnn::quantize_to_int8(weight_data_g, int8_weight_data_g, weight_data_int8_scales_g, opt_q); |
| 254 | } |
| 255 | |
| 256 | convdw->weight_data = int8_weight_data; |
| 257 | } |
| 258 | |
| 259 | convdw->int8_scale_term = 1; |
| 260 | convdw->weight_data_int8_scales = weight_data_int8_scales; |
| 261 | convdw->bottom_blob_int8_scales = bottom_blob_int8_scales; |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | int NetQuantize::quantize_innerproduct() |
| 268 | { |
| 269 | const int layer_count = static_cast<int>(layers.size()); |
| 270 | for (int i = 0; i < layer_count; i++) |
| 271 | { |
| 272 | // find convolution layer |
| 273 | if (layers[i]->type != "InnerProduct") |
| 274 | continue; |
| 275 | |
| 276 | // find InnerProduct layer |
| 277 | std::map<std::string, ncnn::Mat>::iterator iter_data = blob_int8scale_table.find(layers[i]->name); |
| 278 | if (iter_data == blob_int8scale_table.end()) |
| 279 | continue; |
| 280 | |
| 281 | char key[256]; |
| 282 | sprintf(key, "%s_param_0", layers[i]->name.c_str()); |
| 283 | |
| 284 | std::map<std::string, ncnn::Mat>::iterator iter = weight_int8scale_table.find(key); |
| 285 | if (iter == weight_int8scale_table.end()) |
| 286 | { |
| 287 | fprintf(stderr, "this layer need to be quantized, but no scale param!\n"); |
| 288 | return -1; |
| 289 | } |
| 290 | |
| 291 | // InnerProduct - quantize weight from fp32 to int8 |
| 292 | ncnn::InnerProduct* fc = (ncnn::InnerProduct*)layers[i]; |
| 293 | |
| 294 | ncnn::Mat bottom_blob_int8_scales = iter_data->second; |
| 295 | ncnn::Mat weight_data_int8_scales = iter->second; |
| 296 | |
| 297 | fprintf(stderr, "quantize_innerproduct %s\n", fc->name.c_str()); |
| 298 | |
| 299 | { |
| 300 | const int num_input = fc->weight_data_size / fc->num_output; |
| 301 | |
| 302 | ncnn::Mat weight_data_r2 = fc->weight_data.reshape(num_input, fc->num_output); |
| 303 | |
| 304 | ncnn::Mat weight_data_int8; |
| 305 | ncnn::Option opt_q = opt; |
| 306 | opt_q.use_packing_layout = false; |
| 307 | ncnn::quantize_to_int8(weight_data_r2, weight_data_int8, weight_data_int8_scales, opt_q); |
| 308 | if (weight_data_int8.empty()) |
| 309 | return -100; |
| 310 | |
| 311 | fc->weight_data = weight_data_int8.reshape(fc->weight_data_size); |
| 312 | } |
| 313 | |
| 314 | fc->int8_scale_term = 2; |
| 315 | fc->weight_data_int8_scales = weight_data_int8_scales; |
| 316 | fc->bottom_blob_int8_scales = bottom_blob_int8_scales; |
| 317 | } |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | int NetQuantize::quantize_rnn() |
| 323 | { |
| 324 | for (size_t i = 0; i < layers.size(); i++) |
| 325 | { |
| 326 | if (layers[i]->type != "RNN") |
| 327 | continue; |
| 328 | |
| 329 | // RNN - quantize weight from fp32 to int8 |
| 330 | ncnn::RNN* rnn = (ncnn::RNN*)layers[i]; |
| 331 | |
| 332 | fprintf(stderr, "quantize_rnn %s\n", rnn->name.c_str()); |
| 333 | |
| 334 | // TODO move to ncnn2table |
| 335 | const int num_directions = rnn->direction == 2 ? 2 : 1; |
| 336 | const int size = rnn->weight_data_size / num_directions / rnn->num_output; |
| 337 | |
| 338 | ncnn::Mat weight_xc_data_int8_scales(rnn->num_output * num_directions); |
| 339 | ncnn::Mat weight_hc_data_int8_scales(rnn->num_output * num_directions); |
| 340 | |
| 341 | for (int d = 0; d < num_directions; d++) |
| 342 | { |
| 343 | for (int q = 0; q < rnn->num_output; q++) |
| 344 | { |
| 345 | { |
| 346 | const float* weight_xc_ptr = rnn->weight_xc_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_xc_ptr[i])); |
| 351 | } |
| 352 | weight_xc_data_int8_scales[d * rnn->num_output + q] = 127 / absmax; |
| 353 | } |
| 354 | |
| 355 | { |
| 356 | const float* weight_hc_ptr = rnn->weight_hc_data.channel(d).row(q); |
| 357 | float absmax = 0.f; |
| 358 | for (int i = 0; i < size; i++) |
| 359 | { |
| 360 | absmax = std::max(absmax, (float)fabs(weight_hc_ptr[i])); |
| 361 | } |
| 362 | weight_hc_data_int8_scales[d * rnn->num_output + q] = 127 / absmax; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | { |
| 368 | ncnn::Mat weight_xc_data_r2 = rnn->weight_xc_data.reshape(size, rnn->num_output * num_directions); |
| 369 | |
| 370 | ncnn::Mat weight_xc_data_int8; |
| 371 | |
| 372 | ncnn::Option opt_q = opt; |
| 373 | opt_q.blob_allocator = rnn->weight_xc_data.allocator; |
| 374 | opt_q.use_packing_layout = false; |
| 375 | ncnn::quantize_to_int8(weight_xc_data_r2, weight_xc_data_int8, weight_xc_data_int8_scales, opt_q); |
| 376 | if (weight_xc_data_int8.empty()) |
| 377 | return -100; |
| 378 | |
| 379 | rnn->weight_xc_data = weight_xc_data_int8.reshape(size * rnn->num_output * num_directions); |
| 380 | } |
| 381 | { |
| 382 | ncnn::Mat weight_hc_data_r2 = rnn->weight_hc_data.reshape(rnn->num_output, rnn->num_output * num_directions); |
| 383 | |
| 384 | ncnn::Mat weight_hc_data_int8; |
| 385 | |
| 386 | ncnn::Option opt_q = opt; |
| 387 | opt_q.blob_allocator = rnn->weight_hc_data.allocator; |
| 388 | opt_q.use_packing_layout = false; |
| 389 | ncnn::quantize_to_int8(weight_hc_data_r2, weight_hc_data_int8, weight_hc_data_int8_scales, opt_q); |
| 390 | if (weight_hc_data_int8.empty()) |
| 391 | return -100; |
| 392 | |
| 393 | rnn->weight_hc_data = weight_hc_data_int8.reshape(rnn->num_output * rnn->num_output * num_directions); |
| 394 | } |
| 395 | |
| 396 | rnn->int8_scale_term = 2; |
| 397 | rnn->weight_xc_data_int8_scales = weight_xc_data_int8_scales; |
| 398 | rnn->weight_hc_data_int8_scales = weight_hc_data_int8_scales; |
| 399 | } |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | int NetQuantize::quantize_lstm() |
| 405 | { |
| 406 | for (size_t i = 0; i < layers.size(); i++) |
| 407 | { |
| 408 | if (layers[i]->type != "LSTM") |
| 409 | continue; |
| 410 | |
| 411 | // LSTM - quantize weight from fp32 to int8 |
| 412 | ncnn::LSTM* lstm = (ncnn::LSTM*)layers[i]; |
| 413 | |
| 414 | fprintf(stderr, "quantize_lstm %s\n", lstm->name.c_str()); |
| 415 | |
| 416 | // TODO move to ncnn2table |
| 417 | const int num_directions = lstm->direction == 2 ? 2 : 1; |
| 418 | const int size = lstm->weight_data_size / num_directions / lstm->hidden_size / 4; |
| 419 | |
| 420 | ncnn::Mat weight_xc_data_int8_scales(lstm->hidden_size * 4 * num_directions); |
| 421 | ncnn::Mat weight_hc_data_int8_scales(lstm->hidden_size * 4 * num_directions); |
| 422 | |
| 423 | for (int d = 0; d < num_directions; d++) |
| 424 | { |
| 425 | for (int q = 0; q < lstm->hidden_size * 4; q++) |
| 426 | { |
| 427 | { |
| 428 | const float* weight_xc_ptr = lstm->weight_xc_data.channel(d).row(q); |
| 429 | float absmax = 0.f; |
| 430 | for (int i = 0; i < size; i++) |
| 431 | { |
| 432 | absmax = std::max(absmax, (float)fabs(weight_xc_ptr[i])); |
| 433 | } |
| 434 | weight_xc_data_int8_scales[d * lstm->hidden_size * 4 + q] = 127 / absmax; |
| 435 | } |
| 436 | |
| 437 | { |
| 438 | const float* weight_hc_ptr = lstm->weight_hc_data.channel(d).row(q); |
| 439 | float absmax = 0.f; |
| 440 | for (int i = 0; i < size; i++) |
| 441 | { |
| 442 | absmax = std::max(absmax, (float)fabs(weight_hc_ptr[i])); |
| 443 | } |
| 444 | weight_hc_data_int8_scales[d * lstm->hidden_size * 4 + q] = 127 / absmax; |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | { |
| 450 | ncnn::Mat weight_xc_data_r2 = lstm->weight_xc_data.reshape(size, lstm->hidden_size * 4 * num_directions); |
| 451 | |
| 452 | ncnn::Mat weight_xc_data_int8; |
| 453 | |
| 454 | ncnn::Option opt_q = opt; |
| 455 | opt_q.blob_allocator = lstm->weight_xc_data.allocator; |
| 456 | opt_q.use_packing_layout = false; |
| 457 | ncnn::quantize_to_int8(weight_xc_data_r2, weight_xc_data_int8, weight_xc_data_int8_scales, opt_q); |
| 458 | if (weight_xc_data_int8.empty()) |
| 459 | return -100; |
| 460 | |
| 461 | lstm->weight_xc_data = weight_xc_data_int8.reshape(size * lstm->hidden_size * 4 * num_directions); |
| 462 | } |
| 463 | { |
| 464 | ncnn::Mat weight_hc_data_r2 = lstm->weight_hc_data.reshape(lstm->num_output, lstm->hidden_size * 4 * num_directions); |
| 465 | |
| 466 | ncnn::Mat weight_hc_data_int8; |
| 467 | |
| 468 | ncnn::Option opt_q = opt; |
| 469 | opt_q.blob_allocator = lstm->weight_hc_data.allocator; |
| 470 | opt_q.use_packing_layout = false; |
| 471 | ncnn::quantize_to_int8(weight_hc_data_r2, weight_hc_data_int8, weight_hc_data_int8_scales, opt_q); |
| 472 | if (weight_hc_data_int8.empty()) |
| 473 | return -100; |
| 474 | |
| 475 | lstm->weight_hc_data = weight_hc_data_int8.reshape(lstm->num_output * lstm->hidden_size * 4 * num_directions); |
| 476 | } |
| 477 | |
| 478 | lstm->int8_scale_term = 2; |
| 479 | lstm->weight_xc_data_int8_scales = weight_xc_data_int8_scales; |
| 480 | lstm->weight_hc_data_int8_scales = weight_hc_data_int8_scales; |
| 481 | } |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | int NetQuantize::quantize_gru() |
| 487 | { |
| 488 | for (size_t i = 0; i < layers.size(); i++) |
| 489 | { |
| 490 | if (layers[i]->type != "GRU") |
| 491 | continue; |
| 492 | |
| 493 | // GRU - quantize weight from fp32 to int8 |
| 494 | ncnn::GRU* gru = (ncnn::GRU*)layers[i]; |
| 495 | |
| 496 | fprintf(stderr, "quantize_gru %s\n", gru->name.c_str()); |
| 497 | |
| 498 | // TODO move to ncnn2table |
| 499 | const int num_directions = gru->direction == 2 ? 2 : 1; |
| 500 | const int size = gru->weight_data_size / num_directions / gru->num_output / 3; |
| 501 | |
| 502 | ncnn::Mat weight_xc_data_int8_scales(gru->num_output * 3 * num_directions); |
| 503 | ncnn::Mat weight_hc_data_int8_scales(gru->num_output * 3 * num_directions); |
| 504 | |
| 505 | for (int d = 0; d < num_directions; d++) |
| 506 | { |
| 507 | for (int q = 0; q < gru->num_output * 3; q++) |
| 508 | { |
| 509 | { |
| 510 | const float* weight_xc_ptr = gru->weight_xc_data.channel(d).row(q); |
| 511 | float absmax = 0.f; |
| 512 | for (int i = 0; i < size; i++) |
| 513 | { |
| 514 | absmax = std::max(absmax, (float)fabs(weight_xc_ptr[i])); |
| 515 | } |
| 516 | weight_xc_data_int8_scales[d * gru->num_output * 3 + q] = 127 / absmax; |
| 517 | } |
| 518 | |
| 519 | { |
| 520 | const float* weight_hc_ptr = gru->weight_hc_data.channel(d).row(q); |
| 521 | float absmax = 0.f; |
| 522 | for (int i = 0; i < size; i++) |
| 523 | { |
| 524 | absmax = std::max(absmax, (float)fabs(weight_hc_ptr[i])); |
| 525 | } |
| 526 | weight_hc_data_int8_scales[d * gru->num_output * 3 + q] = 127 / absmax; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | { |
| 532 | ncnn::Mat weight_xc_data_r2 = gru->weight_xc_data.reshape(size, gru->num_output * 3 * num_directions); |
| 533 | |
| 534 | ncnn::Mat weight_xc_data_int8; |
| 535 | |
| 536 | ncnn::Option opt_q = opt; |
| 537 | opt_q.blob_allocator = gru->weight_xc_data.allocator; |
| 538 | opt_q.use_packing_layout = false; |
| 539 | ncnn::quantize_to_int8(weight_xc_data_r2, weight_xc_data_int8, weight_xc_data_int8_scales, opt_q); |
| 540 | if (weight_xc_data_int8.empty()) |
| 541 | return -100; |
| 542 | |
| 543 | gru->weight_xc_data = weight_xc_data_int8.reshape(size * gru->num_output * 3 * num_directions); |
| 544 | } |
| 545 | { |
| 546 | ncnn::Mat weight_hc_data_r2 = gru->weight_hc_data.reshape(gru->num_output, gru->num_output * 3 * num_directions); |
| 547 | |
| 548 | ncnn::Mat weight_hc_data_int8; |
| 549 | |
| 550 | ncnn::Option opt_q = opt; |
| 551 | opt_q.blob_allocator = gru->weight_hc_data.allocator; |
| 552 | opt_q.use_packing_layout = false; |
| 553 | ncnn::quantize_to_int8(weight_hc_data_r2, weight_hc_data_int8, weight_hc_data_int8_scales, opt_q); |
| 554 | if (weight_hc_data_int8.empty()) |
| 555 | return -100; |
| 556 | |
| 557 | gru->weight_hc_data = weight_hc_data_int8.reshape(gru->num_output * gru->num_output * 3 * num_directions); |
| 558 | } |
| 559 | |
| 560 | gru->int8_scale_term = 2; |
| 561 | gru->weight_xc_data_int8_scales = weight_xc_data_int8_scales; |
| 562 | gru->weight_hc_data_int8_scales = weight_hc_data_int8_scales; |
| 563 | } |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | int NetQuantize::quantize_embed() |
| 569 | { |
| 570 | for (size_t i = 0; i < layers.size(); i++) |
| 571 | { |
| 572 | if (layers[i]->type != "Embed") |
| 573 | continue; |
| 574 | |
| 575 | // Embed - quantize weight from fp32 to int8 |
| 576 | ncnn::Embed* embed = (ncnn::Embed*)layers[i]; |
| 577 | |
| 578 | fprintf(stderr, "quantize_embed %s\n", embed->name.c_str()); |
| 579 | |
| 580 | // TODO move to ncnn2table |
| 581 | |
| 582 | const int num_output = embed->num_output; |
| 583 | const int input_dim = embed->input_dim; |
| 584 | |
| 585 | ncnn::Mat weight_data_int8_scales(1); |
| 586 | { |
| 587 | const float* ptr = embed->weight_data; |
| 588 | float absmax = 0.f; |
| 589 | for (int i = 0; i < embed->weight_data.w; i++) |
| 590 | { |
| 591 | absmax = std::max(absmax, (float)fabs(ptr[i])); |
| 592 | } |
| 593 | |
| 594 | weight_data_int8_scales[0] = absmax == 0.f ? 1.f : 127 / absmax; |
| 595 | } |
| 596 | |
| 597 | { |
| 598 | ncnn::Mat weight_data_int8; |
| 599 | |
| 600 | ncnn::Option opt_q = opt; |
| 601 | opt_q.blob_allocator = embed->weight_data.allocator; |
| 602 | opt_q.use_packing_layout = false; |
| 603 | ncnn::quantize_to_int8(embed->weight_data, weight_data_int8, weight_data_int8_scales, opt_q); |
| 604 | if (weight_data_int8.empty()) |
| 605 | return -100; |
| 606 | |
| 607 | embed->weight_data = weight_data_int8; |
| 608 | } |
| 609 | |
| 610 | embed->int8_scale_term = 2; |
| 611 | embed->weight_data_int8_scale = weight_data_int8_scales[0]; |
| 612 | } |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | int NetQuantize::quantize_gemm() |
| 618 | { |
| 619 | for (size_t i = 0; i < layers.size(); i++) |
| 620 | { |
| 621 | if (layers[i]->type != "Gemm") |
| 622 | continue; |
| 623 | |
| 624 | // Gemm - quantize weight from fp32 to int8 |
| 625 | ncnn::Gemm* gemm = (ncnn::Gemm*)layers[i]; |
| 626 | |
| 627 | fprintf(stderr, "quantize_gemm %s\n", gemm->name.c_str()); |
| 628 | |
| 629 | // TODO move to ncnn2table |
| 630 | |
| 631 | if (gemm->constantA) |
| 632 | { |
| 633 | if (gemm->transA == 1) |
| 634 | { |
| 635 | // transpose for easier quantization |
| 636 | ncnn::Mat A_data_transposed(gemm->constantK * gemm->constantM); |
| 637 | for (int i = 0; i < gemm->constantM; i++) |
| 638 | { |
| 639 | float* ptr = (float*)A_data_transposed + i * gemm->constantK; |
| 640 | for (int j = 0; j < gemm->constantK; j++) |
| 641 | { |
| 642 | ptr[j] = gemm->A_data[j * gemm->constantM + i]; |
| 643 | } |
| 644 | } |
| 645 | gemm->A_data = A_data_transposed; |
| 646 | gemm->transA = 0; |
| 647 | } |
| 648 | |
| 649 | gemm->A_data_int8_scales.create(gemm->constantM); |
| 650 | for (int i = 0; i < gemm->constantM; i++) |
| 651 | { |
| 652 | float absmax = 0.f; |
| 653 | |
| 654 | const float* ptr = (const float*)gemm->A_data + i * gemm->constantK; |
| 655 | for (int j = 0; j < gemm->constantK; j++) |
| 656 | { |
| 657 | absmax = std::max(absmax, (float)fabs(ptr[j])); |
| 658 | } |
| 659 | |
| 660 | gemm->A_data_int8_scales[i] = absmax == 0.f ? 1.f : 127 / absmax; |
| 661 | } |
| 662 | |
| 663 | ncnn::Mat A_data = gemm->A_data.reshape(gemm->constantK, gemm->constantM); |
| 664 | ncnn::Mat A_data_int8; |
| 665 | |
| 666 | ncnn::Option opt_q = opt; |
| 667 | opt_q.blob_allocator = A_data.allocator; |
| 668 | opt_q.use_packing_layout = false; |
| 669 | ncnn::quantize_to_int8(A_data, A_data_int8, gemm->A_data_int8_scales, opt_q); |
| 670 | if (A_data_int8.empty()) |
| 671 | return -100; |
| 672 | |
| 673 | gemm->A_data = A_data_int8.reshape(gemm->constantK * gemm->constantM); |
| 674 | } |
| 675 | |
| 676 | if (gemm->constantB) |
| 677 | { |
| 678 | if (gemm->transB == 0) |
| 679 | { |
| 680 | // transpose for easier quantization |
| 681 | ncnn::Mat B_data_transposed(gemm->constantK * gemm->constantN); |
| 682 | for (int i = 0; i < gemm->constantN; i++) |
| 683 | { |
| 684 | float* ptr = (float*)B_data_transposed + i * gemm->constantK; |
| 685 | for (int j = 0; j < gemm->constantK; j++) |
| 686 | { |
| 687 | ptr[j] = gemm->B_data[j * gemm->constantN + i]; |
| 688 | } |
| 689 | } |
| 690 | gemm->B_data = B_data_transposed; |
| 691 | gemm->transB = 1; |
| 692 | } |
| 693 | |
| 694 | const float* ptr = gemm->B_data; |
| 695 | float absmax = 0.f; |
| 696 | for (int j = 0; j < gemm->B_data.w; j++) |
| 697 | { |
| 698 | absmax = std::max(absmax, (float)fabs(ptr[j])); |
| 699 | } |
| 700 | |
| 701 | gemm->B_data_int8_scale = absmax == 0.f ? 1.f : 127 / absmax; |
| 702 | |
| 703 | ncnn::Mat B_data_int8_scales(1); |
| 704 | B_data_int8_scales[0] = gemm->B_data_int8_scale; |
| 705 | |
| 706 | ncnn::Mat B_data_int8; |
| 707 | |
| 708 | ncnn::Option opt_q = opt; |
| 709 | opt_q.blob_allocator = gemm->B_data.allocator; |
| 710 | opt_q.use_packing_layout = false; |
| 711 | ncnn::quantize_to_int8(gemm->B_data, B_data_int8, B_data_int8_scales, opt_q); |
| 712 | if (B_data_int8.empty()) |
| 713 | return -100; |
| 714 | |
| 715 | gemm->B_data = B_data_int8; |
| 716 | } |
| 717 | |
| 718 | gemm->int8_scale_term = 2; |
| 719 | } |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | int NetQuantize::fuse_requantize() |
| 725 | { |
| 726 | const size_t layer_count = layers.size(); |
| 727 | for (size_t i = 0; i < layer_count; i++) |
| 728 | { |
| 729 | if (layers[i]->type != "Convolution" && layers[i]->type != "ConvolutionDepthWise") |
| 730 | continue; |
| 731 | |
| 732 | // Convolution/ConvolutionDepthWise - Convolution/ConvolutionDepthWise |
| 733 | int top_blob_index = layers[i]->tops[0]; |
| 734 | |
| 735 | size_t j = i + 1; |
| 736 | for (; j < layer_count; j++) |
| 737 | { |
| 738 | if (layers[j]->type != "Convolution" && layers[j]->type != "ConvolutionDepthWise") |
| 739 | continue; |
| 740 | |
| 741 | if (layers[j]->bottoms.size() != 1) |
| 742 | continue; |
| 743 | |
| 744 | if (layers[j]->bottoms[0] == top_blob_index) |
| 745 | break; |
| 746 | } |
| 747 | |
| 748 | if (j == layer_count) |
| 749 | continue; |
| 750 | |
| 751 | // fuse requantize |
| 752 | fprintf(stderr, "fuse_requantize %s %s\n", layers[i]->name.c_str(), layers[j]->name.c_str()); |
| 753 | |
| 754 | if (layers[i]->type == "Convolution" && layers[j]->type == "Convolution") |
| 755 | { |
| 756 | ncnn::Convolution* convolution1 = (ncnn::Convolution*)layers[i]; |
| 757 | ncnn::Convolution* convolution2 = (ncnn::Convolution*)layers[j]; |
| 758 | |
| 759 | if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u) |
| 760 | continue; |
| 761 | |
| 762 | convolution1->int8_scale_term += 100; |
| 763 | convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales; |
| 764 | } |
| 765 | if (layers[i]->type == "Convolution" && layers[j]->type == "ConvolutionDepthWise") |
| 766 | { |
| 767 | ncnn::Convolution* convolution1 = (ncnn::Convolution*)layers[i]; |
| 768 | ncnn::ConvolutionDepthWise* convolution2 = (ncnn::ConvolutionDepthWise*)layers[j]; |
| 769 | |
| 770 | if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u) |
| 771 | continue; |
| 772 | |
| 773 | convolution1->int8_scale_term += 100; |
| 774 | convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales; |
| 775 | } |
| 776 | if (layers[i]->type == "ConvolutionDepthWise" && layers[j]->type == "Convolution") |
| 777 | { |
| 778 | ncnn::ConvolutionDepthWise* convolution1 = (ncnn::ConvolutionDepthWise*)layers[i]; |
| 779 | ncnn::Convolution* convolution2 = (ncnn::Convolution*)layers[j]; |
| 780 | |
| 781 | if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u) |
| 782 | continue; |
| 783 | |
| 784 | convolution1->int8_scale_term += 100; |
| 785 | convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales; |
| 786 | } |
| 787 | if (layers[i]->type == "ConvolutionDepthWise" && layers[j]->type == "ConvolutionDepthWise") |
| 788 | { |
| 789 | ncnn::ConvolutionDepthWise* convolution1 = (ncnn::ConvolutionDepthWise*)layers[i]; |
| 790 | ncnn::ConvolutionDepthWise* convolution2 = (ncnn::ConvolutionDepthWise*)layers[j]; |
| 791 | |
| 792 | if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u) |
| 793 | continue; |
| 794 | |
| 795 | convolution1->int8_scale_term += 100; |
| 796 | convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | for (size_t i = 0; i < layer_count; i++) |
| 801 | { |
| 802 | if (layers[i]->type != "Convolution" && layers[i]->type != "ConvolutionDepthWise") |
| 803 | continue; |
| 804 | |
| 805 | // Convolution/ConvolutionDepthWise - Split - Convolution/ConvolutionDepthWise |
| 806 | int top_blob_index = layers[i]->tops[0]; |
| 807 | |
| 808 | size_t j = i + 1; |
| 809 | for (; j < layer_count; j++) |
| 810 | { |
| 811 | if (layers[j]->type != "Split") |
| 812 | continue; |
| 813 | |
| 814 | if (layers[j]->bottoms.size() != 1) |
| 815 | continue; |
| 816 | |
| 817 | if (layers[j]->bottoms[0] == top_blob_index) |
| 818 | break; |
| 819 | } |
| 820 | |
| 821 | if (j == layer_count) |
| 822 | continue; |
| 823 | |
| 824 | ncnn::Split* split = (ncnn::Split*)layers[j]; |
| 825 | |
| 826 | bool all_conv = true; |
| 827 | for (size_t p = 0; p < split->tops.size(); p++) |
| 828 | { |
| 829 | int split_top_blob_index = split->tops[p]; |
| 830 | |
| 831 | size_t k = j + 1; |
| 832 | for (; k < layer_count; k++) |
| 833 | { |
| 834 | if (layers[k]->type != "Convolution" && layers[k]->type != "ConvolutionDepthWise") |
| 835 | continue; |
| 836 | |
| 837 | if (layers[k]->bottoms.size() != 1) |
| 838 | continue; |
| 839 | |
| 840 | if (layers[k]->bottoms[0] == split_top_blob_index) |
| 841 | break; |
| 842 | } |
| 843 | |
| 844 | if (k == layer_count) |
| 845 | { |
| 846 | all_conv = false; |
| 847 | break; |
| 848 | } |
| 849 | |
| 850 | if (layers[k]->type == "Convolution") |
| 851 | { |
| 852 | ncnn::Convolution* convolution = (ncnn::Convolution*)layers[k]; |
| 853 | if (convolution->weight_data.elemsize != 1u) |
| 854 | { |
| 855 | all_conv = false; |
| 856 | break; |
| 857 | } |
| 858 | } |
| 859 | if (layers[k]->type == "ConvolutionDepthWise") |
| 860 | { |
| 861 | ncnn::ConvolutionDepthWise* convolution = (ncnn::ConvolutionDepthWise*)layers[k]; |
| 862 | if (convolution->weight_data.elemsize != 1u) |
| 863 | { |
| 864 | all_conv = false; |
| 865 | break; |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | if (!all_conv) |
| 871 | continue; |
| 872 | |
| 873 | j = blobs[split->tops[0]].consumer; |
| 874 | |
| 875 | // fuse requantize |
| 876 | fprintf(stderr, "fuse_requantize %s %s\n", layers[i]->name.c_str(), split->name.c_str()); |
| 877 | |
| 878 | if (layers[i]->type == "Convolution" && layers[j]->type == "Convolution") |
| 879 | { |
| 880 | ncnn::Convolution* convolution1 = (ncnn::Convolution*)layers[i]; |
| 881 | ncnn::Convolution* convolution2 = (ncnn::Convolution*)layers[j]; |
| 882 | |
| 883 | if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u) |
| 884 | continue; |
| 885 | |
| 886 | convolution1->int8_scale_term += 100; |
| 887 | convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales; |
| 888 | } |
| 889 | if (layers[i]->type == "Convolution" && layers[j]->type == "ConvolutionDepthWise") |
| 890 | { |
| 891 | ncnn::Convolution* convolution1 = (ncnn::Convolution*)layers[i]; |
| 892 | ncnn::ConvolutionDepthWise* convolution2 = (ncnn::ConvolutionDepthWise*)layers[j]; |
| 893 | |
| 894 | if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u) |
| 895 | continue; |
| 896 | |
| 897 | convolution1->int8_scale_term += 100; |
| 898 | convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales; |
| 899 | } |
| 900 | if (layers[i]->type == "ConvolutionDepthWise" && layers[j]->type == "Convolution") |
| 901 | { |
| 902 | ncnn::ConvolutionDepthWise* convolution1 = (ncnn::ConvolutionDepthWise*)layers[i]; |
| 903 | ncnn::Convolution* convolution2 = (ncnn::Convolution*)layers[j]; |
| 904 | |
| 905 | if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u) |
| 906 | continue; |
| 907 | |
| 908 | convolution1->int8_scale_term += 100; |
| 909 | convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales; |
| 910 | } |
| 911 | if (layers[i]->type == "ConvolutionDepthWise" && layers[j]->type == "ConvolutionDepthWise") |
| 912 | { |
| 913 | ncnn::ConvolutionDepthWise* convolution1 = (ncnn::ConvolutionDepthWise*)layers[i]; |
| 914 | ncnn::ConvolutionDepthWise* convolution2 = (ncnn::ConvolutionDepthWise*)layers[j]; |
| 915 | |
| 916 | if (convolution1->weight_data.elemsize != 1u || convolution2->weight_data.elemsize != 1u) |
| 917 | continue; |
| 918 | |
| 919 | convolution1->int8_scale_term += 100; |
| 920 | convolution1->top_blob_int8_scales = convolution2->bottom_blob_int8_scales; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | int main(int argc, char** argv) |
| 928 | { |
| 929 | if (argc != 5 && argc != 6) |
| 930 | { |
| 931 | fprintf(stderr, "usage: %s [inparam] [inbin] [outparam] [outbin] [calibration table]\n", argv[0]); |
| 932 | return -1; |
| 933 | } |
| 934 | |
| 935 | const char* inparam = argv[1]; |
| 936 | const char* inbin = argv[2]; |
| 937 | const char* outparam = argv[3]; |
| 938 | const char* outbin = argv[4]; |
| 939 | const char* int8scale_table_path = argc == 6 ? argv[5] : NULL; |
| 940 | |
| 941 | NetQuantize quantizer; |
| 942 | |
| 943 | // parse the calibration scale table |
| 944 | if (int8scale_table_path) |
| 945 | { |
| 946 | bool s2 = read_int8scale_table(int8scale_table_path, quantizer.blob_int8scale_table, quantizer.weight_int8scale_table); |
| 947 | if (!s2) |
| 948 | { |
| 949 | fprintf(stderr, "read_int8scale_table failed\n"); |
| 950 | return -1; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | quantizer.load_param(inparam); |
| 955 | if (strcmp(inbin, "null") == 0) |
| 956 | { |
| 957 | DataReaderFromEmpty dr; |
| 958 | quantizer.load_model(dr); |
| 959 | quantizer.gen_random_weight = true; |
| 960 | } |
| 961 | else |
| 962 | quantizer.load_model(inbin); |
| 963 | |
| 964 | quantizer.quantize_convolution(); |
| 965 | quantizer.quantize_convolutiondepthwise(); |
| 966 | quantizer.quantize_innerproduct(); |
| 967 | |
| 968 | quantizer.quantize_rnn(); |
| 969 | quantizer.quantize_lstm(); |
| 970 | quantizer.quantize_gru(); |
| 971 | quantizer.quantize_embed(); |
| 972 | quantizer.quantize_gemm(); |
| 973 | |
| 974 | quantizer.fuse_requantize(); |
| 975 | |
| 976 | quantizer.save(outparam, outbin); |
| 977 | |
| 978 | return 0; |
| 979 | } |