AndrewZhaoLuo commented on a change in pull request #10089: URL: https://github.com/apache/tvm/pull/10089#discussion_r794764050
########## File path: tests/python/relay/test_analysis_extract_fake_quantized_ops.py ########## @@ -0,0 +1,142 @@ +# 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. +"""Test function extraction""" +import pytest +import tvm +from tvm import relay + + +def test_fake_quantize_conv(): + x = relay.var("x", shape=[1, 3, 224, 224], dtype="int8") + w = relay.var("w", shape=[16, 3, 5, 5], dtype="int8") + 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), + kernel_size=[5, 5], + ) + op = relay.qnn.op.quantize(op, relay.const(1.0), zero, out_dtype="int8") + + mod = tvm.IRModule.from_expr(op) + fake_quantized_op_freqs = relay.analysis.list_fake_quantized_op_freqs(mod) + + assert len(fake_quantized_op_freqs) == 1 Review comment: I think you can just do direct equality throughout this file dict(fake_quantized_op_freqs) == {"nn.conv2d": 1} ########## File path: src/relay/analysis/extract_fake_quantized_ops.cc ########## @@ -0,0 +1,108 @@ +/* + * 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 extract_fake_quantized_ops.cc + * \brief Extract fake quantized operators from an IRModule + */ +#include <tvm/relay/expr.h> +#include <tvm/relay/expr_functor.h> + +namespace tvm { +namespace relay { + +using ExprSet = std::unordered_set<Expr, ObjectPtrHash, ObjectPtrEqual>; + +class FakeQuantizedRegionExtractor : public ExprVisitor { Review comment: Can you just reuse `SubgraphExtractor` in `src/relay/transforms/fake_quantization_to_integer.cc`? ########## File path: src/relay/analysis/extract_fake_quantized_ops.cc ########## @@ -0,0 +1,108 @@ +/* + * 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 extract_fake_quantized_ops.cc + * \brief Extract fake quantized operators from an IRModule + */ +#include <tvm/relay/expr.h> +#include <tvm/relay/expr_functor.h> + +namespace tvm { +namespace relay { + +using ExprSet = std::unordered_set<Expr, ObjectPtrHash, ObjectPtrEqual>; + +class FakeQuantizedRegionExtractor : public ExprVisitor { + public: + const ExprSet GetRegion(const Expr& expr) { + VisitExpr(expr); + ExprSet region; + for (auto kv : this->visit_counter_) { + if (auto call_node = GetRef<ObjectRef>(kv.first).as<CallNode>()) { + if (call_node->op != quantize_op_ && call_node->op != dequantize_op_) { + region.insert(Downcast<Expr>(GetRef<ObjectRef>(kv.first))); + } + } + } + return region; + } + + protected: + void VisitExpr_(const CallNode* call_node) override { + if (call_node->op == quantize_op_) { + // only look at arg0 for quantize + VisitExpr(call_node->args[0]); + } else if (call_node->op != dequantize_op_) { + // run normally on everything else. + ExprVisitor::VisitExpr_(call_node); + } + } + + const Op quantize_op_ = Op::Get("qnn.quantize"); + const Op dequantize_op_ = Op::Get("qnn.dequantize"); +}; + +class ExtractFakeQuantizedOpsWrapper : private MixedModeVisitor { Review comment: Hmm so this MixedModeVisitor is actually critical to the working of this pass since it automatically expands dataflow regions for a search. E.g. if you used ExprVisitor this would fail. This means you also need to manually check for dataflow regions see `src/relay/transforms/fake_quantization_to_integer.cc:102` Alternatively you can just reuse `SubgraphExtractor` which does this -- 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]
