Skip to content
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] Support Various-Length Return Grammar in Dy2stat #25249

Merged
merged 14 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix unittest, test=develop
  • Loading branch information
zhhsplendid committed Jun 30, 2020
commit 70d511124940d501ff7c130769390a93712ad3a8
21 changes: 16 additions & 5 deletions python/paddle/fluid/dygraph/dygraph_to_static/partial_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def _restore_out(self, out_vars):
for i, idx in enumerate(self._outputs.var_ids):
flatten_outputs[idx] = out_vars[i]
outs = self._outputs.restore(flatten_outputs)
if len(outs) == 1:
if outs is not None and len(outs) == 1:
outs = outs[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks to fix this.


return outs
Expand All @@ -260,14 +260,25 @@ def _remove_no_value(self, out_vars):
if self._is_no_value(out_vars):
return None
return out_vars
else:
res = tuple(var for var in out_vars if not self._is_no_value(var))
if len(res) == 0:
elif isinstance(out_vars, (tuple, list)):
if isinstance(out_vars, tuple):
res = tuple(
var for var in out_vars if not self._is_no_value(var))
else:
# isinstance(out_vars, list)
res = [var for var in out_vars if not self._is_no_value(var)]

has_removed = (len(out_vars) > len(res))
# len(out_vars) > len(res) means we have removed var. This is
# preventing out_vars is empty or just one element at the beginning
if len(res) == 0 and has_removed:
return None
elif len(res) == 1:
elif len(res) == 1 and has_removed:
return res[0]
return res

return out_vars

def _set_grad_type(self, params):
# NOTE: if user set sparse gradient mode, the param's gradient
# will be SelectedRows, not LoDTensor. But tracer will just
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ def from_func_spec(func_spec):
# 3. Builds program only once and returns the output Variables.
with param_guard(func_spec.parameters(False)):
outputs = static_func(*inputs)
if not isinstance(outputs, (tuple, list)):
outputs = [outputs] if outputs else []
if not isinstance(outputs,
(tuple, list)) and outputs is not None:
outputs = [outputs]

return ConcreteProgram(
inputs=inputs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ def visit_FunctionDef(self, node):
annotation=None,
type_comment=None)
else:
# We need to initial return value as tuple because control flow
# requires input or output has same structure
return_value_nodes = gast.Tuple(
elts=[
gast.Name(
Expand Down