Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/paddle/framework/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def seed(seed: int) -> paddle.base.core.Generator:

def get_rng_state(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

看下torch源码,torch.random.get_rng_state 没有device参数输入,torch.random.*更偏向于cpu的实现,而torch.cuda.random.*是gpu的实现。

这个更适合从Paddle/python/paddle/device/cpu.py导入,并移除无用的device参数,注意最终paddle.random.get_rng_statetorch.random.get_rng_state需要对齐。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

device参数不太能在原地移除吧,在python\paddle\device\__init__.py中,通过判断平台支持的设备类型自动选择get_rng_state,如果移除device参数会导致方法签名不一样

torch没有这个device参数,直接转换前缀,则device为默认值None,应该不会有问题

device: str | None = None,
) -> list[paddle.base.core.GeneratorState]:
) -> list[core.GeneratorState]:
"""
Get all random states of random generators of specified device.

Expand Down
1 change: 1 addition & 0 deletions python/paddle/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from __future__ import annotations

from paddle.base import core
from paddle.device.cpu import get_rng_state # noqa: F401

__all__ = ["initial_seed"]

Expand Down
5 changes: 5 additions & 0 deletions test/compat/test_rng_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def test_invalid_device_raises(self):
finally:
paddle.set_rng_state(original_state)

def test_api_compatibility(self):
paddle_obj = paddle.device.cpu.get_rng_state
alias_obj = paddle.random.get_rng_state
self.assertTrue(paddle_obj is alias_obj)


if __name__ == "__main__":
unittest.main()
Loading