tvm.relay.dataflow_pattern

The Relay Pattern Language and tooling.

Classes

AltPattern(left, right)

Create a Pattern that can match one of two conditions

AttrPattern(pattern, attrs)

Get match an expression with a certain attributes.

CallPattern(op, args, attrs, type_args)

A pattern matching a function call node in Relay.

Callable

Callable type; Callable[[int], str] is a function of (int) -> str.

ConstantPattern()

A pattern matching a Relay Constant.

DFPattern

Base class of all Patterns.

DFPatternCallback([require_type])

A Callback for Pattern Rewriting.

DataTypePattern(pattern, dtype)

A pattern that matches another pattern with certain data type

Dict

DominatorPattern(parent, path, child)

Match a domination graph.

Expr

Base class of all non-primitive expressions.

ExprPattern(expr)

A pattern which matches a constant expression.

List

ShapePattern(pattern, shape)

A pattern that matches another pattern with a certain tensor shape

TupleGetItemPattern(tuple_value, index)

Get index-th item from a TuplePattern.

TuplePattern(fields)

A patern matching a Relay Tuple.

TypePattern(pattern, ttype)

A pattern that matches another pattern with a certain type annotation.

VarPattern(name_hint, type_annotation)

A local variable in Relay.

WildcardPattern()

A pattern which matches anything.

Functions

dominates(parent, path, child)

Syntatic sugar for creating an Dominator pattern

get(op_name)

Get the Op for a given name

has_attr(attrs[, pattern])

Syntatic sugar for creating an AttrPattern

has_dtype(dtype, pattern)

Syntatic sugar for creating a DataTypePattern

has_shape(shape, pattern)

Syntatic sugar for creating a ShapePattern

has_type(ttype, pattern)

Syntatic sugar for creating a TypePattern

is_constant()

Syntatic sugar for creating a ConstantPattern.

is_expr(expr)

Syntatic sugar for creating an ExprPattern.

is_op(op_name)

Syntatic sugar for creating an operator ExprPattern.

is_tuple(fields)

Syntatic sugar for creating an ExprPattern.

is_tuple_get_item(tuple_value, index)

Syntatic sugar for creating an ExprPattern.

is_var(name)

Syntatic sugar for creating an optionally named VarPattern.

make_node(type_key, **kwargs)

Make a new IR node by its type key and fields

match(pattern, expr)

Match a pattern to an expression

partition(pattern, expr, attrs, …)

Parition the expression into a series of functions that match the pattern

register_df_node([type_key])

Register a Relay node type.

rewrite(callbacks, expr, mod)

Rewrite expression with the given callbacks.

wildcard()

Syntatic sugar for creating a WildcardPattern.

class tvm.relay.dataflow_pattern.Callable

Callable type; Callable[[int], str] is a function of (int) -> str.

The subscription syntax must always be used with exactly two values: the argument list and the return type. The argument list must be a list of types or ellipsis; the return type must be a single type.

There is no syntax to indicate optional or keyword arguments, such function types are rarely used as callback types.

class tvm.relay.dataflow_pattern.Dict
class tvm.relay.dataflow_pattern.List
tvm.relay.dataflow_pattern.Expr

checked_type

Get the checked type of tvm.relay.Expr.

Attributes

alias of tvm.ir.expr.RelayExpr

tvm.relay.dataflow_pattern.make_node(type_key, **kwargs)

Make a new IR node by its type key and fields

Parameters
  • type_key (str) – The type key of the node.

  • **kwargs (dict) – The fields of the node.

Returns

node – The corresponding IR Node

Return type

Node

Note

If the created node is instance of AttrsNode, then the creator function will also run bound checks and default value setup as supported by Attrs.

Example

The following code constructs a IntImm object

x = tvm.ir.make_node("IntImm", dtype="int32", value=10)
assert isinstance(x, tvm.tir.IntImm)
assert x.value == 10
tvm.relay.dataflow_pattern.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.dataflow_pattern.register_df_node(type_key=None)

Register a Relay node type.

Parameters

type_key (str or cls) – The type key of the node.

class tvm.relay.dataflow_pattern.DFPattern

Base class of all Patterns.

Methods

dominates(parent, path)

Create a dominator for this pattern.

has_attr(attrs, tvm.runtime.object.Object])

