tvm.relay

The Relay IR namespace containing the IR definition and compiler.

Classes

Call(op, args[, attrs, type_args])

Function call node in Relay.

Clause(lhs, rhs)

Clause for pattern matching in Relay.

Constant(data)

A constant expression in Relay.

Expr

Base class of all non-primitive expressions.

ExprFunctor()

An abstract visitor defined over Expr.

ExprMutator()

A functional visitor over Expr.

ExprVisitor()

A visitor over Expr.

Function(params, body[, ret_type, …])

A function declaration expression.

If(cond, true_branch, false_branch)

A conditional expression in Relay.

Let(variable, value, body)

Let variable binding expression.

Match(data, clauses[, complete])

Pattern matching expression in Relay.

Pattern

Base type for pattern matching constructs.

PatternConstructor(constructor[, patterns])

Constructor pattern in Relay: Matches an ADT of the given constructor, binds recursively.

PatternTuple([patterns])

Constructor pattern in Relay: Matches a tuple, binds recursively.

PatternVar(var)

Variable pattern in Relay: Matches anything and binds it to the variable.

PatternWildcard()

Wildcard pattern in Relay: Matches any ADT and binds nothing.

Prelude([mod])

Contains standard definitions.

RefCreate(value)

Create a new reference from initial value.

RefRead(ref)

Get the value inside the reference.

RefType

Reference Type in relay.

RefWrite(ref, value)

Update the value inside the reference.

ScopeBuilder()

Scope builder class.

Tuple(fields)

Tuple expression that groups several fields together.

TupleGetItem(tuple_value, index)

Get index-th item from a tuple.

TupleWrapper(tuple_value, size)

TupleWrapper.

TypeData(header, type_vars, constructors)

Stores the definition for an Algebraic Data Type (ADT) in Relay.

TypeFunctor()

An abstract visitor defined over Type.

TypeMutator()

A functional visitor over Type.

TypeVisitor()

A visitor over Type.

Functions

ShapeVar(name)

A helper which constructs a type var of which the shape kind.

abs(data)

Compute element-wise absolute of data.

acos(data)

Compute elementwise acos of data.

acosh(data)

Compute elementwise acosh of data.

add(lhs, rhs)

Addition with numpy-style broadcasting.

all(data[, axis, keepdims, exclude])

Computes the logical AND of boolean array elements over given axes.

any(data[, axis, keepdims, exclude])

Computes the logical OR of boolean array elements over given axes.

arange(start[, stop, step, dtype])

Return evenly spaced values within a given interval.

argmax(data[, axis, keepdims, exclude])

Returns the indices of the maximum values along an axis.

argmin(data[, axis, keepdims, exclude])

Returns the indices of the minimum values along an axis.

argsort(data[, axis, is_ascend, dtype])

Performs sorting along the given axis and returns an array of indicies having same shape as an input array that index data in sorted order.

argwhere(condition)

Find the indices of elements of a tensor that are non-zero.

asin(data)

Compute elementwise asin of data.

asinh(data)

Compute elementwise asinh of data.

atan(data)

Compute elementwise atan of data.

atanh(data)

Compute elementwise atanh of data.

bind(expr, binds)

Bind an free variables in expr or function arguments.

bitwise_and(lhs, rhs)

bitwise AND with numpy-style broadcasting.

bitwise_not(data)

Compute element-wise bitwise not of data.

bitwise_or(lhs, rhs)

bitwise OR with numpy-style broadcasting.

bitwise_xor(lhs, rhs)

bitwise XOR with numpy-style broadcasting.

broadcast_to(data, shape)

Return a scalar value array with the same type, broadcast to the provided shape.

broadcast_to_like(data, broadcast_type)

Return a scalar value array with the same shape and type as the input array.

build(mod[, target, target_host, params])

Helper function that builds a Relay function to run on TVM graph runtime.

build_config([opt_level, required_pass, …])

Configure the build behavior by setting config variables.

cast(data, dtype)

Cast input tensor to data type.

cast_like(data, dtype_like)

Cast input tensor to data type of another tensor.

ceil(data)

Compute element-wise ceil of data.

clip(a, a_min, a_max)

Clip the elements in a between a_min and a_max.

collapse_sum_like(data, collapse_type)

Return a scalar value array with the same shape and type as the input array.

collapse_sum_to(data, shape)

Return a summation of data to the specified shape.

concatenate(data, axis)

Concatenate the input tensors along the given axis.

const(value[, dtype])

Create a constant value.

copy(data)

Copy a tensor.

cos(data)

Compute elementwise cos of data.

cosh(data)

Compute elementwise cosh of data.

create_executor([kind, mod, ctx, target])

Factory function to create an executor.

device_copy(data, src_dev, dst_dev)

Copy data from the source device to the destination device.

divide(lhs, rhs)

Division with numpy-style broadcasting.

equal(lhs, rhs)

Broadcasted elementwise test for (lhs == rhs).

erf(data)

Compute elementwise error function of data.

exp(data)

Compute elementwise exp of data.

expand_dims(data, axis[, num_newaxis])

Insert num_newaxis axises at the position given by axis.

floor(data)

Compute element-wise floor of data.

floor_divide(lhs, rhs)

Floor division with numpy-style broadcasting.

floor_mod(lhs, rhs)

Floor mod with numpy-style broadcasting.

full(fill_value[, shape, dtype])

Fill array with scalar value.

full_like(data, fill_value)

Return a scalar value array with the same shape and type as the input array.

gather(data, axis, indices)

Gather values along given axis from given indices.

gather_nd(data, indices)

Gather elements or slices from data and store to a tensor whose shape is defined by indices.

greater(lhs, rhs)

Broadcasted elementwise test for (lhs > rhs).

greater_equal(lhs, rhs)

Broadcasted elementwise test for (lhs >= rhs).

isfinite(data)

Compute element-wise finiteness of data.

isinf(data)

Compute element-wise infiniteness of data.

isnan(data)

Check nan in input data element-wise.

layout_transform(data, src_layout, dst_layout)

Transform the layout of a tensor

left_shift(lhs, rhs)

Left shift with numpy-style broadcasting.

less(lhs, rhs)

Broadcasted elementwise test for (lhs < rhs).

less_equal(lhs, rhs)

Broadcasted elementwise test for (lhs <= rhs).

load_param_dict(param_bytes)

Load parameter dictionary to binary bytes.

log(data)

Compute elementwise log of data.

log10(data)

Compute elementwise log to the base 10 of data.

log2(data)

Compute elementwise log to the base 2 of data.

logical_and(lhs, rhs)

logical AND with numpy-style broadcasting.

logical_not(data)

Compute element-wise logical not of data.

logical_or(lhs, rhs)

logical OR with numpy-style broadcasting.

logical_xor(lhs, rhs)

logical XOR with numpy-style broadcasting.

logsumexp(data[, axis, keepdims])

Compute the log of the sum of exponentials of input elements over given axes.

max(data[, axis, keepdims, exclude])

Computes the max of array elements over given axes.

maximum(lhs, rhs)

Maximum with numpy-style broadcasting.

mean(data[, axis, keepdims, exclude])

Computes the mean of array elements over given axes.

mean_std(data[, axis, keepdims, exclude])

Computes the mean and standard deviation of data over given axes.

mean_variance(data[, axis, keepdims, exclude])

Computes the mean and variance of data over given axes.

meshgrid(data[, indexing])

Create coordinate matrices from coordinate vectors.

min(data[, axis, keepdims, exclude])

Computes the min of array elements over given axes.

