forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qlinear_unpack.cpp
79 lines (67 loc) · 2.8 KB
/
qlinear_unpack.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
/*
The dispatch registrations at the end of this file applies to fbgemm, qnnpack, and cudnn backends.
The correct unpack backend function is determined using runtime polymorphism through the packed_weight pointer,
which is of type intrusive_ptr<LinearPackedParamsBase> and points to either a PackedLinearWeightsQnnp,
PackedLinearWeights (Fbgemm), or PackedLinearWeightsCudnn at runtime, which all inherit from LinearPackedParamsBase.
The implementations for the unpack functions can be found in /cpu/LinearUnpackImpl.cpp, for fbgemm&qnnpack
and /cudnn/linear_unpack_impl.cpp, for cudnn.
*/
#include <ATen/ATen.h>
#include <ATen/native/quantized/cpu/fbgemm_utils.h>
#include <ATen/native/quantized/PackedParams.h>
#include <ATen/native/quantized/cpu/QnnpackUtils.h>
#include <torch/custom_class.h>
#include <torch/library.h>
int register_linear_params();
namespace at::native {
namespace {
class QLinearUnpackWeightInt8 final {
public:
static std::tuple<at::Tensor, std::optional<Tensor>> run(
const c10::intrusive_ptr<LinearPackedParamsBase>& packed_weight) {
return packed_weight->unpack();
}
};
class QLinearUnpackWeightFp16 final {
public:
static std::tuple<at::Tensor, std::optional<Tensor>> run(
const c10::intrusive_ptr<LinearPackedParamsBase>& packed_weight) {
auto& ctx = at::globalContext();
TORCH_CHECK(
ctx.qEngine() != at::QEngine::QNNPACK,
"quantized::linear_unpack_fp16 is currently "
"not supported by QNNPACK");
return packed_weight->unpack();
}
};
class QLinearUnpackWeightInt8Legacy final {
public:
static std::tuple<at::Tensor, std::optional<Tensor>> run(
const at::Tensor& packed_weight) {
TORCH_CHECK(false,
"quantized.linear_unpack(Tensor) is unsupported! Please "
"upgrade your model to use the newer quantized.linear_"
"unpack(LinearPackedParamsBase) overload");
}
};
class QLinearUnpackWeightFp16Legacy final {
public:
static std::tuple<at::Tensor, std::optional<Tensor>> run(
const at::Tensor& packed_weight) {
TORCH_CHECK(false,
"quantized.linear_unpack(Tensor) is unsupported! Please "
"upgrade your model to use the newer quantized.linear_"
"unpack(LinearPackedParamsBase) overload");
}
};
TORCH_LIBRARY_IMPL(quantized, CPU, m) {
m.impl(TORCH_SELECTIVE_NAME("quantized::linear_unpack.legacy"), TORCH_FN(QLinearUnpackWeightInt8Legacy::run));
m.impl(TORCH_SELECTIVE_NAME("quantized::linear_unpack_fp16.legacy"), TORCH_FN(QLinearUnpackWeightFp16Legacy::run));
}
TORCH_LIBRARY_IMPL(quantized, CatchAll, m) {
register_linear_params();
m.impl(TORCH_SELECTIVE_NAME("quantized::linear_unpack"), TORCH_FN(QLinearUnpackWeightInt8::run));
m.impl(TORCH_SELECTIVE_NAME("quantized::linear_unpack_fp16"), TORCH_FN(QLinearUnpackWeightFp16::run));
}
} // namespace
} // namespace at::native