Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inductor cpp wrapper: support QLinear #112378

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aten/src/ATen/native/quantized/cpu/OnednnUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,13 @@ namespace onednn_utils {

static ideep::attr_t create_attr_by_post_op(
const std::string& post_op_name,
const torch::List<double>& post_op_args) {
const torch::List<c10::optional<at::Scalar>>& post_op_args) {
using ideep::tensor;
PostOps post_op = POST_OP_TABLE[post_op_name];
if (post_op == Relu) {
return ideep::attr_t::fuse_relu();
} else if (post_op == LeakyRelu) {
return ideep::attr_t::fuse_relu_v2(/*alpha=*/post_op_args[0]);
return ideep::attr_t::fuse_relu_v2(/*alpha=*/post_op_args[0].value().to<float>());
} else if (post_op == Tanh) {
return ideep::attr_t::fuse_tanh();
}
Expand Down
4 changes: 2 additions & 2 deletions aten/src/ATen/native/quantized/cpu/qlinear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ static at::Tensor linear_int8_with_onednn_weight(
int64_t output_zero_point,
bool fp32_output,
std::string& post_op_name, // e.g. "none", "relu"
torch::List<double>& post_op_args,
torch::List<c10::optional<at::Scalar>>& post_op_args,
std::string& post_op_algorithm) {
using ideep::tensor;
const int64_t dim = input.dim();
Expand Down Expand Up @@ -1119,7 +1119,7 @@ class QLinearOnednn final {
int64_t output_zero_point,
bool fp32_output,
std::string post_op_name,
torch::List<double> post_op_args,
torch::List<c10::optional<at::Scalar>> post_op_args,
std::string post_op_algorithm) {
#if AT_MKLDNN_ENABLED()
return linear_int8_with_onednn_weight(
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/native/quantized/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,6 @@ TORCH_LIBRARY(onednn, m) {
m.def(TORCH_SELECTIVE_SCHEMA("onednn::qlinear_prepack(Tensor weight, int[]? x_shape) -> Tensor"));

// Linear with unary postop
m.def(TORCH_SELECTIVE_SCHEMA("onednn::qlinear_pointwise(Tensor qx, float x_scale, int x_zero_point, Tensor qw, Tensor w_scale, Tensor w_zero_point, Tensor? bias, float output_scale, int output_zero_point, bool fp32_output, str post_op_name, float[] post_op_args, str post_op_algorithm) -> Tensor"));
m.def(TORCH_SELECTIVE_SCHEMA("onednn::qlinear_pointwise(Tensor qx, float x_scale, int x_zero_point, Tensor qw, Tensor w_scale, Tensor w_zero_point, Tensor? bias, float output_scale, int output_zero_point, bool fp32_output, str post_op_name, Scalar?[] post_op_args, str post_op_algorithm) -> Tensor"));

}
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@
("aten::batch_norm_backward_elemt.out", datetime.date(2023, 12, 31)),
("aten::batch_norm_backward_elemt", datetime.date(2023, 12, 31)),
("aten::sym_constrain_range", datetime.date(2023, 12, 31)),
("onednn::qlinear_pointwise", datetime.date(2023, 12, 31)),
]

ALLOW_LIST_COMPILED = [
Expand Down
18 changes: 18 additions & 0 deletions test/inductor/test_cpp_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,24 @@ class BaseTest(NamedTuple):
test_mkldnn_pattern_matcher.TestPatternMatcher(),
condition=torch.backends.mkldnn.is_available(),
),
BaseTest(
"test_qlinear",
"cpu",
test_mkldnn_pattern_matcher.TestPatternMatcher(),
condition=torch.backends.mkldnn.is_available(),
),
BaseTest(
"test_qlinear_relu",
"cpu",
test_mkldnn_pattern_matcher.TestPatternMatcher(),
condition=torch.backends.mkldnn.is_available(),
),
BaseTest(
"test_qlinear_dequant_promotion",
"cpu",
test_mkldnn_pattern_matcher.TestPatternMatcher(),
condition=torch.backends.mkldnn.is_available(),
),
BaseTest("test_randint"),
BaseTest("test_randn_with_dtype_and_device"),
BaseTest("test_reduction1"), # Reduction
Expand Down
6 changes: 3 additions & 3 deletions test/inductor/test_mkldnn_pattern_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ def forward(self, x):
@skipIfNoDynamoSupport
@skipIfNoONEDNN
@skipIfRocm
def test_qlinear(self):
def test_qlinear_cpu(self):
r"""
This testcase will quantize a single Linear Moduel.
"""
Expand Down Expand Up @@ -731,7 +731,7 @@ def forward(self, x):
@skipIfNoDynamoSupport
@skipIfNoONEDNN
@skipIfRocm
def test_qlinear_relu(self):
def test_qlinear_relu_cpu(self):
r"""
This testcase will quantize a Linear->ReLU pattern.
"""
Expand Down Expand Up @@ -771,7 +771,7 @@ def forward(self, x):
@skipIfNoDynamoSupport
@skipIfNoONEDNN
@skipIfRocm
def test_qlinear_dequant_promotion(self):
def test_qlinear_dequant_promotion_cpu(self):
r"""
This testcase test if dequant node before linear is promoted correctly:
X
Expand Down
64 changes: 46 additions & 18 deletions torch/_inductor/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -5761,7 +5761,30 @@ def __init__(
fp32_output, unary_attr, unary_scalars, unary_algorithm]
"""
self.has_bias = len(inputs) == 5
super().__init__(layout, inputs, constant_args)
super().__init__(
layout,
inputs,
constant_args,
None,
kernel="torch.ops.onednn.qlinear_pointwise",
cpp_kernel="onednn::qlinear_pointwise",
)
self.cpp_kernel_key = "qlinear_pointwise"
self.cpp_op_schema = """
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same comment as in #112373

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as for #112373, let me submit a subsequent PR to clean this up. Created an issue to track it: #112552

at::Tensor(
at::Tensor act,
double act_scale,
int64_t act_zero_point,
at::Tensor weight,
at::Tensor weight_scales,
at::Tensor weight_zero_points,
c10::optional<at::Tensor> bias,
double inv_output_scale,
int64_t output_zero_point,
bool fp32_output,
std::string post_op_name,
torch::List<c10::optional<at::Scalar>> post_op_args,
std::string post_op_algorithm)"""

def codegen(self, wrapper):
# Parser the inputs and constant
Expand All @@ -5784,23 +5807,28 @@ def codegen(self, wrapper):
unary_algorithm,
) = const_args[-8:]

self.kernel = "torch.ops.onednn.qlinear_pointwise"
codegen_args = (
f"{x}"
+ f", {x_scale}"
+ f", {x_zp}"
+ f", {packed_weight}"
+ f", {w_scale}"
+ f", {w_zp}"
+ f", {bias}"
+ f", {o_inv_scale}"
+ f", {o_zp}"
+ f", {fp32_output}"
+ f", {unary_attr}"
+ f", {unary_scalars}"
+ f", {unary_algorithm}"
)
wrapper.writeline(f"{self.get_name()} = {self.kernel}({codegen_args})")
x,
x_scale,
x_zp,
packed_weight,
w_scale,
w_zp,
bias,
o_inv_scale,
o_zp,
fp32_output,
unary_attr,
unary_scalars,
unary_algorithm,
)
wrapper.generate_extern_kernel_alloc_and_find_schema_if_needed(
self.get_name(),
self.kernel,
codegen_args,
self.cpp_op_schema,
self.cpp_kernel_key,
)
if isinstance(self.layout, Layout):
self.codegen_size_asserts(wrapper)

Expand Down Expand Up @@ -5838,7 +5866,7 @@ def create(
output_zero_point,
fp32_output,
unary_attr,
unary_scalars,
may_convert_to_optional(unary_scalars),
unary_algorithm,
]

Expand Down
Loading