minimum(lhs, rhs)

Minimum with numpy-style broadcasting.

mod(lhs, rhs)

Mod with numpy-style broadcasting.

multiply(lhs, rhs)

Multiplication with numpy-style broadcasting.

ndarray_size(data[, dtype])

Get number of elements of input tensor.

negative(data)

Compute element-wise negative of data.

not_equal(lhs, rhs)

Broadcasted elementwise test for (lhs != rhs).

one_hot(indices, on_value, off_value, depth, …)

Returns a one-hot tensor where the locations repsented by indices take value on_value, other locations take value off_value.

ones(shape, dtype)

Fill array with ones.

ones_like(data)

Returns an array of ones, with same type and shape as the input.

optimize(mod[, target, params])

Helper function that optimizes a Relay module.

power(lhs, rhs)

Power with numpy-style broadcasting.

prod(data[, axis, keepdims, exclude])

Computes the products of array elements over given axes.

reinterpret(data, dtype)

Reinterpret input tensor to data type.

repeat(data, repeats, axis)

Repeats elements of an array.

reshape(data, newshape)

Reshape the input array.

reshape_like(data, shape_like)

Reshapes the input array by the size of another array.

reverse(data, axis)

Reverses the order of elements along given axis while preserving array shape.

reverse_reshape(data, newshape)

Reshapes the input array where the special values are inferred from right to left.

reverse_sequence(data, seq_lengths[, …])

Reverse the tensor for variable length slices.

right_shift(lhs, rhs)

Right shift with numpy-style broadcasting.

round(data)

Compute element-wise round of data.

rsqrt(data)

Compute elementwise rsqrt of data.

save_param_dict(params)

Save parameter dictionary to binary bytes.

scalar_type(dtype)

Creates a scalar type.

scatter(data, indices, updates, axis)

Update data at positions defined by indices with values in updates

sequence_mask(data, valid_length[, …])

Sets all elements outside the expected length of the sequence to a constant value.

setrecursionlimit(n)

Set the maximum depth of the Python interpreter stack to n.

shape_of(data[, dtype])

Get shape of a tensor.

sigmoid(data)

Compute elementwise sigmoid of data.

sign(data)

Compute element-wise absolute of data.

sin(data)

Compute elementwise sin of data.

sinh(data)

Compute elementwise sinh of data.

slice_like(data, shape_like[, axes])

Slice the first input with respect to the second input.

sparse_to_dense(sparse_indices, …[, …])

Converts a sparse representation into a dense tensor.

split(data, indices_or_sections[, axis])

Split input tensor along axis by sections or indices.

sqrt(data)

Compute elementwise sqrt of data.

squeeze(data[, axis])

Squeeze axes in the array.

stack(data, axis)

Join a sequence of arrays along a new axis.

std(data[, axis, keepdims, exclude])

Computes the standard deviation of data over given axes.

strided_set(data, v, begin, end[, strides])

Strided set of an array.

strided_slice(data, begin, end[, strides, …])

Strided slice of an array.

subtract(lhs, rhs)

Subtraction with numpy-style broadcasting.

sum(data[, axis, keepdims, exclude])

Computes the sum of array elements over given axes.

take(data, indices[, axis, mode])

Take elements from an array along an axis.

tan(data)

Compute elementwise tan of data.

tanh(data)

Compute element-wise tanh of data.

tile(data, reps)

Repeats the whole array multiple times.

topk(data[, k, axis, ret_type, is_ascend, dtype])

Get the top k elements in an input tensor along the given axis.

transpose(data[, axes])

Permutes the dimensions of an array.

trunc(data)

Compute element-wise trunc of data.

unravel_index(indices, shape)

Convert a flat index or array of flat indices into a tuple of coordinate arrays.

var(name_hint[, type_annotation, shape, dtype])

Create a new tvm.relay.Var.

variance(data[, axis, keepdims, exclude])

Computes the variance of data over given axes.

where(condition, x, y)

Selecting elements from either x or y depending on the value of the condition.

zeros(shape, dtype)

Fill array with zeros.

zeros_like(data)

Returns an array of zeros, with same type and shape as the input.

tvm.relay.setrecursionlimit(n)

Set the maximum depth of the Python interpreter stack to n. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python. The highest possible limit is platform- dependent.

tvm.relay.build(mod, target=None, target_host=None, params=None)

Helper function that builds a Relay function to run on TVM graph runtime.

Parameters
  • mod (IRModule) – The IR module to build. Using relay.Function is deprecated.

  • target (str, tvm.target.Target, or dict of str(i.e. device/context) –

  • to str/tvm.target.Target, optional (name)) – For heterogeneous compilation, it is a dictionary indicating context to target mapping. For homogeneous compilation, it is a build target.

  • target_host (str or tvm.target.Target, optional) – Host compilation target, if target is device. When TVM compiles device specific program such as CUDA, we also need host(CPU) side code to interact with the driver setup the dimensions and parameters correctly. target_host is used to specify the host side codegen target. By default, llvm is used if it is enabled, otherwise a stackvm intepreter is used.

  • params (dict of str to NDArray) – Input parameters to the graph that do not change during inference time. Used for constant folding.

Returns

  • graph_json (str) – The json string that can be accepted by graph runtime.

  • mod (tvm.Module) – The module containing necessary libraries.

  • params (dict) – The parameters of the final graph.

tvm.relay.create_executor(kind='debug', mod=None, ctx=None, target='llvm')

Factory function to create an executor.

Parameters
  • kind (str) – The type of executor

  • mod (IRModule) – The Relay module containing collection of functions

  • ctx (tvmContext) – The context to execute the code.

  • target (tvm.Target) – The corresponding context

tvm.relay.optimize(mod, target=None, params=None)

Helper function that optimizes a Relay module.

Parameters
  • mod (IRModule) – The module to build. Using relay.Function is deprecated.

  • target (str, tvm.target.Target, or dict of str(i.e. device/context) –

  • to str/tvm.target.Target, optional (name)) – For heterogeneous compilation, it is a dictionary indicating context to target mapping. For homogeneous compilation, it is a build target.

  • params (dict of str to NDArray) – Input parameters to the graph that do not change during inference time. Used for constant folding.

Returns

  • mod (IRModule) – The optimized relay module.

  • params (dict) – The parameters of the final graph.

tvm.relay.build_config(opt_level=2, required_pass=None, disabled_pass=None, trace=None)

Configure the build behavior by setting config variables. This function will be deprecated in TVM v0.7. Instead, we should directly use tvm.transform.PassContext.

Parameters
  • opt_level (int, optional) –

    Optimization level. The optimization pass name and level are as the following:

    OPT_PASS_LEVEL = {
        "SimplifyInference": 0,
        "OpFusion": 1,
        "FoldConstant": 2,
        "FoldScaleAxis": 3,
        "AlterOpLayout": 3,
        "CanonicalizeOps": 3,
        "CanonicalizeCast": 3,
        "EliminateCommonSubexpr": 3,
        "CombineParallelConv2D": 4,
        "CombineParallelDense": 4,
        "CombineParallelBatchMatmul": 4,
        "FastMath": 4
    }
    

  • required_pass (set of str, optional) – Optimization passes that are required regardless of optimization level.

  • disabled_pass (set of str, optional) – Optimization passes to be disabled during optimization.

  • trace (Callable[[IRModule, PassInfo, bool], None]) – A tracing function for debugging or introspection.

Returns

pass_context – The pass context for optimizations.

Return type

PassContext

tvm.relay.ShapeVar(name)

A helper which constructs a type var of which the shape kind.

