tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
module.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_IR_MODULE_H_
25 #define TVM_IR_MODULE_H_
26 
27 #include <tvm/ir/expr.h>
28 #include <tvm/ir/function.h>
29 #include <tvm/ir/global_info.h>
30 #include <tvm/ir/source_map.h>
31 #include <tvm/ir/type.h>
35 
36 #include <string>
37 #include <unordered_map>
38 #include <unordered_set>
39 #include <utility>
40 #include <vector>
41 
42 namespace tvm {
43 
44 class IRModule;
45 
56 class IRModuleNode : public Object {
57  public:
62  /* \brief Additional attributes storing meta-data about the module. */
71 
91  template <typename TObjectRef>
93  const std::string& attr_key,
94  Optional<TObjectRef> default_value = Optional<TObjectRef>(nullptr)) const {
95  return attrs.GetAttr(attr_key, default_value);
96  }
97  // variant that uses TObjectRef to enable implicit conversion to default value.
98  template <typename TObjectRef>
99  Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
100  return GetAttr<TObjectRef>(attr_key, Optional<TObjectRef>(default_value));
101  }
102 
107  DictAttrs GetAttrs() const { return attrs; }
108 
128  bool HasNonzeroAttr(const std::string& attr_key) const { return attrs.HasNonzeroAttr(attr_key); }
129 
131 
133  v->Visit("functions", &functions);
134  v->Visit("global_var_map_", &global_var_map_);
135  v->Visit("source_map", &source_map);
136  v->Visit("attrs", &attrs);
137  v->Visit("global_infos", &global_infos);
138  }
139 
140  TVM_DLL bool SEqualReduce(const IRModuleNode* other, SEqualReducer equal) const;
141 
142  TVM_DLL void SHashReduce(SHashReducer hash_reduce) const;
143 
151  TVM_DLL void Add(const GlobalVar& var, const BaseFunc& func, bool update = false);
152 
160  TVM_DLL void AddUnchecked(const GlobalVar& var, const BaseFunc& func);
161 
167  TVM_DLL void Update(const GlobalVar& var, const BaseFunc& func);
168 
174  TVM_DLL void UpdateGlobalInfo(const String& name, const Array<GlobalInfo>& info);
175 
180  TVM_DLL void Remove(const GlobalVar& var);
181 
187  TVM_DLL bool ContainGlobalVar(const String& name) const;
188 
194  TVM_DLL GlobalVar GetGlobalVar(const String& str) const;
195 
202 
208  TVM_DLL BaseFunc Lookup(const GlobalVar& var) const;
209 
215  TVM_DLL BaseFunc Lookup(const String& name) const;
216 
222  TVM_DLL void Update(const IRModule& other);
223 
232  TVM_DLL std::unordered_set<String> Imports() const;
233 
235 
236  static constexpr const char* _type_key = "IRModule";
237  static constexpr const bool _type_has_method_sequal_reduce = true;
238  static constexpr const bool _type_has_method_shash_reduce = true;
240 
241  private:
242  friend class IRModule;
243 };
244 
249 class IRModule : public ObjectRef {
250  public:
258  TVM_DLL explicit IRModule(Map<GlobalVar, BaseFunc> functions, SourceMap map = {},
259  DictAttrs attrs = DictAttrs(),
260  Map<String, Array<GlobalInfo>> global_infos = {});
261 
271  auto* ptr = get_mutable();
272  ICHECK(ptr != nullptr);
273  return static_cast<IRModuleNode*>(ptr);
274  }
275 
280  TVM_DLL static IRModule FromExpr(const RelaxExpr& expr,
281  const Map<GlobalVar, BaseFunc>& global_funcs = {});
282 
289 
292 
294  static constexpr bool _type_is_nullable = false;
295 
296  // allow copy on write.
298 };
299 
300 namespace attr {
301 
302 // Following are attributes for IRModule only.
303 
311 constexpr const char* kModuleName = "mod_name";
312 
313 /*
314  * \brief All the runtime::NDArrays extracted from PrimFunc tir::AllocateConst nodes. The
315  * node will record the index into this array. See also kConstNameToConstant below, which is
316  * the analog for Realy Functions.
317  *
318  * Type: Array<runtime::NDArray>
319  */
320 constexpr const char* kConstants = "constants";
321 
328 constexpr const char* kExternalMods = "external_mods";
329 
357 constexpr const char* kSystemLibPrefix = "system_lib_prefix";
358 
367 constexpr const char* kConstNameToConstant = "const_name_to_constant";
368 
369 } // namespace attr
370 } // namespace tvm
371 #endif // TVM_IR_MODULE_H_
Runtime Array container types.
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
Managed reference to BaseFuncNode.
Definition: function.h:230
Managed reference to DictAttrsNode.
Definition: attrs.h:227
bool HasNonzeroAttr(const std::string &attr_key) const
Check whether the function has an non-zero integer attr.
Definition: attrs.h:306
Optional< TObjectRef > GetAttr(const std::string &attr_key, Optional< TObjectRef > default_value=Optional< TObjectRef >(nullptr)) const
Get a function attribute.
Definition: attrs.h:258
Managed reference to GlobalVarNode.
Definition: expr.h:449
IRModule that holds functions and type definitions.
Definition: module.h:56
bool HasNonzeroAttr(const std::string &attr_key) const
Check whether the module has an non-zero integer attr.
Definition: module.h:128
Map< String, GlobalVar > global_var_map_
A map from string names to global variables that ensures global uniqueness.
Definition: module.h:70
IRModuleNode()
Definition: module.h:130
void Remove(const GlobalVar &var)
Remove a function from the global environment.
Array< GlobalVar > GetGlobalVars() const
Collect all global vars defined in this module, ordered by the global variable name.
GlobalVar GetGlobalVar(const String &str) const
Lookup a global function by its variable.
void SHashReduce(SHashReducer hash_reduce) const
static constexpr const bool _type_has_method_sequal_reduce
Definition: module.h:237
Optional< TObjectRef > GetAttr(const std::string &attr_key, Optional< TObjectRef > default_value=Optional< TObjectRef >(nullptr)) const
Get a module attribute.
Definition: module.h:92
void AddUnchecked(const GlobalVar &var, const BaseFunc &func)
Add a function to the global environment.
bool SEqualReduce(const IRModuleNode *other, SEqualReducer equal) const
TVM_DECLARE_FINAL_OBJECT_INFO(IRModuleNode, Object)
static constexpr const char * _type_key
Definition: module.h:236
BaseFunc Lookup(const String &name) const
Look up a global function by its string name.
IRModule ShallowCopy()
Create a shallow copy of this IRModule.
Map< String, Array< GlobalInfo > > global_infos
Globally static object that are referred by the IR itself.
Definition: module.h:65
Map< GlobalVar, BaseFunc > functions
A map from ids to all global functions.
Definition: module.h:59
static constexpr const bool _type_has_method_shash_reduce
Definition: module.h:238
BaseFunc Lookup(const GlobalVar &var) const
Look up a global function by its variable.
std::unordered_set< String > Imports() const
The set of imported files.
void Update(const IRModule &other)
Update the functions inside this environment by functions in another environment.
DictAttrs GetAttrs() const
Get the metadata attributes.
Definition: module.h:107
Optional< TObjectRef > GetAttr(const std::string &attr_key, TObjectRef default_value) const
Definition: module.h:99
void Add(const GlobalVar &var, const BaseFunc &func, bool update=false)
Add a function to the global environment.
SourceMap source_map
The source map for the module.
Definition: module.h:61
void Update(const GlobalVar &var, const BaseFunc &func)
Update a function in the global environment.
bool ContainGlobalVar(const String &name) const
Check if the global_var_map_ contains a global variable.
TVM_OBJECT_ENABLE_SCRIPT_PRINTER()
DictAttrs attrs
Definition: module.h:63
void UpdateGlobalInfo(const String &name, const Array< GlobalInfo > &info)
Update an array of global infos in the global environment.
void VisitAttrs(AttrVisitor *v)
Definition: module.h:132
Managed reference class to IRModuleNode.
Definition: module.h:249
IRModuleNode * operator->() const
Definition: module.h:270
static constexpr bool _type_is_nullable
Declare whether Ref is nullable.
Definition: module.h:294
IRModule(ObjectPtr< Object > n)
constructor
Definition: module.h:268
static IRModule FromExpr(const RelaxExpr &expr, const Map< GlobalVar, BaseFunc > &global_funcs={})
As for FromExprInContext, but assuming expr is bound to 'main' and no imports.
IRModule(Map< GlobalVar, BaseFunc > functions, SourceMap map={}, DictAttrs attrs=DictAttrs(), Map< String, Array< GlobalInfo >> global_infos={})
constructor
TVM_DEFINE_OBJECT_REF_COW_METHOD(IRModuleNode)
IRModule()
default constructor
Definition: module.h:263
IRModule ShallowCopyIRModule(IRModule mod)
Create a shallow copy of an IRModule.
Managed reference to RelaxExprNode.
Definition: expr.h:405
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:135
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:121
Definition: source_map.h:233
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
A custom smart pointer for Object.
Definition: object.h:363
Base class of all object reference.
Definition: object.h:520
Object * get_mutable() const
Definition: object.h:608
base class of all object containers.
Definition: object.h:172
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to string objects.
Definition: string.h:97
Base expr nodes in TVM.
Function nodes.
GlobalInfo are globally static object that are referred by the IR itself.
IR/AST nodes for the unified type system in TVM.
Runtime Map container types.
constexpr const char * kConstants
Definition: module.h:320
constexpr const char * kConstNameToConstant
All the named runtime::NDArrays accumulated during compilation by external codegen....
Definition: module.h:367
constexpr const char * kModuleName
Name of the module.
Definition: module.h:311
constexpr const char * kExternalMods
All the runtime::Modules accumulated during compilation by external codegen. These modules must be ei...
Definition: module.h:328
constexpr const char * kSystemLibPrefix
A prefix for generating C symbols system lib creation.
Definition: module.h:357
IRModuleFrame IRModule()
The IRModule declaration statement.
Definition: module.h:242
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:290
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
A map from source names to source code.
Runtime String container types.