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

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