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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 9
  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)
  23. {
  24. // value must be positive for pow
  25. Randomize(a, 0.001f, 2.f);
  26. Randomize(b, 0.001f, 2.f);
  27. }
  28. if (op_type == 3 || op_type == 8)
  29. {
  30. // value must be positive for pow
  31. Randomize(a, 0.1f, 10.f);
  32. Randomize(b, 0.1f, 10.f);
  33. }
  34. ncnn::ParamDict pd;
  35. pd.set(0, op_type);
  36. pd.set(1, 0); // with_scalar
  37. pd.set(2, 0.f); // b
  38. std::vector<ncnn::Mat> weights(0);
  39. std::vector<ncnn::Mat> ab(2);
  40. ab[0] = a;
  41. ab[1] = b;
  42. int ret = test_layer<ncnn::BinaryOp>("BinaryOp", pd, weights, ab);
  43. if (ret != 0)
  44. {
  45. fprintf(stderr, "test_binaryop failed a.dims=%d a=(%d %d %d) b.dims=%d b=(%d %d %d) op_type=%d\n", a.dims, a.w, a.h, a.c, b.dims, b.w, b.h, b.c, op_type);
  46. }
  47. return ret;
  48. }
  49. static int test_binaryop(const ncnn::Mat& _a, float b)
  50. {
  51. ncnn::Mat a = _a;
  52. if (op_type == 6)
  53. {
  54. // value must be positive for pow
  55. Randomize(a, 0.001f, 2.f);
  56. b = RandomFloat(0.001f, 2.f);
  57. }
  58. ncnn::ParamDict pd;
  59. pd.set(0, op_type);
  60. pd.set(1, 1); // with_scalar
  61. pd.set(2, b); // b
  62. std::vector<ncnn::Mat> weights(0);
  63. int ret = test_layer<ncnn::BinaryOp>("BinaryOp", pd, weights, a);
  64. if (ret != 0)
  65. {
  66. fprintf(stderr, "test_binaryop failed a.dims=%d a=(%d %d %d) b=%f op_type=%d\n", a.dims, a.w, a.h, a.c, b, op_type);
  67. }
  68. return ret;
  69. }
  70. // https://github.com/Tencent/ncnn/wiki/binaryop-broadcasting
  71. static int test_binaryop_1()
  72. {
  73. return 0
  74. || test_binaryop(RandomMat(1), 1.f);
  75. }
  76. static int test_binaryop_2()
  77. {
  78. return 0
  79. || test_binaryop(RandomMat(1), RandomMat(1))
  80. || test_binaryop(RandomMat(1), RandomMat(4))
  81. || test_binaryop(RandomMat(1), RandomMat(16));
  82. }
  83. static int test_binaryop_3()
  84. {
  85. return 0
  86. || test_binaryop(RandomMat(1), RandomMat(11, 3))
  87. || test_binaryop(RandomMat(1), RandomMat(11, 4))
  88. || test_binaryop(RandomMat(1), RandomMat(11, 16));
  89. }
  90. static int test_binaryop_4()
  91. {
  92. return 0
  93. || test_binaryop(RandomMat(1), RandomMat(11, 6, 2))
  94. || test_binaryop(RandomMat(1), RandomMat(11, 6, 4))
  95. || test_binaryop(RandomMat(1), RandomMat(11, 6, 16));
  96. }
  97. static int test_binaryop_5()
  98. {
  99. return 0
  100. || test_binaryop(RandomMat(2), 1.f)
  101. || test_binaryop(RandomMat(4), 1.f)
  102. || test_binaryop(RandomMat(16), 1.f);
  103. }
  104. static int test_binaryop_6()
  105. {
  106. return 0
  107. || test_binaryop(RandomMat(2), RandomMat(1))
  108. || test_binaryop(RandomMat(4), RandomMat(1))
  109. || test_binaryop(RandomMat(16), RandomMat(1));
  110. }
  111. static int test_binaryop_7()
  112. {
  113. return 0
  114. || test_binaryop(RandomMat(2), RandomMat(2))
  115. || test_binaryop(RandomMat(4), RandomMat(4))
  116. || test_binaryop(RandomMat(16), RandomMat(16));
  117. }
  118. static int test_binaryop_8()
  119. {
  120. return 0
  121. || test_binaryop(RandomMat(3), RandomMat(11, 3))
  122. || test_binaryop(RandomMat(4), RandomMat(11, 4))
  123. || test_binaryop(RandomMat(16), RandomMat(11, 16));
  124. }
  125. static int test_binaryop_9()
  126. {
  127. return 0
  128. || test_binaryop(RandomMat(2), RandomMat(11, 6, 2))
  129. || test_binaryop(RandomMat(4), RandomMat(11, 6, 4))
  130. || test_binaryop(RandomMat(16), RandomMat(11, 6, 16));
  131. }
  132. static int test_binaryop_10()
  133. {
  134. return 0
  135. || test_binaryop(RandomMat(11, 3), 1.f)
  136. || test_binaryop(RandomMat(11, 4), 1.f)
  137. || test_binaryop(RandomMat(11, 16), 1.f);
  138. }
  139. static int test_binaryop_11()
  140. {
  141. return 0
  142. || test_binaryop(RandomMat(11, 3), RandomMat(1))
  143. || test_binaryop(RandomMat(11, 4), RandomMat(1))
  144. || test_binaryop(RandomMat(11, 16), RandomMat(1));
  145. }
  146. static int test_binaryop_12()
  147. {
  148. return 0
  149. || test_binaryop(RandomMat(11, 3), RandomMat(3))
  150. || test_binaryop(RandomMat(11, 4), RandomMat(4))
  151. || test_binaryop(RandomMat(11, 16), RandomMat(16));
  152. }
  153. static int test_binaryop_13()
  154. {
  155. return 0
  156. || test_binaryop(RandomMat(11, 3), RandomMat(11, 3))
  157. || test_binaryop(RandomMat(11, 4), RandomMat(11, 4))
  158. || test_binaryop(RandomMat(11, 16), RandomMat(11, 16));
  159. }
  160. static int test_binaryop_14()
  161. {
  162. return 0
  163. || test_binaryop(RandomMat(6, 2), RandomMat(11, 6, 2))
  164. || test_binaryop(RandomMat(6, 4), RandomMat(11, 6, 4))
  165. || test_binaryop(RandomMat(6, 16), RandomMat(11, 6, 16));
  166. }
  167. static int test_binaryop_15()
  168. {
  169. return 0
  170. || test_binaryop(RandomMat(11, 6, 2), 1.f)
  171. || test_binaryop(RandomMat(11, 6, 4), 1.f)
  172. || test_binaryop(RandomMat(11, 6, 16), 1.f);
  173. }
  174. static int test_binaryop_16()
  175. {
  176. return 0
  177. || test_binaryop(RandomMat(11, 6, 2), RandomMat(1))
  178. || test_binaryop(RandomMat(11, 6, 4), RandomMat(1))
  179. || test_binaryop(RandomMat(11, 6, 16), RandomMat(1));
  180. }
  181. static int test_binaryop_17()
  182. {
  183. return 0
  184. || test_binaryop(RandomMat(11, 6, 2), RandomMat(2))
  185. || test_binaryop(RandomMat(11, 6, 4), RandomMat(4))
  186. || test_binaryop(RandomMat(11, 6, 16), RandomMat(16));
  187. }
  188. static int test_binaryop_18()
  189. {
  190. return 0
  191. || test_binaryop(RandomMat(11, 6, 2), RandomMat(6, 2))
  192. || test_binaryop(RandomMat(11, 6, 4), RandomMat(6, 4))
  193. || test_binaryop(RandomMat(11, 6, 16), RandomMat(6, 16));
  194. }
  195. static int test_binaryop_19()
  196. {
  197. return 0
  198. || test_binaryop(RandomMat(11, 6, 2), RandomMat(11, 6, 2))
  199. || test_binaryop(RandomMat(11, 6, 4), RandomMat(11, 6, 4))
  200. || test_binaryop(RandomMat(11, 6, 16), RandomMat(11, 6, 16));
  201. }
  202. static int test_binaryop_s1()
  203. {
  204. return 0
  205. || test_binaryop(RandomMat(11, 6, 2), RandomMat(1, 1, 2))
  206. || test_binaryop(RandomMat(11, 6, 4), RandomMat(1, 1, 4))
  207. || test_binaryop(RandomMat(11, 6, 16), RandomMat(1, 1, 16));
  208. }
  209. static int test_binaryop_s2()
  210. {
  211. return 0
  212. || test_binaryop(RandomMat(11, 6, 2), RandomMat(11, 6, 1))
  213. || test_binaryop(RandomMat(11, 6, 4), RandomMat(11, 6, 1))
  214. || test_binaryop(RandomMat(11, 6, 16), RandomMat(11, 6, 1));
  215. }
  216. static int test_binaryop_s3()
  217. {
  218. return 0
  219. || test_binaryop(RandomMat(1, 1, 2), RandomMat(11, 6, 2))
  220. || test_binaryop(RandomMat(1, 1, 4), RandomMat(11, 6, 4))
  221. || test_binaryop(RandomMat(1, 1, 16), RandomMat(11, 6, 16));
  222. }
  223. static int test_binaryop_s4()
  224. {
  225. return 0
  226. || test_binaryop(RandomMat(11, 6, 1), RandomMat(11, 6, 2))
  227. || test_binaryop(RandomMat(11, 6, 1), RandomMat(11, 6, 4))
  228. || test_binaryop(RandomMat(11, 6, 1), RandomMat(11, 6, 16));
  229. }
  230. static int test_binaryop_s5()
  231. {
  232. return 0
  233. || test_binaryop(RandomMat(11, 6, 2), RandomMat(1, 6, 2))
  234. || test_binaryop(RandomMat(11, 6, 4), RandomMat(1, 6, 4))
  235. || test_binaryop(RandomMat(11, 6, 16), RandomMat(1, 6, 16));
  236. }
  237. static int test_binaryop_s6()
  238. {
  239. return 0
  240. || test_binaryop(RandomMat(11, 6, 2), RandomMat(11, 1, 2))
  241. || test_binaryop(RandomMat(11, 6, 4), RandomMat(11, 1, 4))
  242. || test_binaryop(RandomMat(11, 6, 16), RandomMat(11, 1, 16));
  243. }
  244. static int test_binaryop_s7()
  245. {
  246. return 0
  247. || test_binaryop(RandomMat(1, 6, 2), RandomMat(11, 6, 2))
  248. || test_binaryop(RandomMat(1, 6, 4), RandomMat(11, 6, 4))
  249. || test_binaryop(RandomMat(1, 6, 16), RandomMat(11, 6, 16));
  250. }
  251. static int test_binaryop_s8()
  252. {
  253. return 0
  254. || test_binaryop(RandomMat(11, 1, 2), RandomMat(11, 6, 2))
  255. || test_binaryop(RandomMat(11, 1, 4), RandomMat(11, 6, 4))
  256. || test_binaryop(RandomMat(11, 1, 16), RandomMat(11, 6, 16));
  257. }
  258. int main()
  259. {
  260. SRAND(7767517);
  261. for (op_type = 0; op_type < OP_TYPE_MAX; op_type++)
  262. {
  263. int ret = 0
  264. || test_binaryop_1()
  265. || test_binaryop_2()
  266. || test_binaryop_3()
  267. || test_binaryop_4()
  268. || test_binaryop_5()
  269. || test_binaryop_6()
  270. || test_binaryop_7()
  271. || test_binaryop_8()
  272. || test_binaryop_9()
  273. || test_binaryop_10()
  274. || test_binaryop_11()
  275. || test_binaryop_12()
  276. || test_binaryop_13()
  277. || test_binaryop_14()
  278. || test_binaryop_15()
  279. || test_binaryop_16()
  280. || test_binaryop_17()
  281. || test_binaryop_18()
  282. || test_binaryop_19()
  283. || test_binaryop_s1()
  284. || test_binaryop_s2()
  285. || test_binaryop_s3()
  286. || test_binaryop_s4()
  287. || test_binaryop_s5()
  288. || test_binaryop_s6()
  289. || test_binaryop_s7()
  290. || test_binaryop_s8();
  291. if (ret != 0)
  292. return ret;
  293. }
  294. return 0;
  295. }