tvm.ir
Common data structures across all IR variants.
Classes:
Attribute node, which is mainly use for defining attributes of operators. |
|
|
Dictionary attributes. |
|
Environment function. |
Base class of all IR Nodes. |
|
|
A identifier for a source location. |
|
Specifies a location in a source program. |
|
A sequence of source spans |
|
Array container that represents a sequence of values in the FFI. |
|
Map container. |
|
Base class of all the expressions. |
|
A global variable in the IR. |
|
Base class of all primitive expressions. |
|
Represent a range in TVM. |
|
Base class of all non-primitive expressions. |
|
Base class of all functions. |
|
Possible kinds of calling conventions. |
Base node for all global info that can appear in the IR |
|
|
|
|
IRModule that holds functions and type definitions. |
|
Primitive operator in the IR. |
|
Function type. |
|
PointerType used in the low-level TIR. |
|
Primitive data type in the low level IR |
|
The type of tuple values. |
|
The base class of all types. |
Functions:
|
Make a new IR node by its type key and fields |
|
Assert lhs and rhs are structurally equal to each other. |
|
Load tvm object from json_str. |
|
Save tvm object as json string. |
|
Check structural equality of lhs and rhs. |
|
Compute structural hash of node |
|
Register Op lowering function |
|
Register an operator property of an operator by name. |
- class tvm.ir.Attrs
Attribute node, which is mainly use for defining attributes of operators.
Used by function registered in python side, such as compute, schedule and alter_layout. Attrs is passed as the first argument to these functions.
Methods:
get_int_tuple(key)Get a python int tuple of a key
get_int(key)Get a python int value of a key
get_str(key)Get a python int value of a key
- get_int_tuple(key)
Get a python int tuple of a key
- class tvm.ir.DictAttrs(__dict__)
Dictionary attributes.
Methods:
keys()Get list of names in the attribute.
get(key[, default])Get an element with a default value.
items()Get items from the map.
- get(key, default=None)
Get an element with a default value.
- items()
Get items from the map.
- tvm.ir.make_node(type_key, **kwargs)
Make a new IR node by its type key and fields
- Parameters:
- Returns:
node – The corresponding IR Node
- Return type:
Note
If the created node is instance of AttrsNode, then the creator function will also run bound checks and default value setup as supported by Attrs.
Example
The following code constructs a IntImm object
x = tvm.ir.make_node("ir.IntImm", dtype="int32", value=10, span=None) assert isinstance(x, tvm.tirx.IntImm) assert x.value == 10
- class tvm.ir.EnvFunc(name, func)
Environment function.
This is a global function object that can be serialized by its name.
Methods:
get(name)Get a static env function
- class tvm.ir.Node
Base class of all IR Nodes.
- class tvm.ir.SourceName(name)
A identifier for a source location.
- Parameters:
name (str) – The name of the source.
- class tvm.ir.Span(source_name, line, end_line, column, end_column)
Specifies a location in a source program.
- Parameters:
source (SourceName) – The source name.
lineno (int) – The line number.
col_offset (int) – The column offset of the location.
- class tvm.ir.SequentialSpan(spans)
A sequence of source spans
This span is specific for an expression, which is from multiple expressions after an IR transform.
- Parameters:
spans (Array) – The array of spans.
- tvm.ir.assert_structural_equal(lhs, rhs, map_free_vars=False)
Assert lhs and rhs are structurally equal to each other.
- Parameters:
:raises ValueError : if assertion does not hold.:
See also
- tvm.ir.structural_equal(lhs, rhs, map_free_vars=False)
Check structural equality of lhs and rhs.
The structural equality is recursively defined in the DAG of IRNodes. There are two kinds of nodes:
Graph node: a graph node in lhs can only be mapped as equal to one and only one graph node in rhs.
Normal node: equality is recursively defined without the restriction of graph nodes.
Vars(tirx::Var, relax::Var) are graph nodes.
A var-type node(e.g. tirx::Var) can be mapped as equal to another var with the same type if one of the following condition holds:
They appear in a same definition point(e.g. function argument).
They points to the same VarNode via the same_as relation.
They appear in a same usage point, and map_free_vars is set to be True.
The rules for var are used to remap variables occurs in function arguments and let-bindings.
- Parameters:
- Returns:
result – The comparison result.
- Return type:
See also
structural_hash,assert_strucural_equal
- tvm.ir.structural_hash(node, map_free_vars=False)
Compute structural hash of node
The structural hash value is recursively defined in the DAG of IRNodes. There are two kinds of nodes:
Normal node: the hash value is defined by its content and type only.
Graph node: each graph node will be assigned a unique index ordered by the first occurrence during the visit. The hash value of a graph node is combined from the hash values of its contents and the index.
structural_hash is made to be concistent with structural_equal. If two nodes are structurally equal to each other, then their structural hash (with the same map_free_vars option) should be equal to each other as well.
If the structural hash of two nodes equals to each other, then it is highly likely(except for rare hash value collison cases) that the two nodes are structurally equal to each other.
- Parameters:
- Returns:
result – The hash result
- Return type:
See also
structrual_equal
- class tvm.ir.Array(input_list: Iterable[T])
Array container that represents a sequence of values in the FFI.
tvm_ffi.convert()will map python list/tuple to this class.- Parameters:
input_list – The list of values to be stored in the array.
Examples
import tvm_ffi a = tvm_ffi.Array([1, 2, 3]) assert tuple(a) == (1, 2, 3)
Notes
For structural equality and hashing, use
structural_equalandstructural_hashAPIs.See also
tvm_ffi.convert()
- class tvm.ir.Map(input_dict: Mapping[K, V])
Map container.
tvm_ffi.convert()will map python dict to this class.- Parameters:
input_dict – The dictionary of values to be stored in the map.
Examples
import tvm_ffi amap = tvm_ffi.Map({"a": 1, "b": 2}) assert len(amap) == 2 assert amap["a"] == 1 assert amap["b"] == 2
Notes
For structural equality and hashing, use
structural_equalandstructural_hashAPIs.See also
tvm_ffi.convert()Methods:
keys()Return a dynamic view of the map's keys.
values()Return a dynamic view of the map's values.
items()Get the items from the map.
get()Get an element with a default value.
- keys() KeysView[K]
Return a dynamic view of the map’s keys.
- values() ValuesView[V]
Return a dynamic view of the map’s values.
- items() ItemsView[K, V]
Get the items from the map.
- class tvm.ir.BaseExpr(span=<object object>)
Base class of all the expressions.
- class tvm.ir.GlobalVar(name_hint: str)
A global variable in the IR.
GlobalVar is used to refer to the global functions stored in the IRModule.
- Parameters:
name_hint (str) – The name of the variable.
- class tvm.ir.PrimExpr(dtype, span=<object object>)
Base class of all primitive expressions.
PrimExpr is used in the low-level code optimizations and integer analysis.
- class tvm.ir.Range(begin: PrimExpr, end: PrimExpr | None = None, span: Span | None = None)
Represent a range in TVM.
You do not need to create a Range explicitly. Python lists and tuples will be converted automatically to a Range in API functions.
- Parameters:
Note
The constructor creates the range [begin, end) if the end argument is not None. Otherwise, it creates [0, begin).
Methods:
from_min_extent(min_value, extent[, span])Construct a Range by min and extent.
- class tvm.ir.RelaxExpr(struct_info_, span=<object object>)
Base class of all non-primitive expressions.
Attributes:
Get the struct info field
- property struct_info: StructInfo | None
Get the struct info field
- Returns:
struct_info – The struct info if available.
- Return type:
- class tvm.ir.BaseFunc(struct_info_, attrs, span=<object object>)
Base class of all functions.
Attributes:
Return the attrs member of the function.
Methods:
with_attr(attr_key_or_dict[, attr_value])Create a new copy of the function and update the attribute.
with_attrs(attr_map)Copy the IRModule and add the given attribute map to it.
without_attr(attr_key)Create a new copy of the function with an attribute without provided key.
- property attrs
Return the attrs member of the function.
- with_attr(attr_key_or_dict, attr_value=None) BaseFunc
Create a new copy of the function and update the attribute.
- class tvm.ir.CallingConv(value)
Possible kinds of calling conventions.
- class tvm.ir.GlobalInfo
Base node for all global info that can appear in the IR
Methods:
same_as(other)Overload with structural equality.
- same_as(other)
Overload with structural equality.
- class tvm.ir.DummyGlobalInfo
- class tvm.ir.IRModule(functions=None, attrs=None, global_infos=None)
IRModule that holds functions and type definitions.
IRModule is the basic unit for all IR transformations across the stack.
- Parameters:
functions (Optional[dict].) – Map of global var to BaseFunc
Methods:
Get items in self.functions.items() in alphabetical order.
update(other)Insert functions in another Module to current one.
update_func(var, func)Update the function corresponding to a global variable in the module.
update_global_info(name, global_info)Update global info in the module
get_global_var(name)Get a global variable in the function by name.
Collect all global vars defined in this module.
replace_global_vars(replacements)Replace GlobalVar instances within the module
from_expr(expr[, functions])Construct a module from a standalone expression.
get_attr(attr_key)Get the IRModule attribute.
with_attr(attr_key, attr_value)Copy the IRModule and add an attribute to it.
without_attr(attr_key)Copy the IRModule and remove an attribute key and its associated value.
with_attrs(attr_map)Copy the IRModule and add the given attribute map to it.
- functions_items()
Get items in self.functions.items() in alphabetical order.
- update(other)
Insert functions in another Module to current one.
- Parameters:
other (IRModule) – The module to merge into the current Module.
- update_func(var, func)
Update the function corresponding to a global variable in the module.
- Parameters:
var (GlobalVar) – The global variable.
func (tvm.ir.BaseFunc) – The function to be inserted.
- update_global_info(name, global_info)
Update global info in the module
- Parameters:
name (str) – The name for the global info.
global_info (List[GlobalInfo]) – The global info to be updated.
- get_global_var(name)
Get a global variable in the function by name.
- get_global_vars()
Collect all global vars defined in this module.
- replace_global_vars(replacements: dict[str | GlobalVar, str | GlobalVar]) IRModule
Replace GlobalVar instances within the module
Replace GlobalVars within the IRModule. Since the IRModule may contain internal references to a GlobalVar, either in TIR or in Relax, this method should be used whenever replacing or renaming a GlobalVar.
- static from_expr(expr, functions=None)
Construct a module from a standalone expression.
- get_attr(attr_key)
Get the IRModule attribute.
- Parameters:
attr_key (str) – The attribute key.
- Returns:
attr_value – Attribute value
- Return type:
Any
- with_attr(attr_key, attr_value)
Copy the IRModule and add an attribute to it.
- class tvm.ir.Op
Primitive operator in the IR.
Methods:
get(op_name)Get the Op for a given name
get_attr(attr_name)Get additional attribute about the operator.
has_attr(attr_name)Check whether the operator has additional attribute.
set_attr(attr_name, value[, plevel])Set attribute about the operator.
reset_attr(attr_name)Reset attribute about the operator.
add_argument(name, type, description)Add arguments information to the function.
set_support_level(level)Set the support level of op.
Set the support level of op.
set_attrs_type_key(key)Set the attribute type key of op.
List all the op names in the op registry.
- static get(op_name)
Get the Op for a given name
- get_attr(attr_name)
Get additional attribute about the operator.
- has_attr(attr_name)
Check whether the operator has additional attribute.
- set_attr(attr_name, value, plevel=10)
Set attribute about the operator.
- reset_attr(attr_name)
Reset attribute about the operator.
- Parameters:
attr_name (str) – The attribute name
- add_argument(name, type, description)
Add arguments information to the function.
- tvm.ir.register_intrin_lowering(op_name, target, *, f=None, level=10)
Register Op lowering function
- tvm.ir.register_op_attr(op_name, attr_key, value=None, level=10)
Register an operator property of an operator by name.
- class tvm.ir.FuncType(arg_types, ret_type)
Function type.
A function type consists of a list of type parameters to enable the definition of generic functions, a set of type constraints which we omit for the time being, a sequence of argument types, and a return type.
- Parameters:
arg_types (List[tvm.ir.Type]) – The argument types
ret_type (tvm.ir.Type) – The return type.
- class tvm.ir.PointerType(element_type, storage_scope='')
PointerType used in the low-level TIR.
- Parameters:
element_type (tvm.ir.Type) – The type of pointer’s element.
storage_scope (str) – The storage scope into which the pointer addresses.
- class tvm.ir.PrimType(dtype)
Primitive data type in the low level IR
- Parameters:
dtype (str) – The runtime data type relates to the primtype.
- class tvm.ir.TupleType(fields)
The type of tuple values.
- Parameters:
fields (List[Type]) – The fields in the tuple
- class tvm.ir.Type(span=<object object>)
The base class of all types.
Methods:
same_as(other)Compares two TVM types by referential equality.
- same_as(other)
Compares two TVM types by referential equality.
tvm.ir.diagnostics
The diagnostic interface to TVM, used for reporting and rendering diagnostic information by the compiler. This module exposes three key abstractions: a Diagnostic, the DiagnosticContext, and the DiagnosticRenderer.
- tvm.ir.diagnostics.get_global_func(name: str, allow_missing: bool = False) Function | None
Get a global function by name.
- Parameters:
name – The name of the global function
allow_missing – Whether allow missing function or raise an error.
- Returns:
The function to be returned,
Noneif function is missing.- Return type:
func
Examples
import tvm_ffi @tvm_ffi.register_global_func("demo.echo") def echo(x): return x f = tvm_ffi.get_global_func("demo.echo") assert f(123) == 123
See also
tvm_ffi.register_global_func()
- tvm.ir.diagnostics.register_global_func(func_name: str | Callable[[...], Any], f: Callable[[...], Any] | None = None, override: bool = False) Any
Register global function.
- Parameters:
func_name – The function name
f – The function to be registered.
override – Whether override existing entry.
- Returns:
Register function if f is not specified.
- Return type:
fregister
Examples
import tvm_ffi # we can use decorator to register a function @tvm_ffi.register_global_func("mytest.echo") def echo(x): return x # After registering, we can get the function by its name f = tvm_ffi.get_global_func("mytest.echo") assert f(1) == 1 # we can also directly register a function tvm_ffi.register_global_func("mytest.add_one", lambda x: x + 1) f = tvm_ffi.get_global_func("mytest.add_one") assert f(1) == 2
See also
tvm_ffi.get_global_func(),tvm_ffi.remove_global_func()
- class tvm.ir.diagnostics.Object
Base class of all TVM FFI objects.
This is the root Python type for objects backed by the TVM FFI runtime. Each instance references a handle to a C++ runtime object. Python subclasses typically correspond to C++ runtime types and are registered via
tvm_ffi.register_object().Notes
Equality of two
Objectinstances uses underlying handle identity unless an overridden implementation is provided on the concrete type. Usesame_as()to check whether two references point to the same underlying object.Subclasses that omit
__slots__get__slots__ = ()injected automatically by the metaclass. To allow a per-instance__dict__, declare__slots__ = ("__dict__",)explicitly in the class body.Most users interact with subclasses (e.g.
Tensor,Function) rather thanObjectdirectly.
Examples
Constructing objects is typically performed by Python wrappers that call into registered constructors on the FFI side.
import tvm_ffi.testing # Acquire a testing object constructed through FFI obj = tvm_ffi.testing.create_object("testing.TestObjectBase", v_i64=12) assert isinstance(obj, tvm_ffi.Object) assert obj.same_as(obj)
Subclasses can declare explicit slots when needed.
@tvm_ffi.register_object("my.MyObject") class MyObject(tvm_ffi.Object): __slots__ = ()
Subclasses that need a per-instance
__dict__(e.g. for attribute caching) can opt in explicitly.@tvm_ffi.register_object("my.MyDynObject") class MyDynObject(tvm_ffi.Object): __slots__ = ("__dict__",)
- same_as(other: object) bool
Return
Trueif both references point to the same object.This checks identity of the underlying FFI handle rather than performing a structural, value-based comparison.
Examples
import tvm_ffi.testing x = tvm_ffi.testing.create_object("testing.TestObjectBase") y = x z = tvm_ffi.testing.create_object("testing.TestObjectBase") assert x.same_as(y) assert not x.same_as(z)
- tvm.ir.diagnostics.get_renderer()
Get the diagnostic renderer.
- Returns:
renderer
- Return type:
- class tvm.ir.diagnostics.DiagnosticLevel(value)
The diagnostic level, see diagnostic.h for more details.
- class tvm.ir.diagnostics.Diagnostic(level, span, message)
A single diagnostic object from TVM.
- class tvm.ir.diagnostics.DiagnosticRenderer(render_func)
A diagnostic renderer, which given a diagnostic context produces a “rendered” form of the diagnostics for either human or computer consumption.