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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2022 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_tile(const ncnn::Mat& a, int axis, int tiles)
  16. {
  17. ncnn::ParamDict pd;
  18. pd.set(0, axis);
  19. pd.set(1, tiles);
  20. std::vector<ncnn::Mat> weights(0);
  21. int ret = test_layer("Tile", pd, weights, a);
  22. if (ret != 0)
  23. {
  24. 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);
  25. }
  26. return ret;
  27. }
  28. static std::vector<int> IntArray(int a0)
  29. {
  30. std::vector<int> m(1);
  31. m[0] = a0;
  32. return m;
  33. }
  34. static std::vector<int> IntArray(int a0, int a1)
  35. {
  36. std::vector<int> m(2);
  37. m[0] = a0;
  38. m[1] = a1;
  39. return m;
  40. }
  41. static std::vector<int> IntArray(int a0, int a1, int a2)
  42. {
  43. std::vector<int> m(3);
  44. m[0] = a0;
  45. m[1] = a1;
  46. m[2] = a2;
  47. return m;
  48. }
  49. static std::vector<int> IntArray(int a0, int a1, int a2, int a3)
  50. {
  51. std::vector<int> m(4);
  52. m[0] = a0;
  53. m[1] = a1;
  54. m[2] = a2;
  55. m[3] = a3;
  56. return m;
  57. }
  58. static void print_int_array(const std::vector<int>& a)
  59. {
  60. fprintf(stderr, "[");
  61. for (size_t i = 0; i < a.size(); i++)
  62. {
  63. fprintf(stderr, " %d", a[i]);
  64. }
  65. fprintf(stderr, " ]");
  66. }
  67. static int test_tile(const ncnn::Mat& a, const std::vector<int>& repeats_array)
  68. {
  69. ncnn::Mat repeats(repeats_array.size());
  70. {
  71. int* p = repeats;
  72. for (size_t i = 0; i < repeats_array.size(); i++)
  73. {
  74. p[i] = repeats_array[i];
  75. }
  76. }
  77. ncnn::ParamDict pd;
  78. pd.set(2, repeats);
  79. std::vector<ncnn::Mat> weights(0);
  80. int ret = test_layer("Tile", pd, weights, a);
  81. if (ret != 0)
  82. {
  83. fprintf(stderr, "test_tile failed a.dims=%d a=(%d %d %d %d) repeats=", a.dims, a.w, a.h, a.d, a.c);
  84. print_int_array(repeats_array);
  85. fprintf(stderr, "\n");
  86. }
  87. return ret;
  88. }
  89. static int test_tile_0()
  90. {
  91. ncnn::Mat a = RandomMat(5, 6, 7, 24);
  92. ncnn::Mat b = RandomMat(7, 8, 9, 12);
  93. ncnn::Mat c = RandomMat(3, 4, 5, 13);
  94. return 0
  95. || test_tile(a, 0, 3)
  96. || test_tile(a, 1, 4)
  97. || test_tile(a, 2, 5)
  98. || test_tile(a, 3, 2)
  99. || test_tile(b, 0, 3)
  100. || test_tile(b, 1, 4)
  101. || test_tile(b, 2, 1)
  102. || test_tile(b, 3, 2)
  103. || test_tile(c, 0, 3)
  104. || test_tile(c, 1, 4)
  105. || test_tile(c, 2, 5)
  106. || test_tile(c, 3, 2)
  107. || test_tile(a, IntArray(3))
  108. || test_tile(a, IntArray(2, 4))
  109. || test_tile(a, IntArray(2, 2, 5))
  110. || test_tile(a, IntArray(3, 1, 3, 2))
  111. || test_tile(b, IntArray(3, 1))
  112. || test_tile(b, IntArray(4, 1, 4))
  113. || test_tile(b, IntArray(2, 2, 2, 1))
  114. || test_tile(b, IntArray(3, 2, 1))
  115. || test_tile(c, IntArray(3))
  116. || test_tile(c, IntArray(1, 1, 4))
  117. || test_tile(c, IntArray(2, 2, 5))
  118. || test_tile(c, IntArray(3, 2, 1, 9));
  119. }
  120. static int test_tile_1()
  121. {
  122. ncnn::Mat a = RandomMat(5, 7, 24);
  123. ncnn::Mat b = RandomMat(7, 9, 12);
  124. ncnn::Mat c = RandomMat(3, 5, 13);
  125. return 0
  126. || test_tile(a, 0, 5)
  127. || test_tile(a, 1, 4)
  128. || test_tile(a, 2, 4)
  129. || test_tile(b, 0, 3)
  130. || test_tile(b, 1, 3)
  131. || test_tile(b, 2, 3)
  132. || test_tile(c, 0, 1)
  133. || test_tile(c, 1, 2)
  134. || test_tile(c, 2, 2)
  135. || test_tile(a, IntArray(5))
  136. || test_tile(a, IntArray(1, 4))
  137. || test_tile(a, IntArray(2, 1, 4))
  138. || test_tile(a, IntArray(1, 2, 1, 4))
  139. || test_tile(b, IntArray(3))
  140. || test_tile(b, IntArray(1, 3, 3))
  141. || test_tile(b, IntArray(2, 3))
  142. || test_tile(b, IntArray(2, 3, 3, 3))
  143. || test_tile(c, IntArray(1))
  144. || test_tile(c, IntArray(2, 1))
  145. || test_tile(c, IntArray(2, 2, 2))
  146. || test_tile(c, IntArray(2, 1, 2, 1));
  147. }
  148. static int test_tile_2()
  149. {
  150. ncnn::Mat a = RandomMat(15, 24);
  151. ncnn::Mat b = RandomMat(17, 12);
  152. ncnn::Mat c = RandomMat(19, 13);
  153. return 0
  154. || test_tile(a, 0, 2)
  155. || test_tile(a, 1, 1)
  156. || test_tile(b, 0, 3)
  157. || test_tile(b, 1, 4)
  158. || test_tile(c, 0, 5)
  159. || test_tile(c, 1, 6)
  160. || test_tile(a, IntArray(2))
  161. || test_tile(a, IntArray(1, 1))
  162. || test_tile(a, IntArray(4, 1, 1))
  163. || test_tile(a, IntArray(2, 4, 4, 1))
  164. || test_tile(b, IntArray(3))
  165. || test_tile(b, IntArray(2, 4))
  166. || test_tile(b, IntArray(2, 4, 3, 1))
  167. || test_tile(b, IntArray(1, 2, 1, 4))
  168. || test_tile(c, IntArray(5))
  169. || test_tile(c, IntArray(6, 1))
  170. || test_tile(c, IntArray(6, 1, 6))
  171. || test_tile(c, IntArray(3, 2, 1, 1));
  172. }
  173. static int test_tile_3()
  174. {
  175. ncnn::Mat a = RandomMat(128);
  176. ncnn::Mat b = RandomMat(124);
  177. ncnn::Mat c = RandomMat(127);
  178. return 0
  179. || test_tile(a, 0, 1)
  180. || test_tile(a, 0, 2)
  181. || test_tile(b, 0, 3)
  182. || test_tile(c, 0, 4)
  183. || test_tile(a, IntArray(10))
  184. || test_tile(a, IntArray(10, 1))
  185. || test_tile(a, IntArray(5, 2, 1))
  186. || test_tile(a, IntArray(2, 2, 2, 3))
  187. || test_tile(b, IntArray(2))
  188. || test_tile(b, IntArray(2, 2))
  189. || test_tile(b, IntArray(2, 2, 1))
  190. || test_tile(b, IntArray(4, 1, 2, 2))
  191. || test_tile(c, IntArray(3))
  192. || test_tile(c, IntArray(4, 3))
  193. || test_tile(c, IntArray(1))
  194. || test_tile(c, IntArray(1, 1))
  195. || test_tile(c, IntArray(1, 1, 1))
  196. || test_tile(c, IntArray(1, 3, 2, 2));
  197. }
  198. int main()
  199. {
  200. SRAND(7767517);
  201. return 0
  202. || test_tile_0()
  203. || test_tile_1()
  204. || test_tile_2()
  205. || test_tile_3();
  206. }