tvm.te#
Namespace for Tensor Expression Language
Functions:
|
Create a new experssion of the union of all conditions in the arguments |
|
Create a new expression of the intersection of all conditions in the |
|
minimum value of dtype |
|
maximum value of dtype |
|
Take exponential of input x. |
|
Take gauss error function of the input x. |
|
Take hyperbolic tanh of input x. |
|
Quick function to get sigmoid |
|
Take log of input x. |
|
Take tan of input x. |
|
Take cos of input x. |
|
Take sin of input x. |
|
Take square root of input x. |
|
Take reciprocal of square root of input x. |
|
Take floor of float input x. |
|
Take ceil of float input x. |
|
Take sinh of input x. |
|
Take cosh of input x. |
|
Take log2 of input x. |
|
Take log10 of input x. |
|
Take asin of input x. |
|
Take asinh of input x. |
|
Take acos of input x. |
|
Take acos of input x. |
|
Take atan of input x. |
|
Take atanh of input x. |
|
Get truncated value of the input. |
|
Get absolute value of the input element-wise. |
|
Round elements of the array to the nearest integer. |
|
Round elements of the array to the nearest integer. |
|
x power y |
|
Count the number of set bits in input x. |
|
Return the remainder of x divided by y with the same sign as x. |
|
Conditional selection expression. |
|
Check if input value is Nan. |
|
Check if input value is finite. |
|
Check if input value is infinite. |
|
Compute a / b as in C/C++ semantics. |
|
Compute floor(a / b) where a and b are non-negative. |
|
Compute the remainder of indexdiv. |
|
Compute the truncdiv of two expressions. |
|
Compute the truncmod of two expressions. |
|
Compute the floordiv of two expressions. |
|
Compute the floormod of two expressions. |
|
Compute the logaddexp of two expressions. |
|
Create a commutative reducer for reduction. |
|
Create a min expression over axis. |
|
Create a max expression over axis. |
|
Create a sum expression over axis. |
|
The operator tag scope. |
|
Construct an empty tensor object. |
|
Construct a new tensor by computing over the shape domain. |
|
Construct new tensors by scanning over axis. |
|
Compute several tensors via an extern function. |
|
Create a new variable with specified name and dtype |
|
Create a new constant with specified value and dtype |
|
Create a new IterVar to represent thread index. |
|
Create a new IterVar for reduction. |
|
Create a TensorIR Function from tensor expression |
|
Compute tensors via a schedulable TIR Function |
Classes:
|
Commutative reduce operator |
|
Reduce node. |
|
Auxiliary data structure for enable slicing syntax from tensor. |
|
Tensor object, to construct, see function.Tensor |
|
Placeholder operation. |
|
Scalar operation. |
|
Scan operation. |
|
External operation. |
- tvm.te.any(*args, span=None)#
Create a new experssion of the union of all conditions in the arguments
- tvm.te.all(*args, span=None)#
- Create a new expression of the intersection of all conditions in the
arguments
- tvm.te.min_value(dtype, span=None)#
minimum value of dtype
- tvm.te.exp(x, *, ty=None, span=None)#
Take exponential of input x.
- tvm.te.erf(x, *, ty=None, span=None)#
Take gauss error function of the input x.
- tvm.te.tanh(x, *, ty=None, span=None)#
Take hyperbolic tanh of input x.
- tvm.te.sigmoid(x, *, ty=None, span=None)#
Quick function to get sigmoid
- tvm.te.log(x, *, ty=None, span=None)#
Take log of input x.
- tvm.te.tan(x, *, ty=None, span=None)#
Take tan of input x.
- tvm.te.cos(x, *, ty=None, span=None)#
Take cos of input x.
- tvm.te.sin(x, *, ty=None, span=None)#
Take sin of input x.
- tvm.te.sqrt(x, *, ty=None, span=None)#
Take square root of input x.
- tvm.te.rsqrt(x, *, ty=None, span=None)#
Take reciprocal of square root of input x.
- tvm.te.floor(x: ExprWithOp, span=None)#
Take floor of float input x.
- tvm.te.ceil(x, span=None)#
Take ceil of float input x.
- tvm.te.sinh(x, *, ty=None, span=None)#
Take sinh of input x.
- tvm.te.cosh(x, *, ty=None, span=None)#
Take cosh of input x.
- tvm.te.log2(x, *, ty=None, span=None)#
Take log2 of input x.
- tvm.te.log10(x, *, ty=None, span=None)#
Take log10 of input x.
- tvm.te.asin(x, *, ty=None, span=None)#
Take asin of input x.
- tvm.te.asinh(x, *, ty=None, span=None)#
Take asinh of input x.
- tvm.te.acos(x, *, ty=None, span=None)#
Take acos of input x.
- tvm.te.acosh(x, *, ty=None, span=None)#
Take acos of input x.
- tvm.te.atan(x, *, ty=None, span=None)#
Take atan of input x.
- tvm.te.atanh(x, *, ty=None, span=None)#
Take atanh of input x.
- tvm.te.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.
- tvm.te.abs(x, span=None)#
Get absolute value of the input element-wise.
- tvm.te.round(x, span=None)#
Round elements of the array to the nearest integer.
- tvm.te.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
- tvm.te.power(x, y, span=None)#
x power y
- tvm.te.popcount(x, *, ty=None, span=None)#
Count the number of set bits in input x.
- tvm.te.fmod(x, y, *, ty=None, span=None)#
Return the remainder of x divided by y with the same sign as x.
- tvm.te.if_then_else(cond, t, f, span=None)#
Conditional selection expression.
- Parameters:
- Returns:
result – The result of conditional expression.
- Return type:
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.te.isnan(x, span=None)#
Check if input value is Nan.
- tvm.te.isfinite(x, span=None)#
Check if input value is finite.
- tvm.te.isinf(x, span=None)#
Check if input value is infinite.
- tvm.te.div(a, b, span=None)#
Compute a / b as in C/C++ semantics.
- Parameters:
- Returns:
res – The result expression.
- Return type:
Note
When operands are integers, returns truncdiv(a, b, span).
- tvm.te.indexdiv(a, b, span=None)#
Compute floor(a / b) where a and b are non-negative.
- Parameters:
- Returns:
res – The result expression.
- Return type:
Note
Use this function to split non-negative indices. This function may take advantage of operands’ non-negativeness.
- tvm.te.indexmod(a, b, span=None)#
Compute the remainder of indexdiv. a and b are non-negative.
- Parameters:
- Returns:
res – The result expression.
- Return type:
Note
Use this function to split non-negative indices. This function may take advantage of operands’ non-negativeness.
- tvm.te.truncdiv(a, b, span=None)#
Compute the truncdiv of two expressions.
- Parameters:
- Returns:
res – The result expression.
- Return type:
Note
This is the default integer division behavior in C.
- tvm.te.truncmod(a, b, span=None)#
Compute the truncmod of two expressions.
- Parameters:
- Returns:
res – The result expression.
- Return type:
Note
This is the default integer division behavior in C.
- tvm.te.floordiv(a, b, span=None)#
Compute the floordiv of two expressions.
- tvm.te.floormod(a, b, span=None)#
Compute the floormod of two expressions.
- tvm.te.logaddexp(a, b, span=None)#
Compute the logaddexp of two expressions.
- Parameters:
- Returns:
res – The result expression.
- Return type:
Alias of
tvm.tirx.logaddexp()
- tvm.te.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:
accept (expr, axis, where) to produce an Reduce Expr on specified axis;
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.te.min(expr, axis, where=None, init=None, *args)#
Create a min expression over axis.
- Parameters:
- Returns:
value – The result value.
- Return type:
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 B = te.compute((m,), lambda i: te.min(A[i, k], axis=k), name="B") # mode 2, simply use it with multiple Exprs: min_res = te.min(m, n)
- tvm.te.max(expr, axis, where=None, init=None, *args)#
Create a max expression over axis.
- Parameters:
- Returns:
value – The result value.
- Return type:
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 B = te.compute((m,), lambda i: te.max(A[i, k], axis=k), name="B") # mode 2, simply use it with multiple Exprs: max_res = te.max(m, n)
- tvm.te.sum(expr, axis, where=None, init=None, *args)#
Create a sum expression over axis.
- Parameters:
- Returns:
value – The result value.
- Return type:
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 B = te.compute((m,), lambda i: te.sum(A[i, k], axis=k), name="B") # mode 2, simply use it with multiple Exprs: sum_res = te.sum(m, n)
- class tvm.te.CommReducer(lhs: list[Var], rhs: list[Var], result: list[Expr], identity_element: list[Expr], span: Span | None = None)#
Commutative reduce operator
- class tvm.te.Reduce(combiner: CommReducer, src: list[Expr], rdom: list[IterVar], condition: Expr, value_index: int, init: list[Expr] | None = None, span: Span | None = None)#
Reduce node.
- Parameters:
combiner (CommReducer) – The combiner.
rdom (list of IterVar) – The iteration domain
condition (Expr) – The reduce condition.
value_index (int) – The value index.
init (list of Expr) – The initial value for output. This can be an int, float, or TE tensor-load Call.
span (Optional[Span]) – The location of this expression in the source code.
- class tvm.te.TensorSlice(tensor, indices)#
Auxiliary data structure for enable slicing syntax from tensor.
Methods:
Attributes:
Data content of the tensor.
- asobject()#
Convert slice to object.
- property dtype#
Data content of the tensor.
- expr_ty()#
Compile-time element type of the tensor.
- class tvm.te.Tensor(shape, dtype, op, value_index, span=<MISSING>, ty=<MISSING>)#
Tensor object, to construct, see function.Tensor
Attributes:
Methods:
expr_ty()Compile-time element type of the tensor.
- property ndim#
Dimension of the tensor.
- property dtype#
Data content of the tensor.
- expr_ty()#
Compile-time element type of the tensor.
- tvm.te.tag_scope(tag)#
The operator tag scope.
- Parameters:
tag (str) – The tag name.
- Returns:
tag_scope – The tag scope object, which can be used as decorator or context manger.
- Return type:
TagScope
Example
n = te.var('n') m = te.var('m') l = te.var('l') A = te.placeholder((n, l), name='A') B = te.placeholder((m, l), name='B') k = te.reduce_axis((0, l), name='k') with tvm.te.tag_scope(tag='matmul'): C = te.compute((n, m), lambda i, j: te.sum(A[i, k] * B[j, k], axis=k)) # or use tag_scope as decorator @tvm.te.tag_scope(tag="conv") def compute_relu(data): return te.compute(data.shape, lambda *i: tvm.tirx.Select(data(*i) < 0, 0.0, data(*i)))
- tvm.te.placeholder(shape, dtype=None, name='placeholder')#
Construct an empty tensor object.
- tvm.te.compute(shape, fcompute, name='compute', tag='', attrs=None, varargs_names=None)#
Construct a new tensor by computing over the shape domain.
The compute rule is result[axis] = fcompute(axis)
- Parameters:
fcompute (lambda function of indices-> value) – Specifies the input source expression
name (str, optional) – The name hint of the tensor
tag (str, optional) – Additional tag information about the compute.
attrs (dict, optional) – The additional auxiliary attributes about the compute.
varargs_names (list, optional) – The names to use for each of the varargs. If not supplied, the varargs will be called i1, i2, …
- Returns:
tensor – The created tensor
- Return type:
- tvm.te.scan(init, update, state_placeholder, inputs=None, name='scan', tag='', attrs=None)#
Construct new tensors by scanning over axis.
- Parameters:
init (Tensor or list of Tensor) – The initial condition of first init.shape[0] timestamps
update (Tensor or list of Tensor) – The update rule of the scan given by symbolic tensor.
state_placeholder (Tensor or list of Tensor) – The placeholder variables used by update.
inputs (Tensor or list of Tensor, optional) – The list of inputs to the scan. This is not required, but can be useful for the compiler to detect scan body faster.
name (str, optional) – The name hint of the tensor
tag (str, optional) – Additonal tag information about the compute.
attrs (dict, optional) – The additional auxiliary attributes about the compute.
- Returns:
tensor – The created tensor or tuple of tensors contains multiple outputs.
- Return type:
Example
# The following code is equivalent to numpy.cumsum m = te.var("m") n = te.var("n") X = te.placeholder((m, n), name="X") s_state = te.placeholder((m, n)) s_init = te.compute((1, n), lambda _, i: X[0, i]) s_update = te.compute((m, n), lambda t, i: s_state[t-1, i] + X[t, i]) res = tvm.te.scan(s_init, s_update, s_state, X)
- tvm.te.extern(shape, inputs, fcompute, name='extern', dtype=None, in_buffers=None, out_buffers=None, tag='', attrs=None)#
Compute several tensors via an extern function.
- Parameters:
shape (tuple or list of tuples.) – The shape of the outputs.
fcompute (lambda function of inputs, outputs-> stmt) –
Specifies the IR statement or sequence of statements to do the computation. See the following note for function signature of fcompute
Note
Parameters
ins (list of
tvm.ir.Var) - Placeholder for each inputsouts (list of
tvm.ir.Var) - Placeholder for each outputs
Returns
stmt (
tvm.tirx.Stmtor a sequence of statements) - The body that carries out array computation.
name (str, optional) – The name hint of the tensor
dtype (str or list of str, optional) – The data types of outputs, by default dtype will be same as inputs.
in_buffers (tvm.ir.Var or list of tvm.ir.Var, optional) – Input buffers.
out_buffers (tvm.ir.Var or list of tvm.ir.Var, optional) – Output buffers.
- tag: str, optional
Additonal tag information about the compute.
- attrs: dict, optional
The additional auxiliary attributes about the compute.
- Returns:
tensor – The created tensor or tuple of tensors contains multiple outputs.
- Return type:
Example
In the code below, C is generated by calling external PackedFunc tvm.contrib.cblas.matmul
A = te.placeholder((n, l), name="A") B = te.placeholder((l, m), name="B") C = te.extern((n, m), [A, B], lambda ins, outs: tvm.tirx.call_packed( "tvm.contrib.cblas.matmul", ins[0], ins[1], outs[0], 0, 0), name="C")
- tvm.te.var(name='tindex', dtype='int32', span=None)#
Create a new variable with specified name and dtype
- tvm.te.const(value, dtype='int32', span=None)#
Create a new constant with specified value and dtype
- tvm.te.thread_axis(dom=None, tag='', name='', span=None)#
Create a new IterVar to represent thread index.
- Parameters:
- Returns:
axis – The thread itervar.
- Return type:
IterVar
- tvm.te.reduce_axis(dom, name='rv', thread_tag='', span=None)#
Create a new IterVar for reduction.
- tvm.te.create_function(ops: list[Tensor | Var], index_dtype_override: str | None = None) Function#
Create a TensorIR Function from tensor expression
- Parameters:
ops (List[Union[_tensor.Tensor, tvm.tirx.Var]]) – The source expression.
Example
We define a matmul kernel using following code:
import tvm from tvm import te from tvm.te import create_function import tvm.script A = te.placeholder((128, 128), name="A") B = te.placeholder((128, 128), name="B") k = te.reduce_axis((0, 128), "k") C = te.compute((128, 128), lambda x, y: te.sum(A[x, k] * B[y, k], axis=k), name="C") func = create_function([A, B, C]) print(func.script())
If we want to use TensorIR schedule to do transformations on such kernel, we need to use create_function([A, B, C]) to create a schedulable Function. The generated function looks like:
@Ts.function def tir_matmul( A: T.Tensor((128, 128)), B: T.Tensor((128, 128)), C: T.Tensor((128, 128)) ) -> None: for i, j, k in T.grid(128, 128, 128): with Ts.sblock(): vi, vj, vk = Ts.axis.remap("SSR", [i, j, k]) with Ts.init(): C[vi, vj] = 0.0 C[vi, vj] += A[vi, vk] * B[vj, vk]
- Returns:
func – The created function.
- Return type:
- tvm.te.extern_function(input_tensors: list[Tensor], function: Function, **kwargs)#
Compute tensors via a schedulable TIR Function
- Parameters:
- Returns:
tensor – The created tensor or tuple of tensors if it contains multiple outputs.
- Return type:
Example
In the code below, a TVMScript defined TIR Function is inlined into a TE ExternOp. Applying te.create_function on this
A = te.placeholder((128, 128), name="A") B = te.placeholder((128, 128), name="B") @Ts.function def before_split(A: T.Tensor((128, 128)), B: T.Tensor((128, 128))) -> None: for i, j in T.grid(128, 128): with Ts.sblock("B"): vi, vj = Ts.axis.remap("SS", [i, j]) B[vi, vj] = A[vi, vj] * 2.0 C = te.extern_function([A, B], func)
- class tvm.te.PlaceholderOp(name, tag, attrs, shape, dtype)#
Placeholder operation.
- class tvm.te.ComputeOp(name, tag, attrs, axis, reduce_axis, body)#
Scalar operation.
- class tvm.te.ScanOp(name, tag, attrs, scan_axis, init, update, state_placeholder, inputs, spatial_axis_)#
Scan operation.
- class tvm.te.ExternOp(name, tag, attrs, inputs, input_placeholders, output_placeholders, body)#
External operation.