Core TIRx IR

Contents

Core TIRx IR#

This page lists the core IR nodes and scalar operations in the top-level namespace. Layouts, execution scopes, visitors, compilation helpers, and tile-dispatch extensions are documented on their focused pages and excluded here so the same objects are not expanded twice.

Namespace for Tensor-level IR

tvm.tirx.Buffer#

alias of Var

class tvm.tirx.BufferAccessKind(value)#

Buffer access modes accepted by buffer_access_ptr().

class tvm.tirx.BufferType(dtype, storage_scope, shape, strides, elem_offset, data_alignment, offset_factor, layout, allocated_addr, span=<MISSING>)#

The structural type carried by an ordinary buffer variable.

tvm.tirx.buffer_data(buffer)#

Project the physical pointer associated with a buffer variable.

tvm.tirx.buffer_data_pointer_type(buffer)#

Return the pointer type produced by buffer_data().

tvm.tirx.is_buffer_var(value) bool#

Return whether value is an ordinary tirx.Var carrying BufferType.

Use this predicate instead of isinstance(value, Buffer). Buffer is a source-compatibility alias for tvm.ir.Var and therefore does not discriminate buffer variables from scalar or pointer variables.

class tvm.tirx.FloatImm(dtype: str | PrimType, value: float, span: Span | None = None)#

Float constant.

Parameters:
  • dtype (str) – The data type

  • value (float) – The constant value.

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

class tvm.tirx.IntImm(dtype: str | PrimType, value: int, span: Span | None = None)#

Int constant.

Parameters:
  • dtype (str) – The data type

  • value (int) – The constant value.

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

class tvm.tirx.StringImm(value: str, span: Span | None = None)#

String constant.

Parameters:
  • value (str) – The value of the function.

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

class tvm.tirx.Cast(dtype: str | PrimType, value, span: Span | None = None)#

Cast expression.

Parameters:
  • dtype (str) – The data type

  • value (Expr) – The value of the function.

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

class tvm.tirx.Add(a: Expr, b: Expr, span: Span | None = None)#

Add node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.Sub(a: Expr, b: Expr, span: Span | None = None)#

Sub node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.Mul(a: Expr, b: Expr, span: Span | None = None)#

Mul node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.Div(a: Expr, b: Expr, span: Span | None = None)#

Div node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.Mod(a: Expr, b: Expr, span: Span | None = None)#

Mod node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.FloorDiv(a: Expr, b: Expr, span: Span | None = None)#

FloorDiv node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.FloorMod(a: Expr, b: Expr, span: Span | None = None)#

FloorMod node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.Min(a: Expr, b: Expr, span: Span | None = None)#

Min node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.Max(a: Expr, b: Expr, span: Span | None = None)#

Max node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.EQ(a: Expr, b: Expr, span: Span | None = None)#

EQ node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.NE(a: Expr, b: Expr, span: Span | None = None)#

NE node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.LT(a: Expr, b: Expr, span: Span | None = None)#

LT node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.LE(a: Expr, b: Expr, span: Span | None = None)#

LE node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.GT(a: Expr, b: Expr, span: Span | None = None)#

GT node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.GE(a: Expr, b: Expr, span: Span | None = None)#

GE node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.And(a: Expr, b: Expr, span: Span | None = None)#

And node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.Or(a: Expr, b: Expr, span: Span | None = None)#

Or node.

Parameters:
  • a (Expr) – The left hand operand.

  • b (Expr) – The right hand operand.

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

class tvm.tirx.Not(a: Expr, span: Span | None = None)#

Not node.

Parameters:
  • a (Expr) – The input value

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

class tvm.tirx.Select(condition: Expr, true_value: Expr, false_value: Expr, span: Span | None = None)#

Select node.

Note

Select may compute both true_value and false_value. Use tvm.tirx.if_then_else instead if you want to get a conditional expression that only evaluates the correct branch.

Parameters:
  • condition (Expr) – The condition expression.

  • true_value (Expr) – The value to take when condition is true.

  • false_value (Expr) – The value to take when condition is false.

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

tvm.tirx.BufferLoad(buffer: Var, indices: list[Expr], span: Span | None = None) TensorLoad#

Construct a validated buffer load.

Parameters:
  • buffer (Buffer) – The buffer to be loaded.

  • indices (List[Expr]) – The buffer indices to load values from.

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

class tvm.tirx.Ramp(base: Expr, stride: Expr, lanes: Expr, span: Span | None = None)#

Ramp node.

Parameters:
  • base (Expr) – The base expression.

  • stride (Expr) – The stride of the ramp.

  • lanes (Expr) – The lanes of the expression.

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

class tvm.tirx.Broadcast(value: Expr, lanes: Expr, span: Span | None = None)#

Broadcast node.

Parameters:
  • value (Expr) – The value of the expression.

  • lanes (Expr) – The lanes of the expression.

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

class tvm.tirx.Shuffle(vectors: list[Expr], indices: list[Expr], span: Span | None = None)#

Shuffle node.

Parameters:
  • vectors (List[Expr]) – The vectors

  • indices (List[Expr]) – The indices

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

class tvm.tirx.CallEffectKind#

Possible kinds of tirx.Call effects.

class tvm.tirx.Let(var: Var, value: Expr, body: Expr, span: Span | None = None)#

Let node.

Parameters:
  • var (tirx.Var) – The variable in the binding.

  • value (Expr) – The value in to be bound.

  • body (Expr) – The body expression.

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

class tvm.tirx.IterVar(dom: Range, var: Var | str, iter_type: int, thread_tag: str = '', span: Span | None = None)#

Represent iteration variable.

IterVar represents axis iterations in the computation.

Parameters:
  • dom (Range) – The domain of the iteration.

  • var (Union[tirx.Var, str]) – The internal variable that is used for iteration.

  • iter_type (int) – The iteration type.

  • thread_tag (str) – The thread type tag.

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

See also

te.thread_axis

Create thread axis IterVar.

te.reduce_axis

Create reduce axis IterVar.

expr_ty() PrimType#

Compile-time type of the iteration variable.

class tvm.tirx.Stmt(span)#

Base class of all the statements.

class tvm.tirx.Bind(var: Var, value: Expr, span: Span | None = None)#

Bind node.

Bind a variable to a value in the enclosing scope. Bind has no body field. The bound variable is visible in all subsequent statements within the same enclosing scope (SeqStmt, ForNode.body, etc.).

Parameters:
  • var (tirx.Var) – The variable in the binding.

  • value (Expr) – The value to be bound.

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

class tvm.tirx.AssertStmt(kind: StringImm, condition: Expr, message_parts: list | None = None, span: Span | None = None)#

AssertStmt node.

