Created by: liym27
1. Support to create LoDTensorArray in control flow (cond and while_loop)
For example:
# Create a null array in control flow sub-block
def true_fn():
null_array = create_array("float32")
# do something else
return
def false_fn():
# do something else
return
res = cond(A==B, true_fn, false_fn)
Before this PR, it throws an error:
ValueError: Var array_0.out is not found recursively
Because null_array(array_0.out )
is considered as input not output of OP.
2. Fix bug: return LoDTensorArray in while_loop
Before this PR, while_loop
doesn't assign LoDTensorArray to parent block. It causes that the returned LoDTensorArray can't be created in while block, which should assign to parent block.
For example:
def cond(i, array_parent):
return i < ten
def body(i, array_parent):
i = i + 1
x = fluid.layers.ones(shape=[3], dtype="int32")
array_sub = fluid.layers.create_array("int32")
for j in range(5):
x = func(array_parent[j])
tensor_j = fluid.layers.fill_constant(shape=[1], dtype="int64", value=j)
fluid.layers.array_write(x,tensor_j, array_sub)
array_parent = array_sub
return i, array_parent
# do something
i = layers.fill_constant(shape=[1], dtype='int64', value=0)
ten = layers.fill_constant(shape=[1], dtype='int64', value=10)
i, array_parent = layers.while_loop(cond, body, [i, array_parent])