
    '}ha+                         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 G d d      Zd Zy)    N)warn)_disable_profiler_legacy_enable_profiler_legacy
DeviceTypeProfilerConfigProfilerState)_filter_name_filter_stack_entry_rewrite_name	EventListFunctionEventMEMORY_EVENT_NAMEprofilec                   x   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y)r   z'DEPRECATED: use torch.profiler instead.F)use_cudarecord_shapes
with_flopsprofile_memory
with_stackwith_modulesc                   || _         | j                   sy || _        d | _        d| _        || _        || _        | xj                  | j
                  z  c_        || _        || _        || _        | j                  r0t        j                  j                         st        d       d| _        | j                  rt        j                  | _        y t        j                   | _        y )NFz/CUDA is not available, disabling CUDA profiling)enabledr   function_eventsenteredr   r   r   r   r   torchcudais_availabler   r   CUDAprofiler_kindCPU)selfr   r   r   r   r   r   r   s           ]/var/www/html/test/engine/venv/lib/python3.12/site-packages/torch/autograd/profiler_legacy.py__init__zprofile.__init__   s     %|| #*$doo-,$(==!8!8!:BC!DM==!.!3!3D!.!2!2D    c           
          t        | j                  | j                  | j                  | j                  | j
                  | j                  t        j                  j                  j                               S N)r   r   r   r   r   r   r   r   _C	_profiler_ExperimentalConfigr!   s    r"   configzprofile.config>   sW    OOOOHH224	
 		
r$   c                 |    | j                   sy | j                  rt        d      d| _        | j                          | S )Nz)Profiler context manager is not reentrantT)r   r   RuntimeError_start_tracer*   s    r"   	__enter__zprofile.__enter__J   s7    ||<<JKKr$   c                 6    t        | j                                y r&   )r   r+   r*   s    r"   r.   zprofile._start_traceS   s    .r$   c                 4   | j                   sy | j                  rt        j                  j	                          t               }t        |      }t        || j                  | j                  | j                        | _
        | j                  j                          y)N)r   r   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__V   ss    ||==JJ""$*,.w7(]]..	 
 	((*r$   c                 F    | j                   yt        | j                         S )Nz$<unfinished profiler_legacy.profile>)r   reprr*   s    r"   __repr__zprofile.__repr__g   s"    '9D(())r$   c                 F    | j                   yt        | j                         S )Nz,<unfinished profile.profiler_legacy.profile>)r   strr*   s    r"   __str__zprofile.__str__l   s"    'A4''((r$   c                 2    | j                   t        d      y )NzProfiler didn't finish running)r   r-   r*   s    r"   _check_finishzprofile._check_finishq   s    '?@@ (r$   Nc           	          | j                          | j                  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)rB   r   table)r!   rD   rE   rF   rG   rH   rI   rJ   s           r"   rK   zprofile.tableu   sU     	##///##))!5"7$;"7 * 
 	
r$   c                 t    | j                          | j                  J | j                  j                  |      S r&   )rB   r   export_chrome_trace)r!   paths     r"   rM   zprofile.export_chrome_trace   s7    ##///##77==r$   rN   metricc                     | j                          | j                  J d       | j                  sJ d       | j                  j                  ||      S )NExpected profiling resultsz(export_stacks() requires with_stack=True)rB   r   r   export_stacks)r!   rN   rO   s      r"   rR   zprofile.export_stacks   sQ    ##/M1MM/J JJ##11$??r$   c                     | j                          | j                  J d       | j                  j                  ||      S NrQ   )rB   r   key_averages)r!   group_by_input_shapegroup_by_stack_ns      r"   rU   zprofile.key_averages   sA    ##/M1MM/##001EGWXXr$   c                 |    | j                          | j                  J d       | j                  j                         S rT   )rB   r   total_averager*   s    r"   rY   zprofile.total_average   s;    ##/M1MM/##1133r$   c                 j    | j                          | j                  J | j                  j                  S )z;Return CPU time as the sum of self times across all events.)rB   r   self_cpu_time_totalr*   s    r"   r[   zprofile.self_cpu_time_total   s4     	##///##777r$   )T)Nd   K   7   P   NF)r[   )Fr   )__name__
__module____qualname____doc__r#   r+   r/   r.   r:   r=   r@   rB   rK   r   rM   r?   rR   rU   rY   propertyr[    r$   r"   r   r      s    1 3 3B

