ncnn

Форк
0
/
test_binaryop_3.cpp 
399 строк · 9.8 Кб
1
// Tencent is pleased to support the open source community by making ncnn available.
2
//
3
// Copyright (C) 2020 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

17
#define OP_TYPE_MAX 12
18

19
static int op_type = 0;
20

21
static int test_binaryop(const ncnn::Mat& _a, const ncnn::Mat& _b, int flag)
22
{
23
    ncnn::Mat a = _a;
24
    ncnn::Mat b = _b;
25
    if (op_type == 6 || op_type == 9)
26
    {
27
        // value must be positive for pow/rpow
28
        a = a.clone();
29
        b = b.clone();
30
        Randomize(a, 0.001f, 2.f);
31
        Randomize(b, 0.001f, 2.f);
32
    }
33
    if (op_type == 3 || op_type == 8)
34
    {
35
        // value must be positive for div/rdiv
36
        a = a.clone();
37
        b = b.clone();
38
        Randomize(a, 0.1f, 10.f);
39
        Randomize(b, 0.1f, 10.f);
40
    }
41
    if (op_type == 10 || op_type == 11)
42
    {
43
        // value must be non-zero for atan2/ratan2
44
        a = a.clone();
45
        b = b.clone();
46
        for (int i = 0; i < a.total(); i++)
47
        {
48
            if (a[i] == 0.f)
49
                a[i] = 0.001f;
50
        }
51
        for (int i = 0; i < b.total(); i++)
52
        {
53
            if (b[i] == 0.f)
54
                b[i] = 0.001f;
55
        }
56
    }
57

58
    ncnn::ParamDict pd;
59
    pd.set(0, op_type);
60
    pd.set(1, 0);   // with_scalar
61
    pd.set(2, 0.f); // b
62

63
    std::vector<ncnn::Mat> weights(0);
64

65
    std::vector<ncnn::Mat> ab(2);
66
    ab[0] = a;
67
    ab[1] = b;
68

69
    int ret = test_layer("BinaryOp", pd, weights, ab, 1, 0.001, 0, flag);
70
    if (ret != 0)
71
    {
72
        fprintf(stderr, "test_binaryop failed a.dims=%d a=(%d %d %d %d) b.dims=%d b=(%d %d %d %d) op_type=%d\n", a.dims, a.w, a.h, a.d, a.c, b.dims, b.w, b.h, b.d, b.c, op_type);
73
    }
74

75
    return ret;
76
}
77

78
static int test_binaryop(const ncnn::Mat& _a, float b, int flag)
79
{
80
    ncnn::Mat a = _a;
81
    if (op_type == 6 || op_type == 9)
82
    {
83
        // value must be positive for pow
84
        Randomize(a, 0.001f, 2.f);
85
        b = RandomFloat(0.001f, 2.f);
86
    }
87
    if (op_type == 3 || op_type == 8)
88
    {
89
        // value must be positive for div/rdiv
90
        a = a.clone();
91
        Randomize(a, 0.1f, 10.f);
92
    }
93
    if (op_type == 10 || op_type == 11)
94
    {
95
        // value must be non-zero for atan2/ratan2
96
        a = a.clone();
97
        for (int i = 0; i < a.total(); i++)
98
        {
99
            if (a[i] == 0.f)
100
                a[i] = 0.001f;
101
        }
102
    }
103

104
    ncnn::ParamDict pd;
105
    pd.set(0, op_type);
106
    pd.set(1, 1); // with_scalar
107
    pd.set(2, b); // b
108

109
    std::vector<ncnn::Mat> weights(0);
110

111
    int ret = test_layer("BinaryOp", pd, weights, a, 0.001, 0, flag);
112
    if (ret != 0)
113
    {
114
        fprintf(stderr, "test_binaryop failed a.dims=%d a=(%d %d %d %d) b=%f op_type=%d\n", a.dims, a.w, a.h, a.d, a.c, b, op_type);
115
    }
116

117
    return ret;
118
}
119

