
    zj&                        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
Z
	 dddZddZ	 dd dZd!dZd"dZd#dZd$dZ G d d          ZdS )%    )annotations)TYPE_CHECKINGN)core)Sequenceseedintreturnpaddle.base.core.Generatorc                h   t          |           } t          j                    rKt          t	          j                              D ])}t	          j        |                              |            *n]t          j                    rJt          t	          j	                              D ])}t	          j
        |                              |            *t          j                                        t          t          j                  rt          fdt	          j                    D                       }t          |          D ]N}t	          j        t          j                                        |                                        |            Ot	          j                                        |           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                r    g | ]3}                                 |                    d           d         k    4S :r   get_device_typesplit.0splaces     g/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/framework/random.py
<listcomp>zseed.<locals>.<listcomp><   H        %%''1773<<?:      )r   paddleis_compiled_with_cudaranger   get_cuda_device_countdefault_cuda_generatormanual_seedis_compiled_with_xpuget_xpu_device_countdefault_xpu_generator	framework_current_expected_place
isinstanceCustomPlacesumget_available_custom_devicedefault_custom_device_generatorr   default_cpu_generator)r   idev_cntr   s      @r   r   r      s   * t99D#%% <t13344 	= 	=A'**66t<<<<	=		$	&	& <t02233 	< 	<A&q))55d;;;;4466E%+,, 
    9;;  
 
 w 	  	 A0"5#8#8#:#:A>> k$%''33D999r   device
str | None%list[paddle.base.core.GeneratorState]c           	        g }| t           j                                        nt           j                            |           t          t           j                  r:|                    t          j	                    
                                           nt          t           j                  r^t          t          j                              D ];}|                    t          j        |          
                                           <n@t          t           j                  r]t          t          j                              D ];}|                    t          j        |          
                                           <nt          t           j                  rt'          fdt          j                    D                       }t          |          D ]`}|                    t          j        t          j                                        |                    
                                           ant/          d           |S )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    g | ]3}                                 |                    d           d         k    4S r   r   r   s     r   r   z!get_rng_state.<locals>.<listcomp>l   r   r   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   s       @r   get_rng_stater;   H   s7   ( J~ 99;;//77%)) 
$466@@BBCCCC	E6+	,	, 
t13344 	J 	JAd9!<<FFHHIIII	J	E6?	+	+ 
t02233 	I 	IAd8;;EEGGHHHH	I	E6-	.	. 
   9;;  
 
 w 	 	A4$U%:%:%<%<a@@ )++   	 KEKK
 
 	
 r   c                     g } t          j                    r\t          t          j                              D ];}|                     t          j        |                                                     <| 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   r5   r   r6   r:   r+   s     r   get_cuda_rng_stater>      sq    $ J#%% Jt13344 	J 	JAd9!<<FFHHIIIIr   r:   )Sequence[paddle.base.core.GeneratorState]Nonec                   |t           j                                        }nt           j                            |          }t          |t           j                  rt          |           t          j	                    k    st          d          t          t          j	                              D ]/}t          j        |                              | |                    0dS t          |t           j                  rt          |           t          j                    k    st          d          t          t          j                              D ]/}t          j        |                              | |                    0dS t          |t           j                  rt          j                    }|d         }t          j        |          }t          |           |k    st          d| d          t          |          D ]T}t          j        t          j        |                                |                                        | |                    UdS t          |t          j                  rPt          |           dk    st          d          t          j                                        | 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 countr   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 1z5set_rng_state is not implemented for current device: )r   r#   r2   r-   r3   r%   r7   lenr   r   r9   r   r   	set_stater8   r!   r"   r&   get_all_custom_device_typeget_custom_device_countr)   r   r4   r*   )r:   r-   r   r+   	dev_typesdev_typer,   s          r   set_rng_staterI      s   2 ~ 99;;//77%)** !
:$"<">">>>R   t13344 	D 	DA'**44Z]CCCC	D 	D	E6?	+	+ 
:$";"="===R   t02233 	C 	CA&q))33JqMBBBB	C 	C	E6-	.	. 
355	Q<.x88:'))cWccc   w 	' 	'A0"5#8#8#:#:A>> i
1&&&&	' 	' 
E4=	)	) 
:!##LMMM"$$..z!}=====KEKK
 
 	
r   c                6   t          j                    rt          |           t          j                    k    st          d          t          t          j                              D ]1}t          j        |                              | |                    0d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   rC   r   r   r9   r   r   rD   r=   s     r   set_cuda_rng_staterK      s    * #%% D:$"<">">>>T   t13344 	D 	DA'**44Z]CCCCD D
	D 	Dr   c                    | 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   staticdefault_main_programrandom_seeddefault_startup_programProgramglobal_seed)r   programs     r   _manual_program_seedrT      s]     8<FM&&((4:>FM))++7m##%%Gr   namestrc                0    t          j        | |           d S N)r   set_random_seed_generator)rU   r   s     r   rY   rY     s    "4.....r   c                *    t          j        |           S rX   )r   get_random_seed_generator)rU   s    r   r[   r[     s    )$///r   c                      e Zd Z	 dddZdS )		GeneratorNr-   str | int | paddle.core.Placer	   core.Generatorc                   t           j                            |          }t          |t          j                  rt	          j                    S t          |t          j                  r&t	          j        |	                                          S t          |t          j
                  r&t	          j        |	                                          S t          |t          j                  rt	          j        |          S dS )a  
        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:
            .. code-block:: python

                >>> import paddle
                >>> g_cpu = paddle.Generator()
        N)r   r-   device_to_placer%   r   r4   r*   r7   r   gpu_device_idr8   r"   r&   r)   )clsr-   r   s      r   __new__zGenerator.__new__  s    " --f55eT]++ 	?-///t~.. 	?.u/B/B/D/DEEEt}-- 	?-e.A.A.C.CDDDt/00 	?7>>>	? 	?r   rX   )r-   r^   r	   r_   )__name__
__module____qualname__rd    r   r   r]   r]     s0        59? ? ? ? ? ? ?r   r]   )r   r   r	   r
   rX   )r-   r.   r	   r/   )r	   r/   )r:   r?   r-   r.   r	   r@   )r:   r?   r	   r@   )r   r   r	   r@   )rU   rV   r   r   r	   r@   )rU   rV   r	   r
   )
__future__r   typingr   r   paddle.baser   collections.abcr   __all__r   r;   r>   rI   rK   rT   rY   r[   r]   rh   r   r   <module>rn      sR   # " " " " "                    )((((((
): ): ): ):Z 4 4 4 4 4n   8 ?
 ?
 ?
 ?
 ?
DD D D D<   &/ / / /0 0 0 0? ? ? ? ? ? ? ? ? ?r   