
    [Th                        % S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
r	S SKrS SKrS SKrS SKrS SKrS SKJr  S SKJrJrJrJrJr  S SKJr  S SKrS SKJrJrJr  S SKJr  S SK J!r!  S SK"J#r#  S S	K$J%r%  S S
K&J'r'  S SK(J)r)J*r*  S SK+J,r,  S SK-J.r.  S SK/J0r0  S SK1J2r2  SSK3J4r4J5r5  SSK6J7r7J8r8J9r9J:r:J;r;  SSK<J=r=  \R|                  " \?5      r@\%" \?S5      rA\%" \?S5      rB\C\   rD\ R                  " SSS/5      rF/ SQrG\R                  " S5      S\I4S j5       rJ  SDS\C\7   S\IS\\K   SS4S jjrLS \C\7   S\R                  4S! jrN  SES\\D   S"\O\K\K4   S#\\K   S$\PSS4
S% jjrQS"\O\K\K4   S\O\K\F4   4S& jrRS'\R>                  RN                  S \DSS4S( jrS\R                  S\S   4S) j5       rU0 rV\O\K\4   \WS*'   SrX\\P   \WS+'    " S, S-5      rY " S. S/5      rZS\DSS4S0 jr[S\DSS4S1 jr\\R                   " S2 S35      5       r^\R                  " 5       r`S4\PS5\O\K\4   S6\O\K\4   S\O\K\O\K\4   4   4S7 jraS8\S9\SS4S: jrbS;\KS\4S< jrcSS=.S>\S?\K4   S@\R                  R                  SA\O\K\4   SB\\.   S\K4
SC jjrfg)F    N)Iterator)AnyCallableIOOptionalUnion)patch)
draw_graphget_aot_graph_nameget_graph_being_compiled)fx)save_graph_repro)get_debug_dir)getArtifactLogger)GraphModule)_extract_tensor_metadataTensorMetadata)legalize_graph)FileLike)
OrderedSet)tree_map   )configir)BaseSchedulerNodeFusedSchedulerNodeNopKernelSchedulerNode
OutputNodeSchedulerNode)Vir_pre_fusionir_post_fusionBufMetanamen_origin)dotz-Gnslimit=2z-Gnslimit1=2z-Gmaxiter=5000returnc                       [         R                  " SS/[         R                  S9  g! [         R                   a     gf = f)Nwhichr&   )stderrTF)
subprocesscheck_outputPIPESubprocessError     M/var/www/auris/envauris/lib/python3.13/site-packages/torch/_inductor/debug.pyhas_dotr2   6   s;    % 0I%% s   %( ??nodesprint_graphfnamec           
         [        5       (       d  [        R                  S5        gUc
  [        5       n[	        U 5      nUR
                   H  nSUR                  ;  a  M  UR                  S   R                  n[        U[        5      (       a$  [        US   [        5      (       a  US   4nOUS   nSn[        U[        R                  5      (       a  UR                  R                  n[        XVSSSSS5      nXtR                  S'   M     U(       a  [!        U5        [#        0 U5      n[%        U5        UR&                  R)                  5         [+        XS[,        R.                  R0                  S9  g)z
Draw a graph in fname.svg.
z*draw_buffers() requires `graphviz` packageNfusion_metar   tensor_metaF)
clear_metadot_graph_shape)r2   logwarningr   create_fx_from_snodesr3   metagroup
isinstancetupleintr   ComputedBufferdatadtyper   printr   r   graphlintr
   r   tracer:   )	r3   r4   r5   rG   noder?   rE   metadatagms	            r1   draw_buffersrM   ?   s"    99@A}(*!%(E		)		-(..eU##%(C((qa dB--..IIOOE!%dD$M#+		- ! $ e	R	B2HHMMO
eV\\5Q5Qr0   snodesc                 t  ^ S[         S[        S[        4   4S jn[        R                  " S/ SQ5      n0 n0 n[
        R                  R                  5       nSn/ nSnU  GH  n	U	R                  5       (       a  Sn
U
nOU	R                  5       (       a  S	n
U
nOm[        U	[        5      (       a  S
n
U
nOS[        U	[        5      (       a  Sn
U	R                  nO/[        U	[        5      (       a  Sn
U	R                  nO[        S5      e[
        R                   R"                  R%                  U	R'                  5       S5      nU
 SU 3nU" U5      n0 n[)        U	S5      (       a  SU	R+                  5       0nUR-                  USUS9nS[.        [0        [        4   S[2        4U4S jjmT" U	5      (       a  UR5                  U5        U	R7                  5       nUUl        U" XU
5      UR:                  S'   XU'   U	R=                  5        H  nXUR7                  5       '   M     Ub  GM  UnGM     U  H  n	U	R7                  5       nU	R>                  R@                  nUU   n/ nU H  nUR8                  U;   a  UUR8                     nODURC                  U5         URE                  UR8                  5      nUUUR8                  '   SSS5        WU:X  a  Mo  UR5                  U5        M     [G        U5      Ul$        M     URK                  [M        U5      S:X  a  US   5        U$ [G        U5      5        U$ ! , (       d  f       Nu= f):
Creates a FX Graph from a list of SchedulerNode objects.
r$   r'   .c                 4    S[         S[        4S jnXl        U$ )Nargsr'   c                      gNr   r/   )rR   s    r1   func1;create_fx_from_snodes.<locals>.get_fake_func.<locals>.func1s   s    r0   )r   rB   __name__)r$   rU   s     r1   get_fake_func,create_fx_from_snodes.<locals>.get_fake_funcr   s!    	 	 	 r0   
FusionMeta)r?   snodetypeNexterntemplatenopcomputefusedzUnknown node typeoriginal_atenz: 
get_devicedevicer/   rR   kwargsr[   c                    > [        U [        5      (       a  [        U4S jU R                   5       5      $ [        S U R	                  5        5       5      $ )Nc              3   4   >#    U  H  nT" U5      v   M     g 7fNr/   ).0x	in_outputs     r1   	<genexpr>;create_fx_from_snodes.<locals>.in_output.<locals>.<genexpr>   s     >A9Q<<s   c              3   ~   #    U  H3  nUR                     H  n[        UR                  [        5      v   M!     M5     g 7fri   )usersr@   rJ   r   )rj   bufusers      r1   rm   rn      s6      .CIID 499j11% 2.s   ;=)r@   r   anyrN   get_outputs)r[   rl   s    r1   rl   (create_fx_from_snodes.<locals>.in_output   sI    %!344>>>>  ,,.  r0   r7   r   r   )'strr   rB   collections
namedtupletorchr   Graph	is_externis_templater@   r   r   r?   r   RuntimeError	_inductorutilsget_fused_kernel_name	get_nodeshasattrrc   call_functionr   r   boolappendget_namer$   r>   rt   read_writesreadsinserting_beforeplaceholderrA   rR   outputlen)rN   rX   rZ   buf_to_fx_nodenode_to_fx_noderG   
first_nodeoutputsr?   r[   	node_type
fused_name	func_name	node_funcrf   fx_noder$   rq   depsnew_argsdepdep_noderl   s                         @r1   r=   r=   m   s   
C HS#X$6  ''6PQJNOHHNNEJGE?? IE  "IE566IE}--!IKKE122IKKE233__**@@OO

 !kJ<0	!),	5,'' 0 0 23F%%ib%H	U#46H#HI 	d 	 UNN7#~~&0y&I]# '$$&C-43<<>* '  Je j ~~  &&!$'Cxx>))#((3++J7$00:H/7N388, 8 7"OOH%  X# & 
LLs7|q0EL 7<GnEL 87s   +L))
L7node_name_to_buf_nameparent_buf_name	n_originsc                 x   U c  g U  H  nUR                  5       nUR                  5       nUb#  [        U5      S:  a  [        UUUc  UOU5        MI  [        U5      S:X  a	  US   U:X  d   eUR                  nUb  UR
                  c  M  UR
                   H  nUR                  n	X;  d  M  Uc  UOUX'   M!     M     g )Nr   r   )r   r   r   $update_orig_fx_node_name_to_buf_namerJ   originsr$   )
r3   r   r   r   rJ   buf_namechildren_nodesir_nodeorigin	node_names
             r1   r   r      s     }==?)%#n*=*A0%+3
 ~&!+q0AT0III))?goo5ooFI 5 / 7H_ &0 &# r0   c                     0 nU R                  5        H-  u  p#X1;  a  [        U/5      X'   M  X   R                  U5        M/     0 nU R                  5        H  u  p#[        X   5      n[	        X55      XB'   M!     U$ ri   )itemsr   addr   r#   )r   buf_name_to_n_noder   r   node_name_to_buf_metan_nodes         r1   get_node_name_to_buf_metar      s     4::<	-+5yk+B((,,Y7	  = 4::<	'12+28+D(  = ! r0   rL   c                     0 n[        X5        Uc  g[        U5      nU R                  R                   H=  nUR                  U;   d  M  UR                  UR                  5      UR                  S'   M?     g)rP   Nbuf_meta)r   r   rG   r3   r$   getr>   )rL   rN   r   r   rJ   s        r1   annotate_orig_fx_with_snodesr     sc     -/(G$56KL99--$9$=$=dii$HDIIj! r0   c               #     #    [         R                  R                  SS5      S:H  n SS Kn[        R
                  " UR                  R                  R                  5      n[        R                  " 5       nU (       d   S v   UR                  5         g UR                  [        SS5      5        [         R                  R                  [!        5       S5      n[         R"                  " USS9  [        R$                  " [         R                  R                  US	['        5        S
35      5      nUR)                  [        R*                  5        UR-                  [        R.                  " S5      5        UR1                  U5         S v   UR3                  U5        UR                  5         g ! UR                  5         f = f! UR3                  U5        UR                  5         f = f7f)NTORCH_COMPILE_DEBUG01r   z*functorch.compile.config.debug_partitionerTtorchinductor)exist_okaot_z
_debug.log3[%(filename)s:%(lineno)d %(levelname)s] %(message)s)osenvironr   torch._functorch.aot_autogradlogging	getLogger
_functorchaot_autogradrW   
contextlib	ExitStackcloseenter_contextr	   pathjoinr   makedirsFileHandlerr   setLevelDEBUGsetFormatter	Formatter
addHandlerremoveHandler)compile_debugry   r;   stackr   fhs         r1   enable_aot_loggingr     sY    JJNN#8#>#EM(


E,,99BB
CC  "E	KKM 
JDQR77<<9DKKt$			
%'(
3	

B KKOOOP NN2"3 KKM0 	"s7   A8G;F$ ?C>G>F9 "G$F66G9#GG _inductor_post_to_pre_grad_nodes_pre_grad_graph_idc                      \ rS rSr% \R
                  " 5       r0 r\\	\
\	   4   \S'   \S\	S\\	   4S j5       rSS jrS\	SS4S	 jr S S
\	S\	S\S\S\\   4
S jjr\R*                   S S
\	S\	S\S\S\\\      4
S jj5       rS\	S\	4S jrSS jrSS jrS
\	S\SS4S jrS\\\      S\\   S\\   SS4S jrSS jr S\	S\\!S      4S jr"Sr#g)!DebugContextiD  ._inductor_triton_kernel_to_post_grad_node_infofolder_namer'   c                 P   [         R                  R                  =(       d
    [        5       n[        R
                   Hg  n[        R                  R                  USU  SU 35      n[        R                  R                  U5      (       a  MO  [        R                  " U5        Us  $    g )Nr   .)r   rI   	debug_dirr   r   _counterr   r   r   existsr   )r   r   ndirnames       r1   create_debug_dirDebugContext.create_debug_dirJ  sy    LL**=mo	&&Aggll-q$G
 77>>'**G$ ' r0   Nc                 T    S U l         S U l        [        R                  " 5       U l        g ri   )_prof_pathr   r   _stack)selfs    r1   __init__DebugContext.__init__X  s     

 **,r0   new_pathc                    U R                   (       d  g UR                  S5      (       d   U5       eSSKJn   U" U S35         [        R
                  R                  U5      (       a  [        R                  " U5        [        R                  " U R                   U5        S S S 5        g ! , (       d  f       g = f! [         a$    [        R                  SU R                   U5         g f = f)Nz.debugr   )FileLockz.lockz(Failed to copy debug files from %s to %s)r   endswithfilelockr   r   r   r   shutilrmtreecopytreeOSErrorr;   r<   )r   r   r   s      r1   copyDebugContext.copy]  s    zz  **4H4*%	XJe,-77>>(++MM(+

H5 .--  	KK:DJJ	s0   B8 AB'B8 '
B51B8 5B8 8+C&%C&filename
write_moderR   rf   c                     U R                   (       d   e[        [        R                  R	                  U R                   U5      U/UQ70 UD6$ ri   r   openr   r   r   )r   r   r   rR   rf   s        r1   fopenDebugContext.fopenm  s:     zzzBGGLLX6
TTTVTTr0   c              /      #    U R                   (       d   e[        [        R                  R	                  U R                   U5      U/UQ70 UD6 nUv   S S S 5        g ! , (       d  f       g = f7fri   r   )r   r   r   rR   rf   fs         r1   fopen_contextDebugContext.fopen_contextw  sN      zzz"'',,tzz84jR4R6RVWG SRRs   AA-A	A-
A*&A-suffixc                 |    U R                   (       d   e[        R                  R                  U R                   U5      $ ri   )r   r   r   r   )r   r   s     r1   r   DebugContext.filename  s'    zzzww||DJJ//r0   c                 *   [         R                  R                  b  SS KnU R                  (       d   e[
        R                  R                  U R                  [
        R                  R                  U R                  5       S35      nUR                  US5       nUR                  U R                  [
        R                  R                  U R                  5      S9  S S S 5        [         R                  R                  U5        g g ! , (       d  f       N/= f)Nr   z.tar.gzzw:gz)arcname)r   rI   
upload_tartarfiler   r   r   r   basenamer   r   )r   r  tar_filetars       r1   r  DebugContext.upload_tar  s    <<"".:::ww||

rww//

;<GDH h/3

BGG,<,<TZZ,HI 0LL##H- / 0/s   AD
Dc                   ^ [         R                  (       al  [        R                  " S5      mTR                  nTR                  [        R                  5        S[        SS 4U4S jjnU R                  R                  X!5        U R                  R                  [        R                  " U 5      5        [         R                  R                  (       d  g U R                  [!        5       5      U l        [         R                  R$                  (       a   U R'                  S[        R                  5        [         R                  R(                  (       a!  U R'                  S[        R*                  5        g g )Nztorch._dynamolevelr'   c                 (   > TR                  U 5        g ri   )r   )r	  r;   s    r1   reset_log_level/DebugContext.__enter__.<locals>.reset_log_level  s    U#r0   z	debug.logzinfo.log)r   debugr   r   r	  r   r   r   r   callbackr   r    set_debug_handlerrI   enabledr   r   r   	debug_log_setup_log_captureinfo_logINFO)r   
prev_levelr  r;   s      @r1   	__enter__DebugContext.__enter__  s    <<##O4CJLL'$s $t $ KK  =!!!"5"5d";<||##**+=+?@
<<!!##K?<<  ##J= !r0   r	  c                    [         R                  " S5      nU R                  R                  U R	                  U5      5      n[         R
                  " U5      nUR                  U5        UR                  [         R                  " S5      5        UR                  U5        UR                  [        UR                  U5      5        U R                  R                  UR                  U5        g )Nztorch._inductorr   )r   r   r   r   r   StreamHandlerr   r   r   r   minr	  r  r   )r   r   r	  r;   fdchs         r1   r  DebugContext._setup_log_capture  s    
  12[[&&tzz(';<""2&
E
ST	
 	rSE*+S..3r0   exc_typeexc_valexc_tbc                 B   U R                   (       a*  U R                   R                  5         U R                  5         U R                  (       a9  U R	                  5         [
        R                  S[        5       U R                  5        U R                  R                  5         g )Nz%s debug trace: %s)
r   disable_save_profile_datar   r  r;   r<   r   r   r   )r   r  r  r   s       r1   __exit__DebugContext.__exit__  sa     ::JJ ##%::OOKK,.F.H$**Ur0   c                    U R                   (       d   eU R                   R                  U R                  S5      5        U R                  S5       n[        R
                  " U R                   US9nUR                  5         UR                  S5        UR                  S5        UR                  S5        UR                  S5        S S S 5        g ! , (       d  f       g = f)Nzcompile.profzcompile.stats)streamcumtimed   tottime)	r   
dump_statsr   r   pstatsStats
strip_dirs
sort_statsprint_stats)r   r  statss      r1   r#  DebugContext._save_profile_data  s    zzz

dmmN;<ZZ(BLLB7EY'c"Y'c" )((s   A4C
Cr$   ).Nc                    [         R                  R                  (       a5  [        [         R                  U5      (       a   [        [	        U 5      U5      $ S[        S[        SS 4S jnU$ ! [
         a    [        R                  SSS9   g f = f)Nz Ignoring exception in debug codeT)exc_inforR   rf   r'   c                      g ri   r/   re   s     r1   ignored)DebugContext.__getattr__.<locals>.ignored  s    r0   )	r   rI   r  getattrDebugFormatter	Exceptionr;   r<   r   )r   r$   r6  s      r1   __getattr__DebugContext.__getattr__  s|    <<GFLL$$?$?~d3T::s c d  N  >Ns    A* *B
B)r   r   r   )r'   N)w)$rW   
__module____qualname____firstlineno__	itertoolscountr   r   dictrv   list__annotations__staticmethodr   r   r   r   r   r   r   r   contextmanagerr   r   r   r  r  rB   r  r\   BaseExceptionr$  r#  r   r;  __static_attributes__r/   r0   r1   r   r   D  s    H LN2Dd3i4HMc hsm  -
S T & UU U 	U
 U 
CU  		 	 		
 	 
"S'		 	0s 0s 0
.>.44 4 
	4 4./ -( 	
 
	# )1D(E r0   r   c                      \ rS rSrS\SS4S jrS\R                  R                  S\	\R                     SS4S jrS\R                  R                  S\	\R                     SS4S	 jrS
\SS4S jrS
\SS4S jr\S
\S\4S j5       rS
\SS4S jrS\R                  R                  S
\SS4S jrS\SS4S jr SS\S\\\\	\   4   \\\4   4   4S jjrS\S\	\R6                     S\S\4   S\S\SS4S jrSrg)r9  i  handlerr'   Nc                 v    UR                   U l         UR                  U l        UR                  U l        Xl        g ri   )r   r   r   rK  )r   rK  s     r1   r   DebugFormatter.__init__  s-    ]]
$22((r0   rL   inputsc                    U R                  S5       nS n[        R                  R                  R                  R
                  (       aR  [        R                  R                  R                  U5      n[        R                  R                  UR                  5      n[        R                  R                  R                  R
                  n[        R                  R                  R                  SSS.5         [        UUUSUUS9  S S S 5        S S S 5        U R                  S5       nUR                  UR!                  SS95        S S S 5        g ! , (       d  f       NO= f! , (       d  f       NX= f! , (       d  f       g = f)Nzfx_graph_runnable.pyF)ztrace.enabledztrace.save_real_tensorsinductor)save_dirstable_hashzfx_graph_readable.pyprint_output)r   ry   r~   r   rI   save_real_tensors_subclasses
fake_utilstry_convert_fake_to_realr   r   r   r$   r	   r   writeprint_readable)r   rL   rN  r  rQ  rR  s         r1   fx_graphDebugFormatter.fx_graph  s   
 ZZ./2H%%++==**55NNvV77??2773
  //0066HHK''--"'EJ !% + 0, ZZ./2HHR&&E&:; 0/  0/, 0/s0   C"E4EE$E.
E	E
E+.
E<c                     U R                  S5       nUR                  UR                  SS95        S S S 5        g ! , (       d  f       g = f)Nzfx_graph_transformed.pyFrS  )r   rY  rZ  )r   rL   rN  r  s       r1   fx_graph_transformed#DebugFormatter.fx_graph_transformed  s9    
 ZZ12bHHR&&E&:; 322s	   :
Ar3   c                     U R                  S5       nUR                  U R                  U5      5        S S S 5        g ! , (       d  f       g = f)Nzir_pre_fusion.txtr   rY  	_write_irr   r3   r  s      r1   r!   DebugFormatter.ir_pre_fusion  s2    ZZ+,HHT^^E*+ -,,	   !<
A
c                     U R                  S5       nUR                  U R                  U5      5        S S S 5        g ! , (       d  f       g = f)Nzir_post_fusion.txtra  rc  s      r1   r"   DebugFormatter.ir_post_fusion  s2    ZZ,-HHT^^E*+ .--re  c                     [         R                  " 5       nU  H3  nUR                  UR                  5       5        UR                  S5        M5     UR	                  5       $ )Nz


)ioStringIOrY  	debug_strgetvalue)r3   rq   rJ   s      r1   rb  DebugFormatter._write_ir  sD    kkmDIIdnn&'IIh  ||~r0   c                 4    [        XR                  S5      S9  g )Nzgraph_diagram.svg)r5   )rM   r   )r   r3   s     r1   graph_diagramDebugFormatter.graph_diagram   s    U--0C"DEr0   c           	          [        X5        [        UU R                  S5      S[        S[        R
                  R                  S9  g )Nzorig_fx_graph_diagram.svgFT)r5   r9   progparse_stack_tracer:   )r   r
   r   GRAPHVIZ_COMMAND_SCALABLEr   rI   r:   )r   rL   r3   s      r1   draw_orig_fx_graph!DebugFormatter.draw_orig_fx_graph#  s:    
 	%R/-- ;<*""LL88	
r0   r   c                 N    [         R                  " XR                  S5      5        g )Nzoutput_code.py)r   r   r   )r   r   s     r1   output_codeDebugFormatter.output_code2  s    Hmm,<=>r0   c                    0 nU R                  US5       n[        R                  SUR                  5        [        R
                  n[        R                  " X#5        S S S 5        0 n[        (       aI  U R                  SS5       n[        [        [        U5      n[        R                  " XC5        S S S 5        X$4$ X$4$ ! , (       d  f       Ng= f! , (       d  f       X$4$ = f)Nr=  z/Writing provenance tracing debugging info to %sz/inductor_provenance_tracking_node_mappings.json)r   r;   infor$   r   r   jsondumpr   create_node_mappingr   )r   r   
debug_infor  node_mappings        r1   1log_inductor_triton_kernel_to_post_grad_node_info@DebugFormatter.log_inductor_triton_kernel_to_post_grad_node_info5  s     
ZZ#&"HHFP%TTJIIj% ' A32&(H*  		,+ ''z'' '&  ''s   AB=,C=
C
Cr$   input_nodestimingsChoiceCallerelapseprecompile_elapsec           	      X  ^^ SSK Jm  S[         R                  S[        [        [        4   4UU4S jjmU[
        R                  R                  5       [
        R                  R                  5       U Vs/ s H  nT" U5      PM     snUUS.nU R                  SSS	S
9 nUR                  5        HZ  u  p[        U	R                  5       5      nUR                  U5        XS'   [        R                  " X5        UR                  S5        M\     S S S 5        g s  snf ! , (       d  f       g = f)Nr   )FixedLayoutrJ   r'   c           	      R  > [        U S5      (       a  U R                  nOSnU[        U 5      R                  S.n U R	                  5       n[        UT5      (       a  Sn [        UR                  5      nT" UR                  UR                  / [        R                  R                  R                  UR                   5      Q/ [        R                  R                  R                  UR"                  5      QUS9n[%        U5      US'   O[%        U5      US'     [%        U R'                  5       5      US'    [%        U R)                  5       5      US	'    [%        [        R                  R                  R                  U R+                  5       5      5      US
'    [%        [        R                  R                  R                  U R-                  5       5      5      US'    [%        [        R                  R                  R                  U R/                  5       5      5      US'   [        U S5      (       a>  [        U R0                  [2        R4                  5      (       a  T" U R0                  5      US'   U$ ! [         aI     [        R                  R                  R                  UR                  SS9n GN9! [         a      GNGf = ff = f! [         a     GNf = f! [         a     GNf = f! [         a     GNf = f! [         a     GNof = f! [         a     GN<f = f! [         a     GN	f = f)Nr$    )r$   r\   r   )fallback)rE   sizestrideoffsetlayoutrE   rd   r  r  numelrD   )r   r$   r\   rW   get_output_specr@   rB   r  r:  r    rG   sizevars	size_hintrd   rE   
size_hintsr  r  rv   	get_dtyperc   
get_strideget_size	get_numelrD   r   IRNode)rJ   r   	node_infor  r  static_layoutr  build_node_infos         r1   r  >DebugFormatter.log_autotuning_results.<locals>.build_node_infoR  s   tV$$ II		!T
++I--/fk22F!!$V]]!3 %0$llHqww//::6;;GHL!1!1!<!<V]]!KL%%M +.m*<Ih'*-f+Ih'%()9%:	'"&)$//*;&<	(#&)GG$$//0AB'	(#
$'(8(8(C(CDMMO(T$U	&!%()9)9)C)CDNNDT)U%V	'" tV$$DIIryy)I)I$3DII$>	&!W % !!%&WW%5%5%?%? & &@ &F  ) ! !!"            s   #K I- 4BK K K 4K% AK6 AL AL -
K 82J-*K -
J<7K 8K ;J<<K  K 
KK
K"!K"%
K32K36
LL
LL
L&%L&)op_namecuda_device_namecuda_device_countr  autotuning_timeprecompile_timezautotuning_result_json_list.txtatzutf-8)encodingbenchmark_result
)r   r  r  rC  rv   ry   cudaget_device_namedevice_countr   r   	info_dictupdater|  r}  rY  )r   r$   r  r  r  r  rJ   general_propertiesr  callertimer  r  r  s               @@r1   log_autotuning_results%DebugFormatter.log_autotuning_resultsH  s    	$:	")) :	S#X :	 :	z  %

 : : <!&!8!8!:>IJkdOD1kJ%0
 -tg   
 ' !1!1!34	  !3404,-		)( !0
 
	 K
 
s   4D
A/D
D))r   r   r   rK  )z.inductor_triton_kernel_to_post_grad_nodes.json)rW   r>  r?  r@  r   r   ry   r   r   rD  Tensorr[  r^  SchedulerNodeListr!   r"   rF  rv   rb  ro  ru  rx  rA   rC  r   r  r   r  floatr  rI  r/   r0   r1   r9  r9    s     <HH  < U\\"< 
	<<<HH  < U\\"< 
	<,#4 , ,,$5 ,$ , * s  F#4 F F
HH  
 !
 
	
?C ?D ? O((	tCcN#T#s(^3	4(&VV "))_V ne+,	V
 V !V 
Vr0   r9  c                     [         R                  [        R                  5      (       a)  [         R	                  S[
        R                  U 5      5        [        R                  R                  U 5        g )NzBEFORE FUSION
%s)
ir_pre_fusion_logisEnabledForr   r  r{  r9  rb  r    r  r!   r3   s    r1   log_ir_pre_fusionr    sE    %%gll332N4L4LU4STGG% r0   c                     [         R                  [        R                  5      (       a)  [         R	                  S[
        R                  U 5      5        [        R                  R                  U 5        g )NzAFTER FUSION
%s)
ir_post_fusion_logr  r   r  r{  r9  rb  r    r  r"   r  s    r1   log_ir_post_fusionr    sE    &&w||44 2N4L4LU4STGG5!r0   c                   >    \ rS rSr% \\S'   \R                  \S'   Srg)TensorMetadataHolderi  tensor_metadatard   r/   N)	rW   r>  r?  r@  r   rE  ry   rd   rI  r/   r0   r1   r  r    s    ##LLr0   r  pre_grad_graph_idpost_to_pre_grad_nodes_jsontriton_kernel_to_post_grad_jsonc           	      r  ^ 0 0 0 0 S.n[         R                  S5        [        U[        5      (       d  [         R	                  S5        U$ [        U[        5      (       d  [         R	                  S5        U$ [        U [
        5      (       d  [         R	                  S5        U$ [        R                  " [        5      n[        R                  " [        5      n[        R                  " [        5      n UR                  5        HO  u  px[        U[        5      (       d  [         R	                  S5        Us  $ U H  n	Xi   R                  U5        M     MQ     S[        [        [        4   S[        4S	 jn
UR                  5        GH[  u  px[        U[        5      (       d  [         R	                  S
5        Us  $ U GH   nU
" U5      (       d  Us  s  $ UR                  S5      U :X  a,  XKS      R                  U5        XW   R                  US   5        UR                  S/ 5       Vs/ s H  oU4PM     nnU(       d  M  UR!                  5       u  nmU
" U5      (       d  Us  s  $ UR                  S5      U :X  a-  XNS      R                  T5        UT   R                  US   5        UR#                  U4S jUR                  S/ 5       5       5        U(       a  M  GM#     GM^     S[        [        [        4   SS4S jnU" U5        U" U5        U" U5        UUUUS.$ s  snf ! [$         a  n[         R	                  SU5        [         R	                  SU5        [         R	                  SU5        [         R	                  SU 5        [         R	                  [&        R(                  " 5       5        Us SnA$ SnAff = f)zCreate bidirectional mappings between:

- pre_grad graph nodes and post_grad graph code nodes, and vice versa
- triton kernel name and post_grad graph code nodes, and vice versa
)	preToPost	postToPrecppCodeToPostpostToCppCodez.Creating node mappings for provenance trackingzCProvenance tacking error: post_to_pre_grad_nodes_json is not a dictzGProvenance tacking error: triton_kernel_to_post_grad_json is not a dictz9Provenance tacking error: pre_grad_graph_id is not an intzMProvenance tacking error: triton_kernel_to_post_grad_json value is not a listrJ   r'   c                     [        U [        5      (       d  [        R                  S5        gSU ;  d  SU ;  d  SU ;  a  [        R                  S5        gg)NzVProvenance tacking error: node provenance in post_to_pre_grad_nodes_json is not a dictFgraph_idr$   	from_nodezYProvenance tacking error: node provenance in post_to_pre_grad_nodes_json has wrong formatT)r@   rC  r;   error)rJ   s    r1   check_format)create_node_mapping.<locals>.check_format  sQ    dD))		l %t);{RV?V		o r0   zIProvenance tacking error: post_to_pre_grad_nodes_json value is not a listr  r$   r  c              3   *   >#    U  H  oT4v   M
     g 7fri   r/   )rj   r   
parent_keys     r1   rm   &create_node_mapping.<locals>.<genexpr>  s      !1RAJ1Rs   dNc                 J    U  H  n[        X   5      X'   M     [        U 5      n g ri   )rD  rC  )r  keys     r1   convert_sets_to_lists2create_node_mapping.<locals>.convert_sets_to_lists  s"    af QAr0   z+Unexpected error in create_node_mapping: %sz post_to_pre_grad_nodes_json:  %sz$triton_kernel_to_post_grad_json:  %szpre_grad_graph_id:  %s)r;   r{  r@   rC  r  rB   rw   defaultdictr   r   rD  r   rv   r   r   r   popextendr:  	traceback
format_exc)r  r  r  empty_returnpre_to_postpost_to_prepost_to_cpp_code	outer_key
node_array	curr_noder  rJ   r   r   current_noder  er  s                    @r1   r~  r~    sI    	/L HH=>1488		WX5t<<		U	
 '--		MN"-"9"9*"EK"-"9"9*"EK'2'>'>z'JK%D%J%J%L!Ij$//		c $#'	 +//	: ( &M	tCH~ 	$ 	 &A%F%F%H!Ij$//		_ $#"#D))''88J'+<<V-11)<*..tF|< 26+r1JK1JAY1JKe/4yy{,L*'55++#''
37HH#$89==jI#J/33L4HILL !1=1A1A+r1R!  e # &I6	T#s(^ 	 	 	k*k*./$$<-	
 	
+ L6  
 			?C		46QR		24S	
 			*,=>		)&&()
sZ   1AL  4BL  6L  AL  $L1L  <%L  "A4L  AL  L   
N6*BN1+N61N6rR   rf   c                     Sn[         R                  R                  U5      (       d  [         R                  " U5        S[        S[        4S jn[        X0U45      u  pESnU SU S[        [        5       S3n[        US	5       n[        R                  " XE4U5        S
S
S
5        [        R                  [        R                  5      (       a  SU SU< S3n	[        U	5        g
g
! , (       d  f       NM= f)z
This function is used to save arguments for a compile_fx_inner function call
to the file system.  Later on one can replay the compile_fx_inner call
with the saved arguments using load_args_and_run_compile_fx_inner.
z/tmp/inductor_saved_argsrk   r'   c                     [        U [        R                  5      (       a  [        [	        U 5      U R
                  5      $ U $ )z
Pickle FakeTensor will result in error:
AttributeError: Can't pickle local object 'WeakValueDictionary.__init__.<locals>.remove'

Convert all Tensor to metadata. This may also makes pickle faster.
)r@   ry   r  r  r   rd   rk   s    r1   handle_tensor5save_args_for_compile_fx_inner.<locals>.handle_tensor9  s1     a&&'(@(CQXXNNHr0   compile_fx_inner/_z.pklwbNz3
Arguments for a compile_fx_inner call is saved to z. To replay the call,
run the following:

from torch._inductor.debug import load_args_and_run_compile_fx_inner
load_args_and_run_compile_fx_inner(z
)
        )r   r   r   mkdirr   r   nextsave_args_cntr   pickler}  r;   r  r   r   rF   )
rR   rf   folderr  args_to_savekwargs_to_savefn_namer   r   messages
             r1   save_args_for_compile_fx_innerr  .  s     (F77>>&!!

 
 
 $,M&>#J L GXQwiqm!4 5T:D	dD	Q\2A6 
 &&337& 9$ %)8 ,	 	g ' 
	s   C##
C1r   c                    SSK Jn  [        U S5       n[        R                  " U5      u  p4S S S 5        S[
        S[
        4S jn[        R                  R                  SS9nU   [        R                  " S	S
5         [        UWW45      u  p4U" U0 UD6sS S S 5        sS S S 5        $ ! , (       d  f       N= f! , (       d  f       O= fS S S 5        g ! , (       d  f       g = f)Nr   )r  rbrk   r'   c                    [        U [        5      (       ar  [        R                  R                  R                  U R                  R                  U R                  R                  U R                  R                  U R                  5      $ U $ ri   )r@   r  ry   _dynamotestingrand_stridedr  shaper  rE   rd   r  s    r1   r  9load_args_and_run_compile_fx_inner.<locals>.handle_tensorb  sh    a-..==((55!!''!!((!!''	  Hr0   T)allow_non_fake_inputs	save_argsF)torch._inductor.compile_fxr  r   r  loadr   ry   rV  FakeTensorModer   r	   r   )r   r  r   rR   rf   r  	fake_modes          r1   "load_args_and_run_compile_fx_innerr  \  s    ;	dD	Q{{1~ 
	 	 	 !!00t0LI	FLLe4f~>00 54 
	 54s/   B'$C<B8	C'
B58
C	C
C )package_pathfunc.exported_programinductor_configsr  c          	         SSK Jn  SSKJn  SSKJn  SSKJn  UR                  R                  nUR                  5       n	[        U	[        R                  R                  5      (       d   eUR                  u  p U(       a"  UR                  R                   S:X  a  U" USUS9  U(       a  UR                  R                   S	:X  a  ["        R$                  " U	5      n["        R$                  " UR                  5      n["        R$                  " U5      nU" UUS   US
   US9u  p['        U5      n[        R(                  R)                  UUSS9nU " UR                  5       UUUSSS9  U " U	U
UUUUS9$ ! U a%  nU" USSUS9  [*        R-                  S5        UeS nAf[.         a5  nU(       a'  SnUR                  R                   S
:X  a  SnU" USUUS9  UeS nAff = f)Nr   )AccuracyError)dump_to_minify)r   )_aoti_flatten_inputs   aot_inductor)options   r   F)strictTaccuracy)r	  r  load_and_runcheck_accuracy)r	  r  r  aot_inductor_accuracyminify)commandr  zAccuracy failedrun)torch._dynamo.debug_utilsr  torch._dynamo.repro.aotir  torch._inductorr   r  r  r  dump_aoti_minifiermoduler@   ry   r   r   example_inputsrepro_levelr   deepcopyrA   exportr;   r<   r:  )r  r  r	  r  r  r  r   r  use_minifierrL   rR   rf   gm_copyexample_inputs_copyconfig_copyflat_example_inputstuple_inputsflattened_epr  r  s                       r1   aot_inductor_minifier_wrapperr*  s  s    87&?&&99L		 	 	"Bb%((..////#22LD>F//;;q@ (
 F//;;q@ mmB'G"&--0@0O0O"P--(89K/C#A&#A&#	0, !!45L <<..wU.SL##%!,)!) -%%
 	
  #$		
 	%& G""..!3 (	 s%   9C4E. .G4 FG!0GG)FNrT   )grw   r   r   dataclasses	functoolsri  rA  r|  r   r   os.pathr  r,  r   r+   r  collections.abcr   typingr   r   r   r   r   unittest.mockr	   ry   functorch.compiler
   r   r   r   torch._dynamo.repro.after_aotr   torch._dynamo.utilsr   torch._loggingr   torch.fx.graph_moduler   torch.fx.passes.shape_propr   r   torch.fx.passes.tools_commonr   torch.typesr   torch.utils._ordered_setr   torch.utils._pytreer   r  r   r   	schedulerr   r   r   r   r   virtualizedr    r   rW   r;   r  r  rD  r  rx   r#   rt  	lru_cacher   r2   rv   rM   rz   r=   rC  rB   r   r   r   rG  r   r   rE  r   r   r9  r  r  	dataclassr  rB  r  r~  r  r  r"  ExportedProgramr*  r/   r0   r1   <module>r@     s        	    	       $ 5 5   V V  : - , - O 7   / (    !%h@ &x1AB I 

 
 VZ,@
AT  T   +!"++ C=+ 
	+\_$'8"9 _bhh _J &*	 %& S>  c]  	 
 
 F!S>!	#w,!"III 
I" %HTN % %V 46  $sCx. 5$( HSM (\ \~{ {|!. !4 !"/ "D "   
 !ss!%c3hs &*#s(^s 
#tCH~
	sl+# + + +\1S 1S 18 (,Q
38
Qll22Q 38n	Q
 8$Q 	Qr0   