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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "testutil.h"
  15. static int test_requantize(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;
  34. int ret = test_layer("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(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(a, scale_in_data_size, scale_out_data_size, bias_data_size, 0, 0.f, 0.f)
  45. || test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 1, 0.f, 0.f)
  46. || test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 2, RandomFloat(0, 1), 0.f)
  47. || test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 3, RandomFloat(-1, 0), RandomFloat(0, 1))
  48. || test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 4, 0.f, 0.f)
  49. || test_requantize(a, scale_in_data_size, scale_out_data_size, bias_data_size, 5, 0.f, 0.f);
  50. }
  51. static int test_requantize_pack8(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("Requantize", pd, weights, a, 1, 0, flag);
  71. if (ret != 0)
  72. {
  73. 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]);
  74. }
  75. return ret;
  76. }
  77. static int test_requantize_pack8(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(a, scale_in_data_size, scale_out_data_size, bias_data_size, 0, 0.f, 0.f)
  81. || test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 1, 0.f, 0.f)
  82. || test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 2, RandomFloat(0, 1), 0.f)
  83. || test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 3, RandomFloat(-1, 0), RandomFloat(0, 1))
  84. || test_requantize_pack8(a, scale_in_data_size, scale_out_data_size, bias_data_size, 4, 0.f, 0.f)
  85. || test_requantize_pack8(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(RandomIntMat(5, 7, 24), 1, 1, 24)
  91. || test_requantize(RandomIntMat(5, 7, 24), 1, 1, 1)
  92. || test_requantize(RandomIntMat(5, 7, 24), 1, 1, 0)
  93. || test_requantize(RandomIntMat(5, 7, 24), 24, 24, 24)
  94. || test_requantize(RandomIntMat(5, 7, 24), 24, 24, 1)
  95. || test_requantize(RandomIntMat(5, 7, 24), 24, 24, 0)
  96. || test_requantize(RandomIntMat(5, 7, 24), 1, 24, 24)
  97. || test_requantize(RandomIntMat(5, 7, 24), 1, 24, 1)
  98. || test_requantize(RandomIntMat(5, 7, 24), 1, 24, 0)
  99. || test_requantize(RandomIntMat(5, 7, 24), 24, 1, 24)
  100. || test_requantize(RandomIntMat(5, 7, 24), 24, 1, 1)
  101. || test_requantize(RandomIntMat(5, 7, 24), 24, 1, 0)
  102. || test_requantize(RandomIntMat(7, 9, 12), 1, 1, 12)
  103. || test_requantize(RandomIntMat(7, 9, 12), 1, 1, 1)
  104. || test_requantize(RandomIntMat(7, 9, 12), 1, 1, 0)
  105. || test_requantize(RandomIntMat(7, 9, 12), 12, 12, 12)
  106. || test_requantize(RandomIntMat(7, 9, 12), 12, 12, 1)
  107. || test_requantize(RandomIntMat(7, 9, 12), 12, 12, 0)
  108. || test_requantize(RandomIntMat(7, 9, 12), 1, 12, 12)
  109. || test_requantize(RandomIntMat(7, 9, 12), 1, 12, 1)
  110. || test_requantize(RandomIntMat(7, 9, 12), 1, 12, 0)
  111. || test_requantize(RandomIntMat(7, 9, 12), 12, 1, 12)
  112. || test_requantize(RandomIntMat(7, 9, 12), 12, 1, 1)
  113. || test_requantize(RandomIntMat(7, 9, 12), 12, 1, 0)
  114. || test_requantize(RandomIntMat(3, 5, 13), 1, 1, 13)
  115. || test_requantize(RandomIntMat(3, 5, 13), 1, 1, 1)
  116. || test_requantize(RandomIntMat(3, 5, 13), 1, 1, 0)
  117. || test_requantize(RandomIntMat(3, 5, 13), 13, 13, 13)
  118. || test_requantize(RandomIntMat(3, 5, 13), 13, 13, 1)
  119. || test_requantize(RandomIntMat(3, 5, 13), 13, 13, 0)
  120. || test_requantize(RandomIntMat(3, 5, 13), 1, 13, 13)
  121. || test_requantize(RandomIntMat(3, 5, 13), 1, 13, 1)
  122. || test_requantize(RandomIntMat(3, 5, 13), 1, 13, 0)
  123. || test_requantize(RandomIntMat(3, 5, 13), 13, 1, 13)
  124. || test_requantize(RandomIntMat(3, 5, 13), 13, 1, 1)
  125. || test_requantize(RandomIntMat(3, 5, 13), 13, 1, 0);
  126. }
  127. static int test_requantize_1()
  128. {
  129. return 0
  130. || test_requantize(RandomIntMat(15, 24), 1, 1, 24)
  131. || test_requantize(RandomIntMat(15, 24), 1, 1, 1)
  132. || test_requantize(RandomIntMat(15, 24), 1, 1, 0)
  133. || test_requantize(RandomIntMat(15, 24), 24, 24, 24)
  134. || test_requantize(RandomIntMat(15, 24), 24, 24, 1)
  135. || test_requantize(RandomIntMat(15, 24), 24, 24, 0)
  136. || test_requantize(RandomIntMat(15, 24), 1, 24, 24)
  137. || test_requantize(RandomIntMat(15, 24), 1, 24, 1)
  138. || test_requantize(RandomIntMat(15, 24), 1, 24, 0)
  139. || test_requantize(RandomIntMat(15, 24), 24, 1, 24)
  140. || test_requantize(RandomIntMat(15, 24), 24, 1, 1)
  141. || test_requantize(RandomIntMat(15, 24), 24, 1, 0)
  142. || test_requantize(RandomIntMat(17, 12), 1, 1, 12)
  143. || test_requantize(RandomIntMat(17, 12), 1, 1, 1)
  144. || test_requantize(RandomIntMat(17, 12), 1, 1, 0)
  145. || test_requantize(RandomIntMat(17, 12), 12, 12, 12)
  146. || test_requantize(RandomIntMat(17, 12), 12, 12, 1)
  147. || test_requantize(RandomIntMat(17, 12), 12, 12, 0)
  148. || test_requantize(RandomIntMat(17, 12), 1, 12, 12)
  149. || test_requantize(RandomIntMat(17, 12), 1, 12, 1)
  150. || test_requantize(RandomIntMat(17, 12), 1, 12, 0)
  151. || test_requantize(RandomIntMat(17, 12), 12, 1, 12)
  152. || test_requantize(RandomIntMat(17, 12), 12, 1, 1)
  153. || test_requantize(RandomIntMat(17, 12), 12, 1, 0)
  154. || test_requantize(RandomIntMat(19, 15), 1, 1, 15)
  155. || test_requantize(RandomIntMat(19, 15), 1, 1, 1)
  156. || test_requantize(RandomIntMat(19, 15), 1, 1, 0)
  157. || test_requantize(RandomIntMat(19, 15), 15, 15, 15)
  158. || test_requantize(RandomIntMat(19, 15), 15, 15, 1)
  159. || test_requantize(RandomIntMat(19, 15), 15, 15, 0)
  160. || test_requantize(RandomIntMat(19, 15), 1, 15, 15)
  161. || test_requantize(RandomIntMat(19, 15), 1, 15, 1)
  162. || test_requantize(RandomIntMat(19, 15), 1, 15, 0)
  163. || test_requantize(RandomIntMat(19, 15), 15, 1, 15)
  164. || test_requantize(RandomIntMat(19, 15), 15, 1, 1)
  165. || test_requantize(RandomIntMat(19, 15), 15, 1, 0);
  166. }
  167. static int test_requantize_2()
  168. {
  169. return 0
  170. || test_requantize(RandomIntMat(128), 1, 1, 128)
  171. || test_requantize(RandomIntMat(128), 1, 1, 1)
  172. || test_requantize(RandomIntMat(128), 1, 1, 0)
  173. || test_requantize(RandomIntMat(128), 128, 128, 128)
  174. || test_requantize(RandomIntMat(128), 128, 128, 1)
  175. || test_requantize(RandomIntMat(128), 128, 128, 0)
  176. || test_requantize(RandomIntMat(128), 1, 128, 128)
  177. || test_requantize(RandomIntMat(128), 1, 128, 1)
  178. || test_requantize(RandomIntMat(128), 1, 128, 0)
  179. || test_requantize(RandomIntMat(128), 128, 1, 128)
  180. || test_requantize(RandomIntMat(128), 128, 1, 1)
  181. || test_requantize(RandomIntMat(128), 128, 1, 0)
  182. || test_requantize(RandomIntMat(124), 1, 1, 124)
  183. || test_requantize(RandomIntMat(124), 1, 1, 1)
  184. || test_requantize(RandomIntMat(124), 1, 1, 0)
  185. || test_requantize(RandomIntMat(124), 124, 124, 124)
  186. || test_requantize(RandomIntMat(124), 124, 124, 1)
  187. || test_requantize(RandomIntMat(124), 124, 124, 0)
  188. || test_requantize(RandomIntMat(124), 1, 124, 124)
  189. || test_requantize(RandomIntMat(124), 1, 124, 1)
  190. || test_requantize(RandomIntMat(124), 1, 124, 0)
  191. || test_requantize(RandomIntMat(124), 124, 1, 124)
  192. || test_requantize(RandomIntMat(124), 124, 1, 1)
  193. || test_requantize(RandomIntMat(124), 124, 1, 0)
  194. || test_requantize(RandomIntMat(127), 1, 1, 127)
  195. || test_requantize(RandomIntMat(127), 1, 1, 1)
  196. || test_requantize(RandomIntMat(127), 1, 1, 0)
  197. || test_requantize(RandomIntMat(127), 127, 127, 127)
  198. || test_requantize(RandomIntMat(127), 127, 127, 1)
  199. || test_requantize(RandomIntMat(127), 127, 127, 0)
  200. || test_requantize(RandomIntMat(127), 1, 127, 127)
  201. || test_requantize(RandomIntMat(127), 1, 127, 1)
  202. || test_requantize(RandomIntMat(127), 1, 127, 0)
  203. || test_requantize(RandomIntMat(127), 127, 1, 127)
  204. || test_requantize(RandomIntMat(127), 127, 1, 1)
  205. || test_requantize(RandomIntMat(127), 127, 1, 0);
  206. }
  207. static int test_requantize_3()
  208. {
  209. return 0
  210. || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 1, 24)
  211. || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 1, 1)
  212. || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 1, 0)
  213. || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 24, 24)
  214. || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 24, 1)
  215. || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 24, 0)
  216. || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 24, 24)
  217. || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 24, 1)
  218. || test_requantize_pack8(RandomIntMat(5, 7, 24), 1, 24, 0)
  219. || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 1, 24)
  220. || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 1, 1)
  221. || test_requantize_pack8(RandomIntMat(5, 7, 24), 24, 1, 0)
  222. || test_requantize_pack8(RandomIntMat(15, 24), 1, 1, 24)
  223. || test_requantize_pack8(RandomIntMat(15, 24), 1, 1, 1)
  224. || test_requantize_pack8(RandomIntMat(15, 24), 1, 1, 0)
  225. || test_requantize_pack8(RandomIntMat(15, 24), 24, 24, 24)
  226. || test_requantize_pack8(RandomIntMat(15, 24), 24, 24, 1)
  227. || test_requantize_pack8(RandomIntMat(15, 24), 24, 24, 0)
  228. || test_requantize_pack8(RandomIntMat(15, 24), 1, 24, 24)
  229. || test_requantize_pack8(RandomIntMat(15, 24), 1, 24, 1)
  230. || test_requantize_pack8(RandomIntMat(15, 24), 1, 24, 0)
  231. || test_requantize_pack8(RandomIntMat(15, 24), 24, 1, 24)
  232. || test_requantize_pack8(RandomIntMat(15, 24), 24, 1, 1)
  233. || test_requantize_pack8(RandomIntMat(15, 24), 24, 1, 0)
  234. || test_requantize_pack8(RandomIntMat(128), 1, 1, 128)
  235. || test_requantize_pack8(RandomIntMat(128), 1, 1, 1)
  236. || test_requantize_pack8(RandomIntMat(128), 1, 1, 0)
  237. || test_requantize_pack8(RandomIntMat(128), 128, 128, 128)
  238. || test_requantize_pack8(RandomIntMat(128), 128, 128, 1)
  239. || test_requantize_pack8(RandomIntMat(128), 128, 128, 0)
  240. || test_requantize_pack8(RandomIntMat(128), 1, 128, 128)
  241. || test_requantize_pack8(RandomIntMat(128), 1, 128, 1)
  242. || test_requantize_pack8(RandomIntMat(128), 1, 128, 0)
  243. || test_requantize_pack8(RandomIntMat(128), 128, 1, 128)
  244. || test_requantize_pack8(RandomIntMat(128), 128, 1, 1)
  245. || test_requantize_pack8(RandomIntMat(128), 128, 1, 0);
  246. }
  247. int main()
  248. {
  249. SRAND(7767517);
  250. return 0
  251. || test_requantize_0()
  252. || test_requantize_1()
  253. || test_requantize_2()
  254. || test_requantize_3();
  255. }