Elffuss BraidHub

kikocis/ncnn public

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