
    `i	                       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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. erddl/m0Z0 ddl1m2Z2 ddl3m4Z4  e
j5        e6          Z7 ed          Z8 ed          Z9e9:                                Z;e;rddl<Z=g Z>dZ?dZ@e;rZg dZ>e=jA        jB        jC        D                                E                    dd          Z@dF                    d e>D                       Z?g dZG G d d          ZHdid ZId!ZJ G d" d#          ZKejL        did$            ZMd%d&djd)ZNd%d&djd*ZOdid+ZPdkd.ZQ G d/ d0eR          ZSdld4ZT	 	 dmdnd;ZU	 dod%d%d<dpd@ZVdqdBZWdrdGZXdsdHZY	 dod%d%d<dtdKZZdudQZ[dvdTZ\ e\ej]                  Z^ e\ ej_        dU                    Z` e\d          Za e\d%          Zb e\d%          Zc G dV dW          Zd G dX dY          Ze G dZ d[          Zf	 	 	 dwdxdeZgdydhZhdS )za  
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://
c                    g | ]}d | d	S )ztorch.ops.load_library("z") .0xs     m/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/_dynamo/debug_utils.py
<listcomp>r#   S   s$    TTTA?!???TTT    )buck2runz@mode/dev-nosanc                  (    e Zd ZddZddZdddZdS )BuckTargetWriterfilenamestrreturnNonec                (   t           j                            t           j                            |                    \  | _        | _        | j                            dd          | _        | j                            dd           d| j         | _        | j        | j                            d          d          | _        | j        dd          | _        | j        }||                    d          d          dd          }d| d	| j         | _	        d S )
Nz.pyr   /.zfbcode.   zfbcode/r   :)
ospathsplitabspathsubdirpy_filereplacetargetfindcmd_line_path)selfr)   tmps      r"   __init__zBuckTargetWriter.__init__Z   s    $&GMM"'//(2K2K$L$L!T\l**5"55 {**344DDt{DD	IdinnY7799:	IabbM	 k#((9%%''(,5#5555r$   c                    d                     d t          D                       }t          j        d| j         d| j         dt           d| d| j         d          S )	Nr   c                    g | ]}d | d	S )z	        "z",r   r   s     r"   r#   z*BuckTargetWriter.build.<locals>.<listcomp>i   s$    #J#J#J!$5$5$5$5#J#J#Jr$   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dedentr9   r7   
cur_targetr3   )r<   extra_cpp_depss     r"   buildzBuckTargetWriter.buildh   s    #J#Jz#J#J#JKK ;	 
 l     " I#  
 
 	
r$   T	print_msgbool	list[str]c                n   t           j                            | j        d          }t	          |d          5 }|                    |                                            d d d            n# 1 swxY w Y   t          | j        gz   }|r.t          
                    dd                    |                     |S )NTARGETSwzFFound an example that reproduces the error. Run this cmd to repro - %s )r2   r3   rA   r6   openwriterG   BUCK_CMD_PREFIXr;   logwarning)r<   rH   target_filefd	cmd_splits        r"   rP   zBuckTargetWriter.write   s    gll4;	::+s## 	#rHHTZZ\\"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# $t'9&::	 	KKX##   s   (A**A.1A.N)r)   r*   r+   r,   r+   r*   )T)rH   rI   r+   rJ   )__name__
__module____qualname__r>   rG   rP   r   r$   r"   r(   r(   Y   sU        6 6 6 6
 
 
 
