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.cpp 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 int test_copyto(const ncnn::Mat& self, const ncnn::Mat& src, int woffset, int hoffset, int doffset, int coffset)
  16. {
  17. ncnn::ParamDict pd;
  18. pd.set(0, woffset); // woffset
  19. pd.set(1, hoffset); // hoffset
  20. pd.set(13, doffset); // doffset
  21. pd.set(2, coffset); // coffset
  22. std::vector<ncnn::Mat> weights(0);
  23. std::vector<ncnn::Mat> as(2);
  24. as[0] = self;
  25. as[1] = src;
  26. int ret = test_layer("CopyTo", pd, weights, as, 1);
  27. if (ret != 0)
  28. {
  29. fprintf(stderr, "test_copyto failed self.dims=%d self=(%d %d %d %d) src.dims=%d src=(%d %d %d %d) woffset=%d hoffset=%d doffset=%d coffset=%d\n", self.dims, self.w, self.h, self.d, self.c, src.dims, src.w, src.h, src.d, src.c, woffset, hoffset, doffset, coffset);
  30. }
  31. return ret;
  32. }
  33. static int test_copyto_0()
  34. {
  35. ncnn::Mat a[] = {
  36. RandomMat(112),
  37. RandomMat(126),
  38. RandomMat(127)
  39. };
  40. ncnn::Mat b[] = {
  41. RandomMat(33),
  42. RandomMat(36),
  43. RandomMat(64)
  44. };
  45. for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  46. {
  47. for (int j = 0; j < sizeof(b) / sizeof(b[0]); j++)
  48. {
  49. const ncnn::Mat& self = a[i];
  50. const ncnn::Mat& src = b[j];
  51. int ret = 0
  52. || test_copyto(self, src, 0, 0, 0, 0)
  53. || test_copyto(self, src, 13, 0, 0, 0)
  54. || test_copyto(self, src, 28, 0, 0, 0)
  55. || test_copyto(self, src, 32, 0, 0, 0);
  56. if (ret != 0)
  57. return ret;
  58. }
  59. }
  60. return 0;
  61. }
  62. static int test_copyto_1()
  63. {
  64. ncnn::Mat a[] = {
  65. RandomMat(72, 112),
  66. RandomMat(87, 126),
  67. RandomMat(64, 127)
  68. };
  69. ncnn::Mat b[] = {
  70. RandomMat(14, 33),
  71. RandomMat(24, 36),
  72. RandomMat(16, 64),
  73. RandomMat(14),
  74. RandomMat(24),
  75. RandomMat(16)
  76. };
  77. for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  78. {
  79. for (int j = 0; j < sizeof(b) / sizeof(b[0]); j++)
  80. {
  81. const ncnn::Mat& self = a[i];
  82. const ncnn::Mat& src = b[j];
  83. int ret = 0
  84. || test_copyto(self, src, 0, 0, 0, 0)
  85. || test_copyto(self, src, 1, 13, 0, 0)
  86. || test_copyto(self, src, 3, 28, 0, 0)
  87. || test_copyto(self, src, 10, 32, 0, 0);
  88. if (ret != 0)
  89. return ret;
  90. }
  91. }
  92. return 0;
  93. }
  94. static int test_copyto_2()
  95. {
  96. ncnn::Mat a[] = {
  97. RandomMat(32, 42, 81),
  98. RandomMat(33, 57, 80),
  99. RandomMat(36, 34, 88)
  100. };
  101. ncnn::Mat b[] = {
  102. RandomMat(1, 14, 23),
  103. RandomMat(12, 1, 28),
  104. RandomMat(11, 8, 32),
  105. RandomMat(1, 14),
  106. RandomMat(12, 1),
  107. RandomMat(11, 8),
  108. RandomMat(1),
  109. RandomMat(12),
  110. RandomMat(11)
  111. };
  112. for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  113. {
  114. for (int j = 0; j < sizeof(b) / sizeof(b[0]); j++)
  115. {
  116. const ncnn::Mat& self = a[i];
  117. const ncnn::Mat& src = b[j];
  118. int ret = 0
  119. || test_copyto(self, src, 0, 0, 0, 0)
  120. || test_copyto(self, src, 0, 1, 0, 13)
  121. || test_copyto(self, src, 4, 3, 0, 28)
  122. || test_copyto(self, src, 5, 0, 0, 32);
  123. if (ret != 0)
  124. return ret;
  125. }
  126. }
  127. return 0;
  128. }
  129. static int test_copyto_3()
  130. {
  131. ncnn::Mat a[] = {
  132. RandomMat(12, 42, 7, 81),
  133. RandomMat(13, 57, 5, 80),
  134. RandomMat(16, 34, 6, 88)
  135. };
  136. ncnn::Mat b[] = {
  137. RandomMat(1, 14, 2, 23),
  138. RandomMat(12, 1, 3, 28),
  139. RandomMat(11, 8, 1, 32),
  140. RandomMat(1, 14, 2),
  141. RandomMat(12, 1, 3),
  142. RandomMat(11, 8, 1),
  143. RandomMat(1, 14),
  144. RandomMat(12, 1),
  145. RandomMat(11, 8),
  146. RandomMat(1),
  147. RandomMat(12),
  148. RandomMat(11)
  149. };
  150. for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  151. {
  152. for (int j = 0; j < sizeof(b) / sizeof(b[0]); j++)
  153. {
  154. const ncnn::Mat& self = a[i];
  155. const ncnn::Mat& src = b[j];
  156. int ret = 0
  157. || test_copyto(self, src, 0, 0, 0, 0)
  158. || test_copyto(self, src, 0, 1, 1, 13)
  159. || test_copyto(self, src, 4, 3, 0, 28)
  160. || test_copyto(self, src, 5, 0, 2, 32);
  161. if (ret != 0)
  162. return ret;
  163. }
  164. }
  165. return 0;
  166. }
  167. static int test_copyto_4()
  168. {
  169. return 0
  170. || test_copyto(RandomMat(15), RandomMat(15), 0, 0, 0, 0)
  171. || test_copyto(RandomMat(11, 15), RandomMat(11, 15), 0, 0, 0, 0)
  172. || test_copyto(RandomMat(4, 5, 16), RandomMat(4, 5, 16), 0, 0, 0, 0)
  173. || test_copyto(RandomMat(3, 4, 5, 16), RandomMat(3, 4, 5, 16), 0, 0, 0, 0);
  174. }
  175. int main()
  176. {
  177. SRAND(776757);
  178. return 0
  179. || test_copyto_0()
  180. || test_copyto_1()
  181. || test_copyto_2()
  182. || test_copyto_3()
  183. || test_copyto_4();
  184. }