
    i                     |    S SK r S SKrS SKJrJrJr  S SKrS SKJr  S SK	J
r
JrJr  S SKJr  S SKJr   " S S5      rg)	    N)castOptionalTuple)	Inspector)Event
EventBlockPerfData)	TimeScale)tabulatec                       \ rS rSrS\SS4S jrSS\S\\R                     4S jjr
S\4S jrS	\S\4S
 jr  SS\\   S\\   SS4S jjrSS jrSS jrSrg)CadenceETDump   
output_dirreturnNc                 |   [         R                  R                  US5      U l        [         R                  R                  US5      U l        [         R                  R                  US5      U l        [         R                  R                  US5      U l        [         R                  R                  U R                  5      (       d  [        U R                   S35      e[         R                  R                  U R
                  5      (       d  [        R                  " S5        S U l        [         R                  R                  U R                  5      (       d  [        R                  " S5        S U l        [        U R                  U R                  U R
                  [        R                  [        R                  S9U l        g )	Ntensorszetdump.etdpzetrecord.binzdebug_output.binz does not existz;ETRecord not found, intermediate tensors will not be dumpedz?Debug buffer not found, intermediate tensors will not be dumped)etdump_pathdebug_buffer_pathetrecordsource_time_scaletarget_time_scale)ospathjointensor_dump_dirr   etrecord_pathr   existsRuntimeErrorloggingwarningr   r
   CYCLESet_inspector)selfr   s     i/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/backends/cadence/runtime/etdump.py__init__CadenceETDump.__init__   s4   $&GGLLY$G "Z G,.GGLL^,T02*1
 ww~~d..//$"2"2!3?CDDww~~d0011OOM "&Dww~~d4455OOQ &*D"'0(("44'''..'..(
    log_to_stdoutc                     U R                   R                   Vs/ s H!  nUR                  S:X  d  M  UR                  PM#     nn[        R
                  " SU 35        US   $ s  snf )NExecutez[CadenceETDump] output: r   )r"   event_blocksname
run_outputr   debug)r#   r(   event_blockoutputs       r$   get_outputsCadenceETDump.get_outputs4   sk      $00==
=9, #K""= 	 

 	09:ay
s
   A!A!c                     U R                   R                   Vs/ s H  oR                  S:X  d  M  UPM     nnUS   $ s  snf )Nr*   r   )r"   r+   r,   )r#   ebexec_blockss      r$   get_execute_event_block%CadenceETDump.get_execute_event_block=   sE    **77
7277i;OB7 	 
 1~
s   ==eventc                 h    UR                   S;   a  gUR                   R                  S5      (       a  gg)N)OPERATOR_CALLMethod::executeFDELEGATE_ZIONT)r,   
startswith)r#   r8   s     r$   should_include_event"CadenceETDump.should_include_eventC   s/    ::== ::  11r'   bundled_prog_sizeexternal_linkc           	         U R                  5       nUR                   Vs/ s H  o@R                  U5      (       d  M  UPM     nn[        U Vs/ s H'  n[	        [
        UR                  5      R                  PM)     sn5      nUR                   Vs/ s H  owR                  S:X  d  M  UPM     nn[        U5      S:X  d"  [        R                  " S[        U5       35        [	        [
        US   R                  5      R                  n	Un
SR                  U	5      SR                  U
5      SR                  X-
  5      SR                  X-
  U	-  5      /n/ SQnUb1  UR                  SR                  U5      5        UR                  S	5        Ub"  UR                  U5        UR                  S
5        [        R                  " S[        U/USS95        gs  snf s  snf s  snf )a   
Print performance summary with optional program size and external link.

Args:
    bundled_prog_size: Size of the bundled program in bytes (optional)
    external_link: External analytics/monitoring link (optional, e.g., Scuba link for Meta internal use)
r;      z,Expected one 'Method::execute' event, found r   z{:,.0f}z{:.2%})zTotal CycleszCycles in OpszOther CycleszFramework Tax (%)NzBundled Program Size (bytes)zExternal LinkzPerformance Summary:
%soutline)headerstablefmt)r6   eventsr>   sumr   r	   	perf_dataavgr,   lenr   r    formatappendinfor   )r#   r@   rA   blocke	op_eventsop_time_sumevoverall_eventtotal_cycles	op_cycles
table_datarE   s                r$   print_summaryCadenceETDump.print_summaryN   s    ,,. %M10I0I!0LQ	MINIq4!++6::INO&+llSlggAR6RlS=!Q&OO>s=?Q>RS HmA&6&@&@AEE	 \*Y'\56OO\5EF	


 (i../@ABNN9:$m,NN?+&"	
G NNSs   GG.GG'Gc                     [         R                  " S5        [         R                  " 5       R                  [         R                  ::  a  U R
                  R                  5         g g )NzProfiled events:)r   rN   	getLoggerlevelINFOr"   print_data_tabular)r#   s    r$   print_event_blockCadenceETDump.print_event_block   sA    '($$4002 5r'   c                    U R                   c  [        R                  " S5        g [        R                  " SU R                   35        [        R
                  " U R                  SS9  U R                  R                   Vs/ s H  oR                  S:X  d  M  UPM     nn[        U5      S:  a#  [        R                  " S[        U5       S35        US	   nUR                   Vs/ s H  oDR                  S
:w  d  M  UPM     nn[        R                  " SS9  U H  nUR                  nUR                  (       d*  [        R                   " SUR                   SU< S35        MJ  [#        U R                   SU S3S5       nUR                   H   n	UR%                  ['        U	5       S35        M"     S S S 5        M     [        R                  " SS9  g s  snf s  snf ! , (       d  f       M  = f)Nz2[CadenceETDump] Intermediate tensors not availablez0[CadenceETDump] Dumping intermediate tensors to T)exist_okr*   rC   zFound z= "Execute" blocks, using the first one and ignoring the rest.r   r:   full)profilez%Missing intermediate tensor data for z (instr_id=)/z.txtwz

default)r   r   rN   r   r   makedirsr"   r+   r,   rK   r    rG   torchset_printoptions_instruction_id
debug_datar.   openwritestr)
r#   r4   r5   rO   rP   rQ   r8   instr_idfdds
             r$   dump_intermediate_tensors'CadenceETDump.dump_intermediate_tensors   s   %LLMN>t?S?S>TU	
 	D((48**77
7277i;OB7 	 
 {aOO[)**gh A !&J1/0IQ	Jv.E,,H##;EJJ<|(TUV --.az>D**BGGs2wit,- + ED  	y11
 K EDs$   ?GG#G:G>1G
G)	)r   r"   r   r   r   )F)NN)r   N)__name__
__module____qualname____firstlineno__rp   r%   boolr   rj   Tensorr1   r   r6   r   r>   r   intrX   r_   rt   __static_attributes__ r'   r$   r   r      s    
3 
4 
> %:M  	% 	D 	 ,0'+7
#C=7
  }7
 
	7
r3
!2r'   r   )r   r   typingr   r   r   rj   executorch.devtoolsr   executorch.devtools.inspectorr   r   r	   .executorch.devtools.inspector._inspector_utilsr
   r   r   r~   r'   r$   <module>r      s1     	 ( (  ) E E D Y2 Y2r'   