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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/innerproduct.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. std::vector<ncnn::Mat> weights(bias ? 2 : 1);
  23. weights[0] = RandomMat(outch*a.w*a.h*a.c);
  24. if (bias)
  25. weights[1] = RandomMat(outch);
  26. ncnn::Option opt;
  27. opt.num_threads = 1;
  28. opt.use_vulkan_compute = true;
  29. opt.use_int8_inference = false;
  30. opt.use_fp16_packed = false;
  31. opt.use_fp16_storage = false;
  32. opt.use_fp16_arithmetic = false;
  33. opt.use_int8_storage = false;
  34. opt.use_int8_arithmetic = false;
  35. int ret = test_layer<ncnn::InnerProduct>("InnerProduct", pd, weights, opt, a);
  36. if (ret != 0)
  37. {
  38. fprintf(stderr, "test_innerproduct failed a.dims=%d a=(%d %d %d) outch=%d bias=%d\n", a.dims, a.w, a.h, a.c, outch, bias);
  39. }
  40. return ret;
  41. }
  42. static int test_innerproduct_0()
  43. {
  44. return 0
  45. || test_innerproduct(RandomMat(1, 3, 1), 1, 1)
  46. || test_innerproduct(RandomMat(3, 2, 2), 2, 1)
  47. || test_innerproduct(RandomMat(9, 3, 8), 7, 1)
  48. || test_innerproduct(RandomMat(2, 2, 8), 8, 1)
  49. || test_innerproduct(RandomMat(4, 3, 15), 8, 1)
  50. || test_innerproduct(RandomMat(6, 2, 16), 16, 1)
  51. || test_innerproduct(RandomMat(6, 2, 16), 7, 1)
  52. || test_innerproduct(RandomMat(6, 2, 5), 16, 1)
  53. ;
  54. }
  55. static int test_innerproduct_1()
  56. {
  57. return 0
  58. || test_innerproduct(RandomMat(1, 1), 1, 1)
  59. || test_innerproduct(RandomMat(3, 2), 2, 1)
  60. || test_innerproduct(RandomMat(9, 8), 7, 1)
  61. || test_innerproduct(RandomMat(2, 8), 8, 1)
  62. || test_innerproduct(RandomMat(4, 15), 8, 1)
  63. || test_innerproduct(RandomMat(6, 16), 16, 1)
  64. || test_innerproduct(RandomMat(6, 16), 7, 1)
  65. || test_innerproduct(RandomMat(6, 5), 16, 1)
  66. ;
  67. }
  68. static int test_innerproduct_2()
  69. {
  70. return 0
  71. || test_innerproduct(RandomMat(1), 1, 1)
  72. || test_innerproduct(RandomMat(2), 2, 1)
  73. || test_innerproduct(RandomMat(8), 7, 1)
  74. || test_innerproduct(RandomMat(8), 8, 1)
  75. || test_innerproduct(RandomMat(15), 8, 1)
  76. || test_innerproduct(RandomMat(16), 16, 1)
  77. || test_innerproduct(RandomMat(16), 7, 1)
  78. || test_innerproduct(RandomMat(5), 16, 1)
  79. ;
  80. }
  81. static int test_innerproduct_int8(const ncnn::Mat& a, int outch, int bias)
  82. {
  83. ncnn::ParamDict pd;
  84. pd.set(0, outch);// num_output
  85. pd.set(1, bias);// bias_term
  86. pd.set(2, outch*a.w*a.h*a.c);
  87. pd.set(8, 1);// int8_scale_term
  88. std::vector<ncnn::Mat> weights(bias ? 4 : 3);
  89. weights[0] = RandomMat(outch*a.w*a.h*a.c);
  90. if (bias)
  91. {
  92. weights[1] = RandomMat(outch);
  93. weights[2] = RandomMat(outch);
  94. weights[3] = RandomMat(1);
  95. }
  96. else
  97. {
  98. weights[1] = RandomMat(outch);
  99. weights[2] = RandomMat(1);
  100. }
  101. ncnn::Option opt;
  102. opt.num_threads = 1;
  103. opt.use_vulkan_compute = false;
  104. opt.use_int8_inference = true;
  105. opt.use_fp16_packed = false;
  106. opt.use_fp16_storage = false;
  107. opt.use_fp16_arithmetic = false;
  108. opt.use_int8_storage = false;
  109. opt.use_int8_arithmetic = false;
  110. int ret = test_layer<ncnn::InnerProduct>("InnerProduct", pd, weights, opt, a);
  111. if (ret != 0)
  112. {
  113. 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);
  114. }
  115. return 0;
  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. }
  133. int main()
  134. {
  135. SRAND(7767517);
  136. return 0
  137. || test_innerproduct_0()
  138. || test_innerproduct_1()
  139. || test_innerproduct_2()
  140. || test_innerproduct_3()
  141. ;
  142. }