tvm.relay.op

Relay core operators.

tvm.relay.op.Op()

A Relay operator definition.

tvm.relay.op.OpPattern()

Operator generic patterns

See also

top.tag()

Contains explanation of the tag type.

tvm.relay.op.get(op_name)

Get the Op for a given name

Parameters

op_name (str) – The operator name

Returns

op – The op of the corresponding name

Return type

Op

tvm.relay.op.register(op_name, attr_key, value=None, level=10)

Register an operator property of an operator.

Parameters
  • op_name (str) – The name of operator

  • attr_key (str) – The attribute name.

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

  • level (int, optional) – The priority level

Returns

fregister – Register function if value is not specified.

Return type

function

tvm.relay.op.register_schedule(op_name, schedule=None, level=10)

Register schedule function for an op

Parameters
  • op_name (str) – The name of the op.

  • schedule (function (attrs: Attrs, outs: List[Tensor], target: Target) -> sch: Schedule) – The schedule function.

  • level (int) – The priority level

tvm.relay.op.register_pattern(op_name, pattern, level=10)

Register operator pattern for an op.

Parameters
  • op_name (str) – The name of the op.

  • pattern (int) – The pattern being used.

  • level (int) – The priority level

tvm.relay.op.register_compute(op_name, compute=None, level=10)

Register compute function for an op.

Parameters
  • op_name (str) – The name of the op.

  • compute (function (attrs: Attrs, inputs: List[Tensor], out_type: Type, target:Target)) – -> List[Tensor] The compute function.

  • level (int) – The priority level

tvm.relay.op.register_gradient(op_name, fgradient=None, level=10)

Register operator pattern for an op.

Parameters
  • op_name (str) – The name of the op.

  • fgradient (function (orig_expr : Expr, output_grad : Expr) -> new_expr : Expr) – The gradient being used.

  • level (int) – The priority level

tvm.relay.op.register_alter_op_layout(op_name, alter_layout=None, level=10)

Register alter op layout function for an op

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

  • alter_layout (function (attrs: Attrs, inputs: List[Expr]) -> new_expr: Expr) – The function for changing the layout or replacing the operator

  • level (int) – The priority level

tvm.relay.op.schedule_injective(attrs, outputs, target)

Generic schedule for binary broadcast.

tvm.relay.op.debug(expr, debug_func=None)

The main entry point to the debugger.

Reduce operators.

tvm.relay.op.reduce.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.op.reduce.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.op.reduce.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.op.reduce.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.op.reduce.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.op.reduce.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.op.reduce.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.op.reduce.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.op.reduce.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.op.reduce.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.op.reduce.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.op.reduce.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

Basic tensor operations.

tvm.relay.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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

tvm.relay.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.ones(shape, dtype)

Fill array with ones.

Parameters
  • shape (tuple of int) – 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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.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.op.tensor.zeros(shape, dtype)

Fill array with zeros.

Parameters
  • shape (tuple of int) – 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.op.tensor.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

Transform operators.

tvm.relay.op.transform.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.op.transform.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 (shape) – Provide the shape to broadcast to.

Returns

result – The resulting tensor.

Return type

relay.Expr

tvm.relay.op.transform.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.op.transform.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.op.transform.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.op.transform.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.op.transform.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.op.transform.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) – 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.op.transform.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.op.transform.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.op.transform.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.op.transform.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.op.transform.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.op.transform.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.op.transform.reshape(data, newshape)

Reshapes the input array.

Example:

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.

Example:

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

Example:

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

Example:

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

Example:

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

Example:

- 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]]) – The new shape. Should be compatible with the original shape.

Returns

result – The reshaped result.

Return type

relay.Expr

tvm.relay.op.transform.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.op.transform.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.op.transform.reverse_reshape(data, newshape)

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

Example:

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.op.transform.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.op.transform.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.op.transform.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.op.transform.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.op.transform.strided_slice(data, begin, end, strides=None)

Strided slice of an array.

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

  • begin (list of int) – The indices to begin with in the slicing.

  • end (list of int) – Indices indicating end of the slice.

  • strides (list of 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.op.transform.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.op.transform.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.

  • note:: (.) – 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.

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.]]
tvm.relay.op.transform.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.op.transform.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]]

Neural network related operators.

Multibox operations.

tvm.relay.op.vision.multibox.multibox_prior(data, sizes=(1.0, ), ratios=(1.0, ), steps=(-1.0, -1.0), offsets=(0.5, 0.5), clip=False)

Generate prior(anchor) boxes from data, sizes and ratios.

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

  • sizes (tuple of float, optional) – Tuple of sizes for anchor boxes.

  • ratios (tuple of float, optional) – Tuple of ratios for anchor boxes.

  • steps (Tuple of float, optional) – Priorbox step across y and x, -1 for auto calculation.

  • offsets (tuple of int, optional) – Priorbox center offsets, y and x respectively.

  • clip (boolean, optional) – Whether to clip out-of-boundary boxes.

Returns

out – 3-D tensor with shape [1, h_in * w_in * (num_sizes + num_ratios - 1), 4]

Return type

relay.Expr

tvm.relay.op.vision.multibox.multibox_transform_loc(cls_prob, loc_pred, anchor, clip=True, threshold=0.01, variances=(0.1, 0.1, 0.2, 0.2))

Location transformation for multibox detection

Parameters
  • cls_prob (tvm.relay.Expr) – Class probabilities.

  • loc_pred (tvm.relay.Expr) – Location regression predictions.

  • anchor (tvm.relay.Expr) – Prior anchor boxes.

  • clip (boolean, optional) – Whether to clip out-of-boundary boxes.

  • threshold (double, optional) – Threshold to be a positive prediction.

  • variances (Tuple of float, optional) – variances to be decoded from box regression output.

Returns

ret

Return type

tuple of tvm.relay.Expr

tvm.relay.vision.nms()

Non-maximum suppression operations.