
    .`i0"                     R   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e          Zed         Ze	j        j        j        e	j        j        j        e	j        j        j        dZ G d de          Z G d de          ZdS )    )ABCabstractmethod)Callablenullcontext)LiteralN)override)ProfilerConfig)_is_uri_path)init_loggerc                       e Zd ZdeddfdZedd            Zedd            ZddZddZ	dd	Z
dd
ZddZddZdefdZdS )WorkerProfilerprofiler_configreturnNc                 &   |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   s     i/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/profiler/wrapper.py__init__zWorkerProfiler.__init__   s    +<q  B$B B B  
 *8?Q44 4 4   ()$ %&!    c                     dS )zStart the profiler.N r   s    r   _startzWorkerProfiler._start,   	     	r   c                     dS )zStop the profiler.Nr!   r"   s    r   _stopzWorkerProfiler._stop1   r$   r   c                     	 |                                   d| _        dS # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)z2Call _start with error handling but no safeguards.TzFailed to start profiler: %sN)r#   r   	Exceptionr   warningr   es     r   _call_startzWorkerProfiler._call_start6   se    	>KKMMM DMMM 	> 	> 	>NN91=========	>s    
AA

Ac                     	 |                                   t                              dd           n2# t          $ r%}t                              d|           Y d}~nd}~ww xY 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*   s     r   
_call_stopzWorkerProfiler._call_stop>   s    	=JJLLL=WMMMM 	= 	= 	=NN8!<<<<<<<<	=s   03 
A"AA"c                     | 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"   s    r   startzWorkerProfiler.startG   sc    < 	LL$   F!! "!r   c                    | j         sdS | xj        dz  c_        | j        sK| j        dk    r@| j        | j        k    r0t                              dd           |                                  | j        r| xj        dz  c_        | j        dk    rI| j        rD| j        | j        k    r6t                              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   r1   r"   s    r   stepzWorkerProfiler.stepS   s    | 	F$$)$$ 	!A%%,0AAA?wOOO= 	+%%*%% Oa  )DO;;
 HPW     OOF  ;;r   c                     | 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   r3   r   r   r   r1   r"   s    r   stopzWorkerProfiler.stopt   sm    | 	LLV   F'($$%!= 	OO	 	r   c                 x    t                               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   r9   r"   s    r   shutdownzWorkerProfiler.shutdown   sA    1AAA= 	IIKKKKK	 	r   namec                     t                      S )z5Return a context manager to annotate profiler traces.r   r   r<   s     r   annotate_context_managerz'WorkerProfiler.annotate_context_manager   s    }}r   r   N)__name__
__module____qualname__r
   r   r   r#   r&   r,   r1   r4   r7   r9   r;   strr?   r!   r   r   r   r      s
        4    0    ^    ^> > > >   
 
 
 
   B      S      r   r   )CPUCUDAXPUc                        e Zd Z	 ddedededee         dee	j
        j        gdf         dz  ddf fdZedd	            Zedd
            Zedefd            Z xZS )TorchProfilerWrapperNr   worker_name
local_rank
activitieson_trace_readyr   c                 :   t                                          |           || _        || _        |j        }|dv rOt
                              d|d           t
                              d|j        |j	        |j
        |j                   ||}n't          j                            |||j                  }d|v ot!          |          dk    | _        t          j                            d	 |D             |j        |j	        |j
        |j        |
          | _        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)rJ   use_gziprE   r6   c                 (    g | ]}t           |         S r!   )TorchProfilerActivityMap).0activitys     r   
<listcomp>z1TorchProfilerWrapper.__init__.<locals>.<listcomp>   s    VVVx0:VVVr   )rL   record_shapesprofile_memory
with_stack
with_flopsrM   )superr   rK   r   torch_profiler_dirr   r   r3   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   rJ   rK   rL   rM   torch_profiler_trace_dirtrace_handler	__class__s	           r   r   zTorchProfilerWrapper.__init__   sA    	)))$.#2#E ""F(    
 LL@<:99   %*MM!NDD('(@ E  M $)J#6#O3z??a;O ..VV:VVV)F*E&@&@( / 
 
r   c                 8    | j                                          d S N)ra   r4   r"   s    r   r#   zTorchProfilerWrapper._start   s    r   c                 F   | j                                          | j        }| j        }|j        r|j        }d}| j                                                             |          }t          |          sA| d| d}t          |d          5 }t          ||           d d d            n# 1 swxY w Y   |dk    rt          |           | j        rN|dk    rJt                              | j                                                             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   )rn   	row_limit)ra   r9   r   rK   #torch_profiler_dump_cuda_time_totalr[   key_averagestabler   openprintre   r   info)r   r   rankprofiler_dirsort_keyrv   profiler_out_filefs           r   r&   zTorchProfilerWrapper._stop   s   .> 	*=L-HM..0066x6HHE  -- )'3$M$M4$M$M$M!+S11 )Q%a(((() ) ) ) ) ) ) ) ) ) ) ) ) ) ) qyye# 			KK**,,221R 3      	 			s   B**B.1B.r<   c                 @    t           j                            |          S rk   )r`   ra   record_functionr>   s     r   r?   z-TorchProfilerWrapper.annotate_context_manager   s    ~--d333r   rk   r@   )rA   rB   rC   r
   rD   intlistTorchProfilerActivityr   r`   ra   rf   r   r	   r#   r&   r?   __classcell__ri   s   @r   rI   rI      s        KO/
 /
'/
 /
 	/

 .//
 !%."8!94!?@4G/
 
/
 /
 /
 /
 /
 /
b    X    X6 4S 4 4 4 X4 4 4 4 4r   rI   c                   t     e Zd Zdeddf fdZed	d            Zed	d            Zedefd            Z	 xZ
S )
CudaProfilerWrapperr   r   Nc                 j    t                                          |           dd lmc m} || _        d S rO   )rZ   r   torch.cuda.profilercudara   _cuda_profiler)r   r   cuda_profilerri   s      r   r   zCudaProfilerWrapper.__init__   sD    )))333333333+r   c                 8    | j                                          d S rk   )r   r4   r"   s    r   r#   zCudaProfilerWrapper._start   s    !!#####r   c                 8    | j                                          d S rk   )r   r9   r"   s    r   r&   zCudaProfilerWrapper._stop   s      """""r   r<   c                 J    t           j        j                            |          S rk   )r`   r   nvtxranger>   s     r   r?   z,CudaProfilerWrapper.annotate_context_manager   s    z$$T***r   r@   )rA   rB   rC   r
   r   r	   r#   r&   rD   r?   r   r   s   @r   r   r      s        , ,4 , , , , , , $ $ $ X$ # # # X# +S + + + X+ + + + +r   r   )abcr   r   collections.abcr   
contextlibr   typingr   r`   typing_extensionsr	   vllm.configr
   vllm.config.profilerr   vllm.loggerr   rA   r   r   r   ra   ProfilerActivityrE   rF   rG   rR   rI   r   r!   r   r   <module>r      s   $ # # # # # # # $ $ $ $ $ $ " " " " " "        & & & & & & & & & & & & - - - - - - # # # # # #	X		w w w w wS w w wt   45 >*.N+0>*.  T4 T4 T4 T4 T4> T4 T4 T4n+ + + + +. + + + + +r   