| 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 "batchnorm_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 | BatchNorm_arm::BatchNorm_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 BatchNorm_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 | if (dims == 1) |
| 66 | { |
| 67 | int w = bottom_top_blob.w; |
| 68 | |
| 69 | #pragma omp parallel for num_threads(opt.num_threads) |
| 70 | for (int i = 0; i < w; i++) |
| 71 | { |
| 72 | float* ptr = (float*)bottom_top_blob + i * 4; |
| 73 | |
| 74 | float32x4_t _a = vld1q_f32((const float*)a_data + i * 4); |
| 75 | float32x4_t _b = vld1q_f32((const float*)b_data + i * 4); |
| 76 | |
| 77 | float32x4_t _p = vld1q_f32(ptr); |
| 78 | _p = vmlaq_f32(_a, _p, _b); |
| 79 | vst1q_f32(ptr, _p); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (dims == 2) |
| 84 | { |
| 85 | int w = bottom_top_blob.w; |
| 86 | int h = bottom_top_blob.h; |
| 87 | |
| 88 | #pragma omp parallel for num_threads(opt.num_threads) |
| 89 | for (int i = 0; i < h; i++) |
| 90 | { |
| 91 | float32x4_t _a = vld1q_f32((const float*)a_data + i * 4); |
| 92 | float32x4_t _b = vld1q_f32((const float*)b_data + i * 4); |
| 93 | |
| 94 | float* ptr = bottom_top_blob.row(i); |
| 95 | |
| 96 | for (int j = 0; j < w; j++) |
| 97 | { |
| 98 | float32x4_t _p = vld1q_f32(ptr); |
| 99 | _p = vmlaq_f32(_a, _p, _b); |
| 100 | vst1q_f32(ptr, _p); |
| 101 | |
| 102 | ptr += 4; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (dims == 3 || dims == 4) |
| 108 | { |
| 109 | int w = bottom_top_blob.w; |
| 110 | int h = bottom_top_blob.h; |
| 111 | int d = bottom_top_blob.d; |
| 112 | int c = bottom_top_blob.c; |
| 113 | int size = w * h * d; |
| 114 | |
| 115 | #pragma omp parallel for num_threads(opt.num_threads) |
| 116 | for (int q = 0; q < c; q++) |
| 117 | { |
| 118 | float32x4_t _a = vld1q_f32((const float*)a_data + q * 4); |
| 119 | float32x4_t _b = vld1q_f32((const float*)b_data + q * 4); |
| 120 | |
| 121 | float* ptr = bottom_top_blob.channel(q); |
| 122 | |
| 123 | for (int i = 0; i < size; i++) |
| 124 | { |
| 125 | float32x4_t _p = vld1q_f32(ptr); |
| 126 | _p = vmlaq_f32(_a, _p, _b); |
| 127 | vst1q_f32(ptr, _p); |
| 128 | |
| 129 | ptr += 4; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | #endif // __ARM_NEON |
| 137 | |
| 138 | if (dims == 1) |
| 139 | { |
| 140 | int w = bottom_top_blob.w; |
| 141 | |
| 142 | float* ptr = bottom_top_blob; |
| 143 | |
| 144 | #pragma omp parallel for num_threads(opt.num_threads) |
| 145 | for (int i = 0; i < w; i++) |
| 146 | { |
| 147 | ptr[i] = b_data[i] * ptr[i] + a_data[i]; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (dims == 2) |
| 152 | { |
| 153 | int w = bottom_top_blob.w; |
| 154 | int h = bottom_top_blob.h; |
| 155 | |
| 156 | #pragma omp parallel for num_threads(opt.num_threads) |
| 157 | for (int i = 0; i < h; i++) |
| 158 | { |
| 159 | float* ptr = bottom_top_blob.row(i); |
| 160 | |
| 161 | float a = a_data[i]; |
| 162 | float b = b_data[i]; |
| 163 | |
| 164 | int j = 0; |
| 165 | #if __ARM_NEON |
| 166 | float32x4_t _a = vdupq_n_f32(a); |
| 167 | float32x4_t _b = vdupq_n_f32(b); |
| 168 | |
| 169 | for (; j + 3 < w; j += 4) |
| 170 | { |
| 171 | float32x4_t _p = vld1q_f32(ptr); |
| 172 | _p = vmlaq_f32(_a, _p, _b); |
| 173 | vst1q_f32(ptr, _p); |
| 174 | |
| 175 | ptr += 4; |
| 176 | } |
| 177 | #endif // __ARM_NEON |
| 178 | for (; j < w; j++) |
| 179 | { |
| 180 | *ptr = b * *ptr + a; |
| 181 | |
| 182 | ptr++; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (dims == 3 || dims == 4) |
| 188 | { |
| 189 | int w = bottom_top_blob.w; |
| 190 | int h = bottom_top_blob.h; |
| 191 | int d = bottom_top_blob.d; |
| 192 | int c = bottom_top_blob.c; |
| 193 | int size = w * h * d; |
| 194 | |
| 195 | #pragma omp parallel for num_threads(opt.num_threads) |
| 196 | for (int q = 0; q < c; q++) |
| 197 | { |
| 198 | float* ptr = bottom_top_blob.channel(q); |
| 199 | |
| 200 | float a = a_data[q]; |
| 201 | float b = b_data[q]; |
| 202 | |
| 203 | int j = 0; |
| 204 | #if __ARM_NEON |
| 205 | float32x4_t _a = vdupq_n_f32(a); |
| 206 | float32x4_t _b = vdupq_n_f32(b); |
| 207 | |
| 208 | for (; j + 15 < size; j += 16) |
| 209 | { |
| 210 | float32x4_t _p0 = vld1q_f32(ptr); |
| 211 | float32x4_t _p1 = vld1q_f32(ptr + 4); |
| 212 | float32x4_t _p2 = vld1q_f32(ptr + 8); |
| 213 | float32x4_t _p3 = vld1q_f32(ptr + 12); |
| 214 | _p0 = vmlaq_f32(_a, _p0, _b); |
| 215 | _p1 = vmlaq_f32(_a, _p1, _b); |
| 216 | _p2 = vmlaq_f32(_a, _p2, _b); |
| 217 | _p3 = vmlaq_f32(_a, _p3, _b); |
| 218 | vst1q_f32(ptr, _p0); |
| 219 | vst1q_f32(ptr + 4, _p1); |
| 220 | vst1q_f32(ptr + 8, _p2); |
| 221 | vst1q_f32(ptr + 12, _p3); |
| 222 | ptr += 16; |
| 223 | } |
| 224 | for (; j + 7 < size; j += 8) |
| 225 | { |
| 226 | float32x4_t _p0 = vld1q_f32(ptr); |
| 227 | float32x4_t _p1 = vld1q_f32(ptr + 4); |
| 228 | _p0 = vmlaq_f32(_a, _p0, _b); |
| 229 | _p1 = vmlaq_f32(_a, _p1, _b); |
| 230 | vst1q_f32(ptr, _p0); |
| 231 | vst1q_f32(ptr + 4, _p1); |
| 232 | ptr += 8; |
| 233 | } |
| 234 | for (; j + 3 < size; j += 4) |
| 235 | { |
| 236 | float32x4_t _p = vld1q_f32(ptr); |
| 237 | _p = vmlaq_f32(_a, _p, _b); |
| 238 | vst1q_f32(ptr, _p); |
| 239 | ptr += 4; |
| 240 | } |
| 241 | #endif // __ARM_NEON |
| 242 | for (; j < size; j++) |
| 243 | { |
| 244 | *ptr = b * *ptr + a; |
| 245 | ptr++; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | #if NCNN_BF16 |
| 254 | int BatchNorm_arm::forward_inplace_bf16s(Mat& bottom_top_blob, const Option& opt) const |
| 255 | { |
| 256 | int dims = bottom_top_blob.dims; |
| 257 | int elempack = bottom_top_blob.elempack; |
| 258 | |
| 259 | #if __ARM_NEON |
| 260 | if (elempack == 4) |
| 261 | { |
| 262 | if (dims == 1) |
| 263 | { |
| 264 | int w = bottom_top_blob.w; |
| 265 | |
| 266 | #pragma omp parallel for num_threads(opt.num_threads) |
| 267 | for (int i = 0; i < w; i++) |
| 268 | { |
| 269 | unsigned short* ptr = (unsigned short*)bottom_top_blob + i * 4; |
| 270 | |
| 271 | float32x4_t _a = vld1q_f32((const float*)a_data + i * 4); |
| 272 | float32x4_t _b = vld1q_f32((const float*)b_data + i * 4); |
| 273 | |
| 274 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 275 | _p = vmlaq_f32(_a, _p, _b); |
| 276 | vst1_u16(ptr, float2bfloat(_p)); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | if (dims == 2) |
| 281 | { |
| 282 | int w = bottom_top_blob.w; |
| 283 | int h = bottom_top_blob.h; |
| 284 | |
| 285 | #pragma omp parallel for num_threads(opt.num_threads) |
| 286 | for (int i = 0; i < h; i++) |
| 287 | { |
| 288 | float32x4_t _a = vld1q_f32((const float*)a_data + i * 4); |
| 289 | float32x4_t _b = vld1q_f32((const float*)b_data + i * 4); |
| 290 | |
| 291 | unsigned short* ptr = bottom_top_blob.row<unsigned short>(i); |
| 292 | |
| 293 | for (int j = 0; j < w; j++) |
| 294 | { |
| 295 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 296 | _p = vmlaq_f32(_a, _p, _b); |
| 297 | vst1_u16(ptr, float2bfloat(_p)); |
| 298 | |
| 299 | ptr += 4; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if (dims == 3 || dims == 4) |
| 305 | { |
| 306 | int w = bottom_top_blob.w; |
| 307 | int h = bottom_top_blob.h; |
| 308 | int d = bottom_top_blob.d; |
| 309 | int c = bottom_top_blob.c; |
| 310 | int size = w * h * d; |
| 311 | |
| 312 | #pragma omp parallel for num_threads(opt.num_threads) |
| 313 | for (int q = 0; q < c; q++) |
| 314 | { |
| 315 | float32x4_t _a = vld1q_f32((const float*)a_data + q * 4); |
| 316 | float32x4_t _b = vld1q_f32((const float*)b_data + q * 4); |
| 317 | |
| 318 | unsigned short* ptr = bottom_top_blob.channel(q); |
| 319 | |
| 320 | for (int i = 0; i < size; i++) |
| 321 | { |
| 322 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 323 | _p = vmlaq_f32(_a, _p, _b); |
| 324 | vst1_u16(ptr, float2bfloat(_p)); |
| 325 | |
| 326 | ptr += 4; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | #endif // __ARM_NEON |
| 334 | |
| 335 | if (dims == 1) |
| 336 | { |
| 337 | int w = bottom_top_blob.w; |
| 338 | |
| 339 | unsigned short* ptr = bottom_top_blob; |
| 340 | |
| 341 | #pragma omp parallel for num_threads(opt.num_threads) |
| 342 | for (int i = 0; i < w; i++) |
| 343 | { |
| 344 | ptr[i] = float32_to_bfloat16(b_data[i] * bfloat16_to_float32(ptr[i]) + a_data[i]); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | if (dims == 2) |
| 349 | { |
| 350 | int w = bottom_top_blob.w; |
| 351 | int h = bottom_top_blob.h; |
| 352 | |
| 353 | #pragma omp parallel for num_threads(opt.num_threads) |
| 354 | for (int i = 0; i < h; i++) |
| 355 | { |
| 356 | unsigned short* ptr = bottom_top_blob.row<unsigned short>(i); |
| 357 | |
| 358 | float a = a_data[i]; |
| 359 | float b = b_data[i]; |
| 360 | |
| 361 | int j = 0; |
| 362 | #if __ARM_NEON |
| 363 | float32x4_t _a = vdupq_n_f32(a); |
| 364 | float32x4_t _b = vdupq_n_f32(b); |
| 365 | |
| 366 | for (; j + 3 < w; j += 4) |
| 367 | { |
| 368 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 369 | _p = vmlaq_f32(_a, _p, _b); |
| 370 | vst1_u16(ptr, float2bfloat(_p)); |
| 371 | |
| 372 | ptr += 4; |
| 373 | } |
| 374 | #endif // __ARM_NEON |
| 375 | for (; j < w; j++) |
| 376 | { |
| 377 | *ptr = float32_to_bfloat16(b * bfloat16_to_float32(*ptr) + a); |
| 378 | |
| 379 | ptr++; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | if (dims == 3 || dims == 4) |
| 385 | { |
| 386 | int w = bottom_top_blob.w; |
| 387 | int h = bottom_top_blob.h; |
| 388 | int d = bottom_top_blob.d; |
| 389 | int c = bottom_top_blob.c; |
| 390 | int size = w * h * d; |
| 391 | |
| 392 | #pragma omp parallel for num_threads(opt.num_threads) |
| 393 | for (int q = 0; q < c; q++) |
| 394 | { |
| 395 | unsigned short* ptr = bottom_top_blob.channel(q); |
| 396 | |
| 397 | float a = a_data[q]; |
| 398 | float b = b_data[q]; |
| 399 | |
| 400 | int j = 0; |
| 401 | #if __ARM_NEON |
| 402 | float32x4_t _a = vdupq_n_f32(a); |
| 403 | float32x4_t _b = vdupq_n_f32(b); |
| 404 | |
| 405 | for (; j + 3 < size; j += 4) |
| 406 | { |
| 407 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 408 | _p = vmlaq_f32(_a, _p, _b); |
| 409 | vst1_u16(ptr, float2bfloat(_p)); |
| 410 | |
| 411 | ptr += 4; |
| 412 | } |
| 413 | #endif // __ARM_NEON |
| 414 | for (; j < size; j++) |
| 415 | { |
| 416 | *ptr = float32_to_bfloat16(b * bfloat16_to_float32(*ptr) + a); |
| 417 | |
| 418 | ptr++; |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | #endif // NCNN_BF16 |
| 426 | |
| 427 | } // namespace ncnn |