o
    i0"                     @   s   d dl mZmZ d dl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 eeZG d
d deZed Ze	jjje	jjje	jjjdZG dd deZG dd deZdS )    )ABCabstractmethod)Callablenullcontext)LiteralN)override)ProfilerConfig)_is_uri_path)init_loggerc                   @   s   e Zd ZdeddfddZedddZeddd	Zdd
dZdddZ	dddZ
dddZdddZdddZdefddZdS )WorkerProfilerprofiler_configreturnNc                 C   sh   |j | _| jdkrtd| j d |j| _| jdkr&td| j d d| _d| _d| _d| _	d S )Nr   zGPU profiling will start z steps after start_profile.zGPU profiling will stop after z0 worker steps, or when stop_profile is received.F)
delay_iterations_delay_iterslogger	info_oncemax_iterations
_max_iters_active_iteration_count_active_profiling_for_iters_running)selfr    r   K/home/ubuntu/vllm_env/lib/python3.10/site-packages/vllm/profiler/wrapper.py__init__   s$   


zWorkerProfiler.__init__c                 C      dS )zStart the profiler.Nr   r   r   r   r   _start,      zWorkerProfiler._startc                 C   r   )zStop the profiler.Nr   r   r   r   r   _stop1   r    zWorkerProfiler._stopc              
   C   sH   z
|    d| _W dS  ty# } ztd| W Y d}~dS d}~ww )z2Call _start with error handling but no safeguards.TzFailed to start profiler: %sN)r   r   	Exceptionr   warningr   er   r   r   _call_start6   s   zWorkerProfiler._call_startc              
   C   sV   z|    tjddd W n ty% } ztd| W Y d}~nd}~ww d| _dS )z1Call _stop with error handling but no safeguards.zProfiler stopped successfully.localscopezFailed to stop profiler: %sNF)r!   r   r   r"   r#   r   r$   r   r   r   
_call_stop>   s   
zWorkerProfiler._call_stopc                 C   s4   | j r
td dS d| _ | jdkr|   dS dS )z=Attempt to start the profiler, accounting for delayed starts.zIstart_profile received when profiler is already active. Ignoring request.NTr   )r   r   debugr   r&   r   r   r   r   startG   s   
zWorkerProfiler.startc                 C   s   | j sdS |  jd7  _| js%| jdkr%| j| jkr%tjddd |   | jr/|  jd7  _| jdkrJ| jrL| j| jkrNtjddd | 	  dS dS dS dS )ziUpdate the profiler state at each worker step,
        to handle delayed starts and max iteration limits.N   r   z Starting profiler after delay...r'   r(   z6Max profiling iterations reached. Stopping profiler...)
r   r   r   r   r   r   r&   r   r   r*   r   r   r   r   stepS   s,   

zWorkerProfiler.stepc                 C   s<   | j s
td dS d| _ d| _d| _| jr|   dS dS )z>Attempt to stop the profiler, accounting for overlapped calls.zDstop_profile received when profiler is not active. Ignoring request.NFr   )r   r   r+   r   r   r   r*   r   r   r   r   stopt   s   zWorkerProfiler.stopc                 C   s$   t jddd | jr|   dS dS )z.Ensure profiler is stopped when shutting down.zShutting down profilerr'   r(   N)r   r   r   r/   r   r   r   r   shutdown   s   zWorkerProfiler.shutdownnamec                 C   s   t  S )z5Return a context manager to annotate profiler traces.r   r   r1   r   r   r   annotate_context_manager   s   z'WorkerProfiler.annotate_context_managerr   N)__name__
__module____qualname__r	   r   r   r   r!   r&   r*   r,   r.   r/   r0   strr3   r   r   r   r   r      s    


	

!
r   )CPUCUDAXPUc                       s~   e Zd Z	ddedededee dee	j
jgdf dB ddf fdd	Zedd
dZedddZedefddZ  ZS )TorchProfilerWrapperNr   worker_name
local_rank
activitieson_trace_readyr   c                    s   t  | || _|| _|j}|dv r(tjd|dd td|j|j	|j
|j |d ur/|}n
tjj|||jd}d|v oBt|dk| _tjjd	d
 |D |j|j	|j
|j|d| _d S )NNr   z4Torch profiling enabled. Traces will be saved to: %sr'   r(   zOProfiler config: record_shapes=%s,profile_memory=%s,with_stack=%s,with_flops=%s)r=   use_gzipr9   r-   c                 S   s   g | ]}t | qS r   )TorchProfilerActivityMap).0activityr   r   r   
<listcomp>   s    z1TorchProfilerWrapper.__init__.<locals>.<listcomp>)r?   record_shapesprofile_memory
with_stack
with_flopsr@   )superr   r>   r   torch_profiler_dirr   r   r+   torch_profiler_record_shapestorch_profiler_with_memorytorch_profiler_with_stacktorch_profiler_with_flopstorchprofilertensorboard_trace_handlertorch_profiler_use_gziplendump_cpu_time_totalprofile)r   r   r=   r>   r?   r@   torch_profiler_trace_dirtrace_handler	__class__r   r   r      sB   zTorchProfilerWrapper.__init__c                 C      | j   d S N)rR   r,   r   r   r   r   r         zTorchProfilerWrapper._startc                 C   s   | j   | j}| j}|jrK|j}d}| j  j|d}t|sC| d| d}t	|d}t
||d W d    n1 s>w   Y  |dkrKt
| | jra|dkrct| j  jdd	d
 d S d S d S )Nself_cuda_time_total)sort_byz/profiler_out_z.txtw)filer   self_cpu_time_total2   )r`   	row_limit)rR   r/   r   r>   #torch_profiler_dump_cuda_time_totalrL   key_averagestabler
   openprintrV   r   info)r   r   rankprofiler_dirsort_keyrh   profiler_out_filefr   r   r   r!      s*   

zTorchProfilerWrapper._stopr1   c                 C   s   t j|S r]   )rQ   rR   record_functionr2   r   r   r   r3      s   z-TorchProfilerWrapper.annotate_context_managerr]   r4   )r5   r6   r7   r	   r8   intlistTorchProfilerActivityr   rQ   rR   rW   r   r   r   r!   r3   __classcell__r   r   rZ   r   r<      s*    1r<   c                       sT   e Zd Zdeddf fddZedddZeddd	Zed
efddZ	  Z
S )CudaProfilerWrapperr   r   Nc                    s(   t  | dd lm  m} || _d S rA   )rK   r   torch.cuda.profilercudarR   _cuda_profiler)r   r   cuda_profilerrZ   r   r   r      s   
zCudaProfilerWrapper.__init__c                 C   r\   r]   )ry   r,   r   r   r   r   r      r^   zCudaProfilerWrapper._startc                 C   r\   r]   )ry   r/   r   r   r   r   r!      r^   zCudaProfilerWrapper._stopr1   c                 C   s   t jj|S r]   )rQ   rx   nvtxranger2   r   r   r   r3      r^   z,CudaProfilerWrapper.annotate_context_managerr4   )r5   r6   r7   r	   r   r   r   r!   r8   r3   ru   r   r   rZ   r   rv      s    rv   )abcr   r   collections.abcr   
contextlibr   typingr   rQ   typing_extensionsr   vllm.configr	   vllm.config.profilerr
   vllm.loggerr   r5   r   r   rt   rR   ProfilerActivityr9   r:   r;   rC   r<   rv   r   r   r   r   <module>   s$   zW