Skip to content

[Bugfix] Support tree-like subgraph matching#58

Merged
chhzh123 merged 5 commits intoawslabs:mainfrom
chhzh123:tree-subgraph
Feb 15, 2023
Merged

[Bugfix] Support tree-like subgraph matching#58
chhzh123 merged 5 commits intoawslabs:mainfrom
chhzh123:tree-subgraph

Conversation

@chhzh123
Copy link
Contributor

Description

For the following tree-like subgraph with multiple inputs, if we use the pattern to match the +, -, and * operators, only + and * will be returned. This is because the current implementation of subgraph matching starts from the first instruction of the original subgraph and target subgraph, and test if users of the instruction are the same. The process is iterative and basically is a DFS. However, this process may ignore instruction b since b is not the user of a.

class Model(nn.Module):
    def forward(self, x, y, z):
        a = x + y
        b = y - z
        c = a * b
        return c
def pattern(x, y, z):
    return (x + y) * (y - z)

To solve this problem, we can simply traverse the graph node from top to bottom following the order of the link list (i.e., visiting the successors in a topological order), and compare the nodes in the original and target subgraph, so that all the +, -, and * operators will be matched. Notice this fix still cannot support commutativity between operands, so pattern (y - z) * (x + y) cannot match any subgraphs. This feature may need more engineering efforts in the future.

Also, this PR adds concrete_args to .replace_module() for the case that the user-defined module has different numbers of arguments with the arguments in the original subgraph.

Checklist

  • PR's title starts with a category (e.g. [Bugfix], [Model], [Tutorial], etc)
  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage
  • Code is well-documented

@chhzh123 chhzh123 requested a review from comaniac February 15, 2023 07:28
Copy link
Contributor

@comaniac comaniac left a comment

Choose a reason for hiding this comment

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

LGTM

@chhzh123
Copy link
Contributor Author

Thanks @comaniac

@chhzh123 chhzh123 merged commit e50b2e6 into awslabs:main Feb 15, 2023
@chhzh123 chhzh123 mentioned this pull request May 13, 2023
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants