o
    پi5                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZmZ e	 r5d dl
Z
de
jjgde
jjggZe
e eeZG dd dZdS )    N)current_platform)CYANRESETinit_loggerzprofiler.profilezprofiler.schedulec                   @   s   e Zd ZdZdZ						d"dedB dedededB d	edB d
edB fddZdd Z	dd Z
dd Zdd Zed#ddZd$dededB fddZdd Zdedefd d!ZdS )%SGLDiffusionProfileraw  
    A wrapper around torch.profiler to simplify usage in pipelines.
    Supports both full profiling and scheduled profiling.


    1. if profile_all_stages is on: profile all stages, including all denoising steps
    2. otherwise, if num_profiled_timesteps is specified: profile {num_profiled_timesteps} denoising steps. profile all steps if num_profiled_timesteps==-1
    Nr   F
request_idrankfull_profile	num_stepsnum_inference_stepslog_dirc                 C   sj  |pd| _ || _|| _|d ur|ntdd| _z
tj| jdd W n	 ty+   Y nw tj	j
jg}tj r?|tj	j
j t rK|tj	j
j t|ddt sUd ntj	| jd}| jrotj	jdi || _	d| _n-d}	|d	krw|n|}
|
|	 | _tj	jdi |d
tj	jdd|	| jddi| _	|
 d| _td| d| j d d| _| t_|   d S )Nprofile_traceSGLANG_TORCH_PROFILER_DIRz./logsTexist_ok)
activitiesrecord_shapes
with_stackon_trace_readyzfull stages   scheduler   )
skip_firstwaitwarmupactiverepeatz stepszProfiling request: z for z...F ) r   r   r	   osgetenvr   makedirsOSErrortorchprofilerProfilerActivityCPUcudais_availableappendCUDAr   is_npu	torch_npuNPUdicttensorboard_trace_handlerprofileprofile_mode_idnum_active_stepsr   loggerinfohas_stoppedr   	_instancestart)selfr   r   r	   r
   r   r   r   common_torch_profiler_argsr   num_actual_stepsr   r   `/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/runtime/utils/profiler.py__init__!   s^   
	






zSGLDiffusionProfiler.__init__c                 C   s   t d | j  d S )NzStarting Profiler...)r2   r3   r#   r6   r7   r   r   r:   r6   e   s   
zSGLDiffusionProfiler.startc                 C   s   | j   d S N)r#   stepr<   r   r   r:   _stepi   s   zSGLDiffusionProfiler._stepc                 C   s   | j r	|   d S d S r=   )r	   r?   r<   r   r   r:   
step_stagel   s   zSGLDiffusionProfiler.step_stagec                 C   s>   | j s| jdkr|   |  jd8  _d S | jdd d S d S )Nr   r   )	dump_rank)r	   r1   r?   stopr<   r   r   r:   step_denoising_stepp   s   
z(SGLDiffusionProfiler.step_denoising_stepreturnc                 C   s   | j S r=   )r5   )clsr   r   r:   get_instancey   s   z!SGLDiffusionProfiler.get_instanceTexport_tracerA   c                 C   sx   | j rd S d| _ td tj rtj  t r"tj	  d}| j
  |r7|d ur3|| jkr3n|   d t_d S )NTzStopping Profiler...F)r4   r2   r3   r"   r&   r'   synchronizer   r*   npur#   rB   r   _export_tracer   r5   )r7   rG   rA   r   r   r:   rB   }   s   





zSGLDiffusionProfiler.stopc                 C   s   zKt j| jdd | jdd}t jt j| j| j d| d| j	 d}| j
| | |rAtdt | t  W d S td	|  W d S  tyf } ztd
|  W Y d }~d S d }~ww )NTr    _-z-global-rankz.trace.json.gzzSaved profiler traces to: zTrace file may be corrupted: zFailed to save trace: )r   r    r   r0   replacepathabspathjoinr   r   r#   export_chrome_trace_check_trace_integrityr2   r3   r   r   warning	Exceptionerror)r7   sanitized_profile_mode_id
trace_pather   r   r:   rJ      s"   
z"SGLDiffusionProfiler._export_tracerX   c              
   C   s   zFt j|rt j|dkrW dS t|d$}| }|ddkr3t	d 	 W d    W dS W d    W dS 1 s?w   Y  W dS  t
ya } zt	d|  W Y d }~dS d }~ww )	Nr   Frbs   r   zMultiple gzip headers detectedTz#Trace file integrity check failed: )r   rO   existsgetsizegzipopenreadcountr2   rT   rU   )r7   rX   fcontentrY   r   r   r:   rS      s&   

z+SGLDiffusionProfiler._check_trace_integrity)Nr   FNNN)rD   r   )TN)__name__
__module____qualname____doc__r5   strintboolr;   r6   r?   r@   rC   classmethodrF   rB   rJ   rS   r   r   r   r:   r      s>    	
D	r   )r]   r   r"   'sglang.multimodal_gen.runtime.platformsr   1sglang.multimodal_gen.runtime.utils.logging_utilsr   r   r   r*   r+   r#   r/   r   patches_apply_patchesrc   r2   r   r   r   r   r:   <module>   s    


