From cc2fc938041636fc0d63bd2f4e945f5540e7fbfc Mon Sep 17 00:00:00 2001 From: Huihuang Zheng Date: Tue, 29 Sep 2020 13:01:23 +0800 Subject: [PATCH] [Dy2stat] Refine Dy2stat APIs to 2.0rc (#27430) Refine Dy2stat APIs to 2.0rc After discussion, we accepted 3 key points from reviewers: 1. In 2.0rc we changed dygraph_to_static folder to dy2static 2. Keep the three files: convert_call_func.py, convert_operators.py, variable_trans_func.py 3. Remove convert_operators path when users import convert_xxx. After this PR, users can import convert_xxx APIs by: `import paddle.jit.dy2static.convert_xxx` The file structure will be: ``` jit dy2static convert_operators.py convert_func_call.py variable_trans_func.py ``` Detail changed API in files: In python/paddle/jit/dygraph_to_static/convert_call_func.py: from ...fluid.dygraph.dygraph_to_static.convert_call_func import convert_call #DEFINE_ALIAS In python/paddle/jit/dygraph_to_static/convert_operators.py: from ...fluid.dygraph.dygraph_to_static.convert_operators import cast_bool_if_necessary #DEFINE_ALIAS from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_assert #DEFINE_ALIAS from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_ifelse #DEFINE_ALIAS from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_len #DEFINE_ALIAS from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_logical_and #DEFINE_ALIAS from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_logical_not #DEFINE_ALIAS from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_logical_or #DEFINE_ALIAS from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_print #DEFINE_ALIAS from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_var_dtype #DEFINE_ALIAS from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_var_shape #DEFINE_ALIAS from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_while_loop #DEFINE_ALIAS In python/paddle/jit/dygraph_to_static/variable_trans_func.py: from ...fluid.dygraph.dygraph_to_static.variable_trans_func import create_fill_constant_node #DEFINE_ALIAS from ...fluid.dygraph.dygraph_to_static.variable_trans_func import create_static_variable_gast_node #DEFINE_ALIAS from ...fluid.dygraph.dygraph_to_static.variable_trans_func import data_layer_not_check #DEFINE_ALIAS from ...fluid.dygraph.dygraph_to_static.variable_trans_func import to_static_variable #DEFINE_ALIAS from ...fluid.dygraph.dygraph_to_static.variable_trans_func import to_static_variable_gast_node #DEFINE_ALIAS --- .../dygraph_to_static/assert_transformer.py | 3 +- .../dygraph_to_static/call_transformer.py | 3 +- .../dygraph_to_static/cast_transformer.py | 2 +- .../dygraph_to_static/ifelse_transformer.py | 8 +-- .../dygraph_to_static/list_transformer.py | 4 +- .../dygraph_to_static/logical_transformer.py | 4 +- .../dygraph_to_static/loop_transformer.py | 2 +- .../dygraph_to_static/print_transformer.py | 3 +- .../tensor_shape_transformer.py | 2 +- .../fluid/dygraph/dygraph_to_static/utils.py | 4 +- .../dygraph_to_static/variable_trans_func.py | 12 ++-- .../dygraph_to_static/ifelse_simple_func.py | 8 +-- .../dygraph_to_static/test_origin_info.py | 8 +-- .../test_program_translator.py | 66 ++++++++----------- .../dygraph_to_static/test_tensor_shape.py | 12 ++-- .../test_variable_trans_func.py | 10 +-- python/paddle/jit/__init__.py | 4 ++ python/paddle/jit/dy2static/__init__.py | 29 ++++++++ .../paddle/jit/dy2static/convert_call_func.py | 18 +++++ .../paddle/jit/dy2static/convert_operators.py | 33 ++++++++++ .../jit/dy2static/variable_trans_func.py | 26 ++++++++ python/setup.py.in | 1 + 22 files changed, 180 insertions(+), 82 deletions(-) create mode 100644 python/paddle/jit/dy2static/__init__.py create mode 100644 python/paddle/jit/dy2static/convert_call_func.py create mode 100644 python/paddle/jit/dy2static/convert_operators.py create mode 100644 python/paddle/jit/dy2static/variable_trans_func.py diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/assert_transformer.py b/python/paddle/fluid/dygraph/dygraph_to_static/assert_transformer.py index 73dba66d3fc..fe70fd1094f 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/assert_transformer.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/assert_transformer.py @@ -37,8 +37,7 @@ class AssertTransformer(gast.NodeTransformer): def visit_Assert(self, node): convert_assert_node = gast.parse( - 'fluid.dygraph.dygraph_to_static.convert_operators.convert_assert({test}, {msg})'. - format( + 'paddle.jit.dy2static.convert_assert({test}, {msg})'.format( test=ast_to_source_code(node.test), msg=ast_to_source_code(node.msg) if node.msg else "")).body[0].value diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/call_transformer.py b/python/paddle/fluid/dygraph/dygraph_to_static/call_transformer.py index 7fc72d42759..c2481d16825 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/call_transformer.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/call_transformer.py @@ -70,8 +70,7 @@ class CallTransformer(gast.NodeTransformer): if PDB_SET in func_str: return node - new_func_str = "fluid.dygraph.dygraph_to_static.convert_call({})".format( - func_str) + new_func_str = "paddle.jit.dy2static.convert_call({})".format(func_str) new_func_ast = gast.parse(new_func_str).body[0].value node.func = new_func_ast diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/cast_transformer.py b/python/paddle/fluid/dygraph/dygraph_to_static/cast_transformer.py index 71cb999eab0..1171b5dbdfa 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/cast_transformer.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/cast_transformer.py @@ -39,7 +39,7 @@ class CastTransformer(gast.NodeTransformer): func_str = ast_to_source_code(node.func).strip() if func_str in self._castable_type and len(node.args) > 0: args_str = ast_to_source_code(node.args[0]).strip() - new_func_str = "fluid.dygraph.dygraph_to_static.convert_operators.convert_var_dtype({}, '{}')".format( + new_func_str = "paddle.jit.dy2static.convert_var_dtype({}, '{}')".format( args_str, func_str) new_node = gast.parse(new_func_str).body[0].value return new_node diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/ifelse_transformer.py b/python/paddle/fluid/dygraph/dygraph_to_static/ifelse_transformer.py index 5b8e6d2a9bd..9c338546e23 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/ifelse_transformer.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/ifelse_transformer.py @@ -310,8 +310,8 @@ def parse_cond_return(parent_vars_dict, if_vars_dict, else_vars_dict, After transformed, q and z are created in parent scope. For example, x, y = 5, 10 - q = fluid.dygraph.dygraph_to_static.variable_trans_func.data_layer_not_check(name='q', shape=[-1], dtype='float32') - z = fluid.dygraph.dygraph_to_static.variable_trans_func.data_layer_not_check(name='z', shape=[-1], dtype='float32') + q = paddle.jit.dy2static.data_layer_not_check(name='q', shape=[-1], dtype='float32') + z = paddle.jit.dy2static.data_layer_not_check(name='z', shape=[-1], dtype='float32') def true_func(x, y, q): x = x+1 @@ -460,7 +460,7 @@ def create_convert_ifelse_node(return_name_ids, false_func, is_if_expr=False): """ - Create `fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse( + Create `paddle.jit.dy2static.convert_ifelse( pred, true_fn, false_fn, true_args, false_args, return_vars)` to replace original `python if/else` statement. """ @@ -491,7 +491,7 @@ def create_convert_ifelse_node(return_name_ids, return_vars = create_name_nodes(return_name_ids) convert_ifelse_layer = gast.parse( - 'fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse(' + 'paddle.jit.dy2static.convert_ifelse(' '{pred}, {true_fn}, {false_fn}, {true_args}, {false_args}, {return_vars})'. format( pred=ast_to_source_code(pred), diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/list_transformer.py b/python/paddle/fluid/dygraph/dygraph_to_static/list_transformer.py index 03b0de99071..9819f5fb72b 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/list_transformer.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/list_transformer.py @@ -188,8 +188,8 @@ class ListTransformer(gast.NodeTransformer): pass elif isinstance(slice_node, gast.Index): value_code = ast_to_source_code(node.value) - i = "fluid.layers.cast(" \ - "x=fluid.dygraph.dygraph_to_static.variable_trans_func.to_static_variable({})," \ + i = "paddle.cast(" \ + "x=paddle.jit.dy2static.to_static_variable({})," \ "dtype='int64')".format(ast_to_source_code(slice_node)) assign_code = "{} = fluid.layers.array_write(x={}, i={}, array={})" \ .format(target_name, value_code, i, target_name) diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/logical_transformer.py b/python/paddle/fluid/dygraph/dygraph_to_static/logical_transformer.py index d7af786ad75..8f3690f26fc 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/logical_transformer.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/logical_transformer.py @@ -34,7 +34,7 @@ class LogicalTransformer(gast.NodeTransformer): self.generic_visit(node) if isinstance(node.op, gast.Not): arg = ast_to_source_code(node.operand) - new_node_str = "fluid.dygraph.dygraph_to_static.convert_operators.convert_logical_not({})".format( + new_node_str = "paddle.jit.dy2static.convert_logical_not({})".format( arg) # NOTE: gast.parse returns Module(body=[expr(value=...)]) new_node = gast.parse(new_node_str).body[0].value @@ -67,7 +67,7 @@ class LogicalTransformer(gast.NodeTransformer): nodes = [pre_logic_node] + [post_logic_node] args = [ast_to_source_code(child) for child in nodes] - new_node_str = "fluid.dygraph.dygraph_to_static.convert_operators.convert_logical_{}(x={}, y={})".format( + new_node_str = "paddle.jit.dy2static.convert_logical_{}(x={}, y={})".format( api_type, args[0], args[1]) # NOTE: gast.parse return Module(body=[expr(...)]) new_node = gast.parse(new_node_str).body[0].value diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/loop_transformer.py b/python/paddle/fluid/dygraph/dygraph_to_static/loop_transformer.py index c66778992c2..0b6c7c45b38 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/loop_transformer.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/loop_transformer.py @@ -46,7 +46,7 @@ def create_while_node(condition_name, body_name, loop_var_names): # For example: loop_var_names = [a, b, foo.x], the type of `a` or `b` is gast.Name, # but the type of `foo.x` gast.Attribute. - while_func_name = "fluid.dygraph.dygraph_to_static.convert_operators.convert_while_loop" + while_func_name = "paddle.jit.dy2static.convert_while_loop" while_node_str = "[{}] = {}({}, {}, [{}])".format( ",".join(loop_var_names), while_func_name, condition_name, body_name, ",".join(loop_var_names)) diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/print_transformer.py b/python/paddle/fluid/dygraph/dygraph_to_static/print_transformer.py index efde2481721..9d1ec35764b 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/print_transformer.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/print_transformer.py @@ -51,6 +51,5 @@ class PrintTransformer(gast.NodeTransformer): def _create_print_node(self, print_args): convert_print_func = gast.parse( - 'fluid.dygraph.dygraph_to_static.convert_operators.convert_print' - ).body[0].value + 'paddle.jit.dy2static.convert_print').body[0].value return gast.Call(func=convert_print_func, args=print_args, keywords=[]) diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/tensor_shape_transformer.py b/python/paddle/fluid/dygraph/dygraph_to_static/tensor_shape_transformer.py index cad70f64c46..6cdf2799624 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/tensor_shape_transformer.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/tensor_shape_transformer.py @@ -26,7 +26,7 @@ from paddle.fluid.dygraph.dygraph_to_static.static_analysis import StaticAnalysi def create_convert_shape_node(var_shape_node): assert isinstance(var_shape_node, (gast.Attribute, gast.Subscript)) - convert_var_shape_func = "fluid.dygraph.dygraph_to_static.convert_operators.convert_var_shape" + convert_var_shape_func = "paddle.jit.dy2static.convert_var_shape" if isinstance(var_shape_node, gast.Attribute): api_shape_node = gast.Call( diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/utils.py b/python/paddle/fluid/dygraph/dygraph_to_static/utils.py index 86593dc24aa..7a234580712 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/utils.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/utils.py @@ -427,7 +427,7 @@ def ast_to_func(ast_root, dyfunc, delete_on_exit=True): os.remove(filepath) source = ast_to_source_code(ast_root) - import_fluid = "import paddle.fluid as fluid\n" + import_fluid = "import paddle\nimport paddle.fluid as fluid\n" source = import_fluid + source if six.PY2: @@ -922,7 +922,7 @@ class ForNodeVisitor(object): else: iter_var_name = ast_to_source_code(self.iter_node).strip() - convert_len_node_source_str = '{} = fluid.dygraph.dygraph_to_static.convert_operators.convert_len({})'.format( + convert_len_node_source_str = '{} = paddle.jit.dy2static.convert_len({})'.format( self.iter_var_len_name, iter_var_name) convert_len_node = gast.parse(convert_len_node_source_str).body[0] diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/variable_trans_func.py b/python/paddle/fluid/dygraph/dygraph_to_static/variable_trans_func.py index bc2851b630c..8da7b40db4c 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/variable_trans_func.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/variable_trans_func.py @@ -22,8 +22,8 @@ from paddle.fluid.layers import fill_constant from paddle.fluid.layer_helper import LayerHelper __all__ = [ - 'to_static_variable_gast_node', 'create_static_variable_gast_node', - 'data_layer_not_check' + 'create_fill_constant_node', 'create_static_variable_gast_node', + 'data_layer_not_check', 'to_static_variable', 'to_static_variable_gast_node' ] @@ -74,20 +74,20 @@ def data_layer_not_check(name, shape, dtype='float32', lod_level=0): def to_static_variable_gast_node(name): - func_code = "{} = fluid.dygraph.dygraph_to_static.variable_trans_func\ - .to_static_variable({})".format(name, name) + func_code = "{} = paddle.jit.dy2static.to_static_variable({})".format(name, + name) return gast.parse(func_code).body[0] def create_static_variable_gast_node(name): - func_code = "{} = fluid.dygraph.dygraph_to_static.variable_trans_func\ + func_code = "{} = paddle.jit.dy2static\ .data_layer_not_check(name='{}', shape=[-1], dtype='float32')".format( name, name) return gast.parse(func_code).body[0] def create_fill_constant_node(name, value): - func_code = "{} = fluid.layers.fill_constant(shape=[1], ".format(name) + func_code = "{} = paddle.fill_constant(shape=[1], ".format(name) if isinstance(value, bool): func_code += "dtype='bool', value={})".format(value) return gast.parse(func_code).body[0] diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/ifelse_simple_func.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/ifelse_simple_func.py index 08d832b64a2..34d7b59a9b4 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/ifelse_simple_func.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/ifelse_simple_func.py @@ -59,9 +59,9 @@ def dyfunc_with_if_else3(x): # The var is created only in one of If.body or If.orelse node, and it used as gast.Load firstly after gast.If node. # The transformed code: """ - q = fluid.dygraph.dygraph_to_static.variable_trans_func. + q = paddle.jit.dy2static. data_layer_not_check(name='q', shape=[-1], dtype='float32') - z = fluid.dygraph.dygraph_to_static.variable_trans_func. + z = paddle.jit.dy2static. data_layer_not_check(name='z', shape=[-1], dtype='float32') def true_fn_0(q, x, y): @@ -77,8 +77,8 @@ def dyfunc_with_if_else3(x): n = x + 3 return q, x, y, z q, x, y, z = fluid.layers.cond(fluid.layers.mean(x)[0] < 5, lambda : - fluid.dygraph.dygraph_to_static.convert_call(true_fn_0)(q, x, y), - lambda : fluid.dygraph.dygraph_to_static.convert_call(false_fn_0)(q, + paddle.jit.dy2static.convert_call(true_fn_0)(q, x, y), + lambda : paddle.jit.dy2static.convert_call(false_fn_0)(q, x, y)) """ y = x + 1 diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py index 3f77e9ade28..144b16873aa 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py @@ -65,7 +65,7 @@ class TestOriginInfo(unittest.TestCase): self.func = simple_func def set_static_lineno(self): - self.static_abs_lineno_list = [2, 3, 4] + self.static_abs_lineno_list = [3, 4, 5] def set_dygraph_info(self): self.line_num = 3 @@ -149,7 +149,7 @@ class TestOriginInfoWithNestedFunc(TestOriginInfo): self.func = nested_func def set_static_lineno(self): - self.static_abs_lineno_list = [2, 4, 5, 6, 7] + self.static_abs_lineno_list = [3, 5, 6, 7, 8] def set_dygraph_info(self): self.line_num = 5 @@ -174,7 +174,7 @@ class TestOriginInfoWithDecoratedFunc(TestOriginInfo): self.func = decorated_func def set_static_lineno(self): - self.static_abs_lineno_list = [2, 3] + self.static_abs_lineno_list = [3, 4] def set_dygraph_info(self): self.line_num = 2 @@ -208,7 +208,7 @@ class TestOriginInfoWithDecoratedFunc2(TestOriginInfo): self.func = decorated_func2 def set_static_lineno(self): - self.static_abs_lineno_list = [2, 3] + self.static_abs_lineno_list = [3, 4] def set_dygraph_info(self): self.line_num = 2 diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py index b0ab55758ee..b308854dc09 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py @@ -64,11 +64,9 @@ def get_source_code(func): class StaticCode1(): # TODO: Transform return statement def dyfunc_with_if_else(x_v, label=None): - __return_1 = fluid.layers.fill_constant( - shape=[1], dtype='bool', value=False) - __return_0 = fluid.layers.fill_constant( - shape=[1], dtype='bool', value=False) - __return_value_init_0 = fluid.layers.fill_constant( + __return_1 = paddle.fill_constant(shape=[1], dtype='bool', value=False) + __return_0 = paddle.fill_constant(shape=[1], dtype='bool', value=False) + __return_value_init_0 = paddle.fill_constant( shape=[1], dtype='float64', value=0.0) __return_value_0 = __return_value_init_0 @@ -80,13 +78,13 @@ class StaticCode1(): x_v = x_v + 1 return x_v - x_v = fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse( + x_v = paddle.jit.dy2static.convert_ifelse( fluid.layers.mean(x_v)[0] > 5, true_fn_0, false_fn_0, (x_v, ), (x_v, ), (x_v, )) def true_fn_1(__return_0, __return_value_0, label, x_v): loss = fluid.layers.cross_entropy(x_v, label) - __return_0 = fluid.layers.fill_constant( + __return_0 = paddle.fill_constant( shape=[1], dtype='bool', value=True) __return_value_0 = loss return __return_0, __return_value_0 @@ -94,14 +92,13 @@ class StaticCode1(): def false_fn_1(__return_0, __return_value_0): return __return_0, __return_value_0 - __return_0, __return_value_0 = ( - fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse( - label is not None, true_fn_1, false_fn_1, - (__return_0, __return_value_0, label, x_v), - (__return_0, __return_value_0), (__return_0, __return_value_0))) + __return_0, __return_value_0 = (paddle.jit.dy2static.convert_ifelse( + label is not None, true_fn_1, false_fn_1, + (__return_0, __return_value_0, label, x_v), + (__return_0, __return_value_0), (__return_0, __return_value_0))) def true_fn_2(__return_1, __return_value_0, x_v): - __return_1 = fluid.layers.fill_constant( + __return_1 = paddle.fill_constant( shape=[1], dtype='bool', value=True) __return_value_0 = x_v return __return_1, __return_value_0 @@ -109,23 +106,19 @@ class StaticCode1(): def false_fn_2(__return_1, __return_value_0): return __return_1, __return_value_0 - __return_1, __return_value_0 = ( - fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse( - fluid.dygraph.dygraph_to_static.convert_operators. - convert_logical_not(__return_0), true_fn_2, false_fn_2, - (__return_1, __return_value_0, x_v), - (__return_1, __return_value_0), (__return_1, __return_value_0))) + __return_1, __return_value_0 = (paddle.jit.dy2static.convert_ifelse( + paddle.jit.dy2static.convert_logical_not(__return_0), true_fn_2, + false_fn_2, (__return_1, __return_value_0, x_v), + (__return_1, __return_value_0), (__return_1, __return_value_0))) return __return_value_0 class StaticCode2(): # TODO: Transform return statement def dyfunc_with_if_else(x_v, label=None): - __return_3 = fluid.layers.fill_constant( - shape=[1], dtype='bool', value=False) - __return_2 = fluid.layers.fill_constant( - shape=[1], dtype='bool', value=False) - __return_value_init_1 = fluid.layers.fill_constant( + __return_3 = paddle.fill_constant(shape=[1], dtype='bool', value=False) + __return_2 = paddle.fill_constant(shape=[1], dtype='bool', value=False) + __return_value_init_1 = paddle.fill_constant( shape=[1], dtype='float64', value=0.0) __return_value_1 = __return_value_init_1 @@ -137,13 +130,13 @@ class StaticCode2(): x_v = x_v + 1 return x_v - x_v = fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse( + x_v = paddle.jit.dy2static.convert_ifelse( fluid.layers.mean(x_v)[0] > 5, true_fn_3, false_fn_3, (x_v, ), (x_v, ), (x_v, )) def true_fn_4(__return_2, __return_value_1, label, x_v): loss = fluid.layers.cross_entropy(x_v, label) - __return_2 = fluid.layers.fill_constant( + __return_2 = paddle.fill_constant( shape=[1], dtype='bool', value=True) __return_value_1 = loss return __return_2, __return_value_1 @@ -151,14 +144,13 @@ class StaticCode2(): def false_fn_4(__return_2, __return_value_1): return __return_2, __return_value_1 - __return_2, __return_value_1 = ( - fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse( - label is not None, true_fn_4, false_fn_4, - (__return_2, __return_value_1, label, x_v), - (__return_2, __return_value_1), (__return_2, __return_value_1))) + __return_2, __return_value_1 = (paddle.jit.dy2static.convert_ifelse( + label is not None, true_fn_4, false_fn_4, + (__return_2, __return_value_1, label, x_v), + (__return_2, __return_value_1), (__return_2, __return_value_1))) def true_fn_5(__return_3, __return_value_1, x_v): - __return_3 = fluid.layers.fill_constant( + __return_3 = paddle.fill_constant( shape=[1], dtype='bool', value=True) __return_value_1 = x_v return __return_3, __return_value_1 @@ -166,12 +158,10 @@ class StaticCode2(): def false_fn_5(__return_3, __return_value_1): return __return_3, __return_value_1 - __return_3, __return_value_1 = ( - fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse( - fluid.dygraph.dygraph_to_static.convert_operators. - convert_logical_not(__return_2), true_fn_5, false_fn_5, - (__return_3, __return_value_1, x_v), - (__return_3, __return_value_1), (__return_3, __return_value_1))) + __return_3, __return_value_1 = (paddle.jit.dy2static.convert_ifelse( + paddle.jit.dy2static.convert_logical_not(__return_2), true_fn_5, + false_fn_5, (__return_3, __return_value_1, x_v), + (__return_3, __return_value_1), (__return_3, __return_value_1))) return __return_value_1 diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py index 46d2b220414..de9554a2d4a 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_shape.py @@ -52,7 +52,7 @@ def dyfunc_tensor_shape_4(x): def dyfunc_tensor_shape_5(x): # `res = fluid.layers.reshape(x, shape=(-1, s))` to # `res = fluid.layers.reshape(x, shape=(-1, - # fluid.dygraph.dygraph_to_static.convert_operators.convert_var_shape(x)[0]))` + # paddle.jit.dy2static.convert_var_shape(x)[0]))` x = fluid.dygraph.to_variable(x) s = x.shape[0] res = fluid.layers.reshape(x, shape=(-1, s)) @@ -65,7 +65,7 @@ def dyfunc_with_if_1(x): x_shape_0 = x.shape[0] if x_shape_0 < 1: # `res.shape[0]` is transformed into - # `fluid.dygraph.dygraph_to_static.convert_operators.convert_var_shape(res)[0]` + # `paddle.jit.dy2static.convert_var_shape(res)[0]` if res.shape[0] > 1: res = fluid.layers.fill_constant( value=2, shape=x.shape, dtype="int32") @@ -89,7 +89,7 @@ def dyfunc_with_if_2(x): def dyfunc_with_for_1(x): x = fluid.dygraph.to_variable(x) res = fluid.layers.fill_constant(value=0, shape=[1], dtype="int32") - # `x.shape[0]` is transformed into `fluid.dygraph.dygraph_to_static.convert_operators.convert_var_shape(x)[0]` + # `x.shape[0]` is transformed into `paddle.jit.dy2static.convert_var_shape(x)[0]` for i in range(x.shape[0]): res += 1 return res @@ -100,7 +100,7 @@ def dyfunc_with_for_2(x): x_shape_0 = x.shape[0] res = fluid.layers.fill_constant(value=0, shape=[1], dtype="int32") - # `x_shape_0` is transformed into `fluid.dygraph.dygraph_to_static.convert_operators.convert_var_shape(x)[0]` + # `x_shape_0` is transformed into `paddle.jit.dy2static.convert_var_shape(x)[0]` for i in range(x_shape_0): res += 1 return res @@ -124,7 +124,7 @@ def dyfunc_with_for_3(x): def dyfunc_with_while_1(x): x = fluid.dygraph.to_variable(x) res = fluid.layers.fill_constant(value=0, shape=[1], dtype="int32") - # `x.shape[0]` is transformed into `fluid.dygraph.dygraph_to_static.convert_operators.convert_var_shape(x)[0]` + # `x.shape[0]` is transformed into `paddle.jit.dy2static.convert_var_shape(x)[0]` i = 1 while i < x.shape[0]: res += 1 @@ -137,7 +137,7 @@ def dyfunc_with_while_2(x): x_shape_0 = x.shape[0] res = fluid.layers.fill_constant(value=0, shape=[1], dtype="int32") i = 1 - # `x_shape_0` is transformed into `fluid.dygraph.dygraph_to_static.convert_operators.convert_var_shape(x)[0]` + # `x_shape_0` is transformed into `paddle.jit.dy2static.convert_var_shape(x)[0]` while i < x_shape_0: res += 1 i = i + 2 diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_variable_trans_func.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_variable_trans_func.py index e7632a0ba8d..e79209cb538 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_variable_trans_func.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_variable_trans_func.py @@ -51,24 +51,24 @@ class TestDataLayerNotCheck(unittest.TestCase): class TestVariableTransFunc(unittest.TestCase): def test_create_fill_constant_node(self): node = create_fill_constant_node("a", 1.0) - source = "a = fluid.layers.fill_constant(shape=[1], dtype='float64', value=1.0)" + source = "a = paddle.fill_constant(shape=[1], dtype='float64', value=1.0)" self.assertEqual(ast_to_source_code(node).strip(), source) node = create_fill_constant_node("b", True) - source = "b = fluid.layers.fill_constant(shape=[1], dtype='bool', value=True)" + source = "b = paddle.fill_constant(shape=[1], dtype='bool', value=True)" self.assertEqual(ast_to_source_code(node).strip(), source) if six.PY2: node = create_fill_constant_node("c", 214) - source = "c = fluid.layers.fill_constant(shape=[1], dtype='int32', value=214)" + source = "c = paddle.fill_constant(shape=[1], dtype='int32', value=214)" self.assertEqual(ast_to_source_code(node).strip(), source) node = create_fill_constant_node("d", long(10086)) - source = "d = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10086)" + source = "d = paddle.fill_constant(shape=[1], dtype='int64', value=10086)" self.assertEqual(ast_to_source_code(node).strip(), source) else: node = create_fill_constant_node("c", 4293) - source = "c = fluid.layers.fill_constant(shape=[1], dtype='int64', value=4293)" + source = "c = paddle.fill_constant(shape=[1], dtype='int64', value=4293)" self.assertEqual(ast_to_source_code(node).strip(), source) self.assertIsNone(create_fill_constant_node("e", None)) diff --git a/python/paddle/jit/__init__.py b/python/paddle/jit/__init__.py index d04a65ad6ea..9e40cb18a00 100644 --- a/python/paddle/jit/__init__.py +++ b/python/paddle/jit/__init__.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import print_function + from ..fluid.dygraph.jit import save #DEFINE_ALIAS from ..fluid.dygraph.jit import load #DEFINE_ALIAS from ..fluid.dygraph.jit import TracedLayer #DEFINE_ALIAS @@ -21,6 +23,8 @@ from ..fluid.dygraph.jit import declarative as to_static #DEFINE_ALIAS from ..fluid.dygraph import ProgramTranslator #DEFINE_ALIAS from ..fluid.dygraph.io import TranslatedLayer #DEFINE_ALIAS +from . import dy2static + __all__ = [ 'save', 'load', 'TracedLayer', 'to_static', 'ProgramTranslator', 'TranslatedLayer', 'set_code_level', 'set_verbosity' diff --git a/python/paddle/jit/dy2static/__init__.py b/python/paddle/jit/dy2static/__init__.py new file mode 100644 index 00000000000..239b554180b --- /dev/null +++ b/python/paddle/jit/dy2static/__init__.py @@ -0,0 +1,29 @@ +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import print_function + +from . import convert_operators +from .convert_operators import * + +from . import convert_call_func +from .convert_call_func import * + +from . import variable_trans_func +from .variable_trans_func import * + +__all__ = [] +__all__ += convert_operators.__all__ +__all__ += convert_call_func.__all__ +__all__ += variable_trans_func.__all__ diff --git a/python/paddle/jit/dy2static/convert_call_func.py b/python/paddle/jit/dy2static/convert_call_func.py new file mode 100644 index 00000000000..be2377608e3 --- /dev/null +++ b/python/paddle/jit/dy2static/convert_call_func.py @@ -0,0 +1,18 @@ +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from __future__ import print_function + +from ...fluid.dygraph.dygraph_to_static.convert_call_func import convert_call #DEFINE_ALIAS + +__all__ = ['convert_call'] diff --git a/python/paddle/jit/dy2static/convert_operators.py b/python/paddle/jit/dy2static/convert_operators.py new file mode 100644 index 00000000000..89df1d0aa77 --- /dev/null +++ b/python/paddle/jit/dy2static/convert_operators.py @@ -0,0 +1,33 @@ +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from __future__ import print_function + +from ...fluid.dygraph.dygraph_to_static.convert_operators import cast_bool_if_necessary #DEFINE_ALIAS +from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_assert #DEFINE_ALIAS +from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_ifelse #DEFINE_ALIAS +from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_len #DEFINE_ALIAS +from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_logical_and #DEFINE_ALIAS +from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_logical_not #DEFINE_ALIAS +from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_logical_or #DEFINE_ALIAS +from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_print #DEFINE_ALIAS +from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_var_dtype #DEFINE_ALIAS +from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_var_shape #DEFINE_ALIAS +from ...fluid.dygraph.dygraph_to_static.convert_operators import convert_while_loop #DEFINE_ALIAS + +__all__ = [ + 'cast_bool_if_necessary', 'convert_assert', 'convert_ifelse', 'convert_len', + 'convert_logical_and', 'convert_logical_not', 'convert_logical_or', + 'convert_print', 'convert_var_dtype', 'convert_var_shape', + 'convert_while_loop' +] diff --git a/python/paddle/jit/dy2static/variable_trans_func.py b/python/paddle/jit/dy2static/variable_trans_func.py new file mode 100644 index 00000000000..08c057934a5 --- /dev/null +++ b/python/paddle/jit/dy2static/variable_trans_func.py @@ -0,0 +1,26 @@ +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import print_function + +from ...fluid.dygraph.dygraph_to_static.variable_trans_func import create_fill_constant_node #DEFINE_ALIAS +from ...fluid.dygraph.dygraph_to_static.variable_trans_func import create_static_variable_gast_node #DEFINE_ALIAS +from ...fluid.dygraph.dygraph_to_static.variable_trans_func import data_layer_not_check #DEFINE_ALIAS +from ...fluid.dygraph.dygraph_to_static.variable_trans_func import to_static_variable #DEFINE_ALIAS +from ...fluid.dygraph.dygraph_to_static.variable_trans_func import to_static_variable_gast_node #DEFINE_ALIAS + +__all__ = [ + 'create_fill_constant_node', 'create_static_variable_gast_node', + 'data_layer_not_check', 'to_static_variable', 'to_static_variable_gast_node' +] diff --git a/python/setup.py.in b/python/setup.py.in index 63cda05488a..414258a3b37 100644 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -156,6 +156,7 @@ packages=['paddle', 'paddle.distributed.fleet.utils', 'paddle.framework', 'paddle.jit', + 'paddle.jit.dy2static', 'paddle.inference', 'paddle.fluid', 'paddle.fluid.inference', -- GitLab