
    9jV                       d Z ddlm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
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mZmZmZ ddlZddlmZ ddlZddlmZ ddl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* ddlm+Z+m,Z,m-Z- erddl.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4  e
jj                  e6      Z7 ed      Z8 ed      Z9e9ju                         Z;e;rddl<Z=g Z>dZ?dZ@e;reg dZ>e=j                  j                  j                  j                         j                  dd      Z@dj                  e>D  cg c]  } d|  d
 c}       Z?g dZG G d d      ZHd?dZId ZJ G d! d"      ZKej                  d?d#       ZMd$d%d@d&ZNd$d%d@d'ZOd?d(ZPdAd)ZQ G d* d+eR      ZSdBd,ZT	 	 dC	 	 	 	 	 	 	 	 	 dDd-ZU	 dEd$d$d.	 	 	 	 	 	 	 	 	 	 	 	 	 dFd/ZVdGd0ZW	 	 	 	 	 	 	 	 dHd1ZX	 	 	 	 	 	 dId2ZY	 dEd$d$d.	 	 	 	 	 	 	 	 	 	 	 	 	 dJd3ZZ	 	 	 	 	 	 dKd4Z[dLd5Z\ e\ej                        Z^ e\ ej                  d6            Z` e\d      Za e\d$      Zb e\d$      Zc G d7 d8      Zd G d9 d:      Ze G d; d<      Zf	 	 	 dM	 	 	 	 	 	 	 	 	 dNd=ZgdOd>Zhyc c} w )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TYPE_CHECKINGTypeVar)Tensor)rand_strided)normalize_path_separator)is_float_dtype)StorageWeakRef)ContentStoreReaderContentStoreWriter   )config)clone_inputsget_debug_dir	warn_once)Callable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                  &    e Zd ZddZddZdddZy)	BuckTargetWriterc                .   t         j                  j                  t         j                  j                  |            \  | _        | _        | j
                  j                  dd      | _        | j                  j                  dd       d| j                   | _        | j                  | j                  j                  d      d  | _        | j                  dd  | _        | j                  }||j                  d      d  dd  }d| d	| j                   | _	        y )
Nz.pyr   /.zfbcode.   zfbcode/r   :)
ospathsplitabspathsubdirpy_filereplacetargetfindcmd_line_path)selffilenametmps      Y/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/_dynamo/debug_utils.py__init__zBuckTargetWriter.__init__[   s    $&GGMM"''//(2K$L!T\ll**5"5 {{**345Qt{{mD	IIdiinnY79:	IIabM	 kk#((9%'(,!#a}5    c                    dj                  t        D cg c]  }d| d
 c}      }t        j                  d| j                   d| j
                   dt         d| d| j                   d	      S c c}w )
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'   )r0   xextra_cpp_depss      r3   buildzBuckTargetWriter.buildi   s    z#J!is"$5#JK ;;- ll^ 
     II; #
 	
 $Ks   A/c                P   t         j                  j                  | j                  d      }t	        |d      5 }|j                  | j                                d d d        t        | j                  gz   }|r%t        j                  ddj                  |             |S # 1 sw Y   FxY w)NTARGETSwzFFound an example that reproduces the error. Run this cmd to repro - %s )r&   r'   r7   r*   openwriter>   BUCK_CMD_PREFIXr/   logwarning)r0   	print_msgtarget_filefd	cmd_splits        r3   rD   zBuckTargetWriter.write   s    ggll4;;	:+s# 	#rHHTZZ\"	# $t'9'9&::	KKX# 	# 	#s    BB%N)r1   strreturnNonerM   rL   )T)rH   boolrM   	list[str])__name__
__module____qualname__r4   r>   rD    r5   r3   r    r    Z   s    6
4r5   r    c                    t         j                  j                  t               d      } | +t	        j
                          dt        j                          } t         j                  j                  |       st        j                  | d       | S )Nminifierz
