
    g(                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZmZ d dl	m
Z
 d dlmZ d dlmZmZ d dlZd dlmZ  e j        e          ZdefdZd	efd
Zd ZddZddZded	ee         fdZd Zd Z d Z!d Z"d Z#e G d d                      Z$d	e$fdZ%d dedee&         d	dfdZ'e
d dedee&         d	dfd            Z(dS )!    N)	dataclassfield)	lru_cache)which)ListOptional)parsecurrent_envc                 H   g d}g }|                                  D ]\  t          fd|D                       rBt                    dk    r/t                    dk    r|                     d d           ct                              d d d           |S )a  
    Verifies that all keys and values in `current_env` do not contain illegal keys or values, and returns a list of
    strings as the result.

    Example:
    ```python
    >>> from accelerate.utils.environment import verify_env

    >>> env = {"ACCELERATE_DEBUG_MODE": "1", "BAD_ENV_NAME": "<mything", "OTHER_ENV": "2"}
    >>> valid_env_items = verify_env(env)
    >>> print(valid_env_items)
    ["ACCELERATE_DEBUG_MODE=1
", "OTHER_ENV=2
"]
    ```
    );
<> c              3   &   K   | ]}|z   vV  d S N ).0charkeyvalues     X/var/www/html/ai-engine/env/lib/python3.11/site-packages/accelerate/utils/environment.py	<genexpr>z0convert_dict_to_env_variables.<locals>.<genexpr>3   s,      EETtC%K(EEEEEE       =r   zWARNING: Skipping z7 as it contains forbidden characters or missing values.)itemsalllenappendloggerwarning)r
   forbidden_charsvalid_env_itemsr   r   s      @@r   convert_dict_to_env_variablesr%   !   s     100OO!'')) v v
UEEEEE_EEEEE 	v#c((VW--\_`e\f\fjk\k\k""c#5#5E#5#5#56666NNtttetttuuuur   returnc                 f    |                                  } | dv rdS | dv rdS t          d|            )z
    Converts a string representation of truth to `True` (1) or `False` (0).

    True values are `y`, `yes`, `t`, `true`, `on`, and `1`; False value are `n`, `no`, `f`, `false`, `off`, and `0`;
    )yyesttrueon1r   )nnoffalseoff0r   zinvalid truth value )lower
ValueError)r   s    r   str_to_boolr6   :   sJ     KKMME444q	7	7	7q777888r   c                 ~    | D ]9}t          t          j                            |d                    }|dk    r|c S :|S )zQReturns the first positive env value found in the `env_keys` list or the default.r   )intosenvironget)env_keysdefaultevals       r   get_int_from_envrA   I   sJ      "*..B''((!88JJJ Nr   Fc                     t           j                            | t          |                    }t	          |          dk    S )zJReturns truthy value for `key` from the env if available else the default.r   )r:   r;   r<   strr6   r   r>   r   s      r   parse_flag_from_envrE   R   s0    JNN3G--Eu""r   r/   c                 `    t           j                            | t          |                    }|S r   )r:   r;   r<   rC   rD   s      r   parse_choice_from_envrG   X   s"    JNN3G--ELr   library_namesc                      d | D             S )zk
    Checks if any of `library_names` are imported in the environment. Will return any names that are.
    c                 R    g | ]$}|t           j                                        v "|%S r   )sysmoduleskeys)r   lib_names     r   
<listcomp>z-are_libraries_initialized.<locals>.<listcomp>a   s0    UUUh#+BRBRBTBT6T6TH6T6T6Tr   r   )rH   s    r   are_libraries_initializedrP   ]   s     VU]UUUUr   c                      t          j                    dk    r't          d          } | t          j        d          d} nd} | S )zC
    Returns the right nvidia-smi command based on the system.
    Windowsz
