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

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