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_innerproduct.cpp 9.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright 2020 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "testutil.h"
  4. static int test_innerproduct(const ncnn::Mat& a, int outch, int bias)
  5. {
  6. ncnn::ParamDict pd;
  7. pd.set(0, outch); // num_output
  8. pd.set(1, bias); // bias_term
  9. pd.set(2, outch * a.w * a.h * a.c);
  10. int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
  11. ncnn::Mat activation_params(2);
  12. activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
  13. activation_params[1] = RandomFloat(0, 1); // beta
  14. pd.set(9, activation_type);
  15. pd.set(10, activation_params);
  16. std::vector<ncnn::Mat> weights(bias ? 2 : 1);
  17. weights[0] = RandomMat(outch * a.w * a.h * a.c);
  18. if (bias)
  19. weights[1] = RandomMat(outch);
  20. int ret = test_layer("InnerProduct", pd, weights, a);
  21. if (ret != 0)
  22. {
  23. fprintf(stderr, "test_innerproduct failed a.dims=%d a=(%d %d %d) outch=%d bias=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, outch, bias, activation_type, activation_params[0], activation_params[1]);
  24. }
  25. return ret;
  26. }
  27. static int test_innerproduct_0()
  28. {
  29. return 0
  30. || test_innerproduct(RandomMat(1, 3, 1), 1, 1)
  31. || test_innerproduct(RandomMat(3, 2, 2), 2, 0)
  32. || test_innerproduct(RandomMat(9, 3, 8), 7, 1)
  33. || test_innerproduct(RandomMat(2, 2, 8), 8, 0)
  34. || test_innerproduct(RandomMat(4, 3, 15), 8, 1)
  35. || test_innerproduct(RandomMat(6, 2, 16), 16, 0)
  36. || test_innerproduct(RandomMat(6, 2, 16), 7, 1)
  37. || test_innerproduct(RandomMat(6, 2, 5), 16, 1);
  38. }
  39. static int test_innerproduct_1()
  40. {
  41. return 0
  42. || test_innerproduct(RandomMat(1, 1), 1, 1)
  43. || test_innerproduct(RandomMat(3, 2), 2, 0)
  44. || test_innerproduct(RandomMat(9, 8), 7, 1)
  45. || test_innerproduct(RandomMat(2, 8), 8, 0)
  46. || test_innerproduct(RandomMat(4, 15), 8, 1)
  47. || test_innerproduct(RandomMat(6, 16), 16, 0)
  48. || test_innerproduct(RandomMat(6, 16), 7, 1)
  49. || test_innerproduct(RandomMat(6, 5), 16, 1);
  50. }
  51. static int test_innerproduct_2()
  52. {
  53. return 0
  54. || test_innerproduct(RandomMat(1), 1, 1)
  55. || test_innerproduct(RandomMat(2), 2, 0)
  56. || test_innerproduct(RandomMat(8), 7, 1)
  57. || test_innerproduct(RandomMat(8), 8, 0)
  58. || test_innerproduct(RandomMat(15), 8, 1)
  59. || test_innerproduct(RandomMat(15), 15, 1)
  60. || test_innerproduct(RandomMat(16), 16, 0)
  61. || test_innerproduct(RandomMat(16), 7, 1)
  62. || test_innerproduct(RandomMat(5), 16, 0)
  63. || test_innerproduct(RandomMat(32), 16, 1)
  64. || test_innerproduct(RandomMat(12), 16, 0)
  65. || test_innerproduct(RandomMat(16), 12, 1)
  66. || test_innerproduct(RandomMat(24), 32, 1);
  67. }
  68. #if NCNN_INT8
  69. static int test_innerproduct_int8(const ncnn::Mat& a, int outch, int bias)
  70. {
  71. ncnn::ParamDict pd;
  72. pd.set(0, outch); // num_output
  73. pd.set(1, bias); // bias_term
  74. pd.set(2, outch * a.w * a.h * a.c);
  75. pd.set(8, 1); // int8_scale_term
  76. int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
  77. ncnn::Mat activation_params(2);
  78. activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
  79. activation_params[1] = RandomFloat(0, 1); // beta
  80. pd.set(9, activation_type);
  81. pd.set(10, activation_params);
  82. std::vector<ncnn::Mat> weights(bias ? 4 : 3);
  83. const int k = a.w * a.h * a.c;
  84. weights[0] = RandomMat(outch * k);
  85. ncnn::Mat weight_scales = scales_mat(weights[0], outch, k, k);
  86. ncnn::Mat input_scales = scales_mat(a, 1, k, k);
  87. if (bias)
  88. {
  89. weights[1] = RandomMat(outch);
  90. weights[2] = weight_scales;
  91. weights[3] = input_scales;
  92. }
  93. else
  94. {
  95. weights[1] = weight_scales;
  96. weights[2] = input_scales;
  97. }
  98. int flag = TEST_LAYER_DISABLE_GPU_TESTING;
  99. int ret = test_layer("InnerProduct", pd, weights, a, 0.001f, 0, flag);
  100. if (ret != 0)
  101. {
  102. fprintf(stderr, "test_innerproduct_int8 failed a.dims=%d a=(%d %d %d) outch=%d bias=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, outch, bias, activation_type, activation_params[0], activation_params[1]);
  103. }
  104. return ret;
  105. }
  106. static int test_innerproduct_3()
  107. {
  108. return 0
  109. || test_innerproduct_int8(RandomMat(1, 3, 1), 1, 1)
  110. || test_innerproduct_int8(RandomMat(3, 2, 2), 2, 1)
  111. || test_innerproduct_int8(RandomMat(5, 3, 3), 3, 1)
  112. || test_innerproduct_int8(RandomMat(7, 2, 3), 12, 1)
  113. || test_innerproduct_int8(RandomMat(9, 3, 4), 4, 1)
  114. || test_innerproduct_int8(RandomMat(2, 2, 7), 7, 1)
  115. || test_innerproduct_int8(RandomMat(4, 3, 8), 3, 1)
  116. || test_innerproduct_int8(RandomMat(6, 2, 8), 8, 1)
  117. || test_innerproduct_int8(RandomMat(8, 3, 15), 15, 1)
  118. || test_innerproduct_int8(RandomMat(7, 2, 16), 4, 1)
  119. || test_innerproduct_int8(RandomMat(6, 3, 16), 16, 1);
  120. }
  121. #endif // NCNN_INT8
  122. static int test_innerproduct_gemm(const ncnn::Mat& a, int outch, int bias)
  123. {
  124. ncnn::ParamDict pd;
  125. pd.set(0, outch);
  126. pd.set(1, bias);
  127. pd.set(2, outch * a.w);
  128. int activation_type = RAND() % 7;
  129. ncnn::Mat activation_params(2);
  130. activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
  131. activation_params[1] = RandomFloat(0, 1);
  132. pd.set(9, activation_type);
  133. pd.set(10, activation_params);
  134. std::vector<ncnn::Mat> weights(bias ? 2 : 1);
  135. weights[0] = RandomMat(outch * a.w);
  136. if (bias)
  137. weights[1] = RandomMat(outch);
  138. int ret = test_layer("InnerProduct", pd, weights, a);
  139. if (ret != 0)
  140. {
  141. fprintf(stderr, "test_innerproduct_gemm failed a.dims=%d a=(%d %d %d) outch=%d bias=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, outch, bias, activation_type, activation_params[0], activation_params[1]);
  142. }
  143. return ret;
  144. }
  145. static int test_innerproduct_4()
  146. {
  147. return 0
  148. || test_innerproduct_gemm(RandomMat(1, 1), 1, 1)
  149. || test_innerproduct_gemm(RandomMat(48, 1), 11, 1)
  150. || test_innerproduct_gemm(RandomMat(1, 5), 1, 1)
  151. || test_innerproduct_gemm(RandomMat(3, 2), 2, 0)
  152. || test_innerproduct_gemm(RandomMat(9, 8), 7, 1)
  153. || test_innerproduct_gemm(RandomMat(2, 8), 8, 0)
  154. || test_innerproduct_gemm(RandomMat(13, 20), 8, 1)
  155. || test_innerproduct_gemm(RandomMat(16, 20), 16, 0)
  156. || test_innerproduct_gemm(RandomMat(11, 24), 8, 0)
  157. || test_innerproduct_gemm(RandomMat(13, 24), 12, 1)
  158. || test_innerproduct_gemm(RandomMat(15, 20), 20, 1)
  159. || test_innerproduct_gemm(RandomMat(16, 20), 11, 1)
  160. || test_innerproduct_gemm(RandomMat(19, 16), 16, 1)
  161. || test_innerproduct_gemm(RandomMat(15, 15), 15, 1)
  162. || test_innerproduct_gemm(RandomMat(14, 15), 8, 1)
  163. || test_innerproduct_gemm(RandomMat(17, 15), 12, 1)
  164. || test_innerproduct_gemm(RandomMat(12, 16), 7, 1)
  165. || test_innerproduct_gemm(RandomMat(11, 32), 32, 1)
  166. || test_innerproduct_gemm(RandomMat(12, 32), 24, 1)
  167. || test_innerproduct_gemm(RandomMat(13, 32), 12, 1)
  168. || test_innerproduct_gemm(RandomMat(14, 32), 14, 1)
  169. || test_innerproduct_gemm(RandomMat(15, 32), 32, 1)
  170. || test_innerproduct_gemm(RandomMat(16, 24), 32, 1)
  171. || test_innerproduct_gemm(RandomMat(17, 20), 32, 1)
  172. || test_innerproduct_gemm(RandomMat(18, 14), 32, 1);
  173. }
  174. #if NCNN_INT8
  175. static int test_innerproduct_gemm_int8(const ncnn::Mat& a, int outch, int bias)
  176. {
  177. ncnn::ParamDict pd;
  178. pd.set(0, outch);
  179. pd.set(1, bias);
  180. pd.set(2, outch * a.w);
  181. pd.set(8, 1); // int8_scale_term
  182. std::vector<ncnn::Mat> weights(bias ? 4 : 3);
  183. const int k = a.w;
  184. weights[0] = RandomMat(outch * k);
  185. ncnn::Mat weight_scales = scales_mat(weights[0], outch, k, k);
  186. ncnn::Mat input_scales = scales_mat(a, 1, k, k);
  187. if (bias)
  188. {
  189. weights[1] = RandomMat(outch);
  190. weights[2] = weight_scales;
  191. weights[3] = input_scales;
  192. }
  193. else
  194. {
  195. weights[1] = weight_scales;
  196. weights[2] = input_scales;
  197. }
  198. int flag = TEST_LAYER_DISABLE_GPU_TESTING;
  199. int ret = test_layer("InnerProduct", pd, weights, a, 0.001f, 0, flag);
  200. if (ret != 0)
  201. {
  202. fprintf(stderr, "test_innerproduct_gemm_int8 failed a.dims=%d a=(%d %d %d) outch=%d bias=%d\n", a.dims, a.w, a.h, a.c, outch, bias);
  203. }
  204. return ret;
  205. }
  206. static int test_innerproduct_5()
  207. {
  208. return 0
  209. || test_innerproduct_gemm_int8(RandomMat(1, 5), 1, 1)
  210. || test_innerproduct_gemm_int8(RandomMat(3, 2), 2, 0)
  211. || test_innerproduct_gemm_int8(RandomMat(9, 8), 7, 1)
  212. || test_innerproduct_gemm_int8(RandomMat(2, 8), 8, 0)
  213. || test_innerproduct_gemm_int8(RandomMat(13, 12), 8, 1)
  214. || test_innerproduct_gemm_int8(RandomMat(16, 12), 16, 0)
  215. || test_innerproduct_gemm_int8(RandomMat(4, 15), 8, 1)
  216. || test_innerproduct_gemm_int8(RandomMat(6, 16), 16, 0)
  217. || test_innerproduct_gemm_int8(RandomMat(12, 16), 7, 1);
  218. }
  219. #endif // NCNN_INT8
  220. int main()
  221. {
  222. SRAND(7767517);
  223. #if NCNN_INT8
  224. return 0
  225. || test_innerproduct_0()
  226. || test_innerproduct_1()
  227. || test_innerproduct_2()
  228. || test_innerproduct_3()
  229. || test_innerproduct_4()
  230. || test_innerproduct_5();
  231. #else
  232. return 0
  233. || test_innerproduct_0()
  234. || test_innerproduct_1()
  235. || test_innerproduct_2()
  236. || test_innerproduct_4();
  237. #endif
  238. }