Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dy2stat] Refine Dy2stat APIs to 2.0rc #27430

Merged
merged 15 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def transform(self):

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def visit_Call(self, node):
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def visit_Call(self, node):
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -491,7 +491,7 @@ def create_name_nodes(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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ def _transform_slice_to_tensor_write(self, node):
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def visit_UnaryOp(self, node):
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
Expand Down Expand Up @@ -67,7 +67,7 @@ def _create_bool_op_node(self, nodes, api_type):
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,5 @@ def visit_Print(self, node):

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=[])
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
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(
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/dygraph/dygraph_to_static/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def remove_if_exit(filepath):
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:
Expand Down Expand Up @@ -922,7 +922,7 @@ def _build_var_len_assign_node(self):
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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
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'
]


Expand Down Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -77,8 +77,8 @@ def false_fn_0(q, x, y):
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def set_test_func(self):
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]
zhhsplendid marked this conversation as resolved.
Show resolved Hide resolved

def set_dygraph_info(self):
self.line_num = 3
Expand Down Expand Up @@ -149,7 +149,7 @@ def set_test_func(self):
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
Expand All @@ -174,7 +174,7 @@ def set_test_func(self):
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
Expand Down Expand Up @@ -208,7 +208,7 @@ def set_test_func(self):
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -80,52 +78,47 @@ def false_fn_0(x_v):
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

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

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

Expand All @@ -137,41 +130,38 @@ def false_fn_3(x_v):
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

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

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


Expand Down
Loading