120
static int test_binaryop_1()
121
{
122
    const int ws[] = {31, 28, 24, 32};
123

124
    for (int i = 0; i < 4; i++)
125
    {
126
        const int w = ws[i];
127
        const int flag = w == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
128

129
        ncnn::Mat a[2];
130
        for (int j = 0; j < 2; j++)
131
        {
132
            int bw = j % 2 == 0 ? w : 1;
133
            a[j] = RandomMat(bw);
134
        }
135

136
        for (int j = 0; j < 2; j++)
137
        {
138
            for (int k = 0; k < 2; k++)
139
            {
140
                int ret = test_binaryop(a[j], a[k], flag);
141
                if (ret != 0)
142
                    return ret;
143
            }
144

145
            int ret = test_binaryop(a[j], 0.2f, flag);
146
            if (ret != 0)
147
                return ret;
148
        }
149
    }
150

151
    return 0;
152
}
153

154
static int test_binaryop_2()
155
{
156
    const int ws[] = {13, 14, 15, 16};
157
    const int hs[] = {31, 28, 24, 32};
158

159
    for (int i = 0; i < 4; i++)
160
    {
161
        const int w = ws[i];
162
        const int h = hs[i];
163
        const int flag = h == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
164

165
        ncnn::Mat a[4];
166
        for (int j = 0; j < 2; j++)
167
        {
168
            for (int k = 0; k < 2; k++)
169
            {
170
                int bw = j % 2 == 0 ? w : 1;
171
                int bh = k % 2 == 0 ? h : 1;
172
                a[j * 2 + k] = RandomMat(bw, bh);
173
            }
174
        }
175

176
        for (int j = 0; j < 4; j++)
177
        {
178
            for (int k = 0; k < 4; k++)
179
            {
180
                int ret = test_binaryop(a[j], a[k], flag);
181
                if (ret != 0)
182
                    return ret;
183
            }
184

185
            int ret = test_binaryop(a[j], 0.2f, flag);
186
            if (ret != 0)
187
                return ret;
188
        }
189
    }
190

191
    return 0;
192
}
193

194
static int test_binaryop_3()
195
{
196
    const int ws[] = {7, 6, 5, 4};
197
    const int hs[] = {3, 4, 5, 6};
198
    const int cs[] = {31, 28, 24, 32};
199

200
    for (int i = 0; i < 4; i++)
201
    {
202
        const int w = ws[i];
203
        const int h = hs[i];
204
        const int c = cs[i];
205
        const int flag = c == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
206

207
        ncnn::Mat a[8];
208
        for (int j = 0; j < 2; j++)
209
        {
210
            for (int k = 0; k < 2; k++)
211
            {
212
                for (int l = 0; l < 2; l++)
213
                {
214
                    int bw = j % 2 == 0 ? w : 1;
215
                    int bh = k % 2 == 0 ? h : 1;
216
                    int bc = l % 2 == 0 ? c : 1;
217
                    a[j * 4 + k * 2 + l] = RandomMat(bw, bh, bc);
218
                }
219
            }
220
        }
221

222
        for (int j = 0; j < 8; j++)
223
        {
224
            for (int k = 0; k < 8; k++)
225
            {
226
                int ret = test_binaryop(a[j], a[k], flag);
227
                if (ret != 0)
228
                    return ret;
229
            }
230

231
            int ret = test_binaryop(a[j], 0.2f, flag);
232
            if (ret != 0)
233
                return ret;
234
        }
235
    }
236

237
    return 0;
238
}
239

