a
    NSicg                     @   s   d dl Z d dlmZmZmZmZ ddlmZmZm	Z	m
Z
 ddlmZ g dZd eeee jf ed	d
dZee dddZd!eeeee jf ddddZee ddddZeddddZeddddZddddZddddZedddZdS )"    N)castIterableListUnion   )
_lazy_init
_lazy_calldevice_countcurrent_device   )Tensor)	get_rng_stateget_rng_state_allset_rng_stateset_rng_state_allmanual_seedmanual_seed_allseedseed_allinitial_seedcuda)devicereturnc                 C   sZ   t   t| trt| } nt| tr2td| } | j}|du rFt }tjj	| }|
 S )aS  Returns 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.
    r   N)r   
isinstancestrtorchr   intindexr
   r   default_generators	get_state)r   idxdefault_generator r"   M/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/cuda/random.pyr      s    


r   )r   c                  C   s&   g } t t D ]}| t| q| S )zRReturns a list of ByteTensor representing the random number states of all devices.)ranger	   appendr   )resultsir"   r"   r#   r   "   s    r   )	new_stater   r   c                    sT   | j tjdt tr$t  nt tr:td   fdd}t| dS )a0  Sets 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_formatr   c                     s6   t tj j} | d u rt } tjj|  }| d S N)r   r   r   r   r
   r   r   	set_stater    r!   r   Znew_state_copyr"   r#   cb9   s
    zset_rng_state.<locals>.cbN)cloner   contiguous_formatr   r   r   r   r   )r(   r   r.   r"   r-   r#   r   +   s    

r   )
new_statesr   c                 C   s    t | D ]\}}t|| qdS )zSets the random number generator state of all devices.

    Args:
        new_states (Iterable of torch.ByteTensor): The desired state for each deviceN)	enumerater   )r1   r'   stater"   r"   r#   r   C   s    r   )r   r   c                    s$   t    fdd}t|dd dS )a  Sets 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                     s    t  } tjj|  }|  d S r*   )r
   r   r   r   r   r,   r   r"   r#   r.   Z   s    zmanual_seed.<locals>.cbTr4   Nr   r   r   r.   r"   r4   r#   r   L   s    r   c                    s$   t    fdd}t|dd dS )zSets 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                     s*   t t D ]} tjj|  }|  q
d S r*   )r$   r	   r   r   r   r   )r'   r!   r4   r"   r#   r.   l   s    zmanual_seed_all.<locals>.cbT)r   Nr5   r6   r"   r4   r#   r   b   s    r   c                  C   s   dd } t |  dS )at  Sets 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                  S   s   t  } tjj|  }|  d S r*   )r
   r   r   r   r   r,   r"   r"   r#   r.   }   s    zseed.<locals>.cbNr   r.   r"   r"   r#   r   t   s    	r   c                  C   s   dd } t |  dS )zSets 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                  S   sL   d} d}t t D ]4}tjj| }|s<|  | } d}q||  qd S )Nr   FT)r$   r	   r   r   r   r   r   r   )Zrandom_seedZseededr'   r!   r"   r"   r#   r.      s    zseed_all.<locals>.cbNr7   r8   r"   r"   r#   r      s    r   c                  C   s    t   t } tjj|  }| S )zzReturns the current random seed of the current GPU.

    .. warning::
        This function eagerly initializes CUDA.
    )r   r
   r   r   r   r   r,   r"   r"   r#   r      s    r   )r   )r   )r   typingr   r   r   r    r   r   r	   r
   r   __all__r   r   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   <module>   s   	 	