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

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