tvm.relay.expr

The expression nodes of Relay.

tvm.relay.expr.var(name_hint, type_annotation=None, shape=None, dtype='float32')

Create a new tvm.relay.Var.

This is a simple wrapper function that allows specify shape and dtype directly.

Parameters:
  • name_hint (str) – The name of the variable. This name only acts as a hint, and is not used for equality.
  • type_annotation (Optional[tvm.relay.Type, str]) – The type annotation on the variable. When type_annotation is a str, we will create a scalar variable.
  • shape (Optional[List[tvm.Expr]]) – The shape of the tensor type.
  • dtype (str, optional) – The data type of the tensor.

Examples

# The following 4 lines are equivalent to each other
x = tvm.relay.Var("x", tvm.relay.TensorType([1, 2]))
x = tvm.relay.var("x", tvm.relay.TensorType([1, 2]))
x = tvm.relay.var("x", shape=[1, 2])
x = tvm.relay.var("x", shape=[1, 2], dtype="float32")

# The following 2 lines are equivalent to each other.
y = tvm.relay.var("x", "float32")
y = tvm.relay.var("x", shape=(), dtype="float32")
tvm.relay.expr.const(value, dtype=None)

Create a constant value.

Parameters:
  • value (Union[bool, int, float, numpy.ndarray, tvm.nd.NDArray]) – The constant value.
  • dtype (str, optional) – The data type of the value.

Note

When dtype is None, we use the following rule:

  • int maps to “int32”
  • float maps to “float32”
  • bool maps to “bool”
  • other using the same default rule as numpy.
tvm.relay.expr.bind(expr, binds)

Bind an free variables in expr or function arguments.

We can bind parameters expr if it is a function.

Parameters:
  • expr (tvm.relay.Expr) – The input expression.
  • binds (Union[Map[tvm.relay.Var, tvm.relay.Expr], Map[str, tvm.relay.Expr]]) – The specific bindings.
Returns:

result – The expression or function after binding.

Return type:

tvm.relay.Expr

class tvm.relay.expr.Expr

The base type for all Relay expressions.

astype(dtype)

Cast the content type of the current data to dtype.

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

Note

This function only works for TensorType Exprs.

Returns:result – The result expression.
Return type:tvm.relay.Expr
checked_type

Get the checked type of tvm.relay.Expr.

Returns:checked_type – The checked type.
Return type:tvm.relay.Type
class tvm.relay.expr.Constant(data)

A constant expression in Relay.

Parameters:data (tvm.nd.NDArray) – The data content of the constant expression.
class tvm.relay.expr.Tuple(fields)

Tuple expression that groups several fields together.

Parameters:fields (List[tvm.relay.Expr]) – The fields in the tuple.
astype(_)

Cast the content type of the current data to dtype.

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

Note

This function only works for TensorType Exprs.

Returns:result – The result expression.
Return type:tvm.relay.Expr
class tvm.relay.expr.Var(name_hint, type_annotation=None)

A local variable in Relay.

Local variable can be used to declare input arguments to a function, or intermediate variables.

Parameters:
  • name_hint (str) – The name of the variable. This name only acts as a hint, and is not used for equality.
  • type_annotation (tvm.relay.Type, optional) – The type annotation on the variable.
name_hint

Get name hint of the current var.

class tvm.relay.expr.GlobalVar(name_hint)

A global variable in Tvm.Relay.

GlobalVar is used to refer to the global functions stored in the module.

Parameters:name_hint (str) – The name of the variable.
class tvm.relay.expr.Function(params, body, ret_type=None, type_params=None, attrs=None)

A function declaration expression.

Parameters:
  • params (List[tvm.relay.Var]) – List of input parameters to the function.
  • body (tvm.relay.Expr) – The body of the function.
  • ret_type (Optional[tvm.relay.Type]) – The return type annotation of the function.
  • type_params (Optional[List[tvm.relay.TypeParam]]) – The additional type parameters, this is only used in advanced usecase of template functions.
class tvm.relay.expr.Call(op, args, attrs=None, type_args=None)

Function call node in Relay.

Call node corresponds the operator application node in computational graph terminology.

Parameters:
  • op (tvm.relay.Op or any tvm.relay.Expr with function type.) – The operation to be called.
  • args (List[tvm.relay.Expr]) – The arguments to the call.
  • attrs (Optional[tvm.Attrs]) – Attributes to the call, can be None
  • type_args (Optional[List[tvm.relay.Type]]) – The additional type arguments, this is only used in advanced usecase of template functions.
class tvm.relay.expr.Let(variable, value, body)

Let variable binding expression.

Parameters:
  • variable (tvm.relay.Var) – The local variable to be bound.
  • value (tvm.relay.Expr) – The value to be bound.
  • body (tvm.relay.Expr) – The body of the let binding.
class tvm.relay.expr.If(cond, true_branch, false_branch)

A conditional expression in Relay.

Parameters:
  • cond (tvm.relay.Expr) – The condition.
  • true_branch (tvm.relay.Expr) – The expression evaluated when condition is true.
  • false_branch (tvm.relay.Expr) – The expression evaluated when condition is false.
class tvm.relay.expr.TupleGetItem(tuple_value, index)

Get index-th item from a tuple.

Parameters:
  • tuple_value (tvm.relay.Expr) – The input tuple expression.
  • index (int) – The index.
class tvm.relay.expr.TempExpr

Baseclass of all TempExpr.

TempExprs are pass specific expression that can be useful to define intermediate result in the rewriting pass such as layout or type transformation.

realize()

Convert the expression to a normal(non-temp) Expr.

Returns:
Return type:The corresponding normal expression.