You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2021/12/02 11:57:20 UTC
[GitHub] [tvm] Icemist opened a new pull request #9637: Add FP32 requantize flow for llvm target
Icemist opened a new pull request #9637:
URL: https://github.com/apache/tvm/pull/9637
Below are some results of comparing the old implementation(int32) with the suggested(fp32):
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 12">
<link id=Main-File rel=Main-File
href="file:///C:\Users\icemist\AppData\Local\Temp\msohtmlclip1\01\clip.htm">
<link rel=File-List
href="file:///C:\Users\icemist\AppData\Local\Temp\msohtmlclip1\01\clip_filelist.xml">
<!--table
{mso-displayed-decimal-separator:"\.";
mso-displayed-thousand-separator:"\,";}
@page
{margin:.75in .7in .75in .7in;
mso-header-margin:.3in;
mso-footer-margin:.3in;}
tr
{mso-height-source:auto;}
col
{mso-width-source:auto;}
br
{mso-data-placement:same-cell;}
td
{padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:black;
font-size:11.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:Calibri, sans-serif;
mso-font-charset:204;
mso-number-format:General;
text-align:general;
vertical-align:bottom;
border:none;
mso-background-source:auto;
mso-pattern:auto;
mso-protection:locked visible;
white-space:nowrap;
mso-rotate:0;}
.xl65
{mso-number-format:Percent;}
-->
</head>
<body link=blue vlink=purple>
resnet50_INT8bit_quantized 50000 images Top1 accuracy | llvm | llvm avx2
-- | -- | --
int32 TONEAREST | 75.38% | 75.39%
int32 UPWARD | 75.38% | 75.39%
float32 TONEAREST | 75.30% | 75.32%
float32 UPWARD | 75.30% | 75.32%
</body>
</html>
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 12">
<link id=Main-File rel=Main-File
href="file:///C:\Users\icemist\AppData\Local\Temp\msohtmlclip1\01\clip.htm">
<link rel=File-List
href="file:///C:\Users\icemist\AppData\Local\Temp\msohtmlclip1\01\clip_filelist.xml">
<!--table
{mso-displayed-decimal-separator:"\.";
mso-displayed-thousand-separator:"\,";}
@page
{margin:.75in .7in .75in .7in;
mso-header-margin:.3in;
mso-footer-margin:.3in;}
tr
{mso-height-source:auto;}
col
{mso-width-source:auto;}
br
{mso-data-placement:same-cell;}
td
{padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:black;
font-size:11.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:Calibri, sans-serif;
mso-font-charset:204;
mso-number-format:General;
text-align:general;
vertical-align:bottom;
border:none;
mso-background-source:auto;
mso-pattern:auto;
mso-protection:locked visible;
white-space:nowrap;
mso-rotate:0;}
.xl65
{mso-number-format:"0\.000";}
-->
</head>
<body link=blue vlink=purple>
relay.qnn.op.requantize median (ms) | llvm | llvm avx2
-- | -- | --
int32 TONEAREST | 2.203 | 1.510
int32 UPWARD | 1.490 | 1.020
float32 TONEAREST | 1.289 | 0.675
float32 UPWARD | 1.140 | 0.666
</body>
</html>
The performance measurements were made via graph_executor.benchmark on AMD Ryzen 7 5800H with TVM_NUM_THREADS=1.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r790334752
##########
File path: include/tvm/relay/qnn/attrs.h
##########
@@ -36,6 +36,7 @@ namespace qnn {
struct RequantizeAttrs : public tvm::AttrsNode<RequantizeAttrs> {
int axis;
std::string rounding;
+ std::string calculation_flow_type;
Review comment:
I prefer `compute_dtype`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] Icemist commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
Icemist commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r791640284
##########
File path: python/tvm/topi/x86/utils.py
##########
@@ -16,8 +16,87 @@
# under the License.
"""Common x86 related utilities"""
import tvm
+import tvm._ffi
+@tvm._ffi.register_func("tvm.topi.x86.utils.target_is_x86")
+def target_is_x86(target):
+ return (
+ target_has_sse41(target)
+ or target_has_sse42(target)
+ or target_has_avx(target)
+ or target_has_avx2(target)
+ or target_has_avx512(target)
+ or target_has_vnni(target)
+ or target
+ in {
+ "k6-2",
+ "x86-64",
+ "atom",
+ "pentium",
+ "nocona",
+ "k8",
+ "i686",
+ "winchip-c6",
+ "prescott",
+ "k6",
+ "athlon-mp",
+ "winchip2",
+ "yonah",
+ "athlon-tbird",
+ "alderlake",
+ "pentium4",
+ "athlon64-sse3",
+ "pentium2",
+ "bonnell",
+ "k8-sse3",
+ "barcelona",
+ "athlon",
+ "c3",
+ "generic",
+ "lakemont",
+ "pentium-mmx",
+ "core2",
+ "pentium3",
+ "btver1",
+ "athlon-fx",
+ "k6-3",
+ "geode",
+ "pentium-m",
+ "i586",
+ "pentium3m",
+ "pentiumpro",
+ "i486",
+ "opteron",
+ "athlon64",
+ "pentium4m",
+ "i386",
+ "opteron-sse3",
+ "amdfam10",
+ "athlon-4",
+ "athlon-xp",
+ "c3-2",
Review comment:
I agree, sse4.1 looks good. Users can always use requantize_config to change the default behavior.
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r791265798
##########
File path: include/tvm/relay/qnn/attrs.h
##########
@@ -54,6 +55,11 @@ struct RequantizeAttrs : public tvm::AttrsNode<RequantizeAttrs> {
"value is rounded away from zero at midpoints (for example, -1.5"
"rounds to -2). More context can be found at following gblic manual"
"https://www.gnu.org/software/libc/manual/html_node/Rounding.html.");
+ TVM_ATTR_FIELD(compute_dtype)
+ .set_default("None")
+ .describe(
+ "Specifies the algorithm of requantize calculating. Supported "
Review comment:
`Specifies the data type used during requantize.`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on a change in pull request #9637: Add FP requantize flow for llvm target
Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r771999877
##########
File path: src/relay/qnn/op/requantize.cc
##########
@@ -208,6 +310,127 @@ Expr RequantizeLower(const Expr& input_tensor, const Expr& input_scale,
return Cast(clipped_t, out_dtype);
}
+// Lowering of qnn.requantize op
+
+/*
+ * \brief Lower requantize to a sequence of ops.
+ * \param input_tensor The input tensor to requantize op.
+ * \param param The requantize op attrs.
+ * \param input_shape The input tensor shape of the requantize op.
+ * \return The sequence of existing Relay ops.
+ * \note RequantizationFP using floating computation. All multiplication/sub/sum
+ * occurs in floating point data type and only at the end is converted to
+ * int32 data type and clamped for output data type.
+ *
+ * The whole computation this can be broken down into following steps
+ * 1) Subtract the input zero point.
+ * 2) Perform multiplication.
+ * 3) Add the output zero point.
+ * 4) Cast to the out_dtype.
+ */
+Expr RequantizeLowerFP(const Expr& input_tensor, const Expr& input_scale,
+ const Expr& input_zero_point, const Expr& output_scale,
+ const Expr& output_zero_point, const RequantizeAttrs* param,
+ const Array<IndexExpr>& input_shape, const DataType& out_dtype) {
+ auto tensor = Cast(input_tensor, DataType::Float(64));
+ auto zero_scalar = MakeConstantScalar(DataType::Int(32), 0);
+ if (!IsEqualScalar(input_zero_point, zero_scalar)) {
+ // Broadcast input zero point if needed.
+ int rank = static_cast<int>(input_shape.size());
+ int axis = (param->axis < 0) ? ((rank > 0) ? rank + param->axis : 0) : param->axis;
+ Expr input_zero_broadcast = ExpandBiasToMatchAxis(Reshape(input_zero_point,
+ {
+ -1,
+ }),
+ rank, {axis});
+ tensor = Subtract(tensor, Cast(input_zero_broadcast, DataType::Float(64)));
+ }
+
+ // 2) If the input and output scales are same, we can skip the multiplication. Check
+ // if the input scale is per-tensor or per-channel. If it is per-tensor, there is single scale for
+ // the whole tensor. For per-channel (aka per-axis), there is a vector of scales for the input
+ // tensor. Depending on the quantization type, the fixed point multiplication routing is called.
+ auto scaled_fp64_t = tensor;
+ double output_scale_float = GetScalarFromConstant<float>(output_scale);
+ if (IsConstScalar(input_scale)) {
+ // This is per-tensor quantization. Single scale.
+ double input_scale_float = GetScalarFromConstant<float>(input_scale);
+ double double_multiplier = input_scale_float / output_scale_float;
+ // Skip if input and output scales are same.
+ if (!IsEqualScalar(input_scale, output_scale)) {
+ double multiplier = double_multiplier;
+ auto m_scalar = MakeConstantScalar(DataType::Float(64), multiplier);
+ scaled_fp64_t = Multiply(m_scalar, scaled_fp64_t);
+ }
+
+ } else {
+ // This is per-channel (per=axis) quantization.
+ std::vector<double> double_multipliers;
+ auto input_axis_scales = GetFloatVectorFromConstant(input_scale);
+ double output_scale_float = GetScalarFromConstant<float>(output_scale);
+ for (auto input_axis_scale : input_axis_scales) {
+ double multiplier = static_cast<double>(input_axis_scale) / output_scale_float;
+ double_multipliers.push_back(multiplier);
+ }
+ int axis = param->axis;
+ axis = (axis == -1) ? input_shape.size() - 1 : axis;
+
+ auto fixed_pt_multiplier_expr = MakeConstantTensor(
+ DataType::Float(64), {(int64_t)double_multipliers.size()}, double_multipliers);
+ size_t n_dim = input_shape.size();
+ auto exp_fixed_pt_multiplier_expr =
+ ExpandBiasToMatchAxis(fixed_pt_multiplier_expr, n_dim, {axis});
+
+ scaled_fp64_t = Multiply(scaled_fp64_t, exp_fixed_pt_multiplier_expr);
+ }
+
+ // 3) Add the output zero point.
+ auto shifted_fp64_t = scaled_fp64_t;
+ if (!IsEqualScalar(output_zero_point, zero_scalar)) {
+ shifted_fp64_t = Add(shifted_fp64_t, Cast(output_zero_point, DataType::Float(64)));
+ }
+
+ if (param->rounding == "UPWARD") {
+ shifted_fp64_t = Upward(shifted_fp64_t);
+ } else /*if (param->rounding == "TONEAREST")*/ {
+ shifted_fp64_t = Tonearest(shifted_fp64_t);
+ }
+
+ shifted_fp64_t = Cast(shifted_fp64_t, DataType::Int(32));
+ // 4) Clip to the out_dtype min/max. Skip clipping if out_dtype is Int32. The fixed point
+ // multiplication keeps the value in int32 range.
+ if (out_dtype == DataType::Int(32)) {
+ return shifted_fp64_t;
+ }
+
+ auto q_min = GetQmin(out_dtype);
+ auto q_max = GetQmax(out_dtype);
+ auto clipped_t = Clip(shifted_fp64_t, q_min, q_max);
+ return Cast(clipped_t, out_dtype);
+}
+
+// Lowering of qnn.requantize op
+/*
+ * \brief Lower requantize to a sequence of ops.
+ * \param input_tensor The input tensor to requantize op.
+ * \param param The requantize op attrs.
+ * \param input_shape The input tensor shape of the requantize op.
+ * \return The sequence of existing Relay ops.
+ */
+Expr RequantizeLower(const Expr& input_tensor, const Expr& input_scale,
+ const Expr& input_zero_point, const Expr& output_scale,
+ const Expr& output_zero_point, const RequantizeAttrs* param,
+ const Array<IndexExpr>& input_shape, const DataType& out_dtype) {
+ auto target = Target::Current(true);
+ if (target.defined() && target->kind->name == "llvm") {
Review comment:
Check it is x86
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on pull request #9637: Add FP requantize flow for llvm target
Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#issuecomment-997459968
Have you benchmarked on ARM? I think we should enable this only for x86 for now.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] jwfromm commented on a change in pull request #9637: Add FP requantize flow for llvm target
Posted by GitBox <gi...@apache.org>.
jwfromm commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r774731091
##########
File path: python/tvm/relay/qnn/op/qnn.py
##########
@@ -92,6 +92,44 @@ def requantize(
)
+def upward(data):
Review comment:
Would it make sense to add these as an attribute to `relay.round`? For example we could do `relay.round(x, mode=upward)`. I think that would be a cleaner interface unless there's a reason for having them as separate operators.
##########
File path: src/relay/qnn/op/requantize.cc
##########
@@ -208,6 +310,127 @@ Expr RequantizeLower(const Expr& input_tensor, const Expr& input_scale,
return Cast(clipped_t, out_dtype);
}
+// Lowering of qnn.requantize op
+
+/*
+ * \brief Lower requantize to a sequence of ops.
+ * \param input_tensor The input tensor to requantize op.
+ * \param param The requantize op attrs.
+ * \param input_shape The input tensor shape of the requantize op.
+ * \return The sequence of existing Relay ops.
+ * \note RequantizationFP using floating computation. All multiplication/sub/sum
+ * occurs in floating point data type and only at the end is converted to
+ * int32 data type and clamped for output data type.
+ *
+ * The whole computation this can be broken down into following steps
+ * 1) Subtract the input zero point.
+ * 2) Perform multiplication.
+ * 3) Add the output zero point.
+ * 4) Cast to the out_dtype.
+ */
+Expr RequantizeLowerFP(const Expr& input_tensor, const Expr& input_scale,
+ const Expr& input_zero_point, const Expr& output_scale,
+ const Expr& output_zero_point, const RequantizeAttrs* param,
+ const Array<IndexExpr>& input_shape, const DataType& out_dtype) {
+ auto tensor = Cast(input_tensor, DataType::Float(64));
+ auto zero_scalar = MakeConstantScalar(DataType::Int(32), 0);
+ if (!IsEqualScalar(input_zero_point, zero_scalar)) {
+ // Broadcast input zero point if needed.
+ int rank = static_cast<int>(input_shape.size());
+ int axis = (param->axis < 0) ? ((rank > 0) ? rank + param->axis : 0) : param->axis;
+ Expr input_zero_broadcast = ExpandBiasToMatchAxis(Reshape(input_zero_point,
+ {
+ -1,
+ }),
+ rank, {axis});
+ tensor = Subtract(tensor, Cast(input_zero_broadcast, DataType::Float(64)));
+ }
+
+ // 2) If the input and output scales are same, we can skip the multiplication. Check
+ // if the input scale is per-tensor or per-channel. If it is per-tensor, there is single scale for
+ // the whole tensor. For per-channel (aka per-axis), there is a vector of scales for the input
+ // tensor. Depending on the quantization type, the fixed point multiplication routing is called.
+ auto scaled_fp64_t = tensor;
+ double output_scale_float = GetScalarFromConstant<float>(output_scale);
+ if (IsConstScalar(input_scale)) {
+ // This is per-tensor quantization. Single scale.
+ double input_scale_float = GetScalarFromConstant<float>(input_scale);
+ double double_multiplier = input_scale_float / output_scale_float;
+ // Skip if input and output scales are same.
+ if (!IsEqualScalar(input_scale, output_scale)) {
+ double multiplier = double_multiplier;
+ auto m_scalar = MakeConstantScalar(DataType::Float(64), multiplier);
+ scaled_fp64_t = Multiply(m_scalar, scaled_fp64_t);
+ }
+
+ } else {
+ // This is per-channel (per=axis) quantization.
+ std::vector<double> double_multipliers;
+ auto input_axis_scales = GetFloatVectorFromConstant(input_scale);
+ double output_scale_float = GetScalarFromConstant<float>(output_scale);
+ for (auto input_axis_scale : input_axis_scales) {
+ double multiplier = static_cast<double>(input_axis_scale) / output_scale_float;
+ double_multipliers.push_back(multiplier);
+ }
+ int axis = param->axis;
+ axis = (axis == -1) ? input_shape.size() - 1 : axis;
+
+ auto fixed_pt_multiplier_expr = MakeConstantTensor(
+ DataType::Float(64), {(int64_t)double_multipliers.size()}, double_multipliers);
+ size_t n_dim = input_shape.size();
+ auto exp_fixed_pt_multiplier_expr =
+ ExpandBiasToMatchAxis(fixed_pt_multiplier_expr, n_dim, {axis});
+
+ scaled_fp64_t = Multiply(scaled_fp64_t, exp_fixed_pt_multiplier_expr);
+ }
+
+ // 3) Add the output zero point.
+ auto shifted_fp64_t = scaled_fp64_t;
+ if (!IsEqualScalar(output_zero_point, zero_scalar)) {
+ shifted_fp64_t = Add(shifted_fp64_t, Cast(output_zero_point, DataType::Float(64)));
+ }
+
+ if (param->rounding == "UPWARD") {
+ shifted_fp64_t = Upward(shifted_fp64_t);
+ } else /*if (param->rounding == "TONEAREST")*/ {
+ shifted_fp64_t = Tonearest(shifted_fp64_t);
+ }
+
+ shifted_fp64_t = Cast(shifted_fp64_t, DataType::Int(32));
+ // 4) Clip to the out_dtype min/max. Skip clipping if out_dtype is Int32. The fixed point
+ // multiplication keeps the value in int32 range.
+ if (out_dtype == DataType::Int(32)) {
+ return shifted_fp64_t;
+ }
+
+ auto q_min = GetQmin(out_dtype);
+ auto q_max = GetQmax(out_dtype);
+ auto clipped_t = Clip(shifted_fp64_t, q_min, q_max);
+ return Cast(clipped_t, out_dtype);
+}
+
+// Lowering of qnn.requantize op
+/*
+ * \brief Lower requantize to a sequence of ops.
+ * \param input_tensor The input tensor to requantize op.
+ * \param param The requantize op attrs.
+ * \param input_shape The input tensor shape of the requantize op.
+ * \return The sequence of existing Relay ops.
+ */
+Expr RequantizeLower(const Expr& input_tensor, const Expr& input_scale,
+ const Expr& input_zero_point, const Expr& output_scale,
+ const Expr& output_zero_point, const RequantizeAttrs* param,
+ const Array<IndexExpr>& input_shape, const DataType& out_dtype) {
+ auto target = Target::Current(true);
+ if (target.defined() && target->kind->name == "llvm") {
Review comment:
I think itd also be nice to add a comment here explaining why we're only doing this for llvm. That will help contributors decide which to apply to new hardware.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r791266059
##########
File path: python/tvm/relay/qnn/op/qnn.py
##########
@@ -70,7 +161,9 @@ def requantize(
rounding : string, optional
Defines the rounding direction when the value is midway between two
representable values.
-
+ compute_dtype:
+ Specifies the algorithm of requantize calculating.
Review comment:
Same comment as above
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] elvin-n edited a comment on pull request #9637: Add FP requantize flow for llvm target
Posted by GitBox <gi...@apache.org>.
elvin-n edited a comment on pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#issuecomment-1000376137
> Have you benchmarked on ARM? I think we should enable this only for x86 for now.
I have measured the first version with just fp32 tir.Round and it worked faster than int64 version. Since we have changed something in the implementation vs first version, we have to remeasure
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] Icemist commented on pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
Icemist commented on pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#issuecomment-1008503724
> Have you benchmarked on ARM? I think we should enable this only for x86 for now.
I turned it off for not x86. It is also now possible to manage the implementation of requantize that will be used.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r791267700
##########
File path: python/tvm/topi/x86/utils.py
##########
@@ -16,8 +16,87 @@
# under the License.
"""Common x86 related utilities"""
import tvm
+import tvm._ffi
+@tvm._ffi.register_func("tvm.topi.x86.utils.target_is_x86")
+def target_is_x86(target):
+ return (
+ target_has_sse41(target)
+ or target_has_sse42(target)
+ or target_has_avx(target)
+ or target_has_avx2(target)
+ or target_has_avx512(target)
+ or target_has_vnni(target)
+ or target
+ in {
+ "k6-2",
+ "x86-64",
+ "atom",
+ "pentium",
+ "nocona",
+ "k8",
+ "i686",
+ "winchip-c6",
+ "prescott",
+ "k6",
+ "athlon-mp",
+ "winchip2",
+ "yonah",
+ "athlon-tbird",
+ "alderlake",
+ "pentium4",
+ "athlon64-sse3",
+ "pentium2",
+ "bonnell",
+ "k8-sse3",
+ "barcelona",
+ "athlon",
+ "c3",
+ "generic",
+ "lakemont",
+ "pentium-mmx",
+ "core2",
+ "pentium3",
+ "btver1",
+ "athlon-fx",
+ "k6-3",
+ "geode",
+ "pentium-m",
+ "i586",
+ "pentium3m",
+ "pentiumpro",
+ "i486",
+ "opteron",
+ "athlon64",
+ "pentium4m",
+ "i386",
+ "opteron-sse3",
+ "amdfam10",
+ "athlon-4",
+ "athlon-xp",
+ "c3-2",
Review comment:
Do we need this level of details? I prefer dropping them. I don't think people would ever specify these targets...
I think sse4.1 - vnni are enough.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] Icemist commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
Icemist commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r791637888
##########
File path: include/tvm/relay/qnn/attrs.h
##########
@@ -54,6 +55,11 @@ struct RequantizeAttrs : public tvm::AttrsNode<RequantizeAttrs> {
"value is rounded away from zero at midpoints (for example, -1.5"
"rounds to -2). More context can be found at following gblic manual"
"https://www.gnu.org/software/libc/manual/html_node/Rounding.html.");
+ TVM_ATTR_FIELD(compute_dtype)
+ .set_default("None")
+ .describe(
+ "Specifies the algorithm of requantize calculating. Supported "
Review comment:
Done.
##########
File path: python/tvm/relay/qnn/op/qnn.py
##########
@@ -70,7 +161,9 @@ def requantize(
rounding : string, optional
Defines the rounding direction when the value is midway between two
representable values.
-
+ compute_dtype:
+ Specifies the algorithm of requantize calculating.
Review comment:
Done.
##########
File path: src/relay/qnn/op/req_config.cc
##########
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/relay/qnn/op/req_config.cc
+ * \brief QNN requantize config.
+ */
+
+#include "./req_config.h"
Review comment:
Done.
##########
File path: python/tvm/topi/x86/utils.py
##########
@@ -16,8 +16,87 @@
# under the License.
"""Common x86 related utilities"""
import tvm
+import tvm._ffi
+@tvm._ffi.register_func("tvm.topi.x86.utils.target_is_x86")
+def target_is_x86(target):
+ return (
+ target_has_sse41(target)
+ or target_has_sse42(target)
+ or target_has_avx(target)
+ or target_has_avx2(target)
+ or target_has_avx512(target)
+ or target_has_vnni(target)
+ or target
+ in {
+ "k6-2",
+ "x86-64",
+ "atom",
+ "pentium",
+ "nocona",
+ "k8",
+ "i686",
+ "winchip-c6",
+ "prescott",
+ "k6",
+ "athlon-mp",
+ "winchip2",
+ "yonah",
+ "athlon-tbird",
+ "alderlake",
+ "pentium4",
+ "athlon64-sse3",
+ "pentium2",
+ "bonnell",
+ "k8-sse3",
+ "barcelona",
+ "athlon",
+ "c3",
+ "generic",
+ "lakemont",
+ "pentium-mmx",
+ "core2",
+ "pentium3",
+ "btver1",
+ "athlon-fx",
+ "k6-3",
+ "geode",
+ "pentium-m",
+ "i586",
+ "pentium3m",
+ "pentiumpro",
+ "i486",
+ "opteron",
+ "athlon64",
+ "pentium4m",
+ "i386",
+ "opteron-sse3",
+ "amdfam10",
+ "athlon-4",
+ "athlon-xp",
+ "c3-2",
Review comment:
I agree, sse4.1 looks good. Users can always use requantize_config to change the default behavior.
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] Icemist commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
Icemist commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r791637888
##########
File path: include/tvm/relay/qnn/attrs.h
##########
@@ -54,6 +55,11 @@ struct RequantizeAttrs : public tvm::AttrsNode<RequantizeAttrs> {
"value is rounded away from zero at midpoints (for example, -1.5"
"rounds to -2). More context can be found at following gblic manual"
"https://www.gnu.org/software/libc/manual/html_node/Rounding.html.");
+ TVM_ATTR_FIELD(compute_dtype)
+ .set_default("None")
+ .describe(
+ "Specifies the algorithm of requantize calculating. Supported "
Review comment:
Done.
##########
File path: python/tvm/relay/qnn/op/qnn.py
##########
@@ -70,7 +161,9 @@ def requantize(
rounding : string, optional
Defines the rounding direction when the value is midway between two
representable values.
-
+ compute_dtype:
+ Specifies the algorithm of requantize calculating.
Review comment:
Done.
##########
File path: src/relay/qnn/op/req_config.cc
##########
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/relay/qnn/op/req_config.cc
+ * \brief QNN requantize config.
+ */
+
+#include "./req_config.h"
Review comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] elvin-n commented on pull request #9637: Add FP requantize flow for llvm target
Posted by GitBox <gi...@apache.org>.
elvin-n commented on pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#issuecomment-1000376137
> Have you benchmarked on ARM? I think we should enable this only for x86 for now.
I have measured the first version with just fp32 tir.Round and it worked faster than int64 version. Since we have changed something in the implementation vs first version, we have to remeasure
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] Icemist commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
Icemist commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r780868062
##########
File path: python/tvm/relay/qnn/op/qnn.py
##########
@@ -92,6 +92,44 @@ def requantize(
)
+def upward(data):
Review comment:
I found this logic enough tested by basic tests and removed these operations.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] Icemist commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
Icemist commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r780872272
##########
File path: python/tvm/relay/qnn/op/qnn.py
##########
@@ -92,6 +92,44 @@ def requantize(
)
+def upward(data):
Review comment:
"I think itd also be nice to add a comment here explaining why we're only doing this for llvm. That will help contributors decide which to apply to new hardware."
I moved this logic to the default reqconfig. I added a little comment there. Other cases can be handled by specified reqconfig or requantize parameters.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#issuecomment-1020498191
Please go through your change and remove all uses of the term `calculation flow`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r791268447
##########
File path: src/relay/qnn/op/req_config.cc
##########
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/relay/qnn/op/req_config.cc
+ * \brief QNN requantize config.
+ */
+
+#include "./req_config.h"
Review comment:
Rename these files (`req_config.h` and `req_config.cc`)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r791265798
##########
File path: include/tvm/relay/qnn/attrs.h
##########
@@ -54,6 +55,11 @@ struct RequantizeAttrs : public tvm::AttrsNode<RequantizeAttrs> {
"value is rounded away from zero at midpoints (for example, -1.5"
"rounds to -2). More context can be found at following gblic manual"
"https://www.gnu.org/software/libc/manual/html_node/Rounding.html.");
+ TVM_ATTR_FIELD(compute_dtype)
+ .set_default("None")
+ .describe(
+ "Specifies the algorithm of requantize calculating. Supported "
Review comment:
`Specifies the data type used during requantize.`
##########
File path: python/tvm/relay/qnn/op/qnn.py
##########
@@ -70,7 +161,9 @@ def requantize(
rounding : string, optional
Defines the rounding direction when the value is midway between two
representable values.
-
+ compute_dtype:
+ Specifies the algorithm of requantize calculating.
Review comment:
Same comment as above
##########
File path: python/tvm/topi/x86/utils.py
##########
@@ -16,8 +16,87 @@
# under the License.
"""Common x86 related utilities"""
import tvm
+import tvm._ffi
+@tvm._ffi.register_func("tvm.topi.x86.utils.target_is_x86")
+def target_is_x86(target):
+ return (
+ target_has_sse41(target)
+ or target_has_sse42(target)
+ or target_has_avx(target)
+ or target_has_avx2(target)
+ or target_has_avx512(target)
+ or target_has_vnni(target)
+ or target
+ in {
+ "k6-2",
+ "x86-64",
+ "atom",
+ "pentium",
+ "nocona",
+ "k8",
+ "i686",
+ "winchip-c6",
+ "prescott",
+ "k6",
+ "athlon-mp",
+ "winchip2",
+ "yonah",
+ "athlon-tbird",
+ "alderlake",
+ "pentium4",
+ "athlon64-sse3",
+ "pentium2",
+ "bonnell",
+ "k8-sse3",
+ "barcelona",
+ "athlon",
+ "c3",
+ "generic",
+ "lakemont",
+ "pentium-mmx",
+ "core2",
+ "pentium3",
+ "btver1",
+ "athlon-fx",
+ "k6-3",
+ "geode",
+ "pentium-m",
+ "i586",
+ "pentium3m",
+ "pentiumpro",
+ "i486",
+ "opteron",
+ "athlon64",
+ "pentium4m",
+ "i386",
+ "opteron-sse3",
+ "amdfam10",
+ "athlon-4",
+ "athlon-xp",
+ "c3-2",
Review comment:
Do we need the level of details? I don't think people would ever specify these targets... I think sse4.1 - vnni are enough.
##########
File path: python/tvm/topi/x86/utils.py
##########
@@ -16,8 +16,87 @@
# under the License.
"""Common x86 related utilities"""
import tvm
+import tvm._ffi
+@tvm._ffi.register_func("tvm.topi.x86.utils.target_is_x86")
+def target_is_x86(target):
+ return (
+ target_has_sse41(target)
+ or target_has_sse42(target)
+ or target_has_avx(target)
+ or target_has_avx2(target)
+ or target_has_avx512(target)
+ or target_has_vnni(target)
+ or target
+ in {
+ "k6-2",
+ "x86-64",
+ "atom",
+ "pentium",
+ "nocona",
+ "k8",
+ "i686",
+ "winchip-c6",
+ "prescott",
+ "k6",
+ "athlon-mp",
+ "winchip2",
+ "yonah",
+ "athlon-tbird",
+ "alderlake",
+ "pentium4",
+ "athlon64-sse3",
+ "pentium2",
+ "bonnell",
+ "k8-sse3",
+ "barcelona",
+ "athlon",
+ "c3",
+ "generic",
+ "lakemont",
+ "pentium-mmx",
+ "core2",
+ "pentium3",
+ "btver1",
+ "athlon-fx",
+ "k6-3",
+ "geode",
+ "pentium-m",
+ "i586",
+ "pentium3m",
+ "pentiumpro",
+ "i486",
+ "opteron",
+ "athlon64",
+ "pentium4m",
+ "i386",
+ "opteron-sse3",
+ "amdfam10",
+ "athlon-4",
+ "athlon-xp",
+ "c3-2",
Review comment:
Do we need this level of details? I prefer dropping them. I don't think people would ever specify these targets...
I think sse4.1 - vnni are enough.
##########
File path: src/relay/qnn/op/req_config.cc
##########
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/relay/qnn/op/req_config.cc
+ * \brief QNN requantize config.
+ */
+
+#include "./req_config.h"
Review comment:
Rename these files (`req_config.h` and `req_config.cc`)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] Icemist commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
Icemist commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r790667429
##########
File path: src/relay/qnn/op/requantize.cc
##########
@@ -208,6 +269,141 @@ Expr RequantizeLower(const Expr& input_tensor, const Expr& input_scale,
return Cast(clipped_t, out_dtype);
}
+// Lowering of qnn.requantize op
+
+/*
+ * \brief Lower requantize to a sequence of ops.
+ * \param input_tensor The input tensor to requantize op.
+ * \param param The requantize op attrs.
+ * \param input_shape The input tensor shape of the requantize op.
+ * \return The sequence of existing Relay ops.
+ * \note RequantizationFP using floating computation. All multiplication/sub/sum
+ * occurs in floating point data type and only at the end is converted to
+ * int32 data type and clamped for output data type.
+ *
+ * The whole computation this can be broken down into following steps
+ * 1) Subtract the input zero point.
+ * 2) Perform multiplication.
+ * 3) Add the output zero point.
+ * 4) Cast to the out_dtype.
+ */
+template <int Bits>
+Expr RequantizeLowerFP(const Expr& input_tensor, const Expr& input_scale,
+ const Expr& input_zero_point, const Expr& output_scale,
+ const Expr& output_zero_point, const RequantizeAttrs* param,
+ const Array<IndexExpr>& input_shape, const DataType& out_dtype) {
+ auto tensor = Cast(input_tensor, DataType::Float(Bits));
+ auto zero_scalar = MakeConstantScalar(DataType::Int(32), 0);
+ if (!IsEqualScalar(input_zero_point, zero_scalar)) {
+ // Broadcast input zero point if needed.
+ int rank = static_cast<int>(input_shape.size());
+ int axis = (param->axis < 0) ? ((rank > 0) ? rank + param->axis : 0) : param->axis;
+ Expr input_zero_broadcast = ExpandBiasToMatchAxis(Reshape(input_zero_point,
+ {
+ -1,
+ }),
+ rank, {axis});
+ tensor = Subtract(Cast(tensor, DataType::Float(Bits)),
+ Cast(input_zero_broadcast, DataType::Float(Bits)));
+ } else {
+ tensor = Cast(tensor, DataType::Float(Bits));
+ }
+
+ // 2) If the input and output scales are same, we can skip the multiplication. Check
+ // if the input scale is per-tensor or per-channel. If it is per-tensor, there is single scale for
+ // the whole tensor. For per-channel (aka per-axis), there is a vector of scales for the input
+ // tensor. Depending on the quantization type, the fixed point multiplication routing is called.
+ auto scaled_fp_t = tensor;
+ double output_scale_float = GetScalarFromConstant<float>(output_scale);
+ if (IsConstScalar(input_scale)) {
+ // This is per-tensor quantization. Single scale.
+ double input_scale_float = GetScalarFromConstant<float>(input_scale);
+ double double_multiplier = static_cast<double>(input_scale_float) / output_scale_float;
+ // Skip if input and output scales are same.
+ if (!IsEqualScalar(input_scale, output_scale)) {
+ double multiplier = double_multiplier;
+ auto m_scalar = MakeConstantScalar(DataType::Float(Bits), multiplier);
+ scaled_fp_t = Multiply(m_scalar, scaled_fp_t);
+ }
+
+ } else {
+ // This is per-channel (per=axis) quantization.
+ std::vector<double> double_multipliers;
+ auto input_axis_scales = GetFloatVectorFromConstant(input_scale);
+ double output_scale_float = GetScalarFromConstant<float>(output_scale);
+ for (auto input_axis_scale : input_axis_scales) {
+ double multiplier = static_cast<double>(input_axis_scale) / output_scale_float;
+ double_multipliers.push_back(multiplier);
+ }
+ int axis = param->axis;
+ axis = (axis == -1) ? input_shape.size() - 1 : axis;
+
+ auto fixed_pt_multiplier_expr = MakeConstantTensor(
+ DataType::Float(Bits), {(int64_t)double_multipliers.size()}, double_multipliers);
+ size_t n_dim = input_shape.size();
+ auto exp_fixed_pt_multiplier_expr =
+ ExpandBiasToMatchAxis(fixed_pt_multiplier_expr, n_dim, {axis});
+
+ scaled_fp_t = Multiply(scaled_fp_t, exp_fixed_pt_multiplier_expr);
+ }
+
+ // 3) Add the output zero point.
+ auto shifted_fp_t = scaled_fp_t;
+ if (!IsEqualScalar(output_zero_point, zero_scalar)) {
+ shifted_fp_t = Add(shifted_fp_t, Cast(output_zero_point, DataType::Float(Bits)));
+ }
+
+ if (param->rounding == "UPWARD") {
+ shifted_fp_t = Upward<Bits>(shifted_fp_t);
+ } else /*if (param->rounding == "TONEAREST")*/ {
+ shifted_fp_t = Tonearest<Bits>(shifted_fp_t);
+ }
+
+ shifted_fp_t = Cast(shifted_fp_t, DataType::Int(32));
+ // 4) Clip to the out_dtype min/max. Skip clipping if out_dtype is Int32. The fixed point
+ // multiplication keeps the value in int32 range.
+ if (out_dtype == DataType::Int(32)) {
+ return shifted_fp_t;
+ }
+
+ auto q_min = GetQmin(out_dtype);
+ auto q_max = GetQmax(out_dtype);
+ auto clipped_t = Clip(shifted_fp_t, q_min, q_max);
+ return Cast(clipped_t, out_dtype);
+}
+
+// Lowering of qnn.requantize op
+/*
+ * \brief Lower requantize to a sequence of ops.
+ * \param input_tensor The input tensor to requantize op.
+ * \param param The requantize op attrs.
+ * \param input_shape The input tensor shape of the requantize op.
+ * \return The sequence of existing Relay ops.
+ */
+Expr RequantizeLower(const Expr& input_tensor, const Expr& input_scale,
+ const Expr& input_zero_point, const Expr& output_scale,
+ const Expr& output_zero_point, const RequantizeAttrs* param,
+ const Array<IndexExpr>& input_shape, const DataType& out_dtype) {
+ // Check rounding validity.
+ ICHECK(param->rounding == "UPWARD" || param->rounding == "TONEAREST")
+ << "QNN requantize supports two rounding modes - UPWARD and "
+ << "TONEAREST";
+ // Check calculation flow validity.
+ ICHECK(param->calculation_flow_type == "int64" || param->calculation_flow_type == "float32" ||
+ param->calculation_flow_type == "float64")
+ << "QNN requantize supports three calculation flow types - \"int64\", \"float32\" and "
+ "\"float64\"";
+ if (param->calculation_flow_type == "float32")
+ return RequantizeLowerFP<32>(input_tensor, input_scale, input_zero_point, output_scale,
+ output_zero_point, param, input_shape, out_dtype);
+ else if (param->calculation_flow_type == "float64")
+ return RequantizeLowerFP<64>(input_tensor, input_scale, input_zero_point, output_scale,
+ output_zero_point, param, input_shape, out_dtype);
+ else /*if (param->calculation_flow_type == "int64") */
+ return RequantizeLowerInt(input_tensor, input_scale, input_zero_point, output_scale,
+ output_zero_point, param, input_shape, out_dtype);
+}
Review comment:
Changed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] Icemist commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
Icemist commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r790667298
##########
File path: python/tvm/relay/qnn/op/qnn.py
##########
@@ -14,19 +14,109 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-# pylint: disable=invalid-name
+# pylint: disable=invalid-name,unused-argument, not-context-manager
"""QNN dialect operators."""
from __future__ import absolute_import as _abs
+import tvm
+import tvm.ir
from tvm import relay
+from tvm.runtime import Object
from tvm.relay.expr import Tuple, TupleWrapper
from tvm.relay.op.nn.utils import get_pad_tuple2d
from tvm.topi.nn.qnn import SQNN_DTYPE_TO_CODE
-
+from tvm.target import Target
+from tvm.topi.x86.utils import target_is_x86
from ... import op as reg
from ...op import OpPattern
from . import _make
+from . import _requantize
+
+
+@tvm._ffi.register_object("relay.qnn.op.ReqConfig")
+class ReqConfig(Object):
Review comment:
Renamed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on pull request #9637: Add FP requantize flow. Set float32 flow by default for llvm x86 targets with sse4.1 support
Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#issuecomment-1021915298
Please kick another CI job.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#issuecomment-1020498191
Please go through your change and remove all uses of the term `calculation flow`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r790334870
##########
File path: src/relay/qnn/op/requantize.cc
##########
@@ -208,6 +269,141 @@ Expr RequantizeLower(const Expr& input_tensor, const Expr& input_scale,
return Cast(clipped_t, out_dtype);
}
+// Lowering of qnn.requantize op
+
+/*
+ * \brief Lower requantize to a sequence of ops.
+ * \param input_tensor The input tensor to requantize op.
+ * \param param The requantize op attrs.
+ * \param input_shape The input tensor shape of the requantize op.
+ * \return The sequence of existing Relay ops.
+ * \note RequantizationFP using floating computation. All multiplication/sub/sum
+ * occurs in floating point data type and only at the end is converted to
+ * int32 data type and clamped for output data type.
+ *
+ * The whole computation this can be broken down into following steps
+ * 1) Subtract the input zero point.
+ * 2) Perform multiplication.
+ * 3) Add the output zero point.
+ * 4) Cast to the out_dtype.
+ */
+template <int Bits>
+Expr RequantizeLowerFP(const Expr& input_tensor, const Expr& input_scale,
+ const Expr& input_zero_point, const Expr& output_scale,
+ const Expr& output_zero_point, const RequantizeAttrs* param,
+ const Array<IndexExpr>& input_shape, const DataType& out_dtype) {
+ auto tensor = Cast(input_tensor, DataType::Float(Bits));
+ auto zero_scalar = MakeConstantScalar(DataType::Int(32), 0);
+ if (!IsEqualScalar(input_zero_point, zero_scalar)) {
+ // Broadcast input zero point if needed.
+ int rank = static_cast<int>(input_shape.size());
+ int axis = (param->axis < 0) ? ((rank > 0) ? rank + param->axis : 0) : param->axis;
+ Expr input_zero_broadcast = ExpandBiasToMatchAxis(Reshape(input_zero_point,
+ {
+ -1,
+ }),
+ rank, {axis});
+ tensor = Subtract(Cast(tensor, DataType::Float(Bits)),
+ Cast(input_zero_broadcast, DataType::Float(Bits)));
+ } else {
+ tensor = Cast(tensor, DataType::Float(Bits));
+ }
+
+ // 2) If the input and output scales are same, we can skip the multiplication. Check
+ // if the input scale is per-tensor or per-channel. If it is per-tensor, there is single scale for
+ // the whole tensor. For per-channel (aka per-axis), there is a vector of scales for the input
+ // tensor. Depending on the quantization type, the fixed point multiplication routing is called.
+ auto scaled_fp_t = tensor;
+ double output_scale_float = GetScalarFromConstant<float>(output_scale);
+ if (IsConstScalar(input_scale)) {
+ // This is per-tensor quantization. Single scale.
+ double input_scale_float = GetScalarFromConstant<float>(input_scale);
+ double double_multiplier = static_cast<double>(input_scale_float) / output_scale_float;
+ // Skip if input and output scales are same.
+ if (!IsEqualScalar(input_scale, output_scale)) {
+ double multiplier = double_multiplier;
+ auto m_scalar = MakeConstantScalar(DataType::Float(Bits), multiplier);
+ scaled_fp_t = Multiply(m_scalar, scaled_fp_t);
+ }
+
+ } else {
+ // This is per-channel (per=axis) quantization.
+ std::vector<double> double_multipliers;
+ auto input_axis_scales = GetFloatVectorFromConstant(input_scale);
+ double output_scale_float = GetScalarFromConstant<float>(output_scale);
+ for (auto input_axis_scale : input_axis_scales) {
+ double multiplier = static_cast<double>(input_axis_scale) / output_scale_float;
+ double_multipliers.push_back(multiplier);
+ }
+ int axis = param->axis;
+ axis = (axis == -1) ? input_shape.size() - 1 : axis;
+
+ auto fixed_pt_multiplier_expr = MakeConstantTensor(
+ DataType::Float(Bits), {(int64_t)double_multipliers.size()}, double_multipliers);
+ size_t n_dim = input_shape.size();
+ auto exp_fixed_pt_multiplier_expr =
+ ExpandBiasToMatchAxis(fixed_pt_multiplier_expr, n_dim, {axis});
+
+ scaled_fp_t = Multiply(scaled_fp_t, exp_fixed_pt_multiplier_expr);
+ }
+
+ // 3) Add the output zero point.
+ auto shifted_fp_t = scaled_fp_t;
+ if (!IsEqualScalar(output_zero_point, zero_scalar)) {
+ shifted_fp_t = Add(shifted_fp_t, Cast(output_zero_point, DataType::Float(Bits)));
+ }
+
+ if (param->rounding == "UPWARD") {
+ shifted_fp_t = Upward<Bits>(shifted_fp_t);
+ } else /*if (param->rounding == "TONEAREST")*/ {
+ shifted_fp_t = Tonearest<Bits>(shifted_fp_t);
+ }
+
+ shifted_fp_t = Cast(shifted_fp_t, DataType::Int(32));
+ // 4) Clip to the out_dtype min/max. Skip clipping if out_dtype is Int32. The fixed point
+ // multiplication keeps the value in int32 range.
+ if (out_dtype == DataType::Int(32)) {
+ return shifted_fp_t;
+ }
+
+ auto q_min = GetQmin(out_dtype);
+ auto q_max = GetQmax(out_dtype);
+ auto clipped_t = Clip(shifted_fp_t, q_min, q_max);
+ return Cast(clipped_t, out_dtype);
+}
+
+// Lowering of qnn.requantize op
+/*
+ * \brief Lower requantize to a sequence of ops.
+ * \param input_tensor The input tensor to requantize op.
+ * \param param The requantize op attrs.
+ * \param input_shape The input tensor shape of the requantize op.
+ * \return The sequence of existing Relay ops.
+ */
+Expr RequantizeLower(const Expr& input_tensor, const Expr& input_scale,
+ const Expr& input_zero_point, const Expr& output_scale,
+ const Expr& output_zero_point, const RequantizeAttrs* param,
+ const Array<IndexExpr>& input_shape, const DataType& out_dtype) {
+ // Check rounding validity.
+ ICHECK(param->rounding == "UPWARD" || param->rounding == "TONEAREST")
+ << "QNN requantize supports two rounding modes - UPWARD and "
+ << "TONEAREST";
+ // Check calculation flow validity.
+ ICHECK(param->calculation_flow_type == "int64" || param->calculation_flow_type == "float32" ||
+ param->calculation_flow_type == "float64")
+ << "QNN requantize supports three calculation flow types - \"int64\", \"float32\" and "
+ "\"float64\"";
+ if (param->calculation_flow_type == "float32")
+ return RequantizeLowerFP<32>(input_tensor, input_scale, input_zero_point, output_scale,
+ output_zero_point, param, input_shape, out_dtype);
+ else if (param->calculation_flow_type == "float64")
+ return RequantizeLowerFP<64>(input_tensor, input_scale, input_zero_point, output_scale,
+ output_zero_point, param, input_shape, out_dtype);
+ else /*if (param->calculation_flow_type == "int64") */
+ return RequantizeLowerInt(input_tensor, input_scale, input_zero_point, output_scale,
+ output_zero_point, param, input_shape, out_dtype);
+}
Review comment:
use braces, `if () {} else {}`
##########
File path: python/tvm/relay/qnn/op/qnn.py
##########
@@ -14,19 +14,109 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-# pylint: disable=invalid-name
+# pylint: disable=invalid-name,unused-argument, not-context-manager
"""QNN dialect operators."""
from __future__ import absolute_import as _abs
+import tvm
+import tvm.ir
from tvm import relay
+from tvm.runtime import Object
from tvm.relay.expr import Tuple, TupleWrapper
from tvm.relay.op.nn.utils import get_pad_tuple2d
from tvm.topi.nn.qnn import SQNN_DTYPE_TO_CODE
-
+from tvm.target import Target
+from tvm.topi.x86.utils import target_is_x86
from ... import op as reg
from ...op import OpPattern
from . import _make
+from . import _requantize
+
+
+@tvm._ffi.register_object("relay.qnn.op.ReqConfig")
+class ReqConfig(Object):
Review comment:
`ReqConifg` -> `RequantizeConfig`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] Icemist commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
Icemist commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r790667751
##########
File path: include/tvm/relay/qnn/attrs.h
##########
@@ -36,6 +36,7 @@ namespace qnn {
struct RequantizeAttrs : public tvm::AttrsNode<RequantizeAttrs> {
int axis;
std::string rounding;
+ std::string calculation_flow_type;
Review comment:
Renamed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on a change in pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#discussion_r791267700
##########
File path: python/tvm/topi/x86/utils.py
##########
@@ -16,8 +16,87 @@
# under the License.
"""Common x86 related utilities"""
import tvm
+import tvm._ffi
+@tvm._ffi.register_func("tvm.topi.x86.utils.target_is_x86")
+def target_is_x86(target):
+ return (
+ target_has_sse41(target)
+ or target_has_sse42(target)
+ or target_has_avx(target)
+ or target_has_avx2(target)
+ or target_has_avx512(target)
+ or target_has_vnni(target)
+ or target
+ in {
+ "k6-2",
+ "x86-64",
+ "atom",
+ "pentium",
+ "nocona",
+ "k8",
+ "i686",
+ "winchip-c6",
+ "prescott",
+ "k6",
+ "athlon-mp",
+ "winchip2",
+ "yonah",
+ "athlon-tbird",
+ "alderlake",
+ "pentium4",
+ "athlon64-sse3",
+ "pentium2",
+ "bonnell",
+ "k8-sse3",
+ "barcelona",
+ "athlon",
+ "c3",
+ "generic",
+ "lakemont",
+ "pentium-mmx",
+ "core2",
+ "pentium3",
+ "btver1",
+ "athlon-fx",
+ "k6-3",
+ "geode",
+ "pentium-m",
+ "i586",
+ "pentium3m",
+ "pentiumpro",
+ "i486",
+ "opteron",
+ "athlon64",
+ "pentium4m",
+ "i386",
+ "opteron-sse3",
+ "amdfam10",
+ "athlon-4",
+ "athlon-xp",
+ "c3-2",
Review comment:
Do we need the level of details? I don't think people would ever specify these targets... I think sse4.1 - vnni are enough.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on pull request #9637: Add FP requantize flow. Set this by default for llvm x86 targets
Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #9637:
URL: https://github.com/apache/tvm/pull/9637#issuecomment-1012091001
cc @jwfromm
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi merged pull request #9637: Add FP requantize flow. Set float32 flow by default for llvm x86 targets with sse4.1 support.
Posted by GitBox <gi...@apache.org>.
masahi merged pull request #9637:
URL: https://github.com/apache/tvm/pull/9637
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org