-
Notifications
You must be signed in to change notification settings - Fork 24k
/
Copy pathfbgemm_utils.cpp
542 lines (502 loc) · 18.6 KB
/
fbgemm_utils.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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#define TORCH_ASSERT_ONLY_METHOD_OPERATORS
#include <ATen/Context.h>
#include <ATen/Dispatch.h>
#include <ATen/Utils.h>
#include <ATen/core/TensorBody.h>
#include <ATen/core/ivalue.h>
#include <ATen/core/jit_type_base.h>
#include <ATen/native/quantized/PackedParams.h>
#include <ATen/native/quantized/cpu/conv_serialization.h>
#include <ATen/native/quantized/cpu/EmbeddingPackedParams.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/TensorFactories.h>
#include <ATen/quantized/QTensorImpl.h>
#include <ATen/quantized/Quantizer.h>
#include <ATen/native/quantized/library.h>
#include <c10/core/QScheme.h>
#include <c10/core/TensorOptions.h>
#include <c10/util/accumulate.h>
#include <c10/util/irange.h>
#include <torch/custom_class.h>
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/Functions.h>
#else
#include <ATen/ops/cat.h>
#include <utility>
#endif
#ifdef USE_FBGEMM
namespace at::native::fbgemm_utils {
namespace {
bool IsChannelsLast3d(const Tensor& tensor) {
if (tensor.dim() != 5) {
return false;
}
const int64_t C = tensor.size(1);
const int64_t D = tensor.size(2);
const int64_t H = tensor.size(3);
const int64_t W = tensor.size(4);
return tensor.stride(0) == D * H * W * C && tensor.stride(1) == 1 &&
tensor.stride(2) == H * W * C && tensor.stride(3) == W * C &&
tensor.stride(4) == C;
}
template <typename T>
void CopyToChannelsLast3dTensor(
int64_t N,
int64_t C,
int64_t D,
int64_t H,
int64_t W,
const T* src,
T* dst) {
const int64_t inner_size = D * H * W;
for (const auto i : c10::irange(N)) {
for (const auto j : c10::irange(inner_size)) {
for (const auto k : c10::irange(C)) {
dst[(i * inner_size + j) * C + k] = src[(i * C + k) * inner_size + j];
}
}
}
}
template <typename T>
void CopyICFirst3dTensorToChannelsLast3dTensor(
int64_t G,
int64_t IC_G,
int64_t OC_G,
int64_t D,
int64_t H,
int64_t W,
const T* src,
T* dst) {
// IC OC/G THW -> G OC/G THW IC/G
const int64_t inner_size = D * H * W;
for (int64_t i = 0; i < G * OC_G; ++i) {
for (const auto j : c10::irange(inner_size)) {
for (const auto ic : c10::irange(IC_G)) {
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
int g = i / OC_G;
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
int oc = i % OC_G;
dst[(i * inner_size + j) * IC_G + ic] =
src[((g * IC_G + ic) * OC_G + oc) * inner_size + j];
}
}
}
}
} // namespace
template <int kSpatialDim>
fbgemm::conv_param_t<kSpatialDim> MakeFbgemmConvParam(
int N,
int C,
int M,
const std::vector<int>& image_shape,
int groups,
const std::vector<int>& kernels,
const std::vector<int>& strides,
const std::vector<int>& pads,
const std::vector<int>& dilations,
const std::vector<int>& output_padding,
bool transposed) {
std::array<int, kSpatialDim> image_shape_{};
std::array<int, kSpatialDim> kernels_{};
std::array<int, kSpatialDim> strides_{};
std::array<int, kSpatialDim * 2> pads_{};
std::array<int, kSpatialDim> dilations_{};
std::array<int, kSpatialDim> output_padding_{};
std::move(image_shape.begin(), image_shape.begin() + image_shape.size(), image_shape_.begin());
std::move(
kernels.begin(), kernels.begin() + kernels.size(), kernels_.begin());
std::move(
strides.begin(), strides.begin() + strides.size(), strides_.begin());
std::move(
dilations.begin(),
dilations.begin() + dilations.size(),
dilations_.begin());
std::move(
output_padding.begin(),
output_padding.begin() + output_padding.size(),
output_padding_.begin());
std::copy(pads.begin(), pads.begin() + pads.size(), pads_.begin());
std::move(pads.begin(), pads.begin() + pads.size(), pads_.begin() + pads.size());
return fbgemm::conv_param_t<kSpatialDim>(
N, // batch size
C, // input channels
M, // output channels
image_shape_, // feature map size
groups, // groups
kernels_, // kernels
strides_, // strides
pads_, // paddings
dilations_, // dilations
output_padding_, // output paddings for conv transpose
transposed);
}
Tensor MakeStridedQTensorCPU(
const IntArrayRef& sizes,
const IntArrayRef& strides,
const TensorOptions& options,
QuantizerPtr quantizer) {
AT_ASSERT(options.device().is_cpu());
at::native::check_size_nonnegative(sizes);
auto* allocator = at::getCPUAllocator();
const int64_t nelements = c10::multiply_integers(sizes);
auto dtype = options.dtype();
TORCH_CHECK(
isQIntType(typeMetaToScalarType(dtype)),
"ScalarType is not supported in new_qtensor_cpu.");
int64_t size_bytes = nelements * dtype.itemsize();
auto storage = c10::make_intrusive<StorageImpl>(
StorageImpl::use_byte_size_t(),
size_bytes,
allocator->allocate(size_bytes),
allocator,
/* resizable = */ true);
constexpr auto quantized_cpu_ks = at::DispatchKeySet(at::DispatchKey::QuantizedCPU);
auto tensor = detail::make_tensor<QTensorImpl>(
storage,
quantized_cpu_ks,
dtype,
quantizer);
get_qtensorimpl(tensor)->set_sizes_and_strides(sizes, strides);
return tensor;
}
Tensor MakeEmptyAffineQuantizedChannelsLast3dTensor(
int64_t N,
int64_t C,
int64_t D,
int64_t H,
int64_t W,
const TensorOptions& options,
double scale,
int64_t zero_point) {
return MakeStridedQTensorCPU(
{N, C, D, H, W},
{D * H * W * C, 1, H * W * C, W * C, C},
options,
make_per_tensor_affine_quantizer(
scale, zero_point, typeMetaToScalarType(options.dtype())));
}
Tensor MakeEmptyPerChannelAffineQuantizedChannelsLast3dTensor(
int64_t N,
int64_t C,
int64_t D,
int64_t H,
int64_t W,
const TensorOptions& options,
const Tensor& scales,
const Tensor& zero_points) {
return MakeStridedQTensorCPU(
{N, C, D, H, W},
{D * H * W * C, 1, H * W * C, W * C, C},
options,
make_per_channel_affine_quantizer(
scales,
zero_points,
0, // axis
typeMetaToScalarType(options.dtype())));
}
Tensor ConvertToChannelsLast3dTensor(const Tensor& src) {
TORCH_CHECK(src.dim() == 5);
Tensor dst;
if (IsChannelsLast3d(src)) {
dst = src;
} else {
const int64_t N = src.size(0);
const int64_t C = src.size(1);
const int64_t D = src.size(2);
const int64_t H = src.size(3);
const int64_t W = src.size(4);
dst = MakeStridedQTensorCPU(
{N, C, D, H, W},
{D * H * W * C, 1, H * W * C, W * C, C},
src.options(),
src.quantizer());
AT_DISPATCH_QINT_TYPES(
src.scalar_type(), "ConvertToChannelsLast3dTensor", [&]() {
const Tensor src_contig = src.contiguous();
CopyToChannelsLast3dTensor<scalar_t>(
N,
C,
D,
H,
W,
src_contig.data_ptr<scalar_t>(),
dst.data_ptr<scalar_t>());
});
}
return dst;
}
template <>
Tensor TransposeConvTensorUnpackConversion<2>(const Tensor& src, int groups) {
// OC IC/G HW -> IC OC/G HW logically
auto oc_g_ic_g_hw_tensors = src.chunk(groups);
auto fused_tensor = at::cat(oc_g_ic_g_hw_tensors, 1);
set_quantizer_(fused_tensor, src.quantizer());
return fused_tensor.permute({1, 0, 2, 3});
}
template fbgemm::conv_param_t<1> MakeFbgemmConvParam<1>(
int N,
int C,
int M,
const std::vector<int>& image_shape,
int groups,
const std::vector<int>& kernels,
const std::vector<int>& strides,
const std::vector<int>& pads,
const std::vector<int>& dilations,
const std::vector<int>& output_padding,
bool transposed);
template fbgemm::conv_param_t<2> MakeFbgemmConvParam<2>(
int N,
int C,
int M,
const std::vector<int>& image_shape,
int groups,
const std::vector<int>& kernels,
const std::vector<int>& strides,
const std::vector<int>& pads,
const std::vector<int>& dilations,
const std::vector<int>& output_padding,
bool transposed);
template fbgemm::conv_param_t<3> MakeFbgemmConvParam<3>(
int N,
int C,
int M,
const std::vector<int>& image_shape,
int groups,
const std::vector<int>& kernels,
const std::vector<int>& strides,
const std::vector<int>& pads,
const std::vector<int>& dilations,
const std::vector<int>& output_padding,
bool transposed);
template <>
Tensor TransposeConvTensorUnpackConversion<3>(const Tensor& src, int groups) {
// OC IC/G DHW -> IC OC/G DHW logically
auto oc_g_ic_g_hw_tensors = src.chunk(groups);
auto fused_tensor = at::cat(oc_g_ic_g_hw_tensors, 1);
set_quantizer_(fused_tensor, src.quantizer());
return fused_tensor.permute({1, 0, 2, 3, 4});
}
template <>
Tensor ConvertConvWeightsToChannelLastTensor<2>(
const at::Tensor& src,
int groups,
bool transpose) {
return transpose ?
// 2D conv transpose weight transform
// IC OC/G KH KW -> G OC/G KH KW IC/G
[&]() {
auto ic_g_oc_g_hw_tensors = src.chunk(groups);
for (auto& tensor : ic_g_oc_g_hw_tensors) {
tensor = tensor.unsqueeze(0);
}
auto fused_tensor = at::cat(ic_g_oc_g_hw_tensors);
set_quantizer_(fused_tensor, src.quantizer());
return fused_tensor.permute({0, 2, 3, 4, 1})
.contiguous(c10::MemoryFormat::Contiguous);
}()
// 2d conv weight transform
: src.contiguous(c10::MemoryFormat::ChannelsLast);
}
template <>
Tensor ConvertConvWeightsToChannelLastTensor<3>(
const at::Tensor& src,
int groups,
bool transpose) {
if (!transpose) {
return ConvertToChannelsLast3dTensor(src);
} else {
TORCH_CHECK(src.dim() == 5);
Tensor dst;
const int64_t N = src.size(0);
const int64_t IC_G = N / groups;
const int64_t OC_G = src.size(1);
const int64_t D = src.size(2);
const int64_t H = src.size(3);
const int64_t W = src.size(4);
dst = MakeStridedQTensorCPU(
{groups * OC_G, IC_G, D, H, W},
{D * H * W * IC_G, 1, H * W * IC_G, W * IC_G, IC_G},
src.options(),
src.quantizer());
AT_DISPATCH_QINT_TYPES(
src.scalar_type(), "CopyICFirst3dTensorToChannelsLast3dTensor", [&]() {
const Tensor src_contig = src.contiguous();
CopyICFirst3dTensorToChannelsLast3dTensor<scalar_t>(
groups,
IC_G,
OC_G,
D,
H,
W,
src_contig.data_ptr<scalar_t>(),
dst.data_ptr<scalar_t>());
});
return dst;
}
}
} // namespace at::native::fbgemm_utils
#endif // USE_FBGEMM
namespace {
// This is really terrible, but couldnt figure out a better way to constexpr convert int to
// string and then perform string concatenation on/with it
constexpr const char* _hack_int_to_class_name(int x) {
switch(x) {
case 2:
return "Conv2dPackedParamsBase";
case 3:
return "Conv3dPackedParamsBase";
default:
assert(false);
return "NotAValidDimension";
}
}
}
template <int kSpatialDim> int register_conv_params() {
[[maybe_unused]] static auto register_conv_params =
torch::selective_class_<ConvPackedParamsBase<kSpatialDim>>(
"quantized", TORCH_SELECTIVE_CLASS(_hack_int_to_class_name(kSpatialDim)))
.def_pickle(
[](const c10::intrusive_ptr<ConvPackedParamsBase<kSpatialDim>>& params)
-> ConvParamsSerializationType { // __getstate__
return serialize_conv<kSpatialDim>(params);
},
// __setstate__ takes c10::IValue because we support parsing historical
// serialization versions.
[](const c10::IValue& v)
-> c10::intrusive_ptr<ConvPackedParamsBase<kSpatialDim>> { // __setstate__
ConvParamsSerializationTypeV3 state = parse_conv_serialized_state<kSpatialDim>(v);
return deserialize_conv<kSpatialDim>(state);
})
.def("weight", [](const c10::intrusive_ptr<ConvPackedParamsBase<kSpatialDim>>& self) {
return std::get<0>(self->unpack());
})
.def("bias", [](const c10::intrusive_ptr<ConvPackedParamsBase<kSpatialDim>>& self) {
return std::get<1>(self->unpack());
})
.def("unpack", &ConvPackedParamsBase<kSpatialDim>::unpack)
.def("stride", &ConvPackedParamsBase<kSpatialDim>::stride)
.def("padding", &ConvPackedParamsBase<kSpatialDim>::padding)
.def("output_padding", &ConvPackedParamsBase<kSpatialDim>::output_padding)
.def("dilation", &ConvPackedParamsBase<kSpatialDim>::dilation)
.def("groups", &ConvPackedParamsBase<kSpatialDim>::groups)
.def("transpose", &ConvPackedParamsBase<kSpatialDim>::transpose);
return 0;
}
template
TORCH_API int register_conv_params<2>();
template
TORCH_API int register_conv_params<3>();
int register_linear_params() {
using SerializationType = std::tuple<at::Tensor, std::optional<at::Tensor>>;
[[maybe_unused]] static auto register_linear_params =
torch::selective_class_<LinearPackedParamsBase>(
"quantized", TORCH_SELECTIVE_CLASS("LinearPackedParamsBase"))
.def_pickle(
[](const c10::intrusive_ptr<LinearPackedParamsBase>& params)
-> SerializationType { // __getstate__
return params->unpack();
},
[](SerializationType state)
-> c10::intrusive_ptr<
LinearPackedParamsBase> { // __setstate__
#ifdef USE_FBGEMM
if (at::globalContext().qEngine() == at::QEngine::FBGEMM ||
at::globalContext().qEngine() == at::QEngine::X86) {
const auto& weight = std::get<0>(state);
if (weight.scalar_type() == at::kQInt8) {
return std::apply(PackedLinearWeight::prepack, std::move(state));
} else if (weight.scalar_type() == at::kFloat) {
// NB: fp16 weight is serialized as float
return std::apply(PackedLinearWeightFp16::prepack, std::move(state));
} else {
TORCH_CHECK(
false,
"Unsupported data type",
c10::toString(weight.scalar_type()),
" in serialized LinearPackedParams object!");
}
}
#endif // USE_FBGEMM
#ifdef USE_PYTORCH_QNNPACK
if (at::globalContext().qEngine() == at::QEngine::QNNPACK) {
const auto& weight = std::get<0>(state);
TORCH_CHECK(
weight.scalar_type() == at::kQInt8,
"QNNPACK only supports INT8 bit width currently. Got ",
c10::toString(weight.scalar_type()));
return std::apply(PackedLinearWeightsQnnp::prepack, std::move(state));
}
#endif // USE_PYTORCH_QNNPACK
#if AT_MKLDNN_ENABLED()
if (at::globalContext().qEngine() == at::QEngine::ONEDNN) {
const auto& weight = std::get<0>(state);
TORCH_CHECK(
weight.scalar_type() == at::kQInt8,
"ONEDNN only supports INT8 bit width currently. Got ",
c10::toString(weight.scalar_type()));
return std::apply(PackedLinearWeightsOnednn::prepack, std::move(state));
}
#endif // #if AT_MKLDNN_ENABLED()
TORCH_CHECK(false, "Unknown qengine");
})
.def("bias", [](const c10::intrusive_ptr<LinearPackedParamsBase>& self) {
return std::get<1>(self->unpack());
})
.def("unpack", &LinearPackedParamsBase::unpack);
// (1) we can't (easily) return the static initializer itself because it can have a different type because of selective build
// (2) we can't return void and be able to call the function in the global scope
return 0;
}
int register_embedding_params() {
// Type for __getstate__/__setstate__ serialization
//
// Element 0 is the version of the PackedParam structure
// Element 1 is the Tensors contained in the Param instance
// Element 2 is the double values (if any) contained in the Param instance
// Element 3 is the int values (if any) contained in the Param instance
using EmbeddingParamsSerializationType = std::tuple<
int64_t, // version
std::vector<at::Tensor>,
std::vector<double>,
std::vector<int64_t>>;
[[maybe_unused]] static auto register_embedding_params =
torch::selective_class_<EmbeddingPackedParamsBase>(
"quantized", TORCH_SELECTIVE_CLASS("EmbeddingPackedParamsBase"))
.def_pickle(
[](const c10::intrusive_ptr<EmbeddingPackedParamsBase>& params)
-> EmbeddingParamsSerializationType { // __getstate__ call
at::Tensor weight = params->unpack();
std::vector<at::Tensor> tensors_to_serialize = {std::move(weight)};
std::vector<double> doubles_to_serialize = {};
int64_t bit_rate = params->bit_rate();
int64_t version = params->version();
std::vector<int64_t> longs_to_serialize = {bit_rate};
return EmbeddingParamsSerializationType(
version,
std::move(tensors_to_serialize),
std::move(doubles_to_serialize),
std::move(longs_to_serialize));
},
[](EmbeddingParamsSerializationType state)
-> c10::intrusive_ptr<EmbeddingPackedParamsBase> { // __setstate__ call
auto [version, tensors, doubles, longs] = std::move(state);
TORCH_INTERNAL_ASSERT(tensors.size() == 1, "EmbeddingPackedParams: Expected weight tensor to be serialized");
TORCH_INTERNAL_ASSERT(longs.size() == 1, "EmbeddingPackedParams: Expected bit_rate to be serialized");
TORCH_CHECK(version == 1, "EmbeddingPackedParams: Currently only version 1 supported.");
at::Tensor weight = std::move(tensors[0]);
return PackedEmbeddingBagWeight::prepack(std::move(weight));
})
.def("bit_rate", &EmbeddingPackedParamsBase::bit_rate)
.def("unpack", &EmbeddingPackedParamsBase::unpack)
.def("version", &EmbeddingPackedParamsBase::version);
return 0;
}
namespace {
[[maybe_unused]] static auto conv2d_params = register_conv_params<2>();
[[maybe_unused]] static auto conv3d_params = register_conv_params<3>();
[[maybe_unused]] static auto linear_params = register_linear_params();
[[maybe_unused]] static auto embedding_params = register_embedding_params();
} // namespace