Parameters:
  • kind (StringImm) – The error kind, e.g. “RuntimeError”, “TypeError”, “ValueError”.

  • condition (Expr) – The assert condition.

  • message_parts (list[StringImm]) – Error message fragments, concatenated at runtime when assertion fails.

  • span (Span | None) – The location of the stmt in the source code.

class tvm.tirx.ForKind(value)#

The kind of the for loop.

Note

ForKind can change the control flow semantics of the loop and need to be considered in all TIR passes.

class tvm.tirx.For(loop_var: Var, min: Expr, extent: Expr, kind: ForKind, body: Stmt, thread_binding: IterVar | None = None, annotations: Mapping[str, Object] | None = None, step: Expr | None = None, span: Span | None = None)#

For node.

Parameters:
  • loop_var (tirx.Var) – The loop variable.

  • min (Expr) – The beginning value.

  • extent (Expr) – The length of the loop.

  • kind (ForKind) – The type of the for.

  • body (Stmt) – The body statement.

  • thread_binding (Optional[tirx.IterVar]) – The thread this loop binds to. Only valid if kind is ThreadBinding

  • step (Expr) – The loop step. Default to none which represent one.

  • annotations (Optional[Mapping[str, Object]]) – Additional annotation hints.

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

class tvm.tirx.While(condition: Expr, body: Stmt, span: Span | None = None)#

While node.

Parameters:
  • condition (Expr) – The termination condition.

  • body (Stmt) – The body statement.

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

class tvm.tirx.Return(value: Expr, span: Span | None = None)#

Return node.

Parameters:
  • value (Expr) – The value to return.

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

class tvm.tirx.Break(span: Span | None = None)#

Break node.

class tvm.tirx.Continue(span: Span | None = None)#

Continue node.

tvm.tirx.LetStmt#

alias of Bind

class tvm.tirx.BufferStore(buffer: Var, value: Expr, indices: list[Expr], span: Span | None = None)#

Buffer store node.

Parameters:
  • buffer (Buffer) – The buffer.

  • value (Expr) – The value we to be stored.

  • indices (List[Expr]) – The indices location to be stored.

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

class tvm.tirx.AllocBuffer(buffer: Var, *args, **kwargs)#

AllocBuffer node.

Allocates a buffer and declares it in scope.

Parameters:
  • buffer (Buffer) – The buffer being allocated and declared.

  • annotations (Optional[dict]) – Additional annotations about the allocation.

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

class tvm.tirx.AttrStmt(node: Any, attr_key: str, value: Expr, body: Stmt, span: Span | None = None)#

AttrStmt node.

Parameters:
  • node (Any) – The node to annotate the attribute

  • attr_key (str) – Attribute type key.

  • value (Expr) – The value of the attribute

  • body (Stmt) – The body statement.

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

class tvm.tirx.DeclBuffer(buffer: Var, *args, **kwargs)#

DeclBuffer node.

Parameters:
  • buffer (Buffer) – The buffer being declared.

  • data (Expr) – The physical data expression bound to the buffer view.

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

class tvm.tirx.SeqStmt(seq: list[Stmt], span: Span | None = None)#

Sequence of statements.

Parameters:
  • seq (List[Stmt]) – The statements

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

class tvm.tirx.IfThenElse(condition: Expr, then_case: Stmt, else_case: Stmt | None, span: Span | None = None)#

IfThenElse node.

Parameters:
  • condition (Expr) – The expression

  • then_case (Stmt) – The statement to execute if condition is true.

  • else_case (Optional[Stmt]) – The statement to execute if condition is false.

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

class tvm.tirx.Evaluate(value: Expr, span: Span | None = None)#

Evaluate node.

Parameters:
  • value (Expr) – The expression to be evaluated.

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

tvm.tirx.stmt_seq(*args: Expr | Stmt) SeqStmt#

Make sequence of statements

Parameters:

*args (Union[Expr, Stmt]) – List of statements to be combined as sequence.

Returns:

stmt – The combined statement.

Return type:

Stmt

tvm.tirx.stmt_list(stmt: Stmt) list[Stmt]#

Make list of stmt from blocks.

Parameters:

stmt (Stmt) – The input statement.

Returns:

stmt_list – The unpacked list of statements

Return type:

List[Stmt]

class tvm.tirx.BufferRegion(buffer: Var, region: list[Range])#

BufferRegion node.

Parameters:
  • buffer (Buffer) – The buffer of the buffer region

  • region (List[Range]) – The region array of the buffer region

class tvm.tirx.BufferRegionType#

The structural type of a BufferRegion expression.

class tvm.tirx.MatchBufferRegion(buffer: Var, source: BufferRegion)#

MatchBufferRegion node.

Parameters:
  • buffer (Buffer) – The target buffer

  • source (BufferRegion) – The region of source buffer

class tvm.tirx.SBlock(iter_vars: list[IterVar], reads: list[BufferRegion], writes: list[BufferRegion], name_hint: str, body: Stmt, init: Stmt | None = None, alloc_buffers: list[Var] | None = None, match_buffers: list[MatchBufferRegion] | None = None, annotations: Mapping[str, Object] | None = None, span: Span | None = None)#

SBlock node.

Parameters:
  • iter_vars (List[IterVar]) – The block Variable.

  • reads (List[BufferRegion]) – The read buffer regions of the block.

  • writes (List[BufferRegion]) – The write buffer regions of the block.

  • name_hint (str) – the name_hint of the block.

  • body (Stmt) – The body of the block.

  • init (Optional[Stmt]) – The init block of the reduction block

  • alloc_buffers (Optional[list[Buffer]]) – The buffer allocations

  • match_buffers (Optional[List[MatchBufferRegion]]) – The subregion buffer match

  • annotations (Optional[Mapping[str, Object]]) – Additional annotation hints.

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

class tvm.tirx.SBlockRealize(iter_values: list[Expr], predicate: Expr | bool, block: SBlock, span: Span | None = None)#

SBlockRealize node.

Parameters:
  • iter_values (List[Expr]) – The binding values of the block var.

  • predicate (Union[Expr, bool]) – The predicate of the block.

  • block (SBlock) – The block to realize

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

class tvm.tirx.ScopeIdDefStmt(def_: ScopeIdDef, span: Span | None = None)#

ScopeIdDefStmt node.

Leaf statement that introduces scope-identifier vars (wg_id = Tx.warpgroup_id([N]), warp_id = Tx.warp_id_in_wg([4]), lane_id = Tx.lane_id([32]), …) at the kernel-body top level. The underlying ScopeIdDef carries the def vars, their extents, and the parent/child scope binding.

Note: the C++ field is named def (a Python keyword). Access it via getattr(stmt, "def") or stmt.__getattribute__("def") — the type-annotation alias here is purely for documentation.

Parameters:
  • def (ScopeIdDef) – The scope-id definition (def vars, extents, scope binding).

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

