
    [Thv                        S 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  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$J%r%  SSK&J'r'  SSKJ(r(J)r)  \RT                  " \+5      r,\" S5      r-\" S5      r.\.R_                  5       r0\0(       a  SSK1r2/ r3Sr4\0(       ah  / SQr3\2Rj                  Rl                  Rn                  Rq                  5       Rs                  SS5      r:SRw                  \3 V s/ s H	  n SU  S3PM     sn 5      r4/ SQr< " S S5      r=S r>Sr? " S S5      r@\R                  " S5      S 5       rBSS .S! jrCSS .S" jrDS# rES$ rF " S% S&\G5      rHS' rISES( jrJ SFSSS).S* jjrKS+ rLS, rMS- rN SFSSS).S. jjrOS/\S0   S1S2S3S04S4 jrPS5\-S3\\\-   /\-4   4S6 jrQ\Q" \R                  5      rS\Q" \R                  " S75      5      rU\Q" S5      rV\Q" S5      rW\Q" S5      rX " S8 S95      rY " S: S;5      rZ " S< S=5      r[   SGS>\\\\   /\\\   4   S?\]S@\\^\]\_4      SA\\_   S3\^\]\4   4
SB jjr`SC\]S3\\-/\-4   4SD jrags  sn f )Ha  
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
    N)Counter)import_module)AnyCallableOptionalTypeVar)Tensor)rand_strided)is_float_dtype)StorageWeakRef)ContentStoreReaderContentStoreWriter   )config)clone_inputsget_debug_dir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                   *    \ rS rSrS rS rSS jrSrg)BuckTargetWriterQ   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      Q/var/www/auris/envauris/lib/python3.13/site-packages/torch/_dynamo/debug_utils.py__init__BuckTargetWriter.__init__R   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"   )r+   xextra_cpp_depss      r.   buildBuckTargetWriter.build`   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"   r3   r%   openwriter:   BUCK_CMD_PREFIXr*   logwarning)r+   	print_msgtarget_filefd	cmd_splits        r.   rA   BuckTargetWriter.writez   s    ggll4;;	:+s#rHHTZZ\" $ $t'9'9&::	KKX#  $#s    B!!
B/)r*   r"   r&   r%   r(   N)T)__name__
__module____qualname____firstlineno__r/   r:   rA   __static_attributes__ r1   r.   r   r   Q   s    6
4r1   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"   r3   r   tempfile
gettempdirgetpassgetuserexistsmakedirs)r"   s    r.   minifier_dirrY      se    77<<4D|%%'(
7??3D2EF77>>$
D4(Kr1      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 5       r\S 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typer\   
safe_reprsaddlenrC   rD   )gmcant_convert_modules       r.   can_convert_to_string&NNModuleToString.can_convert_to_string   s`    u**,IAF|#3#>#>>  ( - |q KKGVr1   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))rZ   )torch.nn.modules.modulerl   r5   r6   r`   __repr__next
parametersis_cuda_buffersitemsnumelMAX_CONSTANT_NUMEL_INLINEtorch._tensor_strro   	thresholdreprtorchis_floating_pointlistshapedtype_parameterscode)re   rl   tab	model_strmodule_namerh   
module_strexample_parambuffer_namebufferro   
tensor_str
param_nameparammaybe_devices                  r.   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	r1   rO   N) rJ   rK   rL   rM   r}   nnLinearConv1dConv2dConv3dBatchNorm1dBatchNorm2dBatchNorm3d	LayerNormDropoutSoftmaxReLUGELUIdentity	MaxPool2d	EmbeddingTanhConvTranspose1dGLULSTMFlattenAdaptiveAvgPool2drb   staticmethodri   r   rN   rO   r1   r.   r\   r\      s0     ""+J0 	 	 = =r1   r\   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     r.   	<genexpr>,_cuda_system_info_comment.<locals>.<genexpr>  s&      /O!

