tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
feature.h
Go to the documentation of this file.
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 
24 #ifndef TVM_RELAY_FEATURE_H_
25 #define TVM_RELAY_FEATURE_H_
26 
27 #include <tvm/ir/module.h>
28 #include <tvm/relay/expr.h>
29 
30 #include <bitset>
31 #include <string>
32 
33 namespace tvm {
34 namespace relay {
35 
37 enum Feature : int {
38  fVar = 0,
40  fConstant = 2,
41  fTuple = 3,
43  fFunction = 5,
44  fOp = 6,
45  fCall = 7,
46  fLet = 8,
47  fIf = 9,
48  fRefCreate = 10,
49  fRefRead = 11,
50  fRefWrite = 12,
52  fMatch = 14,
54  fGraph = 15,
56  fLetRec = 16
57 };
58 
59 constexpr size_t feature_count = 17;
60 
64 class FeatureSet {
65  public:
66  FeatureSet(const FeatureSet&) = default;
68  explicit FeatureSet(Feature ft) { bs_.set(static_cast<size_t>(ft)); }
69  explicit FeatureSet(const tvm::Array<tvm::Integer>& ft) {
70  for (Integer i : ft) {
71  *this += Feature(i.IntValue());
72  }
73  }
74  explicit operator Array<Integer>() const {
76  for (size_t i = 0; i < feature_count; ++i) {
77  if (bs_[i]) {
78  ret.push_back(Integer(i));
79  }
80  }
81  return ret;
82  }
84  static FeatureSet All() {
85  FeatureSet fs;
86  fs.bs_.flip();
87  return fs;
88  }
90  static FeatureSet No() {
91  FeatureSet fs;
92  return fs;
93  }
94  template <typename T>
95  FeatureSet& operator+=(const T& rhs) {
96  bs_ |= FeatureSet(rhs).bs_;
97  return *this;
98  }
100  template <typename T>
101  FeatureSet operator+(const T& rhs) const {
102  FeatureSet fs(*this);
103  fs += rhs;
104  return fs;
105  }
106  template <typename T>
107  FeatureSet& operator-=(const T& rhs) {
108  bs_ &= ~(FeatureSet(rhs)).bs_;
109  return *this;
110  }
112  template <typename T>
113  FeatureSet operator-(const T& rhs) const {
114  FeatureSet fs(*this);
115  fs -= rhs;
116  return fs;
117  }
125  bool is_subset_of(const FeatureSet& rhs) const { return ((*this) - rhs).bs_.none(); }
126 
130  std::string ToString() const;
131 
132  private:
133  std::bitset<feature_count> bs_;
134  FeatureSet() = default;
135  explicit FeatureSet(const std::bitset<feature_count>& bs) : bs_(bs) {}
136 };
137 
146 
155 
164 inline FeatureSet DetectFeature(const Expr& expr, const IRModule& mod) {
165  return DetectFeature(expr) + DetectFeature(mod);
166 }
167 
174 void CheckFeature(const RelayExpr& expr, const FeatureSet& fs);
175 
182 void CheckFeature(const IRModule& mod, const FeatureSet& fs);
183 
191 inline void CheckFeature(const RelayExpr& expr, const IRModule& mod, const FeatureSet& fs) {
192  CheckFeature(expr, fs);
193  CheckFeature(mod, fs);
194 }
195 
196 } // namespace relay
197 } // namespace tvm
198 
199 #endif // TVM_RELAY_FEATURE_H_
Managed reference class to IRModuleNode.
Definition: module.h:348
Container of constant int that adds more constructors.
Definition: expr.h:622
Managed reference to RelayExprNode.
Definition: expr.h:433
A finite set of Feature.
Definition: feature.h:64
FeatureSet(const FeatureSet &)=default
FeatureSet(const tvm::Array< tvm::Integer > &ft)
Definition: feature.h:69
bool is_subset_of(const FeatureSet &rhs) const
Is this a subset of rhs?
Definition: feature.h:125
static FeatureSet No()
The empty set. Contain no Feature.
Definition: feature.h:90
FeatureSet & operator+=(const T &rhs)
Definition: feature.h:95
FeatureSet(Feature ft)
A singleton set containing a single Feature.
Definition: feature.h:68
static FeatureSet All()
A set that contain all the Feature.
Definition: feature.h:84
FeatureSet operator+(const T &rhs) const
Set union.
Definition: feature.h:101
FeatureSet operator-(const T &rhs) const
Set difference.
Definition: feature.h:113
std::string ToString() const
return a string representation.
FeatureSet & operator-=(const T &rhs)
Definition: feature.h:107
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
IRModule that holds the functions and type definitions.
FeatureSet DetectFeature(const RelayExpr &expr)
Calculate the feature of the program.
void CheckFeature(const RelayExpr &expr, const FeatureSet &fs)
Check the feature of the program.
Feature
Different kinds of relay feature a program might use.
Definition: feature.h:37
@ fLet
Definition: feature.h:46
@ fConstant
Definition: feature.h:40
@ fFunction
Definition: feature.h:43
@ fVar
Definition: feature.h:38
@ fRefCreate
Definition: feature.h:48
@ fRefWrite
Definition: feature.h:50
@ fTupleGetItem
Definition: feature.h:42
@ fIf
Definition: feature.h:47
@ fMatch
Definition: feature.h:52
@ fCall
Definition: feature.h:45
@ fRefRead
Definition: feature.h:49
@ fTuple
Definition: feature.h:41
@ fGraph
Whether any non-atom fragment of the program is shared, making the program a graph.
Definition: feature.h:54
@ fOp
Definition: feature.h:44
@ fGlobalVar
Definition: feature.h:39
@ fLetRec
Whether there is local fixpoint in the program.
Definition: feature.h:56
@ fConstructor
Definition: feature.h:51
constexpr size_t feature_count
Definition: feature.h:59
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:290
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
Relay expression language.