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

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