masahi commented on a change in pull request #8126: URL: https://github.com/apache/tvm/pull/8126#discussion_r639305478
########## File path: python/tvm/relay/transform/quantize_fake_quantization.py ########## @@ -0,0 +1,177 @@ +# 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. +"""Relay functions for rewriting fake quantized ops.""" +import tvm +from tvm import relay +from ..op import register_quantize_fake_quantization + + +def fold_constant(expr): + mod = tvm.IRModule.from_expr(expr) + mod = relay.transform.FoldConstant()(mod) + return mod["main"].body + + +@register_quantize_fake_quantization("qnn.dequantize") +def dequantize_qfq(expr, type_map): + """Remove dequantize op""" + out = expr.args[0] + t = type_map[expr] + return [out, t.scale, t.zero_point, t.dtype] + + +@register_quantize_fake_quantization("qnn.quantize") +def quantize_qfq(expr, type_map): + """Turn a quantize op into requantize or remove it""" + out = expr.args[0] + t = type_map[out] + in_scale = fold_constant(t.scale) + in_zero_point = fold_constant(t.zero_point) + if not ( + tvm.ir.structural_equal(in_scale, expr.args[1]) + and tvm.ir.structural_equal(in_zero_point, expr.args[2]) + and tvm.ir.structural_equal(t.dtype, expr.attrs.out_dtype) + ): + out = relay.qnn.op.requantize( + out, + in_scale, + in_zero_point, + expr.args[1], + expr.args[2], + out_dtype=expr.attrs.out_dtype, + ) + return [out, expr.args[1], expr.args[2], expr.attrs.out_dtype] + + +@register_quantize_fake_quantization("reshape") +def reshape_qfq(expr, type_map): + """Rewrite a reshape op""" + arg = expr.args[0] + t = type_map[arg] + out = relay.op.reshape(arg, **expr.attrs) + return [out, t.scale, t.zero_point, t.dtype] + + +@register_quantize_fake_quantization("transpose") +def transpose_qfq(expr, type_map): + """Rewrite a transpose op""" + arg = expr.args[0] + t = type_map[arg] + out = relay.op.transpose(arg, **expr.attrs) + return [out, t.scale, t.zero_point, t.dtype] + + +@register_quantize_fake_quantization("nn.max_pool2d") +def maxpool_qfq(expr, type_map): + """Rewrite a maxpool op""" + arg = expr.args[0] + t = type_map[arg] + out = relay.op.nn.max_pool2d(arg, **expr.attrs) + return [out, t.scale, t.zero_point, t.dtype] + Review comment: How about something like this? ``` @register_quantize_fake_quantization_default("nn.max_pool2d", op.nn.max_pool2d) ``` This will save us from having to name each function like `maxpool_qfq` which is otherwise useless. Maybe the current way is fine when we have only 3 ops, but when we add more and more of these ops I think we would want more concise solution. -- 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: [email protected]
