[Enhancement] RNG: add deterministic launch config for cross-device consistency#78767
Merged
DongBaiYue merged 2 commits intoPaddlePaddle:developfrom Apr 24, 2026
Conversation
|
你的PR提交成功,感谢你对开源项目的贡献! |
When FLAGS_deterministic_rng is enabled, RNG kernels use a fixed grid_size and block_size instead of device-dependent values, ensuring the same seed produces identical random sequences across GPU types. Two new flags: - FLAGS_deterministic_rng (bool, default=false): enable the feature - FLAGS_deterministic_rng_grid (int32, default=1024): grid size cap Modified files: - flags.cc: define the two flags - rng_launch_config.h: new helper (IsDeterministicRNG, GetDeterministicRNGConfig) - distribution_helper.h: if/else branch in distribution_and_transform - dropout_impl.cu.h: if/else branch in DropoutFwGPUKernelDriver - fused_dropout_add_utils.h: if/else branch in GetRandomCudaProp Default (flag off) behavior is unchanged. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
bda210a to
d3fb25d
Compare
…tic-rng-launch-config # Conflicts: # paddle/phi/kernels/fusion/gpu/fused_dropout_add_utils.h
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #78767 +/- ##
===========================================
Coverage ? 100.00%
===========================================
Files ? 1
Lines ? 2
Branches ? 0
===========================================
Hits ? 2
Misses ? 0
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
zhangbo9674
approved these changes
Apr 24, 2026
|
❌ Cherry-pick failed: Conflicts detected when cherry-picking to |
|
❌ Cherry-pick failed: Conflicts detected when cherry-picking to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Category
Operator Mechanism
PR Types
New features
Description
新增
FLAGS_deterministic_rngflag,开启后相同 seed 在不同型号 GPU 上产生相同随机序列。默认关闭,不影响现有行为。问题
python3 -c "import paddle; paddle.seed(42); print(paddle.rand([1000000])[-8:].numpy())"[7.23e-01, 8.73e-01, 3.03e-01, 9.44e-01, 4.01e-01, 1.76e-01, 3.90e-04, 4.10e-01][4.15e-01, 1.43e-01, 4.31e-01, 8.18e-01, 6.12e-01, 3.92e-01, 6.85e-02, 5.67e-01][7.44e-01, 3.75e-01, 8.44e-01, 1.08e-01, 3.15e-01, 4.55e-01, 9.99e-01, 3.71e-01]相同 seed=42,三种设备结果完全不同。
根因
随机数生成(RNG)kernel 使用 Philox4_32_10 PRNG,通过
curand_init(seed, subsequence=thread_id, offset, &state)初始化。grid_size由设备属性计算:方案
Flag 接口:
FLAGS_deterministic_rng(bool,默认 false):开关FLAGS_deterministic_rng_grid(int32,默认 1024):grid size 上限,跨设备一致性要求所有设备使用相同值思路:开启 flag 后,RNG kernel 使用固定的 launch 配置(
block_size=256,grid_size=min(numel/256, FLAGS_deterministic_rng_grid)),使线程到元素的映射与设备无关,无需改 kernel 内部代码。为什么不用 StatelessPhilox:PyTorch 传统路径(DistributionTemplates.h)和 Paddle 存在相同的设备依赖问题。PyTorch 新增的 StatelessPhilox(StatelessPhilox4x32.cuh)固定
subsequence=0,用 offset 寻址全部 128-bit counter 空间,彻底解耦线程映射,但需重写所有 RNG kernel。本方案仅改 host 端 launch 逻辑,同样实现跨设备一致,侵入性更低。覆盖范围
distribution_and_transformpaddle.rand、paddle.uniform、Tensor.uniform_、paddle.randn、paddle.normal、paddle.randint、paddle.exponential_、paddle.nn.functional.rreludistribution_helper.hDropoutFwGPUKernelDriverpaddle.nn.functional.dropoutdropout_impl.cu.hGetRandomCudaPropfused_dropout_add_utils.h修改文件
paddle/common/flags.ccpaddle/phi/kernels/funcs/rng_launch_config.hpaddle/phi/kernels/funcs/distribution_helper.hdistribution_and_transform增加 if/elsepaddle/phi/kernels/funcs/dropout_impl.cu.hDropoutFwGPUKernelDriver增加 if/elsepaddle/phi/kernels/fusion/gpu/fused_dropout_add_utils.hGetRandomCudaProp增加 if/else性能影响
FLAGS_deterministic_rng_grid默认值 1024 大于所有上述设备的 SM/CU 数(A100=108, H800=132, K100-AI=120),足以饱和显存带宽,性能影响预计不大。且默认行为(flag 关闭)不受影响。是否引起精度变化
否
默认行为(flag 关闭)不受影响。