o
    ńpi"  ć                   @  s   d dl mZ d dlmZ d dlZd dlmZ erd dlmZ g Z	d!d
dZ
	d"d#ddZd$ddZ	d"d%ddZd&ddZd'ddZd(ddZd)dd ZdS )*é    )Śannotations)ŚTYPE_CHECKINGN)Ścore)ŚSequenceŚseedŚintŚreturnśpaddle.base.core.Generatorc                   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© ś^/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/framework/random.pyŚ
<listcomp><   ó    ’’zseed.<locals>.<listcomp>)r   ŚpaddleŚis_compiled_with_cudaŚranger   Śget_cuda_device_countŚdefault_cuda_generatorZmanual_seedZis_compiled_with_xpuŚget_xpu_device_countŚdefault_xpu_generatorŚ	frameworkZ_current_expected_placeŚ
isinstanceŚCustomPlaceŚsumŚget_available_custom_deviceŚdefault_custom_device_generatorr   Śdefault_cpu_generator)r   ŚiŚdev_cntr   r   r   r      s*   ’

ž’’
Śdeviceś
str | Noneś%list[paddle.base.core.GeneratorState]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:
        list[GeneratorState], object.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> sts = paddle.get_rng_state()
    Nc                   r
   r   r   r   r   r   r   r   l   r   z!get_rng_state.<locals>.<listcomp>z5get_rng_state is not implemented for current device: )r   r   Ś_current_expected_place_r(   Ś_convert_to_placer    ŚCPUPlaceŚappendr   r%   Ś	get_stateŚ	CUDAPlacer   r   r   ŚXPUPlacer   r   r!   r"   r#   r$   r   Ś
ValueError)r(   Ś
state_listr&   r'   r   r   r   Śget_rng_stateH   s@   čėī
ž’’ż
ü’r4   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/   ©r3   r&   r   r   r   Śget_cuda_rng_state   s
   r6   r3   ś)Sequence[paddle.base.core.GeneratorState]ŚNonec                   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 should be equal to the gpu device countz@Length of xpu state list should 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 should be equal to the z device counté   z-Length of cpu state list should be equal to 1r   z5set_rng_state is not implemented for current device: )r   r   r+   r,   r    r0   Ślenr   r   r2   r   r   Ś	set_stater1   r   r   r!   r"   r#   Zget_dtype_typer$   r   r-   r%   )r3   r(   r&   r'   r   r   r   Śset_rng_state   sT   
’’’’
ž’’’ż’r<   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 should be equal to the cuda device countN)	r   r   r:   r   r   r2   r   r   r;   r5   r   r   r   Śset_cuda_rng_stateŽ   s   ’śr=   c                 C  s0   | t j ” _| t j ” _t j ” }| | ” dS )aS  
    Sets global seed for generating random numbers.

    NOTE(zhiqiu): This is the original implementation 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   
r?   ŚnameŚstrc                 C  s   t  | |” d S ©N)r   Śset_random_seed_generator)r@   r   r   r   r   rC     s   rC   c                 C  s
   t  | ”S rB   )r   Śget_random_seed_generator)r@   r   r   r   rD     s   
rD   )r   r   r   r	   rB   )r(   r)   r   r*   )r   r*   )r3   r7   r(   r)   r   r8   )r3   r7   r   r8   )r   r   r   r8   )r@   rA   r   r   r   r8   )r@   rA   r   r	   )Ś
__future__r   Śtypingr   r   Zpaddle.baser   Ścollections.abcr   Ś__all__r   r4   r6   r<   r=   r?   rC   rD   r   r   r   r   Ś<module>   s"   
-’
7ž
E

