a
    d$                     @   s  d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
 d dlZd dlZd dlmZmZmZmZ e eZeejjZeejjZde
e eeddd	Zeefeeed
ddZddddZ dee
e ddddZ!e	e"ef dddZ#e	e"ef ddddZ$dS )    N)getstate)setstate)AnyDictOptional)	_get_rankrank_prefixed_messagerank_zero_onlyrank_zero_warnF)seedworkersreturnc                 C   s*  | du rzt jd}|du r6ttt} td|   qzt|} W q tyv   ttt} tdt	| d|   Y q0 nt
| tst| } t|   krtksn t|  dt dt  ttt} ttd|  t  t| t jd< t|  tj|  t|  tj|  t| t jd	< | S )
a^  Function that sets seed for pseudo-random number generators in: pytorch, numpy, python.random In addition,
    sets the following environment variables:

    - `PL_GLOBAL_SEED`: will be passed to spawned subprocesses (e.g. ddp_spawn backend).
    - `PL_SEED_WORKERS`: (optional) is set to 1 if ``workers=True``.

    Args:
        seed: the integer value seed for global random state in Lightning.
            If `None`, will read seed from `PL_GLOBAL_SEED` env variable
            or select it randomly.
        workers: if set to ``True``, will properly configure all dataloaders passed to the
            Trainer with a ``worker_init_fn``. If the user already provides such a function
            for their dataloaders, setting this argument will have no influence. See also:
            :func:`~lightning_fabric.utilities.seed.pl_worker_init_function`.
    NPL_GLOBAL_SEEDzNo seed found, seed set to zInvalid seed found: z, seed set to z& is not in bounds, numpy accepts from z to zGlobal seed set to PL_SEED_WORKERS)osenvironget_select_seed_randomlymin_seed_valuemax_seed_valuer
   int
ValueErrorrepr
isinstanceloginfor   r   strrandomr   nptorchmanual_seedcudaZmanual_seed_all)r   r   Zenv_seed r"   h/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lightning_fabric/utilities/seed.pyseed_everything   s.    

 



r$   )r   r   r   c                 C   s   t | |S )N)r   randint)r   r   r"   r"   r#   r   A   s    r   )r   c                  C   sD   t jdd} | du rdS t jdd}tt| tt|d dS )zReset the seed to the value that :func:`lightning_fabric.utilities.seed.seed_everything` previously set.

    If :func:`lightning_fabric.utilities.seed.seed_everything` is unused, this function will do nothing.
    r   Nr   0)r   )r   r   r   r$   r   bool)r   r   r"   r"   r#   
reset_seedE   s
    r(   )	worker_idrankr   c           	      C   s   |dur|nt j}t }||  }td| d|  d|  tj|| |g}tj	|
d |d\}}t|j
dtjdd	  |j
dtjdtd
dg  }t	| dS )a-  The worker_init_fn that Lightning automatically adds to your dataloader if you previously set the seed with
    ``seed_everything(seed, workers=True)``.

    See also the PyTorch documentation on
    `randomness in DataLoaders <https://pytorch.org/docs/stable/notes/randomness.html#dataloader>`_.
    Nz1Initializing random number generators of process z worker z with base seed          )Zdtyper   l            )r	   r*   r   Zinitial_seedr   debugr   r   ZSeedSequencer   Zgenerate_statespawnr    Zuint64Zastypeobjectsum)	r)   r*   Zglobal_rankZprocess_seedZ	base_seedssZtorch_ssZ	stdlib_ssZstdlib_seedr"   r"   r#   pl_worker_init_functionQ   s    "r3   c                   C   s    t  t j tj t dS )z\Collect the global random state of :mod:`torch`, :mod:`torch.cuda`, :mod:`numpy` and Python.)r   
torch.cudanumpypython)r   Zget_rng_stater!   Zget_rng_state_allr   r   Z	get_statepython_get_rng_stater"   r"   r"   r#   _collect_rng_statesk   s
    r8   )rng_state_dictr   c                 C   sZ   t | d  d| v r&t j| d  tj| d  | d \}}}t|t||f dS )zsSet the global random state of :mod:`torch`, :mod:`torch.cuda`, :mod:`numpy` and Python in the current
    process.r   r4   r5   r6   N)	r   Zset_rng_stater!   Zset_rng_state_allr   r   Z	set_statepython_set_rng_statetuple)r9   versionstategaussr"   r"   r#   _set_rng_statesu   s    r?   )NF)N)%loggingr   r   r   r7   r   r:   typingr   r   r   r5   r   r   Z$lightning_fabric.utilities.rank_zeror   r   r	   r
   	getLogger__name__r   ZiinfoZuint32maxr   minr   r   r'   r$   r   r(   r3   r   r8   r?   r"   r"   r"   r#   <module>   s"   
.
