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

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