2020-03-23 22:01:54 +08:00
|
|
|
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
2024-07-22 13:59:45 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2024-08-08 09:23:03 +08:00
|
|
|
from typing import TYPE_CHECKING
|
2024-07-22 13:59:45 +08:00
|
|
|
|
2023-04-19 19:23:48 +08:00
|
|
|
import paddle
|
2023-09-07 17:26:19 +08:00
|
|
|
from paddle.base import core
|
2020-04-20 10:47:23 +08:00
|
|
|
|
2024-08-08 09:23:03 +08:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from collections.abc import Sequence
|
|
|
|
|
|
2021-04-29 19:31:40 +08:00
|
|
|
__all__ = []
|
|
|
|
|
|
2020-04-20 10:47:23 +08:00
|
|
|
|
2024-07-22 13:59:45 +08:00
|
|
|
def seed(seed: int) -> paddle.base.core.Generator:
|
2020-04-20 10:47:23 +08:00
|
|
|
"""
|
2020-05-14 11:27:10 +08:00
|
|
|
|
2020-08-28 15:56:58 +08:00
|
|
|
Sets the seed for global default generator, which manages the random number generation.
|
2020-04-20 10:47:23 +08:00
|
|
|
|
|
|
|
|
Args:
|
2020-08-28 15:56:58 +08:00
|
|
|
seed(int): The random seed to set. It is recommend to set a large int number.
|
2020-04-20 10:47:23 +08:00
|
|
|
|
|
|
|
|
Returns:
|
2020-08-28 15:56:58 +08:00
|
|
|
Generator: The global default generator object.
|
2020-04-20 10:47:23 +08:00
|
|
|
|
|
|
|
|
Examples:
|
2026-02-19 14:07:07 +08:00
|
|
|
.. code-block:: pycon
|
2020-04-20 10:47:23 +08:00
|
|
|
|
[xdoctest] reformat example code with google style in No.21-30 (#55849)
* [Doctest]fix No.21, test=docs_preview
* Revert "[Doctest]fix No.21, test=docs_preview"
This reverts commit 76bcdb280e254d682be6fc6f85588f1940bb1ade.
* [Doctest]fix No.21, test=docs_preview
* fix bugs,test=docs_preview
* [Doctest]fix No.22-24,26,27, test=docs_preview
* update fix
* with pre-commit, test=docs_preview
* fix seed, test=docs_preview
* fix error, test=docs_preview
* fix seed, test=docs_preview
* fix seed, test=docs_preview
2023-08-02 11:50:35 +08:00
|
|
|
>>> import paddle
|
|
|
|
|
>>> gen = paddle.seed(102)
|
2020-08-28 15:56:58 +08:00
|
|
|
|
|
|
|
|
"""
|
2022-10-23 20:01:27 +08:00
|
|
|
# TODO(zhiqiu): 1. remove program.random_seed when all random-related op upgrade
|
2022-06-05 10:58:58 +08:00
|
|
|
# 2. support gpu generator by global device
|
2020-08-28 15:56:58 +08:00
|
|
|
|
|
|
|
|
seed = int(seed)
|
|
|
|
|
|
2024-01-15 11:18:47 +08:00
|
|
|
if paddle.is_compiled_with_cuda():
|
2020-09-04 10:51:31 +08:00
|
|
|
for i in range(core.get_cuda_device_count()):
|
|
|
|
|
core.default_cuda_generator(i).manual_seed(seed)
|
2024-01-15 11:18:47 +08:00
|
|
|
elif paddle.is_compiled_with_xpu():
|
2022-12-23 22:09:09 +08:00
|
|
|
for i in range(core.get_xpu_device_count()):
|
|
|
|
|
core.default_xpu_generator(i).manual_seed(seed)
|
2024-01-15 11:18:47 +08:00
|
|
|
place = paddle.framework._current_expected_place()
|
|
|
|
|
if isinstance(place, paddle.CustomPlace):
|
2023-04-19 19:23:48 +08:00
|
|
|
dev_cnt = sum(
|
|
|
|
|
[
|
|
|
|
|
place.get_device_type() == s.split(':')[0]
|
|
|
|
|
for s in core.get_available_custom_device()
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
for i in range(dev_cnt):
|
|
|
|
|
core.default_custom_device_generator(
|
2024-01-15 11:18:47 +08:00
|
|
|
paddle.CustomPlace(place.get_device_type(), i)
|
2023-04-19 19:23:48 +08:00
|
|
|
).manual_seed(seed)
|
2020-08-28 15:56:58 +08:00
|
|
|
return core.default_cpu_generator().manual_seed(seed)
|
|
|
|
|
|
|
|
|
|
|
2024-07-22 13:59:45 +08:00
|
|
|
def get_rng_state(
|
|
|
|
|
device: str | None = None,
|
|
|
|
|
) -> list[paddle.base.core.GeneratorState]:
|
2022-12-23 22:09:09 +08:00
|
|
|
"""
|
|
|
|
|
Get all random states of random generators of specified device.
|
2023-11-30 10:29:16 +08:00
|
|
|
|
2022-12-23 22:09:09 +08:00
|
|
|
Args:
|
|
|
|
|
device(str): This parameter determines the specific running device.
|
|
|
|
|
It can be ``cpu``, ``gpu``, ``xpu``, Default is None.
|
|
|
|
|
If None, return the generators of current device (specified by ``set_device``).
|
2023-11-30 10:29:16 +08:00
|
|
|
|
2022-12-23 22:09:09 +08:00
|
|
|
Returns:
|
2024-07-22 13:59:45 +08:00
|
|
|
list[GeneratorState], object.
|
2023-11-30 10:29:16 +08:00
|
|
|
|
2022-12-23 22:09:09 +08:00
|
|
|
Examples:
|
2026-02-19 14:07:07 +08:00
|
|
|
.. code-block:: pycon
|
2023-11-30 10:29:16 +08:00
|
|
|
|
[xdoctest] reformat example code with google style in No.21-30 (#55849)
* [Doctest]fix No.21, test=docs_preview
* Revert "[Doctest]fix No.21, test=docs_preview"
This reverts commit 76bcdb280e254d682be6fc6f85588f1940bb1ade.
* [Doctest]fix No.21, test=docs_preview
* fix bugs,test=docs_preview
* [Doctest]fix No.22-24,26,27, test=docs_preview
* update fix
* with pre-commit, test=docs_preview
* fix seed, test=docs_preview
* fix error, test=docs_preview
* fix seed, test=docs_preview
* fix seed, test=docs_preview
2023-08-02 11:50:35 +08:00
|
|
|
>>> import paddle
|
|
|
|
|
>>> sts = paddle.get_rng_state()
|
2022-12-23 22:09:09 +08:00
|
|
|
"""
|
|
|
|
|
state_list = []
|
|
|
|
|
if device is None:
|
2024-08-12 15:26:00 +08:00
|
|
|
place = paddle.framework._current_expected_place_()
|
2022-12-23 22:09:09 +08:00
|
|
|
else:
|
2023-04-19 19:23:48 +08:00
|
|
|
place = paddle.device._convert_to_place(device)
|
2022-12-23 22:09:09 +08:00
|
|
|
|
2024-01-15 11:18:47 +08:00
|
|
|
if isinstance(place, paddle.CPUPlace):
|
2022-12-23 22:09:09 +08:00
|
|
|
state_list.append(core.default_cpu_generator().get_state())
|
2024-01-15 11:18:47 +08:00
|
|
|
elif isinstance(place, paddle.CUDAPlace):
|
2022-12-23 22:09:09 +08:00
|
|
|
for i in range(core.get_cuda_device_count()):
|
|
|
|
|
state_list.append(core.default_cuda_generator(i).get_state())
|
2024-01-15 11:18:47 +08:00
|
|
|
elif isinstance(place, paddle.XPUPlace):
|
2022-12-23 22:09:09 +08:00
|
|
|
for i in range(core.get_xpu_device_count()):
|
|
|
|
|
state_list.append(core.default_xpu_generator(i).get_state())
|
2024-01-15 11:18:47 +08:00
|
|
|
elif isinstance(place, paddle.CustomPlace):
|
2023-04-19 19:23:48 +08:00
|
|
|
dev_cnt = sum(
|
|
|
|
|
[
|
|
|
|
|
place.get_device_type() == s.split(':')[0]
|
|
|
|
|
for s in core.get_available_custom_device()
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
for i in range(dev_cnt):
|
|
|
|
|
state_list.append(
|
|
|
|
|
core.default_custom_device_generator(
|
|
|
|
|
core.CustomPlace(place.get_device_type(), i)
|
|
|
|
|
).get_state()
|
|
|
|
|
)
|
2022-12-23 22:09:09 +08:00
|
|
|
else:
|
|
|
|
|
raise ValueError(
|
2023-09-22 10:14:38 +08:00
|
|
|
f"get_rng_state is not implemented for current device: {place}"
|
2022-12-23 22:09:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return state_list
|
|
|
|
|
|
|
|
|
|
|
2024-07-22 13:59:45 +08:00
|
|
|
def get_cuda_rng_state() -> list[paddle.base.core.GeneratorState]:
|
2020-09-04 10:51:31 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
Get random state of cuda generators.
|
|
|
|
|
|
|
|
|
|
Args:
|
2022-05-18 11:05:32 +08:00
|
|
|
None.
|
2020-09-04 10:51:31 +08:00
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
GeneratorState: object.
|
|
|
|
|
|
|
|
|
|
Examples:
|
2026-02-19 14:07:07 +08:00
|
|
|
.. code-block:: pycon
|
2020-09-04 10:51:31 +08:00
|
|
|
|
[xdoctest] reformat example code with google style in No.21-30 (#55849)
* [Doctest]fix No.21, test=docs_preview
* Revert "[Doctest]fix No.21, test=docs_preview"
This reverts commit 76bcdb280e254d682be6fc6f85588f1940bb1ade.
* [Doctest]fix No.21, test=docs_preview
* fix bugs,test=docs_preview
* [Doctest]fix No.22-24,26,27, test=docs_preview
* update fix
* with pre-commit, test=docs_preview
* fix seed, test=docs_preview
* fix error, test=docs_preview
* fix seed, test=docs_preview
* fix seed, test=docs_preview
2023-08-02 11:50:35 +08:00
|
|
|
>>> import paddle
|
|
|
|
|
>>> sts = paddle.get_cuda_rng_state()
|
2020-09-04 10:51:31 +08:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
state_list = []
|
2024-01-15 11:18:47 +08:00
|
|
|
if paddle.is_compiled_with_cuda():
|
2020-09-04 10:51:31 +08:00
|
|
|
for i in range(core.get_cuda_device_count()):
|
|
|
|
|
state_list.append(core.default_cuda_generator(i).get_state())
|
|
|
|
|
|
|
|
|
|
return state_list
|
|
|
|
|
|
|
|
|
|
|
2024-07-22 13:59:45 +08:00
|
|
|
def set_rng_state(
|
|
|
|
|
state_list: Sequence[paddle.base.core.GeneratorState],
|
|
|
|
|
device: str | None = None,
|
|
|
|
|
) -> None:
|
2022-12-23 22:09:09 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
Sets generator state for all device generators.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
state_list(list|tuple): The device states to set back to device generators. state_list is obtained from get_rng_state().
|
|
|
|
|
device(str): This parameter determines the specific running device.
|
|
|
|
|
It can be ``cpu``, ``gpu``, ``xpu``, Default is None.
|
|
|
|
|
If None, return the generators of current device (specified by ``set_device``).
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
None.
|
|
|
|
|
|
|
|
|
|
Examples:
|
2026-02-19 14:07:07 +08:00
|
|
|
.. code-block:: pycon
|
2022-12-23 22:09:09 +08:00
|
|
|
|
[xdoctest] reformat example code with google style in No.21-30 (#55849)
* [Doctest]fix No.21, test=docs_preview
* Revert "[Doctest]fix No.21, test=docs_preview"
This reverts commit 76bcdb280e254d682be6fc6f85588f1940bb1ade.
* [Doctest]fix No.21, test=docs_preview
* fix bugs,test=docs_preview
* [Doctest]fix No.22-24,26,27, test=docs_preview
* update fix
* with pre-commit, test=docs_preview
* fix seed, test=docs_preview
* fix error, test=docs_preview
* fix seed, test=docs_preview
* fix seed, test=docs_preview
2023-08-02 11:50:35 +08:00
|
|
|
>>> import paddle
|
|
|
|
|
>>> sts = paddle.get_rng_state()
|
|
|
|
|
>>> paddle.set_rng_state(sts)
|
2022-12-23 22:09:09 +08:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
if device is None:
|
2024-08-12 15:26:00 +08:00
|
|
|
place = paddle.framework._current_expected_place_()
|
2022-12-23 22:09:09 +08:00
|
|
|
else:
|
2025-09-23 15:22:09 +08:00
|
|
|
place = paddle.device._convert_to_place(device)
|
2022-12-23 22:09:09 +08:00
|
|
|
|
2024-01-15 11:18:47 +08:00
|
|
|
if isinstance(place, paddle.CUDAPlace):
|
2022-12-23 22:09:09 +08:00
|
|
|
if not len(state_list) == core.get_cuda_device_count():
|
|
|
|
|
raise ValueError(
|
2024-01-23 14:43:28 +08:00
|
|
|
"Length of gpu state list should be equal to the gpu device count"
|
2022-12-23 22:09:09 +08:00
|
|
|
)
|
|
|
|
|
for i in range(core.get_cuda_device_count()):
|
|
|
|
|
core.default_cuda_generator(i).set_state(state_list[i])
|
2024-01-15 11:18:47 +08:00
|
|
|
elif isinstance(place, paddle.XPUPlace):
|
2022-12-23 22:09:09 +08:00
|
|
|
if not len(state_list) == core.get_xpu_device_count():
|
|
|
|
|
raise ValueError(
|
2024-01-23 14:43:28 +08:00
|
|
|
"Length of xpu state list should be equal to the xpu device count"
|
2022-12-23 22:09:09 +08:00
|
|
|
)
|
|
|
|
|
for i in range(core.get_xpu_device_count()):
|
|
|
|
|
core.default_xpu_generator(i).set_state(state_list[i])
|
2024-01-15 11:18:47 +08:00
|
|
|
elif isinstance(place, paddle.CustomPlace):
|
2025-09-12 10:15:38 +08:00
|
|
|
dev_types = core.get_all_custom_device_type()
|
|
|
|
|
dev_type = dev_types[0]
|
|
|
|
|
dev_cnt = core.get_custom_device_count(dev_type)
|
2023-04-19 19:23:48 +08:00
|
|
|
if not len(state_list) == dev_cnt:
|
|
|
|
|
raise ValueError(
|
2025-09-12 10:15:38 +08:00
|
|
|
f"Length of custom device state list should be equal to the {dev_cnt} device count"
|
2023-04-19 19:23:48 +08:00
|
|
|
)
|
|
|
|
|
for i in range(dev_cnt):
|
|
|
|
|
core.default_custom_device_generator(
|
2024-01-15 11:18:47 +08:00
|
|
|
paddle.CustomPlace(place.get_device_type(), i)
|
2023-04-19 19:23:48 +08:00
|
|
|
).set_state(state_list[i])
|
2022-12-23 22:09:09 +08:00
|
|
|
elif isinstance(place, core.CPUPlace):
|
|
|
|
|
if not len(state_list) == 1:
|
2024-01-23 14:43:28 +08:00
|
|
|
raise ValueError("Length of cpu state list should be equal to 1")
|
2022-12-23 22:09:09 +08:00
|
|
|
core.default_cpu_generator().set_state(state_list[0])
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError(
|
2023-09-22 10:14:38 +08:00
|
|
|
f"set_rng_state is not implemented for current device: {place}"
|
2022-12-23 22:09:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-07-22 13:59:45 +08:00
|
|
|
def set_cuda_rng_state(
|
|
|
|
|
state_list: Sequence[paddle.base.core.GeneratorState],
|
|
|
|
|
) -> None:
|
2020-09-04 10:51:31 +08:00
|
|
|
"""
|
|
|
|
|
|
2022-05-18 11:05:32 +08:00
|
|
|
Sets generator state for all cuda generators.
|
2020-09-04 10:51:31 +08:00
|
|
|
|
|
|
|
|
Args:
|
2021-04-27 13:41:05 +08:00
|
|
|
state_list(list|tuple): The cuda states to set back to cuda generators. state_list is obtained from get_cuda_rng_state().
|
2020-09-04 10:51:31 +08:00
|
|
|
|
|
|
|
|
Returns:
|
2022-05-18 11:05:32 +08:00
|
|
|
None.
|
2020-09-04 10:51:31 +08:00
|
|
|
|
|
|
|
|
Examples:
|
2026-02-19 14:07:07 +08:00
|
|
|
.. code-block:: pycon
|
2020-09-04 10:51:31 +08:00
|
|
|
|
[xdoctest] reformat example code with google style in No.21-30 (#55849)
* [Doctest]fix No.21, test=docs_preview
* Revert "[Doctest]fix No.21, test=docs_preview"
This reverts commit 76bcdb280e254d682be6fc6f85588f1940bb1ade.
* [Doctest]fix No.21, test=docs_preview
* fix bugs,test=docs_preview
* [Doctest]fix No.22-24,26,27, test=docs_preview
* update fix
* with pre-commit, test=docs_preview
* fix seed, test=docs_preview
* fix error, test=docs_preview
* fix seed, test=docs_preview
* fix seed, test=docs_preview
2023-08-02 11:50:35 +08:00
|
|
|
>>> import paddle
|
|
|
|
|
>>> sts = paddle.get_cuda_rng_state()
|
|
|
|
|
>>> paddle.set_cuda_rng_state(sts)
|
2020-09-04 10:51:31 +08:00
|
|
|
|
|
|
|
|
"""
|
2024-01-15 11:18:47 +08:00
|
|
|
if paddle.is_compiled_with_cuda():
|
2020-09-04 10:51:31 +08:00
|
|
|
if not len(state_list) == core.get_cuda_device_count():
|
|
|
|
|
raise ValueError(
|
2024-01-23 14:43:28 +08:00
|
|
|
"Length of cuda state list should be equal to the cuda device count"
|
2020-09-04 10:51:31 +08:00
|
|
|
)
|
|
|
|
|
for i in range(core.get_cuda_device_count()):
|
|
|
|
|
core.default_cuda_generator(i).set_state(state_list[i])
|
|
|
|
|
|
|
|
|
|
|
2024-07-22 13:59:45 +08:00
|
|
|
def _manual_program_seed(seed: int) -> None:
|
2020-08-28 15:56:58 +08:00
|
|
|
"""
|
|
|
|
|
Sets global seed for generating random numbers.
|
2022-09-14 21:56:19 +08:00
|
|
|
|
2024-01-23 14:43:28 +08:00
|
|
|
NOTE(zhiqiu): This is the original implementation of seed. Keeps it temporally
|
2020-08-28 15:56:58 +08:00
|
|
|
since CUDA generator is not developed, so we need it in the unittest.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
seed(int): The random seed to set. It is recommend to set a large int number.
|
2022-09-14 21:56:19 +08:00
|
|
|
|
2020-08-28 15:56:58 +08:00
|
|
|
Returns:
|
|
|
|
|
None
|
2020-04-20 10:47:23 +08:00
|
|
|
"""
|
2023-11-10 17:04:10 +08:00
|
|
|
paddle.static.default_main_program().random_seed = seed
|
|
|
|
|
paddle.static.default_startup_program().random_seed = seed
|
|
|
|
|
program = paddle.static.Program()
|
2020-04-20 10:47:23 +08:00
|
|
|
program.global_seed(seed)
|
2021-10-19 14:59:38 +08:00
|
|
|
|
|
|
|
|
|
2024-07-22 13:59:45 +08:00
|
|
|
def set_random_seed_generator(name: str, seed: int) -> None:
|
2021-10-19 14:59:38 +08:00
|
|
|
core.set_random_seed_generator(name, seed)
|
|
|
|
|
|
|
|
|
|
|
2024-07-22 13:59:45 +08:00
|
|
|
def get_random_seed_generator(name: str) -> paddle.base.core.Generator:
|
2021-10-19 14:59:38 +08:00
|
|
|
return core.get_random_seed_generator(name)
|
2025-11-13 21:15:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Generator:
|
|
|
|
|
def __new__(
|
|
|
|
|
cls, device: str | int | paddle.core.Place = None
|
|
|
|
|
) -> core.Generator:
|
|
|
|
|
"""
|
|
|
|
|
Generator is a random number generator.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
device(str|int|paddle.core.Place): The device type to create the generator on.
|
|
|
|
|
It can be ``cpu``, ``gpu``, ``xpu``, or a paddle.core.Place instance.
|
|
|
|
|
default is None, which means using current device.
|
|
|
|
|
|
|
|
|
|
Examples:
|
2026-02-19 14:07:07 +08:00
|
|
|
.. code-block:: pycon
|
2025-11-13 21:15:05 +08:00
|
|
|
|
|
|
|
|
>>> import paddle
|
|
|
|
|
>>> g_cpu = paddle.Generator()
|
|
|
|
|
"""
|
|
|
|
|
place = paddle.device.device_to_place(device)
|
|
|
|
|
if isinstance(place, core.CPUPlace):
|
|
|
|
|
return core.default_cpu_generator()
|
|
|
|
|
elif isinstance(place, core.CUDAPlace):
|
|
|
|
|
return core.default_cuda_generator(place.gpu_device_id())
|
|
|
|
|
elif isinstance(place, core.XPUPlace):
|
|
|
|
|
return core.default_xpu_generator(place.gpu_device_id())
|
|
|
|
|
elif isinstance(place, core.CustomPlace):
|
|
|
|
|
return core.default_custom_device_generator(place)
|