masahi commented on code in PR #15849:
URL: https://github.com/apache/tvm/pull/15849#discussion_r1343525305


##########
src/relax/op/tensor/qdq.cc:
##########
@@ -0,0 +1,201 @@
+/*
+ * 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/relax/op/tensor/qdq.cc
+ * \brief implements quantize/dequantize operators.
+ */
+
+#include "qdq.h"
+
+#include <utility>
+
+#include "../../transform/utils.h"
+#include "../op_common.h"
+
+namespace tvm {
+namespace relax {
+
+TVM_REGISTER_NODE_TYPE(QuantizeAttrs);
+
+/* relax.quantize */
+
+Expr quantize(Expr data, Expr scale, Expr zero_point, int axis, DataType 
out_dtype) {
+  ObjectPtr<QuantizeAttrs> attrs = make_object<QuantizeAttrs>();
+  attrs->axis = axis;
+  attrs->out_dtype = out_dtype;
+  static const Op& op = Op::Get("relax.quantize");
+  return Call(op, {std::move(data), std::move(scale), std::move(zero_point)}, 
Attrs(attrs));
+}
+
+TVM_REGISTER_GLOBAL("relax.op.quantize").set_body_typed(quantize);
+
+StructInfo InferStructInfoQuantize(const Call& call, const BlockBuilder& ctx) {
+  const auto* attrs = call->attrs.as<QuantizeAttrs>();
+  if (attrs->out_dtype != DataType::Int(8) && attrs->out_dtype != 
DataType::UInt(8) &&
+      attrs->out_dtype != DataType::Int(16) && attrs->out_dtype != 
DataType::UInt(16)) {
+    ctx->ReportFatal(Diagnostic::Error(call)
+                     << "Unsupported output datatype attribute for operation: 
'"
+                     << attrs->out_dtype);
+  }
+
+  TensorStructInfo input_sinfo = GetInputTensorStructInfo(call, ctx)[0];
+  TensorStructInfo scale_sinfo = GetInputTensorStructInfo(call, ctx)[1];
+  TensorStructInfo zp_sinfo = GetInputTensorStructInfo(call, ctx)[2];
+
+  // Check input datatype:
+  if (input_sinfo->dtype != DataType::Float(16) && input_sinfo->dtype != 
DataType::Float(32)) {
+    ctx->ReportFatal(Diagnostic::Error(call)
+                     << "Unsupported input datatype for operation: " << 
input_sinfo->dtype);
+  }
+
+  // Check datatype of scale param:
+  if (scale_sinfo->dtype != DataType::Float(32) && scale_sinfo->dtype != 
DataType::Float(16)) {
+    ctx->ReportFatal(Diagnostic::Error(call)
+                     << "scale param datatype should be one of [float16, 
float32], but got "
+                     << scale_sinfo->dtype);
+  }
+
+  // Check datatype of zero_point param:
+  if (zp_sinfo->dtype != DataType::Int(32)) {

Review Comment:
   I remember that zp needs to be 8 bit in practice. How about enforcing int8 
here?



##########
src/relax/op/tensor/qdq.h:
##########
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  Sex 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.  Sex The License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/relax/op/tensor/qdq.h
+ * \brief The functions to make Relax quantize/dequantize operator calls.

Review Comment:
   Is this header needed?



##########
python/tvm/relax/op/qdq.py:
##########
@@ -0,0 +1,88 @@
+# 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.
+"""Relax quantize/dequantize operators"""
+
+from ..expr import Expr
+from . import _ffi_api
+
+
+def quantize(data: Expr, scale: Expr, zero_point: Expr, axis: int = -1, 
out_dtype: str = "int8"):
+    r"""Quantize op
+    This operator takes input and produces quantized output. The input tensor 
can be of any shape.
+    The output shape is the same as input shape.
+
+    Q_output = clamp((round(input_tensor/scale) + zero_point), out_dtype::min, 
out_dtype::max)
+
+    Parameters
+    ----------
+    data : tvm.relax.Expr
+        The input tensor to be quantized.
+
+    scale : tvm.relax.Expr
+        The output scale.
+
+    zero_point : tvm.relay.Expr
+        The output zero_point.
+
+    axis : int
+        The channel axis for quantization. Default value is -1 which 
corresponds to the last axis.
+
+    out_dtype : str, optional
+        The data type of the output tensor.
+
+    Returns
+    -------
+    result : tvm.relax.Expr
+        The computed result.
+    """
+
+    return _ffi_api.quantize(data, scale, zero_point, axis, out_dtype)
+
+
+def dequantize(
+    data: Expr, scale: Expr, zero_point: Expr, axis: int = -1, out_dtype: str 
= "float32"
+):
+    r"""Dequantize op
+    This operator takes input and produces dequantized output. The input 
tensor can be of any shape.
+    The output shape is the same as input shape.
+
+    output = clamp(scale * (input_tensor - zero_point), out_dtype::min, 
out_dtype::max)
+
+    Parameters
+    ----------
+    data : tvm.relax.Expr
+        The input tensor to be dequantized.
+
+    scale : tvm.relax.Expr
+        The output scale.
+
+    zero_point : tvm.relay.Expr
+        The output zero_point.

Review Comment:
   I think "input" scale / zp would be more correct?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to