/"*
)
A   "#
, OO++EM>
 #,"?"?"G"G@# @s @Y
 %1199L4
 &33;;M8 8r$   c                    d }d}d }g }g }t         j                  j                  |       D ]  }|j                         }||dk(  s|} ||j	                         rJ | D ]  }i }	i }
i }t               }d }|D ]  } ||      }t        |j                               s||v r|j                  |       ;|j                         dk(  r|{|j                         |j                         k(  xrD |j                         |j                         k(  xr! |j                         |j                         k(  }|r|j                  |       |||<   d|	|<   d|
|<   n|j                         dk(  r||v sJ d| d       ||   }|	|   }|
|   }|j                         xs! |j                         |j                         k7  }|j	                         }|j                         }t        d"i d|j                         d	|j                         d
t        |j                         d      dt        |j                         d      d|j                         d|j!                  |      d|j!                  |      d|j#                         d|j%                         d|j'                         D cg c]  }t)        |      s| c}d|j+                         d|d|d|d|d|j-                         dt.        j0                  ddd|}|sU|j3                         rE|j5                  |      }|dkD  r/|j7                  |j                         |j9                         |       |j;                  |       ||= |	|= |
|= n|j                         dk(  rt=        |	      }t=        |
      }||k(  sJ |	j?                         D ]  }|	|xx   |jA                         z  cc<    |
j?                         D ]  }|
|xx   |jC                         z  cc<    |dk(  rFt        dtD        d dddg |jA                         |jC                         d
      }|j;                  |       |}  |jG                  d  !       |S c c}w )#Nc                 B    | j                         | j                         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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.idri   nameT)rp   with_wildcard
trace_nameFthreadstart_usend_us
fwd_threadinput_shapesstackscopecpu_memory_usagecuda_memory_usageis_async	is_remotesequence_nrdevice_type	is_legacyflopsmemory_alloc)
ro   rp   rr   rs   rt   ru   rx   rz   r{   r   c                 \    | j                   j                  | j                   j                   gS r&   )
time_rangestartend)evts    r"   <lambda>z'_parse_legacy_records.<locals>.<lambda>.  s"    CNN$8$83>>;M;M:M#N r$   )keyre   )$	itertoolschainfrom_iterablerp   r}   setr	   addkindri   r|   	thread_idr   r   rh   r   cpu_elapsed_usfwd_thread_idshapesrx   r
   ry   r~   r   r    has_cudacuda_elapsed_usappend_kerneldeviceappendlenkeysrz   r{   r   sort)thread_recordsrk   next_idstart_record	functionsrecord_stackrj   rp   thread_record_listcpu_memory_allocscuda_memory_allocsrange_startsfiltered_handlesprev_record
record_key	duplicater   rz   r{   r|   is_remote_eventstart_flopsentryfedurationnum_open_handles_cpunum_open_handles_cudarh   s                               r"   r3   r3      s   3 GLIL ////? "{{}D,=$=!L"
 #L,B,B,DDD, d!5( [	!F(0JFKKM*j<L.L $$Z0{{}& *#((*fkkm; F',,.&++-?F'//1V^^5EE 
 !(,,Z8 +1Z(01!*-12":.%',.X0 =U XX. %Z0#4Z#@ $6z$B! >>+X0AVEUEUEW0W"("2"2"4#kkm" }}"NN, 'EJJLM  -%**,eT	
 !??, *88? (66v>  %224 "' ,1;;="'<OPU<V  ++- &6 '8  &!" .#$ !& 1 1 3%& !+'( #)* &+0  ENN$4$44V<H!|((u||~xP  $ ,%j1&z2.0'*+<'=$(+,>(?%+/DDDD/446 KF%f-1H1H1JJ-K0557 MF&v.&2J2J2LL.M'1,&.#' !"  )/)@)@)B*0*B*B*D"&B $$R( Kw[	!d!X NNNNOms   %Q.6Q.)r   warningsr   r   
torch.cudatorch.autogradr   r   r   r   r   torch.autograd.profiler_utilr	   r
   r   r   r   r   __all__r   r3   re   r$   r"   <module>r      sA          +S8 S8lr$   