
    x-j)                    ^   d dl mZ d dlmZmZ d dlZd dlmZ d dlmZ er"d dl	m
Z
 d dlmZmZ d dlmZ  G d	 d
e          Zg Ze	 d&d'd            Ze	 d&d(d            Ze	 d&d)d            Zd*dZe	 	 d&d+d            Ze	 	 d&d,d             Ze	 	 d&d-d"            Z	 	 d*d#Z	 	 d.d/d%ZdS )0    )annotations)TYPE_CHECKINGoverloadN)base)core)Sequence)LiteralProtocol)	PlaceLikec                      e Zd ZddZdS )_GeneratorStatereturnintc                    d S N )selfs    `/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/incubate/framework/random.pycurrent_seedz_GeneratorState.current_seed   s          N)r   r   )__name__
__module____qualname__r   r   r   r   r   r      s        ******r   r   .devicePlaceLike | None	use_indexLiteral[True]r   	list[int]c                    d S r   r   r   r   s     r   get_rng_stater!   $   s	     r   Literal[False]list[_GeneratorState]c                    d S r   r   r    s     r   r!   r!   *   s	      Cr   bool!list[int] | list[_GeneratorState]c                    d S r   r   r    s     r   r!   r!   0   s	     ),r   Fc                h   fd}g }| t           j                                        nt          j                            |           t          t          j                  r1|	                     |t          j
                                         nt          t          j                  rUt          t          j                              D ]2}|	                     |t          j        |                               3n.t          t          j                  rTt          t          j                              D ]2}|	                     |t          j        |                               3nt          t          j                  rt'          fdt          j                    D                       }t          |          D ]W}|	                     |t          j        t          j                                        |                                         Xnt/          d           |S )aH  
    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``).
        use_index(bool): If use index is True, return the index that saved in the generator
    Returns:
        GeneratorState:  object.
    Examples:
        .. code-block:: python
            >>> import paddle
            >>> sts = paddle.incubate.get_rng_state()
    c                X    r|                                  S |                                 S r   )get_state_index	get_state)	generatorr   s    r   r+   z get_rng_state.<locals>.get_stateF   s/     	),,...&&(((r   Nc                r    g | ]3}                                 |                    d           d         k    4S ):r   )get_device_typesplit).0splaces     r   
<listcomp>z!get_rng_state.<locals>.<listcomp>\   sH        %%''1773<<?:  r   z5get_rng_state is not implemented for current device: )r   	framework_current_expected_place_paddler   _convert_to_place
isinstancer   CPUPlaceappenddefault_cpu_generator	CUDAPlacerangeget_cuda_device_countdefault_cuda_generatorXPUPlaceget_xpu_device_countdefault_xpu_generatorCustomPlacesumget_available_custom_devicedefault_custom_device_generatorr/   
ValueError)r   r   r+   
state_listidev_cntr3   s    `    @r   r!   r!   6   sO    ) ) ) ) ) J~7799//77%'' 
))D$>$@$@AABBBB	E4>	*	* 
t13344 	I 	IAii(CA(F(FGGHHHH	I	E4=	)	) 
t02233 	H 	HAii(B1(E(EFFGGGG	H	E4+	,	, 
   9;;  
 
 w 	 	A	8()>)>)@)@!DD     	 KEKK
 
 	
 r   rI   Sequence[int]Nonec                    d S r   r   rI   r   r   s      r   set_rng_staterP   q   	    
 3r   Sequence[_GeneratorState]c                    d S r   r   rO   s      r   rP   rP   y   rQ   r   )Sequence[int] | Sequence[_GeneratorState]c                    d S r   r   rO   s      r   rP   rP      rQ   r   c           	     P   fd}|t           j                                        }n|                    |          }t	          |t
          j                  r|t          |           t          j                    k    st          d          t          t          j                              D ]&} |t          j        |          | |                    'dS t	          |t
          j                  r|t          |           t          j                    k    st          d          t          t          j                              D ]&} |t          j        |          | |                    'dS t	          |t
          j                  rt          j                    }|d         }t          j        |          }t          |           |k    st          d| d          t          |          D ]K} |t          j        t          j        |                                |                    | |                    LdS t	          |t
          j                  rGt          |           dk    st          d	           |t          j                    | d                    dS t          d
|           )a  

    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``).
        use_index(bool): If use index is True, state_list should be the indices of the states

    Returns:
        None.

    Examples:
        .. code-block:: python

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

    c                d    r|                      |           d S |                     |           d S r   )set_state_index	set_state)r,   stater   s     r   rY   z set_rng_state.<locals>.set_state   s?     	'%%e,,,,,&&&&&r   N@Length of gpu state list should be equal to the gpu device count@Length of xpu state list should be equal to the xpu device countr   :Length of custom device state list should be equal to the  device count   -Length of cpu state list should be equal to 1z5set_rng_state is not implemented for current device: )r   r5   r6   r8   r9   r   r=   lenr?   rH   r>   r@   rA   rB   rC   rD   get_all_custom_device_typeget_custom_device_countrG   r/   r:   r<   )	rI   r   r   rY   r3   rJ   	dev_typesdev_typerK   s	     `      r   rP   rP      s   8' ' ' ' ' ~7799((00%(( $
