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

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