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/04/09 11:34:34 UTC

[GitHub] [tvm] huochaitiantang opened a new pull request #7814: [Topi & Relay] Add quantization support for the vision transform model in GPU

huochaitiantang opened a new pull request #7814:
URL: https://github.com/apache/tvm/pull/7814


   We submit this PR to add quantization support for the [vision transform (vit)](https://arxiv.org/abs/2010.11929) model in GPU. The main change is as follows:
   
   1, In vit model,  time-consuming operators are batch_matmul,  so we first add the compute and schedule of `batch_matmul_int8.cuda` in tvm.topi.cuda
   
   2, To support the quantization of batch_matmul, we then add `batch_matmul_rewrite` and `BatchMatmulRealize` in tvm.relay.quantize 
   
   3, The kl -divergence calibrate could not preserve the accuracy of vit model well, so we add the `_percentile_scale` function 
   
   For the vit-B32-224 model, the performance is as follows:
   
   - Top-1 accuracy in Imagenet validation
     - paper: 73.38
     - nonofficial-model-fp32: 73.27
     - nonofficial-model-int8: 72.78
   
   - The latency in GTX1660 GPU
     - nonofficial-model-fp32: 10.32 ms
     - nonofficial-model-int8: 4.93 ms
   
   Thanks for your review! @jcf94 @tqchen


-- 
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



[GitHub] [tvm] huochaitiantang commented on a change in pull request #7814: [Topi & Relay] Add quantization support for the vision transform model in GPU

Posted by GitBox <gi...@apache.org>.
huochaitiantang commented on a change in pull request #7814:
URL: https://github.com/apache/tvm/pull/7814#discussion_r611298716



##########
File path: python/tvm/relay/op/strategy/cuda.py
##########
@@ -722,12 +722,20 @@ def dense_strategy_cuda(attrs, inputs, out_type, target):
 def batch_matmul_strategy_cuda(attrs, inputs, out_type, target):
     """batch_matmul cuda strategy"""
     strategy = _op.OpStrategy()
-    strategy.add_implementation(
-        wrap_compute_batch_matmul(topi.cuda.batch_matmul),
-        wrap_topi_schedule(topi.cuda.schedule_batch_matmul),
-        name="batch_matmul.cuda",
-        plevel=10,
-    )
+    x, y = inputs
+    if x.dtype == "int8" and y.dtype == "int8" and out_type.dtype == "int32":
+        strategy.add_implementation(
+            wrap_compute_batch_matmul(topi.cuda.batch_matmul_int8, need_out_dtype=True),
+            wrap_topi_schedule(topi.cuda.schedule_batch_matmul_int8),
+            name="batch_matmul_int8.cuda",

Review comment:
       We add plevel=10 for the batch_matmul_int8.cuda, which is the same as dense_int8.cuda




-- 
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



[GitHub] [tvm] jcf94 merged pull request #7814: [Topi & Relay] Add quantization support for the vision transform model in GPU

Posted by GitBox <gi...@apache.org>.
jcf94 merged pull request #7814:
URL: https://github.com/apache/tvm/pull/7814


   


-- 
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



[GitHub] [tvm] huochaitiantang commented on a change in pull request #7814: [Topi & Relay] Add quantization support for the vision transform model in GPU

Posted by GitBox <gi...@apache.org>.
huochaitiantang commented on a change in pull request #7814:
URL: https://github.com/apache/tvm/pull/7814#discussion_r611300473



##########
File path: tests/python/nightly/quantization/test_quantization_accuracy_for_vit.py
##########
@@ -0,0 +1,129 @@
+# 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.
+import tvm
+import os
+from tvm import relay
+from tvm.relay import quantize as qtz
+import logging
+import onnx
+import tvm.testing
+import mxnet as mx
+from test_quantization_accuracy import Config, get_val_data, eval_acc
+
+logging.basicConfig(level=logging.INFO)
+
+
+def calibrate_dataset(model_name, rec_val, batch_size, calibration_samples):
+    val_data, _ = get_val_data(model_name, rec_val=rec_val, batch_size=batch_size)
+    val_data.reset()
+    for i, batch in enumerate(val_data):
+        if i * batch_size >= calibration_samples:
+            break
+        data = batch.data[0].asnumpy()
+        yield {"data": data}
+
+
+def get_onnx_model(model_name, batch_size, qconfig, target=None, original=False, dataset=None):
+
+    assert model_name == "vit32", "Only support vit32 model!"
+    logfile = "gtx1660_vit_B32_224.log"
+    onnx_path = "vit_B32_224.onnx"
+
+    if not os.path.exists(logfile):
+        os.system("wget https://github.com/TheGreatCold/tvm-vit/raw/master/{}".format(logfile))
+    if not os.path.exists(onnx_path):
+        os.system("wget https://github.com/TheGreatCold/tvm-vit/raw/master/{}".format(onnx_path))

Review comment:
       Thanks for your review. We updated the download codes based on your suggestion. Besides, the wget method is not compatible on different platforms, so we use the urllib library instead.




-- 
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



[GitHub] [tvm] jcf94 commented on a change in pull request #7814: [Topi & Relay] Add quantization support for the vision transform model in GPU

Posted by GitBox <gi...@apache.org>.
jcf94 commented on a change in pull request #7814:
URL: https://github.com/apache/tvm/pull/7814#discussion_r611146143



##########
File path: python/tvm/relay/op/strategy/cuda.py
##########
@@ -722,12 +722,20 @@ def dense_strategy_cuda(attrs, inputs, out_type, target):
 def batch_matmul_strategy_cuda(attrs, inputs, out_type, target):
     """batch_matmul cuda strategy"""
     strategy = _op.OpStrategy()
-    strategy.add_implementation(
-        wrap_compute_batch_matmul(topi.cuda.batch_matmul),
-        wrap_topi_schedule(topi.cuda.schedule_batch_matmul),
-        name="batch_matmul.cuda",
-        plevel=10,
-    )
+    x, y = inputs
+    if x.dtype == "int8" and y.dtype == "int8" and out_type.dtype == "int32":
+        strategy.add_implementation(
+            wrap_compute_batch_matmul(topi.cuda.batch_matmul_int8, need_out_dtype=True),
+            wrap_topi_schedule(topi.cuda.schedule_batch_matmul_int8),
+            name="batch_matmul_int8.cuda",

Review comment:
       Add a proper plevel for this implementation.

##########
File path: tests/python/nightly/quantization/test_quantization_accuracy_for_vit.py
##########
@@ -0,0 +1,129 @@
+# 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.
+import tvm
+import os
+from tvm import relay
+from tvm.relay import quantize as qtz
+import logging
+import onnx
+import tvm.testing
+import mxnet as mx
+from test_quantization_accuracy import Config, get_val_data, eval_acc
+
+logging.basicConfig(level=logging.INFO)
+
+
+def calibrate_dataset(model_name, rec_val, batch_size, calibration_samples):
+    val_data, _ = get_val_data(model_name, rec_val=rec_val, batch_size=batch_size)
+    val_data.reset()
+    for i, batch in enumerate(val_data):
+        if i * batch_size >= calibration_samples:
+            break
+        data = batch.data[0].asnumpy()
+        yield {"data": data}
+
+
+def get_onnx_model(model_name, batch_size, qconfig, target=None, original=False, dataset=None):
+
+    assert model_name == "vit32", "Only support vit32 model!"
+    logfile = "gtx1660_vit_B32_224.log"
+    onnx_path = "vit_B32_224.onnx"
+
+    if not os.path.exists(logfile):
+        os.system("wget https://github.com/TheGreatCold/tvm-vit/raw/master/{}".format(logfile))
+    if not os.path.exists(onnx_path):
+        os.system("wget https://github.com/TheGreatCold/tvm-vit/raw/master/{}".format(onnx_path))

Review comment:
       As a unit test, I'm thinking that this may not be so good to involve a resource outside. (Network problem or changes to the `tvm-vit` repo may break the UT. At least use git commit instad of branch like: `https://github.com/TheGreatCold/tvm-vit/raw/d2aa1e60eef42e2fdedbd1e13aa85ac5faf0a7fc/vit_B32_224.onnx` will be better)
   I'm not sure if there's any better solution for this. @tqchen Do you have any suggestion?




-- 
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



[GitHub] [tvm] XHPlus commented on pull request #7814: [Topi & Relay] Add quantization support for the vision transform model in GPU

Posted by GitBox <gi...@apache.org>.
XHPlus commented on pull request #7814:
URL: https://github.com/apache/tvm/pull/7814#issuecomment-816628502


   Thanks for the reviewer. We will keep updating more results for other ViT models and contributing more quantization calibration algorithms.


-- 
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