forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AffineQuantizer.cpp
307 lines (265 loc) · 9.62 KB
/
AffineQuantizer.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
#include <ATen/native/quantized/AffineQuantizer.h>
#include <cfenv>
#ifdef USE_FBGEMM
#include <fbgemm/QuantUtils.h>
#endif
#ifdef __ARM_NEON__
#include <arm_neon.h>
#endif
namespace at {
namespace native {
DEFINE_DISPATCH(quantize_tensor_per_tensor_affine_stub);
DEFINE_DISPATCH(quantize_tensor_per_channel_affine_stub);
DEFINE_DISPATCH(quantize_tensor_per_channel_float_qparams_stub);
DEFINE_DISPATCH(dequantize_tensor_per_tensor_affine_stub);
DEFINE_DISPATCH(dequantize_tensor_per_channel_affine_stub);
DEFINE_DISPATCH(dequantize_tensor_per_channel_float_qparams_stub);
DEFINE_DISPATCH(quantize_tensor_per_tensor_affine_sub_byte_stub);
DEFINE_DISPATCH(dequantize_tensor_per_tensor_affine_sub_byte_stub);
namespace {
void checkRoundingMode(const std::string& fn_name) {
// Disabling this warning message for now as it is printed incorrectly. Need
// to fix
/* TORCH_WARN_ONCE(
std::fegetround() != FE_TONEAREST,
fn_name,
" current rounding mode is not set to round-to-nearest-ties-to-even
(FE_TONEAREST). This will cause accuracy issues in quantized models.");
*/
return;
}
void checkFloatTensor(const std::string& fn_name, const Tensor& t) {
TORCH_CHECK(
t.scalar_type() == kFloat, fn_name, " expects a Float Tensor, got ",
t.scalar_type());
}
void checkSameDevice(
const std::string& fn_name,
const Tensor& t1,
const Tensor& t2) {
TORCH_CHECK(
t1.device() == t2.device(),
fn_name,
" expects a quantized and float tensors to be on the same device.");
}
template <typename T>
void checkQuantizedTensor(const std::string& fn_name, const Tensor& t) {
TORCH_CHECK(t.is_quantized(), fn_name, " expects a quantized Tensor.");
TORCH_CHECK(
t.scalar_type() == caffe2::TypeMeta::Make<T>(),
fn_name,
" expects a ",
caffe2::TypeMeta::Make<T>(),
" Tensor, got ",
t.scalar_type());
}
template <typename T>
void checkZeroPoint(const std::string& fn_name, int64_t zero_point) {
TORCH_CHECK(
zero_point <= std::numeric_limits<T>::max(),
fn_name,
" zero_point ",
zero_point,
" is above upper bound.");
TORCH_CHECK(
zero_point >= std::numeric_limits<T>::min(),
fn_name,
" zero_point ",
zero_point,
" is below lower bound.");
}
template <typename T>
void checkZeroPoints(const std::string& fn_name, const Tensor& zero_points) {
auto zero_points_data = zero_points.data_ptr<int64_t>();
for (const auto i : c10::irange(zero_points.numel())) {
checkZeroPoint<T>(fn_name, zero_points_data[i]);
}
}
void checkSameSize(
const std::string& fn_name,
const Tensor& qt,
const Tensor& rt) {
TORCH_CHECK(
qt.sizes().equals(rt.sizes()),
fn_name,
" only works with Tensors with the same shape");
}
void checkPerChannelParamsSize(
const Tensor& rtensor,
int64_t axis,
const Tensor& scales,
const Tensor& zero_points
) {
int64_t channel = rtensor.size(axis);
TORCH_CHECK(
channel == int64_t(scales.numel()),
"length of scales must equal to channel, expected ", channel, " got, ", scales.numel());
TORCH_CHECK(
channel == int64_t(zero_points.numel()),
"length of zero_points must equal to channel expected ", channel, " got, ", zero_points.numel());
}
} // anonymous namespace
Tensor& quantize_tensor_per_tensor_affine(
const Tensor& rtensor,
Tensor& qtensor,
double scale,
int64_t zero_point) {
static constexpr auto fn_name = "quantize_tensor_per_tensor_affine";
checkRoundingMode(fn_name);
checkFloatTensor(fn_name, rtensor);
checkSameDevice(fn_name, rtensor, qtensor);
checkSameSize(fn_name, qtensor, rtensor);
// NOLINTNEXTLINE(clang-diagnostic-unused-variable)
AT_DISPATCH_QINT_AND_SUB_BYTE_TYPES(qtensor.scalar_type(), fn_name, [&]() {
checkQuantizedTensor<scalar_t>(fn_name, qtensor);
checkZeroPoint<underlying_t>(fn_name, zero_point);
});
// Temporary solution to pack the tensor if dtype is torch.quint4x2
// Can move this into the fbgemm::Quantize op.
if (qtensor.scalar_type() == at::ScalarType::QUInt4x2 || qtensor.scalar_type() == at::ScalarType::QUInt2x4) {
quantize_tensor_per_tensor_affine_sub_byte_stub(
rtensor.device().type(), rtensor, qtensor, scale, zero_point);
} else {
quantize_tensor_per_tensor_affine_stub(
rtensor.device().type(), rtensor, qtensor, scale, zero_point);
}
return qtensor;
}
Tensor& quantize_tensor_per_channel_affine(
const Tensor& rtensor,
Tensor& qtensor,
Tensor scales,
Tensor zero_points,
int64_t axis) {
static constexpr auto fn_name = "quantize_tensor_per_channel_affine";
checkRoundingMode(fn_name);
checkFloatTensor(fn_name, rtensor);
checkSameDevice(fn_name, rtensor, qtensor);
checkSameSize(fn_name, qtensor, rtensor);
AT_DISPATCH_QINT_TYPES(qtensor.scalar_type(), fn_name, [&]() {
checkQuantizedTensor<scalar_t>(fn_name, qtensor);
if (qtensor.device().type() != c10::DeviceType::CUDA &&
qtensor.device().type() != c10::DeviceType::PrivateUse1) {
checkZeroPoints<underlying_t>(fn_name, zero_points);
} // for cuda and privateuse1, this check will occur in the actual device function
});
TORCH_CHECK(
0 <= axis && axis < rtensor.dim(),
"Channel axis out of range in per channel affine quantization. Got: ",
axis,
"Expected: [0, ",
rtensor.dim(),
")");
checkPerChannelParamsSize(rtensor, axis, scales, zero_points);
quantize_tensor_per_channel_affine_stub(
rtensor.device().type(), rtensor, qtensor, scales, zero_points, axis);
return qtensor;
}
Tensor& quantize_tensor_per_channel_float_qparams(
const Tensor& rtensor,
Tensor& qtensor,
Tensor scales,
Tensor zero_points,
int64_t axis) {
static constexpr auto fn_name =
"quantize_tensor_per_channel_float_qparams";
checkRoundingMode(fn_name);
checkFloatTensor(fn_name, rtensor);
checkSameDevice(fn_name, rtensor, qtensor);
checkSameSize(fn_name, qtensor, rtensor);
// NOLINTNEXTLINE(clang-diagnostic-unused-variable)
AT_DISPATCH_QINT_AND_SUB_BYTE_TYPES(qtensor.scalar_type(), fn_name, [&]() {
checkQuantizedTensor<scalar_t>(fn_name, qtensor);
});
TORCH_CHECK(
0 <= axis && axis < rtensor.dim(),
"Channel axis out of range in per channel float qparams quantization. Got: ",
axis,
"Expected: [0, ",
rtensor.dim(),
")");
checkPerChannelParamsSize(rtensor, axis, scales, zero_points);
quantize_tensor_per_channel_float_qparams_stub(
rtensor.device().type(), rtensor, qtensor, scales, zero_points, axis);
return qtensor;
}
Tensor& dequantize_tensor_per_tensor_affine(
const Tensor& qtensor,
Tensor& rtensor,
double scale,
int64_t zero_point) {
static constexpr auto fn_name = "dequantize_tensor_per_tensor_affine";
checkFloatTensor(fn_name, rtensor);
checkSameDevice(fn_name, rtensor, qtensor);
checkSameSize(fn_name, qtensor, rtensor);
// NOLINTNEXTLINE(clang-diagnostic-unused-variable)
AT_DISPATCH_QINT_AND_SUB_BYTE_TYPES(qtensor.scalar_type(), fn_name, [&]() {
checkQuantizedTensor<scalar_t>(fn_name, qtensor);
checkZeroPoint<underlying_t>(fn_name, zero_point);
});
if (qtensor.scalar_type() == at::ScalarType::QUInt4x2 || qtensor.scalar_type() == at::ScalarType::QUInt2x4) {
dequantize_tensor_per_tensor_affine_sub_byte_stub(
qtensor.device().type(), qtensor, rtensor, scale, zero_point);
} else {
dequantize_tensor_per_tensor_affine_stub(
qtensor.device().type(), qtensor, rtensor, scale, zero_point);
}
return rtensor;
}
Tensor& dequantize_tensor_per_channel_affine(
const Tensor& qtensor,
Tensor& rtensor,
Tensor scales,
Tensor zero_points,
int64_t axis) {
static constexpr auto fn_name = "dequantize_tensor_per_channel_affine";
checkFloatTensor(fn_name, rtensor);
checkSameDevice(fn_name, rtensor, qtensor);
checkSameSize(fn_name, qtensor, rtensor);
AT_DISPATCH_QINT_TYPES(qtensor.scalar_type(), fn_name, [&]() {
checkQuantizedTensor<scalar_t>(fn_name, qtensor);
if(qtensor.device().type() != c10::DeviceType::CUDA &&
qtensor.device().type() != c10::DeviceType::PrivateUse1){
checkZeroPoints<underlying_t>(fn_name, zero_points);
} // for cuda and privateuse1, this check will occur in the actual device function
});
TORCH_CHECK(
0 <= axis && axis < qtensor.dim(),
"Channel axis out of range in per channel affine dequantization. Got:",
axis,
" Expected: [0, ",
qtensor.dim(),
")");
checkPerChannelParamsSize(rtensor, axis, scales, zero_points);
dequantize_tensor_per_channel_affine_stub(
qtensor.device().type(), qtensor, rtensor, scales, zero_points, axis);
return rtensor;
}
Tensor& dequantize_tensor_per_channel_float_qparams(
const Tensor& qtensor,
Tensor& rtensor,
Tensor scales,
Tensor zero_points,
int64_t axis) {
static constexpr auto fn_name = "dequantize_tensor_per_channel_affine";
checkFloatTensor(fn_name, rtensor);
checkSameDevice(fn_name, rtensor, qtensor);
checkSameSize(fn_name, qtensor, rtensor);
// NOLINTNEXTLINE(clang-diagnostic-unused-variable)
AT_DISPATCH_QINT_AND_SUB_BYTE_TYPES(qtensor.scalar_type(), fn_name, [&]() {
checkQuantizedTensor<scalar_t>(fn_name, qtensor);
});
TORCH_CHECK(
0 <= axis && axis < qtensor.dim(),
"Channel axis out of range in per channel float qparams dequantization. Got:",
axis,
" Expected: [0, ",
qtensor.dim(),
")");
checkPerChannelParamsSize(rtensor, axis, scales, zero_points);
dequantize_tensor_per_channel_float_qparams_stub(
qtensor.device().type(), qtensor, rtensor, scales, zero_points, axis);
return rtensor;
}
} // namespace native
} // namespace at