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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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(int w, int h, int c, int outch, int bias, bool use_packing_layout)
  17. {
  18. ncnn::Mat a = RandomMat(w, h, c);
  19. ncnn::ParamDict pd;
  20. pd.set(0, outch);// num_output
  21. pd.set(1, bias);// bias_term
  22. pd.set(2, outch*w*h*c);
  23. std::vector<ncnn::Mat> weights(bias ? 2 : 1);
  24. weights[0] = RandomMat(outch*w*h*c);
  25. if (bias)
  26. weights[1] = RandomMat(outch);
  27. ncnn::ModelBinFromMatArray mb(weights.data());
  28. ncnn::Option opt;
  29. opt.num_threads = 1;
  30. opt.use_vulkan_compute = true;
  31. opt.use_int8_inference = false;
  32. opt.use_fp16_packed = false;
  33. opt.use_fp16_storage = false;
  34. opt.use_fp16_arithmetic = false;
  35. opt.use_int8_storage = false;
  36. opt.use_int8_arithmetic = false;
  37. opt.use_packing_layout = use_packing_layout;
  38. int ret = test_layer<ncnn::InnerProduct>("InnerProduct", pd, mb, opt, a);
  39. if (ret != 0)
  40. {
  41. fprintf(stderr, "test_innerproduct failed w=%d h=%d c=%d outch=%d bias=%d use_packing_layout=%d\n", w, h, c, outch, bias, use_packing_layout);
  42. }
  43. return ret;
  44. }
  45. static int test_innerproduct_0()
  46. {
  47. return 0
  48. || test_innerproduct(7, 3, 1, 1, 1, false)
  49. || test_innerproduct(7, 3, 2, 2, 1, false)
  50. || test_innerproduct(7, 3, 3, 3, 1, false)
  51. || test_innerproduct(7, 3, 4, 4, 1, false)
  52. || test_innerproduct(7, 3, 7, 7, 1, false)
  53. || test_innerproduct(7, 3, 8, 8, 1, false)
  54. || test_innerproduct(7, 3, 15, 15, 1, false)
  55. || test_innerproduct(7, 3, 16, 16, 1, false)
  56. || test_innerproduct(7, 3, 1, 1, 1, true)
  57. || test_innerproduct(7, 3, 2, 2, 1, true)
  58. || test_innerproduct(7, 3, 3, 3, 1, true)
  59. || test_innerproduct(7, 3, 3, 12, 1, true)
  60. || test_innerproduct(7, 3, 4, 4, 1, true)
  61. || test_innerproduct(7, 3, 8, 3, 1, true)
  62. || test_innerproduct(7, 3, 8, 8, 1, true)
  63. || test_innerproduct(7, 3, 16, 4, 1, true)
  64. || test_innerproduct(7, 3, 16, 16, 1, true)
  65. ;
  66. }
  67. static int test_innerproduct_int8(int w, int h, int c, int outch, int bias)
  68. {
  69. ncnn::Mat a = RandomMat(w, h, c);
  70. ncnn::ParamDict pd;
  71. pd.set(0, outch);// num_output
  72. pd.set(1, bias);// bias_term
  73. pd.set(2, outch*w*h*c);
  74. pd.set(8, 1);// int8_scale_term
  75. std::vector<ncnn::Mat> weights(bias ? 4 : 3);
  76. weights[0] = RandomMat(outch*w*h*c);
  77. if (bias)
  78. {
  79. weights[1] = RandomMat(outch);
  80. weights[2] = RandomMat(outch);
  81. weights[3] = RandomMat(1);
  82. }
  83. else
  84. {
  85. weights[1] = RandomMat(outch);
  86. weights[2] = RandomMat(1);
  87. }
  88. ncnn::ModelBinFromMatArray mb(weights.data());
  89. ncnn::Option opt;
  90. opt.num_threads = 1;
  91. opt.use_vulkan_compute = false;
  92. opt.use_int8_inference = true;
  93. opt.use_fp16_packed = false;
  94. opt.use_fp16_storage = false;
  95. opt.use_fp16_arithmetic = false;
  96. opt.use_int8_storage = false;
  97. opt.use_int8_arithmetic = false;
  98. opt.use_packing_layout = false;
  99. int ret = test_layer<ncnn::InnerProduct>("InnerProduct", pd, mb, opt, a);
  100. if (ret != 0)
  101. {
  102. fprintf(stderr, "test_innerproduct_int8 failed w=%d h=%d c=%d outch=%d bias=%d\n", w, h, c, outch, bias);
  103. }
  104. return 0;
  105. }
  106. static int test_innerproduct_1()
  107. {
  108. return 0
  109. || test_innerproduct_int8(7, 3, 1, 1, 1)
  110. || test_innerproduct_int8(7, 3, 2, 2, 1)
  111. || test_innerproduct_int8(7, 3, 3, 3, 1)
  112. || test_innerproduct_int8(7, 3, 4, 4, 1)
  113. || test_innerproduct_int8(7, 3, 7, 7, 1)
  114. || test_innerproduct_int8(7, 3, 8, 8, 1)
  115. || test_innerproduct_int8(7, 3, 15, 15, 1)
  116. || test_innerproduct_int8(7, 3, 16, 16, 1)
  117. || test_innerproduct_int8(7, 3, 1, 1, 1)
  118. || test_innerproduct_int8(7, 3, 2, 2, 1)
  119. || test_innerproduct_int8(7, 3, 3, 3, 1)
  120. || test_innerproduct_int8(7, 3, 3, 12, 1)
  121. || test_innerproduct_int8(7, 3, 4, 4, 1)
  122. || test_innerproduct_int8(7, 3, 8, 3, 1)
  123. || test_innerproduct_int8(7, 3, 8, 8, 1)
  124. || test_innerproduct_int8(7, 3, 16, 4, 1)
  125. || test_innerproduct_int8(7, 3, 16, 16, 1)
  126. ;
  127. }
  128. int main()
  129. {
  130. SRAND(7767517);
  131. return test_innerproduct_0() || test_innerproduct_1();
  132. }