From 11f94cdce9dc00d6ea47b7e61871858443d15e89 Mon Sep 17 00:00:00 2001 From: liym27 <33742067+liym27@users.noreply.github.com> Date: Tue, 24 Mar 2020 18:48:37 +0800 Subject: [PATCH] Improve function is_control_flow_to_transform. test=develop (#23109) --- python/paddle/fluid/dygraph/dygraph_to_static/utils.py | 6 ++++-- .../fluid/tests/unittests/dygraph_to_static/test_list.py | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/utils.py b/python/paddle/fluid/dygraph/dygraph_to_static/utils.py index c0ae00e801..581e727a5b 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/utils.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/utils.py @@ -93,8 +93,10 @@ def is_control_flow_to_transform(node, var_name_to_type): "The type of input node must be gast.AST, but received %s." % type(node) if isinstance(node, gast.If): - # TODO: make a better condition - return True + from .ifelse_transformer import IfConditionVisitor + if_visitor = IfConditionVisitor( + node.test, node_var_type_map=var_name_to_type) + return if_visitor.is_control_flow() if isinstance(node, gast.For): # TODO: make a better condition diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_list.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_list.py index 129e7b3c56..b6461c9b2e 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_list.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_list.py @@ -27,7 +27,9 @@ def test_list_without_control_flow(x): # Python list will not be transformed. x = fluid.dygraph.to_variable(x) a = [] - a.append(x) + # It's a plain python control flow which won't be transformed + if 2 > 1: + a.append(x) return a -- GitLab