
    9i	                    p   S r SSKJ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  SSKJrJrJrJrJr  SSKrSSKJr  SSK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-J.r.  \(       a  SSK/J0r0  SSK1J2r2  SSK3J4r4  \
Rj                  " \65      r7\" S5      r8\" S5      r9\9Ru                  5       r;\;(       a  SSK<r=/ r>Sr?Sr@\;(       ah  / SQr>\=R                  R                  R                  R                  5       R                  SS5      r@SR                  \> V s/ s H	  n SU  S3PM     sn 5      r?/ SQrG " S S5      rHS?S jrIS rJ " S! S"5      rK\R                  S?S# j5       rMS$S%.S@S& jjrNS$S%.S@S' jjrOS?S( jrPSAS) jrQ " S* S+\R5      rSSBS, jrT  SC         SDS- jjrU SES$S$S..             SFS/ jjjrVSGS0 jrW        SHS1 jrX      SIS2 jrY SES$S$S..             SJS3 jjjrZ      SKS4 jr[SLS5 jr\\\" \R                  5      r^\\" \R                  " S65      5      r`\\" S5      ra\\" S$5      rb\\" S$5      rc " S7 S85      rd " S9 S:5      re " S; S<5      rf   SM         SNS= jjrgSOS> jrhgs  sn f )Pa  
Debug utilities for TorchDynamo compilation and execution.

This module provides various debugging tools and utilities for TorchDynamo, including:

- Minification support for reducing test cases while preserving bugs
- Input/output handling via InputReader and InputWriter for reproducible testing
- Accuracy checking between original and compiled models
- Neural network module string conversion via NNModuleToString
- Profiling tools and system information collection
- Buck build system integration for Meta-internal testing

Key classes:
- InputReader/InputWriter: Handle serialization of model inputs/outputs
- NNModuleToString: Converts nn.Modules to string representations
- BuckTargetWriter: Manages Buck build system integration
    )annotationsN)Counter)import_module)AnyCallableOptionalTYPE_CHECKINGTypeVar)Tensor)rand_strided)normalize_path_separator)is_float_dtype)StorageWeakRef)ContentStoreReaderContentStoreWriter   )config)clone_inputsget_debug_dir)Sequence)tqdm)UntypedStorageTztorch._inductor.config )z1//caffe2/torch/fb/sparsenn:sparsenn_operators_gpuz-//caffe2/torch/fb/sparsenn:sparsenn_operatorsz///deeplearning/fbgemm/fbgemm_gpu:sparse_ops_cpuz+//deeplearning/fbgemm/fbgemm_gpu:sparse_opszfbcode://
