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_convolution_oom.cpp 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2024 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_convolution_oom(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias)
  16. {
  17. ncnn::Mat a = RandomMat(w, h, c);
  18. ncnn::ParamDict pd;
  19. pd.set(0, outch);
  20. pd.set(1, kernel);
  21. pd.set(2, dilation);
  22. pd.set(3, stride);
  23. pd.set(4, pad);
  24. pd.set(5, bias);
  25. pd.set(6, outch * c * kernel * kernel);
  26. int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
  27. ncnn::Mat activation_params(2);
  28. activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
  29. activation_params[1] = RandomFloat(0, 1); // beta
  30. pd.set(9, activation_type);
  31. pd.set(10, activation_params);
  32. std::vector<ncnn::Mat> weights(bias ? 2 : 1);
  33. weights[0] = RandomMat(outch * c * kernel * kernel);
  34. if (bias)
  35. weights[1] = RandomMat(outch);
  36. int ret = test_layer_oom("Convolution", pd, weights, a);
  37. if (ret != 0)
  38. {
  39. fprintf(stderr, "test_convolution_oom failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]);
  40. return ret;
  41. }
  42. return ret;
  43. }
  44. static int test_convolution_0()
  45. {
  46. return 0
  47. || test_convolution_oom(9, 7, 31, 63, 1, 1, 1, 0, 1)
  48. || test_convolution_oom(9, 7, 31, 63, 3, 1, 1, 1, 1);
  49. }
  50. #if NCNN_INT8
  51. static int test_convolution_oom_int8(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, bool requant = false)
  52. {
  53. ncnn::Mat a = RandomMat(w, h, c);
  54. ncnn::ParamDict pd;
  55. pd.set(0, outch);
  56. pd.set(1, kernel);
  57. pd.set(2, dilation);
  58. pd.set(3, stride);
  59. pd.set(4, pad);
  60. pd.set(5, bias);
  61. pd.set(6, outch * c * kernel * kernel);
  62. pd.set(8, requant ? 101 : 1); // int8_scale_term
  63. int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
  64. ncnn::Mat activation_params(2);
  65. activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
  66. activation_params[1] = RandomFloat(0, 1); // beta
  67. pd.set(9, activation_type);
  68. pd.set(10, activation_params);
  69. std::vector<ncnn::Mat> weights(bias ? 5 : 4);
  70. weights[0] = RandomMat(outch * c * kernel * kernel);
  71. ncnn::Mat weight_scales = scales_mat(weights[0], outch, c * kernel * kernel, c * kernel * kernel);
  72. ncnn::Mat input_scales = scales_mat(a, 1, w * h * c, a.cstep);
  73. ncnn::Mat top_scales = requant ? scales_mat(a, 1, w * h * c, a.cstep) : ncnn::Mat();
  74. if (kernel == 3 && dilation == 1 && stride == 1)
  75. {
  76. // test for 6bit quant
  77. for (int i = 0; i < weight_scales.w; i++)
  78. weight_scales[i] = weight_scales[i] / 4.f;
  79. }
  80. if (bias)
  81. {
  82. weights[1] = RandomMat(outch);
  83. weights[2] = weight_scales;
  84. weights[3] = input_scales;
  85. weights[4] = top_scales;
  86. }
  87. else
  88. {
  89. weights[1] = weight_scales;
  90. weights[2] = input_scales;
  91. weights[3] = top_scales;
  92. }
  93. int flag = TEST_LAYER_DISABLE_GPU_TESTING;
  94. int ret = test_layer_oom("Convolution", pd, weights, a, flag);
  95. if (ret != 0)
  96. {
  97. fprintf(stderr, "test_convolution_oom_int8 failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d requant=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, requant, activation_type, activation_params[0], activation_params[1]);
  98. return ret;
  99. }
  100. return ret;
  101. }
  102. static int test_convolution_1()
  103. {
  104. return 0
  105. || test_convolution_oom_int8(9, 7, 31, 63, 1, 1, 1, 0, 1)
  106. || test_convolution_oom_int8(9, 7, 31, 63, 3, 1, 1, 1, 1);
  107. }
  108. static int test_convolution_2()
  109. {
  110. return 0
  111. || test_convolution_oom_int8(9, 7, 31, 63, 1, 1, 1, 0, 1, true)
  112. || test_convolution_oom_int8(9, 7, 31, 63, 3, 1, 1, 1, 1, true);
  113. }
  114. #endif // NCNN_INT8
  115. int main()
  116. {
  117. SRAND(7767517);
  118. #if __mips__ || __loongarch64 || __riscv
  119. // TODO
  120. return 0;
  121. #endif
  122. #if NCNN_INT8
  123. return test_convolution_0() || test_convolution_1() || test_convolution_2();
  124. #else
  125. return test_convolution_0();
  126. #endif
  127. }