MatchResult

class onnxscript.rewriter.pattern.MatchResult[source]

The state object used by the pattern-matching algorithm.

A match can either succeed or fail. If it succeeds, it returns a list of nodes that matched the pattern and a set of bindings for the variables in the pattern.

Example:

def pattern(x, shape1, shape2):
    t1 = op.Reshape(x, shape1)
    t2 = op.Reshape(t1, shape2)
    return t2

The above pattern matches a sequence of two Reshape ops. The matched_nodes will contain the two Reshape ops, and the bindings will contain the values that are bound to the variables x, shape1, and shape2.

enter_new_match() None[source]

Starts a new sub-match to try out one of multiple alternatives.

abandon_current_match() PartialMatchResult[source]

Abandons the current alternative due to failure.

merge_current_match() None[source]

Merges a successful sub-match for an alternative with the parent one.

fail(reason: str = '', failure_source: Node | Value | list[Node | Value] | None = None) MatchResult[source]
property reason: str

Returns the reason for the failure.

property nodes: Sequence[Node]

Returns the list of nodes that matched the pattern.

bind_node(pattern_node: _pattern_ir.NodePattern, node: ir.Node)[source]

Binds a pattern node to a matched node.

add_node(node: Node) None[source]

Adds a node to the list of matched nodes.

bind_value(pattern_value: _pattern_ir.ValuePattern, value: Any) bool[source]
bind(var: str, value: Any) bool[source]
property bindings: dict[str, Any]

Returns the bindings for the pattern variables.

property value_bindings: dict[_pattern_ir.ValuePattern, ir.Value]

Returns the bindings for the value variables.

property node_bindings: dict[_pattern_ir.NodePattern, ir.Node]

Returns the bindings for the node variables.

property outputs: MutableSequence[Value]

Returns the list of output values that matched the pattern.

property failure_nodes_and_values: list[Node | Value]

Returns the nodes and values that caused the failure.

lookup_node(pattern_node: _pattern_ir.NodePattern) ir.Node | None[source]

Looks up the node that matched the given pattern node.

num_matched_nodes() int[source]

Returns the number of nodes matched so far.