| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 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 "deconvolution_x86.h" |
| 16 | |
| 17 | #include "layer_type.h" |
| 18 | |
| 19 | #if __SSE2__ |
| 20 | #include <emmintrin.h> |
| 21 | #if __AVX__ |
| 22 | #include <immintrin.h> |
| 23 | #endif |
| 24 | #endif // __SSE2__ |
| 25 | |
| 26 | #include "x86_activation.h" |
| 27 | #include "x86_usability.h" |
| 28 | |
| 29 | namespace ncnn { |
| 30 | |
| 31 | #if __SSE2__ |
| 32 | #include "deconvolution_pack4.h" |
| 33 | #include "deconvolution_pack1to4.h" |
| 34 | #include "deconvolution_pack4to1.h" |
| 35 | |
| 36 | #if __AVX__ |
| 37 | #include "deconvolution_pack8.h" |
| 38 | #include "deconvolution_pack4to8.h" |
| 39 | #include "deconvolution_pack8to4.h" |
| 40 | #include "deconvolution_pack1to8.h" |
| 41 | #include "deconvolution_pack8to1.h" |
| 42 | |
| 43 | #if __AVX512F__ |
| 44 | #include "deconvolution_pack16.h" |
| 45 | #include "deconvolution_pack8to16.h" |
| 46 | #include "deconvolution_pack16to8.h" |
| 47 | #include "deconvolution_pack4to16.h" |
| 48 | #include "deconvolution_pack16to4.h" |
| 49 | #include "deconvolution_pack1to16.h" |
| 50 | #include "deconvolution_pack16to1.h" |
| 51 | #endif // __AVX512F__ |
| 52 | #endif // __AVX__ |
| 53 | #endif // __SSE2__ |
| 54 | |
| 55 | Deconvolution_x86::Deconvolution_x86() |
| 56 | { |
| 57 | #if __SSE2__ |
| 58 | support_packing = true; |
| 59 | #endif // __SSE2__ |
| 60 | |
| 61 | activation = 0; |
| 62 | gemm = 0; |
| 63 | } |
| 64 | |
| 65 | int Deconvolution_x86::create_pipeline(const Option& opt) |
| 66 | { |
| 67 | if (dynamic_weight) |
| 68 | return 0; |
| 69 | |
| 70 | activation = create_activation_layer(activation_type, activation_params, opt); |
| 71 | |
| 72 | const int maxk = kernel_w * kernel_h; |
| 73 | int num_input = weight_data_size / maxk / num_output; |
| 74 | |
| 75 | int elempack = 1; |
| 76 | int out_elempack = 1; |
| 77 | #if __SSE2__ |
| 78 | if (opt.use_packing_layout) |
| 79 | { |
| 80 | #if __AVX512F__ |
| 81 | elempack = num_input % 16 == 0 ? 16 : num_input % 8 == 0 ? 8 : num_input % 4 == 0 ? 4 : 1; |
| 82 | out_elempack = num_output % 16 == 0 ? 16 : num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 83 | #elif __AVX__ |
| 84 | elempack = num_input % 8 == 0 ? 8 : num_input % 4 == 0 ? 4 : 1; |
| 85 | out_elempack = num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 86 | #else |
| 87 | elempack = num_input % 4 == 0 ? 4 : 1; |
| 88 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 89 | #endif |
| 90 | } |
| 91 | #endif // __SSE2__ |
| 92 | |
| 93 | if (opt.use_sgemm_convolution) |
| 94 | { |
| 95 | const int maxk = kernel_w * kernel_h; |
| 96 | |
| 97 | gemm = ncnn::create_layer_cpu(ncnn::LayerType::Gemm); |
| 98 | |
| 99 | ncnn::ParamDict pd; |
| 100 | pd.set(2, 1); // transA |
| 101 | pd.set(3, 0); // transB |
| 102 | pd.set(4, 1); // constantA |
| 103 | pd.set(5, 0); // constantB |
| 104 | pd.set(6, 1); // constantC |
| 105 | pd.set(7, maxk * num_output); // M = maxk*num_output |
| 106 | pd.set(8, 0); // N = size |
| 107 | pd.set(9, num_input); // K = inch |
| 108 | pd.set(10, -1); // constant_broadcast_type_C = null |
| 109 | pd.set(11, 0); // output_N1M |
| 110 | pd.set(12, out_elempack); |
| 111 | |
| 112 | gemm->load_param(pd); |
| 113 | |
| 114 | // maxk-inch-outch to pa-maxk-outch/pa-inch |
| 115 | Mat tmp; |
| 116 | { |
| 117 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 118 | |
| 119 | tmp.create(maxk * num_output, num_input); |
| 120 | |
| 121 | for (int p = 0; p < num_input; p += 1) |
| 122 | { |
| 123 | float* g00 = tmp.row(p); |
| 124 | |
| 125 | for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack) |
| 126 | { |
| 127 | for (int k = 0; k < maxk; k++) |
| 128 | { |
| 129 | for (int i = 0; i < out_elempack; i++) |
| 130 | { |
| 131 | const float* k00 = weight_data_r2.channel(q + i).row(p); |
| 132 | g00[0] = k00[k]; |
| 133 | g00++; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | ncnn::Mat weights[1]; |
| 141 | weights[0] = tmp; |
| 142 | |
| 143 | gemm->load_model(ModelBinFromMatArray(weights)); |
| 144 | |
| 145 | gemm->create_pipeline(opt); |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | Mat weight_data_transposed(weight_data.w); |
| 150 | { |
| 151 | float* pt = weight_data_transposed; |
| 152 | const float* p = weight_data; |
| 153 | |
| 154 | for (int i = 0; i < num_input * num_output; i++) |
| 155 | { |
| 156 | for (int k = 0; k < maxk; k++) |
| 157 | { |
| 158 | pt[maxk - 1 - k] = p[k]; |
| 159 | } |
| 160 | |
| 161 | p += maxk; |
| 162 | pt += maxk; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // src = kw-kh-inch-outch |
| 167 | // dst = pb-pa-kw-kh-inch/pa-outch/pb |
| 168 | Mat weight_data_r2 = weight_data_transposed.reshape(maxk, num_input, num_output); |
| 169 | |
| 170 | weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)4u * elempack * out_elempack, elempack * out_elempack); |
| 171 | |
| 172 | for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack) |
| 173 | { |
| 174 | float* g00 = weight_data_tm.channel(q / out_elempack); |
| 175 | |
| 176 | for (int p = 0; p + (elempack - 1) < num_input; p += elempack) |
| 177 | { |
| 178 | for (int k = 0; k < maxk; k++) |
| 179 | { |
| 180 | for (int i = 0; i < elempack; i++) |
| 181 | { |
| 182 | for (int j = 0; j < out_elempack; j++) |
| 183 | { |
| 184 | const float* k00 = weight_data_r2.channel(q + j).row(p + i); |
| 185 | |
| 186 | g00[0] = k00[k]; |
| 187 | |
| 188 | g00++; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if (opt.lightmode) |
| 197 | weight_data.release(); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | int Deconvolution_x86::destroy_pipeline(const Option& opt) |
| 203 | { |
| 204 | if (activation) |
| 205 | { |
| 206 | activation->destroy_pipeline(opt); |
| 207 | delete activation; |
| 208 | activation = 0; |
| 209 | } |
| 210 | |
| 211 | if (gemm) |
| 212 | { |
| 213 | gemm->destroy_pipeline(opt); |
| 214 | delete gemm; |
| 215 | gemm = 0; |
| 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | int Deconvolution_x86::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 222 | { |
| 223 | // deconvolv with NxN kernel |
| 224 | // value = value + bias |
| 225 | |
| 226 | int w = bottom_blob.w; |
| 227 | int h = bottom_blob.h; |
| 228 | size_t elemsize = bottom_blob.elemsize; |
| 229 | int elempack = bottom_blob.elempack; |
| 230 | |
| 231 | // NCNN_LOGE("Deconvolution input %d x %d pad = %d %d ksize=%d %d stride=%d %d", w, h, pad_w, pad_h, kernel_w, kernel_h, stride_w, stride_h); |
| 232 | |
| 233 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 234 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 235 | |
| 236 | int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right; |
| 237 | int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom; |
| 238 | int out_elempack = 1; |
| 239 | #if __SSE2__ |
| 240 | if (opt.use_packing_layout) |
| 241 | { |
| 242 | #if __AVX512F__ |
| 243 | out_elempack = num_output % 16 == 0 ? 16 : num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 244 | #elif __AVX__ |
| 245 | out_elempack = num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 246 | #else |
| 247 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 248 | #endif |
| 249 | } |
| 250 | #endif // __SSE2__ |
| 251 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 252 | |
| 253 | int out_channels = num_output / out_elempack; |
| 254 | |
| 255 | Mat top_blob_bordered; |
| 256 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0)) |
| 257 | { |
| 258 | top_blob_bordered.create(outw, outh, out_channels, out_elemsize, out_elempack, opt.workspace_allocator); |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | top_blob_bordered = top_blob; |
| 263 | top_blob_bordered.create(outw, outh, out_channels, out_elemsize, out_elempack, opt.blob_allocator); |
| 264 | } |
| 265 | if (top_blob_bordered.empty()) |
| 266 | return -100; |
| 267 | |
| 268 | const int maxk = kernel_w * kernel_h; |
| 269 | |
| 270 | if (opt.use_sgemm_convolution) |
| 271 | { |
| 272 | // sgemm |
| 273 | Mat bottom_blob_2 = bottom_blob; |
| 274 | { |
| 275 | bottom_blob_2.w = bottom_blob.w * bottom_blob.h; |
| 276 | bottom_blob_2.h = 1; |
| 277 | } |
| 278 | Mat top_col2im; |
| 279 | Option opt_b = opt; |
| 280 | opt_b.blob_allocator = top_blob_bordered.allocator; |
| 281 | int ret = gemm->forward(bottom_blob_2, top_col2im, opt_b); |
| 282 | if (ret != 0) |
| 283 | return ret; |
| 284 | |
| 285 | { |
| 286 | // col2im |
| 287 | const int gap = (outw * stride_h - w * stride_w) * out_elempack; |
| 288 | |
| 289 | #if __SSE2__ |
| 290 | #if __AVX__ |
| 291 | #if __AVX512F__ |
| 292 | if (out_elempack == 16) |
| 293 | { |
| 294 | #pragma omp parallel for num_threads(opt.num_threads) |
| 295 | for (int p = 0; p < out_channels; p++) |
| 296 | { |
| 297 | const float* sptr = top_col2im.row(p * maxk); |
| 298 | Mat outm = top_blob_bordered.channel(p); |
| 299 | |
| 300 | if (bias_data.empty()) |
| 301 | { |
| 302 | outm.fill(_mm512_setzero_ps()); |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | outm.fill(_mm512_loadu_ps((const float*)bias_data + p * 16)); |
| 307 | } |
| 308 | |
| 309 | for (int u = 0; u < kernel_h; u++) |
| 310 | { |
| 311 | for (int v = 0; v < kernel_w; v++) |
| 312 | { |
| 313 | float* ptr = outm.row(dilation_h * u) + dilation_w * v * 16; |
| 314 | |
| 315 | for (int i = 0; i < h; i++) |
| 316 | { |
| 317 | for (int j = 0; j < w; j++) |
| 318 | { |
| 319 | __m512 _val = _mm512_load_ps(ptr); |
| 320 | __m512 _s = _mm512_load_ps(sptr); |
| 321 | _val = _mm512_add_ps(_val, _s); |
| 322 | _mm512_store_ps(ptr, _val); |
| 323 | |
| 324 | ptr += stride_w * 16; |
| 325 | sptr += 16; |
| 326 | } |
| 327 | |
| 328 | ptr += gap; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | #endif // __AVX512F__ |
| 335 | |
| 336 | if (out_elempack == 8) |
| 337 | { |
| 338 | #pragma omp parallel for num_threads(opt.num_threads) |
| 339 | for (int p = 0; p < out_channels; p++) |
| 340 | { |
| 341 | const float* sptr = top_col2im.row(p * maxk); |
| 342 | Mat outm = top_blob_bordered.channel(p); |
| 343 | |
| 344 | if (bias_data.empty()) |
| 345 | { |
| 346 | outm.fill(_mm256_setzero_ps()); |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | outm.fill(_mm256_loadu_ps((const float*)bias_data + p * 8)); |
| 351 | } |
| 352 | |
| 353 | for (int u = 0; u < kernel_h; u++) |
| 354 | { |
| 355 | for (int v = 0; v < kernel_w; v++) |
| 356 | { |
| 357 | float* ptr = outm.row(dilation_h * u) + dilation_w * v * 8; |
| 358 | |
| 359 | for (int i = 0; i < h; i++) |
| 360 | { |
| 361 | for (int j = 0; j < w; j++) |
| 362 | { |
| 363 | __m256 _val = _mm256_load_ps(ptr); |
| 364 | __m256 _s = _mm256_load_ps(sptr); |
| 365 | _val = _mm256_add_ps(_val, _s); |
| 366 | _mm256_store_ps(ptr, _val); |
| 367 | |
| 368 | ptr += stride_w * 8; |
| 369 | sptr += 8; |
| 370 | } |
| 371 | |
| 372 | ptr += gap; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | #endif // __AVX__ |
| 379 | |
| 380 | if (out_elempack == 4) |
| 381 | { |
| 382 | #pragma omp parallel for num_threads(opt.num_threads) |
| 383 | for (int p = 0; p < out_channels; p++) |
| 384 | { |
| 385 | const float* sptr = top_col2im.row(p * maxk); |
| 386 | Mat outm = top_blob_bordered.channel(p); |
| 387 | |
| 388 | if (bias_data.empty()) |
| 389 | { |
| 390 | outm.fill(_mm_setzero_ps()); |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | outm.fill(_mm_loadu_ps((const float*)bias_data + p * 4)); |
| 395 | } |
| 396 | |
| 397 | for (int u = 0; u < kernel_h; u++) |
| 398 | { |
| 399 | for (int v = 0; v < kernel_w; v++) |
| 400 | { |
| 401 | float* ptr = outm.row(dilation_h * u) + dilation_w * v * 4; |
| 402 | |
| 403 | for (int i = 0; i < h; i++) |
| 404 | { |
| 405 | for (int j = 0; j < w; j++) |
| 406 | { |
| 407 | __m128 _val = _mm_load_ps(ptr); |
| 408 | __m128 _s = _mm_load_ps(sptr); |
| 409 | _val = _mm_add_ps(_val, _s); |
| 410 | _mm_store_ps(ptr, _val); |
| 411 | |
| 412 | ptr += stride_w * 4; |
| 413 | sptr += 4; |
| 414 | } |
| 415 | |
| 416 | ptr += gap; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | #endif // __SSE2__ |
| 423 | |
| 424 | if (out_elempack == 1) |
| 425 | { |
| 426 | #pragma omp parallel for num_threads(opt.num_threads) |
| 427 | for (int p = 0; p < out_channels; p++) |
| 428 | { |
| 429 | const float* sptr = top_col2im.row(p * maxk); |
| 430 | Mat outm = top_blob_bordered.channel(p); |
| 431 | |
| 432 | const float bias = bias_data.empty() ? 0.f : bias_data[p]; |
| 433 | outm.fill(bias); |
| 434 | |
| 435 | for (int u = 0; u < kernel_h; u++) |
| 436 | { |
| 437 | for (int v = 0; v < kernel_w; v++) |
| 438 | { |
| 439 | float* ptr = outm.row(dilation_h * u) + dilation_w * v; |
| 440 | |
| 441 | for (int i = 0; i < h; i++) |
| 442 | { |
| 443 | for (int j = 0; j < w; j++) |
| 444 | { |
| 445 | ptr[0] += sptr[0]; |
| 446 | |
| 447 | ptr += stride_w; |
| 448 | sptr += 1; |
| 449 | } |
| 450 | |
| 451 | ptr += gap; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | if (activation) |
| 460 | { |
| 461 | activation->forward_inplace(top_blob_bordered, opt); |
| 462 | } |
| 463 | } |
| 464 | else |
| 465 | { |
| 466 | #if __SSE2__ |
| 467 | #if __AVX__ |
| 468 | #if __AVX512F__ |
| 469 | if (elempack == 16 && out_elempack == 16) |
| 470 | { |
| 471 | deconvolution_pack16_avx512(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 472 | } |
| 473 | |
| 474 | if (elempack == 8 && out_elempack == 16) |
| 475 | { |
| 476 | deconvolution_pack8to16_avx512(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 477 | } |
| 478 | |
| 479 | if (elempack == 16 && out_elempack == 8) |
| 480 | { |
| 481 | deconvolution_pack16to8_avx512(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 482 | } |
| 483 | |
| 484 | if (elempack == 4 && out_elempack == 16) |
| 485 | { |
| 486 | deconvolution_pack4to16_avx512(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 487 | } |
| 488 | |
| 489 | if (elempack == 16 && out_elempack == 4) |
| 490 | { |
| 491 | deconvolution_pack16to4_avx512(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 492 | } |
| 493 | |
| 494 | if (elempack == 1 && out_elempack == 16) |
| 495 | { |
| 496 | deconvolution_pack1to16_avx512(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 497 | } |
| 498 | |
| 499 | if (elempack == 16 && out_elempack == 1) |
| 500 | { |
| 501 | deconvolution_pack16to1_avx512(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 502 | } |
| 503 | #endif // __AVX512F__ |
| 504 | |
| 505 | if (elempack == 8 && out_elempack == 8) |
| 506 | { |
| 507 | deconvolution_pack8_avx(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 508 | } |
| 509 | |
| 510 | if (elempack == 4 && out_elempack == 8) |
| 511 | { |
| 512 | deconvolution_pack4to8_avx(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 513 | } |
| 514 | |
| 515 | if (elempack == 8 && out_elempack == 4) |
| 516 | { |
| 517 | deconvolution_pack8to4_avx(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 518 | } |
| 519 | |
| 520 | if (elempack == 1 && out_elempack == 8) |
| 521 | { |
| 522 | deconvolution_pack1to8_avx(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 523 | } |
| 524 | |
| 525 | if (elempack == 8 && out_elempack == 1) |
| 526 | { |
| 527 | deconvolution_pack8to1_avx(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 528 | } |
| 529 | #endif // __AVX__ |
| 530 | |
| 531 | if (elempack == 4 && out_elempack == 4) |
| 532 | { |
| 533 | deconvolution_pack4_sse(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 534 | } |
| 535 | |
| 536 | if (elempack == 1 && out_elempack == 4) |
| 537 | { |
| 538 | deconvolution_pack1to4_sse(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 539 | } |
| 540 | |
| 541 | if (elempack == 4 && out_elempack == 1) |
| 542 | { |
| 543 | deconvolution_pack4to1_sse(bottom_blob, top_blob_bordered, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, activation_type, activation_params, opt); |
| 544 | } |
| 545 | #endif // __SSE2__ |
| 546 | |
| 547 | if (elempack == 1 && out_elempack == 1) |
| 548 | { |
| 549 | // num_output |
| 550 | #pragma omp parallel for num_threads(opt.num_threads) |
| 551 | for (int p = 0; p < num_output; p++) |
| 552 | { |
| 553 | float* outptr = top_blob_bordered.channel(p); |
| 554 | |
| 555 | // shadowed variable for less openmp task args |
| 556 | const int w = bottom_blob.w; |
| 557 | const int h = bottom_blob.h; |
| 558 | const int channels = bottom_blob.c; |
| 559 | const int outw = top_blob_bordered.w; |
| 560 | const int outh = top_blob_bordered.h; |
| 561 | |
| 562 | for (int i = 0; i < outh; i++) |
| 563 | { |
| 564 | for (int j = 0; j < outw; j++) |
| 565 | { |
| 566 | float sum = 0.f; |
| 567 | |
| 568 | if (bias_term) |
| 569 | { |
| 570 | sum = bias_data[p]; |
| 571 | } |
| 572 | |
| 573 | const float* kptr = (const float*)weight_data_tm.channel(p); |
| 574 | |
| 575 | // channels |
| 576 | for (int q = 0; q < channels; q++) |
| 577 | { |
| 578 | const Mat m = bottom_blob.channel(q); |
| 579 | |
| 580 | for (int y = 0; y < kernel_h; y++) |
| 581 | { |
| 582 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 583 | if (sys < 0 || sys % stride_h != 0) |
| 584 | continue; |
| 585 | |
| 586 | int sy = sys / stride_h; |
| 587 | if (sy >= h) |
| 588 | continue; |
| 589 | |
| 590 | const float* sptr = m.row(sy); |
| 591 | |
| 592 | for (int x = 0; x < kernel_w; x++) |
| 593 | { |
| 594 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 595 | if (sxs < 0 || sxs % stride_w != 0) |
| 596 | continue; |
| 597 | |
| 598 | int sx = sxs / stride_w; |
| 599 | if (sx >= w) |
| 600 | continue; |
| 601 | |
| 602 | float val = sptr[sx]; |
| 603 | |
| 604 | int k = y * kernel_w + x; |
| 605 | |
| 606 | float w = kptr[k]; |
| 607 | |
| 608 | sum += val * w; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | kptr += maxk; |
| 613 | } |
| 614 | |
| 615 | sum = activation_ss(sum, activation_type, activation_params); |
| 616 | |
| 617 | outptr[j] = sum; |
| 618 | } |
| 619 | |
| 620 | outptr += outw; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | cut_padding(top_blob_bordered, top_blob, opt); |
| 627 | if (top_blob.empty()) |
| 628 | return -100; |
| 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | int Deconvolution_x86::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 634 | { |
| 635 | const Mat& bottom_blob = bottom_blobs[0]; |
| 636 | const Mat& _weight_data = bottom_blobs[1]; |
| 637 | Mat& top_blob = top_blobs[0]; |
| 638 | |
| 639 | const int _num_input = bottom_blob.c * bottom_blob.elempack; |
| 640 | const int _kernel_w = _weight_data.w; |
| 641 | const int _kernel_h = _weight_data.h; |
| 642 | const int _num_output = _weight_data.d * 1; |
| 643 | |
| 644 | Mat weight_data_flattened; |
| 645 | flatten(_weight_data, weight_data_flattened, opt); |
| 646 | if (weight_data_flattened.empty()) |
| 647 | return -100; |
| 648 | |
| 649 | // weight_data_flattened as pack1 |
| 650 | weight_data_flattened.w *= weight_data_flattened.elempack; |
| 651 | weight_data_flattened.elemsize /= weight_data_flattened.elempack; |
| 652 | weight_data_flattened.elempack = 1; |
| 653 | |
| 654 | // transpose group-inch/group-outch/group-kh-kw to group-outch/group-inch/group-kh-kw |
| 655 | Mat weight_data_transposed; |
| 656 | { |
| 657 | weight_data_transposed.create(_kernel_w * _kernel_h * _num_output * _num_input / 1, 4u, opt.workspace_allocator); |
| 658 | if (weight_data_transposed.empty()) |
| 659 | return -100; |
| 660 | |
| 661 | const int outch_g = _num_output / 1; |
| 662 | const int inch_g = _num_input / 1; |
| 663 | const int maxk = _kernel_h * _kernel_w; |
| 664 | |
| 665 | for (int g = 0; g < 1; g++) |
| 666 | { |
| 667 | // reorder weight from inch-outch to outch-inch |
| 668 | float* wg2 = (float*)weight_data_transposed + g * outch_g * inch_g * maxk; |
| 669 | const float* wg = (const float*)weight_data_flattened + g * inch_g * outch_g * maxk; |
| 670 | for (int i = 0; i < outch_g; i++) |
| 671 | { |
| 672 | for (int j = 0; j < inch_g; j++) |
| 673 | { |
| 674 | for (int k = 0; k < maxk; k++) |
| 675 | { |
| 676 | wg2[(i * inch_g + j) * maxk + k] = wg[(j * outch_g + i) * maxk + k]; |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | Mat bias_data_flattened; |
| 684 | if (bias_term) |
| 685 | { |
| 686 | const Mat& _bias_data = bottom_blobs[2]; |
| 687 | flatten(_bias_data, bias_data_flattened, opt); |
| 688 | if (bias_data_flattened.empty()) |
| 689 | return -100; |
| 690 | |
| 691 | // bias_data_flattened as pack1 |
| 692 | bias_data_flattened.w *= bias_data_flattened.elempack; |
| 693 | bias_data_flattened.elemsize /= bias_data_flattened.elempack; |
| 694 | bias_data_flattened.elempack = 1; |
| 695 | } |
| 696 | |
| 697 | ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Deconvolution); |
| 698 | |
| 699 | ncnn::ParamDict pd; |
| 700 | pd.set(0, _num_output); |
| 701 | pd.set(1, _kernel_w); |
| 702 | pd.set(11, _kernel_h); |
| 703 | pd.set(2, dilation_w); |
| 704 | pd.set(12, dilation_h); |
| 705 | pd.set(3, stride_w); |
| 706 | pd.set(13, stride_h); |
| 707 | pd.set(4, pad_left); |
| 708 | pd.set(15, pad_right); |
| 709 | pd.set(14, pad_top); |
| 710 | pd.set(16, pad_bottom); |
| 711 | pd.set(18, output_pad_right); |
| 712 | pd.set(19, output_pad_bottom); |
| 713 | pd.set(20, output_w); |
| 714 | pd.set(21, output_h); |
| 715 | pd.set(5, bias_term); |
| 716 | pd.set(6, weight_data_transposed.w); |
| 717 | pd.set(9, activation_type); |
| 718 | pd.set(10, activation_params); |
| 719 | |
| 720 | op->load_param(pd); |
| 721 | |
| 722 | ncnn::Mat weights[2]; |
| 723 | weights[0] = weight_data_transposed; |
| 724 | weights[1] = bias_data_flattened; |
| 725 | |
| 726 | op->load_model(ncnn::ModelBinFromMatArray(weights)); |
| 727 | |
| 728 | op->create_pipeline(opt); |
| 729 | |
| 730 | op->forward(bottom_blob, top_blob, opt); |
| 731 | |
| 732 | op->destroy_pipeline(opt); |
| 733 | |
| 734 | delete op; |
| 735 | |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | } // namespace ncnn |