Skip to content

[Operator] Align CPU randperm_kernel with PyTorch for bit-exact output#78614

Open
zhengshengning wants to merge 3 commits intoPaddlePaddle:developfrom
zhengshengning:acc_randperm_cpu
Open

[Operator] Align CPU randperm_kernel with PyTorch for bit-exact output#78614
zhengshengning wants to merge 3 commits intoPaddlePaddle:developfrom
zhengshengning:acc_randperm_cpu

Conversation

@zhengshengning
Copy link
Copy Markdown
Contributor

@zhengshengning zhengshengning commented Apr 8, 2026

PR Category

Operator Mechanism

PR Types

Improvements

Description

将 CPU 上的 randperm_kernel 实现与 PyTorch 对齐,使得在相同 seed 下 paddle.randpermtorch.randperm 产生 bit-for-bit 一致的输出。

改动要点

  • 新增 TorchMT19937Engine 类,实现与 PyTorch 完全一致的 Mersenne Twister 随机数引擎(注意:该引擎的行为与 std::mt19937 / std::mt19937_64 不同)。
  • 将原来使用 std::mt19937_64 + std::shuffle 的方案替换为基于 Fisher-Yates shuffle 的实现:
    • 小 n(n < UINT32_MAX / 20):使用 32 位随机数的经典 Fisher-Yates shuffle。
    • 大 n:使用 64 位随机数的 inside-out Fisher-Yates shuffle。
  • 随机种子直接从 dev_ctx.GetGenerator()->GetCurrentSeed() 获取,移除了原来硬编码 seed = 0 的逻辑。

对齐开启方式:
paddle.set_flags({'FLAGS_use_accuracy_compatible_kernel': 1})

是否引起精度变化

是。在相同 seed 下 randperm 的输出序列会发生变化(从与 std 库一致变为与 PyTorch 一致),但 randperm 本身是随机排列算子,不影响模型训练精度。

Replace std::mt19937_64 + std::shuffle with a custom MT19937 engine
that matches PyTorch's implementation, ensuring identical randperm
results under the same seed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@paddle-bot
Copy link
Copy Markdown

paddle-bot bot commented Apr 8, 2026

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@PaddlePaddle PaddlePaddle deleted a comment from codecov-commenter Apr 9, 2026
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (develop@025da99). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             develop    #78614   +/-   ##
===========================================
  Coverage           ?   100.00%           
===========================================
  Files              ?         1           
  Lines              ?        57           
  Branches           ?         0           
===========================================
  Hits               ?        57           
  Misses             ?         0           
  Partials           ?         0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@wanghuancoder wanghuancoder 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
Copy Markdown
Contributor

@From00 From00 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
Copy Markdown
Contributor

@lugimzzz lugimzzz left a comment

Choose a reason for hiding this comment

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

LGTM

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.

5 participants