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

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