diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/utils.py b/python/paddle/fluid/dygraph/dygraph_to_static/utils.py index c0ae00e80136515203df718319675903b2847db1..581e727a5b866bcaa7de5fe7ddfc0cad061d5f19 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 129e7b3c566ad67458b5496b588730ffb33106c9..b6461c9b2e361a729e68ca97f547d38e9ddae213 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