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)

A pattern matching a function call node in Relay.

ConstantPattern()

A pattern matching a Relay Constant.

DFPattern()

Base class of all Patterns.

DFPatternCallback([require_type, rewrite_once])

A Callback for Pattern Rewriting.

DataTypePattern(pattern, dtype)

A pattern that matches another pattern with certain data type

DominatorPattern(parent, path, child)

Match a domination graph.

Expr

RelayExpr 的别名

ExprPattern(expr)

A pattern which matches a constant expression.

FunctionPattern(params, body)

A pattern matching a function node in Relay.

IfPattern(cond, true_branch, false_branch)

A patern matching a Relay If.

LetPattern(var, value, body)

A patern matching a Relay Let.

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

A local variable in Relay.

WildcardPattern()

A pattern which matches anything.

Functions:

astext(obj[, show_meta_data, annotate])

Get the text format of the expression.

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_if(cond, true_branch, false_branch)

Syntatic sugar for creating an IfPattern.

is_let(var, value, body)

Syntatic sugar for creating a LetPattern.

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, check])

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

pretty_print(obj)

Pretty print the object.

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.AltPattern(left: DFPattern, right: DFPattern)[源代码]#

Create a Pattern that can match one of two conditions

参数:
class tvm.relay.dataflow_pattern.AttrPattern(pattern: DFPattern, attrs: Attrs)[源代码]#

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

参数:
class tvm.relay.dataflow_pattern.CallPattern(op: DFPattern, args: List[DFPattern])[源代码]#

A pattern matching a function call node in Relay.

参数:
class tvm.relay.dataflow_pattern.ConstantPattern[源代码]#

A pattern matching a Relay Constant.

class tvm.relay.dataflow_pattern.DFPattern[源代码]#

Base class of all Patterns.

Methods:

astext([show_meta_data, annotate])

Get the text format of the expression.

dominates(parent[, path])

Create a dominator for this pattern.

has_attr(attrs)

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)

Create a optional user of this pattern.

partition(expr[, attrs, check])

Partition the expression into functions defined by this pattern

astext(show_meta_data=True, annotate=None)[源代码]#

Get the text format of the expression.

参数:
  • show_meta_data (bool) -- Whether to include meta data section in the text if there is meta data.

  • annotate (Optional[Object->str]) -- Optionally annotate function to provide additional information in the comment block.

返回:

text -- The text format of the expression.

返回类型:

str

备注

The meta data section is necessary to fully parse the text format. However, it can contain dumps that are big (e.g constant weights), so it can be helpful to skip printing the meta data section.

dominates(parent: DFPattern, path: DFPattern = None)[源代码]#

Create a dominator for this pattern.

参数:
返回:

result -- The resulting DominatorPattern.

返回类型:

tvm.relay.dataflow_pattern.DFPattern

has_attr(attrs: Dict[str, Object])[源代码]#

Add an attribute constraint to this pattern

参数:

attrs (Dict[str, Object])

返回:

result -- The resulting AttrPattern

返回类型:

tvm.relay.dataflow_pattern.DFPattern

has_dtype(dtype: str)[源代码]#

Add a type constraint to this pattern

参数:

dtype (str) -- The dtype to match

返回:

result -- The resulting DataTypePattern

返回类型:

tvm.relay.dataflow_pattern.DFPattern

has_shape(shape: List[PrimExpr])[源代码]#

Add a type constraint to this pattern

参数:

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

返回:

result -- The resulting ShapePattern

返回类型:

tvm.relay.dataflow_pattern.DFPattern

has_type(ttype: Type)[源代码]#

Add a type constraint to this pattern

参数:

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

返回:

result -- The resulting TypePattern

返回类型:

tvm.relay.dataflow_pattern.DFPattern

match(expr: RelayExpr) bool[源代码]#

Match this pattern to an expression

参数:

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

返回:

result -- Whether or not the expression matches the pattern

返回类型:

bool

optional(option_constructor: Callable[[DFPattern], DFPattern])[源代码]#

Create a optional user of this pattern.

参数:

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

返回:

result -- The resulting Pattern

返回类型:

tvm.relay.dataflow_pattern.DFPattern

partition(expr: ~tvm.ir.expr.RelayExpr, attrs: ~typing.Dict[str, ~tvm.runtime.object.Object] | None = None, check: ~typing.Callable[[~tvm.ir.expr.RelayExpr], bool] = <function DFPattern.<lambda>>) RelayExpr[源代码]#

