-
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
[Dy2static] FunctionScopeVisitor Enhance and substitute the original NameVisitor in If #43967
Merged
2742195759
merged 18 commits into
PaddlePaddle:develop
from
2742195759:dy2static-create-variable-transform
Jul 6, 2022
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1a76531
add support for control flow block analysis
2742195759 ae2cbf2
move FunctionNameLivenessAnalysis into utils
2742195759 55dd278
pass test_ifelse.py
2742195759 945de74
merge
2742195759 0444670
remove duplicate data_layer_not_check
2742195759 0b191eb
pass the test_ifelse.py
2742195759 8dd1a4b
fix unittest error .
2742195759 d512394
fix all ci error in first version
2742195759 056f7b8
merge
2742195759 3d22432
temporay disable CreateVariableTransformer
2742195759 624f35f
fix ci errors
2742195759 17fe717
fix function name liveness analysis bugs
2742195759 0be1e75
merge
2742195759 18b3125
modifty def cond
2742195759 e2138f7
fix
2742195759 15fb43a
fix ci error - v2
2742195759 bb6f175
fix by code review
2742195759 14f45b2
change return_name_ids -> return_name
2742195759 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
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 |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import re | ||
|
||
from paddle.fluid.data_feeder import convert_dtype | ||
from paddle.fluid.dygraph.dygraph_to_static.variable_trans_func import to_static_variable | ||
from paddle.fluid.framework import core, Variable | ||
|
@@ -21,7 +23,7 @@ | |
from paddle.fluid.layers import cast, control_flow, logical_and, logical_not, logical_or, nn | ||
from paddle.fluid.layers.control_flow import cond, while_loop, less_than, increment | ||
from paddle.fluid.dygraph.dygraph_to_static.return_transformer import RETURN_NO_VALUE_VAR_NAME | ||
from paddle.fluid.dygraph.dygraph_to_static.utils import UndefinedVar | ||
from paddle.fluid.dygraph.dygraph_to_static.utils import UndefinedVar, Dygraph2StaticException | ||
|
||
|
||
def convert_while_loop(cond, body, getter, setter): | ||
|
@@ -41,11 +43,9 @@ def convert_while_loop(cond, body, getter, setter): | |
# If loop_vars is changed during cond callable, then it causes bug, but current logical_and/logical_not/... doesn't change the loop_vars. | ||
pred = cond() | ||
if isinstance(pred, Variable): | ||
loop_vars = _run_paddle_while(cond, body, getter, setter) | ||
_run_paddle_while(cond, body, getter, setter) | ||
else: | ||
loop_vars = _run_py_while(cond, body, getter, setter) | ||
|
||
return loop_vars | ||
_run_py_while(cond, body, getter, setter) | ||
|
||
|
||
def _run_paddle_while(cond, body, getter, setter): | ||
|
@@ -61,10 +61,13 @@ def _run_paddle_while(cond, body, getter, setter): | |
|
||
|
||
def _run_py_while(cond, body, getter, setter): | ||
loop_vars = getter() | ||
while cond(): | ||
loop_vars = body() | ||
return loop_vars | ||
while True: | ||
pred = cond() | ||
if isinstance(pred, Variable): | ||
raise Dygraph2StaticException( | ||
"python while pred change from bool to variable.") | ||
if not pred: break | ||
body() | ||
|
||
|
||
def convert_logical_and(x_func, y_func): | ||
|
@@ -231,17 +234,32 @@ def _run_paddle_cond(pred, true_fn, false_fn, get_args, set_args, | |
|
||
def new_true_fn(): | ||
set_args(init_args) | ||
outs = true_fn() | ||
_check_no_undefined_var(outs, return_name_ids, 'if_body') | ||
return outs | ||
ret = true_fn() | ||
# IfExpr will return a non-None return value, so we just return ret. | ||
# We assume normal return has no return value. | ||
if ret is None: return get_args() | ||
else: return ret | ||
|
||
def new_false_fn(): | ||
set_args(init_args) | ||
outs = false_fn() | ||
_check_no_undefined_var(outs, return_name_ids, 'else_body') | ||
return outs | ||
|
||
cond_outs = control_flow.cond(pred, new_true_fn, new_false_fn) | ||
ret = false_fn() | ||
if ret is None: return get_args() | ||
else: return ret | ||
|
||
try: | ||
cond_outs = control_flow.cond(pred, new_true_fn, new_false_fn, None, | ||
return_name_ids) | ||
except Exception as e: | ||
if re.search("Unsupported return type of true_fn and false_fn in cond", | ||
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. 这里最好加个单测确保这行代码按照预期触发,因为报错信息有可能被别人迭代优化后,这个分支就失效了 |
||
str(e)): | ||
raise Dygraph2StaticException( | ||
"Your if/else have different return type. TODO: add link to modifty. {}" | ||
.format(str(e))) | ||
if re.search("Incompatible return values of", str(e)): | ||
raise Dygraph2StaticException( | ||
"Your if/else have different number of return value. TODO: add link to modifty. {}" | ||
.format(str(e))) | ||
raise e | ||
return _recover_args_state(cond_outs, get_args, set_args, return_name_ids) | ||
|
||
|
||
|
@@ -251,8 +269,7 @@ def _run_py_ifelse(pred, true_fn, false_fn, get_args, set_args, | |
Evaluate python original branch function if-else. | ||
""" | ||
py_outs = true_fn() if pred else false_fn() | ||
py_outs = _remove_no_value_return_var(py_outs) | ||
return _recover_args_state(py_outs, get_args, set_args, return_name_ids) | ||
return py_outs | ||
|
||
|
||
def _remove_no_value_return_var(out): | ||
|
@@ -317,9 +334,10 @@ def _recover_args_state(outs, get_args, set_args, return_name_ids): | |
assert num_outs <= num_args | ||
|
||
if num_args == 1: | ||
final_outs = (outs, ) | ||
final_outs = (outs, ) if not isinstance(outs, | ||
(list, tuple)) else tuple(outs) | ||
else: | ||
outs = (outs, ) if num_outs == 1 else outs | ||
outs = (outs, ) if num_outs == 1 else tuple(outs) | ||
final_outs = outs + init_args[num_outs:] | ||
|
||
set_args(final_outs) | ||
|
Oops, something went wrong.
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.
raise这个error时会触发用户源码行的标记么?
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.
这个会触发源码行的标记: