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.

test_binaryop.cpp 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 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 "testutil.h"
  15. #include "layer/binaryop.h"
  16. #define OP_TYPE_MAX 9
  17. static int test_binaryop(const ncnn::Mat& _a, const ncnn::Mat& _b, int op_type)
  18. {
  19. ncnn::Mat a = _a;
  20. ncnn::Mat b = _b;
  21. if (op_type == 6)
  22. {
  23. // value must be positive for pow
  24. Randomize(a, 0.001f, 2.f);
  25. Randomize(b, 0.001f, 2.f);
  26. }
  27. ncnn::ParamDict pd;
  28. pd.set(0, op_type);
  29. pd.set(1, 0);// with_scalar
  30. pd.set(2, 0.f);// b
  31. std::vector<ncnn::Mat> weights(0);
  32. ncnn::Option opt;
  33. opt.num_threads = 1;
  34. opt.use_vulkan_compute = true;
  35. opt.use_fp16_packed = false;
  36. opt.use_fp16_storage = false;
  37. opt.use_fp16_arithmetic = false;
  38. opt.use_int8_storage = false;
  39. opt.use_int8_arithmetic = false;
  40. std::vector<ncnn::Mat> ab(2);
  41. ab[0] = a;
  42. ab[1] = b;
  43. int ret = test_layer<ncnn::BinaryOp>("BinaryOp", pd, weights, opt, ab);
  44. if (ret != 0)
  45. {
  46. fprintf(stderr, "test_binaryop failed a.dims=%d a=(%d %d %d) b.dims=%d b=(%d %d %d) op_type=%d\n", a.dims, a.w, a.h, a.c, b.dims, b.w, b.h, b.c, op_type);
  47. }
  48. return ret;
  49. }
  50. static int test_binaryop(const ncnn::Mat& _a, float b, int op_type)
  51. {
  52. ncnn::Mat a = _a;
  53. if (op_type == 6)
  54. {
  55. // value must be positive for pow
  56. Randomize(a, 0.001f, 2.f);
  57. b = RandomFloat(0.001f, 2.f);
  58. }
  59. ncnn::ParamDict pd;
  60. pd.set(0, op_type);
  61. pd.set(1, 1);// with_scalar
  62. pd.set(2, b);// b
  63. std::vector<ncnn::Mat> weights(0);
  64. ncnn::Option opt;
  65. opt.num_threads = 1;
  66. opt.use_vulkan_compute = true;
  67. opt.use_fp16_packed = false;
  68. opt.use_fp16_storage = false;
  69. opt.use_fp16_arithmetic = false;
  70. opt.use_int8_storage = false;
  71. opt.use_int8_arithmetic = false;
  72. int ret = test_layer<ncnn::BinaryOp>("BinaryOp", pd, weights, opt, a);
  73. if (ret != 0)
  74. {
  75. fprintf(stderr, "test_binaryop failed a.dims=%d a=(%d %d %d) b=%f op_type=%d\n", a.dims, a.w, a.h, a.c, b, op_type);
  76. }
  77. return ret;
  78. }
  79. // https://github.com/Tencent/ncnn/wiki/binaryop-broadcasting
  80. static int test_binaryop_1()
  81. {
  82. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  83. {
  84. int ret = 0
  85. || test_binaryop(RandomMat(1), 1.f, op_type)
  86. ;
  87. if (ret != 0)
  88. return -1;
  89. }
  90. return 0;
  91. }
  92. static int test_binaryop_2()
  93. {
  94. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  95. {
  96. int ret = 0
  97. || test_binaryop(RandomMat(1), RandomMat(1), op_type)
  98. || test_binaryop(RandomMat(1), RandomMat(4), op_type)
  99. || test_binaryop(RandomMat(1), RandomMat(8), op_type)
  100. ;
  101. if (ret != 0)
  102. return -1;
  103. }
  104. return 0;
  105. }
  106. static int test_binaryop_3()
  107. {
  108. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  109. {
  110. int ret = 0
  111. || test_binaryop(RandomMat(1), RandomMat(2, 3), op_type)
  112. || test_binaryop(RandomMat(1), RandomMat(2, 4), op_type)
  113. || test_binaryop(RandomMat(1), RandomMat(2, 8), op_type)
  114. ;
  115. if (ret != 0)
  116. return -1;
  117. }
  118. return 0;
  119. }
  120. static int test_binaryop_4()
  121. {
  122. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  123. {
  124. int ret = 0
  125. || test_binaryop(RandomMat(1), RandomMat(2, 3, 2), op_type)
  126. || test_binaryop(RandomMat(1), RandomMat(2, 3, 4), op_type)
  127. || test_binaryop(RandomMat(1), RandomMat(2, 3, 8), op_type)
  128. ;
  129. if (ret != 0)
  130. return -1;
  131. }
  132. return 0;
  133. }
  134. static int test_binaryop_5()
  135. {
  136. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  137. {
  138. int ret = 0
  139. || test_binaryop(RandomMat(2), 1.f, op_type)
  140. || test_binaryop(RandomMat(4), 1.f, op_type)
  141. || test_binaryop(RandomMat(8), 1.f, op_type)
  142. ;
  143. if (ret != 0)
  144. return -1;
  145. }
  146. return 0;
  147. }
  148. static int test_binaryop_6()
  149. {
  150. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  151. {
  152. int ret = 0
  153. || test_binaryop(RandomMat(2), RandomMat(1), op_type)
  154. || test_binaryop(RandomMat(4), RandomMat(1), op_type)
  155. || test_binaryop(RandomMat(8), RandomMat(1), op_type)
  156. ;
  157. if (ret != 0)
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. static int test_binaryop_7()
  163. {
  164. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  165. {
  166. int ret = 0
  167. || test_binaryop(RandomMat(2), RandomMat(2), op_type)
  168. || test_binaryop(RandomMat(4), RandomMat(4), op_type)
  169. || test_binaryop(RandomMat(8), RandomMat(8), op_type)
  170. ;
  171. if (ret != 0)
  172. return -1;
  173. }
  174. return 0;
  175. }
  176. static int test_binaryop_8()
  177. {
  178. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  179. {
  180. int ret = 0
  181. || test_binaryop(RandomMat(3), RandomMat(2, 3), op_type)
  182. || test_binaryop(RandomMat(4), RandomMat(2, 4), op_type)
  183. || test_binaryop(RandomMat(8), RandomMat(2, 8), op_type)
  184. ;
  185. if (ret != 0)
  186. return -1;
  187. }
  188. return 0;
  189. }
  190. static int test_binaryop_9()
  191. {
  192. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  193. {
  194. int ret = 0
  195. || test_binaryop(RandomMat(2), RandomMat(2, 3, 2), op_type)
  196. || test_binaryop(RandomMat(4), RandomMat(2, 3, 4), op_type)
  197. || test_binaryop(RandomMat(8), RandomMat(2, 3, 8), op_type)
  198. ;
  199. if (ret != 0)
  200. return -1;
  201. }
  202. return 0;
  203. }
  204. static int test_binaryop_10()
  205. {
  206. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  207. {
  208. int ret = 0
  209. || test_binaryop(RandomMat(2, 3), 1.f, op_type)
  210. || test_binaryop(RandomMat(2, 4), 1.f, op_type)
  211. || test_binaryop(RandomMat(2, 8), 1.f, op_type)
  212. ;
  213. if (ret != 0)
  214. return -1;
  215. }
  216. return 0;
  217. }
  218. static int test_binaryop_11()
  219. {
  220. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  221. {
  222. int ret = 0
  223. || test_binaryop(RandomMat(2, 3), RandomMat(1), op_type)
  224. || test_binaryop(RandomMat(2, 4), RandomMat(1), op_type)
  225. || test_binaryop(RandomMat(2, 8), RandomMat(1), op_type)
  226. ;
  227. if (ret != 0)
  228. return -1;
  229. }
  230. return 0;
  231. }
  232. static int test_binaryop_12()
  233. {
  234. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  235. {
  236. int ret = 0
  237. || test_binaryop(RandomMat(2, 3), RandomMat(3), op_type)
  238. || test_binaryop(RandomMat(2, 4), RandomMat(4), op_type)
  239. || test_binaryop(RandomMat(2, 8), RandomMat(8), op_type)
  240. ;
  241. if (ret != 0)
  242. return -1;
  243. }
  244. return 0;
  245. }
  246. static int test_binaryop_13()
  247. {
  248. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  249. {
  250. int ret = 0
  251. || test_binaryop(RandomMat(2, 3), RandomMat(2, 3), op_type)
  252. || test_binaryop(RandomMat(2, 4), RandomMat(2, 4), op_type)
  253. || test_binaryop(RandomMat(2, 8), RandomMat(2, 8), op_type)
  254. ;
  255. if (ret != 0)
  256. return -1;
  257. }
  258. return 0;
  259. }
  260. static int test_binaryop_14()
  261. {
  262. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  263. {
  264. int ret = 0
  265. || test_binaryop(RandomMat(3, 2), RandomMat(2, 3, 2), op_type)
  266. || test_binaryop(RandomMat(3, 4), RandomMat(2, 3, 4), op_type)
  267. || test_binaryop(RandomMat(3, 8), RandomMat(2, 3, 8), op_type)
  268. ;
  269. if (ret != 0)
  270. return -1;
  271. }
  272. return 0;
  273. }
  274. static int test_binaryop_15()
  275. {
  276. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  277. {
  278. int ret = 0
  279. || test_binaryop(RandomMat(2, 3, 2), 1.f, op_type)
  280. || test_binaryop(RandomMat(2, 3, 4), 1.f, op_type)
  281. || test_binaryop(RandomMat(2, 3, 8), 1.f, op_type)
  282. ;
  283. if (ret != 0)
  284. return -1;
  285. }
  286. return 0;
  287. }
  288. static int test_binaryop_16()
  289. {
  290. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  291. {
  292. int ret = 0
  293. || test_binaryop(RandomMat(2, 3, 2), RandomMat(1), op_type)
  294. || test_binaryop(RandomMat(2, 3, 4), RandomMat(1), op_type)
  295. || test_binaryop(RandomMat(2, 3, 8), RandomMat(1), op_type)
  296. ;
  297. if (ret != 0)
  298. return -1;
  299. }
  300. return 0;
  301. }
  302. static int test_binaryop_17()
  303. {
  304. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  305. {
  306. int ret = 0
  307. || test_binaryop(RandomMat(2, 3, 2), RandomMat(2), op_type)
  308. || test_binaryop(RandomMat(2, 3, 4), RandomMat(4), op_type)
  309. || test_binaryop(RandomMat(2, 3, 8), RandomMat(8), op_type)
  310. ;
  311. if (ret != 0)
  312. return -1;
  313. }
  314. return 0;
  315. }
  316. static int test_binaryop_18()
  317. {
  318. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  319. {
  320. int ret = 0
  321. || test_binaryop(RandomMat(2, 3, 2), RandomMat(3, 2), op_type)
  322. || test_binaryop(RandomMat(2, 3, 4), RandomMat(3, 4), op_type)
  323. || test_binaryop(RandomMat(2, 3, 8), RandomMat(3, 8), op_type)
  324. ;
  325. if (ret != 0)
  326. return -1;
  327. }
  328. return 0;
  329. }
  330. static int test_binaryop_19()
  331. {
  332. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  333. {
  334. int ret = 0
  335. || test_binaryop(RandomMat(2, 3, 2), RandomMat(2, 3, 2), op_type)
  336. || test_binaryop(RandomMat(2, 3, 4), RandomMat(2, 3, 4), op_type)
  337. || test_binaryop(RandomMat(2, 3, 8), RandomMat(2, 3, 8), op_type)
  338. ;
  339. if (ret != 0)
  340. return -1;
  341. }
  342. return 0;
  343. }
  344. static int test_binaryop_s1()
  345. {
  346. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  347. {
  348. int ret = 0
  349. || test_binaryop(RandomMat(2, 3, 2), RandomMat(1, 1, 2), op_type)
  350. || test_binaryop(RandomMat(2, 3, 4), RandomMat(1, 1, 4), op_type)
  351. || test_binaryop(RandomMat(2, 3, 8), RandomMat(1, 1, 8), op_type)
  352. ;
  353. if (ret != 0)
  354. return -1;
  355. }
  356. return 0;
  357. }
  358. static int test_binaryop_s2()
  359. {
  360. for (int op_type=0; op_type<OP_TYPE_MAX; op_type++)
  361. {
  362. int ret = 0
  363. || test_binaryop(RandomMat(2, 3, 2), RandomMat(2, 3, 1), op_type)
  364. || test_binaryop(RandomMat(2, 3, 4), RandomMat(2, 3, 1), op_type)
  365. || test_binaryop(RandomMat(2, 3, 8), RandomMat(2, 3, 1), op_type)
  366. ;
  367. if (ret != 0)
  368. return -1;
  369. }
  370. return 0;
  371. }
  372. int main()
  373. {
  374. SRAND(7767517);
  375. return 0
  376. || test_binaryop_1()
  377. || test_binaryop_2()
  378. || test_binaryop_3()
  379. || test_binaryop_4()
  380. || test_binaryop_5()
  381. || test_binaryop_6()
  382. || test_binaryop_7()
  383. || test_binaryop_8()
  384. || test_binaryop_9()
  385. || test_binaryop_10()
  386. || test_binaryop_11()
  387. || test_binaryop_12()
  388. || test_binaryop_13()
  389. || test_binaryop_14()
  390. || test_binaryop_15()
  391. || test_binaryop_16()
  392. || test_binaryop_17()
  393. || test_binaryop_18()
  394. || test_binaryop_19()
  395. || test_binaryop_s1()
  396. || test_binaryop_s2()
  397. ;
  398. }