| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 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_riscv.h" |
| 16 | |
| 17 | #include "cpu.h" |
| 18 | #include "layer_type.h" |
| 19 | |
| 20 | #if __riscv_vector |
| 21 | #include <riscv_vector.h> |
| 22 | #endif // __riscv_vector |
| 23 | |
| 24 | #include "riscv_activation.h" |
| 25 | #include "riscv_usability.h" |
| 26 | |
| 27 | namespace ncnn { |
| 28 | |
| 29 | DeconvolutionDepthWise_riscv::DeconvolutionDepthWise_riscv() |
| 30 | { |
| 31 | #if __riscv_vector |
| 32 | support_packing = true; |
| 33 | #if __riscv_zfh |
| 34 | support_fp16_storage = true; |
| 35 | #endif |
| 36 | #endif // __riscv_vector |
| 37 | } |
| 38 | |
| 39 | int DeconvolutionDepthWise_riscv::create_pipeline(const Option& opt) |
| 40 | { |
| 41 | if (dynamic_weight) |
| 42 | return 0; |
| 43 | |
| 44 | #if __riscv_vector && __riscv_zfh |
| 45 | if (opt.use_fp16_storage) |
| 46 | { |
| 47 | return create_pipeline_fp16s(opt); |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | #if __riscv_vector |
| 52 | const int packn = csrr_vlenb() / 4; |
| 53 | #endif |
| 54 | |
| 55 | const int maxk = kernel_w * kernel_h; |
| 56 | int channels = (weight_data_size / group) / maxk / (num_output / group) * group; |
| 57 | |
| 58 | // depth-wise |
| 59 | if (channels == group && group == num_output) |
| 60 | { |
| 61 | int elempack = 1; |
| 62 | #if __riscv_vector |
| 63 | if (opt.use_packing_layout) |
| 64 | { |
| 65 | elempack = channels % packn == 0 ? packn : 1; |
| 66 | } |
| 67 | #endif |
| 68 | |
| 69 | Mat weight_data_transposed(weight_data.w); |
| 70 | { |
| 71 | float* pt = weight_data_transposed; |
| 72 | const float* p = weight_data; |
| 73 | |
| 74 | for (int i = 0; i < (channels / group) * (num_output / group) * group; i++) |
| 75 | { |
| 76 | for (int k = 0; k < maxk; k++) |
| 77 | { |
| 78 | pt[maxk - 1 - k] = p[k]; |
| 79 | } |
| 80 | |
| 81 | p += maxk; |
| 82 | pt += maxk; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | #if __riscv_vector |
| 87 | // packn |
| 88 | if (elempack == packn) |
| 89 | { |
| 90 | Mat weight_data_r2 = weight_data_transposed.reshape(maxk, group); |
| 91 | convert_packing(weight_data_r2, weight_data_tm, packn, opt); |
| 92 | } |
| 93 | #endif // __riscv_vector |
| 94 | |
| 95 | if (elempack == 1) |
| 96 | { |
| 97 | weight_data_tm = weight_data_transposed; |
| 98 | } |
| 99 | |
| 100 | if (opt.lightmode) |
| 101 | weight_data.release(); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | // group convolution |
| 107 | create_group_ops(opt); |
| 108 | |
| 109 | if (opt.lightmode) |
| 110 | weight_data.release(); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | int DeconvolutionDepthWise_riscv::create_group_ops(const Option& opt) |
| 116 | { |
| 117 | // create Deconvolution op for each group |
| 118 | const int maxk = kernel_w * kernel_h; |
| 119 | int channels = (weight_data_size / group) / maxk / (num_output / group) * group; |
| 120 | |
| 121 | for (int i = 0; i < (int)group_ops.size(); i++) |
| 122 | delete group_ops[i]; |
| 123 | |
| 124 | group_ops.clear(); |
| 125 | |
| 126 | const int channels_g = channels / group; |
| 127 | const int num_output_g = num_output / group; |
| 128 | |
| 129 | group_ops.resize(group); |
| 130 | |
| 131 | for (int g = 0; g < group; g++) |
| 132 | { |
| 133 | Mat weight_data_g = weight_data.range(maxk * channels_g * num_output_g * g, maxk * channels_g * num_output_g).clone(); |
| 134 | Mat bias_data_g; |
| 135 | if (bias_term) |
| 136 | bias_data_g = bias_data.range(num_output_g * g, num_output_g); |
| 137 | |
| 138 | ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Deconvolution); |
| 139 | |
| 140 | // set param |
| 141 | ncnn::ParamDict pd; |
| 142 | pd.set(0, num_output_g); // num_output |
| 143 | pd.set(1, kernel_w); |
| 144 | pd.set(11, kernel_h); |
| 145 | pd.set(2, dilation_w); |
| 146 | pd.set(12, dilation_h); |
| 147 | pd.set(3, stride_w); |
| 148 | pd.set(13, stride_h); |
| 149 | pd.set(4, 0); // pad_w |
| 150 | pd.set(14, 0); // pad_h |
| 151 | pd.set(18, output_pad_right); |
| 152 | pd.set(19, output_pad_bottom); |
| 153 | pd.set(5, bias_term); |
| 154 | pd.set(6, maxk * channels_g * num_output_g); // weight_data_size |
| 155 | pd.set(9, activation_type); |
| 156 | pd.set(10, activation_params); |
| 157 | |
| 158 | op->load_param(pd); |
| 159 | |
| 160 | // set weights |
| 161 | if (bias_term) |
| 162 | { |
| 163 | ncnn::Mat weights[2]; |
| 164 | weights[0] = weight_data_g; |
| 165 | weights[1] = bias_data_g; |
| 166 | |
| 167 | op->load_model(ModelBinFromMatArray(weights)); |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | ncnn::Mat weights[1]; |
| 172 | weights[0] = weight_data_g; |
| 173 | |
| 174 | op->load_model(ModelBinFromMatArray(weights)); |
| 175 | } |
| 176 | |
| 177 | op->create_pipeline(opt); |
| 178 | |
| 179 | group_ops[g] = op; |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | int DeconvolutionDepthWise_riscv::destroy_pipeline(const Option& opt) |
| 186 | { |
| 187 | for (int i = 0; i < (int)group_ops.size(); i++) |
| 188 | { |
| 189 | group_ops[i]->destroy_pipeline(opt); |
| 190 | delete group_ops[i]; |
| 191 | } |
| 192 | group_ops.clear(); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | int DeconvolutionDepthWise_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 198 | { |
| 199 | int elembits = bottom_blob.elembits(); |
| 200 | |
| 201 | #if __riscv_vector && __riscv_zfh |
| 202 | if (opt.use_fp16_storage && elembits == 16) |
| 203 | { |
| 204 | if (opt.use_fp16_arithmetic) |
| 205 | return forward_fp16sa(bottom_blob, top_blob, opt); |
| 206 | else |
| 207 | return forward_fp16s(bottom_blob, top_blob, opt); |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | #if __riscv_vector |
| 212 | const int packn = csrr_vlenb() / 4; |
| 213 | const size_t vl = vsetvl_e32m1(packn); |
| 214 | #endif |
| 215 | |
| 216 | // convolv with NxN kernel |
| 217 | // value = value + bias |
| 218 | |
| 219 | int w = bottom_blob.w; |
| 220 | int h = bottom_blob.h; |
| 221 | int channels = bottom_blob.c; |
| 222 | size_t elemsize = bottom_blob.elemsize; |
| 223 | int elempack = bottom_blob.elempack; |
| 224 | |
| 225 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 226 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 227 | |
| 228 | int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right; |
| 229 | int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom; |
| 230 | int out_elempack = 1; |
| 231 | #if __riscv_vector |
| 232 | if (opt.use_packing_layout) |
| 233 | { |
| 234 | out_elempack = num_output % packn == 0 ? packn : 1; |
| 235 | } |
| 236 | #endif |
| 237 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 238 | |
| 239 | Mat top_blob_bordered; |
| 240 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0)) |
| 241 | { |
| 242 | top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.workspace_allocator); |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | top_blob_bordered = top_blob; |
| 247 | top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 248 | } |
| 249 | if (top_blob_bordered.empty()) |
| 250 | return -100; |
| 251 | |
| 252 | const int maxk = kernel_w * kernel_h; |
| 253 | |
| 254 | // depth-wise |
| 255 | if (channels * elempack == group && group == num_output) |
| 256 | { |
| 257 | #if __riscv_vector |
| 258 | if (elempack == packn) |
| 259 | { |
| 260 | { |
| 261 | #pragma omp parallel for num_threads(opt.num_threads) |
| 262 | for (int g = 0; g < channels; g++) |
| 263 | { |
| 264 | float* outptr = top_blob_bordered.channel(g); |
| 265 | const float* kptr = (const float*)weight_data_tm + maxk * g * packn; |
| 266 | const Mat m = bottom_blob.channel(g); |
| 267 | |
| 268 | for (int i = 0; i < outh; i++) |
| 269 | { |
| 270 | for (int j = 0; j < outw; j++) |
| 271 | { |
| 272 | vfloat32m1_t _sum = vfmv_v_f_f32m1(0.f, vl); |
| 273 | |
| 274 | if (bias_term) |
| 275 | { |
| 276 | _sum = vle32_v_f32m1((const float*)bias_data + g * packn, vl); |
| 277 | } |
| 278 | |
| 279 | for (int y = 0; y < kernel_h; y++) |
| 280 | { |
| 281 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 282 | if (sys < 0 || sys % stride_h != 0) |
| 283 | continue; |
| 284 | |
| 285 | int sy = sys / stride_h; |
| 286 | if (sy >= h) |
| 287 | continue; |
| 288 | |
| 289 | for (int x = 0; x < kernel_w; x++) |
| 290 | { |
| 291 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 292 | if (sxs < 0 || sxs % stride_w != 0) |
| 293 | continue; |
| 294 | |
| 295 | int sx = sxs / stride_w; |
| 296 | if (sx >= w) |
| 297 | continue; |
| 298 | |
| 299 | const float* sptr = m.row(sy) + sx * packn; |
| 300 | |
| 301 | int k = y * kernel_w + x; |
| 302 | |
| 303 | vfloat32m1_t _val = vle32_v_f32m1(sptr, vl); |
| 304 | vfloat32m1_t _w = vle32_v_f32m1(kptr + k * packn, vl); |
| 305 | _sum = vfmacc_vv_f32m1(_sum, _val, _w, vl); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | _sum = activation_ps(_sum, activation_type, activation_params, vl); |
| 310 | |
| 311 | vse32_v_f32m1(outptr + j * packn, _sum, vl); |
| 312 | } |
| 313 | |
| 314 | outptr += outw * packn; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | #endif // __riscv_vector |
| 320 | |
| 321 | if (elempack == 1) |
| 322 | { |
| 323 | #pragma omp parallel for num_threads(opt.num_threads) |
| 324 | for (int g = 0; g < channels; g++) |
| 325 | { |
| 326 | float* outptr = top_blob_bordered.channel(g); |
| 327 | const float* kptr = (const float*)weight_data_tm + maxk * g; |
| 328 | const Mat m = bottom_blob.channel(g); |
| 329 | |
| 330 | for (int i = 0; i < outh; i++) |
| 331 | { |
| 332 | for (int j = 0; j < outw; j++) |
| 333 | { |
| 334 | float sum = 0.f; |
| 335 | |
| 336 | if (bias_term) |
| 337 | { |
| 338 | sum = bias_data[g]; |
| 339 | } |
| 340 | |
| 341 | for (int y = 0; y < kernel_h; y++) |
| 342 | { |
| 343 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 344 | if (sys < 0 || sys % stride_h != 0) |
| 345 | continue; |
| 346 | |
| 347 | int sy = sys / stride_h; |
| 348 | if (sy >= h) |
| 349 | continue; |
| 350 | |
| 351 | const float* sptr = m.row(sy); |
| 352 | |
| 353 | for (int x = 0; x < kernel_w; x++) |
| 354 | { |
| 355 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 356 | if (sxs < 0 || sxs % stride_w != 0) |
| 357 | continue; |
| 358 | |
| 359 | int sx = sxs / stride_w; |
| 360 | if (sx >= w) |
| 361 | continue; |
| 362 | |
| 363 | float val = sptr[sx]; |
| 364 | |
| 365 | int k = y * kernel_w + x; |
| 366 | |
| 367 | float w = kptr[k]; |
| 368 | |
| 369 | sum += val * w; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | sum = activation_ss(sum, activation_type, activation_params); |
| 374 | |
| 375 | outptr[j] = sum; |
| 376 | } |
| 377 | |
| 378 | outptr += outw; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | // group deconvolution |
| 386 | const int channels_g = channels * elempack / group; |
| 387 | const int num_output_g = num_output / group; |
| 388 | |
| 389 | int g_elempack = 1; |
| 390 | int out_g_elempack = 1; |
| 391 | #if __riscv_vector |
| 392 | if (opt.use_packing_layout) |
| 393 | { |
| 394 | g_elempack = channels_g % packn == 0 ? packn : 1; |
| 395 | out_g_elempack = num_output_g % packn == 0 ? packn : 1; |
| 396 | } |
| 397 | #endif |
| 398 | |
| 399 | // unpacking |
| 400 | Mat bottom_blob_unpacked = bottom_blob; |
| 401 | if (elempack > g_elempack) |
| 402 | { |
| 403 | Option opt_p = opt; |
| 404 | opt_p.blob_allocator = opt.workspace_allocator; |
| 405 | convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_p); |
| 406 | } |
| 407 | |
| 408 | Mat top_blob_bordered_unpacked = top_blob_bordered; |
| 409 | if (out_g_elempack < out_elempack) |
| 410 | { |
| 411 | top_blob_bordered_unpacked.create(outw, outh, num_output, out_elemsize / out_elempack, 1, opt.workspace_allocator); |
| 412 | if (top_blob_bordered_unpacked.empty()) |
| 413 | return -100; |
| 414 | } |
| 415 | |
| 416 | for (int g = 0; g < group; g++) |
| 417 | { |
| 418 | const Mat bottom_blob_g = bottom_blob_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack); |
| 419 | Mat top_blob_bordered_g = top_blob_bordered_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack); |
| 420 | |
| 421 | const ncnn::Layer* op = group_ops[g]; |
| 422 | |
| 423 | Option opt_g = opt; |
| 424 | opt_g.blob_allocator = top_blob_bordered_unpacked.allocator; |
| 425 | |
| 426 | // forward |
| 427 | op->forward(bottom_blob_g, top_blob_bordered_g, opt_g); |
| 428 | } |
| 429 | |
| 430 | // packing |
| 431 | if (out_g_elempack < out_elempack) |
| 432 | { |
| 433 | convert_packing(top_blob_bordered_unpacked, top_blob_bordered, 4, opt); |
| 434 | } |
| 435 | else |
| 436 | { |
| 437 | top_blob_bordered = top_blob_bordered_unpacked; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | cut_padding(top_blob_bordered, top_blob, opt); |
| 442 | if (top_blob.empty()) |
| 443 | return -100; |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | int DeconvolutionDepthWise_riscv::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 449 | { |
| 450 | const Mat& bottom_blob = bottom_blobs[0]; |
| 451 | const Mat& _weight_data = bottom_blobs[1]; |
| 452 | Mat& top_blob = top_blobs[0]; |
| 453 | |
| 454 | const int _num_input = bottom_blob.c * bottom_blob.elempack; |
| 455 | const int _kernel_w = _weight_data.w; |
| 456 | const int _kernel_h = _weight_data.h; |
| 457 | const int _num_output = _weight_data.d * group; |
| 458 | |
| 459 | Mat weight_data_flattened; |
| 460 | flatten(_weight_data, weight_data_flattened, opt); |
| 461 | if (weight_data_flattened.empty()) |
| 462 | return -100; |
| 463 | |
| 464 | #if NCNN_RVV |
| 465 | if (opt.use_fp16_storage && cpu_support_riscv_v() && cpu_support_riscv_zfh() && weight_data_flattened.elembits() == 16) |
| 466 | { |
| 467 | Mat weight_data_flattened_fp32; |
| 468 | cast_float16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt); |
| 469 | weight_data_flattened = weight_data_flattened_fp32; |
| 470 | } |
| 471 | #endif // NCNN_RVV |
| 472 | |
| 473 | // weight_data_flattened as pack1 |
| 474 | weight_data_flattened.w *= weight_data_flattened.elempack; |
| 475 | weight_data_flattened.elemsize /= weight_data_flattened.elempack; |
| 476 | weight_data_flattened.elempack = 1; |
| 477 | |
| 478 | // transpose group-inch/group-outch/group-kh-kw to group-outch/group-inch/group-kh-kw |
| 479 | Mat weight_data_transposed; |
| 480 | { |
| 481 | weight_data_transposed.create(_kernel_w * _kernel_h * _num_output * _num_input / group, 4u, opt.workspace_allocator); |
| 482 | if (weight_data_transposed.empty()) |
| 483 | return -100; |
| 484 | |
| 485 | const int outch_g = _num_output / group; |
| 486 | const int inch_g = _num_input / group; |
| 487 | const int maxk = _kernel_h * _kernel_w; |
| 488 | |
| 489 | for (int g = 0; g < group; g++) |
| 490 | { |
| 491 | // reorder weight from inch-outch to outch-inch |
| 492 | float* wg2 = (float*)weight_data_transposed + g * outch_g * inch_g * maxk; |
| 493 | const float* wg = (const float*)weight_data_flattened + g * inch_g * outch_g * maxk; |
| 494 | for (int i = 0; i < outch_g; i++) |
| 495 | { |
| 496 | for (int j = 0; j < inch_g; j++) |
| 497 | { |
| 498 | for (int k = 0; k < maxk; k++) |
| 499 | { |
| 500 | wg2[(i * inch_g + j) * maxk + k] = wg[(j * outch_g + i) * maxk + k]; |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | Mat bias_data_flattened; |
| 508 | if (bias_term) |
| 509 | { |
| 510 | const Mat& _bias_data = bottom_blobs[2]; |
| 511 | flatten(_bias_data, bias_data_flattened, opt); |
| 512 | if (bias_data_flattened.empty()) |
| 513 | return -100; |
| 514 | |
| 515 | #if NCNN_RVV |
| 516 | if (opt.use_fp16_storage && cpu_support_riscv_v() && cpu_support_riscv_zfh() && bias_data_flattened.elembits() == 16) |
| 517 | { |
| 518 | Mat bias_data_flattened_fp32; |
| 519 | cast_float16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt); |
| 520 | bias_data_flattened = bias_data_flattened_fp32; |
| 521 | } |
| 522 | #endif // NCNN_RVV |
| 523 | |
| 524 | // bias_data_flattened as pack1 |
| 525 | bias_data_flattened.w *= bias_data_flattened.elempack; |
| 526 | bias_data_flattened.elemsize /= bias_data_flattened.elempack; |
| 527 | bias_data_flattened.elempack = 1; |
| 528 | } |
| 529 | |
| 530 | ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::DeconvolutionDepthWise); |
| 531 | |
| 532 | ncnn::ParamDict pd; |
| 533 | pd.set(0, _num_output); |
| 534 | pd.set(1, _kernel_w); |
| 535 | pd.set(11, _kernel_h); |
| 536 | pd.set(2, dilation_w); |
| 537 | pd.set(12, dilation_h); |
| 538 | pd.set(3, stride_w); |
| 539 | pd.set(13, stride_h); |
| 540 | pd.set(4, pad_left); |
| 541 | pd.set(15, pad_right); |
| 542 | pd.set(14, pad_top); |
| 543 | pd.set(16, pad_bottom); |
| 544 | pd.set(18, output_pad_right); |
| 545 | pd.set(19, output_pad_bottom); |
| 546 | pd.set(20, output_w); |
| 547 | pd.set(21, output_h); |
| 548 | pd.set(5, bias_term); |
| 549 | pd.set(6, weight_data_transposed.w); |
| 550 | pd.set(7, group); |
| 551 | pd.set(9, activation_type); |
| 552 | pd.set(10, activation_params); |
| 553 | |
| 554 | op->load_param(pd); |
| 555 | |
| 556 | ncnn::Mat weights[2]; |
| 557 | weights[0] = weight_data_transposed; |
| 558 | weights[1] = bias_data_flattened; |
| 559 | |
| 560 | op->load_model(ncnn::ModelBinFromMatArray(weights)); |
| 561 | |
| 562 | op->create_pipeline(opt); |
| 563 | |
| 564 | op->forward(bottom_blob, top_blob, opt); |
| 565 | |
| 566 | op->destroy_pipeline(opt); |
| 567 | |
| 568 | delete op; |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | #if __riscv_vector && __riscv_zfh |
| 574 | int DeconvolutionDepthWise_riscv::create_pipeline_fp16s(const Option& opt) |
| 575 | { |
| 576 | const int packn = csrr_vlenb() / 2; |
| 577 | |
| 578 | const int maxk = kernel_w * kernel_h; |
| 579 | int channels = (weight_data_size / group) / maxk / (num_output / group) * group; |
| 580 | |
| 581 | // depth-wise |
| 582 | if (channels == group && group == num_output) |
| 583 | { |
| 584 | int elempack = 1; |
| 585 | if (opt.use_packing_layout) |
| 586 | { |
| 587 | elempack = channels % packn == 0 ? packn : 1; |
| 588 | } |
| 589 | |
| 590 | Mat weight_data_transposed(weight_data.w); |
| 591 | { |
| 592 | float* pt = weight_data_transposed; |
| 593 | const float* p = weight_data; |
| 594 | |
| 595 | for (int i = 0; i < (channels / group) * (num_output / group) * group; i++) |
| 596 | { |
| 597 | for (int k = 0; k < maxk; k++) |
| 598 | { |
| 599 | pt[maxk - 1 - k] = p[k]; |
| 600 | } |
| 601 | |
| 602 | p += maxk; |
| 603 | pt += maxk; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | // packn |
| 608 | if (elempack == packn) |
| 609 | { |
| 610 | Mat weight_data_r2 = weight_data_transposed.reshape(maxk, group); |
| 611 | Mat weight_data_r2_packed; |
| 612 | convert_packing(weight_data_r2, weight_data_r2_packed, packn, opt); |
| 613 | |
| 614 | ncnn::cast_float32_to_float16(weight_data_r2_packed, weight_data_tm, opt); |
| 615 | } |
| 616 | |
| 617 | if (elempack == 1) |
| 618 | { |
| 619 | ncnn::cast_float32_to_float16(weight_data_transposed, weight_data_tm, opt); |
| 620 | } |
| 621 | |
| 622 | ncnn::cast_float32_to_float16(bias_data, bias_data_fp16, opt); |
| 623 | |
| 624 | if (opt.lightmode) |
| 625 | weight_data.release(); |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | // group convolution |
| 631 | create_group_ops(opt); |
| 632 | |
| 633 | if (opt.lightmode) |
| 634 | weight_data.release(); |
| 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | int DeconvolutionDepthWise_riscv::forward_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 640 | { |
| 641 | const int packn = csrr_vlenb() / 2; |
| 642 | const size_t vl = vsetvl_e16m1(packn); |
| 643 | |
| 644 | int w = bottom_blob.w; |
| 645 | int h = bottom_blob.h; |
| 646 | int channels = bottom_blob.c; |
| 647 | size_t elemsize = bottom_blob.elemsize; |
| 648 | int elempack = bottom_blob.elempack; |
| 649 | |
| 650 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 651 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 652 | |
| 653 | int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right; |
| 654 | int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom; |
| 655 | int out_elempack = (opt.use_packing_layout && num_output % packn == 0) ? packn : 1; |
| 656 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 657 | |
| 658 | Mat top_blob_bordered; |
| 659 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0)) |
| 660 | { |
| 661 | top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.workspace_allocator); |
| 662 | } |
| 663 | else |
| 664 | { |
| 665 | top_blob_bordered = top_blob; |
| 666 | top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 667 | } |
| 668 | if (top_blob_bordered.empty()) |
| 669 | return -100; |
| 670 | |
| 671 | const int maxk = kernel_w * kernel_h; |
| 672 | |
| 673 | // depth-wise |
| 674 | if (channels * elempack == group && group == num_output) |
| 675 | { |
| 676 | if (elempack == packn) |
| 677 | { |
| 678 | { |
| 679 | #pragma omp parallel for num_threads(opt.num_threads) |
| 680 | for (int g = 0; g < channels; g++) |
| 681 | { |
| 682 | __fp16* outptr = top_blob_bordered.channel(g); |
| 683 | const __fp16* kptr = (const __fp16*)weight_data_tm + maxk * g * packn; |
| 684 | const Mat m = bottom_blob.channel(g); |
| 685 | |
| 686 | for (int i = 0; i < outh; i++) |
| 687 | { |
| 688 | for (int j = 0; j < outw; j++) |
| 689 | { |
| 690 | vfloat32m2_t _sum = vfmv_v_f_f32m2(0.f, vl); |
| 691 | |
| 692 | if (bias_term) |
| 693 | { |
| 694 | _sum = vle32_v_f32m2((const float*)bias_data + g * packn, vl); |
| 695 | } |
| 696 | |
| 697 | for (int y = 0; y < kernel_h; y++) |
| 698 | { |
| 699 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 700 | if (sys < 0 || sys % stride_h != 0) |
| 701 | continue; |
| 702 | |
| 703 | int sy = sys / stride_h; |
| 704 | if (sy >= h) |
| 705 | continue; |
| 706 | |
| 707 | for (int x = 0; x < kernel_w; x++) |
| 708 | { |
| 709 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 710 | if (sxs < 0 || sxs % stride_w != 0) |
| 711 | continue; |
| 712 | |
| 713 | int sx = sxs / stride_w; |
| 714 | if (sx >= w) |
| 715 | continue; |
| 716 | |
| 717 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * packn; |
| 718 | |
| 719 | int k = y * kernel_w + x; |
| 720 | |
| 721 | vfloat16m1_t _val = vle16_v_f16m1(sptr, vl); |
| 722 | vfloat16m1_t _w = vle16_v_f16m1(kptr + k * packn, vl); |
| 723 | _sum = vfwmacc_vv_f32m2(_sum, _val, _w, vl); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | _sum = activation_ps(_sum, activation_type, activation_params, vl); |
| 728 | |
| 729 | vse16_v_f16m1(outptr + j * packn, vfncvt_f_f_w_f16m1(_sum, vl), vl); |
| 730 | } |
| 731 | |
| 732 | outptr += outw * packn; |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | if (elempack == 1) |
| 739 | { |
| 740 | { |
| 741 | #pragma omp parallel for num_threads(opt.num_threads) |
| 742 | for (int g = 0; g < channels; g++) |
| 743 | { |
| 744 | __fp16* outptr = top_blob_bordered.channel(g); |
| 745 | const __fp16* kptr = (const __fp16*)weight_data_tm + maxk * g; |
| 746 | const Mat m = bottom_blob.channel(g); |
| 747 | |
| 748 | for (int i = 0; i < outh; i++) |
| 749 | { |
| 750 | for (int j = 0; j < outw; j++) |
| 751 | { |
| 752 | float sum = 0.f; |
| 753 | |
| 754 | if (bias_term) |
| 755 | { |
| 756 | sum = bias_data[g]; |
| 757 | } |
| 758 | |
| 759 | for (int y = 0; y < kernel_h; y++) |
| 760 | { |
| 761 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 762 | if (sys < 0 || sys % stride_h != 0) |
| 763 | continue; |
| 764 | |
| 765 | int sy = sys / stride_h; |
| 766 | if (sy >= h) |
| 767 | continue; |
| 768 | |
| 769 | const __fp16* sptr = m.row<const __fp16>(sy); |
| 770 | |
| 771 | for (int x = 0; x < kernel_w; x++) |
| 772 | { |
| 773 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 774 | if (sxs < 0 || sxs % stride_w != 0) |
| 775 | continue; |
| 776 | |
| 777 | int sx = sxs / stride_w; |
| 778 | if (sx >= w) |
| 779 | continue; |
| 780 | |
| 781 | float val = (float)sptr[sx]; |
| 782 | |
| 783 | int k = y * kernel_w + x; |
| 784 | |
| 785 | float w = (float)kptr[k]; |
| 786 | |
| 787 | sum += val * w; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | sum = activation_ss(sum, activation_type, activation_params); |
| 792 | |
| 793 | outptr[j] = (__fp16)sum; |
| 794 | } |
| 795 | |
| 796 | outptr += outw; |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | else |
| 803 | { |
| 804 | // group deconvolution |
| 805 | const int channels_g = channels * elempack / group; |
| 806 | const int num_output_g = num_output / group; |
| 807 | |
| 808 | int g_elempack = (opt.use_packing_layout && channels_g % packn == 0) ? packn : 1; |
| 809 | int out_g_elempack = (opt.use_packing_layout && num_output_g % packn == 0) ? packn : 1; |
| 810 | |
| 811 | // unpacking |
| 812 | Mat bottom_blob_unpacked = bottom_blob; |
| 813 | if (elempack > g_elempack) |
| 814 | { |
| 815 | Option opt_p = opt; |
| 816 | opt_p.blob_allocator = opt.workspace_allocator; |
| 817 | convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_p); |
| 818 | } |
| 819 | |
| 820 | Mat top_blob_bordered_unpacked = top_blob_bordered; |
| 821 | if (out_g_elempack < out_elempack) |
| 822 | { |
| 823 | top_blob_bordered_unpacked.create(outw, outh, num_output, out_elemsize / out_elempack, 1, opt.workspace_allocator); |
| 824 | if (top_blob_bordered_unpacked.empty()) |
| 825 | return -100; |
| 826 | } |
| 827 | |
| 828 | for (int g = 0; g < group; g++) |
| 829 | { |
| 830 | const Mat bottom_blob_g = bottom_blob_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack); |
| 831 | Mat top_blob_bordered_g = top_blob_bordered_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack); |
| 832 | |
| 833 | const ncnn::Layer* op = group_ops[g]; |
| 834 | |
| 835 | Option opt_g = opt; |
| 836 | opt_g.blob_allocator = top_blob_bordered_unpacked.allocator; |
| 837 | |
| 838 | // forward |
| 839 | op->forward(bottom_blob_g, top_blob_bordered_g, opt_g); |
| 840 | } |
| 841 | |
| 842 | // packing |
| 843 | if (out_g_elempack < out_elempack) |
| 844 | { |
| 845 | convert_packing(top_blob_bordered_unpacked, top_blob_bordered, out_elempack, opt); |
| 846 | } |
| 847 | else |
| 848 | { |
| 849 | top_blob_bordered = top_blob_bordered_unpacked; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | cut_padding(top_blob_bordered, top_blob, opt); |
| 854 | if (top_blob.empty()) |
| 855 | return -100; |
| 856 | |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | int DeconvolutionDepthWise_riscv::forward_fp16sa(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 861 | { |
| 862 | const int packn = csrr_vlenb() / 2; |
| 863 | const size_t vl = vsetvl_e16m1(packn); |
| 864 | |
| 865 | int w = bottom_blob.w; |
| 866 | int h = bottom_blob.h; |
| 867 | int channels = bottom_blob.c; |
| 868 | size_t elemsize = bottom_blob.elemsize; |
| 869 | int elempack = bottom_blob.elempack; |
| 870 | |
| 871 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 872 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 873 | |
| 874 | int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right; |
| 875 | int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom; |
| 876 | int out_elempack = (opt.use_packing_layout && num_output % packn == 0) ? packn : 1; |
| 877 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 878 | |
| 879 | Mat top_blob_bordered; |
| 880 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0)) |
| 881 | { |
| 882 | top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.workspace_allocator); |
| 883 | } |
| 884 | else |
| 885 | { |
| 886 | top_blob_bordered = top_blob; |
| 887 | top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 888 | } |
| 889 | if (top_blob_bordered.empty()) |
| 890 | return -100; |
| 891 | |
| 892 | const int maxk = kernel_w * kernel_h; |
| 893 | |
| 894 | // depth-wise |
| 895 | if (channels * elempack == group && group == num_output) |
| 896 | { |
| 897 | if (elempack == packn) |
| 898 | { |
| 899 | { |
| 900 | #pragma omp parallel for num_threads(opt.num_threads) |
| 901 | for (int g = 0; g < channels; g++) |
| 902 | { |
| 903 | __fp16* outptr = top_blob_bordered.channel(g); |
| 904 | const __fp16* kptr = (const __fp16*)weight_data_tm + maxk * g * packn; |
| 905 | const Mat m = bottom_blob.channel(g); |
| 906 | |
| 907 | for (int i = 0; i < outh; i++) |
| 908 | { |
| 909 | for (int j = 0; j < outw; j++) |
| 910 | { |
| 911 | vfloat16m1_t _sum = vfmv_v_f_f16m1((__fp16)0.f, vl); |
| 912 | |
| 913 | if (bias_term) |
| 914 | { |
| 915 | _sum = vle16_v_f16m1((const __fp16*)bias_data_fp16 + g * packn, vl); |
| 916 | } |
| 917 | |
| 918 | for (int y = 0; y < kernel_h; y++) |
| 919 | { |
| 920 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 921 | if (sys < 0 || sys % stride_h != 0) |
| 922 | continue; |
| 923 | |
| 924 | int sy = sys / stride_h; |
| 925 | if (sy >= h) |
| 926 | continue; |
| 927 | |
| 928 | for (int x = 0; x < kernel_w; x++) |
| 929 | { |
| 930 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 931 | if (sxs < 0 || sxs % stride_w != 0) |
| 932 | continue; |
| 933 | |
| 934 | int sx = sxs / stride_w; |
| 935 | if (sx >= w) |
| 936 | continue; |
| 937 | |
| 938 | const __fp16* sptr = m.row<const __fp16>(sy) + sx * packn; |
| 939 | |
| 940 | int k = y * kernel_w + x; |
| 941 | |
| 942 | vfloat16m1_t _val = vle16_v_f16m1(sptr, vl); |
| 943 | vfloat16m1_t _w = vle16_v_f16m1(kptr + k * packn, vl); |
| 944 | _sum = vfmacc_vv_f16m1(_sum, _val, _w, vl); |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | _sum = activation_ps(_sum, activation_type, activation_params, vl); |
| 949 | |
| 950 | vse16_v_f16m1(outptr + j * packn, _sum, vl); |
| 951 | } |
| 952 | |
| 953 | outptr += outw * packn; |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | if (elempack == 1) |
| 960 | { |
| 961 | { |
| 962 | #pragma omp parallel for num_threads(opt.num_threads) |
| 963 | for (int g = 0; g < channels; g++) |
| 964 | { |
| 965 | __fp16* outptr = top_blob_bordered.channel(g); |
| 966 | const __fp16* kptr = (const __fp16*)weight_data_tm + maxk * g; |
| 967 | const Mat m = bottom_blob.channel(g); |
| 968 | |
| 969 | for (int i = 0; i < outh; i++) |
| 970 | { |
| 971 | for (int j = 0; j < outw; j++) |
| 972 | { |
| 973 | float sum = 0.f; |
| 974 | |
| 975 | if (bias_term) |
| 976 | { |
| 977 | sum = bias_data[g]; |
| 978 | } |
| 979 | |
| 980 | for (int y = 0; y < kernel_h; y++) |
| 981 | { |
| 982 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 983 | if (sys < 0 || sys % stride_h != 0) |
| 984 | continue; |
| 985 | |
| 986 | int sy = sys / stride_h; |
| 987 | if (sy >= h) |
| 988 | continue; |
| 989 | |
| 990 | const __fp16* sptr = m.row<const __fp16>(sy); |
| 991 | |
| 992 | for (int x = 0; x < kernel_w; x++) |
| 993 | { |
| 994 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 995 | if (sxs < 0 || sxs % stride_w != 0) |
| 996 | continue; |
| 997 | |
| 998 | int sx = sxs / stride_w; |
| 999 | if (sx >= w) |
| 1000 | continue; |
| 1001 | |
| 1002 | __fp16 val = sptr[sx]; |
| 1003 | |
| 1004 | int k = y * kernel_w + x; |
| 1005 | |
| 1006 | __fp16 w = kptr[k]; |
| 1007 | |
| 1008 | sum += val * w; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | sum = activation_ss(sum, activation_type, activation_params); |
| 1013 | |
| 1014 | outptr[j] = (__fp16)sum; |
| 1015 | } |
| 1016 | |
| 1017 | outptr += outw; |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | else |
| 1024 | { |
| 1025 | // group deconvolution |
| 1026 | const int channels_g = channels * elempack / group; |
| 1027 | const int num_output_g = num_output / group; |
| 1028 | |
| 1029 | int g_elempack = (opt.use_packing_layout && channels_g % packn == 0) ? packn : 1; |
| 1030 | int out_g_elempack = (opt.use_packing_layout && num_output_g % packn == 0) ? packn : 1; |
| 1031 | |
| 1032 | // unpacking |
| 1033 | Mat bottom_blob_unpacked = bottom_blob; |
| 1034 | if (elempack > g_elempack) |
| 1035 | { |
| 1036 | Option opt_p = opt; |
| 1037 | opt_p.blob_allocator = opt.workspace_allocator; |
| 1038 | convert_packing(bottom_blob, bottom_blob_unpacked, g_elempack, opt_p); |
| 1039 | } |
| 1040 | |
| 1041 | Mat top_blob_bordered_unpacked = top_blob_bordered; |
| 1042 | if (out_g_elempack < out_elempack) |
| 1043 | { |
| 1044 | 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); |
| 1045 | if (top_blob_bordered_unpacked.empty()) |
| 1046 | return -100; |
| 1047 | } |
| 1048 | |
| 1049 | for (int g = 0; g < group; g++) |
| 1050 | { |
| 1051 | const Mat bottom_blob_g = bottom_blob_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack); |
| 1052 | Mat top_blob_bordered_g = top_blob_bordered_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack); |
| 1053 | |
| 1054 | const ncnn::Layer* op = group_ops[g]; |
| 1055 | |
| 1056 | Option opt_g = opt; |
| 1057 | opt_g.blob_allocator = top_blob_bordered_unpacked.allocator; |
| 1058 | |
| 1059 | // forward |
| 1060 | op->forward(bottom_blob_g, top_blob_bordered_g, opt_g); |
| 1061 | } |
| 1062 | |
| 1063 | // packing |
| 1064 | if (out_g_elempack < out_elempack) |
| 1065 | { |
| 1066 | convert_packing(top_blob_bordered_unpacked, top_blob_bordered, out_elempack, opt); |
| 1067 | } |
| 1068 | else |
| 1069 | { |
| 1070 | top_blob_bordered = top_blob_bordered_unpacked; |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | cut_padding(top_blob_bordered, top_blob, opt); |
| 1075 | if (top_blob.empty()) |
| 1076 | return -100; |
| 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | #endif // __riscv_vector && __riscv_zfh |
| 1081 | |
| 1082 | } // namespace ncnn |