:$"<">">>>R   t13344 	E 	EAId1!44jmDDDD	E 	E	E4=	)	) 
:$";"="===R   t02233 	D 	DAId033Z]CCCC	D 	D	E4+	,	, 
355	Q<.x88:'))cWccc   w 	 	AI4$U%:%:%<%<a@@  1	   	 	 
E4=	)	) 
:!##LMMM	$,..
1>>>>>KEKK
 
 	
r    Sequence[_GeneratorState] | Nonec           	     J   g }|t           j                                        }n|                    |          }| t	          |          } t          |t          j                  rt          |           t          j	                    k    st          d          t          t          j	                              D ]B}|                    t          j        |                              | |                              Cn't          |t          j                  rt          |           t          j                    k    st          d          t          t          j                              D ]B}|                    t          j        |                              | |                              Cnut          |t          j                  rt          j                    }|d         }t          j        |          }t          |           |k    st          d| d          t          |          D ]g}|                    t          j        t          j        |                                |                                        | |                              hnt          |t          j                  rbt          |           dk    st          d          |                    t          j                                        | d                              nt          d	|           |S )
ax  

    The register_rng_state_as_index function creates and registers a new generator state within the generator.
    It enables users to manage multiple generator states via indices,
    offering a convenient way to switch between these states without directly manipulating the generator's state.

    Args:
        state_list(list|tuple): A list or tuple representing the RNG states for devices.
            If not provided, the function will register the current 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:
        A list of indices representing the positions at which the new states were saved within the generator.
        These indices can be used to switch between states using set_rng_state(use_index=True)


    Examples:
        .. code-block:: python

            >>> import paddle
            >>> old_index = paddle.incubate.get_rng_state(use_index=True)
            >>> print(old_index)
            [0]
            >>> new_index = paddle.incubate.register_rng_state_as_index()
            >>> print(new_index)
            [1]
            >>> paddle.incubate.set_rng_state(old_index, use_index=True)
            >>> paddle.incubate.set_rng_state(new_index, use_index=True)

    Nr[   r\   r   r]   r^   r_   r`   z@register_rng_state_index is not implemented for current device: )r   r5   _current_expected_placer8   r!   r9   r   r=   ra   r?   rH   r>   r;   r@   register_state_indexrA   rB   rC   rD   rb   rc   rG   r/   r:   r<   )rI   r   new_state_index_listr3   rJ   rd   re   rK   s           r   register_rng_state_as_indexrk      s#   H ~6688((00"6**
%(( -
:$"<">">>>R   t13344 	 	A ''+A..CCqM    	 
E4=	)	) "
:$";"="===R   t02233 	 	A ''*1--BBqM    	 
E4+	,	, 
355	Q<.x88:'))cWccc   w 	 	A ''4$U%:%:%<%<a@@ &&z!}55   	 
E4=	)	) 	
:!##LMMM##&((==jmLL	
 	
 	
 	
 VuVV
 
 	
  r   )..)r   r   r   r   r   r   )r   r   r   r"   r   r#   )r   r   r   r%   r   r&   )NF)rI   rL   r   r   r   r   r   rM   )rI   rR   r   r   r   r"   r   rM   )rI   rT   r   r   r   r%   r   rM   )NN)rI   rf   r   r   r   r   )
__future__r   typingr   r   r7   r   paddle.baser   collections.abcr   r	   r
   paddle._typingr   r   __all__r!   rP   rk   r   r   r   <module>rr      s:   # " " " " " * * * * * * * *              +((((((((((((((((((((+ + + + +( + + +  
?B    

 
@C        
 
 
69, , , , 
,
8 8 8 8v 
  #"    
 
  # #    
 
  #    
 K
 K
 K
 K
^ 48#\  \  \  \  \  \  \ r   