
    i                        d dl mZ d dlZd dlmZ ddlmZmZmZmZm	Z	 dde
ez  ej                  z  defdZdee   fd	Zdd
ede
ez  ej                  z  ddfdZdee   ddfdZde
ddfdZde
ddfdZddZddZde
fdZg dZy)    )IterableN)Tensor   )
_lazy_call
_lazy_initcurrent_devicedevice_countis_initializeddevicereturnc                 8   t                t        | t              rt        j                  |       } n&t        | t
              rt        j                  d|       } | j                  }|
t               }t        j                  j                  |   }|j                         S )aN  Return the random number generator state of the specified GPU as a ByteTensor.

    Args:
        device (torch.device or int, optional): The device to return the RNG state of.
            Default: ``'xpu'`` (i.e., ``torch.device('xpu')``, the current XPU device).

    .. warning::
        This function eagerly initializes XPU.
    xpu)r   
isinstancestrtorchr   intindexr   r   default_generators	get_state)r   idxdefault_generators      J/var/www/html/engine/venv/lib/python3.12/site-packages/torch/xpu/random.pyget_rng_stater   
   su     L&#f%	FC	 eV,
,,C
{		44S9&&((    c                  b    t        t                     D  cg c]  } t        |        }} |S c c} w )zQReturn a list of ByteTensor representing the random number states of all devices.)ranger	   r   )iresultss     r   get_rng_state_allr       s,    ).|~)>?A}Q?G?N @s   ,	new_statec                 ~    t               sGt        j                  j                         5   j	                  t        j
                         ddd       t        t              rt        j                        n&t        t              rt        j                  d      d fd}t        |       y# 1 sw Y   hxY w)a,  Set the random number generator state of the specified GPU.

    Args:
        new_state (torch.ByteTensor): The desired state
        device (torch.device or int, optional): The device to set the RNG state.
            Default: ``'xpu'`` (i.e., ``torch.device('xpu')``, the current XPU device).
    )memory_formatNr   c                      j                   } | 
t               } t        j                  j                  |    }|j                         y N)r   r   r   r   r   	set_state)r   r   r   r    s     r   cbzset_rng_state.<locals>.cb7   s<    ll; "C!II88=##I.r   r   N)r
   r   _C_DisableFuncTorchclonecontiguous_formatr   r   r   r   r   )r    r   r&   s   `` r   set_rng_stater,   &   s     XX'') 	O!e6M6MNI	O &#f%	FC	 eV,/ rN	O 	Os   !B33B<
new_statesc                 B    t        |       D ]  \  }}t        ||        y)zSet the random number generator state of all devices.

    Args:
        new_states (Iterable of torch.ByteTensor): The desired state for each device.
    N)	enumerater,   )r-   r   states      r   set_rng_state_allr1   A   s&     j)  5eQ r   seedc                 B     t                d fd}t        |d       y)a  Set the seed for generating random numbers for the current GPU.

    It's safe to call this function if XPU is not available; in that case, it is silently ignored.

    Args:
        seed (int): The desired seed.

    .. warning::
        If you are working with a multi-GPU model, this function is insufficient
        to get determinism.  To seed all GPUs, use :func:`manual_seed_all`.
    Nc                  v    t               } t        j                  j                  |    }|j	                         y r$   )r   r   r   r   manual_seed)r   r   r2   s     r   r&   zmanual_seed.<locals>.cbY   s.    !II88=%%d+r   T)r2   r'   r   r   r2   r&   s   ` r   r5   r5   K   s     t9D,
 rr   c                 B     t                d fd}t        |d       y)zSet the seed for generating random numbers on all GPUs.

    It's safe to call this function if XPU is not available; in that case, it is silently ignored.

    Args:
        seed (int): The desired seed.
    Nc                      t        t                     D ]0  } t        j                  j                  |    }|j                         2 y r$   )r   r	   r   r   r   r5   )r   r   r2   s     r   r&   zmanual_seed_all.<locals>.cbk   s<    |~& 	0A %		 < <Q ?))$/	0r   T)seed_allr'   r6   r7   s   ` r   manual_seed_allr;   a   s     t9D0
 rD!r   c                  "    dd} t        |        y)ao  Set the seed for generating random numbers to a random number for the current GPU.

    It's safe to call this function if XPU is not available; in that case, it is silently ignored.

    .. warning::
        If you are working with a multi-GPU model, this function will only initialize
        the seed on one GPU.  To initialize all GPUs, use :func:`seed_all`.
    Nc                  r    t               } t        j                  j                  |    }|j	                          y r$   )r   r   r   r   r2   r   r   s     r   r&   zseed.<locals>.cb}   s+    !II88= r   r'   r   r&   s    r   r2   r2   s   s    !
 rNr   c                  "    dd} t        |        y)zSet the seed for generating random numbers to a random number on all GPUs.

    It's safe to call this function if XPU is not available; in that case, it is silently ignored.
    Nc                      d} d}t        t                     D ]U  }t        j                  j                  |   }|s#|j                          |j                         } d}E|j                  |        W y )Nr   FT)r   r	   r   r   r   r2   initial_seedr5   )random_seedseededr   r   s       r   r&   zseed_all.<locals>.cb   sg    |~& 	;A %		 < <Q ?!&&(/<<>!--k:	;r   r'   r?   r@   s    r   r:   r:      s    
; rNr   c                      t                t               } t        j                  j                  |    }|j                         S )zxReturn the current random seed of the current GPU.

    .. warning::
        This function eagerly initializes XPU.
    )r   r   r   r   r   rC   r>   s     r   rC   rC      s5     L

C		44S9))++r   )	r   r   r,   r1   r5   r;   r2   r:   rC   )r   r'   )collections.abcr   r   r    r   r   r   r	   r
   r   r   r   r   listr   r,   r1   r5   r;   r2   r:   rC   __all__ r   r   <module>rL      s    $   R R)#)ell2 )v ),4< V S3Y-E RV 6 (6"2  t  c d ,"# "$ "$$*	,c 	,
r   