class tvm.tirx.LambdaExpr(f_pred: Callable[[...], Expr])#

A reified Python lambda: bound variables and a body over them.

Used by tile primitive ops that take a per-element expression over the destination axes (e.g. tirx.tile.select).

apply(indices: list[Expr]) Expr#

Substitute the bound variables with the given indices, returning the body.

class tvm.tirx.TilePrimitiveCall(*args: list[Expr], op: Op | None = None, workspace: dict[str, Var] | None = None, config: dict[str, Any] | None = None, dispatch: str | None = None, scope: ExecScope | None = None)#

TilePrimitiveCall node.

Parameters:
  • op (Op) – The operator.

  • args (List[Expr]) – The arguments.

  • workspace (Map[str, Buffer]) – The workspace.

  • config (Map[str, ObjectRef]) – The scheduler/config dictionary.

  • dispatch (Optional[str]) – The explicit variant name to dispatch to.

  • scope (ExecScope) – The cooperation scope of this call. Defaults to thread (an unscoped call).

replace(**changes: Any) TilePrimitiveCall#

Return a copy of this call with selected fields replaced.

Every field that is not overridden in changes is preserved from self (including scope), so rebuilds never silently drop fields. The returned node is downcast to the registered subclass for op.

Parameters:

**changes (Any) – Field overrides; any of op, args, workspace, config, dispatch, scope.

Returns:

new_call – A new call with the requested fields replaced.

Return type:

TilePrimitiveCall

with_workspace(workspace: dict[str, Var]) TilePrimitiveCall#

Return a copy with workspace replaced, preserving all other fields.

get_private_buffers(buffer_dict: dict[Any, tuple[Var, Stmt | None]], sctx: DispatchContext) dict[str, Any]#

Create private (intermediate) buffers needed in this operator.

Parameters:
  • buffer_dict (Dict[Any, Tuple[Buffer, Optional[Stmt]]]) – A dictionary containing private buffers (and their init stmts) in other operators. Key can be anything to reference the buffer. This is used to reuse private buffers in other operators (like identity tensor etc.). If the buffer is not found in the buffer_dict, it will be created and added to the buffer_dict. If the buffer is found in the buffer_dict but smaller than required, it will be enlarged and updated.

  • sctx (DispatchContext) – The dispatch context. This is used to get the target and reuse op dispatch implementations.

  • Returns

  • -------

  • private_buffer_refs (Dict[str, Any]) – The references to private buffers created in this operator. Key will be the name to add into workspace. private buffer can be accessed by buffer_dict[private_buffer_refs[name]]

class tvm.tirx.PrimFunc(params, body, ret_type=None, attrs=None, span=None)#

A function declaration expression.

Parameters:
  • params (List[Union[tvm.tirx.Var, tvm.tirx.Buffer]]) – List of input parameters to the function.

  • body (tvm.tirx.Stmt) – The body of the function.

  • ret_type (tvm.ir.Type) – The return type annotation of the function.

  • attrs (Optional[tvm.Attrs]) – Attributes of the function, can be None

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

with_body(new_body, span=None)#

Create a new PrimFunc with the same set signatures but a new body.

Parameters:
  • new_body (Stmt) – The new body.

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

Returns:

new_func – The created new function.

Return type:

PrimFunc

specialize(param_map: Mapping[Var, Expr | Var])#

Specialize parameters of PrimFunc

Parameters:

param_map (Mapping[tirx.Var, Union[Expr, Buffer]]) – The mapping from function params to the instance

Examples

We can define a Meta TIR function with symbolic shape:

@T.prim_func(s_tir=True)
def mem_copy(a: T.handle, b: T.handle, m: T.int32, n: T.int32) -> None:
    A = T.match_buffer(a, (m, n), "float32")
    B = T.match_buffer(b, (m, n), "float32")

    for i, j in T.grid(m, n):
        with T.sblock():
            vi, vj = T.axis.remap("SS", [i, j])
            B[vi, vj] = A[vi, vj]

Then we can make it specialized with given shapes or buffers.

a, _, m, n = mem_copy.params
func = mem_copy.specialize({a: tirx.decl_buffer((16, 16))})
# or
func = mem_copy.specialize({n: 16, m: 16})

The specialized function:

@T.prim_func(s_tir=True)
def mem_copy_16_16(a: T.handle, b: T.handle) -> None:
    A = T.match_buffer(a, (16, 16), "float32")
    B = T.match_buffer(b, (16, 16), "float32")

    for i, j in T.grid(16, 16):
        with T.sblock():
            vi, vj = T.axis.remap("SS", [i, j])
            B[vi, vj] = A[vi, vj]
Returns:

func – The new function with parameter specialized

Return type:

PrimFunc

class tvm.tirx.TensorIntrin(desc, impl)#

A tensor intrinsic.

Parameters:
  • desc (PrimFunc) – The function to describe the computation.

  • impl (PrimFunc) – The function of the implementation for the execution.

static register(name: str, desc: PrimFunc, impl: PrimFunc, override: bool = False)#

Register a tensor intrinsic with its name.

Parameters:
  • name (str) – The name of the TensorIntrin to register.

  • desc (PrimFunc) – The function to describe the computation.

  • impl (PrimFunc) – The function of the implementation for the execution.

  • override (bool) – Whether override existing intrinsic.

static get(name: str, allow_missing: bool = False) TensorIntrin | None#

Look up a tensor intrinsic by its name.

