o
    "Õjŗ   ć                   @   sl   d dl Z d dl mZ d dlmZ g Zdd ZdddZdd	 Zdd
dZdd Z	dd Z
dd Zdd ZdS )é    N)Śbase)Ścorec                    sĘ   t | } t ” rtt ” D ]
}t |” | ” qnt ” r/tt ” D ]
}t 	|” | ” q$t
j ”  t tjr\t fddt ” D }t|D ]}t t   ” |”” | ” qKt ”  | ”S )a~  

    Sets the seed for global default generator, which manages the random number generation.

    Args:
        seed(int): The random seed to set. It is recommend to set a large int number.

    Returns:
        Generator: The global default generator object.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> gen = paddle.seed(102)

    c                    ó"   g | ]}   ” | d ”d kqS ©ś:r   ©Śget_device_typeŚsplit©Ś.0Śs©Zplace© śX/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/framework/random.pyŚ
<listcomp>7   ó    ’’zseed.<locals>.<listcomp>)Śintr   Śis_compiled_with_cudaŚrangeŚget_cuda_device_countŚdefault_cuda_generatorZmanual_seedZis_compiled_with_xpuŚget_xpu_device_countŚdefault_xpu_generatorr   Ś	frameworkŚ_current_expected_placeŚ
isinstanceŚCustomPlaceŚsumŚget_available_custom_deviceŚdefault_custom_device_generatorr   Śdefault_cpu_generator)ŚseedŚiŚdev_cntr   r   r   r!      s*   ’

ž’’
r!   c              	      s  g }| du rt j ”  ntj | ” t tjr#| 	t 
”  ” ” |S t tjr>tt ” D ]}| 	t |” ” ” q/|S t tjrYtt ” D ]}| 	t |” ” ” qJ|S t tjrt fddt ” D }t|D ]}| 	t t   ” |”” ” ” qp|S td  )aē  
    Get all random states of random generators of specified device.

    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``).

    Returns:
        GeneratorState:  object.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> sts = paddle.get_rng_state()
    Nc                    r   r   r   r
   r   r   r   r   e   r   z!get_rng_state.<locals>.<listcomp>z5get_rng_state is not implemented for current device: )r   r   r   ŚpaddleŚdeviceŚ_convert_to_placer   r   ŚCPUPlaceŚappendr    Ś	get_stateŚ	CUDAPlacer   r   r   ŚXPUPlacer   r   r   r   r   r   r   Ś
ValueError)r%   Ś
state_listr"   r#   r   r   r   Śget_rng_stateC   s@   čėī
ž’’ż
ü’r.   c                  C   s6   g } t  ” rtt  ” D ]}|  t  |” ” ” q| S )z÷

    Get random state of cuda generators.

    Args:
        None.

    Returns:
        GeneratorState:  object.

    Examples:
        .. code-block:: python

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

    )r   r   r   r   r(   r   r)   ©r-   r"   r   r   r   Śget_cuda_rng_statex   s
   r0   c                    sv  |du r
t j ”  n| |” t tjr6t| t ” ks!t	dt
t ” D ]}t |” | | ” q'dS t tjr]t| t ” ksHt	dt
t ” D ]}t |” | | ” qNdS t tjrt fddt ” D }t| |kst	d  ”  dt
|D ]}t t   ” |”” | | ” qdS t tjr“t| dks©t	d	t ”  | d
 ” dS t	d  )aq  

    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:
        .. code-block:: python

            >>> import paddle
            >>> sts = paddle.get_rng_state()
            >>> paddle.set_rng_state(sts)

    Nz@Length of gpu state list shoule be equal to the gpu device countz@Length of xpu state list shoule be equal to the xpu device countc                    r   r   r   r
   r   r   r   r   ½   r   z!set_rng_state.<locals>.<listcomp>z:Length of custom device state list shoule be equal to the z device counté   z-Length of cpu state list shoule be equal to 1r   z5set_rng_state is not implemented for current device: )r   r   r   r&   r   r   r*   Ślenr   r,   r   r   Ś	set_stater+   r   r   r   r   r   Zget_dtype_typer   r   r'   r    )r-   r%   r"   r#   r   r   r   Śset_rng_state   sT   
’’’’
ž’’’ż’r4   c                 C   sN   t  ” r#t| t  ” kstdtt  ” D ]}t  |” | | ” qdS dS )a  

    Sets generator state for all cuda generators.

    Args:
        state_list(list|tuple): The cuda states to set back to cuda generators. state_list is obtained from get_cuda_rng_state().

    Returns:
        None.

    Examples:
        .. code-block:: python

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

    zBLength of cuda state list shoule be equal to the cuda device countN)r   r   r2   r   r,   r   r   r3   r/   r   r   r   Śset_cuda_rng_stateŌ   s   ’śr5   c                 C   s0   | t j ” _| t j ” _t j ” }| | ” dS )aQ  
    Sets global seed for generating random numbers.

    NOTE(zhiqiu): This is the original implemention of seed. Keeps it temporally
    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
    N)r$   ZstaticZdefault_main_programZrandom_seedZdefault_startup_programZProgramZglobal_seed)r!   Śprogramr   r   r   Ś_manual_program_seedš   s   
r7   c                 C   s   t  | |” d S ©N)r   Śset_random_seed_generator)Śnamer!   r   r   r   r9     s   r9   c                 C   s
   t  | ”S r8   )r   Śget_random_seed_generator)r:   r   r   r   r;     s   
r;   r8   )r$   r   Zpaddle.baser   Ś__all__r!   r.   r0   r4   r5   r7   r9   r;   r   r   r   r   Ś<module>   s   
,5
B