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_tile.cpp 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright 2022 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "testutil.h"
  4. static int test_tile(const ncnn::Mat& a, int axis, int tiles)
  5. {
  6. ncnn::ParamDict pd;
  7. pd.set(0, axis);
  8. pd.set(1, tiles);
  9. std::vector<ncnn::Mat> weights(0);
  10. int ret = test_layer("Tile", pd, weights, a);
  11. if (ret != 0)
  12. {
  13. fprintf(stderr, "test_tile failed a.dims=%d a=(%d %d %d %d) axis=%d tiles=%d\n", a.dims, a.w, a.h, a.d, a.c, axis, tiles);
  14. }
  15. return ret;
  16. }
  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_tile(const ncnn::Mat& a, const std::vector<int>& repeats_array)
  57. {
  58. ncnn::Mat repeats(repeats_array.size());
  59. {
  60. int* p = repeats;
  61. for (size_t i = 0; i < repeats_array.size(); i++)
  62. {
  63. p[i] = repeats_array[i];
  64. }
  65. }
  66. ncnn::ParamDict pd;
  67. pd.set(2, repeats);
  68. std::vector<ncnn::Mat> weights(0);
  69. int ret = test_layer("Tile", pd, weights, a);
  70. if (ret != 0)
  71. {
  72. fprintf(stderr, "test_tile failed a.dims=%d a=(%d %d %d %d) repeats=", a.dims, a.w, a.h, a.d, a.c);
  73. print_int_array(repeats_array);
  74. fprintf(stderr, "\n");
  75. }
  76. return ret;
  77. }
  78. static int test_tile_0()
  79. {
  80. ncnn::Mat a = RandomMat(5, 6, 7, 24);
  81. ncnn::Mat b = RandomMat(7, 8, 9, 12);
  82. ncnn::Mat c = RandomMat(3, 4, 5, 13);
  83. return 0
  84. || test_tile(a, 0, 3)
  85. || test_tile(a, 1, 4)
  86. || test_tile(a, 2, 5)
  87. || test_tile(a, 3, 2)
  88. || test_tile(b, 0, 3)
  89. || test_tile(b, 1, 4)
  90. || test_tile(b, 2, 1)
  91. || test_tile(b, 3, 2)
  92. || test_tile(c, 0, 3)
  93. || test_tile(c, 1, 4)
  94. || test_tile(c, 2, 5)
  95. || test_tile(c, 3, 2)
  96. || test_tile(a, IntArray(3))
  97. || test_tile(a, IntArray(2, 4))
  98. || test_tile(a, IntArray(2, 2, 5))
  99. || test_tile(a, IntArray(3, 1, 3, 2))
  100. || test_tile(b, IntArray(3, 1))
  101. || test_tile(b, IntArray(4, 1, 4))
  102. || test_tile(b, IntArray(2, 2, 2, 1))
  103. || test_tile(b, IntArray(3, 2, 1))
  104. || test_tile(c, IntArray(3))
  105. || test_tile(c, IntArray(1, 1, 4))
  106. || test_tile(c, IntArray(2, 2, 5))
  107. || test_tile(c, IntArray(3, 2, 1, 9));
  108. }
  109. static int test_tile_1()
  110. {
  111. ncnn::Mat a = RandomMat(5, 7, 24);
  112. ncnn::Mat b = RandomMat(7, 9, 12);
  113. ncnn::Mat c = RandomMat(3, 5, 13);
  114. return 0
  115. || test_tile(a, 0, 5)
  116. || test_tile(a, 1, 4)
  117. || test_tile(a, 2, 4)
  118. || test_tile(b, 0, 3)
  119. || test_tile(b, 1, 3)
  120. || test_tile(b, 2, 3)
  121. || test_tile(c, 0, 1)
  122. || test_tile(c, 1, 2)
  123. || test_tile(c, 2, 2)
  124. || test_tile(a, IntArray(5))
  125. || test_tile(a, IntArray(1, 4))
  126. || test_tile(a, IntArray(2, 1, 4))
  127. || test_tile(a, IntArray(1, 2, 1, 4))
  128. || test_tile(b, IntArray(3))
  129. || test_tile(b, IntArray(1, 3, 3))
  130. || test_tile(b, IntArray(2, 3))
  131. || test_tile(b, IntArray(2, 3, 3, 3))
  132. || test_tile(c, IntArray(1))
  133. || test_tile(c, IntArray(2, 1))
  134. || test_tile(c, IntArray(2, 2, 2))
  135. || test_tile(c, IntArray(2, 1, 2, 1));
  136. }
  137. static int test_tile_2()
  138. {
  139. ncnn::Mat a = RandomMat(15, 24);
  140. ncnn::Mat b = RandomMat(17, 12);
  141. ncnn::Mat c = RandomMat(19, 13);
  142. return 0
  143. || test_tile(a, 0, 2)
  144. || test_tile(a, 1, 1)
  145. || test_tile(b, 0, 3)
  146. || test_tile(b, 1, 4)
  147. || test_tile(c, 0, 5)
  148. || test_tile(c, 1, 6)
  149. || test_tile(a, IntArray(2))
  150. || test_tile(a, IntArray(1, 1))
  151. || test_tile(a, IntArray(4, 1, 1))
  152. || test_tile(a, IntArray(2, 4, 4, 1))
  153. || test_tile(b, IntArray(3))
  154. || test_tile(b, IntArray(2, 4))
  155. || test_tile(b, IntArray(2, 4, 3, 1))
  156. || test_tile(b, IntArray(1, 2, 1, 4))
  157. || test_tile(c, IntArray(5))
  158. || test_tile(c, IntArray(6, 1))
  159. || test_tile(c, IntArray(6, 1, 6))
  160. || test_tile(c, IntArray(3, 2, 1, 1));
  161. }
  162. static int test_tile_3()
  163. {
  164. ncnn::Mat a = RandomMat(128);
  165. ncnn::Mat b = RandomMat(124);
  166. ncnn::Mat c = RandomMat(127);
  167. return 0
  168. || test_tile(a, 0, 1)
  169. || test_tile(a, 0, 2)
  170. || test_tile(b, 0, 3)
  171. || test_tile(c, 0, 4)
  172. || test_tile(a, IntArray(10))
  173. || test_tile(a, IntArray(10, 1))
  174. || test_tile(a, IntArray(5, 2, 1))
  175. || test_tile(a, IntArray(2, 2, 2, 3))
  176. || test_tile(b, IntArray(2))
  177. || test_tile(b, IntArray(2, 2))
  178. || test_tile(b, IntArray(2, 2, 1))
  179. || test_tile(b, IntArray(4, 1, 2, 2))
  180. || test_tile(c, IntArray(3))
  181. || test_tile(c, IntArray(4, 3))
  182. || test_tile(c, IntArray(1))
  183. || test_tile(c, IntArray(1, 1))
  184. || test_tile(c, IntArray(1, 1, 1))
  185. || test_tile(c, IntArray(1, 3, 2, 2));
  186. }
  187. int main()
  188. {
  189. SRAND(7767517);
  190. return 0
  191. || test_tile_0()
  192. || test_tile_1()
  193. || test_tile_2()
  194. || test_tile_3();
  195. }