-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[Dy2St]Refine ifelse early return #43328
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6ff753c
Refine ifelse early return
0x45f 9947e97
Add process While and For
0x45f e398d88
Fix test_program_translator.py
0x45f 5483e93
Handle `if/elif` case
0x45f e771908
Add early return UT
0x45f 2ec7ef5
Format code
0x45f 44e8d70
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
0x45f af1c8e8
Handle if/elif/elif case
0x45f 9787b84
Add if/elif UT
0x45f 408cc8e
Polish UT
0x45f 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 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
88 changes: 88 additions & 0 deletions
88
python/paddle/fluid/dygraph/dygraph_to_static/early_return_transformer.py
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,88 @@ | ||
# Copyright (c) 2020 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. | ||
|
||
from __future__ import print_function | ||
|
||
from paddle.utils import gast | ||
from paddle.fluid.dygraph.dygraph_to_static.static_analysis import AstNodeWrapper | ||
|
||
|
||
class EarlyReturnTransformer(gast.NodeTransformer): | ||
""" | ||
Transform if/else return statement of Dygraph into Static Graph. | ||
""" | ||
|
||
def __init__(self, wrapper_root): | ||
assert isinstance( | ||
wrapper_root, AstNodeWrapper | ||
), "Type of input node should be AstNodeWrapper, but received %s ." % type( | ||
wrapper_root) | ||
self.root = wrapper_root.node | ||
|
||
def transform(self): | ||
""" | ||
Main function to transform AST. | ||
""" | ||
self.visit(self.root) | ||
|
||
def is_define_return_in_if(self, node): | ||
assert isinstance( | ||
node, gast.If | ||
), "Type of input node should be gast.If, but received %s ." % type( | ||
node) | ||
for child in node.body: | ||
if isinstance(child, gast.Return): | ||
return True | ||
return False | ||
|
||
def visit_block_nodes(self, nodes): | ||
result_nodes = [] | ||
destination_nodes = result_nodes | ||
for node in nodes: | ||
rewritten_node = self.visit(node) | ||
|
||
if isinstance(rewritten_node, (list, tuple)): | ||
destination_nodes.extend(rewritten_node) | ||
else: | ||
destination_nodes.append(rewritten_node) | ||
|
||
# append other nodes to if.orelse even though if.orelse is not empty | ||
if isinstance(node, gast.If) and self.is_define_return_in_if(node): | ||
destination_nodes = node.orelse | ||
# handle stmt like `if/elif/elif` | ||
while len(destination_nodes) > 0 and \ | ||
isinstance(destination_nodes[0], gast.If) and \ | ||
self.is_define_return_in_if(destination_nodes[0]): | ||
destination_nodes = destination_nodes[0].orelse | ||
|
||
return result_nodes | ||
|
||
def visit_If(self, node): | ||
node.body = self.visit_block_nodes(node.body) | ||
node.orelse = self.visit_block_nodes(node.orelse) | ||
return node | ||
|
||
def visit_While(self, node): | ||
node.body = self.visit_block_nodes(node.body) | ||
node.orelse = self.visit_block_nodes(node.orelse) | ||
return node | ||
|
||
def visit_For(self, node): | ||
node.body = self.visit_block_nodes(node.body) | ||
node.orelse = self.visit_block_nodes(node.orelse) | ||
return node | ||
|
||
def visit_FunctionDef(self, node): | ||
node.body = self.visit_block_nodes(node.body) | ||
return node |
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
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.
为什么不直接使用 destination_nodes = [] ? 另外,函数为什么不直接return destination_nodes? 是有什么特殊含义么?