tvm.ir

Common data structures across all IR variants.

Classes:

Attrs

Attribute node, which is mainly use for defining attributes of operators.

DictAttrs

Dictionary attributes.

EnvFunc

Environment function.

Node

Base class of all IR Nodes.

SourceName(name)

A identifier for a source location.

Span(source_name, line, end_line, column, ...)

Specifies a location in a source program.

SequentialSpan(spans)

A sequence of source spans

Array

Array container of TVM.

Map

Map container of TVM.

BaseExpr

Base class of all the expressions.

GlobalVar(name_hint[, type_annot])

A global variable in the IR.

PrimExpr

Base class of all primitive expressions.

Range(begin[, end, span])

Represent a range in TVM.

RelaxExpr

Base class of all non-primitive expressions.

BaseFunc

Base class of all functions.

CallingConv(value)

Possible kinds of calling conventions.

GlobalInfo

Base node for all global info that can appear in the IR

DummyGlobalInfo()

VDevice([target, vdevice_id, memory_scope])

IRModule([functions, attrs, global_infos])

IRModule that holds functions and type definitions.

Op()

Primitive operator in the IR.

FuncType(arg_types, ret_type)

Function type.

PointerType(element_type[, storage_scope])

PointerType used in the low-level TIR.

PrimType(dtype)

Primitive data type in the low level IR

TupleType(fields)

The type of tuple values.

Type

The base class of all types.

Functions:

make_node(type_key, **kwargs)

Make a new IR node by its type key and fields

assert_structural_equal(lhs, rhs[, ...])

Assert lhs and rhs are structurally equal to each other.

load_json(json_str)

Load tvm object from json_str.

save_json(node)

Save tvm object as json string.

structural_equal(lhs, rhs[, map_free_vars])

Check structural equality of lhs and rhs.

structural_hash(node[, map_free_vars])

Compute structural hash of node

register_intrin_lowering(op_name, target, *)

Register Op lowering function

register_op_attr(op_name, attr_key[, value, ...])

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:

list_field_info()

Get fields information

keys()

Get list of names in the attribute.

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

list_field_info()

Get fields information

Returns:

infos – List of field information

Return type:

list of AttrFieldInfo

keys()

Get list of names in the attribute.

Returns:

keys – List of keys

Return type:

list of str

get_int_tuple(key)

Get a python int tuple of a key

Parameters:

key (str)

Returns:

value

Return type:

Tuple of int

get_int(key)

Get a python int value of a key

Parameters:

key (str)

Returns:

value

Return type:

int

get_str(key)

Get a python int value of a key

Parameters:

key (str)

Returns:

value

Return type:

int

class tvm.ir.DictAttrs

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.

keys()

Get list of names in the attribute.

Returns:

keys – List of keys

Return type:

list of str

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:
  • type_key (str) – The type key of the node.

  • **kwargs (dict) – The fields of the node.

Returns:

node – The corresponding IR Node

Return type:

Node

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("IntImm", dtype="int32", value=10, span=None)
assert isinstance(x, tvm.tir.IntImm)
assert x.value == 10
class tvm.ir.EnvFunc

Environment function.

This is a global function object that can be serialized by its name.

Methods:

get(name)

Get a static env function

static get(name)

Get a static env function

Parameters:

name (str) – The name of the 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:
  • lhs (Object) – The left operand.

  • rhs (Object) – The left operand.

  • map_free_vars (bool) – Whether or not shall we map free vars that does not bound to any definitions as equal to each other.

:raises ValueError : if assertion does not hold.:

See also

structural_equal

tvm.ir.load_json(json_str) Object

Load tvm object from json_str.

Parameters:

json_str (str) – The json string

Returns:

node – The loaded tvm node.

Return type:

Object

tvm.ir.save_json(node) str

Save tvm object as json string.

Parameters:

node (Object) – A TVM object to be saved.

Returns:

json_str – Saved json string.

Return type:

str

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(tir::Var, relax::Var) are graph nodes.

