o
    zi                     @   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
mZ d dlZd dlmZ d dlmZmZmZmZ e eZdZd Zd!d
ee d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edededede
e 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ListOptional)_NUMPY_AVAILABLE)	_get_rankrank_prefixed_messagerank_zero_onlyrank_zero_warn    FTseedworkersverbose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} |rft
td	|  t  t| t jd< t|  tr~ddl}|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`.
        verbose: Whether to print a message on each rank with the seed being set.

    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   r   numpytorchmanual_seed)r   r   r   env_seednp r&   S/home/ubuntu/.local/lib/python3.10/site-packages/lightning_fabric/utilities/seed.pyseed_everything   s4   


r(   c                  C   sF   t jdd} | du rdS t jdd}tt| tt|d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   0F)r   r   )r   r   r   r(   r   bool)r   r   r&   r&   r'   
reset_seedH   s
   r+   	worker_idrankc                 C   s   |dur|nt j}t }||  }td| d|  d|  t|| |dd}t|d  t	|d d	> |d
 B  t
rMddl}|j	|d d@  dS 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    )countr                r   )r   r-   r"   initial_seedr   debug_generate_seed_sequencer#   r    r   r   r!   )r,   r-   global_rankprocess_seed	base_seedseed_sequencer%   r&   r&   r'   pl_worker_init_functionU   s   	r;   r9   r7   r/   c                 C   sD   | d> |d> B |B }g }t |D ]}|d d d@ }|| q|S )zGenerates a sequence of seeds from a base seed, worker id and rank using the linear congruential generator (LCG)
    algorithm.r1      l   -*PB r0   l    )rangeappend)r9   r,   r7   r/   combined_seedseeds_r&   r&   r'   r6   n   s   r6   include_cudac                 C   sN   t  t d}trddl}|j |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"   pythonr   Nr!   
torch.cuda)
r"   get_rng_statepython_get_rng_stater   r!   r    	get_statecudais_availableget_rng_state_all)rB   statesr%   r&   r&   r'   _collect_rng_states{   s   rL   rng_state_dictc                 C   sn   t | d  d| v rt j| d  tr%d| v r%ddl}|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"   rD   r!   r   NrC   )
r"   set_rng_staterH   set_rng_state_allr   r!   r    	set_statepython_set_rng_statetuple)rM   r%   versionstategaussr&   r&   r'   _set_rng_states   s   rV   )NFT)r   N)N)T)"loggingr   r    r   rF   r   rQ   typingr   r   r   r   r"   "lightning_fabric.utilities.importsr   $lightning_fabric.utilities.rank_zeror	   r
   r   r   	getLogger__name__r   r   r   r   r*   r(   r+   r;   r6   r   rL   rV   r&   r&   r&   r'   <module>   s$    
 
4"