o
    pi)                     @  s  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
 d
eZg Ze	d)d*ddZe	d)d+ddZe	d)d,ddZd-ddZe		d)d.dd Ze		d)d/d"d Ze		d)d0d$d Z		d-d%d Z		d1d2d'd(ZdS )3    )annotations)TYPE_CHECKINGoverloadN)base)core)Sequence)LiteralProtocol)	PlaceLikec                   @  s   e Zd ZdddZdS )_GeneratorStatereturnintc                 C     d S N )selfr   r   g/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/incubate/framework/random.pycurrent_seed   s    z_GeneratorState.current_seedN)r   r   )__name__
__module____qualname__r   r   r   r   r   r      s    r   .devicePlaceLike | None	use_indexLiteral[True]r   	list[int]c                 C  r   r   r   r   r   r   r   r   get_rng_state$      r   Literal[False]list[_GeneratorState]c                 C  r   r   r   r   r   r   r   r   *   r   bool!list[int] | list[_GeneratorState]c                 C  r   r   r   r   r   r   r   r   0   r   Fc              
     s$  fdd}g }| du rt j  ntj|  t tjr)|	|t
  |S t tjrDtt D ]}|	|t| q5|S t tjr_tt D ]}|	|t| qP|S t tjrt fddt D }t|D ]}|	|tt  | qv|S td  )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                   s    r|   S |  S r   )Zget_state_index	get_state)	generatorr   r   r   r#   F   s   z get_rng_state.<locals>.get_stateNc                   "   g | ]}   |d d kqS :r   get_device_typesplit.0splacer   r   
<listcomp>\       z!get_rng_state.<locals>.<listcomp>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_cntr   r0   r   r   r   6   sD   
rG   Sequence[int]Nonec                 C  r   r   r   rG   r   r   r   r   r   set_rng_stateq      rN   Sequence[_GeneratorState]c                 C  r   r   r   rM   r   r   r   rN   y   rO   )Sequence[int] | Sequence[_GeneratorState]c                 C  r   r   r   rM   r   r   r   rN      rO   c                   s  fdd}|du rt j  n|| t tjr<t| t ks't	dt
t D ]}|t|| |  q-dS t tjrct| t ksNt	dt
t D ]}|t|| |  qTdS t tjrt fddt D }t| |kst	d   d	t
|D ]}|tt  || |  qdS t tjrt| d
kst	d|t | 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                   s     r	|  | d S | | d S r   )Zset_state_index	set_state)r$   stater%   r   r   rR      s   z set_rng_state.<locals>.set_stateN@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 countc                   r&   r'   r)   r,   r/   r   r   r1      r2   z!set_rng_state.<locals>.<listcomp>:Length of custom device state list should be equal to the  device count   -Length of cpu state list should be equal to 1r   z5set_rng_state is not implemented for current device: )r   r3   r4   r6   r7   r   r;   lenr=   rF   r<   r>   r?   r@   rA   rB   rC   rD   get_dtype_typerE   r*   r8   r:   )rG   r   r   rR   rH   rI   r   rJ   r   rN      sZ   

 Sequence[_GeneratorState] | Nonec              	     s  g }|du rt j  n|| | du rt|} t tjrCt| t	 ks+t
dtt	 D ]}|t|| |  q1|S t tjrmt| t ksUt
dtt D ]}|t|| |  q[|S t tjrt fddt D }t| |kst
d   dt|D ]}|tt  || |  q|S t tjrt| dkst
d	|t | d
  |S t
d  )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)

    NrT   rU   c                   r&   r'   r)   r,   r/   r   r   r1      r2   z/register_rng_state_as_index.<locals>.<listcomp>rV   rW   rX   rY   r   z@register_rng_state_index is not implemented for current device: )r   r3   Z_current_expected_placer6   r   r7   r   r;   rZ   r=   rF   r<   r9   r>   Zregister_state_indexr?   r@   rA   rB   rC   rD   r[   rE   r*   r8   r:   )rG   r   Znew_state_index_listrH   rI   r   r/   r   register_rng_state_as_index   st   $

+
 

r]   )..)r   r   r   r   r   r   )r   r   r   r   r   r    )r   r   r   r!   r   r"   )NF)rG   rK   r   r   r   r   r   rL   )rG   rP   r   r   r   r   r   rL   )rG   rQ   r   r   r   r!   r   rL   )NN)rG   r\   r   r   r   r   )
__future__r   typingr   r   r5   r   Zpaddle.baser   collections.abcr   r   r	   Zpaddle._typingr
   r   __all__r   rN   r]   r   r   r   r   <module>   sN   
;	
R