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

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