| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 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 "batchnorm_arm.h" |
| 16 | |
| 17 | #if __ARM_NEON |
| 18 | #include <arm_neon.h> |
| 19 | #endif // __ARM_NEON |
| 20 | |
| 21 | #include "arm_usability.h" |
| 22 | |
| 23 | namespace ncnn { |
| 24 | |
| 25 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 26 | int BatchNorm_arm::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const |
| 27 | { |
| 28 | int dims = bottom_top_blob.dims; |
| 29 | int elempack = bottom_top_blob.elempack; |
| 30 | |
| 31 | if (elempack == 4) |
| 32 | { |
| 33 | if (dims == 1) |
| 34 | { |
| 35 | int w = bottom_top_blob.w; |
| 36 | |
| 37 | #pragma omp parallel for num_threads(opt.num_threads) |
| 38 | for (int i = 0; i < w; i++) |
| 39 | { |
| 40 | __fp16* ptr = (__fp16*)bottom_top_blob + i * 4; |
| 41 | |
| 42 | float32x4_t _a = vld1q_f32((const float*)a_data + i * 4); |
| 43 | float32x4_t _b = vld1q_f32((const float*)b_data + i * 4); |
| 44 | |
| 45 | float32x4_t _p = vcvt_f32_f16(vld1_f16(ptr)); |
| 46 | _p = vfmaq_f32(_a, _p, _b); |
| 47 | vst1_f16(ptr, vcvt_f16_f32(_p)); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (dims == 2) |
| 52 | { |
| 53 | int w = bottom_top_blob.w; |
| 54 | int h = bottom_top_blob.h; |
| 55 | |
| 56 | #pragma omp parallel for num_threads(opt.num_threads) |
| 57 | for (int i = 0; i < h; i++) |
| 58 | { |
| 59 | float32x4_t _a = vld1q_f32((const float*)a_data + i * 4); |
| 60 | float32x4_t _b = vld1q_f32((const float*)b_data + i * 4); |
| 61 | |
| 62 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 63 | |
| 64 | for (int j = 0; j < w; j++) |
| 65 | { |
| 66 | float32x4_t _p = vcvt_f32_f16(vld1_f16(ptr)); |
| 67 | _p = vfmaq_f32(_a, _p, _b); |
| 68 | vst1_f16(ptr, vcvt_f16_f32(_p)); |
| 69 | |
| 70 | ptr += 4; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (dims == 3 || dims == 4) |
| 76 | { |
| 77 | int w = bottom_top_blob.w; |
| 78 | int h = bottom_top_blob.h; |
| 79 | int d = bottom_top_blob.d; |
| 80 | int c = bottom_top_blob.c; |
| 81 | int size = w * h * d; |
| 82 | |
| 83 | #pragma omp parallel for num_threads(opt.num_threads) |
| 84 | for (int q = 0; q < c; q++) |
| 85 | { |
| 86 | float32x4_t _a = vld1q_f32((const float*)a_data + q * 4); |
| 87 | float32x4_t _b = vld1q_f32((const float*)b_data + q * 4); |
| 88 | |
| 89 | __fp16* ptr = bottom_top_blob.channel(q); |
| 90 | |
| 91 | for (int i = 0; i < size; i++) |
| 92 | { |
| 93 | float32x4_t _p = vcvt_f32_f16(vld1_f16(ptr)); |
| 94 | _p = vfmaq_f32(_a, _p, _b); |
| 95 | vst1_f16(ptr, vcvt_f16_f32(_p)); |
| 96 | |
| 97 | ptr += 4; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | if (dims == 1) |
| 106 | { |
| 107 | int w = bottom_top_blob.w; |
| 108 | |
| 109 | __fp16* ptr = bottom_top_blob; |
| 110 | |
| 111 | #pragma omp parallel for num_threads(opt.num_threads) |
| 112 | for (int i = 0; i < w; i++) |
| 113 | { |
| 114 | ptr[i] = b_data[i] * (float)ptr[i] + a_data[i]; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (dims == 2) |
| 119 | { |
| 120 | int w = bottom_top_blob.w; |
| 121 | int h = bottom_top_blob.h; |
| 122 | |
| 123 | #pragma omp parallel for num_threads(opt.num_threads) |
| 124 | for (int i = 0; i < h; i++) |
| 125 | { |
| 126 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 127 | |
| 128 | float a = a_data[i]; |
| 129 | float b = b_data[i]; |
| 130 | |
| 131 | float32x4_t _a = vdupq_n_f32(a); |
| 132 | float32x4_t _b = vdupq_n_f32(b); |
| 133 | |
| 134 | int j = 0; |
| 135 | for (; j + 3 < w; j += 4) |
| 136 | { |
| 137 | float32x4_t _p = vcvt_f32_f16(vld1_f16(ptr)); |
| 138 | _p = vfmaq_f32(_a, _p, _b); |
| 139 | vst1_f16(ptr, vcvt_f16_f32(_p)); |
| 140 | |
| 141 | ptr += 4; |
| 142 | } |
| 143 | for (; j < w; j++) |
| 144 | { |
| 145 | *ptr = b * (float)*ptr + a; |
| 146 | |
| 147 | ptr++; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (dims == 3 || dims == 4) |
| 153 | { |
| 154 | int w = bottom_top_blob.w; |
| 155 | int h = bottom_top_blob.h; |
| 156 | int d = bottom_top_blob.d; |
| 157 | int c = bottom_top_blob.c; |
| 158 | int size = w * h * d; |
| 159 | |
| 160 | #pragma omp parallel for num_threads(opt.num_threads) |
| 161 | for (int q = 0; q < c; q++) |
| 162 | { |
| 163 | __fp16* ptr = bottom_top_blob.channel(q); |
| 164 | |
| 165 | float a = a_data[q]; |
| 166 | float b = b_data[q]; |
| 167 | |
| 168 | float32x4_t _a = vdupq_n_f32(a); |
| 169 | float32x4_t _b = vdupq_n_f32(b); |
| 170 | |
| 171 | int j = 0; |
| 172 | for (; j + 3 < size; j += 4) |
| 173 | { |
| 174 | float32x4_t _p = vcvt_f32_f16(vld1_f16(ptr)); |
| 175 | _p = vfmaq_f32(_a, _p, _b); |
| 176 | vst1_f16(ptr, vcvt_f16_f32(_p)); |
| 177 | |
| 178 | ptr += 4; |
| 179 | } |
| 180 | for (; j < size; j++) |
| 181 | { |
| 182 | *ptr = b * (float)*ptr + a; |
| 183 | |
| 184 | ptr++; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | int BatchNorm_arm::forward_inplace_fp16sa(Mat& bottom_top_blob, const Option& opt) const |
| 193 | { |
| 194 | int dims = bottom_top_blob.dims; |
| 195 | int elempack = bottom_top_blob.elempack; |
| 196 | |
| 197 | if (elempack == 8) |
| 198 | { |
| 199 | if (dims == 1) |
| 200 | { |
| 201 | int w = bottom_top_blob.w; |
| 202 | |
| 203 | #pragma omp parallel for num_threads(opt.num_threads) |
| 204 | for (int i = 0; i < w; i++) |
| 205 | { |
| 206 | __fp16* ptr = (__fp16*)bottom_top_blob + i * 8; |
| 207 | |
| 208 | float16x8_t _a = vcombine_f16(vcvt_f16_f32(vld1q_f32((const float*)a_data + i * 8)), vcvt_f16_f32(vld1q_f32((const float*)a_data + i * 8 + 4))); |
| 209 | float16x8_t _b = vcombine_f16(vcvt_f16_f32(vld1q_f32((const float*)b_data + i * 8)), vcvt_f16_f32(vld1q_f32((const float*)b_data + i * 8 + 4))); |
| 210 | |
| 211 | float16x8_t _p = vld1q_f16(ptr); |
| 212 | _p = vfmaq_f16(_a, _p, _b); |
| 213 | vst1q_f16(ptr, _p); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if (dims == 2) |
| 218 | { |
| 219 | int w = bottom_top_blob.w; |
| 220 | int h = bottom_top_blob.h; |
| 221 | |
| 222 | #pragma omp parallel for num_threads(opt.num_threads) |
| 223 | for (int i = 0; i < h; i++) |
| 224 | { |
| 225 | float16x8_t _a = vcombine_f16(vcvt_f16_f32(vld1q_f32((const float*)a_data + i * 8)), vcvt_f16_f32(vld1q_f32((const float*)a_data + i * 8 + 4))); |
| 226 | float16x8_t _b = vcombine_f16(vcvt_f16_f32(vld1q_f32((const float*)b_data + i * 8)), vcvt_f16_f32(vld1q_f32((const float*)b_data + i * 8 + 4))); |
| 227 | |
| 228 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 229 | |
| 230 | for (int j = 0; j < w; j++) |
| 231 | { |
| 232 | float16x8_t _p = vld1q_f16(ptr); |
| 233 | _p = vfmaq_f16(_a, _p, _b); |
| 234 | vst1q_f16(ptr, _p); |
| 235 | |
| 236 | ptr += 8; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if (dims == 3 || dims == 4) |
| 242 | { |
| 243 | int w = bottom_top_blob.w; |
| 244 | int h = bottom_top_blob.h; |
| 245 | int d = bottom_top_blob.d; |
| 246 | int c = bottom_top_blob.c; |
| 247 | int size = w * h * d; |
| 248 | |
| 249 | #pragma omp parallel for num_threads(opt.num_threads) |
| 250 | for (int q = 0; q < c; q++) |
| 251 | { |
| 252 | float16x8_t _a = vcombine_f16(vcvt_f16_f32(vld1q_f32((const float*)a_data + q * 8)), vcvt_f16_f32(vld1q_f32((const float*)a_data + q * 8 + 4))); |
| 253 | float16x8_t _b = vcombine_f16(vcvt_f16_f32(vld1q_f32((const float*)b_data + q * 8)), vcvt_f16_f32(vld1q_f32((const float*)b_data + q * 8 + 4))); |
| 254 | |
| 255 | __fp16* ptr = bottom_top_blob.channel(q); |
| 256 | |
| 257 | for (int i = 0; i < size; i++) |
| 258 | { |
| 259 | float16x8_t _p = vld1q_f16(ptr); |
| 260 | _p = vfmaq_f16(_a, _p, _b); |
| 261 | vst1q_f16(ptr, _p); |
| 262 | |
| 263 | ptr += 8; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | if (elempack == 4) |
| 272 | { |
| 273 | if (dims == 1) |
| 274 | { |
| 275 | int w = bottom_top_blob.w; |
| 276 | |
| 277 | #pragma omp parallel for num_threads(opt.num_threads) |
| 278 | for (int i = 0; i < w; i++) |
| 279 | { |
| 280 | __fp16* ptr = (__fp16*)bottom_top_blob + i * 4; |
| 281 | |
| 282 | float16x4_t _a = vcvt_f16_f32(vld1q_f32((const float*)a_data + i * 4)); |
| 283 | float16x4_t _b = vcvt_f16_f32(vld1q_f32((const float*)b_data + i * 4)); |
| 284 | |
| 285 | float16x4_t _p = vld1_f16(ptr); |
| 286 | _p = vfma_f16(_a, _p, _b); |
| 287 | vst1_f16(ptr, _p); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if (dims == 2) |
| 292 | { |
| 293 | int w = bottom_top_blob.w; |
| 294 | int h = bottom_top_blob.h; |
| 295 | |
| 296 | #pragma omp parallel for num_threads(opt.num_threads) |
| 297 | for (int i = 0; i < h; i++) |
| 298 | { |
| 299 | float16x4_t _a = vcvt_f16_f32(vld1q_f32((const float*)a_data + i * 4)); |
| 300 | float16x4_t _b = vcvt_f16_f32(vld1q_f32((const float*)b_data + i * 4)); |
| 301 | |
| 302 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 303 | |
| 304 | for (int j = 0; j < w; j++) |
| 305 | { |
| 306 | float16x4_t _p = vld1_f16(ptr); |
| 307 | _p = vfma_f16(_a, _p, _b); |
| 308 | vst1_f16(ptr, _p); |
| 309 | |
| 310 | ptr += 4; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (dims == 3 || dims == 4) |
| 316 | { |
| 317 | int w = bottom_top_blob.w; |
| 318 | int h = bottom_top_blob.h; |
| 319 | int d = bottom_top_blob.d; |
| 320 | int c = bottom_top_blob.c; |
| 321 | int size = w * h * d; |
| 322 | |
| 323 | #pragma omp parallel for num_threads(opt.num_threads) |
| 324 | for (int q = 0; q < c; q++) |
| 325 | { |
| 326 | float16x4_t _a = vcvt_f16_f32(vld1q_f32((const float*)a_data + q * 4)); |
| 327 | float16x4_t _b = vcvt_f16_f32(vld1q_f32((const float*)b_data + q * 4)); |
| 328 | |
| 329 | __fp16* ptr = bottom_top_blob.channel(q); |
| 330 | |
| 331 | for (int i = 0; i < size; i++) |
| 332 | { |
| 333 | float16x4_t _p = vld1_f16(ptr); |
| 334 | _p = vfma_f16(_a, _p, _b); |
| 335 | vst1_f16(ptr, _p); |
| 336 | |
| 337 | ptr += 4; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | if (dims == 1) |
| 346 | { |
| 347 | int w = bottom_top_blob.w; |
| 348 | |
| 349 | __fp16* ptr = bottom_top_blob; |
| 350 | |
| 351 | #pragma omp parallel for num_threads(opt.num_threads) |
| 352 | for (int i = 0; i < w; i++) |
| 353 | { |
| 354 | ptr[i] = (__fp16)b_data[i] * ptr[i] + (__fp16)a_data[i]; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if (dims == 2) |
| 359 | { |
| 360 | int w = bottom_top_blob.w; |
| 361 | int h = bottom_top_blob.h; |
| 362 | |
| 363 | #pragma omp parallel for num_threads(opt.num_threads) |
| 364 | for (int i = 0; i < h; i++) |
| 365 | { |
| 366 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 367 | |
| 368 | __fp16 a = (__fp16)a_data[i]; |
| 369 | __fp16 b = (__fp16)b_data[i]; |
| 370 | |
| 371 | float16x4_t _a = vdup_n_f16(a); |
| 372 | #if defined(_MSC_VER) && !defined(__clang__) |
| 373 | float16x4_t _b = vcvt_f16_f32(vdupq_n_f32(b_data[i])); |
| 374 | #else |
| 375 | float16x4_t _b = vdup_n_f16(b); |
| 376 | #endif |
| 377 | |
| 378 | int j = 0; |
| 379 | for (; j + 3 < w; j += 4) |
| 380 | { |
| 381 | float16x4_t _p = vld1_f16(ptr); |
| 382 | _p = vfma_f16(_a, _p, _b); |
| 383 | vst1_f16(ptr, _p); |
| 384 | |
| 385 | ptr += 4; |
| 386 | } |
| 387 | for (; j < w; j++) |
| 388 | { |
| 389 | *ptr = b * *ptr + a; |
| 390 | |
| 391 | ptr++; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | if (dims == 3 || dims == 4) |
| 397 | { |
| 398 | int w = bottom_top_blob.w; |
| 399 | int h = bottom_top_blob.h; |
| 400 | int d = bottom_top_blob.d; |
| 401 | int c = bottom_top_blob.c; |
| 402 | int size = w * h * d; |
| 403 | |
| 404 | #pragma omp parallel for num_threads(opt.num_threads) |
| 405 | for (int q = 0; q < c; q++) |
| 406 | { |
| 407 | __fp16* ptr = bottom_top_blob.channel(q); |
| 408 | |
| 409 | __fp16 a = (__fp16)a_data[q]; |
| 410 | __fp16 b = (__fp16)b_data[q]; |
| 411 | |
| 412 | float16x4_t _a = vdup_n_f16(a); |
| 413 | #if defined(_MSC_VER) && !defined(__clang__) |
| 414 | float16x4_t _b = vcvt_f16_f32(vdupq_n_f32(b_data[q])); |
| 415 | #else |
| 416 | float16x4_t _b = vdup_n_f16(b); |
| 417 | #endif |
| 418 | |
| 419 | int j = 0; |
| 420 | for (; j + 3 < size; j += 4) |
| 421 | { |
| 422 | float16x4_t _p = vld1_f16(ptr); |
| 423 | _p = vfma_f16(_a, _p, _b); |
| 424 | vst1_f16(ptr, _p); |
| 425 | |
| 426 | ptr += 4; |
| 427 | } |
| 428 | for (; j < size; j++) |
| 429 | { |
| 430 | *ptr = b * *ptr + a; |
| 431 | |
| 432 | ptr++; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 440 | |
| 441 | } // namespace ncnn |