4      r$   r(   r+   r*   c                    t           j                            t                      d          } | )t	          j                     dt          j                     } t           j                            |           st          j	        | d           | S )Nminifierz
/minifier_T)exist_ok)
r2   r3   rA   r   tempfile
gettempdirgetpassgetuserexistsmakedirs)r3   s    r"   minifier_dirrd      st    7<<44D|%''FF7?3D3DFF7>>$ )
D4((((Kr$      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        j        ej        j        gZed	d            Zed
d            ZdS )NNModuleToStringgmtorch.fx.GraphModuler+   rI   c                   t                      }|                                 D ]5\  }}t          |          t          j        vr|                    |           6t          |          dk    rt                              d|           dS )Nr   z-We have not tested reprs of some modules - %sT)	setnamed_childrentyperg   
safe_reprsaddlenrR   rS   )rh   cant_convert_modules       r"   can_convert_to_stringz&NNModuleToString.can_convert_to_string   s    uu**,, 	) 	)IAvF||#3#>>>  (((|q  KKGVVVtr$   r*   c                   ddl m} d}t          j        d          }|                                 D ][\  }}|                                 }t          |                                d           }||j        r| d}||dz   d| d| d	z  }\| j	        
                                D ]\  }}	|	|	                                t          k    r(dd
lm}
 |
j        t          k    sJ t!          |	          }nUt#          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        
                                D ]F\  }}|d}|j        rd}dt'          |j                   d|j         | d}||dz   d| d| d	z  }G| || 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))re   )torch.nn.modules.modulerv   rC   rD   rl   __repr__next
parametersis_cuda_buffersitemsnumelMAX_CONSTANT_NUMEL_INLINEtorch._tensor_strry   	thresholdreprtorchis_floating_pointlistshapedtype_parameterscode)rh   rv   tab	model_strmodule_namers   
module_strexample_parambuffer_namebufferry   
tensor_str
param_nameparammaybe_devices                  r"   convertzNNModuleToString.convert   s   666666O
 
	 $&#4#4#6#6 	I 	IK"OO--/J !!2!2!4!4d;;M(]-B( *333
C!GHH+HH*HHHHII#%;#4#4#6#6 	 	K~||~~!:::888888!+/HHHHH!&\\

(00 WD,>,>WWWWW

 YT&,-?-?XXXXX  ~ 4 *333
7QQ+QQ*QQQII "$!5!5!7!7 	H 	HJ}L} 10s4;L;LssV[VascosssJC!GGG*GGGGGGII 	

27A..2222	r$   N)rh   ri   r+   rI   )rh   ri   r+   r*   )rX   rY   rZ   r   nnLinearConv1dConv2dConv3dBatchNorm1dBatchNorm2dBatchNorm3d	LayerNormDropoutSoftmaxReLUGELUIdentity	MaxPool2d	EmbeddingTanhConvTranspose1dGLULSTMFlattenAdaptiveAvgPool2drn   staticmethodrt   r   r   r$   r"   rg   rg      s         "+J0 	 	 	 \	 = = = \= = =r$   rg   c                 <   t           j                                        sdS d} 	 t          j        ddg          }|                                                    d          }d                    d |D                       }| | dz  } n!# t          t          j	        f$ r | dz  } Y nw xY wt          d	 t          t           j                                                  D                       }| d
z  } |                                D ]\  }}| d| d| dz  } | dz  } | S )Nz:# torch.cuda.is_available()==False, no GPU info collected
z# CUDA Info: 
nvccz	--versionr   r   c                "    g | ]}|d vd| dS ))r   #  
r   )r    ss     r"   r#   z-_cuda_system_info_comment.<locals>.<listcomp>  s%    TTT1atmm;;;;mmmr$   z# nvcc not found
c              3  T   K   | ]#}t           j                            |          V  $d S N)r   cudaget_device_name)r    is     r"   	<genexpr>z,_cuda_system_info_comment.<locals>.<genexpr>  sC        *+
""1%%     r$   z# GPU Hardware Info: 
r   z : r   )r   r   is_available
subprocesscheck_outputdecoder4   rA   FileNotFoundErrorCalledProcessErrorr   rangedevice_countr   )r   cuda_version_outcuda_version_linescomment	gpu_namesnamecounts          r"   _cuda_system_info_commentr     sa   :""$$ MLL!I*%2FK3HII-4466<<TBB''TT0BTTTUU^^^#		z<= * * *))			*   /4UZ5L5L5N5N/O/O    I **I (( . .e-$--5----		Is   A$B	 	B'&B'F)stable_outputr   rI   c                    | rdS g dg ddfdfd	t           j                                        D             }d
                    |          }t	          d| d          S )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_PATHkeyr*   r+   rI   c                D     t           fdD                       o vS )Nc              3      K   | ]}|v V  	d S r   r   )r    stringr   s     r"   r   z;generate_env_vars_string.<locals>.filter.<locals>.<genexpr>&  s'      ::V6S=::::::r$   )any)r   
