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

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