o
    پi9                     @   s  d Z 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Zddlm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m  mZ ejZejZdZd	Zd
ZdZdZe dZdZddeedidddeddiddgdddidgdddddZG dd dejZ G dd dej!Z"e
d ed!e#d"dfd#d$Z$e
d ed!e#d"dfd%d&Z%d'd( Z&d)d* Z'd+e(d,e(d-ed!e)d.ed/e*d0e*d1ed"dfd2d3Z+G d4d5 d5eZ,d6e#d"e,fd7d8Z-e-e.Z/dZd9d:Z0dZd;e#d<e#dB fd=d>Z1d?d@ Z2d[dBe#fdCdDZ3e
ddEd"e(fdFdGZ4ej5fdHe6e# d,e(fdIdJZ7dKdL Z8ejdMdN Z9G dOdP dPZ:e		d\d ejdQe#dRe(dB dSe(dB fdTdUZ;d ejdVe(dWe<d"dfdXdYZ=dS )]z0Logging configuration for sglang.multimodal_gen.    N)contextmanager)	lru_cachepartial)Logger)
MethodType)Anycastz[1;36mz[91mz[92mz[93mz[0;0mz@%(levelname)s %(asctime)s [%(filename)s: %(lineno)d] %(message)s%m-%d %H:%M:%Ssgl_diffusionzBsglang.multimodal_gen.runtime.utils.logging_utils.ColoredFormatter)classdatefmtformatzlogging.StreamHandlerzext://sys.stdout)r   	formatterlevelstreamWARNINGF)handlersr   	propagateDEBUG)r   r      )
formattersr   loggersrootversiondisable_existing_loggersc                       s<   e Zd ZdZejeejeiZ	dej
def fddZ  ZS )ColoredFormatterz2A logging formatter that adds color to log levels.recordreturnc                    s2   t  |}| j|j}|r| | t }|S )zAdds color to the log)superr   LEVEL_COLORSgetlevelnoRESET)selfr   formatted_messagecolor	__class__ e/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/runtime/utils/logging_utils.pyr   P   s
   zColoredFormatter.format)__name__
