
    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
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CallableOptionalTYPE_CHECKINGTypeVar)Tensor)rand_strided)normalize_path_separator)is_float_dtype)StorageWeakRef)ContentStoreReaderContentStoreWriter   )config)clone_inputsget_debug_dir)Sequence)tqdm)UntypedStorageTztorch._inductor.config )z1//caffe2/torch/fb/sparsenn:sparsenn_operators_gpuz-//caffe2/torch/fb/sparsenn:sparsenn_operatorsz///deeplearning/fbgemm/fbgemm_gpu:sparse_ops_cpuz+//deeplearning/fbgemm/fbgemm_gpu:sparse_opszfbcode://
ztorch.ops.load_library("z"))buck2runz@mode/dev-nosanc                  &    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      S/var/www/html/engine/venv/lib/python3.12/site-packages/torch/_dynamo/debug_utils.py__init__zBuckTargetWriter.__init__Z   s    $&GGMM"''//(2K$L!T\ll**5"5 {{**345Qt{{mD	IIdiinnY79:	IIabM	 kk#((9%'(,!#a}5    c                    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.buildh   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    Y   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                 2   t         j                  j                         syd} 	 t        j                  ddg      }|j                         j                  d      }dj                  |D cg c]  }|dvsd| d	 c}      }| | 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   )r   # z 
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
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     s-   ::""$L!I*%22FK3HI-446<<TB''0BT1atmRs#;TUy^#	  /4UZZ5L5L5N/O I **I ( .er$s5'--	.I Uz<<= *))	*s*   AC5 (	C02C0:C5 0C5 5DDF)stable_outputc           	         | ryg dg ddfd}t         j                  j                         D cg c]  \  }} ||      r	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{   r7   r   )r   r   r   valueconfig_linesconfig_stringr   r   s         @@r3   generate_env_vars_stringr     s     <:JUIT
 ****,C#; se6%*L 
 IIl+M# ) % 	 	s   A4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   4  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   H  s    77<<(>??r5   c                L   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 {minified_repro_path})
r   rF   rG   use_buckr    rD   rC   OSError	exceptionNotImplementedError)contentsminified_repro_pathrJ   es       r3   helper_for_dump_minifyr   L  s    13KK02EF,-335U%s+ 	rHHX	 	 	  Ub!"LMSTTU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   _  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  l  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                  k(  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_fp64r7    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._pytreer9  r;  r   r.  r7  )r   r4  inputsr9  s   `   r3   cast_tor?    sM     -HHUOE (.	 		F &=r5   c                8    t        t        j                  | |      S r   )r?  r   r.  )r4  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_failsrD    st    !MM">~N
 #%'
 
 	
   	#	

 s   69 AAc               4    | | S t        j                  |      S r   )r  make_contiguous_strides_for)strider   s     r3   _stride_or_defaultrH    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   )rK  s   `r3   _mk_defaulterrL  &  s	    ..r5   cpuc                  F    e Zd ZddZddd	 	 	 	 	 	 	 	 	 ddZd	dZd
dZy)NopInputReaderc                    d| _         y )Nr   total)r0   s    r3   r4   zNopInputReader.__init__2  s	    
r5   Ndevice
dtype_hintc               .    | xj                   dz  c_         y )Nr   rQ  )r0   storage_hashnbytesrT  rU  s        r3   storagezNopInputReader.storage5  s     	

a
r5   c                     y r   rU   r0   r
  r/  s      r3   tensorzNopInputReader.tensor?      r5   c                     y r   rU   r[  s      r3   symintzNopInputReader.symintB  r]  r5   rM   rN   )
rW  Optional[str]rX  intrT  ,Optional[torch._prims_common.DeviceLikeType]rU  Optional[torch.dtype]rM   rN   )r
  r   r/  r   rM   zOptional[torch.Tensor])r
  r   r/  r   rM   Optional[int]rR   rS   rT   r4   rY  r\  r_  rU   r5   r3   rO  rO  1  sP     @D,0# 
 = * 
r5   rO  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y)InputReaderN)pbarc               v    |t         j                  d       |t        |      nd | _        g | _        || _        y )Nz0no save_dir specified, will generate random data)rF   rG   r   storer
  ri  )r0   save_dirri  s      r3   r4   zInputReader.__init__I  s9    
 KKJK5=5I'1t
!		r5   rS  c                  | j                   | j                   j                  d       t        |      }t        |      }| j                  P|N	 | j                  j                  |      }||j                  k7  r!t        j                  d||j                         |S t        j                  d|       ||j                  z  f}t        d |      }t        ||||      j                         S # t        $ r Y Zw xY w)Nr   zdevice mismatch: %s != %sz1could not load %s, generating random data insteadr   )ri  update_device_or_default_dtype_or_defaultrk  read_storagerT  rF   rG   r   itemsizerH  r   untyped_storage)r0   rW  rX  rT  rU  rY  r   rG  s           r3   rY  zInputReader.storageT  s     99 IIQ#F+&z2
::!l&>
**11,? W^^+KK ;VW^^T GV:...0#D6E6:v>NNPP % s   C) )	C54C5)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)Nrn  )r   rT  r   )memory_format)rH  _storage_offset_or_defaultrq  _is_leaf_or_default_requires_grad_or_defaultr   r\  rT  no_gradset_enable_gradclonepreserve_format_subclasses
meta_utilssafe_is_leaf_utilsset_tensor_metadatar
  append)
r0   rY  r   rG  ru  r   r   rv  metadatats
             r3   r\  zInputReader.tensorq  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   r_  zInputReader.symint  s    		
r5   r   )rl  ra  ri  zOptional[tqdm])
rW  ra  rX  rb  rT  rc  rU  rd  rM   r   )rY  r   r   torch._prims_common.ShapeTyperG  (Optional[torch._prims_common.StrideType]ru  re  r   rd  r   Optional[bool]rv  r  r  r   rM   torch.Tensor)r  r   rM   r   rf  rU   r5   r3   rh  rh  H  s    	QU 	  @D,0Q#Q Q
 =Q *Q 
QB <@	 )-'+(,"& - 9	 & % &    
Br5   rh  c                  `    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	y)InputWriterFstable_hashc                   g | _         t        j                         | _        || _        |t        ||      nd | _        i | _        y )Nr  )_lines	itertoolsr   storage_counterrl  r   rk  seen_storages)r0   rl  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_hintrU  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  r0  rw   r  rq  rT  rf   rp  rX  rk  write_storager  r  )r0   rt  r  rU  wsvmaybe_dtype_hintr   rT  rX  rW  s              r3   rY  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)rU  r  rn  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  rY  rt  r   rT  rH  r   rG  r  rL   tuplerq  ry  ru  r   r  get_tensor_metadatar  r{   r{  r   r  r  r  rz  r  r7   )	r0   r   r  r  r  rY  r
  tensor_metadatarv  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 )Nr   z# was unsupported type for dumping: z"""[])r  r  rf   r   r   r  	enumerater   r   r\  rb  SymIntr_  unsupported)r0   r   argr   aname_is         r3   r  zInputWriter.unsupported  s    Rv%HcTU 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  )r0   r   s     r3   constzInputWriter.const  s'    D85.OP	
