tvm.relay.dataflow_pattern#
The Relay Pattern Language and tooling.
Classes:
|
Create a Pattern that can match one of two conditions |
|
Get match an expression with a certain attributes. |
|
A pattern matching a function call node in Relay. |
A pattern matching a Relay Constant. |
|
Base class of all Patterns. |
|
|
A Callback for Pattern Rewriting. |
|
A pattern that matches another pattern with certain data type |
|
Match a domination graph. |
|
|
|
A pattern which matches a constant expression. |
|
A pattern matching a function node in Relay. |
|
A patern matching a Relay If. |
|
A patern matching a Relay Let. |
|
A pattern that matches another pattern with a certain tensor shape |
|
Get index-th item from a TuplePattern. |
|
A patern matching a Relay Tuple. |
|
A pattern that matches another pattern with a certain type annotation. |
|
A local variable in Relay. |
A pattern which matches anything. |
|
|
C++ implemenation |
Functions:
|
Get the text format of the expression. |
|
Syntatic sugar for creating an Dominator pattern |
|
Get the Op for a given name |
|
Syntatic sugar for creating an AttrPattern |
|
Syntatic sugar for creating a DataTypePattern |
|
Syntatic sugar for creating a ShapePattern |
|
Syntatic sugar for creating a TypePattern |
Syntatic sugar for creating a ConstantPattern. |
|
|
Syntatic sugar for creating an ExprPattern. |
|
Syntatic sugar for creating an IfPattern. |
|
Syntatic sugar for creating a LetPattern. |
|
Syntatic sugar for creating an operator ExprPattern. |
|
Syntatic sugar for creating an ExprPattern. |
|
Syntatic sugar for creating an ExprPattern. |
|
Syntatic sugar for creating an optionally named VarPattern. |
|
Make a new IR node by its type key and fields |
|
Match a pattern to an expression |
|
Parition the expression into a series of functions that match the pattern |
|
Pretty print the object. |
|
Register a Relay node type. |
|
Rewrite expression with the given callbacks. |
|
Syntatic sugar for creating a WildcardPattern. |
- class tvm.relay.dataflow_pattern.AltPattern(left, right)[源代码]#
Create a Pattern that can match one of two conditions
Parameters#
- left: tvm.relay.dataflow_pattern.DFPattern
One possible matching pattern.
- right: tvm.relay.dataflow_pattern.DFPattern
One possible matching pattern.
- class tvm.relay.dataflow_pattern.AttrPattern(pattern, attrs)[源代码]#
Get match an expression with a certain attributes. Currently only supports Op Attributes, not call Attributes.
Parameters#
- pattern: tvm.relay.dataflow_pattern.DFPattern
The input pattern.
- attrs: tvm.ir.attrs.Attrs
The attributes to match.
- class tvm.relay.dataflow_pattern.CallPattern(op, args)[源代码]#
A pattern matching a function call node in Relay.
Parameters#
- op: relay.dataflow_pattern.DFPattern
The operation to be called.
- args: List[relay.dataflow_pattern.DFPattern]
The arguments to the call or None to match any arguments.
- 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.
Parameters#
- show_meta_databool
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.
Returns#
- textstr
The text format of the expression.
Notes#
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, path=None)[源代码]#
Create a dominator for this pattern.
Parameters#
- parent: tvm.relay.dataflow_pattern.DFPattern
The parent pattern this pattern dominates.
- path: tvm.relay.dataflow_pattern.DFPattern
The fuzzy path pattern.
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting DominatorPattern.
- has_attr(attrs)[源代码]#
Add an attribute constraint to this pattern
Parameters#
attrs: Dict[str, Object]
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting AttrPattern
- has_dtype(dtype)[源代码]#
Add a type constraint to this pattern
Parameters#
- dtype: str
The dtype to match
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting DataTypePattern
- 参数:
dtype (str)
- has_shape(shape)[源代码]#
Add a type constraint to this pattern
Parameters#
- shape: List[tvm.ir.PrimExpr]
The shape to match
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting ShapePattern
- has_type(ttype)[源代码]#
Add a type constraint to this pattern
Parameters#
- ttype: tvm.ir.type.Type
The type to match
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting TypePattern
- 参数:
ttype (Type)
- match(expr)[源代码]#
Match this pattern to an expression
Parameters#
- exprtvm.relay.Expr
The expression to match.
Returns#
- result: bool
Whether or not the expression matches the pattern
- optional(option_constructor)[源代码]#
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: tvm.relay.dataflow_pattern.DFPattern
The resulting Pattern
- partition(expr, attrs=None, check=<function DFPattern.<lambda>>)[源代码]#
Partition the expression into functions defined by this pattern
Parameters#
- exprtvm.relay.Expr
The expression to match.
- attrsOptional[Dict[str, Object]]
A dictionary of Attribute name/values to add to the paritioned function
- checkCallable[[Expr], bool]
A function to perform more complicated checks on the matched expression. Returns true if partitioning should proceed, false otherwise.
Returns#
- resulttvm.relay.Expr
The Expression with matched subgraphs replaced by function calls to that subgraph
- 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
Parameters#
- 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, post, node_map)[源代码]#
Callback function to use when we found a match to the pattern
Parameters#
- pretvm.relay.Expr
The matching expression from the original graph.
- posttvm.relay.Expr
The matching expression with rewritten inputs
- node_maptvm.ir.container.Map[DFPattern, List[Expr]]
The map between patterns and matched expressions
Returns#
- resulttvm.relay.Expr
The Expression with matched subgraph rewritten by the callback
- class tvm.relay.dataflow_pattern.DataTypePattern(pattern, dtype)[源代码]#
A pattern that matches another pattern with certain data type
Parameters#
- pattern: tvm.relay.dataflow_pattern.DFPattern
The input pattern that needs type annotation.
- dtype: str
The dtype to match.
- class tvm.relay.dataflow_pattern.DominatorPattern(parent, path, child)[源代码]#
Match a domination graph.
Parameters#
- parent: tvm.relay.dataflow_pattern.DFPattern
The parent, i.e., the single node which produces something, later aggregated by the child.
- path: tvm.relay.dataflow_pattern.DFPattern
The fuzzy path pattern between parent and child, typically matches elementwise ops.
- child: tvm.relay.dataflow_pattern.DFPattern
The last node in the domination which is the end user for all nodes in the path and the parent.
- tvm.relay.dataflow_pattern.Expr#
RelayExpr
的别名 Attributes:checked_type
Get the checked type of tvm.relay.Expr.
struct_info
Get the struct info field
- class tvm.relay.dataflow_pattern.ExprPattern(expr)[源代码]#
A pattern which matches a constant expression.
Parameters#
- exprtvm.relay.Expr
The expression to match.
- 参数:
expr (RelayExpr)
- class tvm.relay.dataflow_pattern.FunctionPattern(params, body)[源代码]#
A pattern matching a function node in Relay.
Parameters#
- params: List[relay.dataflow_pattern.DFPattern]
The parameters to the Function or None to match any parameters.
- body: relay.dataflow_pattern.DFPattern
The body fo the Function
- class tvm.relay.dataflow_pattern.IfPattern(cond, true_branch, false_branch)[源代码]#
A patern matching a Relay If.
Parameters#
- cond: tvm.relay.dataflow_pattern.DFPattern
The pattern describing the condition of If.
- true_branch: tvm.relay.dataflow_pattern.DFPattern
The pattern describing the true branch of If.
- false_branch: tvm.relay.dataflow_pattern.DFPattern
The pattern describing the false branch of If.
- class tvm.relay.dataflow_pattern.LetPattern(var, value, body)[源代码]#
A patern matching a Relay Let.
Parameters#
- var: tvm.relay.dataflow_pattern.DFPattern
The pattern describing the variable of Let.
- value: tvm.relay.dataflow_pattern.DFPattern
The pattern describing the value of Let.
- body: tvm.relay.dataflow_pattern.DFPattern
The pattern describing the body where the binding is in effect.
- class tvm.relay.dataflow_pattern.ShapePattern(pattern, shape)[源代码]#
A pattern that matches another pattern with a certain tensor shape
Parameters#
- pattern: tvm.relay.dataflow_pattern.DFPattern
The input pattern that needs type annotation.
- shape: List[tvm.ir.PrimExpr]
The shape to match.
- class tvm.relay.dataflow_pattern.TupleGetItemPattern(tuple_value, index=None)[源代码]#
Get index-th item from a TuplePattern.
Parameters#
- tuple_value: tvm.relay.dataflow_pattern.DFPattern
The input tuple expression.
- index: Optional[int]
The index to match; Default (None) to match a TupleGetItem with any index.
- class tvm.relay.dataflow_pattern.TuplePattern(fields)[源代码]#
A patern matching a Relay Tuple.
Parameters#
- fieldsArray[tvm.relay.dataflow_pattern.DFPattern]
The fields in the tuple.
- 参数:
fields (Array)
- class tvm.relay.dataflow_pattern.TypePattern(pattern, ttype)[源代码]#
A pattern that matches another pattern with a certain type annotation.
Parameters#
- pattern: tvm.relay.dataflow_pattern.DFPattern
The input pattern that needs type annotation.
- ttype: tvm.ir.type.Type
The type to match.
- class tvm.relay.dataflow_pattern.VarPattern(name_hint='')[源代码]#
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.
- 参数:
name_hint (str)
- class tvm.relay.dataflow_pattern.WildcardPattern[源代码]#
A pattern which matches anything.
Methods:
redirect_to
(pat)Redirect the WildcardPattern to another pattern
- class tvm.relay.dataflow_pattern._DFPatternCallback(pattern, callback, require_type, rewrite_once)[源代码]#
C++ implemenation
- tvm.relay.dataflow_pattern.astext(obj, show_meta_data=True, annotate=None)[源代码]#
Get the text format of the expression.
Parameters#
- objObject
The object to be printed.
- show_meta_databool
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.
Returns#
- textstr
The text format of the expression.
Notes#
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.
- 参数:
obj (Object)
- tvm.relay.dataflow_pattern.dominates(parent, path, child)[源代码]#
Syntatic sugar for creating an Dominator pattern
Parameters#
- parent: tvm.relay.dataflow_pattern.DFPattern
The parent pattern.
- path: tvm.relay.dataflow_pattern.DFPattern
The fuzzy path pattern.
- child: tvm.relay.dataflow_pattern.DFPattern
The child pattern.
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting DominatorPattern.
- tvm.relay.dataflow_pattern.get(op_name)[源代码]#
Get the Op for a given name
Parameters#
- op_namestr
The operator name
Returns#
- opOp
The op of the corresponding name
- tvm.relay.dataflow_pattern.has_attr(attrs, pattern=None)[源代码]#
Syntatic sugar for creating an AttrPattern
Parameters#
- attrs: Dict[str, Object]
The attributes to match
- pattern: Optional[tvm.relay.dataflow_pattern.DFPattern]
The input pattern.
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting AttrPattern
- 返回类型:
- tvm.relay.dataflow_pattern.has_dtype(dtype, pattern=None)[源代码]#
Syntatic sugar for creating a DataTypePattern
Parameters#
- dtype: str
The dtype to match
- pattern: tvm.relay.dataflow_pattern.DFPattern
The pattern that needs type annotation
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting DataTypePattern
- tvm.relay.dataflow_pattern.has_shape(shape, pattern=None)[源代码]#
Syntatic sugar for creating a ShapePattern
Parameters#
- shape: List[tvm.ir.PrimExpr]
The shape to match
- pattern: tvm.relay.dataflow_pattern.DFPattern
The pattern that needs type annotation
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting ShapePattern
- tvm.relay.dataflow_pattern.has_type(ttype, pattern=None)[源代码]#
Syntatic sugar for creating a TypePattern
Parameters#
- ttype: tvm.ir.type.Type
The type to match
- pattern: tvm.relay.dataflow_pattern.DFPattern
The pattern that needs type annotation
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting TypePattern
- tvm.relay.dataflow_pattern.is_constant()[源代码]#
Syntatic sugar for creating a ConstantPattern.
Parameters#
- name: str
The name of the input pattern to match.
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting pattern.
- 返回类型:
- tvm.relay.dataflow_pattern.is_expr(expr)[源代码]#
Syntatic sugar for creating an ExprPattern.
Parameters#
- expr: Expr
The Relay expression to match.
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting pattern.
- tvm.relay.dataflow_pattern.is_if(cond, true_branch, false_branch)[源代码]#
Syntatic sugar for creating an IfPattern.
Parameters#
- cond: tvm.relay.dataflow_pattern.DFPattern
The pattern describing the condition of If.
- true_branch: tvm.relay.dataflow_pattern.DFPattern
The pattern describing the true branch of If.
- false_branch: tvm.relay.dataflow_pattern.DFPattern
The pattern describing the false branch of If.
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting pattern.
- tvm.relay.dataflow_pattern.is_let(var, value, body)[源代码]#
Syntatic sugar for creating a LetPattern.
Parameters#
- var: tvm.relay.dataflow_pattern.DFPattern
The pattern describing the variable of Let.
- value: tvm.relay.dataflow_pattern.DFPattern
The pattern describing the value of Let.
- body: tvm.relay.dataflow_pattern.DFPattern
The pattern describing the body where the binding is in effect.
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting pattern.
- tvm.relay.dataflow_pattern.is_op(op_name)[源代码]#
Syntatic sugar for creating an operator ExprPattern.
Parameters#
- op_name: String
The name of the relay op
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting ExprPattern
- tvm.relay.dataflow_pattern.is_tuple(fields)[源代码]#
Syntatic sugar for creating an ExprPattern.
Parameters#
- fieldsArray[tvm.relay.dataflow_pattern.DFPattern]
The fields in the tuple.
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting pattern.
- tvm.relay.dataflow_pattern.is_tuple_get_item(tuple_value, index=None)[源代码]#
Syntatic sugar for creating an ExprPattern.
Parameters#
- tuple_value: tvm.relay.dataflow_pattern.DFPattern
The input tuple expression.
- index: Optional[int]
The index to match; Default (None) to match a TupleGetItem with any index.
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting pattern.
- tvm.relay.dataflow_pattern.is_var(name='')[源代码]#
Syntatic sugar for creating an optionally named VarPattern.
Parameters#
- name: str
The name of the input pattern to match.
Returns#
- result: tvm.relay.dataflow_pattern.DFPattern
The resulting pattern.
- tvm.relay.dataflow_pattern.make_node(type_key, **kwargs)[源代码]#
Make a new IR node by its type key and fields
Parameters#
- type_keystr
The type key of the node.
- **kwargsdict
The fields of the node.
Returns#
- nodeNode
The corresponding IR 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, span=None) assert isinstance(x, tvm.tir.IntImm) assert x.value == 10
- tvm.relay.dataflow_pattern.match(pattern, expr)[源代码]#
Match a pattern to an expression
Parameters#
- pattern: tvm.relay.dataflow_pattern.DFPattern
The input pattern.
- exprtvm.relay.Expr
The expression to match.
- tvm.relay.dataflow_pattern.partition(pattern, expr, attrs=None, check=<function <lambda>>)[源代码]#
Parition the expression into a series of functions that match the pattern
Parameters#
- pattern: tvm.relay.dataflow_pattern.DFPattern
The pattern to match
- exprtvm.relay.Expr
The expression to split into functions
- attrsOptional[Dict[str, Object]]
A dict of attributes to apply to the partitioned function
- checkCallable[[Expr], bool]
A function to perform more complicated checks on the matched expression. Returns true if partitioning should proceed, false otherwise.
Returns#
- resulttvm.relay.Expr
The Expression with matched subgraphs replaced by function calls to that subgraph
- tvm.relay.dataflow_pattern.pretty_print(obj)[源代码]#
Pretty print the object.
- 参数:
obj (Object)
- 返回类型:
None
- tvm.relay.dataflow_pattern.register_df_node(type_key=None)[源代码]#
Register a Relay node type.
Parameters#
- type_keystr or cls
The type key of the node.
- tvm.relay.dataflow_pattern.rewrite(callbacks, expr, mod=None)[源代码]#
Rewrite expression with the given callbacks.
Parameters#
- callbacks: tvm.relay.dataflow_pattern.DFPatternCallback
The input callback or list of callbacks.
- exprtvm.relay.Expr
The expression to rewrite.
- modOptional[tvm.ir.IRModule]
The module that associates with the expression.
Returns#
- resulttvm.relay.Expr
The Expression with matched subgraphs rewritten by the callbacks.