Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 4.9 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2024 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_convolution_oom(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias)
18{
19 ncnn::Mat a = RandomMat(w, h, c);
20
21 ncnn::ParamDict pd;
22 pd.set(0, outch);
23 pd.set(1, kernel);
24 pd.set(2, dilation);
25 pd.set(3, stride);
26 pd.set(4, pad);
27 pd.set(5, bias);
28 pd.set(6, outch * c * kernel * kernel);
29
30 int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
31 ncnn::Mat activation_params(2);
32 activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
33 activation_params[1] = RandomFloat(0, 1); // beta
34 pd.set(9, activation_type);
35 pd.set(10, activation_params);
36
37 std::vector<ncnn::Mat> weights(bias ? 2 : 1);
38 weights[0] = RandomMat(outch * c * kernel * kernel);
39 if (bias)
40 weights[1] = RandomMat(outch);
41
42 int ret = test_layer_oom("Convolution", pd, weights, a);
43 if (ret != 0)
44 {
45 fprintf(stderr, "test_convolution_oom failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]);
46 return ret;
47 }
48
49 return ret;
50}
51
52static int test_convolution_0()
53{
54 return 0
55 || test_convolution_oom(9, 7, 31, 63, 1, 1, 1, 0, 1)
56 || test_convolution_oom(9, 7, 31, 63, 3, 1, 1, 1, 1);
57}
58
59#if NCNN_INT8
60static int test_convolution_oom_int8(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, bool requant = false)
61{
62 ncnn::Mat a = RandomMat(w, h, c);
63
64 ncnn::ParamDict pd;
65 pd.set(0, outch);
66 pd.set(1, kernel);
67 pd.set(2, dilation);
68 pd.set(3, stride);
69 pd.set(4, pad);
70 pd.set(5, bias);
71 pd.set(6, outch * c * kernel * kernel);
72 pd.set(8, requant ? 101 : 1); // int8_scale_term
73
74 int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
75 ncnn::Mat activation_params(2);
76 activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
77 activation_params[1] = RandomFloat(0, 1); // beta
78 pd.set(9, activation_type);
79 pd.set(10, activation_params);
80
81 std::vector<ncnn::Mat> weights(bias ? 5 : 4);
82 weights[0] = RandomMat(outch * c * kernel * kernel);
83
84 ncnn::Mat weight_scales = scales_mat(weights[0], outch, c * kernel * kernel, c * kernel * kernel);
85 ncnn::Mat input_scales = scales_mat(a, 1, w * h * c, a.cstep);
86 ncnn::Mat top_scales = requant ? scales_mat(a, 1, w * h * c, a.cstep) : ncnn::Mat();
87
88 if (kernel == 3 && dilation == 1 && stride == 1)
89 {
90 // test for 6bit quant
91 for (int i = 0; i < weight_scales.w; i++)
92 weight_scales[i] = weight_scales[i] / 4.f;
93 }
94
95 if (bias)
96 {
97 weights[1] = RandomMat(outch);
98 weights[2] = weight_scales;
99 weights[3] = input_scales;
100 weights[4] = top_scales;
101 }
102 else
103 {
104 weights[1] = weight_scales;
105 weights[2] = input_scales;
106 weights[3] = top_scales;
107 }
108
109 int flag = TEST_LAYER_DISABLE_GPU_TESTING;
110 int ret = test_layer_oom("Convolution", pd, weights, a, flag);
111 if (ret != 0)
112 {
113 fprintf(stderr, "test_convolution_oom_int8 failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d requant=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, requant, activation_type, activation_params[0], activation_params[1]);
114 return ret;
115 }
116
117 return ret;
118}
119
120static int test_convolution_1()
121{
122 return 0
123 || test_convolution_oom_int8(9, 7, 31, 63, 1, 1, 1, 0, 1)
124 || test_convolution_oom_int8(9, 7, 31, 63, 3, 1, 1, 1, 1);
125}
126
127static int test_convolution_2()
128{
129 return 0
130 || test_convolution_oom_int8(9, 7, 31, 63, 1, 1, 1, 0, 1, true)
131 || test_convolution_oom_int8(9, 7, 31, 63, 3, 1, 1, 1, 1, true);
132}
133#endif // NCNN_INT8
134
135int main()
136{
137 SRAND(7767517);
138
139#if __mips__ || __loongarch64 || __riscv
140 // TODO
141 return 0;
142#endif
143
144#if NCNN_INT8
145 return test_convolution_0() || test_convolution_1() || test_convolution_2();
146#else
147 return test_convolution_0();
148#endif
149}