Elffuss BraidHub

kikocis/ncnn public

master · 39.1 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 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 "binaryop_arm.h"
16
17#if __ARM_NEON
18#include <arm_neon.h>
19#include "neon_mathfun.h"
20#endif // __ARM_NEON
21
22#include "arm_usability.h"
23#include "cpu.h"
24
25namespace ncnn {
26
27BinaryOp_arm::BinaryOp_arm()
28{
29#if __ARM_NEON
30 support_packing = true;
31#if NCNN_ARM82
32 support_fp16_storage = cpu_support_arm_asimdhp();
33#endif
34#endif // __ARM_NEON
35
36#if NCNN_BF16
37 support_bf16_storage = true;
38#endif
39}
40
41template<typename Op>
42static void binary_op_vector_no_broadcast(const float* ptr, const float* ptr1, float* outptr, int size)
43{
44 const Op op;
45
46 int i = 0;
47#if __ARM_NEON
48 for (; i + 3 < size; i += 4)
49 {
50 float32x4_t _p = vld1q_f32(ptr);
51 float32x4_t _b = vld1q_f32(ptr1);
52 float32x4_t _outp = op(_p, _b);
53 vst1q_f32(outptr, _outp);
54 ptr += 4;
55 ptr1 += 4;
56 outptr += 4;
57 }
58#endif // __ARM_NEON
59 for (; i < size; i++)
60 {
61 *outptr = op(*ptr, *ptr1);
62 ptr += 1;
63 ptr1 += 1;
64 outptr += 1;
65 }
66}
67
68template<typename Op>
69static void binary_op_vector_broadcast_b(const float* ptr, const float* ptr1, float* outptr, int size, int elempack)
70{
71 const Op op;
72
73 const float b = *ptr1;
74
75 int i = 0;
76#if __ARM_NEON
77 float32x4_t _b_128 = (elempack == 4) ? vld1q_f32(ptr1) : vdupq_n_f32(b);
78 for (; i + 3 < size; i += 4)
79 {
80 float32x4_t _p = vld1q_f32(ptr);
81 float32x4_t _outp = op(_p, _b_128);
82 vst1q_f32(outptr, _outp);
83 ptr += 4;
84 outptr += 4;
85 }
86#endif // __ARM_NEON
87 for (; i < size; i++)
88 {
89 *outptr = op(*ptr, b);
90 ptr += 1;
91 outptr += 1;
92 }
93}
94
95template<typename Op>
96static void binary_op_vector_broadcast_a(const float* ptr, const float* ptr1, float* outptr, int size, int elempack)
97{
98 const Op op;
99
100 const float a = *ptr;
101
102 int i = 0;
103#if __ARM_NEON
104 float32x4_t _a_128 = (elempack == 4) ? vld1q_f32(ptr) : vdupq_n_f32(a);
105 for (; i + 3 < size; i += 4)
106 {
107 float32x4_t _b = vld1q_f32(ptr1);
108 float32x4_t _outp = op(_a_128, _b);
109 vst1q_f32(outptr, _outp);
110 ptr1 += 4;
111 outptr += 4;
112 }
113#endif // __ARM_NEON
114 for (; i < size; i++)
115 {
116 *outptr = op(a, *ptr1);
117 ptr1 += 1;
118 outptr += 1;
119 }
120}
121
122template<typename Op>
123static void binary_op_vector_broadcast_pb(const float* ptr, const float* ptr1, float* outptr, int w, int elempack)
124{
125 const Op op;
126
127#if __ARM_NEON
128 if (elempack == 4)
129 {
130 int i = 0;
131 for (; i < w; i++)
132 {
133 float32x4_t _p = vld1q_f32(ptr);
134 float32x4_t _b = vdupq_n_f32(*ptr1);
135 float32x4_t _outp = op(_p, _b);
136 vst1q_f32(outptr, _outp);
137 ptr += 4;
138 ptr1 += 1;
139 outptr += 4;
140 }
141 }
142#endif // __ARM_NEON
143}
144
145template<typename Op>
146static void binary_op_vector_broadcast_pb_b(const float* ptr, const float* ptr1, float* outptr, int w, int elempack)
147{
148 const Op op;
149
150 const int size = w * elempack;
151
152 int i = 0;
153#if __ARM_NEON
154 float32x4_t _b = vdupq_n_f32(*ptr1);
155 for (; i + 3 < size; i += 4)
156 {
157 float32x4_t _p = vld1q_f32(ptr);
158 float32x4_t _outp = op(_p, _b);
159 vst1q_f32(outptr, _outp);
160 ptr += 4;
161 outptr += 4;
162 }
163#endif // __ARM_NEON
164}
165
166template<typename Op>
167static void binary_op_vector_broadcast_pb_a(const float* ptr, const float* ptr1, float* outptr, int w, int elempack)
168{
169 const Op op;
170
171#if __ARM_NEON
172 if (elempack == 4)
173 {
174 int i = 0;
175 float32x4_t _p = vld1q_f32(ptr);
176 for (; i < w; i++)
177 {
178 float32x4_t _b = vdupq_n_f32(*ptr1);
179 float32x4_t _outp = op(_p, _b);
180 vst1q_f32(outptr, _outp);
181 ptr1 += 1;
182 outptr += 4;
183 }
184 }
185#endif // __ARM_NEON
186}
187
188template<typename Op>
189static void binary_op_vector(const float* ptr, const float* ptr1, float* outptr, int aw, int bw, int ap, int bp)
190{
191 const int w = std::max(aw, bw);
192 const int elempack = std::max(ap, bp);
193 const int size = w * elempack;
194
195 if (ap == bp)
196 {
197 if (aw == bw)
198 {
199 // no broadcast
200 return binary_op_vector_no_broadcast<Op>(ptr, ptr1, outptr, size);
201 }
202
203 if (bw == 1)
204 {
205 // broadcast single b
206 return binary_op_vector_broadcast_b<Op>(ptr, ptr1, outptr, size, elempack);
207 }
208
209 if (aw == 1)
210 {
211 // broadcast single a
212 return binary_op_vector_broadcast_a<Op>(ptr, ptr1, outptr, size, elempack);
213 }
214 }
215
216 if (bp == 1)
217 {
218 if (aw == bw)
219 {
220 // broadcast pack1 b
221 return binary_op_vector_broadcast_pb<Op>(ptr, ptr1, outptr, w, elempack);
222 }
223
224 if (bw == 1)
225 {
226 // broadcast pack1 single b
227 return binary_op_vector_broadcast_pb_b<Op>(ptr, ptr1, outptr, w, elempack);
228 }
229
230 if (aw == 1)
231 {
232 // broadcast single a and pack1 b
233 return binary_op_vector_broadcast_pb_a<Op>(ptr, ptr1, outptr, w, elempack);
234 }
235 }
236
237 // shall never reach here
238}
239
240namespace BinaryOp_arm_functor {
241
242#if __ARM_NEON
243#define MAKE_FUNCTION(NAME, IMPL, IMPL4) \
244 struct NAME \
245 { \
246 float operator()(const float& x, const float& y) const \
247 { \
248 return IMPL; \
249 } \
250 float32x4_t operator()(const float32x4_t& x, const float32x4_t& y) const \
251 { \
252 return IMPL4; \
253 } \
254 };
255#else
256#define MAKE_FUNCTION(NAME, IMPL, IMPL4) \
257 struct NAME \
258 { \
259 float operator()(const float& x, const float& y) const \
260 { \
261 return IMPL; \
262 } \
263 };
264#endif
265
266// clang-format off
267// *INDENT-OFF*
268MAKE_FUNCTION(binary_op_add, x + y, vaddq_f32(x, y))
269MAKE_FUNCTION(binary_op_sub, x - y, vsubq_f32(x, y))
270MAKE_FUNCTION(binary_op_mul, x * y, vmulq_f32(x, y))
271#if __aarch64__
272MAKE_FUNCTION(binary_op_div, x / y, vdivq_f32(x, y))
273#else
274MAKE_FUNCTION(binary_op_div, x / y, div_ps(x, y))
275#endif
276MAKE_FUNCTION(binary_op_max, std::max(x, y), vmaxq_f32(x, y))
277MAKE_FUNCTION(binary_op_min, std::min(x, y), vminq_f32(x, y))
278MAKE_FUNCTION(binary_op_pow, (float)powf(x, y), pow_ps(x, y))
279MAKE_FUNCTION(binary_op_rsub, y - x, vsubq_f32(y, x))
280#if __aarch64__
281MAKE_FUNCTION(binary_op_rdiv, y / x, vdivq_f32(y, x))
282#else
283MAKE_FUNCTION(binary_op_rdiv, y / x, div_ps(y, x))
284#endif
285MAKE_FUNCTION(binary_op_rpow, (float)powf(y, x), pow_ps(y, x))
286MAKE_FUNCTION(binary_op_atan2, (float)atan2f(x, y), atan2_ps(x, y))
287MAKE_FUNCTION(binary_op_ratan2, (float)atan2f(y, x), atan2_ps(y, x))
288// *INDENT-ON*
289// clang-format on
290
291#undef MAKE_FUNCTION
292
293} // namespace BinaryOp_arm_functor
294
295static void binary_op_vector(const float* ptr, const float* ptr1, float* outptr, int aw, int bw, int ap, int bp, int op_type)
296{
297 using namespace BinaryOp_arm_functor;
298
299 if (op_type == BinaryOp::Operation_ADD) return binary_op_vector<binary_op_add>(ptr, ptr1, outptr, aw, bw, ap, bp);
300 if (op_type == BinaryOp::Operation_SUB) return binary_op_vector<binary_op_sub>(ptr, ptr1, outptr, aw, bw, ap, bp);
301 if (op_type == BinaryOp::Operation_MUL) return binary_op_vector<binary_op_mul>(ptr, ptr1, outptr, aw, bw, ap, bp);
302 if (op_type == BinaryOp::Operation_DIV) return binary_op_vector<binary_op_div>(ptr, ptr1, outptr, aw, bw, ap, bp);
303 if (op_type == BinaryOp::Operation_MAX) return binary_op_vector<binary_op_max>(ptr, ptr1, outptr, aw, bw, ap, bp);
304 if (op_type == BinaryOp::Operation_MIN) return binary_op_vector<binary_op_min>(ptr, ptr1, outptr, aw, bw, ap, bp);
305 if (op_type == BinaryOp::Operation_POW) return binary_op_vector<binary_op_pow>(ptr, ptr1, outptr, aw, bw, ap, bp);
306 if (op_type == BinaryOp::Operation_RSUB) return binary_op_vector<binary_op_rsub>(ptr, ptr1, outptr, aw, bw, ap, bp);
307 if (op_type == BinaryOp::Operation_RDIV) return binary_op_vector<binary_op_rdiv>(ptr, ptr1, outptr, aw, bw, ap, bp);
308 if (op_type == BinaryOp::Operation_RPOW) return binary_op_vector<binary_op_rpow>(ptr, ptr1, outptr, aw, bw, ap, bp);
309 if (op_type == BinaryOp::Operation_ATAN2) return binary_op_vector<binary_op_atan2>(ptr, ptr1, outptr, aw, bw, ap, bp);
310 if (op_type == BinaryOp::Operation_RATAN2) return binary_op_vector<binary_op_ratan2>(ptr, ptr1, outptr, aw, bw, ap, bp);
311
312 // should never reach here
313}
314
315static void binary_op_scalar(const Mat& a, float b, Mat& c, int op_type, const Option& opt)
316{
317 const int channels = a.c;
318 const int size = a.w * a.h * a.d * a.elempack;
319
320 #pragma omp parallel for num_threads(opt.num_threads)
321 for (int q = 0; q < channels; q++)
322 {
323 const float* ptr = a.channel(q);
324 float* outptr = c.channel(q);
325
326 binary_op_vector(ptr, &b, outptr, size, 1, 1, 1, op_type);
327 }
328}
329
330static void binary_op_no_broadcast(const Mat& a, const Mat& b, Mat& c, int op_type, const Option& opt)
331{
332 const int channels = a.c;
333 const int size = a.w * a.h * a.d * a.elempack;
334
335 #pragma omp parallel for num_threads(opt.num_threads)
336 for (int q = 0; q < channels; q++)
337 {
338 const float* ptr = a.channel(q);
339 const float* ptr1 = b.channel(q);
340 float* outptr = c.channel(q);
341
342 binary_op_vector(ptr, ptr1, outptr, size, size, 1, 1, op_type);
343 }
344}
345
346static void binary_op_broadcast(const Mat& a, const Mat& b, Mat& c, int op_type, const Option& opt)
347{
348 if (b.w * b.h * b.d * b.c * b.elempack == 1)
349 {
350 return binary_op_scalar(a, b[0], c, op_type, opt);
351 }
352
353 if (a.dims == b.dims && a.w == b.w && a.h == b.h && a.d == b.d && a.c == b.c && a.elempack == b.elempack)
354 {
355 return binary_op_no_broadcast(a, b, c, op_type, opt);
356 }
357
358 const int dims = c.dims;
359
360 if (dims == 2)
361 {
362 const int h = c.h;
363
364 #pragma omp parallel for num_threads(opt.num_threads)
365 for (int y = 0; y < h; y++)
366 {
367 const int y0 = std::min(y, a.h - 1);
368 const int y1 = std::min(y, b.h - 1);
369
370 const float* ptr = a.row(y0);
371 const float* ptr1 = b.row(y1);
372 float* outptr = c.row(y);
373
374 binary_op_vector(ptr, ptr1, outptr, a.w, b.w, a.elempack, b.elempack, op_type);
375 }
376 }
377
378 if (dims == 3 || dims == 4)
379 {
380 const int channels = c.c;
381
382 #pragma omp parallel for num_threads(opt.num_threads)
383 for (int q = 0; q < channels; q++)
384 {
385 const int q0 = std::min(q, a.c - 1);
386 const int q1 = std::min(q, b.c - 1);
387
388 if (b.d * b.h * b.w == 1)
389 {
390 const float* ptr = a.channel(q0);
391 const float* ptr1 = b.channel(q1);
392 float* outptr = c.channel(q);
393
394 binary_op_vector(ptr, ptr1, outptr, a.w * a.h * a.d, 1, a.elempack, b.elempack, op_type);
395 continue;
396 }
397
398 if (b.h * b.w == 1)
399 {
400 for (int z = 0; z < c.d; z++)
401 {
402 const int z0 = std::min(z, a.d - 1);
403 const int z1 = std::min(z, b.d - 1);
404
405 const float* ptr = a.channel(q0).depth(z0);
406 const float* ptr1 = b.channel(q1).depth(z1);
407 float* outptr = c.channel(q).depth(z);
408
409 binary_op_vector(ptr, ptr1, outptr, a.w * a.h, 1, a.elempack, b.elempack, op_type);
410 }
411 continue;
412 }
413
414 for (int z = 0; z < c.d; z++)
415 {
416 const int z0 = std::min(z, a.d - 1);
417 const int z1 = std::min(z, b.d - 1);
418
419 for (int y = 0; y < c.h; y++)
420 {
421 const int y0 = std::min(y, a.h - 1);
422 const int y1 = std::min(y, b.h - 1);
423
424 const float* ptr = a.channel(q0).depth(z0).row(y0);
425 const float* ptr1 = b.channel(q1).depth(z1).row(y1);
426 float* outptr = c.channel(q).depth(z).row(y);
427
428 binary_op_vector(ptr, ptr1, outptr, a.w, b.w, a.elempack, b.elempack, op_type);
429 }
430 }
431 }
432 }
433}
434
435static void binary_op_scalar_inplace(Mat& a, float b, int op_type, const Option& opt)
436{
437 const int channels = a.c;
438 const int size = a.w * a.h * a.d * a.elempack;
439
440 #pragma omp parallel for num_threads(opt.num_threads)
441 for (int q = 0; q < channels; q++)
442 {
443 float* ptr = a.channel(q);
444
445 binary_op_vector(ptr, &b, ptr, size, 1, 1, 1, op_type);
446 }
447}
448
449static int get_reverse_op_type(int op_type)
450{
451 if (op_type == BinaryOp::Operation_SUB) return BinaryOp::Operation_RSUB;
452 if (op_type == BinaryOp::Operation_DIV) return BinaryOp::Operation_RDIV;
453 if (op_type == BinaryOp::Operation_POW) return BinaryOp::Operation_RPOW;
454 if (op_type == BinaryOp::Operation_ATAN2) return BinaryOp::Operation_RATAN2;
455 if (op_type == BinaryOp::Operation_RSUB) return BinaryOp::Operation_SUB;
456 if (op_type == BinaryOp::Operation_RDIV) return BinaryOp::Operation_DIV;
457 if (op_type == BinaryOp::Operation_RPOW) return BinaryOp::Operation_POW;
458 if (op_type == BinaryOp::Operation_RATAN2) return BinaryOp::Operation_ATAN2;
459 return op_type;
460}
461
462int BinaryOp_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
463{
464 int elembits = std::max(bottom_blobs[0].elembits(), bottom_blobs[1].elembits());
465
466#if NCNN_ARM82
467 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
468 return forward_fp16s(bottom_blobs, top_blobs, opt);
469#endif
470
471#if NCNN_BF16
472 if (opt.use_bf16_storage && elembits == 16)
473 return forward_bf16s(bottom_blobs, top_blobs, opt);
474#endif
475
476 const Mat& A = bottom_blobs[0];
477 const Mat& B = bottom_blobs[1];
478 const int outdims = std::max(A.dims, B.dims);
479
480 Mat A2 = A;
481 Mat B2 = B;
482 if (A.dims < outdims)
483 {
484 // expand inner axes
485 if (outdims == 2)
486 {
487 if (A.w * A.elempack == B.h * B.elempack)
488 A2 = A.reshape(1, A.w, opt.workspace_allocator);
489 else // if (A.w == B.w)
490 {
491 A2.dims = 2;
492 A2.w = A.w * A.elempack;
493 A2.elempack = 1;
494 A2.elemsize = A.elemsize / A.elempack;
495 A2.cstep = A2.w;
496 }
497 }
498 if (outdims == 3 && A.dims == 1)
499 {
500 if (A.w * A.elempack == B.c * B.elempack)
501 A2 = A.reshape(1, 1, A.w, opt.workspace_allocator);
502 else // if (A.w == B.w)
503 {
504 A2.dims = 3;
505 A2.w = A.w * A.elempack;
506 A2.elempack = 1;
507 A2.elemsize = A.elemsize / A.elempack;
508 A2.cstep = A2.w;
509 }
510 }
511 if (outdims == 3 && A.dims == 2)
512 A2 = A.reshape(1, A.w, A.h, opt.workspace_allocator);
513 if (outdims == 4 && A.dims == 1)
514 {
515 if (A.w * A.elempack == B.c * B.elempack)
516 A2 = A.reshape(1, 1, 1, A.w, opt.workspace_allocator);
517 else // if (A.w == B.w)
518 {
519 A2.dims = 4;
520 A2.w = A.w * A.elempack;
521 A2.elempack = 1;
522 A2.elemsize = A.elemsize / A.elempack;
523 A2.cstep = A2.w;
524 }
525 }
526 if (outdims == 4 && A.dims == 2)
527 A2 = A.reshape(1, 1, A.w, A.h, opt.workspace_allocator);
528 if (outdims == 4 && A.dims == 3)
529 A2 = A.reshape(1, A.w, A.h, A.c, opt.workspace_allocator);
530 }
531 if (B.dims < outdims)
532 {
533 // expand inner axes
534 if (outdims == 2)
535 {
536 if (B.w * B.elempack == A.h * A.elempack)
537 B2 = B.reshape(1, B.w, opt.workspace_allocator);
538 else // if (B.w == A.w)
539 {
540 B2.dims = 2;
541 B2.w = B.w * B.elempack;
542 B2.elempack = 1;
543 B2.elemsize = B.elemsize / B.elempack;
544 B2.cstep = B2.w;
545 }
546 }
547 if (outdims == 3 && B.dims == 1)
548 {
549 if (B.w * B.elempack == A.c * A.elempack)
550 B2 = B.reshape(1, 1, B.w, opt.workspace_allocator);
551 else // if (B.w == A.w)
552 {
553 B2.dims = 3;
554 B2.w = B.w * B.elempack;
555 B2.elempack = 1;
556 B2.elemsize = B.elemsize / B.elempack;
557 B2.cstep = B2.w;
558 }
559 }
560 if (outdims == 3 && B.dims == 2)
561 B2 = B.reshape(1, B.w, B.h, opt.workspace_allocator);
562 if (outdims == 4 && B.dims == 1)
563 {
564 if (B.w * B.elempack == A.c * A.elempack)
565 B2 = B.reshape(1, 1, 1, B.w, opt.workspace_allocator);
566 else // if (B.w == A.w)
567 {
568 B2.dims = 4;
569 B2.w = B.w * B.elempack;
570 B2.elempack = 1;
571 B2.elemsize = B.elemsize / B.elempack;
572 B2.cstep = B2.w;
573 }
574 }
575 if (outdims == 4 && B.dims == 2)
576 B2 = B.reshape(1, 1, B.w, B.h, opt.workspace_allocator);
577 if (outdims == 4 && B.dims == 3)
578 B2 = B.reshape(1, B.w, B.h, B.c, opt.workspace_allocator);
579 }
580
581 const int outw = std::max(A2.w, B2.w);
582 const int outh = std::max(A2.h, B2.h);
583 const int outd = std::max(A2.d, B2.d);
584 const int outc = std::max(A2.c, B2.c);
585 const size_t out_elemsize = std::max(A2.elemsize, B2.elemsize);
586 const int out_elempack = std::max(A2.elempack, B2.elempack);
587
588 Mat& top_blob = top_blobs[0];
589 if (outdims == 1)
590 {
591 top_blob.create(outw, out_elemsize, out_elempack, opt.blob_allocator);
592 }
593 if (outdims == 2)
594 {
595 top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_allocator);
596 }
597 if (outdims == 3)
598 {
599 top_blob.create(outw, outh, outc, out_elemsize, out_elempack, opt.blob_allocator);
600 }
601 if (outdims == 4)
602 {
603 top_blob.create(outw, outh, outd, outc, out_elemsize, out_elempack, opt.blob_allocator);
604 }
605 if (top_blob.empty())
606 return -100;
607
608 const bool a_pack_is_lower = A2.elempack < B2.elempack;
609 const bool a_pack_is_equal = A2.elempack == B2.elempack;
610 const bool a_size_is_lower = A2.w * A2.h * A2.d * A2.c * A2.elempack < B2.w * B2.h * B2.d * B2.c * B2.elempack;
611 if (a_pack_is_lower || (a_pack_is_equal && a_size_is_lower))
612 {
613 binary_op_broadcast(B2, A2, top_blob, get_reverse_op_type(op_type), opt);
614 }
615 else
616 {
617 binary_op_broadcast(A2, B2, top_blob, op_type, opt);
618 }
619
620 return 0;
621}
622
623int BinaryOp_arm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
624{
625 int elembits = bottom_top_blob.elembits();
626
627#if NCNN_ARM82
628 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
629 return forward_inplace_fp16s(bottom_top_blob, opt);
630#endif
631
632#if NCNN_BF16
633 if (opt.use_bf16_storage && elembits == 16)
634 return forward_inplace_bf16s(bottom_top_blob, opt);
635#endif
636
637 binary_op_scalar_inplace(bottom_top_blob, b, op_type, opt);
638
639 return 0;
640}
641
642#if NCNN_BF16
643template<typename Op>
644static void binary_op_vector_no_broadcast_bf16s(const unsigned short* ptr, const unsigned short* ptr1, unsigned short* outptr, int size)
645{
646 const Op op;
647
648 int i = 0;
649#if __ARM_NEON
650 for (; i + 3 < size; i += 4)
651 {
652 float32x4_t _p = bfloat2float(vld1_u16(ptr));
653 float32x4_t _b = bfloat2float(vld1_u16(ptr1));
654 float32x4_t _outp = op(_p, _b);
655 vst1_u16(outptr, float2bfloat(_outp));
656 ptr += 4;
657 ptr1 += 4;
658 outptr += 4;
659 }
660#endif // __ARM_NEON
661 for (; i < size; i++)
662 {
663 *outptr = float32_to_bfloat16(op(bfloat16_to_float32(*ptr), bfloat16_to_float32(*ptr1)));
664 ptr += 1;
665 ptr1 += 1;
666 outptr += 1;
667 }
668}
669
670template<typename Op>
671static void binary_op_vector_broadcast_b_bf16s(const unsigned short* ptr, const unsigned short* ptr1, unsigned short* outptr, int size, int elempack)
672{
673 const Op op;
674
675 const float b = bfloat16_to_float32(*ptr1);
676
677 int i = 0;
678#if __ARM_NEON
679 float32x4_t _b_128 = (elempack == 4) ? bfloat2float(vld1_u16(ptr1)) : vdupq_n_f32(b);
680 for (; i + 3 < size; i += 4)
681 {
682 float32x4_t _p = bfloat2float(vld1_u16(ptr));
683 float32x4_t _outp = op(_p, _b_128);
684 vst1_u16(outptr, float2bfloat(_outp));
685 ptr += 4;
686 outptr += 4;
687 }
688#endif // __ARM_NEON
689 for (; i < size; i++)
690 {
691 *outptr = float32_to_bfloat16(op(bfloat16_to_float32(*ptr), b));
692 ptr += 1;
693 outptr += 1;
694 }
695}
696
697template<typename Op>
698static void binary_op_vector_broadcast_a_bf16s(const unsigned short* ptr, const unsigned short* ptr1, unsigned short* outptr, int size, int elempack)
699{
700 const Op op;
701
702 const float a = bfloat16_to_float32(*ptr);
703
704 int i = 0;
705#if __ARM_NEON
706 float32x4_t _a_128 = (elempack == 4) ? bfloat2float(vld1_u16(ptr)) : vdupq_n_f32(a);
707 for (; i + 3 < size; i += 4)
708 {
709 float32x4_t _b = bfloat2float(vld1_u16(ptr1));
710 float32x4_t _outp = op(_a_128, _b);
711 vst1_u16(outptr, float2bfloat(_outp));
712 ptr1 += 4;
713 outptr += 4;
714 }
715#endif // __ARM_NEON
716 for (; i < size; i++)
717 {
718 *outptr = float32_to_bfloat16(op(a, bfloat16_to_float32(*ptr1)));
719 ptr1 += 1;
720 outptr += 1;
721 }
722}
723
724template<typename Op>
725static void binary_op_vector_broadcast_pb_bf16s(const unsigned short* ptr, const unsigned short* ptr1, unsigned short* outptr, int w, int elempack)
726{
727 const Op op;
728
729#if __ARM_NEON
730 if (elempack == 4)
731 {
732 int i = 0;
733 for (; i < w; i++)
734 {
735 float32x4_t _p = bfloat2float(vld1_u16(ptr));
736 float32x4_t _b = bfloat2float(vdup_n_u16(*ptr1));
737 float32x4_t _outp = op(_p, _b);
738 vst1_u16(outptr, float2bfloat(_outp));
739 ptr += 4;
740 ptr1 += 1;
741 outptr += 4;
742 }
743 }
744#endif // __ARM_NEON
745}
746
747template<typename Op>
748static void binary_op_vector_broadcast_pb_b_bf16s(const unsigned short* ptr, const unsigned short* ptr1, unsigned short* outptr, int w, int elempack)
749{
750 const Op op;
751
752 const int size = w * elempack;
753
754 int i = 0;
755#if __ARM_NEON
756 float32x4_t _b = bfloat2float(vdup_n_u16(*ptr1));
757 for (; i + 3 < size; i += 4)
758 {
759 float32x4_t _p = bfloat2float(vld1_u16(ptr));
760 float32x4_t _outp = op(_p, _b);
761 vst1_u16(outptr, float2bfloat(_outp));
762 ptr += 4;
763 outptr += 4;
764 }
765#endif // __ARM_NEON
766}
767
768template<typename Op>
769static void binary_op_vector_broadcast_pb_a_bf16s(const unsigned short* ptr, const unsigned short* ptr1, unsigned short* outptr, int w, int elempack)
770{
771 const Op op;
772
773#if __ARM_NEON
774 if (elempack == 4)
775 {
776 int i = 0;
777 float32x4_t _p = bfloat2float(vld1_u16(ptr));
778 for (; i < w; i++)
779 {
780 float32x4_t _b = bfloat2float(vdup_n_u16(*ptr1));
781 float32x4_t _outp = op(_p, _b);
782 vst1_u16(outptr, float2bfloat(_outp));
783 ptr1 += 1;
784 outptr += 4;
785 }
786 }
787#endif // __ARM_NEON
788}
789
790template<typename Op>
791static void binary_op_vector_bf16s(const unsigned short* ptr, const unsigned short* ptr1, unsigned short* outptr, int aw, int bw, int ap, int bp)
792{
793 const int w = std::max(aw, bw);
794 const int elempack = std::max(ap, bp);
795 const int size = w * elempack;
796
797 if (ap == bp)
798 {
799 if (aw == bw)
800 {
801 // no broadcast
802 return binary_op_vector_no_broadcast_bf16s<Op>(ptr, ptr1, outptr, size);
803 }
804
805 if (bw == 1)
806 {
807 // broadcast single b
808 return binary_op_vector_broadcast_b_bf16s<Op>(ptr, ptr1, outptr, size, elempack);
809 }
810
811 if (aw == 1)
812 {
813 // broadcast single a
814 return binary_op_vector_broadcast_a_bf16s<Op>(ptr, ptr1, outptr, size, elempack);
815 }
816 }
817
818 if (bp == 1)
819 {
820 if (aw == bw)
821 {
822 // broadcast pack1 b
823 return binary_op_vector_broadcast_pb_bf16s<Op>(ptr, ptr1, outptr, w, elempack);
824 }
825
826 if (bw == 1)
827 {
828 // broadcast pack1 single b
829 return binary_op_vector_broadcast_pb_b_bf16s<Op>(ptr, ptr1, outptr, w, elempack);
830 }
831
832 if (aw == 1)
833 {
834 // broadcast single a and pack1 b
835 return binary_op_vector_broadcast_pb_a_bf16s<Op>(ptr, ptr1, outptr, w, elempack);
836 }
837 }
838
839 // shall never reach here
840}
841
842static void binary_op_vector_bf16s(const unsigned short* ptr, const unsigned short* ptr1, unsigned short* outptr, int aw, int bw, int ap, int bp, int op_type)
843{
844 using namespace BinaryOp_arm_functor;
845
846 if (op_type == BinaryOp::Operation_ADD) return binary_op_vector_bf16s<binary_op_add>(ptr, ptr1, outptr, aw, bw, ap, bp);
847 if (op_type == BinaryOp::Operation_SUB) return binary_op_vector_bf16s<binary_op_sub>(ptr, ptr1, outptr, aw, bw, ap, bp);
848 if (op_type == BinaryOp::Operation_MUL) return binary_op_vector_bf16s<binary_op_mul>(ptr, ptr1, outptr, aw, bw, ap, bp);
849 if (op_type == BinaryOp::Operation_DIV) return binary_op_vector_bf16s<binary_op_div>(ptr, ptr1, outptr, aw, bw, ap, bp);
850 if (op_type == BinaryOp::Operation_MAX) return binary_op_vector_bf16s<binary_op_max>(ptr, ptr1, outptr, aw, bw, ap, bp);
851 if (op_type == BinaryOp::Operation_MIN) return binary_op_vector_bf16s<binary_op_min>(ptr, ptr1, outptr, aw, bw, ap, bp);
852 if (op_type == BinaryOp::Operation_POW) return binary_op_vector_bf16s<binary_op_pow>(ptr, ptr1, outptr, aw, bw, ap, bp);
853 if (op_type == BinaryOp::Operation_RSUB) return binary_op_vector_bf16s<binary_op_rsub>(ptr, ptr1, outptr, aw, bw, ap, bp);
854 if (op_type == BinaryOp::Operation_RDIV) return binary_op_vector_bf16s<binary_op_rdiv>(ptr, ptr1, outptr, aw, bw, ap, bp);
855 if (op_type == BinaryOp::Operation_RPOW) return binary_op_vector_bf16s<binary_op_rpow>(ptr, ptr1, outptr, aw, bw, ap, bp);
856 if (op_type == BinaryOp::Operation_ATAN2) return binary_op_vector_bf16s<binary_op_atan2>(ptr, ptr1, outptr, aw, bw, ap, bp);
857 if (op_type == BinaryOp::Operation_RATAN2) return binary_op_vector_bf16s<binary_op_ratan2>(ptr, ptr1, outptr, aw, bw, ap, bp);
858
859 // should never reach here
860}
861
862template<typename Op>
863static void binary_op_vector_scalar_b_bf16s(const unsigned short* ptr, float b, unsigned short* outptr, int size)
864{
865 const Op op;
866
867 int i = 0;
868#if __ARM_NEON
869 float32x4_t _b_128 = vdupq_n_f32(b);
870 for (; i + 3 < size; i += 4)
871 {
872 float32x4_t _p = bfloat2float(vld1_u16(ptr));
873 float32x4_t _outp = op(_p, _b_128);
874 vst1_u16(outptr, float2bfloat(_outp));
875 ptr += 4;
876 outptr += 4;
877 }
878#endif // __ARM_NEON
879 for (; i < size; i++)
880 {
881 *outptr = float32_to_bfloat16(op(bfloat16_to_float32(*ptr), b));
882 ptr += 1;
883 outptr += 1;
884 }
885}
886
887static void binary_op_vector_scalar_b_bf16s(const unsigned short* ptr, float b, unsigned short* outptr, int size, int op_type)
888{
889 using namespace BinaryOp_arm_functor;
890
891 if (op_type == BinaryOp::Operation_ADD) return binary_op_vector_scalar_b_bf16s<binary_op_add>(ptr, b, outptr, size);
892 if (op_type == BinaryOp::Operation_SUB) return binary_op_vector_scalar_b_bf16s<binary_op_sub>(ptr, b, outptr, size);
893 if (op_type == BinaryOp::Operation_MUL) return binary_op_vector_scalar_b_bf16s<binary_op_mul>(ptr, b, outptr, size);
894 if (op_type == BinaryOp::Operation_DIV) return binary_op_vector_scalar_b_bf16s<binary_op_div>(ptr, b, outptr, size);
895 if (op_type == BinaryOp::Operation_MAX) return binary_op_vector_scalar_b_bf16s<binary_op_max>(ptr, b, outptr, size);
896 if (op_type == BinaryOp::Operation_MIN) return binary_op_vector_scalar_b_bf16s<binary_op_min>(ptr, b, outptr, size);
897 if (op_type == BinaryOp::Operation_POW) return binary_op_vector_scalar_b_bf16s<binary_op_pow>(ptr, b, outptr, size);
898 if (op_type == BinaryOp::Operation_RSUB) return binary_op_vector_scalar_b_bf16s<binary_op_rsub>(ptr, b, outptr, size);
899 if (op_type == BinaryOp::Operation_RDIV) return binary_op_vector_scalar_b_bf16s<binary_op_rdiv>(ptr, b, outptr, size);
900 if (op_type == BinaryOp::Operation_RPOW) return binary_op_vector_scalar_b_bf16s<binary_op_rpow>(ptr, b, outptr, size);
901 if (op_type == BinaryOp::Operation_ATAN2) return binary_op_vector_scalar_b_bf16s<binary_op_atan2>(ptr, b, outptr, size);
902 if (op_type == BinaryOp::Operation_RATAN2) return binary_op_vector_scalar_b_bf16s<binary_op_ratan2>(ptr, b, outptr, size);
903
904 // should never reach here
905}
906
907static void binary_op_scalar_bf16s(const Mat& a, float b, Mat& c, int op_type, const Option& opt)
908{
909 const int channels = a.c;
910 const int size = a.w * a.h * a.d * a.elempack;
911
912 #pragma omp parallel for num_threads(opt.num_threads)
913 for (int q = 0; q < channels; q++)
914 {
915 const unsigned short* ptr = a.channel(q);
916 unsigned short* outptr = c.channel(q);
917
918 binary_op_vector_scalar_b_bf16s(ptr, b, outptr, size, op_type);
919 }
920}
921
922static void binary_op_no_broadcast_bf16s(const Mat& a, const Mat& b, Mat& c, int op_type, const Option& opt)
923{
924 const int channels = a.c;
925 const int size = a.w * a.h * a.d * a.elempack;
926
927 #pragma omp parallel for num_threads(opt.num_threads)
928 for (int q = 0; q < channels; q++)
929 {
930 const unsigned short* ptr = a.channel(q);
931 const unsigned short* ptr1 = b.channel(q);
932 unsigned short* outptr = c.channel(q);
933
934 binary_op_vector_bf16s(ptr, ptr1, outptr, size, size, 1, 1, op_type);
935 }
936}
937
938static void binary_op_broadcast_bf16s(const Mat& a, const Mat& b, Mat& c, int op_type, const Option& opt)
939{
940 if (b.w * b.h * b.d * b.c * b.elempack == 1)
941 {
942 return binary_op_scalar_bf16s(a, bfloat16_to_float32(((const unsigned short*)b)[0]), c, op_type, opt);
943 }
944
945 if (a.dims == b.dims && a.w == b.w && a.h == b.h && a.d == b.d && a.c == b.c && a.elempack == b.elempack)
946 {
947 return binary_op_no_broadcast_bf16s(a, b, c, op_type, opt);
948 }
949
950 const int dims = c.dims;
951
952 if (dims == 2)
953 {
954 const int h = c.h;
955
956 #pragma omp parallel for num_threads(opt.num_threads)
957 for (int y = 0; y < h; y++)
958 {
959 const int y0 = std::min(y, a.h - 1);
960 const int y1 = std::min(y, b.h - 1);
961
962 const unsigned short* ptr = a.row<const unsigned short>(y0);
963 const unsigned short* ptr1 = b.row<const unsigned short>(y1);
964 unsigned short* outptr = c.row<unsigned short>(y);
965
966 binary_op_vector_bf16s(ptr, ptr1, outptr, a.w, b.w, a.elempack, b.elempack, op_type);
967 }
968 }
969
970 if (dims == 3 || dims == 4)
971 {
972 const int channels = c.c;
973
974 #pragma omp parallel for num_threads(opt.num_threads)
975 for (int q = 0; q < channels; q++)
976 {
977 const int q0 = std::min(q, a.c - 1);
978 const int q1 = std::min(q, b.c - 1);
979
980 if (b.d * b.h * b.w == 1)
981 {
982 const unsigned short* ptr = a.channel(q0);
983 const unsigned short* ptr1 = b.channel(q1);
984 unsigned short* outptr = c.channel(q);
985
986 binary_op_vector_bf16s(ptr, ptr1, outptr, a.w * a.h * a.d, 1, a.elempack, b.elempack, op_type);
987 continue;
988 }
989
990 if (b.h * b.w == 1)
991 {
992 for (int z = 0; z < c.d; z++)
993 {
994 const int z0 = std::min(z, a.d - 1);
995 const int z1 = std::min(z, b.d - 1);
996
997 const unsigned short* ptr = a.channel(q0).depth(z0);
998 const unsigned short* ptr1 = b.channel(q1).depth(z1);
999 unsigned short* outptr = c.channel(q).depth(z);
1000
1001 binary_op_vector_bf16s(ptr, ptr1, outptr, a.w * a.h, 1, a.elempack, b.elempack, op_type);
1002 }
1003 continue;
1004 }
1005
1006 for (int z = 0; z < c.d; z++)
1007 {
1008 const int z0 = std::min(z, a.d - 1);
1009 const int z1 = std::min(z, b.d - 1);
1010
1011 for (int y = 0; y < c.h; y++)
1012 {
1013 const int y0 = std::min(y, a.h - 1);
1014 const int y1 = std::min(y, b.h - 1);
1015
1016 const unsigned short* ptr = a.channel(q0).depth(z0).row<const unsigned short>(y0);
1017 const unsigned short* ptr1 = b.channel(q1).depth(z1).row<const unsigned short>(y1);
1018 unsigned short* outptr = c.channel(q).depth(z).row<unsigned short>(y);
1019
1020 binary_op_vector_bf16s(ptr, ptr1, outptr, a.w, b.w, a.elempack, b.elempack, op_type);
1021 }
1022 }
1023 }
1024 }
1025}
1026
1027static void binary_op_scalar_inplace_bf16s(Mat& a, float b, int op_type, const Option& opt)
1028{
1029 const int channels = a.c;
1030 const int size = a.w * a.h * a.d * a.elempack;
1031
1032 #pragma omp parallel for num_threads(opt.num_threads)
1033 for (int q = 0; q < channels; q++)
1034 {
1035 unsigned short* ptr = a.channel(q);
1036
1037 binary_op_vector_scalar_b_bf16s(ptr, b, ptr, size, op_type);
1038 }
1039}
1040
1041int BinaryOp_arm::forward_bf16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
1042{
1043 const Mat& A = bottom_blobs[0];
1044 const Mat& B = bottom_blobs[1];
1045 const int outdims = std::max(A.dims, B.dims);
1046
1047 Mat A2 = A;
1048 Mat B2 = B;
1049 if (A.dims < outdims)
1050 {
1051 // expand inner axes
1052 if (outdims == 2)
1053 {
1054 if (A.w * A.elempack == B.h * B.elempack)
1055 A2 = A.reshape(1, A.w, opt.workspace_allocator);
1056 else // if (A.w == B.w)
1057 {
1058 A2.dims = 2;
1059 A2.w = A.w * A.elempack;
1060 A2.elempack = 1;
1061 A2.elemsize = A.elemsize / A.elempack;
1062 A2.cstep = A2.w;
1063 }
1064 }
1065 if (outdims == 3 && A.dims == 1)
1066 {
1067 if (A.w * A.elempack == B.c * B.elempack)
1068 A2 = A.reshape(1, 1, A.w, opt.workspace_allocator);
1069 else // if (A.w == B.w)
1070 {
1071 A2.dims = 3;
1072 A2.w = A.w * A.elempack;
1073 A2.elempack = 1;
1074 A2.elemsize = A.elemsize / A.elempack;
1075 A2.cstep = A2.w;
1076 }
1077 }
1078 if (outdims == 3 && A.dims == 2)
1079 A2 = A.reshape(1, A.w, A.h, opt.workspace_allocator);
1080 if (outdims == 4 && A.dims == 1)
1081 {
1082 if (A.w * A.elempack == B.c * B.elempack)
1083 A2 = A.reshape(1, 1, 1, A.w, opt.workspace_allocator);
1084 else // if (A.w == B.w)
1085 {
1086 A2.dims = 4;
1087 A2.w = A.w * A.elempack;
1088 A2.elempack = 1;
1089 A2.elemsize = A.elemsize / A.elempack;
1090 A2.cstep = A2.w;
1091 }
1092 }
1093 if (outdims == 4 && A.dims == 2)
1094 A2 = A.reshape(1, 1, A.w, A.h, opt.workspace_allocator);
1095 if (outdims == 4 && A.dims == 3)
1096 A2 = A.reshape(1, A.w, A.h, A.c, opt.workspace_allocator);
1097 }
1098 if (B.dims < outdims)
1099 {
1100 // expand inner axes
1101 if (outdims == 2)
1102 {
1103 if (B.w * B.elempack == A.h * A.elempack)
1104 B2 = B.reshape(1, B.w, opt.workspace_allocator);
1105 else // if (B.w == A.w)
1106 {
1107 B2.dims = 2;
1108 B2.w = B.w * B.elempack;
1109 B2.elempack = 1;
1110 B2.elemsize = B.elemsize / B.elempack;
1111 B2.cstep = B2.w;
1112 }
1113 }
1114 if (outdims == 3 && B.dims == 1)
1115 {
1116 if (B.w * B.elempack == A.c * A.elempack)
1117 B2 = B.reshape(1, 1, B.w, opt.workspace_allocator);
1118 else // if (B.w == A.w)
1119 {
1120 B2.dims = 3;
1121 B2.w = B.w * B.elempack;
1122 B2.elempack = 1;
1123 B2.elemsize = B.elemsize / B.elempack;
1124 B2.cstep = B2.w;
1125 }
1126 }
1127 if (outdims == 3 && B.dims == 2)
1128 B2 = B.reshape(1, B.w, B.h, opt.workspace_allocator);
1129 if (outdims == 4 && B.dims == 1)
1130 {
1131 if (B.w * B.elempack == A.c * A.elempack)
1132 B2 = B.reshape(1, 1, 1, B.w, opt.workspace_allocator);
1133 else // if (B.w == A.w)
1134 {
1135 B2.dims = 4;
1136 B2.w = B.w * B.elempack;
1137 B2.elempack = 1;
1138 B2.elemsize = B.elemsize / B.elempack;
1139 B2.cstep = B2.w;
1140 }
1141 }
1142 if (outdims == 4 && B.dims == 2)
1143 B2 = B.reshape(1, 1, B.w, B.h, opt.workspace_allocator);
1144 if (outdims == 4 && B.dims == 3)
1145 B2 = B.reshape(1, B.w, B.h, B.c, opt.workspace_allocator);
1146 }
1147
1148 const int outw = std::max(A2.w, B2.w);
1149 const int outh = std::max(A2.h, B2.h);
1150 const int outd = std::max(A2.d, B2.d);
1151 const int outc = std::max(A2.c, B2.c);
1152 const size_t out_elemsize = std::max(A2.elemsize, B2.elemsize);
1153 const int out_elempack = std::max(A2.elempack, B2.elempack);
1154
1155 Mat& top_blob = top_blobs[0];
1156 if (outdims == 1)
1157 {
1158 top_blob.create(outw, out_elemsize, out_elempack, opt.blob_allocator);
1159 }
1160 if (outdims == 2)
1161 {
1162 top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_allocator);
1163 }
1164 if (outdims == 3)
1165 {
1166 top_blob.create(outw, outh, outc, out_elemsize, out_elempack, opt.blob_allocator);
1167 }
1168 if (outdims == 4)
1169 {
1170 top_blob.create(outw, outh, outd, outc, out_elemsize, out_elempack, opt.blob_allocator);
1171 }
1172 if (top_blob.empty())
1173 return -100;
1174
1175 const bool a_pack_is_lower = A2.elempack < B2.elempack;
1176 const bool a_pack_is_equal = A2.elempack == B2.elempack;
1177 const bool a_size_is_lower = A2.w * A2.h * A2.d * A2.c * A2.elempack < B2.w * B2.h * B2.d * B2.c * B2.elempack;
1178 if (a_pack_is_lower || (a_pack_is_equal && a_size_is_lower))
1179 {
1180 binary_op_broadcast_bf16s(B2, A2, top_blob, get_reverse_op_type(op_type), opt);
1181 }
1182 else
1183 {
1184 binary_op_broadcast_bf16s(A2, B2, top_blob, op_type, opt);
1185 }
1186
1187 return 0;
1188}
1189
1190int BinaryOp_arm::forward_inplace_bf16s(Mat& bottom_top_blob, const Option& opt) const
1191{
1192 binary_op_scalar_inplace_bf16s(bottom_top_blob, b, op_type, opt);
1193
1194 return 0;
1195}
1196#endif // NCNN_BF16
1197
1198} // namespace ncnn