Partition the expression into functions defined by this pattern

参数:
  • 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.

返回:

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

返回类型:

tvm.relay.Expr

class tvm.relay.dataflow_pattern.DFPatternCallback(require_type=False, rewrite_once=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

参数:
  • require_type (bool) -- Whether InferType is required to be run before the callback.

  • rewrite_once (bool) -- If True, run the callback only once.

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

callback(pre: RelayExpr, post: RelayExpr, node_map: Map) RelayExpr[源代码]#

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

参数:
  • 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

返回:

result -- The Expression with matched subgraph rewritten by the callback

返回类型:

tvm.relay.Expr

rewrite(expr: RelayExpr) RelayExpr[源代码]#

Rewrite expression with this callback

参数:

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

返回:

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

返回类型:

tvm.relay.Expr

class tvm.relay.dataflow_pattern.DataTypePattern(pattern: DFPattern, dtype: str)[源代码]#

A pattern that matches another pattern with certain data type

参数:
class tvm.relay.dataflow_pattern.DominatorPattern(parent: DFPattern, path: DFPattern, child: DFPattern)[源代码]#

Match a domination graph.

参数:
tvm.relay.dataflow_pattern.Expr#

RelayExpr 的别名

class tvm.relay.dataflow_pattern.ExprPattern(expr: RelayExpr)[源代码]#

A pattern which matches a constant expression.

参数:

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

class tvm.relay.dataflow_pattern.FunctionPattern(params: List[DFPattern], body: DFPattern)[源代码]#

A pattern matching a function node in Relay.

参数:
class tvm.relay.dataflow_pattern.IfPattern(cond: DFPattern, true_branch: DFPattern, false_branch: DFPattern)[源代码]#

A patern matching a Relay If.

参数:
class tvm.relay.dataflow_pattern.LetPattern(var: DFPattern, value: DFPattern, body: DFPattern)[源代码]#

A patern matching a Relay Let.

参数:
class tvm.relay.dataflow_pattern.ShapePattern(pattern: DFPattern, shape: List[PrimExpr])[源代码]#

A pattern that matches another pattern with a certain tensor shape

参数:
class tvm.relay.dataflow_pattern.TupleGetItemPattern(tuple_value: DFPattern, index: int | None = None)[源代码]#

Get index-th item from a TuplePattern.

参数:
class tvm.relay.dataflow_pattern.TuplePattern(fields: Array)[源代码]#

A patern matching a Relay Tuple.

参数:

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

class tvm.relay.dataflow_pattern.TypePattern(pattern: DFPattern, ttype: Type)[源代码]#

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

参数:
class tvm.relay.dataflow_pattern.VarPattern(name_hint: str = '')[源代码]#

A local variable in Relay.

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

参数:
  • 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.WildcardPattern[源代码]#

A pattern which matches anything.

Methods:

redirect_to(pat)

Redirect the WildcardPattern to another pattern

redirect_to(pat: DFPattern)[源代码]#

Redirect the WildcardPattern to another pattern

参数:

pat (relay.dataflow_pattern.DFPattern) -- The pattern that wildcard is redirected to.

tvm.relay.dataflow_pattern.astext(obj: Object, show_meta_data=True, annotate=None)[源代码]#

Get the text format of the expression.

参数:
  • obj (Object) -- The object to be printed.

  • show_meta_data (bool) -- Whether to include meta data section in the text if there is meta data.

  • annotate (Optional[Object->str]) -- Optionally annotate function to provide additional information in the comment block.

返回:

text -- The text format of the expression.

返回类型:

str

备注

The meta data section is necessary to fully parse the text format. However, it can contain dumps that are big (e.g constant weights), so it can be helpful to skip printing the meta data section.

tvm.relay.dataflow_pattern.dominates(parent: DFPattern, path: DFPattern, child: DFPattern) DFPattern[源代码]#

Syntatic sugar for creating an Dominator pattern

参数:
返回:

result -- The resulting DominatorPattern.

返回类型:

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.get(op_name)[源代码]#

Get the Op for a given name

参数:

op_name (str) -- The operator name

返回:

op -- The op of the corresponding name

返回类型:

Op

tvm.relay.dataflow_pattern.has_attr(attrs, pattern=None) DFPattern[源代码]#

Syntatic sugar for creating an AttrPattern

参数:
返回:

result -- The resulting AttrPattern

返回类型:

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.has_dtype(dtype: str, pattern: DFPattern = None) DFPattern[源代码]#

Syntatic sugar for creating a DataTypePattern

参数:
返回:

result -- The resulting DataTypePattern

返回类型:

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.has_shape(shape: List[PrimExpr], pattern: DFPattern = None) DFPattern[源代码]#

Syntatic sugar for creating a ShapePattern

参数:
返回:

result -- The resulting ShapePattern

返回类型:

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.has_type(ttype: Type, pattern: DFPattern = None) DFPattern[源代码]#

Syntatic sugar for creating a TypePattern

参数:
返回:

result -- The resulting TypePattern

返回类型:

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.is_constant() DFPattern[源代码]#

Syntatic sugar for creating a ConstantPattern.

参数:

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

返回:

result -- The resulting pattern.

返回类型:

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.is_expr(expr: RelayExpr) DFPattern[源代码]#

Syntatic sugar for creating an ExprPattern.

参数:

expr (Expr) -- The Relay expression to match.

返回:

result -- The resulting pattern.

返回类型:

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.is_if(cond, true_branch, false_branch)[源代码]#

Syntatic sugar for creating an IfPattern.

参数:
返回:

result -- The resulting pattern.

返回类型:

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.is_let(var, value, body)[源代码]#

Syntatic sugar for creating a LetPattern.

参数:
返回:

result -- The resulting pattern.

返回类型:

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.is_op(op_name: str) DFPattern[源代码]#

Syntatic sugar for creating an operator ExprPattern.

参数:

op_name (String) -- The name of the relay op

返回:

result -- The resulting ExprPattern

返回类型:

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.is_tuple(fields: Array) DFPattern[源代码]#

Syntatic sugar for creating an ExprPattern.

参数:

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

返回:

result -- The resulting pattern.

返回类型:

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.is_tuple_get_item(tuple_value: DFPattern, index: int | None = None) DFPattern[源代码]#

Syntatic sugar for creating an ExprPattern.

参数:
返回:

result -- The resulting pattern.

返回类型:

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.is_var(name: str = '') DFPattern[源代码]#

Syntatic sugar for creating an optionally named VarPattern.

参数:

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

返回:

result -- The resulting pattern.

返回类型:

tvm.relay.dataflow_pattern.DFPattern

tvm.relay.dataflow_pattern.make_node(type_key, **kwargs)[源代码]#

Make a new IR node by its type key and fields

参数:
  • type_key (str) -- The type key of the node.

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

返回:

node -- The corresponding IR Node

返回类型:

Node

备注

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.

示例

The following code constructs a IntImm object

x = tvm.ir.make_node("IntImm", dtype="int32", value=10, span=None)
assert isinstance(x, tvm.tir.IntImm)
assert x.value == 10
tvm.relay.dataflow_pattern.match(pattern: DFPattern, expr: RelayExpr) bool[源代码]#

Match a pattern to an expression

参数:
tvm.relay.dataflow_pattern.partition(pattern: ~tvm.relay.dataflow_pattern.DFPattern, expr: ~tvm.ir.expr.RelayExpr, attrs: ~typing.Dict[str, ~tvm.runtime.object.Object] | None = None, check: ~typing.Callable[[~tvm.ir.expr.RelayExpr], bool] = <function <lambda>>) RelayExpr[源代码]#

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

参数:
  • pattern (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.

返回:

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

返回类型:

tvm.relay.Expr

tvm.relay.dataflow_pattern.pretty_print(obj: Object) None[源代码]#

Pretty print the object.

tvm.relay.dataflow_pattern.register_df_node(type_key=None)[源代码]#

Register a Relay node type.

参数:

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

tvm.relay.dataflow_pattern.rewrite(callbacks, expr: RelayExpr, mod: IRModule | None = None) RelayExpr[源代码]#

Rewrite expression with the given callbacks.

参数:
返回:

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

返回类型:

tvm.relay.Expr

tvm.relay.dataflow_pattern.wildcard() DFPattern[源代码]#

Syntatic sugar for creating a WildcardPattern.

返回:

result -- The resulting pattern.

返回类型:

tvm.relay.dataflow_pattern.DFPattern