
    קg-                         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           Z/var/www/html/ai-engine/env/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__q   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__v   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        J | j                            |||||||          S )N)sort_by	row_limitmax_src_column_widthmax_name_column_widthmax_shapes_column_widthheadertop_level_events_only)rH   r   table)r&   rN   rO   rP   rQ   rR   rS   rT   s           r'   rU   zprofile.table   sZ     	#///#))!5"7$;"7 * 
 
 	
r)   c                 p    |                                   | j        J | j                            |          S r+   )rH   r   export_chrome_trace)r&   paths     r'   rW   zprofile.export_chrome_trace   s:    #///#77===r)   self_cpu_time_totalrX   metricc                     |                                   | j        
J d            | j        s
J d            | j                            ||          S )NExpected profiling resultsz(export_stacks() requires with_stack=True)rH   r   r   export_stacks)r&   rX   rZ   s      r'   r]   zprofile.export_stacks   s\    #//1M///JJ JJJJ#11$???r)   r   c                     |                                   | j        
J d            | j                            ||          S Nr\   )rH   r   key_averages)r&   group_by_input_shapegroup_by_stack_ns      r'   r`   zprofile.key_averages   sG    #//1M///#001EGWXXXr)   c                 ~    |                                   | j        
J d            | j                                        S r_   )rH   r   total_averager/   s    r'   rd   zprofile.total_average   sA    #//1M///#11333r)   c                 T    |                                   | j        J | j        j        S )z;Return CPU time as the sum of self times across all events.)rH   r   rY   r/   s    r'   rY   zprofile.self_cpu_time_total   s1     	#///#77r)   )T)NrI   rJ   rK   rL   NF)rY   )Fr   )__name__
__module____qualname____doc__r(   r0   r4   r3   r@   rC   rF   rH   rU   r   rW   rE   r]   r`   rd   propertyrY    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}d }g }g }t           j                            |           D ] }|                                }||dk    r|}!||                                rJ | 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 sJ 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t.          j        ddd |}|sj|                                rV|                    |          }|dk    r;|                    |                                |                                |           |                    |           ||= |	|= |
|= n|                                d!k    rt;          |	          }t;          |
          }||k    sJ |	                                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)   r   __start_profilepushpopzExpected record with key zo to exist in range_starts.
                    This means that the pop event did not have a corresponding push.idro   nameT)rv   with_wildcard
trace_nameFthreadstart_usend_us
fwd_threadinput_shapesstackc                 0    g | ]}t          |          |S rk   )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_alloc)
ru   rv   rx   ry   rz   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>9  s    CN$83>;M:M#N r)   )keyrk   )#	itertoolschainfrom_iterablerv   r   setr	   addkindro   r   	thread_idr   r   rn   r   cpu_elapsed_usfwd_thread_idshapesr~   r   has_cudar   r   r%   cuda_elapsed_usappend_kerneldeviceappendlenkeysr   cuda_memory_usager   sort)thread_recordsrq   next_idstart_record	functionsrecord_stackrp   rv   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_cudarn   s                              r'   r9   r9      sW   3 3 3 GLIL ///?? " "{{}}D,=$=$=!L#L,B,B,D,D###, e! e!55( \	! \	!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":..%'',...X X X X /.. %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'*+<'='=$(+,>(?(?%+/DDDDD/4466 K KF%f---1H1H1J1JJ----05577 M MF&v...&2J2J2L2LL....'1,,&.#' !"  )/)@)@)B)B,2,D,D,F,F"&  B $$R((( KKy\	!H 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   rk   r)   r'   <module>r      sW        ( ( ( ( ( (                                  + +  
V8 V8 V8 V8 V8 V8 V8 
V8r@ @ @ @ @r)   