24 #ifndef TVM_IR_MODULE_H_ 25 #define TVM_IR_MODULE_H_ 37 #include <unordered_map> 38 #include <unordered_set> 106 template <
typename TObjectRef>
108 const std::string& attr_key,
110 return attrs.
GetAttr(attr_key, default_value);
113 template <
typename TObjectRef>
142 v->Visit(
"functions", &functions);
143 v->Visit(
"type_definitions", &type_definitions);
144 v->Visit(
"global_var_map_", &global_var_map_);
145 v->Visit(
"global_type_var_map_", &global_type_var_map_);
146 v->Visit(
"source_map", &source_map);
147 v->Visit(
"attrs", &attrs);
191 bool update =
false);
329 TVM_DLL std::unordered_set<String>
Imports()
const;
360 std::unordered_set<String> import_set = {},
SourceMap map = {},
372 auto* ptr = get_mutable();
373 ICHECK(ptr !=
nullptr);
402 static std::pair<IRModule, GlobalVar> FromExprInContext(
405 std::unordered_set<String> import_set = {});
434 static constexpr
bool _type_is_nullable =
false;
518 #endif // TVM_IR_MODULE_H_ static constexpr const bool _type_has_method_shash_reduce
Definition: module.h:335
Map< String, GlobalVar > global_var_map_
A map from string names to global variables that ensures global uniqueness.
Definition: module.h:70
constexpr const char * kExecutor
Executor targeted by the module.
Definition: module.h:460
void VisitAttrs(AttrVisitor *v)
Definition: module.h:141
A custom smart pointer for Object.
Definition: object.h:358
TVM_DECLARE_FINAL_OBJECT_INFO(IRModuleNode, Object)
void ImportFromStd(const String &path)
Import Relay code from the file at path, relative to the standard library.
void AddUnchecked(const GlobalVar &var, const BaseFunc &func)
Add a function to the global environment.
constexpr const char * kConstantMemoryPools
constant memory pools of the module
Definition: module.h:487
Runtime String container types.
SourceMap source_map
The source map for the module.
Definition: module.h:63
void AddTypeDef(const GlobalTypeVar &var, const TypeData &type, bool update=false)
Add a type-level definition to the global environment.
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:124
IRModuleNode()
Definition: module.h:139
TypeData LookupTypeDef(const GlobalTypeVar &var) const
Look up a global type definition by its variable.
void Add(const GlobalVar &var, const BaseFunc &func, bool update=false)
Add a function to the global environment.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Optional< TObjectRef > GetAttr(const std::string &attr_key, TObjectRef default_value) const
Definition: module.h:114
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:110
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
constexpr const char * kConstNameToConstant
All the named runtime::NDArrays accumulated during compilation by external codegen. Generally the associated runtime::Module will indicate it requires bindings for these names, and during module initialization these bindings will be recovered from a ConstLoaderModule. See also kConstantsArray above, which is the analog for PrimFuncs.
Definition: module.h:514
constexpr const char * kExternalMods
All the runtime::Modules accumulated during compilation by external codegen. These modules must be ei...
Definition: module.h:504
GlobalTypeVar GetGlobalTypeVar(const String &str) const
Look up a global function by its name.
Managed reference to GlobalTypeVarNode.
Definition: type.h:332
A map from source names to source code.
bool ContainGlobalTypeVar(const String &name) const
Check if the global_type_var_map_ contains a global type variable.
constexpr const char * kModuleName
Name of the module.
Definition: module.h:451
Managed reference to DictAttrsNode.
Definition: attrs.h:227
base class of all object containers.
Definition: object.h:167
Constructor GetConstructor(const String &adt, const String &cons) const
Find constructor of ADT using name.
Managed reference to ConstructorNode.
Definition: adt.h:88
Map< String, GlobalTypeVar > global_type_var_map_
A map from string names to global type variables (ADT names) that ensures global uniqueness.
Definition: module.h:75
static constexpr const char * _type_key
Definition: module.h:333
bool ContainGlobalVar(const String &name) const
Check if the global_var_map_ contains a global variable.
void AddTypeDefUnchecked(const GlobalTypeVar &var, const TypeData &type, bool update=false)
Add a type-level definition to the global environment.
Definition: source_map.h:189
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
std::unordered_map< int32_t, Constructor > constructor_tag_map_
A map from constructor tags to constructor objects for convenient access.
Definition: module.h:80
GlobalVar GetGlobalVar(const String &str) const
Lookup a global function by its variable.
IR/AST nodes for the unified type system in TVM.
Map< GlobalVar, BaseFunc > functions
A map from ids to all global functions.
Definition: module.h:59
void Remove(const GlobalVar &var)
Remove a function from the global environment.
Optional< TObjectRef > GetAttr(const std::string &attr_key, Optional< TObjectRef > default_value=Optional< TObjectRef >(nullptr)) const
Get a function attribute.
Definition: attrs.h:259
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Constructor LookupTag(const int32_t tag)
Look up a constructor by its tag.
Array< GlobalTypeVar > GetGlobalTypeVars() const
Collect all global type vars defined in this module.
Managed reference to GlobalVarNode.
Definition: expr.h:477
IRModuleNode * operator->() const
Definition: module.h:371
IRModule(ObjectPtr< Object > n)
constructor
Definition: module.h:369
Reference to string objects.
Definition: string.h:98
TVM_OBJECT_ENABLE_SCRIPT_PRINTER()
Managed reference to RelayExprNode.
Definition: expr.h:433
friend class IRModule
Definition: module.h:341
IRModule ShallowCopy()
Create a shallow copy of this IRModule.
Algebraic data type definitions.
Array< GlobalVar > GetGlobalVars() const
Collect all global vars defined in this module.
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
Base class of all object reference.
Definition: object.h:511
#define TVM_DEFINE_OBJECT_REF_COW_METHOD(ObjectName)
Define CopyOnWrite function in an ObjectRef.
Definition: object.h:785
constexpr const char * kWorkspaceMemoryPools
workspace memory pools of the module
Definition: module.h:478
void UpdateTypeDef(const GlobalTypeVar &var, const TypeData &type)
Update a type definition in the global environment.
void Import(const String &path)
Import Relay code from the file at path.
Managed reference class to IRModuleNode.
Definition: module.h:348
Stores all data for an Algebraic Data Type (ADT).
Definition: adt.h:149
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics, which means map is mutable but copy will happen when array is referenced in more than two places.
Definition: map.h:1271
Runtime Map container types.
Managed reference to BaseFuncNode.
Definition: function.h:143
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
DictAttrs attrs
Definition: module.h:65
constexpr const char * kConstants
Definition: module.h:496
bool SEqualReduce(const IRModuleNode *other, SEqualReducer equal) const
std::unordered_set< String > Imports() const
The set of imported files.
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:290
std::unordered_set< String > import_set_
The files previously imported, required to ensure importing is idempotent for each module...
Definition: module.h:85
IRModule that holds functions and type definitions.
Definition: module.h:56
void SHashReduce(SHashReducer hash_reduce) const
constexpr const char * kRuntime
Runtime target of the module.
Definition: module.h:469
BaseFunc Lookup(const GlobalVar &var) const
Look up a global function by its variable.
bool HasNonzeroAttr(const std::string &attr_key) const
Check whether the module has an non-zero integer attr.
Definition: module.h:137
IRModuleFrame IRModule()
The IRModule declaration statement.
void Update(const GlobalVar &var, const BaseFunc &func)
Update a function in the global environment.
IRModule()
default constructor
Definition: module.h:364
Map< GlobalTypeVar, TypeData > type_definitions
A map from global type vars to ADT type data.
Definition: module.h:61
Optional< TObjectRef > GetAttr(const std::string &attr_key, Optional< TObjectRef > default_value=Optional< TObjectRef >(nullptr)) const
Get a module attribute.
Definition: module.h:107
static constexpr const bool _type_has_method_sequal_reduce
Definition: module.h:334
bool HasNonzeroAttr(const std::string &attr_key) const
Check whether the function has an non-zero integer attr.
Definition: attrs.h:298