
    o
qi                        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dS )    )IterableN)Tensor   )
_lazy_call
_lazy_initcurrent_devicedevice_countis_initializedxpudevicereturnc                 J   t                       t          | t                    rt          j        |           } n*t          | t
                    rt          j        d|           } | j        }|t                      }t          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.
    r   )r   
isinstancestrtorchr   intindexr   r   default_generators	get_state)r   idxdefault_generators      p/var/www/html/bestrading.cuttalo.com/services/ml-inference/venv/lib/python3.11/site-packages/torch/xpu/random.pyget_rng_stater   
   s     LLL&# -f%%	FC	 	  -eV,,
,C
{	4S9&&(((    c                  P    d t          t                                D             } | S )zQReturn a list of ByteTensor representing the random number states of all devices.c                 ,    g | ]}t          |          S  )r   ).0is     r   
<listcomp>z%get_rng_state_all.<locals>.<listcomp>"   s     ???A}Q???r   )ranger	   )resultss    r   get_rng_state_allr#       s&    ??|~~)>)>???GNr   	new_statec                     t                      sVt          j                                        5                       t          j                   ddd           n# 1 swxY w Y   t          t                    rt          j                  n*t          t                    rt          j        d          d fd}t          |           dS )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   r   c                      j         } | t                      } t          j        j        |          }|                               d S N)r   r   r   r   r   	set_state)r   r   r   r$   s     r   cbzset_rng_state.<locals>.cb7   sD    l; ""C!I8=##I.....r   r   N)r
   r   _C_DisableFuncTorchclonecontiguous_formatr   r   r   r   r   )r$   r   r*   s   `` r   set_rng_stater0   &   s!     OX'')) 	O 	O!e6MNNI	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O &# -f%%	FC	 	  -eV,,/ / / / / / / rNNNNNs   !AA #A 
new_statesc                 P    t          |           D ]\  }}t          ||           dS )zSet the random number generator state of all devices.

    Args:
        new_states (Iterable of torch.ByteTensor): The desired state for each device.
    N)	enumerater0   )r1   r   states      r   set_rng_state_allr5   A   s<     j))    5eQ   r   seedc                 T     t                      d fd}t          |d           dS )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`.
    r   Nc                  |    t                      } t          j        j        |          }|                               d S r(   )r   r   r   r   manual_seed)r   r   r6   s     r   r*   zmanual_seed.<locals>.cbY   s8    !I8=%%d+++++r   T)r6   r+   r   r   r6   r*   s   ` r   r9   r9   K   sI     t99D, , , , , ,
 rr   c                 T     t                      d fd}t          |d           dS )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.
    r   Nc                      t          t                                D ].} t          j        j        |          }|                               /d S r(   )r!   r	   r   r   r   r9   )r   r   r6   s     r   r*   zmanual_seed_all.<locals>.cbk   sO    |~~&& 	0 	0A %	 <Q ?))$////	0 	0r   T)seed_allr+   r:   r;   s   ` r   manual_seed_allr?   a   sI     t99D0 0 0 0 0 0
 rD!!!!!!r   c                  ,    dd} t          |            dS )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`.
    r   Nc                  x    t                      } t          j        j        |          }|                                 d S r(   )r   r   r   r   r6   r   r   s     r   r*   zseed.<locals>.cb}   s5    !I8=     r   r+   r   r*   s    r   r6   r6   s   s'    ! ! ! !
 rNNNNNr   c                  ,    dd} t          |            dS )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.
    r   Nc                      d} d}t          t                                D ][}t          j        j        |         }|s+|                                 |                                } d}F|                    |            \d S )Nr   FT)r!   r	   r   r   r   r6   initial_seedr9   )random_seedseededr   r   s       r   r*   zseed_all.<locals>.cb   s    |~~&& 	; 	;A %	 <Q ? ;!&&(((/<<>>!--k::::	; 	;r   r+   rC   rD   s    r   r>   r>      s'    
; 
; 
; 
; rNNNNNr   c                      t                       t                      } t          j        j        |          }|                                S )zxReturn the current random seed of the current GPU.

    .. warning::
        This function eagerly initializes XPU.
    )r   r   r   r   r   rG   rB   s     r   rG   rG      s:     LLL


C	4S9))+++r   )	r   r#   r0   r5   r9   r?   r6   r>   rG   )r   r+   )collections.abcr   r   r    r   r   r   r	   r
   r   r   r   r   listr#   r0   r5   r9   r?   r6   r>   rG   __all__r   r   r   <module>rO      s   $ $ $ $ $ $        R R R R R R R R R R R R R R) )#)el2 )v ) ) ) ),4<     V S3Y-E RV    6 (6"2  t        c d    ,"# "$ " " " "$   $   *	,c 	, 	, 	, 	,
 
 
r   