未验证 提交 41a7b071 编写于 作者: L liym27 提交者: GitHub

[Dy2Stat] Fix bug for loop: a variable is used and created in loop, but used...

[Dy2Stat] Fix bug for loop: a variable is used and created in loop, but used before created (#29769)
上级 e5af650b
......@@ -167,7 +167,13 @@ class NameVisitor(gast.NodeVisitor):
# var_a = func2(x)
#
if isinstance(var_name_to_ctxs[name][0], gast.Load):
is_created = False
for ctx in var_name_to_ctxs[name]:
if isinstance(ctx, gast.Store):
is_created = True
if isinstance(var_name_to_ctxs[name][0],
gast.Load) and is_created:
loop_var_names.add(name)
create_var_names.add(name)
......
......@@ -86,11 +86,15 @@ def for_loop_dyfunc(max_len):
def for_loop_dyfunc2(max_len):
# Test case: a variable is used and created in loop, but used before created
x = fluid.layers.fill_constant(shape=[1, 2], dtype="int32", value=1)
for i in range(max_len):
if i > 1:
s = a
a = 1
ret = fluid.layers.fill_constant(shape=[1], dtype="int32", value=s)
q, _ = x.shape # test var x.shape only used but not created in loop
ret = fluid.layers.fill_constant(shape=[1], dtype="int32", value=s + q)
return ret
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册