o
    :/i7,                     @   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ef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   b/lsinfo/ai/hellotax_ai/llm_service/venv_vllm/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r3|  r3|  jd7  _| j	dkrN| jrP| j| j	krRt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&   _profiler_stepr   r   r*   r   r   r   r   stepS   s,   

zWorkerProfiler.stepc                 C   r   )a  Called each step when profiler is running.
        Override in subclasses to handle schedule-based profiling.

        Returns:
            True if the step was an active profiling step (data recorded),
            False if the step was a warmup step (data discarded).
        Tr   r   r   r   r   r.   v   s   zWorkerProfiler._profiler_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   stop   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   r0   r   r   r   r   shutdown   s   zWorkerProfiler.shutdownnamec                 C   s   t  S )z5Return a context manager to annotate profiler traces.r   r   r2   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/   boolr.   r0   r1   strr4   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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| _d }|jd	ksP|jd	krotjjd	|j|j|jdd
}|dv rotjd|j|j|jdd tjjdd |D ||j|j	|j
|j|d| _|d u| _|j| _t|j|j d 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_gzipr;   r-   r   )
skip_firstwaitwarmupactiverepeatz;Profiler schedule configured: wait=%d, warmup=%d, active=%dc                 S   s   g | ]}t | qS r   )TorchProfilerActivityMap).0activityr   r   r   
<listcomp>   s    z1TorchProfilerWrapper.__init__.<locals>.<listcomp>)rA   schedulerecord_shapesprofile_memory
with_stack
with_flopsrB   )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warmup_iterationswait_iterationsrN   active_iterationsprofile_uses_schedule_warmup_iterationsmax_warmup_steps_remaining)	r   r   r?   r@   rA   rB   torch_profiler_trace_dirtrace_handlerprofiler_schedule	__class__r   r   r      sr   

zTorchProfilerWrapper.__init__c                 C      | j   d S N)rZ   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   )rp   	row_limit)rZ   r0   r   r@   #torch_profiler_dump_cuda_time_totalrT   key_averagestabler
   openprintr^   r   info)r   r   rankprofiler_dirsort_keyrx   profiler_out_filefr   r   r   r!      s*   

zTorchProfilerWrapper._stopc                 C   s0   | j r| j  | jdkr|  jd8  _dS dS )zCall profiler.step() when using schedule-based profiling.

        Returns:
            True if the step was an active profiling step (data recorded),
            False if the step was a warmup step (data discarded).
        r   r-   FT)rc   rZ   r/   rf   r   r   r   r   r.     s   

z#TorchProfilerWrapper._profiler_stepr2   c                 C   s   t j|S rm   )rY   rZ   record_functionr3   r   r   r   r4   #  s   z-TorchProfilerWrapper.annotate_context_managerrm   r5   )r6   r7   r8   r	   r:   intlistTorchProfilerActivityr   rY   rZ   rb   r   r   r   r!   r9   r.   r4   __classcell__r   r   rj   r   r>      s.    Q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 rC   )rS   r   torch.cuda.profilercudarZ   _cuda_profiler)r   r   cuda_profilerrj   r   r   r   )  s   
zCudaProfilerWrapper.__init__c                 C   rl   rm   )r   r,   r   r   r   r   r   0  rn   zCudaProfilerWrapper._startc                 C   rl   rm   )r   r0   r   r   r   r   r!   4  rn   zCudaProfilerWrapper._stopr2   c                 C   s   t jj|S rm   )rY   r   nvtxranger3   r   r   r   r4   8  rn   z,CudaProfilerWrapper.annotate_context_managerr5   )r6   r7   r8   r	   r   r   r   r!   r:   r4   r   r   r   rj   r   r   (  s    r   )abcr   r   collections.abcr   
contextlibr   typingr   rY   typing_extensionsr   vllm.configr	   vllm.config.profilerr
   vllm.loggerr   r6   r   r   r   rZ   ProfilerActivityr;   r<   r=   rJ   r>   r   r   r   r   r   <module>   s(     