Add an attribute constraint to this pattern

has_dtype(dtype)

Add a type constraint to this pattern

has_shape(shape)

Add a type constraint to this pattern

has_type(ttype)

Add a type constraint to this pattern

match(expr)

Match this pattern to an expression

optional(option_constructor, DFPattern])

Create a optional user of this pattern.

partition(expr, attrs, …)

Parition the expression into functions defined by this pattern

has_attr(attrs: Dict[str, tvm.runtime.object.Object])

Add an attribute constraint to this pattern

Parameters

attrs (Dict[str, Object]) –

Returns

result – The resulting AttrPattern

Return type

tvm.relay.dataflow_pattern.DFPattern

has_type(ttype: tvm.ir.type.Type)

Add a type constraint to this pattern

Parameters

ttype (tvm.ir.type.Type) – The type to match

Returns

result – The resulting TypePattern

Return type

tvm.relay.dataflow_pattern.DFPattern

has_dtype(dtype: str)

Add a type constraint to this pattern

Parameters

dtype (str) – The dtype to match

Returns

result – The resulting DataTypePattern

Return type

tvm.relay.dataflow_pattern.DFPattern

has_shape(shape: List[tvm.ir.expr.PrimExpr])

Add a type constraint to this pattern

Parameters

shape (List[tvm.ir.PrimExpr]) – The shape to match

Returns

result – The resulting ShapePattern

Return type

tvm.relay.dataflow_pattern.DFPattern

match(expr: tvm.ir.expr.RelayExpr)bool

Match this pattern to an expression

Parameters

expr (tvm.relay.Expr) – The expression to match.

Returns

result – Whether or not the expression matches the pattern

Return type

bool

partition(expr: tvm.ir.expr.RelayExpr, attrs: Optional[Dict[str, tvm.runtime.object.Object]] = None, check: Callable[tvm.ir.expr.RelayExpr, bool] = <function DFPattern.<lambda>>) → tvm.ir.expr.RelayExpr

Parition the expression into functions defined by this pattern

Parameters
  • expr (tvm.relay.Expr) – The expression to match.

  • attrs (Optional[Dict[str, Object]]) – A dictionary of Attribute name/values to add to the paritioned function

  • check (Callable[[Expr], bool]) – A function to perform more complicated checks on the matched expression. Returns true if partitioning should proceed, false otherwise.

Returns

result – The Expression with matched subgraphs replaced by function calls to that subgraph

Return type

tvm.relay.Expr

dominates(parent: tvm.relay.dataflow_pattern.DFPattern, path: Optional[tvm.relay.dataflow_pattern.DFPattern] = None)

Create a dominator for this pattern.

Parameters
Returns

result – The resulting DominatorPattern.

Return type

tvm.relay.dataflow_pattern.DFPattern

optional(option_constructor: Callable[DFPattern, DFPattern])

Create a optional user of this pattern.

Parameters

option_constructor (function) – A function that takes a single Pattern parameter and returns a constructed pattern matching the option

Returns

result – The resulting Pattern

Return type

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.is_var(name: str = '')tvm.relay.dataflow_pattern.DFPattern

Syntatic sugar for creating an optionally named VarPattern.

Parameters

name (str) – The name of the input pattern to match.

Returns

result – The resulting pattern.

Return type

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.is_constant()tvm.relay.dataflow_pattern.DFPattern

Syntatic sugar for creating a ConstantPattern.

Parameters

name (str) – The name of the input pattern to match.

Returns

result – The resulting pattern.

Return type

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.is_expr(expr: tvm.ir.expr.RelayExpr)tvm.relay.dataflow_pattern.DFPattern

Syntatic sugar for creating an ExprPattern.

Parameters

expr (Expr) – The Relay expression to match.

Returns

result – The resulting pattern.

Return type

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.is_op(op_name: str)tvm.relay.dataflow_pattern.DFPattern

Syntatic sugar for creating an operator ExprPattern.

Parameters

op_name (String) – The name of the relay op

Returns

result – The resulting ExprPattern

Return type

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.is_tuple(fields: tvm.ir.container.Array)tvm.relay.dataflow_pattern.DFPattern

Syntatic sugar for creating an ExprPattern.

Parameters

fields (Array[tvm.relay.dataflow_pattern.DFPattern]) – The fields in the tuple.

