
    rhA                     "   d dl mZ 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 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)UnionN)Tensor   )
_lazy_call
_lazy_initcurrent_devicedevice_countis_initialized)	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      d/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/cuda/random.pyr   r      su     L&#f%	FC	 ff-
,,C
{

55c:&&((    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"   r   r   .   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       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   r(   s     r"   cbzset_rng_state.<locals>.cbH   s<    ll; "C!JJ99#>##I.r#   )r   r   _C_DisableFuncTorchclonecontiguous_formatr   r   r   r   r   )r(   r   r.   s   `` r"   r   r   4   s     XX'') 	O "e6M6MNI	O &#f%	FC	 ff-/ rN!	O 	Os   !B22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   )r3   r&   states      r"   r   r   R   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"   r.   zmanual_seed.<locals>.cbk   s.    !JJ99#>%%d+r#   T)r   Nr   r   r   r.   s   ` r"   r   r   \   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"   r.   zmanual_seed_all.<locals>.cb~   s<    |~& 	0A %

 = =a @))$/	0r#   T)r   Nr9   r:   s   ` r"   r   r   s   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"   r.   zseed.<locals>.cb   s+    !JJ99#> r#   Nr   r.   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"   r.   zseed_all.<locals>.cb   sg    |~& 	;A %

 = =a @!&&(/<<>!--k:	;r#   Nr@   rA   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   r?   s     r"   r   r      s5     L

C

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