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