| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "convolution.h" |
| 16 | |
| 17 | #include "layer_type.h" |
| 18 | |
| 19 | #include "fused_activation.h" |
| 20 | |
| 21 | namespace ncnn { |
| 22 | |
| 23 | Convolution::Convolution() |
| 24 | { |
| 25 | one_blob_only = true; |
| 26 | support_inplace = false; |
| 27 | } |
| 28 | |
| 29 | int Convolution::load_param(const ParamDict& pd) |
| 30 | { |
| 31 | num_output = pd.get(0, 0); |
| 32 | kernel_w = pd.get(1, 0); |
| 33 | kernel_h = pd.get(11, kernel_w); |
| 34 | dilation_w = pd.get(2, 1); |
| 35 | dilation_h = pd.get(12, dilation_w); |
| 36 | stride_w = pd.get(3, 1); |
| 37 | stride_h = pd.get(13, stride_w); |
| 38 | pad_left = pd.get(4, 0); |
| 39 | pad_right = pd.get(15, pad_left); |
| 40 | pad_top = pd.get(14, pad_left); |
| 41 | pad_bottom = pd.get(16, pad_top); |
| 42 | pad_value = pd.get(18, 0.f); |
| 43 | bias_term = pd.get(5, 0); |
| 44 | weight_data_size = pd.get(6, 0); |
| 45 | int8_scale_term = pd.get(8, 0); |
| 46 | activation_type = pd.get(9, 0); |
| 47 | activation_params = pd.get(10, Mat()); |
| 48 | |
| 49 | dynamic_weight = pd.get(19, 0); |
| 50 | |
| 51 | if (dynamic_weight) |
| 52 | { |
| 53 | one_blob_only = false; |
| 54 | } |
| 55 | |
| 56 | if (int8_scale_term) |
| 57 | { |
| 58 | #if NCNN_INT8 |
| 59 | support_int8_storage = true; |
| 60 | #else |
| 61 | NCNN_LOGE("please build ncnn with NCNN_INT8 enabled for int8 inference"); |
| 62 | return -1; |
| 63 | #endif |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | int Convolution::load_model(const ModelBin& mb) |
| 70 | { |
| 71 | if (dynamic_weight) |
| 72 | return 0; |
| 73 | |
| 74 | weight_data = mb.load(weight_data_size, 0); |
| 75 | if (weight_data.empty()) |
| 76 | return -100; |
| 77 | |
| 78 | if (bias_term) |
| 79 | { |
| 80 | bias_data = mb.load(num_output, 1); |
| 81 | if (bias_data.empty()) |
| 82 | return -100; |
| 83 | } |
| 84 | |
| 85 | #if NCNN_INT8 |
| 86 | if (int8_scale_term) |
| 87 | { |
| 88 | weight_data_int8_scales = mb.load(num_output, 1); |
| 89 | bottom_blob_int8_scales = mb.load(1, 1); |
| 90 | } |
| 91 | |
| 92 | if (int8_scale_term > 100) |
| 93 | { |
| 94 | top_blob_int8_scales = mb.load(1, 1); |
| 95 | } |
| 96 | #endif // NCNN_INT8 |
| 97 | |
| 98 | #if NCNN_INT8 |
| 99 | // runtime quantize the weight data |
| 100 | if (weight_data.elemsize == (size_t)4u && int8_scale_term) |
| 101 | { |
| 102 | const int maxk = kernel_w * kernel_h; |
| 103 | const int num_input = weight_data_size / num_output / maxk; |
| 104 | |
| 105 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 106 | |
| 107 | Mat weight_data_int8; |
| 108 | |
| 109 | Option opt_q; |
| 110 | opt_q.num_threads = 1; |
| 111 | opt_q.blob_allocator = weight_data.allocator; |
| 112 | opt_q.use_packing_layout = false; |
| 113 | quantize_to_int8(weight_data_r2, weight_data_int8, weight_data_int8_scales, opt_q); |
| 114 | if (weight_data_int8.empty()) |
| 115 | return -100; |
| 116 | |
| 117 | weight_data = weight_data_int8.reshape(weight_data_size); |
| 118 | } |
| 119 | #endif // NCNN_INT8 |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int convolution(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data, const Mat& bias_data, int kernel_w, int kernel_h, int stride_w, int stride_h, int dilation_w, int dilation_h, int activation_type, const Mat& activation_params, const Option& opt) |
| 125 | { |
| 126 | const int w = bottom_blob.w; |
| 127 | const int inch = bottom_blob.c; |
| 128 | |
| 129 | const int outw = top_blob.w; |
| 130 | const int outh = top_blob.h; |
| 131 | const int outch = top_blob.c; |
| 132 | |
| 133 | const int bias_term = bias_data.empty() ? 0 : 1; |
| 134 | |
| 135 | const int maxk = kernel_w * kernel_h; |
| 136 | |
| 137 | // kernel offsets |
| 138 | std::vector<int> _space_ofs(maxk); |
| 139 | int* space_ofs = &_space_ofs[0]; |
| 140 | { |
| 141 | int p1 = 0; |
| 142 | int p2 = 0; |
| 143 | int gap = w * dilation_h - kernel_w * dilation_w; |
| 144 | for (int i = 0; i < kernel_h; i++) |
| 145 | { |
| 146 | for (int j = 0; j < kernel_w; j++) |
| 147 | { |
| 148 | space_ofs[p1] = p2; |
| 149 | p1++; |
| 150 | p2 += dilation_w; |
| 151 | } |
| 152 | p2 += gap; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | #pragma omp parallel for num_threads(opt.num_threads) |
| 157 | for (int p = 0; p < outch; p++) |
| 158 | { |
| 159 | float* outptr = top_blob.channel(p); |
| 160 | |
| 161 | for (int i = 0; i < outh; i++) |
| 162 | { |
| 163 | for (int j = 0; j < outw; j++) |
| 164 | { |
| 165 | float sum = 0.f; |
| 166 | |
| 167 | if (bias_term) |
| 168 | sum = bias_data[p]; |
| 169 | |
| 170 | const float* kptr = (const float*)weight_data + maxk * inch * p; |
| 171 | |
| 172 | for (int q = 0; q < inch; q++) |
| 173 | { |
| 174 | const Mat m = bottom_blob.channel(q); |
| 175 | const float* sptr = m.row(i * stride_h) + j * stride_w; |
| 176 | |
| 177 | for (int k = 0; k < maxk; k++) // 29.23 |
| 178 | { |
| 179 | float val = sptr[space_ofs[k]]; // 20.72 |
| 180 | float wt = kptr[k]; |
| 181 | sum += val * wt; // 41.45 |
| 182 | } |
| 183 | |
| 184 | kptr += maxk; |
| 185 | } |
| 186 | |
| 187 | outptr[j] = activation_ss(sum, activation_type, activation_params); |
| 188 | } |
| 189 | |
| 190 | outptr += outw; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | int Convolution::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 198 | { |
| 199 | #if NCNN_INT8 |
| 200 | if (opt.use_int8_inference && weight_data.elemsize == (size_t)1u) |
| 201 | { |
| 202 | return forward_int8(bottom_blob, top_blob, opt); |
| 203 | } |
| 204 | #endif |
| 205 | |
| 206 | // flattened blob, implement as InnerProduct |
| 207 | if (bottom_blob.dims == 1 && kernel_w == 1 && kernel_h == 1) |
| 208 | { |
| 209 | int num_input = weight_data_size / num_output; |
| 210 | if (bottom_blob.w * bottom_blob.elempack == num_input) |
| 211 | { |
| 212 | // call InnerProduct |
| 213 | ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::InnerProduct); |
| 214 | |
| 215 | // set param |
| 216 | ncnn::ParamDict pd; |
| 217 | pd.set(0, num_output); |
| 218 | pd.set(1, bias_term); |
| 219 | pd.set(2, weight_data_size); |
| 220 | pd.set(8, int8_scale_term); |
| 221 | pd.set(9, activation_type); |
| 222 | pd.set(10, activation_params); |
| 223 | |
| 224 | op->load_param(pd); |
| 225 | |
| 226 | // set weights |
| 227 | ncnn::Mat weights[4]; |
| 228 | weights[0] = weight_data; |
| 229 | weights[1] = bias_data; |
| 230 | |
| 231 | #if NCNN_INT8 |
| 232 | if (int8_scale_term) |
| 233 | { |
| 234 | weights[2] = weight_data_int8_scales; |
| 235 | weights[3] = bottom_blob_int8_scales; |
| 236 | } |
| 237 | #endif |
| 238 | |
| 239 | op->load_model(ModelBinFromMatArray(weights)); |
| 240 | |
| 241 | op->create_pipeline(opt); |
| 242 | |
| 243 | // forward |
| 244 | int ret = op->forward(bottom_blob, top_blob, opt); |
| 245 | |
| 246 | op->destroy_pipeline(opt); |
| 247 | |
| 248 | delete op; |
| 249 | |
| 250 | return ret; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | Mat bottom_blob_bordered; |
| 255 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 256 | if (bottom_blob_bordered.empty()) |
| 257 | return -100; |
| 258 | |
| 259 | const int w = bottom_blob_bordered.w; |
| 260 | const int h = bottom_blob_bordered.h; |
| 261 | const size_t elemsize = bottom_blob_bordered.elemsize; |
| 262 | |
| 263 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 264 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 265 | |
| 266 | const int outw = (w - kernel_extent_w) / stride_w + 1; |
| 267 | const int outh = (h - kernel_extent_h) / stride_h + 1; |
| 268 | |
| 269 | top_blob.create(outw, outh, num_output, elemsize, opt.blob_allocator); |
| 270 | if (top_blob.empty()) |
| 271 | return -100; |
| 272 | |
| 273 | int ret = convolution(bottom_blob_bordered, top_blob, weight_data, bias_data, kernel_w, kernel_h, stride_w, stride_h, dilation_w, dilation_h, activation_type, activation_params, opt); |
| 274 | if (ret != 0) |
| 275 | return ret; |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | int Convolution::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 281 | { |
| 282 | const Mat& bottom_blob = bottom_blobs[0]; |
| 283 | const Mat& _weight_data = bottom_blobs[1]; |
| 284 | Mat& top_blob = top_blobs[0]; |
| 285 | |
| 286 | const int _kernel_w = _weight_data.w; |
| 287 | const int _kernel_h = _weight_data.h; |
| 288 | const int _num_output = _weight_data.c; |
| 289 | |
| 290 | Mat weight_data_flattened; |
| 291 | flatten(_weight_data, weight_data_flattened, opt); |
| 292 | if (weight_data_flattened.empty()) |
| 293 | return -100; |
| 294 | |
| 295 | Mat bias_data_flattened; |
| 296 | if (bias_term) |
| 297 | { |
| 298 | const Mat& _bias_data = bottom_blobs[2]; |
| 299 | flatten(_bias_data, bias_data_flattened, opt); |
| 300 | if (bias_data_flattened.empty()) |
| 301 | return -100; |
| 302 | } |
| 303 | |
| 304 | Mat bottom_blob_bordered; |
| 305 | make_padding(bottom_blob, bottom_blob_bordered, _kernel_w, _kernel_h, opt); |
| 306 | if (bottom_blob_bordered.empty()) |
| 307 | return -100; |
| 308 | |
| 309 | const int w = bottom_blob_bordered.w; |
| 310 | const int h = bottom_blob_bordered.h; |
| 311 | const size_t elemsize = bottom_blob_bordered.elemsize; |
| 312 | |
| 313 | const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1; |
| 314 | const int kernel_extent_h = dilation_h * (_kernel_h - 1) + 1; |
| 315 | |
| 316 | const int outw = (w - kernel_extent_w) / stride_w + 1; |
| 317 | const int outh = (h - kernel_extent_h) / stride_h + 1; |
| 318 | |
| 319 | top_blob.create(outw, outh, _num_output, elemsize, opt.blob_allocator); |
| 320 | if (top_blob.empty()) |
| 321 | return -100; |
| 322 | |
| 323 | int ret = convolution(bottom_blob_bordered, top_blob, weight_data_flattened, bias_data_flattened, _kernel_w, _kernel_h, stride_w, stride_h, dilation_w, dilation_h, activation_type, activation_params, opt); |
| 324 | if (ret != 0) |
| 325 | return ret; |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | void Convolution::make_padding(const Mat& bottom_blob, Mat& bottom_blob_bordered, const Option& opt) const |
| 331 | { |
| 332 | make_padding(bottom_blob, bottom_blob_bordered, kernel_w, kernel_h, opt); |
| 333 | } |
| 334 | |
| 335 | void Convolution::make_padding(const Mat& bottom_blob, Mat& bottom_blob_bordered, int _kernel_w, int _kernel_h, const Option& opt) const |
| 336 | { |
| 337 | int w = bottom_blob.w; |
| 338 | int h = bottom_blob.h; |
| 339 | |
| 340 | const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1; |
| 341 | const int kernel_extent_h = dilation_h * (_kernel_h - 1) + 1; |
| 342 | |
| 343 | bottom_blob_bordered = bottom_blob; |
| 344 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) |
| 345 | { |
| 346 | Option opt_b = opt; |
| 347 | opt_b.blob_allocator = opt.workspace_allocator; |
| 348 | copy_make_border(bottom_blob, bottom_blob_bordered, pad_top, pad_bottom, pad_left, pad_right, BORDER_CONSTANT, pad_value, opt_b); |
| 349 | } |
| 350 | else if (pad_left == -233 && pad_right == -233 && pad_top == -233 && pad_bottom == -233) |
| 351 | { |
| 352 | // tensorflow padding=SAME or onnx padding=SAME_UPPER |
| 353 | int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w; |
| 354 | int hpad = kernel_extent_h + (h - 1) / stride_h * stride_h - h; |
| 355 | if (wpad > 0 || hpad > 0) |
| 356 | { |
| 357 | Option opt_b = opt; |
| 358 | opt_b.blob_allocator = opt.workspace_allocator; |
| 359 | copy_make_border(bottom_blob, bottom_blob_bordered, hpad / 2, hpad - hpad / 2, wpad / 2, wpad - wpad / 2, BORDER_CONSTANT, pad_value, opt_b); |
| 360 | } |
| 361 | } |
| 362 | else if (pad_left == -234 && pad_right == -234 && pad_top == -234 && pad_bottom == -234) |
| 363 | { |
| 364 | // onnx padding=SAME_LOWER |
| 365 | int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w; |
| 366 | int hpad = kernel_extent_h + (h - 1) / stride_h * stride_h - h; |
| 367 | if (wpad > 0 || hpad > 0) |
| 368 | { |
| 369 | Option opt_b = opt; |
| 370 | opt_b.blob_allocator = opt.workspace_allocator; |
| 371 | copy_make_border(bottom_blob, bottom_blob_bordered, hpad - hpad / 2, hpad / 2, wpad - wpad / 2, wpad / 2, BORDER_CONSTANT, pad_value, opt_b); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | #if NCNN_INT8 |
| 377 | static inline signed char float2int8(float v) |
| 378 | { |
| 379 | int int32 = static_cast<int>(round(v)); |
| 380 | if (int32 > 127) return 127; |
| 381 | if (int32 < -127) return -127; |
| 382 | return (signed char)int32; |
| 383 | } |
| 384 | |
| 385 | int Convolution::forward_int8(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 386 | { |
| 387 | int w = bottom_blob.w; |
| 388 | int h = bottom_blob.h; |
| 389 | int channels = bottom_blob.c; |
| 390 | size_t elemsize = bottom_blob.elemsize; |
| 391 | |
| 392 | // NCNN_LOGE("Convolution input %d x %d ksize=%d %d stride=%d %d", w, h, kernel_w, kernel_h, stride_w, stride_h); |
| 393 | |
| 394 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 395 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 396 | |
| 397 | Mat bottom_blob_unbordered = bottom_blob; |
| 398 | if (elemsize != 1) |
| 399 | { |
| 400 | Option opt_g = opt; |
| 401 | opt_g.blob_allocator = opt.workspace_allocator; |
| 402 | |
| 403 | quantize_to_int8(bottom_blob, bottom_blob_unbordered, bottom_blob_int8_scales, opt_g); |
| 404 | if (bottom_blob_unbordered.empty()) |
| 405 | return -100; |
| 406 | } |
| 407 | |
| 408 | Mat bottom_blob_bordered; |
| 409 | make_padding(bottom_blob_unbordered, bottom_blob_bordered, opt); |
| 410 | if (bottom_blob_bordered.empty()) |
| 411 | return -100; |
| 412 | |
| 413 | w = bottom_blob_bordered.w; |
| 414 | h = bottom_blob_bordered.h; |
| 415 | |
| 416 | int outw = (w - kernel_extent_w) / stride_w + 1; |
| 417 | int outh = (h - kernel_extent_h) / stride_h + 1; |
| 418 | |
| 419 | const int maxk = kernel_w * kernel_h; |
| 420 | |
| 421 | // kernel offsets |
| 422 | std::vector<int> _space_ofs(maxk); |
| 423 | int* space_ofs = &_space_ofs[0]; |
| 424 | { |
| 425 | int p1 = 0; |
| 426 | int p2 = 0; |
| 427 | int gap = w * dilation_h - kernel_w * dilation_w; |
| 428 | for (int i = 0; i < kernel_h; i++) |
| 429 | { |
| 430 | for (int j = 0; j < kernel_w; j++) |
| 431 | { |
| 432 | space_ofs[p1] = p2; |
| 433 | p1++; |
| 434 | p2 += dilation_w; |
| 435 | } |
| 436 | p2 += gap; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // int8 |
| 441 | bool use_int8_requantize = int8_scale_term > 100; |
| 442 | size_t out_elemsize = use_int8_requantize ? 1u : 4u; |
| 443 | |
| 444 | top_blob.create(outw, outh, num_output, out_elemsize, opt.blob_allocator); |
| 445 | if (top_blob.empty()) |
| 446 | return -100; |
| 447 | |
| 448 | // num_output |
| 449 | #pragma omp parallel for num_threads(opt.num_threads) |
| 450 | for (int p = 0; p < num_output; p++) |
| 451 | { |
| 452 | signed char* outptr = top_blob.channel(p); |
| 453 | |
| 454 | for (int i = 0; i < outh; i++) |
| 455 | { |
| 456 | for (int j = 0; j < outw; j++) |
| 457 | { |
| 458 | int sum = 0; |
| 459 | |
| 460 | const signed char* kptr = (const signed char*)weight_data + maxk * channels * p; |
| 461 | |
| 462 | // channels |
| 463 | for (int q = 0; q < channels; q++) |
| 464 | { |
| 465 | const Mat m = bottom_blob_bordered.channel(q); |
| 466 | const signed char* sptr = m.row<signed char>(i * stride_h) + j * stride_w; |
| 467 | |
| 468 | for (int k = 0; k < maxk; k++) |
| 469 | { |
| 470 | int val = sptr[space_ofs[k]]; |
| 471 | int wt = kptr[k]; |
| 472 | sum += val * wt; |
| 473 | } |
| 474 | |
| 475 | kptr += maxk; |
| 476 | } |
| 477 | |
| 478 | float scale_in; |
| 479 | if (weight_data_int8_scales[p] == 0) |
| 480 | scale_in = 0; |
| 481 | else |
| 482 | scale_in = 1.f / (bottom_blob_int8_scales[0] * weight_data_int8_scales[p]); |
| 483 | |
| 484 | float sumfp32 = sum * scale_in; |
| 485 | |
| 486 | if (bias_term) |
| 487 | sumfp32 += bias_data[p]; |
| 488 | |
| 489 | sumfp32 = activation_ss(sumfp32, activation_type, activation_params); |
| 490 | |
| 491 | if (use_int8_requantize) |
| 492 | { |
| 493 | // requantize |
| 494 | float scale_out = top_blob_int8_scales[0]; |
| 495 | signed char sums8 = float2int8(sumfp32 * scale_out); |
| 496 | outptr[0] = sums8; |
| 497 | outptr += 1; |
| 498 | } |
| 499 | else |
| 500 | { |
| 501 | // dequantize |
| 502 | ((float*)outptr)[0] = sumfp32; |
| 503 | outptr += 4; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | #endif // NCNN_INT8 |
| 512 | |
| 513 | } // namespace ncnn |