| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2023 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 "arm_usability.h" |
| 22 | #include "neon_mathfun_fp16s.h" |
| 23 | #endif // __ARM_NEON |
| 24 | |
| 25 | namespace ncnn { |
| 26 | |
| 27 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 28 | int Softmax_arm::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const |
| 29 | { |
| 30 | const int dims = bottom_top_blob.dims; |
| 31 | const int elempack = bottom_top_blob.elempack; |
| 32 | const int positive_axis = axis < 0 ? dims + axis : axis; |
| 33 | |
| 34 | if (dims == 1) // positive_axis == 0 |
| 35 | { |
| 36 | const int w = bottom_top_blob.w; |
| 37 | const int size = w * elempack; |
| 38 | |
| 39 | __fp16 max = -65504.f; |
| 40 | float16x8_t _max = vdupq_n_f16(-65504.f); |
| 41 | { |
| 42 | const __fp16* ptr = bottom_top_blob; |
| 43 | |
| 44 | int i = 0; |
| 45 | for (; i + 7 < size; i += 8) |
| 46 | { |
| 47 | float16x8_t _p = vld1q_f16(ptr); |
| 48 | _max = vmaxq_f16(_max, _p); |
| 49 | ptr += 8; |
| 50 | } |
| 51 | for (; i + 3 < size; i += 4) |
| 52 | { |
| 53 | float16x4_t _p = vld1_f16(ptr); |
| 54 | _max = vcombine_f16(vmax_f16(vget_low_f16(_max), _p), vget_high_f16(_max)); |
| 55 | ptr += 4; |
| 56 | } |
| 57 | for (; i < size; i++) |
| 58 | { |
| 59 | max = std::max(max, *ptr); |
| 60 | ptr++; |
| 61 | } |
| 62 | |
| 63 | max = std::max(max, vmaxvq_f16(_max)); |
| 64 | _max = vdupq_n_f16(max); |
| 65 | } |
| 66 | |
| 67 | __fp16 sum = 0.f; |
| 68 | float16x8_t _sum = vdupq_n_f16(0.f); |
| 69 | { |
| 70 | __fp16* ptr = bottom_top_blob; |
| 71 | |
| 72 | int i = 0; |
| 73 | for (; i + 7 < size; i += 8) |
| 74 | { |
| 75 | float16x8_t _p = vld1q_f16(ptr); |
| 76 | _p = exp_ps_f16(vsubq_f16(_p, _max)); |
| 77 | vst1q_f16(ptr, _p); |
| 78 | _sum = vaddq_f16(_sum, _p); |
| 79 | ptr += 8; |
| 80 | } |
| 81 | for (; i + 3 < size; i += 4) |
| 82 | { |
| 83 | float16x4_t _p = vld1_f16(ptr); |
| 84 | _p = exp_ps_f16(vsub_f16(_p, vget_low_f16(_max))); |
| 85 | vst1_f16(ptr, _p); |
| 86 | _sum = vcombine_f16(vadd_f16(vget_low_f16(_sum), _p), vget_high_f16(_sum)); |
| 87 | ptr += 4; |
| 88 | } |
| 89 | for (; i < size; i++) |
| 90 | { |
| 91 | *ptr = (__fp16)expf(*ptr - max); |
| 92 | sum += *ptr; |
| 93 | ptr++; |
| 94 | } |
| 95 | |
| 96 | float16x4_t _sum2 = vadd_f16(vget_low_f16(_sum), vget_high_f16(_sum)); |
| 97 | float16x4_t _ss2 = vpadd_f16(_sum2, _sum2); |
| 98 | sum += vget_lane_f16(_ss2, 0) + vget_lane_f16(_ss2, 1); |
| 99 | _sum = vdupq_n_f16(sum); |
| 100 | } |
| 101 | |
| 102 | { |
| 103 | __fp16* ptr = bottom_top_blob; |
| 104 | |
| 105 | int i = 0; |
| 106 | float16x8_t _reciprocal_sum = vrecpeq_f16(_sum); |
| 107 | _reciprocal_sum = vmulq_f16(vrecpsq_f16(_sum, _reciprocal_sum), _reciprocal_sum); |
| 108 | for (; i + 7 < size; i += 8) |
| 109 | { |
| 110 | float16x8_t _p = vld1q_f16(ptr); |
| 111 | _p = vmulq_f16(_p, _reciprocal_sum); |
| 112 | vst1q_f16(ptr, _p); |
| 113 | ptr += 8; |
| 114 | } |
| 115 | for (; i + 3 < size; i += 4) |
| 116 | { |
| 117 | float16x4_t _p = vld1_f16(ptr); |
| 118 | _p = vmul_f16(_p, vget_low_f16(_reciprocal_sum)); |
| 119 | vst1_f16(ptr, _p); |
| 120 | ptr += 4; |
| 121 | } |
| 122 | for (; i < size; i++) |
| 123 | { |
| 124 | *ptr = *ptr / sum; |
| 125 | ptr++; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (dims == 2 && positive_axis == 0) |
| 131 | { |
| 132 | const int w = bottom_top_blob.w; |
| 133 | const int h = bottom_top_blob.h; |
| 134 | |
| 135 | Mat maxsum(w, 1, 2, 2u, opt.workspace_allocator); |
| 136 | if (maxsum.empty()) |
| 137 | return -100; |
| 138 | |
| 139 | Mat max = maxsum.channel(0); |
| 140 | max.fill<__fp16>((__fp16)-65504.f); |
| 141 | |
| 142 | for (int i = 0; i < h; i++) |
| 143 | { |
| 144 | const __fp16* ptr = bottom_top_blob.row<const __fp16>(i); |
| 145 | __fp16* maxptr = max; |
| 146 | |
| 147 | if (elempack == 8) |
| 148 | { |
| 149 | int j = 0; |
| 150 | for (; j + 3 < w; j += 4) |
| 151 | { |
| 152 | float16x8_t _p0 = vld1q_f16(ptr); |
| 153 | float16x8_t _p1 = vld1q_f16(ptr + 8); |
| 154 | float16x8_t _p2 = vld1q_f16(ptr + 16); |
| 155 | float16x8_t _p3 = vld1q_f16(ptr + 24); |
| 156 | float16x4_t _max = vld1_f16(maxptr); |
| 157 | float16x8_t _max01 = vpmaxq_f16(_p0, _p1); |
| 158 | float16x8_t _max23 = vpmaxq_f16(_p2, _p3); |
| 159 | float16x8_t _max2 = vpmaxq_f16(_max01, _max23); |
| 160 | _max = vmax_f16(_max, vpmax_f16(vget_low_f16(_max2), vget_high_f16(_max2))); |
| 161 | vst1_f16(maxptr, _max); |
| 162 | ptr += 32; |
| 163 | maxptr += 4; |
| 164 | } |
| 165 | for (; j < w; j++) |
| 166 | { |
| 167 | float16x8_t _p = vld1q_f16(ptr); |
| 168 | __fp16 max0 = vmaxvq_f16(_p); |
| 169 | *maxptr = std::max(*maxptr, max0); |
| 170 | ptr += 8; |
| 171 | maxptr++; |
| 172 | } |
| 173 | } |
| 174 | if (elempack == 4) |
| 175 | { |
| 176 | int j = 0; |
| 177 | for (; j + 3 < w; j += 4) |
| 178 | { |
| 179 | float16x8_t _p0 = vld1q_f16(ptr); |
| 180 | float16x8_t _p1 = vld1q_f16(ptr + 8); |
| 181 | float16x4_t _max = vld1_f16(maxptr); |
| 182 | float16x8_t _max2 = vpmaxq_f16(_p0, _p1); |
| 183 | _max = vmax_f16(_max, vpmax_f16(vget_low_f16(_max2), vget_high_f16(_max2))); |
| 184 | vst1_f16(maxptr, _max); |
| 185 | ptr += 16; |
| 186 | maxptr += 4; |
| 187 | } |
| 188 | for (; j < w; j++) |
| 189 | { |
| 190 | float16x4_t _p = vld1_f16(ptr); |
| 191 | __fp16 max0 = vmaxv_f16(_p); |
| 192 | *maxptr = std::max(*maxptr, max0); |
| 193 | ptr += 4; |
| 194 | maxptr++; |
| 195 | } |
| 196 | } |
| 197 | if (elempack == 1) |
| 198 | { |
| 199 | int j = 0; |
| 200 | for (; j + 7 < w; j += 8) |
| 201 | { |
| 202 | float16x8_t _p = vld1q_f16(ptr); |
| 203 | float16x8_t _max = vld1q_f16(maxptr); |
| 204 | _max = vmaxq_f16(_max, _p); |
| 205 | vst1q_f16(maxptr, _max); |
| 206 | ptr += 8; |
| 207 | maxptr += 8; |
| 208 | } |
| 209 | for (; j + 3 < w; j += 4) |
| 210 | { |
| 211 | float16x4_t _p = vld1_f16(ptr); |
| 212 | float16x4_t _max = vld1_f16(maxptr); |
| 213 | _max = vmax_f16(_max, _p); |
| 214 | vst1_f16(maxptr, _max); |
| 215 | ptr += 4; |
| 216 | maxptr += 4; |
| 217 | } |
| 218 | for (; j < w; j++) |
| 219 | { |
| 220 | *maxptr = std::max(*maxptr, *ptr); |
| 221 | ptr++; |
| 222 | maxptr++; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | Mat sum = maxsum.channel(1); |
| 228 | sum.fill<__fp16>((__fp16)0.f); |
| 229 | |
| 230 | for (int i = 0; i < h; i++) |
| 231 | { |
| 232 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 233 | const __fp16* maxptr = max; |
| 234 | __fp16* sumptr = sum; |
| 235 | |
| 236 | if (elempack == 8) |
| 237 | { |
| 238 | int j = 0; |
| 239 | for (; j + 3 < w; j += 4) |
| 240 | { |
| 241 | float16x8_t _p0 = vld1q_f16(ptr); |
| 242 | float16x8_t _p1 = vld1q_f16(ptr + 8); |
| 243 | float16x8_t _p2 = vld1q_f16(ptr + 16); |
| 244 | float16x8_t _p3 = vld1q_f16(ptr + 24); |
| 245 | float16x4_t _max = vld1_f16(maxptr); |
| 246 | float16x4_t _sum = vld1_f16(sumptr); |
| 247 | _p0 = exp_ps_f16(vsubq_f16(_p0, vdupq_lane_f16(_max, 0))); |
| 248 | _p1 = exp_ps_f16(vsubq_f16(_p1, vdupq_lane_f16(_max, 1))); |
| 249 | _p2 = exp_ps_f16(vsubq_f16(_p2, vdupq_lane_f16(_max, 2))); |
| 250 | _p3 = exp_ps_f16(vsubq_f16(_p3, vdupq_lane_f16(_max, 3))); |
| 251 | vst1q_f16(ptr, _p0); |
| 252 | vst1q_f16(ptr + 8, _p1); |
| 253 | vst1q_f16(ptr + 16, _p2); |
| 254 | vst1q_f16(ptr + 24, _p3); |
| 255 | float16x8_t _ss01 = vpaddq_f16(_p0, _p1); |
| 256 | float16x8_t _ss23 = vpaddq_f16(_p2, _p3); |
| 257 | float16x8_t _ss2 = vpaddq_f16(_ss01, _ss23); |
| 258 | _sum = vadd_f16(_sum, vpadd_f16(vget_low_f16(_ss2), vget_high_f16(_ss2))); |
| 259 | vst1_f16(sumptr, _sum); |
| 260 | ptr += 32; |
| 261 | maxptr += 4; |
| 262 | sumptr += 4; |
| 263 | } |
| 264 | for (; j < w; j++) |
| 265 | { |
| 266 | float16x8_t _p = vld1q_f16(ptr); |
| 267 | float16x8_t _max = vdupq_n_f16(*maxptr); |
| 268 | _p = exp_ps_f16(vsubq_f16(_p, _max)); |
| 269 | vst1q_f16(ptr, _p); |
| 270 | float16x4_t _sum2 = vadd_f16(vget_low_f16(_p), vget_high_f16(_p)); |
| 271 | float16x4_t _ss2 = vpadd_f16(_sum2, _sum2); |
| 272 | __fp16 sum0 = vget_lane_f16(_ss2, 0) + vget_lane_f16(_ss2, 1); |
| 273 | *sumptr += sum0; |
| 274 | ptr += 8; |
| 275 | maxptr++; |
| 276 | sumptr++; |
| 277 | } |
| 278 | } |
| 279 | if (elempack == 4) |
| 280 | { |
| 281 | int j = 0; |
| 282 | for (; j + 3 < w; j += 4) |
| 283 | { |
| 284 | float16x8_t _p0 = vld1q_f16(ptr); |
| 285 | float16x8_t _p1 = vld1q_f16(ptr + 8); |
| 286 | float16x4_t _max = vld1_f16(maxptr); |
| 287 | float16x4_t _sum = vld1_f16(sumptr); |
| 288 | float16x8_t _max0 = vcombine_f16(vdup_lane_f16(_max, 0), vdup_lane_f16(_max, 1)); |
| 289 | float16x8_t _max1 = vcombine_f16(vdup_lane_f16(_max, 2), vdup_lane_f16(_max, 3)); |
| 290 | _p0 = exp_ps_f16(vsubq_f16(_p0, _max0)); |
| 291 | _p1 = exp_ps_f16(vsubq_f16(_p1, _max1)); |
| 292 | vst1q_f16(ptr, _p0); |
| 293 | vst1q_f16(ptr + 8, _p1); |
| 294 | float16x8_t _ss2 = vpaddq_f16(_p0, _p1); |
| 295 | _sum = vadd_f16(_sum, vpadd_f16(vget_low_f16(_ss2), vget_high_f16(_ss2))); |
| 296 | vst1_f16(sumptr, _sum); |
| 297 | ptr += 16; |
| 298 | maxptr += 4; |
| 299 | sumptr += 4; |
| 300 | } |
| 301 | for (; j < w; j++) |
| 302 | { |
| 303 | float16x4_t _p = vld1_f16(ptr); |
| 304 | float16x4_t _max = vdup_n_f16(*maxptr); |
| 305 | _p = exp_ps_f16(vsub_f16(_p, _max)); |
| 306 | vst1_f16(ptr, _p); |
| 307 | float16x4_t _ss2 = vpadd_f16(_p, _p); |
| 308 | __fp16 sum0 = vget_lane_f16(_ss2, 0) + vget_lane_f16(_ss2, 1); |
| 309 | *sumptr += sum0; |
| 310 | ptr += 4; |
| 311 | maxptr++; |
| 312 | sumptr++; |
| 313 | } |
| 314 | } |
| 315 | if (elempack == 1) |
| 316 | { |
| 317 | int j = 0; |
| 318 | for (; j + 7 < w; j += 8) |
| 319 | { |
| 320 | float16x8_t _p = vld1q_f16(ptr); |
| 321 | float16x8_t _max = vld1q_f16(maxptr); |
| 322 | float16x8_t _sum = vld1q_f16(sumptr); |
| 323 | _p = exp_ps_f16(vsubq_f16(_p, _max)); |
| 324 | _sum = vaddq_f16(_sum, _p); |
| 325 | vst1q_f16(ptr, _p); |
| 326 | vst1q_f16(sumptr, _sum); |
| 327 | ptr += 8; |
| 328 | maxptr += 8; |
| 329 | sumptr += 8; |
| 330 | } |
| 331 | for (; j + 3 < w; j += 4) |
| 332 | { |
| 333 | float16x4_t _p = vld1_f16(ptr); |
| 334 | float16x4_t _max = vld1_f16(maxptr); |
| 335 | float16x4_t _sum = vld1_f16(sumptr); |
| 336 | _p = exp_ps_f16(vsub_f16(_p, _max)); |
| 337 | _sum = vadd_f16(_sum, _p); |
| 338 | vst1_f16(ptr, _p); |
| 339 | vst1_f16(sumptr, _sum); |
| 340 | ptr += 4; |
| 341 | maxptr += 4; |
| 342 | sumptr += 4; |
| 343 | } |
| 344 | for (; j < w; j++) |
| 345 | { |
| 346 | *ptr = (__fp16)expf(*ptr - *maxptr); |
| 347 | *sumptr += *ptr; |
| 348 | ptr++; |
| 349 | maxptr++; |
| 350 | sumptr++; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | for (int i = 0; i < h; i++) |
| 356 | { |
| 357 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 358 | const __fp16* sumptr = sum; |
| 359 | |
| 360 | if (elempack == 8) |
| 361 | { |
| 362 | for (int j = 0; j < w; j++) |
| 363 | { |
| 364 | float16x8_t _p = vld1q_f16(ptr); |
| 365 | float16x8_t _sum = vld1q_dup_f16(sumptr); |
| 366 | _p = vdivq_f16(_p, _sum); |
| 367 | vst1q_f16(ptr, _p); |
| 368 | ptr += 8; |
| 369 | sumptr++; |
| 370 | } |
| 371 | } |
| 372 | if (elempack == 4) |
| 373 | { |
| 374 | for (int j = 0; j < w; j++) |
| 375 | { |
| 376 | float16x4_t _p = vld1_f16(ptr); |
| 377 | float16x4_t _sum = vld1_dup_f16(sumptr); |
| 378 | _p = vdiv_f16(_p, _sum); |
| 379 | vst1_f16(ptr, _p); |
| 380 | ptr += 4; |
| 381 | sumptr++; |
| 382 | } |
| 383 | } |
| 384 | if (elempack == 1) |
| 385 | { |
| 386 | int j = 0; |
| 387 | for (; j + 7 < w; j += 8) |
| 388 | { |
| 389 | float16x8_t _p = vld1q_f16(ptr); |
| 390 | float16x8_t _sum = vld1q_f16(sumptr); |
| 391 | _p = vdivq_f16(_p, _sum); |
| 392 | vst1q_f16(ptr, _p); |
| 393 | ptr += 8; |
| 394 | sumptr += 8; |
| 395 | } |
| 396 | for (; j + 3 < w; j += 4) |
| 397 | { |
| 398 | float16x4_t _p = vld1_f16(ptr); |
| 399 | float16x4_t _sum = vld1_f16(sumptr); |
| 400 | _p = vdiv_f16(_p, _sum); |
| 401 | vst1_f16(ptr, _p); |
| 402 | ptr += 4; |
| 403 | sumptr += 4; |
| 404 | } |
| 405 | for (; j < w; j++) |
| 406 | { |
| 407 | *ptr /= *sumptr; |
| 408 | ptr++; |
| 409 | sumptr++; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | if (dims == 2 && positive_axis == 1) |
| 416 | { |
| 417 | const int w = bottom_top_blob.w; |
| 418 | const int h = bottom_top_blob.h; |
| 419 | const int size = w * elempack; |
| 420 | |
| 421 | #pragma omp parallel for num_threads(opt.num_threads) |
| 422 | for (int i = 0; i < h; i++) |
| 423 | { |
| 424 | __fp16 max = -65504.f; |
| 425 | float16x8_t _max = vdupq_n_f16(-65504.f); |
| 426 | { |
| 427 | const __fp16* ptr = bottom_top_blob.row<const __fp16>(i); |
| 428 | |
| 429 | int j = 0; |
| 430 | for (; j + 7 < size; j += 8) |
| 431 | { |
| 432 | float16x8_t _p = vld1q_f16(ptr); |
| 433 | _max = vmaxq_f16(_max, _p); |
| 434 | ptr += 8; |
| 435 | } |
| 436 | for (; j + 3 < size; j += 4) |
| 437 | { |
| 438 | float16x4_t _p = vld1_f16(ptr); |
| 439 | _max = vcombine_f16(vmax_f16(vget_low_f16(_max), _p), vget_high_f16(_max)); |
| 440 | ptr += 4; |
| 441 | } |
| 442 | for (; j < size; j++) |
| 443 | { |
| 444 | max = std::max(max, *ptr); |
| 445 | ptr++; |
| 446 | } |
| 447 | |
| 448 | if (elempack == 4) |
| 449 | { |
| 450 | float16x4_t _max2 = vmax_f16(vget_low_f16(_max), vget_high_f16(_max)); |
| 451 | _max = vcombine_f16(_max2, _max2); |
| 452 | } |
| 453 | if (elempack == 1) |
| 454 | { |
| 455 | max = std::max(max, vmaxvq_f16(_max)); |
| 456 | _max = vdupq_n_f16(max); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | __fp16 sum = 0.f; |
| 461 | float16x8_t _sum = vdupq_n_f16(0.f); |
| 462 | { |
| 463 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 464 | |
| 465 | int j = 0; |
| 466 | for (; j + 7 < size; j += 8) |
| 467 | { |
| 468 | float16x8_t _p = vld1q_f16(ptr); |
| 469 | _p = exp_ps_f16(vsubq_f16(_p, _max)); |
| 470 | vst1q_f16(ptr, _p); |
| 471 | _sum = vaddq_f16(_sum, _p); |
| 472 | ptr += 8; |
| 473 | } |
| 474 | for (; j + 3 < size; j += 4) |
| 475 | { |
| 476 | float16x4_t _p = vld1_f16(ptr); |
| 477 | _p = exp_ps_f16(vsub_f16(_p, vget_low_f16(_max))); |
| 478 | vst1_f16(ptr, _p); |
| 479 | _sum = vcombine_f16(vadd_f16(vget_low_f16(_sum), _p), vget_high_f16(_sum)); |
| 480 | ptr += 4; |
| 481 | } |
| 482 | for (; j < size; j++) |
| 483 | { |
| 484 | *ptr = (__fp16)expf(*ptr - max); |
| 485 | sum += *ptr; |
| 486 | ptr++; |
| 487 | } |
| 488 | |
| 489 | if (elempack == 4) |
| 490 | { |
| 491 | float16x4_t _sum2 = vadd_f16(vget_low_f16(_sum), vget_high_f16(_sum)); |
| 492 | _sum = vcombine_f16(_sum2, _sum2); |
| 493 | } |
| 494 | if (elempack == 1) |
| 495 | { |
| 496 | float16x4_t _sum2 = vadd_f16(vget_low_f16(_sum), vget_high_f16(_sum)); |
| 497 | float16x4_t _ss2 = vpadd_f16(_sum2, _sum2); |
| 498 | sum += vget_lane_f16(_ss2, 0) + vget_lane_f16(_ss2, 1); |
| 499 | _sum = vdupq_n_f16(sum); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | { |
| 504 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 505 | |
| 506 | int j = 0; |
| 507 | float16x8_t _reciprocal_sum = vrecpeq_f16(_sum); |
| 508 | _reciprocal_sum = vmulq_f16(vrecpsq_f16(_sum, _reciprocal_sum), _reciprocal_sum); |
| 509 | for (; j + 7 < size; j += 8) |
| 510 | { |
| 511 | float16x8_t _p = vld1q_f16(ptr); |
| 512 | _p = vmulq_f16(_p, _reciprocal_sum); |
| 513 | vst1q_f16(ptr, _p); |
| 514 | ptr += 8; |
| 515 | } |
| 516 | for (; j + 3 < size; j += 4) |
| 517 | { |
| 518 | float16x4_t _p = vld1_f16(ptr); |
| 519 | _p = vmul_f16(_p, vget_low_f16(_reciprocal_sum)); |
| 520 | vst1_f16(ptr, _p); |
| 521 | ptr += 4; |
| 522 | } |
| 523 | for (; j < size; j++) |
| 524 | { |
| 525 | *ptr /= sum; |
| 526 | ptr++; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | if (dims == 3 && positive_axis == 0) |
| 533 | { |
| 534 | const int w = bottom_top_blob.w; |
| 535 | const int h = bottom_top_blob.h; |
| 536 | const int channels = bottom_top_blob.c; |
| 537 | const int size = w * h; |
| 538 | |
| 539 | Mat maxsum(w, h, 2, 2u, opt.workspace_allocator); |
| 540 | if (maxsum.empty()) |
| 541 | return -100; |
| 542 | |
| 543 | Mat max = maxsum.channel(0); |
| 544 | max.fill<__fp16>((__fp16)-65504.f); |
| 545 | |
| 546 | for (int q = 0; q < channels; q++) |
| 547 | { |
| 548 | const __fp16* ptr = bottom_top_blob.channel(q); |
| 549 | __fp16* maxptr = max; |
| 550 | |
| 551 | if (elempack == 8) |
| 552 | { |
| 553 | int i = 0; |
| 554 | for (; i + 3 < size; i += 4) |
| 555 | { |
| 556 | float16x8_t _p0 = vld1q_f16(ptr); |
| 557 | float16x8_t _p1 = vld1q_f16(ptr + 8); |
| 558 | float16x8_t _p2 = vld1q_f16(ptr + 16); |
| 559 | float16x8_t _p3 = vld1q_f16(ptr + 24); |
| 560 | float16x4_t _max = vld1_f16(maxptr); |
| 561 | float16x8_t _max01 = vpmaxq_f16(_p0, _p1); |
| 562 | float16x8_t _max23 = vpmaxq_f16(_p2, _p3); |
| 563 | float16x8_t _max2 = vpmaxq_f16(_max01, _max23); |
| 564 | _max = vmax_f16(_max, vpmax_f16(vget_low_f16(_max2), vget_high_f16(_max2))); |
| 565 | vst1_f16(maxptr, _max); |
| 566 | ptr += 32; |
| 567 | maxptr += 4; |
| 568 | } |
| 569 | for (; i < size; i++) |
| 570 | { |
| 571 | float16x8_t _p = vld1q_f16(ptr); |
| 572 | __fp16 max0 = vmaxvq_f16(_p); |
| 573 | *maxptr = std::max(*maxptr, max0); |
| 574 | ptr += 8; |
| 575 | maxptr++; |
| 576 | } |
| 577 | } |
| 578 | if (elempack == 4) |
| 579 | { |
| 580 | int i = 0; |
| 581 | for (; i + 3 < size; i += 4) |
| 582 | { |
| 583 | float16x8_t _p0 = vld1q_f16(ptr); |
| 584 | float16x8_t _p1 = vld1q_f16(ptr + 8); |
| 585 | float16x4_t _max = vld1_f16(maxptr); |
| 586 | float16x8_t _max2 = vpmaxq_f16(_p0, _p1); |
| 587 | _max = vmax_f16(_max, vpmax_f16(vget_low_f16(_max2), vget_high_f16(_max2))); |
| 588 | vst1_f16(maxptr, _max); |
| 589 | ptr += 16; |
| 590 | maxptr += 4; |
| 591 | } |
| 592 | for (; i < size; i++) |
| 593 | { |
| 594 | float16x4_t _p = vld1_f16(ptr); |
| 595 | __fp16 max0 = vmaxv_f16(_p); |
| 596 | *maxptr = std::max(*maxptr, max0); |
| 597 | ptr += 4; |
| 598 | maxptr++; |
| 599 | } |
| 600 | } |
| 601 | if (elempack == 1) |
| 602 | { |
| 603 | int i = 0; |
| 604 | for (; i + 7 < size; i += 8) |
| 605 | { |
| 606 | float16x8_t _p = vld1q_f16(ptr); |
| 607 | float16x8_t _max = vld1q_f16(maxptr); |
| 608 | _max = vmaxq_f16(_max, _p); |
| 609 | vst1q_f16(maxptr, _max); |
| 610 | ptr += 8; |
| 611 | maxptr += 8; |
| 612 | } |
| 613 | for (; i + 3 < size; i += 4) |
| 614 | { |
| 615 | float16x4_t _p = vld1_f16(ptr); |
| 616 | float16x4_t _max = vld1_f16(maxptr); |
| 617 | _max = vmax_f16(_max, _p); |
| 618 | vst1_f16(maxptr, _max); |
| 619 | ptr += 4; |
| 620 | maxptr += 4; |
| 621 | } |
| 622 | for (; i < size; i++) |
| 623 | { |
| 624 | *maxptr = std::max(*maxptr, *ptr); |
| 625 | ptr++; |
| 626 | maxptr++; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | #pragma omp parallel for num_threads(opt.num_threads) |
| 632 | for (int q = 0; q < channels; q++) |
| 633 | { |
| 634 | __fp16* ptr = bottom_top_blob.channel(q); |
| 635 | const __fp16* maxptr = max; |
| 636 | |
| 637 | if (elempack == 8) |
| 638 | { |
| 639 | int i = 0; |
| 640 | for (; i + 3 < size; i += 4) |
| 641 | { |
| 642 | float16x8_t _p0 = vld1q_f16(ptr); |
| 643 | float16x8_t _p1 = vld1q_f16(ptr + 8); |
| 644 | float16x8_t _p2 = vld1q_f16(ptr + 16); |
| 645 | float16x8_t _p3 = vld1q_f16(ptr + 24); |
| 646 | float16x4_t _max = vld1_f16(maxptr); |
| 647 | _p0 = exp_ps_f16(vsubq_f16(_p0, vdupq_lane_f16(_max, 0))); |
| 648 | _p1 = exp_ps_f16(vsubq_f16(_p1, vdupq_lane_f16(_max, 1))); |
| 649 | _p2 = exp_ps_f16(vsubq_f16(_p2, vdupq_lane_f16(_max, 2))); |
| 650 | _p3 = exp_ps_f16(vsubq_f16(_p3, vdupq_lane_f16(_max, 3))); |
| 651 | vst1q_f16(ptr, _p0); |
| 652 | vst1q_f16(ptr + 8, _p1); |
| 653 | vst1q_f16(ptr + 16, _p2); |
| 654 | vst1q_f16(ptr + 24, _p3); |
| 655 | ptr += 32; |
| 656 | maxptr += 4; |
| 657 | } |
| 658 | for (; i < size; i++) |
| 659 | { |
| 660 | float16x8_t _p = vld1q_f16(ptr); |
| 661 | float16x8_t _max = vdupq_n_f16(*maxptr); |
| 662 | _p = exp_ps_f16(vsubq_f16(_p, _max)); |
| 663 | vst1q_f16(ptr, _p); |
| 664 | ptr += 8; |
| 665 | maxptr++; |
| 666 | } |
| 667 | } |
| 668 | if (elempack == 4) |
| 669 | { |
| 670 | int i = 0; |
| 671 | for (; i + 3 < size; i += 4) |
| 672 | { |
| 673 | float16x8_t _p0 = vld1q_f16(ptr); |
| 674 | float16x8_t _p1 = vld1q_f16(ptr + 8); |
| 675 | float16x4_t _max = vld1_f16(maxptr); |
| 676 | float16x8_t _max0 = vcombine_f16(vdup_lane_f16(_max, 0), vdup_lane_f16(_max, 1)); |
| 677 | float16x8_t _max1 = vcombine_f16(vdup_lane_f16(_max, 2), vdup_lane_f16(_max, 3)); |
| 678 | _p0 = exp_ps_f16(vsubq_f16(_p0, _max0)); |
| 679 | _p1 = exp_ps_f16(vsubq_f16(_p1, _max1)); |
| 680 | vst1q_f16(ptr, _p0); |
| 681 | vst1q_f16(ptr + 8, _p1); |
| 682 | ptr += 16; |
| 683 | maxptr += 4; |
| 684 | } |
| 685 | for (; i < size; i++) |
| 686 | { |
| 687 | float16x4_t _p = vld1_f16(ptr); |
| 688 | float16x4_t _max = vdup_n_f16(*maxptr); |
| 689 | _p = exp_ps_f16(vsub_f16(_p, _max)); |
| 690 | vst1_f16(ptr, _p); |
| 691 | ptr += 4; |
| 692 | maxptr++; |
| 693 | } |
| 694 | } |
| 695 | if (elempack == 1) |
| 696 | { |
| 697 | int i = 0; |
| 698 | for (; i + 7 < size; i += 8) |
| 699 | { |
| 700 | float16x8_t _p = vld1q_f16(ptr); |
| 701 | float16x8_t _max = vld1q_f16(maxptr); |
| 702 | _p = exp_ps_f16(vsubq_f16(_p, _max)); |
| 703 | vst1q_f16(ptr, _p); |
| 704 | ptr += 8; |
| 705 | maxptr += 8; |
| 706 | } |
| 707 | for (; i + 3 < size; i += 4) |
| 708 | { |
| 709 | float16x4_t _p = vld1_f16(ptr); |
| 710 | float16x4_t _max = vld1_f16(maxptr); |
| 711 | _p = exp_ps_f16(vsub_f16(_p, _max)); |
| 712 | vst1_f16(ptr, _p); |
| 713 | ptr += 4; |
| 714 | maxptr += 4; |
| 715 | } |
| 716 | for (; i < size; i++) |
| 717 | { |
| 718 | *ptr = (__fp16)expf(*ptr - *maxptr); |
| 719 | ptr++; |
| 720 | maxptr++; |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | Mat sum = maxsum.channel(1); |
| 726 | sum.fill<__fp16>((__fp16)0.f); |
| 727 | |
| 728 | for (int q = 0; q < channels; q++) |
| 729 | { |
| 730 | __fp16* ptr = bottom_top_blob.channel(q); |
| 731 | __fp16* sumptr = sum; |
| 732 | |
| 733 | if (elempack == 8) |
| 734 | { |
| 735 | int i = 0; |
| 736 | for (; i + 3 < size; i += 4) |
| 737 | { |
| 738 | float16x8_t _p0 = vld1q_f16(ptr); |
| 739 | float16x8_t _p1 = vld1q_f16(ptr + 8); |
| 740 | float16x8_t _p2 = vld1q_f16(ptr + 16); |
| 741 | float16x8_t _p3 = vld1q_f16(ptr + 24); |
| 742 | float16x4_t _sum = vld1_f16(sumptr); |
| 743 | float16x8_t _ss01 = vpaddq_f16(_p0, _p1); |
| 744 | float16x8_t _ss23 = vpaddq_f16(_p2, _p3); |
| 745 | float16x8_t _ss2 = vpaddq_f16(_ss01, _ss23); |
| 746 | _sum = vadd_f16(_sum, vpadd_f16(vget_low_f16(_ss2), vget_high_f16(_ss2))); |
| 747 | vst1_f16(sumptr, _sum); |
| 748 | ptr += 32; |
| 749 | sumptr += 4; |
| 750 | } |
| 751 | for (; i < size; i++) |
| 752 | { |
| 753 | float16x8_t _p = vld1q_f16(ptr); |
| 754 | float16x4_t _sum2 = vadd_f16(vget_low_f16(_p), vget_high_f16(_p)); |
| 755 | float16x4_t _ss2 = vpadd_f16(_sum2, _sum2); |
| 756 | __fp16 sum0 = vget_lane_f16(_ss2, 0) + vget_lane_f16(_ss2, 1); |
| 757 | *sumptr += sum0; |
| 758 | ptr += 8; |
| 759 | sumptr++; |
| 760 | } |
| 761 | } |
| 762 | if (elempack == 4) |
| 763 | { |
| 764 | int i = 0; |
| 765 | for (; i + 3 < size; i += 4) |
| 766 | { |
| 767 | float16x8_t _p0 = vld1q_f16(ptr); |
| 768 | float16x8_t _p1 = vld1q_f16(ptr + 8); |
| 769 | float16x4_t _sum = vld1_f16(sumptr); |
| 770 | float16x8_t _ss2 = vpaddq_f16(_p0, _p1); |
| 771 | _sum = vadd_f16(_sum, vpadd_f16(vget_low_f16(_ss2), vget_high_f16(_ss2))); |
| 772 | vst1_f16(sumptr, _sum); |
| 773 | ptr += 16; |
| 774 | sumptr += 4; |
| 775 | } |
| 776 | for (; i < size; i++) |
| 777 | { |
| 778 | float16x4_t _p = vld1_f16(ptr); |
| 779 | float16x4_t _ss2 = vpadd_f16(_p, _p); |
| 780 | __fp16 sum0 = vget_lane_f16(_ss2, 0) + vget_lane_f16(_ss2, 1); |
| 781 | *sumptr += sum0; |
| 782 | ptr += 4; |
| 783 | sumptr++; |
| 784 | } |
| 785 | } |
| 786 | if (elempack == 1) |
| 787 | { |
| 788 | int i = 0; |
| 789 | for (; i + 7 < size; i += 8) |
| 790 | { |
| 791 | float16x8_t _p = vld1q_f16(ptr); |
| 792 | float16x8_t _sum = vld1q_f16(sumptr); |
| 793 | _sum = vaddq_f16(_sum, _p); |
| 794 | vst1q_f16(sumptr, _sum); |
| 795 | ptr += 8; |
| 796 | sumptr += 8; |
| 797 | } |
| 798 | for (; i + 3 < size; i += 4) |
| 799 | { |
| 800 | float16x4_t _p = vld1_f16(ptr); |
| 801 | float16x4_t _sum = vld1_f16(sumptr); |
| 802 | _sum = vadd_f16(_sum, _p); |
| 803 | vst1_f16(sumptr, _sum); |
| 804 | ptr += 4; |
| 805 | sumptr += 4; |
| 806 | } |
| 807 | for (; i < size; i++) |
| 808 | { |
| 809 | *sumptr += *ptr; |
| 810 | ptr++; |
| 811 | sumptr++; |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | #pragma omp parallel for num_threads(opt.num_threads) |
| 817 | for (int q = 0; q < channels; q++) |
| 818 | { |
| 819 | __fp16* ptr = bottom_top_blob.channel(q); |
| 820 | const __fp16* sumptr = sum; |
| 821 | |
| 822 | if (elempack == 8) |
| 823 | { |
| 824 | for (int i = 0; i < size; i++) |
| 825 | { |
| 826 | float16x8_t _p = vld1q_f16(ptr); |
| 827 | float16x8_t _sum = vld1q_dup_f16(sumptr); |
| 828 | _p = vdivq_f16(_p, _sum); |
| 829 | vst1q_f16(ptr, _p); |
| 830 | ptr += 8; |
| 831 | sumptr++; |
| 832 | } |
| 833 | } |
| 834 | if (elempack == 4) |
| 835 | { |
| 836 | for (int i = 0; i < size; i++) |
| 837 | { |
| 838 | float16x4_t _p = vld1_f16(ptr); |
| 839 | float16x4_t _sum = vld1_dup_f16(sumptr); |
| 840 | _p = vdiv_f16(_p, _sum); |
| 841 | vst1_f16(ptr, _p); |
| 842 | ptr += 4; |
| 843 | sumptr++; |
| 844 | } |
| 845 | } |
| 846 | if (elempack == 1) |
| 847 | { |
| 848 | int i = 0; |
| 849 | for (; i + 7 < size; i += 8) |
| 850 | { |
| 851 | float16x8_t _p = vld1q_f16(ptr); |
| 852 | float16x8_t _sum = vld1q_f16(sumptr); |
| 853 | _p = vdivq_f16(_p, _sum); |
| 854 | vst1q_f16(ptr, _p); |
| 855 | ptr += 8; |
| 856 | sumptr += 8; |
| 857 | } |
| 858 | for (; i + 3 < size; i += 4) |
| 859 | { |
| 860 | float16x4_t _p = vld1_f16(ptr); |
| 861 | float16x4_t _sum = vld1_f16(sumptr); |
| 862 | _p = vdiv_f16(_p, _sum); |
| 863 | vst1_f16(ptr, _p); |
| 864 | ptr += 4; |
| 865 | sumptr += 4; |
| 866 | } |
| 867 | for (; i < size; i++) |
| 868 | { |
| 869 | *ptr /= *sumptr; |
| 870 | ptr++; |
| 871 | sumptr++; |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | if (dims == 3 && positive_axis == 1) |
| 878 | { |
| 879 | const int w = bottom_top_blob.w; |
| 880 | const int h = bottom_top_blob.h; |
| 881 | const int channels = bottom_top_blob.c; |
| 882 | const int size = w * elempack; |
| 883 | |
| 884 | Mat maxsum(w * elempack, channels, 2, 2u, opt.workspace_allocator); |
| 885 | if (maxsum.empty()) |
| 886 | return -100; |
| 887 | |
| 888 | Mat max = maxsum.channel(0); |
| 889 | max.fill<__fp16>((__fp16)-65504.f); |
| 890 | |
| 891 | #pragma omp parallel for num_threads(opt.num_threads) |
| 892 | for (int q = 0; q < channels; q++) |
| 893 | { |
| 894 | const __fp16* ptr = bottom_top_blob.channel(q); |
| 895 | |
| 896 | for (int i = 0; i < h; i++) |
| 897 | { |
| 898 | __fp16* maxptr = max.row<__fp16>(q); |
| 899 | |
| 900 | int j = 0; |
| 901 | for (; j + 7 < size; j += 8) |
| 902 | { |
| 903 | float16x8_t _p = vld1q_f16(ptr); |
| 904 | float16x8_t _max = vld1q_f16(maxptr); |
| 905 | _max = vmaxq_f16(_max, _p); |
| 906 | vst1q_f16(maxptr, _max); |
| 907 | ptr += 8; |
| 908 | maxptr += 8; |
| 909 | } |
| 910 | for (; j + 3 < size; j += 4) |
| 911 | { |
| 912 | float16x4_t _p = vld1_f16(ptr); |
| 913 | float16x4_t _max = vld1_f16(maxptr); |
| 914 | _max = vmax_f16(_max, _p); |
| 915 | vst1_f16(maxptr, _max); |
| 916 | ptr += 4; |
| 917 | maxptr += 4; |
| 918 | } |
| 919 | for (; j < size; j++) |
| 920 | { |
| 921 | *maxptr = std::max(*maxptr, *ptr); |
| 922 | ptr++; |
| 923 | maxptr++; |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | Mat sum = maxsum.channel(1); |
| 929 | sum.fill<__fp16>((__fp16)0.f); |
| 930 | |
| 931 | #pragma omp parallel for num_threads(opt.num_threads) |
| 932 | for (int q = 0; q < channels; q++) |
| 933 | { |
| 934 | __fp16* ptr = bottom_top_blob.channel(q); |
| 935 | |
| 936 | for (int i = 0; i < h; i++) |
| 937 | { |
| 938 | const __fp16* maxptr = max.row<const __fp16>(q); |
| 939 | __fp16* sumptr = sum.row<__fp16>(q); |
| 940 | |
| 941 | int j = 0; |
| 942 | for (; j + 7 < size; j += 8) |
| 943 | { |
| 944 | float16x8_t _p = vld1q_f16(ptr); |
| 945 | float16x8_t _max = vld1q_f16(maxptr); |
| 946 | float16x8_t _sum = vld1q_f16(sumptr); |
| 947 | _p = exp_ps_f16(vsubq_f16(_p, _max)); |
| 948 | _sum = vaddq_f16(_sum, _p); |
| 949 | vst1q_f16(ptr, _p); |
| 950 | vst1q_f16(sumptr, _sum); |
| 951 | ptr += 8; |
| 952 | maxptr += 8; |
| 953 | sumptr += 8; |
| 954 | } |
| 955 | for (; j + 3 < size; j += 4) |
| 956 | { |
| 957 | float16x4_t _p = vld1_f16(ptr); |
| 958 | float16x4_t _max = vld1_f16(maxptr); |
| 959 | float16x4_t _sum = vld1_f16(sumptr); |
| 960 | _p = exp_ps_f16(vsub_f16(_p, _max)); |
| 961 | _sum = vadd_f16(_sum, _p); |
| 962 | vst1_f16(ptr, _p); |
| 963 | vst1_f16(sumptr, _sum); |
| 964 | ptr += 4; |
| 965 | maxptr += 4; |
| 966 | sumptr += 4; |
| 967 | } |
| 968 | for (; j < size; j++) |
| 969 | { |
| 970 | *ptr = (__fp16)expf(*ptr - *maxptr); |
| 971 | *sumptr += *ptr; |
| 972 | ptr++; |
| 973 | maxptr++; |
| 974 | sumptr++; |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | #pragma omp parallel for num_threads(opt.num_threads) |
| 980 | for (int q = 0; q < channels; q++) |
| 981 | { |
| 982 | __fp16* ptr = bottom_top_blob.channel(q); |
| 983 | |
| 984 | for (int i = 0; i < h; i++) |
| 985 | { |
| 986 | const __fp16* sumptr = sum.row<const __fp16>(q); |
| 987 | |
| 988 | int j = 0; |
| 989 | for (; j + 7 < size; j += 8) |
| 990 | { |
| 991 | float16x8_t _p = vld1q_f16(ptr); |
| 992 | float16x8_t _sum = vld1q_f16(sumptr); |
| 993 | _p = vdivq_f16(_p, _sum); |
| 994 | vst1q_f16(ptr, _p); |
| 995 | ptr += 8; |
| 996 | sumptr += 8; |
| 997 | } |
| 998 | for (; j + 3 < size; j += 4) |
| 999 | { |
| 1000 | float16x4_t _p = vld1_f16(ptr); |
| 1001 | float16x4_t _sum = vld1_f16(sumptr); |
| 1002 | _p = vdiv_f16(_p, _sum); |
| 1003 | vst1_f16(ptr, _p); |
| 1004 | ptr += 4; |
| 1005 | sumptr += 4; |
| 1006 | } |
| 1007 | for (; j < size; j++) |
| 1008 | { |
| 1009 | *ptr /= *sumptr; |
| 1010 | ptr++; |
| 1011 | sumptr++; |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | if (dims == 3 && positive_axis == 2) |
| 1018 | { |
| 1019 | const int w = bottom_top_blob.w; |
| 1020 | const int h = bottom_top_blob.h; |
| 1021 | const int channels = bottom_top_blob.c; |
| 1022 | const int size = w * elempack; |
| 1023 | |
| 1024 | #pragma omp parallel for num_threads(opt.num_threads) |
| 1025 | for (int q = 0; q < channels; q++) |
| 1026 | { |
| 1027 | for (int i = 0; i < h; i++) |
| 1028 | { |
| 1029 | __fp16 max = -65504.f; |
| 1030 | float16x8_t _max = vdupq_n_f16(-65504.f); |
| 1031 | { |
| 1032 | const __fp16* ptr = bottom_top_blob.channel(q).row<const __fp16>(i); |
| 1033 | |
| 1034 | int j = 0; |
| 1035 | for (; j + 7 < size; j += 8) |
| 1036 | { |
| 1037 | float16x8_t _p = vld1q_f16(ptr); |
| 1038 | _max = vmaxq_f16(_max, _p); |
| 1039 | ptr += 8; |
| 1040 | } |
| 1041 | for (; j + 3 < size; j += 4) |
| 1042 | { |
| 1043 | float16x4_t _p = vld1_f16(ptr); |
| 1044 | _max = vcombine_f16(vmax_f16(vget_low_f16(_max), _p), vget_high_f16(_max)); |
| 1045 | ptr += 4; |
| 1046 | } |
| 1047 | for (; j < size; j++) |
| 1048 | { |
| 1049 | max = std::max(max, *ptr); |
| 1050 | ptr++; |
| 1051 | } |
| 1052 | |
| 1053 | if (elempack == 4) |
| 1054 | { |
| 1055 | float16x4_t _max2 = vmax_f16(vget_low_f16(_max), vget_high_f16(_max)); |
| 1056 | _max = vcombine_f16(_max2, _max2); |
| 1057 | } |
| 1058 | if (elempack == 1) |
| 1059 | { |
| 1060 | max = std::max(max, vmaxvq_f16(_max)); |
| 1061 | _max = vdupq_n_f16(max); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | __fp16 sum = 0.f; |
| 1066 | float16x8_t _sum = vdupq_n_f16(0.f); |
| 1067 | { |
| 1068 | __fp16* ptr = bottom_top_blob.channel(q).row<__fp16>(i); |
| 1069 | |
| 1070 | int j = 0; |
| 1071 | for (; j + 7 < size; j += 8) |
| 1072 | { |
| 1073 | float16x8_t _p = vld1q_f16(ptr); |
| 1074 | _p = exp_ps_f16(vsubq_f16(_p, _max)); |
| 1075 | vst1q_f16(ptr, _p); |
| 1076 | _sum = vaddq_f16(_sum, _p); |
| 1077 | ptr += 8; |
| 1078 | } |
| 1079 | for (; j + 3 < size; j += 4) |
| 1080 | { |
| 1081 | float16x4_t _p = vld1_f16(ptr); |
| 1082 | _p = exp_ps_f16(vsub_f16(_p, vget_low_f16(_max))); |
| 1083 | vst1_f16(ptr, _p); |
| 1084 | _sum = vcombine_f16(vadd_f16(vget_low_f16(_sum), _p), vget_high_f16(_sum)); |
| 1085 | ptr += 4; |
| 1086 | } |
| 1087 | for (; j < size; j++) |
| 1088 | { |
| 1089 | *ptr = (__fp16)expf(*ptr - max); |
| 1090 | sum += *ptr; |
| 1091 | ptr++; |
| 1092 | } |
| 1093 | |
| 1094 | if (elempack == 4) |
| 1095 | { |
| 1096 | float16x4_t _sum2 = vadd_f16(vget_low_f16(_sum), vget_high_f16(_sum)); |
| 1097 | _sum = vcombine_f16(_sum2, _sum2); |
| 1098 | } |
| 1099 | if (elempack == 1) |
| 1100 | { |
| 1101 | float16x4_t _sum2 = vadd_f16(vget_low_f16(_sum), vget_high_f16(_sum)); |
| 1102 | float16x4_t _ss2 = vpadd_f16(_sum2, _sum2); |
| 1103 | sum += vget_lane_f16(_ss2, 0) + vget_lane_f16(_ss2, 1); |
| 1104 | _sum = vdupq_n_f16(sum); |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | { |
| 1109 | __fp16* ptr = bottom_top_blob.channel(q).row<__fp16>(i); |
| 1110 | |
| 1111 | int j = 0; |
| 1112 | float16x8_t _reciprocal_sum = vrecpeq_f16(_sum); |
| 1113 | _reciprocal_sum = vmulq_f16(vrecpsq_f16(_sum, _reciprocal_sum), _reciprocal_sum); |
| 1114 | for (; j + 7 < size; j += 8) |
| 1115 | { |
| 1116 | float16x8_t _p = vld1q_f16(ptr); |
| 1117 | _p = vmulq_f16(_p, _reciprocal_sum); |
| 1118 | vst1q_f16(ptr, _p); |
| 1119 | ptr += 8; |
| 1120 | } |
| 1121 | for (; j + 3 < size; j += 4) |
| 1122 | { |
| 1123 | float16x4_t _p = vld1_f16(ptr); |
| 1124 | _p = vmul_f16(_p, vget_low_f16(_reciprocal_sum)); |
| 1125 | vst1_f16(ptr, _p); |
| 1126 | ptr += 4; |
| 1127 | } |
| 1128 | for (; j < size; j++) |
| 1129 | { |
| 1130 | *ptr /= sum; |
| 1131 | ptr++; |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | return 0; |
| 1139 | } |
| 1140 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 1141 | |
| 1142 | } // namespace ncnn |