ztorch.ops.load_library("z"))buck2runz@mode/dev-nosanc                  6    \ rS rSrSS jrSS jrS	S
S jjrSrg)BuckTargetWriterY   c                ,   [         R                  R                  [         R                  R                  U5      5      u  U l        U l        U R
                  R                  SS5      U l        U R                  R                  SS5       SU R                   3U l        U R                  U R                  R                  S5      S  U l        U R                  SS  U l        U R                  nX"R                  S5      S  SS  nSU S	U R                   3U l	        g )
Nz.pyr   /.zfbcode.   zfbcode/r   :)
ospathsplitabspathsubdirpy_filereplacetargetfindcmd_line_path)selffilenametmps      Y/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/_dynamo/debug_utils.py__init__BuckTargetWriter.__init__Z   s    $&GGMM"''//(2K$L!T\ll**5"5 {{**345Qt{{mD	IIdiinnY79:	IIabM	 kk((9%'(,!#a}5    c                    SR                  [         Vs/ s H	  nSU S3PM     sn5      n[        R                  " SU R                   SU R
                   S[         SU SU R                   S	35      $ s  snf )
Nr   z	        "z",za
load("@fbcode_macros//build_defs:python_binary.bzl", "python_binary")

python_binary(
    name="z",
    srcs = ["z"],
    compile = False,
    deps = [
        "//caffe2:torch",
        "//caffe2:libtorch",
        "//caffe2/functorch:functorch",
        "//triton:triton",
        "z",
    ],
    cpp_deps = [
z
    ],
    main_module = "z",
    par_style = "xar",
)
)join
extra_depstextwrapdedentr.   r,   
cur_targetr(   )r1   xextra_cpp_depss      r4   buildBuckTargetWriter.buildh   s    z#Jz!is"$5z#JK ;;- ll^ 
     II; #
 	
 $Ks   A3c                d   [         R                  R                  U R                  S5      n[	        US5       nUR                  U R                  5       5        S S S 5        [        U R                  /-   nU(       a%  [        R                  SSR                  U5      5        U$ ! , (       d  f       NP= f)NTARGETSwzFFound an example that reproduces the error. Run this cmd to repro - %s )r'   r(   r9   r+   openwriter@   BUCK_CMD_PREFIXr0   logwarning)r1   	print_msgtarget_filefd	cmd_splits        r4   rG   BuckTargetWriter.write   s    ggll4;;	:+s#rHHTZZ\" $ $t'9'9&::	KKX#  $#s    B!!
B/)r0   r(   r,   r+   r.   N)r2   strreturnNonerQ   rP   )T)rK   boolrQ   	list[str])__name__
__module____qualname____firstlineno__r5   r@   rG   __static_attributes__ r7   r4   r    r    Y   s    6
4 r7   r    c                 (   [         R                  R                  [        5       S5      n U c-  [        R
                  " 5        S[        R                  " 5        3n [         R                  R                  U 5      (       d  [         R                  " U SS9  U $ )Nminifierz
/minifier_T)exist_ok)
r'   r(   r9   r   tempfile
gettempdirgetpassgetuserexistsmakedirs)r(   s    r4   minifier_dirre      se    77<<4D|%%'(
7??3D2EF77>>$
D4(Kr7      c                     \ rS rSr\R
                  R                  \R
                  R                  \R
                  R                  \R
                  R                  \R
                  R                  \R
                  R                  \R
                  R                  \R
                  R                  \R
                  R                  \R
                  R                  \R
                  R                   \R
                  R"                  \R
                  R$                  \R
                  R&                  \R
                  R(                  \R
                  R*                  \R
                  R,                  \R
                  R.                  \R
                  R0                  \R
                  R2                  \R
                  R4                  /r\SS j5       r\SS j5       rSrg)NNModuleToString   c                    [        5       nU R                  5        H5  u  p#[        U5      [        R                  ;  d  M$  UR                  U5        M7     [        U5      S:  a  [        R                  SU5        g)Nr   z-We have not tested reprs of some modules - %sT)	setnamed_childrentyperh   
safe_reprsaddlenrI   rJ   )gmcant_convert_modules       r4   can_convert_to_string&NNModuleToString.can_convert_to_string   s`    u**,IAF|#3#>#>>  ( - |q KKGVr7   c                $   SSK Jn  Sn[        R                  " S5      nU R	                  5        HY  u  pEUR                  5        n[        UR                  5       S 5      nUb  UR                  (       a  U S3nX2S-   SU SU S	3-  nM[     U R                  R                  5        H  u  pU	c  M
  U	R                  5       [        ::  a(  SS
KJn
  U
R                  [        :  d   e[!        U	5      nOh["        R$                  " U	5      (       a'  S['        U	R(                  5       SU	R*                   S3nO&S['        U	R(                  5       SU	R*                   S3nU	R                  (       a  U S3nUUS-   SU SU S3-  nM     U R,                  R                  5        HW  u  pUc  M
  SnUR                  (       a  SnS['        UR(                  5       SUR*                   U S3nX2S-   SU SU S	3-  nMY     X1" U R.                  S5       S	3-  nU$ )Nr   )
_addindent    z
            from torch.nn import *
            class Repro(torch.nn.Module):
                def __init__(self) -> None:
                    super().__init__()
            z.cuda()   zself.z = r   )
PRINT_OPTSztorch.randn(z, dtype=)ztorch.randint(1, size=zself.register_buffer('z', z)
r   z, device="cuda"ztorch.nn.Parameter(torch.randn(z))rf   )torch.nn.modules.modulerx   r;   r<   rl   __repr__next
parametersis_cuda_buffersitemsnumelMAX_CONSTANT_NUMEL_INLINEtorch._tensor_strr{   	thresholdreprtorchis_floating_pointlistshapedtype_parameterscode)rq   rx   tab	model_strmodule_namert   
module_strexample_parambuffer_namebufferr{   
tensor_str
param_nameparammaybe_devices                  r4   convertNNModuleToString.convert   s$   6OO
	 $&#4#4#6K"OO-.J !!2!2!4d;M(]-B-B *|73
!G9E+c*RHHI $7 $&;;#4#4#6K~||~!::8!++/HHHH!&\
((00+D,>+?x~UVW
 -T&,,-?,@VWX  ~~ *|73
7)1+c*SQI# $7* "$!5!5!7J}L}}0:4;L:MXV[VaVaUbcobpprsJ!G9E*SBGGI "8  	
277A./r22	r7   r[   N)rq   torch.fx.GraphModulerQ   rT   )rq   r   rQ   rP   ) rV   rW   rX   rY   r   nnLinearConv1dConv2dConv3dBatchNorm1dBatchNorm2dBatchNorm3d	LayerNormDropoutSoftmaxReLUGELUIdentity	MaxPool2d	EmbeddingTanhConvTranspose1dGLULSTMFlattenAdaptiveAvgPool2drn   staticmethodru   r   rZ   r[   r7   r4   rh   rh      s0     ""+J0 	 	 = =r7   rh   c                 J   [         R                  R                  5       (       d  gSn  [        R                  " SS/5      nUR                  5       R                  S5      nSR                  U Vs/ s H  o3S;  d  M
  SU S	3PM     sn5      nX S3-  n [        S [        [         R                  R                  5       5       5       5      nU S-  n UR                  5        H  u  pgU SU SU S	3-  n M     U S-  n U $ s  snf ! [        [        R                  4 a    U S
-  n  Nf = f)Nz:# torch.cuda.is_available()==False, no GPU info collected
z# CUDA Info: 
nvccz	--versionr   r   )r   # z 
z# nvcc not found
c              3  `   #    U  H$  n[         R                  R                  U5      v   M&     g 7fN)r   cudaget_device_name).0is     r4   	<genexpr>,_cuda_system_info_comment.<locals>.<genexpr>  s&      /O!

""1%%/Os   ,.z# GPU Hardware Info: 
z : )r   r   is_available
subprocesscheck_outputdecoder)   r9   FileNotFoundErrorCalledProcessErrorr   rangedevice_countr   )r   cuda_version_outcuda_version_linesscomment	gpu_namesnamecounts           r4   _cuda_system_info_commentr     s-   ::""$$L!I*%22FK3HI-446<<TB''0BT0B1tm;Rs#;0BTUy^#	  /4UZZ5L5L5N/O I **I (r$s5'--	 )I Uz<<= *))	*s*   AD  .	C;;
C;D  ;D   D"!D"F)stable_outputc           	       ^^ U (       a  g/ SQm/ SQmSUU4S jjn[         R                  R                  5        VVs/ s H  u  p#U" U5      (       d  M  SU SU S3PM     nnnSR                  U5      n[	        S	U S
35      $ s  snnf )zd
Generate a string configuration for environment variables related to Dynamo, Inductor, and Triton.
z+# env var omitted due to stable_output=True)TORCHDYNAMOINDUCTORTRITON)TRITON_LIBDEVICE_PATHTRITON_PTXAS_PATHTRITON_LIBCUDA_PATHc                H   >^  [        U 4S jT 5       5      =(       a    T T;  $ )Nc              3  ,   >#    U  H	  oT;   v   M     g 7fr   r[   )r   stringkeys     r4   r   ;generate_env_vars_string.<locals>.filter.<locals>.<genexpr>&  s     :zVS=zs   )any)r   
allow_list	skip_lists   `r4   filter(generate_env_vars_string.<locals>.filter%  s    :z::Ss)?SSr7   zos.environ['z'] = ''r   z
import os
z
    )r   rP   rQ   rT   )r'   environr   r9   r   )r   r   r   valueconfig_linesconfig_stringr   r   s         @@r4   generate_env_vars_stringr     s     <:JUIT T
 ****,,JC#; 	+,se6%*,  
 IIl+M# ) % 	 	s   BBc           	     h   SS K nSS KnU (       a  gUR                  R                  R                  R                  5       nSUR                  R                  R                  5        SUR                  R                  R                  5        SUR                  R                  R                  5        SU S3	$ )Nr   z*# config omitted due to stable_output=Truez~import torch._dynamo.config
import torch._inductor.config
import torch._functorch.config
import torch.fx.experimental._config
r   )
torch._functorch.configtorch._inductor.configfxexperimental_configcodegen_config_dynamor   	_inductor
_functorch)r   r   experimental_configs      r4   generate_config_stringr   4  s    "!;((//77FFH
 $$& ' (&&( ) *'') * +  	 	r7   c                 R    [         R                  R                  [        5       S5      $ )Nzminifier_launcher.py)r'   r(   r9   re   r[   r7   r4   get_minifier_repro_pathr   H  s    77<<(>??r7   c                b   [        5       n[        R                  SU5        [        (       a  [	        U5      R                  5          [        US5       nUR                  U 5        S S S 5        g ! , (       d  f       g = f! [         a&  n[        R                  S5        [        S5      UeS nAff = f)NzWriting minified repro to:
%srD   r   z(Could not write to {minified_repro_path})
r   rI   rJ   use_buckr    rG   rF   OSError	exceptionNotImplementedError)contentsminified_repro_pathrM   es       r4   helper_for_dump_minifyr   L  s    13KK02EFx,-335U%s+rHHX ,++  Ub!"LMSTTUs6   A> A-$A> -
A;7A> ;A> >
B.!B))B.c                      \ rS rSrSrg)AccuracyErrori[  r[   N)rV   rW   rX   rY   rZ   r[   r7   r4   r  r  [  s    r7   r  c                    [        U 5      n[        [        U 5      5       HE  n[        X   [        R
                  5      (       d  M&  X   R                  X   R                  5        MG     U$ )z
This clone inputs is different from utils clone_input. In case of minifier,
all the tensors are leaf tensors while creating a new graph. So, we set the
requires_grad field w/o checking the leafness of the tensor.
)r   r   rp   
isinstancer   r   requires_grad_requires_grad)example_inputscloned_inputsidxs      r4   clone_inputs_retaining_gradnessr	  _  sX     !0MS()m(%,,77--n.A.O.OP * r7   c                X   SSK JnJnJn  [        R
                  " U 5      n U(       d  [        U5      n[        U S5      (       a  U R                  S5        [        U SS5      (       a  U " U5      OU " U6 nU(       a  U$ U" U5      (       a  U" U5      nUR                  5         U" XSU5      $ )z
Runs a forward and possibly backward iteration for a given mod and args.

When disable_clone is True, we will use args as-is without cloning.
This is higher fidelity but we may destroy the args in the process.
r   )collect_resultsreduce_to_scalar_lossrequires_bwd_pass	zero_gradT_boxed_callFN)testingr  r  r  copydeepcopyr	  hasattrr  getattrbackward)	rq   argsonly_fwddisable_cloner  r  r  outlosss	            r4   run_fwd_maybe_bwdr  l  s     SR	r	B.t4r;
T b-77"T(RYC
$S)2D$//r7   require_fp64ignore_non_fpc          	        SSK Jn  [        XU5      nSn[        R                  (       a8   [        [        R                  " U 5      [        U5      5      u  p[        XU5      n [        XU5      nU" UUU[        R                  SUS9nU$ ! [         a*    U(       a  [        S5      e[        R                  S5         N[f = f! [         a    [        R                  S5         gf = f)	aI  
Check two models have same accuracy.

require_fp64: if True, raise an error if we unable to calculate the fp64 reference
ignore_non_fp: if True, do not compare outputs which are not floating point.  This
    is mostly useful for the minifier (which wants to avoid quantizing floating point
    error into integer/boolean error)
r   )sameNzfCould not generate fp64 outputs, workaround with torch._dynamo.config.same_two_models_use_fp64 = FalsezCould not generate fp64 outputszWhile minifying the program in accuracy minification mode, ran into a runtime exception which is likely an unrelated issue. Skipping this graph.T)tol	equal_nanr  )utilsr   r  r   same_two_models_use_fp64cast_to_fp64r  r  r	  	ExceptionRuntimeErrorrI   rJ   r   repro_tolerance)rq   opt_gmr  r  r  r  r   reffp64_ref
fp64_modelfp64_examplesrespassings                r4   same_two_modelsr0    s    " 
B
9CH&&
	;(4b!#B>#R)%J )HMH
A ""#G N7  	;"|  KK9:	;   	$	

 s#   7B
 #C 
1B>=B>C#"C#c                   U R                   R                   GH@  nUR                  S:X  a  UR                  [        R
                  R                  R                  R                  :X  a}  [        UR                  5      S:X  d   e[        UR                  S   5      (       aE  UR                  S   [        R                  :w  a$  UR                  S   [        R                  4Ul
        UR                  S:X  d  M  UR                  R                  S5      nUc  M  [        U5      (       d  GM  [        UR                  5      n[        R                  US'   X1l        GMC     U R                   R!                  5         U R#                  5         U $ )Ncall_functionrz   r   r   r   )graphnodesopr.   r   opsprimsconvert_element_typedefaultrp   r  r   float64kwargsgetdictlint	recompile)modelnoder   
new_kwargss       r4   cast_dtype_args_to_fp64rC    s   !!GG&uyyCCKKKtyy>Q&&&diil++		!0M!YYq\5==9	77o%KKOOG,E ^E%:%:!$++.
&+mm
7#( " 
KK	OOLr7   c                   ^  SSK Jn  UR                  T 5      nT [        R                  :X  a  [        U5      nU" U 4S jU5      nX4$ )Nr   )tree_mapc                   > [        U [        R                  5      (       a&  U R                  5       (       a  U R	                  T5      $ U $ r   )r  r   r   r   to)r>   r   s    r4   <lambda>cast_to.<locals>.<lambda>  s<    a&&1+>+>+@+@ $$u+ r7   )torch.utils._pytreerE  rG  r   r:  rC  )r   r@  inputsrE  s   `   r4   cast_torL    sK     -HHUOE (.	 		F =r7   c                6    [        [        R                  X5      $ r   )rL  r   r:  )r@  rK  s     r4   r%  r%    s     5==%00r7   c          	          U" [         R                  " U 5      [        U5      5      n[        U UUUUUS9(       + $ ! [         a    [
        R                  S5         gf = f)Nr  zWhile minifying the program in accuracy minification mode, ran into a runtime exception which is likely an unrelated issue. Skipping this graphF)r  r  r	  r0  r&  rI   r   )rq   r  compiler_fnr  r  r  compiled_gms          r4   backend_accuracy_failsrQ    st    !MM">~N
 #%'
 
 	
   	#	

 s   8; AAc               8    U b  U $ [         R                  " U5      $ r   )r#  make_contiguous_strides_for)strider   s     r4   _stride_or_defaultrU    s    
 '6UU-N-Nu-UUr7   c                   ^  U 4S j$ )Nc                   > U b  U $ T$ r   r[   )r>   ds    r4   rH  _mk_defaulter.<locals>.<lambda>'  s    !-Q.Q.r7   r[   )rX  s   `r4   _mk_defaulterrZ  &  s	    ..r7   cpuc                  X    \ rS rSrS	S jrSSS.         S
S jjrSS jrSS jrSrg)NopInputReaderi1  c                    SU l         g )Nr   total)r1   s    r4   r5   NopInputReader.__init__2  s	    
r7   Ndevice
dtype_hintc               .    U =R                   S-  sl         g )Nr   r_  )r1   storage_hashnbytesrc  rd  s        r4   storageNopInputReader.storage5  s     	

a
r7   c                    g r   r[   r1   r  r;  s      r4   tensorNopInputReader.tensor?      r7   c                    g r   r[   rk  s      r4   symintNopInputReader.symintB  rn  r7   r_  rQ   rR   )
rf  Optional[str]rg  intrc  ,Optional[torch._prims_common.DeviceLikeType]rd  Optional[torch.dtype]rQ   rR   )r  r   r;  r   rQ   zOptional[torch.Tensor])r  r   r;  r   rQ   Optional[int]	rV   rW   rX   rY   r5   rh  rl  rp  rZ   r[   r7   r4   r]  r]  1  sP     @D,0# 
 = * 
r7   r]  c                      \ rS rSrSSS.SS jjjrSSS.         SS jjr SSSSSS.                 SS jjjrSS	 jrS
rg)InputReaderiH  N)pbarc               x    Uc  [         R                  S5        Ub  [        U5      OS U l        / U l        X l        g )Nz0no save_dir specified, will generate random data)rI   rJ   r   storer  r{  )r1   save_dirr{  s      r4   r5   InputReader.__init__I  s7    
 KKJK5=5I'1t
!		r7   rb  c                  U R                   b  U R                   R                  S5        [        U5      n[        U5      nU R                  bP  UbM   U R                  R                  U5      nX5R                  :w  a   [        R                  SX5R                  5        U$ [        R                  SU5        X$R                  -  4n[        S US9n[        XgXC5      R                  5       $ ! [         a     NUf = f)Nr   zdevice mismatch: %s != %sz1could not load %s, generating random data insteadr   )r{  update_device_or_default_dtype_or_defaultr}  read_storagerc  rI   rJ   r   itemsizerU  r   untyped_storage)r1   rf  rg  rc  rd  rh  r   rT  s           r4   rh  InputReader.storageT  s     99 IIQ#F+&z2
::!l&>
**11,? ^^+KK ;V^^T GV...0#D6E:>NNPP % s   C% %
C21C2)storage_offsetr   r  is_leafc               ,   [        X2S9n[        U5      n[        U5      n[        U5      n[	        U5      n[
        R                  " / XQR                  US9n	[
        R                  " 5          U	R                  XX#5        S S S 5        U(       dk  [
        R                  " 5          U	R                  [
        R                  S9n	S S S 5        [
        R                  " 5          U	R                  XX#5        S S S 5        [
        R                  R                  R                  U	5      U:X  d   e[
        R                   R#                  X5        U R$                  R'                  U	5        U	$ ! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       N= f)Nr  )r   rc  r  )memory_format)rU  _storage_offset_or_defaultr  _is_leaf_or_default_requires_grad_or_defaultr   rl  rc  no_gradset_enable_gradclonepreserve_format_subclasses
meta_utilssafe_is_leaf_utilsset_tensor_metadatar  append)
r1   rh  r   rT  r  r   r  r  metadatats
             r4   rl  InputReader.tensorq  s    $F83NC!%(%g.1-@LLeNN-
 ]]_FF7E: ""$GG%*?*?G@ %w> !  ++88;wFFF((5		 _ %$ s$   +E#"E4F#
E14
F
Fc                <    U R                   R                  U5        U$ r   )r  r  )r1   vals     r4   rp  InputReader.symint  s    		
r7   )r  r{  r}  r   )r~  rs  r{  zOptional[tqdm])
rf  rs  rg  rt  rc  ru  rd  rv  rQ   r   )rh  r   r   torch._prims_common.ShapeTyperT  (Optional[torch._prims_common.StrideType]r  rw  r   rv  r  Optional[bool]r  r  r  r   rQ   torch.Tensor)r  r   rQ   r   rx  r[   r7   r4   rz  rz  H  s    	QU 	 	  @D,0Q#Q Q
 =Q *Q 
QB <@	 )-'+(,"& - 9	 & % &    
Br7   rz  c                  z    \ rS rSrSS.SS jjrSS jrSSS.       SS jjrSS	 jrSS
 jrSS jr	SS jr
Srg)InputWriteri  Fstable_hashc                   / U l         [        R                  " 5       U l        Xl        Ub	  [        XS9OS U l        0 U l        g )Nr  )_lines	itertoolsr   storage_counterr~  r   r}  seen_storages)r1   r~  r  s      r4   r5   InputWriter.__init__  sC    !#(0  # xA 	

 9;r7   c                r    S/nUR                  S U R                   5       5        UR                  S5        U$ )Nzdef load_args(reader):c              3  ,   #    U  H
  nS U 3v   M     g7f)ry   Nr[   )r   ls     r4   r   $InputWriter.lines.<locals>.<genexpr>  s     1[4s[s   zload_args._version = 0)extendr  r  )r1   rs     r4   linesInputWriter.lines  s8    $
 	
1T[[11 	
)*r7   N)device_hintrd  c          
     X   [        U5      nU R                  R                  U5      nUb  U$ S[        U R                  5       3nSn[        S 5      [        U5      :w  a  SU< 3nSnUR                  nUR                  S:X  a  Uc   eUn[        S 5      U:w  a  SU< 3nUR                  5       n	S n
U R                  b5  UR                  R                  S:w  a  U R                  R                  U5      n
U R                  R                  U SU
< SU	< U U S35        XPR                  U'   U$ )	Nbufr   z, dtype_hint=metaz	, device=z = reader.storage(, r|   )r   r  r<  r   r  r  rc  rm   r  rg  r}  write_storager  r  )r1   r  r  rd  wsvmaybe_dtype_hintr   rc  rg  rf  s              r4   rh  InputWriter.storage  sB    O,""2&=H$t++,-.T"&7
&CC!.zn=  '';;& *** Fd#v-&vj1L '')::!o&<&<&A&AV&K::33ODLc#L#32fZ~N^M__`a	
 "#2r7   c                   SSK JnJn  U R                  UR	                  5       UR
                  UR                  S9n/ nU" U" [        S UR                  S9UR                  5       5      5      (       d1  UR                  [        [        UR                  5       5      5      5        [        S 5      UR
                  :w  a  UR                  SUR
                  < 35        U" [        S 5      UR                  5       :H  5      (       d#  UR                  SUR                  5       < 35        [         R"                  R%                  U5      nU(       a&  UR'                  S UR)                  5        5       5        [+        S 5      UR,                  :w  a  UR                  SUR,                  < 35        [         R.                  R0                  R3                  U5      n[5        S 5      U:w  a  UR                  S	U< 35        U R6                  R                  S
SR9                  U[        [        UR                  5      5      /UQ5      -   SU 3-   5        g )Nr   )statically_known_truesym_eq)rd  r  r  zdtype=zstorage_offset=c              3  6   #    U  H  u  pU S U< 3v   M     g7f)=Nr[   )r   kr  s      r4   r   %InputWriter.tensor.<locals>.<genexpr>  s     I1H1#Qqe1Hs   zrequires_grad=zis_leaf=zreader.tensor(r  )  # )%torch.fx.experimental.symbolic_shapesr  r  rh  r  r   rc  rU  r   rT  r  rP   tupler  r  r  r   r  get_tensor_metadatar  r   r  r  r  r  r  r  r  r9   )	r1   r   r  r  r  rh  r  tensor_metadatar  s	            r4   rl  InputWriter.tensor  s   W,,AGG  
 $%d!'':AHHJG
 
 KKE!((*-./T"agg-KK&,-$&t,0@0@0BB
 
 KK/!*:*:*<)?@A,,::1=KKI1F1F1HII$T*aoo=KK.(;<=##..;;A>t$/KK(7+./ii#eAGGn"5==>?dVn	
r7   c                *   U R                   R                  SU S[        U5       35        [        U[        [
        45      (       a  U R                   R                  S5        [        U5       H  u  p4U SU S3n[        U[        R                  5      (       a  U R                  XT5        M?  [        U[        [        R                  45      (       a  U R                  XT5        Mw  U R                  XT5        M     U R                   R                  S5        g g )Nr   z# was unsupported type for dumping: z"""[])r  r  rm   r  r   r  	enumerater   r   rl  rt  SymIntrp  unsupported)r1   r   argr   aname_is         r4   r  InputWriter.unsupported  s    Rv%HcTU cD%=))KKu%!# 61#Qa..KK*C#677KK*$$V/ ' KKu% *r7   c                J    U R                   R                  SU< SU S35        g )Nzreader.const(r  z!, filtered out during compilation)r  r  )r1   r   s     r4   constInputWriter.const  s'    D85.OP	
r7   c                    [        U[        R                  5      (       a  UR                  R                  nU R
                  R                  SU< SU 35        g )Nzreader.symint(r  )r  r   r  rA  hintr  r  )r1   r   r  s      r4   rp  InputWriter.symint  s?    c5<<((((--C^C7%v>?r7   )r  r~  r  r  r}  )r~  rs  r  rT   rQ   rR   )rQ   rU   )r  r   r  ru  rd  rv  rQ   rP   )r   rP   r  r  rQ   rR   )r   rP   r  r   rQ   rR   )r   rP   rQ   rR   )r   rP   r  r   rQ   rR   )rV   rW   rX   rY   r5   r  rh  rl  r  r  rp  rZ   r[   r7   r4   r  r    s\    GL 
;& EI,0 '  B	 
 *  
 D
@&$
@r7   r  c           	       ^^^^ SSK Jn  UR                  5        VVs0 s H  u  pVXe_M	     nnnSR                  UR	                  5       5      n[
        R                  " U 5      n	SU S3n
SU S3nSn " S	 S
5      n0 nU=(       d    0 mSUU4S jjmSUU4S jjnU R                  nUR                  " 5        H  u  nnUS:X  a  M  [        R                  " UU5      nU(       a>  UR                  5       u  nn[        UR                  S5      5      nUU   nU" UU5      UU'   [        R                  " UU5      nU(       d  M  T" UR                  S5      5      UU'   M     S[
        R                  " U 5      R                  ;   aq  U" 5       nUUS'   [        R                   " X5       HK  nUR                  5       u  nnnn[        UR                  S5      5      nUU   n[#        UUU" UU5      5        MM     U$ s  snnf )a  
Takes in a function which has been printed with print_readable() and constructs kwargs to run it.

Handles Tensor inputs, Symints, and a graph module which might have tensor constants.

Consider a function `forward` defined as follows:

def forward(self, primals_1: "f32[1001, 6]", primals_2: "f32[s0]", primals_3: "Sym(s0)",):
    _tensor_constant0: "i64[4190]" = self._tensor_constant0
    # Further implementation

kwargs = aot_graph_input_parser(forward)
forward(**kwargs)
r   )dtype_abbrs|z(_tensor_constant\d+): \"(z0)\[\s*(.*?)\s*\]\" = self\.(_tensor_constant\d+)(z)\[\s*(.*?)\s*\]zSym\((s\d+)\)c                      \ rS rSrSrSrg)/aot_graph_input_parser.<locals>.TensorContaineriC  z#Container for tensors as attributesr[   N)rV   rW   rX   rY   __doc__rZ   r[   r7   r4   TensorContainerr  C  s    -r7   r  rQ   c                |   >^  [         R                  " T T;   =(       d    TS LU 4S j5        TR                  T T5      $ )Nc                    > T  S3$ )Nz; not in symbolic_shapes and default sym shape not passed inr[   )rp  s   r4   rH  =aot_graph_input_parser.<locals>.get_sym_int.<locals>.<lambda>N  s    vhYZr7   )r   _checkr<  )rp  default_sym_shapesym_shapes_dicts   `r4   get_sym_int+aot_graph_input_parser.<locals>.get_sym_intK  s;    o%F):$)FZ	
 ""6+<==r7   c                  > / n/ n[        U 5       Hj  u  pEUR                  5       nSU;   a,  T" U5      nUR                  U5        UR                  U5        MG  U(       d  MP  UR                  [        U5      5        Ml     UR                  (       a  [
        R                  O[
        R                  nU" X!T
S9nU H"  n	[
        R                  R                  X5        M$     U$ )Nr   )r   rc  )
r  stripr  rt  r   r   randnzerosr   mark_dynamic)r   r   resolved_shapedynamic_dimsr   dimr   constructorr  rX  rc  r  s             r4   
gen_tensor*aot_graph_input_parser.<locals>.gen_tensorR  s    &FA))+Ccz$%%a(##A&3"))#c(3 ' &+%<%<ekk%++.fEAMM&&s. 
r7   ,r   r1   )rp  rP   rQ   rt  )r   r  r   torch.dtyperQ   r   )torch.utils._dtype_abbrsr  r   r9   valuesinspect	getsource__annotations__researchgroupsr  r)   group	signaturer   finditersetattr)funcrc  
sym_shapesr  r  r   r   	dtype_mapdtype_patternsourcetensor_assignment_regextensor_regexsym_shape_regexr  r;  r  r   r   
annotationmatch	data_type	shape_strr   r   	container	attr_namers   r  r  s    ` `                       @@r4   aot_graph_input_parserr    s   * 5 &1%6%6%8)%8zs
%8  ) +"4"4"67M t$F "<M?Jz{&67L&O. .  F&0&6BO> > * &&K(..0zH		,
3#(<<> Iy)//#./Ei(E&ue4F5M		/:65'A7F5M 1  ""4(333#%	"v[[!8AE16.Iy)Q)//#./Ei(EIy*UE*BC	 B MW)s   G7c                   ^ ^ [         R                  " 5       m[        R                  R	                  [        R                  R                  T 5      5      m SU4S jjnSU U4S jjn[        R                  " U5        U$ )z
Decorator to cProfile a given function and save the result to disk on process exit.

Args:
    filename: filename to save profile to
c                N   >^  [         R                  " T 5      SU U4S jj5       nU$ )Nc                    > TR                  5          T" U 0 UD6TR                  5         $ ! TR                  5         f = fr   )enabledisable)r  r;  fnprofs     r4   wrapper3profile_to_file.<locals>.decorator.<locals>.wrapper  s1    KKM4*6*s   + =)r  r   r;  r   rQ   r   )	functoolswraps)r  r  r  s   ` r4   	decorator"profile_to_file.<locals>.decorator  s%    			 
	 r7   c            	        > TR                  T 5        [        R                  R                  [        R
                  " ST  ST  S35      5        g )Nz!                Wrote profile to z+, view with:

                    snakeviz z

                )
dump_statssysstderrrG   r;   r<   )r2   r  s   r4   save_it profile_to_file.<locals>.save_it  sK    !

OO""* ,&Z (		
r7   )r  r   rQ   r   rr  )cProfileProfiler'   r(   r*   
expanduseratexitregister)r2   r!  r'  r  s   `  @r4   profile_to_filer.    sW     Dwwrww11(;<H	
 
 OOGr7   rS   )r   rT   rQ   rP   )r   rP   rQ   rR   )r  Sequence[Any]rQ   	list[Any])FF)
rq   r   r  r/  r  rT   r  rT   rQ   r   )F)rq   r   r)  r   r  r/  r  rT   r  rT   r  rT   rQ   rT   )r@  r   rQ   r   )r   r  r@  r   rK  r0  rQ   &tuple[torch.fx.GraphModule, list[Any]])r@  r   rK  r0  rQ   r1  )rq   r   r  r/  rO  zACallable[[torch.fx.GraphModule, list[Any]], torch.fx.GraphModule]r  rT   r  rT   r  rT   rQ   rT   )rT  r  r   r  rQ   ztorch._prims_common.StrideType)rX  r   rQ   zCallable[[Optional[T]], T])r   NN)
r  z&Callable[[list[Tensor]], list[Tensor]]rc  rP   r  zOptional[dict[str, int]]r  rw  rQ   zdict[str, Any])r2   rP   rQ   zCallable[[T], T])ir  
__future__r   r,  r  r)  r  ra   r  r  loggingr'   r   r   r%  r_   r;   collectionsr   	importlibr   typingr   r   r   r	   r
   r   torch._prims_common_prims_commonr#  torch._subclasses.meta_utilsr   torch._dynamo.testingr   torch._inductor.cpp_builderr   r    torch.multiprocessing.reductionsr   torch.utils._content_storer   r   r   r   r   r   collections.abcr   	torch.hubr   torch.storager   	getLoggerrV   rI   r   inductor_config	is_fbcoder   libfb.py.build_infolibfbr:   extra_importsr=   py
build_info	BuildInfoget_build_ruler-   r9   rH   r    re   r   rh   cacher   r   r   r   r   r&  r  r	  r  r0  rC  rL  r%  rQ  rU  rZ  float32r  rc  r  r  r  r  r]  rz  r  r  r.  )r>   s   0r4   <module>rM     s  $ #         	 	  
    # B B  # #  . @ . ; M  . (, !CL   89$$& 

J $$..==?GG	SWXJIITA!9!B?TUM 64 4n  c cL  0 7< 	2 5: (@U	I 	
  	00
0 0 	0
 	0H 	7 77 7 "7 	7 7 7 
7t*3=F+(11)21+1 	 ! S 	   
RV4V )V $	V/ "%--0 "5<<#67 *1- )%0 #E*  .L Lvy@ y@| +/'+	b
0bb )b %	b
 bJ#c Us   J3