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_1.cpp 10 kB

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