| 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 "deconvolutiondepthwise_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 | int DeconvolutionDepthWise_arm::create_pipeline_fp16s(const Option& opt) |
| 27 | { |
| 28 | // create Deconvolution op for each group |
| 29 | const int maxk = kernel_w * kernel_h; |
| 30 | int channels = (weight_data_size / group) / maxk / (num_output / group) * group; |
| 31 | |
| 32 | // depth-wise |
| 33 | if (channels == group && group == num_output) |
| 34 | { |
| 35 | Mat weight_data_transposed(weight_data.w); |
| 36 | { |
| 37 | float* pt = weight_data_transposed; |
| 38 | const float* p = weight_data; |
| 39 | |
| 40 | for (int i = 0; i < (channels / group) * (num_output / group) * group; i++) |
| 41 | { |
| 42 | for (int k = 0; k < maxk; k++) |
| 43 | { |
| 44 | pt[maxk - 1 - k] = p[k]; |
| 45 | } |
| 46 | |
| 47 | p += maxk; |
| 48 | pt += maxk; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | int elempack = 1; |
| 53 | if (opt.use_packing_layout) |
| 54 | { |
| 55 | elempack = opt.use_fp16_arithmetic && channels % 8 == 0 ? 8 : channels % 4 == 0 ? 4 : 1; |
| 56 | } |
| 57 | |
| 58 | if (elempack == 8) |
| 59 | { |
| 60 | Mat weight_data_r2 = weight_data_transposed.reshape(maxk, group); |
| 61 | Mat weight_data_r2_packed; |
| 62 | convert_packing(weight_data_r2, weight_data_r2_packed, 8, opt); |
| 63 | |
| 64 | ncnn::cast_float32_to_float16(weight_data_r2_packed, weight_data_tm, opt); |
| 65 | } |
| 66 | |
| 67 | if (elempack == 4) |
| 68 | { |
| 69 | Mat weight_data_r2 = weight_data_transposed.reshape(maxk, group); |
| 70 | Mat weight_data_r2_packed; |
| 71 | convert_packing(weight_data_r2, weight_data_r2_packed, 4, opt); |
| 72 | |
| 73 | ncnn::cast_float32_to_float16(weight_data_r2_packed, weight_data_tm, opt); |
| 74 | } |
| 75 | |
| 76 | if (elempack == 1) |
| 77 | { |
| 78 | ncnn::cast_float32_to_float16(weight_data_transposed, weight_data_tm, opt); |
| 79 | } |
| 80 | |
| 81 | ncnn::cast_float32_to_float16(bias_data, bias_data_fp16, opt); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | // group deconvolution |
| 86 | for (int i = 0; i < (int)group_ops.size(); i++) |
| 87 | delete group_ops[i]; |
| 88 | |
| 89 | group_ops.clear(); |
| 90 | |
| 91 | const int channels_g = channels / group; |
| 92 | const int num_output_g = num_output / group; |
| 93 | |
| 94 | group_ops.resize(group); |
| 95 | |
| 96 | for (int g = 0; g < group; g++) |
| 97 | { |
| 98 | Mat weight_data_g = weight_data.range(maxk * channels_g * num_output_g * g, maxk * channels_g * num_output_g).clone(); |
| 99 | Mat bias_data_g; |
| 100 | if (bias_term) |
| 101 | bias_data_g = bias_data.range(num_output_g * g, num_output_g); |
| 102 | |
| 103 | ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Deconvolution); |
| 104 | |
| 105 | // set param |
| 106 | ncnn::ParamDict pd; |
| 107 | pd.set(0, num_output_g); // num_output |
| 108 | pd.set(1, kernel_w); |
| 109 | pd.set(11, kernel_h); |
| 110 | pd.set(2, dilation_w); |
| 111 | pd.set(12, dilation_h); |
| 112 | pd.set(3, stride_w); |
| 113 | pd.set(13, stride_h); |
| 114 | pd.set(4, 0); // pad_w |
| 115 | pd.set(14, 0); // pad_h |
| 116 | pd.set(18, output_pad_right); |
| 117 | pd.set(19, output_pad_bottom); |
| 118 | pd.set(5, bias_term); |
| 119 | pd.set(6, maxk * channels_g * num_output_g); // weight_data_size |
| 120 | pd.set(9, activation_type); |
| 121 | pd.set(10, activation_params); |
| 122 | |
| 123 | op->load_param(pd); |
| 124 | |
| 125 | // set weights |
| 126 | if (bias_term) |
| 127 | { |
| 128 | ncnn::Mat weights[2]; |
| 129 | weights[0] = weight_data_g; |
| 130 | weights[1] = bias_data_g; |
| 131 | |
| 132 | op->load_model(ModelBinFromMatArray(weights)); |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | ncnn::Mat weights[1]; |
| 137 | weights[0] = weight_data_g; |
| 138 | |
| 139 | op->load_model(ModelBinFromMatArray(weights)); |
| 140 | } |
| 141 | |
| 142 | op->create_pipeline(opt); |
| 143 | |
| 144 | group_ops[g] = op; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (opt.lightmode) |
| 149 | weight_data.release(); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | int DeconvolutionDepthWise_arm::forward_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 155 | { |
| 156 | int w = bottom_blob.w; |
| 157 | int h = bottom_blob.h; |
| 158 | int channels = bottom_blob.c; |
| 159 | size_t elemsize = bottom_blob.elemsize; |
| 160 | int elempack = bottom_blob.elempack; |
| 161 | |
| 162 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 163 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 164 | |
| 165 | int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right; |
| 166 | int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom; |
| 167 | int out_elempack = (opt.use_packing_layout && num_output % 4 == 0) ? 4 : 1; |
| 168 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 169 | |
| 170 | Mat top_blob_bordered; |
| 171 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0)) |
| 172 | { |
| 173 | top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.workspace_allocator); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | top_blob_bordered = top_blob; |
| 178 | top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 179 | } |
| 180 | if (top_blob_bordered.empty()) |
| 181 | return -100; |
| 182 | |
| 183 | const int maxk = kernel_w * kernel_h; |
| 184 | |
| 185 | // depth-wise |
| 186 | if (channels * elempack == group && group == num_output) |
| 187 | { |
| 188 | if (elempack == 4) |
| 189 | { |
| 190 | { |
| 191 | #pragma omp parallel for num_threads(opt.num_threads) |
| 192 | for (int g = 0; g < channels; g++) |
| 193 | { |
| 194 | __fp16* outptr = top_blob_bordered.channel(g); |
| 195 | const __fp16* kptr = (const __fp16*)weight_data_tm + maxk * g * 4; |
| 196 | const Mat m = bottom_blob.channel(g); |
| 197 | |
| 198 | for (int i = 0; i < outh; i++) |
| 199 | { |
| 200 | for (int j = 0; j < outw; j++) |
| 201 | { |
| 202 | float32x4_t _sum = vdupq_n_f32(0.f); |
| 203 | |
| 204 | if (bias_term) |
| 205 | { |
| 206 | _sum = vld1q_f32((const float*)bias_data + g * 4); |
| 207 | } |
| 208 | |
| 209 | for (int y = 0; y < kernel_h; y++) |
| 210 | { |
| 211 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 212 | if (sys < 0 || sys % stride_h != 0) |
| 213 | continue; |
| 214 | |
| 215 | int sy = sys / stride_h; |
| 216 | if (sy >= h) |
| 217 | continue; |
| 218 | |
| 219 | for (int x = 0; x < kernel_w; x++) |
| 220 | { |
| 221 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 222 | if (sxs < 0 || sxs % stride_w != 0) |
| 223 | continue; |
| 224 | |
| 225 | int sx = sxs / stride_w; |
| 226 | if (sx >= w) |
| 227 | continue; |
| 228 | |
| 229 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * 4; |
| 230 | |
| 231 | float32x4_t _val = vcvt_f32_f16(vld1_f16(sptr)); |
| 232 | |
| 233 | int k = y * kernel_w + x; |
| 234 | |
| 235 | float32x4_t _w = vcvt_f32_f16(vld1_f16(kptr + k * 4)); |
| 236 | |
| 237 | _sum = vfmaq_f32(_sum, _val, _w); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | _sum = activation_ps(_sum, activation_type, activation_params); |
| 242 | |
| 243 | vst1_f16(outptr + j * 4, vcvt_f16_f32(_sum)); |
| 244 | } |
| 245 | |
| 246 | outptr += outw * 4; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if (elempack == 1) |
| 253 | { |
| 254 | { |
| 255 | #pragma omp parallel for num_threads(opt.num_threads) |
| 256 | for (int g = 0; g < channels; g++) |
| 257 | { |
| 258 | __fp16* outptr = top_blob_bordered.channel(g); |
| 259 | const __fp16* kptr = (const __fp16*)weight_data_tm + maxk * g; |
| 260 | const Mat m = bottom_blob.channel(g); |
| 261 | |
| 262 | for (int i = 0; i < outh; i++) |
| 263 | { |
| 264 | for (int j = 0; j < outw; j++) |
| 265 | { |
| 266 | float sum = 0.f; |
| 267 | |
| 268 | if (bias_term) |
| 269 | { |
| 270 | sum = bias_data[g]; |
| 271 | } |
| 272 | |
| 273 | for (int y = 0; y < kernel_h; y++) |
| 274 | { |
| 275 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 276 | if (sys < 0 || sys % stride_h != 0) |
| 277 | continue; |
| 278 | |
| 279 | int sy = sys / stride_h; |
| 280 | if (sy >= h) |
| 281 | continue; |
| 282 | |
| 283 | const __fp16* sptr = m.row<const __fp16>(sy); |
| 284 | |
| 285 | for (int x = 0; x < kernel_w; x++) |
| 286 | { |
| 287 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 288 | if (sxs < 0 || sxs % stride_w != 0) |
| 289 | continue; |
| 290 | |
| 291 | int sx = sxs / stride_w; |
| 292 | if (sx >= w) |
| 293 | continue; |
| 294 | |
| 295 | float val = (float)sptr[sx]; |
| 296 | |
| 297 | int k = y * kernel_w + x; |
| 298 | |
| 299 | float w = (float)kptr[k]; |
| 300 | |
| 301 | sum += val * w; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | sum = activation_ss(sum, activation_type, activation_params); |
| 306 | |
| 307 | outptr[j] = (__fp16)sum; |
| 308 | } |
| 309 | |
| 310 | outptr += outw; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | // group deconvolution |
| 319 | const int channels_g = channels * elempack / group; |
| 320 | const int num_output_g = num_output / group; |
| 321 | |
| 322 | int g_elempack = (opt.use_packing_layout && channels_g % 4 == 0) ? 4 : 1; |
| 323 | int out_g_elempack = (opt.use_packing_layout && num_output_g % 4 == 0) ? 4 : 1; |
| 324 | |
| 325 | // unpacking |
| 326 | Mat bottom_blob_unpacked = bottom_blob; |
| 327 | if (elempack == 4 && g_elempack == 1) |
| 328 | { |
| 329 | Option opt_p = opt; |
| 330 | opt_p.blob_allocator = opt.workspace_allocator; |
| 331 | convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_p); |
| 332 | if (bottom_blob_unpacked.empty()) |
| 333 | return -100; |
| 334 | } |
| 335 | |
| 336 | Mat top_blob_bordered_unpacked = top_blob_bordered; |
| 337 | if (out_g_elempack == 1 && out_elempack == 4) |
| 338 | { |
| 339 | top_blob_bordered_unpacked.create(outw, outh, num_output, out_elemsize / out_elempack, 1, opt.workspace_allocator); |
| 340 | if (top_blob_bordered_unpacked.empty()) |
| 341 | return -100; |
| 342 | } |
| 343 | |
| 344 | for (int g = 0; g < group; g++) |
| 345 | { |
| 346 | const Mat bottom_blob_g = bottom_blob_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack); |
| 347 | Mat top_blob_bordered_g = top_blob_bordered_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack); |
| 348 | |
| 349 | const ncnn::Layer* op = group_ops[g]; |
| 350 | |
| 351 | Option opt_g = opt; |
| 352 | opt_g.blob_allocator = top_blob_bordered_unpacked.allocator; |
| 353 | |
| 354 | // forward |
| 355 | int ret = op->forward(bottom_blob_g, top_blob_bordered_g, opt_g); |
| 356 | if (ret != 0) |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | // packing |
| 361 | if (out_g_elempack == 1 && out_elempack == 4) |
| 362 | { |
| 363 | convert_packing(top_blob_bordered_unpacked, top_blob_bordered, 4, opt); |
| 364 | if (top_blob_bordered.empty()) |
| 365 | return -100; |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | top_blob_bordered = top_blob_bordered_unpacked; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | cut_padding(top_blob_bordered, top_blob, opt); |
| 374 | if (top_blob.empty()) |
| 375 | return -100; |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | int DeconvolutionDepthWise_arm::forward_fp16sa(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 381 | { |
| 382 | int w = bottom_blob.w; |
| 383 | int h = bottom_blob.h; |
| 384 | int channels = bottom_blob.c; |
| 385 | size_t elemsize = bottom_blob.elemsize; |
| 386 | int elempack = bottom_blob.elempack; |
| 387 | |
| 388 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 389 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 390 | |
| 391 | int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right; |
| 392 | int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom; |
| 393 | int out_elempack = 1; |
| 394 | if (opt.use_packing_layout) |
| 395 | { |
| 396 | out_elempack = opt.use_fp16_arithmetic && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 397 | } |
| 398 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 399 | |
| 400 | Mat top_blob_bordered; |
| 401 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0)) |
| 402 | { |
| 403 | top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.workspace_allocator); |
| 404 | } |
| 405 | else |
| 406 | { |
| 407 | top_blob_bordered = top_blob; |
| 408 | top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 409 | } |
| 410 | if (top_blob_bordered.empty()) |
| 411 | return -100; |
| 412 | |
| 413 | const int maxk = kernel_w * kernel_h; |
| 414 | |
| 415 | // depth-wise |
| 416 | if (channels * elempack == group && group == num_output) |
| 417 | { |
| 418 | if (elempack == 8) |
| 419 | { |
| 420 | { |
| 421 | #pragma omp parallel for num_threads(opt.num_threads) |
| 422 | for (int g = 0; g < channels; g++) |
| 423 | { |
| 424 | __fp16* outptr = top_blob_bordered.channel(g); |
| 425 | const __fp16* kptr = (const __fp16*)weight_data_tm + maxk * g * 8; |
| 426 | const Mat m = bottom_blob.channel(g); |
| 427 | |
| 428 | for (int i = 0; i < outh; i++) |
| 429 | { |
| 430 | for (int j = 0; j < outw; j++) |
| 431 | { |
| 432 | float16x8_t _sum = vdupq_n_f16((__fp16)0.f); |
| 433 | |
| 434 | if (bias_term) |
| 435 | { |
| 436 | _sum = vld1q_f16((const __fp16*)bias_data_fp16 + g * 8); |
| 437 | } |
| 438 | |
| 439 | for (int y = 0; y < kernel_h; y++) |
| 440 | { |
| 441 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 442 | if (sys < 0 || sys % stride_h != 0) |
| 443 | continue; |
| 444 | |
| 445 | int sy = sys / stride_h; |
| 446 | if (sy >= h) |
| 447 | continue; |
| 448 | |
| 449 | for (int x = 0; x < kernel_w; x++) |
| 450 | { |
| 451 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 452 | if (sxs < 0 || sxs % stride_w != 0) |
| 453 | continue; |
| 454 | |
| 455 | int sx = sxs / stride_w; |
| 456 | if (sx >= w) |
| 457 | continue; |
| 458 | |
| 459 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * 8; |
| 460 | |
| 461 | float16x8_t _val = vld1q_f16(sptr); |
| 462 | |
| 463 | int k = y * kernel_w + x; |
| 464 | |
| 465 | float16x8_t _w = vld1q_f16(kptr + k * 8); |
| 466 | |
| 467 | _sum = vfmaq_f16(_sum, _val, _w); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | _sum = activation_ps_f16(_sum, activation_type, activation_params); |
| 472 | |
| 473 | vst1q_f16(outptr + j * 8, _sum); |
| 474 | } |
| 475 | |
| 476 | outptr += outw * 8; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if (elempack == 4) |
| 483 | { |
| 484 | { |
| 485 | #pragma omp parallel for num_threads(opt.num_threads) |
| 486 | for (int g = 0; g < channels; g++) |
| 487 | { |
| 488 | __fp16* outptr = top_blob_bordered.channel(g); |
| 489 | const __fp16* kptr = (const __fp16*)weight_data_tm + maxk * g * 4; |
| 490 | const Mat m = bottom_blob.channel(g); |
| 491 | |
| 492 | for (int i = 0; i < outh; i++) |
| 493 | { |
| 494 | for (int j = 0; j < outw; j++) |
| 495 | { |
| 496 | float16x4_t _sum = vdup_n_f16((__fp16)0.f); |
| 497 | |
| 498 | if (bias_term) |
| 499 | { |
| 500 | _sum = vld1_f16((const __fp16*)bias_data_fp16 + g * 4); |
| 501 | } |
| 502 | |
| 503 | for (int y = 0; y < kernel_h; y++) |
| 504 | { |
| 505 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 506 | if (sys < 0 || sys % stride_h != 0) |
| 507 | continue; |
| 508 | |
| 509 | int sy = sys / stride_h; |
| 510 | if (sy >= h) |
| 511 | continue; |
| 512 | |
| 513 | for (int x = 0; x < kernel_w; x++) |
| 514 | { |
| 515 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 516 | if (sxs < 0 || sxs % stride_w != 0) |
| 517 | continue; |
| 518 | |
| 519 | int sx = sxs / stride_w; |
| 520 | if (sx >= w) |
| 521 | continue; |
| 522 | |
| 523 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * 4; |
| 524 | |
| 525 | float16x4_t _val = vld1_f16(sptr); |
| 526 | |
| 527 | int k = y * kernel_w + x; |
| 528 | |
| 529 | float16x4_t _w = vld1_f16(kptr + k * 4); |
| 530 | |
| 531 | _sum = vfma_f16(_sum, _val, _w); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | _sum = activation_ps_f16(_sum, activation_type, activation_params); |
| 536 | |
| 537 | vst1_f16(outptr + j * 4, _sum); |
| 538 | } |
| 539 | |
| 540 | outptr += outw * 4; |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | if (elempack == 1) |
| 547 | { |
| 548 | { |
| 549 | #pragma omp parallel for num_threads(opt.num_threads) |
| 550 | for (int g = 0; g < channels; g++) |
| 551 | { |
| 552 | __fp16* outptr = top_blob_bordered.channel(g); |
| 553 | const __fp16* kptr = (const __fp16*)weight_data_tm + maxk * g; |
| 554 | const Mat m = bottom_blob.channel(g); |
| 555 | |
| 556 | for (int i = 0; i < outh; i++) |
| 557 | { |
| 558 | for (int j = 0; j < outw; j++) |
| 559 | { |
| 560 | float sum = 0.f; |
| 561 | |
| 562 | if (bias_term) |
| 563 | { |
| 564 | sum = bias_data[g]; |
| 565 | } |
| 566 | |
| 567 | for (int y = 0; y < kernel_h; y++) |
| 568 | { |
| 569 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 570 | if (sys < 0 || sys % stride_h != 0) |
| 571 | continue; |
| 572 | |
| 573 | int sy = sys / stride_h; |
| 574 | if (sy >= h) |
| 575 | continue; |
| 576 | |
| 577 | const __fp16* sptr = m.row<const __fp16>(sy); |
| 578 | |
| 579 | for (int x = 0; x < kernel_w; x++) |
| 580 | { |
| 581 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 582 | if (sxs < 0 || sxs % stride_w != 0) |
| 583 | continue; |
| 584 | |
| 585 | int sx = sxs / stride_w; |
| 586 | if (sx >= w) |
| 587 | continue; |
| 588 | |
| 589 | __fp16 val = sptr[sx]; |
| 590 | |
| 591 | int k = y * kernel_w + x; |
| 592 | |
| 593 | __fp16 w = kptr[k]; |
| 594 | |
| 595 | sum += val * w; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | sum = activation_ss_f16(sum, activation_type, activation_params); |
| 600 | |
| 601 | outptr[j] = (__fp16)sum; |
| 602 | } |
| 603 | |
| 604 | outptr += outw; |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | else |
| 611 | { |
| 612 | // group deconvolution |
| 613 | const int channels_g = channels * elempack / group; |
| 614 | const int num_output_g = num_output / group; |
| 615 | |
| 616 | int g_elempack = 1; |
| 617 | int out_g_elempack = 1; |
| 618 | if (opt.use_packing_layout) |
| 619 | { |
| 620 | g_elempack = opt.use_fp16_arithmetic && channels_g % 8 == 0 ? 8 : channels_g % 4 == 0 ? 4 : 1; |
| 621 | out_g_elempack = opt.use_fp16_arithmetic && num_output_g % 8 == 0 ? 8 : num_output_g % 4 == 0 ? 4 : 1; |
| 622 | } |
| 623 | |
| 624 | // unpacking |
| 625 | Mat bottom_blob_unpacked = bottom_blob; |
| 626 | if (elempack > g_elempack) |
| 627 | { |
| 628 | Option opt_p = opt; |
| 629 | opt_p.blob_allocator = opt.workspace_allocator; |
| 630 | convert_packing(bottom_blob, bottom_blob_unpacked, g_elempack, opt_p); |
| 631 | if (bottom_blob_unpacked.empty()) |
| 632 | return -100; |
| 633 | } |
| 634 | |
| 635 | Mat top_blob_bordered_unpacked = top_blob_bordered; |
| 636 | if (out_g_elempack < out_elempack) |
| 637 | { |
| 638 | top_blob_bordered_unpacked.create(outw, outh, num_output / out_g_elempack, out_elemsize / out_elempack * out_g_elempack, out_g_elempack, opt.workspace_allocator); |
| 639 | if (top_blob_bordered_unpacked.empty()) |
| 640 | return -100; |
| 641 | } |
| 642 | |
| 643 | for (int g = 0; g < group; g++) |
| 644 | { |
| 645 | const Mat bottom_blob_g = bottom_blob_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack); |
| 646 | Mat top_blob_bordered_g = top_blob_bordered_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack); |
| 647 | |
| 648 | const ncnn::Layer* op = group_ops[g]; |
| 649 | |
| 650 | Option opt_g = opt; |
| 651 | opt_g.blob_allocator = top_blob_bordered_unpacked.allocator; |
| 652 | |
| 653 | // forward |
| 654 | int ret = op->forward(bottom_blob_g, top_blob_bordered_g, opt_g); |
| 655 | if (ret != 0) |
| 656 | return ret; |
| 657 | } |
| 658 | |
| 659 | // packing |
| 660 | if (out_g_elempack < out_elempack) |
| 661 | { |
| 662 | convert_packing(top_blob_bordered_unpacked, top_blob_bordered, out_elempack, opt); |
| 663 | if (top_blob_bordered.empty()) |
| 664 | return -100; |
| 665 | } |
| 666 | else |
| 667 | { |
| 668 | top_blob_bordered = top_blob_bordered_unpacked; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | cut_padding(top_blob_bordered, top_blob, opt); |
| 673 | if (top_blob.empty()) |
| 674 | return -100; |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 679 | |
| 680 | } // namespace ncnn |