Parameters

name (str) –

Returns

type_var – The shape variable.

Return type

tvm.relay.TypeVar

tvm.relay.scalar_type(dtype)

Creates a scalar type.

This function returns TensorType((), dtype)

Parameters

dtype (str) – The content data type.

Returns

s_type – The result type.

Return type

tvm.relay.TensorType

tvm.relay.RefType

alias of tvm.ir.type.RelayRefType

tvm.relay.Expr

checked_type

Get the checked type of tvm.relay.Expr.

Attributes

alias of tvm.ir.expr.RelayExpr

class tvm.relay.Constant(data)

A constant expression in Relay.

Parameters

data (tvm.nd.NDArray) – The data content of the constant expression.

class tvm.relay.Tuple(fields)

Tuple expression that groups several fields together.

Parameters

fields (List[tvm.relay.Expr]) – The fields in the tuple.

Methods

astype(_)

Cast the content type of the current data to dtype.

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.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.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.ir.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.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.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.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.RefCreate(value)

Create a new reference from initial value. :param value: The initial value. :type value: tvm.relay.Expr

class tvm.relay.RefRead(ref)

Get the value inside the reference. :param ref: The reference. :type ref: tvm.relay.Expr

class tvm.relay.RefWrite(ref, value)

Update the value inside the reference. The whole expression will evaluate to an empty tuple. :param ref: The reference. :type ref: tvm.relay.Expr :param value: The new value. :type value: tvm.relay.Expr

class tvm.relay.Pattern

Base type for pattern matching constructs.

class tvm.relay.PatternWildcard

Wildcard pattern in Relay: Matches any ADT and binds nothing.

class tvm.relay.PatternVar(var)

Variable pattern in Relay: Matches anything and binds it to the variable.

class tvm.relay.PatternConstructor(constructor, patterns=None)

Constructor pattern in Relay: Matches an ADT of the given constructor, binds recursively.

class tvm.relay.PatternTuple(patterns=None)

Constructor pattern in Relay: Matches a tuple, binds recursively.

class tvm.relay.TypeData(header, type_vars, constructors)

Stores the definition for an Algebraic Data Type (ADT) in Relay.

Note that ADT definitions are treated as type-level functions because the type parameters need to be given for an instance of the ADT. Thus, any global type var that is an ADT header needs to be wrapped in a type call that passes in the type params.

Parameters
  • header (GlobalTypeVar) – The name of the ADT. ADTs with the same constructors but different names are treated as different types.

  • type_vars (List[TypeVar]) – Type variables that appear in constructors.

  • constructors (List[Constructor]) – The constructors for the ADT.

Alias of tvm.ir.TypeData()

class tvm.relay.Clause(lhs, rhs)

Clause for pattern matching in Relay.

class tvm.relay.Match(data, clauses, complete=True)

Pattern matching expression in Relay.

tvm.relay.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.const(value, dtype=None)

Create a constant value.

Parameters

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

An abstract visitor defined over Type.

Defines the default dispatch over types.

Methods

visit(typ)

Apply the visitor to a type.

visit(typ)

Apply the visitor to a type.

class tvm.relay.TypeVisitor

A visitor over Type.

The default behavior recursively traverses the AST.

class tvm.relay.TypeMutator

A functional visitor over Type.

The default behavior recursively traverses the AST and reconstructs the AST.

class tvm.relay.ExprFunctor

An abstract visitor defined over Expr.

Defines the default dispatch over expressions, and implements memoization.

Methods

visit(expr)

Apply the visitor to an expression.

visit(expr)

Apply the visitor to an expression.

class tvm.relay.ExprVisitor

A visitor over Expr.

The default behavior recursively traverses the AST.

class tvm.relay.ExprMutator

A functional visitor over Expr.

The default behavior recursively traverses the AST and reconstructs the AST.

class tvm.relay.Prelude(mod=None)

Contains standard definitions.

Methods

get_name(canonical, dtype)

Get name corresponding to the canonical name

get_name_static(canonical, dtype, shape)

Get name corresponding to the canonical name

get_var(canonical, dtype)

Get var corresponding to the canonical name

get_var_static(canonical, dtype, shape)

Get var corresponding to the canonical name

load_prelude()

Parses the Prelude from Relay’s text format into a module.

get_name(canonical, dtype)

Get name corresponding to the canonical name

get_var(canonical, dtype)

Get var corresponding to the canonical name

get_name_static(canonical, dtype, shape)

Get name corresponding to the canonical name

get_var_static(canonical, dtype, shape)

Get var corresponding to the canonical name

load_prelude()

Parses the Prelude from Relay’s text format into a module.

class tvm.relay.ScopeBuilder

Scope builder class.

Enables users to build up a nested scope(let, if) expression easily.

Examples

Methods

else_scope()

Create a new else scope.

get()

Get the generated result.

if_scope(cond)

Create a new if scope.

let(var, value)

Create a new let binding.

ret(value)

Set the return value of this scope.

type_of(expr)

Compute the type of an expression.

let(var, value)

Create a new let binding.

Parameters
  • var (Union[Tuple[str, relay.Type], tvm.relay.Var]) – The variable or name of variable.

  • value (tvm.relay.Expr) – The value to be binded

if_scope(cond)

Create a new if scope.

Parameters

cond (tvm.relay.expr.Expr) – The condition

Returns

scope – The if scope.

Return type

WithScope

Note

The user must follows with an else scope.

else_scope()

Create a new else scope.

Returns

scope – The if scope.

Return type

WithScope

type_of(expr)

Compute the type of an expression.

Parameters

expr (relay.Expr) – The expression to compute the type of.

ret(value)

Set the return value of this scope.

Parameters

value (tvm.relay.expr.Expr) – The return value.

get()

Get the generated result.

Returns

value – The final result of the expression.

Return type

tvm.relay.expr.Expr

class tvm.relay.TupleWrapper(tuple_value, size)

TupleWrapper.

This class is a Python wrapper for a Relay tuple of known size. It allows for accessing the fields of the Relay tuple as though it were a Python tuple.

Parameters
  • tuple_value (tvm.relay.Expr) – The input tuple

  • size (int) – The size of the tuple.

Methods

astext()

Get the text format of the tuple expression.

astuple()

Returns the underlying Relay tuple if this wrapper is passed as an argument to an FFI function.

astuple()

Returns the underlying Relay tuple if this wrapper is passed as an argument to an FFI function.

astext()

Get the text format of the tuple expression.

Returns

text – The text format of the tuple expression.

Return type

str

tvm.relay.abs(data)

Compute element-wise absolute of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.acos(data)

Compute elementwise acos of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.acosh(data)

Compute elementwise acosh of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.add(lhs, rhs)

Addition with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

Examples

x = relay.Var("a") # shape is [2, 3]
y = relay.Var("b") # shape is [2, 1]
z = relay.add(x, y)  # result shape is [2, 3]
tvm.relay.all(data, axis=None, keepdims=False, exclude=False)

Computes the logical AND of boolean array elements over given axes.

Parameters
  • data (relay.Expr) – The input boolean tensor

  • axis (None or int or tuple of int) – Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • exclude (bool) – If exclude is true, reduction will be performed on the axes that are NOT in axis instead.

Returns

result – The computed result.

Return type

relay.Expr

Examples

data = relay.Constant(tvm.nd.array([[[ True,  True,  True],
                                   [ True,  True,  True],
                                   [False,  True, False]],
                                  [[ True, False, False],
                                   [ True,  True, False],
                                   [False,  True,  True]]]))

