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