PaddlePaddle / Paddle

PArallel Distributed Deep LEarning: Machine Learning Framework from Industrial Practice (『飞桨』核心框架,深度学习&机器学习高性能单机、分布式训练和跨平台部署)
http://www.paddlepaddle.org/
Apache License 2.0
21.95k stars 5.52k forks source link

[Enhancement] multi_thread training / prediction is unfriendly to user. #14416

Closed TomorrowIsAnOtherDay closed 4 years ago

TomorrowIsAnOtherDay commented 5 years ago

In reinforcement learning algorithm, having training and prediction at the same time is necessary. But it's hard to implement our parallel algorithm using current API of Fluid. Also, it's very easy to get into trouble when you try to use multi-thread training in your algorithm. Here is an example to show how to use multi-thread safely in Fluid.

#!/usr/bin/env python
# coding=utf8
# File: paddle_milti_thread.py

from paddle import fluid
import numpy as np
import threading as th
import time

def train_thread(idx, output):
    local_scope = fluid.global_scope().new_scope()

    place = fluid.CPUPlace()
    exe = fluid.Executor(place=place)

    feed = {'inputs': np.array(idx)}
    output_np = exe.run(feed=feed, fetch_list=[output], scope=local_scope, feed_var_name='feed' + str(idx), fetch_var_name='fetch' + str(idx))[0]
    print('thread idx: {}, ideal output: {}, real output: {}'.format(idx, idx, output_np))

def main():
    with fluid.unique_name.guard():
        inputs = fluid.layers.data(name='inputs', shape=[], dtype='int64')
        output = inputs
        ass = fluid.layers.assign(input=output)

    place = fluid.CPUPlace()
    exe = fluid.Executor(place=place)
    exe.run(fluid.default_startup_program())

    start = time.time()
    th_list = []
    train_thread_num = 10

    for i in range(train_thread_num):
        t = th.Thread(target=train_thread, args=(i,ass))
        t.start()
        th_list.append(t)

    for t in th_list:
        t.join()
    print('time: {}'.format(time.time() - start))

main()

To use multi-thread in your algorithm, there are three rules you have to obey:

  1. each thread has its own executor.
  2. each thread has its own scope.
  3. specify the prefix name for each executor in each run since some mechanisms below executor.

It's really unfriendly to users who want to use multi-thread in their algorithms. Could we simplify these limitation or hide these process below the fluid API?

TomorrowIsAnOtherDay commented 5 years ago

We found the top2 rules are unnecessary in multi-thread, which means that following the 3rd rule we could make our multi-thread code work, even in the complicated network.

@velconia Hope this information will be helpful for you.

velconia commented 5 years ago

Yes, I will push a PR to fix this api later.

paddle-bot-old[bot] commented 4 years ago

Since you haven\'t replied for more than a year, we have closed this issue/pr. If the problem is not solved or there is a follow-up one, please reopen it at any time and we will continue to follow up. 由于您超过一年未回复,我们将关闭这个issue/pr。 若问题未解决或有后续问题,请随时重新打开,我们会继续跟进。