relay.all(data, axis=1)
# [[False,  True, False],
# [False, False, False]]

relay.all(data, axis=0)
# [[ True, False, False],
# [ True,  True, False],
# [False,  True, False]]
tvm.relay.any(data, axis=None, keepdims=False, exclude=False)

Computes the logical OR of boolean array elements over given axes.

Parameters
  • data (relay.Expr) – The input boolean tensor

  • axis (None or int or tuple of int) – Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • exclude (bool) – If exclude is true, reduction will be performed on the axes that are NOT in axis instead.

Returns

result – The computed result.

Return type

relay.Expr

Examples

data = relay.Constant(tvm.nd.array([[[ True,  True,  True],
                                    [ True,  True,  True],
                                    [False,  True, False]],
                                    [[ True, False, False],
                                    [ True,  True, False],
                                    [False,  True,  True]]]))

relay.any(data, axis=1)
# [[True, True, True],
# [True,  True, True]]

relay.any(data, axis=0)
# [[ True, True, True],
# [ True,  True, True],
# [False,  True, True]]
tvm.relay.arange(start, stop=None, step=None, dtype='float32')

Return evenly spaced values within a given interval.

Note

Similar to numpy.arange, when only one argument is given, it is used as stop instead of start while start takes default value 0.

Warning: Undefined behavior when dtype is incompatible with start/stop/step. It could lead to different results compared to numpy, MXNet, pytorch, etc.

Parameters
  • start (tvm.Expr, optional) – Start of interval. The interval includes this value. The default start value is 0.

  • stop (tvm.Expr) – Stop of interval. The interval does not include this value.

  • step (tvm.Expr, optional) – Spacing between values. The default step size is 1.

  • dtype (str, optional) – The target data type.

Returns

result – The resulting tensor.

Return type

relay.Expr

Examples

relay.arange(5) = [0, 1, 2, 3, 4]
relay.arange(1, 5) = [1, 2, 3, 4]
relay.arange(1, 5, 1.5) = [1, 2.5, 4]
tvm.relay.argmax(data, axis=None, keepdims=False, exclude=False)

Returns the indices of the maximum values along an axis.

Parameters
  • data (relay.Expr) – The input data

  • axis (None or int or tuple of int) – Axis or axes along which a argmax operation is performed. The default, axis=None, will find the indices of the maximum element of the elements of the input array. If axis is negative it counts from the last to the first axis.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • exclude (bool) – If exclude is true, reduction will be performed on the axes that are NOT in axis instead.

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.argmin(data, axis=None, keepdims=False, exclude=False)

Returns the indices of the minimum values along an axis.

Parameters
  • data (relay.Expr) – The input data

  • axis (None or int or tuple of int) – Axis or axes along which a argmin operation is performed. The default, axis=None, will find the indices of minimum element all of the elements of the input array. If axis is negative it counts from the last to the first axis.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • exclude (bool) – If exclude is true, reduction will be performed on the axes that are NOT in axis instead.

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.argsort(data, axis=- 1, is_ascend=1, dtype='int32')

Performs sorting along the given axis and returns an array of indicies having same shape as an input array that index data in sorted order.

Parameters
  • data (relay.Expr) – The input data tensor.

  • valid_count (tvm.te.Tensor) – The number of valid elements to be sorted.

  • axis (int, optional) – Axis long which to sort the input tensor.

  • is_ascend (boolean, optional) – Whether to sort in ascending or descending order.

  • dtype (string, optional) – The data type of the output indices.

Returns

out – Tensor with same shape as data.

Return type

relay.Expr

tvm.relay.argwhere(condition)

Find the indices of elements of a tensor that are non-zero.

Parameters

condition (relay.Expr) – The input condition tensor.

Returns

out – Tensor with the indices of elements that are non-zero.

Return type

relay.Expr

Examples

condition = [[True, False], [False, True]]
relay.argwhere(condition) = [[0, 0], [1, 1]]
tvm.relay.asin(data)

Compute elementwise asin of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.asinh(data)

Compute elementwise asinh of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.atan(data)

Compute elementwise atan of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.atanh(data)

Compute elementwise atanh of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.bitwise_and(lhs, rhs)

bitwise AND with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.bitwise_not(data)

Compute element-wise bitwise not of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.bitwise_or(lhs, rhs)

bitwise OR with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.bitwise_xor(lhs, rhs)

bitwise XOR with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.broadcast_to(data, shape)

Return a scalar value array with the same type, broadcast to the provided shape.

Parameters
  • data (relay.Expr) – The input tensor.

  • shape (tuple of int or relay.Expr) – Provide the shape to broadcast to.

Returns

result – The resulting tensor.

Return type

relay.Expr

tvm.relay.broadcast_to_like(data, broadcast_type)

Return a scalar value array with the same shape and type as the input array.

Parameters
  • data (relay.Expr) – The input tensor.

  • broadcast_type (relay.Expr) – Provide the type to broadcast to.

Returns

result – The resulting tensor.

Return type

relay.Expr

tvm.relay.cast(data, dtype)

Cast input tensor to data type.

Parameters
  • data (relay.Expr) – The input data to the operator.

  • dtype (str) – The target data type

Returns

result – The casted result.

Return type

relay.Expr

tvm.relay.cast_like(data, dtype_like)

Cast input tensor to data type of another tensor. :param data: The input data to the operator. :type data: relay.Expr :param dtype_like: The tensor to cast to. :type dtype_like: relay.Expr

Returns

result – The casted result.

Return type

relay.Expr

tvm.relay.ceil(data)

Compute element-wise ceil of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.clip(a, a_min, a_max)

Clip the elements in a between a_min and a_max. a_min and a_max are cast to a’s dtype.

Parameters
  • a (relay.Expr) – The input tensor.

  • a_min (float) – The clip minimum.

  • a_max (float) – The clip maximum.

Returns

resulta with elements clipped between a_min and a_max.

Return type

relay.Expr

Examples

x = relay.Constant(tvm.nd.array([0, 1, 5, 3, 4, 2]))
relay.clip(x, 1., 4.)
# [1, 1, 4, 3, 4, 2]
tvm.relay.collapse_sum_like(data, collapse_type)

Return a scalar value array with the same shape and type as the input array.

Parameters
  • data (relay.Expr) – The input tensor.

  • collapse_type (relay.Expr) – Provide the type to collapse to.

Returns

result – The resulting tensor.

Return type

relay.Expr

tvm.relay.collapse_sum_to(data, shape)

Return a summation of data to the specified shape.

Parameters
  • data (relay.Expr) – The input tensor.

  • shape (relay.Expr) – Shape to collapse to.

Returns

result – The resulting tensor.

Return type

relay.Expr

tvm.relay.concatenate(data, axis)

Concatenate the input tensors along the given axis.

Parameters
  • data (Union(List[relay.Expr], Tuple[relay.Expr])) – A list of tensors.

  • axis (int) – The axis along which the tensors are concatenated.

Returns

result – The concatenated tensor.

Return type

relay.Expr

tvm.relay.copy(data)

Copy a tensor.

Parameters

data (relay.Expr) – The tensor to be copied.

Returns

result – The copied result.

Return type

relay.Expr

tvm.relay.cos(data)

Compute elementwise cos of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.cosh(data)

Compute elementwise cosh of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.device_copy(data, src_dev, dst_dev)

Copy data from the source device to the destination device. This operator helps data transferring between difference contexts for heterogeneous execution.

Parameters
  • data (tvm.relay.Expr) – The tensor to be copied.

  • src_dev (Union[TVMContext, str]) – The source device where the data is copied from.

  • dst_dev (Union[TVMContext, str]) – The destination device where the data is copied to.

