Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hybrid] static model parallel dropout support deterministic RandomSeedGenerator #36228

Merged
merged 9 commits into from
Oct 19, 2021

Conversation

wangxicoding
Copy link
Contributor

@wangxicoding wangxicoding commented Sep 29, 2021

PR types

New features

PR changes

APIs

Describe

static model parallel support deterministic RandomSeedGenerator.
image
如图,在模型并行中,对于duplicated变量需要确保dropout op的一致性,需要使用相同的随机种子生成器。而对于distributed变量为保证随机性,需要使用不同的随机种子生成器(相同种子理论上也可收敛)。

目前静态图中不支持在executor运行时切换随机种子生成器,故通过seed op来使用固定的随机种子生成器,以seed op的输出作为dropout的种子输入,确保dropout在不同卡上的随机性一致。

PR中添加了set_random_seed_generator来设置随机种子生成器,可通过对应的名称获取。同时由于dropout需要固定随机种子生成器的功能目前只在模型并行中有需求,所以在fleet.meta_parallel.parallel_layers添加中包含rng_name即随机种子生成器名称参数的dropout,供模型并行使用。

此外,由于在recompute中,需要固定前向和反向重计算前向时dropout的输入种子,在develop中,如果添加了dropout,则在前向中插入seed op保证随机性。由于本PR的dropout已添加了seed op确保了随机性,所以不需要重复插入。

使用方式:

import numpy as np
import paddle
import paddle.static as static
from paddle.framework.random import set_random_seed_generator
from paddle.distributed.fleet.meta_parallel.parallel_layers.random import dropout

paddle.enable_static()

set_random_seed_generator('local_seed', 123)
set_random_seed_generator('global_seed', 1234)

input = static.data(name="input", shape=[5, 5], dtype="float32")
res1 = dropout(
    input,
    p=0.3,
    training=True,
    mode='upscale_in_train',
    rng_name='local_seed')
res2 = dropout(
    input,
    p=0.3,
    training=True,
    mode='upscale_in_train',
    rng_name='global_seed')

in_np = np.ones([5, 5]).astype('float32')

exe = static.Executor()
res_list = [res1, res2]
for i in range(2):
    out1, out2 = exe.run(static.default_main_program(),
                         feed={"input": in_np},
                         fetch_list=res_list)
    print(out1, out2)

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@wangxicoding wangxicoding changed the title static model parallel support deterministic RandomSeedGenerator [hybrid] static model parallel support deterministic RandomSeedGenerator Oct 12, 2021
@wangxicoding wangxicoding changed the title [hybrid] static model parallel support deterministic RandomSeedGenerator [hybrid] static model parallel dropout support deterministic RandomSeedGenerator Oct 14, 2021
Copy link
Contributor

@gongweibao gongweibao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@zhiqiu zhiqiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for backward.py

@wangxicoding wangxicoding merged commit 8cc8e41 into PaddlePaddle:develop Oct 19, 2021
@wangxicoding wangxicoding deleted the static_mp_random_seed branch October 19, 2021 06:59
wangxicoding added a commit to wangxicoding/Paddle that referenced this pull request Oct 25, 2021
fuyinno4 pushed a commit that referenced this pull request Oct 25, 2021
… RandomSeedGenerator (#36682)

* Revert "Add fused_dropout wrapper to ease use. (#36185) (#36640)"

This reverts commit 05d7e2f.

* [hybrid] seed and dropout op support force-cpu (#35820)

* [HIP] fix op not support AMD GPU bug, the flag PADDLE_WITH_ROCM is invalid

* [HIP] fix op not support AMD GPU bug, the flag PADDLE_WITH_ROCM is invalid

* [HIP] fix op not support AMD GPU bug

* [hybrid] seed and dropout op support force-cpu

* [hybrid] seed and dropout op support force-cpu

* [hybrid] seed and dropout op support force-cpu

* [hybrid] seed and dropout op support force-cpu

* [hybrid] seed and dropout op support force-cpu

* [hybrid] fix seed ci failed issue

* add AsExtra for force_cpu of seed op

* Add fused_dropout wrapper to ease use. (#36185)

* [hybrid] static model parallel dropout support deterministic RandomSeedGenerator (#36228)

Co-authored-by: xiayanming <41795079@qq.com>
Co-authored-by: Li Min <11663212+limin2021@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants