Paddle训练, 是否支持多线程或者多进程同时触发多个任务呢?
Created by: aprilvkuo
背景: Paddle训练, 是否支持多线程或者多进程同时触发多个不同的训练任务呢?
# 训练代码
def run_model():
train_program = fluid.Program()
startup_program = fluid.Program()
with fluid.program_guard(train_program, startup_program):
data = fluid.data(name='X', shape=[None, 1], dtype='float32')
hidden = fluid.layers.fc(input=data, size=10)
loss = fluid.layers.mean(hidden)
sgd = fluid.optimizer.SGD(learning_rate=0.001)
sgd.minimize(loss)
use_cuda = False
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place)
# Run the startup program once and only once.
# Not need to optimize/compile the startup program.
startup_program.random_seed=1
exe.run(startup_program)
# Run the main program directly without compile.
x = numpy.random.random(size=(10, 1)).astype('float32')
loss_data, = exe.run(train_program,
feed={"X": x},
fetch_list=[loss.name])
# Or use CompiledProgram:
compiled_prog = fluid.CompiledProgram(train_program)
loss_data, = exe.run(compiled_prog,
feed={"X": x},
fetch_list=[loss.name])
return 0, "success"
# 线程池调用代码
import logging
from concurrent.futures import ThreadPoolExecutor, as_completed
def train_model():
"""[summary]
"""
code, mes = run_model()
return code, mes
if __name__ == "__main__":
repeat_times = 1000
task_list = 100
executor = ThreadPoolExecutor(12)
all_tasks = []
cnt = 0
for _ in range(repeat_times):
for i in range(task_list):
all_tasks.append(executor.submit(train_model))
cnt += 1
for index, future in enumerate(as_completed(all_tasks)):
result = future.result()
print(result)
code, mes = result
if code == 0:
logging.info("task %d: quick slu train success" % index)
else:
logging.error("task %d: quick slu train failed, [%s]" % (index, mes))