Skip to content

Commit 81de083

Browse files
authored
Fused quant mul kernel (pytorch#19455)
Differential Revision: D103754685 Pull Request resolved: pytorch#19455
1 parent acfa561 commit 81de083

5 files changed

Lines changed: 712 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#include <executorch/backends/cadence/fused_quant/op_mul.h>
10+
#include <executorch/backends/cadence/fused_quant/quant_utils.h>
11+
#include <executorch/runtime/kernel/kernel_includes.h>
12+
13+
namespace cadence {
14+
namespace fused_quant {
15+
namespace native {
16+
17+
using executorch::aten::optional;
18+
using executorch::aten::ScalarType;
19+
using executorch::aten::Tensor;
20+
using executorch::runtime::KernelRuntimeContext;
21+
22+
namespace {
23+
24+
void mul_kernel(
25+
const float* inp,
26+
const float* other,
27+
float* out,
28+
int64_t numel) {
29+
for (int64_t i = 0; i < numel; ++i) {
30+
out[i] = inp[i] * other[i];
31+
}
32+
}
33+
34+
} // namespace
35+
36+
Tensor& mul_out(
37+
KernelRuntimeContext& ctx,
38+
const Tensor& inp,
39+
const Tensor& other,
40+
const optional<Tensor>& inp_scale,
41+
const optional<Tensor>& inp_zero_point,
42+
ScalarType inp_dtype,
43+
int64_t inp_quant_min,
44+
int64_t inp_quant_max,
45+
optional<int64_t> inp_axis,
46+
const optional<Tensor>& other_scale,
47+
const optional<Tensor>& other_zero_point,
48+
ScalarType other_dtype,
49+
int64_t other_quant_min,
50+
int64_t other_quant_max,
51+
optional<int64_t> other_axis,
52+
const optional<Tensor>& out_scale,
53+
const optional<Tensor>& out_zero_point,
54+
ScalarType out_dtype,
55+
int64_t out_quant_min,
56+
int64_t out_quant_max,
57+
optional<int64_t> out_axis,
58+
Tensor& out) {
59+
(void)ctx;
60+
(void)inp_dtype;
61+
(void)other_dtype;
62+
(void)out_dtype;
63+
64+
int64_t numel = inp.numel();
65+
66+
bool inp_quantized = inp_scale.has_value();
67+
bool other_quantized = other_scale.has_value();
68+
bool out_quantized = out_scale.has_value();
69+
70+
std::vector<float> inp_buf;
71+
const float* const inp_float = [&]() -> const float* {
72+
if (!inp_quantized) {
73+
return inp.const_data_ptr<float>();
74+
}
75+
inp_buf.resize(numel);
76+
QParams qp = extract_qparams(
77+
inp_scale, inp_zero_point, inp_quant_min, inp_quant_max, inp_axis, inp);
78+
FUSED_QUANT_DTYPE_SWITCH(
79+
inp.scalar_type(),
80+
scalar_t,
81+
dequantize_buffer(
82+
inp.const_data_ptr<scalar_t>(), inp_buf.data(), numel, qp);)
83+
return inp_buf.data();
84+
}();
85+
86+
std::vector<float> other_buf;
87+
const float* const other_float = [&]() -> const float* {
88+
if (!other_quantized) {
89+
return other.const_data_ptr<float>();
90+
}
91+
other_buf.resize(numel);
92+
QParams qp = extract_qparams(
93+
other_scale,
94+
other_zero_point,
95+
other_quant_min,
96+
other_quant_max,
97+
other_axis,
98+
other);
99+
FUSED_QUANT_DTYPE_SWITCH(
100+
other.scalar_type(),
101+
scalar_t,
102+
dequantize_buffer(
103+
other.const_data_ptr<scalar_t>(), other_buf.data(), numel, qp);)
104+
return other_buf.data();
105+
}();
106+
107+
if (out_quantized) {
108+
std::vector<float> result_float(numel);
109+
mul_kernel(inp_float, other_float, result_float.data(), numel);
110+
111+
QParams qp = extract_qparams(
112+
out_scale, out_zero_point, out_quant_min, out_quant_max, out_axis, out);
113+
FUSED_QUANT_DTYPE_SWITCH(
114+
out.scalar_type(),
115+
scalar_t,
116+
quantize_buffer(
117+
result_float.data(), out.mutable_data_ptr<scalar_t>(), numel, qp);)
118+
} else {
119+
mul_kernel(inp_float, other_float, out.mutable_data_ptr<float>(), numel);
120+
}
121+
122+
return out;
123+
}
124+
125+
} // namespace native
126+
} // namespace fused_quant
127+
} // namespace cadence
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#pragma once
10+
11+
#include <executorch/runtime/core/exec_aten/exec_aten.h>
12+
#include <executorch/runtime/kernel/kernel_includes.h>
13+
14+
namespace cadence {
15+
namespace fused_quant {
16+
namespace native {
17+
18+
executorch::aten::Tensor& mul_out(
19+
executorch::runtime::KernelRuntimeContext& ctx,
20+
const executorch::aten::Tensor& inp,
21+
const executorch::aten::Tensor& other,
22+
const executorch::aten::optional<executorch::aten::Tensor>& inp_scale,
23+
const executorch::aten::optional<executorch::aten::Tensor>& inp_zero_point,
24+
executorch::aten::ScalarType inp_dtype,
25+
int64_t inp_quant_min,
26+
int64_t inp_quant_max,
27+
executorch::aten::optional<int64_t> inp_axis,
28+
const executorch::aten::optional<executorch::aten::Tensor>& other_scale,
29+
const executorch::aten::optional<executorch::aten::Tensor>&
30+
other_zero_point,
31+
executorch::aten::ScalarType other_dtype,
32+
int64_t other_quant_min,
33+
int64_t other_quant_max,
34+
executorch::aten::optional<int64_t> other_axis,
35+
const executorch::aten::optional<executorch::aten::Tensor>& out_scale,
36+
const executorch::aten::optional<executorch::aten::Tensor>& out_zero_point,
37+
executorch::aten::ScalarType out_dtype,
38+
int64_t out_quant_min,
39+
int64_t out_quant_max,
40+
executorch::aten::optional<int64_t> out_axis,
41+
executorch::aten::Tensor& out);
42+
43+
} // namespace native
44+
} // namespace fused_quant
45+
} // namespace cadence

‎backends/cadence/fused_quant/targets.bzl‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,15 @@ def define_common_targets():
2222
],
2323
visibility = ["PUBLIC"],
2424
)
25+
26+
runtime.cxx_library(
27+
name = "op_mul",
28+
srcs = ["op_mul.cpp"],
29+
exported_headers = ["op_mul.h"],
30+
platforms = CXX,
31+
deps = [
32+
":quant_utils",
33+
"//executorch/runtime/kernel:kernel_includes",
34+
],
35+
visibility = ["PUBLIC"],
36+
)

‎backends/cadence/fused_quant/tests/BUCK‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@ runtime.cxx_test(
1313
"//executorch/runtime/core/exec_aten/testing_util:tensor_util",
1414
],
1515
)
16+
17+
runtime.cxx_test(
18+
name = "test_op_mul",
19+
srcs = ["test_op_mul.cpp"],
20+
platforms = CXX,
21+
deps = [
22+
"//executorch/backends/cadence/fused_quant:op_mul",
23+
"//executorch/kernels/test:gtest_utils",
24+
"//executorch/runtime/core/exec_aten/testing_util:tensor_util",
25+
],
26+
)

0 commit comments

Comments
 (0)