From bf447e60e31f6ca81e434320f321471aae5fe6e3 Mon Sep 17 00:00:00 2001 From: zhhsplendid Date: Mon, 21 Sep 2020 09:50:46 +0000 Subject: [PATCH] Change fluid.dygraph dy2stat APIs to paddle.jit, test=develop --- .../dygraph/dygraph_to_static/assert_transformer.py | 2 +- .../dygraph/dygraph_to_static/cast_transformer.py | 2 +- .../dygraph/dygraph_to_static/ifelse_transformer.py | 4 ++-- .../dygraph/dygraph_to_static/list_transformer.py | 4 ++-- .../dygraph/dygraph_to_static/logical_transformer.py | 2 +- .../dygraph/dygraph_to_static/loop_transformer.py | 2 +- .../dygraph/dygraph_to_static/print_transformer.py | 2 +- .../dygraph_to_static/tensor_shape_transformer.py | 2 +- .../paddle/fluid/dygraph/dygraph_to_static/utils.py | 2 +- .../dygraph/dygraph_to_static/variable_trans_func.py | 6 +++--- .../dygraph_to_static/ifelse_simple_func.py | 4 ++-- .../unittests/dygraph_to_static/test_tensor_shape.py | 12 ++++++------ 12 files changed, 22 insertions(+), 22 deletions(-) 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 73dba66d3f..bf51ed9391 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/assert_transformer.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/assert_transformer.py @@ -37,7 +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})'. + 'paddle.jit.dygraph_to_static.convert_operators.convert_assert({test}, {msg})'. format( test=ast_to_source_code(node.test), msg=ast_to_source_code(node.msg) 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 71cb999eab..d724638f59 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.dygraph_to_static.convert_operators.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 5b8e6d2a9b..997c6cad76 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.dygraph_to_static.variable_trans_func.data_layer_not_check(name='q', shape=[-1], dtype='float32') + z = paddle.jit.dygraph_to_static.variable_trans_func.data_layer_not_check(name='z', shape=[-1], dtype='float32') def true_func(x, y, q): x = x+1 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 03b0de9907..de24fb1615 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.dygraph_to_static.variable_trans_func.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 d7af786ad7..a12373da47 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.dygraph_to_static.convert_operators.convert_logical_not({})".format( arg) # NOTE: gast.parse returns Module(body=[expr(value=...)]) 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 c66778992c..40b76e1421 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.dygraph_to_static.convert_operators.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 d555c8ed28..bfeeb3bd9b 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/print_transformer.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/print_transformer.py @@ -57,6 +57,6 @@ 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' + 'paddle.jit.dygraph_to_static.convert_operators.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 cad70f64c4..33ff673067 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.dygraph_to_static.convert_operators.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 86593dc24a..3b690c91f9 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/utils.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/utils.py @@ -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.dygraph_to_static.convert_operators.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 a7d7d55d7a..44da4f8410 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 @@ -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\ + func_code = "{} = paddle.jit.dygraph_to_static.variable_trans_func\ .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.dygraph_to_static.variable_trans_func\ .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 08d832b64a..a0a68447f3 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.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. + z = paddle.jit.dygraph_to_static.variable_trans_func. data_layer_not_check(name='z', shape=[-1], dtype='float32') def true_fn_0(q, x, y): 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 46d2b22041..9047aef234 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.dygraph_to_static.convert_operators.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.dygraph_to_static.convert_operators.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.dygraph_to_static.convert_operators.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.dygraph_to_static.convert_operators.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.dygraph_to_static.convert_operators.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.dygraph_to_static.convert_operators.convert_var_shape(x)[0]` while i < x_shape_0: res += 1 i = i + 2 -- GitLab