| 1 | // Xavier Hsinyuan is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 Xavier Hsinyuan <me@lstlx.com>. 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 "batchnorm_riscv.h" |
| 16 | |
| 17 | #if __riscv_vector |
| 18 | #include <riscv_vector.h> |
| 19 | #endif // __riscv_vector |
| 20 | |
| 21 | #include "riscv_usability.h" |
| 22 | |
| 23 | namespace ncnn { |
| 24 | |
| 25 | BatchNorm_riscv::BatchNorm_riscv() |
| 26 | { |
| 27 | #if __riscv_vector |
| 28 | support_packing = true; |
| 29 | #if __riscv_zfh |
| 30 | support_fp16_storage = true; |
| 31 | #endif |
| 32 | #endif // __riscv_vector |
| 33 | } |
| 34 | |
| 35 | int BatchNorm_riscv::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 36 | { |
| 37 | #if __riscv_vector |
| 38 | int elembits = bottom_top_blob.elembits(); |
| 39 | |
| 40 | #if __riscv_zfh |
| 41 | if (opt.use_fp16_storage && elembits == 16) |
| 42 | { |
| 43 | if (opt.use_fp16_arithmetic) |
| 44 | return forward_inplace_fp16sa(bottom_top_blob, opt); |
| 45 | else |
| 46 | return forward_inplace_fp16s(bottom_top_blob, opt); |
| 47 | } |
| 48 | #endif |
| 49 | int elempack = bottom_top_blob.elempack; |
| 50 | #endif // __riscv_vector |
| 51 | int dims = bottom_top_blob.dims; |
| 52 | if (dims == 1) |
| 53 | { |
| 54 | float* ptr = bottom_top_blob; |
| 55 | #if __riscv_vector |
| 56 | const float* ptr_a = a_data; |
| 57 | const float* ptr_b = b_data; |
| 58 | int n = bottom_top_blob.w * elempack; |
| 59 | while (n > 0) |
| 60 | { |
| 61 | size_t vl = vsetvl_e32m8(n); |
| 62 | |
| 63 | vfloat32m8_t _p = vle32_v_f32m8(ptr, vl); |
| 64 | vfloat32m8_t _a = vle32_v_f32m8(ptr_a, vl); |
| 65 | vfloat32m8_t _b = vle32_v_f32m8(ptr_b, vl); |
| 66 | |
| 67 | _p = vfmadd_vv_f32m8(_p, _b, _a, vl); |
| 68 | |
| 69 | vse32_v_f32m8(ptr, _p, vl); |
| 70 | |
| 71 | ptr += vl; |
| 72 | ptr_a += vl; |
| 73 | ptr_b += vl; |
| 74 | n -= vl; |
| 75 | } |
| 76 | #else |
| 77 | int w = bottom_top_blob.w; |
| 78 | #pragma omp parallel for num_threads(opt.num_threads) |
| 79 | for (int i = 0; i < w; i++) |
| 80 | { |
| 81 | ptr[i] = b_data[i] * ptr[i] + a_data[i]; |
| 82 | } |
| 83 | #endif // __riscv_vector |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | #if __riscv_vector |
| 88 | if (elempack == 1) |
| 89 | #endif |
| 90 | { |
| 91 | int w = bottom_top_blob.w; |
| 92 | int h = bottom_top_blob.h; |
| 93 | if (dims == 2) |
| 94 | { |
| 95 | #pragma omp parallel for num_threads(opt.num_threads) |
| 96 | for (int i = 0; i < h; i++) |
| 97 | { |
| 98 | float* ptr = bottom_top_blob.row(i); |
| 99 | float a = a_data[i]; |
| 100 | float b = b_data[i]; |
| 101 | |
| 102 | #if __riscv_vector |
| 103 | int n = w; |
| 104 | while (n > 0) |
| 105 | { |
| 106 | size_t vl = vsetvl_e32m8(n); |
| 107 | vfloat32m8_t _p = vle32_v_f32m8(ptr, vl); |
| 108 | _p = vfmul_vf_f32m8(_p, b, vl); |
| 109 | _p = vfadd_vf_f32m8(_p, a, vl); |
| 110 | vse32_v_f32m8(ptr, _p, vl); |
| 111 | |
| 112 | ptr += vl; |
| 113 | n -= vl; |
| 114 | } |
| 115 | #else |
| 116 | for (int j = 0; j < w; j++) |
| 117 | { |
| 118 | ptr[j] = b * ptr[j] + a; |
| 119 | } |
| 120 | #endif // __riscv_vector |
| 121 | } |
| 122 | } |
| 123 | if (dims == 3 || dims == 4) |
| 124 | { |
| 125 | int d = bottom_top_blob.d; |
| 126 | int c = bottom_top_blob.c; |
| 127 | int size = w * h * d; |
| 128 | |
| 129 | #pragma omp parallel for num_threads(opt.num_threads) |
| 130 | for (int q = 0; q < c; q++) |
| 131 | { |
| 132 | float* ptr = bottom_top_blob.channel(q); |
| 133 | float a = a_data[q]; |
| 134 | float b = b_data[q]; |
| 135 | |
| 136 | #if __riscv_vector |
| 137 | int n = size; |
| 138 | while (n > 0) |
| 139 | { |
| 140 | size_t vl = vsetvl_e32m8(n); |
| 141 | vfloat32m8_t _p = vle32_v_f32m8(ptr, vl); |
| 142 | _p = vfmul_vf_f32m8(_p, b, vl); |
| 143 | _p = vfadd_vf_f32m8(_p, a, vl); |
| 144 | vse32_v_f32m8(ptr, _p, vl); |
| 145 | |
| 146 | ptr += vl; |
| 147 | n -= vl; |
| 148 | } |
| 149 | #else |
| 150 | for (int i = 0; i < size; i++) |
| 151 | { |
| 152 | ptr[i] = b * ptr[i] + a; |
| 153 | } |
| 154 | #endif // __riscv_vector |
| 155 | } |
| 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | #if __riscv_vector |
| 161 | const int packn = csrr_vlenb() / 4; |
| 162 | if (elempack == packn) |
| 163 | { |
| 164 | int w = bottom_top_blob.w; |
| 165 | int h = bottom_top_blob.h; |
| 166 | |
| 167 | const size_t vl = vsetvl_e32m1(packn); |
| 168 | if (dims == 2) |
| 169 | { |
| 170 | #pragma omp parallel for num_threads(opt.num_threads) |
| 171 | for (int i = 0; i < h; i++) |
| 172 | { |
| 173 | float* ptr = bottom_top_blob.row(i); |
| 174 | const float* ptr_a = a_data; |
| 175 | ptr_a += i * elempack; |
| 176 | const float* ptr_b = b_data; |
| 177 | ptr_b += i * elempack; |
| 178 | int n = w * elempack; |
| 179 | |
| 180 | vfloat32m1_t _a = vle32_v_f32m1(ptr_a, vl); |
| 181 | vfloat32m1_t _b = vle32_v_f32m1(ptr_b, vl); |
| 182 | while (n > 0) |
| 183 | { |
| 184 | vfloat32m1_t _p = vle32_v_f32m1(ptr, vl); |
| 185 | _p = vfmadd_vv_f32m1(_p, _b, _a, vl); |
| 186 | vse32_v_f32m1(ptr, _p, vl); |
| 187 | |
| 188 | ptr += vl; |
| 189 | n -= vl; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (dims == 3 || dims == 4) |
| 195 | { |
| 196 | int d = bottom_top_blob.d; |
| 197 | int c = bottom_top_blob.c; |
| 198 | int size = w * h * d * elempack; |
| 199 | |
| 200 | #pragma omp parallel for num_threads(opt.num_threads) |
| 201 | for (int q = 0; q < c; q++) |
| 202 | { |
| 203 | float* ptr = bottom_top_blob.channel(q); |
| 204 | const float* ptr_a = (const float*)a_data + q * elempack; |
| 205 | const float* ptr_b = (const float*)b_data + q * elempack; |
| 206 | |
| 207 | vfloat32m1_t _a = vle32_v_f32m1(ptr_a, vl); |
| 208 | vfloat32m1_t _b = vle32_v_f32m1(ptr_b, vl); |
| 209 | |
| 210 | int n = size; |
| 211 | while (n > 0) |
| 212 | { |
| 213 | vfloat32m1_t _p = vle32_v_f32m1(ptr, vl); |
| 214 | _p = vfmadd_vv_f32m1(_p, _b, _a, vl); |
| 215 | vse32_v_f32m1(ptr, _p, vl); |
| 216 | |
| 217 | ptr += vl; |
| 218 | n -= vl; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | #endif |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | #if __riscv_vector && __riscv_zfh |
| 228 | int BatchNorm_riscv::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const |
| 229 | { |
| 230 | int dims = bottom_top_blob.dims; |
| 231 | int elempack = bottom_top_blob.elempack; |
| 232 | if (dims == 1) |
| 233 | { |
| 234 | int n = bottom_top_blob.w * elempack; |
| 235 | __fp16* ptr = bottom_top_blob; |
| 236 | const float* ptr_a = a_data; |
| 237 | const float* ptr_b = b_data; |
| 238 | while (n > 0) |
| 239 | { |
| 240 | size_t vl = vsetvl_e16m4(n); |
| 241 | |
| 242 | vfloat32m8_t _p = vfwcvt_f_f_v_f32m8(vle16_v_f16m4(ptr, vl), vl); |
| 243 | vfloat32m8_t _a = vle32_v_f32m8(ptr_a, vl); |
| 244 | vfloat32m8_t _b = vle32_v_f32m8(ptr_b, vl); |
| 245 | |
| 246 | _p = vfmadd_vv_f32m8(_p, _b, _a, vl); |
| 247 | |
| 248 | vse16_v_f16m4(ptr, vfncvt_f_f_w_f16m4(_p, vl), vl); |
| 249 | |
| 250 | ptr += vl; |
| 251 | ptr_a += vl; |
| 252 | ptr_b += vl; |
| 253 | n -= vl; |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | if (elempack == 1) |
| 260 | { |
| 261 | int w = bottom_top_blob.w; |
| 262 | int h = bottom_top_blob.h; |
| 263 | if (dims == 2) |
| 264 | { |
| 265 | #pragma omp parallel for num_threads(opt.num_threads) |
| 266 | for (int i = 0; i < h; i++) |
| 267 | { |
| 268 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 269 | float a = a_data[i]; |
| 270 | float b = b_data[i]; |
| 271 | |
| 272 | int n = w; |
| 273 | while (n > 0) |
| 274 | { |
| 275 | size_t vl = vsetvl_e16m4(n); |
| 276 | vfloat32m8_t _p = vfwcvt_f_f_v_f32m8(vle16_v_f16m4(ptr, vl), vl); |
| 277 | _p = vfmul_vf_f32m8(_p, b, vl); |
| 278 | _p = vfadd_vf_f32m8(_p, a, vl); |
| 279 | vse16_v_f16m4(ptr, vfncvt_f_f_w_f16m4(_p, vl), vl); |
| 280 | |
| 281 | ptr += vl; |
| 282 | n -= vl; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | if (dims == 3 || dims == 4) |
| 287 | { |
| 288 | int d = bottom_top_blob.d; |
| 289 | int c = bottom_top_blob.c; |
| 290 | int size = w * h * d; |
| 291 | |
| 292 | #pragma omp parallel for num_threads(opt.num_threads) |
| 293 | for (int q = 0; q < c; q++) |
| 294 | { |
| 295 | __fp16* ptr = bottom_top_blob.channel(q); |
| 296 | float a = a_data[q]; |
| 297 | float b = b_data[q]; |
| 298 | |
| 299 | int n = size; |
| 300 | while (n > 0) |
| 301 | { |
| 302 | size_t vl = vsetvl_e16m4(n); |
| 303 | vfloat32m8_t _p = vfwcvt_f_f_v_f32m8(vle16_v_f16m4(ptr, vl), vl); |
| 304 | ; |
| 305 | _p = vfmul_vf_f32m8(_p, b, vl); |
| 306 | _p = vfadd_vf_f32m8(_p, a, vl); |
| 307 | vse16_v_f16m4(ptr, vfncvt_f_f_w_f16m4(_p, vl), vl); |
| 308 | |
| 309 | ptr += vl; |
| 310 | n -= vl; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | const int packn = csrr_vlenb() / 2; // fp16 |
| 319 | if (elempack == packn) |
| 320 | { |
| 321 | int w = bottom_top_blob.w; |
| 322 | int h = bottom_top_blob.h; |
| 323 | |
| 324 | const size_t vl = vsetvl_e16m1(packn); |
| 325 | if (dims == 2) |
| 326 | { |
| 327 | #pragma omp parallel for num_threads(opt.num_threads) |
| 328 | for (int i = 0; i < h; i++) |
| 329 | { |
| 330 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 331 | const float* ptr_a = (const float*)a_data + i * elempack; |
| 332 | const float* ptr_b = (const float*)b_data + i * elempack; |
| 333 | int n = w * elempack; |
| 334 | |
| 335 | vfloat32m2_t _a = vle32_v_f32m2(ptr_a, vl); |
| 336 | vfloat32m2_t _b = vle32_v_f32m2(ptr_b, vl); |
| 337 | while (n > 0) |
| 338 | { |
| 339 | vfloat32m2_t _p = vfwcvt_f_f_v_f32m2(vle16_v_f16m1(ptr, vl), vl); |
| 340 | _p = vfmadd_vv_f32m2(_p, _b, _a, vl); |
| 341 | vse16_v_f16m1(ptr, vfncvt_f_f_w_f16m1(_p, vl), vl); |
| 342 | |
| 343 | ptr += vl; |
| 344 | n -= vl; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | if (dims == 3 || dims == 4) |
| 350 | { |
| 351 | int d = bottom_top_blob.d; |
| 352 | int c = bottom_top_blob.c; |
| 353 | int size = w * h * d * elempack; |
| 354 | |
| 355 | #pragma omp parallel for num_threads(opt.num_threads) |
| 356 | for (int q = 0; q < c; q++) |
| 357 | { |
| 358 | __fp16* ptr = bottom_top_blob.channel(q); |
| 359 | const float* ptr_a = (const float*)a_data + q * elempack; |
| 360 | const float* ptr_b = (const float*)b_data + q * elempack; |
| 361 | |
| 362 | vfloat32m2_t _a = vle32_v_f32m2(ptr_a, vl); |
| 363 | vfloat32m2_t _b = vle32_v_f32m2(ptr_b, vl); |
| 364 | |
| 365 | int n = size; |
| 366 | while (n > 0) |
| 367 | { |
| 368 | vfloat32m2_t _p = vfwcvt_f_f_v_f32m2(vle16_v_f16m1(ptr, vl), vl); |
| 369 | _p = vfmadd_vv_f32m2(_p, _b, _a, vl); |
| 370 | vse16_v_f16m1(ptr, vfncvt_f_f_w_f16m1(_p, vl), vl); |
| 371 | |
| 372 | ptr += vl; |
| 373 | n -= vl; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | int BatchNorm_riscv::forward_inplace_fp16sa(Mat& bottom_top_blob, const Option& opt) const |
| 383 | { |
| 384 | int dims = bottom_top_blob.dims; |
| 385 | int elempack = bottom_top_blob.elempack; |
| 386 | if (dims == 1) |
| 387 | { |
| 388 | int n = bottom_top_blob.w * elempack; |
| 389 | __fp16* ptr = bottom_top_blob; |
| 390 | const float* ptr_a = a_data; |
| 391 | const float* ptr_b = b_data; |
| 392 | while (n > 0) |
| 393 | { |
| 394 | size_t vl = vsetvl_e16m4(n); |
| 395 | |
| 396 | vfloat16m4_t _p = vle16_v_f16m4(ptr, vl); |
| 397 | vfloat16m4_t _a = vfncvt_f_f_w_f16m4(vle32_v_f32m8(ptr_a, vl), vl); |
| 398 | vfloat16m4_t _b = vfncvt_f_f_w_f16m4(vle32_v_f32m8(ptr_b, vl), vl); |
| 399 | |
| 400 | _p = vfmadd_vv_f16m4(_p, _b, _a, vl); |
| 401 | |
| 402 | vse16_v_f16m4(ptr, _p, vl); |
| 403 | |
| 404 | ptr += vl; |
| 405 | ptr_a += vl; |
| 406 | ptr_b += vl; |
| 407 | n -= vl; |
| 408 | } |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | if (elempack == 1) |
| 414 | { |
| 415 | int w = bottom_top_blob.w; |
| 416 | int h = bottom_top_blob.h; |
| 417 | if (dims == 2) |
| 418 | { |
| 419 | #pragma omp parallel for num_threads(opt.num_threads) |
| 420 | for (int i = 0; i < h; i++) |
| 421 | { |
| 422 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 423 | float a = a_data[i]; |
| 424 | float b = b_data[i]; |
| 425 | |
| 426 | int n = w; |
| 427 | while (n > 0) |
| 428 | { |
| 429 | size_t vl = vsetvl_e16m8(n); |
| 430 | vfloat16m8_t _p = vle16_v_f16m8(ptr, vl); |
| 431 | _p = vfmul_vf_f16m8(_p, b, vl); |
| 432 | _p = vfadd_vf_f16m8(_p, a, vl); |
| 433 | vse16_v_f16m8(ptr, _p, vl); |
| 434 | |
| 435 | ptr += vl; |
| 436 | n -= vl; |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | if (dims == 3 || dims == 4) |
| 441 | { |
| 442 | int d = bottom_top_blob.d; |
| 443 | int c = bottom_top_blob.c; |
| 444 | int size = w * h * d; |
| 445 | |
| 446 | #pragma omp parallel for num_threads(opt.num_threads) |
| 447 | for (int q = 0; q < c; q++) |
| 448 | { |
| 449 | __fp16* ptr = bottom_top_blob.channel(q); |
| 450 | float a = a_data[q]; |
| 451 | float b = b_data[q]; |
| 452 | |
| 453 | int n = size; |
| 454 | while (n > 0) |
| 455 | { |
| 456 | size_t vl = vsetvl_e16m8(n); |
| 457 | vfloat16m8_t _p = vle16_v_f16m8(ptr, vl); |
| 458 | ; |
| 459 | _p = vfmul_vf_f16m8(_p, b, vl); |
| 460 | _p = vfadd_vf_f16m8(_p, a, vl); |
| 461 | vse16_v_f16m8(ptr, _p, vl); |
| 462 | |
| 463 | ptr += vl; |
| 464 | n -= vl; |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | const int packn = csrr_vlenb() / 2; // fp16 |
| 473 | if (elempack == packn) |
| 474 | { |
| 475 | int w = bottom_top_blob.w; |
| 476 | int h = bottom_top_blob.h; |
| 477 | |
| 478 | const size_t vl = vsetvl_e16m1(packn); |
| 479 | if (dims == 2) |
| 480 | { |
| 481 | #pragma omp parallel for num_threads(opt.num_threads) |
| 482 | for (int i = 0; i < h; i++) |
| 483 | { |
| 484 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 485 | const float* ptr_a = (const float*)a_data + i * elempack; |
| 486 | const float* ptr_b = (const float*)b_data + i * elempack; |
| 487 | int n = w * elempack; |
| 488 | |
| 489 | vfloat16m1_t _a = vfncvt_f_f_w_f16m1(vle32_v_f32m2(ptr_a, vl), vl); |
| 490 | vfloat16m1_t _b = vfncvt_f_f_w_f16m1(vle32_v_f32m2(ptr_b, vl), vl); |
| 491 | while (n > 0) |
| 492 | { |
| 493 | vfloat16m1_t _p = vle16_v_f16m1(ptr, vl); |
| 494 | _p = vfmadd_vv_f16m1(_p, _b, _a, vl); |
| 495 | vse16_v_f16m1(ptr, _p, vl); |
| 496 | |
| 497 | ptr += vl; |
| 498 | n -= vl; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | if (dims == 3 || dims == 4) |
| 504 | { |
| 505 | int d = bottom_top_blob.d; |
| 506 | int c = bottom_top_blob.c; |
| 507 | int size = w * h * d * elempack; |
| 508 | |
| 509 | #pragma omp parallel for num_threads(opt.num_threads) |
| 510 | for (int q = 0; q < c; q++) |
| 511 | { |
| 512 | __fp16* ptr = bottom_top_blob.channel(q); |
| 513 | const float* ptr_a = (const float*)a_data + q * elempack; |
| 514 | const float* ptr_b = (const float*)b_data + q * elempack; |
| 515 | |
| 516 | vfloat16m1_t _a = vfncvt_f_f_w_f16m1(vle32_v_f32m2(ptr_a, vl), vl); |
| 517 | vfloat16m1_t _b = vfncvt_f_f_w_f16m1(vle32_v_f32m2(ptr_b, vl), vl); |
| 518 | |
| 519 | int n = size; |
| 520 | while (n > 0) |
| 521 | { |
| 522 | vfloat16m1_t _p = vle16_v_f16m1(ptr, vl); |
| 523 | _p = vfmadd_vv_f16m1(_p, _b, _a, vl); |
| 524 | vse16_v_f16m1(ptr, _p, vl); |
| 525 | |
| 526 | ptr += vl; |
| 527 | n -= vl; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | #endif // __riscv_vector && __riscv_zfh |
| 537 | } // namespace ncnn |