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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 "testutil.h"
  15. #include "layer/binaryop.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. }
  92. static int test_binaryop_2()
  93. {
  94. return 0
  95. || test_binaryop(RandomMat(1), RandomMat(1))
  96. || test_binaryop(RandomMat(1), RandomMat(4))
  97. || test_binaryop(RandomMat(1), RandomMat(8))
  98. ;
  99. }
  100. static int test_binaryop_3()
  101. {
  102. return 0
  103. || test_binaryop(RandomMat(1), RandomMat(2, 3))
  104. || test_binaryop(RandomMat(1), RandomMat(2, 4))
  105. || test_binaryop(RandomMat(1), RandomMat(2, 8))
  106. ;
  107. }
  108. static int test_binaryop_4()
  109. {
  110. return 0
  111. || test_binaryop(RandomMat(1), RandomMat(2, 3, 2))
  112. || test_binaryop(RandomMat(1), RandomMat(2, 3, 4))
  113. || test_binaryop(RandomMat(1), RandomMat(2, 3, 8))
  114. ;
  115. }
  116. static int test_binaryop_5()
  117. {
  118. return 0
  119. || test_binaryop(RandomMat(2), 1.f)
  120. || test_binaryop(RandomMat(4), 1.f)
  121. || test_binaryop(RandomMat(8), 1.f)
  122. ;
  123. }
  124. static int test_binaryop_6()
  125. {
  126. return 0
  127. || test_binaryop(RandomMat(2), RandomMat(1))
  128. || test_binaryop(RandomMat(4), RandomMat(1))
  129. || test_binaryop(RandomMat(8), RandomMat(1))
  130. ;
  131. }
  132. static int test_binaryop_7()
  133. {
  134. return 0
  135. || test_binaryop(RandomMat(2), RandomMat(2))
  136. || test_binaryop(RandomMat(4), RandomMat(4))
  137. || test_binaryop(RandomMat(8), RandomMat(8))
  138. ;
  139. }
  140. static int test_binaryop_8()
  141. {
  142. return 0
  143. || test_binaryop(RandomMat(3), RandomMat(2, 3))
  144. || test_binaryop(RandomMat(4), RandomMat(2, 4))
  145. || test_binaryop(RandomMat(8), RandomMat(2, 8))
  146. ;
  147. }
  148. static int test_binaryop_9()
  149. {
  150. return 0
  151. || test_binaryop(RandomMat(2), RandomMat(2, 3, 2))
  152. || test_binaryop(RandomMat(4), RandomMat(2, 3, 4))
  153. || test_binaryop(RandomMat(8), RandomMat(2, 3, 8))
  154. ;
  155. }
  156. static int test_binaryop_10()
  157. {
  158. return 0
  159. || test_binaryop(RandomMat(2, 3), 1.f)
  160. || test_binaryop(RandomMat(2, 4), 1.f)
  161. || test_binaryop(RandomMat(2, 8), 1.f)
  162. ;
  163. }
  164. static int test_binaryop_11()
  165. {
  166. return 0
  167. || test_binaryop(RandomMat(2, 3), RandomMat(1))
  168. || test_binaryop(RandomMat(2, 4), RandomMat(1))
  169. || test_binaryop(RandomMat(2, 8), RandomMat(1))
  170. ;
  171. }
  172. static int test_binaryop_12()
  173. {
  174. return 0
  175. || test_binaryop(RandomMat(2, 3), RandomMat(3))
  176. || test_binaryop(RandomMat(2, 4), RandomMat(4))
  177. || test_binaryop(RandomMat(2, 8), RandomMat(8))
  178. ;
  179. }
  180. static int test_binaryop_13()
  181. {
  182. return 0
  183. || test_binaryop(RandomMat(2, 3), RandomMat(2, 3))
  184. || test_binaryop(RandomMat(2, 4), RandomMat(2, 4))
  185. || test_binaryop(RandomMat(2, 8), RandomMat(2, 8))
  186. ;
  187. }
  188. static int test_binaryop_14()
  189. {
  190. return 0
  191. || test_binaryop(RandomMat(3, 2), RandomMat(2, 3, 2))
  192. || test_binaryop(RandomMat(3, 4), RandomMat(2, 3, 4))
  193. || test_binaryop(RandomMat(3, 8), RandomMat(2, 3, 8))
  194. ;
  195. }
  196. static int test_binaryop_15()
  197. {
  198. return 0
  199. || test_binaryop(RandomMat(2, 3, 2), 1.f)
  200. || test_binaryop(RandomMat(2, 3, 4), 1.f)
  201. || test_binaryop(RandomMat(2, 3, 8), 1.f)
  202. ;
  203. }
  204. static int test_binaryop_16()
  205. {
  206. return 0
  207. || test_binaryop(RandomMat(2, 3, 2), RandomMat(1))
  208. || test_binaryop(RandomMat(2, 3, 4), RandomMat(1))
  209. || test_binaryop(RandomMat(2, 3, 8), RandomMat(1))
  210. ;
  211. }
  212. static int test_binaryop_17()
  213. {
  214. return 0
  215. || test_binaryop(RandomMat(2, 3, 2), RandomMat(2))
  216. || test_binaryop(RandomMat(2, 3, 4), RandomMat(4))
  217. || test_binaryop(RandomMat(2, 3, 8), RandomMat(8))
  218. ;
  219. }
  220. static int test_binaryop_18()
  221. {
  222. return 0
  223. || test_binaryop(RandomMat(2, 3, 2), RandomMat(3, 2))
  224. || test_binaryop(RandomMat(2, 3, 4), RandomMat(3, 4))
  225. || test_binaryop(RandomMat(2, 3, 8), RandomMat(3, 8))
  226. ;
  227. }
  228. static int test_binaryop_19()
  229. {
  230. return 0
  231. || test_binaryop(RandomMat(2, 3, 2), RandomMat(2, 3, 2))
  232. || test_binaryop(RandomMat(2, 3, 4), RandomMat(2, 3, 4))
  233. || test_binaryop(RandomMat(2, 3, 8), RandomMat(2, 3, 8))
  234. ;
  235. }
  236. static int test_binaryop_s1()
  237. {
  238. return 0
  239. || test_binaryop(RandomMat(2, 3, 2), RandomMat(1, 1, 2))
  240. || test_binaryop(RandomMat(2, 3, 4), RandomMat(1, 1, 4))
  241. || test_binaryop(RandomMat(2, 3, 8), RandomMat(1, 1, 8))
  242. ;
  243. }
  244. static int test_binaryop_s2()
  245. {
  246. return 0
  247. || test_binaryop(RandomMat(2, 3, 2), RandomMat(2, 3, 1))
  248. || test_binaryop(RandomMat(2, 3, 4), RandomMat(2, 3, 1))
  249. || test_binaryop(RandomMat(2, 3, 8), RandomMat(2, 3, 1))
  250. ;
  251. }
  252. static int test_binaryop_s3()
  253. {
  254. return 0
  255. || test_binaryop(RandomMat(1, 1, 2), RandomMat(2, 3, 2))
  256. || test_binaryop(RandomMat(1, 1, 4), RandomMat(2, 3, 4))
  257. || test_binaryop(RandomMat(1, 1, 8), RandomMat(2, 3, 8))
  258. ;
  259. }
  260. static int test_binaryop_s4()
  261. {
  262. return 0
  263. || test_binaryop(RandomMat(2, 3, 1), RandomMat(2, 3, 2))
  264. || test_binaryop(RandomMat(2, 3, 1), RandomMat(2, 3, 4))
  265. || test_binaryop(RandomMat(2, 3, 1), RandomMat(2, 3, 8))
  266. ;
  267. }
  268. int main()
  269. {
  270. SRAND(7767517);
  271. return 0
  272. || test_binaryop_1()
  273. || test_binaryop_2()
  274. || test_binaryop_3()
  275. || test_binaryop_4()
  276. || test_binaryop_5()
  277. || test_binaryop_6()
  278. || test_binaryop_7()
  279. || test_binaryop_8()
  280. || test_binaryop_9()
  281. || test_binaryop_10()
  282. || test_binaryop_11()
  283. || test_binaryop_12()
  284. || test_binaryop_13()
  285. || test_binaryop_14()
  286. || test_binaryop_15()
  287. || test_binaryop_16()
  288. || test_binaryop_17()
  289. || test_binaryop_18()
  290. || test_binaryop_19()
  291. || test_binaryop_s1()
  292. || test_binaryop_s2()
  293. || test_binaryop_s3()
  294. || test_binaryop_s4()
  295. ;
  296. }