o
    پi.                     @   s^  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	 d dl
mZ d dlmZmZmZ d dlZd dlmZ d dlZd dlm  mZ d dlmZ d dlmZmZmZmZmZ eeZ e j!G d	d
 d
Z"e j!G dd dZ#de$fddZ%e	ddde$fddZ&de"fddZ'e j!G dd dZ(G dd dZ)G dd dZ*dS )    N)datetime)	lru_cache)Path)AnyDictOptional)UTC)current_platform)CYANRESET_SGLDiffusionLoggerget_is_main_processinit_loggerc                   @   sD   e Zd ZU eed< eed< eed< eed< deeef fddZdS )	MemorySnapshotallocated_mbreserved_mbpeak_allocated_mbpeak_reserved_mbreturnc                 C   s.   t | jdt | jdt | jdt | jddS )N   r   r   r   r   )roundr   r   r   r   self r   c/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/runtime/utils/perf_logger.pyto_dict&   s
   



zMemorySnapshot.to_dictN)	__name__
__module____qualname__float__annotations__r   strr   r   r   r   r   r   r      s   
 r   c                   @   s|   e Zd ZdZdefddZedefddZded	efd
dZ	de
d	efddZdedefddZdeeef fddZdS )RequestMetricszQPerformance metrics for a single request, including timings and memory snapshots.
request_idc                 C   s"   || _ i | _g | _d| _i | _d S N        r$   stagesstepstotal_duration_msmemory_snapshots)r   r$   r   r   r   __init__3   s
   
zRequestMetrics.__init__r   c                 C   s
   | j d S )Ng     @@)r*   r   r   r   r   total_duration_s;   s   
zRequestMetrics.total_duration_s
stage_name
duration_sc                 C   s   |d | j |< dS )z(Records the duration of a pipeline stage  N)r(   )r   r.   r/   r   r   r   record_stage?   s   zRequestMetrics.record_stageindexc                 C   s&   |t | jks	J | j|d  dS )z(Records the duration of a denoising stepr0   N)lenr)   append)r   r2   r/   r   r   r   record_stepsC   s   zRequestMetrics.record_stepscheckpoint_namesnapshotc                 C   s   || j |< d S N)r+   )r   r6   r7   r   r   r   record_memory_snapshotH   s   z%RequestMetrics.record_memory_snapshotc                 C   s(   | j | j| j| jdd | j D dS )z,Serializes the metrics data to a dictionary.c                 S      i | ]	\}}||  qS r   r   .0namer7   r   r   r   
<dictcomp>R       z*RequestMetrics.to_dict.<locals>.<dictcomp>r'   )r$   r(   r)   r*   r+   itemsr   r   r   r   r   K   s   zRequestMetrics.to_dictN)r   r   r   __doc__r"   r,   propertyr    r-   r1   intr5   r   r9   r   r   r   r   r   r   r   r#   /   s    r#   r   c                  C   sL   t jd} | rt j| S | du r$ttj }|j	d  }t
|S dS )z8
    Determines the directory for performance logs.
    SGLANG_PERF_LOG_DIRNz../../.cache/logs )osenvirongetpathabspathr   sglang__file__resolveparentr"   )log_dirsglang_pathtarget_pathr   r   r   get_diffusion_perf_log_dirY   s   rS      )maxsizec               	   C   sX   zt jd} | stjg dtjd d} | }| W S  tjt	fy+   d}Y dS w )NSGLANG_GIT_COMMIT)gitz	rev-parseHEAD)stderrutf-8zN/A)
rG   rH   rI   
subprocesscheck_outputDEVNULLstripdecodeCalledProcessErrorFileNotFoundError)commit_hash_CACHED_COMMIT_HASHr   r   r   get_git_commit_hashg   s   
rd   c                  C   sb   t j stdddddS t j } t j }t j }t j }t| d |d |d |d dS )Nr&   r   i   )torchcudais_availabler   memory_allocatedmemory_reservedmax_memory_allocatedmax_memory_reserved)	allocatedreservedpeak_allocatedpeak_reservedr   r   r   capture_memory_snapshotz   s"   




rp   c                   @   sx   e Zd ZU eed< eed< eed< eed< ee ed< ee ed< eed< ej	edZ
eeef ed	< 	
	
dddZd
S )RequestPerfRecordr$   	timestamprb   tagr(   r)   r*   )default_factoryr+   Nc	           	      C   sR   || _ |d ur|| _ntt | _|| _|| _|| _|| _	|| _
|p%i | _d S r8   )r$   rr   r   nowr   	isoformatrb   rs   r(   r)   r*   r+   )	r   r$   rb   rs   r(   r)   r*   r+   rr   r   r   r   r,      s   zRequestPerfRecord.__init__)NN)r   r   r   r"   r!   listdictr    dataclassesfieldr+   r,   r   r   r   r   rq      s   
 
rq   c                   @   sN   e Zd ZdZ			ddededed deded	efd
dZdd Z	dd Z
dS )StageProfilerz
    A unified context manager, records performance metrics (usually of a single Stage or a step) into a provided RequestMetrics object (usually from a Req).
    Fr.   loggermetricsr#   log_stage_start_endperf_dump_path_providedcapture_memoryc                 C   s4   || _ || _|| _d| _|ptj| _|| _|| _d S r%   )	r.   r}   r|   