""1%%/Os   ,.z# GPU Hardware Info: 
z : )r}   r   is_available
subprocesscheck_outputdecoder#   r3   FileNotFoundErrorCalledProcessErrorr   rangedevice_countrw   )r   cuda_version_outcuda_version_linesscomment	gpu_namesnamecounts           r.   _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UU4S 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$ 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   rO   )r   stringkeys     r.   r   ;generate_env_vars_string.<locals>.filter.<locals>.<genexpr>  s     :zVS=zs   )any)r   
allow_list	skip_lists   `r.   filter(generate_env_vars_string.<locals>.filter  s    :z::Ss)?SSr1   zos.environ['z'] = ''r   z
import os
z
    )r!   environrw   r3   )r   r   r   valueconfig_linesconfig_stringr   r   s         @@r.   generate_env_vars_stringr     s     <:JUIT
 ****,,JC#; 	+,se6%*,  
 IIl+M  s   A9A9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      r.   generate_config_stringr   ,  s    "!;((//77FFH
 $$& ' (&&( ) *'') * +  	 	r1   c                  R    [         R                  R                  [        5       S5      $ )Nzminifier_launcher.py)r!   r"   r3   rY   rO   r1   r.   get_minifier_repro_pathr   @  s    77<<(>??r1   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:
%sr>   r   z(Could not write to {minified_repro_path})
r   rC   rD   use_buckr   rA   r@   OSError	exceptionNotImplementedError)contentsminified_repro_pathrG   es       r.   helper_for_dump_minifyr   D  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)AccuracyErroriS  rO   N)rJ   rK   rL   rM   rN   rO   r1   r.   r   r   S  s    r1   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   rd   
isinstancer}   r	   requires_grad_requires_grad)example_inputscloned_inputsidxs      r.   clone_inputs_retaining_gradnessr   W  sX     !0MS()m(%,,77--n.A.O.OP * r1   c                 V   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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_callN)
testingr   r   r   copydeepcopyr   hasattrr  backward)	re   argsonly_fwddisable_cloner   r   r   outlosss	            r.   run_fwd_maybe_bwdr  d  s     SR	r	B.t4r;
T b-00"T(b$iC
$S)2D$//r1   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RuntimeErrorrC   rD   r   repro_tolerance)re   opt_gmr   r	  r  r  r  reffp64_ref
fp64_modelfp64_examplesrespassings                r.   same_two_modelsr"    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_functionrn   r   r   r   )graphnodesopr(   r}   opsprimsconvert_element_typedefaultrd   r  r   float64kwargsgetdictlint	recompile)modelnoder   
new_kwargss       r.   cast_dtype_args_to_fp64r5    s   !!GG&uyyCCKKKtyy>Q&&&diil++		!0M!YYq\5==9	77o%KKOOG,E ^E%:%:!$++.
&+mm
7#( " 
KK	OOLr1   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)r8   r   s    r.   <lambda>cast_to.<locals>.<lambda>  s<    a&&1+>+>+@+@ $$u+ r1   )torch.utils._pytreer7  r9  r}   r,  r5  )r   r2  inputsr7  s   `   r.   cast_tor>    sI    ,HHUOE (.	 		F =r1   c                 6    [        [        R                  X5      $ r   )r>  r}   r,  )r2  r=  s     r.   r  r    s    5==%00r1   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   r"  r  rC   r   )re   r   compiler_fnr	  r  r  compiled_gms          r.   backend_accuracy_failsrC    st    !MM">~N
 #%'
 
 	
   	#	

 s   8; AAstrideztorch._prims_common.StrideTyper   ztorch._prims_common.ShapeTypereturnc                8    U b  U $ [         R                  " U5      $ r   )r  make_contiguous_strides_for)rD  r   s     r.   _stride_or_defaultrH    s    
 '6UU-N-Nu-UUr1   dc                    ^  U 4S j$ )Nc                    > U b  U $ T$ r   rO   )r8   rI  s    r.   r:  _mk_defaulter.<locals>.<lambda>  s    !-Q.Q.r1   rO   )rI  s   `r.   _mk_defaulterrM    s	    ..r1   cpuc                   :    \ rS rSrS	S jrSSS.S jrS rS rSrg)
NopInputReaderi   Nc                     SU l         g )Nr   total)r+   s    r.   r/   NopInputReader.__init__!  s	    
r1   device
dtype_hintc                .    U =R                   S-  sl         g )Nr   rR  )r+   storage_hashnbytesrV  rW  s        r.   storageNopInputReader.storage$  s    

