
    .`io#                    j   d dl m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Zd dlmZ d dlmZ d dlmZ d dlmZ d	d
lmZmZ  ee          ZdZdZd2dZej        d3d            Z ej        d             Z!d4dZ"d Z#d Z$dej%        fd5d"Z&d6d(Z'd7d8d+Z(d9d,Z)d:d;d/Z*d<d1Z+dS )=    )annotationsN)CallableIterator)Path)TextIO)init_logger)in_wsl)is_in_ray_actor   )cuda_is_initializedxpu_is_initializedz[0;36mz[0;0m	envs_dictdict[str, str]c                    |                                  D ]e\  }}|t          j        v rCt          j        |         |k    r-t                              d|t          j        |         |           |t          j        |<   fdS )z3Update multiple environment variables with logging.z5Overwriting environment variable %s from '%s' to '%s'N)itemsosenvironloggerwarning)r   kvs      k/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/utils/system_utils.pyupdate_environment_variablesr   !   s{    !!  1
??rz!}11NNG
1	   
1     keystrvaluereturnIterator[None]c              #  D  K   t           j                            |           }|t           j        | <   	 dV  |"t           j                            | d           dS |t           j        | <   dS # |!t           j                            | d           n|t           j        | <   w xY w)z(Temporarily set an environment variable.N)r   r   getpop)r   r   olds      r   set_env_varr$   .   s       *..

CBJsO";JNN3%%%%%!BJsOOO ;JNN3%%%%!BJsO!!!!s   A+ +4Bc               #    K   t           j        dk    rdV  dS t          j                                        } t          j        |           }t          j        t
          j        t
          j	                  }	 t          j        
                                 t          j        ||            dV  t          j        
                                 t          j        ||            t          j        |           t          j        |           dS # t          j        
                                 t          j        ||            t          j        |           t          j        |           w xY w)a  
    Suppress stdout from C libraries at the file descriptor level.

    Only suppresses stdout, not stderr, to preserve error messages.
    Suppression is disabled when VLLM_LOGGING_LEVEL is set to DEBUG.

    Example:
        with suppress_stdout():
            # C library calls that would normally print to stdout
            torch.distributed.new_group(ranks, backend="gloo")
    DEBUGN)envsVLLM_LOGGING_LEVELsysstdoutfilenor   dupopendevnullO_WRONLYflushdup2close)	stdout_fd
stdout_dup
devnull_fds      r   suppress_stdoutr6   <   s*      '))
!!##I	""JR[11J


I&&&


I&&&

 	


I&&&

s   57D	 	AE&fnCallable[[int], Path]r   c                V    d}	  | |          }|                                 s|S |dz  }')zGenerate a unique file path by trying incrementing integers.

    Note: This function has a TOCTOU race condition.
    Caller should use atomic operations (e.g., open with 'x' mode)
    when creating the file to ensure thread safety.
    r   Tr   )exists)r7   ips      r   unique_filepathr=   `   s>     	
ABqEExxzz 	H	Q	r   c                 D   t           j                            d          dk    rdS g } t                      r?ddl}|                                j        t           j        d<   |                     d           t                      r|                     d           n#t                      r|                     d           t                      r|                     d	           | r?t                              d
d                    |                      dt           j        d<   dS dS )zWCheck if we need to force the use of the `spawn` multiprocessing start
    method.
    VLLM_WORKER_MULTIPROC_METHODspawnNr   RAY_ADDRESSz&In a Ray actor and can only be spawnedzCUDA is initializedzXPU is initializedz4WSL is detected and NVML is not compatible with forkzWe must use the `spawn` multiprocessing start method. Overriding VLLM_WORKER_MULTIPROC_METHOD to 'spawn'. See https://docs.vllm.ai/en/latest/usage/troubleshooting.html#python-multiprocessing for more information. Reasons: %sz; )r   r   r!   r
   rayget_runtime_contextgcs_addressappendr   r   r	   r   r   join)reasonsrB   s     r   _maybe_force_spawnrH   r   s(    
z~~455@@G A 	


$'$;$;$=$=$I
=!?@@@ -,----			 -+,,,xx OMNNN 	=0
 IIg	
 	
 	
 6=
1222	= 	=r   c                 ^    t                       t          j        } t          j        |           S )aJ  Get a multiprocessing context with a particular method (spawn or fork).
    By default we follow the value of the VLLM_WORKER_MULTIPROC_METHOD to
    determine the multiprocessing method (default is fork). However, under
    certain conditions, we may enforce spawn and override the value of
    VLLM_WORKER_MULTIPROC_METHOD.
    )rH   r'   r?   multiprocessingget_context)	mp_methods    r   get_mp_contextrM      s)     1I&y111r    namesuffixprefixNonec                z    	 ddl }n# t          $ r Y dS w xY w|r|  d| } |                     | d|             dS )z3Set the current process title with optional suffix.r   N_z::)setproctitleImportError)rO   rP   rQ   rU   s       r   set_process_titlerW      s}         "!!!!1141122222s    
