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_operators.cc
22 * \brief Extract unique operators from an IRModule
23 */
24#include <tvm/node/structural_hash.h>
25#include <tvm/relay/analysis.h>
26#include <tvm/relay/expr.h>
27#include <tvm/relay/expr_functor.h>
28
29namespace tvm {
30namespace relay {
31
32class OperatorExtractorWrapper : private MixedModeVisitor {
33 public:
34 explicit OperatorExtractorWrapper(const IRModule& mod) : mod_(mod) {}
35
36 Map<String, tvm::Integer> Extract() {
37 VisitExpr(this->mod_->Lookup("main"));
38
39 return operator_freqs_;
40 }
41
42 private:
43 using MixedModeVisitor::VisitExpr_;
44
45 const IRModule mod_;
46 /*! \brief Map of operator to frequency. */
47 Map<String, tvm::Integer> operator_freqs_;
48
49 void VisitExpr_(const CallNode* n) final {
50 VisitExpr(n->op);
51
52 auto op = n->op.as<OpNode>();
53 if (op) {
54 auto it = operator_freqs_.find(op->name);
55 ICHECK(it != operator_freqs_.end())
56 << "Call's OpNode must be visited and registered before access";
57 operator_freqs_.Set(op->name, 1 + operator_freqs_.at(op->name).IntValue());
58 }
59
60 MixedModeVisitor::VisitExpr_(n);
61 }
62
63 void VisitExpr_(const OpNode* n) final {
64 // NOTE: OpNode is visited only once for every operator kind
65 // regardless of how many times that op appears in the graph.
66 operator_freqs_.Set(n->name, 0U);
67 }
68};
69
70Map<String, tvm::Integer> ExtractOperatorsPacked(const IRModule& mod) {
71 return OperatorExtractorWrapper(mod).Extract();
72}
73
74TVM_REGISTER_GLOBAL("relay.analysis.ExtractOperators").set_body_typed(ExtractOperatorsPacked);
75
76} // namespace relay
77} // namespace tvm
78