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 9.7 kB

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