Created by: liym27
- PR types: New features
- PR changes: dygraph to static graph
- Describe:
after_visit
in IfElseTransformer
, and use simpler code to achieve the same effect.
1.Remove 2. Support return variable in only one of If.body or If.orelse node.
E.g.
x, y = 5, 10
if x > 4:
x = x+1
z = x*x
q = 10
else:
y = y - 1
z = y*y
m = 20
n = 20
print(q)
n = 30
print(n)
The return_ids are (x, y, z, q) for If.body
and If.orelse
node, because
-
x
is modified in If.body node, -
y
is modified in If.body node, -
z
is both created in If.body and If.orelse node, -
q
is created only in If.body, and it is used byprint(q)
as gast.Load. Note: After transformed, q is 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')
def true_func(x, y, q):
x = x+1
z = x*x
q = 10
return x,y,z,q
def false_func(x, y, q):
y = y - 1
z = y*y
m = 20
n = 20
return x,y,z,q
x,y,z,q = fluid.layers.cond(x>4, lambda: true_func(x, y), lambda: false_func(x, y, q))
m and n are not in return_ids, because
5. m
is created only in If.orelse, but it is not used after gast.If node.
6. n
is created only in If.orelse, and it is used by n = 30
and print(n)
, but it is not used as gast.Load firstly but gast.Store .