/minifier_T)exist_ok)
r&   r'   r7   r   tempfile
gettempdirgetpassgetuserexistsmakedirs)r'   s    r3   minifier_dirr_      sb    77<<4D|%%'(
7??3D2EF77>>$
D4(Kr5      c                     e Zd Zej                  j
                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                   ej                  j"                  ej                  j$                  ej                  j&                  ej                  j(                  ej                  j*                  ej                  j,                  ej                  j.                  ej                  j0                  ej                  j2                  gZedd       Zedd       Zy)NNModuleToStringc                    t               }| j                         D ]2  \  }}t        |      t        j                  vs"|j                  |       4 t        |      dkD  rt        j                  d|       y)Nr   z-We have not tested reprs of some modules - %sT)	setnamed_childrentyperb   
safe_reprsaddlenrF   rG   )gmcant_convert_modules       r3   can_convert_to_stringz&NNModuleToString.can_convert_to_string   sg    u**, 	)IAvF|#3#>#>>  (	) |q KKGVr5   c                   ddl m} d}t        j                  d      }| j	                         D ]T  \  }}|j                          }t        |j                         d       }||j                  r| d}||dz   d| d| d	z  }V | j                  j                         D ]  \  }}	|		|	j                         t        k  r'dd
lm}
 |
j                  t        k\  sJ t!        |	      }nbt#        j$                  |	      r'dt'        |	j(                         d|	j*                   d}n&dt'        |	j(                         d|	j*                   d}|	j                  r| d}||dz   d| d| dz  } | j,                  j                         D ]Q  \  }}|	d}|j                  rd}dt'        |j(                         d|j*                   | d}||dz   d| d| d	z  }S | || j.                  d       d	z  }|S )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))r`   )torch.nn.modules.modulerp   r9   r:   re   __repr__next
parametersis_cuda_buffersitemsnumelMAX_CONSTANT_NUMEL_INLINEtorch._tensor_strrs   	thresholdreprtorchis_floating_pointlistshapedtype_parameterscode)rj   rp   tab	model_strmodule_namerm   
module_strexample_parambuffer_namebufferrs   
tensor_str
param_nameparammaybe_devices                  r3   convertzNNModuleToString.convert   s>   6OO
	 $&#4#4#6 	IK"OO-.J !!2!2!4d;M(]-B-B *|73
C!G9E+c*RHHI	I $&;;#4#4#6 	K~||~!::8!++/HHHH!&\
((0+D,>+?x~UVW
 -T&,,-?,@VWX  ~~ *|73
7)1+c*SQI#	* "$!5!5!7 	HJ}L}}0:4;L:MXV[VaVaUbcobpprsJC!G9E*SBGGI	H  	
277A./r22	r5   N)rj   torch.fx.GraphModulerM   rP   )rj   r   rM   rL   )rR   rS   rT   r   nnLinearConv1dConv2dConv3dBatchNorm1dBatchNorm2dBatchNorm3d	LayerNormDropoutSoftmaxReLUGELUIdentity	MaxPool2d	EmbeddingTanhConvTranspose1dGLULSTMFlattenAdaptiveAvgPool2drg   staticmethodrn   r   rU   r5   r3   rb   rb      s0     ""+J0 	 	 = =r5   rb   c                 t   t         j                  j                         syd} 	 t         j                  j                  ht        j                  ddg      }|j                         j                  d      }dj                  |D cg c]  }|dk7  s	d| d c}      }| | dz  } n| d	z  } t        d t        t         j                  j                               D              }| dz  } |j                         D ]  \  }}| d| d| dz  }  | dz  } | S c c}w # t        t
        j                  f$ r | d
z  } Y w xY w)Nz:# torch.cuda.is_available()==False, no GPU info collected
z# CUDA Info: 
nvccz	--versionr   r   z# z 
z'# Not searching for nvcc on ROCM setup
z# nvcc not found
c              3  Z   K   | ]#  }t         j                  j                  |       % y wN)r   cudaget_device_name).0is     r3   	<genexpr>z,_cuda_system_info_comment.<locals>.<genexpr>  s$      *+

