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