Returns

result – The copied result.

Return type

tvm.relay.Expr

tvm.relay.divide(lhs, rhs)

Division with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.equal(lhs, rhs)

Broadcasted elementwise test for (lhs == rhs).

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.erf(data)

Compute elementwise error function of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.exp(data)

Compute elementwise exp of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.expand_dims(data, axis, num_newaxis=1)

Insert num_newaxis axises at the position given by axis.

Parameters
  • data (relay.Expr) – The input data to the operator.

  • axis (int) – The axis at which the input array is expanded. Should lie in range [-data.ndim - 1, data.ndim]. If axis < 0, it is the first axis inserted; If axis >= 0, it is the last axis inserted in Python’s negative indexing.

  • num_newaxis (int) – Number of axes to be inserted. Should be >= 0.

Returns

result – The reshaped result.

Return type

relay.Expr

tvm.relay.floor(data)

Compute element-wise floor of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.floor_divide(lhs, rhs)

Floor division with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.floor_mod(lhs, rhs)

Floor mod with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.full(fill_value, shape=(), dtype='')

Fill array with scalar value.

Parameters
  • fill_value (relay.Expr) – The value to fill. Must be a scalar.

  • shape (tuple of int or relay.Expr) – The shape of the target.

  • dtype (data type, optional (defaults to data type of the fill value)) – The data type of the target.

Returns

result – The resulting tensor.

Return type

relay.Expr

tvm.relay.full_like(data, fill_value)

Return a scalar value array with the same shape and type as the input array.

Parameters
  • data (relay.Expr) – The input tensor.

  • fill_value (relay.Expr) – The scalar value to fill.

Returns

result – The resulting tensor.

Return type

relay.Expr

tvm.relay.gather(data, axis, indices)

Gather values along given axis from given indices.

E.g. for a 3D tensor, output is computed as:

out[i][j][k] = data[indices[i][j][k]][j][k]  # if axis == 0
out[i][j][k] = data[i][indices[i][j][k]][k]  # if axis == 1
out[i][j][k] = data[i][j][indices[i][j][k]]  # if axis == 2

indices must have same shape as data, except at dimension axis which must just be not null. Output will have same shape as indices.

Parameters
  • data (relay.Expr) – The input data to the operator.

  • axis (int) – The axis along which to index.

  • indices (relay.Expr) – The indices of values to gather.

Examples

data = [[1, 2], [3, 4]]
axis = 1
indices = [[0, 0], [1, 0]]
relay.gather(data, axis, indices) = [[1, 1], [4, 3]]
tvm.relay.gather_nd(data, indices)

Gather elements or slices from data and store to a tensor whose shape is defined by indices.

Parameters
  • data (relay.Expr) – The input data to the operator.

  • indices (relay.Expr) – The shape of output tensor.

Returns

ret – The computed result.

Return type

relay.Expr

Examples

data = [[0, 1], [2, 3]]
indices = [[1, 1, 0], [0, 1, 0]]
relay.gather_nd(data, indices) = [2, 3, 0]

data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
indices = [[0, 1], [1, 0]]
relay.gather_nd(data, indices) = [[3, 4], [5, 6]]
tvm.relay.greater(lhs, rhs)

Broadcasted elementwise test for (lhs > rhs).

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.greater_equal(lhs, rhs)

Broadcasted elementwise test for (lhs >= rhs).

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.isfinite(data)

Compute element-wise finiteness of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.isinf(data)

Compute element-wise infiniteness of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.isnan(data)

Check nan in input data element-wise.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.layout_transform(data, src_layout, dst_layout)

Transform the layout of a tensor

Parameters
  • data (relay.Expr) – The source tensor to be transformed

  • src_layout (str) – The source layout. (e.g NCHW)

  • dst_layout (str) – The destination layout. (e.g. NCHW16c)

Returns

ret – The transformed tensor.

Return type

relay.Expr

tvm.relay.left_shift(lhs, rhs)

Left shift with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.less(lhs, rhs)

Broadcasted elementwise test for (lhs < rhs).

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.less_equal(lhs, rhs)

Broadcasted elementwise test for (lhs <= rhs).

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.log(data)

Compute elementwise log of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.log10(data)

Compute elementwise log to the base 10 of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.log2(data)

Compute elementwise log to the base 2 of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.logical_and(lhs, rhs)

logical AND with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.logical_not(data)

Compute element-wise logical not of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.logical_or(lhs, rhs)

logical OR with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.logical_xor(lhs, rhs)

logical XOR with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.logsumexp(data, axis=None, keepdims=False)

Compute the log of the sum of exponentials of input elements over given axes.

This function is more numerically stable than log(sum(exp(input))). It avoids overflows caused by taking the exp of large inputs and underflows caused by taking the log of small inputs.

Parameters
  • data (relay.Expr) – The input data

  • axis (None or int or tuple of int) – Axis or axes along which a standard deviation operation is performed. The default, axis=None, will compute the log of the sum of exponentials of all elements in the input array. If axis is negative it counts from the last to the first axis.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one.

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.max(data, axis=None, keepdims=False, exclude=False)

Computes the max of array elements over given axes.

Parameters
  • data (relay.Expr) – The input data

  • axis (None or int or tuple of int) – Axis or axes along which the max operation is performed. The default, axis=None, will find the max element from all of the elements of the input array. If axis is negative it counts from the last to the first axis.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • exclude (bool) – If exclude is true, reduction will be performed on the axes that are NOT in axis instead.

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.maximum(lhs, rhs)

Maximum with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.mean(data, axis=None, keepdims=False, exclude=False)

Computes the mean of array elements over given axes.

Parameters
  • data (relay.Expr) – The input data

  • axis (None or int or tuple of int) – Axis or axes along which a mean operation is performed. The default, axis=None, will compute the mean of all elements in the input array. If axis is negative it counts from the last to the first axis.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • exclude (bool) – If exclude is true, reduction will be performed on the axes that are NOT in axis instead.

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.mean_std(data, axis=None, keepdims=False, exclude=False)

Computes the mean and standard deviation of data over given axes.

Parameters
  • data (relay.Expr) – The input data

  • axis (None or int or tuple of int) – Axis or axes along which a mean and standard deviation operation is performed. The default, axis=None, will compute the mean and standard deviation of all elements in the input array. If axis is negative it counts from the last to the first axis.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • exclude (bool) – If exclude is true, reduction will be performed on the axes that are NOT in axis instead.

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.mean_variance(data, axis=None, keepdims=False, exclude=False)

Computes the mean and variance of data over given axes.

Parameters
  • data (relay.Expr) – The input data

  • axis (None or int or tuple of int) – Axis or axes along which a mean and variance operation is performed. The default, axis=None, will compute the mean and variance of all elements in the input array. If axis is negative it counts from the last to the first axis.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • exclude (bool) – If exclude is true, reduction will be performed on the axes that are NOT in axis instead.

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.meshgrid(data, indexing='ij')

Create coordinate matrices from coordinate vectors.

Note

Similar to numpy.meshgrid.

Parameters
  • data (Union(List[relay.Expr], Tuple[relay.Expr])) – A list of tensors, which must be either scalars or 1-D vectors.

  • indexing (str) – Indexing mode, either “ij” for matrix indexing or “xy” for Cartesian indexing.

Returns

ret – The computed result.

Return type

relay.Tuple([relay.Expr, relay.Expr])

Examples

x = [1, 2, 3]
y = [4, 5]

