Matches all possible non-overlapping sets of operators and their
data dependencies (pattern) in the Graph of a GraphModule
(gm), then replaces each of these matched subgraphs with another
subgraph (replacement).
Parameters:
gm (GraphModule) – The GraphModule that wraps the Graph to operate on
pattern (Callable[[...], Any] | GraphModule) – The subgraph to match in gm for replacement
A list of Match objects representing the places
in the original graph that pattern was matched to. The list
is empty if there are no matches. Match is defined as:
classMatch(NamedTuple):# Node from which the match was foundanchor:Node# Maps nodes in the pattern subgraph to nodes in the larger graphnodes_map:Dict[Node,Node]
The above code will first match pattern in the forward
method of traced_module. Pattern-matching is done based on
use-def relationships, not node names. For example, if you had
p=torch.cat([a,b]) in pattern, you could match
m=torch.cat([a,b]) in the original forward function,
despite the variable names being different (p vs m).
The return statement in pattern is matched based on its
value only; it may or may not match to the return statement in
the larger graph. In other words, the pattern doesn’t have to extend
to the end of the larger graph.
When the pattern is matched, it will be removed from the larger
function and replaced by replacement. If there are multiple
matches for pattern in the larger function, each non-overlapping
match will be replaced. In the case of a match overlap, the first
found match in the set of overlapping matches will be replaced.
(“First” here being defined as the first in a topological ordering
of the Nodes’ use-def relationships. In most cases, the first Node
is the parameter that appears directly after self, while the
last Node is whatever the function returns.)
One important thing to note is that the parameters of the
pattern Callable must be used in the Callable itself,
and the parameters of the replacement Callable must match
the pattern. The first rule is why, in the above code block, the
forward function has parameters x,w1,w2, but the
pattern function only has parameters w1,w2. pattern
doesn’t use x, so it shouldn’t specify x as a parameter.
As an example of the second rule, consider replacing
defpattern(x,y):returntorch.neg(x)+torch.relu(y)
with
defreplacement(x,y):returntorch.relu(x)
In this case, replacement needs the same number of parameters
as pattern (both x and y), even though the parameter
y isn’t used in replacement.
After calling subgraph_rewriter.replace_pattern, the generated
Python code looks like this: