Elffuss BraidHub

kikocis/ncnn public

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