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_requantize_oom.cpp 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_requantize_pack1_oom(const ncnn::Mat& a, int scale_in_data_size, int scale_out_data_size, int bias_data_size, int activation_type, float alpha, float beta)
  16. {
  17. ncnn::ParamDict pd;
  18. pd.set(0, scale_in_data_size);
  19. pd.set(1, scale_out_data_size);
  20. pd.set(2, bias_data_size);
  21. ncnn::Mat activation_params(2);
  22. activation_params[0] = alpha;
  23. activation_params[1] = beta;
  24. pd.set(3, activation_type);
  25. pd.set(4, activation_params);
  26. std::vector<ncnn::Mat> weights(bias_data_size ? 3 : 2);
  27. weights[0] = RandomMat(scale_in_data_size);
  28. weights[1] = RandomMat(scale_out_data_size);
  29. if (bias_data_size)
  30. weights[2] = RandomMat(bias_data_size);
  31. Randomize(weights[0], 0.0001, 0.001);
  32. Randomize(weights[1], 10, 100);
  33. int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING | TEST_LAYER_DISABLE_AUTO_INPUT_PACKING;
  34. int ret = test_layer_oom("Requantize", pd, weights, a, flag);
  35. if (ret != 0)
  36. {
  37. fprintf(stderr, "test_requantize_pack1_oom failed a.dims=%d a=(%d %d %d) scale_in_data_size=%d scale_out_data_size=%d bias_data_size=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, scale_in_data_size, scale_out_data_size, bias_data_size, activation_type, activation_params[0], activation_params[1]);
  38. }
  39. return ret;
  40. }
  41. static int test_requantize_pack1_oom(const ncnn::Mat& a, int scale_in_data_size, int scale_out_data_size, int bias_data_size)
  42. {
  43. return 0
  44. || test_requantize_pack1_oom(a, scale_in_data_size, scale_out_data_size, bias_data_size, 0, 0.f, 0.f)
  45. || test_requantize_pack1_oom(a, scale_in_data_size, scale_out_data_size, bias_data_size, 1, 0.f, 0.f)
  46. || test_requantize_pack1_oom(a, scale_in_data_size, scale_out_data_size, bias_data_size, 2, RandomFloat(0, 1), 0.f)
  47. || test_requantize_pack1_oom(a, scale_in_data_size, scale_out_data_size, bias_data_size, 3, RandomFloat(-1, 0), RandomFloat(0, 1))
  48. || test_requantize_pack1_oom(a, scale_in_data_size, scale_out_data_size, bias_data_size, 4, 0.f, 0.f)
  49. || test_requantize_pack1_oom(a, scale_in_data_size, scale_out_data_size, bias_data_size, 5, 0.f, 0.f);
  50. }
  51. static int test_requantize_pack8_oom(const ncnn::Mat& a, int scale_in_data_size, int scale_out_data_size, int bias_data_size, int activation_type, float alpha, float beta)
  52. {
  53. ncnn::ParamDict pd;
  54. pd.set(0, scale_in_data_size);
  55. pd.set(1, scale_out_data_size);
  56. pd.set(2, bias_data_size);
  57. ncnn::Mat activation_params(2);
  58. activation_params[0] = alpha;
  59. activation_params[1] = beta;
  60. pd.set(3, activation_type);
  61. pd.set(4, activation_params);
  62. std::vector<ncnn::Mat> weights(bias_data_size ? 3 : 2);
  63. weights[0] = RandomMat(scale_in_data_size);
  64. weights[1] = RandomMat(scale_out_data_size);
  65. if (bias_data_size)
  66. weights[2] = RandomMat(bias_data_size);
  67. Randomize(weights[0], 0.0001, 0.001);
  68. Randomize(weights[1], 10, 100);
  69. int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING | TEST_LAYER_ENABLE_FORCE_INPUT_PACK8;
  70. int ret = test_layer_oom("Requantize", pd, weights, a, flag);
  71. if (ret != 0)
  72. {
  73. fprintf(stderr, "test_requantize_pack8_oom failed a.dims=%d a=(%d %d %d) scale_in_data_size=%d scale_out_data_size=%d bias_data_size=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, scale_in_data_size, scale_out_data_size, bias_data_size, activation_type, activation_params[0], activation_params[1]);
  74. }
  75. return ret;
  76. }
  77. static int test_requantize_pack8_oom(const ncnn::Mat& a, int scale_in_data_size, int scale_out_data_size, int bias_data_size)
  78. {
  79. return 0
  80. || test_requantize_pack8_oom(a, scale_in_data_size, scale_out_data_size, bias_data_size, 0, 0.f, 0.f)
  81. || test_requantize_pack8_oom(a, scale_in_data_size, scale_out_data_size, bias_data_size, 1, 0.f, 0.f)
  82. || test_requantize_pack8_oom(a, scale_in_data_size, scale_out_data_size, bias_data_size, 2, RandomFloat(0, 1), 0.f)
  83. || test_requantize_pack8_oom(a, scale_in_data_size, scale_out_data_size, bias_data_size, 3, RandomFloat(-1, 0), RandomFloat(0, 1))
  84. || test_requantize_pack8_oom(a, scale_in_data_size, scale_out_data_size, bias_data_size, 4, 0.f, 0.f)
  85. || test_requantize_pack8_oom(a, scale_in_data_size, scale_out_data_size, bias_data_size, 5, 0.f, 0.f);
  86. }
  87. static int test_requantize_0()
  88. {
  89. return 0
  90. || test_requantize_pack1_oom(RandomIntMat(7, 9, 12), 12, 12, 12)
  91. || test_requantize_pack1_oom(RandomIntMat(3, 5, 13), 13, 13, 13);
  92. }
  93. static int test_requantize_1()
  94. {
  95. return 0
  96. || test_requantize_pack1_oom(RandomIntMat(17, 12), 12, 12, 12)
  97. || test_requantize_pack1_oom(RandomIntMat(19, 15), 15, 15, 15);
  98. }
  99. static int test_requantize_2()
  100. {
  101. return test_requantize_pack1_oom(RandomIntMat(124), 1, 1, 1);
  102. }
  103. static int test_requantize_3()
  104. {
  105. return 0
  106. || test_requantize_pack8_oom(RandomIntMat(5, 7, 24), 24, 24, 24)
  107. || test_requantize_pack8_oom(RandomIntMat(15, 24), 24, 24, 24)
  108. || test_requantize_pack8_oom(RandomIntMat(128), 1, 1, 1);
  109. }
  110. int main()
  111. {
  112. SRAND(7767517);
  113. return 0
  114. || test_requantize_0()
  115. || test_requantize_1()
  116. || test_requantize_2()
  117. || test_requantize_3();
  118. }