Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ torch.fx.proxy.TracerBase.iter(self, obj: 'Proxy') -> Iterator
torch.fx.proxy.TracerBase.keys(self, obj: 'Proxy') -> Any
torch.fx.proxy.TracerBase.proxy(self, node: torch.fx.node.Node) -> 'Proxy'
torch.fx.proxy.TracerBase.to_bool(self, obj: 'Proxy') -> bool
torch.fx.subgraph_rewriter.replace_pattern(gm: torch.fx.graph_module.GraphModule, pattern: Callable, replacement: Callable) -> List[torch.fx.subgraph_rewriter.Match]
torch.fx.subgraph_rewriter.replace_pattern(gm: torch.fx.graph_module.GraphModule, pattern: Union[Callable, torch.fx.graph_module.GraphModule], replacement: Union[Callable, torch.fx.graph_module.GraphModule]) -> List[torch.fx.subgraph_rewriter.Match]
28 changes: 20 additions & 8 deletions torch/fx/subgraph_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ._compatibility import compatibility

import copy
from typing import Callable, Dict, List, NamedTuple, Optional, Set
from typing import Callable, Dict, List, NamedTuple, Optional, Set, Union
import torch

__all__ = ['Match', 'replace_pattern', 'replace_pattern_with_filters']
Expand Down Expand Up @@ -65,7 +65,11 @@ def try_get_submodule(mod: torch.nn.Module, target: str) -> Optional[torch.nn.Mo


@compatibility(is_backward_compatible=True)
def replace_pattern(gm: GraphModule, pattern: Callable, replacement: Callable) -> List[Match]:
def replace_pattern(
gm: GraphModule,
pattern: Union[Callable, GraphModule],
replacement: Union[Callable, GraphModule]
) -> List[Match]:
"""
Matches all possible non-overlapping sets of operators and their
data dependencies (``pattern``) in the Graph of a GraphModule
Expand Down Expand Up @@ -187,8 +191,8 @@ def forward(self, x, w1, w2):
@compatibility(is_backward_compatible=False)
def replace_pattern_with_filters(
gm: GraphModule,
pattern: Callable,
replacement: Callable,
pattern: Union[Callable, GraphModule],
replacement: Union[Callable, GraphModule],
match_filters: List[Callable[["InternalMatch", Graph, Graph], bool]], # type: ignore[name-defined]
) -> List[Match]:
"""
Expand All @@ -205,8 +209,8 @@ def replace_pattern_with_filters(

def _replace_pattern(
gm: GraphModule,
pattern: Callable,
replacement: Callable,
pattern: Union[Callable, GraphModule],
replacement: Union[Callable, GraphModule],
match_filters: List[Callable[["InternalMatch", Graph, Graph], bool]] = None # type: ignore[name-defined]
) -> List[Match]:

Expand All @@ -217,8 +221,16 @@ def _replace_pattern(

# Get the graphs for `gm`, `pattern`, `replacement`
original_graph: Graph = gm.graph
pattern_graph: Graph = symbolic_trace(pattern).graph
replacement_graph: Graph = symbolic_trace(replacement).graph

if isinstance(pattern, GraphModule):
pattern_graph = pattern.graph
else:
pattern_graph = symbolic_trace(pattern).graph
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we do symbolic_trace then the pattern can be a nn.Module I think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol... nn.Module is also a Callable, coz it implemented call
So we are good

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I think I misread the PR, sounds good


if isinstance(replacement, GraphModule):
replacement_graph = replacement.graph
else:
replacement_graph = symbolic_trace(replacement).graph

matcher = SubgraphMatcher(pattern_graph, match_output=False, match_placeholder=False,
remove_overlapping_matches=True)
Expand Down