allow_list	skip_lists   `r"   filterz(generate_env_vars_string.<locals>.filter%  s1    ::::z:::::Ss)?SSr$   c                >    g | ]\  }} |          d | d| dS )zos.environ['z'] = ''r   )r    r   valuer   s      r"   r#   z,generate_env_vars_string.<locals>.<listcomp>(  sN       C6#;;*s**%***  r$   r   z
import os
z
    )r   r*   r+   rI   )r2   environr   rA   r   )r   config_linesconfig_stringr   r   r   s      @@@r"   generate_env_vars_stringr     s      =<<:::JUUUIT T T T T T T   ***,,  L
 IIl++M# %% % % 	 	 	r$   c           	     &   dd l }dd l}| rdS |j        j        j                                        }d|j        j                                         d|j        j                                         d|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      r"   generate_config_stringr   4  s    """"!!!! <;;(/7FFHH	
 $$&&	 	 &&((	 	 ''))	 	 	 	 	 	r$   c                 Z    t           j                            t                      d          S )Nzminifier_launcher.py)r2   r3   rA   rd   r   r$   r"   get_minifier_repro_pathr   H  s    7<<(>???r$   contentsr,   c                   t                      }t                              d|           t          r!t	          |                                           	 t          |d          5 }|                    |            d d d            d S # 1 swxY w Y   d S # t          $ r/}t                              d           t          d          |d }~ww xY w)NzWriting minified repro to:
%srM   r   z(Could not write to {minified_repro_path})
r   rR   rS   use_buckr(   rP   rO   OSError	exceptionNotImplementedError)r   minified_repro_pathrU   es       r"   helper_for_dump_minifyr   L  s   133KK02EFFF 6,--33555U%s++ 	rHHX	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  U U Ub!"LMMSTTUs<   B #B9B B

B B
B 
C*CCc                      e Zd ZdS )AccuracyErrorN)rX   rY   rZ   r   r$   r"   r   r   [  s        Dr$   r   example_inputsSequence[Any]	list[Any]c                    t          |           }t          t          |                     D ]H}t          ||         t          j                  r&||                             | |         j                   I|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   rp   
isinstancer   r   requires_grad_requires_grad)r   cloned_inputsidxs      r"   clone_inputs_retaining_gradnessr  _  st     !00MS(()) Q QmC(%,77 	Q#--nS.A.OPPPr$   rh   ri   argsonly_fwddisable_cloner   c                d   ddl m}m}m} t	          j        |           } |st          |          }t          | d          r|                     d           t          | dd          r | |          n | | }|r|S  ||          r ||          }|
                                  || |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)	rh   r  r  r	  r  r  r  outlosss	            r"   run_fwd_maybe_bwdr  l  s     SRRRRRRRRR	r		B 5.t44r; 
T b-77
F""T(((RRYC 
 $$S))?2sD$///r$   require_fp64ignore_non_fpopt_gmr  r  c                  ddl m} t          | ||          }d}t          j        r	 t          t          j        |           t          |                    \  }	}
t          |	|
|          }n;# t          $ r. |rt          d          t                              d           Y nw xY w	 t          |||          }n+# t          $ r t                              d           Y dS w xY w ||||t          j        d|          }|S )	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RuntimeErrorrR   rS   r   repro_tolerance)rh   r  r   r  r  r  r  reffp64_ref
fp64_modelfp64_examplesrespassings                r"   same_two_modelsr-    s]   " 
B
9
9CH& ;
	;(4b!!#B>#R#R) )%J )]HMMHH 	; 	; 	; "|   KK9:::::	;
AA    	$	
 	
 	

 tt d"#  G Ns$   AA+ +5B#"B#'B9 9$C! C!modelc                v   | j         j        D ]}|j        dk    r|j        t          j        j        j        j        k    rmt          |j
                  dk    sJ t          |j
        d                   r9|j
        d         t          j        k    r|j
        d         t          j        f|_
        |j        dk    rU|j                            d          }|9t          |          r*t          |j                  }t          j        |d<   ||_        | j                                          |                                  | S )Ncall_functionrx   r   r   r   )graphnodesopr9   r   opsprimsconvert_element_typedefaultrp   r  r   float64kwargsgetdictlint	recompile)r.  noder   
new_kwargss       r"   cast_dtype_args_to_fp64r@    s   ! ) )G&&uyCKKKty>>Q&&&&dil++ :	!0M0M!Yq\5=9	7o%%KOOG,,E ^E%:%: !$+..
&+m
7#(	K	OOLr$   r   torch.dtypeinputs&tuple[torch.fx.GraphModule, list[Any]]c                     ddl m} |                               } t          j        k    rt          |          } | fd|          }||fS )Nr   )tree_mapc                    t          | t          j                  r)|                                 r|                               n| S r   )r  r   r   r   to)r!   r   s    r"   <lambda>zcast_to.<locals>.<lambda>  sA    a&&+,+>+>+@+@!$$u+++ r$   )torch.utils._pytreerE  rG  r   r8  r@  )r   r.  rB  rE  s   `   r"   cast_torJ    sx     -,,,,,HHUOOE (..X	 	 	 	 		 F &=r$   c                8    t          t          j        | |          S r   )rJ  r   r8  )r.  rB  s     r"   r#  r#    s     5=%000r$   compiler_fnACallable[[torch.fx.GraphModule, list[Any]], torch.fx.GraphModule]c                   	  |t          j        |           t          |                    }t          | |||||           S # t          $ r t
                              d           Y dS 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$  rR   r   )rh   r   rL  r  r  r  compiled_gms          r"   backend_accuracy_failsrP    s    !kM">~NN
 
 #%'
 
 
 
 	
     	#	
 	
 	

 uus   A A $A+*A+stride(Optional[torch._prims_common.StrideType]r   torch._prims_common.ShapeTypetorch._prims_common.StrideTypec               2    | | nt          j        |          S r   )r!  make_contiguous_strides_for)rQ  r   s     r"   _stride_or_defaultrW    s    
 '66U-Nu-U-UUr$   dCallable[[Optional[T]], T]c                      fdS )Nc                    | | nS r   r   )r!   rX  s    r"   rH  z_mk_defaulter.<locals>.<lambda>'  s    !-QQQ r$   r   )rX  s   `r"   _mk_defaulterr\  &  s    .....r$   cpuc                  6    e Zd ZddZdddddZddZddZdS )NopInputReaderr+   r,   c                    d| _         d S )Nr   total)r<   s    r"   r>   zNopInputReader.__init__2  s    


r$   Ndevice
dtype_hintstorage_hashOptional[str]nbytesintrd  ,Optional[torch._prims_common.DeviceLikeType]re  Optional[torch.dtype]c               &    | xj         dz  c_         d S )Nr   ra  )r<   rf  rh  rd  re  s        r"   storagezNopInputReader.storage5  s     	

a



r$   r  r   r9  Optional[torch.Tensor]c                    d S r   r   r<   r  r9  s      r"   tensorzNopInputReader.tensor?      r$   Optional[int]c                    d S r   r   rp  s      r"   symintzNopInputReader.symintB  rr  r$   r+   r,   )
rf  rg  rh  ri  rd  rj  re  rk  r+   r,   )r  r   r9  r   r+   rn  )r  r   r9  r   r+   rs  rX   rY   rZ   r>   rm  rq  ru  r   r$   r"   r_  r_  1  sx            @D,0             r$   r_  c                  N    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S )*InputReaderN)pbarsave_dirrg  rz  Optional[tqdm]c                   |t                               d           |t          |          nd | _        g | _        || _        d S )Nz0no save_dir specified, will generate random data)rR   rS   r   storer  rz  )r<   r{  rz  s      r"   r>   zInputReader.__init__I  sI    
 KKJKKK5=5I'111t
!				r$   rc  rf  rh  ri  rd  rj  re  rk  r+   r   c                  | j         | j                             d           t          |          }t          |          }| j        [|Y	 | j                            |          }||j        k    r!t                              d||j                   |S # t          $ r Y nw xY wt                              d|           ||j
        z  f}t          d |          }t          ||||                                          S )Nr   zdevice mismatch: %s != %sz1could not load %s, generating random data insteadr   )rz  update_device_or_default_dtype_or_defaultr~  read_storagerd  rR   rS   r   itemsizerW  r   untyped_storage)r<   rf  rh  rd  re  rm  r   rQ  s           r"   rm  zInputReader.storageT  s    9 IQ#F++&z22
:!l&>
*11,?? W^++KK ;VW^TTT  %    	GVVV:..0#D666E6:v>>NNPPPs   
B 
BB)storage_offsetr   r  is_leafrm  r   rS  rQ  rR  r  rs  r   r  Optional[bool]r  metadatar   torch.Tensorc               H   t          ||          }t          |          }t          |          }t          |          }t	          |          }t          j        g ||j        |          }	t          j                    5  |		                    ||||           d d d            n# 1 swxY w Y   |st          j
                    5  |	                    t
          j                  }	d d d            n# 1 swxY w Y   t          j                    5  |		                    ||||           d d d            n# 1 swxY w Y   t
          j        j                            |	          |k    sJ t
          j                            |	|           | j                            |	           |	S )Nr  )r   rd  r  )memory_format)rW  _storage_offset_or_defaultr  _is_leaf_or_default_requires_grad_or_defaultr   rq  rd  no_gradset_enable_gradclonepreserve_format_subclasses
meta_utilssafe_is_leaf_utilsset_tensor_metadatar  append)
r<   rm  r   rQ  r  r   r  r  r  ts
             r"   rq  zInputReader.tensorq  sJ    $F%8883NCC!%((%g..1-@@LeGN-
 
 
 ]__ 	; 	;FF7NE6:::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	?"$$ A AGG%*?G@@A A A A A A A A A A A A A A A ? ?wv>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? +88;;wFFFF((H555	s6   >B##B'*B'!C00C47C4D33D7:D7valc                :    | j                             |           |S r   )r  r  )r<   r  s     r"   ru  zInputReader.symint  s    	
r$   r   )r{  rg  rz  r|  )
rf  rg  rh  ri  rd  rj  re  rk  r+   r   )rm  r   r   rS  rQ  rR  r  rs  r   rk  r  r  r  r  r  r   r+   r  )r  r   r+   r   rw  r   r$   r"   ry  ry  H  s        	QU 	 	 	 	 	 	  @D,0Q Q Q Q Q QB <@	 )-'+(,"&     B     r$   ry  c                  T    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S )'InputWriterFstable_hashr{  rg  r  rI   r+   r,   c                   g | _         t          j                    | _        || _        |t          ||          nd | _        i | _        d S )Nr  )_lines	itertoolsr   storage_counterr{  r   r~  seen_storages)r<   r{  r  s      r"   r>   zInputWriter.__init__  sW    !#(00  # x[AAAA 	

 9;r$   rJ   c                ~    dg}|                     d | j        D                        |                    d           |S )Nzdef load_args(reader):c              3      K   | ]	}d | V  
dS )rw   Nr   )r    ls     r"   r   z$InputWriter.lines.<locals>.<genexpr>  s(      11111111r$   zload_args._version = 0)extendr  r  )r<   rs     r"   lineszInputWriter.lines  sK    $
 	
11T[111111 	
)***r$   N)device_hintre  r  r   r  rj  re  rk  r*   c          
     0   t          |          }| j                            |          }||S dt          | j                   }d}t          d           t          |          k    rd|}d}|j        }|j        dk    r|J |}t          d           |k    rd|}|	                                }	d }
| j
        *|j        j        dk    r| j
                            |          }
| j                            | d|
d|	| | d           || j        |<   |S )	Nbufr   z, dtype_hint=metaz	, device=z = reader.storage(, rz   )r   r  r:  r}   r  r  rd  rm   r  rh  r~  write_storager  r  )r<   r  r  re  wsvmaybe_dtype_hintr   rd  rh  rf  s              r"   rm  zInputWriter.storage  s^    O,,""2&&=H.$t+,,..T""&7
&C&CCC=z==  ';&  *** Fd##v--1v11L '')):!o&<&AV&K&K:33ODDLaaLaafaaN^aaa	
 	
 	
 "#2r$   r   r  r  c                   ddl m}m} |                     |                                |j        |j                  }g } | |t          d |j                  |	                                                    sA|
                    t          t          |	                                                               t          d           |j        k    r|
                    d|j                    |t          d           |                                k              s*|
                    d|                                           t           j                            |          }|r1|                    d |                                D                        t+          d           |j        k    r|
                    d|j                   t           j        j                            |          }t5          d           |k    r|
                    d	|           | j        
                    d
d                    |t          t          |j                            g|          z   d| z              d S )Nr   )statically_known_truesym_eq)re  r  r  zdtype=zstorage_offset=c              3  *   K   | ]\  }}| d |V  dS )=Nr   )r    kr  s      r"   r   z%InputWriter.tensor.<locals>.<genexpr>  s0      IIA1qIIIIIIr$   zrequires_grad=zis_leaf=zreader.tensor(r  )  # )%torch.fx.experimental.symbolic_shapesr  r  rm  r  r   rd  rW  r   rQ  r  r*   tupler  r  r  r   r  get_tensor_metadatar  r   r  r  r  r  r  r  r  rA   )	r<   r   r  r  r  rm  r  tensor_metadatar  s	            r"   rq  zInputWriter.tensor  sl   WWWWWWWW,,AG  
 
 $$F%d!':::AHHJJGG
 
 	0 KKE!((**--..///T""ag--KK,,,---$$&t,,0@0@0B0BB
 
 	B KK@!*:*:*<*<@@AAA,::1== 	JKKII1F1F1H1HIIIIII$T**ao==KK<<<===#.;;A>>t$$//KK.7..///ii#eAGnn"5"5==>>?dnn	
 	
 	
 	
 	
r$   argr   c                <   | j                             d| dt          |                      t          |t          t
          f          r| j                             d           t          |          D ]\  }}| d| d}t          |t          j                  r| 	                    ||           >t          |t          t          j        f          r|                     ||           v|                     ||           | j                             d           d S d S )Nr   z# was unsupported type for dumping: z"""[])r  r  rm   r  r   r  	enumerater   r   rq  ri  SymIntru  unsupported)r<   r   r  r   aname_is         r"   r  zInputWriter.unsupported  s!   TTTcTTUUU cD%=)) 
	&Ku%%%!# 0 01 1a.. 0KK****C#677 0KK****$$VQ////Ku%%%%%
	& 
	&r$   c                H    | j                             d|d| d           d S )Nzreader.const(r  z!, filtered out during compilation)r  r  )r<   r   s     r"   constzInputWriter.const  s<    PDPPPPP	
 	
 	
 	
 	
r$   r  c                    t          |t          j                  r|j        j        }| j                            d|d|            d S )Nzreader.symint(r  )r  r   r  r>  hintr  r  )r<   r   r  s      r"   ru  zInputWriter.symint  sL    c5<(( 	 (-C>C>>>>?????r$   )r{  rg  r  rI   r+   r,   )r+   rJ   )r  r   r  rj  re  rk  r+   r*   )r   r*   r  r  r+   r,   )r   r*   r  r   r+   r,   )r   r*   r+   r,   )r   r*   r  r   r+   r,   )
rX   rY   rZ   r>   r  rm  rq  r  r  ru  r   r$   r"   r  r    s        GL 
; 
; 
; 
; 
; 
;   & EI,0           D
 
 
 
@& & & &$
 
 
 
@ @ @ @ @ @r$   r  r   func&Callable[[list[Tensor]], list[Tensor]]rd  
sym_shapesOptional[dict[str, int]]default_sym_shapers  dict[str, Any]c           	        ddl m} d |                                D             }d                    |                                          }t          j        |           }d| d}d| d}	d	}
 G d
 d          }i }|pi dfddfd}| j        } |j                    D ]\  }}|dk    rt          j	        |	|          }|rP|
                                \  }}t          |                    d                    }||         } |||          ||<   t          j	        |
|          }|r! |                    d                    ||<   dt          j        |           j        v r |            }||d<   t          j        ||          D ]`}|
                                \  }}}}t          |                    d                    }||         }t#          || |||                     a|S )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_abbrsc                    i | ]\  }}||	S r   r   )r    r   r   s      r"   
