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.cpp 16 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
8 years ago
6 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
8 years ago
6 years ago
8 years ago
8 years ago
6 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
8 years ago
6 years ago
8 years ago
8 years ago
6 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "binaryop.h"
  15. #include <math.h>
  16. namespace ncnn {
  17. BinaryOp::BinaryOp()
  18. {
  19. one_blob_only = false;
  20. support_inplace = false;
  21. }
  22. int BinaryOp::load_param(const ParamDict& pd)
  23. {
  24. op_type = pd.get(0, 0);
  25. with_scalar = pd.get(1, 0);
  26. b = pd.get(2, 0.f);
  27. if (with_scalar != 0)
  28. {
  29. one_blob_only = true;
  30. support_inplace = true;
  31. }
  32. return 0;
  33. }
  34. // broadcasting rule
  35. // https://github.com/Tencent/ncnn/wiki/binaryop-broadcasting
  36. template<typename Op>
  37. static int binary_op(const Mat& a, const Mat& b, Mat& c, const Option& opt)
  38. {
  39. Op op;
  40. int w = a.w;
  41. int h = a.h;
  42. int channels = a.c;
  43. int size = w * h;
  44. size_t elemsize = a.elemsize;
  45. int w1 = b.w;
  46. int h1 = b.h;
  47. int channels1 = b.c;
  48. int size1 = w1 * h1;
  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, 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. for (int i = 0; i < size; i++)
  66. {
  67. outptr[i] = op(ptr[i], b0[0]);
  68. }
  69. }
  70. return 0;
  71. }
  72. if (w1 == w && h1 == h && channels1 == 1)
  73. {
  74. // special type 2
  75. c.create(w, h, channels, elemsize, opt.blob_allocator);
  76. if (c.empty())
  77. return -100;
  78. #pragma omp parallel for num_threads(opt.num_threads)
  79. for (int q = 0; q < channels; q++)
  80. {
  81. const float* ptr = a.channel(q);
  82. const float* ptr1 = b;
  83. float* outptr = c.channel(q);
  84. for (int i = 0; i < size; i++)
  85. {
  86. outptr[i] = op(ptr[i], ptr1[i]);
  87. }
  88. }
  89. return 0;
  90. }
  91. if (w == 1 && h == 1 && channels1 == channels)
  92. {
  93. // special type 3
  94. c.create(w1, h1, channels1, elemsize, opt.blob_allocator);
  95. if (c.empty())
  96. return -100;
  97. #pragma omp parallel for num_threads(opt.num_threads)
  98. for (int q = 0; q < channels1; q++)
  99. {
  100. const float* a0 = a.channel(q);
  101. const float* ptr1 = b.channel(q);
  102. float* outptr = c.channel(q);
  103. for (int i = 0; i < size1; i++)
  104. {
  105. outptr[i] = op(a0[0], ptr1[i]);
  106. }
  107. }
  108. return 0;
  109. }
  110. if (w1 == w && h1 == h && channels == 1)
  111. {
  112. // special type 4
  113. c.create(w1, h1, channels1, elemsize, opt.blob_allocator);
  114. if (c.empty())
  115. return -100;
  116. #pragma omp parallel for num_threads(opt.num_threads)
  117. for (int q = 0; q < channels1; q++)
  118. {
  119. const float* ptr = a;
  120. const float* ptr1 = b.channel(q);
  121. float* outptr = c.channel(q);
  122. for (int i = 0; i < size1; i++)
  123. {
  124. outptr[i] = op(ptr[i], ptr1[i]);
  125. }
  126. }
  127. return 0;
  128. }
  129. // type 19
  130. c.create(w, h, channels, elemsize, opt.blob_allocator);
  131. if (c.empty())
  132. return -100;
  133. #pragma omp parallel for num_threads(opt.num_threads)
  134. for (int q = 0; q < channels; q++)
  135. {
  136. const float* ptr = a.channel(q);
  137. const float* ptr1 = b.channel(q);
  138. float* outptr = c.channel(q);
  139. for (int i = 0; i < size; i++)
  140. {
  141. outptr[i] = op(ptr[i], ptr1[i]);
  142. }
  143. }
  144. return 0;
  145. }
  146. c.create(w, h, channels, elemsize, opt.blob_allocator);
  147. if (c.empty())
  148. return -100;
  149. if (b.dims == 2)
  150. {
  151. // type 18
  152. #pragma omp parallel for num_threads(opt.num_threads)
  153. for (int q = 0; q < channels; q++)
  154. {
  155. const float* ptr = a.channel(q);
  156. const float* ptr1 = b.row(q);
  157. float* outptr = c.channel(q);
  158. for (int y = 0; y < h; y++)
  159. {
  160. const float b0 = ptr1[y];
  161. for (int x = 0; x < w; x++)
  162. {
  163. outptr[x] = op(ptr[x], b0);
  164. }
  165. ptr += w;
  166. outptr += w;
  167. }
  168. }
  169. return 0;
  170. }
  171. if (b.dims == 1)
  172. {
  173. if (b.w == 1)
  174. {
  175. // type 16
  176. const float b0 = b[0];
  177. #pragma omp parallel for num_threads(opt.num_threads)
  178. for (int q = 0; q < channels; q++)
  179. {
  180. const float* ptr = a.channel(q);
  181. float* outptr = c.channel(q);
  182. for (int i = 0; i < size; i++)
  183. {
  184. outptr[i] = op(ptr[i], b0);
  185. }
  186. }
  187. return 0;
  188. }
  189. // type 17
  190. #pragma omp parallel for num_threads(opt.num_threads)
  191. for (int q = 0; q < channels; q++)
  192. {
  193. const float* ptr = a.channel(q);
  194. const float b0 = b[q];
  195. float* outptr = c.channel(q);
  196. for (int i = 0; i < size; i++)
  197. {
  198. outptr[i] = op(ptr[i], b0);
  199. }
  200. }
  201. return 0;
  202. }
  203. }
  204. else if (a.dims == 2)
  205. {
  206. if (b.dims == 3)
  207. {
  208. // type 14
  209. c.create(w1, h1, channels1, elemsize, opt.blob_allocator);
  210. if (c.empty())
  211. return -100;
  212. #pragma omp parallel for num_threads(opt.num_threads)
  213. for (int q = 0; q < channels1; q++)
  214. {
  215. const float* ptr = a.row(q);
  216. const float* ptr1 = b.channel(q);
  217. float* outptr = c.channel(q);
  218. for (int y = 0; y < h1; y++)
  219. {
  220. const float a0 = ptr[y];
  221. for (int x = 0; x < w1; x++)
  222. {
  223. outptr[x] = op(a0, ptr1[x]);
  224. }
  225. ptr1 += w1;
  226. outptr += w1;
  227. }
  228. }
  229. return 0;
  230. }
  231. c.create(w, h, elemsize, opt.blob_allocator);
  232. if (c.empty())
  233. return -100;
  234. if (b.dims == 2)
  235. {
  236. // type 13
  237. for (int i = 0; i < size; i++)
  238. {
  239. c[i] = op(a[i], b[i]);
  240. }
  241. return 0;
  242. }
  243. if (b.dims == 1)
  244. {
  245. c.create(w, h, elemsize, opt.blob_allocator);
  246. if (c.empty())
  247. return -100;
  248. if (b.w == 1)
  249. {
  250. // type 11
  251. const float b0 = b[0];
  252. for (int i = 0; i < size; i++)
  253. {
  254. c[i] = op(a[i], b0);
  255. }
  256. return 0;
  257. }
  258. // type 12
  259. const float* ptr = a;
  260. float* outptr = c;
  261. for (int y = 0; y < h; y++)
  262. {
  263. const float b0 = b[y];
  264. for (int x = 0; x < w; x++)
  265. {
  266. outptr[x] = op(ptr[x], b0);
  267. }
  268. ptr += w;
  269. outptr += w;
  270. }
  271. return 0;
  272. }
  273. }
  274. else if (a.dims == 1)
  275. {
  276. if (a.w == 1)
  277. {
  278. if (b.dims == 3)
  279. {
  280. // type 4
  281. c.create(w1, h1, channels1, elemsize, opt.blob_allocator);
  282. if (c.empty())
  283. return -100;
  284. const float a0 = a[0];
  285. #pragma omp parallel for num_threads(opt.num_threads)
  286. for (int q = 0; q < channels1; q++)
  287. {
  288. const float* ptr1 = b.channel(q);
  289. float* outptr = c.channel(q);
  290. for (int i = 0; i < size1; i++)
  291. {
  292. outptr[i] = op(a0, ptr1[i]);
  293. }
  294. }
  295. return 0;
  296. }
  297. if (b.dims == 2)
  298. {
  299. // type 3
  300. c.create(w1, h1, elemsize, opt.blob_allocator);
  301. if (c.empty())
  302. return -100;
  303. const float a0 = a[0];
  304. for (int i = 0; i < size1; i++)
  305. {
  306. c[i] = op(a0, b[i]);
  307. }
  308. return 0;
  309. }
  310. if (b.dims == 1)
  311. {
  312. // type 2
  313. c.create(w1, elemsize, opt.blob_allocator);
  314. if (c.empty())
  315. return -100;
  316. const float a0 = a[0];
  317. for (int i = 0; i < w1; i++)
  318. {
  319. c[i] = op(a0, b[i]);
  320. }
  321. return 0;
  322. }
  323. }
  324. if (b.dims == 3)
  325. {
  326. // type 9
  327. c.create(w1, h1, channels1, elemsize, opt.blob_allocator);
  328. if (c.empty())
  329. return -100;
  330. #pragma omp parallel for num_threads(opt.num_threads)
  331. for (int q = 0; q < channels1; q++)
  332. {
  333. const float a0 = a[q];
  334. const float* ptr1 = b.channel(q);
  335. float* outptr = c.channel(q);
  336. for (int i = 0; i < size1; i++)
  337. {
  338. outptr[i] = op(a0, ptr1[i]);
  339. }
  340. }
  341. return 0;
  342. }
  343. if (b.dims == 2)
  344. {
  345. // type 8
  346. c.create(w1, h1, elemsize, opt.blob_allocator);
  347. if (c.empty())
  348. return -100;
  349. const float* ptr1 = b;
  350. float* outptr = c;
  351. for (int y = 0; y < h1; y++)
  352. {
  353. const float a0 = a[y];
  354. for (int x = 0; x < w1; x++)
  355. {
  356. outptr[x] = op(a0, ptr1[x]);
  357. }
  358. ptr1 += w1;
  359. outptr += w1;
  360. }
  361. return 0;
  362. }
  363. if (b.dims == 1)
  364. {
  365. c.create(w, elemsize, opt.blob_allocator);
  366. if (c.empty())
  367. return -100;
  368. if (b.w == 1)
  369. {
  370. // type 6
  371. const float b0 = b[0];
  372. for (int i = 0; i < w; i++)
  373. {
  374. c[i] = op(a[i], b0);
  375. }
  376. return 0;
  377. }
  378. // type 7
  379. for (int i = 0; i < w; i++)
  380. {
  381. c[i] = op(a[i], b[i]);
  382. }
  383. }
  384. }
  385. return 0;
  386. }
  387. template<typename Op>
  388. static int binary_op_scalar_inplace(Mat& a, float b, const Option& opt)
  389. {
  390. Op op;
  391. int w = a.w;
  392. int h = a.h;
  393. int channels = a.c;
  394. int size = w * h;
  395. #pragma omp parallel for num_threads(opt.num_threads)
  396. for (int q = 0; q < channels; q++)
  397. {
  398. float* ptr = a.channel(q);
  399. for (int i = 0; i < size; i++)
  400. {
  401. ptr[i] = op(ptr[i], b);
  402. }
  403. }
  404. return 0;
  405. }
  406. struct binary_op_add
  407. {
  408. float operator()(const float& x, const float& y) const
  409. {
  410. return x + y;
  411. }
  412. };
  413. struct binary_op_sub
  414. {
  415. float operator()(const float& x, const float& y) const
  416. {
  417. return x - y;
  418. }
  419. };
  420. struct binary_op_mul
  421. {
  422. float operator()(const float& x, const float& y) const
  423. {
  424. return x * y;
  425. }
  426. };
  427. struct binary_op_div
  428. {
  429. float operator()(const float& x, const float& y) const
  430. {
  431. return x / y;
  432. }
  433. };
  434. struct binary_op_max
  435. {
  436. float operator()(const float& x, const float& y) const
  437. {
  438. return std::max(x, y);
  439. }
  440. };
  441. struct binary_op_min
  442. {
  443. float operator()(const float& x, const float& y) const
  444. {
  445. return std::min(x, y);
  446. }
  447. };
  448. struct binary_op_pow
  449. {
  450. float operator()(const float& x, const float& y) const
  451. {
  452. return (float)pow(x, y);
  453. }
  454. };
  455. struct binary_op_rsub
  456. {
  457. float operator()(const float& x, const float& y) const
  458. {
  459. return y - x;
  460. }
  461. };
  462. struct binary_op_rdiv
  463. {
  464. float operator()(const float& x, const float& y) const
  465. {
  466. return y / x;
  467. }
  468. };
  469. int BinaryOp::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  470. {
  471. const Mat& bottom_blob = bottom_blobs[0];
  472. const Mat& bottom_blob1 = bottom_blobs[1];
  473. Mat& top_blob = top_blobs[0];
  474. if (op_type == Operation_ADD)
  475. return binary_op<binary_op_add>(bottom_blob, bottom_blob1, top_blob, opt);
  476. if (op_type == Operation_SUB)
  477. return binary_op<binary_op_sub>(bottom_blob, bottom_blob1, top_blob, opt);
  478. if (op_type == Operation_MUL)
  479. return binary_op<binary_op_mul>(bottom_blob, bottom_blob1, top_blob, opt);
  480. if (op_type == Operation_DIV)
  481. return binary_op<binary_op_div>(bottom_blob, bottom_blob1, top_blob, opt);
  482. if (op_type == Operation_MAX)
  483. return binary_op<binary_op_max>(bottom_blob, bottom_blob1, top_blob, opt);
  484. if (op_type == Operation_MIN)
  485. return binary_op<binary_op_min>(bottom_blob, bottom_blob1, top_blob, opt);
  486. if (op_type == Operation_POW)
  487. return binary_op<binary_op_pow>(bottom_blob, bottom_blob1, top_blob, opt);
  488. if (op_type == Operation_RSUB)
  489. return binary_op<binary_op_rsub>(bottom_blob, bottom_blob1, top_blob, opt);
  490. if (op_type == Operation_RDIV)
  491. return binary_op<binary_op_rdiv>(bottom_blob, bottom_blob1, top_blob, opt);
  492. return 0;
  493. }
  494. int BinaryOp::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
  495. {
  496. if (op_type == Operation_ADD)
  497. return binary_op_scalar_inplace<binary_op_add>(bottom_top_blob, b, opt);
  498. if (op_type == Operation_SUB)
  499. return binary_op_scalar_inplace<binary_op_sub>(bottom_top_blob, b, opt);
  500. if (op_type == Operation_MUL)
  501. return binary_op_scalar_inplace<binary_op_mul>(bottom_top_blob, b, opt);
  502. if (op_type == Operation_DIV)
  503. return binary_op_scalar_inplace<binary_op_div>(bottom_top_blob, b, opt);
  504. if (op_type == Operation_MAX)
  505. return binary_op_scalar_inplace<binary_op_max>(bottom_top_blob, b, opt);
  506. if (op_type == Operation_MIN)
  507. return binary_op_scalar_inplace<binary_op_min>(bottom_top_blob, b, opt);
  508. if (op_type == Operation_POW)
  509. return binary_op_scalar_inplace<binary_op_pow>(bottom_top_blob, b, opt);
  510. if (op_type == Operation_RSUB)
  511. return binary_op_scalar_inplace<binary_op_rsub>(bottom_top_blob, b, opt);
  512. if (op_type == Operation_RDIV)
  513. return binary_op_scalar_inplace<binary_op_rdiv>(bottom_top_blob, b, opt);
  514. return 0;
  515. }
  516. } // namespace ncnn