Returns

result – The resulting pattern.

Return type

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.is_tuple_get_item(tuple_value: tvm.relay.dataflow_pattern.DFPattern, index: Optional[int] = None)tvm.relay.dataflow_pattern.DFPattern

Syntatic sugar for creating an ExprPattern.

Parameters
Returns

result – The resulting pattern.

Return type

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.wildcard()tvm.relay.dataflow_pattern.DFPattern

Syntatic sugar for creating a WildcardPattern.

Returns

result – The resulting pattern.

Return type

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.has_type(ttype: tvm.ir.type.Type, pattern: Optional[tvm.relay.dataflow_pattern.DFPattern] = None)tvm.relay.dataflow_pattern.DFPattern

Syntatic sugar for creating a TypePattern

Parameters
Returns

result – The resulting TypePattern

Return type

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.has_dtype(dtype: str, pattern: Optional[tvm.relay.dataflow_pattern.DFPattern] = None)tvm.relay.dataflow_pattern.DFPattern

Syntatic sugar for creating a DataTypePattern

Parameters
Returns

result – The resulting DataTypePattern

Return type

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.has_shape(shape: List[tvm.ir.expr.PrimExpr], pattern: Optional[tvm.relay.dataflow_pattern.DFPattern] = None)tvm.relay.dataflow_pattern.DFPattern

Syntatic sugar for creating a ShapePattern

Parameters
Returns

result – The resulting ShapePattern

Return type

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.has_attr(attrs, pattern=None)tvm.relay.dataflow_pattern.DFPattern

Syntatic sugar for creating an AttrPattern

Parameters
Returns

result – The resulting AttrPattern

Return type

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.dominates(parent: tvm.relay.dataflow_pattern.DFPattern, path: tvm.relay.dataflow_pattern.DFPattern, child: tvm.relay.dataflow_pattern.DFPattern)tvm.relay.dataflow_pattern.DFPattern

Syntatic sugar for creating an Dominator pattern

Parameters
Returns

result – The resulting DominatorPattern.

Return type

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.match(pattern: tvm.relay.dataflow_pattern.DFPattern, expr: tvm.ir.expr.RelayExpr)bool

Match a pattern to an expression

Parameters
class tvm.relay.dataflow_pattern.ExprPattern(expr: tvm.ir.expr.RelayExpr)

A pattern which matches a constant expression.

Parameters

expr (tvm.relay.Expr) – The expression to match.

class tvm.relay.dataflow_pattern.VarPattern(name_hint: str = '', type_annotation: Optional[tvm.ir.type.Type] = None)

A local variable in Relay.

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

Parameters
  • name_hint (str) – The name of the variable. Optional, if not provided, the pattern will match any VarNode.

  • type_annotation (tvm.ir.type.Type, optional) – The type annotation on the variable.

class tvm.relay.dataflow_pattern.ConstantPattern

A pattern matching a Relay Constant.

class tvm.relay.dataflow_pattern.CallPattern(op: tvm.relay.dataflow_pattern.DFPattern, args: List[DFPattern], attrs: Optional[tvm.ir.attrs.Attrs] = None, type_args: Optional[List[tvm.ir.type.Type]] = None)

A pattern matching a function call node in Relay.

Parameters
  • op (realy.dataflow_pattern.DFPattern) – The operation to be called.

  • args (List[realy.dataflow_pattern.DFPattern]) – The arguments to the call.

  • attrs (Optional[tvm.ir.attrs.Attrs]) – Attributes to the call, can be None

  • type_args (Optional[List[tvm.ir.type.Type]]) – The additional type arguments, this is only used in advanced usecase of template functions.

class tvm.relay.dataflow_pattern.TuplePattern(fields: tvm.ir.container.Array)

A patern matching a Relay Tuple.

Parameters

fields (Array[tvm.relay.dataflow_pattern.DFPattern]) – The fields in the tuple.

class tvm.relay.dataflow_pattern.TupleGetItemPattern(tuple_value: tvm.relay.dataflow_pattern.DFPattern, index: Optional[int] = None)

Get index-th item from a TuplePattern.

Parameters
class tvm.relay.dataflow_pattern.AltPattern(left: tvm.relay.dataflow_pattern.DFPattern, right: tvm.relay.dataflow_pattern.DFPattern)

