-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
[CINN] Add symbolic select #63971
Merged
Merged
[CINN] Add symbolic select #63971
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3fdc961
fix where op infer meta check
phlrain 5e76350
convert where to cinn select
phlrain fe0bb4a
convert where to cinn select
phlrain 5e1ad8a
[CINN] add symbolic select for where
hxzd5568 1e9a628
Merge branch 'develop' into select
hxzd5568 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,71 @@ | ||
# 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 numpy as np | ||
|
||
import paddle | ||
|
||
|
||
class WhereCase(paddle.nn.Layer): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def forward( | ||
self, | ||
cond, | ||
true_branch, | ||
false_branch, | ||
): | ||
return paddle.tensor.where(cond, true_branch.sin(), false_branch) | ||
|
||
|
||
class TestWhere(unittest.TestCase): | ||
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. 这里建议继承 base.py 里的TestBase,只重写 init函数逻辑即可。 |
||
def setUp(self): | ||
self.inputs = ( | ||
paddle.rand(shape=[16, 16], dtype=paddle.float32).cast("bool"), | ||
paddle.rand(shape=[16, 16], dtype=paddle.float32), | ||
paddle.rand(shape=[16, 16], dtype=paddle.float32), | ||
) | ||
|
||
def train(self, net, to_static, with_prim=False, with_cinn=False): | ||
if to_static: | ||
paddle.set_flags({'FLAGS_prim_all': with_prim}) | ||
if with_cinn: | ||
build_strategy = paddle.static.BuildStrategy() | ||
build_strategy.build_cinn_pass = True | ||
net = paddle.jit.to_static( | ||
net, build_strategy=build_strategy, full_graph=True | ||
) | ||
else: | ||
net = paddle.jit.to_static(net, full_graph=True) | ||
paddle.seed(123) | ||
outs = net(*self.inputs) | ||
return outs | ||
|
||
def test_where(self): | ||
net = WhereCase() | ||
st_out = self.train(net, to_static=True) | ||
cinn_out = self.train( | ||
net, to_static=True, with_prim=True, with_cinn=True | ||
) | ||
for st, cinn in zip( | ||
paddle.utils.flatten(st_out), paddle.utils.flatten(cinn_out) | ||
): | ||
np.testing.assert_allclose(st.numpy(), cinn.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.
这里对于cond_dim、x_dim、y_dim的比较逻辑看起来是一致的,可以考虑使用lambda函数服用逻辑