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

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