""1%s   )+z# GPU Hardware Info: 
z : )r   r   is_availableversionhip
subprocesscheck_outputdecoder(   r7   FileNotFoundErrorCalledProcessErrorr   rangedevice_countr{   )r   cuda_version_outcuda_version_linesscomment	gpu_namesnamecounts           r3   _cuda_system_info_commentr     sJ   ::""$L!I	*==$)667LM!1!8!8!:!@!@!Fgg4FRq!r'A3c{RSGG9B'ICCI  /4UZZ5L5L5N/O I **I ( .er$s5'--	.I S z<<= *))	*s*   AD 
DDD D D76D7F)stable_outputc                   | ryg dg ddfd}t         j                  j                         D cg c]&  \  }} ||      rd| d|j                  dd       d( }}}d	j	                  |      }t        d
| d      S c c}}w )zl
    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                :     t         fdD              xr  vS )Nc              3  &   K   | ]  }|v  
 y wr   rU   )r   stringkeys     r3   r   z;generate_env_vars_string.<locals>.filter.<locals>.<genexpr>*  s     :V6S=:s   )any)r   
allow_list	skip_lists   `r3   filterz(generate_env_vars_string.<locals>.filter)  s    :z::Ss)?SSr5   zos.environ['z'] = ''"r   z
import os
z
    )r   rL   rM   rP   )r&   environr{   r,   r7   r   )r   r   r   valueconfig_linesconfig_stringr   r   s         @@r3   generate_env_vars_stringr     s     <:JUIT
 ****,C#; VEMM#s$;#<A@L 
 IIl+M# ) % 	 	s   +Bc           	     ^   dd l }dd l}| ry|j                  j                  j                  j                         }d|j                  j                  j                          d|j                  j                  j                          d|j                  j                  j                          d| d	S )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      r3   generate_config_stringr   8  s    "!;((//77FFH
 $$& ' (&&( ) *'') * +  	 	r5   c                 R    t         j                  j                  t               d      S )Nzminifier_launcher.py)r&   r'   r7   r_   rU   r5   r3   get_minifier_repro_pathr   L  s    77<<(>??r5   c                R   t               }t        j                  d|       t        rt	        |      j                          	 t        |d      5 }|j                  |        d d d        y # 1 sw Y   y xY w# t        $ r)}t        j                  d       t        d|       |d }~ww xY w)NzWriting minified repro to:
%srA   r   zCould not write to )
r   rF   rG   use_buckr    rD   rC   OSError	exceptionNotImplementedError)contentsminified_repro_pathrJ   es       r3   helper_for_dump_minifyr   P  s    13KK02EF,-335V%s+ 	rHHX	 	 	  Vb!$78K7L"MNTUUVs6   A4 A(A4 (A1-A4 1A4 4	B&=$B!!B&c                      e Zd Zy)AccuracyErrorN)rR   rS   rT   rU   r5   r3   r   r   _  s    r5   r   c                    t        |       }t        t        |             D ]A  }t        ||   t        j
                        s!||   j                  | |   j                         C |S )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   ri   
isinstancer   r	   requires_grad_requires_grad)example_inputscloned_inputsidxs      r3   clone_inputs_retaining_gradnessr   c  sb     !0MS() QmC(%,,7#--nS.A.O.OPQ r5   c                &   ddl m}m}m} t	        j
                  |       } |st        |      }t        | d      r| j                  d       t        | dd      r | |      n | | }|r|S  ||      r ||      }|j                           || |d|      S )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)	rj   argsonly_fwddisable_cloner  r  r  outlosss	            r3   run_fwd_maybe_bwdr  p  s     SR	r	B.t4r;
T b-7"T(RYC
$S)2sD$//r5   require_fp64ignore_non_fpc                  ddl m} t        | ||      }d}t        j                  r9	 t        t        j                  |       t        |            \  }	}
t        |	|
|      }	 t        |||      } ||||t        j                  d|      }|S # t        $ r% |rt        d      t        j                  d       Y Yw xY w# t        $ r t        j                  d       Y yw xY w)	aa  
    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RuntimeErrorrF   rG   r   repro_tolerance)rj   opt_gmr   r  r  r  r  reffp64_ref
fp64_modelfp64_examplesrespassings                r3   same_two_modelsr&    s    " 
B
9CH&&
	;(4b!#B>#R)%J )]HMH
A ""#G N7  	;"|  KK9:	;   	$	

 s#   8B  B< +B98B9<CCc                   | j                   j                  D ]+  }|j                  dk(  r|j                  t        j
                  j                  j                  j                  u rvt        |j                        dk(  sJ t        |j                  d         rD|j                  d   t        j                  k7  r$|j                  d   t        j                  f|_
        |j                  dk(  s|j                  j                  d      }|t        |      st        |j                        }t        j                  |d<   ||_        . | j                   j!                          | j#                          | S )Ncall_functionrr   r   r   r   )graphnodesopr-   r   opsprimsconvert_element_typedefaultri   r  r   float64kwargsgetdictlint	recompile)modelnoder   
new_kwargss       r3   cast_dtype_args_to_fp64r9    s   !! )GG&uyyCCKKKtyy>Q&&&diil+		!0M!YYq\5==9	77o%KKOOG,E ^E%:!$++.
&+mm
7#() 
KK	OOLr5   c                     ddl m} |j                         } t        j                  k(  rt        |      } | fd|      }||fS )Nr   )tree_mapc                ~    t        | t        j                        r!| j                         r| j	                        S | S r   )r   r   r	   r   to)r<   r   s    r3   <lambda>zcast_to.<locals>.<lambda>  s3    a&1+>+>+@ $$u+  r5   )torch.utils._pytreer;  r=  r   r0  r9  )r   r6  inputsr;  s   `   r3   cast_torA    sM     -HHUOE (.	 		F &=r5   c                8    t        t        j                  | |      S r   )rA  r   r0  )r6  r@  s     r3   r  r    s     5==%00r5   c                   	  |t        j                  |       t        |            }t        | |||||       S # t        $ r t
        j                  d       Y yw xY w)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  rF   r   )rj   r   compiler_fnr  r  r  compiled_gms          r3   backend_accuracy_failsrF    st    !MM">~N
 #%'
 
 	
   	#	

 s   69 AAc               4    | | S t        j                  |      S r   )r  make_contiguous_strides_for)strider   s     r3   _stride_or_defaultrJ  "  s    
 '6UU-N-Nu-UUr5   c                      fdS )Nc                    | | S S r   rU   )r<   ds    r3   r>  z_mk_defaulter.<locals>.<lambda>+  s    !-Q Q r5   rU   )rM  s   `r3   _mk_defaulterrN  *  s	    ..r5   cpuc                  f    e Zd ZddZddd	 	 	 	 	 	 	 	 	 ddZddZddZddZddZdd	Z	dd
