AndrewZhaoLuo commented on a change in pull request #10239:
URL: https://github.com/apache/tvm/pull/10239#discussion_r807263764
##########
File path: tests/python/relay/test_pass_fake_quantization_to_integer.py
##########
@@ -627,3 +628,217 @@ def conv2d(expr, type_map): # pylint:
disable=unused-variable
# Catch a generic exception because the tvm FFI eats the python exception
type
with pytest.raises(Exception):
mod_int =
tvm.relay.transform.FakeQuantizationToInteger(hard_fail=True)(mod)
+
+
+def compare_expected_fq_to_int(expr, expected_expr, args,
allow_rounding_error=False):
+ mod = tvm.IRModule.from_expr(expr)
+ mod_def = tvm.relay.transform.InferType()(mod)
+ mod_int = tvm.relay.transform.FakeQuantizationToInteger(False)(mod_def)
+ mod_exp =
tvm.relay.transform.InferType()(tvm.IRModule.from_expr(expected_expr))
+ print("mod_def\n", mod_def, "\n")
Review comment:
remove prints
##########
File path: src/relay/transforms/type_infer.cc
##########
@@ -918,11 +919,7 @@ Type InferTypeLocal(const Expr& expr) {
mod = transform::InferType()(mod);
Type result_type;
- if (expr.as<FunctionNode>()) {
- result_type = mod->Lookup("main")->checked_type();
- } else {
- result_type = mod->Lookup("main").as<FunctionNode>()->body->checked_type();
- }
+ result_type = relay::InferType(sub_graph)->checked_type();
Review comment:
`relay::InferType()` will run `transform::InferType()(mod)` again which
is wasteful. just remove the additional call to InferType() above
##########
File path: src/relay/transforms/fake_quantization_to_integer.cc
##########
@@ -270,8 +293,233 @@ class FakeQuantizationRewriter : public MixedModeMutator {
const bool hard_fail_;
};
+bool is_op_enabled_for_optional_fq2i(const CallNode* call_node) {
+ const Op op = Downcast<Op>(call_node->op);
+ static auto fqfq =
Op::GetAttrMap<FTVMFakeQuantizationToInteger>("FTVMFakeQuantizationToInteger");
+ static std::unordered_set<Op, tvm::ObjectHash, tvm::ObjectEqual> ops = {
+ Op::Get("reshape"),
Review comment:
Can you reorganize this in alphabetical order of op name?
(sorry for asking this lol)
##########
File path: tests/python/relay/test_pass_fake_quantization_to_integer.py
##########
@@ -627,3 +628,217 @@ def conv2d(expr, type_map): # pylint:
disable=unused-variable
# Catch a generic exception because the tvm FFI eats the python exception
type
with pytest.raises(Exception):
mod_int =
tvm.relay.transform.FakeQuantizationToInteger(hard_fail=True)(mod)
+
+
+def compare_expected_fq_to_int(expr, expected_expr, args,
allow_rounding_error=False):
+ mod = tvm.IRModule.from_expr(expr)
+ mod_def = tvm.relay.transform.InferType()(mod)
+ mod_int = tvm.relay.transform.FakeQuantizationToInteger(False)(mod_def)
+ mod_exp =
tvm.relay.transform.InferType()(tvm.IRModule.from_expr(expected_expr))
+ print("mod_def\n", mod_def, "\n")
+ print("mod_int\n", mod_int, "\n")
+ print("mod_exp\n", mod_exp, "\n")
+ assert not tvm.ir.structural_equal(mod, mod_int)
+ assert tvm.ir.structural_equal(mod_int, mod_exp)
+ result_def = (
+ relay.create_executor("vm", mod=mod_def, device=tvm.cpu(),
target="llvm")
+ .evaluate()(*args)
+ .numpy()
+ )
+ result_int = (
+ relay.create_executor("vm", mod=mod_int, device=tvm.cpu(),
target="llvm")
+ .evaluate()(*args)
+ .numpy()
+ )
+ result_exp = (
+ relay.create_executor("vm", mod=mod_exp, device=tvm.cpu(),
target="llvm")
+ .evaluate()(*args)
+ .numpy()
+ )
+ print("result_def\n", result_def)
+ print("result_int\n", result_int)
+ print("result_exp\n", result_exp)
+ if allow_rounding_error:
+ assert np.all(np.abs(result_def.astype("int32") -
result_int.astype("int32")) <= 1)
+ else:
+ assert np.array_equal(result_def, result_int)
+
+ assert np.array_equal(result_int, result_exp)
+
+
+def test_fq2i_optional_op_chaind_with_disabled_op():
+ shape_x = [1, 4, 2]
+ shape_w = [1, 4, 2]
+ a = relay.var("a", shape=shape_x, dtype="int8")
+ b = relay.var("b", shape=shape_w, dtype="int8")
+
+ op0 = relay.qnn.op.dequantize(a, relay.const(2.0), relay.const(0))
+ op1 = relay.qnn.op.dequantize(b, relay.const(6.0), relay.const(0))
+ op2 = relay.op.nn.batch_matmul(op0, op1)
+ op3 = relay.op.add(op2, relay.const(1.0))
+ expr = relay.op.erf(op3)
+
+ op0 = relay.qnn.op.qnn.batch_matmul(
+ a, b, relay.const(0), relay.const(0), relay.const(2.0),
relay.const(6.0)
+ )
+ op1 = relay.qnn.op.qnn.dequantize(op0, relay.const(12.0), relay.const(0))
+ op2 = relay.op.add(op1, relay.const(1.0))
+ expected_expr = relay.op.erf(op2)
+
+ x_np = np.random.randint(-128, 127, size=shape_x, dtype="int8")
+ w_np = np.random.randint(-128, 127, size=shape_w, dtype="int8")
+ compare_expected_fq_to_int(expr, expected_expr, [x_np, w_np], False)
Review comment:
nit: don't need to include default_kwargs (allow_rounding=False)
##########
File path: tests/python/relay/test_pass_fake_quantization_to_integer.py
##########
@@ -627,3 +628,217 @@ def conv2d(expr, type_map): # pylint:
disable=unused-variable
# Catch a generic exception because the tvm FFI eats the python exception
type
with pytest.raises(Exception):
mod_int =
tvm.relay.transform.FakeQuantizationToInteger(hard_fail=True)(mod)
+
+
+def compare_expected_fq_to_int(expr, expected_expr, args,
allow_rounding_error=False):
+ mod = tvm.IRModule.from_expr(expr)
+ mod_def = tvm.relay.transform.InferType()(mod)
+ mod_int = tvm.relay.transform.FakeQuantizationToInteger(False)(mod_def)
+ mod_exp =
tvm.relay.transform.InferType()(tvm.IRModule.from_expr(expected_expr))
+ print("mod_def\n", mod_def, "\n")
+ print("mod_int\n", mod_int, "\n")
+ print("mod_exp\n", mod_exp, "\n")
+ assert not tvm.ir.structural_equal(mod, mod_int)
+ assert tvm.ir.structural_equal(mod_int, mod_exp)
+ result_def = (
+ relay.create_executor("vm", mod=mod_def, device=tvm.cpu(),
target="llvm")
+ .evaluate()(*args)
+ .numpy()
+ )
+ result_int = (
+ relay.create_executor("vm", mod=mod_int, device=tvm.cpu(),
target="llvm")
+ .evaluate()(*args)
+ .numpy()
+ )
+ result_exp = (
+ relay.create_executor("vm", mod=mod_exp, device=tvm.cpu(),
target="llvm")
+ .evaluate()(*args)
+ .numpy()
+ )
+ print("result_def\n", result_def)
+ print("result_int\n", result_int)
+ print("result_exp\n", result_exp)
+ if allow_rounding_error:
+ assert np.all(np.abs(result_def.astype("int32") -
result_int.astype("int32")) <= 1)
+ else:
+ assert np.array_equal(result_def, result_int)
+
+ assert np.array_equal(result_int, result_exp)
+
+
+def test_fq2i_optional_op_chaind_with_disabled_op():
Review comment:
Some of these tests are bit complicated, so I would ask a short comment
explaining what this tests for
It'll also help me review
##########
File path: include/tvm/relay/qnn/op/dequantize.h
##########
@@ -0,0 +1,39 @@
+/*
+ * 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 tvm/relay/executor.h
+ * \brief Relay dequantize.
+ */
+#ifndef TVM_RELAY_QNN_OP_DEQUANTIZE_H_
+#define TVM_RELAY_QNN_OP_DEQUANTIZE_H_
+
+#include <tvm/relay/expr.h>
Review comment:
nit: include header in dequantize.cc too
##########
File path: tests/python/relay/test_pass_fake_quantization_to_integer.py
##########
@@ -627,3 +628,217 @@ def conv2d(expr, type_map): # pylint:
disable=unused-variable
# Catch a generic exception because the tvm FFI eats the python exception
type
with pytest.raises(Exception):
mod_int =
tvm.relay.transform.FakeQuantizationToInteger(hard_fail=True)(mod)
+
+
+def compare_expected_fq_to_int(expr, expected_expr, args,
allow_rounding_error=False):
+ mod = tvm.IRModule.from_expr(expr)
+ mod_def = tvm.relay.transform.InferType()(mod)
+ mod_int = tvm.relay.transform.FakeQuantizationToInteger(False)(mod_def)
+ mod_exp =
tvm.relay.transform.InferType()(tvm.IRModule.from_expr(expected_expr))
+ print("mod_def\n", mod_def, "\n")
+ print("mod_int\n", mod_int, "\n")
+ print("mod_exp\n", mod_exp, "\n")
+ assert not tvm.ir.structural_equal(mod, mod_int)
+ assert tvm.ir.structural_equal(mod_int, mod_exp)
+ result_def = (
+ relay.create_executor("vm", mod=mod_def, device=tvm.cpu(),
target="llvm")
+ .evaluate()(*args)
+ .numpy()
+ )
+ result_int = (
+ relay.create_executor("vm", mod=mod_int, device=tvm.cpu(),
target="llvm")
+ .evaluate()(*args)
+ .numpy()
+ )
+ result_exp = (
+ relay.create_executor("vm", mod=mod_exp, device=tvm.cpu(),
target="llvm")
+ .evaluate()(*args)
+ .numpy()
+ )
+ print("result_def\n", result_def)
+ print("result_int\n", result_int)
+ print("result_exp\n", result_exp)
+ if allow_rounding_error:
+ assert np.all(np.abs(result_def.astype("int32") -
result_int.astype("int32")) <= 1)
+ else:
+ assert np.array_equal(result_def, result_int)
+
+ assert np.array_equal(result_int, result_exp)
+
+
+def test_fq2i_optional_op_chaind_with_disabled_op():
Review comment:
`chand` --> `chained`
--
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]