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.8 kB

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