Elffuss BraidHub

kikocis/ncnn public

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