start_timeenvsSGLANG_DIFFUSION_STAGE_LOGGING
log_timingr~   r   )r   r.   r|   r}   r~   r   r   r   r   r   r,      s   	
zStageProfiler.__init__c                 C   s   | j r$d| j d}| jtjr|dtt d d7 }| j	| | j
r*| js-| j rKtjdddkrF| jd	rFtj rFtj  t | _| S )
N[z] started...z (r   z	 GB left)%SGLANG_DIFFUSION_SYNC_STAGE_PROFILING01denoising_step_)r~   r.   r|   isEnabledForloggingDEBUGr   r	   get_available_gpu_memoryinfor   r}   rG   rH   rI   
startswithre   rf   rg   synchronizetimeperf_counterr   )r   msgr   r   r   	__enter__   s   


zStageProfiler.__enter__c                 C   s  | j r| js| jsdS tjdddkr$| jdr$tj	
 r$tj	  t | j }|r=| jjd| j|d |dd	 dS | jrO| jd
| j d|dd | j r| jrd| jv rmt| jtdd  }| j|| n| j| j| | jrtj	
 rt }| jd| j | dS )NFr   r   r   r   z-[%s] Error during execution after %.4f ms: %sr0   T)exc_infor   z] finished in z.4fz secondsafter_)r   r}   r~   rG   rH   rI   r.   r   re   rf   rg   r   r   r   r   r|   errorr   rD   r3   r5   r1   r   rp   r9   )r   exc_typeexc_valexc_tbexecution_time_sr2   r7   r   r   r   __exit__   sB   


zStageProfiler.__exit__N)FFF)r   r   r   rB   r"   r   r   boolr,   r   r   r   r   r   r   r{      s(    	
r{   c                   @   sZ   e Zd ZdZe		ddedddeeeef  defd	d
Z	e	ddddefddZ
dS )PerformanceLoggera	  
    A global utility class for logging performance metrics for all request, categorized by request-id.

    Serves both as a runtime logger (stream to file) and a dump utility.

    Notice that RequestMetrics stores the performance metrics of a single request
    Nbenchmark_dump	file_pathr}   r#   metars   c              
   C   s$  dd |j  D }dd t|jD }dd |j D }tt |j	t
 ||j||||p1i d	}z?tj|}	tjtj|	dd t|	d	d
d}
tj||
dd W d   n1 s`w   Y  tdt |	 t  W dS  ty } ztd|	 d|  W Y d}~dS d}~ww )z
        Static method to dump a standardized benchmark report to a file.
        Eliminates duplicate logic in CLI/Client code.
        c                 S      g | ]	\}}||d qS ))r>   duration_msr   r=   r>   r   r   r   r   
<listcomp>  r@   z;PerformanceLogger.dump_benchmark_report.<locals>.<listcomp>c                 S   r   ))stepr   r   )r=   idxr   r   r   r   r   $  r@   c                 S   r:   r   r;   r<   r   r   r   r?   )  r@   z;PerformanceLogger.dump_benchmark_report.<locals>.<dictcomp>)	rr   r$   rb   rs   r*   r)   denoise_steps_msmemory_checkpointsr   Texist_okwrZ   encodingr   )indentNzMetrics dumped to: zFailed to dump metrics to z: )r(   rA   	enumerater)   r+   r   ru   r   rv   r$   rd   r*   rG   rJ   rK   makedirsdirnameopenjsondumpr|   r   r
   r   IOErrorr   )clsr   r}   r   rs   formatted_stepsr   r   reportabs_pathfer   r   r   dump_benchmark_report  s<   $z'PerformanceLogger.dump_benchmark_reporttotal_inference_timec           
   
   C   s  dd |j  D }dd |j D }t|jt d||j|j|d}zFt rgt	 }t
j|s7t
j|dd t
j|d	}t|d
dd}|tt|d  W d   W dS 1 s_w   Y  W dS W dS  ttfy }	 ztd|	 tjd W Y d}	~	dS d}	~	ww )zlogs the stage metrics and total duration for a completed request
        to the performance_log file.

        Note that this accords to the time spent internally in server, postprocess is not included
        c                 S   r   ))r>   execution_time_msr   r   r   r   r   r   N  r@   z9PerformanceLogger.log_request_summary.<locals>.<listcomp>c                 S   r:   r   r;   r<   r   r   r   r?   S  r@   z9PerformanceLogger.log_request_summary.<locals>.<dictcomp>pipeline_stage_metrics)rb   rs   r(   r)   r*   r+   Tr   zperformance.logarZ   r   
Nz+WARNING: Failed to log performance record: )file)r(   rA   r+   rq   r$   rd   r)   r*   r   rS   rG   rJ   existsr   joinr   writer   dumpsry   asdictOSErrorPermissionErrorprintsysrY   )
r   r}   rs   formatted_stagesr   recordrP   log_filer   r   r   r   r   log_request_summaryC  s:   
&
"z%PerformanceLogger.log_request_summary)Nr   )r   )r   r   r   rB   classmethodr"   r   r   r   r   r   r   r   r   r   r   
  s*    /r   )+ry   r   r   rG   r[   r   r   r   	functoolsr   pathlibr   typingr   r   r   re   dateutil.tzr   rL   sglang.multimodal_gen.envsmultimodal_genr   'sglang.multimodal_gen.runtime.platformsr	   1sglang.multimodal_gen.runtime.utils.logging_utilsr
   r   r   r   r   r   r|   	dataclassr   r#   r"   rS   rd   rp   rq   r{   r   r   r   r   r   <module>   s<   )%T