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_reduction.cpp 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #define OP_TYPE_MAX 11
  16. static int op_type = 0;
  17. static std::vector<int> IntArray(int a0)
  18. {
  19. std::vector<int> m(1);
  20. m[0] = a0;
  21. return m;
  22. }
  23. static std::vector<int> IntArray(int a0, int a1)
  24. {
  25. std::vector<int> m(2);
  26. m[0] = a0;
  27. m[1] = a1;
  28. return m;
  29. }
  30. static std::vector<int> IntArray(int a0, int a1, int a2)
  31. {
  32. std::vector<int> m(3);
  33. m[0] = a0;
  34. m[1] = a1;
  35. m[2] = a2;
  36. return m;
  37. }
  38. static std::vector<int> IntArray(int a0, int a1, int a2, int a3)
  39. {
  40. std::vector<int> m(4);
  41. m[0] = a0;
  42. m[1] = a1;
  43. m[2] = a2;
  44. m[3] = a3;
  45. return m;
  46. }
  47. static void print_int_array(const std::vector<int>& a)
  48. {
  49. fprintf(stderr, "[");
  50. for (size_t i = 0; i < a.size(); i++)
  51. {
  52. fprintf(stderr, " %d", a[i]);
  53. }
  54. fprintf(stderr, " ]");
  55. }
  56. static int test_reduction(const ncnn::Mat& _a, float coeff, int keepdims)
  57. {
  58. ncnn::Mat a = _a;
  59. if (op_type == 9 || op_type == 10)
  60. {
  61. // value must be positive for logsum and logsumexp
  62. Randomize(a, 0.001f, 2.f);
  63. }
  64. ncnn::ParamDict pd;
  65. pd.set(0, op_type);
  66. pd.set(1, 1); // reduce_all
  67. pd.set(2, coeff);
  68. pd.set(4, keepdims);
  69. std::vector<ncnn::Mat> weights(0);
  70. int ret = test_layer("Reduction", pd, weights, a);
  71. if (ret != 0)
  72. {
  73. fprintf(stderr, "test_reduction failed a.dims=%d a=(%d %d %d %d) op_type=%d coeff=%f keepdims=%d reduce_all=1\n", a.dims, a.w, a.h, a.d, a.c, op_type, coeff, keepdims);
  74. }
  75. return ret;
  76. }
  77. static int test_reduction(const ncnn::Mat& _a, float coeff, int keepdims, const std::vector<int>& axes_array)
  78. {
  79. ncnn::Mat a = _a;
  80. if (op_type == 9 || op_type == 10)
  81. {
  82. // value must be positive for logsum and logsumexp
  83. Randomize(a, 0.001f, 2.f);
  84. }
  85. ncnn::Mat axes(axes_array.size());
  86. {
  87. int* p = axes;
  88. for (size_t i = 0; i < axes_array.size(); i++)
  89. {
  90. p[i] = axes_array[i];
  91. }
  92. }
  93. ncnn::ParamDict pd;
  94. pd.set(0, op_type);
  95. pd.set(1, 0); // reduce_all
  96. pd.set(2, coeff);
  97. pd.set(3, axes);
  98. pd.set(4, keepdims);
  99. pd.set(5, 1); // fixbug0
  100. std::vector<ncnn::Mat> weights(0);
  101. int ret = test_layer("Reduction", pd, weights, a);
  102. if (ret != 0)
  103. {
  104. fprintf(stderr, "test_reduction failed a.dims=%d a=(%d %d %d %d) op_type=%d coeff=%f keepdims=%d", a.dims, a.w, a.h, a.d, a.c, op_type, coeff, keepdims);
  105. fprintf(stderr, " axes=");
  106. print_int_array(axes_array);
  107. fprintf(stderr, "\n");
  108. }
  109. return ret;
  110. }
  111. static int test_reduction_nd(const ncnn::Mat& a)
  112. {
  113. int ret1 = 0
  114. || test_reduction(a, 1.f, 0)
  115. || test_reduction(a, 2.f, 0)
  116. || test_reduction(a, 1.f, 1)
  117. || test_reduction(a, 2.f, 1)
  118. || test_reduction(a, 1.f, 0, IntArray(0))
  119. || test_reduction(a, 1.f, 1, IntArray(0));
  120. if (a.dims == 1 || ret1 != 0)
  121. return ret1;
  122. int ret2 = 0
  123. || test_reduction(a, 2.f, 0, IntArray(1))
  124. || test_reduction(a, 2.f, 1, IntArray(1))
  125. || test_reduction(a, 1.f, 0, IntArray(0, 1))
  126. || test_reduction(a, 1.f, 1, IntArray(0, 1));
  127. if (a.dims == 2 || ret2 != 0)
  128. return ret2;
  129. int ret3 = 0
  130. || test_reduction(a, 1.f, 0, IntArray(2))
  131. || test_reduction(a, 1.f, 1, IntArray(2))
  132. || test_reduction(a, 2.f, 0, IntArray(0, 2))
  133. || test_reduction(a, 2.f, 0, IntArray(1, 2))
  134. || test_reduction(a, 2.f, 1, IntArray(0, 2))
  135. || test_reduction(a, 2.f, 1, IntArray(1, 2))
  136. || test_reduction(a, 1.f, 0, IntArray(0, 1, 2))
  137. || test_reduction(a, 1.f, 1, IntArray(0, 1, 2));
  138. if (a.dims == 3 || ret3 != 0)
  139. return ret3;
  140. int ret4 = 0
  141. || test_reduction(a, 2.f, 0, IntArray(3))
  142. || test_reduction(a, 2.f, 1, IntArray(3))
  143. || test_reduction(a, 1.f, 0, IntArray(0, 3))
  144. || test_reduction(a, 1.f, 0, IntArray(1, 3))
  145. || test_reduction(a, 2.f, 0, IntArray(2, 3))
  146. || test_reduction(a, 1.f, 1, IntArray(0, 3))
  147. || test_reduction(a, 1.f, 1, IntArray(1, 3))
  148. || test_reduction(a, 2.f, 1, IntArray(2, 3))
  149. || test_reduction(a, 2.f, 0, IntArray(0, 1, 3))
  150. || test_reduction(a, 1.f, 0, IntArray(0, 2, 3))
  151. || test_reduction(a, 2.f, 0, IntArray(1, 2, 3))
  152. || test_reduction(a, 2.f, 1, IntArray(0, 1, 3))
  153. || test_reduction(a, 1.f, 1, IntArray(0, 2, 3))
  154. || test_reduction(a, 2.f, 1, IntArray(1, 2, 3))
  155. || test_reduction(a, 1.f, 0, IntArray(0, 1, 2, 3))
  156. || test_reduction(a, 1.f, 1, IntArray(0, 1, 2, 3));
  157. return ret4;
  158. }
  159. static int test_reduction_0()
  160. {
  161. ncnn::Mat a = RandomMat(5, 6, 7, 24);
  162. ncnn::Mat b = RandomMat(7, 8, 9, 12);
  163. ncnn::Mat c = RandomMat(3, 4, 5, 13);
  164. return 0
  165. || test_reduction_nd(a)
  166. || test_reduction_nd(b)
  167. || test_reduction_nd(c);
  168. }
  169. static int test_reduction_1()
  170. {
  171. ncnn::Mat a = RandomMat(5, 7, 24);
  172. ncnn::Mat b = RandomMat(7, 9, 12);
  173. ncnn::Mat c = RandomMat(3, 5, 13);
  174. return 0
  175. || test_reduction_nd(a)
  176. || test_reduction_nd(b)
  177. || test_reduction_nd(c);
  178. }
  179. static int test_reduction_2()
  180. {
  181. ncnn::Mat a = RandomMat(15, 24);
  182. ncnn::Mat b = RandomMat(17, 12);
  183. ncnn::Mat c = RandomMat(19, 15);
  184. return 0
  185. || test_reduction_nd(a)
  186. || test_reduction_nd(b)
  187. || test_reduction_nd(c);
  188. }
  189. static int test_reduction_3()
  190. {
  191. ncnn::Mat a = RandomMat(128);
  192. ncnn::Mat b = RandomMat(124);
  193. ncnn::Mat c = RandomMat(127);
  194. return 0
  195. || test_reduction_nd(a)
  196. || test_reduction_nd(b)
  197. || test_reduction_nd(c);
  198. }
  199. int main()
  200. {
  201. SRAND(7767517);
  202. for (op_type = 0; op_type < OP_TYPE_MAX; op_type++)
  203. {
  204. int ret = 0
  205. || test_reduction_0()
  206. || test_reduction_1()
  207. || test_reduction_2()
  208. || test_reduction_3();
  209. if (ret != 0)
  210. return ret;
  211. }
  212. return 0;
  213. }