| 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 "softmax_arm.h" |
| 16 | |
| 17 | #include <float.h> |
| 18 | |
| 19 | #if __ARM_NEON |
| 20 | #include <arm_neon.h> |
| 21 | #include "neon_mathfun.h" |
| 22 | #endif // __ARM_NEON |
| 23 | |
| 24 | #include "arm_usability.h" |
| 25 | #include "cpu.h" |
| 26 | |
| 27 | namespace ncnn { |
| 28 | |
| 29 | Softmax_arm::Softmax_arm() |
| 30 | { |
| 31 | #if __ARM_NEON |
| 32 | support_packing = true; |
| 33 | #if NCNN_ARM82 |
| 34 | support_fp16_storage = cpu_support_arm_asimdhp(); |
| 35 | #endif |
| 36 | #endif // __ARM_NEON |
| 37 | |
| 38 | #if NCNN_BF16 |
| 39 | support_bf16_storage = true; |
| 40 | #endif |
| 41 | } |
| 42 | |
| 43 | int Softmax_arm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 44 | { |
| 45 | int elembits = bottom_top_blob.elembits(); |
| 46 | |
| 47 | #if NCNN_ARM82 |
| 48 | if (support_fp16_storage && opt.use_fp16_storage && elembits == 16) |
| 49 | return forward_inplace_fp16s(bottom_top_blob, opt); |
| 50 | #endif |
| 51 | |
| 52 | #if NCNN_BF16 |
| 53 | if (opt.use_bf16_storage && elembits == 16) |
| 54 | return forward_inplace_bf16s(bottom_top_blob, opt); |
| 55 | #endif |
| 56 | |
| 57 | const int dims = bottom_top_blob.dims; |
| 58 | const int elempack = bottom_top_blob.elempack; |
| 59 | const int positive_axis = axis < 0 ? dims + axis : axis; |
| 60 | |
| 61 | if (dims == 1) // positive_axis == 0 |
| 62 | { |
| 63 | const int w = bottom_top_blob.w; |
| 64 | const int size = w * elempack; |
| 65 | |
| 66 | float max = -FLT_MAX; |
| 67 | #if __ARM_NEON |
| 68 | float32x4_t _max = vdupq_n_f32(-FLT_MAX); |
| 69 | #endif // __ARM_NEON |
| 70 | { |
| 71 | const float* ptr = bottom_top_blob; |
| 72 | |
| 73 | int i = 0; |
| 74 | #if __ARM_NEON |
| 75 | for (; i + 3 < size; i += 4) |
| 76 | { |
| 77 | float32x4_t _p = vld1q_f32(ptr); |
| 78 | _max = vmaxq_f32(_max, _p); |
| 79 | ptr += 4; |
| 80 | } |
| 81 | #endif // __ARM_NEON |
| 82 | for (; i < size; i++) |
| 83 | { |
| 84 | max = std::max(max, *ptr); |
| 85 | ptr++; |
| 86 | } |
| 87 | |
| 88 | #if __ARM_NEON |
| 89 | #if __aarch64__ |
| 90 | max = std::max(max, vmaxvq_f32(_max)); |
| 91 | #else |
| 92 | float32x2_t _max2 = vmax_f32(vget_low_f32(_max), vget_high_f32(_max)); |
| 93 | float32x2_t _mm2 = vpmax_f32(_max2, _max2); |
| 94 | max = std::max(max, vget_lane_f32(_mm2, 0)); |
| 95 | #endif |
| 96 | _max = vdupq_n_f32(max); |
| 97 | #endif // __ARM_NEON |
| 98 | } |
| 99 | |
| 100 | float sum = 0.f; |
| 101 | #if __ARM_NEON |
| 102 | float32x4_t _sum = vdupq_n_f32(0.f); |
| 103 | #endif // __ARM_NEON |
| 104 | { |
| 105 | float* ptr = bottom_top_blob; |
| 106 | |
| 107 | int i = 0; |
| 108 | #if __ARM_NEON |
| 109 | for (; i + 3 < size; i += 4) |
| 110 | { |
| 111 | float32x4_t _p = vld1q_f32(ptr); |
| 112 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 113 | vst1q_f32(ptr, _p); |
| 114 | _sum = vaddq_f32(_sum, _p); |
| 115 | ptr += 4; |
| 116 | } |
| 117 | #endif // __ARM_NEON |
| 118 | for (; i < size; i++) |
| 119 | { |
| 120 | *ptr = (float)expf(*ptr - max); |
| 121 | sum += *ptr; |
| 122 | ptr++; |
| 123 | } |
| 124 | |
| 125 | #if __ARM_NEON |
| 126 | #if __aarch64__ |
| 127 | sum += vaddvq_f32(_sum); |
| 128 | #else |
| 129 | float32x2_t _sum2 = vadd_f32(vget_low_f32(_sum), vget_high_f32(_sum)); |
| 130 | float32x2_t _ss2 = vpadd_f32(_sum2, _sum2); |
| 131 | sum += vget_lane_f32(_ss2, 0); |
| 132 | #endif |
| 133 | _sum = vdupq_n_f32(sum); |
| 134 | #endif // __ARM_NEON |
| 135 | } |
| 136 | |
| 137 | { |
| 138 | float* ptr = bottom_top_blob; |
| 139 | |
| 140 | int i = 0; |
| 141 | #if __ARM_NEON |
| 142 | float32x4_t _reciprocal_sum = vrecpeq_f32(_sum); |
| 143 | _reciprocal_sum = vmulq_f32(vrecpsq_f32(_sum, _reciprocal_sum), _reciprocal_sum); |
| 144 | for (; i + 3 < size; i += 4) |
| 145 | { |
| 146 | float32x4_t _p = vld1q_f32(ptr); |
| 147 | _p = vmulq_f32(_p, _reciprocal_sum); |
| 148 | vst1q_f32(ptr, _p); |
| 149 | ptr += 4; |
| 150 | } |
| 151 | #endif // __ARM_NEON |
| 152 | for (; i < size; i++) |
| 153 | { |
| 154 | *ptr = *ptr / sum; |
| 155 | ptr++; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (dims == 2 && positive_axis == 0) |
| 161 | { |
| 162 | const int w = bottom_top_blob.w; |
| 163 | const int h = bottom_top_blob.h; |
| 164 | |
| 165 | Mat maxsum(w, 1, 2, 4u, opt.workspace_allocator); |
| 166 | if (maxsum.empty()) |
| 167 | return -100; |
| 168 | |
| 169 | Mat max = maxsum.channel(0); |
| 170 | max.fill(-FLT_MAX); |
| 171 | |
| 172 | for (int i = 0; i < h; i++) |
| 173 | { |
| 174 | const float* ptr = bottom_top_blob.row(i); |
| 175 | float* maxptr = max; |
| 176 | |
| 177 | #if __ARM_NEON |
| 178 | if (elempack == 4) |
| 179 | { |
| 180 | for (int j = 0; j < w; j++) |
| 181 | { |
| 182 | float32x4_t _p = vld1q_f32(ptr); |
| 183 | #if __aarch64__ |
| 184 | float max0 = vmaxvq_f32(_p); |
| 185 | #else |
| 186 | float32x2_t _max2 = vmax_f32(vget_low_f32(_p), vget_high_f32(_p)); |
| 187 | float32x2_t _mm2 = vpmax_f32(_max2, _max2); |
| 188 | float max0 = vget_lane_f32(_mm2, 0); |
| 189 | #endif |
| 190 | *maxptr = std::max(*maxptr, max0); |
| 191 | ptr += 4; |
| 192 | maxptr++; |
| 193 | } |
| 194 | } |
| 195 | #endif // __ARM_NEON |
| 196 | if (elempack == 1) |
| 197 | { |
| 198 | int j = 0; |
| 199 | #if __ARM_NEON |
| 200 | for (; j + 3 < w; j += 4) |
| 201 | { |
| 202 | float32x4_t _p = vld1q_f32(ptr); |
| 203 | float32x4_t _max = vld1q_f32(maxptr); |
| 204 | _max = vmaxq_f32(_max, _p); |
| 205 | vst1q_f32(maxptr, _max); |
| 206 | ptr += 4; |
| 207 | maxptr += 4; |
| 208 | } |
| 209 | #endif // __ARM_NEON |
| 210 | for (; j < w; j++) |
| 211 | { |
| 212 | *maxptr = std::max(*maxptr, *ptr); |
| 213 | ptr++; |
| 214 | maxptr++; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | Mat sum = maxsum.channel(1); |
| 220 | sum.fill(0.f); |
| 221 | |
| 222 | for (int i = 0; i < h; i++) |
| 223 | { |
| 224 | float* ptr = bottom_top_blob.row(i); |
| 225 | const float* maxptr = max; |
| 226 | float* sumptr = sum; |
| 227 | |
| 228 | #if __ARM_NEON |
| 229 | if (elempack == 4) |
| 230 | { |
| 231 | for (int j = 0; j < w; j++) |
| 232 | { |
| 233 | float32x4_t _p = vld1q_f32(ptr); |
| 234 | float32x4_t _max = vdupq_n_f32(*maxptr); |
| 235 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 236 | vst1q_f32(ptr, _p); |
| 237 | #if __aarch64__ |
| 238 | float sum0 = vaddvq_f32(_p); |
| 239 | #else |
| 240 | float32x2_t _sum2 = vadd_f32(vget_low_f32(_p), vget_high_f32(_p)); |
| 241 | float32x2_t _ss2 = vpadd_f32(_sum2, _sum2); |
| 242 | float sum0 = vget_lane_f32(_ss2, 0); |
| 243 | #endif |
| 244 | *sumptr += sum0; |
| 245 | ptr += 4; |
| 246 | maxptr++; |
| 247 | sumptr++; |
| 248 | } |
| 249 | } |
| 250 | #endif // __ARM_NEON |
| 251 | if (elempack == 1) |
| 252 | { |
| 253 | int j = 0; |
| 254 | #if __ARM_NEON |
| 255 | for (; j + 3 < w; j += 4) |
| 256 | { |
| 257 | float32x4_t _p = vld1q_f32(ptr); |
| 258 | float32x4_t _max = vld1q_f32(maxptr); |
| 259 | float32x4_t _sum = vld1q_f32(sumptr); |
| 260 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 261 | _sum = vaddq_f32(_sum, _p); |
| 262 | vst1q_f32(ptr, _p); |
| 263 | vst1q_f32(sumptr, _sum); |
| 264 | ptr += 4; |
| 265 | maxptr += 4; |
| 266 | sumptr += 4; |
| 267 | } |
| 268 | #endif // __ARM_NEON |
| 269 | for (; j < w; j++) |
| 270 | { |
| 271 | *ptr = (float)expf(*ptr - *maxptr); |
| 272 | *sumptr += *ptr; |
| 273 | ptr++; |
| 274 | maxptr++; |
| 275 | sumptr++; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | for (int i = 0; i < h; i++) |
| 281 | { |
| 282 | float* ptr = bottom_top_blob.row(i); |
| 283 | const float* sumptr = sum; |
| 284 | |
| 285 | #if __ARM_NEON |
| 286 | if (elempack == 4) |
| 287 | { |
| 288 | for (int j = 0; j < w; j++) |
| 289 | { |
| 290 | float32x4_t _p = vld1q_f32(ptr); |
| 291 | float32x4_t _sum = vld1q_dup_f32(sumptr); |
| 292 | _p = div_ps(_p, _sum); |
| 293 | vst1q_f32(ptr, _p); |
| 294 | ptr += 4; |
| 295 | sumptr++; |
| 296 | } |
| 297 | } |
| 298 | #endif // __ARM_NEON |
| 299 | if (elempack == 1) |
| 300 | { |
| 301 | int j = 0; |
| 302 | #if __ARM_NEON |
| 303 | for (; j + 3 < w; j += 4) |
| 304 | { |
| 305 | float32x4_t _p = vld1q_f32(ptr); |
| 306 | float32x4_t _sum = vld1q_f32(sumptr); |
| 307 | _p = div_ps(_p, _sum); |
| 308 | vst1q_f32(ptr, _p); |
| 309 | ptr += 4; |
| 310 | sumptr += 4; |
| 311 | } |
| 312 | #endif // __ARM_NEON |
| 313 | for (; j < w; j++) |
| 314 | { |
| 315 | *ptr /= *sumptr; |
| 316 | ptr++; |
| 317 | sumptr++; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if (dims == 2 && positive_axis == 1) |
| 324 | { |
| 325 | const int w = bottom_top_blob.w; |
| 326 | const int h = bottom_top_blob.h; |
| 327 | const int size = w * elempack; |
| 328 | |
| 329 | #pragma omp parallel for num_threads(opt.num_threads) |
| 330 | for (int i = 0; i < h; i++) |
| 331 | { |
| 332 | float max = -FLT_MAX; |
| 333 | #if __ARM_NEON |
| 334 | float32x4_t _max = vdupq_n_f32(-FLT_MAX); |
| 335 | #endif // __ARM_NEON |
| 336 | { |
| 337 | const float* ptr = bottom_top_blob.row(i); |
| 338 | |
| 339 | int j = 0; |
| 340 | #if __ARM_NEON |
| 341 | for (; j + 3 < size; j += 4) |
| 342 | { |
| 343 | float32x4_t _p = vld1q_f32(ptr); |
| 344 | _max = vmaxq_f32(_max, _p); |
| 345 | ptr += 4; |
| 346 | } |
| 347 | #endif // __ARM_NEON |
| 348 | for (; j < size; j++) |
| 349 | { |
| 350 | max = std::max(max, *ptr); |
| 351 | ptr++; |
| 352 | } |
| 353 | |
| 354 | #if __ARM_NEON |
| 355 | if (elempack == 1) |
| 356 | { |
| 357 | #if __aarch64__ |
| 358 | max = std::max(max, vmaxvq_f32(_max)); |
| 359 | #else |
| 360 | float32x2_t _max2 = vmax_f32(vget_low_f32(_max), vget_high_f32(_max)); |
| 361 | float32x2_t _mm2 = vpmax_f32(_max2, _max2); |
| 362 | max = std::max(max, vget_lane_f32(_mm2, 0)); |
| 363 | #endif |
| 364 | _max = vdupq_n_f32(max); |
| 365 | } |
| 366 | #endif // __ARM_NEON |
| 367 | } |
| 368 | |
| 369 | float sum = 0.f; |
| 370 | #if __ARM_NEON |
| 371 | float32x4_t _sum = vdupq_n_f32(0.f); |
| 372 | #endif // __ARM_NEON |
| 373 | { |
| 374 | float* ptr = bottom_top_blob.row(i); |
| 375 | |
| 376 | int j = 0; |
| 377 | #if __ARM_NEON |
| 378 | for (; j + 3 < size; j += 4) |
| 379 | { |
| 380 | float32x4_t _p = vld1q_f32(ptr); |
| 381 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 382 | vst1q_f32(ptr, _p); |
| 383 | _sum = vaddq_f32(_sum, _p); |
| 384 | ptr += 4; |
| 385 | } |
| 386 | #endif // __ARM_NEON |
| 387 | for (; j < size; j++) |
| 388 | { |
| 389 | *ptr = (float)expf(*ptr - max); |
| 390 | sum += *ptr; |
| 391 | ptr++; |
| 392 | } |
| 393 | |
| 394 | #if __ARM_NEON |
| 395 | if (elempack == 1) |
| 396 | { |
| 397 | #if __aarch64__ |
| 398 | sum += vaddvq_f32(_sum); |
| 399 | #else |
| 400 | float32x2_t _sum2 = vadd_f32(vget_low_f32(_sum), vget_high_f32(_sum)); |
| 401 | float32x2_t _ss2 = vpadd_f32(_sum2, _sum2); |
| 402 | sum += vget_lane_f32(_ss2, 0); |
| 403 | #endif |
| 404 | _sum = vdupq_n_f32(sum); |
| 405 | } |
| 406 | #endif // __ARM_NEON |
| 407 | } |
| 408 | |
| 409 | { |
| 410 | float* ptr = bottom_top_blob.row(i); |
| 411 | |
| 412 | int j = 0; |
| 413 | #if __ARM_NEON |
| 414 | float32x4_t _reciprocal_sum = vrecpeq_f32(_sum); |
| 415 | _reciprocal_sum = vmulq_f32(vrecpsq_f32(_sum, _reciprocal_sum), _reciprocal_sum); |
| 416 | for (; j + 3 < size; j += 4) |
| 417 | { |
| 418 | float32x4_t _p = vld1q_f32(ptr); |
| 419 | _p = vmulq_f32(_p, _reciprocal_sum); |
| 420 | vst1q_f32(ptr, _p); |
| 421 | ptr += 4; |
| 422 | } |
| 423 | #endif // __ARM_NEON |
| 424 | for (; j < size; j++) |
| 425 | { |
| 426 | *ptr /= sum; |
| 427 | ptr++; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | if (dims == 3 && positive_axis == 0) |
| 434 | { |
| 435 | const int w = bottom_top_blob.w; |
| 436 | const int h = bottom_top_blob.h; |
| 437 | const int channels = bottom_top_blob.c; |
| 438 | const int size = w * h; |
| 439 | |
| 440 | Mat maxsum(w, h, 2, 4u, opt.workspace_allocator); |
| 441 | if (maxsum.empty()) |
| 442 | return -100; |
| 443 | |
| 444 | Mat max = maxsum.channel(0); |
| 445 | max.fill(-FLT_MAX); |
| 446 | |
| 447 | for (int q = 0; q < channels; q++) |
| 448 | { |
| 449 | const float* ptr = bottom_top_blob.channel(q); |
| 450 | float* maxptr = max; |
| 451 | |
| 452 | #if __ARM_NEON |
| 453 | if (elempack == 4) |
| 454 | { |
| 455 | for (int i = 0; i < size; i++) |
| 456 | { |
| 457 | float32x4_t _p = vld1q_f32(ptr); |
| 458 | #if __aarch64__ |
| 459 | float max0 = vmaxvq_f32(_p); |
| 460 | #else |
| 461 | float32x2_t _max2 = vmax_f32(vget_low_f32(_p), vget_high_f32(_p)); |
| 462 | float32x2_t _mm2 = vpmax_f32(_max2, _max2); |
| 463 | float max0 = vget_lane_f32(_mm2, 0); |
| 464 | #endif |
| 465 | *maxptr = std::max(*maxptr, max0); |
| 466 | ptr += 4; |
| 467 | maxptr++; |
| 468 | } |
| 469 | } |
| 470 | #endif // __ARM_NEON |
| 471 | if (elempack == 1) |
| 472 | { |
| 473 | int i = 0; |
| 474 | #if __ARM_NEON |
| 475 | for (; i + 3 < size; i += 4) |
| 476 | { |
| 477 | float32x4_t _p = vld1q_f32(ptr); |
| 478 | float32x4_t _max = vld1q_f32(maxptr); |
| 479 | _max = vmaxq_f32(_max, _p); |
| 480 | vst1q_f32(maxptr, _max); |
| 481 | ptr += 4; |
| 482 | maxptr += 4; |
| 483 | } |
| 484 | #endif // __ARM_NEON |
| 485 | for (; i < size; i++) |
| 486 | { |
| 487 | *maxptr = std::max(*maxptr, *ptr); |
| 488 | ptr++; |
| 489 | maxptr++; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | #pragma omp parallel for num_threads(opt.num_threads) |
| 495 | for (int q = 0; q < channels; q++) |
| 496 | { |
| 497 | float* ptr = bottom_top_blob.channel(q); |
| 498 | const float* maxptr = max; |
| 499 | |
| 500 | #if __ARM_NEON |
| 501 | if (elempack == 4) |
| 502 | { |
| 503 | for (int i = 0; i < size; i++) |
| 504 | { |
| 505 | float32x4_t _p = vld1q_f32(ptr); |
| 506 | float32x4_t _max = vdupq_n_f32(*maxptr); |
| 507 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 508 | vst1q_f32(ptr, _p); |
| 509 | ptr += 4; |
| 510 | maxptr++; |
| 511 | } |
| 512 | } |
| 513 | #endif // __ARM_NEON |
| 514 | if (elempack == 1) |
| 515 | { |
| 516 | int i = 0; |
| 517 | #if __ARM_NEON |
| 518 | for (; i + 3 < size; i += 4) |
| 519 | { |
| 520 | float32x4_t _p = vld1q_f32(ptr); |
| 521 | float32x4_t _max = vld1q_f32(maxptr); |
| 522 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 523 | vst1q_f32(ptr, _p); |
| 524 | ptr += 4; |
| 525 | maxptr += 4; |
| 526 | } |
| 527 | #endif // __ARM_NEON |
| 528 | for (; i < size; i++) |
| 529 | { |
| 530 | *ptr = (float)expf(*ptr - *maxptr); |
| 531 | ptr++; |
| 532 | maxptr++; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | Mat sum = maxsum.channel(1); |
| 538 | sum.fill(0.f); |
| 539 | |
| 540 | for (int q = 0; q < channels; q++) |
| 541 | { |
| 542 | float* ptr = bottom_top_blob.channel(q); |
| 543 | float* sumptr = sum; |
| 544 | |
| 545 | #if __ARM_NEON |
| 546 | if (elempack == 4) |
| 547 | { |
| 548 | for (int i = 0; i < size; i++) |
| 549 | { |
| 550 | float32x4_t _p = vld1q_f32(ptr); |
| 551 | #if __aarch64__ |
| 552 | float sum0 = vaddvq_f32(_p); |
| 553 | #else |
| 554 | float32x2_t _sum2 = vadd_f32(vget_low_f32(_p), vget_high_f32(_p)); |
| 555 | float32x2_t _ss2 = vpadd_f32(_sum2, _sum2); |
| 556 | float sum0 = vget_lane_f32(_ss2, 0); |
| 557 | #endif |
| 558 | *sumptr += sum0; |
| 559 | ptr += 4; |
| 560 | sumptr++; |
| 561 | } |
| 562 | } |
| 563 | #endif // __ARM_NEON |
| 564 | if (elempack == 1) |
| 565 | { |
| 566 | int i = 0; |
| 567 | #if __ARM_NEON |
| 568 | for (; i + 3 < size; i += 4) |
| 569 | { |
| 570 | float32x4_t _p = vld1q_f32(ptr); |
| 571 | float32x4_t _sum = vld1q_f32(sumptr); |
| 572 | _sum = vaddq_f32(_sum, _p); |
| 573 | vst1q_f32(sumptr, _sum); |
| 574 | ptr += 4; |
| 575 | sumptr += 4; |
| 576 | } |
| 577 | #endif // __ARM_NEON |
| 578 | for (; i < size; i++) |
| 579 | { |
| 580 | *sumptr += *ptr; |
| 581 | ptr++; |
| 582 | sumptr++; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | #pragma omp parallel for num_threads(opt.num_threads) |
| 588 | for (int q = 0; q < channels; q++) |
| 589 | { |
| 590 | float* ptr = bottom_top_blob.channel(q); |
| 591 | const float* sumptr = sum; |
| 592 | |
| 593 | #if __ARM_NEON |
| 594 | if (elempack == 4) |
| 595 | { |
| 596 | for (int i = 0; i < size; i++) |
| 597 | { |
| 598 | float32x4_t _p = vld1q_f32(ptr); |
| 599 | float32x4_t _sum = vdupq_n_f32(*sumptr); |
| 600 | _p = div_ps(_p, _sum); |
| 601 | vst1q_f32(ptr, _p); |
| 602 | ptr += 4; |
| 603 | sumptr++; |
| 604 | } |
| 605 | } |
| 606 | #endif // __ARM_NEON |
| 607 | if (elempack == 1) |
| 608 | { |
| 609 | int i = 0; |
| 610 | #if __ARM_NEON |
| 611 | for (; i + 3 < size; i += 4) |
| 612 | { |
| 613 | float32x4_t _p = vld1q_f32(ptr); |
| 614 | float32x4_t _sum = vld1q_f32(sumptr); |
| 615 | _p = div_ps(_p, _sum); |
| 616 | vst1q_f32(ptr, _p); |
| 617 | ptr += 4; |
| 618 | sumptr += 4; |
| 619 | } |
| 620 | #endif // __ARM_NEON |
| 621 | for (; i < size; i++) |
| 622 | { |
| 623 | *ptr /= *sumptr; |
| 624 | ptr++; |
| 625 | sumptr++; |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | if (dims == 3 && positive_axis == 1) |
| 632 | { |
| 633 | const int w = bottom_top_blob.w; |
| 634 | const int h = bottom_top_blob.h; |
| 635 | const int channels = bottom_top_blob.c; |
| 636 | const int size = w * elempack; |
| 637 | |
| 638 | Mat maxsum(w * elempack, channels, 2, 4u, opt.workspace_allocator); |
| 639 | if (maxsum.empty()) |
| 640 | return -100; |
| 641 | |
| 642 | Mat max = maxsum.channel(0); |
| 643 | max.fill(-FLT_MAX); |
| 644 | |
| 645 | #pragma omp parallel for num_threads(opt.num_threads) |
| 646 | for (int q = 0; q < channels; q++) |
| 647 | { |
| 648 | const float* ptr = bottom_top_blob.channel(q); |
| 649 | |
| 650 | for (int i = 0; i < h; i++) |
| 651 | { |
| 652 | float* maxptr = max.row(q); |
| 653 | |
| 654 | int j = 0; |
| 655 | #if __ARM_NEON |
| 656 | for (; j + 3 < size; j += 4) |
| 657 | { |
| 658 | float32x4_t _p = vld1q_f32(ptr); |
| 659 | float32x4_t _max = vld1q_f32(maxptr); |
| 660 | _max = vmaxq_f32(_max, _p); |
| 661 | vst1q_f32(maxptr, _max); |
| 662 | ptr += 4; |
| 663 | maxptr += 4; |
| 664 | } |
| 665 | #endif // __ARM_NEON |
| 666 | for (; j < size; j++) |
| 667 | { |
| 668 | *maxptr = std::max(*maxptr, *ptr); |
| 669 | ptr++; |
| 670 | maxptr++; |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | Mat sum = maxsum.channel(1); |
| 676 | sum.fill(0.f); |
| 677 | |
| 678 | #pragma omp parallel for num_threads(opt.num_threads) |
| 679 | for (int q = 0; q < channels; q++) |
| 680 | { |
| 681 | float* ptr = bottom_top_blob.channel(q); |
| 682 | |
| 683 | for (int i = 0; i < h; i++) |
| 684 | { |
| 685 | const float* maxptr = max.row(q); |
| 686 | float* sumptr = sum.row(q); |
| 687 | |
| 688 | int j = 0; |
| 689 | #if __ARM_NEON |
| 690 | for (; j + 3 < size; j += 4) |
| 691 | { |
| 692 | float32x4_t _p = vld1q_f32(ptr); |
| 693 | float32x4_t _max = vld1q_f32(maxptr); |
| 694 | float32x4_t _sum = vld1q_f32(sumptr); |
| 695 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 696 | _sum = vaddq_f32(_sum, _p); |
| 697 | vst1q_f32(ptr, _p); |
| 698 | vst1q_f32(sumptr, _sum); |
| 699 | ptr += 4; |
| 700 | maxptr += 4; |
| 701 | sumptr += 4; |
| 702 | } |
| 703 | #endif // __ARM_NEON |
| 704 | for (; j < size; j++) |
| 705 | { |
| 706 | *ptr = (float)expf(*ptr - *maxptr); |
| 707 | *sumptr += *ptr; |
| 708 | ptr++; |
| 709 | maxptr++; |
| 710 | sumptr++; |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | #pragma omp parallel for num_threads(opt.num_threads) |
| 716 | for (int q = 0; q < channels; q++) |
| 717 | { |
| 718 | float* ptr = bottom_top_blob.channel(q); |
| 719 | |
| 720 | for (int i = 0; i < h; i++) |
| 721 | { |
| 722 | const float* sumptr = sum.row(q); |
| 723 | |
| 724 | int j = 0; |
| 725 | #if __ARM_NEON |
| 726 | for (; j + 3 < size; j += 4) |
| 727 | { |
| 728 | float32x4_t _p = vld1q_f32(ptr); |
| 729 | float32x4_t _sum = vld1q_f32(sumptr); |
| 730 | _p = div_ps(_p, _sum); |
| 731 | vst1q_f32(ptr, _p); |
| 732 | ptr += 4; |
| 733 | sumptr += 4; |
| 734 | } |
| 735 | #endif // __ARM_NEON |
| 736 | for (; j < size; j++) |
| 737 | { |
| 738 | *ptr /= *sumptr; |
| 739 | ptr++; |
| 740 | sumptr++; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | if (dims == 3 && positive_axis == 2) |
| 747 | { |
| 748 | const int w = bottom_top_blob.w; |
| 749 | const int h = bottom_top_blob.h; |
| 750 | const int channels = bottom_top_blob.c; |
| 751 | const int size = w * elempack; |
| 752 | |
| 753 | #pragma omp parallel for num_threads(opt.num_threads) |
| 754 | for (int q = 0; q < channels; q++) |
| 755 | { |
| 756 | for (int i = 0; i < h; i++) |
| 757 | { |
| 758 | float max = -FLT_MAX; |
| 759 | #if __ARM_NEON |
| 760 | float32x4_t _max = vdupq_n_f32(-FLT_MAX); |
| 761 | #endif // __ARM_NEON |
| 762 | { |
| 763 | const float* ptr = bottom_top_blob.channel(q).row(i); |
| 764 | |
| 765 | int j = 0; |
| 766 | #if __ARM_NEON |
| 767 | for (; j + 3 < size; j += 4) |
| 768 | { |
| 769 | float32x4_t _p = vld1q_f32(ptr); |
| 770 | _max = vmaxq_f32(_max, _p); |
| 771 | ptr += 4; |
| 772 | } |
| 773 | #endif // __ARM_NEON |
| 774 | for (; j < size; j++) |
| 775 | { |
| 776 | max = std::max(max, *ptr); |
| 777 | ptr++; |
| 778 | } |
| 779 | |
| 780 | #if __ARM_NEON |
| 781 | if (elempack == 1) |
| 782 | { |
| 783 | #if __aarch64__ |
| 784 | max = std::max(max, vmaxvq_f32(_max)); |
| 785 | #else |
| 786 | float32x2_t _max2 = vmax_f32(vget_low_f32(_max), vget_high_f32(_max)); |
| 787 | float32x2_t _mm2 = vpmax_f32(_max2, _max2); |
| 788 | max = std::max(max, vget_lane_f32(_mm2, 0)); |
| 789 | #endif |
| 790 | _max = vdupq_n_f32(max); |
| 791 | } |
| 792 | #endif // __ARM_NEON |
| 793 | } |
| 794 | |
| 795 | float sum = 0.f; |
| 796 | #if __ARM_NEON |
| 797 | float32x4_t _sum = vdupq_n_f32(0.f); |
| 798 | #endif // __ARM_NEON |
| 799 | { |
| 800 | float* ptr = bottom_top_blob.channel(q).row(i); |
| 801 | |
| 802 | int j = 0; |
| 803 | #if __ARM_NEON |
| 804 | for (; j + 3 < size; j += 4) |
| 805 | { |
| 806 | float32x4_t _p = vld1q_f32(ptr); |
| 807 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 808 | vst1q_f32(ptr, _p); |
| 809 | _sum = vaddq_f32(_sum, _p); |
| 810 | ptr += 4; |
| 811 | } |
| 812 | #endif // __ARM_NEON |
| 813 | for (; j < size; j++) |
| 814 | { |
| 815 | *ptr = (float)expf(*ptr - max); |
| 816 | sum += *ptr; |
| 817 | ptr++; |
| 818 | } |
| 819 | |
| 820 | #if __ARM_NEON |
| 821 | if (elempack == 1) |
| 822 | { |
| 823 | #if __aarch64__ |
| 824 | sum += vaddvq_f32(_sum); |
| 825 | #else |
| 826 | float32x2_t _sum2 = vadd_f32(vget_low_f32(_sum), vget_high_f32(_sum)); |
| 827 | float32x2_t _ss2 = vpadd_f32(_sum2, _sum2); |
| 828 | sum += vget_lane_f32(_ss2, 0); |
| 829 | #endif |
| 830 | _sum = vdupq_n_f32(sum); |
| 831 | } |
| 832 | #endif // __ARM_NEON |
| 833 | } |
| 834 | |
| 835 | { |
| 836 | float* ptr = bottom_top_blob.channel(q).row(i); |
| 837 | |
| 838 | int j = 0; |
| 839 | #if __ARM_NEON |
| 840 | float32x4_t _reciprocal_sum = vrecpeq_f32(_sum); |
| 841 | _reciprocal_sum = vmulq_f32(vrecpsq_f32(_sum, _reciprocal_sum), _reciprocal_sum); |
| 842 | for (; j + 3 < size; j += 4) |
| 843 | { |
| 844 | float32x4_t _p = vld1q_f32(ptr); |
| 845 | _p = vmulq_f32(_p, _reciprocal_sum); |
| 846 | vst1q_f32(ptr, _p); |
| 847 | ptr += 4; |
| 848 | } |
| 849 | #endif // __ARM_NEON |
| 850 | for (; j < size; j++) |
| 851 | { |
| 852 | *ptr /= sum; |
| 853 | ptr++; |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | #if NCNN_BF16 |
| 864 | int Softmax_arm::forward_inplace_bf16s(Mat& bottom_top_blob, const Option& opt) const |
| 865 | { |
| 866 | const int dims = bottom_top_blob.dims; |
| 867 | const int elempack = bottom_top_blob.elempack; |
| 868 | const int positive_axis = axis < 0 ? dims + axis : axis; |
| 869 | |
| 870 | if (dims == 1) // positive_axis == 0 |
| 871 | { |
| 872 | const int w = bottom_top_blob.w; |
| 873 | const int size = w * elempack; |
| 874 | |
| 875 | float max = -FLT_MAX; |
| 876 | #if __ARM_NEON |
| 877 | float32x4_t _max = vdupq_n_f32(-FLT_MAX); |
| 878 | #endif // __ARM_NEON |
| 879 | { |
| 880 | const unsigned short* ptr = bottom_top_blob; |
| 881 | |
| 882 | int i = 0; |
| 883 | #if __ARM_NEON |
| 884 | for (; i + 3 < size; i += 4) |
| 885 | { |
| 886 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 887 | _max = vmaxq_f32(_max, _p); |
| 888 | ptr += 4; |
| 889 | } |
| 890 | #endif // __ARM_NEON |
| 891 | for (; i < size; i++) |
| 892 | { |
| 893 | max = std::max(max, bfloat16_to_float32(*ptr)); |
| 894 | ptr++; |
| 895 | } |
| 896 | |
| 897 | #if __ARM_NEON |
| 898 | #if __aarch64__ |
| 899 | max = std::max(max, vmaxvq_f32(_max)); |
| 900 | #else |
| 901 | float32x2_t _max2 = vmax_f32(vget_low_f32(_max), vget_high_f32(_max)); |
| 902 | float32x2_t _mm2 = vpmax_f32(_max2, _max2); |
| 903 | max = std::max(max, vget_lane_f32(_mm2, 0)); |
| 904 | #endif |
| 905 | _max = vdupq_n_f32(max); |
| 906 | #endif // __ARM_NEON |
| 907 | } |
| 908 | |
| 909 | float sum = 0.f; |
| 910 | #if __ARM_NEON |
| 911 | float32x4_t _sum = vdupq_n_f32(0.f); |
| 912 | #endif // __ARM_NEON |
| 913 | { |
| 914 | unsigned short* ptr = bottom_top_blob; |
| 915 | |
| 916 | int i = 0; |
| 917 | #if __ARM_NEON |
| 918 | for (; i + 3 < size; i += 4) |
| 919 | { |
| 920 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 921 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 922 | vst1_u16(ptr, float2bfloat(_p)); |
| 923 | _sum = vaddq_f32(_sum, _p); |
| 924 | ptr += 4; |
| 925 | } |
| 926 | #endif // __ARM_NEON |
| 927 | for (; i < size; i++) |
| 928 | { |
| 929 | float v = (float)expf(bfloat16_to_float32(*ptr) - max); |
| 930 | *ptr = float32_to_bfloat16(v); |
| 931 | sum += v; |
| 932 | ptr++; |
| 933 | } |
| 934 | |
| 935 | #if __ARM_NEON |
| 936 | #if __aarch64__ |
| 937 | sum += vaddvq_f32(_sum); |
| 938 | #else |
| 939 | float32x2_t _sum2 = vadd_f32(vget_low_f32(_sum), vget_high_f32(_sum)); |
| 940 | float32x2_t _ss2 = vpadd_f32(_sum2, _sum2); |
| 941 | sum += vget_lane_f32(_ss2, 0); |
| 942 | #endif |
| 943 | _sum = vdupq_n_f32(sum); |
| 944 | #endif // __ARM_NEON |
| 945 | } |
| 946 | |
| 947 | { |
| 948 | unsigned short* ptr = bottom_top_blob; |
| 949 | |
| 950 | int i = 0; |
| 951 | #if __ARM_NEON |
| 952 | float32x4_t _reciprocal_sum = vrecpeq_f32(_sum); |
| 953 | _reciprocal_sum = vmulq_f32(vrecpsq_f32(_sum, _reciprocal_sum), _reciprocal_sum); |
| 954 | for (; i + 3 < size; i += 4) |
| 955 | { |
| 956 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 957 | _p = vmulq_f32(_p, _reciprocal_sum); |
| 958 | vst1_u16(ptr, float2bfloat(_p)); |
| 959 | ptr += 4; |
| 960 | } |
| 961 | #endif // __ARM_NEON |
| 962 | for (; i < size; i++) |
| 963 | { |
| 964 | *ptr = float32_to_bfloat16(bfloat16_to_float32(*ptr) / sum); |
| 965 | ptr++; |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | if (dims == 2 && positive_axis == 0) |
| 971 | { |
| 972 | const int w = bottom_top_blob.w; |
| 973 | const int h = bottom_top_blob.h; |
| 974 | |
| 975 | Mat maxsum(w, 1, 2, 4u, opt.workspace_allocator); |
| 976 | if (maxsum.empty()) |
| 977 | return -100; |
| 978 | |
| 979 | Mat max = maxsum.channel(0); |
| 980 | max.fill(-FLT_MAX); |
| 981 | |
| 982 | for (int i = 0; i < h; i++) |
| 983 | { |
| 984 | const unsigned short* ptr = bottom_top_blob.row<const unsigned short>(i); |
| 985 | float* maxptr = max; |
| 986 | |
| 987 | #if __ARM_NEON |
| 988 | if (elempack == 4) |
| 989 | { |
| 990 | for (int j = 0; j < w; j++) |
| 991 | { |
| 992 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 993 | #if __aarch64__ |
| 994 | float max0 = vmaxvq_f32(_p); |
| 995 | #else |
| 996 | float32x2_t _max2 = vmax_f32(vget_low_f32(_p), vget_high_f32(_p)); |
| 997 | float32x2_t _mm2 = vpmax_f32(_max2, _max2); |
| 998 | float max0 = vget_lane_f32(_mm2, 0); |
| 999 | #endif |
| 1000 | *maxptr = std::max(*maxptr, max0); |
| 1001 | ptr += 4; |
| 1002 | maxptr++; |
| 1003 | } |
| 1004 | } |
| 1005 | #endif // __ARM_NEON |
| 1006 | if (elempack == 1) |
| 1007 | { |
| 1008 | int j = 0; |
| 1009 | #if __ARM_NEON |
| 1010 | for (; j + 3 < w; j += 4) |
| 1011 | { |
| 1012 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1013 | float32x4_t _max = vld1q_f32(maxptr); |
| 1014 | _max = vmaxq_f32(_max, _p); |
| 1015 | vst1q_f32(maxptr, _max); |
| 1016 | ptr += 4; |
| 1017 | maxptr += 4; |
| 1018 | } |
| 1019 | #endif // __ARM_NEON |
| 1020 | for (; j < w; j++) |
| 1021 | { |
| 1022 | *maxptr = std::max(*maxptr, bfloat16_to_float32(*ptr)); |
| 1023 | ptr++; |
| 1024 | maxptr++; |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | Mat sum = maxsum.channel(1); |
| 1030 | sum.fill(0.f); |
| 1031 | |
| 1032 | for (int i = 0; i < h; i++) |
| 1033 | { |
| 1034 | unsigned short* ptr = bottom_top_blob.row<unsigned short>(i); |
| 1035 | const float* maxptr = max; |
| 1036 | float* sumptr = sum; |
| 1037 | |
| 1038 | #if __ARM_NEON |
| 1039 | if (elempack == 4) |
| 1040 | { |
| 1041 | for (int j = 0; j < w; j++) |
| 1042 | { |
| 1043 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1044 | float32x4_t _max = vdupq_n_f32(*maxptr); |
| 1045 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 1046 | vst1_u16(ptr, float2bfloat(_p)); |
| 1047 | #if __aarch64__ |
| 1048 | float sum0 = vaddvq_f32(_p); |
| 1049 | #else |
| 1050 | float32x2_t _sum2 = vadd_f32(vget_low_f32(_p), vget_high_f32(_p)); |
| 1051 | float32x2_t _ss2 = vpadd_f32(_sum2, _sum2); |
| 1052 | float sum0 = vget_lane_f32(_ss2, 0); |
| 1053 | #endif |
| 1054 | *sumptr += sum0; |
| 1055 | ptr += 4; |
| 1056 | maxptr++; |
| 1057 | sumptr++; |
| 1058 | } |
| 1059 | } |
| 1060 | #endif // __ARM_NEON |
| 1061 | if (elempack == 1) |
| 1062 | { |
| 1063 | int j = 0; |
| 1064 | #if __ARM_NEON |
| 1065 | for (; j + 3 < w; j += 4) |
| 1066 | { |
| 1067 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1068 | float32x4_t _max = vld1q_f32(maxptr); |
| 1069 | float32x4_t _sum = vld1q_f32(sumptr); |
| 1070 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 1071 | vst1_u16(ptr, float2bfloat(_p)); |
| 1072 | _sum = vaddq_f32(_sum, _p); |
| 1073 | vst1q_f32(sumptr, _sum); |
| 1074 | ptr += 4; |
| 1075 | maxptr += 4; |
| 1076 | sumptr += 4; |
| 1077 | } |
| 1078 | #endif // __ARM_NEON |
| 1079 | for (; j < w; j++) |
| 1080 | { |
| 1081 | float v = (float)expf(bfloat16_to_float32(*ptr) - *maxptr); |
| 1082 | *ptr = float32_to_bfloat16(v); |
| 1083 | *sumptr += v; |
| 1084 | ptr++; |
| 1085 | maxptr++; |
| 1086 | sumptr++; |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | for (int i = 0; i < h; i++) |
| 1092 | { |
| 1093 | unsigned short* ptr = bottom_top_blob.row<unsigned short>(i); |
| 1094 | const float* sumptr = sum; |
| 1095 | |
| 1096 | #if __ARM_NEON |
| 1097 | if (elempack == 4) |
| 1098 | { |
| 1099 | for (int j = 0; j < w; j++) |
| 1100 | { |
| 1101 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1102 | float32x4_t _sum = vld1q_dup_f32(sumptr); |
| 1103 | _p = div_ps(_p, _sum); |
| 1104 | vst1_u16(ptr, float2bfloat(_p)); |
| 1105 | ptr += 4; |
| 1106 | sumptr++; |
| 1107 | } |
| 1108 | } |
| 1109 | #endif // __ARM_NEON |
| 1110 | if (elempack == 1) |
| 1111 | { |
| 1112 | int j = 0; |
| 1113 | #if __ARM_NEON |
| 1114 | for (; j + 3 < w; j += 4) |
| 1115 | { |
| 1116 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1117 | float32x4_t _sum = vld1q_f32(sumptr); |
| 1118 | _p = div_ps(_p, _sum); |
| 1119 | vst1_u16(ptr, float2bfloat(_p)); |
| 1120 | ptr += 4; |
| 1121 | sumptr += 4; |
| 1122 | } |
| 1123 | #endif // __ARM_NEON |
| 1124 | for (; j < w; j++) |
| 1125 | { |
| 1126 | *ptr = float32_to_bfloat16(bfloat16_to_float32(*ptr) / *sumptr); |
| 1127 | ptr++; |
| 1128 | sumptr++; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | if (dims == 2 && positive_axis == 1) |
| 1135 | { |
| 1136 | const int w = bottom_top_blob.w; |
| 1137 | const int h = bottom_top_blob.h; |
| 1138 | const int size = w * elempack; |
| 1139 | |
| 1140 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1141 | for (int i = 0; i < h; i++) |
| 1142 | { |
| 1143 | float max = -FLT_MAX; |
| 1144 | #if __ARM_NEON |
| 1145 | float32x4_t _max = vdupq_n_f32(-FLT_MAX); |
| 1146 | #endif // __ARM_NEON |
| 1147 | { |
| 1148 | const unsigned short* ptr = bottom_top_blob.row<const unsigned short>(i); |
| 1149 | |
| 1150 | int j = 0; |
| 1151 | #if __ARM_NEON |
| 1152 | for (; j + 3 < size; j += 4) |
| 1153 | { |
| 1154 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1155 | _max = vmaxq_f32(_max, _p); |
| 1156 | ptr += 4; |
| 1157 | } |
| 1158 | #endif // __ARM_NEON |
| 1159 | for (; j < size; j++) |
| 1160 | { |
| 1161 | max = std::max(max, bfloat16_to_float32(*ptr)); |
| 1162 | ptr++; |
| 1163 | } |
| 1164 | |
| 1165 | #if __ARM_NEON |
| 1166 | if (elempack == 1) |
| 1167 | { |
| 1168 | #if __aarch64__ |
| 1169 | max = std::max(max, vmaxvq_f32(_max)); |
| 1170 | #else |
| 1171 | float32x2_t _max2 = vmax_f32(vget_low_f32(_max), vget_high_f32(_max)); |
| 1172 | float32x2_t _mm2 = vpmax_f32(_max2, _max2); |
| 1173 | max = std::max(max, vget_lane_f32(_mm2, 0)); |
| 1174 | #endif |
| 1175 | _max = vdupq_n_f32(max); |
| 1176 | } |
| 1177 | #endif // __ARM_NEON |
| 1178 | } |
| 1179 | |
| 1180 | float sum = 0.f; |
| 1181 | #if __ARM_NEON |
| 1182 | float32x4_t _sum = vdupq_n_f32(0.f); |
| 1183 | #endif // __ARM_NEON |
| 1184 | { |
| 1185 | unsigned short* ptr = bottom_top_blob.row<unsigned short>(i); |
| 1186 | |
| 1187 | int j = 0; |
| 1188 | #if __ARM_NEON |
| 1189 | for (; j + 3 < size; j += 4) |
| 1190 | { |
| 1191 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1192 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 1193 | vst1_u16(ptr, float2bfloat(_p)); |
| 1194 | _sum = vaddq_f32(_sum, _p); |
| 1195 | ptr += 4; |
| 1196 | } |
| 1197 | #endif // __ARM_NEON |
| 1198 | for (; j < size; j++) |
| 1199 | { |
| 1200 | float v = (float)expf(bfloat16_to_float32(*ptr) - max); |
| 1201 | *ptr = float32_to_bfloat16(v); |
| 1202 | sum += v; |
| 1203 | ptr++; |
| 1204 | } |
| 1205 | |
| 1206 | #if __ARM_NEON |
| 1207 | if (elempack == 1) |
| 1208 | { |
| 1209 | #if __aarch64__ |
| 1210 | sum += vaddvq_f32(_sum); |
| 1211 | #else |
| 1212 | float32x2_t _sum2 = vadd_f32(vget_low_f32(_sum), vget_high_f32(_sum)); |
| 1213 | float32x2_t _ss2 = vpadd_f32(_sum2, _sum2); |
| 1214 | sum += vget_lane_f32(_ss2, 0); |
| 1215 | #endif |
| 1216 | _sum = vdupq_n_f32(sum); |
| 1217 | } |
| 1218 | #endif // __ARM_NEON |
| 1219 | } |
| 1220 | |
| 1221 | { |
| 1222 | unsigned short* ptr = bottom_top_blob.row<unsigned short>(i); |
| 1223 | |
| 1224 | int j = 0; |
| 1225 | #if __ARM_NEON |
| 1226 | float32x4_t _reciprocal_sum = vrecpeq_f32(_sum); |
| 1227 | _reciprocal_sum = vmulq_f32(vrecpsq_f32(_sum, _reciprocal_sum), _reciprocal_sum); |
| 1228 | for (; j + 3 < size; j += 4) |
| 1229 | { |
| 1230 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1231 | _p = vmulq_f32(_p, _reciprocal_sum); |
| 1232 | vst1_u16(ptr, float2bfloat(_p)); |
| 1233 | ptr += 4; |
| 1234 | } |
| 1235 | #endif // __ARM_NEON |
| 1236 | for (; j < size; j++) |
| 1237 | { |
| 1238 | *ptr = float32_to_bfloat16(bfloat16_to_float32(*ptr) / sum); |
| 1239 | ptr++; |
| 1240 | } |
| 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | if (dims == 3 && positive_axis == 0) |
| 1246 | { |
| 1247 | const int w = bottom_top_blob.w; |
| 1248 | const int h = bottom_top_blob.h; |
| 1249 | const int channels = bottom_top_blob.c; |
| 1250 | const int size = w * h; |
| 1251 | |
| 1252 | Mat maxsum(w, h, 2, 4u, opt.workspace_allocator); |
| 1253 | if (maxsum.empty()) |
| 1254 | return -100; |
| 1255 | |
| 1256 | Mat max = maxsum.channel(0); |
| 1257 | max.fill(-FLT_MAX); |
| 1258 | |
| 1259 | for (int q = 0; q < channels; q++) |
| 1260 | { |
| 1261 | const unsigned short* ptr = bottom_top_blob.channel(q); |
| 1262 | float* maxptr = max; |
| 1263 | |
| 1264 | #if __ARM_NEON |
| 1265 | if (elempack == 4) |
| 1266 | { |
| 1267 | for (int i = 0; i < size; i++) |
| 1268 | { |
| 1269 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1270 | #if __aarch64__ |
| 1271 | float max0 = vmaxvq_f32(_p); |
| 1272 | #else |
| 1273 | float32x2_t _max2 = vmax_f32(vget_low_f32(_p), vget_high_f32(_p)); |
| 1274 | float32x2_t _mm2 = vpmax_f32(_max2, _max2); |
| 1275 | float max0 = vget_lane_f32(_mm2, 0); |
| 1276 | #endif |
| 1277 | *maxptr = std::max(*maxptr, max0); |
| 1278 | ptr += 4; |
| 1279 | maxptr++; |
| 1280 | } |
| 1281 | } |
| 1282 | #endif // __ARM_NEON |
| 1283 | if (elempack == 1) |
| 1284 | { |
| 1285 | int i = 0; |
| 1286 | #if __ARM_NEON |
| 1287 | for (; i + 3 < size; i += 4) |
| 1288 | { |
| 1289 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1290 | float32x4_t _max = vld1q_f32(maxptr); |
| 1291 | _max = vmaxq_f32(_max, _p); |
| 1292 | vst1q_f32(maxptr, _max); |
| 1293 | ptr += 4; |
| 1294 | maxptr += 4; |
| 1295 | } |
| 1296 | #endif // __ARM_NEON |
| 1297 | for (; i < size; i++) |
| 1298 | { |
| 1299 | *maxptr = std::max(*maxptr, bfloat16_to_float32(*ptr)); |
| 1300 | ptr++; |
| 1301 | maxptr++; |
| 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1307 | for (int q = 0; q < channels; q++) |
| 1308 | { |
| 1309 | unsigned short* ptr = bottom_top_blob.channel(q); |
| 1310 | const float* maxptr = max; |
| 1311 | |
| 1312 | #if __ARM_NEON |
| 1313 | if (elempack == 4) |
| 1314 | { |
| 1315 | for (int i = 0; i < size; i++) |
| 1316 | { |
| 1317 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1318 | float32x4_t _max = vdupq_n_f32(*maxptr); |
| 1319 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 1320 | vst1_u16(ptr, float2bfloat(_p)); |
| 1321 | ptr += 4; |
| 1322 | maxptr++; |
| 1323 | } |
| 1324 | } |
| 1325 | #endif // __ARM_NEON |
| 1326 | if (elempack == 1) |
| 1327 | { |
| 1328 | int i = 0; |
| 1329 | #if __ARM_NEON |
| 1330 | for (; i + 3 < size; i += 4) |
| 1331 | { |
| 1332 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1333 | float32x4_t _max = vld1q_f32(maxptr); |
| 1334 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 1335 | vst1_u16(ptr, float2bfloat(_p)); |
| 1336 | ptr += 4; |
| 1337 | maxptr += 4; |
| 1338 | } |
| 1339 | #endif // __ARM_NEON |
| 1340 | for (; i < size; i++) |
| 1341 | { |
| 1342 | *ptr = float32_to_bfloat16(expf(bfloat16_to_float32(*ptr) - *maxptr)); |
| 1343 | ptr++; |
| 1344 | maxptr++; |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | Mat sum = maxsum.channel(1); |
| 1350 | sum.fill(0.f); |
| 1351 | |
| 1352 | for (int q = 0; q < channels; q++) |
| 1353 | { |
| 1354 | unsigned short* ptr = bottom_top_blob.channel(q); |
| 1355 | float* sumptr = sum; |
| 1356 | |
| 1357 | #if __ARM_NEON |
| 1358 | if (elempack == 4) |
| 1359 | { |
| 1360 | for (int i = 0; i < size; i++) |
| 1361 | { |
| 1362 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1363 | #if __aarch64__ |
| 1364 | float sum0 = vaddvq_f32(_p); |
| 1365 | #else |
| 1366 | float32x2_t _sum2 = vadd_f32(vget_low_f32(_p), vget_high_f32(_p)); |
| 1367 | float32x2_t _ss2 = vpadd_f32(_sum2, _sum2); |
| 1368 | float sum0 = vget_lane_f32(_ss2, 0); |
| 1369 | #endif |
| 1370 | *sumptr += sum0; |
| 1371 | ptr += 4; |
| 1372 | sumptr++; |
| 1373 | } |
| 1374 | } |
| 1375 | #endif // __ARM_NEON |
| 1376 | if (elempack == 1) |
| 1377 | { |
| 1378 | int i = 0; |
| 1379 | #if __ARM_NEON |
| 1380 | for (; i + 3 < size; i += 4) |
| 1381 | { |
| 1382 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1383 | float32x4_t _sum = vld1q_f32(sumptr); |
| 1384 | _sum = vaddq_f32(_sum, _p); |
| 1385 | vst1q_f32(sumptr, _sum); |
| 1386 | ptr += 4; |
| 1387 | sumptr += 4; |
| 1388 | } |
| 1389 | #endif // __ARM_NEON |
| 1390 | for (; i < size; i++) |
| 1391 | { |
| 1392 | *sumptr += bfloat16_to_float32(*ptr); |
| 1393 | ptr++; |
| 1394 | sumptr++; |
| 1395 | } |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1400 | for (int q = 0; q < channels; q++) |
| 1401 | { |
| 1402 | unsigned short* ptr = bottom_top_blob.channel(q); |
| 1403 | const float* sumptr = sum; |
| 1404 | |
| 1405 | #if __ARM_NEON |
| 1406 | if (elempack == 4) |
| 1407 | { |
| 1408 | for (int i = 0; i < size; i++) |
| 1409 | { |
| 1410 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1411 | float32x4_t _sum = vdupq_n_f32(*sumptr); |
| 1412 | _p = div_ps(_p, _sum); |
| 1413 | vst1_u16(ptr, float2bfloat(_p)); |
| 1414 | ptr += 4; |
| 1415 | sumptr++; |
| 1416 | } |
| 1417 | } |
| 1418 | #endif // __ARM_NEON |
| 1419 | if (elempack == 1) |
| 1420 | { |
| 1421 | int i = 0; |
| 1422 | #if __ARM_NEON |
| 1423 | for (; i + 3 < size; i += 4) |
| 1424 | { |
| 1425 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1426 | float32x4_t _sum = vld1q_f32(sumptr); |
| 1427 | _p = div_ps(_p, _sum); |
| 1428 | vst1_u16(ptr, float2bfloat(_p)); |
| 1429 | ptr += 4; |
| 1430 | sumptr += 4; |
| 1431 | } |
| 1432 | #endif // __ARM_NEON |
| 1433 | for (; i < size; i++) |
| 1434 | { |
| 1435 | *ptr = float32_to_bfloat16(bfloat16_to_float32(*ptr) / *sumptr); |
| 1436 | ptr++; |
| 1437 | sumptr++; |
| 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | if (dims == 3 && positive_axis == 1) |
| 1444 | { |
| 1445 | const int w = bottom_top_blob.w; |
| 1446 | const int h = bottom_top_blob.h; |
| 1447 | const int channels = bottom_top_blob.c; |
| 1448 | const int size = w * elempack; |
| 1449 | |
| 1450 | Mat maxsum(w * elempack, channels, 2, 4u, opt.workspace_allocator); |
| 1451 | if (maxsum.empty()) |
| 1452 | return -100; |
| 1453 | |
| 1454 | Mat max = maxsum.channel(0); |
| 1455 | max.fill(-FLT_MAX); |
| 1456 | |
| 1457 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1458 | for (int q = 0; q < channels; q++) |
| 1459 | { |
| 1460 | const unsigned short* ptr = bottom_top_blob.channel(q); |
| 1461 | |
| 1462 | for (int i = 0; i < h; i++) |
| 1463 | { |
| 1464 | float* maxptr = max.row(q); |
| 1465 | |
| 1466 | int j = 0; |
| 1467 | #if __ARM_NEON |
| 1468 | for (; j + 3 < size; j += 4) |
| 1469 | { |
| 1470 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1471 | float32x4_t _max = vld1q_f32(maxptr); |
| 1472 | _max = vmaxq_f32(_max, _p); |
| 1473 | vst1q_f32(maxptr, _max); |
| 1474 | ptr += 4; |
| 1475 | maxptr += 4; |
| 1476 | } |
| 1477 | #endif // __ARM_NEON |
| 1478 | for (; j < size; j++) |
| 1479 | { |
| 1480 | *maxptr = std::max(*maxptr, bfloat16_to_float32(*ptr)); |
| 1481 | ptr++; |
| 1482 | maxptr++; |
| 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | Mat sum = maxsum.channel(1); |
| 1488 | sum.fill(0.f); |
| 1489 | |
| 1490 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1491 | for (int q = 0; q < channels; q++) |
| 1492 | { |
| 1493 | unsigned short* ptr = bottom_top_blob.channel(q); |
| 1494 | |
| 1495 | for (int i = 0; i < h; i++) |
| 1496 | { |
| 1497 | const float* maxptr = max.row(q); |
| 1498 | float* sumptr = sum.row(q); |
| 1499 | |
| 1500 | int j = 0; |
| 1501 | #if __ARM_NEON |
| 1502 | for (; j + 3 < size; j += 4) |
| 1503 | { |
| 1504 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1505 | float32x4_t _max = vld1q_f32(maxptr); |
| 1506 | float32x4_t _sum = vld1q_f32(sumptr); |
| 1507 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 1508 | vst1_u16(ptr, float2bfloat(_p)); |
| 1509 | _sum = vaddq_f32(_sum, _p); |
| 1510 | vst1q_f32(sumptr, _sum); |
| 1511 | ptr += 4; |
| 1512 | maxptr += 4; |
| 1513 | sumptr += 4; |
| 1514 | } |
| 1515 | #endif // __ARM_NEON |
| 1516 | for (; j < size; j++) |
| 1517 | { |
| 1518 | float v = (float)expf(bfloat16_to_float32(*ptr) - *maxptr); |
| 1519 | *ptr = float32_to_bfloat16(v); |
| 1520 | *sumptr += v; |
| 1521 | ptr++; |
| 1522 | maxptr++; |
| 1523 | sumptr++; |
| 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1529 | for (int q = 0; q < channels; q++) |
| 1530 | { |
| 1531 | unsigned short* ptr = bottom_top_blob.channel(q); |
| 1532 | |
| 1533 | for (int i = 0; i < h; i++) |
| 1534 | { |
| 1535 | const float* sumptr = sum.row(q); |
| 1536 | |
| 1537 | int j = 0; |
| 1538 | #if __ARM_NEON |
| 1539 | for (; j + 3 < size; j += 4) |
| 1540 | { |
| 1541 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1542 | float32x4_t _sum = vld1q_f32(sumptr); |
| 1543 | _p = div_ps(_p, _sum); |
| 1544 | vst1_u16(ptr, float2bfloat(_p)); |
| 1545 | ptr += 4; |
| 1546 | sumptr += 4; |
| 1547 | } |
| 1548 | #endif // __ARM_NEON |
| 1549 | for (; j < size; j++) |
| 1550 | { |
| 1551 | *ptr = float32_to_bfloat16(bfloat16_to_float32(*ptr) / *sumptr); |
| 1552 | ptr++; |
| 1553 | sumptr++; |
| 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | if (dims == 3 && positive_axis == 2) |
| 1560 | { |
| 1561 | const int w = bottom_top_blob.w; |
| 1562 | const int h = bottom_top_blob.h; |
| 1563 | const int channels = bottom_top_blob.c; |
| 1564 | const int size = w * elempack; |
| 1565 | |
| 1566 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1567 | for (int q = 0; q < channels; q++) |
| 1568 | { |
| 1569 | for (int i = 0; i < h; i++) |
| 1570 | { |
| 1571 | float max = -FLT_MAX; |
| 1572 | #if __ARM_NEON |
| 1573 | float32x4_t _max = vdupq_n_f32(-FLT_MAX); |
| 1574 | #endif // __ARM_NEON |
| 1575 | { |
| 1576 | const unsigned short* ptr = bottom_top_blob.channel(q).row<const unsigned short>(i); |
| 1577 | |
| 1578 | int j = 0; |
| 1579 | #if __ARM_NEON |
| 1580 | for (; j + 3 < size; j += 4) |
| 1581 | { |
| 1582 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1583 | _max = vmaxq_f32(_max, _p); |
| 1584 | ptr += 4; |
| 1585 | } |
| 1586 | #endif // __ARM_NEON |
| 1587 | for (; j < size; j++) |
| 1588 | { |
| 1589 | max = std::max(max, bfloat16_to_float32(*ptr)); |
| 1590 | ptr++; |
| 1591 | } |
| 1592 | |
| 1593 | #if __ARM_NEON |
| 1594 | if (elempack == 1) |
| 1595 | { |
| 1596 | #if __aarch64__ |
| 1597 | max = std::max(max, vmaxvq_f32(_max)); |
| 1598 | #else |
| 1599 | float32x2_t _max2 = vmax_f32(vget_low_f32(_max), vget_high_f32(_max)); |
| 1600 | float32x2_t _mm2 = vpmax_f32(_max2, _max2); |
| 1601 | max = std::max(max, vget_lane_f32(_mm2, 0)); |
| 1602 | #endif |
| 1603 | _max = vdupq_n_f32(max); |
| 1604 | } |
| 1605 | #endif // __ARM_NEON |
| 1606 | } |
| 1607 | |
| 1608 | float sum = 0.f; |
| 1609 | #if __ARM_NEON |
| 1610 | float32x4_t _sum = vdupq_n_f32(0.f); |
| 1611 | #endif // __ARM_NEON |
| 1612 | { |
| 1613 | unsigned short* ptr = bottom_top_blob.channel(q).row<unsigned short>(i); |
| 1614 | |
| 1615 | int j = 0; |
| 1616 | #if __ARM_NEON |
| 1617 | for (; j + 3 < size; j += 4) |
| 1618 | { |
| 1619 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1620 | _p = exp_ps(vsubq_f32(_p, _max)); |
| 1621 | vst1_u16(ptr, float2bfloat(_p)); |
| 1622 | _sum = vaddq_f32(_sum, _p); |
| 1623 | ptr += 4; |
| 1624 | } |
| 1625 | #endif // __ARM_NEON |
| 1626 | for (; j < size; j++) |
| 1627 | { |
| 1628 | float v = (float)expf(bfloat16_to_float32(*ptr) - max); |
| 1629 | *ptr = float32_to_bfloat16(v); |
| 1630 | sum += v; |
| 1631 | ptr++; |
| 1632 | } |
| 1633 | |
| 1634 | #if __ARM_NEON |
| 1635 | if (elempack == 1) |
| 1636 | { |
| 1637 | #if __aarch64__ |
| 1638 | sum += vaddvq_f32(_sum); |
| 1639 | #else |
| 1640 | float32x2_t _sum2 = vadd_f32(vget_low_f32(_sum), vget_high_f32(_sum)); |
| 1641 | float32x2_t _ss2 = vpadd_f32(_sum2, _sum2); |
| 1642 | sum += vget_lane_f32(_ss2, 0); |
| 1643 | #endif |
| 1644 | _sum = vdupq_n_f32(sum); |
| 1645 | } |
| 1646 | #endif // __ARM_NEON |
| 1647 | } |
| 1648 | |
| 1649 | { |
| 1650 | unsigned short* ptr = bottom_top_blob.channel(q).row<unsigned short>(i); |
| 1651 | |
| 1652 | int j = 0; |
| 1653 | #if __ARM_NEON |
| 1654 | float32x4_t _reciprocal_sum = vrecpeq_f32(_sum); |
| 1655 | _reciprocal_sum = vmulq_f32(vrecpsq_f32(_sum, _reciprocal_sum), _reciprocal_sum); |
| 1656 | for (; j + 3 < size; j += 4) |
| 1657 | { |
| 1658 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 1659 | _p = vmulq_f32(_p, _reciprocal_sum); |
| 1660 | vst1_u16(ptr, float2bfloat(_p)); |
| 1661 | ptr += 4; |
| 1662 | } |
| 1663 | #endif // __ARM_NEON |
| 1664 | for (; j < size; j++) |
| 1665 | { |
| 1666 | *ptr = float32_to_bfloat16(bfloat16_to_float32(*ptr) / sum); |
| 1667 | ptr++; |
| 1668 | } |
| 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | return 0; |
| 1675 | } |
| 1676 | #endif // NCNN_BF16 |
| 1677 | |
| 1678 | } // namespace ncnn |