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