forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuantUtilsTest.cc
442 lines (369 loc) · 12.6 KB
/
QuantUtilsTest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
* Copyright (c) Facebook, Inc. and its affiliates.
* All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <algorithm>
#include <climits>
#include <limits>
#include <random>
#include <gtest/gtest.h>
#include "fbgemm/QuantUtils.h"
#include "fbgemm/Utils.h"
using namespace std;
using namespace fbgemm;
// tuple represents K, C, X, G, layout_t
// layout_t can be KCX or KXC
class QuantizeGroupwiseTest
: public testing::TestWithParam<tuple<int, int, int, int, layout_t>> {};
class QuantizeTest : public testing::TestWithParam<int> {};
class FusedQuantizeDequantizeTest : public testing::TestWithParam<int> {};
INSTANTIATE_TEST_CASE_P(
InstantiationName,
QuantizeGroupwiseTest,
::testing::Combine(
::testing::ValuesIn({4, 12, 64}), // K
::testing::ValuesIn({12, 16, 32}), // C
::testing::ValuesIn({1, 10, 15, 30}), // X
::testing::ValuesIn({1, 4}), // G
::testing::ValuesIn({layout_t::KCX, layout_t::KXC})));
INSTANTIATE_TEST_CASE_P(
InstantiationName,
QuantizeTest,
::testing::Values(1, 2, 5, 8, 9, 16, 20, 28, 32, 33));
template <typename T, layout_t LT>
void ref_impl(
const vector<float>& src,
int K,
int C,
int X,
int G,
const vector<float>& scales,
const vector<int>& zero_points,
vector<T>& dst) {
int C_per_G = C / G;
for (int i = 0; i < K; ++i) {
for (int g = 0; g < G; ++g) {
for (int c = 0; c < C / G; ++c) {
for (int x = 0; x < X; ++x) {
float num;
if (LT == layout_t::KCX) {
num = src[(i * C + g * C_per_G + c) * X + x];
} else {
num = src[(i * X + x) * C + g * C_per_G + c];
}
int res = nearbyint(zero_points[g] + num / scales[g]);
T final_res = min<T>(
max<T>(res, numeric_limits<T>::min()), numeric_limits<T>::max());
if (LT == layout_t::KCX) {
dst[(i * C + g * C_per_G + c) * X + x] = final_res;
} else {
dst[(i * X + x) * C + g * C_per_G + c] = final_res;
}
}
}
}
}
}
template <typename T, layout_t LT>
void runTests(
const vector<float>& src,
int K,
int C,
int X,
int G,
const vector<float>& scales,
const vector<int>& zero_points,
vector<T>& dst,
vector<T>& dst_ref) {
QuantizeGroupwise<T, LT>(
src.data(), K, C, X, G, scales.data(), zero_points.data(), dst.data());
ref_impl<T, LT>(src, K, C, X, G, scales, zero_points, dst_ref);
}
/**
* There can be off-by-one error in quantized values due to how the mid-point
* cases are rounded-off in vectorized vs scalar codes and due to adding of
* zero_point before rounding vs after rounding. We ignore such differences
* while comparing results.
*/
template <typename T>
::testing::AssertionResult isNear(
const vector<T>& res,
const vector<T>& res_ref) {
bool match = true;
if (res.size() == res_ref.size()) {
for (int i = 0; i < res.size(); ++i) {
if (!(res[i] == res_ref[i] || res[i] == res_ref[i] + 1 ||
res[i] == res_ref[i] - 1)) {
match = false;
break;
}
}
}
if (match)
return ::testing::AssertionSuccess();
else
return ::testing::AssertionFailure() << " Quantized results do not match";
}
bool floatEqualAll(vector<float>& a, vector<float>& b) {
if (a.size() != b.size()) {
return false;
}
for (int i = 0; i < a.size(); i++) {
if (fabs(a[i] - b[i]) > std::numeric_limits<float>::epsilon()) {
return false;
}
}
return true;
}
/**
* Test for QuantizeGroupwise
*/
TEST_P(QuantizeGroupwiseTest, quantizeGTest) {
int K, C, X, G;
layout_t layout;
tie(K, C, X, G, layout) = GetParam();
random_device rd;
mt19937 gen(rd());
uniform_real_distribution<float> disFP(0.1, 1.1);
vector<float> inp(K * C * X);
generate(inp.begin(), inp.end(), [&, disFP]() mutable { return disFP(gen); });
vector<float> scales(G);
generate(scales.begin(), scales.end(), [&, disFP]() mutable {
return disFP(gen);
});
uniform_int_distribution<> disUInt8(0, 8);
vector<int> zero_points_uint8(G);
generate(
zero_points_uint8.begin(),
zero_points_uint8.end(),
[&, disUInt8]() mutable { return disUInt8(gen); });
uniform_int_distribution<> disInt8(-64, 63);
vector<int> zero_points_int8(G);
generate(
zero_points_int8.begin(), zero_points_int8.end(), [&, disInt8]() mutable {
return disInt8(gen);
});
uniform_int_distribution<> disInt32(-512, 512);
vector<int> zero_points_int32(G);
generate(
zero_points_int32.begin(),
zero_points_int32.end(),
[&, disInt32]() mutable { return disInt32(gen); });
vector<uint8_t> dstuint8(K * C * X);
vector<uint8_t> dstuint8_ref(K * C * X);
vector<int8_t> dstint8(K * C * X);
vector<int8_t> dstint8_ref(K * C * X);
vector<int32_t> dstint32(K * C * X);
vector<int32_t> dstint32_ref(K * C * X);
if (layout == layout_t::KCX) {
runTests<uint8_t, layout_t::KCX>(
inp, K, C, X, G, scales, zero_points_uint8, dstuint8, dstuint8_ref);
runTests<int8_t, layout_t::KCX>(
inp, K, C, X, G, scales, zero_points_int8, dstint8, dstint8_ref);
runTests<int32_t, layout_t::KCX>(
inp, K, C, X, G, scales, zero_points_int32, dstint32, dstint32_ref);
} else {
runTests<uint8_t, layout_t::KXC>(
inp, K, C, X, G, scales, zero_points_uint8, dstuint8, dstuint8_ref);
runTests<int8_t, layout_t::KXC>(
inp, K, C, X, G, scales, zero_points_int8, dstint8, dstint8_ref);
runTests<int32_t, layout_t::KXC>(
inp, K, C, X, G, scales, zero_points_int32, dstint32, dstint32_ref);
}
EXPECT_TRUE(isNear(dstuint8, dstuint8_ref));
EXPECT_TRUE(isNear(dstint8, dstint8_ref));
EXPECT_TRUE(isNear(dstint32, dstint32_ref));
}
template <typename T>
void runQuantizeTests(
const vector<float>& src,
float scale,
int zero_point,
vector<T>& dst,
vector<T>& dst_ref) {
// reference
for (int i = 0; i < src.size(); ++i) {
dst_ref[i] = Quantize<T>(src[i], zero_point, scale, CHAR_BIT * sizeof(T));
}
TensorQuantizationParams qparams;
qparams.scale = scale;
qparams.zero_point = zero_point;
qparams.precision = CHAR_BIT * sizeof(T);
Quantize<T>(src.data(), dst.data(), src.size(), qparams);
}
/**
* Test for QuantizeGroupwise
*/
TEST_P(QuantizeTest, quantizeTest) {
int len;
len = GetParam();
random_device rd;
mt19937 gen(rd());
uniform_real_distribution<float> disFP(-1.0e6, 1.0e6);
vector<float> inp(len);
generate(inp.begin(), inp.end(), [&, disFP]() mutable { return disFP(gen); });
float scale = disFP(gen);
// Generate a number between [0, 255] both inclusive
uniform_int_distribution<> disUInt8(0, 255);
int zero_point_uint8 = disUInt8(gen);
uniform_int_distribution<> disInt8(-128, 127);
int zero_point_int8 = disInt8(gen);
vector<uint8_t> dstuint8(len);
vector<uint8_t> dstuint8_ref(len);
vector<int8_t> dstint8(len);
vector<int8_t> dstint8_ref(len);
runQuantizeTests<uint8_t>(
inp, scale, zero_point_uint8, dstuint8, dstuint8_ref);
runQuantizeTests<int8_t>(inp, scale, zero_point_int8, dstint8, dstint8_ref);
EXPECT_TRUE(isNear(dstuint8, dstuint8_ref));
EXPECT_TRUE(isNear(dstint8, dstint8_ref));
}
// vector and scalar code should have the same behavior
TEST(QuantizeTestSingle, vectorScalar) {
// This length will exercise both the vector and scalar path
int len = 33;
vector<float> src(len);
vector<uint8_t> dst(len);
for (int i = 0; i < len; ++i) {
src[i] = -2.9483526e-05;
}
float scale = 2.3124334356729307e-07;
int zero_point = 128;
TensorQuantizationParams qparams;
qparams.scale = scale;
qparams.zero_point = zero_point;
qparams.precision = CHAR_BIT * sizeof(uint8_t);
Quantize<uint8_t>(src.data(), dst.data(), len, qparams);
// Check if all elements are equal
EXPECT_TRUE(
adjacent_find(dst.begin(), dst.end(), not_equal_to<int>()) == dst.end());
}
TEST(QuantizeTest, cornerCases) {
TensorQuantizationParams qparams;
qparams.scale = 1.19209e-07;
qparams.zero_point = 0;
qparams.precision = 8;
std::vector<float> src1 = {3.40282e+38, -2.16845e+38};
std::vector<int8_t> dst_int8(src1.size());
Quantize<int8_t>(src1.data(), dst_int8.data(), dst_int8.size(), qparams);
EXPECT_EQ(dst_int8[0], 127);
EXPECT_EQ(dst_int8[1], -128);
// Tests vectorized and remainder paths
std::vector<float> src2 = {3.40282e+38,
-2.16845e+38,
3.40282e+38,
-2.16845e+38,
3.40282e+38,
-2.16845e+38,
3.40282e+38,
-2.16845e+38,
3.40282e+38};
std::vector<uint8_t> dst_uint8(src2.size());
Quantize<uint8_t>(src2.data(), dst_uint8.data(), dst_uint8.size(), qparams);
EXPECT_EQ(dst_uint8[0], 255);
EXPECT_EQ(dst_uint8[1], 0);
EXPECT_EQ(dst_uint8[8], 255);
qparams.precision = 16;
std::vector<int16_t> dst_int16(src2.size());
Quantize<int16_t>(src2.data(), dst_int16.data(), dst_int16.size(), qparams);
EXPECT_EQ(dst_int16[0], 32767);
EXPECT_EQ(dst_int16[1], -32768);
}
template <typename T>
void runFusedQuantizeDequantizeTests(
const vector<float>& src,
float scale,
int zero_point,
vector<float>& dst,
vector<float>& dst_ref) {
TensorQuantizationParams qparams;
qparams.scale = scale;
qparams.zero_point = zero_point;
qparams.precision = CHAR_BIT * sizeof(T);
// reference
for (int i = 0; i < src.size(); ++i) {
dst_ref[i] = FusedQuantizeDequantize<T>(src[i], qparams);
}
FusedQuantizeDequantize<T>(src.data(), dst.data(), src.size(), qparams);
}
TEST_P(FusedQuantizeDequantizeTest, fusedQuantizeDequantizeTest) {
int len;
len = GetParam();
random_device rd;
mt19937 gen(rd());
uniform_real_distribution<float> disFP(-1.0e6, 1.0e6);
vector<float> inp(len);
generate(inp.begin(), inp.end(), [&, disFP]() mutable { return disFP(gen); });
float scale = disFP(gen);
// Generate a number between [0, 255] both inclusive
uniform_int_distribution<> disUInt8(0, 255);
int zero_point_uint8 = disUInt8(gen);
uniform_int_distribution<> disInt8(-128, 127);
int zero_point_int8 = disInt8(gen);
vector<float> dstfloat(len);
vector<float> dstfloat_ref(len);
runFusedQuantizeDequantizeTests<uint8_t>(
inp, scale, zero_point_uint8, dstfloat, dstfloat_ref);
EXPECT_TRUE(floatEqualAll(dstfloat, dstfloat_ref));
runFusedQuantizeDequantizeTests<int8_t>(
inp, scale, zero_point_int8, dstfloat, dstfloat_ref);
EXPECT_TRUE(floatEqualAll(dstfloat, dstfloat_ref));
}
// vector and scalar code should have the same behavior
TEST(FusedQuantizeDequantizeTestSingle, vectorScalar) {
// This length will exercise both the vector and scalar path
int len = 33;
vector<float> src(len);
vector<float> dst(len);
for (int i = 0; i < len; ++i) {
src[i] = -2.9483526e-05;
}
float scale = 2.3124334356729307e-07;
int zero_point = 128;
TensorQuantizationParams qparams;
qparams.scale = scale;
qparams.zero_point = zero_point;
qparams.precision = CHAR_BIT * sizeof(uint8_t);
FusedQuantizeDequantize<uint8_t>(src.data(), dst.data(), src.size(), qparams);
// Check if all elements are equal
EXPECT_TRUE(
adjacent_find(dst.begin(), dst.end(), not_equal_to<float>()) ==
dst.end());
}
TEST(FusedQuantizeDequantizeTest, cornerCases) {
TensorQuantizationParams qparams;
qparams.scale = 1.19209e-07;
qparams.zero_point = 0;
qparams.precision = 8;
vector<float> src1 = {3.40282e+38, -2.16845e+38};
vector<float> ref = {1.5139543e-05, -1.5258752e-05};
vector<float> dst_int8(src1.size());
FusedQuantizeDequantize<int8_t>(
src1.data(), dst_int8.data(), src1.size(), qparams);
EXPECT_TRUE(floatEqualAll(dst_int8, ref));
// Tests vectorized and remainder paths
vector<float> src2 = {3.40282e+38,
-2.16845e+38,
3.40282e+38,
-2.16845e+38,
3.40282e+38,
-2.16845e+38,
3.40282e+38,
-2.16845e+38,
3.40282e+38};
vector<float> ref2 = {3.0398295e-05,
0,
3.0398295e-05,
0,
3.0398295e-05,
0,
3.0398295e-05,
0,
3.0398295e-05};
std::vector<float> dst_uint8(src2.size(), 0);
FusedQuantizeDequantize<uint8_t>(
src2.data(), dst_uint8.data(), src2.size(), qparams);
EXPECT_TRUE(floatEqualAll(dst_uint8, ref2));
}