random.py 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   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.

# TODO: define random api
16
import paddle.fluid as fluid
L
Leo Chen 已提交
17
from paddle.fluid import core
18

19 20
__all__ = []

21

C
cnn 已提交
22
def seed(seed):
23
    """
S
swtkiwi 已提交
24

L
Leo Chen 已提交
25
    Sets the seed for global default generator, which manages the random number generation.
26 27

    Args:
L
Leo Chen 已提交
28
        seed(int): The random seed to set. It is recommend to set a large int number.
29 30

    Returns:
L
Leo Chen 已提交
31
        Generator: The global default generator object.
32 33 34 35

    Examples:
        .. code-block:: python

L
Leo Chen 已提交
36
            import paddle
C
cnn 已提交
37
            gen = paddle.seed(102)
L
Leo Chen 已提交
38 39 40 41 42 43 44

    """
    #TODO(zhiqiu): 1. remove program.random_seed when all random-related op upgrade
    # 2. support gpu generator by global device 

    seed = int(seed)

Y
yaoxuefeng 已提交
45 46 47 48 49
    if core.is_compiled_with_cuda():
        for i in range(core.get_cuda_device_count()):
            core.default_cuda_generator(i)._is_init_py = True
            core.default_cuda_generator(i).manual_seed(seed)

L
Leo Chen 已提交
50 51 52 53
    core.default_cpu_generator()._is_init_py = True
    return core.default_cpu_generator().manual_seed(seed)


Y
yaoxuefeng 已提交
54 55 56 57 58 59
def get_cuda_rng_state():
    """

    Get random state of cuda generators.

    Args:
60
        None.
Y
yaoxuefeng 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

    Returns:
        GeneratorState:  object.

    Examples:
        .. code-block:: python

            import paddle
            sts = paddle.get_cuda_rng_state()

    """
    state_list = []
    if core.is_compiled_with_cuda():
        for i in range(core.get_cuda_device_count()):
            state_list.append(core.default_cuda_generator(i).get_state())

    return state_list


def set_cuda_rng_state(state_list):
    """

83
    Sets generator state for all cuda generators.
Y
yaoxuefeng 已提交
84 85

    Args:
86
        state_list(list|tuple): The cuda states to set back to cuda generators. state_list is obtained from get_cuda_rng_state().
Y
yaoxuefeng 已提交
87 88

    Returns:
89
        None.
Y
yaoxuefeng 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

    Examples:
        .. code-block:: python

            import paddle
            sts = paddle.get_cuda_rng_state()
            paddle.set_cuda_rng_state(sts)

    """
    if core.is_compiled_with_cuda():
        if not len(state_list) == core.get_cuda_device_count():
            raise ValueError(
                "Length of cuda state list shoule be equal to the cuda device count"
            )
        for i in range(core.get_cuda_device_count()):
            core.default_cuda_generator(i).set_state(state_list[i])


L
Leo Chen 已提交
108 109 110 111
def _manual_program_seed(seed):
    """
    Sets global seed for generating random numbers.
  
C
cnn 已提交
112
    NOTE(zhiqiu): This is the original implemention of seed. Keeps it temporally
L
Leo Chen 已提交
113 114 115 116 117 118 119
    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.
    
    Returns:
        None
120 121 122 123 124
    """
    fluid.default_main_program().random_seed = seed
    fluid.default_startup_program().random_seed = seed
    program = fluid.Program()
    program.global_seed(seed)
125 126 127 128 129 130 131 132


def set_random_seed_generator(name, seed):
    core.set_random_seed_generator(name, seed)


def get_random_seed_generator(name):
    return core.get_random_seed_generator(name)