o
    ̳im                     @   s   U d dl Z d dlZd dlZd dlmZmZ d dlZd dlZd dl	m
Z
 d dlmZmZ e Ze jed< 	ddee deeeef  defd	d
ZdS )    N)OptionalUnion)_broadcast_tensor)
get_loggerget_world_size_and_rank_logseed
debug_modereturnc           	      C   sJ  t  \}}ttjj| d }ttjj}| du r)t||d}t|d	 } | |k s1| |kr?t
d|  d| d| d| | }|dkrUtd	| d
|  d|  t| tj| t| |durtd|  t| t }|dkrtd dtjj_dtjj_| S td dtjj_dtjj_dtjd< | S )a	  Function that sets seed for pseudo-random number generators across commonly used libraries.

    This seeds PyTorch, NumPy, and the python.random module. For distributed jobs, each local process
    sets its own seed, computed seed + rank.
    For more details, see https://pytorch.org/docs/stable/notes/randomness.html.

    Args:
        seed (Optional[int]): the integer value seed. If `None`, a random seed will be generated and set.
        debug_mode (Optional[Union[str, int]]): Controls debug_mode settings for deterministic operations within PyTorch.

            * If `None`, don't set any PyTorch global values.
            * If "default" or 0, don't error or warn on nondeterministic operations and additionally enable PyTorch CuDNN benchmark.
            * If "warn" or 1, warn on nondeterministic operations and disable PyTorch CuDNN benchmark.
            * If "error" or 2, error on nondeterministic operations and disable PyTorch CuDNN benchmark.
            * For more details, see :func:`torch.set_deterministic_debug_mode` and
              https://pytorch.org/docs/stable/notes/randomness.html#avoiding-nondeterministic-algorithms.

    Returns:
        int: the current seed

    Raises:
        ValueError: If the input seed value is outside the required range.
       N)r   r   zInvalid seed value provided: z. Value must be in the range [z, ]z"Setting manual seed to local seed z. Local seed is seed + rank = z + z$Setting deterministic debug mode to z"Disabling cuDNN deterministic modeFTz!Enabling cuDNN deterministic modez:4096:8CUBLAS_WORKSPACE_CONFIG)r   npiinfouint32maxmintorchrandintr   item
ValueErrorr   debugmanual_seedrandomr   set_deterministic_debug_modeget_deterministic_debug_modebackendscudnndeterministic	benchmarkosenviron)	r   r	   
world_sizerankmax_valmin_val	rand_seed
local_seeddeterministic_debug_mode r)   K/home/ubuntu/.local/lib/python3.10/site-packages/torchtune/training/seed.pyset_seed   s@   










r+   )NN)loggingr    r   typingr   r   numpyr   r   torchtune.training._distributedr   torchtune.utilsr   r   r   Logger__annotations__intstrr+   r)   r)   r)   r*   <module>   s"   
