
    Pg-                         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y)
    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                   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                  r<t        j                  j                         st        j                  dd       d| _        | j                  rt        j                  | _        y t        j"                  | _        y )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           e/var/www/html/suriana-translation/venv/lib/python3.12/site-packages/torch/autograd/profiler_legacy.py__init__zprofile.__init__$   s     %|| #*$doo-,$(==!8!8!:MMA "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.configH   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__T   s7    ||<<JKKr)   c                 6    t        | j                                y r+   )r   r0   r/   s    r'   r3   zprofile._start_trace]   s    .r)   c                 <   | j                   sy | j                  rt        j                  j	                          t               }t        |      }t        || j                  rdnd | j                  | j                        | _
        | j                  j                          y)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__`   sw    ||==JJ""$*,.w7(!%vD..	 
 	((*r)   c                 F    | j                   yt        | j                         S )Nz$<unfinished profiler_legacy.profile>)r   reprr/   s    r'   __repr__zprofile.__repr__q   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__v   s"    'A4''((r)   c                 2    | j                   t        d      y )NzProfiler didn't finish running)r   r2   r/   s    r'   _check_finishzprofile._check_finish{   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)rH   r   table)r&   rJ   rK   rL   rM   rN   rO   rP   s           r'   rQ   zprofile.table   sU     	##///##))!5"7$;"7 * 
 	
r)   c                 t    | j                          | j                  J | j                  j                  |      S r+   )rH   r   export_chrome_trace)r&   paths     r'   rS   zprofile.export_chrome_trace   s7    ##///##77==r)   rT   metricc                     | j                          | j                  J d       | j                  sJ d       | j                  j                  ||      S )NExpected profiling resultsz(export_stacks() requires with_stack=True)rH   r   r   export_stacks)r&   rT   rU   s      r'   rX   zprofile.export_stacks   sQ    ##/M1MM/J JJ##11$??r)   c                     | j                          | j                  J d       | j                  j                  ||      S NrW   )rH   r   key_averages)r&   group_by_input_shapegroup_by_stack_ns      r'   r[   zprofile.key_averages   sA    ##/M1MM/##001EGWXXr)   c                 |    | j                          | j                  J d       | j                  j                         S rZ   )rH   r   total_averager/   s    r'   r_   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.)rH   r   self_cpu_time_totalr/   s    r'   ra   zprofile.self_cpu_time_total   s4     	##///##777r)   )T)Nd   K   7   P   NF)ra   )Fr   )__name__
__module____qualname____doc__r(   r0   r4   r3   r@   rC   rF   rH   rQ   r   rS   rE   rX   r[   r_   propertyra    r)   r'   r   r      s     2 "3 "3H

/"*
)
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|j-                         rdnd d|d|d|d|d|j/                         dt0        j2                  ddd|}|sU|j-                         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.idro   nameT)rv   with_wildcard
trace_nameFthreadstart_usend_us
fwd_threadinput_shapesstack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                 \    | j                   j                  | j                   j                   gS r+   )
time_rangestartend)evts    r'   <lambda>z'_parse_legacy_records.<locals>.<lambda>9  s"    CNN$8$83>>;M;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
   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entryfedurationnum_open_handles_cpunum_open_handles_cudarn   s                               r'   r9   r9      s   3 GLIL ////? "{{}D,=$=!L"
 #L,B,B,DDD, e!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  ++- */)9vt &6  ):!" &#$ .%& !& 1 1 3'( !+)* #+, &-2  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,2,D,D,F"&B $$R( Ky\	!e!Z NNNNOos   %R6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      sa      (     + +
V8 V8
V8r@r)   