a
r1   c                     g r   rO   r+   r  r-  s      r.   tensorNopInputReader.tensor'      r1   c                     g r   rO   r^  s      r.   symintNopInputReader.symint*  ra  r1   rR  rE  N	rJ   rK   rL   rM   r/   r[  r_  rc  rN   rO   r1   r.   rP  rP     s     7;t r1   rP  c                   V    \ rS rSrSSS.S jjrSSS.S jr SSSSSS.S jjrS	 rS
rg)InputReaderi0  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)rC   rD   r   storer  ri  )r+   save_dirri  s      r.   r/   InputReader.__init__1  s7    
 KKJK5=5I'1t
		r1   rU  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   )ri  update_device_or_default_dtype_or_defaultrk  read_storagerV  rC   rD   r   itemsizerH  r
   untyped_storage)r+   rY  rZ  rV  rW  r[  r   rD  s           r.   r[  InputReader.storage<  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)Nro  )r   rV  r   )memory_format)rH  _storage_offset_or_defaultrr  _is_leaf_or_default_requires_grad_or_defaultr}   r_  rV  no_gradset_enable_gradclonepreserve_format_subclasses
meta_utilssafe_is_leaf_utilsset_tensor_metadatar  append)
r+   r[  r   rD  rw  r   r   rx  metadatats
             r.   r_  InputReader.tensorR  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  )r+   vals     r.   rc  InputReader.symints  s    		
r1   )r  ri  rk  r   rf  rO   r1   r.   rh  rh  0  s>    	d 	 7;t Q4 	 Br1   rh  c                   d    \ rS rSrSS.S jrS rSSS.S\4S	 jjrSS
 jrS 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_counterrl  r   rk  seen_storages)r+   rl  r  s      r.   r/   InputWriter.__init__  sC    (0  # xA 	

  r1   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)rm   NrO   )r   ls     r.   r   $InputWriter.lines.<locals>.<genexpr>  s     1[4s[s   zload_args._version = 0)extendr  r  )r+   rs     r.   linesInputWriter.lines  s8    $
 	