gx, gy = relay.meshgrid([x, y])

gx = [[1., 1.],
      [2., 2.],
      [3., 3.]]

gy = [[4., 5.],
      [4., 5.],
      [4., 5.]]
tvm.relay.min(data, axis=None, keepdims=False, exclude=False)

Computes the min of array elements over given axes.

Parameters
  • data (relay.Expr) – The input data

  • axis (None or int or tuple of int) – Axis or axes along which a minimum operation is performed. The default, axis=None, will find the minimum element from all of the elements of the input array. If axis is negative it counts from the last to the first axis.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • exclude (bool) – If exclude is true, reduction will be performed on the axes that are NOT in axis instead.

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.minimum(lhs, rhs)

Minimum with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.mod(lhs, rhs)

Mod with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.multiply(lhs, rhs)

Multiplication with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.ndarray_size(data, dtype='int32')

Get number of elements of input tensor.

Parameters
  • data (tvm.relay.Expr) – The input tensor.

  • dtype (str, optional) – The target data type.

Returns

result – The number of elements of input tensor.

Return type

tvm.relay.Expr

tvm.relay.negative(data)

Compute element-wise negative of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.not_equal(lhs, rhs)

Broadcasted elementwise test for (lhs != rhs).

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.one_hot(indices, on_value, off_value, depth, axis, dtype)

Returns a one-hot tensor where the locations repsented by indices take value on_value, other locations take value off_value. Final dimension is <indices outer dimensions> x depth x <indices inner dimensions>.

Parameters
  • indices (relay.Expr) – Locations to set to on_value.

  • on_value (relay.Expr) – Value to fill at indices.

  • off_value (relay.Expr) – Value to fill at all other positions besides indices.

  • depth (int) – Depth of the one-hot dimension.

  • axis (int) – Axis to fill.

  • dtype (str) – Data type of the output tensor.

Returns

ret – The one-hot tensor.

Return type

relay.Expr

Examples

indices = [0, 1, 2]

relay.one_hot(indices, 3) =
    [[1, 0, 0],
     [0, 1, 0],
     [0, 0, 1]]
tvm.relay.ones(shape, dtype)

Fill array with ones.

Parameters
  • shape (tuple of int or relay.Expr) – The shape of the target.

  • dtype (data type) – The data type of the target.

Returns

result – The resulting tensor.

Return type

relay.Expr

tvm.relay.ones_like(data)

Returns an array of ones, with same type and shape as the input.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.power(lhs, rhs)

Power with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.prod(data, axis=None, keepdims=False, exclude=False)

Computes the products of array elements over given axes.

Parameters
  • data (relay.Expr) – The input data

  • axis (None or int or tuple of int) – Axis or axes along which a product is performed. The default, axis=None, will find the indices of minimum element all of the elements of the input array. If axis is negative it counts from the last to the first axis.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • exclude (bool) – If exclude is true, reduction will be performed on the axes that are NOT in axis instead.

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.reinterpret(data, dtype)

Reinterpret input tensor to data type.

Parameters
  • data (relay.Expr) – The input data to the operator.

  • dtype (str) – The target data type

Returns

result – The reinterpreted result.

Return type

relay.Expr

tvm.relay.repeat(data, repeats, axis)

Repeats elements of an array. By default, repeat flattens the input array into 1-D and then repeats the elements.

repeatsint

The number of repetitions for each element.

axis: int

The axis along which to repeat values. The negative numbers are interpreted counting from the backward. By default, use the flattened input array, and return a flat output array.

Returns

ret – The computed result.

Return type

relay.Expr

Examples

x = [[1, 2], [3, 4]]
relay.repeat(x, repeats=2) = [1., 1., 2., 2., 3., 3., 4., 4.]

relay.repeat(x, repeats=2, axis=1) = [[1., 1., 2., 2.],
                                      [3., 3., 4., 4.]]
tvm.relay.reshape(data, newshape)

Reshape the input array.

To give user more convenience in without doing manual shape inference, some dimensions of the shape can take special values from the set {0, -1, -2, -3, -4}. The significance of each is explained below:

0 copy this dimension from the input to the output shape.

data.shape = (2,3,4), newshape = (4,0,2), result.shape = (4,3,2)
data.shape = (2,3,4), newshape = (2,0,0), result.shape = (2,3,4)

-1 infers the dimension of the output shape by using the remainder of the input dimensions keeping the size of the new array same as that of the input array. At most one dimension of shape can be -1.

data.shape = (2,3,4), newshape = (6,1,-1), result.shape = (6,1,4)
data.shape = (2,3,4), newshape = (3,-1,8), result.shape = (3,1,8)
data.shape = (2,3,4), newshape = (-1,), result.shape = (24,)

-2 copy all/remainder of the input dimensions to the output shape.

data.shape = (2,3,4), newshape = (-2,), result.shape = (2,3,4)
data.shape = (2,3,4), newshape = (2,-2), result.shape = (2,3,4)
data.shape = (2,3,4), newshape = (-2,1,1), result.shape = (2,3,4,1,1)

-3 use the product of two consecutive dimensions of the input shape as the output dimension.

data.shape = (2,3,4), newshape = (-3,4), result.shape = (6,4)
data.shape = (2,3,4,5), newshape = (-3,-3), result.shape = (6,20)
data.shape = (2,3,4), newshape = (0,-3), result.shape = (2,12)
data.shape = (2,3,4), newshape = (-3,-2), result.shape = (6,4)

-4 split one dimension of the input into two dimensions passed subsequent to -4 in shape (can contain -1).

data.shape = (2,3,4), newshape = (-4,1,2,-2), result.shape = (1,2,3,4)
data.shape = (2,3,4), newshape = (2,-4,-1,3,-2), result.shape = (2,1,3,4)
Parameters
  • data (relay.Expr) – The input data to the operator.

  • newshape (Union[int, Tuple[int], List[int]] or relay.Expr) – The new shape. Should be compatible with the original shape.

Returns

result – The reshaped result.

Return type

relay.Expr

tvm.relay.reshape_like(data, shape_like)

Reshapes the input array by the size of another array. For an input array with shape (d1, d2, ..., dk), reshape_like operation reshapes the input array into an output array with the same shape as the second input array.

Note

Sizes for both array should be compatible.

Parameters
  • data (relay.Expr) – The input data to the operator.

  • shape_like (tuple of int) – The new shape. Should be compatible with the original shape.

Returns

ret – The computed result.

Return type

relay.Expr

tvm.relay.reverse(data, axis)

Reverses the order of elements along given axis while preserving array shape. By default, repeat flattens the input array into 1-D and then repeats the elements.

Parameters
  • data (relay.Expr) – The input data to the operator.

  • axis (int) – The axis along which to reverse elements.

Returns

ret – The computed result.

Return type

relay.Expr

Examples

x = [[1., 2.], [3., 4.]]
relay.reverse(x, axis=0) = [[3., 4.], [1., 2.]]

relay.reverse(x, axis=1) = [[2., 1.], [4., 3.]]
tvm.relay.reverse_reshape(data, newshape)

Reshapes the input array where the special values are inferred from right to left.

The special values have the same semantics as tvm.relay.reshape. The difference is that special values are inferred from right to left. It can be explained in the example below.

data.shape = (10,5,4), newshape = (-1,0), reshape results in (40,5)
data.shape = (10,5,4), newshape = (-1,0), reverse_reshape results in (40,5)
Parameters
  • data (relay.Expr) – The input data to the operator.

  • newshape (Union[int, Tuple[int], List[int]]) – The new shape. Should be compatible with the original shape.

