Elffuss BraidHub

kikocis/ncnn public

master · 22.9 KiBRawPermalink
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 "binaryop_arm.h"
16
17#if __ARM_NEON
18#include <arm_neon.h>
19#include "neon_mathfun.h"
20#include "arm_usability.h"
21#endif // __ARM_NEON
22
23namespace ncnn {
24
25#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
26template<typename Op>
27static void binary_op_vector_no_broadcast_fp16s(const __fp16* ptr, const __fp16* ptr1, __fp16* outptr, int size)
28{
29 const Op op;
30
31 int i = 0;
32 for (; i + 7 < size; i += 8)
33 {
34 float16x8_t _p = vld1q_f16(ptr);
35 float16x8_t _b = vld1q_f16(ptr1);
36 float16x8_t _outp = op(_p, _b);
37 vst1q_f16(outptr, _outp);
38 ptr += 8;
39 ptr1 += 8;
40 outptr += 8;
41 }
42 for (; i + 3 < size; i += 4)
43 {
44 float16x4_t _p = vld1_f16(ptr);
45 float16x4_t _b = vld1_f16(ptr1);
46 float16x4_t _outp = op(_p, _b);
47 vst1_f16(outptr, _outp);
48 ptr += 4;
49 ptr1 += 4;
50 outptr += 4;
51 }
52 for (; i < size; i++)
53 {
54 *outptr = op(*ptr, *ptr1);
55 ptr += 1;
56 ptr1 += 1;
57 outptr += 1;
58 }
59}
60
61template<typename Op>
62static void binary_op_vector_broadcast_b_fp16s(const __fp16* ptr, const __fp16* ptr1, __fp16* outptr, int size, int elempack)
63{
64 const Op op;
65
66 const __fp16 b = *ptr1;
67
68 int i = 0;
69 float16x4_t _b_128 = (elempack == 4) ? vld1_f16(ptr1) : vdup_n_f16(b);
70 float16x8_t _b_256 = (elempack == 8) ? vld1q_f16(ptr1) : vcombine_f16(_b_128, _b_128);
71 for (; i + 7 < size; i += 8)
72 {
73 float16x8_t _p = vld1q_f16(ptr);
74 float16x8_t _outp = op(_p, _b_256);
75 vst1q_f16(outptr, _outp);
76 ptr += 8;
77 outptr += 8;
78 }
79 for (; i + 3 < size; i += 4)
80 {
81 float16x4_t _p = vld1_f16(ptr);
82 float16x4_t _outp = op(_p, _b_128);
83 vst1_f16(outptr, _outp);
84 ptr += 4;
85 outptr += 4;
86 }
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_fp16s(const __fp16* ptr, const __fp16* ptr1, __fp16* outptr, int size, int elempack)
97{
98 const Op op;
99
100 const __fp16 a = *ptr;
101
102 int i = 0;
103 float16x4_t _a_128 = (elempack == 4) ? vld1_f16(ptr) : vdup_n_f16(a);
104 float16x8_t _a_256 = (elempack == 8) ? vld1q_f16(ptr) : vcombine_f16(_a_128, _a_128);
105 for (; i + 7 < size; i += 8)
106 {
107 float16x8_t _b = vld1q_f16(ptr1);
108 float16x8_t _outp = op(_a_256, _b);
109 vst1q_f16(outptr, _outp);
110 ptr1 += 8;
111 outptr += 8;
112 }
113 for (; i + 3 < size; i += 4)
114 {
115 float16x4_t _b = vld1_f16(ptr1);
116 float16x4_t _outp = op(_a_128, _b);
117 vst1_f16(outptr, _outp);
118 ptr1 += 4;
119 outptr += 4;
120 }
121 for (; i < size; i++)
122 {
123 *outptr = op(a, *ptr1);
124 ptr1 += 1;
125 outptr += 1;
126 }
127}
128
129template<typename Op>
130static void binary_op_vector_broadcast_pb_fp16s(const __fp16* ptr, const __fp16* ptr1, __fp16* outptr, int w, int elempack)
131{
132 const Op op;
133
134 if (elempack == 8)
135 {
136 int i = 0;
137 for (; i < w; i++)
138 {
139 float16x8_t _p = vld1q_f16(ptr);
140 float16x8_t _b = vdupq_n_f16(*ptr1);
141 float16x8_t _outp = op(_p, _b);
142 vst1q_f16(outptr, _outp);
143 ptr += 8;
144 ptr1 += 1;
145 outptr += 8;
146 }
147 }
148 if (elempack == 4)
149 {
150 int i = 0;
151 for (; i + 1 < w; i += 2)
152 {
153 float16x8_t _p = vld1q_f16(ptr);
154 float16x4_t _b0 = vdup_n_f16(ptr1[0]);
155 float16x4_t _b1 = vdup_n_f16(ptr1[1]);
156 float16x8_t _b = vcombine_f16(_b0, _b1);
157 float16x8_t _outp = op(_p, _b);
158 vst1q_f16(outptr, _outp);
159 ptr += 8;
160 ptr1 += 2;
161 outptr += 8;
162 }
163 for (; i < w; i++)
164 {
165 float16x4_t _p = vld1_f16(ptr);
166 float16x4_t _b = vdup_n_f16(*ptr1);
167 float16x4_t _outp = op(_p, _b);
168 vst1_f16(outptr, _outp);
169 ptr += 4;
170 ptr1 += 1;
171 outptr += 4;
172 }
173 }
174}
175
176template<typename Op>
177static void binary_op_vector_broadcast_pb_b_fp16s(const __fp16* ptr, const __fp16* ptr1, __fp16* outptr, int w, int elempack)
178{
179 const Op op;
180
181 const int size = w * elempack;
182
183 int i = 0;
184 float16x8_t _b = vdupq_n_f16(*ptr1);
185 for (; i + 7 < size; i += 8)
186 {
187 float16x8_t _p = vld1q_f16(ptr);
188 float16x8_t _outp = op(_p, _b);
189 vst1q_f16(outptr, _outp);
190 ptr += 8;
191 outptr += 8;
192 }
193 for (; i + 3 < size; i += 4)
194 {
195 float16x4_t _p = vld1_f16(ptr);
196 float16x4_t _outp = op(_p, vget_low_f16(_b));
197 vst1_f16(outptr, _outp);
198 ptr += 4;
199 outptr += 4;
200 }
201}
202
203template<typename Op>
204static void binary_op_vector_broadcast_pb_a_fp16s(const __fp16* ptr, const __fp16* ptr1, __fp16* outptr, int w, int elempack)
205{
206 const Op op;
207
208 if (elempack == 8)
209 {
210 int i = 0;
211 float16x8_t _p = vld1q_f16(ptr);
212 for (; i < w; i++)
213 {
214 float16x8_t _b = vdupq_n_f16(*ptr1);
215 float16x8_t _outp = op(_p, _b);
216 vst1q_f16(outptr, _outp);
217 ptr1 += 1;
218 outptr += 8;
219 }
220 }
221 if (elempack == 4)
222 {
223 int i = 0;
224 float16x4_t _p0 = vld1_f16(ptr);
225 float16x8_t _p = vcombine_f16(_p0, _p0);
226 for (; i + 1 < w; i += 2)
227 {
228 float16x4_t _b0 = vdup_n_f16(ptr1[0]);
229 float16x4_t _b1 = vdup_n_f16(ptr1[1]);
230 float16x8_t _b = vcombine_f16(_b0, _b1);
231 float16x8_t _outp = op(_p, _b);
232 vst1q_f16(outptr, _outp);
233 ptr1 += 2;
234 outptr += 8;
235 }
236 for (; i < w; i++)
237 {
238 float16x4_t _b = vdup_n_f16(*ptr1);
239 float16x4_t _outp = op(_p0, _b);
240 vst1_f16(outptr, _outp);
241 ptr1 += 1;
242 outptr += 4;
243 }
244 }
245}
246
247template<typename Op>
248static void binary_op_vector_fp16s(const __fp16* ptr, const __fp16* ptr1, __fp16* outptr, int aw, int bw, int ap, int bp)
249{
250 const int w = std::max(aw, bw);
251 const int elempack = std::max(ap, bp);
252 const int size = w * elempack;
253
254 if (ap == bp)
255 {
256 if (aw == bw)
257 {
258 // no broadcast
259 return binary_op_vector_no_broadcast_fp16s<Op>(ptr, ptr1, outptr, size);
260 }
261
262 if (bw == 1)
263 {
264 // broadcast single b
265 return binary_op_vector_broadcast_b_fp16s<Op>(ptr, ptr1, outptr, size, elempack);
266 }
267
268 if (aw == 1)
269 {
270 // broadcast single a
271 return binary_op_vector_broadcast_a_fp16s<Op>(ptr, ptr1, outptr, size, elempack);
272 }
273 }
274
275 if (bp == 1)
276 {
277 if (aw == bw)
278 {
279 // broadcast pack1 b
280 return binary_op_vector_broadcast_pb_fp16s<Op>(ptr, ptr1, outptr, w, elempack);
281 }
282
283 if (bw == 1)
284 {
285 // broadcast pack1 single b
286 return binary_op_vector_broadcast_pb_b_fp16s<Op>(ptr, ptr1, outptr, w, elempack);
287 }
288
289 if (aw == 1)
290 {
291 // broadcast single a and pack1 b
292 return binary_op_vector_broadcast_pb_a_fp16s<Op>(ptr, ptr1, outptr, w, elempack);
293 }
294 }
295
296 // shall never reach here
297}
298
299namespace BinaryOp_arm_functor {
300
301#define MAKE_FUNCTION(NAME, IMPL, IMPL4, IMPL8) \
302 struct NAME \
303 { \
304 __fp16 operator()(const __fp16& x, const __fp16& y) const \
305 { \
306 return IMPL; \
307 } \
308 float16x4_t operator()(const float16x4_t& x, const float16x4_t& y) const \
309 { \
310 return IMPL4; \
311 } \
312 float16x8_t operator()(const float16x8_t& x, const float16x8_t& y) const \
313 { \
314 return IMPL8; \
315 } \
316 };
317
318// clang-format off
319// *INDENT-OFF*
320MAKE_FUNCTION(binary_op_add_fp16s, x + y, vadd_f16(x, y), vaddq_f16(x, y))
321MAKE_FUNCTION(binary_op_sub_fp16s, x - y, vsub_f16(x, y), vsubq_f16(x, y))
322MAKE_FUNCTION(binary_op_mul_fp16s, x * y, vmul_f16(x, y), vmulq_f16(x, y))
323MAKE_FUNCTION(binary_op_div_fp16s, x / y, vdiv_f16(x, y), vdivq_f16(x, y))
324MAKE_FUNCTION(binary_op_max_fp16s, std::max(x, y), vmax_f16(x, y), vmaxq_f16(x, y))
325MAKE_FUNCTION(binary_op_min_fp16s, std::min(x, y), vmin_f16(x, y), vminq_f16(x, y))
326MAKE_FUNCTION(binary_op_pow_fp16s, (__fp16)powf(x, y), vcvt_f16_f32(pow_ps(vcvt_f32_f16(x), vcvt_f32_f16(y))), vcombine_f16(vcvt_f16_f32(pow_ps(vcvt_f32_f16(vget_low_f16(x)), vcvt_f32_f16(vget_low_f16(y)))), vcvt_f16_f32(pow_ps(vcvt_f32_f16(vget_high_f16(x)), vcvt_f32_f16(vget_high_f16(y))))))
327MAKE_FUNCTION(binary_op_rsub_fp16s, y - x, vsub_f16(y, x), vsubq_f16(y, x))
328MAKE_FUNCTION(binary_op_rdiv_fp16s, y / x, vdiv_f16(y, x), vdivq_f16(y, x))
329MAKE_FUNCTION(binary_op_rpow_fp16s, (__fp16)powf(y, x), vcvt_f16_f32(pow_ps(vcvt_f32_f16(y), vcvt_f32_f16(x))), vcombine_f16(vcvt_f16_f32(pow_ps(vcvt_f32_f16(vget_low_f16(y)), vcvt_f32_f16(vget_low_f16(x)))), vcvt_f16_f32(pow_ps(vcvt_f32_f16(vget_high_f16(y)), vcvt_f32_f16(vget_high_f16(x))))))
330MAKE_FUNCTION(binary_op_atan2_fp16s, (__fp16)atan2f(x, y), vcvt_f16_f32(atan2_ps(vcvt_f32_f16(x), vcvt_f32_f16(y))), vcombine_f16(vcvt_f16_f32(atan2_ps(vcvt_f32_f16(vget_low_f16(x)), vcvt_f32_f16(vget_low_f16(y)))), vcvt_f16_f32(atan2_ps(vcvt_f32_f16(vget_high_f16(x)), vcvt_f32_f16(vget_high_f16(y))))))
331MAKE_FUNCTION(binary_op_ratan2_fp16s, (__fp16)atan2f(y, x), vcvt_f16_f32(atan2_ps(vcvt_f32_f16(y), vcvt_f32_f16(x))), vcombine_f16(vcvt_f16_f32(atan2_ps(vcvt_f32_f16(vget_low_f16(y)), vcvt_f32_f16(vget_low_f16(x)))), vcvt_f16_f32(atan2_ps(vcvt_f32_f16(vget_high_f16(y)), vcvt_f32_f16(vget_high_f16(x))))))
332// *INDENT-ON*
333// clang-format on
334
335#undef MAKE_FUNCTION
336
337} // namespace BinaryOp_arm_functor
338
339static void binary_op_vector_fp16s(const __fp16* ptr, const __fp16* ptr1, __fp16* outptr, int aw, int bw, int ap, int bp, int op_type)
340{
341 using namespace BinaryOp_arm_functor;
342
343 if (op_type == BinaryOp::Operation_ADD) return binary_op_vector_fp16s<binary_op_add_fp16s>(ptr, ptr1, outptr, aw, bw, ap, bp);
344 if (op_type == BinaryOp::Operation_SUB) return binary_op_vector_fp16s<binary_op_sub_fp16s>(ptr, ptr1, outptr, aw, bw, ap, bp);
345 if (op_type == BinaryOp::Operation_MUL) return binary_op_vector_fp16s<binary_op_mul_fp16s>(ptr, ptr1, outptr, aw, bw, ap, bp);
346 if (op_type == BinaryOp::Operation_DIV) return binary_op_vector_fp16s<binary_op_div_fp16s>(ptr, ptr1, outptr, aw, bw, ap, bp);
347 if (op_type == BinaryOp::Operation_MAX) return binary_op_vector_fp16s<binary_op_max_fp16s>(ptr, ptr1, outptr, aw, bw, ap, bp);
348 if (op_type == BinaryOp::Operation_MIN) return binary_op_vector_fp16s<binary_op_min_fp16s>(ptr, ptr1, outptr, aw, bw, ap, bp);
349 if (op_type == BinaryOp::Operation_POW) return binary_op_vector_fp16s<binary_op_pow_fp16s>(ptr, ptr1, outptr, aw, bw, ap, bp);
350 if (op_type == BinaryOp::Operation_RSUB) return binary_op_vector_fp16s<binary_op_rsub_fp16s>(ptr, ptr1, outptr, aw, bw, ap, bp);
351 if (op_type == BinaryOp::Operation_RDIV) return binary_op_vector_fp16s<binary_op_rdiv_fp16s>(ptr, ptr1, outptr, aw, bw, ap, bp);
352 if (op_type == BinaryOp::Operation_RPOW) return binary_op_vector_fp16s<binary_op_rpow_fp16s>(ptr, ptr1, outptr, aw, bw, ap, bp);
353 if (op_type == BinaryOp::Operation_ATAN2) return binary_op_vector_fp16s<binary_op_atan2_fp16s>(ptr, ptr1, outptr, aw, bw, ap, bp);
354 if (op_type == BinaryOp::Operation_RATAN2) return binary_op_vector_fp16s<binary_op_ratan2_fp16s>(ptr, ptr1, outptr, aw, bw, ap, bp);
355
356 // should never reach here
357}
358
359static void binary_op_scalar_fp16s(const Mat& a, __fp16 b, Mat& c, int op_type, const Option& opt)
360{
361 const int channels = a.c;
362 const int size = a.w * a.h * a.d * a.elempack;
363
364 #pragma omp parallel for num_threads(opt.num_threads)
365 for (int q = 0; q < channels; q++)
366 {
367 const __fp16* ptr = a.channel(q);
368 __fp16* outptr = c.channel(q);
369
370 binary_op_vector_fp16s(ptr, &b, outptr, size, 1, 1, 1, op_type);
371 }
372}
373
374static void binary_op_no_broadcast_fp16s(const Mat& a, const Mat& b, Mat& c, int op_type, const Option& opt)
375{
376 const int channels = a.c;
377 const int size = a.w * a.h * a.d * a.elempack;
378
379 #pragma omp parallel for num_threads(opt.num_threads)
380 for (int q = 0; q < channels; q++)
381 {
382 const __fp16* ptr = a.channel(q);
383 const __fp16* ptr1 = b.channel(q);
384 __fp16* outptr = c.channel(q);
385
386 binary_op_vector_fp16s(ptr, ptr1, outptr, size, size, 1, 1, op_type);
387 }
388}
389
390static void binary_op_broadcast_fp16s(const Mat& a, const Mat& b, Mat& c, int op_type, const Option& opt)
391{
392 if (b.w * b.h * b.d * b.c * b.elempack == 1)
393 {
394 return binary_op_scalar_fp16s(a, ((const __fp16*)b)[0], c, op_type, opt);
395 }
396
397 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)
398 {
399 return binary_op_no_broadcast_fp16s(a, b, c, op_type, opt);
400 }
401
402 const int dims = c.dims;
403
404 if (dims == 2)
405 {
406 const int h = c.h;
407
408 #pragma omp parallel for num_threads(opt.num_threads)
409 for (int y = 0; y < h; y++)
410 {
411 const int y0 = std::min(y, a.h - 1);
412 const int y1 = std::min(y, b.h - 1);
413
414 const __fp16* ptr = a.row<const __fp16>(y0);
415 const __fp16* ptr1 = b.row<const __fp16>(y1);
416 __fp16* outptr = c.row<__fp16>(y);
417
418 binary_op_vector_fp16s(ptr, ptr1, outptr, a.w, b.w, a.elempack, b.elempack, op_type);
419 }
420 }
421
422 if (dims == 3 || dims == 4)
423 {
424 const int channels = c.c;
425
426 #pragma omp parallel for num_threads(opt.num_threads)
427 for (int q = 0; q < channels; q++)
428 {
429 const int q0 = std::min(q, a.c - 1);
430 const int q1 = std::min(q, b.c - 1);
431
432 if (b.d * b.h * b.w == 1)
433 {
434 const __fp16* ptr = a.channel(q0);
435 const __fp16* ptr1 = b.channel(q1);
436 __fp16* outptr = c.channel(q);
437
438 binary_op_vector_fp16s(ptr, ptr1, outptr, a.w * a.h * a.d, 1, a.elempack, b.elempack, op_type);
439 continue;
440 }
441
442 if (b.h * b.w == 1)
443 {
444 for (int z = 0; z < c.d; z++)
445 {
446 const int z0 = std::min(z, a.d - 1);
447 const int z1 = std::min(z, b.d - 1);
448
449 const __fp16* ptr = a.channel(q0).depth(z0);
450 const __fp16* ptr1 = b.channel(q1).depth(z1);
451 __fp16* outptr = c.channel(q).depth(z);
452
453 binary_op_vector_fp16s(ptr, ptr1, outptr, a.w * a.h, 1, a.elempack, b.elempack, op_type);
454 }
455 continue;
456 }
457
458 for (int z = 0; z < c.d; z++)
459 {
460 const int z0 = std::min(z, a.d - 1);
461 const int z1 = std::min(z, b.d - 1);
462
463 for (int y = 0; y < c.h; y++)
464 {
465 const int y0 = std::min(y, a.h - 1);
466 const int y1 = std::min(y, b.h - 1);
467
468 const __fp16* ptr = a.channel(q0).depth(z0).row<const __fp16>(y0);
469 const __fp16* ptr1 = b.channel(q1).depth(z1).row<const __fp16>(y1);
470 __fp16* outptr = c.channel(q).depth(z).row<__fp16>(y);
471
472 binary_op_vector_fp16s(ptr, ptr1, outptr, a.w, b.w, a.elempack, b.elempack, op_type);
473 }
474 }
475 }
476 }
477}
478
479static void binary_op_scalar_inplace_fp16s(Mat& a, __fp16 b, int op_type, const Option& opt)
480{
481 const int channels = a.c;
482 const int size = a.w * a.h * a.d * a.elempack;
483
484 #pragma omp parallel for num_threads(opt.num_threads)
485 for (int q = 0; q < channels; q++)
486 {
487 __fp16* ptr = a.channel(q);
488
489 binary_op_vector_fp16s(ptr, &b, ptr, size, 1, 1, 1, op_type);
490 }
491}
492
493static int get_reverse_op_type(int op_type)
494{
495 if (op_type == BinaryOp::Operation_SUB) return BinaryOp::Operation_RSUB;
496 if (op_type == BinaryOp::Operation_DIV) return BinaryOp::Operation_RDIV;
497 if (op_type == BinaryOp::Operation_POW) return BinaryOp::Operation_RPOW;
498 if (op_type == BinaryOp::Operation_ATAN2) return BinaryOp::Operation_RATAN2;
499 if (op_type == BinaryOp::Operation_RSUB) return BinaryOp::Operation_SUB;
500 if (op_type == BinaryOp::Operation_RDIV) return BinaryOp::Operation_DIV;
501 if (op_type == BinaryOp::Operation_RPOW) return BinaryOp::Operation_POW;
502 if (op_type == BinaryOp::Operation_RATAN2) return BinaryOp::Operation_ATAN2;
503 return op_type;
504}
505
506int BinaryOp_arm::forward_fp16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
507{
508 const Mat& A = bottom_blobs[0];
509 const Mat& B = bottom_blobs[1];
510 const int outdims = std::max(A.dims, B.dims);
511
512 Mat A2 = A;
513 Mat B2 = B;
514 if (A.dims < outdims)
515 {
516 // expand inner axes
517 if (outdims == 2)
518 {
519 if (A.w * A.elempack == B.h * B.elempack)
520 A2 = A.reshape(1, A.w, opt.workspace_allocator);
521 else // if (A.w == B.w)
522 {
523 A2.dims = 2;
524 A2.w = A.w * A.elempack;
525 A2.elempack = 1;
526 A2.elemsize = A.elemsize / A.elempack;
527 A2.cstep = A2.w;
528 }
529 }
530 if (outdims == 3 && A.dims == 1)
531 {
532 if (A.w * A.elempack == B.c * B.elempack)
533 A2 = A.reshape(1, 1, A.w, opt.workspace_allocator);
534 else // if (A.w == B.w)
535 {
536 A2.dims = 3;
537 A2.w = A.w * A.elempack;
538 A2.elempack = 1;
539 A2.elemsize = A.elemsize / A.elempack;
540 A2.cstep = A2.w;
541 }
542 }
543 if (outdims == 3 && A.dims == 2)
544 A2 = A.reshape(1, A.w, A.h, opt.workspace_allocator);
545 if (outdims == 4 && A.dims == 1)
546 {
547 if (A.w * A.elempack == B.c * B.elempack)
548 A2 = A.reshape(1, 1, 1, A.w, opt.workspace_allocator);
549 else // if (A.w == B.w)
550 {
551 A2.dims = 4;
552 A2.w = A.w * A.elempack;
553 A2.elempack = 1;
554 A2.elemsize = A.elemsize / A.elempack;
555 A2.cstep = A2.w;
556 }
557 }
558 if (outdims == 4 && A.dims == 2)
559 A2 = A.reshape(1, 1, A.w, A.h, opt.workspace_allocator);
560 if (outdims == 4 && A.dims == 3)
561 A2 = A.reshape(1, A.w, A.h, A.c, opt.workspace_allocator);
562 }
563 if (B.dims < outdims)
564 {
565 // expand inner axes
566 if (outdims == 2)
567 {
568 if (B.w * B.elempack == A.h * A.elempack)
569 B2 = B.reshape(1, B.w, opt.workspace_allocator);
570 else // if (B.w == A.w)
571 {
572 B2.dims = 2;
573 B2.w = B.w * B.elempack;
574 B2.elempack = 1;
575 B2.elemsize = B.elemsize / B.elempack;
576 B2.cstep = B2.w;
577 }
578 }
579 if (outdims == 3 && B.dims == 1)
580 {
581 if (B.w * B.elempack == A.c * A.elempack)
582 B2 = B.reshape(1, 1, B.w, opt.workspace_allocator);
583 else // if (B.w == A.w)
584 {
585 B2.dims = 3;
586 B2.w = B.w * B.elempack;
587 B2.elempack = 1;
588 B2.elemsize = B.elemsize / B.elempack;
589 B2.cstep = B2.w;
590 }
591 }
592 if (outdims == 3 && B.dims == 2)
593 B2 = B.reshape(1, B.w, B.h, opt.workspace_allocator);
594 if (outdims == 4 && B.dims == 1)
595 {
596 if (B.w * B.elempack == A.c * A.elempack)
597 B2 = B.reshape(1, 1, 1, B.w, opt.workspace_allocator);
598 else // if (B.w == A.w)
599 {
600 B2.dims = 4;
601 B2.w = B.w * B.elempack;
602 B2.elempack = 1;
603 B2.elemsize = B.elemsize / B.elempack;
604 B2.cstep = B2.w;
605 }
606 }
607 if (outdims == 4 && B.dims == 2)
608 B2 = B.reshape(1, 1, B.w, B.h, opt.workspace_allocator);
609 if (outdims == 4 && B.dims == 3)
610 B2 = B.reshape(1, B.w, B.h, B.c, opt.workspace_allocator);
611 }
612
613 const int outw = std::max(A2.w, B2.w);
614 const int outh = std::max(A2.h, B2.h);
615 const int outd = std::max(A2.d, B2.d);
616 const int outc = std::max(A2.c, B2.c);
617 const size_t out_elemsize = std::max(A2.elemsize, B2.elemsize);
618 const int out_elempack = std::max(A2.elempack, B2.elempack);
619
620 Mat& top_blob = top_blobs[0];
621 if (outdims == 1)
622 {
623 top_blob.create(outw, out_elemsize, out_elempack, opt.blob_allocator);
624 }
625 if (outdims == 2)
626 {
627 top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_allocator);
628 }
629 if (outdims == 3)
630 {
631 top_blob.create(outw, outh, outc, out_elemsize, out_elempack, opt.blob_allocator);
632 }
633 if (outdims == 4)
634 {
635 top_blob.create(outw, outh, outd, outc, out_elemsize, out_elempack, opt.blob_allocator);
636 }
637 if (top_blob.empty())
638 return -100;
639
640 const bool a_pack_is_lower = A2.elempack < B2.elempack;
641 const bool a_pack_is_equal = A2.elempack == B2.elempack;
642 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;
643 if (a_pack_is_lower || (a_pack_is_equal && a_size_is_lower))
644 {
645 binary_op_broadcast_fp16s(B2, A2, top_blob, get_reverse_op_type(op_type), opt);
646 }
647 else
648 {
649 binary_op_broadcast_fp16s(A2, B2, top_blob, op_type, opt);
650 }
651
652 return 0;
653}
654
655int BinaryOp_arm::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const
656{
657 binary_op_scalar_inplace_fp16s(bottom_top_blob, b, op_type, opt);
658
659 return 0;
660}
661#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
662
663} // namespace ncnn