Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 6.9 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 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(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 float epsilon = 0.001;
43
44 int ret = test_layer("Convolution", pd, weights, a, epsilon);
45 if (ret != 0)
46 {
47 fprintf(stderr, "test_convolution 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]);
48 return ret;
49 }
50
51 {
52 ncnn::Option opt;
53 opt.num_threads = 1;
54 opt.use_packing_layout = true;
55 opt.use_fp16_packed = false;
56 opt.use_fp16_storage = false;
57 opt.use_fp16_arithmetic = false;
58 opt.use_bf16_storage = false;
59 opt.use_shader_pack8 = false;
60 opt.use_image_storage = false;
61 opt.use_sgemm_convolution = false;
62 opt.use_winograd_convolution = false;
63
64 ret = test_layer_opt("Convolution", pd, weights, opt, a, epsilon);
65 if (ret != 0)
66 {
67 fprintf(stderr, "test_convolution 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]);
68 return ret;
69 }
70 }
71
72 {
73 ncnn::Option opt;
74 opt.num_threads = 1;
75 opt.use_packing_layout = true;
76 opt.use_fp16_packed = true;
77 opt.use_fp16_storage = true;
78 opt.use_fp16_arithmetic = true;
79 opt.use_bf16_storage = true;
80 opt.use_shader_pack8 = true;
81 opt.use_image_storage = true;
82 opt.use_sgemm_convolution = false;
83 opt.use_winograd_convolution = false;
84
85 ret = test_layer_opt("Convolution", pd, weights, opt, a, epsilon);
86 if (ret != 0)
87 {
88 fprintf(stderr, "test_convolution 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]);
89 return ret;
90 }
91 }
92
93#if __aarch64__
94 {
95 ncnn::Option opt;
96 opt.num_threads = 1;
97 opt.use_a53_a55_optimized_kernel = true;
98
99 ret = test_layer_opt("Convolution", pd, weights, opt, a, epsilon);
100 if (ret != 0)
101 {
102 fprintf(stderr, "test_convolution 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]);
103 return ret;
104 }
105 }
106#endif // __aarch64__
107
108 return ret;
109}
110
111static int test_convolution_0()
112{
113 static const int kdsp[16][4] = {
114 {1, 1, 1, 0},
115 {1, 1, 2, 0},
116 {2, 1, 1, 1},
117 {2, 1, 2, -233},
118 {3, 1, 1, 1},
119 {3, 1, 2, 1},
120 {3, 2, 1, 1},
121 {4, 1, 1, 2},
122 {4, 1, 2, -233},
123 {4, 2, 1, -234},
124 {5, 1, 1, -234},
125 {5, 1, 2, 2},
126 {5, 2, 2, 2},
127 {7, 1, 1, 3},
128 {7, 1, 2, 3},
129 {7, 2, 1, -233},
130 };
131
132 for (int i = 0; i < 12; i++)
133 {
134 const int k = kdsp[i][0];
135 const int d = kdsp[i][1];
136 const int s = kdsp[i][2];
137 const int p = kdsp[i][3];
138
139 int ret = 0
140 || test_convolution(9, 7, 1, 1, k, d, s, p, 1)
141 || test_convolution(9, 7, 4, 13, k, d, s, p, 0)
142 || test_convolution(9, 7, 13, 4, k, d, s, p, 1)
143 || test_convolution(9, 7, 12, 12, k, d, s, p, 0)
144 || test_convolution(9, 7, 8, 12, k, d, s, p, 1)
145 || test_convolution(9, 7, 8, 13, k, d, s, p, 0)
146 || test_convolution(9, 7, 13, 24, k, d, s, p, 1)
147 || test_convolution(9, 7, 12, 16, k, d, s, p, 0)
148 || test_convolution(9, 7, 15, 15, k, d, s, p, 0)
149 || test_convolution(9, 7, 16, 16, k, d, s, p, 0)
150 || test_convolution(18, 17, 1, 1, k, d, s, p, 1)
151 || test_convolution(18, 17, 4, 13, k, d, s, p, 0)
152 || test_convolution(18, 17, 13, 4, k, d, s, p, 1)
153 || test_convolution(18, 17, 12, 12, k, d, s, p, 0)
154 || test_convolution(18, 17, 8, 12, k, d, s, p, 1)
155 || test_convolution(18, 17, 8, 13, k, d, s, p, 0)
156 || test_convolution(18, 17, 13, 24, k, d, s, p, 1)
157 || test_convolution(18, 17, 12, 16, k, d, s, p, 0)
158 || test_convolution(18, 17, 15, 15, k, d, s, p, 0)
159 || test_convolution(18, 17, 16, 16, k, d, s, p, 0)
160 || test_convolution(25, 33, 1, 1, k, d, s, p, 1)
161 || test_convolution(25, 33, 4, 13, k, d, s, p, 0)
162 || test_convolution(25, 33, 13, 4, k, d, s, p, 1)
163 || test_convolution(25, 33, 12, 12, k, d, s, p, 0)
164 || test_convolution(25, 33, 8, 12, k, d, s, p, 1)
165 || test_convolution(25, 33, 8, 13, k, d, s, p, 0)
166 || test_convolution(25, 33, 13, 24, k, d, s, p, 1)
167 || test_convolution(25, 33, 12, 16, k, d, s, p, 0)
168 || test_convolution(25, 33, 15, 15, k, d, s, p, 0)
169 || test_convolution(25, 33, 16, 16, k, d, s, p, 0);
170
171 if (ret != 0)
172 return -1;
173 }
174
175 return 0;
176}
177
178int main()
179{
180 SRAND(7767517);
181
182 return test_convolution_0();
183}