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/05/25 21:55:22 UTC

[GitHub] [tvm] mbrookhart commented on a change in pull request #8126: [Relay] Convert a fake quantized or QAT graph into QNN ops

mbrookhart commented on a change in pull request #8126:
URL: https://github.com/apache/tvm/pull/8126#discussion_r639232640



##########
File path: tests/python/relay/test_pass_quantize_fake_quantization.py
##########
@@ -0,0 +1,280 @@
+# 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.
+# pylint: disable=unused-wildcard-import
+import numpy as np
+import pytest
+
+import tvm
+from tvm import relay
+from tvm.relay.dataflow_pattern import *
+
+
+def test_fake_quantize_conv():
+    for out_dtype in ["int8", "uint8"]:
+        x = relay.var("x", shape=[1, 3, 224, 224], dtype="int8")
+        w = relay.var("w", shape=[16, 3, 5, 5], dtype="int8")
+        one = relay.const(1.0)
+        zero = relay.const(0)
+
+        op = relay.op.nn.conv2d(
+            relay.qnn.op.dequantize(x, relay.const(2.0), zero),
+            relay.qnn.op.dequantize(w, relay.const(0.5), zero),
+        )
+        op = relay.qnn.op.quantize(op, one, zero, out_dtype=out_dtype)
+
+        mod = tvm.IRModule.from_expr(op)
+        mod = tvm.relay.transform.InferType()(mod)
+
+        x_np = np.random.randint(-128, 127, size=[1, 3, 224, 224], dtype="int8")
+        w_np = np.random.randint(-128, 127, size=[16, 3, 5, 5], dtype="int8")
+
+        mod2 = tvm.relay.transform.QuantizeFakeQuantization()(mod)
+        assert not tvm.ir.structural_equal(mod, mod2)
+        mod2 = tvm.relay.transform.FoldConstant()(mod2)
+
+        ex = relay.create_executor("vm", mod=mod, device=tvm.cpu(), target="llvm")
+        result = ex.evaluate()(x_np, w_np).asnumpy()
+
+        ex = relay.create_executor("vm", mod=mod2, device=tvm.cpu(), target="llvm")
+        result2 = ex.evaluate()(x_np, w_np).asnumpy()
+
+        assert np.array_equal(result, result2)

Review comment:
       I can imagine a time when fp32 rounding error causes an issue when casting back to int, but I can't actually make it fail in practice, I've run this about 50 times 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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org