| 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_arm.h" |
| 16 | |
| 17 | #if __ARM_NEON |
| 18 | #include <arm_neon.h> |
| 19 | #endif // __ARM_NEON |
| 20 | |
| 21 | #include "arm_activation.h" |
| 22 | |
| 23 | namespace ncnn { |
| 24 | |
| 25 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 26 | #include "deconvolution_4x4_fp16s.h" |
| 27 | #endif |
| 28 | |
| 29 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 30 | int Deconvolution_arm::create_pipeline_fp16s(const Option& opt) |
| 31 | { |
| 32 | const int maxk = kernel_w * kernel_h; |
| 33 | const int num_input = weight_data_size / maxk / num_output; |
| 34 | |
| 35 | int elempack = 1; |
| 36 | int out_elempack = 1; |
| 37 | |
| 38 | if (opt.use_packing_layout) |
| 39 | { |
| 40 | elempack = opt.use_fp16_arithmetic && num_input % 8 == 0 ? 8 : num_input % 4 == 0 ? 4 : 1; |
| 41 | out_elempack = opt.use_fp16_arithmetic && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 42 | } |
| 43 | |
| 44 | if (opt.use_fp16_arithmetic && opt.use_sgemm_convolution) |
| 45 | { |
| 46 | const int maxk = kernel_w * kernel_h; |
| 47 | |
| 48 | gemm = ncnn::create_layer_cpu(ncnn::LayerType::Gemm); |
| 49 | |
| 50 | ncnn::ParamDict pd; |
| 51 | pd.set(2, 1); // transA |
| 52 | pd.set(3, 0); // transB |
| 53 | pd.set(4, 1); // constantA |
| 54 | pd.set(5, 0); // constantB |
| 55 | pd.set(6, 1); // constantC |
| 56 | pd.set(7, maxk * num_output); // M = maxk*num_output |
| 57 | pd.set(8, 0); // N = size |
| 58 | pd.set(9, num_input); // K = inch |
| 59 | pd.set(10, -1); // constant_broadcast_type_C = null |
| 60 | pd.set(11, 0); // output_N1M |
| 61 | pd.set(12, out_elempack); |
| 62 | |
| 63 | gemm->load_param(pd); |
| 64 | |
| 65 | // maxk-inch-outch to pa-maxk-outch/pa-inch |
| 66 | Mat tmp; |
| 67 | { |
| 68 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 69 | |
| 70 | tmp.create(maxk * num_output, num_input); |
| 71 | |
| 72 | for (int p = 0; p < num_input; p += 1) |
| 73 | { |
| 74 | float* g00 = tmp.row(p); |
| 75 | |
| 76 | for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack) |
| 77 | { |
| 78 | for (int k = 0; k < maxk; k++) |
| 79 | { |
| 80 | for (int i = 0; i < out_elempack; i++) |
| 81 | { |
| 82 | const float* k00 = weight_data_r2.channel(q + i).row(p); |
| 83 | g00[0] = k00[k]; |
| 84 | g00++; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | ncnn::Mat weights[1]; |
| 92 | weights[0] = tmp; |
| 93 | |
| 94 | gemm->load_model(ModelBinFromMatArray(weights)); |
| 95 | |
| 96 | gemm->create_pipeline(opt); |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | Mat weight_data_transposed(weight_data.w); |
| 101 | { |
| 102 | float* pt = weight_data_transposed; |
| 103 | const float* p = weight_data; |
| 104 | |
| 105 | for (int i = 0; i < num_input * num_output; i++) |
| 106 | { |
| 107 | for (int k = 0; k < maxk; k++) |
| 108 | { |
| 109 | pt[maxk - 1 - k] = p[k]; |
| 110 | } |
| 111 | |
| 112 | p += maxk; |
| 113 | pt += maxk; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // src = kw-kh-inch-outch |
| 118 | // dst = pb-pa-kw-kh-inch/pa-outch/pb |
| 119 | Mat weight_data_r2 = weight_data_transposed.reshape(maxk, num_input, num_output); |
| 120 | |
| 121 | weight_data_tm.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)2u * elempack * out_elempack, elempack * out_elempack); |
| 122 | |
| 123 | for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack) |
| 124 | { |
| 125 | __fp16* g00 = weight_data_tm.channel(q / out_elempack); |
| 126 | |
| 127 | for (int p = 0; p + (elempack - 1) < num_input; p += elempack) |
| 128 | { |
| 129 | for (int k = 0; k < maxk; k++) |
| 130 | { |
| 131 | for (int i = 0; i < elempack; i++) |
| 132 | { |
| 133 | for (int j = 0; j < out_elempack; j++) |
| 134 | { |
| 135 | const float* k00 = weight_data_r2.channel(q + j).row(p + i); |
| 136 | |
| 137 | g00[0] = (__fp16)k00[k]; |
| 138 | |
| 139 | g00++; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (elempack == 1 && out_elempack == 1 && opt.use_fp16_arithmetic) |
| 148 | { |
| 149 | if (kernel_w == 4 && kernel_h == 4 && stride_w == 2 && stride_h == 2 && dilation_w == 1 && dilation_h == 1) |
| 150 | { |
| 151 | ncnn::cast_float32_to_float16(weight_data, weight_data_tm, opt); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | ncnn::cast_float32_to_float16(bias_data, bias_data_fp16, opt); |
| 156 | |
| 157 | if (opt.lightmode) |
| 158 | weight_data.release(); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | int Deconvolution_arm::forward_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 164 | { |
| 165 | // deconvolv with NxN kernel |
| 166 | // value = value + bias |
| 167 | |
| 168 | int w = bottom_blob.w; |
| 169 | int h = bottom_blob.h; |
| 170 | int channels = bottom_blob.c; |
| 171 | size_t elemsize = bottom_blob.elemsize; |
| 172 | int elempack = bottom_blob.elempack; |
| 173 | |
| 174 | // 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); |
| 175 | |
| 176 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 177 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 178 | |
| 179 | int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right; |
| 180 | int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom; |
| 181 | int out_elempack = opt.use_packing_layout && num_output % 4 == 0 ? 4 : 1; |
| 182 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 183 | |
| 184 | Mat top_blob_bordered; |
| 185 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0)) |
| 186 | { |
| 187 | top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.workspace_allocator); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | top_blob_bordered = top_blob; |
| 192 | top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 193 | } |
| 194 | if (top_blob_bordered.empty()) |
| 195 | return -100; |
| 196 | |
| 197 | const int maxk = kernel_w * kernel_h; |
| 198 | |
| 199 | if (elempack == 4 && out_elempack == 4) |
| 200 | { |
| 201 | { |
| 202 | // num_output |
| 203 | #pragma omp parallel for num_threads(opt.num_threads) |
| 204 | for (int p = 0; p < num_output / out_elempack; p++) |
| 205 | { |
| 206 | __fp16* outptr = top_blob_bordered.channel(p); |
| 207 | |
| 208 | for (int i = 0; i < outh; i++) |
| 209 | { |
| 210 | for (int j = 0; j < outw; j++) |
| 211 | { |
| 212 | float32x4_t _sum = vdupq_n_f32(0.f); |
| 213 | |
| 214 | if (bias_term) |
| 215 | { |
| 216 | _sum = vld1q_f32(((const float*)bias_data) + p * 4); |
| 217 | } |
| 218 | |
| 219 | const __fp16* kptr = weight_data_tm.channel(p); |
| 220 | |
| 221 | // channels |
| 222 | for (int q = 0; q < channels; q++) |
| 223 | { |
| 224 | const Mat m = bottom_blob.channel(q); |
| 225 | |
| 226 | for (int y = 0; y < kernel_h; y++) |
| 227 | { |
| 228 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 229 | if (sys < 0 || sys % stride_h != 0) |
| 230 | continue; |
| 231 | |
| 232 | int sy = sys / stride_h; |
| 233 | if (sy >= h) |
| 234 | continue; |
| 235 | |
| 236 | for (int x = 0; x < kernel_w; x++) |
| 237 | { |
| 238 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 239 | if (sxs < 0 || sxs % stride_w != 0) |
| 240 | continue; |
| 241 | |
| 242 | int sx = sxs / stride_w; |
| 243 | if (sx >= w) |
| 244 | continue; |
| 245 | |
| 246 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * 4; |
| 247 | |
| 248 | float32x4_t _val = vcvt_f32_f16(vld1_f16(sptr)); |
| 249 | |
| 250 | int k = y * kernel_w + x; |
| 251 | |
| 252 | float32x4_t _w0 = vcvt_f32_f16(vld1_f16(kptr + k * 16)); |
| 253 | float32x4_t _w1 = vcvt_f32_f16(vld1_f16(kptr + k * 16 + 4)); |
| 254 | float32x4_t _w2 = vcvt_f32_f16(vld1_f16(kptr + k * 16 + 8)); |
| 255 | float32x4_t _w3 = vcvt_f32_f16(vld1_f16(kptr + k * 16 + 12)); |
| 256 | |
| 257 | _sum = vfmaq_laneq_f32(_sum, _w0, _val, 0); |
| 258 | _sum = vfmaq_laneq_f32(_sum, _w1, _val, 1); |
| 259 | _sum = vfmaq_laneq_f32(_sum, _w2, _val, 2); |
| 260 | _sum = vfmaq_laneq_f32(_sum, _w3, _val, 3); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | kptr += maxk * 16; |
| 265 | } |
| 266 | |
| 267 | _sum = activation_ps(_sum, activation_type, activation_params); |
| 268 | |
| 269 | vst1_f16(outptr + j * 4, vcvt_f16_f32(_sum)); |
| 270 | } |
| 271 | |
| 272 | outptr += outw * 4; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (elempack == 1 && out_elempack == 4) |
| 279 | { |
| 280 | { |
| 281 | // num_output |
| 282 | #pragma omp parallel for num_threads(opt.num_threads) |
| 283 | for (int p = 0; p < num_output / out_elempack; p++) |
| 284 | { |
| 285 | __fp16* outptr = top_blob_bordered.channel(p); |
| 286 | |
| 287 | for (int i = 0; i < outh; i++) |
| 288 | { |
| 289 | for (int j = 0; j < outw; j++) |
| 290 | { |
| 291 | float32x4_t _sum = vdupq_n_f32(0.f); |
| 292 | |
| 293 | if (bias_term) |
| 294 | { |
| 295 | _sum = vld1q_f32(((const float*)bias_data) + p * 4); |
| 296 | } |
| 297 | |
| 298 | const __fp16* kptr = weight_data_tm.channel(p); |
| 299 | |
| 300 | // channels |
| 301 | for (int q = 0; q < channels; q++) |
| 302 | { |
| 303 | const Mat m = bottom_blob.channel(q); |
| 304 | |
| 305 | for (int y = 0; y < kernel_h; y++) |
| 306 | { |
| 307 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 308 | if (sys < 0 || sys % stride_h != 0) |
| 309 | continue; |
| 310 | |
| 311 | int sy = sys / stride_h; |
| 312 | if (sy >= h) |
| 313 | continue; |
| 314 | |
| 315 | const __fp16* sptr = m.row<const __fp16>(sy); |
| 316 | |
| 317 | for (int x = 0; x < kernel_w; x++) |
| 318 | { |
| 319 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 320 | if (sxs < 0 || sxs % stride_w != 0) |
| 321 | continue; |
| 322 | |
| 323 | int sx = sxs / stride_w; |
| 324 | if (sx >= w) |
| 325 | continue; |
| 326 | |
| 327 | float32x4_t _val = vdupq_n_f32((float)sptr[sx]); |
| 328 | |
| 329 | int k = y * kernel_w + x; |
| 330 | |
| 331 | float32x4_t _w = vcvt_f32_f16(vld1_f16(kptr + k * 4)); |
| 332 | |
| 333 | _sum = vfmaq_f32(_sum, _val, _w); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | kptr += maxk * 4; |
| 338 | } |
| 339 | |
| 340 | _sum = activation_ps(_sum, activation_type, activation_params); |
| 341 | |
| 342 | vst1_f16(outptr + j * 4, vcvt_f16_f32(_sum)); |
| 343 | } |
| 344 | |
| 345 | outptr += outw * 4; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | if (elempack == 4 && out_elempack == 1) |
| 352 | { |
| 353 | { |
| 354 | // num_output |
| 355 | #pragma omp parallel for num_threads(opt.num_threads) |
| 356 | for (int p = 0; p < num_output / out_elempack; p++) |
| 357 | { |
| 358 | __fp16* outptr = top_blob_bordered.channel(p); |
| 359 | |
| 360 | for (int i = 0; i < outh; i++) |
| 361 | { |
| 362 | for (int j = 0; j < outw; j++) |
| 363 | { |
| 364 | float sum = 0.f; |
| 365 | |
| 366 | if (bias_term) |
| 367 | { |
| 368 | sum = bias_data[p]; |
| 369 | } |
| 370 | |
| 371 | const __fp16* kptr = weight_data_tm.channel(p); |
| 372 | |
| 373 | // channels |
| 374 | for (int q = 0; q < channels; q++) |
| 375 | { |
| 376 | const Mat m = bottom_blob.channel(q); |
| 377 | |
| 378 | for (int y = 0; y < kernel_h; y++) |
| 379 | { |
| 380 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 381 | if (sys < 0 || sys % stride_h != 0) |
| 382 | continue; |
| 383 | |
| 384 | int sy = sys / stride_h; |
| 385 | if (sy >= h) |
| 386 | continue; |
| 387 | |
| 388 | for (int x = 0; x < kernel_w; x++) |
| 389 | { |
| 390 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 391 | if (sxs < 0 || sxs % stride_w != 0) |
| 392 | continue; |
| 393 | |
| 394 | int sx = sxs / stride_w; |
| 395 | if (sx >= w) |
| 396 | continue; |
| 397 | |
| 398 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * 4; |
| 399 | |
| 400 | float32x4_t _val = vcvt_f32_f16(vld1_f16(sptr)); |
| 401 | |
| 402 | int k = y * kernel_w + x; |
| 403 | |
| 404 | float32x4_t _w = vcvt_f32_f16(vld1_f16(kptr + k * 4)); |
| 405 | |
| 406 | float32x4_t _s4 = vmulq_f32(_val, _w); |
| 407 | |
| 408 | sum += vaddvq_f32(_s4); // dot |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | kptr += maxk * 4; |
| 413 | } |
| 414 | |
| 415 | sum = activation_ss(sum, activation_type, activation_params); |
| 416 | |
| 417 | outptr[j] = (__fp16)sum; |
| 418 | } |
| 419 | |
| 420 | outptr += outw; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | if (elempack == 1 && out_elempack == 1) |
| 427 | { |
| 428 | { |
| 429 | // num_output |
| 430 | #pragma omp parallel for num_threads(opt.num_threads) |
| 431 | for (int p = 0; p < num_output; p++) |
| 432 | { |
| 433 | __fp16* outptr = top_blob_bordered.channel(p); |
| 434 | |
| 435 | for (int i = 0; i < outh; i++) |
| 436 | { |
| 437 | for (int j = 0; j < outw; j++) |
| 438 | { |
| 439 | float sum = 0.f; |
| 440 | |
| 441 | if (bias_term) |
| 442 | { |
| 443 | sum = bias_data[p]; |
| 444 | } |
| 445 | |
| 446 | const __fp16* kptr = weight_data_tm.channel(p); |
| 447 | |
| 448 | // channels |
| 449 | for (int q = 0; q < channels; q++) |
| 450 | { |
| 451 | const Mat m = bottom_blob.channel(q); |
| 452 | |
| 453 | for (int y = 0; y < kernel_h; y++) |
| 454 | { |
| 455 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 456 | if (sys < 0 || sys % stride_h != 0) |
| 457 | continue; |
| 458 | |
| 459 | int sy = sys / stride_h; |
| 460 | if (sy >= h) |
| 461 | continue; |
| 462 | |
| 463 | const __fp16* sptr = m.row<const __fp16>(sy); |
| 464 | |
| 465 | for (int x = 0; x < kernel_w; x++) |
| 466 | { |
| 467 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 468 | if (sxs < 0 || sxs % stride_w != 0) |
| 469 | continue; |
| 470 | |
| 471 | int sx = sxs / stride_w; |
| 472 | if (sx >= w) |
| 473 | continue; |
| 474 | |
| 475 | float val = (float)sptr[sx]; |
| 476 | |
| 477 | int k = y * kernel_w + x; |
| 478 | |
| 479 | float w = (float)kptr[k]; |
| 480 | |
| 481 | sum += val * w; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | kptr += maxk; |
| 486 | } |
| 487 | |
| 488 | sum = activation_ss(sum, activation_type, activation_params); |
| 489 | |
| 490 | outptr[j] = (__fp16)sum; |
| 491 | } |
| 492 | |
| 493 | outptr += outw; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | cut_padding(top_blob_bordered, top_blob, opt); |
| 500 | if (top_blob.empty()) |
| 501 | return -100; |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | int Deconvolution_arm::forward_fp16sa(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 507 | { |
| 508 | // deconvolv with NxN kernel |
| 509 | // value = value + bias |
| 510 | |
| 511 | int w = bottom_blob.w; |
| 512 | int h = bottom_blob.h; |
| 513 | int channels = bottom_blob.c; |
| 514 | size_t elemsize = bottom_blob.elemsize; |
| 515 | int elempack = bottom_blob.elempack; |
| 516 | |
| 517 | // 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); |
| 518 | |
| 519 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 520 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 521 | |
| 522 | int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right; |
| 523 | int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom; |
| 524 | int out_elempack = 1; |
| 525 | if (opt.use_packing_layout) |
| 526 | { |
| 527 | out_elempack = opt.use_fp16_arithmetic && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 528 | } |
| 529 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 530 | |
| 531 | int out_channels = num_output / out_elempack; |
| 532 | |
| 533 | Mat top_blob_bordered; |
| 534 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0)) |
| 535 | { |
| 536 | top_blob_bordered.create(outw, outh, out_channels, out_elemsize, out_elempack, opt.workspace_allocator); |
| 537 | } |
| 538 | else |
| 539 | { |
| 540 | top_blob_bordered = top_blob; |
| 541 | top_blob_bordered.create(outw, outh, out_channels, out_elemsize, out_elempack, opt.blob_allocator); |
| 542 | } |
| 543 | if (top_blob_bordered.empty()) |
| 544 | return -100; |
| 545 | |
| 546 | const int maxk = kernel_w * kernel_h; |
| 547 | |
| 548 | if (opt.use_sgemm_convolution) |
| 549 | { |
| 550 | // sgemm |
| 551 | Mat bottom_blob_2 = bottom_blob; |
| 552 | { |
| 553 | bottom_blob_2.w = bottom_blob.w * bottom_blob.h; |
| 554 | bottom_blob_2.h = 1; |
| 555 | } |
| 556 | Mat top_col2im; |
| 557 | Option opt_b = opt; |
| 558 | opt_b.blob_allocator = top_blob_bordered.allocator; |
| 559 | int ret = gemm->forward(bottom_blob_2, top_col2im, opt_b); |
| 560 | if (ret != 0) |
| 561 | return ret; |
| 562 | |
| 563 | { |
| 564 | // col2im |
| 565 | const int gap = (outw * stride_h - w * stride_w) * out_elempack; |
| 566 | |
| 567 | if (out_elempack == 8) |
| 568 | { |
| 569 | #pragma omp parallel for num_threads(opt.num_threads) |
| 570 | for (int p = 0; p < out_channels; p++) |
| 571 | { |
| 572 | const __fp16* sptr = top_col2im.row<const __fp16>(p * maxk); |
| 573 | Mat outm = top_blob_bordered.channel(p); |
| 574 | |
| 575 | if (bias_data.empty()) |
| 576 | { |
| 577 | outm.fill(vdupq_n_f16(0.f)); |
| 578 | } |
| 579 | else |
| 580 | { |
| 581 | outm.fill(vld1q_f16((const __fp16*)bias_data_fp16 + p * 8)); |
| 582 | } |
| 583 | |
| 584 | for (int u = 0; u < kernel_h; u++) |
| 585 | { |
| 586 | for (int v = 0; v < kernel_w; v++) |
| 587 | { |
| 588 | __fp16* ptr = outm.row<__fp16>(dilation_h * u) + dilation_w * v * 8; |
| 589 | |
| 590 | for (int i = 0; i < h; i++) |
| 591 | { |
| 592 | for (int j = 0; j < w; j++) |
| 593 | { |
| 594 | float16x8_t _val = vld1q_f16(ptr); |
| 595 | float16x8_t _s = vld1q_f16(sptr); |
| 596 | _val = vaddq_f16(_val, _s); |
| 597 | vst1q_f16(ptr, _val); |
| 598 | |
| 599 | ptr += stride_w * 8; |
| 600 | sptr += 8; |
| 601 | } |
| 602 | |
| 603 | ptr += gap; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | if (out_elempack == 4) |
| 611 | { |
| 612 | #pragma omp parallel for num_threads(opt.num_threads) |
| 613 | for (int p = 0; p < out_channels; p++) |
| 614 | { |
| 615 | const __fp16* sptr = top_col2im.row<const __fp16>(p * maxk); |
| 616 | Mat outm = top_blob_bordered.channel(p); |
| 617 | |
| 618 | if (bias_data.empty()) |
| 619 | { |
| 620 | outm.fill(vdup_n_f16(0.f)); |
| 621 | } |
| 622 | else |
| 623 | { |
| 624 | outm.fill(vld1_f16((const __fp16*)bias_data_fp16 + p * 4)); |
| 625 | } |
| 626 | |
| 627 | for (int u = 0; u < kernel_h; u++) |
| 628 | { |
| 629 | for (int v = 0; v < kernel_w; v++) |
| 630 | { |
| 631 | __fp16* ptr = outm.row<__fp16>(dilation_h * u) + dilation_w * v * 4; |
| 632 | |
| 633 | for (int i = 0; i < h; i++) |
| 634 | { |
| 635 | for (int j = 0; j < w; j++) |
| 636 | { |
| 637 | float16x4_t _val = vld1_f16(ptr); |
| 638 | float16x4_t _s = vld1_f16(sptr); |
| 639 | _val = vadd_f16(_val, _s); |
| 640 | vst1_f16(ptr, _val); |
| 641 | |
| 642 | ptr += stride_w * 4; |
| 643 | sptr += 4; |
| 644 | } |
| 645 | |
| 646 | ptr += gap; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | if (out_elempack == 1) |
| 654 | { |
| 655 | #pragma omp parallel for num_threads(opt.num_threads) |
| 656 | for (int p = 0; p < out_channels; p++) |
| 657 | { |
| 658 | const __fp16* sptr = top_col2im.row<const __fp16>(p * maxk); |
| 659 | Mat outm = top_blob_bordered.channel(p); |
| 660 | |
| 661 | const __fp16 bias = bias_data_fp16.empty() ? 0.f : ((const __fp16*)bias_data_fp16)[p]; |
| 662 | outm.fill(bias); |
| 663 | |
| 664 | for (int u = 0; u < kernel_h; u++) |
| 665 | { |
| 666 | for (int v = 0; v < kernel_w; v++) |
| 667 | { |
| 668 | __fp16* ptr = outm.row<__fp16>(dilation_h * u) + dilation_w * v; |
| 669 | |
| 670 | for (int i = 0; i < h; i++) |
| 671 | { |
| 672 | for (int j = 0; j < w; j++) |
| 673 | { |
| 674 | ptr[0] += sptr[0]; |
| 675 | |
| 676 | ptr += stride_w; |
| 677 | sptr += 1; |
| 678 | } |
| 679 | |
| 680 | ptr += gap; |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | if (activation) |
| 689 | { |
| 690 | activation->forward_inplace(top_blob_bordered, opt); |
| 691 | } |
| 692 | } |
| 693 | else |
| 694 | { |
| 695 | if (elempack == 8 && out_elempack == 8) |
| 696 | { |
| 697 | // num_output |
| 698 | #pragma omp parallel for num_threads(opt.num_threads) |
| 699 | for (int p = 0; p < out_channels; p++) |
| 700 | { |
| 701 | __fp16* outptr = top_blob_bordered.channel(p); |
| 702 | |
| 703 | for (int i = 0; i < outh; i++) |
| 704 | { |
| 705 | for (int j = 0; j < outw; j++) |
| 706 | { |
| 707 | float16x8_t _sum = vdupq_n_f16((__fp16)0.f); |
| 708 | |
| 709 | if (bias_term) |
| 710 | { |
| 711 | _sum = vld1q_f16((const __fp16*)bias_data_fp16 + p * 8); |
| 712 | } |
| 713 | |
| 714 | const __fp16* kptr = weight_data_tm.channel(p); |
| 715 | |
| 716 | // channels |
| 717 | for (int q = 0; q < channels; q++) |
| 718 | { |
| 719 | const Mat m = bottom_blob.channel(q); |
| 720 | |
| 721 | for (int y = 0; y < kernel_h; y++) |
| 722 | { |
| 723 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 724 | if (sys < 0 || sys % stride_h != 0) |
| 725 | continue; |
| 726 | |
| 727 | int sy = sys / stride_h; |
| 728 | if (sy >= h) |
| 729 | continue; |
| 730 | |
| 731 | for (int x = 0; x < kernel_w; x++) |
| 732 | { |
| 733 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 734 | if (sxs < 0 || sxs % stride_w != 0) |
| 735 | continue; |
| 736 | |
| 737 | int sx = sxs / stride_w; |
| 738 | if (sx >= w) |
| 739 | continue; |
| 740 | |
| 741 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * 8; |
| 742 | |
| 743 | float16x8_t _val = vld1q_f16(sptr); |
| 744 | |
| 745 | int k = y * kernel_w + x; |
| 746 | |
| 747 | float16x8_t _w0 = vld1q_f16(kptr + k * 64); |
| 748 | float16x8_t _w1 = vld1q_f16(kptr + k * 64 + 8); |
| 749 | float16x8_t _w2 = vld1q_f16(kptr + k * 64 + 16); |
| 750 | float16x8_t _w3 = vld1q_f16(kptr + k * 64 + 24); |
| 751 | float16x8_t _w4 = vld1q_f16(kptr + k * 64 + 32); |
| 752 | float16x8_t _w5 = vld1q_f16(kptr + k * 64 + 40); |
| 753 | float16x8_t _w6 = vld1q_f16(kptr + k * 64 + 48); |
| 754 | float16x8_t _w7 = vld1q_f16(kptr + k * 64 + 56); |
| 755 | |
| 756 | _sum = vfmaq_laneq_f16(_sum, _w0, _val, 0); |
| 757 | _sum = vfmaq_laneq_f16(_sum, _w1, _val, 1); |
| 758 | _sum = vfmaq_laneq_f16(_sum, _w2, _val, 2); |
| 759 | _sum = vfmaq_laneq_f16(_sum, _w3, _val, 3); |
| 760 | _sum = vfmaq_laneq_f16(_sum, _w4, _val, 4); |
| 761 | _sum = vfmaq_laneq_f16(_sum, _w5, _val, 5); |
| 762 | _sum = vfmaq_laneq_f16(_sum, _w6, _val, 6); |
| 763 | _sum = vfmaq_laneq_f16(_sum, _w7, _val, 7); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | kptr += maxk * 64; |
| 768 | } |
| 769 | |
| 770 | _sum = activation_ps_f16(_sum, activation_type, activation_params); |
| 771 | |
| 772 | vst1q_f16(outptr + j * 8, _sum); |
| 773 | } |
| 774 | |
| 775 | outptr += outw * 8; |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | if (elempack == 1 && out_elempack == 8) |
| 781 | { |
| 782 | // num_output |
| 783 | #pragma omp parallel for num_threads(opt.num_threads) |
| 784 | for (int p = 0; p < out_channels; p++) |
| 785 | { |
| 786 | __fp16* outptr = top_blob_bordered.channel(p); |
| 787 | |
| 788 | for (int i = 0; i < outh; i++) |
| 789 | { |
| 790 | for (int j = 0; j < outw; j++) |
| 791 | { |
| 792 | float16x8_t _sum = vdupq_n_f16((__fp16)0.f); |
| 793 | |
| 794 | if (bias_term) |
| 795 | { |
| 796 | _sum = vld1q_f16((const __fp16*)bias_data_fp16 + p * 8); |
| 797 | } |
| 798 | |
| 799 | const __fp16* kptr = weight_data_tm.channel(p); |
| 800 | |
| 801 | // channels |
| 802 | for (int q = 0; q < channels; q++) |
| 803 | { |
| 804 | const Mat m = bottom_blob.channel(q); |
| 805 | |
| 806 | for (int y = 0; y < kernel_h; y++) |
| 807 | { |
| 808 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 809 | if (sys < 0 || sys % stride_h != 0) |
| 810 | continue; |
| 811 | |
| 812 | int sy = sys / stride_h; |
| 813 | if (sy >= h) |
| 814 | continue; |
| 815 | |
| 816 | const __fp16* sptr = m.row<const __fp16>(sy); |
| 817 | |
| 818 | for (int x = 0; x < kernel_w; x++) |
| 819 | { |
| 820 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 821 | if (sxs < 0 || sxs % stride_w != 0) |
| 822 | continue; |
| 823 | |
| 824 | int sx = sxs / stride_w; |
| 825 | if (sx >= w) |
| 826 | continue; |
| 827 | |
| 828 | float16x8_t _val = vdupq_n_f16(sptr[sx]); |
| 829 | |
| 830 | int k = y * kernel_w + x; |
| 831 | |
| 832 | float16x8_t _w = vld1q_f16(kptr + k * 8); |
| 833 | |
| 834 | _sum = vfmaq_f16(_sum, _val, _w); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | kptr += maxk * 8; |
| 839 | } |
| 840 | |
| 841 | _sum = activation_ps_f16(_sum, activation_type, activation_params); |
| 842 | |
| 843 | vst1q_f16(outptr + j * 8, _sum); |
| 844 | } |
| 845 | |
| 846 | outptr += outw * 8; |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | if (elempack == 4 && out_elempack == 8) |
| 852 | { |
| 853 | // num_output |
| 854 | #pragma omp parallel for num_threads(opt.num_threads) |
| 855 | for (int p = 0; p < out_channels; p++) |
| 856 | { |
| 857 | __fp16* outptr = top_blob_bordered.channel(p); |
| 858 | |
| 859 | for (int i = 0; i < outh; i++) |
| 860 | { |
| 861 | for (int j = 0; j < outw; j++) |
| 862 | { |
| 863 | float16x8_t _sum = vdupq_n_f16((__fp16)0.f); |
| 864 | |
| 865 | if (bias_term) |
| 866 | { |
| 867 | _sum = vld1q_f16((const __fp16*)bias_data_fp16 + p * 8); |
| 868 | } |
| 869 | |
| 870 | const __fp16* kptr = weight_data_tm.channel(p); |
| 871 | |
| 872 | // channels |
| 873 | for (int q = 0; q < channels; q++) |
| 874 | { |
| 875 | const Mat m = bottom_blob.channel(q); |
| 876 | |
| 877 | for (int y = 0; y < kernel_h; y++) |
| 878 | { |
| 879 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 880 | if (sys < 0 || sys % stride_h != 0) |
| 881 | continue; |
| 882 | |
| 883 | int sy = sys / stride_h; |
| 884 | if (sy >= h) |
| 885 | continue; |
| 886 | |
| 887 | for (int x = 0; x < kernel_w; x++) |
| 888 | { |
| 889 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 890 | if (sxs < 0 || sxs % stride_w != 0) |
| 891 | continue; |
| 892 | |
| 893 | int sx = sxs / stride_w; |
| 894 | if (sx >= w) |
| 895 | continue; |
| 896 | |
| 897 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * 4; |
| 898 | |
| 899 | float16x4_t _val = vld1_f16(sptr); |
| 900 | |
| 901 | int k = y * kernel_w + x; |
| 902 | |
| 903 | float16x8_t _w0 = vld1q_f16(kptr + k * 32); |
| 904 | float16x8_t _w1 = vld1q_f16(kptr + k * 32 + 8); |
| 905 | float16x8_t _w2 = vld1q_f16(kptr + k * 32 + 16); |
| 906 | float16x8_t _w3 = vld1q_f16(kptr + k * 32 + 24); |
| 907 | |
| 908 | _sum = vfmaq_lane_f16(_sum, _w0, _val, 0); |
| 909 | _sum = vfmaq_lane_f16(_sum, _w1, _val, 1); |
| 910 | _sum = vfmaq_lane_f16(_sum, _w2, _val, 2); |
| 911 | _sum = vfmaq_lane_f16(_sum, _w3, _val, 3); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | kptr += maxk * 32; |
| 916 | } |
| 917 | |
| 918 | _sum = activation_ps_f16(_sum, activation_type, activation_params); |
| 919 | |
| 920 | vst1q_f16(outptr + j * 8, _sum); |
| 921 | } |
| 922 | |
| 923 | outptr += outw * 8; |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | if (elempack == 8 && out_elempack == 1) |
| 929 | { |
| 930 | // num_output |
| 931 | #pragma omp parallel for num_threads(opt.num_threads) |
| 932 | for (int p = 0; p < out_channels; p++) |
| 933 | { |
| 934 | __fp16* outptr = top_blob_bordered.channel(p); |
| 935 | |
| 936 | for (int i = 0; i < outh; i++) |
| 937 | { |
| 938 | for (int j = 0; j < outw; j++) |
| 939 | { |
| 940 | float sum = 0.f; |
| 941 | |
| 942 | if (bias_term) |
| 943 | { |
| 944 | sum = bias_data[p]; |
| 945 | } |
| 946 | |
| 947 | const __fp16* kptr = weight_data_tm.channel(p); |
| 948 | |
| 949 | // channels |
| 950 | for (int q = 0; q < channels; q++) |
| 951 | { |
| 952 | const Mat m = bottom_blob.channel(q); |
| 953 | |
| 954 | for (int y = 0; y < kernel_h; y++) |
| 955 | { |
| 956 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 957 | if (sys < 0 || sys % stride_h != 0) |
| 958 | continue; |
| 959 | |
| 960 | int sy = sys / stride_h; |
| 961 | if (sy >= h) |
| 962 | continue; |
| 963 | |
| 964 | for (int x = 0; x < kernel_w; x++) |
| 965 | { |
| 966 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 967 | if (sxs < 0 || sxs % stride_w != 0) |
| 968 | continue; |
| 969 | |
| 970 | int sx = sxs / stride_w; |
| 971 | if (sx >= w) |
| 972 | continue; |
| 973 | |
| 974 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * 8; |
| 975 | |
| 976 | float16x8_t _val = vld1q_f16(sptr); |
| 977 | |
| 978 | int k = y * kernel_w + x; |
| 979 | |
| 980 | float16x8_t _w = vld1q_f16(kptr + k * 8); |
| 981 | |
| 982 | float16x8_t _s8 = vmulq_f16(_val, _w); |
| 983 | |
| 984 | float16x4_t _s4 = vadd_f16(vget_low_f16(_s8), vget_high_f16(_s8)); |
| 985 | sum += vaddvq_f32(vcvt_f32_f16(_s4)); // dot |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | kptr += maxk * 8; |
| 990 | } |
| 991 | |
| 992 | sum = activation_ss_f16(sum, activation_type, activation_params); |
| 993 | |
| 994 | outptr[j] = (__fp16)sum; |
| 995 | } |
| 996 | |
| 997 | outptr += outw; |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | if (elempack == 8 && out_elempack == 4) |
| 1003 | { |
| 1004 | // num_output |
| 1005 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1006 | for (int p = 0; p < out_channels; p++) |
| 1007 | { |
| 1008 | __fp16* outptr = top_blob_bordered.channel(p); |
| 1009 | |
| 1010 | for (int i = 0; i < outh; i++) |
| 1011 | { |
| 1012 | for (int j = 0; j < outw; j++) |
| 1013 | { |
| 1014 | float16x4_t _sum = vdup_n_f16((__fp16)0.f); |
| 1015 | |
| 1016 | if (bias_term) |
| 1017 | { |
| 1018 | _sum = vld1_f16((const __fp16*)bias_data_fp16 + p * 4); |
| 1019 | } |
| 1020 | |
| 1021 | const __fp16* kptr = weight_data_tm.channel(p); |
| 1022 | |
| 1023 | // channels |
| 1024 | for (int q = 0; q < channels; q++) |
| 1025 | { |
| 1026 | const Mat m = bottom_blob.channel(q); |
| 1027 | |
| 1028 | for (int y = 0; y < kernel_h; y++) |
| 1029 | { |
| 1030 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 1031 | if (sys < 0 || sys % stride_h != 0) |
| 1032 | continue; |
| 1033 | |
| 1034 | int sy = sys / stride_h; |
| 1035 | if (sy >= h) |
| 1036 | continue; |
| 1037 | |
| 1038 | for (int x = 0; x < kernel_w; x++) |
| 1039 | { |
| 1040 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 1041 | if (sxs < 0 || sxs % stride_w != 0) |
| 1042 | continue; |
| 1043 | |
| 1044 | int sx = sxs / stride_w; |
| 1045 | if (sx >= w) |
| 1046 | continue; |
| 1047 | |
| 1048 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * 8; |
| 1049 | |
| 1050 | float16x8_t _val = vld1q_f16(sptr); |
| 1051 | |
| 1052 | int k = y * kernel_w + x; |
| 1053 | |
| 1054 | float16x4_t _w0 = vld1_f16(kptr + k * 32); |
| 1055 | float16x4_t _w1 = vld1_f16(kptr + k * 32 + 4); |
| 1056 | float16x4_t _w2 = vld1_f16(kptr + k * 32 + 8); |
| 1057 | float16x4_t _w3 = vld1_f16(kptr + k * 32 + 12); |
| 1058 | float16x4_t _w4 = vld1_f16(kptr + k * 32 + 16); |
| 1059 | float16x4_t _w5 = vld1_f16(kptr + k * 32 + 20); |
| 1060 | float16x4_t _w6 = vld1_f16(kptr + k * 32 + 24); |
| 1061 | float16x4_t _w7 = vld1_f16(kptr + k * 32 + 28); |
| 1062 | |
| 1063 | _sum = vfma_laneq_f16(_sum, _w0, _val, 0); |
| 1064 | _sum = vfma_laneq_f16(_sum, _w1, _val, 1); |
| 1065 | _sum = vfma_laneq_f16(_sum, _w2, _val, 2); |
| 1066 | _sum = vfma_laneq_f16(_sum, _w3, _val, 3); |
| 1067 | _sum = vfma_laneq_f16(_sum, _w4, _val, 4); |
| 1068 | _sum = vfma_laneq_f16(_sum, _w5, _val, 5); |
| 1069 | _sum = vfma_laneq_f16(_sum, _w6, _val, 6); |
| 1070 | _sum = vfma_laneq_f16(_sum, _w7, _val, 7); |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | kptr += maxk * 32; |
| 1075 | } |
| 1076 | |
| 1077 | _sum = activation_ps_f16(_sum, activation_type, activation_params); |
| 1078 | |
| 1079 | vst1_f16(outptr + j * 4, _sum); |
| 1080 | } |
| 1081 | |
| 1082 | outptr += outw * 4; |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | if (elempack == 4 && out_elempack == 4) |
| 1088 | { |
| 1089 | // num_output |
| 1090 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1091 | for (int p = 0; p < out_channels; p++) |
| 1092 | { |
| 1093 | __fp16* outptr = top_blob_bordered.channel(p); |
| 1094 | |
| 1095 | for (int i = 0; i < outh; i++) |
| 1096 | { |
| 1097 | for (int j = 0; j < outw; j++) |
| 1098 | { |
| 1099 | float16x4_t _sum = vdup_n_f16((__fp16)0.f); |
| 1100 | |
| 1101 | if (bias_term) |
| 1102 | { |
| 1103 | _sum = vld1_f16((const __fp16*)bias_data_fp16 + p * 4); |
| 1104 | } |
| 1105 | |
| 1106 | const __fp16* kptr = weight_data_tm.channel(p); |
| 1107 | |
| 1108 | // channels |
| 1109 | for (int q = 0; q < channels; q++) |
| 1110 | { |
| 1111 | const Mat m = bottom_blob.channel(q); |
| 1112 | |
| 1113 | for (int y = 0; y < kernel_h; y++) |
| 1114 | { |
| 1115 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 1116 | if (sys < 0 || sys % stride_h != 0) |
| 1117 | continue; |
| 1118 | |
| 1119 | int sy = sys / stride_h; |
| 1120 | if (sy >= h) |
| 1121 | continue; |
| 1122 | |
| 1123 | for (int x = 0; x < kernel_w; x++) |
| 1124 | { |
| 1125 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 1126 | if (sxs < 0 || sxs % stride_w != 0) |
| 1127 | continue; |
| 1128 | |
| 1129 | int sx = sxs / stride_w; |
| 1130 | if (sx >= w) |
| 1131 | continue; |
| 1132 | |
| 1133 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * 4; |
| 1134 | |
| 1135 | float16x4_t _val = vld1_f16(sptr); |
| 1136 | |
| 1137 | int k = y * kernel_w + x; |
| 1138 | |
| 1139 | float16x4_t _w0 = vld1_f16(kptr + k * 16); |
| 1140 | float16x4_t _w1 = vld1_f16(kptr + k * 16 + 4); |
| 1141 | float16x4_t _w2 = vld1_f16(kptr + k * 16 + 8); |
| 1142 | float16x4_t _w3 = vld1_f16(kptr + k * 16 + 12); |
| 1143 | |
| 1144 | _sum = vfma_lane_f16(_sum, _w0, _val, 0); |
| 1145 | _sum = vfma_lane_f16(_sum, _w1, _val, 1); |
| 1146 | _sum = vfma_lane_f16(_sum, _w2, _val, 2); |
| 1147 | _sum = vfma_lane_f16(_sum, _w3, _val, 3); |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | kptr += maxk * 16; |
| 1152 | } |
| 1153 | |
| 1154 | _sum = activation_ps_f16(_sum, activation_type, activation_params); |
| 1155 | |
| 1156 | vst1_f16(outptr + j * 4, _sum); |
| 1157 | } |
| 1158 | |
| 1159 | outptr += outw * 4; |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | if (elempack == 1 && out_elempack == 4) |
| 1165 | { |
| 1166 | // num_output |
| 1167 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1168 | for (int p = 0; p < out_channels; p++) |
| 1169 | { |
| 1170 | __fp16* outptr = top_blob_bordered.channel(p); |
| 1171 | |
| 1172 | for (int i = 0; i < outh; i++) |
| 1173 | { |
| 1174 | for (int j = 0; j < outw; j++) |
| 1175 | { |
| 1176 | float16x4_t _sum = vdup_n_f16((__fp16)0.f); |
| 1177 | |
| 1178 | if (bias_term) |
| 1179 | { |
| 1180 | _sum = vld1_f16((const __fp16*)bias_data_fp16 + p * 4); |
| 1181 | } |
| 1182 | |
| 1183 | const __fp16* kptr = weight_data_tm.channel(p); |
| 1184 | |
| 1185 | // channels |
| 1186 | for (int q = 0; q < channels; q++) |
| 1187 | { |
| 1188 | const Mat m = bottom_blob.channel(q); |
| 1189 | |
| 1190 | for (int y = 0; y < kernel_h; y++) |
| 1191 | { |
| 1192 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 1193 | if (sys < 0 || sys % stride_h != 0) |
| 1194 | continue; |
| 1195 | |
| 1196 | int sy = sys / stride_h; |
| 1197 | if (sy >= h) |
| 1198 | continue; |
| 1199 | |
| 1200 | const __fp16* sptr = m.row<const __fp16>(sy); |
| 1201 | |
| 1202 | for (int x = 0; x < kernel_w; x++) |
| 1203 | { |
| 1204 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 1205 | if (sxs < 0 || sxs % stride_w != 0) |
| 1206 | continue; |
| 1207 | |
| 1208 | int sx = sxs / stride_w; |
| 1209 | if (sx >= w) |
| 1210 | continue; |
| 1211 | |
| 1212 | float16x4_t _val = vdup_n_f16(sptr[sx]); |
| 1213 | |
| 1214 | int k = y * kernel_w + x; |
| 1215 | |
| 1216 | float16x4_t _w = vld1_f16(kptr + k * 4); |
| 1217 | |
| 1218 | _sum = vfma_f16(_sum, _val, _w); |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | kptr += maxk * 4; |
| 1223 | } |
| 1224 | |
| 1225 | _sum = activation_ps_f16(_sum, activation_type, activation_params); |
| 1226 | |
| 1227 | vst1_f16(outptr + j * 4, _sum); |
| 1228 | } |
| 1229 | |
| 1230 | outptr += outw * 4; |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | if (elempack == 4 && out_elempack == 1) |
| 1236 | { |
| 1237 | // num_output |
| 1238 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1239 | for (int p = 0; p < out_channels; p++) |
| 1240 | { |
| 1241 | __fp16* outptr = top_blob_bordered.channel(p); |
| 1242 | |
| 1243 | for (int i = 0; i < outh; i++) |
| 1244 | { |
| 1245 | for (int j = 0; j < outw; j++) |
| 1246 | { |
| 1247 | float sum = 0.f; |
| 1248 | |
| 1249 | if (bias_term) |
| 1250 | { |
| 1251 | sum = bias_data[p]; |
| 1252 | } |
| 1253 | |
| 1254 | const __fp16* kptr = weight_data_tm.channel(p); |
| 1255 | |
| 1256 | // channels |
| 1257 | for (int q = 0; q < channels; q++) |
| 1258 | { |
| 1259 | const Mat m = bottom_blob.channel(q); |
| 1260 | |
| 1261 | for (int y = 0; y < kernel_h; y++) |
| 1262 | { |
| 1263 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 1264 | if (sys < 0 || sys % stride_h != 0) |
| 1265 | continue; |
| 1266 | |
| 1267 | int sy = sys / stride_h; |
| 1268 | if (sy >= h) |
| 1269 | continue; |
| 1270 | |
| 1271 | for (int x = 0; x < kernel_w; x++) |
| 1272 | { |
| 1273 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 1274 | if (sxs < 0 || sxs % stride_w != 0) |
| 1275 | continue; |
| 1276 | |
| 1277 | int sx = sxs / stride_w; |
| 1278 | if (sx >= w) |
| 1279 | continue; |
| 1280 | |
| 1281 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * 4; |
| 1282 | |
| 1283 | float16x4_t _val = vld1_f16(sptr); |
| 1284 | |
| 1285 | int k = y * kernel_w + x; |
| 1286 | |
| 1287 | float16x4_t _w = vld1_f16(kptr + k * 4); |
| 1288 | |
| 1289 | float16x4_t _s4 = vmul_f16(_val, _w); |
| 1290 | |
| 1291 | sum += vaddvq_f32(vcvt_f32_f16(_s4)); // dot |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | kptr += maxk * 4; |
| 1296 | } |
| 1297 | |
| 1298 | sum = activation_ss_f16(sum, activation_type, activation_params); |
| 1299 | |
| 1300 | outptr[j] = (__fp16)sum; |
| 1301 | } |
| 1302 | |
| 1303 | outptr += outw; |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | if (elempack == 1 && out_elempack == 1) |
| 1309 | { |
| 1310 | if (kernel_w == 4 && kernel_h == 4 && stride_w == 2 && stride_h == 2 && dilation_w == 1 && dilation_h == 1) |
| 1311 | { |
| 1312 | deconv4x4s2_fp16sa_neon(bottom_blob, top_blob_bordered, weight_data_tm, bias_data_fp16, opt); |
| 1313 | |
| 1314 | if (activation) |
| 1315 | { |
| 1316 | activation->forward_inplace(top_blob_bordered, opt); |
| 1317 | } |
| 1318 | } |
| 1319 | else |
| 1320 | { |
| 1321 | // num_output |
| 1322 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1323 | for (int p = 0; p < num_output; p++) |
| 1324 | { |
| 1325 | __fp16* outptr = top_blob_bordered.channel(p); |
| 1326 | |
| 1327 | for (int i = 0; i < outh; i++) |
| 1328 | { |
| 1329 | for (int j = 0; j < outw; j++) |
| 1330 | { |
| 1331 | float sum = 0.f; |
| 1332 | |
| 1333 | if (bias_term) |
| 1334 | { |
| 1335 | sum = bias_data[p]; |
| 1336 | } |
| 1337 | |
| 1338 | const __fp16* kptr = weight_data_tm.channel(p); |
| 1339 | |
| 1340 | // channels |
| 1341 | for (int q = 0; q < channels; q++) |
| 1342 | { |
| 1343 | const Mat m = bottom_blob.channel(q); |
| 1344 | |
| 1345 | for (int y = 0; y < kernel_h; y++) |
| 1346 | { |
| 1347 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 1348 | if (sys < 0 || sys % stride_h != 0) |
| 1349 | continue; |
| 1350 | |
| 1351 | int sy = sys / stride_h; |
| 1352 | if (sy >= h) |
| 1353 | continue; |
| 1354 | |
| 1355 | const __fp16* sptr = m.row<const __fp16>(sy); |
| 1356 | |
| 1357 | for (int x = 0; x < kernel_w; x++) |
| 1358 | { |
| 1359 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 1360 | if (sxs < 0 || sxs % stride_w != 0) |
| 1361 | continue; |
| 1362 | |
| 1363 | int sx = sxs / stride_w; |
| 1364 | if (sx >= w) |
| 1365 | continue; |
| 1366 | |
| 1367 | __fp16 val = sptr[sx]; |
| 1368 | |
| 1369 | int k = y * kernel_w + x; |
| 1370 | |
| 1371 | __fp16 w = kptr[k]; |
| 1372 | |
| 1373 | sum += val * w; |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | kptr += maxk; |
| 1378 | } |
| 1379 | |
| 1380 | sum = activation_ss_f16(sum, activation_type, activation_params); |
| 1381 | |
| 1382 | outptr[j] = (__fp16)sum; |
| 1383 | } |
| 1384 | |
| 1385 | outptr += outw; |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | cut_padding(top_blob_bordered, top_blob, opt); |
| 1393 | if (top_blob.empty()) |
| 1394 | return -100; |
| 1395 | |
| 1396 | return 0; |
| 1397 | } |
| 1398 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 1399 | |
| 1400 | } // namespace ncnn |