nvidia-smiNsystemdrivez6\Program Files\NVIDIA Corporation\NVSMI\nvidia-smi.exe)platformsystemr   r:   r;   )commands    r   _nvidia_smirW   d   sK     I%% %%?M2nnnGNr   c                      t          j        t                      ddgd          } |                                 } |                     t
          j                  }t          |          }d |D             }||fS )z
    Gets GPU count and names using `nvidia-smi` instead of torch to not initialize CUDA.

    Largely based on the `gputil` library.
    z--query-gpu=count,name--format=csv,noheaderTuniversal_newlinesc                 h    g | ]/}|                     d           d                                         0S ),r   )splitstrip)r   gpus     r   rO   z get_gpu_info.<locals>.<listcomp>   s3    ;;;s3"((**;;;r   )
subprocesscheck_outputrW   r_   r^   r:   linesepr   )outputgpus	gpu_count	gpu_namess       r   get_gpu_inforh   s   sy     $	02IJ_c  F \\^^F<<
##DD		I;;d;;;Iir   c                      t          j        t                      ddgd          } |                                 } |                     t
          j                  d         S )z^
    Returns the driver version

    In the case of multiple GPUs, will return the first.
    z--query-gpu=driver_versionrY   TrZ   r   )ra   rb   rW   r_   r^   r:   rc   )rd   s    r   get_driver_versionrj      sV     $	46MNcg  F \\^^F<<
##A&&r   c                      	 t                      \  } }dh|dk    rOt          fd| D                       r4d}t                      }t          |          t          |          k     rdS dS n# t          $ r Y nw xY wdS )z
    Checks if the devices being used have issues with P2P and IB communications, namely any consumer GPU hardware after
    the 3090.

    Noteably uses `nvidia-smi` instead of torch to not initialize CUDA.
    zRTX 40r   c              3   *   K   | ]}D ]}||v V  	d S r   r   )r   device_nameunsupported_deviceunsupported_devicess      r   r   z,check_cuda_p2p_ib_support.<locals>.<genexpr>   sS        *=  ' #k1      r   z	550.40.07FT)rh   anyrj   r	   	Exception)device_namesdevice_countacceptable_driver_versioncurrent_driver_versionro   s       @r   check_cuda_p2p_ib_supportrv      s    %1^^"l'j!    #/     
 -8));)=)=&/0059R3S3SSS 5t   4s   A%A- -
A:9A:c                  J    t           j                                        } | dk    S )zs
    Checks if all the current GPUs available support FP8.

    Notably must initialize `torch.cuda` to check.
    )   	   )torchcudaget_device_capability)cuda_device_capacitys    r   check_fp8_capabilityr~      s#     !:;;==6))r   c                       e Zd ZU dZ edddi          Zeed<    edddi          Zeed	<    eddd
i          Z	eed<    edddi          Z
eed<   dS )CPUInformationag  
    Stores information about the CPU in a distributed environment. It contains the following attributes:
    - rank: The rank of the current process.
    - world_size: The total number of processes in the world.
    - local_rank: The rank of the current process on the local node.
    - local_world_size: The total number of processes on the local node.
    r   helpz The rank of the current process.)r>   metadatarankr   z+The total number of processes in the world.
world_sizez2The rank of the current process on the local node.
local_rankz0The total number of processes on the local node.local_world_sizeN)__name__
__module____qualname____doc__r   r   r9   __annotations__r   r   r   r   r   r   r   r      s           a63U*VWWWD#WWWeA9f0ghhhJhhheA9m0noooJooo!E!v?q6rssscsssssr   r   c                      i } t          g dd          | d<   t          g dd          | d<   t          g dd          | d<   t          g d	d          | d
<   t          di | S )z
    Returns various information about the environment in relation to CPU distributed training as a `CPUInformation`
    dataclass.
    )RANKPMI_RANKOMPI_COMM_WORLD_RANKMV2_COMM_WORLD_RANKr   r   )
