
    sgL                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZ d dlZd dlmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlmZ d dl m!Z! ddl"m#Z# dd	l$m%Z%m&Z& dd
l'm(Z(  ejR                  e*      Z+ e	d      Z,e,j[                         Z.d Z/ G d d      Z0de1fdZ2	 d#dddddZ3d#dZ4d#dZ5d Z6e&d        Z7e&d        Z8d Z9d Z:d Z;d Z<dddddd d!ee=e1f   fd"Z>y)$    N)import_module)Union)AccuracyErrorbackend_accuracy_failsBUCK_CMD_PREFIXBuckTargetWriterextra_importsgenerate_config_stringhelper_for_dump_minifyInputReaderInputWriterminifier_dirNNModuleToStringNopInputReaderrun_fwd_maybe_bwdsame_two_models)fx_placeholder_targets)tqdm   )config)lookup_backendregister_debug_backend)clone_inputs_retaining_gradnessztorch._inductor.configc                 Z    t        | ||t        j                  t        j                        S )Nonly_fwdignore_non_fp)r   r   repro_forward_onlyrepro_ignore_non_fp)gmexample_inputscompiler_fns      S/var/www/html/venv/lib/python3.12/site-packages/torch/_dynamo/repro/after_dynamo.py_accuracy_failsr$   2   s*    !
**00     c                   "    e Zd ZdeddfdZd Zy)WrapBackendDebugcompiler_namereturnNc                     t        j                  |      |        || _        || _        t	        |d      r|j
                  | _        t	        |d      r|j                  | _        t	        |d      r|j                  | _        y y )N__name__r(   get_compiler_config)	functoolswraps_torchdynamo_orig_callable_compiler_namehasattrr+   r(   r,   )selfunconfigured_compiler_fnr(   s      r#   __init__zWrapBackendDebug.__init__=   sv    1	01$7*B'++Z84==DM+_=4BBDM+-BC'?'S'SD$ Dr%   c           	         t        j                  | j                  fi |}t        j                  dv sJ t        j                  dk(  rd }t        j
                  dk(  rt        ||| j                         t        j
                  dk(  r |t        j                  |      |      }t        |||      rt        j                  d       t        t        j                  |t        j                  |j                              || j                         t!        d      } ||       |	  |t        j                  |      |      }t#        ||       |S |S  |||      }|S # t$        $ r}t        j                  d       t        j
                  d	k(  r`t        j                  t&        | j                  
      } |t        j                  |t        j                  |j                              |       n[t        j
                  dk(  rHt        t        j                  |t        j                  |j                              || j                          ||        d }~ww xY w)N)dynamoaotNr6   c                     t         j                  j                  t               d      | _        t
        r<dj                  t        t        | j                        j                  gz         | _	        y y )Nzminifier_launcher.py )
ospathjoinr   minifier_pathuse_buckr   r   cmd_line_pathbuck_command)excs    r#   	add_pathsz,WrapBackendDebug.__call__.<locals>.add_pathsN   sU    $&GGLLAW$X!'*xx'+C,=,=>LLMN(C$ r%         zXAccuracy failed for the TorchDynamo produced graph. Creating script to minify the error.zBad accuracy detected.DCompiled Fx GraphModule failed. Creating script to minify the error.   r(   r   )r-   partialr/   r   repro_afterrepro_leveldump_to_minify_after_dynamor0   copydeepcopyr$   logwarningfxGraphModulegraphr   r   	Exceptiondump_backend_state)	r2   r    r!   kwargsr"   rB   compiled_gmrA   dump_state_fns	            r#   __call__zWrapBackendDebug.__call__H   s   ''(G(GR6R!!%<<<<) !!Q&+B@S@ST !!Q&)$--*;^L"2~{CKKr 0r4==+BC&++
 ((@ACcNI"-dmmB.?"PK%k>B. { &b.9K- ! KK^ ))Q.(1(9(9.d>Q>Q) &NN2t}}RXX/FG  ++q03NN2t}}RXX/FG* //
 cN%s   %(E 	I%C,II)r+   
__module____qualname__strr4   rX    r%   r#   r'   r'   <   s    	T 	T 	T;r%   r'   r(   c                     t        | |      S )a  
    A minifier decorator that wraps the TorchDynamo produced Fx graph modules.
    As opposed to wrap_compiler_debug, this wrapper intercepts at the
    TorchDynamo produced Fx Graph Module. This makes it backend-agnostic to some
    level, e.g., it is useful for minifying issues related to Aot Autograd
    tracing.  If an error is found, we minify and save the minified repro in
    repro.tar.gz.
    )r'   )r3   r(   s     r#   wrap_backend_debugr^      s     4mDDr%   Frun)stable_outputsave_dircommandc                :   t        j                  |       }t        |d      }t        t	        |       |      D ]r  \  }	}
t        |
t        t        j                  f      r|j                  |	|
       9t        |
t        j                        r|j                  |	|
       ft        d|
        dj                  |j                               }t        j                   dt#        |       dt$         d| d| d	|d
|d|dt        j&                         d|d      S )zH
    Generate a repro string for backend-agnostic minified version.
    T)stable_hashz,arg is neither SymInt/int nor torch.Tensor, 
z
from math import inf
import torch
from torch import tensor, device
import torch.fx as fx
import torch._dynamo
from torch._dynamo.testing import rand_strided
from torch._dynamo.debug_utils import run_fwd_maybe_bwd

)r`   z

z
mod = Repro()

z

if __name__ == '__main__':
    from torch._dynamo.repro.after_dynamo import run_repro
    run_repro(mod, load_args, accuracy=z
, command=z,
        save_dir=z, autocast=z
, backend=z)
)r   convertr   zipr   
isinstanceinttorchSymIntsymintTensortensor	TypeErrorr<   linestextwrapdedentr
   r	   is_autocast_enabled)r    argsr(   check_accuracyr`   ra   rb   	model_strwriterplaceholderarg	load_argss               r#   generate_dynamo_fx_repro_stringr{      s/    !((,I t4F 6r :DA RScC./MM+s+U\\*MM+s+J3%PQQR 		&,,.)I??	 m4 5 6 
   ( )7'9G; O[)B)B)D(GzR_Qb c+	 r%   c           
      >   t        j                         }t         j                  j                  t        j                         d      }t         j                  j	                  |      st        j
                  |d       t         j                  j                  |dt        | j                  j                         d      }t        j                  dt        | j                  j                        |       t        |d      5 }|j                  t        | ||||             d	d	d	       t         j                  j                  |d
      }t        j                  d||       t        rt        |      j                          t!        j"                  ||       y	# 1 sw Y   vxY w)z,
    Saves the repro to a repro.py file
    checkpointsTexist_ok	minified_z	_nodes.pyz&Writing checkpoint with %s nodes to %sw)ra   Nzrepro.pyz Copying %s to %s for convenience)r:   getcwdr;   r<   existsmakedirslenrR   nodesrN   rO   openwriter{   r>   r   shutilcopyfile)	r    rt   r(   ru   curdirsubdir	file_namefdlatest_repros	            r#   dump_backend_repro_as_filer      s    YY[FWW\\"))+}5F77>>&!
FT*VyRXX^^1D0EY%OPIKK0#bhhnn2Ey 
i	 

+D-&	

 77<<
3LKK2I|L&,,.
OOI|,
 
s   > FFc                 L    t        j                  |       sJ t        | |||      S )a  
    Dumps the dynamo graph to repro the issue.
    1) It tries to convert Fx GraphModule to a string. If we can, it writes to a
    repro.py file.
    2) If we can't convert Fx GraphModule to a string, we use to_folder to save
    the module and save a tar file.
    )r   can_convert_to_stringr   )r    rt   r(   ru   s       r#   rT   rT      s)     11"555%b$~NNr%   c           
         t         j                  j                  t               d      }t         j                  j	                  |      st        j
                  |d       t        t        | ||t        j                  dk(  |d             y )Nr}   Tr~   rD   minify)ru   ra   rb   )
r:   r;   r<   r   r   r   r   r{   r   rJ   )r    rt   r(   r   s       r#   rK   rK      sc    WW\\,.-8F77>>&!
FT*'!--2	
	r%   c           	      V   ddl m} t        |      }|D cg c]4  }t        |t        j
                        r|j                  j                  n|6 }}	  || |      }t        ||       t        d      c c}w # t        $ r}t        |      }t        j                  d       t        j                  t         |      }	 |	t#        j$                  | t'        j(                  | j*                              |       t        j                  t,        ||      }
 || ||
|	       Y d }~| S d }~ww xY w)Nr   minifierzNo issue was detectedrE   rG   )r"   orig_failuremodule_fails
dump_state)functorch.compiler   r   rh   rj   rk   nodehintr   
ValueErrorrS   r[   rN   rO   r-   rH   rT   rP   rQ   rL   rM   rR   backend_fails)r    r!   r(   r   r"   irV   rA   r   rW   fails_fns              r#   dynamo_minifier_backendr     s   * /K DR>?z!U\\29N 
!"n5+~6011  
3xR	
 "))m
 	bnnRrxx)@A>R$$#%

 	!$		
 	
 I)
s   9A2 A7 7	D( BD##D(c                    ddl m} t        |      }| j                          t	        | ||      rt
        j                  d       t        j                  t        |d      }t        j                  t        |      } |t        j                  | t        j                  | j                              |        || |||       | S t
        j                  d       | S )	Nr   r   z2Accuracy failed for the TorchDynamo produced graphT)r(   ru   )r"   r   z*Input graph does not fail accuracy testing)r   r   r   evalr$   rN   rO   r-   rH   rT   rP   rQ   rL   rM   rR   error)r    r!   r(   r   r"   rW   r   s          r#    dynamo_accuracy_minifier_backendr   :  s    * /K GGI r>;7HI!))mD
 $$#
 	bnnRrxx)@A>R!$		
 I 			>?Ir%   c                     ddl m} 	 t        | t        |              || |      }t        |t        |             y# t        $ r5}t        |      } |d||      j                         dkD  rY d}~yY d}~yd}~ww xY w)a.  
    Minifier uses this function to identify if the minified graph module fails
    with the same error.

    One caveat is that minifier can potentially go into a wrong direction when
    the resulting graph module fails for a different reason. To avoid this, we
    save the string for the original exception and check similarity between new
    and old exception. They can be somewhat different in some cases, when the
    exception string depends on the failing node information. So, we have a
    loose similarity metric to guide the minifier path.
    r   )SequenceMatcherNg      ?TF)difflibr   r   r   rS   r[   ratio)r    r!   r"   r   r   rV   enew_failures           r#   r   r   Y  s~     ("=nMN!"n5+'F~'VW
 	  !f4{;AACcI J	s   3< 	A:&A55A:c                 |   t        |d      st        j                  d       n/|j                  dkD  r t        j                  d|j                         t	               } ||       t        d|j                        5 }t        | j                  |      } ||       |j                  }d d d        |S # 1 sw Y   S xY w)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)ra   pbar)
r1   rN   rO   r   r   r   r   r   ra   rt   )optionsmodrz   
nop_readerr   input_readerrt   s          r#   run_load_argsr   x  s    9j)>	

 !KK@ ""	  !Jj	#:+;+;	< !"G,<,<4H,  !
 K!
 Ks   ;,B11B;c                    t        | ||      }| j                  st        d      }nt        d      }| j                  t	        d      t        j                  || j                        }t        j                  j                  |      |      }t        j                  j                  d| j                        5   ||  d d d        y # 1 sw Y   y xY w)Nr   r   zCompiler name is None - this likely means that a custom compiler was called by torchdynamo. Please remove this error, import your custom compiler function, and replace the backend=None line in run_repro to backend=<my_imported_custom_function>rG   cudaenabled)r   accuracyr   backendRuntimeErrorr-   rH   rj   _dynamooptimizeampautocast)r   r   rz   rt   r"   r   opt_mods          r#   repro_minifyr     s    #y1D $%>?$%GHI
 	
 (//oo mm$$%<=cBG			FG,<,<		=   s   8CCc                    t        j                  j                  | j                        |      }| j                  dk7  r|j                          |j                          t         j                  j                  d| j                        5  t        | ||      }t        |||      sJ d       t        |||t        j                  t        j                        st        d      	 d d d        y t         j                  j                  d| j                        5  t        | ||      }t        ||| j                  d      }~t        | ||      }t        ||| j                  d      }d d d        y # 1 sw Y   y xY w# 1 sw Y   y xY w)	N r   r   zEager itself failedr   zDynamo failedT)r   disable_clone)rj   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   rz   r   rt   refress          r#   	repro_runr     s]   mm$$W__5c:G2
YY0@0@A 	5 #y9D"3T2I4II2"22$88 $O44		5 	5 YY0@0@A 
	 #y9D#TG$4$4DC  #y9D#(8(8C
	 
		5 	5
	 
	s   	AE.AE:.E7:Fr   inductor)rb   r   ra   r   r   r   c                b   |D ]  }t         j                  d|        du rdndu rdt        j                  d| d| dd	d
	t        j                        }	fd}
|	j                  ddd      }|j                  dd      } |
|       |j                  ddd       |j                  dd      } |
|       d }t        t        j                        dk  r|gt        j                  dd  }|	j                  |      }t        t        d} ||j                     || |       y )NzPUnrecognized kwarg %s; perhaps this repro was made on a newer version of PyTorchTr   Fr   zAn after_dynamo repro script, typically triggering a bug in Dynamo or
AOTAutograd.  When run with no arguments, this script defaults to running
'z9'.  Extra flags may be available; to find out more, try 'zs
--help'.  There are also alternate subcommands available, see below.

default settings on this script:
  accuracy=z
  save_dir=re   )descriptionformatter_classc                    | j                         }|j                  ddddd       |j                  dddd	       | j                  d
t        dd       | j                  dddd d       | j                  ddddd       | j                  ddd       | j                  dddd       | j                  d t        d!d"       y )#Nz--no-accuracyr   store_constr   z>do not test accuracy, just run the module and see if it errors)destactionconstdefaulthelpz
--accuracyztest accuracy)r   r   r   r   z
--save-dirDIRz!directory where saved inputs live)typer   metavarr   z--no-save-dirra   z(don't use any directory for saved inputs)r   r   r   r   z--no-isolateisolatestore_falseFz1no isolate (doesn't do anything for after_dynamo))r   r   r   r   z
--autocast
store_truezuse torch.cuda.amp.autocast)r   r   r   z--no-autocastr   z!don't use torch.cuda.amp.autocast)r   r   r   z	--backendBACKENDztorch.compile backend to use)add_mutually_exclusive_groupadd_argumentr[   )parseraccuracy_groupr   r   r   ra   s     r#   common_flagszrun_repro.<locals>.common_flags  s,   <<>## Q 	$ 	
 	##   	$ 	
 	4 	 	
 	 ; 	 	
 	 D 	 	
 	.	 	 	
 	 4	 	 	
 	/ 	 	
r%   rb   z{run,minify})r   r   requiredr_   zjust run the repro)r   z
--only-fwdr   z+don't run backwards compilation for testing)r   r   r   zrun the minifier on the reprorF   )r   r_   )rN   rO   argparseArgumentParserRawTextHelpFormatteradd_subparsers
add_parserr   r   sysargv
parse_argsr   r   rb   )r   rz   rb   r   ra   r   r   rU   kr   r   
subparsers
parser_runparser_minifyrt   r   COMMAND_FNSs      ````          r#   	run_repror     s     
^	

 4	U	$$ 

CG9 M + + 	 !55F8
t && ' J &&! ' J :   ))6 * M D
388}'#((12,'%GK !K #y9r%   )F)?r   rL   r-   loggingr:   r   r   rq   	importlibr   typingr   rj   torch.fxrP   torch._dynamo.debug_utilsr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   %torch.fx.experimental.symbolic_shapesr   	torch.hubr   r   r   backends.registryr   r   debug_utilsr   	getLoggerr+   rN   inductor_config	is_fbcoder>   r$   r'   r[   r^   r{   r   rT   rK   r   r   r   r   r   r   boolr   r\   r%   r#   <module>r      s2       	  
  #        I   F 9 g!   89$$&G GT	E 	E* 	4 4n-8	O$, $ $N  <>46J !#{:
 D#I{:r%   