1 // Tencent is pleased to support the open source community by making ncnn available.
2 //
3 // Copyright (C) 2021 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 "layer/dequantize.h"
16 #include "testutil.h"
17
RandomInt(int a=-10000,int b=10000)18 static int RandomInt(int a = -10000, int b = 10000)
19 {
20 float random = ((float)RAND()) / (float)uint64_t(-1); //RAND_MAX;
21 int diff = b - a;
22 float r = random * diff;
23 return a + (int)r;
24 }
25
RandomizeInt(ncnn::Mat & m,int a=-10000,int b=10000)26 static void RandomizeInt(ncnn::Mat& m, int a = -10000, int b = 10000)
27 {
28 for (size_t i = 0; i < m.total(); i++)
29 {
30 ((int*)m)[i] = RandomInt(a, b);
31 }
32 }
33
RandomIntMat(int w)34 static ncnn::Mat RandomIntMat(int w)
35 {
36 ncnn::Mat m(w);
37 RandomizeInt(m);
38 return m;
39 }
40
RandomIntMat(int w,int h)41 static ncnn::Mat RandomIntMat(int w, int h)
42 {
43 ncnn::Mat m(w, h);
44 RandomizeInt(m);
45 return m;
46 }
47
RandomIntMat(int w,int h,int c)48 static ncnn::Mat RandomIntMat(int w, int h, int c)
49 {
50 ncnn::Mat m(w, h, c);
51 RandomizeInt(m);
52 return m;
53 }
54
test_dequantize(const ncnn::Mat & a,int scale_data_size,int bias_data_size)55 static int test_dequantize(const ncnn::Mat& a, int scale_data_size, int bias_data_size)
56 {
57 ncnn::ParamDict pd;
58 pd.set(0, scale_data_size);
59 pd.set(1, bias_data_size);
60
61 std::vector<ncnn::Mat> weights(bias_data_size ? 2 : 1);
62 weights[0] = RandomMat(scale_data_size);
63 if (bias_data_size)
64 weights[1] = RandomMat(bias_data_size);
65
66 int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING;
67 int ret = test_layer<ncnn::Dequantize>("Dequantize", pd, weights, a, 0.001, 0, flag);
68 if (ret != 0)
69 {
70 fprintf(stderr, "test_dequantize failed a.dims=%d a=(%d %d %d) scale_data_size=%d bias_data_size=%d\n", a.dims, a.w, a.h, a.c, scale_data_size, bias_data_size);
71 }
72
73 return ret;
74 }
75
test_dequantize_pack8(const ncnn::Mat & a,int scale_data_size,int bias_data_size)76 static int test_dequantize_pack8(const ncnn::Mat& a, int scale_data_size, int bias_data_size)
77 {
78 ncnn::ParamDict pd;
79 pd.set(0, scale_data_size);
80 pd.set(1, bias_data_size);
81
82 std::vector<ncnn::Mat> weights(bias_data_size ? 2 : 1);
83 weights[0] = RandomMat(scale_data_size);
84 if (bias_data_size)
85 weights[1] = RandomMat(bias_data_size);
86
87 int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING | TEST_LAYER_ENABLE_FORCE_INPUT_PACK8;
88 int ret = test_layer<ncnn::Dequantize>("Dequantize", pd, weights, a, 0.001, 0, flag);
89 if (ret != 0)
90 {
91 fprintf(stderr, "test_dequantize_pack8 failed a.dims=%d a=(%d %d %d) scale_data_size=%d bias_data_size=%d\n", a.dims, a.w, a.h, a.c, scale_data_size, bias_data_size);
92 }
93
94 return ret;
95 }
96
test_dequantize_0()97 static int test_dequantize_0()
98 {
99 return 0
100 || test_dequantize(RandomIntMat(5, 7, 24), 1, 24)
101 || test_dequantize(RandomIntMat(5, 7, 24), 1, 1)
102 || test_dequantize(RandomIntMat(5, 7, 24), 1, 0)
103 || test_dequantize(RandomIntMat(5, 7, 24), 24, 24)
104 || test_dequantize(RandomIntMat(5, 7, 24), 24, 1)
105 || test_dequantize(RandomIntMat(5, 7, 24), 24, 0)
106 || test_dequantize(RandomIntMat(7, 9, 12), 1, 12)
107 || test_dequantize(RandomIntMat(7, 9, 12), 1, 1)
108 || test_dequantize(RandomIntMat(7, 9, 12), 1, 0)
109 || test_dequantize(RandomIntMat(7, 9, 12), 12, 12)
110 || test_dequantize(RandomIntMat(7, 9, 12), 12, 1)
111 || test_dequantize(RandomIntMat(7, 9, 12), 12, 0)
112 || test_dequantize(RandomIntMat(3, 5, 13), 1, 13)
113 || test_dequantize(RandomIntMat(3, 5, 13), 1, 1)
114 || test_dequantize(RandomIntMat(3, 5, 13), 1, 0)
115 || test_dequantize(RandomIntMat(3, 5, 13), 13, 13)
116 || test_dequantize(RandomIntMat(3, 5, 13), 13, 1)
117 || test_dequantize(RandomIntMat(3, 5, 13), 13, 0);
118 }
119
test_dequantize_1()120 static int test_dequantize_1()
121 {
122 return 0
123 || test_dequantize(RandomIntMat(15, 24), 1, 24)
124 || test_dequantize(RandomIntMat(15, 24), 1, 1)
125 || test_dequantize(RandomIntMat(15, 24), 1, 0)
126 || test_dequantize(RandomIntMat(15, 24), 24, 24)
127 || test_dequantize(RandomIntMat(15, 24), 24, 1)
128 || test_dequantize(RandomIntMat(15, 24), 24, 0)
129 || test_dequantize(RandomIntMat(17, 12), 1, 12)
130 || test_dequantize(RandomIntMat(17, 12), 1, 1)
131 || test_dequantize(RandomIntMat(17, 12), 1, 0)
132 || test_dequantize(RandomIntMat(17, 12), 12, 12)
133 || test_dequantize(RandomIntMat(17, 12), 12, 1)
134 || test_dequantize(RandomIntMat(17, 12), 12, 0)
135 || test_dequantize(RandomIntMat(19, 15), 1, 15)
136 || test_dequantize(RandomIntMat(19, 15), 1, 1)
137 || test_dequantize(RandomIntMat(19, 15), 1, 0)
138 || test_dequantize(RandomIntMat(19, 15), 15, 15)
139 || test_dequantize(RandomIntMat(19, 15), 15, 1)
140 || test_dequantize(RandomIntMat(19, 15), 15, 0);
141 }
142
test_dequantize_2()143 static int test_dequantize_2()
144 {
145 return 0
146 || test_dequantize(RandomIntMat(128), 1, 128)
147 || test_dequantize(RandomIntMat(128), 1, 1)
148 || test_dequantize(RandomIntMat(128), 1, 0)
149 || test_dequantize(RandomIntMat(128), 128, 128)
150 || test_dequantize(RandomIntMat(128), 128, 1)
151 || test_dequantize(RandomIntMat(128), 128, 0)
152 || test_dequantize(RandomIntMat(124), 1, 124)
153 || test_dequantize(RandomIntMat(124), 1, 1)
154 || test_dequantize(RandomIntMat(124), 1, 0)
155 || test_dequantize(RandomIntMat(124), 124, 124)
156 || test_dequantize(RandomIntMat(124), 124, 1)
157 || test_dequantize(RandomIntMat(124), 124, 0)
158 || test_dequantize(RandomIntMat(127), 1, 127)
159 || test_dequantize(RandomIntMat(127), 1, 1)
160 || test_dequantize(RandomIntMat(127), 1, 0)
161 || test_dequantize(RandomIntMat(127), 127, 127)
162 || test_dequantize(RandomIntMat(127), 127, 1)
163 || test_dequantize(RandomIntMat(127), 127, 0);
164 }
165
test_dequantize_3()166 static int test_dequantize_3()
167 {
168 return 0
169 || test_dequantize_pack8(RandomIntMat(5, 7, 24), 1, 24)
170 || test_dequantize_pack8(RandomIntMat(5, 7, 24), 1, 1)
171 || test_dequantize_pack8(RandomIntMat(5, 7, 24), 1, 0)
172 || test_dequantize_pack8(RandomIntMat(5, 7, 24), 24, 24)
173 || test_dequantize_pack8(RandomIntMat(5, 7, 24), 24, 1)
174 || test_dequantize_pack8(RandomIntMat(5, 7, 24), 24, 0)
175 || test_dequantize_pack8(RandomIntMat(15, 24), 1, 24)
176 || test_dequantize_pack8(RandomIntMat(15, 24), 1, 1)
177 || test_dequantize_pack8(RandomIntMat(15, 24), 1, 0)
178 || test_dequantize_pack8(RandomIntMat(15, 24), 24, 24)
179 || test_dequantize_pack8(RandomIntMat(15, 24), 24, 1)
180 || test_dequantize_pack8(RandomIntMat(15, 24), 24, 0)
181 || test_dequantize_pack8(RandomIntMat(128), 1, 128)
182 || test_dequantize_pack8(RandomIntMat(128), 1, 1)
183 || test_dequantize_pack8(RandomIntMat(128), 1, 0)
184 || test_dequantize_pack8(RandomIntMat(128), 128, 128)
185 || test_dequantize_pack8(RandomIntMat(128), 128, 1)
186 || test_dequantize_pack8(RandomIntMat(128), 128, 0);
187 }
188
main()189 int main()
190 {
191 SRAND(7767517);
192
193 return 0
194 || test_dequantize_0()
195 || test_dequantize_1()
196 || test_dequantize_2()
197 || test_dequantize_3();
198 }
199