
    Ǆg                        d dl mZmZmZ d dlZd dlmZ ddlmZmZm	Z	m
Z
 g dZddeeeej                  f   defd	Zdee   fd
Z	 ddedeeeej                  f   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y)    )IterableListUnionN)Tensor   )
_lazy_call
_lazy_initcurrent_devicedevice_count)	get_rng_stateget_rng_state_allset_rng_stateset_rng_state_allmanual_seedmanual_seed_allseedseed_allinitial_seeddevicereturnc                 8   t                t        | t              rt        j                  |       } n&t        | t
              rt        j                  d|       } | j                  }|
t               }t        j                  j                  |   }|j                         S )aR  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: ``'cuda'`` (i.e., ``torch.device('cuda')``, the current CUDA device).

    .. warning::
        This function eagerly initializes CUDA.
    cuda)r	   
isinstancestrtorchr   intindexr
   r   default_generators	get_state)r   idxdefault_generators      Y/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/cuda/random.pyr   r      su     L&#f%	FC	 ff-
,,C
{

55c:&&((    c                  n    g } t        t                     D ]  }| j                  t        |              | S )zQReturn a list of ByteTensor representing the random number states of all devices.)ranger   appendr   )resultsis     r"   r   r   -   s3    G<>" )}Q'()Nr#   	new_statec                 h   t         j                  j                         5  | j                  t         j                        ddd       t        t              rt        j                        n&t        t              rt        j                  d      fd}t        |       y# 1 sw Y   gx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: ``'cuda'`` (i.e., ``torch.device('cuda')``, the current CUDA device).
    )memory_formatNr   c                      j                   } | 
t               } t        j                  j                  |    }|j                         y N)r   r
   r   r   r   	set_state)r    r!   r   new_state_copys     r"   cbzset_rng_state.<locals>.cbF   s<    ll; "C!JJ99#>##N3r#   )
r   _C_DisableFuncTorchclonecontiguous_formatr   r   r   r   r   )r)   r   r0   r/   s    ` @r"   r   r   5   s     
	#	#	% P"u7N7NOP&#f%	FC	 ff-4 rNP Ps   !B((B1
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   )r5   r(   states      r"   r   r   P   s&     j)  5eQ r#   r   c                 @     t                 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 CUDA 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`.
    c                  v    t               } t        j                  j                  |    }|j	                         y r-   )r
   r   r   r   r   )r    r!   r   s     r"   r0   zmanual_seed.<locals>.cbi   s.    !JJ99#>%%d+r#   T)r   Nr   r   r   r0   s   ` r"   r   r   Z   s     t9D,
 rr#   c                 @     t                 fd}t        |d       y)zSet the seed for generating random numbers on all GPUs.

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

    Args:
        seed (int): The desired seed.
    c                      t        t                     D ]0  } t        j                  j                  |    }|j                         2 y r-   )r%   r   r   r   r   r   )r(   r!   r   s     r"   r0   zmanual_seed_all.<locals>.cb|   s<    |~& 	0A %

 = =a @))$/	0r#   T)r   Nr;   r<   s   ` r"   r   r   q   s     t9D0
 rD!r#   c                       d } t        |        y)at  Set the seed for generating random numbers to a random number for the current GPU.

    It's safe to call this function if CUDA 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`.
    c                  r    t               } t        j                  j                  |    }|j	                          y r-   )r
   r   r   r   r   r    r!   s     r"   r0   zseed.<locals>.cb   s+    !JJ99#> r#   Nr   r0   s    r"   r   r      s    !
 rNr#   c                       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 CUDA is not available; in that
    case, it is silently ignored.
    c                      d} d}t        t                     D ]U  }t        j                  j                  |   }|s#|j                          |j                         } d}E|j                  |        W y )Nr   FT)r%   r   r   r   r   r   r   r   )random_seedseededr(   r!   s       r"   r0   zseed_all.<locals>.cb   sg    |~& 	;A %

 = =a @!&&(/<<>!--k:	;r#   NrB   rC   s    r"   r   r      s    
; rNr#   c                      t                t               } t        j                  j                  |    }|j                         S )zyReturn the current random seed of the current GPU.

    .. warning::
        This function eagerly initializes CUDA.
    )r	   r
   r   r   r   r   rA   s     r"   r   r      s5     L

C

55c:))++r#   )r   )r   N)typingr   r   r   r   r    r   r	   r
   r   __all__r   r   r   r   r   r   r   r   r   r   r   r    r#   r"   <module>rM      s    ( (   B B
)%S%,, 67 )V ),4<  @F$S#u||%;<	6 (6"2  t  c d ."# "$ "&&,	,c 	,r#   