You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

binaryop_arm.cpp 63 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  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. #include "binaryop_arm.h"
  15. #include <math.h>
  16. #include <algorithm>
  17. #if __ARM_NEON
  18. #include <arm_neon.h>
  19. #include "neon_mathfun.h"
  20. #endif // __ARM_NEON
  21. namespace ncnn {
  22. DEFINE_LAYER_CREATOR(BinaryOp_arm)
  23. BinaryOp_arm::BinaryOp_arm()
  24. {
  25. #if __ARM_NEON
  26. support_packing = true;
  27. #endif // __ARM_NEON
  28. support_bf16_storage = true;
  29. }
  30. #if __ARM_NEON
  31. // broadcasting rule
  32. // https://github.com/Tencent/ncnn/wiki/binaryop-broadcasting
  33. template<typename Op>
  34. static int binary_op_pack4(const Mat& a, const Mat& b, Mat& c, const Option& opt)
  35. {
  36. Op op;
  37. int w = a.w;
  38. int h = a.h;
  39. int channels = a.c;
  40. int size = w * h;
  41. size_t elemsize = a.elemsize;
  42. int elempack = a.elempack;
  43. int w1 = b.w;
  44. int h1 = b.h;
  45. int channels1 = b.c;
  46. int size1 = w1 * h1;
  47. size_t elemsize1 = b.elemsize;
  48. int elempack1 = b.elempack;
  49. if (a.dims == 3)
  50. {
  51. if (b.dims == 3)
  52. {
  53. if (w1 == 1 && h1 == 1 && channels1 == channels)
  54. {
  55. // special type 1
  56. c.create(w, h, channels, elemsize, elempack, opt.blob_allocator);
  57. if (c.empty())
  58. return -100;
  59. #pragma omp parallel for num_threads(opt.num_threads)
  60. for (int q=0; q<channels; q++)
  61. {
  62. const float* ptr = a.channel(q);
  63. const float* b0 = b.channel(q);
  64. float* outptr = c.channel(q);
  65. float32x4_t _b0 = vld1q_f32(b0);
  66. for (int i = 0; i < size; i++)
  67. {
  68. float32x4_t _p = vld1q_f32(ptr);
  69. float32x4_t _outp = op(_p, _b0);
  70. vst1q_f32(outptr, _outp);
  71. ptr += 4;
  72. outptr += 4;
  73. }
  74. }
  75. return 0;
  76. }
  77. if (w1 == w && h1 == h && channels1 == 1 && elempack1 == 1)
  78. {
  79. // special type 2
  80. c.create(w, h, channels, elemsize, elempack, opt.blob_allocator);
  81. if (c.empty())
  82. return -100;
  83. #pragma omp parallel for num_threads(opt.num_threads)
  84. for (int q = 0; q < channels; q++)
  85. {
  86. const float* ptr = a.channel(q);
  87. const float* ptr1 = b;
  88. float* outptr = c.channel(q);
  89. for (int i = 0; i < size; i++)
  90. {
  91. float32x4_t _p = vld1q_f32(ptr);
  92. float32x4_t _p1 = vld1q_dup_f32(ptr1);
  93. float32x4_t _outp = op(_p, _p1);
  94. vst1q_f32(outptr, _outp);
  95. ptr += 4;
  96. ptr1 += 1;
  97. outptr += 4;
  98. }
  99. }
  100. return 0;
  101. }
  102. if (w == 1 && h == 1 && channels1 == channels)
  103. {
  104. // special type 3
  105. c.create(w1, h1, channels1, elemsize1, elempack1, opt.blob_allocator);
  106. if (c.empty())
  107. return -100;
  108. #pragma omp parallel for num_threads(opt.num_threads)
  109. for (int q=0; q<channels1; q++)
  110. {
  111. const float* a0 = a.channel(q);
  112. const float* ptr1 = b.channel(q);
  113. float* outptr = c.channel(q);
  114. float32x4_t _a0 = vld1q_f32(a0);
  115. for (int i = 0; i < size1; i++)
  116. {
  117. float32x4_t _p1 = vld1q_f32(ptr1);
  118. float32x4_t _outp = op(_a0, _p1);
  119. vst1q_f32(outptr, _outp);
  120. ptr1 += 4;
  121. outptr += 4;
  122. }
  123. }
  124. return 0;
  125. }
  126. if (w1 == w && h1 == h && channels == 1 && elempack == 1)
  127. {
  128. // special type 4
  129. c.create(w1, h1, channels1, elemsize1, elempack1, opt.blob_allocator);
  130. if (c.empty())
  131. return -100;
  132. #pragma omp parallel for num_threads(opt.num_threads)
  133. for (int q = 0; q < channels1; q++)
  134. {
  135. const float* ptr = a;
  136. const float* ptr1 = b.channel(q);
  137. float* outptr = c.channel(q);
  138. for (int i = 0; i < size1; i++)
  139. {
  140. float32x4_t _p = vld1q_dup_f32(ptr);
  141. float32x4_t _p1 = vld1q_f32(ptr1);
  142. float32x4_t _outp = op(_p, _p1);
  143. vst1q_f32(outptr, _outp);
  144. ptr += 1;
  145. ptr1 += 4;
  146. outptr += 4;
  147. }
  148. }
  149. return 0;
  150. }
  151. // type 19
  152. c.create(w, h, channels, elemsize, elempack, opt.blob_allocator);
  153. if (c.empty())
  154. return -100;
  155. #pragma omp parallel for num_threads(opt.num_threads)
  156. for (int q=0; q<channels; q++)
  157. {
  158. const float* ptr = a.channel(q);
  159. const float* ptr1 = b.channel(q);
  160. float* outptr = c.channel(q);
  161. for (int i=0; i<size; i++)
  162. {
  163. float32x4_t _p = vld1q_f32(ptr);
  164. float32x4_t _p1 = vld1q_f32(ptr1);
  165. float32x4_t _outp = op(_p, _p1);
  166. vst1q_f32(outptr, _outp);
  167. ptr += 4;
  168. ptr1 += 4;
  169. outptr += 4;
  170. }
  171. }
  172. return 0;
  173. }
  174. c.create(w, h, channels, elemsize, elempack, opt.blob_allocator);
  175. if (c.empty())
  176. return -100;
  177. if (b.dims == 2)
  178. {
  179. // type 18
  180. #pragma omp parallel for num_threads(opt.num_threads)
  181. for (int q=0; q<channels; q++)
  182. {
  183. const float* ptr = a.channel(q);
  184. const float* ptr1 = b.row(q);
  185. float* outptr = c.channel(q);
  186. for (int y=0; y<h; y++)
  187. {
  188. float32x4_t _b0 = vld1q_f32(ptr1);
  189. for (int x=0; x<w; x++)
  190. {
  191. float32x4_t _p = vld1q_f32(ptr);
  192. float32x4_t _outp = op(_p, _b0);
  193. vst1q_f32(outptr, _outp);
  194. ptr += 4;
  195. outptr += 4;
  196. }
  197. ptr1 += 4;
  198. }
  199. }
  200. return 0;
  201. }
  202. if (b.dims == 1)
  203. {
  204. if (b.w == 1 && elempack1 == 1)
  205. {
  206. // type 16
  207. float32x4_t _b0 = vdupq_n_f32(b[0]);
  208. #pragma omp parallel for num_threads(opt.num_threads)
  209. for (int q=0; q<channels; q++)
  210. {
  211. const float* ptr = a.channel(q);
  212. float* outptr = c.channel(q);
  213. for (int i=0; i<size; i++)
  214. {
  215. float32x4_t _p = vld1q_f32(ptr);
  216. float32x4_t _outp = op(_p, _b0);
  217. vst1q_f32(outptr, _outp);
  218. ptr += 4;
  219. outptr += 4;
  220. }
  221. }
  222. return 0;
  223. }
  224. // type 17
  225. #pragma omp parallel for num_threads(opt.num_threads)
  226. for (int q=0; q<channels; q++)
  227. {
  228. const float* ptr = a.channel(q);
  229. float32x4_t _b0 = vld1q_f32((const float*)b + q * 4);
  230. float* outptr = c.channel(q);
  231. for (int i=0; i<size; i++)
  232. {
  233. float32x4_t _p = vld1q_f32(ptr);
  234. float32x4_t _outp = op(_p, _b0);
  235. vst1q_f32(outptr, _outp);
  236. ptr += 4;
  237. outptr += 4;
  238. }
  239. }
  240. return 0;
  241. }
  242. }
  243. else if (a.dims == 2)
  244. {
  245. if (b.dims == 3)
  246. {
  247. // type 14
  248. c.create(w1, h1, channels1, elemsize1, elempack1, opt.blob_allocator);
  249. if (c.empty())
  250. return -100;
  251. #pragma omp parallel for num_threads(opt.num_threads)
  252. for (int q=0; q<channels1; q++)
  253. {
  254. const float* ptr = a.row(q);
  255. const float* ptr1 = b.channel(q);
  256. float* outptr = c.channel(q);
  257. for (int y=0; y<h1; y++)
  258. {
  259. float32x4_t _a0 = vld1q_f32(ptr);
  260. for (int x=0; x<w1; x++)
  261. {
  262. float32x4_t _p1 = vld1q_f32(ptr1);
  263. float32x4_t _outp = op(_a0, _p1);
  264. vst1q_f32(outptr, _outp);
  265. ptr1 += 4;
  266. outptr += 4;
  267. }
  268. ptr += 4;
  269. }
  270. }
  271. return 0;
  272. }
  273. c.create(w, h, elemsize, elempack, opt.blob_allocator);
  274. if (c.empty())
  275. return -100;
  276. if (b.dims == 2)
  277. {
  278. // type 13
  279. const float* ptr = a;
  280. const float* ptr1 = b;
  281. float* outptr = c;
  282. for (int i=0; i<size; i++)
  283. {
  284. float32x4_t _p = vld1q_f32(ptr);
  285. float32x4_t _p1 = vld1q_f32(ptr1);
  286. float32x4_t _outp = op(_p, _p1);
  287. vst1q_f32(outptr, _outp);
  288. ptr += 4;
  289. ptr1 += 4;
  290. outptr += 4;
  291. }
  292. return 0;
  293. }
  294. if (b.dims == 1)
  295. {
  296. c.create(w, h, elemsize, elempack, opt.blob_allocator);
  297. if (c.empty())
  298. return -100;
  299. if (b.w == 1 && elempack1 == 1)
  300. {
  301. // type 11
  302. float32x4_t _b0 = vdupq_n_f32(b[0]);
  303. const float* ptr = a;
  304. float* outptr = c;
  305. for (int i=0; i<size; i++)
  306. {
  307. float32x4_t _p = vld1q_f32(ptr);
  308. float32x4_t _outp = op(_p, _b0);
  309. vst1q_f32(outptr, _outp);
  310. ptr += 4;
  311. outptr += 4;
  312. }
  313. return 0;
  314. }
  315. // type 12
  316. const float* ptr = a;
  317. const float* ptr1 = b;
  318. float* outptr = c;
  319. for (int y=0; y<h; y++)
  320. {
  321. float32x4_t _b0 = vld1q_f32(ptr1);
  322. for (int x=0; x<w; x++)
  323. {
  324. float32x4_t _p = vld1q_f32(ptr);
  325. float32x4_t _outp = op(_p, _b0);
  326. vst1q_f32(outptr, _outp);
  327. ptr += 4;
  328. outptr += 4;
  329. }
  330. ptr1 += 4;
  331. }
  332. return 0;
  333. }
  334. }
  335. else if (a.dims == 1)
  336. {
  337. if (a.w == 1 && elempack == 1)
  338. {
  339. if (b.dims == 3)
  340. {
  341. // type 4
  342. c.create(w1, h1, channels1, elemsize1, elempack1, opt.blob_allocator);
  343. if (c.empty())
  344. return -100;
  345. float32x4_t _a0 = vdupq_n_f32(a[0]);
  346. #pragma omp parallel for num_threads(opt.num_threads)
  347. for (int q=0; q<channels1; q++)
  348. {
  349. const float* ptr1 = b.channel(q);
  350. float* outptr = c.channel(q);
  351. for (int i=0; i<size1; i++)
  352. {
  353. float32x4_t _p1 = vld1q_f32(ptr1);
  354. float32x4_t _outp = op(_a0, _p1);
  355. vst1q_f32(outptr, _outp);
  356. ptr1 += 4;
  357. outptr += 4;
  358. }
  359. }
  360. return 0;
  361. }
  362. if (b.dims == 2)
  363. {
  364. // type 3
  365. c.create(w1, h1, elemsize1, elempack1, opt.blob_allocator);
  366. if (c.empty())
  367. return -100;
  368. float32x4_t _a0 = vdupq_n_f32(a[0]);
  369. const float* ptr1 = b;
  370. float* outptr = c;
  371. for (int i=0; i<size1; i++)
  372. {
  373. float32x4_t _p1 = vld1q_f32(ptr1);
  374. float32x4_t _outp = op(_a0, _p1);
  375. vst1q_f32(outptr, _outp);
  376. ptr1 += 4;
  377. outptr += 4;
  378. }
  379. return 0;
  380. }
  381. if (b.dims == 1)
  382. {
  383. // type 2
  384. c.create(w1, elemsize1, elempack1, opt.blob_allocator);
  385. if (c.empty())
  386. return -100;
  387. float32x4_t _a0 = vdupq_n_f32(a[0]);
  388. const float* ptr1 = b;
  389. float* outptr = c;
  390. for (int i=0; i<w1; i++)
  391. {
  392. float32x4_t _p1 = vld1q_f32(ptr1);
  393. float32x4_t _outp = op(_a0, _p1);
  394. vst1q_f32(outptr, _outp);
  395. ptr1 += 4;
  396. outptr += 4;
  397. }
  398. return 0;
  399. }
  400. }
  401. if (b.dims == 3)
  402. {
  403. // type 9
  404. c.create(w1, h1, channels1, elemsize1, elempack1, opt.blob_allocator);
  405. if (c.empty())
  406. return -100;
  407. #pragma omp parallel for num_threads(opt.num_threads)
  408. for (int q=0; q<channels1; q++)
  409. {
  410. float32x4_t _a0 = vld1q_f32((const float*)a + q * 4);
  411. const float* ptr1 = b.channel(q);
  412. float* outptr = c.channel(q);
  413. for (int i=0; i<size1; i++)
  414. {
  415. float32x4_t _p1 = vld1q_f32(ptr1);
  416. float32x4_t _outp = op(_a0, _p1);
  417. vst1q_f32(outptr, _outp);
  418. ptr1 += 4;
  419. outptr += 4;
  420. }
  421. }
  422. return 0;
  423. }
  424. if (b.dims == 2)
  425. {
  426. // type 8
  427. c.create(w1, h1, elemsize1, elempack1, opt.blob_allocator);
  428. if (c.empty())
  429. return -100;
  430. const float* ptr = a;
  431. const float* ptr1 = b;
  432. float* outptr = c;
  433. for (int y=0; y<h1; y++)
  434. {
  435. float32x4_t _a0 = vld1q_f32(ptr);
  436. for (int x=0; x<w1; x++)
  437. {
  438. float32x4_t _p1 = vld1q_f32(ptr1);
  439. float32x4_t _outp = op(_a0, _p1);
  440. vst1q_f32(outptr, _outp);
  441. ptr1 += 4;
  442. outptr += 4;
  443. }
  444. ptr += 4;
  445. }
  446. return 0;
  447. }
  448. if (b.dims == 1)
  449. {
  450. c.create(w, elemsize, elempack, opt.blob_allocator);
  451. if (c.empty())
  452. return -100;
  453. if (b.w == 1 && elempack1 == 1)
  454. {
  455. // type 6
  456. float32x4_t _b0 = vdupq_n_f32(b[0]);
  457. const float* ptr = a;
  458. float* outptr = c;
  459. for (int i=0; i<w; i++)
  460. {
  461. float32x4_t _p = vld1q_f32(ptr);
  462. float32x4_t _outp = op(_p, _b0);
  463. vst1q_f32(outptr, _outp);
  464. ptr += 4;
  465. outptr += 4;
  466. }
  467. return 0;
  468. }
  469. // type 7
  470. const float* ptr = a;
  471. const float* ptr1 = b;
  472. float* outptr = c;
  473. for (int i=0; i<w; i++)
  474. {
  475. float32x4_t _p = vld1q_f32(ptr);
  476. float32x4_t _p1 = vld1q_f32(ptr1);
  477. float32x4_t _outp = op(_p, _p1);
  478. vst1q_f32(outptr, _outp);
  479. ptr += 4;
  480. ptr1 += 4;
  481. outptr += 4;
  482. }
  483. }
  484. }
  485. return 0;
  486. }
  487. template<typename Op>
  488. static int binary_op_scalar_inplace_pack4(Mat& a, float b, const Option& opt)
  489. {
  490. Op op;
  491. int w = a.w;
  492. int h = a.h;
  493. int channels = a.c;
  494. int size = w * h;
  495. float32x4_t _b = vdupq_n_f32(b);
  496. #pragma omp parallel for num_threads(opt.num_threads)
  497. for (int q=0; q<channels; q++)
  498. {
  499. float* ptr = a.channel(q);
  500. for (int i=0; i<size; i++)
  501. {
  502. float32x4_t _p = vld1q_f32(ptr);
  503. _p = op(_p, _b);
  504. vst1q_f32(ptr, _p);
  505. ptr += 4;
  506. }
  507. }
  508. return 0;
  509. }
  510. struct binary_op_add_pack4 {
  511. float32x4_t operator() (const float32x4_t& x, const float32x4_t& y) const { return vaddq_f32(x, y); }
  512. };
  513. struct binary_op_sub_pack4 {
  514. float32x4_t operator() (const float32x4_t& x, const float32x4_t& y) const { return vsubq_f32(x, y); }
  515. };
  516. struct binary_op_mul_pack4 {
  517. float32x4_t operator() (const float32x4_t& x, const float32x4_t& y) const { return vmulq_f32(x, y); }
  518. };
  519. struct binary_op_div_pack4 {
  520. float32x4_t operator() (const float32x4_t& x, const float32x4_t& y) const
  521. #if __aarch64__
  522. { return vdivq_f32(x, y); }
  523. #else
  524. { return div_ps(x, y); }
  525. #endif
  526. };
  527. struct binary_op_max_pack4 {
  528. float32x4_t operator() (const float32x4_t& x, const float32x4_t& y) const { return vmaxq_f32(x, y); }
  529. };
  530. struct binary_op_min_pack4 {
  531. float32x4_t operator() (const float32x4_t& x, const float32x4_t& y) const { return vminq_f32(x, y); }
  532. };
  533. struct binary_op_pow_pack4 {
  534. float32x4_t operator() (const float32x4_t& x, const float32x4_t& y) const { return pow_ps(x, y); }
  535. };
  536. struct binary_op_rsub_pack4 {
  537. float32x4_t operator() (const float32x4_t& x, const float32x4_t& y) const { return vsubq_f32(y, x); }
  538. };
  539. struct binary_op_rdiv_pack4 {
  540. float32x4_t operator() (const float32x4_t& x, const float32x4_t& y) const
  541. #if __aarch64__
  542. { return vdivq_f32(y, x); }
  543. #else
  544. { return div_ps(y, x); }
  545. #endif
  546. };
  547. #endif // __ARM_NEON
  548. int BinaryOp_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  549. {
  550. if (opt.use_bf16_storage)
  551. return forward_bf16s(bottom_blobs, top_blobs, opt);
  552. const Mat& bottom_blob = bottom_blobs[0];
  553. const Mat& bottom_blob1 = bottom_blobs[1];
  554. Mat& top_blob = top_blobs[0];
  555. #if __ARM_NEON
  556. int elempack = bottom_blob.elempack;
  557. int elempack1 = bottom_blob1.elempack;
  558. if (elempack == 4 || elempack1 == 4)
  559. {
  560. if (op_type == Operation_ADD)
  561. return binary_op_pack4<binary_op_add_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  562. if (op_type == Operation_SUB)
  563. return binary_op_pack4<binary_op_sub_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  564. if (op_type == Operation_MUL)
  565. return binary_op_pack4<binary_op_mul_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  566. if (op_type == Operation_DIV)
  567. return binary_op_pack4<binary_op_div_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  568. if (op_type == Operation_MAX)
  569. return binary_op_pack4<binary_op_max_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  570. if (op_type == Operation_MIN)
  571. return binary_op_pack4<binary_op_min_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  572. if (op_type == Operation_POW)
  573. return binary_op_pack4<binary_op_pow_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  574. if (op_type == Operation_RSUB)
  575. return binary_op_pack4<binary_op_rsub_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  576. if (op_type == Operation_RDIV)
  577. return binary_op_pack4<binary_op_rdiv_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  578. }
  579. #endif // __ARM_NEON
  580. return BinaryOp::forward(bottom_blobs, top_blobs, opt);
  581. }
  582. int BinaryOp_arm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
  583. {
  584. if (opt.use_bf16_storage)
  585. return forward_inplace_bf16s(bottom_top_blob, opt);
  586. #if __ARM_NEON
  587. int elempack = bottom_top_blob.elempack;
  588. if (elempack == 4)
  589. {
  590. if (op_type == Operation_ADD)
  591. return binary_op_scalar_inplace_pack4<binary_op_add_pack4>(bottom_top_blob, b, opt);
  592. if (op_type == Operation_SUB)
  593. return binary_op_scalar_inplace_pack4<binary_op_sub_pack4>(bottom_top_blob, b, opt);
  594. if (op_type == Operation_MUL)
  595. return binary_op_scalar_inplace_pack4<binary_op_mul_pack4>(bottom_top_blob, b, opt);
  596. if (op_type == Operation_DIV)
  597. return binary_op_scalar_inplace_pack4<binary_op_div_pack4>(bottom_top_blob, b, opt);
  598. if (op_type == Operation_MAX)
  599. return binary_op_scalar_inplace_pack4<binary_op_max_pack4>(bottom_top_blob, b, opt);
  600. if (op_type == Operation_MIN)
  601. return binary_op_scalar_inplace_pack4<binary_op_min_pack4>(bottom_top_blob, b, opt);
  602. if (op_type == Operation_POW)
  603. return binary_op_scalar_inplace_pack4<binary_op_pow_pack4>(bottom_top_blob, b, opt);
  604. if (op_type == Operation_RSUB)
  605. return binary_op_scalar_inplace_pack4<binary_op_rsub_pack4>(bottom_top_blob, b, opt);
  606. if (op_type == Operation_RDIV)
  607. return binary_op_scalar_inplace_pack4<binary_op_rdiv_pack4>(bottom_top_blob, b, opt);
  608. }
  609. #endif // __ARM_NEON
  610. return BinaryOp::forward_inplace(bottom_top_blob, opt);
  611. }
  612. #if __ARM_NEON
  613. template<typename Op>
  614. static int binary_op_pack4_bf16s(const Mat& a, const Mat& b, Mat& c, const Option& opt)
  615. {
  616. Op op;
  617. int w = a.w;
  618. int h = a.h;
  619. int channels = a.c;
  620. int size = w * h;
  621. size_t elemsize = a.elemsize;
  622. int elempack = a.elempack;
  623. int w1 = b.w;
  624. int h1 = b.h;
  625. int channels1 = b.c;
  626. int size1 = w1 * h1;
  627. size_t elemsize1 = b.elemsize;
  628. int elempack1 = b.elempack;
  629. if (a.dims == 3)
  630. {
  631. if (b.dims == 3)
  632. {
  633. if (w1 == 1 && h1 == 1 && channels1 == channels)
  634. {
  635. // special type 1
  636. c.create(w, h, channels, elemsize, elempack, opt.blob_allocator);
  637. if (c.empty())
  638. return -100;
  639. #pragma omp parallel for num_threads(opt.num_threads)
  640. for (int q=0; q<channels; q++)
  641. {
  642. const unsigned short* ptr = a.channel(q);
  643. unsigned short* outptr = c.channel(q);
  644. const unsigned short* b0 = b.channel(q);
  645. float32x4_t _b0 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(b0), 16));
  646. for (int i = 0; i < size; i++)
  647. {
  648. float32x4_t _p = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr), 16));
  649. float32x4_t _outp = op(_p, _b0);
  650. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  651. ptr += 4;
  652. outptr += 4;
  653. }
  654. }
  655. return 0;
  656. }
  657. if (w1 == w && h1 == h && channels1 == 1 && elempack1 == 1)
  658. {
  659. // special type 2
  660. c.create(w, h, channels, elemsize, elempack, opt.blob_allocator);
  661. if (c.empty())
  662. return -100;
  663. #pragma omp parallel for num_threads(opt.num_threads)
  664. for (int q = 0; q < channels; q++)
  665. {
  666. const unsigned short* ptr = a.channel(q);
  667. const unsigned short* ptr1 = b;
  668. unsigned short* outptr = c.channel(q);
  669. for (int i = 0; i < size; i++)
  670. {
  671. float32x4_t _p = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr), 16));
  672. float32x4_t _p1 = vdupq_n_f32(bfloat16_to_float32(*ptr1));
  673. float32x4_t _outp = op(_p, _p1);
  674. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  675. ptr += 4;
  676. ptr1 += 1;
  677. outptr += 4;
  678. }
  679. }
  680. return 0;
  681. }
  682. if (w == 1 && h == 1 && channels1 == channels)
  683. {
  684. // special type 3
  685. c.create(w1, h1, channels1, elemsize1, elempack1, opt.blob_allocator);
  686. if (c.empty())
  687. return -100;
  688. #pragma omp parallel for num_threads(opt.num_threads)
  689. for (int q=0; q<channels1; q++)
  690. {
  691. const unsigned short* a0 = a.channel(q);
  692. unsigned short* outptr = c.channel(q);
  693. const unsigned short* ptr1 = b.channel(q);
  694. float32x4_t _a0 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(a0), 16));
  695. for (int i = 0; i < size1; i++)
  696. {
  697. float32x4_t _p1 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr1), 16));
  698. float32x4_t _outp = op(_a0, _p1);
  699. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  700. ptr1 += 4;
  701. outptr += 4;
  702. }
  703. }
  704. return 0;
  705. }
  706. if (w1 == w && h1 == h && channels == 1 && elempack == 1)
  707. {
  708. // special type 4
  709. c.create(w1, h1, channels1, elemsize1, elempack1, opt.blob_allocator);
  710. if (c.empty())
  711. return -100;
  712. #pragma omp parallel for num_threads(opt.num_threads)
  713. for (int q = 0; q < channels1; q++)
  714. {
  715. const unsigned short* ptr = a;
  716. const unsigned short* ptr1 = b.channel(q);
  717. unsigned short* outptr = c.channel(q);
  718. for (int i = 0; i < size1; i++)
  719. {
  720. float32x4_t _p = vdupq_n_f32(bfloat16_to_float32(*ptr));
  721. float32x4_t _p1 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr1), 16));
  722. float32x4_t _outp = op(_p, _p1);
  723. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  724. ptr += 1;
  725. ptr1 += 4;
  726. outptr += 4;
  727. }
  728. }
  729. return 0;
  730. }
  731. // type 19
  732. c.create(w, h, channels, elemsize, elempack, opt.blob_allocator);
  733. if (c.empty())
  734. return -100;
  735. #pragma omp parallel for num_threads(opt.num_threads)
  736. for (int q=0; q<channels; q++)
  737. {
  738. const unsigned short* ptr = a.channel(q);
  739. const unsigned short* ptr1 = b.channel(q);
  740. unsigned short* outptr = c.channel(q);
  741. for (int i=0; i<size; i++)
  742. {
  743. float32x4_t _p = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr), 16));
  744. float32x4_t _p1 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr1), 16));
  745. float32x4_t _outp = op(_p, _p1);
  746. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  747. ptr += 4;
  748. ptr1 += 4;
  749. outptr += 4;
  750. }
  751. }
  752. return 0;
  753. }
  754. c.create(w, h, channels, elemsize, elempack, opt.blob_allocator);
  755. if (c.empty())
  756. return -100;
  757. if (b.dims == 2)
  758. {
  759. // type 18
  760. #pragma omp parallel for num_threads(opt.num_threads)
  761. for (int q=0; q<channels; q++)
  762. {
  763. const unsigned short* ptr = a.channel(q);
  764. const unsigned short* ptr1 = b.row<const unsigned short>(q);
  765. unsigned short* outptr = c.channel(q);
  766. for (int y=0; y<h; y++)
  767. {
  768. float32x4_t _b0 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr1), 16));
  769. for (int x=0; x<w; x++)
  770. {
  771. float32x4_t _p = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr), 16));
  772. float32x4_t _outp = op(_p, _b0);
  773. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  774. ptr += 4;
  775. outptr += 4;
  776. }
  777. ptr1 += 4;
  778. }
  779. }
  780. return 0;
  781. }
  782. if (b.dims == 1)
  783. {
  784. if (b.w == 1 && elempack1 == 1)
  785. {
  786. // type 16
  787. float32x4_t _b0 = vdupq_n_f32(bfloat16_to_float32(((const unsigned short*)b)[0]));
  788. #pragma omp parallel for num_threads(opt.num_threads)
  789. for (int q=0; q<channels; q++)
  790. {
  791. const unsigned short* ptr = a.channel(q);
  792. unsigned short* outptr = c.channel(q);
  793. for (int i=0; i<size; i++)
  794. {
  795. float32x4_t _p = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr), 16));
  796. float32x4_t _outp = op(_p, _b0);
  797. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  798. ptr += 4;
  799. outptr += 4;
  800. }
  801. }
  802. return 0;
  803. }
  804. // type 17
  805. #pragma omp parallel for num_threads(opt.num_threads)
  806. for (int q=0; q<channels; q++)
  807. {
  808. const unsigned short* ptr = a.channel(q);
  809. float32x4_t _b0 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16((const unsigned short*)b + q * 4), 16));
  810. unsigned short* outptr = c.channel(q);
  811. for (int i=0; i<size; i++)
  812. {
  813. float32x4_t _p = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr), 16));
  814. float32x4_t _outp = op(_p, _b0);
  815. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  816. ptr += 4;
  817. outptr += 4;
  818. }
  819. }
  820. return 0;
  821. }
  822. }
  823. else if (a.dims == 2)
  824. {
  825. if (b.dims == 3)
  826. {
  827. // type 14
  828. c.create(w1, h1, channels1, elemsize1, elempack1, opt.blob_allocator);
  829. if (c.empty())
  830. return -100;
  831. #pragma omp parallel for num_threads(opt.num_threads)
  832. for (int q=0; q<channels1; q++)
  833. {
  834. const unsigned short* ptr = a.row<const unsigned short>(q);
  835. const unsigned short* ptr1 = b.channel(q);
  836. unsigned short* outptr = c.channel(q);
  837. for (int y=0; y<h1; y++)
  838. {
  839. float32x4_t _a0 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr), 16));
  840. for (int x=0; x<w1; x++)
  841. {
  842. float32x4_t _p1 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr1), 16));
  843. float32x4_t _outp = op(_a0, _p1);
  844. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  845. ptr1 += 4;
  846. outptr += 4;
  847. }
  848. ptr += 4;
  849. }
  850. }
  851. return 0;
  852. }
  853. c.create(w, h, elemsize, elempack, opt.blob_allocator);
  854. if (c.empty())
  855. return -100;
  856. if (b.dims == 2)
  857. {
  858. // type 13
  859. const unsigned short* ptr = a;
  860. const unsigned short* ptr1 = b;
  861. unsigned short* outptr = c;
  862. for (int i=0; i<size; i++)
  863. {
  864. float32x4_t _p = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr), 16));
  865. float32x4_t _p1 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr1), 16));
  866. float32x4_t _outp = op(_p, _p1);
  867. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  868. ptr += 4;
  869. ptr1 += 4;
  870. outptr += 4;
  871. }
  872. return 0;
  873. }
  874. if (b.dims == 1)
  875. {
  876. c.create(w, h, elemsize, elempack, opt.blob_allocator);
  877. if (c.empty())
  878. return -100;
  879. if (b.w == 1 && elempack1 == 1)
  880. {
  881. // type 11
  882. float32x4_t _b0 = vdupq_n_f32(bfloat16_to_float32(((const unsigned short*)b)[0]));
  883. const unsigned short* ptr = a;
  884. unsigned short* outptr = c;
  885. for (int i=0; i<size; i++)
  886. {
  887. float32x4_t _p = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr), 16));
  888. float32x4_t _outp = op(_p, _b0);
  889. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  890. ptr += 4;
  891. outptr += 4;
  892. }
  893. return 0;
  894. }
  895. // type 12
  896. const unsigned short* ptr = a;
  897. const unsigned short* ptr1 = b;
  898. unsigned short* outptr = c;
  899. for (int y=0; y<h; y++)
  900. {
  901. float32x4_t _b0 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr1), 16));
  902. for (int x=0; x<w; x++)
  903. {
  904. float32x4_t _p = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr), 16));
  905. float32x4_t _outp = op(_p, _b0);
  906. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  907. ptr += 4;
  908. outptr += 4;
  909. }
  910. ptr1 += 4;
  911. }
  912. return 0;
  913. }
  914. }
  915. else if (a.dims == 1)
  916. {
  917. if (a.w == 1 && elempack == 1)
  918. {
  919. if (b.dims == 3)
  920. {
  921. // type 4
  922. c.create(w1, h1, channels1, elemsize1, elempack1, opt.blob_allocator);
  923. if (c.empty())
  924. return -100;
  925. float32x4_t _a0 = vdupq_n_f32(bfloat16_to_float32(((const unsigned short*)a)[0]));
  926. #pragma omp parallel for num_threads(opt.num_threads)
  927. for (int q=0; q<channels1; q++)
  928. {
  929. const unsigned short* ptr1 = b.channel(q);
  930. unsigned short* outptr = c.channel(q);
  931. for (int i=0; i<size1; i++)
  932. {
  933. float32x4_t _p1 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr1), 16));
  934. float32x4_t _outp = op(_a0, _p1);
  935. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  936. ptr1 += 4;
  937. outptr += 4;
  938. }
  939. }
  940. return 0;
  941. }
  942. if (b.dims == 2)
  943. {
  944. // type 3
  945. c.create(w1, h1, elemsize1, elempack1, opt.blob_allocator);
  946. if (c.empty())
  947. return -100;
  948. float32x4_t _a0 = vdupq_n_f32(bfloat16_to_float32(((const unsigned short*)a)[0]));
  949. const unsigned short* ptr1 = b;
  950. unsigned short* outptr = c;
  951. for (int i=0; i<size1; i++)
  952. {
  953. float32x4_t _p1 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr1), 16));
  954. float32x4_t _outp = op(_a0, _p1);
  955. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  956. ptr1 += 4;
  957. outptr += 4;
  958. }
  959. return 0;
  960. }
  961. if (b.dims == 1)
  962. {
  963. // type 2
  964. c.create(w1, elemsize1, elempack1, opt.blob_allocator);
  965. if (c.empty())
  966. return -100;
  967. float32x4_t _a0 = vdupq_n_f32(bfloat16_to_float32(((const unsigned short*)a)[0]));
  968. const unsigned short* ptr1 = b;
  969. unsigned short* outptr = c;
  970. for (int i=0; i<w1; i++)
  971. {
  972. float32x4_t _p1 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr1), 16));
  973. float32x4_t _outp = op(_a0, _p1);
  974. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  975. ptr1 += 4;
  976. outptr += 4;
  977. }
  978. return 0;
  979. }
  980. }
  981. if (b.dims == 3)
  982. {
  983. // type 9
  984. c.create(w1, h1, channels1, elemsize1, elempack1, opt.blob_allocator);
  985. if (c.empty())
  986. return -100;
  987. #pragma omp parallel for num_threads(opt.num_threads)
  988. for (int q=0; q<channels1; q++)
  989. {
  990. float32x4_t _a0 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16((const unsigned short*)a + q * 4), 16));
  991. const unsigned short* ptr1 = b.channel(q);
  992. unsigned short* outptr = c.channel(q);
  993. for (int i=0; i<size1; i++)
  994. {
  995. float32x4_t _p1 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr1), 16));
  996. float32x4_t _outp = op(_a0, _p1);
  997. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  998. ptr1 += 4;
  999. outptr += 4;
  1000. }
  1001. }
  1002. return 0;
  1003. }
  1004. if (b.dims == 2)
  1005. {
  1006. // type 8
  1007. c.create(w1, h1, elemsize1, elempack1, opt.blob_allocator);
  1008. if (c.empty())
  1009. return -100;
  1010. const unsigned short* ptr = a;
  1011. const unsigned short* ptr1 = b;
  1012. unsigned short* outptr = c;
  1013. for (int y=0; y<h1; y++)
  1014. {
  1015. float32x4_t _a0 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr), 16));
  1016. for (int x=0; x<w1; x++)
  1017. {
  1018. float32x4_t _p1 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr1), 16));
  1019. float32x4_t _outp = op(_a0, _p1);
  1020. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  1021. ptr1 += 4;
  1022. outptr += 4;
  1023. }
  1024. ptr += 4;
  1025. }
  1026. return 0;
  1027. }
  1028. if (b.dims == 1)
  1029. {
  1030. c.create(w, elemsize, elempack, opt.blob_allocator);
  1031. if (c.empty())
  1032. return -100;
  1033. if (b.w == 1 && elempack1 == 1)
  1034. {
  1035. // type 6
  1036. float32x4_t _b0 = vdupq_n_f32(bfloat16_to_float32(((const unsigned short*)b)[0]));
  1037. const unsigned short* ptr = a;
  1038. unsigned short* outptr = c;
  1039. for (int i=0; i<w; i++)
  1040. {
  1041. float32x4_t _p = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr), 16));
  1042. float32x4_t _outp = op(_p, _b0);
  1043. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  1044. ptr += 4;
  1045. outptr += 4;
  1046. }
  1047. return 0;
  1048. }
  1049. // type 7
  1050. const unsigned short* ptr = a;
  1051. const unsigned short* ptr1 = b;
  1052. unsigned short* outptr = c;
  1053. for (int i=0; i<w; i++)
  1054. {
  1055. float32x4_t _p = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr), 16));
  1056. float32x4_t _p1 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr1), 16));
  1057. float32x4_t _outp = op(_p, _p1);
  1058. vst1_u16(outptr, vshrn_n_u32(vreinterpretq_u32_f32(_outp), 16));
  1059. ptr += 4;
  1060. ptr1 += 4;
  1061. outptr += 4;
  1062. }
  1063. }
  1064. }
  1065. return 0;
  1066. }
  1067. template<typename Op>
  1068. static int binary_op_scalar_inplace_pack4_bf16s(Mat& a, float b, const Option& opt)
  1069. {
  1070. Op op;
  1071. int w = a.w;
  1072. int h = a.h;
  1073. int channels = a.c;
  1074. int size = w * h;
  1075. float32x4_t _b = vdupq_n_f32(b);
  1076. #pragma omp parallel for num_threads(opt.num_threads)
  1077. for (int q=0; q<channels; q++)
  1078. {
  1079. unsigned short* ptr = a.channel(q);
  1080. for (int i=0; i<size; i++)
  1081. {
  1082. float32x4_t _p = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(ptr), 16));
  1083. _p = op(_p, _b);
  1084. vst1_u16(ptr, vshrn_n_u32(vreinterpretq_u32_f32(_p), 16));
  1085. ptr += 4;
  1086. }
  1087. }
  1088. return 0;
  1089. }
  1090. #endif // __ARM_NEON
  1091. template<typename Op>
  1092. static int binary_op_bf16s(const Mat& a, const Mat& b, Mat& c, const Option& opt)
  1093. {
  1094. Op op;
  1095. int w = a.w;
  1096. int h = a.h;
  1097. int channels = a.c;
  1098. int size = w * h;
  1099. size_t elemsize = a.elemsize;
  1100. int w1 = b.w;
  1101. int h1 = b.h;
  1102. int channels1 = b.c;
  1103. int size1 = w1 * h1;
  1104. if (a.dims == 3)
  1105. {
  1106. if (b.dims == 3)
  1107. {
  1108. if (w1 == 1 && h1 == 1 && channels1 == channels)
  1109. {
  1110. // special type 1
  1111. c.create(w, h, channels, elemsize, opt.blob_allocator);
  1112. if (c.empty())
  1113. return -100;
  1114. #pragma omp parallel for num_threads(opt.num_threads)
  1115. for (int q = 0; q < channels; q++)
  1116. {
  1117. const unsigned short* ptr = a.channel(q);
  1118. const unsigned short* b0 = b.channel(q);
  1119. unsigned short* outptr = c.channel(q);
  1120. for (int i = 0; i < size; i++)
  1121. {
  1122. outptr[i] = float32_to_bfloat16(op(bfloat16_to_float32(ptr[i]), bfloat16_to_float32(b0[0])));
  1123. }
  1124. }
  1125. return 0;
  1126. }
  1127. if (w1 == w && h1 == h && channels1 == 1)
  1128. {
  1129. // special type 2
  1130. c.create(w, h, channels, elemsize, opt.blob_allocator);
  1131. if (c.empty())
  1132. return -100;
  1133. #pragma omp parallel for num_threads(opt.num_threads)
  1134. for (int q = 0; q < channels; q++)
  1135. {
  1136. const unsigned short* ptr = a.channel(q);
  1137. const unsigned short* ptr1 = b;
  1138. unsigned short* outptr = c.channel(q);
  1139. for (int i = 0; i < size; i++)
  1140. {
  1141. outptr[i] = float32_to_bfloat16(op(bfloat16_to_float32(ptr[i]), bfloat16_to_float32(ptr1[i])));
  1142. }
  1143. }
  1144. return 0;
  1145. }
  1146. if (w == 1 && h == 1 && channels1 == channels)
  1147. {
  1148. // special type 3
  1149. c.create(w1, h1, channels1, elemsize, opt.blob_allocator);
  1150. if (c.empty())
  1151. return -100;
  1152. #pragma omp parallel for num_threads(opt.num_threads)
  1153. for (int q = 0; q < channels1; q++)
  1154. {
  1155. const unsigned short* a0 = a.channel(q);
  1156. const unsigned short* ptr1 = b.channel(q);
  1157. unsigned short* outptr = c.channel(q);
  1158. for (int i = 0; i < size1; i++)
  1159. {
  1160. outptr[i] = float32_to_bfloat16(op(bfloat16_to_float32(a0[0]), bfloat16_to_float32(ptr1[i])));
  1161. }
  1162. }
  1163. return 0;
  1164. }
  1165. if (w1 == w && h1 == h && channels == 1)
  1166. {
  1167. // special type 4
  1168. c.create(w1, h1, channels1, elemsize, opt.blob_allocator);
  1169. if (c.empty())
  1170. return -100;
  1171. #pragma omp parallel for num_threads(opt.num_threads)
  1172. for (int q = 0; q < channels1; q++)
  1173. {
  1174. const unsigned short* ptr = a;
  1175. const unsigned short* ptr1 = b.channel(q);
  1176. unsigned short* outptr = c.channel(q);
  1177. for (int i = 0; i < size1; i++)
  1178. {
  1179. outptr[i] = float32_to_bfloat16(op(bfloat16_to_float32(ptr[i]), bfloat16_to_float32(ptr1[i])));
  1180. }
  1181. }
  1182. return 0;
  1183. }
  1184. // type 19
  1185. c.create(w, h, channels, elemsize, opt.blob_allocator);
  1186. if (c.empty())
  1187. return -100;
  1188. #pragma omp parallel for num_threads(opt.num_threads)
  1189. for (int q=0; q<channels; q++)
  1190. {
  1191. const unsigned short* ptr = a.channel(q);
  1192. const unsigned short* ptr1 = b.channel(q);
  1193. unsigned short* outptr = c.channel(q);
  1194. for (int i=0; i<size; i++)
  1195. {
  1196. outptr[i] = float32_to_bfloat16(op(bfloat16_to_float32(ptr[i]), bfloat16_to_float32(ptr1[i])));
  1197. }
  1198. }
  1199. return 0;
  1200. }
  1201. c.create(w, h, channels, elemsize, opt.blob_allocator);
  1202. if (c.empty())
  1203. return -100;
  1204. if (b.dims == 2)
  1205. {
  1206. // type 18
  1207. #pragma omp parallel for num_threads(opt.num_threads)
  1208. for (int q=0; q<channels; q++)
  1209. {
  1210. const unsigned short* ptr = a.channel(q);
  1211. const unsigned short* ptr1 = b.row<const unsigned short>(q);
  1212. unsigned short* outptr = c.channel(q);
  1213. for (int y=0; y<h; y++)
  1214. {
  1215. const float b0 = bfloat16_to_float32(ptr1[y]);
  1216. for (int x=0; x<w; x++)
  1217. {
  1218. outptr[x] = float32_to_bfloat16(op(bfloat16_to_float32(ptr[x]), b0));
  1219. }
  1220. ptr += w;
  1221. outptr += w;
  1222. }
  1223. }
  1224. return 0;
  1225. }
  1226. if (b.dims == 1)
  1227. {
  1228. if (b.w == 1)
  1229. {
  1230. // type 16
  1231. const float b0 = bfloat16_to_float32(((const unsigned short*)b)[0]);
  1232. #pragma omp parallel for num_threads(opt.num_threads)
  1233. for (int q=0; q<channels; q++)
  1234. {
  1235. const unsigned short* ptr = a.channel(q);
  1236. unsigned short* outptr = c.channel(q);
  1237. for (int i=0; i<size; i++)
  1238. {
  1239. outptr[i] = float32_to_bfloat16(op(bfloat16_to_float32(ptr[i]), b0));
  1240. }
  1241. }
  1242. return 0;
  1243. }
  1244. // type 17
  1245. #pragma omp parallel for num_threads(opt.num_threads)
  1246. for (int q=0; q<channels; q++)
  1247. {
  1248. const unsigned short* ptr = a.channel(q);
  1249. const float b0 = bfloat16_to_float32(((const unsigned short*)b)[q]);
  1250. unsigned short* outptr = c.channel(q);
  1251. for (int i=0; i<size; i++)
  1252. {
  1253. outptr[i] = float32_to_bfloat16(op(bfloat16_to_float32(ptr[i]), b0));
  1254. }
  1255. }
  1256. return 0;
  1257. }
  1258. }
  1259. else if (a.dims == 2)
  1260. {
  1261. if (b.dims == 3)
  1262. {
  1263. // type 14
  1264. c.create(w1, h1, channels1, elemsize, opt.blob_allocator);
  1265. if (c.empty())
  1266. return -100;
  1267. #pragma omp parallel for num_threads(opt.num_threads)
  1268. for (int q=0; q<channels1; q++)
  1269. {
  1270. const unsigned short* ptr = a.row<const unsigned short>(q);
  1271. const unsigned short* ptr1 = b.channel(q);
  1272. unsigned short* outptr = c.channel(q);
  1273. for (int y=0; y<h1; y++)
  1274. {
  1275. const float a0 = bfloat16_to_float32(ptr[y]);
  1276. for (int x=0; x<w1; x++)
  1277. {
  1278. outptr[x] = float32_to_bfloat16(op(a0, bfloat16_to_float32(ptr1[x])));
  1279. }
  1280. ptr1 += w1;
  1281. outptr += w1;
  1282. }
  1283. }
  1284. return 0;
  1285. }
  1286. c.create(w, h, elemsize, opt.blob_allocator);
  1287. if (c.empty())
  1288. return -100;
  1289. if (b.dims == 2)
  1290. {
  1291. // type 13
  1292. const unsigned short* ptr = a;
  1293. const unsigned short* ptr1 = b;
  1294. unsigned short* outptr = c;
  1295. for (int i=0; i<size; i++)
  1296. {
  1297. outptr[i] = float32_to_bfloat16(op(bfloat16_to_float32(ptr[i]), bfloat16_to_float32(ptr1[i])));
  1298. }
  1299. return 0;
  1300. }
  1301. if (b.dims == 1)
  1302. {
  1303. c.create(w, h, elemsize, opt.blob_allocator);
  1304. if (c.empty())
  1305. return -100;
  1306. if (b.w == 1)
  1307. {
  1308. // type 11
  1309. const float b0 = bfloat16_to_float32(((const unsigned short*)b)[0]);
  1310. const unsigned short* ptr = a;
  1311. unsigned short* outptr = c;
  1312. for (int i=0; i<size; i++)
  1313. {
  1314. outptr[i] = float32_to_bfloat16(op(bfloat16_to_float32(ptr[i]), b0));
  1315. }
  1316. return 0;
  1317. }
  1318. // type 12
  1319. const unsigned short* ptr = a;
  1320. unsigned short* outptr = c;
  1321. for (int y=0; y<h; y++)
  1322. {
  1323. const float b0 = bfloat16_to_float32(((const unsigned short*)b)[y]);
  1324. for (int x=0; x<w; x++)
  1325. {
  1326. outptr[x] = float32_to_bfloat16(op(bfloat16_to_float32(ptr[x]), b0));
  1327. }
  1328. ptr += w;
  1329. outptr += w;
  1330. }
  1331. return 0;
  1332. }
  1333. }
  1334. else if (a.dims == 1)
  1335. {
  1336. if (a.w == 1)
  1337. {
  1338. if (b.dims == 3)
  1339. {
  1340. // type 4
  1341. c.create(w1, h1, channels1, elemsize, opt.blob_allocator);
  1342. if (c.empty())
  1343. return -100;
  1344. const float a0 = bfloat16_to_float32(((const unsigned short*)a)[0]);
  1345. #pragma omp parallel for num_threads(opt.num_threads)
  1346. for (int q=0; q<channels1; q++)
  1347. {
  1348. const unsigned short* ptr1 = b.channel(q);
  1349. unsigned short* outptr = c.channel(q);
  1350. for (int i=0; i<size1; i++)
  1351. {
  1352. outptr[i] = float32_to_bfloat16(op(a0, bfloat16_to_float32(ptr1[i])));
  1353. }
  1354. }
  1355. return 0;
  1356. }
  1357. if (b.dims == 2)
  1358. {
  1359. // type 3
  1360. c.create(w1, h1, elemsize, opt.blob_allocator);
  1361. if (c.empty())
  1362. return -100;
  1363. const float a0 = bfloat16_to_float32(((const unsigned short*)a)[0]);
  1364. const unsigned short* ptr1 = b;
  1365. unsigned short* outptr = c;
  1366. for (int i=0; i<size1; i++)
  1367. {
  1368. outptr[i] = float32_to_bfloat16(op(a0, bfloat16_to_float32(ptr1[i])));
  1369. }
  1370. return 0;
  1371. }
  1372. if (b.dims == 1)
  1373. {
  1374. // type 2
  1375. c.create(w1, elemsize, opt.blob_allocator);
  1376. if (c.empty())
  1377. return -100;
  1378. const float a0 = bfloat16_to_float32(((const unsigned short*)a)[0]);
  1379. const unsigned short* ptr1 = b;
  1380. unsigned short* outptr = c;
  1381. for (int i=0; i<w1; i++)
  1382. {
  1383. outptr[i] = float32_to_bfloat16(op(a0, bfloat16_to_float32(ptr1[i])));
  1384. }
  1385. return 0;
  1386. }
  1387. }
  1388. if (b.dims == 3)
  1389. {
  1390. // type 9
  1391. c.create(w1, h1, channels1, elemsize, opt.blob_allocator);
  1392. if (c.empty())
  1393. return -100;
  1394. #pragma omp parallel for num_threads(opt.num_threads)
  1395. for (int q=0; q<channels1; q++)
  1396. {
  1397. const float a0 = bfloat16_to_float32(((const unsigned short*)a)[q]);
  1398. const unsigned short* ptr1 = b.channel(q);
  1399. unsigned short* outptr = c.channel(q);
  1400. for (int i=0; i<size1; i++)
  1401. {
  1402. outptr[i] = float32_to_bfloat16(op(a0, bfloat16_to_float32(ptr1[i])));
  1403. }
  1404. }
  1405. return 0;
  1406. }
  1407. if (b.dims == 2)
  1408. {
  1409. // type 8
  1410. c.create(w1, h1, elemsize, opt.blob_allocator);
  1411. if (c.empty())
  1412. return -100;
  1413. const unsigned short* ptr1 = b;
  1414. unsigned short* outptr = c;
  1415. for (int y=0; y<h1; y++)
  1416. {
  1417. const float a0 = bfloat16_to_float32(((const unsigned short*)a)[y]);
  1418. for (int x=0; x<w1; x++)
  1419. {
  1420. outptr[x] = float32_to_bfloat16(op(a0, bfloat16_to_float32(ptr1[x])));
  1421. }
  1422. ptr1 += w1;
  1423. outptr += w1;
  1424. }
  1425. return 0;
  1426. }
  1427. if (b.dims == 1)
  1428. {
  1429. c.create(w, elemsize, opt.blob_allocator);
  1430. if (c.empty())
  1431. return -100;
  1432. if (b.w == 1)
  1433. {
  1434. // type 6
  1435. const float b0 = bfloat16_to_float32(((const unsigned short*)b)[0]);
  1436. const unsigned short* ptr = a;
  1437. unsigned short* outptr = c;
  1438. for (int i=0; i<w; i++)
  1439. {
  1440. outptr[i] = float32_to_bfloat16(op(bfloat16_to_float32(ptr[i]), b0));
  1441. }
  1442. return 0;
  1443. }
  1444. // type 7
  1445. const unsigned short* ptr = a;
  1446. const unsigned short* ptr1 = b;
  1447. unsigned short* outptr = c;
  1448. for (int i=0; i<w; i++)
  1449. {
  1450. outptr[i] = float32_to_bfloat16(op(bfloat16_to_float32(ptr[i]), bfloat16_to_float32(ptr1[i])));
  1451. }
  1452. }
  1453. }
  1454. return 0;
  1455. }
  1456. template<typename Op>
  1457. static int binary_op_scalar_inplace_bf16s(Mat& a, float b, const Option& opt)
  1458. {
  1459. Op op;
  1460. int w = a.w;
  1461. int h = a.h;
  1462. int channels = a.c;
  1463. int size = w * h;
  1464. #pragma omp parallel for num_threads(opt.num_threads)
  1465. for (int q=0; q<channels; q++)
  1466. {
  1467. unsigned short* ptr = a.channel(q);
  1468. for (int i=0; i<size; i++)
  1469. {
  1470. ptr[i] = float32_to_bfloat16(op(bfloat16_to_float32(ptr[i]), b));
  1471. }
  1472. }
  1473. return 0;
  1474. }
  1475. struct binary_op_add {
  1476. float operator() (const float& x, const float& y) const { return x + y; }
  1477. };
  1478. struct binary_op_sub {
  1479. float operator() (const float& x, const float& y) const { return x - y; }
  1480. };
  1481. struct binary_op_mul {
  1482. float operator() (const float& x, const float& y) const { return x * y; }
  1483. };
  1484. struct binary_op_div {
  1485. float operator() (const float& x, const float& y) const { return x / y; }
  1486. };
  1487. struct binary_op_max {
  1488. float operator() (const float& x, const float& y) const { return std::max(x, y); }
  1489. };
  1490. struct binary_op_min {
  1491. float operator() (const float& x, const float& y) const { return std::min(x, y); }
  1492. };
  1493. struct binary_op_pow {
  1494. float operator() (const float& x, const float& y) const { return (float)pow(x, y); }
  1495. };
  1496. struct binary_op_rsub {
  1497. float operator() (const float& x, const float& y) const { return y - x; }
  1498. };
  1499. struct binary_op_rdiv {
  1500. float operator() (const float& x, const float& y) const { return y / x; }
  1501. };
  1502. int BinaryOp_arm::forward_bf16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  1503. {
  1504. const Mat& bottom_blob = bottom_blobs[0];
  1505. const Mat& bottom_blob1 = bottom_blobs[1];
  1506. Mat& top_blob = top_blobs[0];
  1507. int elempack = bottom_blob.elempack;
  1508. int elempack1 = bottom_blob1.elempack;
  1509. #if __ARM_NEON
  1510. if (elempack == 4 || elempack1 == 4)
  1511. {
  1512. if (op_type == Operation_ADD)
  1513. return binary_op_pack4_bf16s<binary_op_add_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  1514. if (op_type == Operation_SUB)
  1515. return binary_op_pack4_bf16s<binary_op_sub_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  1516. if (op_type == Operation_MUL)
  1517. return binary_op_pack4_bf16s<binary_op_mul_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  1518. if (op_type == Operation_DIV)
  1519. return binary_op_pack4_bf16s<binary_op_div_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  1520. if (op_type == Operation_MAX)
  1521. return binary_op_pack4_bf16s<binary_op_max_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  1522. if (op_type == Operation_MIN)
  1523. return binary_op_pack4_bf16s<binary_op_min_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  1524. if (op_type == Operation_POW)
  1525. return binary_op_pack4_bf16s<binary_op_pow_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  1526. if (op_type == Operation_RSUB)
  1527. return binary_op_pack4_bf16s<binary_op_rsub_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  1528. if (op_type == Operation_RDIV)
  1529. return binary_op_pack4_bf16s<binary_op_rdiv_pack4>(bottom_blob, bottom_blob1, top_blob, opt);
  1530. }
  1531. #endif // __ARM_NEON
  1532. if (elempack == 1 && elempack1 == 1)
  1533. {
  1534. if (op_type == Operation_ADD)
  1535. return binary_op_bf16s<binary_op_add>(bottom_blob, bottom_blob1, top_blob, opt);
  1536. if (op_type == Operation_SUB)
  1537. return binary_op_bf16s<binary_op_sub>(bottom_blob, bottom_blob1, top_blob, opt);
  1538. if (op_type == Operation_MUL)
  1539. return binary_op_bf16s<binary_op_mul>(bottom_blob, bottom_blob1, top_blob, opt);
  1540. if (op_type == Operation_DIV)
  1541. return binary_op_bf16s<binary_op_div>(bottom_blob, bottom_blob1, top_blob, opt);
  1542. if (op_type == Operation_MAX)
  1543. return binary_op_bf16s<binary_op_max>(bottom_blob, bottom_blob1, top_blob, opt);
  1544. if (op_type == Operation_MIN)
  1545. return binary_op_bf16s<binary_op_min>(bottom_blob, bottom_blob1, top_blob, opt);
  1546. if (op_type == Operation_POW)
  1547. return binary_op_bf16s<binary_op_pow>(bottom_blob, bottom_blob1, top_blob, opt);
  1548. if (op_type == Operation_RSUB)
  1549. return binary_op_bf16s<binary_op_rsub>(bottom_blob, bottom_blob1, top_blob, opt);
  1550. if (op_type == Operation_RDIV)
  1551. return binary_op_bf16s<binary_op_rdiv>(bottom_blob, bottom_blob1, top_blob, opt);
  1552. }
  1553. return 0;
  1554. }
  1555. int BinaryOp_arm::forward_inplace_bf16s(Mat& bottom_top_blob, const Option& opt) const
  1556. {
  1557. int elempack = bottom_top_blob.elempack;
  1558. #if __ARM_NEON
  1559. if (elempack == 4)
  1560. {
  1561. if (op_type == Operation_ADD)
  1562. return binary_op_scalar_inplace_pack4_bf16s<binary_op_add_pack4>(bottom_top_blob, b, opt);
  1563. if (op_type == Operation_SUB)
  1564. return binary_op_scalar_inplace_pack4_bf16s<binary_op_sub_pack4>(bottom_top_blob, b, opt);
  1565. if (op_type == Operation_MUL)
  1566. return binary_op_scalar_inplace_pack4_bf16s<binary_op_mul_pack4>(bottom_top_blob, b, opt);
  1567. if (op_type == Operation_DIV)
  1568. return binary_op_scalar_inplace_pack4_bf16s<binary_op_div_pack4>(bottom_top_blob, b, opt);
  1569. if (op_type == Operation_MAX)
  1570. return binary_op_scalar_inplace_pack4_bf16s<binary_op_max_pack4>(bottom_top_blob, b, opt);
  1571. if (op_type == Operation_MIN)
  1572. return binary_op_scalar_inplace_pack4_bf16s<binary_op_min_pack4>(bottom_top_blob, b, opt);
  1573. if (op_type == Operation_POW)
  1574. return binary_op_scalar_inplace_pack4_bf16s<binary_op_pow_pack4>(bottom_top_blob, b, opt);
  1575. if (op_type == Operation_RSUB)
  1576. return binary_op_scalar_inplace_pack4_bf16s<binary_op_rsub_pack4>(bottom_top_blob, b, opt);
  1577. if (op_type == Operation_RDIV)
  1578. return binary_op_scalar_inplace_pack4_bf16s<binary_op_rdiv_pack4>(bottom_top_blob, b, opt);
  1579. }
  1580. #endif // __ARM_NEON
  1581. if (elempack == 1)
  1582. {
  1583. if (op_type == Operation_ADD)
  1584. return binary_op_scalar_inplace_bf16s<binary_op_add>(bottom_top_blob, b, opt);
  1585. if (op_type == Operation_SUB)
  1586. return binary_op_scalar_inplace_bf16s<binary_op_sub>(bottom_top_blob, b, opt);
  1587. if (op_type == Operation_MUL)
  1588. return binary_op_scalar_inplace_bf16s<binary_op_mul>(bottom_top_blob, b, opt);
  1589. if (op_type == Operation_DIV)
  1590. return binary_op_scalar_inplace_bf16s<binary_op_div>(bottom_top_blob, b, opt);
  1591. if (op_type == Operation_MAX)
  1592. return binary_op_scalar_inplace_bf16s<binary_op_max>(bottom_top_blob, b, opt);
  1593. if (op_type == Operation_MIN)
  1594. return binary_op_scalar_inplace_bf16s<binary_op_min>(bottom_top_blob, b, opt);
  1595. if (op_type == Operation_POW)
  1596. return binary_op_scalar_inplace_bf16s<binary_op_pow>(bottom_top_blob, b, opt);
  1597. if (op_type == Operation_RSUB)
  1598. return binary_op_scalar_inplace_bf16s<binary_op_rsub>(bottom_top_blob, b, opt);
  1599. if (op_type == Operation_RDIV)
  1600. return binary_op_scalar_inplace_bf16s<binary_op_rdiv>(bottom_top_blob, b, opt);
  1601. }
  1602. return 0;
  1603. }
  1604. } // namespace ncnn