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_3.cpp 9.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // Copyright 2020 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "testutil.h"
  4. #define OP_TYPE_MAX 12
  5. static int op_type = 0;
  6. static int test_binaryop(const ncnn::Mat& _a, const ncnn::Mat& _b, int flag)
  7. {
  8. ncnn::Mat a = _a;
  9. ncnn::Mat b = _b;
  10. if (op_type == 6 || op_type == 9)
  11. {
  12. // value must be positive for pow/rpow
  13. a = a.clone();
  14. b = b.clone();
  15. Randomize(a, 0.001f, 2.f);
  16. Randomize(b, 0.001f, 2.f);
  17. }
  18. if (op_type == 3 || op_type == 8)
  19. {
  20. // value must be positive for div/rdiv
  21. a = a.clone();
  22. b = b.clone();
  23. Randomize(a, 0.1f, 10.f);
  24. Randomize(b, 0.1f, 10.f);
  25. }
  26. if (op_type == 10 || op_type == 11)
  27. {
  28. // value must be non-zero for atan2/ratan2
  29. a = a.clone();
  30. b = b.clone();
  31. for (int i = 0; i < a.total(); i++)
  32. {
  33. if (a[i] == 0.f)
  34. a[i] = 0.001f;
  35. }
  36. for (int i = 0; i < b.total(); i++)
  37. {
  38. if (b[i] == 0.f)
  39. b[i] = 0.001f;
  40. }
  41. }
  42. ncnn::ParamDict pd;
  43. pd.set(0, op_type);
  44. pd.set(1, 0); // with_scalar
  45. pd.set(2, 0.f); // b
  46. std::vector<ncnn::Mat> weights(0);
  47. std::vector<ncnn::Mat> ab(2);
  48. ab[0] = a;
  49. ab[1] = b;
  50. int ret = test_layer("BinaryOp", pd, weights, ab, 1, 0.001, 0, flag);
  51. if (ret != 0)
  52. {
  53. fprintf(stderr, "test_binaryop failed a.dims=%d a=(%d %d %d %d) b.dims=%d b=(%d %d %d %d) op_type=%d\n", a.dims, a.w, a.h, a.d, a.c, b.dims, b.w, b.h, b.d, b.c, op_type);
  54. }
  55. return ret;
  56. }
  57. static int test_binaryop(const ncnn::Mat& _a, float b, int flag)
  58. {
  59. ncnn::Mat a = _a;
  60. if (op_type == 6 || op_type == 9)
  61. {
  62. // value must be positive for pow
  63. Randomize(a, 0.001f, 2.f);
  64. b = RandomFloat(0.001f, 2.f);
  65. }
  66. if (op_type == 3 || op_type == 8)
  67. {
  68. // value must be positive for div/rdiv
  69. a = a.clone();
  70. Randomize(a, 0.1f, 10.f);
  71. }
  72. if (op_type == 10 || op_type == 11)
  73. {
  74. // value must be non-zero for atan2/ratan2
  75. a = a.clone();
  76. for (int i = 0; i < a.total(); i++)
  77. {
  78. if (a[i] == 0.f)
  79. a[i] = 0.001f;
  80. }
  81. }
  82. ncnn::ParamDict pd;
  83. pd.set(0, op_type);
  84. pd.set(1, 1); // with_scalar
  85. pd.set(2, b); // b
  86. std::vector<ncnn::Mat> weights(0);
  87. int ret = test_layer("BinaryOp", pd, weights, a, 0.001, 0, flag);
  88. if (ret != 0)
  89. {
  90. fprintf(stderr, "test_binaryop failed a.dims=%d a=(%d %d %d %d) b=%f op_type=%d\n", a.dims, a.w, a.h, a.d, a.c, b, op_type);
  91. }
  92. return ret;
  93. }
  94. static int test_binaryop_1()
  95. {
  96. const int ws[] = {31, 28, 24, 32};
  97. for (int i = 0; i < 4; i++)
  98. {
  99. const int w = ws[i];
  100. const int flag = w == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
  101. ncnn::Mat a[2];
  102. for (int j = 0; j < 2; j++)
  103. {
  104. int bw = j % 2 == 0 ? w : 1;
  105. a[j] = RandomMat(bw);
  106. }
  107. for (int j = 0; j < 2; j++)
  108. {
  109. for (int k = 0; k < 2; k++)
  110. {
  111. int ret = test_binaryop(a[j], a[k], flag);
  112. if (ret != 0)
  113. return ret;
  114. }
  115. int ret = test_binaryop(a[j], 0.2f, flag);
  116. if (ret != 0)
  117. return ret;
  118. }
  119. }
  120. return 0;
  121. }
  122. static int test_binaryop_2()
  123. {
  124. const int ws[] = {13, 14, 15, 16};
  125. const int hs[] = {31, 28, 24, 32};
  126. for (int i = 0; i < 4; i++)
  127. {
  128. const int w = ws[i];
  129. const int h = hs[i];
  130. const int flag = h == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
  131. ncnn::Mat a[4];
  132. for (int j = 0; j < 2; j++)
  133. {
  134. for (int k = 0; k < 2; k++)
  135. {
  136. int bw = j % 2 == 0 ? w : 1;
  137. int bh = k % 2 == 0 ? h : 1;
  138. a[j * 2 + k] = RandomMat(bw, bh);
  139. }
  140. }
  141. for (int j = 0; j < 4; j++)
  142. {
  143. for (int k = 0; k < 4; k++)
  144. {
  145. int ret = test_binaryop(a[j], a[k], flag);
  146. if (ret != 0)
  147. return ret;
  148. }
  149. int ret = test_binaryop(a[j], 0.2f, flag);
  150. if (ret != 0)
  151. return ret;
  152. }
  153. }
  154. return 0;
  155. }
  156. static int test_binaryop_3()
  157. {
  158. const int ws[] = {7, 6, 5, 4};
  159. const int hs[] = {3, 4, 5, 6};
  160. const int cs[] = {31, 28, 24, 32};
  161. for (int i = 0; i < 4; i++)
  162. {
  163. const int w = ws[i];
  164. const int h = hs[i];
  165. const int c = cs[i];
  166. const int flag = c == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
  167. ncnn::Mat a[8];
  168. for (int j = 0; j < 2; j++)
  169. {
  170. for (int k = 0; k < 2; k++)
  171. {
  172. for (int l = 0; l < 2; l++)
  173. {
  174. int bw = j % 2 == 0 ? w : 1;
  175. int bh = k % 2 == 0 ? h : 1;
  176. int bc = l % 2 == 0 ? c : 1;
  177. a[j * 4 + k * 2 + l] = RandomMat(bw, bh, bc);
  178. }
  179. }
  180. }
  181. for (int j = 0; j < 8; j++)
  182. {
  183. for (int k = 0; k < 8; k++)
  184. {
  185. int ret = test_binaryop(a[j], a[k], flag);
  186. if (ret != 0)
  187. return ret;
  188. }
  189. int ret = test_binaryop(a[j], 0.2f, flag);
  190. if (ret != 0)
  191. return ret;
  192. }
  193. }
  194. return 0;
  195. }
  196. static int test_binaryop_4()
  197. {
  198. const int ws[] = {2, 3, 4, 5};
  199. const int hs[] = {7, 6, 5, 4};
  200. const int ds[] = {3, 4, 5, 6};
  201. const int cs[] = {31, 28, 24, 32};
  202. for (int i = 0; i < 4; i++)
  203. {
  204. const int w = ws[i];
  205. const int h = hs[i];
  206. const int d = ds[i];
  207. const int c = cs[i];
  208. const int flag = c == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
  209. ncnn::Mat a[16];
  210. for (int j = 0; j < 2; j++)
  211. {
  212. for (int k = 0; k < 2; k++)
  213. {
  214. for (int l = 0; l < 2; l++)
  215. {
  216. for (int m = 0; m < 2; m++)
  217. {
  218. int bw = j % 2 == 0 ? w : 1;
  219. int bh = k % 2 == 0 ? h : 1;
  220. int bd = l % 2 == 0 ? d : 1;
  221. int bc = m % 2 == 0 ? c : 1;
  222. a[j * 8 + k * 4 + l * 2 + m] = RandomMat(bw, bh, bd, bc);
  223. }
  224. }
  225. }
  226. }
  227. for (int j = 0; j < 16; j++)
  228. {
  229. for (int k = 0; k < 16; k++)
  230. {
  231. int ret = test_binaryop(a[j], a[k], flag);
  232. if (ret != 0)
  233. return ret;
  234. }
  235. int ret = test_binaryop(a[j], 0.2f, flag);
  236. if (ret != 0)
  237. return ret;
  238. }
  239. }
  240. return 0;
  241. }
  242. static int test_binaryop_5()
  243. {
  244. const int ws[] = {2, 3, 4, 5};
  245. const int hs[] = {7, 6, 5, 4};
  246. const int ds[] = {3, 4, 5, 6};
  247. const int cs[] = {31, 28, 24, 32};
  248. for (int i = 0; i < 4; i++)
  249. {
  250. const int w = ws[i];
  251. const int h = hs[i];
  252. const int d = ds[i];
  253. const int c = cs[i];
  254. const int flag = c == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
  255. ncnn::Mat a[4] = {
  256. RandomMat(c),
  257. RandomMat(d, c),
  258. RandomMat(h, d, c),
  259. RandomMat(w, h, d, c),
  260. };
  261. for (int j = 0; j < 4; j++)
  262. {
  263. for (int k = 0; k < 4; k++)
  264. {
  265. if (j == k)
  266. continue;
  267. int ret = test_binaryop(a[j], a[k], flag);
  268. if (ret != 0)
  269. return ret;
  270. }
  271. }
  272. }
  273. return 0;
  274. }
  275. static int test_binaryop_6()
  276. {
  277. const int ws[] = {16, 12, 16, 15};
  278. const int hs[] = {15, 16, 15, 12};
  279. const int ds[] = {12, 14, 12, 16};
  280. const int cs[] = {31, 28, 24, 32};
  281. for (int i = 0; i < 4; i++)
  282. {
  283. const int w = ws[i];
  284. const int h = hs[i];
  285. const int d = ds[i];
  286. const int c = cs[i];
  287. const int flag = c == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
  288. ncnn::Mat a[3] = {
  289. RandomMat(d, c),
  290. RandomMat(h, d, c),
  291. RandomMat(w, h, d, c),
  292. };
  293. for (int j = 0; j < 3; j++)
  294. {
  295. ncnn::Mat b = RandomMat(a[j].w);
  296. int ret = test_binaryop(a[j], b, flag) || test_binaryop(b, a[j], flag);
  297. if (ret != 0)
  298. return ret;
  299. }
  300. ncnn::Mat aa[3] = {
  301. RandomMat(c, c),
  302. RandomMat(c, d, c),
  303. RandomMat(c, h, d, c),
  304. };
  305. for (int j = 0; j < 3; j++)
  306. {
  307. ncnn::Mat b = RandomMat(aa[j].w);
  308. int ret = test_binaryop(aa[j], b, flag) || test_binaryop(b, aa[j], flag);
  309. if (ret != 0)
  310. return ret;
  311. }
  312. }
  313. return 0;
  314. }
  315. int main()
  316. {
  317. SRAND(7767517);
  318. for (op_type = 9; op_type < OP_TYPE_MAX; op_type++)
  319. {
  320. int ret = 0
  321. || test_binaryop_1()
  322. || test_binaryop_2()
  323. || test_binaryop_3()
  324. || test_binaryop_4()
  325. || test_binaryop_5()
  326. || test_binaryop_6();
  327. if (ret != 0)
  328. return ret;
  329. }
  330. return 0;
  331. }