Z
y)NopInputReaderc                    d| _         y )Nr   total)r0   s    r3   r4   zNopInputReader.__init__6  s	    
r5   Ndevice
dtype_hintc               .    | xj                   dz  c_         y Nr   rS  )r0   storage_hashnbytesrV  rW  s        r3   storagezNopInputReader.storage9  s     	

a
r5   c                     y r   rU   r0   r  r1  s      r3   tensorzNopInputReader.tensorC      r5   c                     y r   rU   r^  s      r3   symintzNopInputReader.symintF  r`  r5   c                     y r   rU   r0   r   s     r3   constzNopInputReader.constI  r`  r5   c                     y r   rU   rd  s     r3   unsupportedzNopInputReader.unsupportedL  r`  r5   c                     y r   rU   )r0   device_typedevice_indexs      r3   	generatorzNopInputReader.generatorO  r`  r5   c                .    | xj                   dz  c_         y rY  rS  r0   script_class_names     r3   opaquezNopInputReader.opaqueR  s    

a
r5   rM   rN   )
rZ  
str | Noner[  intrV  )torch._prims_common.DeviceLikeType | NonerW  torch.dtype | NonerM   rN   )r  r   r1  r   rM   ztorch.Tensor | None)r  r   r1  r   rM   
int | Noner   rL   rM   rN   )ri  rL   rj  rr  rM   rN   rn  rL   rM   rN   rR   rS   rT   r4   r\  r_  rb  re  rg  rk  ro  rU   r5   r3   rQ  rQ  5  sc     =A)-  
 : ' 