1T[[11 	
)*r1   NrW  device_hintrE  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(, rp   )r   r  r.  rs   r  rr  rV  ra   rq  rZ  rk  write_storager  r  )r+   ru  rW  r  wsvmaybe_dtype_hintr   rV  rZ  rY  s              r.   r[  InputWriter.storage  s@   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r1   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_eqr  ro  zdtype=zstorage_offset=c              3   6   #    U  H  u  pU S U< 3v   M     g7f)=NrO   )r   kr  s      r.   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  r[  ru  r   rV  rH  r   rD  r  strtuplerr  r{  rw  r}   r  get_tensor_metadatar  rw   r}  r   r  r  r  r|  r  r3   )	r+   r   r  r  r  r[  r  tensor_metadatarx  s	            r.   r_  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	
r1   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  ra   r   r   r  	enumerater}   r	   r_  intSymIntrc  unsupported)r+   r   argr   aname_is         r.   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% *r1   c                 J    U R                   R                  SU< SU S35        g )Nzreader.const(r  z!, filtered out during compilation)r  r  )r+   r   s     r.   constInputWriter.const  s'    D85.OP	
r1   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  r3  hintr  r  )r+   r   r  s      r.   rc  InputWriter.symint  s?    c5<<((((--C^C7%v>?r1   )r  rl  r  r  rk  re  )rJ   rK   rL   rM   r/   r  r  r[  r_  r  r  rc  rN   rO   r1   r.   r  r    s9    05 
  6:t PS 8
@&$
@r1   r  funcrV  
sym_shapesdefault_sym_shapec           	         ^^^^ 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T=(       d    0 mUU4S jmS[        4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>.TensorContaineri  z#Container for tensors as attributesrO   N)rJ   rK   rL   rM   __doc__rN   rO   r1   r.   TensorContainerr    s    -r1   r  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 inrO   )rc  s   r.   r:  =aot_graph_input_parser.<locals>.get_sym_int.<locals>.<lambda>'  s    vhYZr1   )r}   _checkr.  )rc  r  r  s   `r.   get_sym_int+aot_graph_input_parser.<locals>.get_sym_int$  s9    j A$5T$AZ	
 ~~f&788r1   rE  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   rV  )
r  stripr  r  r~   r}   randnzerosr   mark_dynamic)r   r   resolved_shapedynamic_dimsr   dimr   constructorr  rI  rV  r  s             r.   
gen_tensor*aot_graph_input_parser.<locals>.gen_tensor+  s    &FA))+Ccz$%%a(##A&3"))#c(3 ' &+%<%<ekk%++.fEAMM&&s. 
r1   ,r   r+   )torch.fx.graphr  rw   r3   valuesinspect	getsourcer	   __annotations__researchgroupsr  r#   group	signaturert   finditersetattr)r  rV  r  r  r  r   r   	dtype_mapdtype_patternsourcetensor_assignment_regextensor_regexsym_shape_regexr  r-  r  annotationsr   
annotationmatch	data_type	shape_strr   r   	container	attr_namerg   r  s    ```                       @r.   aot_graph_input_parserr    s   * +.9.?.?.AB.A
.AIBHH[//12M t$F "<M?Jz{&67L&O. .  F!rJ9F  * &&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 MS Cs   G:r,   c                    ^ ^ [         R                  " 5       m[        R                  R	                  [        R                  R                  T 5      5      m U4S jnU U4S 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                 J   >^  [         R                  " T 5      U U4S 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     r.   wrapper3profile_to_file.<locals>.decorator.<locals>.wrapperh  s1    KKM4*6*s   + =)	functoolswraps)r  r	  r  s   ` r.   	decorator"profile_to_file.<locals>.decoratorg  s%    			 
	 r1   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stderrrA   r5   r6   )r,   r  s   r.   save_it profile_to_file.<locals>.save_itr  sK    !

OO""* ,&Z (		
r1   )cProfileProfiler!   r"   r$   
expanduseratexitregister)r,   r  r  r  s   `  @r.   profile_to_filer  ]  sR     Dwwrww11(;<H	
 OOGr1   )FF)F)r   NN)br  r  r  r  r  rU   r  r  loggingr!   r  r   r  rS   r5   collectionsr   	importlibr   typingr   r   r   r   r}   torch._prims_common_prims_commonr  torch._subclasses.meta_utilsr	   torch._dynamo.testingr
   r    torch.multiprocessing.reductionsr   torch.utils._content_storer   r   r   r   r   r   	getLoggerrJ   rC   r   inductor_config	is_fbcoder   libfb.py.build_infolibfbr4   extra_importspy
build_info	BuildInfoget_build_ruler'   r7   r3   rB   r   rY   ry   r\   	lru_cacher   r   r   r   r   r  r   r   r  r"  r5  r>  r  rC  rH  rM  float32rr  rV  rq  r{  r}  r|  rP  rh  r  r   r  r/  r  r  r  )r8   s   0r.   <module>r1     s  $         	 	  
    # 3 3  # #  . . ; M  . !CL   89$$& 
J $$..==?GG	SWXJIITA!9!B?TUM 64 4n  c cL T 0 /4 2 -2 (@U	I 	
0> 	7 7t*$1 	 RV56V +V &	V/Q /8Xa[M1$45 / "%--0 "5<<#67 *1- )%0 #E*   E Ehs@ s@p +/'+	`
DL>4</
0`` c3h(`  }	`
 
#s(^`F#c #hsAv&6 #e Us   (I: