[BugFix][Relax] change FuseOpsByPattern strategy to pattern-match maximal subgraph #16922
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
At first, FuseOpsByPattern for Relax used a strategy to match maximal subgraph.
But this was changed in a previous PR #15308.
The intention was to fix a bug: in FuseOpsByPattern, when two matched subgraph are intersected, they are incorrectly merged together.
The bug was fixed by returning only the first subgraph if multiple subgraph are intersected.
The solution successfully solved the problem described there, but unfortunately, resulted in a minimal subgraph match strategy, probably unexpected.
Let me clarify with a compute graph as an example:
Consider this pattern:
matmul->add | matmul->add->clip
. It should match bothlv0+lv1
andlv0+lv1+lv2
. The solution in the aforementioned PR matches onlylv0+lv1
. But a stategy to match alllv0+lv1+lv2
is expected.This bug has been fixed by checking, when two matched subgraph are intersected, whether they have an inclusion relationship(i.e. whether subgraph A is contained in subgraph B), and if they have, the larger one is retained.
cc @wrongtest-intellif