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.cpp 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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/requantize.h"
  15. #include "testutil.h"
  16. static int test_requantize(const ncnn::Mat& a, int scale_in_data_size, int scale_out_data_size, int bias_data_size)
  17. {
  18. ncnn::ParamDict pd;
  19. pd.set(0, scale_in_data_size);
  20. pd.set(1, scale_out_data_size);
  21. pd.set(2, bias_data_size);
  22. int activation_type = RAND() % 6; // 0 1 2 3 4 5
  23. ncnn::Mat activation_params(2);
  24. activation_params[0] = RandomFloat(-1, 0); // alpha
  25. activation_params[1] = RandomFloat(0, 1); // beta
  26. pd.set(3, activation_type);
  27. pd.set(4, activation_params);
  28. std::vector<ncnn::Mat> weights(bias_data_size ? 3 : 2);
  29. weights[0] = RandomMat(scale_in_data_size);
  30. weights[1] = RandomMat(scale_out_data_size);
  31. if (bias_data_size)
  32. weights[2] = RandomMat(bias_data_size);
  33. int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING;
  34. int ret = test_layer<ncnn::Requantize>("Requantize", pd, weights, a, 1, 0, flag);
  35. if (ret != 0)
  36. {
  37. fprintf(stderr, "test_requantize 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_pack8(const ncnn::Mat& a, int scale_in_data_size, int scale_out_data_size, int bias_data_size)
  42. {
  43. ncnn::ParamDict pd;
  44. pd.set(0, scale_in_data_size);
  45. pd.set(1, scale_out_data_size);
  46. pd.set(2, bias_data_size);
  47. int activation_type = RAND() % 6; // 0 1 2 3 4 5
  48. ncnn::Mat activation_params(2);
  49. activation_params[0] = RandomFloat(-1, 0); // alpha
  50. activation_params[1] = RandomFloat(0, 1); // beta
  51. pd.set(3, activation_type);
  52. pd.set(4, activation_params);
  53. std::vector<ncnn::Mat> weights(bias_data_size ? 3 : 2);
  54. weights[0] = RandomMat(scale_in_data_size);
  55. weights[1] = RandomMat(scale_out_data_size);
  56. if (bias_data_size)
  57. weights[2] = RandomMat(bias_data_size);
  58. int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING | TEST_LAYER_ENABLE_FORCE_INPUT_PACK8;
  59. int ret = test_layer<ncnn::Requantize>("Requantize", pd, weights, a, 1, 0, flag);
  60. if (ret != 0)
  61. {
  62. fprintf(stderr, "test_requantize_pack8 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]);
  63. }
  64. return ret;
  65. }
  66. static int test_requantize_0()
  67. {
  68. return 0
  69. || test_requantize(RandomIntMat(5, 7, 24), 1, 1, 24)
  70. || test_requantize(RandomIntMat(5, 7, 24), 1, 1, 1)
  71. || test_requantize(RandomIntMat(5, 7, 24), 1, 1, 0)
  72. || test_requantize(RandomIntMat(5, 7, 24), 24, 24, 24)
  73. || test_requantize(RandomIntMat(5, 7, 24), 24, 24, 1)
  74. || test_requantize(RandomIntMat(5, 7, 24), 24, 24, 0)
  75. || test_requantize(RandomIntMat(5, 7, 24), 1, 24, 24)
  76. || test_requantize(RandomIntMat(5, 7, 24), 1, 24, 1)
  77. || test_requantize(RandomIntMat(5, 7, 24), 1, 24, 0)
  78. || test_requantize(RandomIntMat(5, 7, 24), 24, 1, 24)
  79. || test_requantize(RandomIntMat(5, 7, 24), 24, 1, 1)
  80. || test_requantize(RandomIntMat(5, 7, 24), 24, 1, 0)
  81. || test_requantize(RandomIntMat(7, 9, 12), 1, 1, 12)
  82. || test_requantize(RandomIntMat(7, 9, 12), 1, 1, 1)
  83. || test_requantize(RandomIntMat(7, 9, 12), 1, 1, 0)
  84. || test_requantize(RandomIntMat(7, 9, 12), 12, 12, 12)
  85. || test_requantize(RandomIntMat(7, 9, 12), 12, 12, 1)
  86. || test_requantize(RandomIntMat(7, 9, 12), 12, 12, 0)
  87. || test_requantize(RandomIntMat(7, 9, 12), 1, 12, 12)
  88. || test_requantize(RandomIntMat(7, 9, 12), 1, 12, 1)
  89. || test_requantize(RandomIntMat(7, 9, 12), 1, 12, 0)
  90. || test_requantize(RandomIntMat(7, 9, 12), 12, 1, 12)
  91. || test_requantize(RandomIntMat(7, 9, 12), 12, 1, 1)
  92. || test_requantize(RandomIntMat(7, 9, 12), 12, 1, 0)
  93. || test_requantize(RandomIntMat(3, 5, 13), 1, 1, 13)
  94. || test_requantize(RandomIntMat(3, 5, 13), 1, 1, 1)
  95. || test_requantize(RandomIntMat(3, 5, 13), 1, 1, 0)
  96. || test_requantize(RandomIntMat(3, 5, 13), 13, 13, 13)
  97. || test_requantize(RandomIntMat(3, 5, 13), 13, 13, 1)
  98. || test_requantize(RandomIntMat(3, 5, 13), 13, 13, 0)
  99. || test_requantize(RandomIntMat(3, 5, 13), 1, 13, 13)
  100. || test_requantize(RandomIntMat(3, 5, 13), 1, 13, 1)
  101. || test_requantize(RandomIntMat(3, 5, 13), 1, 13, 0)
  102. || test_requantize(RandomIntMat(3, 5, 13), 13, 1, 13)
  103. || test_requantize(RandomIntMat(3, 5, 13), 13, 1, 1)
  104. || test_requantize(RandomIntMat(3, 5, 13), 13, 1, 0);
  105. }
  106. static int test_requantize_1()
  107. {
  108. return 0
  109. || test_requantize(RandomIntMat(15, 24), 1, 1, 24)
  110. || test_requantize(RandomIntMat(15, 24), 1, 1, 1)
  111. || test_requantize(RandomIntMat(15, 24), 1, 1, 0)
  112. || test_requantize(RandomIntMat(15, 24), 24, 24, 24)
  113. || test_requantize(RandomIntMat(15, 24), 24, 24, 1)
  114. || test_requantize(RandomIntMat(15, 24), 24, 24, 0)
  115. || test_requantize(RandomIntMat(15, 24), 1, 24, 24)
  116. || test_requantize(RandomIntMat(15, 24), 1, 24, 1)
  117. || test_requantize(RandomIntMat(15, 24), 1, 24, 0)
  118. || test_requantize(RandomIntMat(15, 24), 24, 1, 24)
  119. || test_requantize(RandomIntMat(15, 24), 24, 1, 1)
  120. || test_requantize(RandomIntMat(15, 24), 24, 1, 0)
  121. || test_requantize(RandomIntMat(17, 12), 1, 1, 12)
  122. || test_requantize(RandomIntMat(17, 12), 1, 1, 1)
  123. || test_requantize(RandomIntMat(17, 12), 1, 1, 0)
  124. || test_requantize(RandomIntMat(17, 12), 12, 12, 12)
  125. || test_requantize(RandomIntMat(17, 12), 12, 12, 1)
  126. || test_requantize(RandomIntMat(17, 12), 12, 12, 0)
  127. || test_requantize(RandomIntMat(17, 12), 1, 12, 12)
  128. || test_requantize(RandomIntMat(17, 12), 1, 12, 1)
  129. || test_requantize(RandomIntMat(17, 12), 1, 12, 0)
  130. || test_requantize(RandomIntMat(17, 12), 12, 1, 12)
  131. || test_requantize(RandomIntMat(17, 12), 12, 1, 1)
  132. || test_requantize(RandomIntMat(17, 12), 12, 1, 0)
  133. || test_requantize(RandomIntMat(19, 15), 1, 1, 15)
  134. || test_requantize(RandomIntMat(19, 15), 1, 1, 1)
  135. || test_requantize(RandomIntMat(19, 15), 1, 1, 0)
  136. || test_requantize(RandomIntMat(19, 15), 15, 15, 15)
  137. || test_requantize(RandomIntMat(19, 15), 15, 15, 1)
  138. || test_requantize(RandomIntMat(19, 15), 15, 15, 0)
  139. || test_requantize(RandomIntMat(19, 15), 1, 15, 15)
  140. || test_requantize(RandomIntMat(19, 15), 1, 15, 1)
  141. || test_requantize(RandomIntMat(19, 15), 1, 15, 0)
  142. || test_requantize(RandomIntMat(19, 15), 15, 1, 15)
  143. || test_requantize(RandomIntMat(19, 15), 15, 1, 1)
  144. || test_requantize(RandomIntMat(19, 15), 15, 1, 0);
  145. }
  146. static int test_requantize_2()
  147. {
  148. return 0
  149. || test_requantize(RandomIntMat(128), 1, 1, 128)
  150. || test_requantize(RandomIntMat(128), 1, 1, 1)
  151. || test_requantize(RandomIntMat(128), 1, 1, 0)
  152. || test_requantize(RandomIntMat(128), 128, 128, 128)
  153. || test_requantize(RandomIntMat(128), 128, 128, 1)
  154. || test_requantize(RandomIntMat(128), 128, 128, 0)
  155. || test_requantize(RandomIntMat(128), 1, 128, 128)
  156. || test_requantize(RandomIntMat(128), 1, 128, 1)
  157. || test_requantize(RandomIntMat(128), 1, 128, 0)
  158. || test_requantize(RandomIntMat(128), 128, 1, 128)
  159. || test_requantize(RandomIntMat(128), 128, 1, 1)
  160. || test_requantize(RandomIntMat(128), 128, 1, 0)
  161. || test_requantize(RandomIntMat(124), 1, 1, 124)
  162. || test_requantize(RandomIntMat(124), 1, 1, 1)
  163. || test_requantize(RandomIntMat(124), 1, 1, 0)
  164. || test_requantize(RandomIntMat(124), 124, 124, 124)
  165. || test_requantize(RandomIntMat(124), 124, 124, 1)
  166. || test_requantize(RandomIntMat(124), 124, 124, 0)
  167. || test_requantize(RandomIntMat(124), 1, 124, 124)
  168. || test_requantize(RandomIntMat(124), 1, 124, 1)
  169. || test_requantize(RandomIntMat(124), 1, 124, 0)
  170. || test_requantize(RandomIntMat(124), 124, 1, 124)
  171. || test_requantize(RandomIntMat(124), 124, 1, 1)
  172. || test_requantize(RandomIntMat(124), 124, 1, 0)
  173. || test_requantize(RandomIntMat(127), 1, 1, 127)
  174. || test_requantize(RandomIntMat(127), 1, 1, 1)
  175. || test_requantize(RandomIntMat(127), 1, 1, 0)
  176. || test_requantize(RandomIntMat(127), 127, 127, 127)
  177. || test_requantize(RandomIntMat(127), 127, 127, 1)
  178. || test_requantize(RandomIntMat(127), 127, 127, 0)
  179. || test_requantize(RandomIntMat(127), 1, 127, 127)
  180. || test_requantize(RandomIntMat(127), 1, 127, 1)
  181. || test_requantize(RandomIntMat(127), 1, 127, 0)
  182. || test_requantize(RandomIntMat(127), 127, 1, 127)
  183. || test_requantize(RandomIntMat(127), 127, 1, 1)
  184. || test_requantize(RandomIntMat(127), 127, 1, 0);
  185. }
  186. static int test_requantize_3()
  187. {
  188. return 0
  189. || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 1, 24)
  190. || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 1, 1)
  191. || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 1, 0)
  192. || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 24, 24)
  193. || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 24, 1)
  194. || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 24, 0)
  195. || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 24, 24)
  196. || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 24, 1)
  197. || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 24, 0)
  198. || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 1, 24)
  199. || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 1, 1)
  200. || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 1, 0)
  201. || test_requantize_pack8(RandomIntMat(15, 24), 1, 1, 24)
  202. || test_requantize_pack8(RandomIntMat(15, 24), 1, 1, 1)
  203. || test_requantize_pack8(RandomIntMat(15, 24), 1, 1, 0)
  204. || test_requantize_pack8(RandomIntMat(15, 24), 24, 24, 24)
  205. || test_requantize_pack8(RandomIntMat(15, 24), 24, 24, 1)
  206. || test_requantize_pack8(RandomIntMat(15, 24), 24, 24, 0)
  207. || test_requantize_pack8(RandomIntMat(15, 24), 1, 24, 24)
  208. || test_requantize_pack8(RandomIntMat(15, 24), 1, 24, 1)
  209. || test_requantize_pack8(RandomIntMat(15, 24), 1, 24, 0)
  210. || test_requantize_pack8(RandomIntMat(15, 24), 24, 1, 24)
  211. || test_requantize_pack8(RandomIntMat(15, 24), 24, 1, 1)
  212. || test_requantize_pack8(RandomIntMat(15, 24), 24, 1, 0)
  213. || test_requantize_pack8(RandomIntMat(128), 1, 1, 128)
  214. || test_requantize_pack8(RandomIntMat(128), 1, 1, 1)
  215. || test_requantize_pack8(RandomIntMat(128), 1, 1, 0)
  216. || test_requantize_pack8(RandomIntMat(128), 128, 128, 128)
  217. || test_requantize_pack8(RandomIntMat(128), 128, 128, 1)
  218. || test_requantize_pack8(RandomIntMat(128), 128, 128, 0)
  219. || test_requantize_pack8(RandomIntMat(128), 1, 128, 128)
  220. || test_requantize_pack8(RandomIntMat(128), 1, 128, 1)
  221. || test_requantize_pack8(RandomIntMat(128), 1, 128, 0)
  222. || test_requantize_pack8(RandomIntMat(128), 128, 1, 128)
  223. || test_requantize_pack8(RandomIntMat(128), 128, 1, 1)
  224. || test_requantize_pack8(RandomIntMat(128), 128, 1, 0);
  225. }
  226. int main()
  227. {
  228. SRAND(7767517);
  229. return 0
  230. || test_requantize_0()
  231. || test_requantize_1()
  232. || test_requantize_2()
  233. || test_requantize_3();
  234. }