-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Dy2St]Refine ifelse early return (#43328)
* Refine ifelse early return
- Loading branch information
Showing
4 changed files
with
149 additions
and
44 deletions.
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