Returns

result – The reshaped result.

Return type

relay.Expr

tvm.relay.reverse_sequence(data, seq_lengths, seq_axis=1, batch_axis=0)

Reverse the tensor for variable length slices. Input is first sliced along batch axis and then elements are reversed along seq axis.

Parameters
  • data (relay.Expr) – The tensor to be reversed.

  • seq_lengths (relay.Expr) – A 1D Tensor with length a.dims[batch_axis] Must be one of the following types: int32, int64 if seq_lengths[i] > a.dims[seq_axis], it is rounded to a.dims[seq_axis] if seq_lengths[i] < 1, it is rounded to 1

  • seq_axis (int, optional) – The axis along which the elements will be reversed. Default is 1.

  • batch_axis (int, optional) – The axis along which the tensor will be sliced. Default is 0.

Returns

ret – The computed result of same shape and type as of input.

Return type

relay.Expr

Examples

x = [[0, 1, 2, 3],
     [4, 5, 6, 7],
     [8, 9, 10, 11],
     [12, 13, 14, 15]]
relay.reverse(x, [1, 2, 3, 4], 0, 1) = [[0, 5, 10, 15],
                                        [4, 1, 6, 11],
                                        [8, 9, 2, 7],
                                        [12, 13, 14, 3]]

relay.reverse(x, [1, 2, 3, 4], 1, 0) = [[0, 1, 2, 3],
                                        [5, 4, 6, 7],
                                        [10, 9, 8, 11],
                                        [15, 14, 13, 12]]
tvm.relay.right_shift(lhs, rhs)

Right shift with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.round(data)

Compute element-wise round of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.rsqrt(data)

Compute elementwise rsqrt of data.

\[1/sqrt(x)\]
Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.scatter(data, indices, updates, axis)

Update data at positions defined by indices with values in updates

Parameters
  • data (relay.Expr) – The input data to the operator.

  • indices (relay.Expr) – The index locations to update.

  • updates (relay.Expr) – The values to update.

  • axis (int) – The axis to scatter on

Returns

ret – The computed result.

Return type

relay.Expr

tvm.relay.sequence_mask(data, valid_length, mask_value=0, axis=0)

Sets all elements outside the expected length of the sequence to a constant value.

This function takes an n-dimensional input array of the form [MAX_LENGTH, batch_size, …] or [batch_size, MAX_LENGTH, …] and returns an array of the same shape.

Parameters
  • data (relay.Expr) – The input data.

  • valid_length (relay.Expr) – The expected (valid) length of each sequence in the tensor.

  • mask_value (float) – The masking value.

  • axis (int) – The axis of the length dimension.

Returns

ret – The computed result.

Return type

relay.Expr

Examples

 x = [[[  1.,   2.,   3.], [  4.,   5.,   6.]],
      [[  7.,   8.,   9.], [ 10.,  11.,  12.]],
      [[ 13.,  14.,   15.], [ 16.,  17.,   18.]]]

relay.sequence_mask(x, valid_length=[1, 1]) =
     [[[  1.,   2.,   3.], [  4.,   5.,   6.]],
      [[  0.,   0.,   0.], [  0.,   0.,   0.]],
      [[  0.,   0.,   0.], [  0.,   0.,   0.]]]

relay.sequence_mask(x, valid_length=[2, 3], mask_value=0.1) =
     [[[  1.,   2.,   3.], [  4.,   5.,   6.]],
      [[  7.,   8.,   9.], [  10.,  11.,  12.]],
      [[  0.1,  0.1,  0.1], [  16.,  17.,  18.]]]
tvm.relay.shape_of(data, dtype='int32')

Get shape of a tensor.

Parameters
  • data (tvm.relay.Expr) – The input tensor.

  • dtype (str, optional) – The target data type.

Returns

result – The shape tensor.

Return type

tvm.relay.Expr

tvm.relay.sigmoid(data)

Compute elementwise sigmoid of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.sign(data)

Compute element-wise absolute of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.sin(data)

Compute elementwise sin of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.sinh(data)

Compute elementwise sinh of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.slice_like(data, shape_like, axes=None)

Slice the first input with respect to the second input.

For an input array with shape (d1, d2, ..., dk), slice_like operation slices the the input array corresponding size of second array. By default will slice on all axes.

Parameters
  • data (tvm.relay.Expr) – The source array.

  • shape_like (tvm.relay.Expr) – The new shape.

  • axes (Optional[Tuple[int]]) – List of axes on which input data will be sliced according to the corresponding size of the second input. By default will slice on all axes. Negative axes mean counting in reverse.

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.sparse_to_dense(sparse_indices, output_shape, sparse_values, default_value=0)

Converts a sparse representation into a dense tensor.

Example:: - sparse_to_dense([[0, 0], [1, 1]], [2, 2], [3, 3], 0) = [[3, 0], [0, 3]]

Parameters
  • sparse_indices (relay.Expr) – A 0-D, 1-D, or 2-D tensor of integers containing location of sparse values.

  • output_shape (relay.Expr) – A list of integers. Shape of the dense output tensor.

  • sparse_values (relay.Expr) – A 0-D or 1-D tensor containing the sparse values for the sparse indices.

  • default_value (relay.Expr) – A 0-D tensor containing the default value for the remaining locations. Defaults to 0.

Returns

result – Dense tensor of shape output_shape. Has the same type as sparse_values.

Return type

relay.Expr

tvm.relay.split(data, indices_or_sections, axis=0)

Split input tensor along axis by sections or indices.

If indices_or_sections is an integer, the input will be divided equally along given axis. If such a split is not possible, an error is raised.

If indices_or_sections is a tuple of sorted integers, the entries indicate where along axis the array is split.

Parameters
  • data (relay.Expr) – The source array.

  • indices_or_sections (int or tuple of int) – Indices or sections to split into. Accepts an int or a tuple

  • axis (int, optional) – The axis over which to split.

Returns

ret – The computed result.

Return type

relay.Tuple([relay.Expr, relay.Expr])

tvm.relay.sqrt(data)

Compute elementwise sqrt of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.squeeze(data, axis=None)

Squeeze axes in the array.

Parameters
  • data (tvm.relay.Expr) – The input data to the operator.

  • axis (None or List[int]) – The set of axes to remove. If axis = None, remove all axis of dimensions 1. If any specified axis has dimension that does not equal 1, it is an error.

Returns

result – The squeezed result.

Return type

tvm.relay.Expr

tvm.relay.stack(data, axis)

Join a sequence of arrays along a new axis.

Parameters
  • data (Union(List[relay.Expr], Tuple(relay.Expr))) – A list of tensors.

  • axis (int) – The axis in the result array along which the input arrays are stacked.

Returns

ret – The stacked tensor.

Return type

relay.Expr

tvm.relay.std(data, axis=None, keepdims=False, exclude=False)

Computes the standard deviation of data over given axes.

Parameters
  • data (relay.Expr) – The input data

  • axis (None or int or tuple of int) – Axis or axes along which a standard deviation operation is performed. The default, axis=None, will compute the standard deviation of all elements in the input array. If axis is negative it counts from the last to the first axis.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • exclude (bool) – If exclude is true, reduction will be performed on the axes that are NOT in axis instead.

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.strided_set(data, v, begin, end, strides=None)

Strided set of an array.

