masahi commented on a change in pull request #7474: URL: https://github.com/apache/tvm/pull/7474#discussion_r578913474
########## File path: python/tvm/relay/transform/quantize/_requantizer.py ########## @@ -0,0 +1,312 @@ +# 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. + +"""Removes extraneous qnn.quantize and qnn.dequantize from calibrated modules, and replaces them +with qnn.requanize ops.""" +import math + +import tvm +from tvm import relay +from tvm.relay.dataflow_pattern import DFPatternCallback, wildcard, is_op, dominates, rewrite + + +class Requantizer: + """Removes extraneous qnn.quantize and qnn.dequantize and replaces + them with qnn.requantize.""" + + class RequantizerCallback(DFPatternCallback): + """First pass that inserts requantize ops, specifically taking + qnn.dequantize -> qnn.quantize to qnn.requantize + and + qnn.dequantize -> int8_op* -> qnn.quantize to requantize -> int8_op* + """ + + def __init__(self): + super().__init__() + + self.data = wildcard() + self.dequantize_scale = wildcard() + self.dequantize_zp = wildcard() + + self.quantize_scale = wildcard() + self.quantize_zp = wildcard() + + # Ops that are permitted inbetween quantize and dequantize if we are + # rewriting to requantize + self.is_int_8_op = ( + is_op("nn.max_pool2d")(wildcard()) + | is_op("nn.max_pool2d")(wildcard()) + | is_op("nn.max_pool3d")(wildcard()) + | is_op("nn.relu")(wildcard()) + | is_op("transpose")(wildcard()) + | is_op("reshape")(wildcard()) + | is_op("nn.pad")(wildcard()) + | is_op("squeeze")(wildcard()) + | is_op("nn.global_avg_pool2d") + | is_op("nn.batch_flatten") + | is_op("copy") + | is_op("mean") + | is_op("sqrt") + ) Review comment: This is too ad hoc, it can easily break and leaves more dequantize/quantize than necessary. And the patterns are not 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org