<dictcomp>z*aot_graph_input_parser.<locals>.<dictcomp>6  s+     ) ) )!sEs) ) )r$   |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dS )/aot_graph_input_parser.<locals>.TensorContainerz#Container for tensors as attributesN)rX   rY   rZ   __doc__r   r$   r"   TensorContainerr  C  s        ----r$   r  ru  r*   r+   ri  c                n     t          j         v pd u fd                                          S )Nc                       dS )Nz; not in symbolic_shapes and default sym shape not passed inr   )ru  s   r"   rH  z=aot_graph_input_parser.<locals>.get_sym_int.<locals>.<lambda>N  s    vZZZ r$   )r   _checkr:  )ru  r  sym_shapes_dicts   `r"   get_sym_intz+aot_graph_input_parser.<locals>.get_sym_intK  sP    o%F):$)FZZZZ	
 	
 	
 ""6+<===r$   r   rS  r   rA  r   c                   g }g }t          |           D ]w\  }}|                                }d|v r6 |          }|                    |           |                    |           S|r"|                    t          |                     x|j        rt
          j        nt
          j        } |||
          }|D ]"}	t
          j        	                    ||	           #|S )Nr   )r   rd  )
r  stripr  ri  r   r   randnzerosr   mark_dynamic)r   r   resolved_shapedynamic_dimsr   dimr   constructorr  rX  rd  r  s             r"   
gen_tensorz*aot_graph_input_parser.<locals>.gen_tensorR  s    && 	4 	4FAs))++CczzK$$%%a(((##A&&&& 4"))#c((333%*%<Mekk%+k.fEEE 	/ 	/AM&&sA....
r$   ,r   r<   )ru  r*   r+   ri  )r   rS  r   rA  r+   r   )torch.utils._dtype_abbrsr  r   rA   valuesinspect	getsource__annotations__researchgroupsr  r4   group	signaturer~   finditersetattr)r  rd  r  r  r  	dtype_mapdtype_patternsourcetensor_assignment_regextensor_regexsym_shape_regexr  r9  r  r   r   
annotationmatch	data_type	shape_strr   r   	container	attr_namerr   r  r  s    ` `                     @@r"   aot_graph_input_parserr    s   * 544444) )%0%6%6%8%8) ) )I +"4"4"6"677M t$$F |M{{{7777L&O. . . . . . . .  F&0&6BO> > > > > > >      * &K.[.00 8 8zH	,
33 	5#(<<>> Iy)//#..//Ei(E&Jue44F5M	/:66 	8'KA77F5M"4((333#O%%	"v[!8&AA 	D 	DE16.Iy)Q)//#..//Ei(EIy**UE*B*BCCCCMr$   r)   Callable[[T], T]c                     t          j                    t          j                            t          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
    fnr   r+   c                L     t          j                   d fd            }|S )Nr  r   r9  r+   c                                                       	  | i |                                 S #                                  w xY wr   )enabledisable)r  r9  r
  profs     r"   wrapperz3profile_to_file.<locals>.decorator.<locals>.wrapper  sH    KKMMMr4*6**s	   3 A	)r  r   r9  r   r+   r   )	functoolswraps)r
  r  r  s   ` r"   	decoratorz"profile_to_file.<locals>.decorator  sC    				 	 	 	 	 	 
		 r$   r,   c            	                                          t          j                            t	          j        d  d  d                     d S )Nz!                Wrote profile to z+, view with:

                    snakeviz z

                )
dump_statssysstderrrP   rC   rD   )r)   r  s   r"   save_itz profile_to_file.<locals>.save_it  sn    !!!
O"*  '   		
 		
 		
 		
 		
r$   )r
  r   r+   r   rv  )cProfileProfiler2   r3   r5   
expanduseratexitregister)r)   r  r  r  s   `  @r"   profile_to_filer    s     Dwrw11(;;<<H	 	 	 	 	 	
 
 
 
 
 
 
 OGr$   rW   )r   rI   r+   r*   )r   r*   r+   r,   )r   r   r+   r   )FF)
