Skip to content

Commit

Permalink
add docs for fuzzy matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew committed Apr 1, 2021
1 parent d374908 commit 100374d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/langref/relay_pattern.rst
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,22 @@ The final example is matching diamonds with a post-dominator relationship. We em
assert diamond.match(out)


Matching Fuzzy Patterns
=======================

The Dominator analysis above lets one match a subgraph of Relay AST that doesn't correspond to a set of patterns nodes exactly 1-to-1. There are a few other places where we support such "fuzzy" matching.

Tuples, Functions, and Call nodes with any number of inputs can be matched by passing `None` as the argument value, i.e.::

tuple_pattern = is_tuple(None)
func_pattern = FunctionPattern(None, wildcard() + wildcard())
call_pattern = func_pattern(None)

These patterns allow matching more generic classes patterns by constraining the use of the arguments rather than the number of arguments.

Additionally, we support matching Functions with fuzzy bodies, i.e., a function body that is under constrained by the pattern. The pattern `FunctionPattern([is_var(), is_var()], wildcard() + wildcard()])` will match `relay.Function([x, y], x + y)`, but it will also match `relay.Function([x, y], x * x + y)`. In the second case, the pattern doesn't perfectly constrain the body of the function, so the resulting match is fuzzy.


Pattern Language Design
=======================

Expand Down
2 changes: 1 addition & 1 deletion src/relay/ir/dataflow_matcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ class PatternPartitioner : protected MixedModeMutator {
Expr Partition(const DFPattern& pattern, const Expr& pre, const Map<String, ObjectRef>& attrs,
PackedFunc check) {
if (pattern.as<FunctionPatternNode>()) {
LOG(WARNING) << "Paritioning a Function that isn't called doesn't make sense, skipping"
LOG(WARNING) << "Partioning a Function that isn't called doesn't make sense, skipping"
<< pattern;
return pre;
}
Expand Down

0 comments on commit 100374d

Please sign in to comment.