A var-type node(e.g. tir::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:
  • lhs (Object) – The left operand.

  • rhs (Object) – The left operand.

  • map_free_vars (bool) – Whether free variables (i.e. variables without a definition site) should be mapped as equal to each other.

Returns:

result – The comparison result.

Return type:

bool

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 occurence 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:
  • node (Object) – The input to be hashed.

  • map_free_vars (bool) – If map_free_vars is set to true, we will hash free variables by the order of their occurrences. Otherwise, we will hash by their in-memory pointer address.

Returns:

result – The hash result

Return type:

int

See also

structrual_equal

class tvm.ir.Array

Array container of TVM.

You do not need to create Array explicitly. Normally python list and tuple will be converted automatically to Array during tvm function call. You may get Array in return values of TVM function call.

class tvm.ir.Map

Map container of TVM.

You do not need to create Map explicitly. Normally python dict will be converted automatically to Map during tvm function call. You can use convert to create a dict[Object-> Object] into a Map

Methods:

items()

Get the items from the map

get(key[, default])

Get an element with a default value.

items()

Get the items from the map

get(key, default=None)

Get an element with a default value.

Parameters:
  • key (object) – The attribute key.

  • default (object) – The default object.

Returns:

value – The result value.

Return type:

object

class tvm.ir.BaseExpr

Base class of all the expressions.

class tvm.ir.GlobalVar(name_hint: str, type_annot: Type | None = None)

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

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:
  • begin (PrimExpr) – The begin value of the range when end is None. Otherwise it is the length of the range.

  • end (Optional[PrimExpr]) – The end value of the range.

  • span (Optional[Span]) – The location of this node in the source code.

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.

static from_min_extent(min_value: PrimExpr, extent: PrimExpr, span: Span | None = None) Range

Construct a Range by min and extent.

This constructs a range in [min_value, min_value + extent)

Parameters:
  • min_value (PrimExpr) – The minimum value of the range.

  • extent (PrimExpr) – The extent of the range.

  • span (Optional[Span]) – The location of this node in the source code.

Returns:

rng – The constructed range.

Return type:

Range

class tvm.ir.RelaxExpr

Base class of all non-primitive expressions.

Attributes:

checked_type

Get the checked type of tvm.relax.Expr.

struct_info

Get the struct info field

property checked_type

Get the checked type of tvm.relax.Expr.

Returns:

checked_type – The checked type.

Return type:

tvm.ir.Type

property struct_info: StructInfo | None

Get the struct info field

Returns:

struct_info – The struct info if available.

Return type:

tvm.relax.StructInfo

class tvm.ir.BaseFunc

Base class of all functions.

Attributes:

attrs

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.

Parameters:
  • attr_key_or_dict (Union[str, dict]) – The attribute key to use or a dict containing multiple key value pairs.

  • attr_value (Object) – The new attribute value.

Returns:

func – A new copy of the function

Return type:

BaseFunc

with_attrs(attr_map: DictAttrs | Dict[str, Object]) BaseFunc

Copy the IRModule and add the given attribute map to it. :param attr_map: The attribute map :type attr_map: Union[DictAttrs, Dict[str, Object]]

Returns:

func – A new copy of the function

Return type:

BaseFunc

without_attr(attr_key: str) BaseFunc

Create a new copy of the function with an attribute without provided key.

Parameters:

attr_key (str) – The attribute key to delete from the attrubte pairs.

Returns:

func – A new copy of the function

Return type:

BaseFunc

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.VDevice(target=None, vdevice_id: int = 0, memory_scope: str = 'global')
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:

functions_items()

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.

get_global_vars()

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.

Returns:

items – The functions items.

Return type:

List[Tuple[GlobalVar, Function]]

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:
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.

Parameters:

name (str) – The name of the global variable.

Returns:

global_var – The global variable mapped to name.

Return type:

GlobalVar

Raises:

tvm.error.TVMError if we cannot find corresponding global var.

get_global_vars()

Collect all global vars defined in this module.

Returns:

global_vars – An array of global vars.

Return type:

Array[GlobalVar]

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.

Parameters:

replacements (Dict[Union[str, _expr.GlobalVar], Union[str, _expr.GlobalVar]]) – A dictionary where each key is a GlobalVar to be replaced, and the corresponding value is the GlobalVar with which to replace it.

Returns:

The updated module

Return type:

IRModule

static from_expr(expr, functions=None)

Construct a module from a standalone expression.

Parameters:
  • expr (RelaxExpr) – The starting expression

  • global_funcs (Optional[dict]) – Map of global vars to function definitions

Returns:

mod – A module containing the passed definitions, where expr is set as the entry point (wrapped in a function if necessary)

Return type:

Module

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.

Parameters:
  • attr_key (str) – The attribute key.

  • attr_value (Object) – The new attribute value.

Returns:

mod – A new copy of the IRModule with the attribute

Return type:

IRModule

without_attr(attr_key: str) IRModule

Copy the IRModule and remove an attribute key and its associated value. :param attr_key: The attribute key. :type attr_key: str

Returns:

mod – A new copy of the IRModule without the attribute

Return type:

IRModule

with_attrs(attr_map: DictAttrs | Dict[str, Object]) IRModule

Copy the IRModule and add the given attribute map to it. :param attr_map: The attribute map :type attr_map: Union[DictAttrs, Dict[str, Object]]

Returns:

mod – A new copy of the IRModule with the attribute

Return type:

IRModule

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_num_inputs(n)

Set the support level of op.

set_attrs_type_key(key)

Set the attribute type key of op.

list_op_names()

List all the op names in the op registry.

static get(op_name)

Get the Op for a given name

Parameters:

op_name (str) – The operator name

Returns:

op – The op of the corresponding name

Return type:

Op

get_attr(attr_name)

Get additional attribute about the operator.

Parameters:

attr_name (str) – The attribute name.

Returns:

value – The attribute value

Return type:

object

has_attr(attr_name)

Check whether the operator has additional attribute.

Parameters:

attr_name (str) – The attribute name.

Returns:

value – Whether the operator has additional attribute

Return type:

bool

set_attr(attr_name, value, plevel=10)

Set attribute about the operator.

Parameters:
  • attr_name (str) – The attribute name

  • value (object) – The attribute value

  • plevel (int) – The priority level

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.

Parameters:
  • name (str) – The argument name.

  • type (str) – The argument type.

  • description (str) – The argument description.

set_support_level(level)

Set the support level of op.

Parameters:

level (int) – The support level.

set_num_inputs(n)

Set the support level of op.

Parameters:

n (int) – The input number.

set_attrs_type_key(key)

Set the attribute type key of op.

Parameters:

key (str) – The type key.

static list_op_names()

List all the op names in the op registry.

Returns:

value – The registered op names

Return type:

List[str]

tvm.ir.register_intrin_lowering(op_name, target, *, f=None, level=10)

Register Op lowering function

Parameters:
  • op_name (str) – The op name

  • target (str) – The target string for given intrinsic lowering function

  • f (function, optional) – The function to be registered.

  • level (int) – The priority level

Returns:

fregister – Register op lowering function if f is not specified.

Return type:

function

tvm.ir.register_op_attr(op_name, attr_key, value=None, level=10)

Register an operator property of an operator by name.

Parameters:
  • op_name (str) – The name of operator

  • attr_key (str) – The attribute name.

  • value (object, optional) – The value to set

  • level (int, optional) – The priority level

Returns:

fregister – Register function if value is not specified.

Return type:

function

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:
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

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.