r5   rQ  c                      e Zd Z	 ddd	 	 	 	 	 ddZddd	 	 	 	 	 	 	 	 	 ddZ	 dddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZdd	Zdd
ZddZ	ddZ
y)InputReaderN)pbarc               v    |t         j                  d       |t        |      nd | _        g | _        || _        y )Nz0no save_dir specified, will generate random data)rF   rG   r   storer  r{  )r0   save_dirr{  s      r3   r4   zInputReader.__init__Y  s9     KKJK5=5I'1t
!		r5   rU  c                  | j                   | j                   j                  d       t        |      }t        |      }| j                  P|N	 | j                  j                  |      }||j                  k7  r!t        j                  d||j                         |S t        d| d       ||j                  z  f}t        d |      }t        ||||      j                         S # t        $ r Y Sw xY w)Nr   zdevice mismatch: %s != %szcould not load z , generating random data insteadr   )r{  update_device_or_default_dtype_or_defaultr}  read_storagerV  rF   rG   r   r   itemsizerJ  r
   untyped_storage)r0   rZ  r[  rV  rW  r\  r   rI  s           r3   r\  zInputReader.storagef  s     99 IIQ#F+&z2
::!l&>
**11,? W^^+KK ;VW^^T OL>1QRS:...0#D6E6:v>NNPP % s   C" "	C.-C.)storage_offsetr   r   is_leafc                  t        ||      }t        |      }t        |      }t        |      }t	        |      }t        j                  g ||j                  |      }	t        j                         5  |	j                  ||||       d d d        |snt        j                         5  |	j                  t
        j                        }	d d d        t        j                         5  |	j                  ||||       d d d        t
        j                  j                  j                  |	      |k(  sJ t
        j                   j#                  |	|       | j$                  j'                  |	       |	S # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w)Nr  )r   rV  r   )memory_format)rJ  _storage_offset_or_defaultr  _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)
r0   r\  r   rI  r  r   r   r  metadatats
             r3   r_  zInputReader.tensor  sC    $F%83NC!%(%g.1-@LLeGNN-
 ]]_ 	;FF7NE6:	;""$ AGG%*?*?G@A ?wv>?  ++88;wFFF((H5			; 	;A A? ?s$   1E)$!E5!F)E25E>F
c                <    | j                   j                  |       |S r   r  r  )r0   vals     r3   rb  zInputReader.symint  s    		
r5   c                :    | j                   j                  d        y r   r  rd  s     r3   re  zInputReader.const      		r5   c                :    | j                   j                  d        y r   r  rd  s     r3   rg  zInputReader.unsupported  r  r5   c                    t         j                  j                  |   j                         }| j                  j                  |       |S r   )r   r   default_generatorsclone_stater  r  )r0   ri  rj  gens       r3   rk  zInputReader.generator  s6    jj++L9EEG		
r5   c                :    | j                   j                  d        y r   r  rm  s     r3   ro  zInputReader.opaque  r  r5   r   )r~  rq  r{  ztqdm | NonerM   rN   )
rZ  rq  r[  rr  rV  rs  rW  rt  rM   r   )r\  r   r   torch._prims_common.ShapeTyperI  %torch._prims_common.StrideType | Noner  ru  r   rt  r   bool | Noner  r  r  r   rM   torch.Tensor)r  r   rM   r   rv  )ri  rL   rj  rr  rM   torch._C.Generatorrw  rx  rU   r5   r3   rz  rz  X  s    %)BF"4?	$ =A)-Q Q Q
 :Q 'Q 
