o
    ¥ôj‘  ã                   @   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de
e ded	efd
d„Zddd„Zddede
e d	dfdd„Z dded	e	e!ef fdd„Z"de	e!ef d	df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 r6t j d¡}|du rd} td| › �ƒ n'zt|ƒ} W n  ty5   d} tdt|ƒ› d| › �ƒ Y n
w t| tƒs?t| ƒ} t|   krIt	ksXn t| › dt› dt	› �ƒ d} t
 td	| › �tƒ ƒ¡ t| ƒt jd< t | ¡ tj | ¡ t | ¡ t|ƒ› t jd
< | S )aÅ  Function that sets the seed for pseudo-random number generators in: torch, numpy, and Python's random module.
    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``, it will read the seed from ``PL_GLOBAL_SEED`` env variable. If ``None`` and the
            ``PL_GLOBAL_SEED`` env variable is not set, then the seed defaults to 0.
        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_SEEDr   z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Seed set to ÚPL_SEED_WORKERS)ÚosÚenvironÚgetr
   ÚintÚ
ValueErrorÚreprÚ
isinstanceÚmin_seed_valueÚmax_seed_valueÚlogÚinfor   r   ÚstrÚrandomr   ÚnpÚtorchÚmanual_seed)r   r   Zenv_seed© r    úr/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/lightning/fabric/utilities/seed.pyÚseed_everything   s.   þ


r"   c                  C   sD   t j dd¡} | du rdS t j dd¡}tt| ƒtt|ƒƒd� dS )zÚReset 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_seedA   s
   r%   Ú	worker_idÚrankc           	      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 é   é   é   )Údtyper   l            )r	   r'   r   Úinitial_seedr   Údebugr   r   ÚSeedSequencer   Úgenerate_stateÚspawnr   Úuint64ÚastypeÚobjectÚsum)	r&   r'   Úglobal_rankZprocess_seedÚ	base_seedÚssZtorch_ssZ	stdlib_ssZstdlib_seedr    r    r!   Úpl_worker_init_functionN   s   	ÿ"r8   TÚinclude_cudac                 C   s<   t  ¡ tj ¡ tƒ dœ}| rt j ¡ rt j ¡ ng |d< |S )z\Collect the global random state of :mod:`torch`, :mod:`torch.cuda`, :mod:`numpy` and Python.)r   ÚnumpyÚpythonú
torch.cuda)	r   Úget_rng_stater   r   Ú	get_stateÚpython_get_rng_stateÚcudaÚis_availableÚget_rng_state_all)r9   Ústatesr    r    r!   Ú_collect_rng_statesi   s   ýrD   Úrng_state_dictc                 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   r<   r:   r;   N)	r   Úset_rng_stater@   Úset_rng_state_allr   r   Ú	set_stateÚpython_set_rng_stateÚtuple)rE   ÚversionÚstateÚgaussr    r    r!   Ú_set_rng_statesu   s   rN   )NF)r   N)N)T)$Úloggingr   r   r   r?   r   rI   Útypingr   r   r   r:   r   r   Ú$lightning.fabric.utilities.rank_zeror   r   r	   r
   Ú	getLoggerÚ__name__r   ÚiinfoÚuint32Úmaxr   Úminr   r   r$   r"   r%   r8   r   rD   rN   r    r    r    r!   Ú<module>   s"    

.