-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[CINN] Support injective group fusion #61197
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ad067f1
support injective group fusion
zyfncg 1c9379c
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg ffee527
[CINN+PIR]Fix IsSupportCINN Logic
Aurelius84 ce45ad3
fix comment
Aurelius84 8f878a1
Merge commit 'refs/pull/61225/head' of https://github.com/PaddlePaddl…
zyfncg 6a14bf7
merge gather_nd
zyfncg 7c772ef
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 51c809b
refactor some trick code
zyfncg 2889e53
revert some code
zyfncg fb05023
fix bug
zyfncg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -302,7 +302,11 @@ inline bool horizontal_or_can_inline( | |
| return false; | ||
| } | ||
| } | ||
|
|
||
| // vertical relation: 1.can compute inline | ||
| if (producer->result(0).use_count() == 1) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这是能融合slice/concat的tricky代码吗?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 是的,这里的判断不算很tricky,如果融合规则明确的话,后面是可以直接使用的 |
||
| return true; | ||
| } | ||
| // if (helper->GetNodeData(producer)->outlinks().size() == 1 && | ||
| // helper->output_ops_set_.count(producer) == 0) { | ||
| // return true; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import unittest | ||
|
|
||
| import paddle | ||
| from paddle import nn | ||
|
|
||
|
|
||
| def apply_to_static(net, use_cinn, input_spec=None): | ||
| build_strategy = paddle.static.BuildStrategy() | ||
| build_strategy.build_cinn_pass = use_cinn | ||
| return paddle.jit.to_static( | ||
| net, | ||
| input_spec=input_spec, | ||
| build_strategy=build_strategy, | ||
| full_graph=True, | ||
| ) | ||
|
|
||
|
|
||
| class RotaryPosEmb(nn.Layer): | ||
| def __init__(self): | ||
| super().__init__() | ||
|
|
||
| def forward(self, q, k, cos, sin, position_ids): | ||
| cos = cos.squeeze(axis=[0, 2]) # [seq_len, dim] | ||
| sin = sin.squeeze(axis=[0, 2]) # [seq_len, dim] | ||
|
|
||
| cos = cos[position_ids].unsqueeze(2) # [bs, seq_len, 1, dim] | ||
| sin = sin[position_ids].unsqueeze(2) # [bs, seq_len, 1, dim] | ||
| q_embed = (q * cos) + (self.rotate_half(q) * sin) | ||
| k_embed = (k * cos) + (self.rotate_half(k) * sin) | ||
| return q_embed, k_embed | ||
|
|
||
| def rotate_half(self, x): | ||
| """Rotates half the hidden dims of the input.""" | ||
| x1 = x[..., : x.shape[-1] // 2] | ||
| x2 = x[..., x.shape[-1] // 2 :] | ||
| return paddle.concat([-x2, x1], axis=-1) # shape is the same as x | ||
|
|
||
|
|
||
| class TestRotaryPosEmb(unittest.TestCase): | ||
| def setUp(self): | ||
| paddle.seed(2022) | ||
| self.prepare_data() | ||
|
|
||
| def prepare_data(self): | ||
| self.q = paddle.randn([1, 2048, 8, 96], dtype="float32") | ||
| self.q.stop_gradient = False | ||
|
|
||
| self.k = paddle.randn([1, 2048, 8, 96], dtype="float32") | ||
| self.k.stop_gradient = False | ||
|
|
||
| self.cos = paddle.randn([1, 2048, 1, 96], dtype="float32") | ||
| self.cos.stop_gradient = False | ||
|
|
||
| self.sin = paddle.randn([1, 2048, 1, 96], dtype="float32") | ||
| self.sin.stop_gradient = False | ||
|
|
||
| self.position_ids = paddle.arange(end=2048, dtype="int64").unsqueeze(0) | ||
| self.position_ids.stop_gradient = False | ||
|
|
||
| def eval(self, use_cinn): | ||
| paddle.seed(2022) | ||
| net = RotaryPosEmb() | ||
| net.eval() | ||
| if use_cinn: | ||
| net = apply_to_static(net, use_cinn) | ||
|
|
||
| out = net(self.q, self.k, self.cos, self.sin, self.position_ids) | ||
| return out | ||
|
|
||
| def test_eval(self): | ||
| cinn_outs = self.eval(use_cinn=True) | ||
| # dy_outs = self.eval(use_cinn=False) | ||
|
|
||
| # TODO(phlrain): Need to check result | ||
| # for cinn_out, dy_out in zip(cinn_outs, dy_outs): | ||
| # np.testing.assert_allclose( | ||
| # cinn_out.numpy(), dy_out.numpy(), atol=1e-8 | ||
| # ) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
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.
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.
VLOG mismatch?
Uh oh!
There was an error while loading. Please reload this page.
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.
just for debug, will change back before merging