o
    i9:j+%                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlZg dZer$d dlm	Z	 d dl
mZ dejddfd	d
ZdejfddZdejjfddZdejjfddZdefddZdddZdefddZdae j					d defddZdejdB fddZdS )!    N)	Generator)TYPE_CHECKING)set_rng_stateget_rng_statemanual_seedseedinitial_seedfork_rngthread_safe_generator)
WorkerInfo)default_generator	new_statereturnc                 C   s   t |  dS )zSets the random number generator state.

    .. note:: This function only works for CPU. For CUDA, please use
        :func:`torch.manual_seed`, which works for both CPU and CUDA.

    Args:
        new_state (torch.ByteTensor): The desired state
    N)r   	set_state)r    r   S/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/torch/random.pyr      s   	r   c                   C      t  S )zReturns the random number generator state as a `torch.ByteTensor`.

    .. note:: The returned state is for the default generator on CPU only.

    See also: :func:`torch.random.fork_rng`.
    )r   	get_stater   r   r   r   r   '   s   r   c                 C   s   t | S )a  Sets the seed for generating random numbers on all devices. Returns a
    `torch.Generator` object.

    Args:
        seed (int): The desired seed. Value must be within the inclusive range
            `[-0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff]`. Otherwise, a RuntimeError
            is raised. Negative inputs are remapped to positive values with the formula
            `0xffff_ffff_ffff_ffff + seed`.
    )_manual_seed_impl)r   r   r   r   r   1   s   
r   c                 C   s   t | } dd l}|j s|j|  dd l}|j s"|j|  dd l}|j	 s1|j	|  dd l
}|j s@|j|  t|  t| S )Nr   )int
torch.cudacuda_is_in_bad_forkmanual_seed_all	torch.mpsmpsr   	torch.xpuxpu
torch.mtiamtia_seed_custom_devicer   r   torchr   r   r   r   >   s   




r   c                  C   s   t  } ddl}|j s|j|  ddl}|j s"|j|  ddl	}|j
 s1|j
|  ddl}|j s@|j|  t|  | S )zSets the seed for generating random numbers to a non-deterministic
    random number on all devices. Returns a 64 bit number used to seed the RNG.
    r   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r   r   r   r   Y   s   



r   c                 C   s   t | } tj }tt|rPtt|}d}d}t||r2t||r2t|| s0t|||  dS dS d| d}|d| d| d| d7 }tj|td	d
 dS dS )zSets the seed to generate random numbers for custom device.

    Args:
        seed (int): The desired seed.

    See [Note: support the custom device with privateuse1]
    r   r   zSet seed for `z0` device does not take effect, please add API's `z` and `z` to `z` device module.   
stacklevelN)	r   r"   _C_get_privateuse1_backend_namehasattrgetattrwarningswarnUserWarning)r   custom_backend_namecustom_device_mod_bad_fork_name_seed_all_namemessager   r   r   r    w   s    


r    c                   C   r   )zReturns the initial seed for generating random numbers as a
    Python `long`.

    .. note:: The returned seed is for the default generator on CPU only.
    )r   r   r   r   r   r   r      s   r   FTr	   devicesr   c                 #   s|   |dkr
dV  dS t |j}tt |d  du r$td| dd |s+dV  dS | du r{  }|dkrttst|  d| d| d	|  d
|  d|  d|  d| d| d|  d| d| d}tj	|dd dat
t|} nt
| } t  } fdd| D }zdV  W t | t| |D ]
\}	}
 |
|	 qdS t | t| |D ]
\}	}
 |
|	 qw )az  
    Forks the RNG, so that when you return, the RNG is reset
    to the state that it was previously in.

    Args:
        devices (iterable of Device IDs): devices for which to fork
            the RNG. CPU RNG state is always forked. By default, :meth:`fork_rng` operates
            on all devices, but will emit a warning if your machine has a lot
            of devices, since this function will run very slowly in that case.
            If you explicitly specify devices, this warning will be suppressed
        enabled (bool): if ``False``, the RNG is not forked.  This is a convenience
            argument for easily disabling the context manager without having
            to delete it and unindent your Python code under it.
        device_type (str): device type str, default is `cuda`. As for supported device,
            see details in :ref:`accelerator<accelerators>`
    metaNztorch has no module of `z`, you should register z,a module by `torch._register_device_module`.   z reports that you have z& available devices, and you have used z_ without explicitly specifying which devices are being used. For safety, we initialize *every* zA device by default, which can be quite slow if you have a lot of z5s. If you know that you are only making use of a few z' devices, set the environment variable z_VISIBLE_DEVICES or the 'z' keyword argument of z with the set of devices you are actually using. For example, if you are using CPU only, set device.upper()_VISIBLE_DEVICES= or devices=[]; if you are using device 0 only, set zb_VISIBLE_DEVICES=0 or devices=[0].  To initialize all devices and suppress this warning, set the 'z#' keyword argument to `range(torch.z.device_count())`.   r%   Tc                    s   g | ]}  |qS r   )r   ).0device
device_modr   r   
<listcomp>   s    zfork_rng.<locals>.<listcomp>)r"   r8   typer*   RuntimeErrordevice_count_fork_rng_warned_alreadyupperr+   r,   listranger   r   zip)r3   enabled_caller_devices_kwdevice_typenum_devicesr2   cpu_rng_statedevice_rng_statesr8   device_rng_stater   r9   r   r	      sp   

	


c                  C   s:   ddl m}  |  }|dur|jdkr|jdur|jjS dS )as  Returns a thread-safe random number generator for use in DataLoader workers.
    This function provides a convenient way for transforms and user code to use
    thread-safe random number generation without manually checking worker context.
    When called in a DataLoader thread worker, returns the worker's thread-local
    :class:`torch.Generator`. When called in the main process or process workers,
    returns ``None`` (which causes PyTorch functions to use the default global RNG).
    Returns:
        Optional[torch.Generator]: Thread-local generator in thread workers, None otherwise.
    Example::
        >>> from torch.random import thread_safe_generator
        >>> generator = thread_safe_generator()
        >>> torch.randint(0, 10, (5,), generator=generator)
    Example with transforms::
        >>> from torch.random import thread_safe_generator
        >>> class MyRandomTransform:
        ...     def __call__(self, img):
        ...         generator = thread_safe_generator()
        ...         offset = torch.randint(0, 10, (2,), generator=generator)
        ...         return img[..., offset[0]:, offset[1]:]
    r   )get_worker_infoNthread)torch.utils.datarL   worker_methodrngtorch_generator)rL   worker_infor   r   r   r
      s   

r
   )r   N)NTr	   r3   r   )
contextlibr+   collections.abcr   typingr   r"   __all__torch.utils.data._utils.workerr   torch._Cr   Tensorr   r   r'   r   r   r   r   r    r   r?   contextmanagerr	   r
   r   r   r   r   <module>   s6   

	P