
    9i/                         d dl Z d dlZd dlmZ d dlZd dlZd dlmZmZm	Z	m
Z
mZ d dlmZmZmZmZmZmZ dgZ edd           G d d                      Zd	 ZdS )
    N)
deprecated)_disable_profiler_legacy_enable_profiler_legacy
DeviceTypeProfilerConfigProfilerState)_filter_name_filter_stack_entry_rewrite_name	EventListFunctionEventMEMORY_EVENT_NAMEprofilez`torch.autograd.profiler_legacy.profile` is deprecated and will be removed in a future release. Please use `torch.profiler` instead.)categoryc                   0   e Zd ZdZ	 dddddddddZd Zd Zd Zd	 Zd
 Z	d Z
d Z	 	 	 	 	 	 	 ddZej        j        e_        d Zej        j        e_        ddedefdZddZej        j        e_        d Zej        j        e_        ed             ZdS ) r   z'DEPRECATED: use torch.profiler instead.TF)use_cudarecord_shapes
with_flopsprofile_memory
with_stackwith_modulesc                   || _         | j         sd S || _        d | _        d| _        || _        || _        | xj        | j        z  c_        || _        || _        || _        | j        r;t          j
                                        st          j        dd           d| _        | j        rt          j        | _        d S t          j        | _        d S )NFz/CUDA is not available, disabling CUDA profiling   )
stacklevel)enabledr   function_eventsenteredr   r   r   r   r   torchcudais_availablewarningswarnr   CUDAprofiler_kindCPU)selfr   r   r   r   r   r   r   s           Q/var/www/icac/venv/lib/python3.11/site-packages/torch/autograd/profiler_legacy.py__init__zprofile.__init__$   s     %| 	F #*$do-,$(= 	"!8!8!:!: 	"MA    "DM= 	3!.!3D!.!2D    c           
          t          | j        | j        | j        | j        | j        | j        t          j        j	        
                                          S N)r   r$   r   r   r   r   r   r   _C	_profiler_ExperimentalConfigr&   s    r'   configzprofile.configH   sJ    OOH2244	
 	
 		
r)   c                 z    | j         sd S | j        rt          d          d| _        |                                  | S )Nz)Profiler context manager is not reentrantT)r   r   RuntimeError_start_tracer/   s    r'   	__enter__zprofile.__enter__T   sI    | 	F< 	LJKKKr)   c                 H    t          |                                            d S r+   )r   r0   r/   s    r'   r3   zprofile._start_trace]   s    .....r)   c                 $   | j         sd S | j        rt          j                                         t                      }t          |          }t          || j        rdnd | j        | j	                  | _
        | j
                                         dS )Nr   )
use_devicer   r   F)r   r   r   r   synchronizer   _parse_legacy_recordsr   r   r   r   _build_tree)r&   exc_typeexc_valexc_tbrecordsparsed_resultss         r'   __exit__zprofile.__exit__`   s    | 	F= 	%J""$$$*,,.w77(!%8vvD.	 
  
  
 	((***ur)   c                 <    | j         dS t          | j                   S )Nz$<unfinished profiler_legacy.profile>)r   reprr/   s    r'   __repr__zprofile.__repr__s   s"    '99D()))r)   c                 <    | j         dS t          | j                   S )Nz,<unfinished profile.profiler_legacy.profile>)r   strr/   s    r'   __str__zprofile.__str__x   s"    'AA4'(((r)   c                 2    | j         t          d          d S )NzProfiler didn't finish running)r   r2   r/   s    r'   _check_finishzprofile._check_finish}   s"    '?@@@ ('r)   Nd   K   7   P   c           	          |                                   | j        t          d          | j                            |||||||          S )NExpected profiling results)sort_by	row_limitmax_src_column_widthmax_name_column_widthmax_shapes_column_widthheadertop_level_events_only)rH   r   AssertionErrortable)r&   rO   rP   rQ   rR   rS   rT   rU   s           r'   rW   zprofile.table   sc     	' !=>>>#))!5"7$;"7 * 
 
 	
r)   c                     |                                   | j        t          d          | j                            |          S NrN   )rH   r   rV   export_chrome_trace)r&   paths     r'   rZ   zprofile.export_chrome_trace   sC    ' !=>>>#77===r)   self_cpu_time_totalr[   metricc                     |                                   | j        t          d          | j        st          d          | j                            ||          S )NrN   z(export_stacks() requires with_stack=True)rH   r   rV   r   export_stacks)r&   r[   r]   s      r'   r_   zprofile.export_stacks   s_    ' !=>>> 	M !KLLL#11$???r)   r   c                     |                                   | j        t          d          | j                            ||          S rY   )rH   r   rV   key_averages)r&   group_by_input_shapegroup_by_stack_ns      r'   ra   zprofile.key_averages   sG    ' !=>>>#001EGWXXXr)   c                     |                                   | j        t          d          | j                                        S rY   )rH   r   rV   total_averager/   s    r'   re   zprofile.total_average   sA    ' !=>>>#11333r)   c                 n    |                                   | j        t          d          | j        j        S )z;Return CPU time as the sum of self times across all events.NrN   )rH   r   rV   r\   r/   s    r'   r\   zprofile.self_cpu_time_total   s:     	' !=>>>#77r)   )T)NrI   rJ   rK   rL   NF)r\   )Fr   )__name__
__module____qualname____doc__r(   r0   r4   r3   r@   rC   rF   rH   rW   r   rZ   rE   r_   ra   re   propertyr\    r)   r'   r   r      s        21 "3 "3 "3 "3 "3 "3H

 

 

  / / /  &* * *
) ) )
A A A   "#
 
 
 
