| 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 "convolutiondepthwise_x86.h" |
| 16 | |
| 17 | #if __SSE2__ |
| 18 | #include <emmintrin.h> |
| 19 | #if __AVX__ |
| 20 | #include <immintrin.h> |
| 21 | #endif |
| 22 | #endif // __SSE2__ |
| 23 | |
| 24 | #include "x86_activation.h" |
| 25 | #include "x86_usability.h" |
| 26 | |
| 27 | #include "layer_type.h" |
| 28 | |
| 29 | namespace ncnn { |
| 30 | |
| 31 | #if __SSE2__ |
| 32 | #include "convolutiondepthwise_3x3_pack4.h" |
| 33 | #include "convolutiondepthwise_5x5_pack4.h" |
| 34 | #if __AVX__ |
| 35 | #include "convolutiondepthwise_3x3_pack8.h" |
| 36 | #include "convolutiondepthwise_5x5_pack8.h" |
| 37 | #if __AVX512F__ |
| 38 | #include "convolutiondepthwise_3x3_pack16.h" |
| 39 | #include "convolutiondepthwise_5x5_pack16.h" |
| 40 | #endif // __AVX512F__ |
| 41 | #endif // __AVX__ |
| 42 | #endif // __SSE2__ |
| 43 | #include "convolutiondepthwise_3x3.h" |
| 44 | |
| 45 | #if NCNN_INT8 |
| 46 | #include "convolutiondepthwise_3x3_int8.h" |
| 47 | #endif // NCNN_INT8 |
| 48 | |
| 49 | ConvolutionDepthWise_x86::ConvolutionDepthWise_x86() |
| 50 | { |
| 51 | #if __SSE2__ |
| 52 | support_packing = true; |
| 53 | #endif // __SSE2__ |
| 54 | activation = 0; |
| 55 | } |
| 56 | |
| 57 | int ConvolutionDepthWise_x86::create_pipeline(const Option& opt) |
| 58 | { |
| 59 | if (dynamic_weight) |
| 60 | return 0; |
| 61 | |
| 62 | activation = create_activation_layer(activation_type, activation_params, opt); |
| 63 | |
| 64 | #if NCNN_INT8 |
| 65 | if (opt.use_int8_inference && weight_data.elemsize == (size_t)1u) |
| 66 | { |
| 67 | return create_pipeline_int8_x86(opt); |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | const int maxk = kernel_w * kernel_h; |
| 72 | int channels = (weight_data_size / group) / maxk / (num_output / group) * group; |
| 73 | |
| 74 | // depth-wise |
| 75 | if (channels == group && group == num_output) |
| 76 | { |
| 77 | int elempack = 1; |
| 78 | #if __SSE2__ |
| 79 | if (opt.use_packing_layout) |
| 80 | { |
| 81 | #if __AVX512F__ |
| 82 | elempack = channels % 16 == 0 ? 16 : channels % 8 == 0 ? 8 : channels % 4 == 0 ? 4 : 1; |
| 83 | #elif __AVX__ |
| 84 | elempack = channels % 8 == 0 ? 8 : channels % 4 == 0 ? 4 : 1; |
| 85 | #else |
| 86 | elempack = channels % 4 == 0 ? 4 : 1; |
| 87 | #endif |
| 88 | } |
| 89 | #endif // __SSE2__ |
| 90 | |
| 91 | #if __SSE2__ |
| 92 | #if __AVX__ |
| 93 | // pack16 |
| 94 | #if __AVX512F__ |
| 95 | if (elempack == 16) |
| 96 | { |
| 97 | Mat weight_data_r2 = weight_data.reshape(maxk, group); |
| 98 | convert_packing(weight_data_r2, weight_data_tm, 16, opt); |
| 99 | } |
| 100 | #endif // __AVX512F__ |
| 101 | |
| 102 | // pack8 |
| 103 | if (elempack == 8) |
| 104 | { |
| 105 | Mat weight_data_r2 = weight_data.reshape(maxk, group); |
| 106 | convert_packing(weight_data_r2, weight_data_tm, 8, opt); |
| 107 | } |
| 108 | #endif // __AVX__ |
| 109 | |
| 110 | // pack4 |
| 111 | if (elempack == 4) |
| 112 | { |
| 113 | Mat weight_data_r2 = weight_data.reshape(maxk, group); |
| 114 | convert_packing(weight_data_r2, weight_data_tm, 4, opt); |
| 115 | } |
| 116 | #endif // __SSE2__ |
| 117 | |
| 118 | if (elempack == 1) |
| 119 | { |
| 120 | // depth-wise specific |
| 121 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 122 | { |
| 123 | weight_data_tm = weight_data; |
| 124 | } |
| 125 | else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 126 | { |
| 127 | weight_data_tm = weight_data; |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | create_group_ops(opt); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (opt.lightmode) |
| 136 | weight_data.release(); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | // group convolution |
| 142 | create_group_ops(opt); |
| 143 | |
| 144 | if (opt.lightmode) |
| 145 | weight_data.release(); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | int ConvolutionDepthWise_x86::create_group_ops(const Option& opt) |
| 151 | { |
| 152 | // create Convolution op for each group |
| 153 | const int maxk = kernel_w * kernel_h; |
| 154 | int channels = (weight_data_size / group) / maxk / (num_output / group) * group; |
| 155 | |
| 156 | for (int i = 0; i < (int)group_ops.size(); i++) |
| 157 | delete group_ops[i]; |
| 158 | |
| 159 | group_ops.clear(); |
| 160 | |
| 161 | const int channels_g = channels / group; |
| 162 | const int num_output_g = num_output / group; |
| 163 | |
| 164 | group_ops.resize(group); |
| 165 | |
| 166 | for (int g = 0; g < group; g++) |
| 167 | { |
| 168 | Mat weight_data_g = weight_data.range(maxk * channels_g * num_output_g * g, maxk * channels_g * num_output_g).clone(); |
| 169 | Mat bias_data_g; |
| 170 | if (bias_term) |
| 171 | bias_data_g = bias_data.range(num_output_g * g, num_output_g); |
| 172 | |
| 173 | ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Convolution); |
| 174 | |
| 175 | // set param |
| 176 | ncnn::ParamDict pd; |
| 177 | pd.set(0, num_output_g); // num_output |
| 178 | pd.set(1, kernel_w); |
| 179 | pd.set(11, kernel_h); |
| 180 | pd.set(2, dilation_w); |
| 181 | pd.set(12, dilation_h); |
| 182 | pd.set(3, stride_w); |
| 183 | pd.set(13, stride_h); |
| 184 | pd.set(4, 0); // pad_w |
| 185 | pd.set(14, 0); // pad_h |
| 186 | pd.set(5, bias_term); |
| 187 | pd.set(6, maxk * channels_g * num_output_g); // weight_data_size |
| 188 | pd.set(8, int8_scale_term); |
| 189 | pd.set(9, activation_type); |
| 190 | pd.set(10, activation_params); |
| 191 | |
| 192 | op->load_param(pd); |
| 193 | |
| 194 | // set weights |
| 195 | if (bias_term) |
| 196 | { |
| 197 | ncnn::Mat weights[5]; |
| 198 | weights[0] = weight_data_g; |
| 199 | weights[1] = bias_data_g; |
| 200 | |
| 201 | #if NCNN_INT8 |
| 202 | if (int8_scale_term) |
| 203 | { |
| 204 | Mat weight_data_int8_scales_g(num_output_g); |
| 205 | weight_data_int8_scales_g.fill(weight_data_int8_scales[g]); |
| 206 | weights[2] = weight_data_int8_scales_g; |
| 207 | weights[3] = bottom_blob_int8_scales.range(g, 1); |
| 208 | } |
| 209 | if (int8_scale_term > 100) |
| 210 | { |
| 211 | weights[4] = top_blob_int8_scales.range(g, 1); |
| 212 | } |
| 213 | #endif |
| 214 | |
| 215 | op->load_model(ModelBinFromMatArray(weights)); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | ncnn::Mat weights[4]; |
| 220 | weights[0] = weight_data_g; |
| 221 | |
| 222 | #if NCNN_INT8 |
| 223 | if (int8_scale_term) |
| 224 | { |
| 225 | Mat weight_data_int8_scales_g(num_output_g); |
| 226 | weight_data_int8_scales_g.fill(weight_data_int8_scales[g]); |
| 227 | weights[1] = weight_data_int8_scales_g; |
| 228 | weights[2] = bottom_blob_int8_scales.range(g, 1); |
| 229 | } |
| 230 | if (int8_scale_term > 100) |
| 231 | { |
| 232 | weights[3] = top_blob_int8_scales.range(g, 1); |
| 233 | } |
| 234 | #endif |
| 235 | |
| 236 | op->load_model(ModelBinFromMatArray(weights)); |
| 237 | } |
| 238 | |
| 239 | op->create_pipeline(opt); |
| 240 | |
| 241 | group_ops[g] = op; |
| 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | int ConvolutionDepthWise_x86::destroy_pipeline(const Option& opt) |
| 248 | { |
| 249 | if (activation) |
| 250 | { |
| 251 | activation->destroy_pipeline(opt); |
| 252 | delete activation; |
| 253 | activation = 0; |
| 254 | } |
| 255 | |
| 256 | for (int i = 0; i < (int)group_ops.size(); i++) |
| 257 | { |
| 258 | group_ops[i]->destroy_pipeline(opt); |
| 259 | delete group_ops[i]; |
| 260 | } |
| 261 | group_ops.clear(); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | int ConvolutionDepthWise_x86::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 267 | { |
| 268 | #if NCNN_INT8 |
| 269 | if (opt.use_int8_inference && int8_scale_term) |
| 270 | { |
| 271 | return forward_int8_x86(bottom_blob, top_blob, opt); |
| 272 | } |
| 273 | #endif |
| 274 | |
| 275 | int w = bottom_blob.w; |
| 276 | int h = bottom_blob.h; |
| 277 | int channels = bottom_blob.c; |
| 278 | size_t elemsize = bottom_blob.elemsize; |
| 279 | int elempack = bottom_blob.elempack; |
| 280 | |
| 281 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 282 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 283 | |
| 284 | Mat bottom_blob_bordered; |
| 285 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 286 | if (bottom_blob_bordered.empty()) |
| 287 | return -100; |
| 288 | |
| 289 | w = bottom_blob_bordered.w; |
| 290 | h = bottom_blob_bordered.h; |
| 291 | |
| 292 | int outw = (w - kernel_extent_w) / stride_w + 1; |
| 293 | int outh = (h - kernel_extent_h) / stride_h + 1; |
| 294 | int out_elempack = 1; |
| 295 | #if __SSE2__ |
| 296 | if (opt.use_packing_layout) |
| 297 | { |
| 298 | #if __AVX512F__ |
| 299 | out_elempack = num_output % 16 == 0 ? 16 : num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 300 | #elif __AVX__ |
| 301 | out_elempack = num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 302 | #else |
| 303 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 304 | #endif |
| 305 | } |
| 306 | #endif // __SSE2__ |
| 307 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 308 | |
| 309 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 310 | if (top_blob.empty()) |
| 311 | return -100; |
| 312 | |
| 313 | // depth-wise |
| 314 | if (channels * elempack == group && group == num_output) |
| 315 | { |
| 316 | #if __SSE2__ |
| 317 | #if __AVX__ |
| 318 | #if __AVX512F__ |
| 319 | if (elempack == 16) |
| 320 | { |
| 321 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 322 | { |
| 323 | convdw3x3s1_pack16_avx512(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 324 | |
| 325 | if (activation) |
| 326 | { |
| 327 | activation->forward_inplace(top_blob, opt); |
| 328 | } |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 333 | { |
| 334 | convdw3x3s2_pack16_avx512(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 335 | |
| 336 | if (activation) |
| 337 | { |
| 338 | activation->forward_inplace(top_blob, opt); |
| 339 | } |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 344 | { |
| 345 | convdw5x5s1_pack16_avx512(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 346 | |
| 347 | if (activation) |
| 348 | { |
| 349 | activation->forward_inplace(top_blob, opt); |
| 350 | } |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 355 | { |
| 356 | convdw5x5s2_pack16_avx512(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 357 | |
| 358 | if (activation) |
| 359 | { |
| 360 | activation->forward_inplace(top_blob, opt); |
| 361 | } |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | const int maxk = kernel_w * kernel_h; |
| 368 | |
| 369 | // kernel offsets |
| 370 | std::vector<int> _space_ofs(maxk); |
| 371 | int* space_ofs = &_space_ofs[0]; |
| 372 | { |
| 373 | int p1 = 0; |
| 374 | int p2 = 0; |
| 375 | int gap = w * dilation_h - kernel_w * dilation_w; |
| 376 | for (int i = 0; i < kernel_h; i++) |
| 377 | { |
| 378 | for (int j = 0; j < kernel_w; j++) |
| 379 | { |
| 380 | space_ofs[p1] = p2; |
| 381 | p1++; |
| 382 | p2 += dilation_w; |
| 383 | } |
| 384 | p2 += gap; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | #pragma omp parallel for num_threads(opt.num_threads) |
| 389 | for (int g = 0; g < channels; g++) |
| 390 | { |
| 391 | float* outptr = top_blob.channel(g); |
| 392 | const float* kptr = (const float*)weight_data_tm + maxk * g * 16; |
| 393 | const Mat m = bottom_blob_bordered.channel(g); |
| 394 | |
| 395 | for (int i = 0; i < outh; i++) |
| 396 | { |
| 397 | for (int j = 0; j < outw; j++) |
| 398 | { |
| 399 | __m512 _sum = _mm512_set1_ps(0.f); |
| 400 | |
| 401 | if (bias_term) |
| 402 | { |
| 403 | _sum = _mm512_loadu_ps(((const float*)bias_data) + g * 16); |
| 404 | } |
| 405 | |
| 406 | const float* sptr = m.row(i * stride_h) + j * stride_w * 16; |
| 407 | |
| 408 | for (int k = 0; k < maxk; k++) |
| 409 | { |
| 410 | __m512 _val = _mm512_loadu_ps(sptr + space_ofs[k] * 16); |
| 411 | __m512 _w = _mm512_loadu_ps(kptr + k * 16); |
| 412 | _sum = _mm512_fmadd_ps(_val, _w, _sum); |
| 413 | } |
| 414 | |
| 415 | _mm512_storeu_ps(outptr, _sum); |
| 416 | outptr += 16; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if (activation) |
| 422 | { |
| 423 | activation->forward_inplace(top_blob, opt); |
| 424 | } |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | } |
| 429 | #endif // __AVX512F__ |
| 430 | if (elempack == 8) |
| 431 | { |
| 432 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 433 | { |
| 434 | convdw3x3s1_pack8_avx(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 435 | |
| 436 | if (activation) |
| 437 | { |
| 438 | activation->forward_inplace(top_blob, opt); |
| 439 | } |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 444 | { |
| 445 | convdw3x3s2_pack8_avx(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 446 | |
| 447 | if (activation) |
| 448 | { |
| 449 | activation->forward_inplace(top_blob, opt); |
| 450 | } |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 455 | { |
| 456 | convdw5x5s1_pack8_avx(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 457 | |
| 458 | if (activation) |
| 459 | { |
| 460 | activation->forward_inplace(top_blob, opt); |
| 461 | } |
| 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 466 | { |
| 467 | convdw5x5s2_pack8_avx(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 468 | |
| 469 | if (activation) |
| 470 | { |
| 471 | activation->forward_inplace(top_blob, opt); |
| 472 | } |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | else |
| 477 | { |
| 478 | const int maxk = kernel_w * kernel_h; |
| 479 | |
| 480 | // kernel offsets |
| 481 | std::vector<int> _space_ofs(maxk); |
| 482 | int* space_ofs = &_space_ofs[0]; |
| 483 | { |
| 484 | int p1 = 0; |
| 485 | int p2 = 0; |
| 486 | int gap = w * dilation_h - kernel_w * dilation_w; |
| 487 | for (int i = 0; i < kernel_h; i++) |
| 488 | { |
| 489 | for (int j = 0; j < kernel_w; j++) |
| 490 | { |
| 491 | space_ofs[p1] = p2; |
| 492 | p1++; |
| 493 | p2 += dilation_w; |
| 494 | } |
| 495 | p2 += gap; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | #pragma omp parallel for num_threads(opt.num_threads) |
| 500 | for (int g = 0; g < channels; g++) |
| 501 | { |
| 502 | float* outptr = top_blob.channel(g); |
| 503 | const float* kptr = (const float*)weight_data_tm + maxk * g * 8; |
| 504 | const Mat m = bottom_blob_bordered.channel(g); |
| 505 | |
| 506 | for (int i = 0; i < outh; i++) |
| 507 | { |
| 508 | for (int j = 0; j < outw; j++) |
| 509 | { |
| 510 | __m256 _sum = _mm256_set1_ps(0.f); |
| 511 | |
| 512 | if (bias_term) |
| 513 | { |
| 514 | _sum = _mm256_loadu_ps(((const float*)bias_data) + g * 8); |
| 515 | } |
| 516 | |
| 517 | const float* sptr = m.row(i * stride_h) + j * stride_w * 8; |
| 518 | |
| 519 | for (int k = 0; k < maxk; k++) |
| 520 | { |
| 521 | __m256 _val = _mm256_loadu_ps(sptr + space_ofs[k] * 8); |
| 522 | __m256 _w = _mm256_loadu_ps(kptr + k * 8); |
| 523 | _sum = _mm256_comp_fmadd_ps(_val, _w, _sum); |
| 524 | } |
| 525 | |
| 526 | _mm256_storeu_ps(outptr + j * 8, _sum); |
| 527 | } |
| 528 | |
| 529 | outptr += outw * 8; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | if (activation) |
| 534 | { |
| 535 | activation->forward_inplace(top_blob, opt); |
| 536 | } |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | } |
| 541 | #endif // __AVX__ |
| 542 | |
| 543 | if (elempack == 4) |
| 544 | { |
| 545 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 546 | { |
| 547 | convdw3x3s1_pack4_sse(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 548 | |
| 549 | if (activation) |
| 550 | { |
| 551 | activation->forward_inplace(top_blob, opt); |
| 552 | } |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 557 | { |
| 558 | convdw3x3s2_pack4_sse(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 559 | |
| 560 | if (activation) |
| 561 | { |
| 562 | activation->forward_inplace(top_blob, opt); |
| 563 | } |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 568 | { |
| 569 | convdw5x5s1_pack4_sse(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 570 | |
| 571 | if (activation) |
| 572 | { |
| 573 | activation->forward_inplace(top_blob, opt); |
| 574 | } |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 579 | { |
| 580 | convdw5x5s2_pack4_sse(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 581 | |
| 582 | if (activation) |
| 583 | { |
| 584 | activation->forward_inplace(top_blob, opt); |
| 585 | } |
| 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | { |
| 590 | const int maxk = kernel_w * kernel_h; |
| 591 | |
| 592 | // kernel offsets |
| 593 | std::vector<int> _space_ofs(maxk); |
| 594 | int* space_ofs = &_space_ofs[0]; |
| 595 | { |
| 596 | int p1 = 0; |
| 597 | int p2 = 0; |
| 598 | int gap = w * dilation_h - kernel_w * dilation_w; |
| 599 | for (int i = 0; i < kernel_h; i++) |
| 600 | { |
| 601 | for (int j = 0; j < kernel_w; j++) |
| 602 | { |
| 603 | space_ofs[p1] = p2; |
| 604 | p1++; |
| 605 | p2 += dilation_w; |
| 606 | } |
| 607 | p2 += gap; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | #pragma omp parallel for num_threads(opt.num_threads) |
| 612 | for (int g = 0; g < channels; g++) |
| 613 | { |
| 614 | float* outptr = top_blob.channel(g); |
| 615 | const float* kptr = (const float*)weight_data_tm + maxk * g * 4; |
| 616 | const Mat m = bottom_blob_bordered.channel(g); |
| 617 | |
| 618 | for (int i = 0; i < outh; i++) |
| 619 | { |
| 620 | for (int j = 0; j < outw; j++) |
| 621 | { |
| 622 | __m128 _sum = _mm_set1_ps(0.f); |
| 623 | |
| 624 | if (bias_term) |
| 625 | { |
| 626 | _sum = _mm_loadu_ps(((const float*)bias_data) + g * 4); |
| 627 | } |
| 628 | |
| 629 | const float* sptr = m.row(i * stride_h) + j * stride_w * 4; |
| 630 | |
| 631 | for (int k = 0; k < maxk; k++) |
| 632 | { |
| 633 | __m128 _val = _mm_loadu_ps(sptr + space_ofs[k] * 4); |
| 634 | __m128 _w = _mm_loadu_ps(kptr + k * 4); |
| 635 | _sum = _mm_add_ps(_mm_mul_ps(_val, _w), _sum); |
| 636 | } |
| 637 | |
| 638 | _sum = activation_sse(_sum, activation_type, activation_params); |
| 639 | |
| 640 | _mm_storeu_ps(outptr + j * 4, _sum); |
| 641 | } |
| 642 | |
| 643 | outptr += outw * 4; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | } |
| 650 | #endif // __SSE2__ |
| 651 | |
| 652 | if (elempack == 1) |
| 653 | { |
| 654 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) |
| 655 | { |
| 656 | convdw3x3s1_sse(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 657 | |
| 658 | if (activation) |
| 659 | { |
| 660 | activation->forward_inplace(top_blob, opt); |
| 661 | } |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) |
| 666 | { |
| 667 | convdw3x3s2_sse(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt); |
| 668 | |
| 669 | if (activation) |
| 670 | { |
| 671 | activation->forward_inplace(top_blob, opt); |
| 672 | } |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | // group convolution |
| 680 | const int channels_g = channels * elempack / group; |
| 681 | const int num_output_g = num_output / group; |
| 682 | |
| 683 | int g_elempack = 1; |
| 684 | int out_g_elempack = 1; |
| 685 | #if __SSE2__ |
| 686 | if (opt.use_packing_layout) |
| 687 | { |
| 688 | #if __AVX512F__ |
| 689 | g_elempack = channels_g % 16 == 0 ? 16 : channels_g % 8 == 0 ? 8 : channels_g % 4 == 0 ? 4 : 1; |
| 690 | out_g_elempack = num_output_g % 16 == 0 ? 16 : num_output_g % 8 == 0 ? 8 : num_output_g % 4 == 0 ? 4 : 1; |
| 691 | #elif __AVX__ |
| 692 | g_elempack = channels_g % 8 == 0 ? 8 : channels_g % 4 == 0 ? 4 : 1; |
| 693 | out_g_elempack = num_output_g % 8 == 0 ? 8 : num_output_g % 4 == 0 ? 4 : 1; |
| 694 | #else |
| 695 | g_elempack = channels_g % 4 == 0 ? 4 : 1; |
| 696 | out_g_elempack = num_output_g % 4 == 0 ? 4 : 1; |
| 697 | #endif |
| 698 | } |
| 699 | #endif // __SSE2__ |
| 700 | |
| 701 | // unpacking |
| 702 | Mat bottom_blob_bordered_unpacked = bottom_blob_bordered; |
| 703 | if (elempack > g_elempack) |
| 704 | { |
| 705 | Option opt_p = opt; |
| 706 | opt_p.blob_allocator = opt.workspace_allocator; |
| 707 | convert_packing(bottom_blob_bordered, bottom_blob_bordered_unpacked, g_elempack, opt_p); |
| 708 | if (bottom_blob_bordered_unpacked.empty()) |
| 709 | return -100; |
| 710 | } |
| 711 | |
| 712 | Mat top_blob_unpacked = top_blob; |
| 713 | if (out_g_elempack < out_elempack) |
| 714 | { |
| 715 | top_blob_unpacked.create(outw, outh, num_output / out_g_elempack, out_elemsize / out_elempack * out_g_elempack, out_g_elempack, opt.workspace_allocator); |
| 716 | if (top_blob_unpacked.empty()) |
| 717 | return -100; |
| 718 | } |
| 719 | |
| 720 | for (int g = 0; g < group; g++) |
| 721 | { |
| 722 | const Mat bottom_blob_bordered_g = bottom_blob_bordered_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack); |
| 723 | Mat top_blob_g = top_blob_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack); |
| 724 | |
| 725 | const ncnn::Layer* op = group_ops[g]; |
| 726 | |
| 727 | Option opt_g = opt; |
| 728 | opt_g.blob_allocator = top_blob_unpacked.allocator; |
| 729 | |
| 730 | // forward |
| 731 | int ret = op->forward(bottom_blob_bordered_g, top_blob_g, opt_g); |
| 732 | if (ret != 0) |
| 733 | return ret; |
| 734 | } |
| 735 | |
| 736 | // packing |
| 737 | if (out_g_elempack < out_elempack) |
| 738 | { |
| 739 | convert_packing(top_blob_unpacked, top_blob, out_elempack, opt); |
| 740 | if (top_blob.empty()) |
| 741 | return -100; |
| 742 | } |
| 743 | else |
| 744 | { |
| 745 | top_blob = top_blob_unpacked; |
| 746 | } |
| 747 | |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | int ConvolutionDepthWise_x86::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 752 | { |
| 753 | const Mat& bottom_blob = bottom_blobs[0]; |
| 754 | const Mat& _weight_data = bottom_blobs[1]; |
| 755 | Mat& top_blob = top_blobs[0]; |
| 756 | |
| 757 | const int _kernel_w = _weight_data.w; |
| 758 | const int _kernel_h = _weight_data.h; |
| 759 | const int _num_output = _weight_data.c * _weight_data.elempack; |
| 760 | |
| 761 | Mat weight_data_flattened; |
| 762 | flatten(_weight_data, weight_data_flattened, opt); |
| 763 | if (weight_data_flattened.empty()) |
| 764 | return -100; |
| 765 | |
| 766 | // weight_data_flattened as pack1 |
| 767 | weight_data_flattened.w *= weight_data_flattened.elempack; |
| 768 | weight_data_flattened.elemsize /= weight_data_flattened.elempack; |
| 769 | weight_data_flattened.elempack = 1; |
| 770 | |
| 771 | Mat bias_data_flattened; |
| 772 | if (bias_term) |
| 773 | { |
| 774 | const Mat& _bias_data = bottom_blobs[2]; |
| 775 | flatten(_bias_data, bias_data_flattened, opt); |
| 776 | if (bias_data_flattened.empty()) |
| 777 | return -100; |
| 778 | |
| 779 | // bias_data_flattened as pack1 |
| 780 | bias_data_flattened.w *= bias_data_flattened.elempack; |
| 781 | bias_data_flattened.elemsize /= bias_data_flattened.elempack; |
| 782 | bias_data_flattened.elempack = 1; |
| 783 | } |
| 784 | |
| 785 | ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::ConvolutionDepthWise); |
| 786 | |
| 787 | ncnn::ParamDict pd; |
| 788 | pd.set(0, _num_output); |
| 789 | pd.set(1, _kernel_w); |
| 790 | pd.set(11, _kernel_h); |
| 791 | pd.set(2, dilation_w); |
| 792 | pd.set(12, dilation_h); |
| 793 | pd.set(3, stride_w); |
| 794 | pd.set(13, stride_h); |
| 795 | pd.set(4, pad_left); |
| 796 | pd.set(15, pad_right); |
| 797 | pd.set(14, pad_top); |
| 798 | pd.set(16, pad_bottom); |
| 799 | pd.set(18, pad_value); |
| 800 | pd.set(5, bias_term); |
| 801 | pd.set(6, weight_data_flattened.w); |
| 802 | pd.set(7, group); |
| 803 | pd.set(8, int8_scale_term); |
| 804 | pd.set(9, activation_type); |
| 805 | pd.set(10, activation_params); |
| 806 | |
| 807 | op->load_param(pd); |
| 808 | |
| 809 | ncnn::Mat weights[2]; |
| 810 | weights[0] = weight_data_flattened; |
| 811 | weights[1] = bias_data_flattened; |
| 812 | |
| 813 | op->load_model(ncnn::ModelBinFromMatArray(weights)); |
| 814 | |
| 815 | op->create_pipeline(opt); |
| 816 | |
| 817 | op->forward(bottom_blob, top_blob, opt); |
| 818 | |
| 819 | op->destroy_pipeline(opt); |
| 820 | |
| 821 | delete op; |
| 822 | |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | #if NCNN_INT8 |
| 827 | int ConvolutionDepthWise_x86::create_pipeline_int8_x86(const Option& opt) |
| 828 | { |
| 829 | const int maxk = kernel_w * kernel_h; |
| 830 | int channels = (weight_data_size / group) / maxk / (num_output / group) * group; |
| 831 | |
| 832 | // depth-wise |
| 833 | if (channels == group && group == num_output) |
| 834 | { |
| 835 | int elempack = 1; |
| 836 | #if __SSE2__ |
| 837 | if (opt.use_packing_layout) |
| 838 | { |
| 839 | elempack = channels % 8 == 0 ? 8 : 1; |
| 840 | } |
| 841 | #endif // __SSE2__ |
| 842 | |
| 843 | if (elempack == 8) |
| 844 | { |
| 845 | Mat weight_data_r2 = weight_data.reshape(maxk, group); |
| 846 | convert_packing(weight_data_r2, weight_data_tm, 8, opt); |
| 847 | } |
| 848 | |
| 849 | if (elempack == 1) |
| 850 | { |
| 851 | weight_data_tm = weight_data; |
| 852 | } |
| 853 | |
| 854 | if (opt.lightmode) |
| 855 | weight_data.release(); |
| 856 | |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | // group convolution |
| 861 | create_group_ops(opt); |
| 862 | |
| 863 | if (opt.lightmode) |
| 864 | weight_data.release(); |
| 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | int ConvolutionDepthWise_x86::forward_int8_x86(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 870 | { |
| 871 | int w = bottom_blob.w; |
| 872 | int h = bottom_blob.h; |
| 873 | int channels = bottom_blob.c; |
| 874 | int elempack = bottom_blob.elempack; |
| 875 | |
| 876 | int elembits = bottom_blob.elembits(); |
| 877 | |
| 878 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 879 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 880 | |
| 881 | Mat bottom_blob_int8 = bottom_blob; |
| 882 | if (elembits != 8) |
| 883 | { |
| 884 | const int channels_g = channels * elempack / group; |
| 885 | |
| 886 | Mat scales(channels * elempack); |
| 887 | { |
| 888 | float* ps = scales; |
| 889 | for (int g = 0; g < group; g++) |
| 890 | { |
| 891 | float scale = bottom_blob_int8_scales[g]; |
| 892 | for (int q = 0; q < channels_g; q++) |
| 893 | { |
| 894 | *ps++ = scale; |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | Option opt_q = opt; |
| 900 | opt_q.blob_allocator = opt.workspace_allocator; |
| 901 | quantize_to_int8(bottom_blob, bottom_blob_int8, scales, opt_q); |
| 902 | if (bottom_blob_int8.empty()) |
| 903 | return -100; |
| 904 | } |
| 905 | |
| 906 | Mat bottom_blob_bordered; |
| 907 | make_padding(bottom_blob_int8, bottom_blob_bordered, opt); |
| 908 | if (bottom_blob_bordered.empty()) |
| 909 | return -100; |
| 910 | |
| 911 | w = bottom_blob_bordered.w; |
| 912 | h = bottom_blob_bordered.h; |
| 913 | channels = bottom_blob_bordered.c; |
| 914 | elempack = bottom_blob_bordered.elempack; |
| 915 | |
| 916 | int outw = (w - kernel_extent_w) / stride_w + 1; |
| 917 | int outh = (h - kernel_extent_h) / stride_h + 1; |
| 918 | |
| 919 | // depth-wise |
| 920 | if (channels * elempack == group && group == num_output) |
| 921 | { |
| 922 | int out_elempack = 1; |
| 923 | #if __SSE2__ |
| 924 | if (opt.use_packing_layout) |
| 925 | { |
| 926 | out_elempack = num_output % 8 == 0 ? 8 : 1; |
| 927 | } |
| 928 | #endif // __SSE2__ |
| 929 | bool use_int8_requantize = int8_scale_term > 100; |
| 930 | size_t out_elemsize = use_int8_requantize ? 1u * out_elempack : 4u * out_elempack; |
| 931 | |
| 932 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 933 | if (top_blob.empty()) |
| 934 | return -100; |
| 935 | |
| 936 | #if __SSE2__ |
| 937 | if (elempack == 8) |
| 938 | { |
| 939 | { |
| 940 | const int maxk = kernel_w * kernel_h; |
| 941 | |
| 942 | // kernel offsets |
| 943 | std::vector<int> _space_ofs(maxk); |
| 944 | int* space_ofs = &_space_ofs[0]; |
| 945 | { |
| 946 | int p1 = 0; |
| 947 | int p2 = 0; |
| 948 | int gap = w * dilation_h - kernel_w * dilation_w; |
| 949 | for (int i = 0; i < kernel_h; i++) |
| 950 | { |
| 951 | for (int j = 0; j < kernel_w; j++) |
| 952 | { |
| 953 | space_ofs[p1] = p2; |
| 954 | p1++; |
| 955 | p2 += dilation_w; |
| 956 | } |
| 957 | p2 += gap; |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | #pragma omp parallel for num_threads(opt.num_threads) |
| 962 | for (int g = 0; g < channels; g++) |
| 963 | { |
| 964 | signed char* outptr_s8 = top_blob.channel(g); |
| 965 | float* outptr_f32 = top_blob.channel(g); |
| 966 | const signed char* kptr = (const signed char*)weight_data_tm + maxk * g * 8; |
| 967 | const Mat m = bottom_blob_bordered.channel(g); |
| 968 | |
| 969 | for (int i = 0; i < outh; i++) |
| 970 | { |
| 971 | for (int j = 0; j < outw; j++) |
| 972 | { |
| 973 | __m128i _sum0 = _mm_setzero_si128(); |
| 974 | __m128i _sum1 = _mm_setzero_si128(); |
| 975 | |
| 976 | const signed char* sptr = m.row<const signed char>(i * stride_h) + j * stride_w * 8; |
| 977 | |
| 978 | for (int k = 0; k < maxk; k++) |
| 979 | { |
| 980 | // TODO use _mm_cvtepi8_epi16 on sse4.1 |
| 981 | __m128i _val = _mm_loadl_epi64((const __m128i*)(sptr + space_ofs[k] * 8)); |
| 982 | _val = _mm_unpacklo_epi8(_val, _mm_cmpgt_epi8(_mm_setzero_si128(), _val)); |
| 983 | |
| 984 | __m128i _w = _mm_loadl_epi64((const __m128i*)(kptr + k * 8)); |
| 985 | _w = _mm_unpacklo_epi8(_w, _mm_cmpgt_epi8(_mm_setzero_si128(), _w)); |
| 986 | |
| 987 | __m128i _sl = _mm_mullo_epi16(_val, _w); |
| 988 | __m128i _sh = _mm_mulhi_epi16(_val, _w); |
| 989 | __m128i _s0 = _mm_unpacklo_epi16(_sl, _sh); |
| 990 | __m128i _s1 = _mm_unpackhi_epi16(_sl, _sh); |
| 991 | |
| 992 | _sum0 = _mm_add_epi32(_sum0, _s0); |
| 993 | _sum1 = _mm_add_epi32(_sum1, _s1); |
| 994 | } |
| 995 | |
| 996 | __m128 _scale_in0; |
| 997 | __m128 _scale_in1; |
| 998 | { |
| 999 | __m128 _bottom_blob_int8_scales0 = _mm_loadu_ps((const float*)bottom_blob_int8_scales + g * 8); |
| 1000 | __m128 _bottom_blob_int8_scales1 = _mm_loadu_ps((const float*)bottom_blob_int8_scales + g * 8 + 4); |
| 1001 | __m128 _weight_data_int8_scales0 = _mm_loadu_ps((const float*)weight_data_int8_scales + g * 8); |
| 1002 | __m128 _weight_data_int8_scales1 = _mm_loadu_ps((const float*)weight_data_int8_scales + g * 8 + 4); |
| 1003 | _scale_in0 = _mm_rcp_ps(_mm_mul_ps(_bottom_blob_int8_scales0, _weight_data_int8_scales0)); |
| 1004 | _scale_in1 = _mm_rcp_ps(_mm_mul_ps(_bottom_blob_int8_scales1, _weight_data_int8_scales1)); |
| 1005 | |
| 1006 | __m128 _m0 = _mm_cmpneq_ps(_weight_data_int8_scales0, _mm_setzero_ps()); |
| 1007 | __m128 _m1 = _mm_cmpneq_ps(_weight_data_int8_scales1, _mm_setzero_ps()); |
| 1008 | _scale_in0 = _mm_and_ps(_scale_in0, _m0); |
| 1009 | _scale_in1 = _mm_and_ps(_scale_in1, _m1); |
| 1010 | } |
| 1011 | |
| 1012 | __m128 _sumfp32_0 = _mm_mul_ps(_mm_cvtepi32_ps(_sum0), _scale_in0); |
| 1013 | __m128 _sumfp32_1 = _mm_mul_ps(_mm_cvtepi32_ps(_sum1), _scale_in1); |
| 1014 | |
| 1015 | if (bias_term) |
| 1016 | { |
| 1017 | __m128 _bias0 = _mm_loadu_ps((const float*)bias_data + g * 8); |
| 1018 | __m128 _bias1 = _mm_loadu_ps((const float*)bias_data + g * 8 + 4); |
| 1019 | _sumfp32_0 = _mm_add_ps(_sumfp32_0, _bias0); |
| 1020 | _sumfp32_1 = _mm_add_ps(_sumfp32_1, _bias1); |
| 1021 | } |
| 1022 | |
| 1023 | _sumfp32_0 = activation_sse(_sumfp32_0, activation_type, activation_params); |
| 1024 | _sumfp32_1 = activation_sse(_sumfp32_1, activation_type, activation_params); |
| 1025 | |
| 1026 | if (use_int8_requantize) |
| 1027 | { |
| 1028 | // requantize and relu |
| 1029 | __m128 _scale_out0 = _mm_loadu_ps((const float*)top_blob_int8_scales + g * 8); |
| 1030 | __m128 _scale_out1 = _mm_loadu_ps((const float*)top_blob_int8_scales + g * 8 + 4); |
| 1031 | _sumfp32_0 = _mm_mul_ps(_sumfp32_0, _scale_out0); |
| 1032 | _sumfp32_1 = _mm_mul_ps(_sumfp32_1, _scale_out1); |
| 1033 | int64_t _sum8 = float2int8_sse(_sumfp32_0, _sumfp32_1); |
| 1034 | |
| 1035 | *(int64_t*)outptr_s8 = _sum8; |
| 1036 | outptr_s8 += 8; |
| 1037 | } |
| 1038 | else |
| 1039 | { |
| 1040 | // dequantize and relu |
| 1041 | _mm_storeu_ps(outptr_f32, _sumfp32_0); |
| 1042 | _mm_storeu_ps(outptr_f32 + 4, _sumfp32_1); |
| 1043 | outptr_f32 += 8; |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | #endif // __SSE2__ |
| 1051 | |
| 1052 | if (elempack == 1) |
| 1053 | { |
| 1054 | if (kernel_w == 3 && kernel_h == 3 && stride_w == 1 && stride_h == 1 && dilation_w == 1 && dilation_h == 1 && (activation_type == 0 || activation_type == 1)) |
| 1055 | { |
| 1056 | if (use_int8_requantize) |
| 1057 | { |
| 1058 | std::vector<float> requantize_scales; |
| 1059 | for (int g = 0; g < group; g++) |
| 1060 | { |
| 1061 | float scale_in; |
| 1062 | if (weight_data_int8_scales[g] == 0) |
| 1063 | scale_in = 0; |
| 1064 | else |
| 1065 | scale_in = 1.f / (bottom_blob_int8_scales[g] * weight_data_int8_scales[g]); |
| 1066 | |
| 1067 | float scale_out = top_blob_int8_scales[g]; |
| 1068 | |
| 1069 | requantize_scales.push_back(scale_in); |
| 1070 | requantize_scales.push_back(scale_out); |
| 1071 | } |
| 1072 | |
| 1073 | convdw3x3s1_int8_requant_sse(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, requantize_scales, opt); |
| 1074 | } |
| 1075 | else |
| 1076 | { |
| 1077 | std::vector<float> dequantize_scales; |
| 1078 | for (int g = 0; g < group; g++) |
| 1079 | { |
| 1080 | float top_rescale = 1.f / (bottom_blob_int8_scales[g] * weight_data_int8_scales[g]); |
| 1081 | |
| 1082 | dequantize_scales.push_back(top_rescale); |
| 1083 | } |
| 1084 | |
| 1085 | convdw3x3s1_int8_dequant_sse(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, dequantize_scales, opt); |
| 1086 | } |
| 1087 | |
| 1088 | if (activation) |
| 1089 | { |
| 1090 | activation->forward_inplace(top_blob, opt); |
| 1091 | } |
| 1092 | } |
| 1093 | else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (activation_type == 0 || activation_type == 1)) |
| 1094 | { |
| 1095 | if (use_int8_requantize) |
| 1096 | { |
| 1097 | std::vector<float> requantize_scales; |
| 1098 | for (int g = 0; g < group; g++) |
| 1099 | { |
| 1100 | float scale_in; |
| 1101 | if (weight_data_int8_scales[g] == 0) |
| 1102 | scale_in = 0; |
| 1103 | else |
| 1104 | scale_in = 1.f / (bottom_blob_int8_scales[g] * weight_data_int8_scales[g]); |
| 1105 | |
| 1106 | float scale_out = top_blob_int8_scales[g]; |
| 1107 | |
| 1108 | requantize_scales.push_back(scale_in); |
| 1109 | requantize_scales.push_back(scale_out); |
| 1110 | } |
| 1111 | |
| 1112 | convdw3x3s2_int8_requant_sse(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, requantize_scales, opt); |
| 1113 | } |
| 1114 | else |
| 1115 | { |
| 1116 | std::vector<float> dequantize_scales; |
| 1117 | for (int g = 0; g < group; g++) |
| 1118 | { |
| 1119 | float top_rescale = 1.f / (bottom_blob_int8_scales[g] * weight_data_int8_scales[g]); |
| 1120 | |
| 1121 | dequantize_scales.push_back(top_rescale); |
| 1122 | } |
| 1123 | |
| 1124 | convdw3x3s2_int8_dequant_sse(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, dequantize_scales, opt); |
| 1125 | } |
| 1126 | |
| 1127 | if (activation) |
| 1128 | { |
| 1129 | activation->forward_inplace(top_blob, opt); |
| 1130 | } |
| 1131 | } |
| 1132 | else |
| 1133 | { |
| 1134 | const int maxk = kernel_w * kernel_h; |
| 1135 | |
| 1136 | // kernel offsets |
| 1137 | std::vector<int> _space_ofs(maxk); |
| 1138 | int* space_ofs = &_space_ofs[0]; |
| 1139 | { |
| 1140 | int p1 = 0; |
| 1141 | int p2 = 0; |
| 1142 | int gap = w * dilation_h - kernel_w * dilation_w; |
| 1143 | for (int i = 0; i < kernel_h; i++) |
| 1144 | { |
| 1145 | for (int j = 0; j < kernel_w; j++) |
| 1146 | { |
| 1147 | space_ofs[p1] = p2; |
| 1148 | p1++; |
| 1149 | p2 += dilation_w; |
| 1150 | } |
| 1151 | p2 += gap; |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1156 | for (int g = 0; g < group; g++) |
| 1157 | { |
| 1158 | signed char* outptr_s8 = top_blob.channel(g); |
| 1159 | float* outptr_f32 = top_blob.channel(g); |
| 1160 | const signed char* kptr = (const signed char*)weight_data_tm + maxk * g; |
| 1161 | const Mat m = bottom_blob_bordered.channel(g); |
| 1162 | |
| 1163 | for (int i = 0; i < outh; i++) |
| 1164 | { |
| 1165 | for (int j = 0; j < outw; j++) |
| 1166 | { |
| 1167 | int sum = 0; |
| 1168 | |
| 1169 | const signed char* sptr = m.row<const signed char>(i * stride_h) + j * stride_w; |
| 1170 | |
| 1171 | for (int k = 0; k < maxk; k++) |
| 1172 | { |
| 1173 | signed char val = sptr[space_ofs[k]]; |
| 1174 | signed char w = kptr[k]; |
| 1175 | sum += val * w; |
| 1176 | } |
| 1177 | |
| 1178 | float scale_in; |
| 1179 | if (weight_data_int8_scales[g] == 0) |
| 1180 | scale_in = 0; |
| 1181 | else |
| 1182 | scale_in = 1.f / (bottom_blob_int8_scales[g] * weight_data_int8_scales[g]); |
| 1183 | |
| 1184 | float sumfp32 = sum * scale_in; |
| 1185 | |
| 1186 | if (bias_term) |
| 1187 | sumfp32 += bias_data[g]; |
| 1188 | |
| 1189 | sumfp32 = activation_ss(sumfp32, activation_type, activation_params); |
| 1190 | |
| 1191 | if (use_int8_requantize) |
| 1192 | { |
| 1193 | // requantize |
| 1194 | float scale_out = top_blob_int8_scales[g]; |
| 1195 | signed char sums8 = float2int8(sumfp32 * scale_out); |
| 1196 | outptr_s8[0] = sums8; |
| 1197 | outptr_s8 += 1; |
| 1198 | } |
| 1199 | else |
| 1200 | { |
| 1201 | // dequantize |
| 1202 | outptr_f32[0] = sumfp32; |
| 1203 | outptr_f32 += 1; |
| 1204 | } |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
| 1214 | bool use_int8_requantize = int8_scale_term > 100; |
| 1215 | int out_elempack = 1; |
| 1216 | #if __SSE2__ |
| 1217 | if (opt.use_packing_layout) |
| 1218 | { |
| 1219 | if (use_int8_requantize) |
| 1220 | out_elempack = num_output % 8 == 0 ? 8 : 1; |
| 1221 | else |
| 1222 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 1223 | } |
| 1224 | #endif // __SSE2__ |
| 1225 | size_t out_elemsize = use_int8_requantize ? 1u * out_elempack : 4u * out_elempack; |
| 1226 | |
| 1227 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 1228 | if (top_blob.empty()) |
| 1229 | return -100; |
| 1230 | |
| 1231 | // group convolution |
| 1232 | const int channels_g = channels * elempack / group; |
| 1233 | const int num_output_g = num_output / group; |
| 1234 | |
| 1235 | int g_elempack = 1; |
| 1236 | int out_g_elempack = 1; |
| 1237 | #if __SSE2__ |
| 1238 | if (opt.use_packing_layout) |
| 1239 | { |
| 1240 | g_elempack = channels_g % 8 == 0 ? 8 : 1; |
| 1241 | if (use_int8_requantize) |
| 1242 | out_g_elempack = num_output_g % 8 == 0 ? 8 : 1; |
| 1243 | else |
| 1244 | out_g_elempack = num_output_g % 4 == 0 ? 4 : 1; |
| 1245 | } |
| 1246 | #endif // __SSE2__ |
| 1247 | |
| 1248 | // unpacking |
| 1249 | Mat bottom_blob_bordered_unpacked = bottom_blob_bordered; |
| 1250 | if (elempack > g_elempack) |
| 1251 | { |
| 1252 | Option opt_p = opt; |
| 1253 | opt_p.blob_allocator = opt.workspace_allocator; |
| 1254 | convert_packing(bottom_blob_bordered, bottom_blob_bordered_unpacked, g_elempack, opt_p); |
| 1255 | if (bottom_blob_bordered_unpacked.empty()) |
| 1256 | return -100; |
| 1257 | } |
| 1258 | |
| 1259 | Mat top_blob_unpacked = top_blob; |
| 1260 | if (out_g_elempack < out_elempack) |
| 1261 | { |
| 1262 | top_blob_unpacked.create(outw, outh, num_output / out_g_elempack, out_elemsize / out_elempack * out_g_elempack, out_g_elempack, opt.workspace_allocator); |
| 1263 | if (top_blob_unpacked.empty()) |
| 1264 | return -100; |
| 1265 | } |
| 1266 | |
| 1267 | for (int g = 0; g < group; g++) |
| 1268 | { |
| 1269 | const Mat bottom_blob_bordered_g = bottom_blob_bordered_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack); |
| 1270 | Mat top_blob_g = top_blob_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack); |
| 1271 | |
| 1272 | const ncnn::Layer* op = group_ops[g]; |
| 1273 | |
| 1274 | Option opt_g = opt; |
| 1275 | opt_g.blob_allocator = top_blob_unpacked.allocator; |
| 1276 | |
| 1277 | // forward |
| 1278 | int ret = op->forward(bottom_blob_bordered_g, top_blob_g, opt_g); |
| 1279 | if (ret != 0) |
| 1280 | return ret; |
| 1281 | } |
| 1282 | |
| 1283 | // packing |
| 1284 | if (out_g_elempack < out_elempack) |
| 1285 | { |
| 1286 | convert_packing(top_blob_unpacked, top_blob, out_elempack, opt); |
| 1287 | if (top_blob.empty()) |
| 1288 | return -100; |
| 1289 | } |
| 1290 | else |
| 1291 | { |
| 1292 | top_blob = top_blob_unpacked; |
| 1293 | } |
| 1294 | |
| 1295 | return 0; |
| 1296 | } |
| 1297 | #endif // NCNN_INT8 |
| 1298 | |
| 1299 | } // namespace ncnn |