1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
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
8
// https://opensource.org/licenses/BSD-3-Clause
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.
19
static int op_type = 0;
21
static int test_binaryop(const ncnn::Mat& _a, const ncnn::Mat& _b, int flag)
25
if (op_type == 6 || op_type == 9)
27
// value must be positive for pow/rpow
30
Randomize(a, 0.001f, 2.f);
31
Randomize(b, 0.001f, 2.f);
33
if (op_type == 3 || op_type == 8)
35
// value must be positive for div/rdiv
38
Randomize(a, 0.1f, 10.f);
39
Randomize(b, 0.1f, 10.f);
41
if (op_type == 10 || op_type == 11)
43
// value must be non-zero for atan2/ratan2
46
for (int i = 0; i < a.total(); i++)
51
for (int i = 0; i < b.total(); i++)
60
pd.set(1, 0); // with_scalar
63
std::vector<ncnn::Mat> weights(0);
65
std::vector<ncnn::Mat> ab(2);
69
int ret = test_layer("BinaryOp", pd, weights, ab, 1, 0.001, 0, flag);
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);
78
static int test_binaryop(const ncnn::Mat& _a, float b, int flag)
81
if (op_type == 6 || op_type == 9)
83
// value must be positive for pow
84
Randomize(a, 0.001f, 2.f);
85
b = RandomFloat(0.001f, 2.f);
87
if (op_type == 3 || op_type == 8)
89
// value must be positive for div/rdiv
91
Randomize(a, 0.1f, 10.f);
93
if (op_type == 10 || op_type == 11)
95
// value must be non-zero for atan2/ratan2
97
for (int i = 0; i < a.total(); i++)
106
pd.set(1, 1); // with_scalar
109
std::vector<ncnn::Mat> weights(0);
111
int ret = test_layer("BinaryOp", pd, weights, a, 0.001, 0, flag);
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);
120
static int test_binaryop_1()
122
const int ws[] = {31, 28, 24, 32};
124
for (int i = 0; i < 4; i++)
127
const int flag = w == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
130
for (int j = 0; j < 2; j++)
132
int bw = j % 2 == 0 ? w : 1;
133
a[j] = RandomMat(bw);
136
for (int j = 0; j < 2; j++)
138
for (int k = 0; k < 2; k++)
140
int ret = test_binaryop(a[j], a[k], flag);
145
int ret = test_binaryop(a[j], 0.2f, flag);
154
static int test_binaryop_2()
156
const int ws[] = {13, 14, 15, 16};
157
const int hs[] = {31, 28, 24, 32};
159
for (int i = 0; i < 4; i++)
163
const int flag = h == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
166
for (int j = 0; j < 2; j++)
168
for (int k = 0; k < 2; k++)
170
int bw = j % 2 == 0 ? w : 1;
171
int bh = k % 2 == 0 ? h : 1;
172
a[j * 2 + k] = RandomMat(bw, bh);
176
for (int j = 0; j < 4; j++)
178
for (int k = 0; k < 4; k++)
180
int ret = test_binaryop(a[j], a[k], flag);
185
int ret = test_binaryop(a[j], 0.2f, flag);
194
static int test_binaryop_3()
196
const int ws[] = {7, 6, 5, 4};
197
const int hs[] = {3, 4, 5, 6};
198
const int cs[] = {31, 28, 24, 32};
200
for (int i = 0; i < 4; i++)
205
const int flag = c == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
208
for (int j = 0; j < 2; j++)
210
for (int k = 0; k < 2; k++)
212
for (int l = 0; l < 2; l++)
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);
222
for (int j = 0; j < 8; j++)
224
for (int k = 0; k < 8; k++)
226
int ret = test_binaryop(a[j], a[k], flag);
231
int ret = test_binaryop(a[j], 0.2f, flag);
240
static int test_binaryop_4()
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};
247
for (int i = 0; i < 4; i++)
253
const int flag = c == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
256
for (int j = 0; j < 2; j++)
258
for (int k = 0; k < 2; k++)
260
for (int l = 0; l < 2; l++)
262
for (int m = 0; m < 2; m++)
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);
274
for (int j = 0; j < 16; j++)
276
for (int k = 0; k < 16; k++)
278
int ret = test_binaryop(a[j], a[k], flag);
283
int ret = test_binaryop(a[j], 0.2f, flag);
292
static int test_binaryop_5()
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};
299
for (int i = 0; i < 4; i++)
305
const int flag = c == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
311
RandomMat(w, h, d, c),
314
for (int j = 0; j < 4; j++)
316
for (int k = 0; k < 4; k++)
321
int ret = test_binaryop(a[j], a[k], flag);
331
static int test_binaryop_6()
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};
338
for (int i = 0; i < 4; i++)
344
const int flag = c == 32 ? TEST_LAYER_DISABLE_GPU_TESTING : 0;
349
RandomMat(w, h, d, c),
352
for (int j = 0; j < 3; j++)
354
ncnn::Mat b = RandomMat(a[j].w);
356
int ret = test_binaryop(a[j], b, flag) || test_binaryop(b, a[j], flag);
364
RandomMat(c, h, d, c),
367
for (int j = 0; j < 3; j++)
369
ncnn::Mat b = RandomMat(aa[j].w);
371
int ret = test_binaryop(aa[j], b, flag) || test_binaryop(b, aa[j], flag);
384
for (op_type = 9; op_type < OP_TYPE_MAX; op_type++)
392
|| test_binaryop_6();