
    Wj?                       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 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/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5  e
j6        e7          Z8 ed          Z9 ed          Z:e:;                                Z<e<rddl=Z>g Z?dZ@dZAe<rZg dZ?e>jB        jC        jD        E                                F                    dd          ZAdG                    d e?D                       Z@g dZH G d d          ZIdid ZJd!ZK G d" d#          ZLejM        did$            ZNd%d&djd)ZOd%d&djd*ZPdid+ZQdkd.ZR G d/ d0eS          ZTdld4ZU	 	 dmdnd;ZV	 dod%d%d<dpd@ZWdqdBZXdrdGZYdsdHZZ	 dod%d%d<dtdKZ[dudQZ\dvdTZ] e]ej^                  Z_ e] ej`        dU                    Za e]d          Zb e]d%          Zc e]d%          Zd G dV dW          Ze G dX dY          Zf G dZ d[          Zg	 	 	 dwdxdeZhdydhZid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Optional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://
c                    g | ]}d | d	S )ztorch.ops.load_library("z") .0xs     ^/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/torch/_dynamo/debug_utils.py
<listcomp>r$   T   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__[   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>j   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dedentr:   r8   
cur_targetr4   )r=   extra_cpp_depss     r#   buildzBuckTargetWriter.buildi   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 )r3   r4   rB   r7   openwriterH   BUCK_CMD_PREFIXr<   logwarning)r=   rI   target_filefd	cmd_splits        r#   rQ   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)rI   rJ   r,   rK   )__name__
__module____qualname__r?   rH   rQ   r   r%   r#   r)   r)   Z   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)
r3   r4   rB   r   tempfile
gettempdirgetpassgetuserexistsmakedirs)r4   s    r#   minifier_dirre      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,   rJ   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typerh   
safe_reprsaddlenrS   rT   )ri   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))rf   )torch.nn.modules.modulerw   rD   rE   rm   __repr__next
parametersis_cuda_buffersitemsnumelMAX_CONSTANT_NUMEL_INLINEtorch._tensor_strrz   	thresholdreprtorchis_floating_pointlistshapedtype_parameterscode)ri   rw   tab	model_strmodule_namert   
module_strexample_parambuffer_namebufferrz   
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)ri   rj   r,   rJ   )ri   rj   r,   r+   )rY   rZ   r[   r   nnLinearConv1dConv2dConv3dBatchNorm1dBatchNorm2dBatchNorm3d	LayerNormDropoutSoftmaxReLUGELUIdentity	MaxPool2d	EmbeddingTanhConvTranspose1dGLULSTMFlattenAdaptiveAvgPool2dro   staticmethodru   r   r   r%   r#   rh   rh      s         "+J0 	 	 	 \	 = = = \= = =r%   rh   c                 j   t           j                                        sdS d} 	 t           j        j        et          j        ddg          }|                                                    d          }d	                    d |D                       }| | dz  } n| 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 k    d| dS )r   #  
r   )r!   ss     r#   r$   z-_cuda_system_info_comment.<locals>.<listcomp>  s%    RRRq!r''{A{{{'''r%   z'# Not searching for nvcc on ROCM setup
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versionhip
subprocesscheck_outputdecoder5   rB   FileNotFoundErrorCalledProcessErrorr   rangedevice_countr   )r   cuda_version_outcuda_version_linescomment	gpu_namesnamecounts          r#   _cuda_system_info_commentr     sx   :""$$ MLL!I	*=$)67LMM!1!8!8!:!:!@!@!F!FggRR4FRRRSSGG'IICCIz<= * * *))			*   /4UZ5L5L5N5N/O/O    I **I (( . .e-$--5----		Is   A;B   B>=B>F)stable_outputr   rJ   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,   rJ   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           	     f    g | ]-\  }} |          d | d|                     dd           d.S )zos.environ['z'] = ''")r9   )r!   r   valuer   s      r#   r$   z,generate_env_vars_string.<locals>.<listcomp>,  s\       C6#;;@@@EMM#s$;$;@@@  r%   r   z
import os
z
    )r   r+   r,   rJ   )r3   environr   rB   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   8  s    """"!!!! <;;(/7FFHH	
 $$&&	 	 &&((	 	 ''))	 	 	 	 	 	r%   c                 Z    t           j                            t                      d          S )Nzminifier_launcher.py)r3   r4   rB   re   r   r%   r#   get_minifier_repro_pathr   L  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          $ r2}t                              d           t          d|           |d }~ww xY w)NzWriting minified repro to:
%srN   r   zCould not write to )
r   rS   rT   use_buckr)   rQ   rP   OSError	exceptionNotImplementedError)r   minified_repro_pathrV   es       r#   helper_for_dump_minifyr   P  s#   133KK02EFFF 6,--33555V%s++ 	rHHX	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  V V Vb!"M8K"M"MNNTUUVs<   B #B9B B

B B
B 
C-C

Cc                      e Zd ZdS )AccuracyErrorN)rY   rZ   r[   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   rq   
isinstancer   r
   requires_grad_requires_grad)r  cloned_inputsidxs      r#   clone_inputs_retaining_gradnessr
  c  st     !00MS(()) Q QmC(%,77 	Q#--nS.A.OPPPr%   ri   rj   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)	ri   r  r  r  r  r  r  outlosss	            r#   run_fwd_maybe_bwdr  p  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RuntimeErrorrS   rT   r   repro_tolerance)ri   r   r  r  r  r  r"  reffp64_ref
fp64_modelfp64_examplesrespassings                r#   same_two_modelsr1    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                r   | j         j        D ]}|j        dk    r|j        t          j        j        j        j        u 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_functionry   r   r   r   )graphnodesopr:   r   opsprimsconvert_element_typedefaultrq   r  r   float64kwargsgetdictlint	recompile)r2  noder   
new_kwargss       r#   cast_dtype_args_to_fp64rD    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._pytreerI  rK  r   r<  rD  )r   r2  rF  rI  s   `   r#   cast_torN    sx     -,,,,,HHUOOE (..X	 	 	 	 		 F &=r%   c                8    t          t          j        | |          S r   )rN  r   r<  )r2  rF  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
  r1  r(  rS   r   )ri   r  rP  r  r  r  compiled_gms          r#   backend_accuracy_failsrT    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)rU  r   s     r#   _stride_or_defaultr[  "  s    
 '66U-Nu-U-UUr%   dCallable[[Optional[T]], T]c                      fdS )Nc                    | | nS r   r   )r"   r\  s    r#   rL  z_mk_defaulter.<locals>.<lambda>+  s    !-QQQ r%   r   )r\  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__6  s    


r%   Ndevice
dtype_hintstorage_hashOptional[str]nbytesintrh  ,Optional[torch._prims_common.DeviceLikeType]ri  Optional[torch.dtype]c               &    | xj         dz  c_         d S )Nr   re  )r=   rj  rl  rh  ri  s        r#   storagezNopInputReader.storage9  s     	

a



r%   r  r   r=  Optional[torch.Tensor]c                    d S r   r   r=   r  r=  s      r#   tensorzNopInputReader.tensorC      r%   Optional[int]c                    d S r   r   rt  s      r#   symintzNopInputReader.symintF  rv  r%   r,   r-   )
rj  rk  rl  rm  rh  rn  ri  ro  r,   r-   )r  r   r=  r   r,   rr  )r  r   r=  r   r,   rw  rY   rZ   r[   r?   rq  ru  ry  r   r%   r#   rc  rc  5  sx            @D,0             r%   rc  c                  P    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_dir
str | Noner~  tqdm | Noner,   r-   c                   |t                               d           |t          |          nd | _        g | _        || _        d S )Nz0no save_dir specified, will generate random data)rS   rT   r   storer  r~  )r=   r  r~  s      r#   r?   zInputReader.__init__M  sI     KKJKKK5=5I'111t
!				r%   rg  rj  rk  rl  rm  rh  rn  ri  ro  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| d           ||j        z  f}t          d |          }t          ||||                                          S )Nr   zdevice mismatch: %s != %szcould not load z , generating random data insteadr   )r~  update_device_or_default_dtype_or_defaultr  read_storagerh  rS   rT   r   r   itemsizer[  r   untyped_storage)r=   rj  rl  rh  ri  rq  r   rU  s           r#   rq  zInputReader.storageZ  s    9 IQ#F++&z22
:!l&>
*11,?? W^++KK ;VW^TTT  %    	RLRRRSSS:..0#D666E6:v>>NNPPPs   
B 
BB)storage_offsetr   r  is_leafrq  r   rW  rU  rV  r  rw  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   rh  r  )memory_format)r[  _storage_offset_or_defaultr  _is_leaf_or_default_requires_grad_or_defaultr   ru  rh  no_gradset_enable_gradclonepreserve_format_subclasses
meta_utilssafe_is_leaf_utilsset_tensor_metadatar  append)
r=   rq  r   rU  r  r   r  r  r  ts
             r#   ru  zInputReader.tensorw  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#   ry  zInputReader.symint  s    	
r%   r   )r  r  r~  r  r,   r-   )
rj  rk  rl  rm  rh  rn  ri  ro  r,   r   )rq  r   r   rW  rU  rV  r  rw  r   ro  r  r  r  r  r  r   r,   r  )r  r   r,   r   r{  r   r%   r#   r}  r}  L  s        %)BF     $ @D,0Q Q Q Q Q QB <@	 )-'+(,"&     B     r%   r}  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  rk  r  rJ   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%   rK   c                ~    dg}|                     d | j        D                        |                    d           |S )Nzdef load_args(reader):c              3      K   | ]	}d | V  
dS )rx   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_hintri  r  r   r  rn  ri  ro  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(, r{   )r   r  r>  r~   r  r  rh  rn   r  rl  r  write_storager  r  )r=   r  r  ri  wsvmaybe_dtype_hintr   rh  rl  rj  s              r#   rq  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)ri  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  rq  r  r   rh  r[  r   rU  r  r+   tupler  r  r  r   r  get_tensor_metadatar  r   r  r  r  r  r  r  r  rB   )	r=   r   r  r  r  rq  r  tensor_metadatar  s	            r#   ru  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  rn   r  r   r  	enumerater   r
   ru  rm  SymIntry  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  rB  hintr  r  )r=   r   r  s      r#   ry  zInputWriter.symint  sL    c5<(( 	 (-C>C>>>>?????r%   )r  rk  r  rJ   r,   r-   )r,   rK   )r  r   r  rn  ri  ro  r,   r+   )r   r+   r  r  r,   r-   )r   r+   r  r   r,   r-   )r   r+   r,   r-   )r   r+   r  r   r,   r-   )
rY   rZ   r[   r?   r  rq  ru  r  r  ry  r   r%   r#   r  r    s        GL 
; 
; 
; 
; 
; 
;   & EI,0           D
 
 
 
@& & & &$
 
 
 
@ @ @ @ @ @r%   r  r   func&Callable[[list[Tensor]], list[Tensor]]rh  
sym_shapesOptional[dict[str, int]]default_sym_shaperw  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><  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)rY   rZ   r[   __doc__r   r%   r#   TensorContainerr  I  s        ----r%   r  ry  r+   r,   rm  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   )ry  s   r#   rL  z=aot_graph_input_parser.<locals>.get_sym_int.<locals>.<lambda>T  s    vZZZ r%   )r   _checkr>  )ry  r  sym_shapes_dicts   `r#   get_sym_intz+aot_graph_input_parser.<locals>.get_sym_intQ  sP    o%F):$)FZZZZ	
 	
 	
 ""6+<===r%   r   rW  r   rE  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   rh  )
r  stripr  rm  r   r   randnzerosr   mark_dynamic)r   r   resolved_shapedynamic_dimsr   dimr   constructorr  r\  rh  r  s             r#   
gen_tensorz*aot_graph_input_parser.<locals>.gen_tensorX  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=   )ry  r+   r,   rm  )r   rW  r   rE  r,   r
   )torch.utils._dtype_abbrsr  r   rB   valuesinspect	getsource__annotations__researchgroupsr  r5   group	signaturer   finditersetattr)r  rh  r  r  r  	dtype_mapdtype_patternsourcetensor_assignment_regextensor_regexsym_shape_regexr  r=  r  r   r   
annotationmatch	data_type	shape_strr   r   	container	attr_namers   r  r  s    ` `                     @@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   r=  r,   c                                                       	  | i |                                 S #                                  w xY wr   )enabledisable)r  r=  r  profs     r#   wrapperz3profile_to_file.<locals>.decorator.<locals>.wrapper  sH    KKMMMr4*6**s	   3 A	)r  r   r=  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stderrrQ   rD   rE   )r*   r  s   r#   save_itz profile_to_file.<locals>.save_it  sn    !!!
O"*  '   		
 		
 		
 		
 		
r%   )r  r   r,   r   rz  )cProfileProfiler3   r4   r6   
expanduseratexitregister)r*   r  r  r  s   `  @r#   profile_to_filer#    s     Dwrw11(;;<<H	 	 	 	 	 	
 
 
 
 
 
 
 OGr%   rX   )r   rJ   r,   r+   )r   r+   r,   r-   )r  r  r,   r  )FF)
ri   rj   r  r  r  rJ   r  rJ   r,   r   )F)ri   rj   r   rj   r  r  r  rJ   r  rJ   r  rJ   r,   rJ   )r2  rj   r,   rj   )r   rE  r2  rj   rF  r  r,   rG  )r2  rj   rF  r  r,   rG  )ri   rj   r  r  rP  rQ  r  rJ   r  rJ   r  rJ   r,   rJ   )rU  rV  r   rW  r,   rX  )r\  r   r,   r]  )r   NN)
r  r  rh  r+   r  r  r  rw  r,   r  )r*   r+   r,   r  )jr  
__future__r   r!  r  r  r  ra   r  r  loggingr3   r  r   r  r_   rD   collectionsr   	importlibr   typingr   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   r   collections.abcr   r   	torch.hubr   torch.storager   	getLoggerrY   rS   r   inductor_config	is_fbcoder   libfb.py.build_infolibfbrC   extra_importsrF   py
build_info	BuildInfoget_build_ruler9   rB   rR   r)   re   r   rh   cacher   r   r   r   r   r(  r   r
  r  r1  rD  rN  r'  rT  r[  r`  float32r  rh  r  r  r  r  rc  r}  r  r  r#  r   r%   r#   <module>r?     sO   $ # " " " " "               				 				     



         # # # # # # 8 8 8 8 8 8 8 8 8 8 8 8  # # # # # # # # # #       . . . . . . @ @ @ @ @ @ . . . . . . ; ; ; ; ; ; M M M M M M M M       9 9 9 9 9 9 9 9 9 9  -22222222,,,,,, 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    6 7< 	 	 	 	 	 	2 5:      (@ @ @ @V V V V	 	 	 	 	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**        .N N N N N N N Nzy@ y@ y@ y@ y@ y@ y@ y@| +/'+	d d d d dN# # # # # #r%   