Parameters
  • data (relay.Expr) – The source array to be sliced.

  • v (relay.Expr) – The data to be set.

  • begin (relay.Expr, Tuple[int], or List[int]) – The indices to begin with in the slicing.

  • end (relay.Expr, Tuple[int], or List[int]) – Indices indicating end of the slice.

  • strides (relay.Expr, Tuple[int], or List[int], optional) – Specifies the stride values, it can be negative in that case, the input tensor will be reversed in that particular axis.

Returns

ret – The computed result.

Return type

relay.Expr

tvm.relay.strided_slice(data, begin, end, strides=None, slice_mode='end')

Strided slice of an array.

Parameters
  • data (relay.Expr) – The source array to be sliced.

  • begin (relay.Expr, Tuple[int], or List[int]) – The indices to begin with in the slicing.

  • end (relay.Expr, Tuple[int], or List[int]) – Indices indicating end of the slice.

  • strides (relay.Expr, Tuple[int], or List[int], optional) – Specifies the stride values, it can be negative in that case, the input tensor will be reversed in that particular axis.

  • slice_mode (str, optional) – The slice mode [end, size]. end: The ending indices for the slice [default]. size: The input strides will be ignored, input end in this mode indicates the size of a slice starting at the location specified by begin. If end[i] is -1, all remaining elements in that dimension are included in the slice.

Returns

ret – The computed result.

Return type

relay.Expr

tvm.relay.subtract(lhs, rhs)

Subtraction with numpy-style broadcasting.

Parameters
  • lhs (relay.Expr) – The left hand side input data

  • rhs (relay.Expr) – The right hand side input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.sum(data, axis=None, keepdims=False, exclude=False)

Computes the sum of array elements over given axes.

Parameters
  • data (relay.Expr) – The input data

  • axis (None or int or tuple of int) – Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • exclude (bool) – If exclude is true, reduction will be performed on the axes that are NOT in axis instead.

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.take(data, indices, axis=None, mode='clip')

Take elements from an array along an axis.

Parameters
  • data (relay.Expr) – The source array.

  • indices (rely.Expr) – The indices of the values to extract.

  • axis (int, optional) – The axis over which to select values. By default, the flattened input array is used.

  • mode (str, optional) – Specifies how out-of-bound indices will behave [clip, wrap, fast]. clip: clip to the range (default). wrap: wrap around the indices. fast: no clip or wrap around (user must make sure indices are in-bound).

Returns

ret – The computed result.

Return type

relay.Expr

tvm.relay.tan(data)

Compute elementwise tan of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.tanh(data)

Compute element-wise tanh of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.tile(data, reps)

Repeats the whole array multiple times.

Parameters
  • data (relay.Expr) – The input data to the operator.

  • reps (tuple of int) – The number of times repeating the tensor data.

Returns

ret – The computed result.

Return type

relay.Expr

Examples

x = [[1, 2], [3, 4]]
relay.tile(x, reps=(2,3)) = [[1., 2., 1., 2., 1., 2.],
                             [3., 4., 3., 4., 3., 4.],
                             [1., 2., 1., 2., 1., 2.],
                             [3., 4., 3., 4., 3., 4.]]

relay.tile(x, reps=(2,)) = [[1., 2., 1., 2.],
                            [3., 4., 3., 4.]]

Notes

Each dim size of reps must be a positive integer. If reps has length d, the result will have dimension of max(d, data.ndim); If data.ndim < d, data is promoted to be d-dimensional by prepending new axes. If data.ndim >= d, reps is promoted to a.ndim by pre-pending 1’s to it.

tvm.relay.topk(data, k=1, axis=- 1, ret_type='both', is_ascend=False, dtype='int32')

Get the top k elements in an input tensor along the given axis.

ret_type specifies the return type, can be one of (“both”, “values”, “indices”).

Parameters
  • data (relay.Expr) – The input data tensor.

  • k (int or relay.Expr, optional) – Number of top elements to select. Return all elements if k < 1.

  • axis (int, optional) – Axis long which to sort the input tensor.

  • ret_type (str, optional) – The return type [both, values, indices]. “both”: return both top k data and indices. “values”: return top k data only. “indices”: return top k indices only.

  • is_ascend (boolean, optional) – Whether to sort in ascending or descending order.

  • dtype (string, optional) – The data type of the indices output.

Returns

out – The computed result.

Return type

relay.Expr or List[relay.Expr]

tvm.relay.transpose(data, axes=None)

Permutes the dimensions of an array.

Parameters
  • data (relay.Expr) – The input data to the operator.

  • axes (None or List[int]) – The target axes order, reverse order if not specified.

Returns

result – The transposed result.

Return type

relay.Expr

tvm.relay.trunc(data)

Compute element-wise trunc of data.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.unravel_index(indices, shape)

Convert a flat index or array of flat indices into a tuple of coordinate arrays.

Example:: - unravel_index([22, 41, 37], [7, 6]) = [[3, 6, 6],[4, 5, 1]]

Parameters
  • indices (relay.Expr) – An integer array containing indices.

  • shape (relay.Expr) – The shape of the array.

Returns

result – The tuple of coordinate arrays.

Return type

relay.Expr

tvm.relay.variance(data, axis=None, keepdims=False, exclude=False)

Computes the variance of data over given axes.

Parameters
  • data (relay.Expr) – The input data

  • axis (None or int or tuple of int) – Axis or axes along which a variance operation is performed. The default, axis=None, will compute the variance of all elements in the input array. If axis is negative it counts from the last to the first axis.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • exclude (bool) – If exclude is true, reduction will be performed on the axes that are NOT in axis instead.

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.where(condition, x, y)

Selecting elements from either x or y depending on the value of the condition.

Note

The shape of condition, x, and y needs to be the same.

Parameters
  • condition (relay.Expr) – The condition array. The n-th element in y is selected when the n-th value in the condition array is zero. Otherwise, the corresponding element from x will be picked.

  • x (relay.Expr) – The first array to be selected.

  • y (relay.Expr) – The second array to be selected.

Returns

result – The selected array.

Return type

relay.Expr

Examples

x = [[1, 2], [3, 4]]
y = [[5, 6], [7, 8]]
condition = [[0, 1], [-1, 0]]
relay.where(conditon, x, y) = [[5, 2], [3, 8]]

condition = [1, 0]
relay.where(conditon, x, y) = [[1, 2], [7, 8]]
tvm.relay.zeros(shape, dtype)

Fill array with zeros.

Parameters
  • shape (tuple of int or relay.Expr) – The shape of the target.

  • dtype (data type) – The data type of the target.

Returns

result – The resulting tensor.

Return type

relay.Expr

tvm.relay.zeros_like(data)

Returns an array of zeros, with same type and shape as the input.

Parameters

data (relay.Expr) – The input data

Returns

result – The computed result.

Return type

relay.Expr

tvm.relay.save_param_dict(params)

Save parameter dictionary to binary bytes.

The result binary bytes can be loaded by the GraphModule with API “load_params”.

Parameters

params (dict of str to NDArray) – The parameter dictionary.

Returns

param_bytes – Serialized parameters.

Return type

bytearray

Examples

# compile and save the modules to file.
graph, lib, params = tvm.relay.build(func, target=target, params=params)
module = graph_runtime.create(graph, lib, tvm.gpu(0))
# save the parameters as byte array
param_bytes = tvm.relay.save_param_dict(params)
# We can serialize the param_bytes and load it back later.
# Pass in byte array to module to directly set parameters
module.load_params(param_bytes)
tvm.relay.load_param_dict(param_bytes)

Load parameter dictionary to binary bytes.

Parameters

param_bytes (bytearray) – Serialized parameters.

Returns

params – The parameter dictionary.

Return type

dict of str to NDArray