240
static int test_binaryop_4()
241
{
242
    const int ws[] = {2, 3, 4, 5};
243
    const int hs[] = {7, 6, 5, 4};
244
    const int ds[] = {3, 4, 5, 6};
245
    const int cs[] = {31, 28, 24, 32};
246

247
    for (int i = 0; i < 4; i++)
248
    {
249
        const int w = ws[i];
250
        const int h = hs[i];
251
        const int d = ds[i];
252
        const int c = cs[i];
253
        const int flag = c == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
254

255
        ncnn::Mat a[16];
256
        for (int j = 0; j < 2; j++)
257
        {
258
            for (int k = 0; k < 2; k++)
259
            {
260
                for (int l = 0; l < 2; l++)
261
                {
262
                    for (int m = 0; m < 2; m++)
263
                    {
264
                        int bw = j % 2 == 0 ? w : 1;
265
                        int bh = k % 2 == 0 ? h : 1;
266
                        int bd = l % 2 == 0 ? d : 1;
267
                        int bc = m % 2 == 0 ? c : 1;
268
                        a[j * 8 + k * 4 + l * 2 + m] = RandomMat(bw, bh, bd, bc);
269
                    }
270
                }
271
            }
272
        }
273

274
        for (int j = 0; j < 16; j++)
275
        {
276
            for (int k = 0; k < 16; k++)
277
            {
278
                int ret = test_binaryop(a[j], a[k], flag);
279
                if (ret != 0)
280
                    return ret;
281
            }
282

283
            int ret = test_binaryop(a[j], 0.2f, flag);
284
            if (ret != 0)
285
                return ret;
286
        }
287
    }
288

289
    return 0;
290
}
291

292
static int test_binaryop_5()
293
{
294
    const int ws[] = {2, 3, 4, 5};
295
    const int hs[] = {7, 6, 5, 4};
296
    const int ds[] = {3, 4, 5, 6};
297
    const int cs[] = {31, 28, 24, 32};
298

299
    for (int i = 0; i < 4; i++)
300
    {
301
        const int w = ws[i];
302
        const int h = hs[i];
303
        const int d = ds[i];
304
        const int c = cs[i];
305
        const int flag = c == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
306

307
        ncnn::Mat a[4] = {
308
            RandomMat(c),
309
            RandomMat(d, c),
310
            RandomMat(h, d, c),
311
            RandomMat(w, h, d, c),
312
        };
313

314
        for (int j = 0; j < 4; j++)
315
        {
316
            for (int k = 0; k < 4; k++)
317
            {
318
                if (j == k)
319
                    continue;
320

321
                int ret = test_binaryop(a[j], a[k], flag);
322
                if (ret != 0)
323
                    return ret;
324
            }
325
        }
326
    }
327

328
    return 0;
329
}
330

331
static int test_binaryop_6()
332
{
333
    const int ws[] = {16, 12, 16, 15};
334
    const int hs[] = {15, 16, 15, 12};
335
    const int ds[] = {12, 14, 12, 16};
336
    const int cs[] = {31, 28, 24, 32};
337

338
    for (int i = 0; i < 4; i++)
339
    {
340
        const int w = ws[i];
341
        const int h = hs[i];
342
        const int d = ds[i];
343
        const int c = cs[i];
344
        const int flag = c == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
345

346
        ncnn::Mat a[3] = {
347
            RandomMat(d, c),
348
            RandomMat(h, d, c),
349
            RandomMat(w, h, d, c),
350
        };
351

352
        for (int j = 0; j < 3; j++)
353
        {
354
            ncnn::Mat b = RandomMat(a[j].w);
355

356
            int ret = test_binaryop(a[j], b, flag) || test_binaryop(b, a[j], flag);
357
            if (ret != 0)
358
                return ret;
359
        }
360

361
        ncnn::Mat aa[3] = {
362
            RandomMat(c, c),
363
            RandomMat(c, d, c),
364
            RandomMat(c, h, d, c),
365
        };
366

367
        for (int j = 0; j < 3; j++)
368
        {
369
            ncnn::Mat b = RandomMat(aa[j].w);
370

371
            int ret = test_binaryop(aa[j], b, flag) || test_binaryop(b, aa[j], flag);
372
            if (ret != 0)
373
                return ret;
374
        }
375
    }
376

377
    return 0;
378
}
379

380
int main()
381
{
382
    SRAND(7767517);
383

384
    for (op_type = 9; op_type < OP_TYPE_MAX; op_type++)
385
    {
386
        int ret = 0
387
                  || test_binaryop_1()
388
                  || test_binaryop_2()
389
                  || test_binaryop_3()
390
                  || test_binaryop_4()
391
                  || test_binaryop_5()
392
                  || test_binaryop_6();
393

394
        if (ret != 0)
395
            return ret;
396
    }
397

398
    return 0;
399
}
400

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.