Parameters:
  • name (str) – The name of the TensorIntrin to look up.

  • allow_missing (bool) – Whether to allow missing tensor intrin. If False, raise an error if the tensor intrin

  • exist. (doesn't)

Returns:

result – The TensorIntrin with the specified name, or None if not found.

Return type:

Optional[TensorIntrin]

class tvm.tirx.IndexMap(initial_indices, final_indices, inverse_index_map)#

A mapping from multi-dimensional indices to another set of multi-dimensional indices

Parameters:
  • initial_indices (List[tirx.Var]) – Variables representing the indices prior to remapping.

  • final_indices (List[Expr]) – Expressions defining the indices after remapping.

  • inverse_index_map (Union[Callable, Optional[IndexMap]]) – The optional pre-defined inverse index map. When this is defined, IndexMap::Inverse will return the pre-defined inverse index map. Otherwise, the inverse index map will be computed on the fly. It is the user’s responsibility to ensure the correctness of the pre-defined inverse index map.

static from_func(mapping_function: Callable, ndim: int | None = None, inverse_index_map: Callable | IndexMap | None = None, *, index_dtype: str = 'int64')#

Create an index map from a function

Parameters:
  • mapping_function (Callable) – The function to map from source indices to target indices. The function should accept tirx.Var parameters and return a either a tirx.Expr, or a list of tirx.Expr. Returning a tirx.Expr is equivalent to returning a list of length 1 containing that tirx.Expr.

  • ndim (Optional[int]) – The dimensionality of the buffer to which this transformation should be applied. If mapping_function uses variadic argument *args, ndim must be specified. If mapping_function does not use variadic arguments, ndim is optional.

  • inverse_index_map (Union[Callable, Optional[IndexMap]]) – The optional pre-defined inverse index map. When this is defined, IndexMap::Inverse will return the pre-defined inverse index map. Otherwise, the inverse index map will be computed on the fly. It is the user’s responsibility to ensure the correctness of the pre-defined inverse index map.

  • index_dtype (str) – The default index dtype to use for input iters in the mapping function.

Returns:

index_map – Returns an IndexMap representing the mapping_function.

Return type:

IndexMap

is_equivalent_to(other_map: IndexMap, analyzer=None) bool#

Return if the index maps are equivalent.

Parameters:
  • other_map (IndexMap) – The IndexMap to which the comparison should be made.

  • analyzer (Optional[tvm.arith.Analyzer]) – The analyzer to use while comparing the mapped indices. When provided, its accumulated bindings and constraints are reused so that maps that are only equivalent under those bindings can be proven equal.

Returns:

is_equivalent – True if the two mappings represent the same transformation, otherwise False

Return type:

bool

map_indices(indices: list[Expr], analyzer=None) list[Expr]#

Apply the index map to a set of indices

Parameters:
  • indices (List[Expr]) – The indices to be mapped

  • analyzer (Optional[tvm.arith.Analyzer]) – The analyzer to use while simplifying mapped indices.

Returns:

result – The mapped indices

Return type:

List[Expr]

map_shape(shape: list[Expr], analyzer=None) list[Expr]#

Apply the index map to a buffer shape

Parameters:
  • shape (List[Expr]) – The buffer shape to be mapped

  • analyzer (Optional[tvm.arith.Analyzer]) – The analyzer to use while simplifying mapped shape expressions.

Returns:

result – The mapped shape

Return type:

List[Expr]

map_tensor(arr_src: Tensor) Tensor#

Apply thie index map to transform the layout of the input Tensor

Parameters:

arr_src (runtime.Tensor) – The Tensor to be transformed

Returns:

arr_dst – The transformed Tensor

Return type:

runtime.Tensor

inverse(shape: list[Range | Expr], analyzer=None) IndexMap#

Return the inverse of the map

Throws an error if the function is not bijective.

Parameters:
  • shape (List[Union[Range,Expr]]) – The region over which the inverse should be determined. Used for validating that the mapping is bijective over this range.

  • analyzer (Optional[tvm.arith.Analyzer]) – The analyzer to use while deriving and validating the inverse.

Returns:

inverse – The inverse

Return type:

IndexMap

non_surjective_inverse(shape: list[Range | Expr], analyzer=None) tuple[IndexMap, Expr]#

Return the inverse of the map

Can be applied to transformations that introduce padding.

Parameters:
  • shape (List[Union[Range,Expr]]) – The region over which the inverse should be determined. Used for determining the predicate.

  • analyzer (Optional[tvm.arith.Analyzer]) – The analyzer to use while deriving the inverse and padding predicate.

Returns:

result – The inverse, and a predicate for which the inverse maps to a valid index in the input range.

Return type:

Tuple[IndexMap, Expr]

Examples

index_map = IndexMap.from_func(lambda i: [i//4, i%4])
inverse_map, predicate = index_map.non_surjective_inverse([14])
assert inverse_map.is_equivalent_to(IndexMap.from_func(lambda j,k: [4*j + k])
print(predicate) # Prints "(axis0==3) && (axis2 >= 2)"
tvm.tirx.call_packed_lowered(*args, span=None)#

Lowered version of call packed. The argument to packed function can be Expr or Buffer. The argument is the corresponding POD type when Expr is presented. When the argument is Buffer, the corresponding PackedFunc will receive an TVMArrayHandle whose content is valid during the callback period. If the PackedFunc is a python callback, then the corresponding argument is Tensor.

Parameters:
  • args (list of Expr or Buffer.) – Positional arguments.

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

Returns:

call – The call expression.

Return type:

Expr

See also

te.extern

Create tensor with extern function call.

tvm.tirx.call_cpacked_lowered(*args, span=None)#

Lowered version of call c-packed. Same as call_packed, except that the first argument is the function name (as in call_extern), and the last argument is the resource handle.

Parameters:
  • args (list of Expr or Buffer.) – Positional arguments.

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

Returns:

call – The call expression.

Return type:

Expr

See also

te.extern

Create tensor with extern function call.

tvm.tirx.call_tir(global_var: GlobalVar, *args)#

Performs a call into another PrimFunc in the same IRModule

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.call_packed(*args, span=None)#

Build expression by call an external packed function.

The argument to packed function can be Expr or Buffer. The argument is the corresponding POD type when Expr is presented.

When the argument is Buffer, the corresponding PackedFunc will receive an TVMArrayHandle whose content is valid during the callback period. If the PackedFunc is a python callback, then the corresponding argument is Tensor.

Parameters:
  • args (list of Expr or Buffer.) – Positional arguments.

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

Returns:

call – The call expression.

Return type:

Expr

See also

te.extern

Create tensor with extern function call.

tvm.tirx.call_cpacked(*args, span=None)#

Build expression by call an external packed function.

Same as call_packed, except that the first argument is the function name (as in call_extern), and the last argument is the resource handle.

Parameters:
  • args (list of Expr or Buffer.) – Positional arguments.

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

Returns:

call – The call expression.

Return type:

Expr

See also

te.extern

Create tensor with extern function call.

tvm.tirx.call_intrin(dtype: str | Type, func_name, *args, attrs=None, span=None)#

Build expression by calling an intrinsic function.

Intrinsics can be overloaded with multiple data types via the intrinsic translation rule.

Parameters:
  • dtype (str or tvm.ir.Type) – The data type of the result.

  • func_name (str) – The intrinsic function name.

  • args (list) – Positional arguments.

  • attrs (Optional[tvm.ir.Attrs or Dict[str, Object]]) – Additional attributes for the call.

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

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.call_pure_extern(dtype, func_name, *args, span=None)#

Build expression by calling a pure extern function.

Parameters:
  • dtype (str) – The data type of the result.

  • func_name (str) – The extern function name.

  • args (list) – Positional arguments.

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

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.call_extern(dtype, func_name, *args, span=None)#

Build expression by calling a extern function.

Parameters:
  • dtype (str) – The data type of the result.

  • func_name (str) – The extern function name.

  • args (list) – Positional arguments.

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

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.call_llvm_intrin(dtype, name, *args, span=None)#

Build expression by calling a llvm intrinsic function

Parameters:
  • dtype (str) – The data type of the result.

  • name (str) – The name of the llvm intrinsic function.

  • args (list) – Positional arguments.

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

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.call_llvm_pure_intrin(dtype, name, *args, span=None)#

Build expression by calling a pure llvm intrinsic function

Parameters:
  • dtype (str) – The data type of the result.

  • name (str) – The name of the llvm intrinsic function.

  • args (list) – Positional arguments.

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

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.all(*args, span=None)#
Create a new expression of the intersection of all conditions in the

arguments

Parameters:
  • args (list) – List of symbolic boolean expressions

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

Returns:

expr – Expression

Return type:

Expr

tvm.tirx.any(*args, span=None)#

Create a new experssion of the union of all conditions in the arguments

Parameters:
  • args (list) – List of symbolic boolean expressions

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

Returns:

expr – Expression

Return type:

Expr

tvm.tirx.min_value(dtype, span=None)#

minimum value of dtype

Parameters:
  • dtype (str) – The data type.

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

Returns:

value – The minimum value of dtype.

Return type:

tvm.Expr

tvm.tirx.max_value(dtype: str, span: Span | None = None) Any#

maximum value of dtype

Parameters:
  • dtype (str) – The data type.

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

Returns:

value – The maximum value of dtype.

Return type:

tvm.Expr

tvm.tirx.trace(args, trace_action='tvm.default_trace_action')#

Trace tensor data at the runtime.

The trace function allows to trace specific tensor at the runtime. The tracing value should come as last argument. The trace action should be specified, by default tvm.default_trace_action is used.

Parameters:
  • args (list of Expr or Buffers.) – Positional arguments.

  • trace_action (str.) – The name of the trace action.

Returns:

call – The call expression.

Return type:

Expr

See also

tvm.tirx.call_packed

Creates packed function.

tvm.tirx.tvm_stack_alloca(dtype_str, num)#

Return new on stack dtype[num]

Parameters:
  • dtype_str (str) – The data type of array.

  • num (int) – The size of array.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.tvm_stack_make_shape(*args)#

Allocate a shape tuple on stack, return the handle

Parameters:

args (int) – The tuple shape.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.tvm_stack_make_array(data, shape, strides, ndim, arr_dtype, elem_offset)#

Allocate a Tensor(DLTensor) on stack, return the handle

Parameters:
  • data (Expr) – The data of array.

  • shape (Expr) – The shape of array.

  • strides (Expr) – The strides of array.

  • ndim (Expr) – The dimensions of array.

  • arr_dtype (Expr) – The data type of array.

  • elem_offse (Expr) – The element offset of array.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.tvm_tuple(*value)#

Create a tuple structure in value field of AttrStmt

Parameters:

value (Expr) – The value in tuple.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.handle_add_byte_offset(handle, offset)#

Add offset to handle

Parameters:
  • handle (Expr) – The handle.

  • offset (int) – The offset.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.tvm_struct_get(arr, index, field, dtype)#

Get struct field value in array

Parameters:
  • dtype (str) – The date type of the result.

  • arr (StructType*) – The array of struct.

  • index (int) – The index of struct.

  • field (int) – The field of struct.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.tvm_struct_set(arr, index, field, value)#

Set value in struct field in array

Parameters:
  • arr (StructType*) – The array of struct.

  • index (int) – The index of struct.

  • field (int) – The field of struct.

  • value (Expr) – The value to be set in field.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.address_of(obj: Var | TensorLoad, span: Span | None = None) Expr#

Returns the address of a buffer element or addressable variable.

Parameters:
  • obj (Union[Buffer, TensorLoad, tirx.Var]) – The buffer, buffer load, or addressable variable.

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

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.lookup_param(param_name, span=None)#

Returns the param by name

Parameters:
  • param_name (str) – The name of param.

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

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.assume(cond=None)#

Provide a true statement that can be used for simplifications

Parameters:

cond (Expr) – The constraint condition.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.undef()#

Returns an initialized but arbitrary value

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.continue_loop(span=None)#

Create a tir intrinsic call to represent continue expression

Parameters:

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

Returns:

ret – The continue expression

Return type:

Expr

tvm.tirx.break_loop(span=None)#

Create a tir intrinsic call to represent break expression

Parameters:

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

Returns:

ret – The break expression

Return type:

Expr

tvm.tirx.tvm_thread_allreduce(*freduce_args)#

Perform allreduce inside threadblock.

Parameters:

freduce_args (Expr) – The args.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.type_annotation(dtype)#

Create a type annotation expression

Parameters:

dtype (Expr) – The data type.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.tvm_access_ptr(ptype, data, offset, extent, rw_mask)#

Get head access address with memory access pattern info

Parameters:
  • ptype (Expr, PrimType, or str) – The data type of pointer. If a PrimType or str, it is wrapped via type_annotation() so that the lowering rule (which reads args[0].dtype() for the cast type) sees the intended dtype instead of void from a raw StringImm.

  • data (DType*) – The data of pointer.

  • offset (int) – The offset of pointer.

  • extent (int) – The extent of pointer.

  • rw_mask (int) – The read write mask.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.ptr_byte_offset(data, byte_offset, dtype)#

Cast data + byte_offset to dtype*.

byte_offset is always in bytes. Use this when the source CUDA shape needs an explicitly typed local pointer derived from a byte-addressed base.

tvm.tirx.tvm_throw_last_error()#

Throw TVMGetLastError()

Returns:

ret – The return expression

Return type:

Expr

tvm.tirx.tvm_load_matrix_sync(fragment, m, n, k, index, buffer_ptr, stride, layout)#

TVM intrinsic for tensor core load operators

Parameters:
  • fragment (tirx.Var) – The wmma fragment.

  • m (UIntImm) – The shape of wmma fragment.

  • n (UIntImm) – The shape of wmma fragment.

  • k (UIntImm) – The shape of wmma fragment.

  • index (Expr) – The fragment index.

  • buffer_ptr (Expr) – The fragment buffer pointer.

  • stride (Expr) – The fragment stride.

  • layout (Literal["row_major", "column_major"]) – The fragment layout.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.tvm_store_matrix_sync(fragment, m, n, k, index, buffer_ptr, stride, layout)#

TVM intrinsic for tensor core store operators

Parameters:
  • fragment (tirx.Var) – The wmma fragment.

  • m (UIntImm) – The shape of wmma fragment.

  • n (UIntImm) – The shape of wmma fragment.

  • k (UIntImm) – The shape of wmma fragment.

  • index (Expr) – The fragment index.

  • buffer_ptr (Expr) – The fragment buffer pointer.

  • stride (Expr) – The fragment stride.

  • layout (Literal["row_major", "column_major"]) – The fragment layout.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.tvm_mma_sync(fragment_d, index_d, fragment_a, index_a, fragment_b, index_b, fragment_c, index_c)#

TVM intrinsic for tensor core mma_sync operators

Parameters:
  • fragment_d (tirx.Var) – The wmma fragment_d.

  • index_d (Expr) – The fragment_d index.

  • fragment_a (tirx.Var) – The wmma fragment_a.

  • index_a (Expr) – The fragment_a index.

  • fragment_b (tirx.Var) – The wmma fragment_b.

  • index_b (Expr) – The fragment_b index.

  • fragment_c (tirx.Var) – The wmma fragment_c.

  • index_c (Expr) – The fragment_c index.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.tvm_bmma_sync(fragment_d, index_d, fragment_a, index_a, fragment_b, index_b, fragment_c, index_c)#

TVM intrinsic for tensor core bmma_sync operators

Parameters:
  • fragment_d (tirx.Var) – The bwmma fragment_d.

  • index_d (Expr) – The fragment_d index.

  • fragment_a (tirx.Var) – The bwmma fragment_a.

  • index_a (Expr) – The fragment_a index.

  • fragment_b (tirx.Var) – The bwmma fragment_b.

  • index_b (Expr) – The fragment_b index.

  • fragment_c (tirx.Var) – The bwmma fragment_c.

  • index_c (Expr) – The fragment_c index.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.tvm_fill_fragment(fragment, m, n, k, index, value)#

TVM intrinsic for tensor core fill_fragment operators

Parameters:
  • fragment (tirx.Var) – The wmma fragment

  • m (UIntImm) – The shape of wmma fragment.

  • n (UIntImm) – The shape of wmma fragment.

  • k (UIntImm) – The shape of wmma fragment.

  • index (Expr) – The fragment index.

  • value (Expr) – The value to be filled in fragment.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.vectorlow(dtype, vec)#

Get the low level half of the vector

Parameters:
  • dtype (str) – The data type of the result.

  • vec (list) – The input vector.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.vectorhigh(dtype, vec)#

Get the high level half of the vector

Parameters:
  • dtype (str) – The data type of the result.

  • vec (list) – The input vector.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.vectorcombine(dtype, vec1, vec2)#

Concat two vectors

Parameters:
  • vec1 (list) – The input vector.

  • vec2 (list) – The input vector.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.infinity(dtype: str, span: Span | None = None) Any#

infinity value of dtype

Parameters:
  • dtype (str) – The data type.

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

Returns:

value – The infinity value of dtype.

Return type:

tvm.Expr

tvm.tirx.reinterpret(dtype, value, span: Span | None = None) Expr#

Reinterpret a value as an exact primitive or pointer type.

Parameters:
  • dtype (str or tvm.ir.Type) – The data type.

  • value (Expr) – The input value.

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

Returns:

value – The reinterpret cast value of dtype.

Return type:

tvm.Expr

tvm.tirx.exp(x)#

Take exponential of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.exp2(x)#

Calculate 2**x

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.exp10(x)#

Calculate 10**x

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.log(x)#

Take log of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.log2(x)#

Take log2 of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.log10(x)#

Take log10 of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.log1p(x)#

Take log(x + 1) with respect to input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.ldexp(x1, x2)#

Returns x1 * (2 ** x2).

Parameters:
  • x1 (Expr) – Input argument.

  • x2 (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.clz(x)#

Count leading zero bits of an integer x.

Parameters:

x (Expr) – Input 32 or 64 bit integer. The result is undefined if the input is 0.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.sin(x)#

Take sin of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.sinh(x)#

Take sinh of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.asin(x)#

Take asin of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.asinh(x)#

Take asinh of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.cos(x)#

Take cos of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.cosh(x)#

Take cosh of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.acos(x)#

Take acos of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.acosh(x)#

Take acos of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.tan(x)#

Take tan of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.tanh(x)#

Take hyperbolic tanh of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.atan(x)#

Take atan of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.atan2(x1, x2)#

Take arctan2(x1, x2).

Parameters:
  • x1 (Expr) – Input argument.

  • x2 (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.atanh(x)#

Take atanh of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.bitwise_and(x, y, span=None)#

Take bitwise and of two values

Parameters:
  • x (Expr) – Left operand

  • y (Expr) – Right operand

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

Returns:

res – The result.

Return type:

Expr

tvm.tirx.bitwise_not(x, span=None)#

Take bitwise not of input value

Parameters:
  • x (Expr) – Input operand

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

Returns:

res – The result.

Return type:

Expr

tvm.tirx.bitwise_or(x, y, span=None)#

Take bitwise or of two values

Parameters:
  • x (Expr) – Left operand

  • y (Expr) – Right operand

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

Returns:

res – The result.

Return type:

Expr

tvm.tirx.bitwise_xor(x, y, span=None)#

Take bitwise xor of two values

Parameters:
  • x (Expr) – Left operand

  • y (Expr) – Right operand

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

Returns:

res – The result.

Return type:

Expr

tvm.tirx.erf(x)#

Take gauss error function of the input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.sigmoid(x)#

Quick function to get sigmoid

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.sqrt(x)#

Take square root of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.rsqrt(x)#

Take reciprocal of square root of input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.floor(x: ExprWithOp, span=None)#

Take floor of float input x.

Parameters:
  • x (Expr) – Input argument.

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

Returns:

y – The result.

Return type:

Expr

tvm.tirx.ceil(x, span=None)#

Take ceil of float input x.

Parameters:
  • x (Expr) – Input argument.

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

Returns:

y – The result.

Return type:

Expr

tvm.tirx.hypot(x1, x2)#

Equivalent to sqrt(x1**2 + x2**2), element-wise.

Parameters:
  • x1 (Expr) – Input argument.

  • x2 (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.trunc(x, span=None)#

Get truncated value of the input.

The truncated value of the scalar x is the nearest integer i which is closer to zero than x is.

Parameters:
  • x (Expr) – Input argument.

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

Returns:

y – The result.

Return type:

Expr

tvm.tirx.abs(x, span=None)#

Get absolute value of the input element-wise.

Parameters:
  • x (Expr) – Input argument.

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

Returns:

y – The result.

Return type:

Expr

tvm.tirx.round(x, span=None)#

Round elements of the array to the nearest integer.

Parameters:
  • x (Expr) – Input argument.

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

Returns:

y – The result.

Return type:

Expr

tvm.tirx.nextafter(x1, x2)#

Return the next floating-point value after x1 towards x2.

Parameters:
  • x1 (Expr) – Input argument.

  • x2 (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.nearbyint(x, span=None)#

Round elements of the array to the nearest integer. This intrinsic uses llvm.nearbyint instead of llvm.round which is faster but will results different from te.round. Notably nearbyint rounds according to the rounding mode, whereas te.round (llvm.round) ignores that. For differences between the two see: https://en.cppreference.com/w/cpp/numeric/math/round https://en.cppreference.com/w/cpp/numeric/math/nearbyint

Parameters:
  • x (Expr) – Input argument.

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

Returns:

y – The result.

Return type:

Expr

tvm.tirx.power(x, y, span=None)#

x power y

Parameters:
  • x (Expr) – Input argument.

  • y (Expr) – The exponent

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

Returns:

z – The result.

Return type:

Expr

tvm.tirx.pow(x, y, span=None)#

x power y

Parameters:
  • x (Expr) – Input argument.

  • y (Expr) – The exponent

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

Returns:

z – The result.

Return type:

Expr

tvm.tirx.popcount(x)#

Count the number of set bits in input x.

Parameters:

x (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.fmod(x, y)#

Return the remainder of x divided by y with the same sign as x.

Parameters:
  • x (Expr) – Input argument.

  • y (Expr) – Input argument.

Returns:

z – The result.

Return type:

Expr

tvm.tirx.if_then_else(cond, t, f, span=None)#

Conditional selection expression.

Parameters:
  • cond (Expr) – The condition

  • t (Expr) – The result expression if cond is true.

  • f (Expr) – The result expression if cond is false.

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

Returns:

result – The result of conditional expression.

Return type:

Node

Note

Unlike Select, if_then_else will not execute the branch that does not satisfy the condition. You can use it to guard against out of bound access. Unlike Select, if_then_else cannot be vectorized if some lanes in the vector have different conditions.

tvm.tirx.likely(cond, span=None)#

Mark condition as likely.

Parameters:
  • cond (Expr) – Input argument.

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

Returns:

y – The marked expression.

Return type:

Expr

tvm.tirx.isnan(x, span=None)#

Check if input value is Nan.

Parameters:
  • x (Expr) – Input argument.

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

Returns:

y – The result.

Return type:

Expr

tvm.tirx.isnullptr(x, span=None)#

Check if input value is nullptr.

Parameters:
  • x (Expr) – Input argument.

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

Returns:

y – The result.

Return type:

Expr

tvm.tirx.isfinite(x, span=None)#

Check if input value is finite.

Parameters:
  • x (Expr) – Input argument.

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

Returns:

y – The result.

Return type:

Expr

tvm.tirx.isinf(x, span=None)#

Check if input value is infinite.

Parameters:
  • x (Expr) – Input argument.

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

Returns:

y – The result.

Return type:

Expr

tvm.tirx.copysign(x1, x2)#

Change the sign of x1 to that of x2, element-wise.

Parameters:
  • x1 (Expr) – Input argument.

  • x2 (Expr) – Input argument.

Returns:

y – The result.

Return type:

Expr

tvm.tirx.div(a, b, span=None)#

Compute a / b as in C/C++ semantics.

Parameters:
  • a (Expr) – The left hand operand, known to be non-negative.

  • b (Expr) – The right hand operand, known to be non-negative.

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

Returns:

res – The result expression.

Return type:

Expr

Note

When operands are integers, returns truncdiv(a, b, span).

tvm.tirx.indexdiv(a, b, span=None)#

Compute floor(a / b) where a and b are non-negative.

Parameters:
  • a (Expr) – The left hand operand, known to be non-negative.

  • b (Expr) – The right hand operand, known to be non-negative.

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

Returns:

res – The result expression.

Return type:

Expr

Note

Use this function to split non-negative indices. This function may take advantage of operands’ non-negativeness.

tvm.tirx.indexmod(a, b, span=None)#

Compute the remainder of indexdiv. a and b are non-negative.

Parameters:
  • a (Expr) – The left hand operand, known to be non-negative.

  • b (Expr) – The right hand operand, known to be non-negative.

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

Returns:

res – The result expression.

Return type:

Expr

Note

Use this function to split non-negative indices. This function may take advantage of operands’ non-negativeness.

tvm.tirx.truncdiv(a, b, span=None)#

Compute the truncdiv of two expressions.

Parameters:
  • a (Expr) – The left hand operand

  • b (Expr) – The right hand operand

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

Returns:

res – The result expression.

Return type:

Expr

Note

This is the default integer division behavior in C.

tvm.tirx.truncmod(a, b, span=None)#

Compute the truncmod of two expressions.

Parameters:
  • a (Expr) – The left hand operand

  • b (Expr) – The right hand operand

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

Returns:

res – The result expression.

Return type:

Expr

Note

This is the default integer division behavior in C.

tvm.tirx.floordiv(a, b, span=None)#

Compute the floordiv of two expressions.

Parameters:
  • a (Expr) – The left hand operand

  • b (Expr) – The right hand operand

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

Returns:

res – The result expression.

Return type:

Expr

tvm.tirx.floormod(a, b, span=None)#

Compute the floormod of two expressions.

Parameters:
  • a (Expr) – The left hand operand

  • b (Expr) – The right hand operand

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

Returns:

res – The result expression.

Return type:

Expr

tvm.tirx.ceildiv(lhs, rhs, span=None)#

Generic ceildiv operator.

Parameters:
  • lhs (object) – The left operand.

  • rhs (object) – The right operand.

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

Returns:

op – The result Expr of ceildiv operaton.

Return type:

tvm.Expr

tvm.tirx.logaddexp(a, b, span=None)#

Compute the logaddexp of two expressions.

Parameters:
  • a (Expr) – The left hand operand

  • b (Expr) – The right hand operand

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

Returns:

res – The result expression.

Return type:

Expr

tvm.tirx.comm_reducer(fcombine, fidentity, name='reduce')#

Create a commutative reducer for reduction.

Parameters:
  • fcombine (function(Expr -> Expr -> Expr)) – A binary function which takes two Expr as input to return a Expr.

  • fidentity (function(str -> Expr)) – A function which takes a type string as input to return a const Expr.

Returns:

reducer – A function which creates a reduce expression over axis. There are two ways to use it:

  1. accept (expr, axis, where) to produce an Reduce Expr on specified axis;

  2. simply use it with multiple Exprs.

Return type:

function

Example

n = te.var("n")
m = te.var("m")
mysum = te.comm_reducer(lambda x, y: x+y,
    lambda t: tvm.tirx.const(0, dtype=t), name="mysum")
A = te.placeholder((n, m), name="A")
k = te.reduce_axis((0, m), name="k")
B = te.compute((n,), lambda i: mysum(A[i, k], axis=k), name="B")
tvm.tirx.min(expr, axis, where=None, init=None, *args)#

Create a min expression over axis.

Parameters:
  • expr (Expr) – The source expression.

  • axis (IterVar) – The reduction IterVar axis

  • where (optional, Expr) – Filtering predicate of the reduction.

Returns:

value – The result value.

Return type:

Expr

Example

m = te.var("m")
n = te.var("n")
A = te.placeholder((m, n), name="A")
k = te.reduce_axis((0, n), name="k")

# there are two way to use this min reducer:
# mode 1, accept (expr, axis, where) to produce an Reduce Expr
# tvm.min represents tvm.te.min or tvm.tirx.min.
B = te.compute((m,), lambda i: tvm.min(A[i, k], axis=k), name="B")

# mode 2, simply use it with multiple Exprs:
min_res = tvm.min(m, n)
tvm.tirx.max(expr, axis, where=None, init=None, *args)#

Create a max expression over axis.

Parameters:
  • expr (Expr) – The source expression.

  • axis (IterVar) – The reduction IterVar axis

  • where (optional, Expr) – Filtering predicate of the reduction.

Returns:

value – The result value.

Return type:

Expr

Example

m = te.var("m")
n = te.var("n")
A = te.placeholder((m, n), name="A")
k = te.reduce_axis((0, n), name="k")

# there are two way to use this max reducer:
# mode 1, accept (expr, axis, where) to produce an Reduce Expr
# tvm.max represents tvm.te.max or tvm.tirx.max.
B = te.compute((m,), lambda i: tvm.max(A[i, k], axis=k), name="B")

# mode 2, simply use it with multiple Exprs:
max_res = tvm.max(m, n)
tvm.tirx.sum(expr, axis, where=None, init=None, *args)#

Create a sum expression over axis.

Parameters:
  • expr (Expr) – The source expression.

  • axis (IterVar) – The reduction IterVar axis

  • where (optional, Expr) – Filtering predicate of the reduction.

Returns:

value – The result value.

Return type:

Expr

Example

m = te.var("m")
n = te.var("n")
A = te.placeholder((m, n), name="A")
k = te.reduce_axis((0, n), name="k")

# there are two way to use this sum reducer:
# mode 1, accept (expr, axis, where) to produce an Reduce Expr
# tvm.sum represents tvm.te.sum or tvm.tirx.sum.
B = te.compute((m,), lambda i: tvm.sum(A[i, k], axis=k), name="B")

# mode 2, simply use it with multiple Exprs:
sum_res = tvm.sum(m, n)
tvm.tirx.q_multiply_shift(x, y, q, s)#

Execute a multiplication between two Q-numbers x and y followed by a right shift s. The mathematical expression is:

out = round(x*y*2^-s)

More about Q-numbers here: https://en.wikipedia.org/wiki/Q_(number_format) The rounding rule is to the nearest value, rounding half up (i.e., round(x.1) = x and round (x.5) = x+1)

Parameters:
  • x (Expr) – First Q-number

  • y (Expr) – Second Q-number

  • q (Expr) – Number of fractional bits in x and y. Needs to be > 0

  • s (Expr) – Integer shift

Returns:

y – The result.

Return type:

Expr

tvm.tirx.q_multiply_shift_per_axis(x: Expr, y: Expr, ls: Expr, rs: Expr, q: IntImm, is_lshift_required: IntImm, is_rshift_required: IntImm)#

Execute a multiplication between two Q-numbers x and y

Parameters:
  • x (Expr) – First Q-number.

  • y (Expr) – Second Q-number.

  • ls (Expr) – Integer left shift.

  • rs (Expr) – Integer right shift.

  • q (IntImm) – Number of fractional bits in x and y. Needs to be > 0.

  • is_lshift_required (IntImm) – Whether we need to do left shift or not.

  • is_rshift_required (IntImm) – Whether we need to do right shift or not.

Returns:

z – The result.

Return type:

Expr

tvm.tirx.shift_left(x, y, span=None)#

Return the result of x left shifted by y bits.

Parameters:
  • x (Expr) – Input argument.

  • y (Expr) – Input argument.

Returns:

z – The result.

Return type:

Expr

tvm.tirx.shift_right(x, y, span=None)#

Return the result of x right shifted by y bits.

Parameters:
  • x (Expr) – Input argument.

  • y (Expr) – Input argument.

Returns:

z – The result.

Return type:

Expr

tvm.tirx.TVMBackendAllocWorkspace(device_type, device_id, nbytes, dtype_code_hint, dtype_bits_hint)#

Backend function to allocate temporal workspace

Parameters:
  • device_type (int) – The device type which the space will be allocated.

  • device_id (int) – The device id which the space will be allocated.

  • nbytes (int) – The size of the space requested.

  • dtype_code_hint (int) – The type code of the array elements. Only used in certain backends such as OpenGL.

  • dtype_bits_hint (int) – The type bits of the array elements. Only used in certain backends such as OpenGL.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.TVMBackendFreeWorkspace(device_type, device_id, ptr)#

Backend function to free temporal workspace.

Parameters:
  • device_type (int) – The device type which the space will be allocated.

  • device_id (int) – The device id which the space will be allocated.

  • ptr (tirx.Var) – The result allocated space pointer.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.start_profile_intrinsic(id)#

Start profile intrinsic. :param id: The intrinsic id. :type id: int

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.end_profile_intrinsic(id)#

End profile intrinsic. :param id: The intrinsic id. :type id: int

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.vscale()#

Get the target’s vscale value. It will be lowered to llvm.vscale intrinsic (https://llvm.org/docs/LangRef.html#llvm-vscale-intrinsic) :returns: call – tirx.Call to the vscale intrinsic :rtype: Expr

tvm.tirx.get_active_lane_mask(dtype, base, limit)#

Calculate a predicate mask given an upper bound (limit) and a current value (base).

It will be lowered to the llvm.get.active.lane.mask intrinsic. (https://llvm.org/docs/LangRef.html#llvm-get-active-lane-mask-intrinsics)

Parameters:
  • dtype (str) – The data type of the result.

  • base (Expr) – An expression reprsenting the base.

  • limit (Expr) – An expression representing the limit.

tvm.tirx.get_vscale_expr(dtype: str | dtype, min_size: int = 128) Expr#

Create a datatype dependent scalable expression.

Parameters:
  • dtype (Union[str, tvm_ffi.DataType]) – Element data type.

  • min_size (int) – The minimum size of the scalable vector in bits.

tvm.tirx.dp4a(vec1, vec2, acc=0)#

Dot product of two int8x4 vectors and add an optional accumulator

Parameters:
  • vec1 (int8x4) – The input vector.

  • vec2 (int8x4) – The input vector.

  • acc (int32) – The accumulator.

Returns:

call – The call expression.

Return type:

Expr

tvm.tirx.ignore_loop_partition(predicate) Expr#

Annotate a predicate not be considered as target condition of loop partition.

Parameters:

predicate (Expr) – The annotated predicate expression.