__module____qualname____doc__loggingERRORREDr   YELLOWr   	LogRecordstrr   __classcell__r(   r(   r&   r)   r   H   s     r   c                       s    e Zd ZdZ fddZ  ZS )SortedHelpFormatterzASortedHelpFormatter that sorts arguments by their option strings.c                    s    t |dd d}t | d S )Nc                 S   s   | j S N)option_strings)xr(   r(   r)   <lambda>`   s    z3SortedHelpFormatter.add_arguments.<locals>.<lambda>)key)sortedr   add_arguments)r#   actionsr&   r(   r)   r<   _   s   z!SortedHelpFormatter.add_arguments)r*   r+   r,   r-   r<   r4   r(   r(   r&   r)   r5   \   s    r5   loggermsgr   c                 C      | j |dd d S N   )
stacklevel)infor>   r?   r(   r(   r)   _print_info_onced      rF   c                 C   r@   rA   )warningrE   r(   r(   r)   _print_warning_oncej   rG   rI   c               	   C   :   zt tjd } W | dkS  ttfy   d} Y | dkS w )NRANKr   intosenvironKeyError
ValueErrorrankr(   r(   r)   get_is_main_processp      rT   c               	   C   rJ   )N
LOCAL_RANKr   rL   rR   r(   r(   r)   get_is_local_main_processx   rU   rW   server_log_levelr   logger_selfargsmain_process_onlylocal_main_process_onlykwargsc                O   s   t  }t }	| r| p|o|p|o|	p| tjk}
|
r?d|v r.|j||g|R i | dS |j||g|R ddi| dS dS )zJHelper function to log a message if the process rank matches the criteria.rC      N)rT   rW   r.   r   log)rX   r   rY   r?   r[   r\   rZ   r]   is_main_processis_local_main_process
should_logr(   r(   r)   _log_process_aware   s    "rc   c                   @   s   e Zd ZdZdeddfddZdeddfddZd	d	d
dedede	de	deddfddZ
d	d	d
dedede	de	deddfddZdd	d
dedede	de	deddfddZdd	d
dedede	de	deddfddZdS )_SGLDiffusionLoggera  
    Note:
        This class is just to provide type information.
        We actually patch the methods directly on the :class:`logging.Logger`
        instance to avoid conflicting with other libraries such as
        `intel_extension_for_pytorch.utils._logger`.
    r?   r   Nc                 C      t | | dS )zk
        As :meth:`info`, but subsequent calls with the same message
        are silently dropped.
        N)rF   r#   r?   r(   r(   r)   	info_once      z_SGLDiffusionLogger.info_oncec                 C   re   )zn
        As :meth:`warning`, but subsequent calls with the same message
        are silently dropped.
        N)rI   rf   r(   r(   r)   warning_once   rh   z _SGLDiffusionLogger.warning_onceTr[   r\   rZ   r[   r\   r]   c                O      d S r6   r(   r#   r?   r[   r\   rZ   r]   r(   r(   r)   rD         z_SGLDiffusionLogger.infoc                O   rk   r6   r(   rl   r(   r(   r)   debug   rm   z_SGLDiffusionLogger.debugFc                O   rk   r6   r(   rl   r(   r(   r)   rH      rm   z_SGLDiffusionLogger.warningc                O   rk   r6   r(   rl   r(   r(   r)   error   rm   z_SGLDiffusionLogger.error)r*   r+   r,   r-   r3   rg   ri   objectr   boolrD   rn   rH   ro   r(   r(   r(   r)   rd      s    


rd   namec                    s   t | }|  t|dtt| t|dtt| dtdtdtf fdd}t|dt|t j	d	d	| t|d
t|t j
d	d	| t|dt|t jdd	| t|dt|t jdd| tt|S )zThe main purpose of this function is to ensure that loggers are
    retrieved in such a way that we can be sure the root sgl_diffusion logger has
    already been configured.rg   ri   r   main_process_only_defaultlocal_main_process_only_defaultc                    s8   ||ddt dtdtdtdtdtdd f fd	d
}|S )Nrj   r#   r?   rZ   r[   r\   r]   r   c                   s&   t  | |g|R ||d| d S )Nrj   )rc   rl   )r   rX   r(   r)   _method   s   
z<init_logger.<locals>._create_patched_method.<locals>._method)r   rp   r   rq   )r   rs   rt   ru   rX   )r   r)   _create_patched_method   s$   	z+init_logger.<locals>._create_patched_methodrD   Trn   rH   Fro   )r.   	getLoggergetEffectiveLevelsetattrr   rF   rI   rM   rq   INFOr   r   r/   r   rd   )rr   r>   rw   r(   rv   r)   init_logger   s@   

r|   c                 C   s.  |dv r|j j}|j}|j j}||sd S zq|j}|d ur*|j j}	|j}
|j j}nd}	d}
d}t| dI}tj 	d}|dkr]|
| d| d| d	| d
| d|	 d	|
 d n|
| d| d| d	| d| d|	 d	|
 d W d    n1 sw   Y  W n	 ty   Y nw tt| |S )N)callr    r   az%Y-%m-%d %H:%M:%S.%fr}   z	 Call to z in :z from 
z Return from z to )f_codeco_filenamef_linenoco_name
startswithf_backopendatetimenowstrftimewrite	NameErrorr   _trace_calls)log_pathroot_dirframeeventargfilenamelineno	func_name
last_framelast_filenamelast_linenolast_func_nameftsr(   r(   r)   r     sn   

r   log_file_pathr   c                 C   sH   t d t d|  |du rtjtjt}tt	t
| | dS )a  
    Enable tracing of every function call in code under `root_dir`.
    This is useful for debugging hangs or crashes.
    `log_file_path` is the path to the log file.
    `root_dir` is the root directory of the code to trace. If None, it is the
    sgl_diffusion root directory.

    Note that this call is thread-level, any threads calling this function
    will have the trace enabled. Other threads will not be affected.
    zSGLANG_DIFFUSION_TRACE_FUNCTION is enabled. It will record every function executed by Python. This will slow down the code. It is suggested to be used for debugging hang or crashes only.zTrace frame log is saved to %sN)r>   rH   rD   rN   pathdirname__file__syssettracer   r   )r   r   r(   r(   r)   enable_trace_function_callG  s   r   c                  C   sP   ddl m}  d| d d d< d| d d d< d	| d d
 d< d| d d
 d< d S )Nr   LOGGING_CONFIGz)[%(asctime)s] %(levelprefix)s %(message)sr   defaultfmtz%Y-%m-%d %H:%M:%Sr   zR[%(asctime)s] %(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)saccess)uvicorn.configr   r   r(   r(   r)   set_uvicorn_logging_configs^  s   

r   r~   prefixc                 C   sn   d| d}d}t ||d}ttj}|| t }|j  |	| |
tt| j  t  d S )Nz[%(asctime)sz] %(message)sr	   )r   )r   r.   StreamHandlerr   stdoutsetFormatterrx   r   clear
addHandlersetLevelgetattr	log_levelupperr   )server_argsr   
log_formatr   r   handlerr   r(   r(   r)   configure_loggerk  s   



r   )maxsizec                  C   s   t  } | jS r6   )r.   rx   r   )r   r(   r(   r)   get_log_level{  s   r   loggers_to_suppressc                 C   s0   i }| D ]}t |}|j||< || q|S r6   )r.   rx   r   r   )r   r   original_levelslogger_namer>   r(   r(   r)   suppress_loggers  s   

r   c                  C   s(   g d} | D ]}t |t j qd S )N)imageioimageio_ffmpegPIL	PIL_Imagezpython_multipart.multipartfilelockurllib3)r.   rx   r   r/   )target_namesrr   r(   r(   r)   globally_suppress_loggers  s   
r   c               
   c   s    t j } t| }ttjtj}z&t j  t	||  dV  W t j  t	||  t
| t
| dS t j  t	||  t
| t
| w )a;  
    Suppress stdout from C libraries at the file descriptor level.

    Only suppresses stdout, not stderr, to preserve error messages.
    Example:
        with suppress_stdout():
            # C library calls that would normally print to stdout
            torch.distributed.new_group(ranks, backend="gloo")
    N)r   r   filenorN   dupr   devnullO_WRONLYflushdup2close)	stdout_fd
stdout_dup
devnull_fdr(   r(   r)   suppress_stdout  s    






r   c                   @   s   e Zd Zdd ZdS )GenerationTimerc                 C   s   d| _ d| _d| _d S )Ng        )
start_timeend_timeduration)r#   r(   r(   r)   __init__  s   
zGenerationTimer.__init__N)r*   r+   r,   r   r(   r(   r(   r)   r     s    r   promptrequest_idxtotal_requestsc              
   c   s    |d ur|d ur|  d|||d d  t }t |_z |V  t |_|j|j |_|  dt dt d|j W d S  t	yb } z|d urS| j
d||dd  | j
d	| dd  d }~ww )
NzProcessing prompt %d/%d: %sd   z%Pixel data generated successfully in %.2f secondsz+Failed to generate output for prompt %d: %sT)exc_infoz&Failed to generate output for prompt: )rD   r   timeperf_counterr   r   r   GREENr"   	Exceptionro   )r>   r   r   r   timerer(   r(   r)   log_generation_timer  sD   



r   num_outputs
total_timec                 C   s    |  dt dt d|| d S )Nz4Completed batch processing. Generated %d outputs in r   r   )rD   r   r"   )r>   r   r   r(   r(   r)   log_batch_completion  s
   r   r6   )r~   )NN)>r-   argparse
contextlibr   r.   rN   r   r   r   	functoolsr   r   r   typesr   typingr   r   sglang.multimodal_gen.envsmultimodal_genenvsSGLANG_DIFFUSION_LOGGING_LEVELSGLANG_DIFFUSION_LOGGING_PREFIXCYANr0   r   r1   r"   _FORMAT_DATE_FORMATDEFAULT_LOGGING_CONFIG	Formatterr   HelpFormatterr5   r3   rF   rI   rT   rW   rM   rp   rq   rc   rd   r|   r*   r>   r   r   r   r   r   r   listr   r   r   r   r   floatr   r(   r(   r(   r)   <module>   s   	 	
<?
+
(