
    IЦiqB                        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rS SKJrJrJrJrJrJrJrJrJr  S SKJr  S SKJr  \R@                  " \!5      r"\
" S5      r#\#RI                  5       r% " S S	\&5      r' S'S
\S\(S\\\(\4      4S jjr)S r*SSSSSSSSSSSS.S
\\   S\\RV                  RX                     S\\\      S\\\(\(4      4S jjr-SSSS.S jr.SSSS.S\\\(\(4      4S jjr/S r0S r1S r2S r3 S(S\\RV                  RX                     4S  jjr4S! r5SSS"SSSS#SS$.S\\\(\(4      S%\\6\(4   4S& jjr7g))    N)import_module)AnyDictOptionalTupleUnion)	_cuda_system_info_commentBuckTargetWriterextra_importsgenerate_config_stringhelper_for_dump_minifyInputReaderminifier_dirNNModuleToStringNopInputReader)ExportedProgram)tqdmztorch._inductor.configc                   (   ^  \ rS rSrU 4S jrSrU =r$ )AOTIMinifierError%   c                 T   > SnU S[        U5       3n[        TU ]	  U5        Xl        g )NzRThis error is caused by a bug in the AOTI minifier, please report a bug to PyTorchz: )strsuper__init__original_exception)selfr   additional_messagefull_message	__class__s       W/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/_dynamo/repro/aoti.pyr   AOTIMinifierError.__init__&   s4    q,-R4F0G/HI&"4    )r   )__name__
__module____qualname____firstlineno__r   __static_attributes____classcell__)r   s   @r    r   r   %   s    5 5r"   r   exported_programcompiler_nameoptionsc           	      <   [         R                  " 5       n[        R                  R	                  [        5       S5      n[        R                  R                  U5      (       d  [        R                  " USS9  [        UUU USUS9  [        UR                  5       5      $ )NcheckpointsTexist_okminify)r)   save_dircommandconfig_patches)ioStringIOospathjoinr   existsmakedirssave_graph_repro_epr   getvalue)r)   r*   r+   outsubdirs        r    dump_to_minifyr?   -   ss    
 ++-CWW\\,.-8F77>>&!!
FT*) "#,,.11r"   c                 D    S n[         R                  " U 5      nU" U5      $ )Nc                 $   U R                  S5      n[        U5      S:X  a  SU -   $ UR                  S5      n[        [        U5      5       H(  nX   nUR	                  5       S:w  a	  SU-   X'   M$  SX'   M*     SR                  U5      nUS-   U-   nU$ )N
   z# r    )splitlenpoprangestripr8   )s_sfirstilines        r    _convert_to_comment.get_module_string.<locals>._convert_to_commentB   s    HHTNq6Q;"9as1vA4Dzz|r!d{  IIaLDL1r"   )r   convert)gmrO   module_strings      r    get_module_stringrT   A   s$     %,,R0M}--r"   Frun)r)   rR   argsr3   stable_outputr1   r2   accuracy	check_strmodule_in_commentstrictrR   rV   r3   c                   Uc  Uc  [        S5      eUb  Ub  [        S5      eUb  Uc  [        S5      eUc)  Uc   eUc   e[        R                  R                  X4US9nOUc  UR                  5       n[	        U5      nU R                  U5        U R                  [        UUUUS95        U	c  SU;   n	U R                  S5        U R                  S5        U R                  S	U	< S
U< SU< SU
< S3	5        g )Nz.One of exported_program and gm must be definedz2Only one of exported_program and gm can be definedz-If gm is defined, args should also be definedr[   r+   rW   r1   	_accuracyzif __name__ == '__main__':
z3    from torch._dynamo.repro.aoti import run_repro
zf    with torch.no_grad():
        run_repro(exported_program, config_patches=config_patches, accuracy=z
, command=z, save_dir=z, check_str=z)
)r   torchexportmodulerT   write(generate_compiler_repro_exported_program)fdr*   r)   rR   rV   r3   rW   r1   r2   rX   rY   rZ   r[   rS   s                 r    r;   r;   U   s-   ( BJ PQQ# TUU	~$, OPP~~ <<..r.G	$$& &b)MHH] HH0"'		
 -/HH+,HHCDHHWW_Vbblmtlw x<|I=	=r"   )r3   rX   r[   c                   [         R                  R                  [        5       S5      n[         R                  R	                  U5      (       d  [         R
                  " USS9  [         R                  R                  U[        U R                  R                  5       S35      n[        R                  S[        U R                  R                  5      U5        [        US5       n[        UUU [        U5      UUUSUS9	  S S S 5        [         R                  " 5       n	[         R                  R                  U	S5      n
 [        R                   " Xz5        [        R                  S	U
5        ["        (       a  [%        U5      R'                  5         g g ! , (       d  f       N= f! [(         a    [        R                  S
U
5         g f = f)Nr-   Tr.   z.pyz&Writing checkpoint with %s nodes to %sw)rR   rV   r3   r1   rX   rZ   r[   zrepro.pyz(Copying repro file for convenience to %szNo write permissions for %s)r6   r7   r8   r   r9   r:   rF   graphnodeslogwarningopenr;   tuplegetcwdshutilcopyfileuse_buckr
   rc   OSError)rR   rV   r*   r3   rX   r[   r>   	file_namere   curdir
repro_paths              r    dump_compiler_graph_staterv      sB    WW\\,.-8F77>>&!!
FT*VBHHNN(;'<C%@AIKK0#bhhnn2Ey 
i	t)"
	
 
 YY[Ffj1J?	.>
K8Y'--/ # 
	&  ?1:>?s   "F;AF 
F G Gr^   c                l   [         R                  " S[        US9 S[         S35      nU(       d  US[        R
                  R                   S3-  n[        [        R
                  S5      (       a!  US[        R
                  R                   S3-  n[        [        R
                  S	5      (       a!  US
[        R
                  R                   S3-  nU[        5       -  n[        R                  R                  US5      n[        R                  R                  X5        USU S3-  nUS-  nUSU S3-  nU$ )Nz5
import torch
import torch._inductor.inductor_prims

)rW   z!

isolate_fails_code_str = None

z


        z# torch version: rB   cudaz# torch cuda version: git_versionz# torch git version: z


zexported_program.pt2z&exported_program = torch.export.load('z')
z # print(exported_program.graph)
zconfig_patches=)textwrapdedentr   r   r`   version__version__hasattrrx   ry   r	   r6   r7   r8   ra   save)r)   r+   rW   r1   	model_strep_paths         r    rd   rd      s,     m4 5 6  	
	I ()B)B(C2FF	5==&))1%--2D2D1ERHHI5==-0001J1J0K6RRI.00	ggll8%;<G	LL&09'$GGI44I?7)2..Ir"   c                    [        U S5      (       d  [        R                  S5        O0U R                  S:  a   [        R                  SU R                  5        [	        5       nU " U5        [        SUR                  S9 n[        XS9nU " U5        UR                  nS S S 5        [        W5      $ ! , (       d  f       N= f)N_versionzzload_args does not have a _version attribute, please file a bug to PyTorch and describe how you generate this repro scriptr   zload_args is version %s, but this version of PyTorch only supports version 0.  We will try to run it anyway but there may be an incompatibility; if so, try upgrading your version of PyTorch.zLoading inputs)desctotal)r1   pbar)
r~   rj   rk   r   r   r   r   r   rV   rm   )	load_argsr1   
nop_readerr   input_readerrV   s         r    repro_load_argsr      s    9j))>	

 !KK@ ""	  !Jj	#:+;+;	<"H@,   
=
 ; 
=	<s   >B//
B=c                     S[         R                  R                  l        UR	                  5       nUR
                  u  p4X#U4$ )NT)r`   	_inductorconfiggenerate_intermediate_hooksrb   example_inputs)r+   r)   modrV   kwargss        r    repro_commonr      s:    9=EOO6

!
!
#C#22LDfr"   c                 &    [        X5      u  p4nX4U4$ N)r   )r+   r)   r3   r   rV   r   s         r    repro_get_argsr     s    $W?Cvfr"   c                 >   SSK JnJn  [        X5      u  pVnSSKJn  U" UUUSUS9n	U" U	5      n
[        U
[        5      (       a   eSnU H9  n[        U[        R                  5      (       d  M$  UR                  (       d  M7  Sn  O   U
" U0 UD6  U(       a  U" 5         g g )Nr   )_aoti_compile_and_package_inneraoti_load_packagesynchronizeFload_and_runinductor_configsT)torch._inductorr   r   r   
torch.cudar   
isinstancer   r`   Tensoris_cuda)r+   r)   r3   r   r   rR   rV   r   r   package_pathcompiled	need_syncargs                r    	repro_runr     s    R#G>Bf&2
'L !.H(C((((Ic5<<((S[[[I 
 df r"   Treturnc                    SSK JnJn   [        R                  R	                  XUS9nUR                  5       n U $ ! [         a  nU(       a   S nAg [        Xt5      (       a  UR                  UR                  :X  a   S nAg [        U[        5      (       a(  Sn[        R                  " U[        U5      5      b   S nAg [        U5      UeS nAff = f)Nr   )	UserErrorUserErrorTyper]   z/Found .* in output, which is not a known type\.)torch._dynamo.excr   r   r`   ra   rb   	Exceptionr   
error_typeINVALID_OUTPUTRuntimeErrorresearchr   r   )	rR   tuple_inputsr[   skip_export_errorr   r   epepatterns	            r    export_for_aoti_minifierr   $  s     ;*\\  & AYY[	 *a##8T8T(Ta&&HGyy#a&)5")*s'   /: 
CC*C?8C<CCc                 T  ^^^^^^ SSK Jn  SSKJm  SSKJn  [        X5      u  pVnU" XVXrS9u  nmSn	U R                  S;   d   eU R                  S:H  mU R                  mSS	K	J
m  S
mU H9  n
[        U
[        R                  5      (       d  M$  U
R                  (       d  M7  Sm  O   SUUUUUU4S jjnU" UU[        R                   " XR"                  S9[        R                   " [$        U	UTS9U R&                  U R(                  U R*                  U R,                  U R.                  S9	  g )Nr   )minifier)r   )_aoti_flatten_inputs)r+   aot_inductor)dynamopythonr   r   FTc                   > [        U5      n[        XT	TS9n U c  g[        U [        R                  R
                  5      (       d   e T" U USTS9  T(       a  T
" 5         g! [         a!  nUb  U[        U5      ;  a   S nAg S nAgS nAff = f)N)r[   r   FTr   )rm   r   r   r`   fxGraphModuler   repr)rR   flat_example_inputsrY   r   r   r   r   r   r   r[   r   s        r    module_fails"repro_minify.<locals>.module_failsa  s    01%V?P
 :"ehh223333	+!!1	  	$$q')A	s   A   
B*BB)rY   )r*   r3   r[   )r   
dump_stater1   offload_to_diskskip_offloadskip_sanitymax_granularityr   )functorch.compiler   r   r   torch._inductor.compile_fxr   r   minifier_export_moder   r   r   r   r`   r   r   	functoolspartialrY   rv   r1   r   skip_saving_eager_intermediatesr   r   )r+   r)   r3   r   r   r   rV   r   r   r*   r   r   r   r   r   r   r[   r   s               @@@@@@r    repro_minifyr   H  s   *??$W?Cv -A6-)) #M''+????))X5F11&Ic5<<((S[[[I 
 : &&|?P?PQ$$%')	
 !!//<<''//r"   rD   r   )r3   r2   rX   r1   tracing_moderY   r   r   rX   c                  ^ U	 H  n
[         R                  SU
5        M     USL a  Sn[        S5      eUSL a  Sn[        R                  " SU SU S	U< S
U< ST< SU< S3[        R
                  S9nU4S jnUR                  SSSS9nUR                  SSS9nU" U5        UR                  SSS9nU" U5        UR                  SSS9nU" U5        UR                  SSSS9  UR                  SSSS9  UR                  S SS!S9  UR                  S"[        S S#S$9  UR                  S%[        US&S$9  UR                  S'[        US(S$9  UR                  S)[        US*S$9  UR                  S+5      nU" U5        UR                  S%[        US&S$9  S n[        [        R                  5      S,::  a  U/[        R                  S,S  QnUR                  U5      n[         ["        [$        S-.nUUR&                     " UXS.9$ )/NzPUnrecognized kwarg %s; perhaps this repro was made on a newer version of PyTorchTrX   z'check for accuracy is not supported yetFrD   zAn AOTI repro script, typically triggering a bug in PyTorch AOTInductor.
When run with no arguments, this script defaults to running 'z8'.
Extra flags may be available; to find out more, try 'zr --help'.
There are also alternate subcommands available, see below.

default settings on this script:
  accuracy=z
  tracing_mode=z
  save_dir=z
  check_str=rB   )descriptionformatter_classc                 Z   > U R                  S[        TSSS9  U R                  SSSS SS	9  g )
Nz
--save-dirDIRz!directory where saved inputs live)typedefaultmetavarhelpz--no-save-dirr1   store_constz(don't use any directory for saved inputs)destactionconstr   )add_argumentr   )parserr1   s    r    common_flagsrun_repro.<locals>.common_flags  sI    4 	 	
 	 ; 	 	
r"   r2   z{run,minify,analyze})r   r   requiredrU   zjust run the repro)r   r0   zrun the minifier on the reproget_argszget the argsz!--skip-saving-eager-intermediates
store_truez+skip saving eager intermediates on --minify)r   r   z--offload-to-diskzYduring minification, offload delta debugging intermediates to disk.  Use if you're OOMingz--skip-sanityz@skip sanity check at beginning of minification on original graphz--max-granularityz;start at this granularity and work down; must be power of 2)r   r   r   z--check-strzBrequire minified program to fail with error containing this stringz--minifier-export-modezThe export mode used in minifier, either dynamo or python.`dynamo` corresponds to strict=True, and `python` corresponds to strict=False.z--skip-export-errorz1Skip intermediate graphs that cannot be exported.zminifier-queryrC   )r0   rU   r   )r3   )rj   rk   NotImplementedErrorargparseArgumentParserRawTextHelpFormatteradd_subparsers
add_parserr   intr   boolrF   sysargv
parse_argsr   r   r   r2   )r)   r3   r2   rX   r1   r   rY   r   r   more_kwargskr   r   
subparsers
parser_runparser_minifyparser_get_argsparser_minifier_queryrV   r+   COMMAND_FNSs       `                r    	run_repror     s    ^	
  4!"KLL	U	$$>>EY G66=Y ? + / + ,  !55F 
  && 6 ' J &&! ' J ))6 * M  ++J^+LO!+:  
 h  
 O  
 J	   Q	    $]   !@	   '11 &'&&Q	 '  D
388}'#((12,'%G"K
 w'! r"   r   )FT)8r   r   r4   loggingr6   r   ro   r   rz   	importlibr   typingr   r   r   r   r   r`   torch._dynamo.debug_utilsr	   r
   r   r   r   r   r   r   r   torch.exportr   	torch.hubr   	getLoggerr#   rj   inductor_config	is_fbcoderq   r   r   r   r?   rT   nnModuler;   rv   rd   r   r   r   r   r   r   r   r    r"   r    <module>r     s     	  	 	  
  # 4 4 
 
 
 )  !   89$$&5	 5 )-2%22 d38n%2(.0 37$(!%/37 /	7
 	!7 5:
7 T#s(^,7~ $?^ )-" d38n%"J4
> 7;!ehhoo!HEV 04!#!L T#s(^,L
 D#ILr"   