-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Unity] Remove non-deterministic behavior from graph pattern matching #14417
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
Conversation
|
Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.
Generated by tvm-bot |
481f10b to
e9a116a
Compare
| matmul2 = is_op("relax.matmul")(inp_pat, K_weight_pat) | ||
| matmul3 = is_op("relax.matmul")(inp_pat, V_weight_pat) | ||
|
|
||
| # TODO(masahi): Automate addition of used_by constraints during is_op |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a follow-up PR to address this, which removes all used_by stuff below @ganler
|
|
||
| const auto& uses = def2use.at(rparent->ptr); | ||
| // skip if `rparent` is not used by `r`. | ||
| if (uses.cend() == uses.find(r->ptr)) continue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been removed since it seems to be always false.
|
@ganler Is |
|
@masahi Thanks. Start hint means the start point of graph matching. It is useful when you have multiple matchable patterns and want to explicitly match one of them which are close to / subsuming certain node. |
|
(Traveling) if you can wait I can file a review after 10 hours but definitely within 24 hours. :). Hope it won't get you blocked. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the improvement!
Removing non-determinism should be a right way to go.
Overall, it looks good to me. We can merge this when @ganler can confirm with the implementation details.
ganler
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes make sense to me. I don't see any (apparent) logic flaws. Thanks for optimizing the code style and modernizing the code to C++17 standard!
|
|
||
| const auto commit = [&undo_stack](PNode* p, RNode* r) { | ||
| // match with each other. | ||
| // TODO(ganler, masahi): Why commit on the same p-r pair happens more than once? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess in either way the eventual results are the same. But yeah doing a pre-check could be faster (avoid the overhead of undo_stack.emplace(p, r)).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here my concern is not about performance. If the same pair is committed more than once, I think there is something odd about the matching algorithm.
Later I'll try to improve the matching algorithm implementation. In particular, I want to remove try_match loop on parents,
tvm/src/relax/ir/dataflow_matcher.cc
Lines 577 to 607 in 278bc9b
| for (auto& [pparent, constraints] : p->parents) { | |
| bool any_cons_sat = false; | |
| for (auto& rparent : r->parents) { | |
| // skip if mismatch. | |
| if (rparent->matched && rparent->matched != pparent->ptr) continue; | |
| const auto& uses = def2use.at(rparent->ptr); | |
| // check edge constraints. | |
| bool cons_sat = true; | |
| for (const auto& cons : constraints) { | |
| if (cons.type == PairCons::kOnlyUsedBy && uses.size() != 1) { | |
| cons_sat = false; | |
| break; | |
| } | |
| if (cons.index != -1) { | |
| const auto& callees = use2def.at(r->ptr); | |
| if (callees.size() <= static_cast<size_t>(cons.index) || | |
| callees[cons.index] != rparent->ptr) { | |
| cons_sat = false; | |
| break; | |
| } | |
| } | |
| } | |
| if (!cons_sat) continue; | |
| any_cons_sat = true; | |
| // try all parent R nodes that are not matched yet. | |
| // as long as ppattern can match one node. | |
| if (!pparent->matched && try_match(pparent, rparent, m, def2use, use2def)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Because the constraint or relation between nodes may or may not be single-way so in the beginning I made it bidirectional such that the pattern can be matched as long as you can let any node of the matched subgraph be start hint.
For example, for A->B pattern, you can start matching from either A or B (forward or backward).
Specifying certain matching order definitely makes the code logic and debugging easier but I am afraid it also cuts the flexibility in some way. Not sure if it is worth to have some flexibility or if we can keep them both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have an example in test_dataflow_pattern.py that requires such flexibility? Similarly to how I don't understand the need for start_hint, I don't understand why we might want to start matching from B in A -> B.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also going to look at how similar projects like MLIR, OpenVINO etc implements general graph pattern matching.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I need some time to illustrate but my flight is taking off now. I will get you back probably after 8 hours. 🥲
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok :) I'll share my thought in the meantime.
Looking at an example in https://github.com/apache/tvm/blob/unity/tests/python/relax/test_dataflow_pattern.py#L540-L545, I cannot imagine how one would use start_hint in practice. Here, you wrote the input mod by hand, so you know dfb.bindings[0].var is associated to the "left" branch of the CBRx2. But in general, we don't have such information, especially in e2e scenarios for real world models.
Even if there was a good use case for it, I claim that it doesn't justify making API and the implementation more complicated (two additional params + try_match loop on parants). If one has such advanced knowledge of model structure and variables, they may as well have a different way to match & extract such subgraph.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for bringing it up. When designing the usages it was my bad that I did not know enough use cases and I made it "more capable" by compromising simplicity. masa you have more professional experience with how patterns look in practice so I strongly agree that we should cut off the implementation/complexity for such long-tail use cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, also to explain why I was using the code style of CONSTANT logic_op VAR. This is just a practice to avoid writing VAR = CONSTANT which causes silent logic errors. But yeah it is nice to normalize the code style.
|
Just hit an interesting bug in the graph matcher. The following fake QKV projection mod and pattern should not match. Without this PR, I get segfault. With this PR, it incorrectly matches with the result I'll work on a fix in a separate PR. UPDATE: Found a bug in how tvm/src/relax/ir/dataflow_matcher.cc Lines 565 to 571 in 278bc9b
undo_stacks for parent and child nodes that have matched tentatively also need to be cleaned up as well.
So the fix is to return |
…#14417) * Remove all non-determinsm from graph matching * add test * typo * try fixing compile error for gcc * more style update * suppress compile warning
…#14417) * Remove all non-determinsm from graph matching * add test * typo * try fixing compile error for gcc * more style update * suppress compile warning
…#14417) * Remove all non-determinsm from graph matching * add test * typo * try fixing compile error for gcc * more style update * suppress compile warning
Graph pattern matching uses
std::mapandstd::setkeyed on pointers, which leads to non-deterministic matching result. This PR removed all non-determinism.For example, consider matching against QKV projection before attention. Given a sequence of bindings,
and patterns,
intuitively I expect the following matching result:
But since three matmul patterns are independent of each other, each pattern can match any of three matmuls in the bindings. And since graph pattern matching processes patterns and variables in the bindings in a non-deterministic order, matching result becomes non deterministic as well.
To make graph pattern matching deterministic, I introduced an implicit ordering constraint on the patterns: Patterns are matched in the order that they are declared in the pattern context. So in the example above, we make sure to start pattern matching from the first matmul pattern. Likewise, the variables in the bindings, which are the candidates for matching, are also considered for matching in the order they appear in the bindings. So
lv0is matched first, followed bylv1etc.@ganler @sunggg