filer   worker_namepidintc                     t           j        r
d| d| dnt           d| d| dt           d j        d fd}d	 _        | _        d
S )z5Add colored prefix to file output for log decoration.(z pid=z) ) sr   c                ^   | sd S j         r            d}|                     d|          x}dk    r]|dz  } | ||                    |t          |           k    r	d_         d S             |}|                     d|          x}dk    ] | |d                     d_         d S )Nr   
r   TF)start_new_linefindlen)r`   idxnext_idxrX   
file_writerQ   s      r   write_with_prefixz&_add_prefix.<locals>.write_with_prefix   s     	F 	Jv66$,,,x33MHJqX'''3q66!!&*#JvC 66$,,,x33 	
1STT7#r   TN)r`   r   )r'   NO_COLORCYANRESETwriterd   )rX   rY   rZ   rj   ri   rQ   s   `   @@r   _add_prefixro      s    } <.[..s...;;;;;S;;5;;;J$ $ $ $ $ $ $ $" D"DJJJr   process_name
str | Nonec                   t           j        sdS | %t                                                      j        } t          j                    }t          t          j	        | |           t          t          j
        | |           dS )z8Decorate stdout/stderr with process name and PID prefix.N)r'   VLLM_CONFIGURE_LOGGINGrM   current_processrO   r   getpidro   r)   r*   stderr)rp   rZ   s     r   decorate_logsrw      sm     & %''7799>
)++C
L#...
L#.....r   c                   	 t          j        |           }n# t           j        $ r Y dS w xY w|                    d          }|D ]W}t	          j        t                    5  t          j        |j	        t          j                   ddd           n# 1 swxY w Y   Xt	          j        t                    5  t          j        | t          j                   ddd           dS # 1 swxY w Y   dS )z
    Kills all descendant processes of the given pid by sending SIGKILL.

    Args:
        pid (int): Process ID of the parent process
    NT)	recursive)psutilProcessNoSuchProcesschildren
contextlibsuppressProcessLookupErrorr   killrZ   signalSIGKILL)rZ   parentr}   childs       r   kill_process_treer      sm   $$    ..H  / / !344 	/ 	/GEIv~...	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 
	/	0	0 % %
V^$$$% % % % % % % % % % % % % % % % % %s0    ** %BB	B	6 C##C'*C'  target_soft_limitc                l   t           j                            d          rt                              d           d S dd l}|j        }|                    |          \  }}|| k     rO	 |                    || |f           d S # t          $ r'}t          
                    d||           Y d }~d S d }~ww xY wd S )Nwinz-Windows detected, skipping ulimit adjustment.r   zFound ulimit of %s and failed to automatically increase with error %s. This can cause fd limit errors like `OSError: [Errno 24] Too many open files`. Consider increasing with ulimit -n)r)   platform
startswithr   inforesourceRLIMIT_NOFILE	getrlimit	setrlimit
ValueErrorr   )r   r   resource_typecurrent_softcurrent_hardes         r   
set_ulimitr      s    
|u%% CDDDOOO*M!)!3!3M!B!BL,'''
	}/@,.OPPPPP 	 	 	NN,         	 ('s   &B   
B1
B,,B1lib_namec                   d}t          d          5 }|D ]
}| |v r|} nddd           n# 1 swxY w Y   |dS |                    d          }||d                                         }|                    d          d         }|                    d          d                             |           sJ d| d|              |S )	a)  
    According to according to https://man7.org/linux/man-pages/man5/proc_pid_maps.5.html,
    the file `/proc/self/maps` contains the memory maps of the process, which includes the
    shared libraries loaded by the process. We can use this file to find the path of the
    a loaded library.
    Nz/proc/self/maps/rc   z.sor   zUnexpected filename: z for library )r-   indexstripsplit
rpartitionr   )r   
found_lineflinestartpathfilenames          r   find_loaded_libraryr     s8    J		 	  A 	 	D4!
                
 t S!!Eeff##%%Dzz#r"Hu%%a(33H==  AAAxAA = Ks   ,00)r   r   )r   r   r   r   r   r   )r7   r8   r   r   )rO   r   rP   r   rQ   r   r   rR   )rX   r   rY   r   rZ   r[   r   rR   )N)rp   rq   r   rR   )rZ   r[   )r   )r   r[   )r   r   r   rq   ),
__future__r   r~   rJ   r   r   r)   collections.abcr   r   pathlibr   typingr   rz   	vllm.envsr'   vllm.loggerr   vllm.platforms.interfacer	   vllm.ray.lazy_utilsr
   platform_utilsr   r   __name__r   rl   rm   r   contextmanagerr$   r6   r=   rH   rM   VLLM_PROCESS_NAME_PREFIXrW   ro   rw   r   r   r    r   r   <module>r      s1   # " " " " "         				  



 . . . . . . . .                    # # # # # # + + + + + + / / / / / / C C C C C C C C	X		
 
 
 
 
" 
" 
" 
"   F   $"= "= "=J	2 	2 	2 /3 3 3 3 3"# # # #:/ / / / /% % % %:    0     r   