-
Notifications
You must be signed in to change notification settings - Fork 24k
/
Copy pathqconv_dynamic.cpp
239 lines (198 loc) · 7.23 KB
/
qconv_dynamic.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
#define TORCH_ASSERT_ONLY_METHOD_OPERATORS
#include <algorithm>
#include <ATen/core/Tensor.h>
#include <ATen/core/ivalue.h>
#include <ATen/Parallel.h>
#include <ATen/SmallVector.h>
#include <ATen/native/quantized/PackedParams.h>
#include <ATen/native/quantized/cpu/fbgemm_utils.h>
#include <ATen/native/quantized/cpu/QnnpackUtils.h>
#include <ATen/native/quantized/cpu/OnednnUtils.h>
#include <ATen/native/quantized/cpu/QuantUtils.h>
#include <c10/util/irange.h>
#include <torch/library.h>
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/Functions.h>
#else
#include <ATen/ops/dequantize.h> // for dequantize
#include <ATen/ops/quantize_per_tensor.h>
#endif
#ifdef USE_FBGEMM
template <int kSpatialDim>
at::Tensor PackedConvWeight<kSpatialDim>::apply_dynamic(
const at::Tensor& input,
bool reduce_range) {
TORCH_CHECK(
fbgemm::fbgemmSupportedCPU(), "Your CPU does not support FBGEMM.");
float x_min, x_max;
fbgemm::FindMinMax(
/*m=*/input.data_ptr<float>(),
/*min=*/&x_min,
/*max=*/&x_max,
/*len=*/input.numel());
// Input tensor is quantized as 8-bit unsigned values
static constexpr int precision = 8;
static constexpr bool is_signed = false;
// Calculate scale and zero point for quantization of input tensor
auto q_params = quant_utils::ChooseQuantizationParams(
/*min=*/x_min,
/*max=*/x_max,
/*qmin=*/is_signed ? -(1 << (precision - 1)) : 0,
/*qmax=*/
is_signed ? ((1 << (precision - 1)) - 1) : (1 << precision) - 1,
/*preserve_sparsity=*/false,
/*force_scale_power_of_two=*/false,
/*reduce_range=*/reduce_range);
// Quantize input
at::Tensor q_input = at::quantize_per_tensor(
input, q_params.scale, q_params.zero_point, c10::kQUInt8);
at::Tensor out =
apply_impl<false>(q_input, q_params.scale, q_params.zero_point);
return at::dequantize(out); // TODO: optimized kernel that outputs fp32 so
// this step isn't necessary
}
template at::Tensor PackedConvWeight<2>::apply_dynamic(
const at::Tensor& input,
bool reduce_range);
template at::Tensor PackedConvWeight<3>::apply_dynamic(
const at::Tensor& input,
bool reduce_range);
#endif // USE_FBGEMM
#ifdef USE_PYTORCH_QNNPACK
template <int kSpatialDim>
at::Tensor PackedConvWeightsQnnp<kSpatialDim>::apply_dynamic(
const at::Tensor& input,
bool reduce_range) {
if (reduce_range) {
TORCH_WARN("Currently, qnnpack incorrectly ignores reduce_range when it is set to true; this may change in a future release.");
}
// On empty input, no output data will be generated,
// so use arbitrary qparams.
float x_min = 0;
float x_max = 0;
// Otherwise...
if (input.numel() > 0) {
x_min = input.min().item<float>();
x_max = input.max().item<float>();
}
// Input tensor is quantized as 8-bit unsigned values
static constexpr int precision = 8;
static constexpr bool is_signed = false;
// Calculate scale and zero point for quantization of input tensor
auto q_params = quant_utils::ChooseQuantizationParams(
/*min=*/x_min,
/*max=*/x_max,
/*qmin=*/is_signed ? -(1 << (precision - 1)) : 0,
/*qmax=*/
is_signed ? ((1 << (precision - 1)) - 1) : (1 << precision) - 1,
/*preserve_sparsity=*/false,
/*force_scale_power_of_two=*/false,
/*reduce_range=*/false); // note: this is set to false rather than
// reduce_range for qnnpack
// Quantize input
at::Tensor q_input = at::quantize_per_tensor(
input, q_params.scale, q_params.zero_point, c10::kQUInt8);
at::Tensor out =
apply_impl<false>(q_input, q_params.scale, q_params.zero_point);
return at::dequantize(out); // TODO: optimized kernel that outputs fp32 so
// this step isn't necessary
}
template at::Tensor PackedConvWeightsQnnp<2>::apply_dynamic(
const at::Tensor& input,
bool reduce_range);
template at::Tensor PackedConvWeightsQnnp<3>::apply_dynamic(
const at::Tensor& input,
bool reduce_range);
#endif // USE_PYTORCH_QNNPACK
#if AT_MKLDNN_ENABLED()
template <int kSpatialDim>
at::Tensor PackedConvWeightsOnednn<kSpatialDim>::apply_dynamic(
const at::Tensor& input,
bool reduce_range) {
// Find min/max of input
float x_max = 0, x_min = 0;
if (input.numel() > 0) {
x_min = input.min().item<float>();
x_max = input.max().item<float>();
}
// Input tensor is quantized as 8-bit unsigned values
static constexpr int precision = 8;
static constexpr bool is_signed = false;
// Calculate scale and zero point for quantization of input tensor
auto q_params = quant_utils::ChooseQuantizationParams(
/*min=*/x_min,
/*max=*/x_max,
/*qmin=*/is_signed ? -(1 << (precision - 1)) : 0,
/*qmax=*/
is_signed ? ((1 << (precision - 1)) - 1) : (1 << precision) - 1,
/*preserve_sparsity=*/false,
/*force_scale_power_of_two=*/false,
/*reduce_range=*/reduce_range);
// Quantize input
at::Tensor q_input = at::quantize_per_tensor(
input, q_params.scale, q_params.zero_point, c10::kQUInt8);
at::Tensor out =
apply_impl<false>(q_input, /*accum*/std::nullopt, q_params.scale, q_params.zero_point);
// TODO: Modify ideep to allow fp32 input & output
// to avoid explicit `quantize - dequantize`
return at::dequantize(out);
}
template at::Tensor PackedConvWeightsOnednn<2>::apply_dynamic(
const at::Tensor& input,
bool reduce_range);
template at::Tensor PackedConvWeightsOnednn<3>::apply_dynamic(
const at::Tensor& input,
bool reduce_range);
#endif // AT_MKLDNN_ENABLED()
namespace at::native {
namespace {
// note: this works for both Conv and ConvT due to transpose()
template <int kSpatialDim>
class QConvDynamicInt8 final {
public:
static at::Tensor run(
at::Tensor input,
const c10::intrusive_ptr<ConvPackedParamsBase<kSpatialDim>>&
packed_weight,
bool reduce_range) {
return packed_weight->apply_dynamic(input, reduce_range);
}
};
// note: this works for both Conv and ConvT due to transpose()
class QConv1dDynamicInt8 final {
public:
static at::Tensor run(
at::Tensor input,
const c10::intrusive_ptr<ConvPackedParamsBase<2>>& packed_weight,
bool reduce_range) {
at::Tensor output;
// N, C, L -> N, C, 1, L
input = input.unsqueeze(quant_utils::kConv1dSqueezeDim + 2);
output = packed_weight->apply_dynamic(input, reduce_range);
// N, C, 1, L -> N, C, L
return output.squeeze_(quant_utils::kConv1dSqueezeDim + 2);
}
};
TORCH_LIBRARY_IMPL(quantized, CPU, m) {
m.impl(
TORCH_SELECTIVE_NAME("quantized::conv1d_dynamic"),
TORCH_FN(QConv1dDynamicInt8::run));
m.impl(
TORCH_SELECTIVE_NAME("quantized::conv2d_dynamic"),
TORCH_FN(QConvDynamicInt8<2>::run));
m.impl(
TORCH_SELECTIVE_NAME("quantized::conv3d_dynamic"),
TORCH_FN(QConvDynamicInt8<3>::run));
// transpose
m.impl(
TORCH_SELECTIVE_NAME("quantized::conv_transpose1d_dynamic"),
TORCH_FN(QConv1dDynamicInt8::run));
m.impl(
TORCH_SELECTIVE_NAME("quantized::conv_transpose2d_dynamic"),
TORCH_FN(QConvDynamicInt8<2>::run));
m.impl(
TORCH_SELECTIVE_NAME("quantized::conv_transpose3d_dynamic"),
TORCH_FN(QConvDynamicInt8<3>::run));
}
} // namespace
} // namespace at::native