Create a Pattern that can match one of two conditions

Parameters
class tvm.relay.dataflow_pattern.WildcardPattern

A pattern which matches anything.

class tvm.relay.dataflow_pattern.TypePattern(pattern: tvm.relay.dataflow_pattern.DFPattern, ttype: tvm.ir.type.Type)

A pattern that matches another pattern with a certain type annotation.

Parameters
class tvm.relay.dataflow_pattern.DataTypePattern(pattern: tvm.relay.dataflow_pattern.DFPattern, dtype: str)

A pattern that matches another pattern with certain data type

Parameters
class tvm.relay.dataflow_pattern.ShapePattern(pattern: tvm.relay.dataflow_pattern.DFPattern, shape: List[tvm.ir.expr.PrimExpr])

A pattern that matches another pattern with a certain tensor shape

Parameters
class tvm.relay.dataflow_pattern.AttrPattern(pattern: tvm.relay.dataflow_pattern.DFPattern, attrs: tvm.ir.attrs.Attrs)

Get match an expression with a certain attributes. Currently only supports Op Attributes, not call Attributes.

Parameters
class tvm.relay.dataflow_pattern.DominatorPattern(parent: tvm.relay.dataflow_pattern.DFPattern, path: tvm.relay.dataflow_pattern.DFPattern, child: tvm.relay.dataflow_pattern.DFPattern)

Match a domination graph.

Parameters
class tvm.relay.dataflow_pattern.DFPatternCallback(require_type=False)

A Callback for Pattern Rewriting.

When rewrite is called on this DFPatternCallback, the backend will find matches for the pattern, call the callback function, and replace the matched expression with whatever the callback returns.

Users are expect to inherit from this class and provide a “self.pattern” to match

Parameters

require_type (bool) – Whether InferType is required to be run before the callback.

Methods

callback(pre, post, node_map)

Callback function to use when we found a match to the pattern

rewrite(expr)

Rewrite expression with this callback

rewrite(expr: tvm.ir.expr.RelayExpr) → tvm.ir.expr.RelayExpr

Rewrite expression with this callback

Parameters

expr (tvm.relay.Expr) – The expression to rewrite.

Returns

result – The Expression with matched subgraphs rewritten by the callbacks.

Return type

tvm.relay.Expr

callback(pre: tvm.ir.expr.RelayExpr, post: tvm.ir.expr.RelayExpr, node_map: tvm.ir.container.Map) → tvm.ir.expr.RelayExpr

Callback function to use when we found a match to the pattern

Parameters
  • pre (tvm.relay.Expr) – The matching expression from the original graph.

  • post (tvm.relay.Expr) – The matching expression with rewritten inputs

  • node_map (tvm.ir.container.Map[DFPattern, List[Expr]]) – The map between patterns and matched expressions

Returns

result – The Expression with matched subgraph rewritten by the callback

Return type

tvm.relay.Expr

tvm.relay.dataflow_pattern.rewrite(callbacks, expr: tvm.ir.expr.RelayExpr, mod: Optional[tvm.ir.module.IRModule] = None) → tvm.ir.expr.RelayExpr

Rewrite expression with the given callbacks.

Parameters
Returns

result – The Expression with matched subgraphs rewritten by the callbacks.

Return type

tvm.relay.Expr

tvm.relay.dataflow_pattern.partition(pattern: tvm.relay.dataflow_pattern.DFPattern, expr: tvm.ir.expr.RelayExpr, attrs: Optional[Dict[str, tvm.runtime.object.Object]] = None, check: Callable[tvm.ir.expr.RelayExpr, bool] = <function <lambda>>) → tvm.ir.expr.RelayExpr

Parition the expression into a series of functions that match the pattern

Parameters
  • partion (tvm.relay.dataflow_pattern.DFPattern) – The pattern to match

  • expr (tvm.relay.Expr) – The expression to split into functions

  • attrs (Optional[Dict[str, Object]]) – A dict of attributes to apply to the partitioned function

  • check (Callable[[Expr], bool]) – A function to perform more complicated checks on the matched expression. Returns true if partitioning should proceed, false otherwise.

Returns

result – The Expression with matched subgraphs replaced by function calls to that subgraph

Return type

tvm.relay.Expr