r5   c                    t        |t        j                        r|j                  j                  }| j
                  j                  d|d|        y )Nzreader.symint(r  )r   r   r  r5  hintr  r  )r0   r   r  s      r3   r_  zInputWriter.symint  s<    c5<<(((--C^C7%v>?r5   )rl  ra  r  rP   rM   rN   )rM   rQ   )rt  r   r  rc  rU  rd  rM   rL   )r   rL   r  r  rM   rN   )r   rL   r  r   rM   rN   )r   rL   rM   rN   )r   rL   r  r   rM   rN   )
rR   rS   rT   r4   r  rY  r\  r  r  r_  rU   r5   r3   r  r    s\    GL 
;& EI,0 '  B	 
 *  
 D
@&$
@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  C  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   )r_  s   r3   r<  z=aot_graph_input_parser.<locals>.get_sym_int.<locals>.<lambda>N  s    vhYZ r5   )r   _checkr0  )r_  default_sym_shapesym_shapes_dicts   `r3   get_sym_intz+aot_graph_input_parser.<locals>.get_sym_intK  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   rT  )
r  stripr  rb  r   r   randnzerosr   mark_dynamic)r   r   resolved_shapedynamic_dimsr   dimr   constructorr  rK  rT  r  s             r3   
gen_tensorz*aot_graph_input_parser.<locals>.gen_tensorR  s    & 	4FAs))+Ccz$%%a(##A&"))#c(3	4 &+%<%<ekk%++.fE 	/AMM&&sA.	/
r5   ,r   r0   )r_  rL   rM   rb  )r   r  r   torch.dtyperM   r   )torch.utils._dtype_abbrsr  r{   r7   valuesinspect	getsource__annotations__researchgroupsr  r(   group	signaturerx   finditersetattr)funcrT  
sym_shapesr  r  r   r   	dtype_mapdtype_patternsourcetensor_assignment_regextensor_regexsym_shape_regexr  r/  r  r   r   
annotationmatch	data_type	shape_strr   r   	container	attr_namerl   r  r  s    ` `                       @@r3   aot_graph_input_parserr    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 MW)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
  r/  fnprofs     r3   wrapperz3profile_to_file.<locals>.decorator.<locals>.wrapper  s1    KKM4*6*s   + =)r
  r   r/  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   r`  )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   )r4  r   rM   r   )r   r  r4  r   r>  r  rM   &tuple[torch.fx.GraphModule, list[Any]])r4  r   r>  r  rM   r  )rj   r   r   r
  rB  zACallable[[torch.fx.GraphModule, list[Any]], torch.fx.GraphModule]r  rP   r  rP   r  rP   rM   rP   )rG  r  r   r  rM   ztorch._prims_common.StrideType)rK  r   rM   zCallable[[Optional[T]], T])r   NN)
r  z&Callable[[list[Tensor]], list[Tensor]]rT  rL   r  zOptional[dict[str, int]]r  re  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	   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   	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$  r7  r?  r  rD  rH  rL  float32rq  rT  rp  ry  r{  rz  rO  rh  r  r  r	  )r<   s   0r3   <module>r(     s  $ #         	 	  
    # B B  # #  . @ . ; M  . (, g!CL   89$$& 

J $$..==?GG	SWXJIITA!9!B?TUM 64 4n  c cL  0 7< 	2 5: (@U	I 	
  	00
0 0 	0
 	0H 	7 77 7 "7 	7 7 7 
7t*3=F+(11)21+1 	 ! S 	   
RV4V )V $	V/ "%--0 "<5<<#67 *1- )%0 #E*  .L Lvy@ y@| +/'+	b
0bb )b %	b
 bJ#c Us   J