/
quantized_test.cpp
354 lines (320 loc) · 11.5 KB
/
quantized_test.cpp
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
#include <gtest/gtest.h>
#include <ATen/ATen.h>
#include <ATen/test/test_assert.h>
#include <cmath>
#include <iostream>
#include <limits>
#include <memory>
#include <sstream>
#include <type_traits>
// For quantize_val
#include <ATen/native/quantized/AffineQuantizer.h>
#include <c10/core/ScalarType.h>
#include <c10/util/irange.h>
#include <ATen/quantized/Quantizer.h>
using namespace at;
#ifndef ATEN_CPU_STATIC_DISPATCH
TEST(TestQTensor, QuantDequantAPIs) {
auto num_elements = 10;
Tensor r = at::ones({num_elements});
const double scale = 1.0;
const int64_t zero_point = 2;
const Tensor qr = at::quantize_per_tensor(r, scale, zero_point, kQUInt8);
ASSERT_EQ(qr.q_scale(), scale);
ASSERT_EQ(qr.q_zero_point(), zero_point);
ASSERT_TRUE(qr.is_quantized());
ASSERT_FALSE(r.is_quantized());
// int_repr
Tensor int_repr = qr.int_repr();
auto* int_repr_data = int_repr.data_ptr<uint8_t>();
for (const auto i : c10::irange(num_elements)) {
ASSERT_EQ(int_repr_data[i], 3);
}
// Check for correct quantization
auto r_data = r.data_ptr<float>();
auto qr_data = qr.data_ptr<quint8>();
for (const auto i : c10::irange(num_elements)) {
ASSERT_EQ(
native::quantize_val<quint8>(scale, zero_point, r_data[i]).val_,
qr_data[i].val_);
}
// Check for correct dequantization
Tensor rqr = qr.dequantize();
auto rqr_data = rqr.data_ptr<float>();
for (const auto i : c10::irange(num_elements)) {
ASSERT_EQ(r_data[i], rqr_data[i]);
}
for (const auto i : c10::irange(num_elements)) {
ASSERT_EQ(
r_data[i],
native::dequantize_val(qr.q_scale(), qr.q_zero_point(), qr_data[i]));
}
// Check for correct requantization
double new_scale = 2.0;
int64_t new_zero_point = 1;
Tensor reqr = at::quantize_per_tensor(r, new_scale, new_zero_point, kQInt8);
auto reqr_data = reqr.data_ptr<qint8>();
for (const auto i : c10::irange(num_elements)) {
reqr_data[i].val_ =
native::requantize_val<quint8, qint8>(
scale, zero_point, new_scale, new_zero_point, qr_data[i])
.val_;
const qint8 expected =
native::quantize_val<qint8>(new_scale, new_zero_point, rqr_data[i]);
ASSERT_EQ(expected.val_, reqr_data[i].val_);
}
}
TEST(TestQTensor, RoundingMode) {
// We assume that quantization is defined as:
// qx = clamp(zero_point + round(x / scale))
// If the zero_point is added before rounding, the result will be wrong.
int32_t zero_point = 5;
std::vector<float> x_values{
-5.5, -4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5};
std::vector<uint8_t> qx_expect{
0, 1, 1, 3, 3, 5, 5, 7, 7, 9, 9, 11}; // scale = 1.0
Tensor x = from_blob(x_values.data(), x_values.size());
Tensor qx = at::quantize_per_tensor(x, /*scale=*/1.0, zero_point, kQUInt8);
auto qx_data = qx.data_ptr<quint8>();
for (const auto idx : c10::irange(x_values.size())) {
ASSERT_EQ(qx_expect[idx], qx_data[idx].val_)
<< "Tie breaking during rounding element " << idx << " failed!";
}
}
TEST(TestQTensor, Item) {
Tensor r = at::ones({1});
const float scale = 1;
const int32_t zero_point = 2;
Tensor qr = at::quantize_per_tensor(r, scale, zero_point, kQUInt8);
ASSERT_EQ(r.item().to<float>(), qr.item().to<float>());
}
TEST(TestQTensor, EmptyQuantized) {
float scale = 0.5;
int zero_point = 10;
int val = 100;
int numel = 10;
Tensor q = at::_empty_affine_quantized(
{numel}, at::device(at::kCPU).dtype(kQUInt8), scale, zero_point);
// Assigning to QTensor
auto* q_data = q.data_ptr<quint8>();
for (const auto i : c10::irange(numel)) {
q_data[i].val_ = val;
}
// dequantize
auto r = q.dequantize();
auto* r_data = r.data_ptr<float>();
for (const auto i : c10::irange(numel)) {
ASSERT_EQ(r_data[i], (val - zero_point) * scale);
}
}
TEST(TestQTensor, EmptyPerchannelQuantized) {
int numel = 10;
auto scales = rand({numel}).toType(kDouble);
auto zero_points = randint(10, {10}).toType(kLong);
int val = 100;
int ch_axis = 0;
Tensor q = at::_empty_per_channel_affine_quantized(
{numel},
scales,
zero_points,
ch_axis,
at::device(at::kCPU).dtype(kQUInt8));
// Assigning to QTensor
auto* q_data = q.data_ptr<quint8>();
for (const auto i : c10::irange(numel)) {
q_data[i].val_ = val;
}
// dequantize
auto r = q.dequantize();
auto* r_data = r.data_ptr<float>();
for (const auto i : c10::irange(numel)) {
ASSERT_EQ(
r_data[i],
(val - zero_points[i].item().to<int>()) * scales[i].item().to<float>());
}
}
TEST(TestQTensor, QuantizePerChannel4d) {
int C = 64, H = 10, W = 10;
auto scales = rand({C}).toType(kDouble);
auto zero_points = randint(10, {C}).toType(kLong);
int ch_axis = 1;
// create 4d tensor where each H x W image is a range(0, H*W)
Tensor tensor = at::empty({1, C, H, W}, at::device(at::kCPU).dtype(kFloat));
auto* tensor_data = tensor.mutable_data_ptr<float>();
for (int c = 0, i = 0; c < C; ++c) {
for (int e = 0; e < H * W; ++e, ++i) {
tensor_data[i] = e;
}
}
// quantize and check values
Tensor q = at::native::quantize_per_channel(
tensor, scales, zero_points, ch_axis, kQUInt8);
auto* q_data = (uint8_t*)q.data_ptr<quint8>();
for (int c = 0, i = 0; c < C; ++c) {
float inv_scale = 1.0f / static_cast<float>(scales[c].item<double>());
int64_t zero_point = zero_points[c].item<int64_t>();
for (int e = 0; e < H * W; ++e, ++i) {
// downsize qval to 255 if val is greater than max uint8_t value
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers,bugprone-narrowing-conversions)
int qval = std::min<int>(zero_point + std::nearbyint(e * inv_scale), 255);
ASSERT_EQ((int)q_data[i], qval);
}
}
}
TEST(TestQTensor, QuantizePerChannel4dChannelsLast) {
int C = 64, H = 10, W = 10;
auto scales = rand({C}).toType(kDouble);
auto zero_points = randint(10, {C}).toType(kLong);
int ch_axis = 1;
// create 4d tensor where each H x W image is a range(0, H*W)
Tensor tensor = at::empty(
{1, C, H, W},
at::device(at::kCPU).dtype(kFloat).memory_format(
at::MemoryFormat::ChannelsLast));
auto* tensor_data = tensor.data_ptr<float>();
for (int e = 0, i = 0; e < H * W; ++e) {
for (int c = 0; c < C; ++c, ++i) {
tensor_data[i] = e;
}
}
// quantize and check values
Tensor q = at::native::quantize_per_channel(
tensor, scales, zero_points, ch_axis, kQUInt8);
auto* q_data = (uint8_t*)q.data_ptr<quint8>();
for (int e = 0, i = 0; e < H * W; ++e) {
for (int c = 0; c < C; ++c, ++i) {
float inv_scale = 1.0f / static_cast<float>(scales[c].item<double>());
int64_t zero_point = zero_points[c].item<int64_t>();
// downsize qval to 255 if val is greater than max uint8_t value
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,cppcoreguidelines-avoid-magic-numbers,bugprone-narrowing-conversions)
int qval = std::min<int>(zero_point + std::nearbyint(e * inv_scale), 255);
ASSERT_EQ((int)q_data[i], qval);
}
}
}
TEST(TestQTensor, FromBlobQuantizedPerTensor) {
const float scale = 0.1;
const int64_t zero_point = 10;
std::vector<int64_t> shape = {5, 10};
auto numel = c10::multiply_integers(shape);
TensorOptions options(at::kQUInt8);
auto custom_vec = std::make_unique<std::vector<uint8_t>>();
custom_vec->resize(numel);
uint8_t* custom_data = custom_vec->data();
for (const auto i : c10::irange(numel)) {
custom_data[i] = i;
}
bool customDataDeleted{false};
auto deleteWhenDone = custom_vec.release();
auto deleter = [deleteWhenDone, custom_data, &customDataDeleted](void* inp) {
ASSERT_EQ((void*)inp, (void*)custom_data);
delete deleteWhenDone;
customDataDeleted = true;
};
{
Tensor qtensor = at::from_blob_quantized_per_tensor_affine(custom_data, shape, deleter, scale, zero_point, options);
uint8_t* q_data = (uint8_t*)qtensor.data_ptr<quint8>();
for (const auto i : c10::irange(numel)) {
ASSERT_EQ((int)custom_data[i], (int)q_data[i]);
}
for (int h = 0, i = 0; h < shape[0]; ++h) {
for (int w = 0; w < shape[1]; ++w, ++i) {
ASSERT_EQ(
qtensor[h][w].item<float>(),
(custom_data[i] - zero_point) * scale);
}
}
ASSERT_EQ((float)qtensor.q_scale(), (float)scale);
ASSERT_EQ(qtensor.q_zero_point(), zero_point);
}
TORCH_CHECK(customDataDeleted);
}
TEST(TestQTensor, FromBlobQuantizedPerChannel) {
int C = 64, H = 10, W = 5;
std::vector<int64_t> shape = {1, C, H, W};
auto scales = rand({C}).toType(kDouble);
auto zero_points = randint(10, {C}).toType(kLong);
auto numel = c10::multiply_integers(shape);
int ch_axis = 1;
TensorOptions options(at::kQUInt8);
auto custom_vec = std::make_unique<std::vector<uint8_t>>();
custom_vec->resize(numel);
uint8_t* custom_data = custom_vec->data();
for (const auto i : c10::irange(numel)) {
custom_data[i] = i;
}
bool customDataDeleted{false};
auto deleteWhenDone = custom_vec.release();
auto deleter = [deleteWhenDone, custom_data, &customDataDeleted](void* inp) {
ASSERT_EQ((void*)inp, (void*)custom_data);
delete deleteWhenDone;
customDataDeleted = true;
};
{
Tensor qtensor = at::from_blob_quantized_per_channel_affine(custom_data, shape, deleter, scales, zero_points, ch_axis, options);
uint8_t* q_data = (uint8_t*)qtensor.data_ptr<quint8>();
for (const auto i : c10::irange(numel)) {
ASSERT_EQ((int)custom_data[i], (int)q_data[i]);
}
ASSERT_TRUE(at::allclose(qtensor.q_per_channel_scales(), scales));
ASSERT_TRUE(at::allclose(qtensor.q_per_channel_zero_points(), zero_points));
ASSERT_TRUE(qtensor.is_quantized());
}
TORCH_CHECK(customDataDeleted);
}
#if defined(__ARM_NEON__) || defined(__aarch64__)
TEST(TestQTensor, TestArmVectorizedQuantizeDequantize) {
const float scale = 7;
const int numel = 132;
std::vector<float> x_values;
for (const auto i : c10::irange(numel)) {
x_values.push_back(9 * i);
}
const Tensor x = from_blob(x_values.data(), x_values.size());
auto test_for_datatype = [&](
const ScalarType scalar_type,
const auto get_data_ptr,
const auto quantize_val_with_datatype,
const int zero_point_min,
const int zero_point_max) {
for (int zero_point : {zero_point_min, 10, zero_point_max}) {
const Tensor q = at::quantize_per_tensor(x, scale, zero_point, scalar_type);
auto* q_data = get_data_ptr(q);
for (const auto i : c10::irange(numel)) {
ASSERT_EQ(
q_data[i].val_,
quantize_val_with_datatype(scale, zero_point, x_values[i]).val_);
}
const Tensor r = q.dequantize();
const float* r_data = r.const_data_ptr<float>();
for (const auto i : c10::irange(numel)) {
ASSERT_FLOAT_EQ(
r_data[i],
native::dequantize_val(scale, zero_point, q_data[i]));
}
}
};
// Unsigned Int 8
test_for_datatype(
kQUInt8,
[](Tensor q) { return q.data_ptr<quint8>(); },
native::quantize_val<quint8>,
std::numeric_limits<uint8_t>::min(),
std::numeric_limits<uint8_t>::max());
// Signed Int 8
test_for_datatype(
kQInt8,
[](Tensor q) { return q.data_ptr<qint8>(); },
native::quantize_val<qint8>,
std::numeric_limits<int8_t>::min(),
std::numeric_limits<int8_t>::max());
// Signed Int 32 (not optimized with vectorization)
test_for_datatype(
kQInt32,
[](Tensor q) { return q.data_ptr<qint32>(); },
native::quantize_val<qint32>,
std::numeric_limits<int32_t>::min(),
std::numeric_limits<int32_t>::max());
}
#endif // (__ARM_NEON__) || defined(__aarch64__)
#endif // ATEN_CPU_STATIC_DISPATCH