1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/*!
21 * \file extract_fake_quantized_ops.cc
22 * \brief Extract fake quantized operators from an IRModule
23 */
24#include <tvm/relay/expr.h>
25#include <tvm/relay/expr_functor.h>
26#include <tvm/relay/transform.h>
27
28#include "../transforms/fake_quantization_to_integer.h"
29
30namespace tvm {
31namespace relay {
32
33using ExprSet = std::unordered_set<Expr, ObjectPtrHash, ObjectPtrEqual>;
34
35class ExtractFakeQuantizedOpsWrapper : private MixedModeVisitor {
36 public:
37 Map<String, tvm::Integer> Extract(const IRModule& m) {
38 IRModule mod(m);
39 mod = transform::InferType()(mod);
40 VisitExpr(mod->Lookup("main"));
41
42 return fake_quantized_op_freqs_;
43 }
44
45 private:
46 using MixedModeVisitor::VisitExpr_;
47
48 void VisitExpr_(const CallNode* call_node) override {
49 if (call_node->op == quantize_op_) {
50 SubgraphExtractor extractor;
51 ExprSet subgraph = extractor.GetSubgraph(GetRef<Expr>(call_node));
52
53 for (auto expr : subgraph) {
54 const Op op = Downcast<Op>(expr.as<CallNode>()->op);
55 if (op != dequantize_op_) {
56 if (fake_quantized_op_freqs_.find(op->name) != fake_quantized_op_freqs_.end()) {
57 fake_quantized_op_freqs_.Set(op->name,
58 fake_quantized_op_freqs_.at(op->name).IntValue() + 1);
59 } else {
60 fake_quantized_op_freqs_.Set(op->name, 1);
61 }
62 }
63 }
64 }
65 }
66
67 Map<String, tvm::Integer> fake_quantized_op_freqs_;
68 const Op quantize_op_ = Op::Get("qnn.quantize");
69 const Op dequantize_op_ = Op::Get("qnn.dequantize");
70};
71
72Map<String, tvm::Integer> ExtractFakeQuantizedOpsPacked(const IRModule& mod) {
73 return ExtractFakeQuantizedOpsWrapper().Extract(mod);
74}
75
76TVM_REGISTER_GLOBAL("relay.analysis.ExtractFakeQuantizedOps")
77 .set_body_typed(ExtractFakeQuantizedOpsPacked);
78
79} // namespace relay
80} // namespace tvm
81