Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

paddle单机多卡加速比不高以及profile

Open dashulu opened this issue 7 years ago • 12 comments

paddle 2卡,4卡,8卡跑vgg模型时,每秒处理图像分别为216张/sec, 341张/sec,408张/sec。4卡到8卡加速比仅为1.2,请问这个会是什么原因?如何提高加速比?

利用paddle profile工具进行分析,前三占比的op数据如下: 2卡

Event Calls Total Min. Max. Ave. Ratio.
thread0::elementwise_add 1320 3744.65 0.009216 78.4374 2.83686 0.481853
thread0::conv2d_grad 320 1143.44 1.34246 8.54323 3.57326 0.147136
thread0::all_reduce 110 834.865 0.124928 78.5367 7.58968 0.107429

4卡

Event Calls Total Min. Max. Ave. Ratio.
thread0::elementwise_add 1320 3415.35 0.009184 89.4362 2.58738 0.358557
thread0::conv2d_grad 320 2056.93 2.62554 15.9181 6.42789 0.215944
thread0::all_reduce 110 1052.51 0.172032 91.5722 9.56831 0.110497

8卡

Event Calls Total Min. Max. Ave. Ratio.
thread0::conv2d_grad 320 4012.45 5.05446 30.4189 12.5389 0.30868
thread0::elementwise_add 1320 2833.65 0.009216 90.7602 2.1467 0.217994
thread0::all_reduce 110 1447.11 0.40448 93.2229 13.1555 0.111327

此处统计为5个迭代的数据,理论上每个GPU的conv2d_grad此处应为40,为什么现在不管使用几张GPU,conv2d_grad的次数都为320?

dashulu avatar Dec 17 '18 04:12 dashulu

Please have a try of py_reader, which enhance the io performance eagerly. For details: https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/fluid/tests/unittests/test_py_reader_using_executor.py

dzhwinter avatar Dec 17 '18 04:12 dzhwinter

使用模型脚本是官方提供的吗? vgg16印象里8卡加速比大约是6,可以参考一下

dzhwinter avatar Dec 17 '18 04:12 dzhwinter

@dashulu 感觉你这个profile统计的是每张卡上的op的调用次数和耗时,请问一下你profile加的位置在哪

hjchen2 avatar Dec 17 '18 05:12 hjchen2

使用模型脚本是官方提供的吗? vgg16印象里8卡加速比大约是6,可以参考一下

使用的是官方模型vgg11.

dashulu avatar Dec 17 '18 05:12 dashulu

Please have a try of py_reader, which enhance the io performance eagerly. For details: https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/fluid/tests/unittests/test_py_reader_using_executor.py

我的数据是直接random生成的,这个用py_reader能加速吗?

dashulu avatar Dec 17 '18 05:12 dashulu

@dashulu 使用parallel executor进行多卡训练时,需要将executor的strategy线程数设为卡数,比如可以改成下面:

    strategy.num_threads = 2  # gpu卡数
    strategy.allow_op_delay = False
    exe = fluid.ParallelExecutor(
        True,
        avg_loss.name,
        exec_strategy=strategy,
        num_trainers=num_trainers,
        build_strategy=build_st,
        trainer_id=trainer_id)

多卡训练时,profiler输出的结果是每张卡对应一个线程号,比如:

thread2::conv2d_grad                              306         656.905     0.380928    6.70822     2.14675     0.346359
thread2::conv2d                                   299         251.506     0.18832     1.72851     0.841157    0.132609
thread2::all_reduce                               113         194.864     0.07168     10.5861     1.72446     0.102744
thread2::momentum                                 884         157.486     0.00512     2.52723     0.178152    0.0830363
...
thread1::conv2d_grad                              334         715.464     0.37888     5.19885     2.14211     0.357768
thread1::conv2d                                   341         269.697     0.17552     1.69882     0.7909      0.134862
thread1::all_reduce                               107         201.884     0.084992    10.24       1.88676     0.100952
thread1::momentum                                 876         148.58      0.005024    2.52621     0.169611    0.0742972
...

hjchen2 avatar Dec 17 '18 12:12 hjchen2

@dashulu 使用parallel executor进行多卡训练时,需要将executor的strategy线程数设为卡数,比如可以改成下面:

    strategy.num_threads = 2  # gpu卡数
    strategy.allow_op_delay = False
    exe = fluid.ParallelExecutor(
        True,
        avg_loss.name,
        exec_strategy=strategy,
        num_trainers=num_trainers,
        build_strategy=build_st,
        trainer_id=trainer_id)

多卡训练时,profiler输出的结果是每张卡对应一个线程号,比如:

thread2::conv2d_grad                              306         656.905     0.380928    6.70822     2.14675     0.346359
thread2::conv2d                                   299         251.506     0.18832     1.72851     0.841157    0.132609
thread2::all_reduce                               113         194.864     0.07168     10.5861     1.72446     0.102744
thread2::momentum                                 884         157.486     0.00512     2.52723     0.178152    0.0830363
...
thread1::conv2d_grad                              334         715.464     0.37888     5.19885     2.14211     0.357768
thread1::conv2d                                   341         269.697     0.17552     1.69882     0.7909      0.134862
thread1::all_reduce                               107         201.884     0.084992    10.24       1.88676     0.100952
thread1::momentum                                 876         148.58      0.005024    2.52621     0.169611    0.0742972
...

@hjchen2 把thread_num改为和GPU数一样之后,加速比并没有明显提升,从2卡到4卡为191 image/sec到281 image/sec, 性能只提升50%。 另外,这个all_reduce为什么两个thread会不一样? image

dashulu avatar Dec 19 '18 07:12 dashulu

@hjchen2 还有第二个问题,在设置2卡时,all_reduce次数为110次,conv2d_grad次数为80次, image 4卡训练时,conv2d_grad变为160次,为什么all_reduce还是110次? image

dashulu avatar Dec 19 '18 08:12 dashulu

每个batch训练结束后,所有卡进行一次同步,每同步一次,会调用一次all_reduce。如果你每张卡训练的batch数不变的话,all_reduce次数应该是不变的。

hjchen2 avatar Dec 19 '18 12:12 hjchen2

至于2卡到4卡只加速了50%的问题,请问一下batch size多大呢?由于每个batch计算完梯度后gpu都要同步一次,如果每张卡计算梯度的时间比较少,而同步的延时相对较大,就会拖累加速比,而且看你profile的结果,all reduce的占比还挺大的。你可以将batch size调大一些再看看4卡加速比。

hjchen2 avatar Dec 19 '18 12:12 hjchen2

@dashulu Is there any activation?

dzhwinter avatar Dec 28 '18 06:12 dzhwinter

Is there any activation

@dzhwinter vgg uses relu

dashulu avatar Dec 28 '18 07:12 dashulu