QB 9=	 &*$(%)# - 6	 # " #   
B
r5   rz  c                  p    e Zd ZddddZddZddd	 	 	 	 	 	 	 ddZddZdd	Zdd
ZddZ	ddZ
ddZy)InputWriterFstable_hashc                   g | _         t        j                         | _        || _        |t        ||      nd | _        i | _        y )Nr  )_lines	itertoolsr   storage_counterr~  r   r}  seen_storages)r0   r~  r  s      r3   r4   zInputWriter.__init__  sG    !#(0  # x[A 	

 9;r5   c                r    dg}|j                  d | j                  D               |j                  d       |S )Nzdef load_args(reader):c              3  &   K   | ]	  }d |   yw)rq   NrU   )r   ls     r3   r   z$InputWriter.lines.<locals>.<genexpr>  s     14s1s   zload_args._version = 0)extendr  r  )r0   rs     r3   lineszInputWriter.lines  s8    $
 	
1T[[11 	
)*r5   N)device_hintrW  c          
     D   t        |      }| j                  j                  |      }||S dt        | j                         }d}t        d       t        |      k7  rd|}d}|j                  }|j                  dk(  r|J |}t        d       |k7  rd|}|j                         }	d }
| j                  4|j                  j                  dk7  r| j                  j                  |      }
| j                  j                  | d|
d|	| | d       || j                  |<   |S )	Nbufr   z, dtype_hint=metaz	, device=z = reader.storage(, rt   )r   r  r2  rw   r  r  rV  rf   r  r[  r}  write_storager  r  )r0   r  r  rW  wsvmaybe_dtype_hintr   rV  r[  rZ  s              r3   r\  zInputWriter.storage  sD    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r5   c                t   ddl m}m} | j                  |j	                         |j
                  |j                        }g } | |t        d |j                        |j                                     s1|j                  t        t        |j                                            t        d       |j
                  k7  r|j                  d|j
                          |t        d       |j                         k(        s"|j                  d|j                                t         j"                  j%                  |      }|r&|j'                  d |j)                         D               t+        d       |j,                  k7  r|j                  d|j,                         t         j.                  j0                  j3                  |      }t5        d       |k7  r|j                  d	|       | j6                  j                  d
dj9                  |t        t        |j                              g|      z   d| z          y )Nr   )statically_known_truesym_eq)rW  r  r  zdtype=zstorage_offset=c              3  0   K   | ]  \  }}| d |  yw)=NrU   )r   kr  s      r3   r   z%InputWriter.tensor.<locals>.<genexpr>  s     IA1#QqeIs   zrequires_grad=zis_leaf=zreader.tensor(r  )  # )%torch.fx.experimental.symbolic_shapesr  r  r\  r  r   rV  rJ  r   rI  r  rL   tupler  r  r  r   r  get_tensor_metadatar  r{   r  r   r  r  r  r  r  r7   )	r0   r   r  r  r  r\  r  tensor_metadatar  s	            r3   r_  z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	
r5   c                   | j                   j                  d|dt        |              t        |t        t
        f      r| j                   j                  d       t        |      D ]  \  }}| d| d}t        |t        j                        r| j                  ||       ;t        |t        t        j                  f      r| j                  ||       n| j                  ||        | j                   j                  d       y y )Nzreader.unsupported(z#)  # unsupported type for dumping: z"""[])r  r  rf   r   r   r  	enumerater   r	   r_  rr  SymIntrb  rg  )r0   r   argr   aname_is         r3   rg  zInputWriter.unsupported!  s    !$)LTRUYKX	

 cD%=)KKu%!# 01 61#Qa.KK*C#67KK*$$VQ/0 KKu% *r5   c                H    | j                   j                  d|d| d       y )Nzreader.const(r  z!, filtered out during compilationr  r  rd  s     r3   re  zInputWriter.const5  s'    D85.OP	