rh   ri   r  r   r  rI   r	  rI   r+   r   )F)rh   ri   r  ri   r   r   r  rI   r  rI   r  rI   r+   rI   )r.  ri   r+   ri   )r   rA  r.  ri   rB  r   r+   rC  )r.  ri   rB  r   r+   rC  )rh   ri   r   r   rL  rM  r  rI   r  rI   r  rI   r+   rI   )rQ  rR  r   rS  r+   rT  )rX  r   r+   rY  )r   NN)
r  r  rd  r*   r  r  r  rs  r+   r  )r)   r*   r+   r  )ir  
__future__r   r  r  r  r  r`   r  r  loggingr2   r  r   r  r^   rC   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   	getLoggerrX   rR   r   inductor_config	is_fbcoder   libfb.py.build_infolibfbrB   extra_importsrE   py
build_info	BuildInfoget_build_ruler8   rA   rQ   r(   rd   r   rg   cacher   r   r   r   r   r$  r   r  r  r-  r@  rJ  r#  rP  rW  r\  float32r  rd  r  r  r  r  r_  ry  r  r  r  r   r$   r"   <module>r:     sI   $ # " " " " "               				 				     



         # # # # # # B B B B B B B B B B B B B B  # # # # # # # # # #       . . . . . . @ @ @ @ @ @ . . . . . . ; ; ; ; ; ; M M M M M M M M       . . . . . . . .  -((((((,,,,,, g!!GCLL  - 899$$&&  

 V  J $.==??GG	SWXXJIITTTTTUUM 6554 4 4 4 4 4 4 4n     c c c c c c c cL    0 7< 	 	 	 	 	 	2 5:      (@ @ @ @U U U U	 	 	 	 	I 	 	 	
 
 
 
  	0 0 0 0 0H 	7 7 7 7 7 7 7t   *   (1 1 1 1 	      RV V V V/ / / / "M%-00 "]<5<#6#677 *]1-- )M%00 #mE**        .L L L L L L L Lvy@ y@ y@ y@ y@ y@ y@ y@| +/'+	b b b b bJ# # # # # #r$   