o
    hw                     @   s  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
 ddeeeejf d	efd
dZd	ee fddZ	ddedeeeejf d	dfddZdee d	dfddZded	dfddZded	dfddZd ddZd ddZd	efddZg dZdS )!    )Iterable)UnionN)Tensor   )
_lazy_call
_lazy_initcurrent_devicedevice_countxpudevicereturnc                 C   sZ   t   t| trt| } nt| trtd| } | j}|du r#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
   N)r   
isinstancestrtorchr   intindexr   r
   default_generators	get_state)r   idxdefault_generator r   d/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torch/xpu/random.pyget_rng_state   s   


r   c                  C   s   dd t t D } | S )zQReturn a list of ByteTensor representing the random number states of all devices.c                 S   s   g | ]}t |qS r   )r   ).0ir   r   r   
<listcomp>#   s    z%get_rng_state_all.<locals>.<listcomp>)ranger	   )resultsr   r   r   get_rng_state_all!   s   r   	new_statec                    s~   t j  | jt jdW d   n1 sw   Y  t tr't   nt tr2t d   fd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
   c                     s.    j } | d u r
t } tjj|  }| d S N)r   r   r   r
   r   	set_stater   r   r   new_state_copyr   r   cb8   s
   zset_rng_state.<locals>.cb)
r   _C_DisableFuncTorchclonecontiguous_formatr   r   r   r   r   )r   r   r&   r   r$   r   set_rng_state'   s   


r+   
new_statesc                 C   s    t | D ]	\}}t|| q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)	enumerater+   )r,   r   stater   r   r   set_rng_state_allB   s   r/   seedc                    $   t    fdd}t|d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`.
    c                     s    t  } tjj|  }|  d S r!   )r   r   r
   r   manual_seedr#   r0   r   r   r&   Z   s   zmanual_seed.<locals>.cbTr3   Nr   r   r0   r&   r   r3   r   r2   L   s   r2   c                    r1   )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.
    c                     s*   t t D ]} tjj|  }|  qd S r!   )r   r	   r   r
   r   r2   )r   r   r3   r   r   r&   l   s   zmanual_seed_all.<locals>.cbT)seed_allNr4   r5   r   r3   r   manual_seed_allb   s   r7   c                  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`.
    c                  S   s   t  } tjj|  }|  d S r!   )r   r   r
   r   r0   r#   r   r   r   r&   ~   s   zseed.<locals>.cbNr   r&   r   r   r   r0   t   s   
c                  C   r8   )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.
    c                  S   sL   d} d}t t D ]}tjj| }|s|  | } d}q	||  q	d S )Nr   FT)r   r	   r   r
   r   r0   initial_seedr2   )random_seedseededr   r   r   r   r   r&      s   zseed_all.<locals>.cbNr9   r:   r   r   r   r6      s   r6   c                  C   s    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   r;   r#   r   r   r   r;      s   r;   )	r   r   r+   r/   r2   r7   r0   r6   r;   )r
   )r   N)collections.abcr   typingr   r   r    r   r   r   r	   r   r   r   r   listr   r+   r/   r2   r7   r0   r6   r;   __all__r   r   r   r   <module>   s,    



