Created by: gfwm2013
本PR主要是实现了用户可以通过executor中fetch_list来获取程序中的LoDTensorArray的值。
该PR修改的核心主要是对于FetchList
类型的修改,FetchList
类型的更改如下。修改很多文件是因为要适配FetchList
类型的更改和添加新FetchList
类型的支持。
FetchList = vector<LoDTensor> //更改前
FetchList = vector<boost::variant<LoDTensor, LoDTensorArray>> // 更改后
示例代码:
import paddle.fluid as fluid
import numpy as np
place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)
data = fluid.data(name='X', shape=[None, 2], dtype='float32')
i = fluid.layers.zeros(shape=[1], dtype='int64')
array = fluid.layers.array_write(x=data, i=i)
fluid.layers.increment(i)
fluid.layers.array_write(x=data, i=i, array=array)
class_dim = 2
prediction = fluid.layers.fc(input=data, size=class_dim)
loss = fluid.layers.mean(prediction)
adam = fluid.optimizer.Adam()
adam.minimize(loss)
batch_size = 2
x = np.ones([batch_size, 2]).astype('float32')
exe.run(fluid.default_startup_program())
// 单卡
res, res_array = exe.run(fluid.default_main_program(), feed={'X':x}, fetch_list=[data, array], return_merged=True)
print(res)
# [array([[1., 1.], [1., 1.]], dtype=float32)]
print(res_array)
# [[array([[1., 1.], [1., 1.]], dtype=float32),
# array([[1., 1.], [1., 1.]], dtype=float32)]]
build_strategy = fluid.BuildStrategy()
build_strategy.reduce_strategy = fluid.BuildStrategy.ReduceStrategy.AllReduce
binary = fluid.CompiledProgram(fluid.default_main_program()).with_data_parallel(
loss_name=loss.name, build_strategy=build_strategy)
// 多卡,return_merged=False
unmerged_data, unmerged_array = exe.run(binary, feed={'X': x},
fetch_list=[data, array],
return_merged=False)
print(unmerged_data)
# [array([[1., 1.]], dtype=float32), array([[1., 1.]], dtype=float32)]
print(unmerged_array)
# [[array([[1., 1.]], dtype=float32), array([[1., 1.]], dtype=float32)],
# [array([[1., 1.]], dtype=float32), array([[1., 1.]], dtype=float32)]]
// 多卡,return_merged=True
merged_data, merged_array = exe.run(binary, feed={'X': x},
fetch_list=[data, array],
return_merged=True)
print(unmerged_data)
# [[1. 1.] [1. 1.]]
print(unmerged_array)
# [array([[1., 1.],[1., 1.]], dtype=float32),
# array([[1., 1.],[1., 1.]], dtype=float32)]