WORLD_SIZEPMI_SIZEOMPI_COMM_WORLD_SIZEMV2_COMM_WORLD_SIZEr   r   )
LOCAL_RANKMPI_LOCALRANKIDOMPI_COMM_WORLD_LOCAL_RANKMV2_COMM_WORLD_LOCAL_RANKr   )LOCAL_WORLD_SIZEMPI_LOCALNRANKSOMPI_COMM_WORLD_LOCAL_SIZEMV2_COMM_WORLD_LOCAL_SIZEr   r   )rA   r   )informations    r   get_cpu_distributed_informationr      s    
 K*+n+n+npqrrK 0QQQST! !K !1dddfg! !K '7jjj	' 'K"# ((K(((r   local_process_indexverbosec                    |t          dd          }t          j                                        r,ddlm}  |            st          d          ddl}|                                 t          j
        t          j                    dz            }|                    |           }d}|                    ||          D ]	}|d	| }
d
 |D             }|                                 d t!          |          D             }	t          j        d|	           |rHt          j        d          }
t&                              dt+          |
           d|  d|
            dS dS dS )a  
    Overrides whatever NUMA affinity is set for the current process. This is very taxing and requires recalculating the
    affinity to set, ideally you should use `utils.environment.set_numa_affinity` instead.

    Args:
        local_process_index (int):
            The index of the current process on the current server.
        verbose (bool, *optional*):
            Whether to log out the assignment of each CPU. If `ACCELERATE_DEBUG_MODE` is enabled, will default to True.
    NACCELERATE_DEBUG_MODEFr   )is_pynvml_availablez_To set CPU affinity on CUDA GPUs the `pynvml` package must be available. (`pip install pynvml`)@    064bc                 ,    g | ]}t          |          S r   )r9   )r   xs     r   rO   z*override_numa_affinity.<locals>.<listcomp>   s    999AQ999r   c                 $    g | ]\  }}|d k    |S )r   r   )r   ir?   s      r   rO   z*override_numa_affinity.<locals>.<listcomp>   s!    LLLAQ!VV1VVVr   z
Assigning z cpu cores to process z: )rE   rz   r{   is_availableaccelerate.utilsr   ImportErrorpynvmlnvmlInitmathceilr:   	cpu_countnvmlDeviceGetHandleByIndexnvmlDeviceGetCpuAffinityreverse	enumeratesched_setaffinitysched_getaffinityr!   infor   )r   r   r   nvmlnum_elementshandleaffinity_stringjaffinity_listaffinity_to_set	cpu_coress              r   override_numa_affinityr      s    %&=uEEz   o888888""$$ 	q   	 	y"!455001DEE..v|DD 	; 	;A!"::::OO99999LL=)A)ALLL
Q000 	o,Q//IKKmS^^mmK^mmbkmmnnnnn/o o*	o 	or   c                 (    t          | |           dS )a2  
    Assigns the current process to a specific NUMA node. Ideally most efficient when having at least 2 cpus per node.

    This result is cached between calls. If you want to override it, please use
    `accelerate.utils.environment.override_numa_afifnity`.

    Args:
        local_process_index (int):
            The index of the current process on the current server.
        verbose (bool, *optional*):
            Whether to print the new cpu cores assignment for each process. If `ACCELERATE_DEBUG_MODE` is enabled, will
            default to True.
    r   r   N)r   r   s     r   set_numa_affinityr     s     /BGTTTTTTr   )F)r/   r   ))loggingr   r:   rT   ra   rK   dataclassesr   r   	functoolsr   shutilr   typingr   r   rz   packaging.versionr	   	getLoggerr   r!   dictr%   r9   r6   rA   rE   rG   rC   rP   rW   rh   rj   rv   r~   r   r   boolr   r   r   r   r   <module>r      s     				      



 ( ( ( ( ( ( ( (             ! ! ! ! ! ! ! !  # # # # # # 
	8	$	$t    29# 9 9 9 9  # # # #   
Vc Vd3i V V V V       $
' 
' 
'  8* * * t t t t t t t t) ) ) ) )($o $o $ohtn $oX\ $o $o $o $oN U U3 U$ USW U U U U U Ur   