3
I4W^  ã            	   @   s†   d dl Z ddlmZmZmZmZ dddddd	d
ddg	Zddd„Zdd„ Zddd„Z	dd„ Z
dd„ Zdd	„ Zdd
„ Zdd„ Zdd„ ZdS )é    Né   )Ú
_lazy_initÚ
_lazy_callÚdevice_countÚcurrent_deviceÚget_rng_stateÚget_rng_state_allÚset_rng_stateÚset_rng_state_allÚmanual_seedÚmanual_seed_allÚseedÚseed_allÚinitial_seedÚcudac             C   sZ   t ƒ  t| tƒrtj| ƒ} nt| tƒr2tjd| ƒ} | j}|dkrFtƒ }tj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ÚtorchÚdeviceÚintÚindexr   r   Údefault_generatorsZ	get_state)r   ÚidxÚdefault_generator© r   ú2/tmp/pip-build-e66j97b2/torch/torch/cuda/random.pyr   
   s    


c              C   s*   g } x t tƒ ƒD ]}| jt|ƒƒ qW | S )zSReturns a tuple of ByteTensor representing the random number states of all devices.)Úranger   Úappendr   )ÚresultsÚir   r   r   r       s    c                sN   | j ƒ ‰tˆ tƒrtjˆ ƒ‰ ntˆ tƒr4tj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).
    r   c                 s.   ˆ j } | d krtƒ } tjj|  }|jˆƒ d S )N)r   r   r   r   r   Z	set_state)r   r   )r   Únew_state_copyr   r   Úcb7   s
    zset_rng_state.<locals>.cbN)Úcloner   r   r   r   r   r   )Z	new_stater   r!   r   )r   r    r   r	   )   s    

c             C   s$   xt | ƒD ]\}}t||ƒ q
W dS )z’Sets the random number generator state of all devices.

    Args:
        new_state (tuple of torch.ByteTensor): The desired state for each deviceN)Ú	enumerater	   )Z
new_statesr   Ústater   r   r   r
   A   s    c                s    t ˆ ƒ‰ ‡ fdd„}t|ƒ 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|  }|jˆ ƒ d S )N)r   r   r   r   r   )r   r   )r   r   r   r!   X   s    zmanual_seed.<locals>.cbN)r   r   )r   r!   r   )r   r   r   J   s    c                s    t ˆ ƒ‰ ‡ fdd„}t|ƒ dS )zÖSets 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.   x(t tƒ ƒD ]} tjj|  }|jˆ ƒ qW d S )N)r   r   r   r   r   r   )r   r   )r   r   r   r!   j   s    zmanual_seed_all.<locals>.cbN)r   r   )r   r!   r   )r   r   r   `   s    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|  }|jƒ  d S )N)r   r   r   r   r   )r   r   r   r   r   r!   {   s    zseed.<locals>.cbN)r   )r!   r   r   r   r   r   s    	c              C   s   dd„ } t | ƒ dS )z¸Sets 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   sP   d} d}xBt tƒ ƒD ]4}tjj| }|s>|jƒ  |jƒ } d}q|j| ƒ qW 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>.cbN)r   )r!   r   r   r   r   ƒ   s    c              C   s    t ƒ  tƒ } tj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   r   —   s    )r   )r   )r   Ú r   r   r   r   Ú__all__r   r   r	   r
   r   r   r   r   r   r   r   r   r   Ú<module>   s   

	
	