未验证 提交 cc2fc938 编写于 作者: H Huihuang Zheng 提交者: GitHub

[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
上级 3baf561a
...@@ -37,8 +37,7 @@ class AssertTransformer(gast.NodeTransformer): ...@@ -37,8 +37,7 @@ class AssertTransformer(gast.NodeTransformer):
def visit_Assert(self, node): def visit_Assert(self, node):
convert_assert_node = gast.parse( convert_assert_node = gast.parse(
'fluid.dygraph.dygraph_to_static.convert_operators.convert_assert({test}, {msg})'. 'paddle.jit.dy2static.convert_assert({test}, {msg})'.format(
format(
test=ast_to_source_code(node.test), test=ast_to_source_code(node.test),
msg=ast_to_source_code(node.msg) msg=ast_to_source_code(node.msg)
if node.msg else "")).body[0].value if node.msg else "")).body[0].value
......
...@@ -70,8 +70,7 @@ class CallTransformer(gast.NodeTransformer): ...@@ -70,8 +70,7 @@ class CallTransformer(gast.NodeTransformer):
if PDB_SET in func_str: if PDB_SET in func_str:
return node return node
new_func_str = "fluid.dygraph.dygraph_to_static.convert_call({})".format( new_func_str = "paddle.jit.dy2static.convert_call({})".format(func_str)
func_str)
new_func_ast = gast.parse(new_func_str).body[0].value new_func_ast = gast.parse(new_func_str).body[0].value
node.func = new_func_ast node.func = new_func_ast
......
...@@ -39,7 +39,7 @@ class CastTransformer(gast.NodeTransformer): ...@@ -39,7 +39,7 @@ class CastTransformer(gast.NodeTransformer):
func_str = ast_to_source_code(node.func).strip() func_str = ast_to_source_code(node.func).strip()
if func_str in self._castable_type and len(node.args) > 0: if func_str in self._castable_type and len(node.args) > 0:
args_str = ast_to_source_code(node.args[0]).strip() 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) args_str, func_str)
new_node = gast.parse(new_func_str).body[0].value new_node = gast.parse(new_func_str).body[0].value
return new_node return new_node
......
...@@ -310,8 +310,8 @@ def parse_cond_return(parent_vars_dict, if_vars_dict, else_vars_dict, ...@@ -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, After transformed, q and z are created in parent scope. For example,
x, y = 5, 10 x, y = 5, 10
q = fluid.dygraph.dygraph_to_static.variable_trans_func.data_layer_not_check(name='q', shape=[-1], dtype='float32') q = paddle.jit.dy2static.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') z = paddle.jit.dy2static.data_layer_not_check(name='z', shape=[-1], dtype='float32')
def true_func(x, y, q): def true_func(x, y, q):
x = x+1 x = x+1
...@@ -460,7 +460,7 @@ def create_convert_ifelse_node(return_name_ids, ...@@ -460,7 +460,7 @@ def create_convert_ifelse_node(return_name_ids,
false_func, false_func,
is_if_expr=False): 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)` pred, true_fn, false_fn, true_args, false_args, return_vars)`
to replace original `python if/else` statement. to replace original `python if/else` statement.
""" """
...@@ -491,7 +491,7 @@ def create_convert_ifelse_node(return_name_ids, ...@@ -491,7 +491,7 @@ def create_convert_ifelse_node(return_name_ids,
return_vars = create_name_nodes(return_name_ids) return_vars = create_name_nodes(return_name_ids)
convert_ifelse_layer = gast.parse( 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})'. '{pred}, {true_fn}, {false_fn}, {true_args}, {false_args}, {return_vars})'.
format( format(
pred=ast_to_source_code(pred), pred=ast_to_source_code(pred),
......
...@@ -188,8 +188,8 @@ class ListTransformer(gast.NodeTransformer): ...@@ -188,8 +188,8 @@ class ListTransformer(gast.NodeTransformer):
pass pass
elif isinstance(slice_node, gast.Index): elif isinstance(slice_node, gast.Index):
value_code = ast_to_source_code(node.value) value_code = ast_to_source_code(node.value)
i = "fluid.layers.cast(" \ i = "paddle.cast(" \
"x=fluid.dygraph.dygraph_to_static.variable_trans_func.to_static_variable({})," \ "x=paddle.jit.dy2static.to_static_variable({})," \
"dtype='int64')".format(ast_to_source_code(slice_node)) "dtype='int64')".format(ast_to_source_code(slice_node))
assign_code = "{} = fluid.layers.array_write(x={}, i={}, array={})" \ assign_code = "{} = fluid.layers.array_write(x={}, i={}, array={})" \
.format(target_name, value_code, i, target_name) .format(target_name, value_code, i, target_name)
......
...@@ -34,7 +34,7 @@ class LogicalTransformer(gast.NodeTransformer): ...@@ -34,7 +34,7 @@ class LogicalTransformer(gast.NodeTransformer):
self.generic_visit(node) self.generic_visit(node)
if isinstance(node.op, gast.Not): if isinstance(node.op, gast.Not):
arg = ast_to_source_code(node.operand) 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) arg)
# NOTE: gast.parse returns Module(body=[expr(value=...)]) # NOTE: gast.parse returns Module(body=[expr(value=...)])
new_node = gast.parse(new_node_str).body[0].value new_node = gast.parse(new_node_str).body[0].value
...@@ -67,7 +67,7 @@ class LogicalTransformer(gast.NodeTransformer): ...@@ -67,7 +67,7 @@ class LogicalTransformer(gast.NodeTransformer):
nodes = [pre_logic_node] + [post_logic_node] nodes = [pre_logic_node] + [post_logic_node]
args = [ast_to_source_code(child) for child in nodes] 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]) api_type, args[0], args[1])
# NOTE: gast.parse return Module(body=[expr(...)]) # NOTE: gast.parse return Module(body=[expr(...)])
new_node = gast.parse(new_node_str).body[0].value new_node = gast.parse(new_node_str).body[0].value
......
...@@ -46,7 +46,7 @@ def create_while_node(condition_name, body_name, loop_var_names): ...@@ -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, # 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. # 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( while_node_str = "[{}] = {}({}, {}, [{}])".format(
",".join(loop_var_names), while_func_name, condition_name, body_name, ",".join(loop_var_names), while_func_name, condition_name, body_name,
",".join(loop_var_names)) ",".join(loop_var_names))
......
...@@ -51,6 +51,5 @@ class PrintTransformer(gast.NodeTransformer): ...@@ -51,6 +51,5 @@ class PrintTransformer(gast.NodeTransformer):
def _create_print_node(self, print_args): def _create_print_node(self, print_args):
convert_print_func = gast.parse( convert_print_func = gast.parse(
'fluid.dygraph.dygraph_to_static.convert_operators.convert_print' 'paddle.jit.dy2static.convert_print').body[0].value
).body[0].value
return gast.Call(func=convert_print_func, args=print_args, keywords=[]) return gast.Call(func=convert_print_func, args=print_args, keywords=[])
...@@ -26,7 +26,7 @@ from paddle.fluid.dygraph.dygraph_to_static.static_analysis import StaticAnalysi ...@@ -26,7 +26,7 @@ from paddle.fluid.dygraph.dygraph_to_static.static_analysis import StaticAnalysi
def create_convert_shape_node(var_shape_node): def create_convert_shape_node(var_shape_node):
assert isinstance(var_shape_node, (gast.Attribute, gast.Subscript)) 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): if isinstance(var_shape_node, gast.Attribute):
api_shape_node = gast.Call( api_shape_node = gast.Call(
......
...@@ -427,7 +427,7 @@ def ast_to_func(ast_root, dyfunc, delete_on_exit=True): ...@@ -427,7 +427,7 @@ def ast_to_func(ast_root, dyfunc, delete_on_exit=True):
os.remove(filepath) os.remove(filepath)
source = ast_to_source_code(ast_root) 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 source = import_fluid + source
if six.PY2: if six.PY2:
...@@ -922,7 +922,7 @@ class ForNodeVisitor(object): ...@@ -922,7 +922,7 @@ class ForNodeVisitor(object):
else: else:
iter_var_name = ast_to_source_code(self.iter_node).strip() 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) self.iter_var_len_name, iter_var_name)
convert_len_node = gast.parse(convert_len_node_source_str).body[0] convert_len_node = gast.parse(convert_len_node_source_str).body[0]
......
...@@ -22,8 +22,8 @@ from paddle.fluid.layers import fill_constant ...@@ -22,8 +22,8 @@ from paddle.fluid.layers import fill_constant
from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.layer_helper import LayerHelper
__all__ = [ __all__ = [
'to_static_variable_gast_node', 'create_static_variable_gast_node', 'create_fill_constant_node', 'create_static_variable_gast_node',
'data_layer_not_check' '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): ...@@ -74,20 +74,20 @@ def data_layer_not_check(name, shape, dtype='float32', lod_level=0):
def to_static_variable_gast_node(name): def to_static_variable_gast_node(name):
func_code = "{} = fluid.dygraph.dygraph_to_static.variable_trans_func\ func_code = "{} = paddle.jit.dy2static.to_static_variable({})".format(name,
.to_static_variable({})".format(name, name) name)
return gast.parse(func_code).body[0] return gast.parse(func_code).body[0]
def create_static_variable_gast_node(name): 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( .data_layer_not_check(name='{}', shape=[-1], dtype='float32')".format(
name, name) name, name)
return gast.parse(func_code).body[0] return gast.parse(func_code).body[0]
def create_fill_constant_node(name, value): 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): if isinstance(value, bool):
func_code += "dtype='bool', value={})".format(value) func_code += "dtype='bool', value={})".format(value)
return gast.parse(func_code).body[0] return gast.parse(func_code).body[0]
......
...@@ -59,9 +59,9 @@ def dyfunc_with_if_else3(x): ...@@ -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 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: # 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') 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') data_layer_not_check(name='z', shape=[-1], dtype='float32')
def true_fn_0(q, x, y): def true_fn_0(q, x, y):
...@@ -77,8 +77,8 @@ def dyfunc_with_if_else3(x): ...@@ -77,8 +77,8 @@ def dyfunc_with_if_else3(x):
n = x + 3 n = x + 3
return q, x, y, z return q, x, y, z
q, x, y, z = fluid.layers.cond(fluid.layers.mean(x)[0] < 5, lambda : 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), paddle.jit.dy2static.convert_call(true_fn_0)(q, x, y),
lambda : fluid.dygraph.dygraph_to_static.convert_call(false_fn_0)(q, lambda : paddle.jit.dy2static.convert_call(false_fn_0)(q,
x, y)) x, y))
""" """
y = x + 1 y = x + 1
......
...@@ -65,7 +65,7 @@ class TestOriginInfo(unittest.TestCase): ...@@ -65,7 +65,7 @@ class TestOriginInfo(unittest.TestCase):
self.func = simple_func self.func = simple_func
def set_static_lineno(self): 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): def set_dygraph_info(self):
self.line_num = 3 self.line_num = 3
...@@ -149,7 +149,7 @@ class TestOriginInfoWithNestedFunc(TestOriginInfo): ...@@ -149,7 +149,7 @@ class TestOriginInfoWithNestedFunc(TestOriginInfo):
self.func = nested_func self.func = nested_func
def set_static_lineno(self): 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): def set_dygraph_info(self):
self.line_num = 5 self.line_num = 5
...@@ -174,7 +174,7 @@ class TestOriginInfoWithDecoratedFunc(TestOriginInfo): ...@@ -174,7 +174,7 @@ class TestOriginInfoWithDecoratedFunc(TestOriginInfo):
self.func = decorated_func self.func = decorated_func
def set_static_lineno(self): def set_static_lineno(self):
self.static_abs_lineno_list = [2, 3] self.static_abs_lineno_list = [3, 4]
def set_dygraph_info(self): def set_dygraph_info(self):
self.line_num = 2 self.line_num = 2
...@@ -208,7 +208,7 @@ class TestOriginInfoWithDecoratedFunc2(TestOriginInfo): ...@@ -208,7 +208,7 @@ class TestOriginInfoWithDecoratedFunc2(TestOriginInfo):
self.func = decorated_func2 self.func = decorated_func2
def set_static_lineno(self): def set_static_lineno(self):
self.static_abs_lineno_list = [2, 3] self.static_abs_lineno_list = [3, 4]
def set_dygraph_info(self): def set_dygraph_info(self):
self.line_num = 2 self.line_num = 2
......
...@@ -64,11 +64,9 @@ def get_source_code(func): ...@@ -64,11 +64,9 @@ def get_source_code(func):
class StaticCode1(): class StaticCode1():
# TODO: Transform return statement # TODO: Transform return statement
def dyfunc_with_if_else(x_v, label=None): def dyfunc_with_if_else(x_v, label=None):
__return_1 = fluid.layers.fill_constant( __return_1 = paddle.fill_constant(shape=[1], dtype='bool', value=False)
shape=[1], dtype='bool', value=False) __return_0 = paddle.fill_constant(shape=[1], dtype='bool', value=False)
__return_0 = fluid.layers.fill_constant( __return_value_init_0 = paddle.fill_constant(
shape=[1], dtype='bool', value=False)
__return_value_init_0 = fluid.layers.fill_constant(
shape=[1], dtype='float64', value=0.0) shape=[1], dtype='float64', value=0.0)
__return_value_0 = __return_value_init_0 __return_value_0 = __return_value_init_0
...@@ -80,13 +78,13 @@ class StaticCode1(): ...@@ -80,13 +78,13 @@ class StaticCode1():
x_v = x_v + 1 x_v = x_v + 1
return x_v 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, ), fluid.layers.mean(x_v)[0] > 5, true_fn_0, false_fn_0, (x_v, ),
(x_v, ), (x_v, )) (x_v, ), (x_v, ))
def true_fn_1(__return_0, __return_value_0, label, x_v): def true_fn_1(__return_0, __return_value_0, label, x_v):
loss = fluid.layers.cross_entropy(x_v, label) 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) shape=[1], dtype='bool', value=True)
__return_value_0 = loss __return_value_0 = loss
return __return_0, __return_value_0 return __return_0, __return_value_0
...@@ -94,14 +92,13 @@ class StaticCode1(): ...@@ -94,14 +92,13 @@ class StaticCode1():
def false_fn_1(__return_0, __return_value_0): def false_fn_1(__return_0, __return_value_0):
return __return_0, __return_value_0 return __return_0, __return_value_0
__return_0, __return_value_0 = ( __return_0, __return_value_0 = (paddle.jit.dy2static.convert_ifelse(
fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse(
label is not None, true_fn_1, false_fn_1, label is not None, true_fn_1, false_fn_1,
(__return_0, __return_value_0, label, x_v), (__return_0, __return_value_0, label, x_v),
(__return_0, __return_value_0), (__return_0, __return_value_0))) (__return_0, __return_value_0), (__return_0, __return_value_0)))
def true_fn_2(__return_1, __return_value_0, x_v): 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) shape=[1], dtype='bool', value=True)
__return_value_0 = x_v __return_value_0 = x_v
return __return_1, __return_value_0 return __return_1, __return_value_0
...@@ -109,11 +106,9 @@ class StaticCode1(): ...@@ -109,11 +106,9 @@ class StaticCode1():
def false_fn_2(__return_1, __return_value_0): def false_fn_2(__return_1, __return_value_0):
return __return_1, __return_value_0 return __return_1, __return_value_0
__return_1, __return_value_0 = ( __return_1, __return_value_0 = (paddle.jit.dy2static.convert_ifelse(
fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse( paddle.jit.dy2static.convert_logical_not(__return_0), true_fn_2,
fluid.dygraph.dygraph_to_static.convert_operators. false_fn_2, (__return_1, __return_value_0, x_v),
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), (__return_1, __return_value_0)))
return __return_value_0 return __return_value_0
...@@ -121,11 +116,9 @@ class StaticCode1(): ...@@ -121,11 +116,9 @@ class StaticCode1():
class StaticCode2(): class StaticCode2():
# TODO: Transform return statement # TODO: Transform return statement
def dyfunc_with_if_else(x_v, label=None): def dyfunc_with_if_else(x_v, label=None):
__return_3 = fluid.layers.fill_constant( __return_3 = paddle.fill_constant(shape=[1], dtype='bool', value=False)
shape=[1], dtype='bool', value=False) __return_2 = paddle.fill_constant(shape=[1], dtype='bool', value=False)
__return_2 = fluid.layers.fill_constant( __return_value_init_1 = paddle.fill_constant(
shape=[1], dtype='bool', value=False)
__return_value_init_1 = fluid.layers.fill_constant(
shape=[1], dtype='float64', value=0.0) shape=[1], dtype='float64', value=0.0)
__return_value_1 = __return_value_init_1 __return_value_1 = __return_value_init_1
...@@ -137,13 +130,13 @@ class StaticCode2(): ...@@ -137,13 +130,13 @@ class StaticCode2():
x_v = x_v + 1 x_v = x_v + 1
return x_v 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, ), fluid.layers.mean(x_v)[0] > 5, true_fn_3, false_fn_3, (x_v, ),
(x_v, ), (x_v, )) (x_v, ), (x_v, ))
def true_fn_4(__return_2, __return_value_1, label, x_v): def true_fn_4(__return_2, __return_value_1, label, x_v):
loss = fluid.layers.cross_entropy(x_v, label) 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) shape=[1], dtype='bool', value=True)
__return_value_1 = loss __return_value_1 = loss
return __return_2, __return_value_1 return __return_2, __return_value_1
...@@ -151,14 +144,13 @@ class StaticCode2(): ...@@ -151,14 +144,13 @@ class StaticCode2():
def false_fn_4(__return_2, __return_value_1): def false_fn_4(__return_2, __return_value_1):
return __return_2, __return_value_1 return __return_2, __return_value_1
__return_2, __return_value_1 = ( __return_2, __return_value_1 = (paddle.jit.dy2static.convert_ifelse(
fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse(
label is not None, true_fn_4, false_fn_4, label is not None, true_fn_4, false_fn_4,
(__return_2, __return_value_1, label, x_v), (__return_2, __return_value_1, label, x_v),
(__return_2, __return_value_1), (__return_2, __return_value_1))) (__return_2, __return_value_1), (__return_2, __return_value_1)))
def true_fn_5(__return_3, __return_value_1, x_v): 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) shape=[1], dtype='bool', value=True)
__return_value_1 = x_v __return_value_1 = x_v
return __return_3, __return_value_1 return __return_3, __return_value_1
...@@ -166,11 +158,9 @@ class StaticCode2(): ...@@ -166,11 +158,9 @@ class StaticCode2():
def false_fn_5(__return_3, __return_value_1): def false_fn_5(__return_3, __return_value_1):
return __return_3, __return_value_1 return __return_3, __return_value_1
__return_3, __return_value_1 = ( __return_3, __return_value_1 = (paddle.jit.dy2static.convert_ifelse(
fluid.dygraph.dygraph_to_static.convert_operators.convert_ifelse( paddle.jit.dy2static.convert_logical_not(__return_2), true_fn_5,
fluid.dygraph.dygraph_to_static.convert_operators. false_fn_5, (__return_3, __return_value_1, x_v),
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), (__return_3, __return_value_1)))
return __return_value_1 return __return_value_1
......
...@@ -52,7 +52,7 @@ def dyfunc_tensor_shape_4(x): ...@@ -52,7 +52,7 @@ def dyfunc_tensor_shape_4(x):
def dyfunc_tensor_shape_5(x): def dyfunc_tensor_shape_5(x):
# `res = fluid.layers.reshape(x, shape=(-1, s))` to # `res = fluid.layers.reshape(x, shape=(-1, s))` to
# `res = fluid.layers.reshape(x, shape=(-1, # `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) x = fluid.dygraph.to_variable(x)
s = x.shape[0] s = x.shape[0]
res = fluid.layers.reshape(x, shape=(-1, s)) res = fluid.layers.reshape(x, shape=(-1, s))
...@@ -65,7 +65,7 @@ def dyfunc_with_if_1(x): ...@@ -65,7 +65,7 @@ def dyfunc_with_if_1(x):
x_shape_0 = x.shape[0] x_shape_0 = x.shape[0]
if x_shape_0 < 1: if x_shape_0 < 1:
# `res.shape[0]` is transformed into # `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: if res.shape[0] > 1:
res = fluid.layers.fill_constant( res = fluid.layers.fill_constant(
value=2, shape=x.shape, dtype="int32") value=2, shape=x.shape, dtype="int32")
...@@ -89,7 +89,7 @@ def dyfunc_with_if_2(x): ...@@ -89,7 +89,7 @@ def dyfunc_with_if_2(x):
def dyfunc_with_for_1(x): def dyfunc_with_for_1(x):
x = fluid.dygraph.to_variable(x) x = fluid.dygraph.to_variable(x)
res = fluid.layers.fill_constant(value=0, shape=[1], dtype="int32") 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]): for i in range(x.shape[0]):
res += 1 res += 1
return res return res
...@@ -100,7 +100,7 @@ def dyfunc_with_for_2(x): ...@@ -100,7 +100,7 @@ def dyfunc_with_for_2(x):
x_shape_0 = x.shape[0] x_shape_0 = x.shape[0]
res = fluid.layers.fill_constant(value=0, shape=[1], dtype="int32") 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): for i in range(x_shape_0):
res += 1 res += 1
return res return res
...@@ -124,7 +124,7 @@ def dyfunc_with_for_3(x): ...@@ -124,7 +124,7 @@ def dyfunc_with_for_3(x):
def dyfunc_with_while_1(x): def dyfunc_with_while_1(x):
x = fluid.dygraph.to_variable(x) x = fluid.dygraph.to_variable(x)
res = fluid.layers.fill_constant(value=0, shape=[1], dtype="int32") 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 i = 1
while i < x.shape[0]: while i < x.shape[0]:
res += 1 res += 1
...@@ -137,7 +137,7 @@ def dyfunc_with_while_2(x): ...@@ -137,7 +137,7 @@ def dyfunc_with_while_2(x):
x_shape_0 = x.shape[0] x_shape_0 = x.shape[0]
res = fluid.layers.fill_constant(value=0, shape=[1], dtype="int32") res = fluid.layers.fill_constant(value=0, shape=[1], dtype="int32")
i = 1 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: while i < x_shape_0:
res += 1 res += 1
i = i + 2 i = i + 2
......
...@@ -51,24 +51,24 @@ class TestDataLayerNotCheck(unittest.TestCase): ...@@ -51,24 +51,24 @@ class TestDataLayerNotCheck(unittest.TestCase):
class TestVariableTransFunc(unittest.TestCase): class TestVariableTransFunc(unittest.TestCase):
def test_create_fill_constant_node(self): def test_create_fill_constant_node(self):
node = create_fill_constant_node("a", 1.0) 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) self.assertEqual(ast_to_source_code(node).strip(), source)
node = create_fill_constant_node("b", True) 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) self.assertEqual(ast_to_source_code(node).strip(), source)
if six.PY2: if six.PY2:
node = create_fill_constant_node("c", 214) 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) self.assertEqual(ast_to_source_code(node).strip(), source)
node = create_fill_constant_node("d", long(10086)) 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) self.assertEqual(ast_to_source_code(node).strip(), source)
else: else:
node = create_fill_constant_node("c", 4293) 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.assertEqual(ast_to_source_code(node).strip(), source)
self.assertIsNone(create_fill_constant_node("e", None)) self.assertIsNone(create_fill_constant_node("e", None))
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from __future__ import print_function
from ..fluid.dygraph.jit import save #DEFINE_ALIAS from ..fluid.dygraph.jit import save #DEFINE_ALIAS
from ..fluid.dygraph.jit import load #DEFINE_ALIAS from ..fluid.dygraph.jit import load #DEFINE_ALIAS
from ..fluid.dygraph.jit import TracedLayer #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 ...@@ -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 import ProgramTranslator #DEFINE_ALIAS
from ..fluid.dygraph.io import TranslatedLayer #DEFINE_ALIAS from ..fluid.dygraph.io import TranslatedLayer #DEFINE_ALIAS
from . import dy2static
__all__ = [ __all__ = [
'save', 'load', 'TracedLayer', 'to_static', 'ProgramTranslator', 'save', 'load', 'TracedLayer', 'to_static', 'ProgramTranslator',
'TranslatedLayer', 'set_code_level', 'set_verbosity' 'TranslatedLayer', 'set_code_level', 'set_verbosity'
......
# 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__
# 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']
# 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'
]
# 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'
]
...@@ -156,6 +156,7 @@ packages=['paddle', ...@@ -156,6 +156,7 @@ packages=['paddle',
'paddle.distributed.fleet.utils', 'paddle.distributed.fleet.utils',
'paddle.framework', 'paddle.framework',
'paddle.jit', 'paddle.jit',
'paddle.jit.dy2static',
'paddle.inference', 'paddle.inference',
'paddle.fluid', 'paddle.fluid',
'paddle.fluid.inference', 'paddle.fluid.inference',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册