-
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
[Dy2Stat]Allow ifelse return buildin type in paddle cond #37888
Merged
Aurelius84
merged 5 commits into
PaddlePaddle:develop
from
0x45f:dy2stat_ifelse_ret_int
Dec 13, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -102,6 +102,41 @@ def select_input(inputs, mask): | |
return out | ||
|
||
|
||
def select_input_with_buildin_type(inputs, mask): | ||
from paddle.fluid.dygraph.dygraph_to_static.variable_trans_func import to_static_variable | ||
support_ret_buildin_type = (bool, float, six.integer_types) | ||
false_var, true_var = inputs | ||
|
||
if isinstance(false_var, Variable) and isinstance(true_var, Variable): | ||
return select_input(inputs, mask) | ||
|
||
elif (isinstance(false_var, (support_ret_buildin_type)) and | ||
isinstance(false_var, type(true_var))): | ||
if false_var == true_var: | ||
return false_var | ||
else: | ||
inputs = [ | ||
to_static_variable(false_var), to_static_variable(true_var) | ||
] | ||
# Deal with the situations like this: false_var is int and true_var is Variable | ||
elif ((isinstance(false_var, support_ret_buildin_type) and | ||
isinstance(true_var, Variable)) or | ||
(isinstance(true_var, support_ret_buildin_type) and | ||
isinstance(false_var, Variable))): | ||
inputs = [to_static_variable(false_var), to_static_variable(true_var)] | ||
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. 这里的处理逻辑是:
|
||
warnings.warn( | ||
"Return results from different branches in cond are not same type: " | ||
"false_var returned by fasle_fn is '{}' and true_var of true_fn is " | ||
"'{}'".format(type(false_var), type(true_var))) | ||
else: | ||
raise TypeError( | ||
"Unsupported return type of true_fn and false_fn in cond: false_var " | ||
"returned by fasle_fn is '{}' and true_var of true_fn is '{}'". | ||
format(type(false_var), type(true_var))) | ||
|
||
return select_input(inputs, mask) | ||
|
||
|
||
def split_lod_tensor(input, mask, level=0): | ||
""" | ||
This function takes in an input that contains the complete lod information, | ||
|
@@ -2282,8 +2317,8 @@ def append_conditional_block_grad(self, parent_block, inside_block, | |
|
||
|
||
def copy_var_to_parent_block(var, layer_helper): | ||
if var is None: | ||
return None | ||
if not isinstance(var, Variable): | ||
return var | ||
prog = layer_helper.main_program | ||
parent_idx = prog.current_block().parent_idx | ||
assert parent_idx >= 0, "Got wrong parent block index when assigning var to parent scope in control_flow" | ||
|
@@ -2466,7 +2501,7 @@ def false_func(): | |
format(e)) | ||
|
||
mask = cast(pred, dtype='int32') | ||
merge_func = lambda false_var, true_var : select_input([false_var, true_var], mask) | ||
merge_func = lambda false_var, true_var : select_input_with_buildin_type([false_var, true_var], mask) | ||
merged_output = map_structure(merge_func, false_output, true_output) | ||
return merged_output | ||
|
||
|
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.
import 放在这里是因为有循环引用么?