Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update InternalMatch in subgraph_rewriter after repeated replacements #99039

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions test/fx/test_subgraph_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def test_subgraph_rewriter_with_trivial_replacement(self):
class M(torch.nn.Module):
def forward(self, x):
val = torch.neg(x)
val = torch.add(val, val)
return torch.add(val, val)

def pattern(x):
Expand All @@ -115,9 +116,9 @@ def comparison(x):

ref_output = comparison_fn(x)
test_output = traced.forward(x)
one_replacement = len(matches) == 1 and len(matches[0].replacements) == 0
no_replacements = len(matches) == 2 and len(matches[1].replacements) == 0
self.assertEqual(ref_output, test_output)
self.assertTrue(one_replacement)
self.assertTrue(no_replacements)

def test_subgraph_rewriter_single_pattern_match(self):
class M(torch.nn.Module):
Expand Down
5 changes: 5 additions & 0 deletions torch/fx/subgraph_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ def _replace_pattern(
for rn, gn in zip(replacement_placeholders, match.placeholder_nodes):
if isinstance(gn, Node):
val_map[rn] = match_changed_node.get(gn, gn)
if gn in match.placeholder_nodes and gn in match_changed_node:
sanchitintel marked this conversation as resolved.
Show resolved Hide resolved
gn_ind = match.placeholder_nodes.index(gn)
match.placeholder_nodes[gn_ind] = match_changed_node[gn]
map_key = list(match.nodes_map.keys())[list(match.nodes_map.values()).index(gn)]
match.nodes_map[map_key] = match_changed_node[gn]
else:
val_map[rn] = gn

Expand Down