r5   c                    t        |t        j                        r|j                  j                  }| j
                  j                  d|d|        y )Nzreader.symint(r  )r   r   r  r7  hintr  r  )r0   r   r  s      r3   rb  zInputWriter.symint;  s<    c5<<(((--C^C7%v>?r5   c                    |j                   }| j                  j                  d|j                  d|j                  d|        y )Nzreader.generator(r  r  )rV  r  r  rf   index)r0   r   r  rV  s       r3   rk  zInputWriter.generator@  s<    b0@dVL	
r5   c                F    | j                   j                  d|d|        y )Nzreader.opaque(r  r  )r0   r   rn  s      r3   ro  zInputWriter.opaqueF  s#    ^,=+@dVLMr5   )r~  rq  r  rP   rM   rN   )rM   rQ   )r  r   r  rs  rW  rt  rM   rL   )r   rL   r  r  rM   rN   )r   rL   r  r   rM   rN   rv  )r   rL   r  r   rM   rN   )r   rL   r  r  rM   rN   )r   rL   rn  rL   rM   rN   )rR   rS   rT   r4   r  r\  r_  rg  re  rb  rk  ro  rU   r5   r3   r  r    sf    DI 
;& BF)- '  ?	 
 '  
 D
@&(
@

Nr5   r  c           	        ddl m} |j                         D ci c]  \  }}||
 }}}dj                  |j	                               }t        j                  |       }	d| d}
d| d}d} G d	 d
      }i }|xs i dfddfd}| j                  } |j                         D ]  \  }}|dk(  rt        j                  ||      }|r>|j                         \  }}t        |j                  d            }||   } |||      ||<   t        j                  ||      }|s{ |j                  d            ||<    dt        j                  |       j                  v ro |       }||d<   t        j                   |
|	      D ]J  }|j                         \  }}}}t        |j                  d            }||   }t#        || |||             L |S c c}}w )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                      e Zd ZdZy)/aot_graph_input_parser.<locals>.TensorContainerz#Container for tensors as attributesN)rR   rS   rT   __doc__rU   r5   r3   TensorContainerr  n  s    -r5   r  rM   c                l     t        j                   v xs d u fd       j                         S )Nc                       dS )Nz; not in symbolic_shapes and default sym shape not passed inrU   )rb  s   r3   r>  z=aot_graph_input_parser.<locals>.get_sym_int.<locals>.<lambda>y  s    vhYZ r5   )r   _checkr2  )rb  default_sym_shapesym_shapes_dicts   `r3   get_sym_intz+aot_graph_input_parser.<locals>.get_sym_intv  s;    o%F):$)FZ	
 ""6+<==r5   c                   g }g }t        |       D ]a  \  }}|j                         }d|v r+ |      }|j                  |       |j                  |       E|sH|j                  t        |             c |j                  rt
        j                  nt
        j                  } |||
      }|D ]"  }	t
        j                  j                  ||	       $ |S )Nr   )r   rV  )
