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