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_dequantize.cpp 7.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2021 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 "layer/dequantize.h"
  15. #include "testutil.h"
  16. static int RandomInt(int a = -10000, int b = 10000)
  17. {
  18. float random = ((float)RAND()) / (float)uint64_t(-1); //RAND_MAX;
  19. int diff = b - a;
  20. float r = random * diff;
  21. return a + (int)r;
  22. }
  23. static void RandomizeInt(ncnn::Mat& m, int a = -10000, int b = 10000)
  24. {
  25. for (size_t i = 0; i < m.total(); i++)
  26. {
  27. ((int*)m)[i] = RandomInt(a, b);
  28. }
  29. }
  30. static ncnn::Mat RandomIntMat(int w)
  31. {
  32. ncnn::Mat m(w);
  33. RandomizeInt(m);
  34. return m;
  35. }
  36. static ncnn::Mat RandomIntMat(int w, int h)
  37. {
  38. ncnn::Mat m(w, h);
  39. RandomizeInt(m);
  40. return m;
  41. }
  42. static ncnn::Mat RandomIntMat(int w, int h, int c)
  43. {
  44. ncnn::Mat m(w, h, c);
  45. RandomizeInt(m);
  46. return m;
  47. }
  48. static int test_dequantize(const ncnn::Mat& a, int scale_data_size, int bias_data_size)
  49. {
  50. ncnn::ParamDict pd;
  51. pd.set(0, scale_data_size);
  52. pd.set(1, bias_data_size);
  53. std::vector<ncnn::Mat> weights(bias_data_size ? 2 : 1);
  54. weights[0] = RandomMat(scale_data_size);
  55. if (bias_data_size)
  56. weights[1] = RandomMat(bias_data_size);
  57. int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING;
  58. int ret = test_layer<ncnn::Dequantize>("Dequantize", pd, weights, a, 0.001, 0, flag);
  59. if (ret != 0)
  60. {
  61. fprintf(stderr, "test_dequantize failed a.dims=%d a=(%d %d %d) scale_data_size=%d bias_data_size=%d\n", a.dims, a.w, a.h, a.c, scale_data_size, bias_data_size);
  62. }
  63. return ret;
  64. }
  65. static int test_dequantize_pack8(const ncnn::Mat& a, int scale_data_size, int bias_data_size)
  66. {
  67. ncnn::ParamDict pd;
  68. pd.set(0, scale_data_size);
  69. pd.set(1, bias_data_size);
  70. std::vector<ncnn::Mat> weights(bias_data_size ? 2 : 1);
  71. weights[0] = RandomMat(scale_data_size);
  72. if (bias_data_size)
  73. weights[1] = RandomMat(bias_data_size);
  74. int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING | TEST_LAYER_ENABLE_FORCE_INPUT_PACK8;
  75. int ret = test_layer<ncnn::Dequantize>("Dequantize", pd, weights, a, 0.001, 0, flag);
  76. if (ret != 0)
  77. {
  78. fprintf(stderr, "test_dequantize_pack8 failed a.dims=%d a=(%d %d %d) scale_data_size=%d bias_data_size=%d\n", a.dims, a.w, a.h, a.c, scale_data_size, bias_data_size);
  79. }
  80. return ret;
  81. }
  82. static int test_dequantize_0()
  83. {
  84. return 0
  85. || test_dequantize(RandomIntMat(5, 7, 24), 1, 24)
  86. || test_dequantize(RandomIntMat(5, 7, 24), 1, 1)
  87. || test_dequantize(RandomIntMat(5, 7, 24), 1, 0)
  88. || test_dequantize(RandomIntMat(5, 7, 24), 24, 24)
  89. || test_dequantize(RandomIntMat(5, 7, 24), 24, 1)
  90. || test_dequantize(RandomIntMat(5, 7, 24), 24, 0)
  91. || test_dequantize(RandomIntMat(7, 9, 12), 1, 12)
  92. || test_dequantize(RandomIntMat(7, 9, 12), 1, 1)
  93. || test_dequantize(RandomIntMat(7, 9, 12), 1, 0)
  94. || test_dequantize(RandomIntMat(7, 9, 12), 12, 12)
  95. || test_dequantize(RandomIntMat(7, 9, 12), 12, 1)
  96. || test_dequantize(RandomIntMat(7, 9, 12), 12, 0)
  97. || test_dequantize(RandomIntMat(3, 5, 13), 1, 13)
  98. || test_dequantize(RandomIntMat(3, 5, 13), 1, 1)
  99. || test_dequantize(RandomIntMat(3, 5, 13), 1, 0)
  100. || test_dequantize(RandomIntMat(3, 5, 13), 13, 13)
  101. || test_dequantize(RandomIntMat(3, 5, 13), 13, 1)
  102. || test_dequantize(RandomIntMat(3, 5, 13), 13, 0);
  103. }
  104. static int test_dequantize_1()
  105. {
  106. return 0
  107. || test_dequantize(RandomIntMat(15, 24), 1, 24)
  108. || test_dequantize(RandomIntMat(15, 24), 1, 1)
  109. || test_dequantize(RandomIntMat(15, 24), 1, 0)
  110. || test_dequantize(RandomIntMat(15, 24), 24, 24)
  111. || test_dequantize(RandomIntMat(15, 24), 24, 1)
  112. || test_dequantize(RandomIntMat(15, 24), 24, 0)
  113. || test_dequantize(RandomIntMat(17, 12), 1, 12)
  114. || test_dequantize(RandomIntMat(17, 12), 1, 1)
  115. || test_dequantize(RandomIntMat(17, 12), 1, 0)
  116. || test_dequantize(RandomIntMat(17, 12), 12, 12)
  117. || test_dequantize(RandomIntMat(17, 12), 12, 1)
  118. || test_dequantize(RandomIntMat(17, 12), 12, 0)
  119. || test_dequantize(RandomIntMat(19, 15), 1, 15)
  120. || test_dequantize(RandomIntMat(19, 15), 1, 1)
  121. || test_dequantize(RandomIntMat(19, 15), 1, 0)
  122. || test_dequantize(RandomIntMat(19, 15), 15, 15)
  123. || test_dequantize(RandomIntMat(19, 15), 15, 1)
  124. || test_dequantize(RandomIntMat(19, 15), 15, 0);
  125. }
  126. static int test_dequantize_2()
  127. {
  128. return 0
  129. || test_dequantize(RandomIntMat(128), 1, 128)
  130. || test_dequantize(RandomIntMat(128), 1, 1)
  131. || test_dequantize(RandomIntMat(128), 1, 0)
  132. || test_dequantize(RandomIntMat(128), 128, 128)
  133. || test_dequantize(RandomIntMat(128), 128, 1)
  134. || test_dequantize(RandomIntMat(128), 128, 0)
  135. || test_dequantize(RandomIntMat(124), 1, 124)
  136. || test_dequantize(RandomIntMat(124), 1, 1)
  137. || test_dequantize(RandomIntMat(124), 1, 0)
  138. || test_dequantize(RandomIntMat(124), 124, 124)
  139. || test_dequantize(RandomIntMat(124), 124, 1)
  140. || test_dequantize(RandomIntMat(124), 124, 0)
  141. || test_dequantize(RandomIntMat(127), 1, 127)
  142. || test_dequantize(RandomIntMat(127), 1, 1)
  143. || test_dequantize(RandomIntMat(127), 1, 0)
  144. || test_dequantize(RandomIntMat(127), 127, 127)
  145. || test_dequantize(RandomIntMat(127), 127, 1)
  146. || test_dequantize(RandomIntMat(127), 127, 0);
  147. }
  148. static int test_dequantize_3()
  149. {
  150. return 0
  151. || test_dequantize_pack8(RandomIntMat(5, 7, 24), 1, 24)
  152. || test_dequantize_pack8(RandomIntMat(5, 7, 24), 1, 1)
  153. || test_dequantize_pack8(RandomIntMat(5, 7, 24), 1, 0)
  154. || test_dequantize_pack8(RandomIntMat(5, 7, 24), 24, 24)
  155. || test_dequantize_pack8(RandomIntMat(5, 7, 24), 24, 1)
  156. || test_dequantize_pack8(RandomIntMat(5, 7, 24), 24, 0)
  157. || test_dequantize_pack8(RandomIntMat(15, 24), 1, 24)
  158. || test_dequantize_pack8(RandomIntMat(15, 24), 1, 1)
  159. || test_dequantize_pack8(RandomIntMat(15, 24), 1, 0)
  160. || test_dequantize_pack8(RandomIntMat(15, 24), 24, 24)
  161. || test_dequantize_pack8(RandomIntMat(15, 24), 24, 1)
  162. || test_dequantize_pack8(RandomIntMat(15, 24), 24, 0)
  163. || test_dequantize_pack8(RandomIntMat(128), 1, 128)
  164. || test_dequantize_pack8(RandomIntMat(128), 1, 1)
  165. || test_dequantize_pack8(RandomIntMat(128), 1, 0)
  166. || test_dequantize_pack8(RandomIntMat(128), 128, 128)
  167. || test_dequantize_pack8(RandomIntMat(128), 128, 1)
  168. || test_dequantize_pack8(RandomIntMat(128), 128, 0);
  169. }
  170. int main()
  171. {
  172. SRAND(7767517);
  173. return 0
  174. || test_dequantize_0()
  175. || test_dequantize_1()
  176. || test_dequantize_2()
  177. || test_dequantize_3();
  178. }