r  stripr  rr  r   r   randnzerosr   mark_dynamic)r   r   resolved_shapedynamic_dimsr   dimr   constructorr  rM  rV  r  s             r3   
gen_tensorz*aot_graph_input_parser.<locals>.gen_tensor}  s    & 	4FAs))+Ccz$%%a(##A&"))#c(3	4 &+%<%<ekk%++.fE 	/AMM&&sA.	/
r5   ,r   r0   )rb  rL   rM   rr  )r   r  r   torch.dtyperM   r	   )torch.utils._dtype_abbrsr  r{   r7   valuesinspect	getsource__annotations__researchgroupsr  r(   group	signaturerx   finditersetattr)funcrV  
sym_shapesr  r  r   r   	dtype_mapdtype_patternsourcetensor_assignment_regextensor_regexsym_shape_regexr  r1  r  r   r   
annotationmatch	data_type	shape_strr   r   	container	attr_namerl   r  r  s    ` `                       @@r3   aot_graph_input_parserr  J  s   * 5 &1%6%6%8)!sEs
)I ) +"4"4"67M t$F "<M?Jz{&67L&O. .  F&0&6BO>* &&K.[..0 8zH		,
3#(<<> Iy)//#./Ei(E&ue4F5M		/:6'A7F5M8" ""4(333#%	"v[[!8&A 	DE16.Iy)Q)//#./Ei(EIy*UE*BC	D M[)s   Gc                     t        j                         t        j                  j	                  t        j                  j                                dfd}d fd}t        j                  |       |S )z
    Decorator to cProfile a given function and save the result to disk on process exit.

    Args:
        filename: filename to save profile to
    c                H     t        j                         d fd       }|S )Nc                     j                          	  | i |j                          S # j                          w xY wr   )enabledisable)r  r1  fnprofs     r3   wrapperz3profile_to_file.<locals>.decorator.<locals>.wrapper  s1    KKM4*6*s   + =)r  r   r1  r   rM   r   )	functoolswraps)r  r  r  s   ` r3   	decoratorz"profile_to_file.<locals>.decorator  s%    			 
	 r5   c            	         j                          t        j                  j                  t	        j
                  d  d  d             y )Nz!                Wrote profile to z+, view with:

                    snakeviz z

                )
dump_statssysstderrrD   r9   r:   )r1   r  s   r3   save_itz profile_to_file.<locals>.save_it  sK    !

OO""* ,&Z (		
r5   )r  r   rM   r   rp  )cProfileProfiler&   r'   r)   
expanduseratexitregister)r1   r  r!  r  s   `  @r3   profile_to_filer'    sR     Dwwrww11(;<H	
 OOGr5   rO   )r   rP   rM   rL   )r   rL   rM   rN   )r   Sequence[Any]rM   	list[Any])FF)
rj   r   r  r(  r  rP   r  rP   rM   r   )F)rj   r   r  r   r   r(  r  rP   r  rP   r  rP   rM   rP   )r6  r   rM   r   )r   r  r6  r   r@  r)  rM   &tuple[torch.fx.GraphModule, list[Any]])r6  r   r@  r)  rM   r*  )rj   r   r   r(  rD  zACallable[[torch.fx.GraphModule, list[Any]], torch.fx.GraphModule]r  rP   r  rP   r  rP   rM   rP   )rI  r  r   r  rM   ztorch._prims_common.StrideType)rM  r   rM   zCallable[[T | None], T])r   NN)
r  z&Callable[[list[Tensor]], list[Tensor]]rV  rL   r  zdict[str, int] | Noner  ru  rM   zdict[str, Any])r1   rL   rM   zCallable[[T], T])ir  
__future__r   r%  r  r"  r  r[   r  r  loggingr&   r  r   r  rY   r9   collectionsr   	importlibr   typingr   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   r   collections.abcr   r   	torch.hubr   torch.storager   	getLoggerrR   rF   r   inductor_config	is_fbcoder   libfb.py.build_infolibfbr8   extra_importsr;   py
build_info	BuildInfoget_build_ruler,   r7   rE   r    r_   r}   rb   cacher   r   r   r   r   r  r   r   r  r&  r9  rA  r  rF  rJ  rN  float32r  rV  r  r  r  r  rQ  rz  r  r  r'  )r<   s   0r3   <module>rF     s  $ #         	 	  
    # . .  # #  . @ . ; M  9 9 2, g!CL   89$$& 

J $$..==?GG	SWXJIITA!9!B?TUM 64 4n  c cL  6 7< 	2 5: (@V	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1V )V $	V/ "%--0 "<5<<#67 *1- )%0 #E*  F\ \VDN DNR (,$(	d
0dd &d "	d
 dN#{ Us   J