. O+EM> > > #,"?"G@ @# @s @ @ @ @Y Y Y Y %19L4 4 4 &3;M8 8 X8 8 8r)   c                 
   d }d }g }t           j                            |           D ] }|                                }||dk    r|}!||                                rt          d          | D ]}i }i }i }	t                      }
d }|D ]} ||          }t          |                                          s||
v r|
                    |           I|	                                dk    r||                                |                                k    oS|	                                |	                                k    o)|
                                |
                                k    }|r|
                    |           ||	|<   d||<   d||<   n|	                                dk    r||	vrt          d| d          |	|         }||         }||         }|                                p)|                                |                                k    }|                                }|                                }t          d)i d	|                                d
|
                                dt!          |                                d          dt!          |                                d          d|                                d|                    |          d|                    |          d|                                d|                                dd |                                D             d|                                d|                                rdnd d|d|d|d|d|                                dt0          j        d dd!|}|sj|                                rV|                    |          }|dk    r;|                    |                                |                                |           |                    |           |	|= ||= ||= n|	                                d"k    rt=          |          }t=          |          }||k    rt          d#| d$| d%          |D ]$}||xx         |                                z  cc<   %|D ]$}||xx         |                                 z  cc<   %|dk    rWt          dtB          d dddg |                                |                                 d&
  
        }|                    |           |}|"                    d' (           |S )*Nc                 R    |                                  |                                 fS )zPReturn a tuple for correlating start and end records in `_parse_legacy_records`.)handlenode_id)records    r'   _get_record_keyz._parse_legacy_records.<locals>._get_record_key   s    !1!122r)   __start_profilez#Expected a valid local start_recordpushr   popzExpected record with key z[ to exist in range_starts. This means that the pop event did not have a corresponding push.idrp   nameT)rw   with_wildcard
trace_nameFthreadstart_usend_us
fwd_threadinput_shapesstackc                 0    g | ]}t          |          |S rl   )r
   ).0entrys     r'   
<listcomp>z)_parse_legacy_records.<locals>.<listcomp>  s7       "'<OPU<V<V  r)   scoper7   r   cpu_memory_usagedevice_memory_usageis_async	is_remotesequence_nrdevice_type	is_legacyflopsmemory_alloczBExpected CPU and CUDA memory allocation handles to match, but got z	 CPU and z CUDA)
rv   rw   ry   rz   r{   r|   r   r   r   r   c                 4    | j         j        | j         j         gS r+   )
time_rangestartend)evts    r'   <lambda>z'_parse_legacy_records.<locals>.<lambda>F  s    CN$83>;M:M#N r)   )keyrl   )#	itertoolschainfrom_iterablerw   r   rV   setr	   addkindrp   r   	thread_idr   r   ro   r   cpu_elapsed_usfwd_thread_idshapesr   r   has_cudar   r   r%   cuda_elapsed_usappend_kerneldeviceappendlenr   cuda_memory_usager   sort)thread_recordsrr   start_record	functionsrq   rw   thread_record_listcpu_memory_allocscuda_memory_allocsrange_startsfiltered_handlesprev_record
record_key	duplicater   r   r   r   is_remote_eventstart_flopsfedurationnum_open_handles_cpunum_open_handles_cudaro   s                            r'   r9   r9      sz   3 3 3 LI ///?? " "{{}}D,=$=$=!L|5577BCCC, j! j!55( a	! a	!F(00JFKKMM** j<L.L.L $$Z000{{}}&& *#((**fkkmm; F',,..&++--?F'//11V^^5E5EE 
 ! !(,,Z888 +1Z(01!*-12":..%''\11([J [ [ [  
 %Z0#4Z#@ $6z$B! >>++X0A0AVEUEUEWEW0W"("2"2"4"4#kkmm"   }}"NN,,, 'EJJLLMMMM  -%**,,eTTTT	
 !??,,, *88??? (66v>>>  %22444 "' +0;;==     ++--- */)9)9Cvvt &6%5  ):(9!" &X#$ .o%& !& 1 1 3 3 3'( !+)* #d+, &+-2   QENN$4$4 Q$44V<<H!||((u||~~xPPP  $$$ ,%j1&z22.00'*+<'='=$(+,>(?(?%'+@@@(_#7_ _BW_ _ _   0 K KF%f---1H1H1J1JJ----0 M MF&v...&2J2J2L2LL....'1,,&.#' !"  )/)@)@)B)B,2,D,D,F,F"&  B $$R((( KKCa	!R NNNNNOOOr)   )r   r!   typing_extensionsr   r   
torch.cudatorch.autogradr   r   r   r   r   torch.autograd.profiler_utilr	   r
   r   r   r   r   __all__r   r9   rl   r)   r'   <module>r      sW        ( ( ( ( ( (                                  + +  
_8 _8 _8 _8 _8 _8 _8 
_8DD D D D Dr)   