| 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 "deformableconv2d_x86.h" |
| 16 | |
| 17 | #if __SSE2__ |
| 18 | #include <emmintrin.h> |
| 19 | #if __SSE4_1__ |
| 20 | #include <smmintrin.h> |
| 21 | #if __AVX__ |
| 22 | #include <immintrin.h> |
| 23 | #endif |
| 24 | #endif // __SSE4_1__ |
| 25 | #endif // __SSE2__ |
| 26 | #include "x86_activation.h" |
| 27 | #include "x86_usability.h" |
| 28 | |
| 29 | #include "benchmark.h" |
| 30 | #include "cpu.h" |
| 31 | #include "layer_type.h" |
| 32 | |
| 33 | namespace ncnn { |
| 34 | |
| 35 | #if __SSE2__ |
| 36 | #include "deformableconv2d_pack4.h" |
| 37 | #include "deformableconv2d_pack1to4.h" |
| 38 | #include "deformableconv2d_pack4to1.h" |
| 39 | |
| 40 | #if __AVX__ |
| 41 | #include "deformableconv2d_pack8.h" |
| 42 | #include "deformableconv2d_pack4to8.h" |
| 43 | #include "deformableconv2d_pack1to8.h" |
| 44 | #include "deformableconv2d_pack8to4.h" |
| 45 | #include "deformableconv2d_pack8to1.h" |
| 46 | |
| 47 | #if __AVX512F__ |
| 48 | #include "deformableconv2d_pack16.h" |
| 49 | #include "deformableconv2d_pack8to16.h" |
| 50 | #include "deformableconv2d_pack4to16.h" |
| 51 | #include "deformableconv2d_pack1to16.h" |
| 52 | #include "deformableconv2d_pack16to8.h" |
| 53 | #include "deformableconv2d_pack16to4.h" |
| 54 | #include "deformableconv2d_pack16to1.h" |
| 55 | #endif // __AVX512F__ |
| 56 | #endif // __AVX__ |
| 57 | #endif // __SSE2__ |
| 58 | |
| 59 | DeformableConv2D_x86::DeformableConv2D_x86() |
| 60 | { |
| 61 | #if __SSE2__ |
| 62 | support_packing = true; |
| 63 | #endif // __SSE2__ |
| 64 | |
| 65 | activation = 0; |
| 66 | gemm = 0; |
| 67 | } |
| 68 | |
| 69 | static int _4Dindex_to_1Dindex(int i0, int i1, int i2, int i3, int l1, int l2, int l3) |
| 70 | { |
| 71 | return ((i0 * l1 + i1) * l2 + i2) * l3 + i3; |
| 72 | } |
| 73 | |
| 74 | static int _6Dindex_to_1Dindex(int i0, int i1, int i2, int i3, int i4, int i5, int l1, int l2, int l3, int l4, int l5) |
| 75 | { |
| 76 | return ((((i0 * l1 + i1) * l2 + i2) * l3 + i3) * l4 + i4) * l5 + i5; |
| 77 | } |
| 78 | |
| 79 | static void deformableconv2d_transform_kernel_packed_sse(const Mat& weight_data, Mat& weight_data_tm, int num_input, int num_output, int kernel_w, int kernel_h, int elempack, int out_elempack) |
| 80 | { |
| 81 | const int maxk = kernel_w * kernel_h; |
| 82 | |
| 83 | // src = kw-kh-inch-outch |
| 84 | // dst = pb-pa-inch/pa-kw-kh-outch/pb |
| 85 | { |
| 86 | const float* weight_ptr = weight_data; |
| 87 | |
| 88 | weight_data_tm.create(num_input * maxk * num_output / (elempack * out_elempack), (size_t)4u * elempack * out_elempack, elempack * out_elempack); |
| 89 | float* ptr = weight_data_tm; |
| 90 | for (int oc = 0; oc < num_output; oc++) |
| 91 | { |
| 92 | for (int i = 0; i < kernel_h; i++) |
| 93 | { |
| 94 | for (int j = 0; j < kernel_w; j++) |
| 95 | { |
| 96 | for (int ic = 0; ic < num_input; ic++) |
| 97 | { |
| 98 | ptr[_6Dindex_to_1Dindex(oc / out_elempack, i, j, ic / elempack, ic % elempack, oc % out_elempack, kernel_h, kernel_w, num_input / elempack, elempack, out_elempack)] = weight_ptr[_4Dindex_to_1Dindex(oc, ic, i, j, num_input, kernel_h, kernel_w)]; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | weight_data_tm = weight_data_tm.reshape(num_input / elempack, maxk, num_output / out_elempack); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | int DeformableConv2D_x86::create_pipeline(const Option& opt) |
| 108 | { |
| 109 | activation = create_activation_layer(activation_type, activation_params, opt); |
| 110 | |
| 111 | int kernel_size = kernel_w * kernel_h; |
| 112 | int num_input = weight_data_size / kernel_size / num_output; |
| 113 | |
| 114 | int elempack = 1; |
| 115 | int out_elempack = 1; |
| 116 | |
| 117 | #if __SSE2__ |
| 118 | if (opt.use_packing_layout) |
| 119 | { |
| 120 | #if __AVX512F__ |
| 121 | elempack = num_input % 16 == 0 ? 16 : num_input % 8 == 0 ? 8 : num_input % 4 == 0 ? 4 : 1; |
| 122 | out_elempack = num_output % 16 == 0 ? 16 : num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 123 | #elif __AVX__ |
| 124 | elempack = num_input % 8 == 0 ? 8 : num_input % 4 == 0 ? 4 : 1; |
| 125 | out_elempack = num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 126 | #else |
| 127 | elempack = num_input % 4 == 0 ? 4 : 1; |
| 128 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 129 | #endif |
| 130 | } |
| 131 | #endif // __SSE2__ |
| 132 | |
| 133 | if (opt.use_sgemm_convolution) |
| 134 | { |
| 135 | const int maxk = kernel_w * kernel_h; |
| 136 | |
| 137 | gemm = ncnn::create_layer_cpu(ncnn::LayerType::Gemm); |
| 138 | |
| 139 | ncnn::ParamDict pd; |
| 140 | pd.set(2, 0); // transA |
| 141 | pd.set(3, 0); // transB |
| 142 | pd.set(4, 1); // constantA |
| 143 | pd.set(5, 0); // constantB |
| 144 | pd.set(6, 1); // constantC |
| 145 | pd.set(7, num_output); // M = outch |
| 146 | pd.set(8, 0); // N = size |
| 147 | pd.set(9, maxk * num_input); // K = maxk*inch |
| 148 | pd.set(10, bias_term ? 1 : -1); // constant_broadcast_type_C = (M) |
| 149 | pd.set(11, 1); // output_N1M |
| 150 | |
| 151 | gemm->load_param(pd); |
| 152 | |
| 153 | // maxk-inch-outch to pa-maxk-inch/pa-outch |
| 154 | Mat tmp; |
| 155 | { |
| 156 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 157 | |
| 158 | tmp.create(maxk * num_input, num_output); |
| 159 | |
| 160 | for (int q = 0; q < num_output; q += 1) |
| 161 | { |
| 162 | float* g00 = tmp.row(q); |
| 163 | |
| 164 | for (int p = 0; p + (elempack - 1) < num_input; p += elempack) |
| 165 | { |
| 166 | for (int k = 0; k < maxk; k++) |
| 167 | { |
| 168 | for (int i = 0; i < elempack; i++) |
| 169 | { |
| 170 | const float* k00 = weight_data_r2.channel(q).row(p + i); |
| 171 | g00[0] = k00[k]; |
| 172 | g00++; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (bias_term) |
| 180 | { |
| 181 | ncnn::Mat weights[2]; |
| 182 | weights[0] = tmp; |
| 183 | weights[1] = bias_data; |
| 184 | |
| 185 | gemm->load_model(ModelBinFromMatArray(weights)); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | ncnn::Mat weights[1]; |
| 190 | weights[0] = tmp; |
| 191 | |
| 192 | gemm->load_model(ModelBinFromMatArray(weights)); |
| 193 | } |
| 194 | |
| 195 | gemm->create_pipeline(opt); |
| 196 | } |
| 197 | else if (elempack == 1 && out_elempack == 1) |
| 198 | { |
| 199 | weight_data_tm = weight_data; |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | deformableconv2d_transform_kernel_packed_sse(weight_data, weight_data_tm, num_input, num_output, kernel_w, kernel_h, elempack, out_elempack); |
| 204 | } |
| 205 | |
| 206 | if (opt.lightmode) |
| 207 | weight_data.release(); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | int DeformableConv2D_x86::destroy_pipeline(const Option& opt) |
| 213 | { |
| 214 | if (activation) |
| 215 | { |
| 216 | activation->destroy_pipeline(opt); |
| 217 | delete activation; |
| 218 | activation = 0; |
| 219 | } |
| 220 | |
| 221 | if (gemm) |
| 222 | { |
| 223 | gemm->destroy_pipeline(opt); |
| 224 | delete gemm; |
| 225 | gemm = 0; |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | int DeformableConv2D_x86::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 232 | { |
| 233 | const Mat& bottom_blob = bottom_blobs[0]; |
| 234 | const Mat& offset = bottom_blobs[1]; |
| 235 | const bool has_mask = (bottom_blobs.size() == 3); |
| 236 | Mat& top_blob = top_blobs[0]; |
| 237 | |
| 238 | int w = bottom_blob.w; |
| 239 | int h = bottom_blob.h; |
| 240 | int channels = bottom_blob.c; |
| 241 | size_t elemsize = bottom_blob.elemsize; |
| 242 | int elempack = bottom_blob.elempack; |
| 243 | |
| 244 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 245 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 246 | const int outw = (w + pad_left + pad_right - kernel_extent_w) / stride_w + 1; |
| 247 | const int outh = (h + pad_top + pad_bottom - kernel_extent_h) / stride_h + 1; |
| 248 | |
| 249 | int out_elempack = 1; |
| 250 | #if __SSE2__ |
| 251 | if (opt.use_packing_layout) |
| 252 | { |
| 253 | #if __AVX512F__ |
| 254 | out_elempack = num_output % 16 == 0 ? 16 : num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 255 | #elif __AVX__ |
| 256 | out_elempack = num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 257 | #else |
| 258 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 259 | #endif |
| 260 | } |
| 261 | #endif // __SSE2__ |
| 262 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 263 | |
| 264 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 265 | if (top_blob.empty()) |
| 266 | return -100; |
| 267 | |
| 268 | if (opt.use_sgemm_convolution) |
| 269 | { |
| 270 | const int size = outw * outh; |
| 271 | const int maxk = kernel_w * kernel_h; |
| 272 | |
| 273 | Mat offset_unpacked; |
| 274 | convert_packing(offset, offset_unpacked, 1, opt); |
| 275 | |
| 276 | Mat mask_unpacked; |
| 277 | if (has_mask) |
| 278 | { |
| 279 | const Mat& mask = bottom_blobs[2]; |
| 280 | convert_packing(mask, mask_unpacked, 1, opt); |
| 281 | } |
| 282 | |
| 283 | // im2col |
| 284 | Mat bottom_im2col(size, maxk * channels, elemsize, elempack, opt.workspace_allocator); |
| 285 | #if __SSE2__ |
| 286 | #if __AVX__ |
| 287 | #if __AVX512F__ |
| 288 | if (elempack == 16) |
| 289 | { |
| 290 | #pragma omp parallel for num_threads(opt.num_threads) |
| 291 | for (int p = 0; p < channels; p++) |
| 292 | { |
| 293 | const Mat img = bottom_blob.channel(p); |
| 294 | float* ptr = bottom_im2col.row(p * maxk); |
| 295 | |
| 296 | for (int u = 0; u < kernel_h; u++) |
| 297 | { |
| 298 | for (int v = 0; v < kernel_w; v++) |
| 299 | { |
| 300 | const Mat offset_h_k = offset_unpacked.channel((u * kernel_w + v) * 2); |
| 301 | const Mat offset_w_k = offset_unpacked.channel((u * kernel_w + v) * 2 + 1); |
| 302 | const Mat mask_k = has_mask ? mask_unpacked.channel(u * kernel_w + v) : 0; |
| 303 | |
| 304 | for (int i = 0; i < outh; i++) |
| 305 | { |
| 306 | for (int j = 0; j < outw; j++) |
| 307 | { |
| 308 | float offset_h = offset_h_k.row(i)[j]; |
| 309 | float offset_w = offset_w_k.row(i)[j]; |
| 310 | |
| 311 | int h_in = i * stride_h - pad_top; |
| 312 | int w_in = j * stride_w - pad_left; |
| 313 | |
| 314 | const float h_im = h_in + u * dilation_h + offset_h; |
| 315 | const float w_im = w_in + v * dilation_w + offset_w; |
| 316 | |
| 317 | // Bilinear |
| 318 | __m512 _val = _mm512_setzero_ps(); |
| 319 | bool cond = h_im > -1 && w_im > -1 && h_im < h && w_im < w; |
| 320 | if (cond) |
| 321 | { |
| 322 | int h_low = floor(h_im); |
| 323 | int w_low = floor(w_im); |
| 324 | int h_high = h_low + 1; |
| 325 | int w_high = w_low + 1; |
| 326 | |
| 327 | float lh = h_im - h_low; |
| 328 | float lw = w_im - w_low; |
| 329 | float hh = 1 - lh; |
| 330 | float hw = 1 - lw; |
| 331 | |
| 332 | bool v1_cond = (h_low >= 0 && w_low >= 0); |
| 333 | bool v2_cond = (h_low >= 0 && w_high <= w - 1); |
| 334 | bool v3_cond = (h_high <= h - 1 && w_low >= 0); |
| 335 | bool v4_cond = (h_high <= h - 1 && w_high <= w - 1); |
| 336 | |
| 337 | float w1 = hh * hw; |
| 338 | float w2 = hh * lw; |
| 339 | float w3 = lh * hw; |
| 340 | float w4 = lh * lw; |
| 341 | |
| 342 | __m512 _v1 = v1_cond ? _mm512_load_ps(img.row(h_low) + w_low * 16) : _mm512_setzero_ps(); |
| 343 | __m512 _v2 = v2_cond ? _mm512_load_ps(img.row(h_low) + w_high * 16) : _mm512_setzero_ps(); |
| 344 | __m512 _v3 = v3_cond ? _mm512_load_ps(img.row(h_high) + w_low * 16) : _mm512_setzero_ps(); |
| 345 | __m512 _v4 = v4_cond ? _mm512_load_ps(img.row(h_high) + w_high * 16) : _mm512_setzero_ps(); |
| 346 | |
| 347 | _val = _mm512_fmadd_ps(_v1, _mm512_set1_ps(w1), _val); |
| 348 | _val = _mm512_fmadd_ps(_v2, _mm512_set1_ps(w2), _val); |
| 349 | _val = _mm512_fmadd_ps(_v3, _mm512_set1_ps(w3), _val); |
| 350 | _val = _mm512_fmadd_ps(_v4, _mm512_set1_ps(w4), _val); |
| 351 | |
| 352 | if (has_mask) |
| 353 | _val = _mm512_mul_ps(_val, _mm512_set1_ps(mask_k.row(i)[j])); |
| 354 | } |
| 355 | |
| 356 | _mm512_store_ps(ptr, _val); |
| 357 | |
| 358 | ptr += 16; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | #endif // __AVX512F__ |
| 366 | |
| 367 | if (elempack == 8) |
| 368 | { |
| 369 | #pragma omp parallel for num_threads(opt.num_threads) |
| 370 | for (int p = 0; p < channels; p++) |
| 371 | { |
| 372 | const Mat img = bottom_blob.channel(p); |
| 373 | float* ptr = bottom_im2col.row(p * maxk); |
| 374 | |
| 375 | for (int u = 0; u < kernel_h; u++) |
| 376 | { |
| 377 | for (int v = 0; v < kernel_w; v++) |
| 378 | { |
| 379 | const Mat offset_h_k = offset_unpacked.channel((u * kernel_w + v) * 2); |
| 380 | const Mat offset_w_k = offset_unpacked.channel((u * kernel_w + v) * 2 + 1); |
| 381 | const Mat mask_k = has_mask ? mask_unpacked.channel(u * kernel_w + v) : 0; |
| 382 | |
| 383 | for (int i = 0; i < outh; i++) |
| 384 | { |
| 385 | for (int j = 0; j < outw; j++) |
| 386 | { |
| 387 | float offset_h = offset_h_k.row(i)[j]; |
| 388 | float offset_w = offset_w_k.row(i)[j]; |
| 389 | |
| 390 | int h_in = i * stride_h - pad_top; |
| 391 | int w_in = j * stride_w - pad_left; |
| 392 | |
| 393 | const float h_im = h_in + u * dilation_h + offset_h; |
| 394 | const float w_im = w_in + v * dilation_w + offset_w; |
| 395 | |
| 396 | // Bilinear |
| 397 | __m256 _val = _mm256_setzero_ps(); |
| 398 | bool cond = h_im > -1 && w_im > -1 && h_im < h && w_im < w; |
| 399 | if (cond) |
| 400 | { |
| 401 | int h_low = floor(h_im); |
| 402 | int w_low = floor(w_im); |
| 403 | int h_high = h_low + 1; |
| 404 | int w_high = w_low + 1; |
| 405 | |
| 406 | float lh = h_im - h_low; |
| 407 | float lw = w_im - w_low; |
| 408 | float hh = 1 - lh; |
| 409 | float hw = 1 - lw; |
| 410 | |
| 411 | bool v1_cond = (h_low >= 0 && w_low >= 0); |
| 412 | bool v2_cond = (h_low >= 0 && w_high <= w - 1); |
| 413 | bool v3_cond = (h_high <= h - 1 && w_low >= 0); |
| 414 | bool v4_cond = (h_high <= h - 1 && w_high <= w - 1); |
| 415 | |
| 416 | float w1 = hh * hw; |
| 417 | float w2 = hh * lw; |
| 418 | float w3 = lh * hw; |
| 419 | float w4 = lh * lw; |
| 420 | |
| 421 | __m256 _v1 = v1_cond ? _mm256_load_ps(img.row(h_low) + w_low * 8) : _mm256_setzero_ps(); |
| 422 | __m256 _v2 = v2_cond ? _mm256_load_ps(img.row(h_low) + w_high * 8) : _mm256_setzero_ps(); |
| 423 | __m256 _v3 = v3_cond ? _mm256_load_ps(img.row(h_high) + w_low * 8) : _mm256_setzero_ps(); |
| 424 | __m256 _v4 = v4_cond ? _mm256_load_ps(img.row(h_high) + w_high * 8) : _mm256_setzero_ps(); |
| 425 | |
| 426 | _val = _mm256_comp_fmadd_ps(_v1, _mm256_set1_ps(w1), _val); |
| 427 | _val = _mm256_comp_fmadd_ps(_v2, _mm256_set1_ps(w2), _val); |
| 428 | _val = _mm256_comp_fmadd_ps(_v3, _mm256_set1_ps(w3), _val); |
| 429 | _val = _mm256_comp_fmadd_ps(_v4, _mm256_set1_ps(w4), _val); |
| 430 | |
| 431 | if (has_mask) |
| 432 | _val = _mm256_mul_ps(_val, _mm256_set1_ps(mask_k.row(i)[j])); |
| 433 | } |
| 434 | |
| 435 | _mm256_store_ps(ptr, _val); |
| 436 | |
| 437 | ptr += 8; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | #endif // __AVX__ |
| 445 | |
| 446 | if (elempack == 4) |
| 447 | { |
| 448 | #pragma omp parallel for num_threads(opt.num_threads) |
| 449 | for (int p = 0; p < channels; p++) |
| 450 | { |
| 451 | const Mat img = bottom_blob.channel(p); |
| 452 | float* ptr = bottom_im2col.row(p * maxk); |
| 453 | |
| 454 | for (int u = 0; u < kernel_h; u++) |
| 455 | { |
| 456 | for (int v = 0; v < kernel_w; v++) |
| 457 | { |
| 458 | const Mat offset_h_k = offset_unpacked.channel((u * kernel_w + v) * 2); |
| 459 | const Mat offset_w_k = offset_unpacked.channel((u * kernel_w + v) * 2 + 1); |
| 460 | const Mat mask_k = has_mask ? mask_unpacked.channel(u * kernel_w + v) : 0; |
| 461 | |
| 462 | for (int i = 0; i < outh; i++) |
| 463 | { |
| 464 | for (int j = 0; j < outw; j++) |
| 465 | { |
| 466 | float offset_h = offset_h_k.row(i)[j]; |
| 467 | float offset_w = offset_w_k.row(i)[j]; |
| 468 | |
| 469 | int h_in = i * stride_h - pad_top; |
| 470 | int w_in = j * stride_w - pad_left; |
| 471 | |
| 472 | const float h_im = h_in + u * dilation_h + offset_h; |
| 473 | const float w_im = w_in + v * dilation_w + offset_w; |
| 474 | |
| 475 | // Bilinear |
| 476 | __m128 _val = _mm_setzero_ps(); |
| 477 | bool cond = h_im > -1 && w_im > -1 && h_im < h && w_im < w; |
| 478 | if (cond) |
| 479 | { |
| 480 | int h_low = (int)floorf(h_im); |
| 481 | int w_low = (int)floorf(w_im); |
| 482 | int h_high = h_low + 1; |
| 483 | int w_high = w_low + 1; |
| 484 | |
| 485 | float lh = h_im - h_low; |
| 486 | float lw = w_im - w_low; |
| 487 | float hh = 1 - lh; |
| 488 | float hw = 1 - lw; |
| 489 | |
| 490 | bool v1_cond = (h_low >= 0 && w_low >= 0); |
| 491 | bool v2_cond = (h_low >= 0 && w_high <= w - 1); |
| 492 | bool v3_cond = (h_high <= h - 1 && w_low >= 0); |
| 493 | bool v4_cond = (h_high <= h - 1 && w_high <= w - 1); |
| 494 | |
| 495 | float w1 = hh * hw; |
| 496 | float w2 = hh * lw; |
| 497 | float w3 = lh * hw; |
| 498 | float w4 = lh * lw; |
| 499 | |
| 500 | __m128 _v1 = v1_cond ? _mm_load_ps(img.row(h_low) + w_low * 4) : _mm_setzero_ps(); |
| 501 | __m128 _v2 = v2_cond ? _mm_load_ps(img.row(h_low) + w_high * 4) : _mm_setzero_ps(); |
| 502 | __m128 _v3 = v3_cond ? _mm_load_ps(img.row(h_high) + w_low * 4) : _mm_setzero_ps(); |
| 503 | __m128 _v4 = v4_cond ? _mm_load_ps(img.row(h_high) + w_high * 4) : _mm_setzero_ps(); |
| 504 | |
| 505 | _val = _mm_comp_fmadd_ps(_v1, _mm_set1_ps(w1), _val); |
| 506 | _val = _mm_comp_fmadd_ps(_v2, _mm_set1_ps(w2), _val); |
| 507 | _val = _mm_comp_fmadd_ps(_v3, _mm_set1_ps(w3), _val); |
| 508 | _val = _mm_comp_fmadd_ps(_v4, _mm_set1_ps(w4), _val); |
| 509 | |
| 510 | if (has_mask) |
| 511 | _val = _mm_mul_ps(_val, _mm_set1_ps(mask_k.row(i)[j])); |
| 512 | } |
| 513 | |
| 514 | _mm_store_ps(ptr, _val); |
| 515 | |
| 516 | ptr += 4; |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | #endif // __SSE2__ |
| 524 | |
| 525 | if (elempack == 1) |
| 526 | { |
| 527 | #pragma omp parallel for num_threads(opt.num_threads) |
| 528 | for (int p = 0; p < channels; p++) |
| 529 | { |
| 530 | const Mat img = bottom_blob.channel(p); |
| 531 | float* ptr = bottom_im2col.row(p * maxk); |
| 532 | |
| 533 | for (int u = 0; u < kernel_h; u++) |
| 534 | { |
| 535 | for (int v = 0; v < kernel_w; v++) |
| 536 | { |
| 537 | const Mat offset_h_k = offset_unpacked.channel((u * kernel_w + v) * 2); |
| 538 | const Mat offset_w_k = offset_unpacked.channel((u * kernel_w + v) * 2 + 1); |
| 539 | const Mat mask_k = has_mask ? mask_unpacked.channel(u * kernel_w + v) : 0; |
| 540 | |
| 541 | for (int i = 0; i < outh; i++) |
| 542 | { |
| 543 | for (int j = 0; j < outw; j++) |
| 544 | { |
| 545 | float offset_h = offset_h_k.row(i)[j]; |
| 546 | float offset_w = offset_w_k.row(i)[j]; |
| 547 | |
| 548 | int h_in = i * stride_h - pad_top; |
| 549 | int w_in = j * stride_w - pad_left; |
| 550 | |
| 551 | const float h_im = h_in + u * dilation_h + offset_h; |
| 552 | const float w_im = w_in + v * dilation_w + offset_w; |
| 553 | |
| 554 | // Bilinear |
| 555 | float val = 0.f; |
| 556 | bool cond = h_im > -1 && w_im > -1 && h_im < h && w_im < w; |
| 557 | if (cond) |
| 558 | { |
| 559 | int h_low = (int)floorf(h_im); |
| 560 | int w_low = (int)floorf(w_im); |
| 561 | int h_high = h_low + 1; |
| 562 | int w_high = w_low + 1; |
| 563 | |
| 564 | float lh = h_im - h_low; |
| 565 | float lw = w_im - w_low; |
| 566 | float hh = 1 - lh; |
| 567 | float hw = 1 - lw; |
| 568 | |
| 569 | bool v1_cond = (h_low >= 0 && w_low >= 0); |
| 570 | bool v2_cond = (h_low >= 0 && w_high <= w - 1); |
| 571 | bool v3_cond = (h_high <= h - 1 && w_low >= 0); |
| 572 | bool v4_cond = (h_high <= h - 1 && w_high <= w - 1); |
| 573 | |
| 574 | float w1 = hh * hw; |
| 575 | float w2 = hh * lw; |
| 576 | float w3 = lh * hw; |
| 577 | float w4 = lh * lw; |
| 578 | |
| 579 | float v1 = v1_cond ? img.row(h_low)[w_low] : 0.f; |
| 580 | float v2 = v2_cond ? img.row(h_low)[w_high] : 0.f; |
| 581 | float v3 = v3_cond ? img.row(h_high)[w_low] : 0.f; |
| 582 | float v4 = v4_cond ? img.row(h_high)[w_high] : 0.f; |
| 583 | val = w1 * v1 + w2 * v2 + w3 * v3 + w4 * v4; |
| 584 | |
| 585 | if (has_mask) |
| 586 | val *= mask_k.row(i)[j]; |
| 587 | } |
| 588 | |
| 589 | ptr[0] = val; |
| 590 | |
| 591 | ptr += 1; |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | // sgemm |
| 600 | { |
| 601 | top_blob.w = outw * outh; |
| 602 | top_blob.h = 1; |
| 603 | } |
| 604 | Option opt_b = opt; |
| 605 | opt_b.blob_allocator = opt.workspace_allocator; |
| 606 | gemm->forward(bottom_im2col, top_blob, opt_b); |
| 607 | { |
| 608 | top_blob.w = outw; |
| 609 | top_blob.h = outh; |
| 610 | } |
| 611 | |
| 612 | if (activation) |
| 613 | { |
| 614 | activation->forward_inplace(top_blob, opt); |
| 615 | } |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | #if __SSE2__ |
| 621 | #if __AVX__ |
| 622 | #if __AVX512F__ |
| 623 | if (elempack == 16 && out_elempack == 16) |
| 624 | { |
| 625 | deformableconv2d_pack16_avx512(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 626 | } |
| 627 | |
| 628 | if (elempack == 8 && out_elempack == 16) |
| 629 | { |
| 630 | deformableconv2d_pack8to16_avx512(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 631 | } |
| 632 | |
| 633 | if (elempack == 16 && out_elempack == 8) |
| 634 | { |
| 635 | deformableconv2d_pack16to8_avx512(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 636 | } |
| 637 | |
| 638 | if (elempack == 4 && out_elempack == 16) |
| 639 | { |
| 640 | deformableconv2d_pack4to16_avx512(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 641 | } |
| 642 | |
| 643 | if (elempack == 16 && out_elempack == 4) |
| 644 | { |
| 645 | deformableconv2d_pack16to4_avx512(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 646 | } |
| 647 | |
| 648 | if (elempack == 1 && out_elempack == 16) |
| 649 | { |
| 650 | deformableconv2d_pack1to16_avx512(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 651 | } |
| 652 | |
| 653 | if (elempack == 16 && out_elempack == 1) |
| 654 | { |
| 655 | deformableconv2d_pack16to1_avx512(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 656 | } |
| 657 | |
| 658 | #endif // __AVX512F__ |
| 659 | |
| 660 | if (elempack == 8 && out_elempack == 8) |
| 661 | { |
| 662 | deformableconv2d_pack8_avx(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 663 | } |
| 664 | |
| 665 | if (elempack == 1 && out_elempack == 8) |
| 666 | { |
| 667 | deformableconv2d_pack1to8_avx(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 668 | } |
| 669 | |
| 670 | if (elempack == 4 && out_elempack == 8) |
| 671 | { |
| 672 | deformableconv2d_pack4to8_avx(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 673 | } |
| 674 | |
| 675 | if (elempack == 8 && out_elempack == 1) |
| 676 | { |
| 677 | deformableconv2d_pack8to1_avx(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 678 | } |
| 679 | |
| 680 | if (elempack == 8 && out_elempack == 4) |
| 681 | { |
| 682 | deformableconv2d_pack8to4_avx(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 683 | } |
| 684 | #endif // __AVX__ |
| 685 | |
| 686 | if (elempack == 4 && out_elempack == 4) |
| 687 | { |
| 688 | deformableconv2d_pack4_sse(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 689 | } |
| 690 | |
| 691 | if (elempack == 1 && out_elempack == 4) |
| 692 | { |
| 693 | deformableconv2d_pack1to4_sse(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 694 | } |
| 695 | |
| 696 | if (elempack == 4 && out_elempack == 1) |
| 697 | { |
| 698 | deformableconv2d_pack4to1_sse(bottom_blobs, top_blob, weight_data_tm, bias_data, kernel_w, kernel_h, dilation_w, dilation_h, stride_w, stride_h, pad_left, pad_top, activation_type, activation_params, opt); |
| 699 | } |
| 700 | #endif // __SSE2__ |
| 701 | |
| 702 | if (elempack == 1 && out_elempack == 1) |
| 703 | { |
| 704 | const bool offset_not_pack = offset.elempack == 1; |
| 705 | const bool mask_not_pack = has_mask ? bottom_blobs[2].elempack == 1 : true; |
| 706 | const float* weight_ptr = weight_data_tm; |
| 707 | |
| 708 | // naive deformable conv |
| 709 | #pragma omp parallel for num_threads(opt.num_threads) |
| 710 | for (int h_col = 0; h_col < outh; h_col++) |
| 711 | { |
| 712 | for (int w_col = 0; w_col < outw; w_col++) |
| 713 | { |
| 714 | int h_in = h_col * stride_h - pad_top; |
| 715 | int w_in = w_col * stride_w - pad_left; |
| 716 | for (int oc = 0; oc < num_output; oc++) |
| 717 | { |
| 718 | float sum = 0.f; |
| 719 | if (bias_term) |
| 720 | sum = bias_data[oc]; |
| 721 | for (int i = 0; i < kernel_h; i++) |
| 722 | { |
| 723 | for (int j = 0; j < kernel_w; j++) |
| 724 | { |
| 725 | float offset_h = 0.f; |
| 726 | float offset_w = 0.f; |
| 727 | float mask_ = 1.f; |
| 728 | if (offset_not_pack) |
| 729 | { |
| 730 | offset_h = offset.channel((i * kernel_w + j) * 2).row(h_col)[w_col]; |
| 731 | offset_w = offset.channel((i * kernel_w + j) * 2 + 1).row(h_col)[w_col]; |
| 732 | } |
| 733 | else |
| 734 | { |
| 735 | const int y_c = (i * kernel_w + j) * 2; |
| 736 | const int x_c = (i * kernel_w + j) * 2 + 1; |
| 737 | offset_h = offset.channel(y_c / offset.elempack).row(h_col)[w_col * offset.elempack + y_c % offset.elempack]; |
| 738 | offset_w = offset.channel(x_c / offset.elempack).row(h_col)[w_col * offset.elempack + x_c % offset.elempack]; |
| 739 | } |
| 740 | if (has_mask) |
| 741 | { |
| 742 | const Mat& mask = bottom_blobs[2]; |
| 743 | if (mask_not_pack) |
| 744 | { |
| 745 | mask_ = mask.channel(i * kernel_w + j).row(h_col)[w_col]; |
| 746 | } |
| 747 | else |
| 748 | { |
| 749 | const int m_c = i * kernel_w + j; |
| 750 | mask_ = mask.channel(m_c / mask.elempack).row(h_col)[w_col * mask.elempack + m_c % mask.elempack]; |
| 751 | } |
| 752 | } |
| 753 | const float h_im = h_in + i * dilation_h + offset_h; |
| 754 | const float w_im = w_in + j * dilation_w + offset_w; |
| 755 | |
| 756 | // Bilinear |
| 757 | const bool cond = h_im > -1 && w_im > -1 && h_im < h && w_im < w; |
| 758 | int h_low = 0; |
| 759 | int w_low = 0; |
| 760 | int h_high = 0; |
| 761 | int w_high = 0; |
| 762 | float w1 = 0.f; |
| 763 | float w2 = 0.f; |
| 764 | float w3 = 0.f; |
| 765 | float w4 = 0.f; |
| 766 | bool v1_cond = false; |
| 767 | bool v2_cond = false; |
| 768 | bool v3_cond = false; |
| 769 | bool v4_cond = false; |
| 770 | if (cond) |
| 771 | { |
| 772 | h_low = (int)floorf(h_im); |
| 773 | w_low = (int)floorf(w_im); |
| 774 | h_high = h_low + 1; |
| 775 | w_high = w_low + 1; |
| 776 | |
| 777 | float lh = h_im - h_low; |
| 778 | float lw = w_im - w_low; |
| 779 | float hh = 1 - lh; |
| 780 | float hw = 1 - lw; |
| 781 | |
| 782 | v1_cond = (h_low >= 0 && w_low >= 0); |
| 783 | v2_cond = (h_low >= 0 && w_high <= w - 1); |
| 784 | v3_cond = (h_high <= h - 1 && w_low >= 0); |
| 785 | v4_cond = (h_high <= h - 1 && w_high <= w - 1); |
| 786 | |
| 787 | w1 = hh * hw; |
| 788 | w2 = hh * lw; |
| 789 | w3 = lh * hw; |
| 790 | w4 = lh * lw; |
| 791 | } |
| 792 | |
| 793 | for (int ic = 0; ic < channels; ic++) |
| 794 | { |
| 795 | float val = 0.f; |
| 796 | if (cond) |
| 797 | { |
| 798 | float v1 = v1_cond ? bottom_blob.channel(ic).row(h_low)[w_low] : 0.f; |
| 799 | float v2 = v2_cond ? bottom_blob.channel(ic).row(h_low)[w_high] : 0.f; |
| 800 | float v3 = v3_cond ? bottom_blob.channel(ic).row(h_high)[w_low] : 0.f; |
| 801 | float v4 = v4_cond ? bottom_blob.channel(ic).row(h_high)[w_high] : 0.f; |
| 802 | val = w1 * v1 + w2 * v2 + w3 * v3 + w4 * v4; |
| 803 | } |
| 804 | sum += val * mask_ * weight_ptr[((oc * channels + ic) * kernel_h + i) * kernel_w + j]; |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | top_blob.channel(oc).row(h_col)[w_col] = activation_ss(sum, activation_type, activation_params); |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | } // namespace ncnn |