
    קgwl                     .   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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%m&Z& d
dl'm(Z( d
dlm)Z)m*Z*  ej+        e,          Z- ed          Z. ed          Z/e/0                                Z1e1rd dl2Z3g Z4dZ5e1rZg dZ4e3j6        j7        j8        9                                :                    dd          Z;d<                    d e4D                       Z5g dZ= G d d          Z>d Z?dZ@ G d d          ZA ejB        d          d             ZCdddZDd  ZEd! ZF G d" d#eG          ZHd$ ZIdCd%ZJ	 dDddd&d'ZKd( ZLd) ZMd* ZN	 dDddd&d+ZOd,ed-         d.d/d0d-fd1ZPd2e.d0eee.         ge.f         fd3ZQ eQejR                  ZS eQ ejT        d4                    ZU eQd           ZV eQd          ZW eQd          ZX G d5 d6          ZY G d7 d8          ZZ G d9 d:          Z[	 	 	 dEd<eee         gee         f         d=e\d>eee\e]f                  d?ee]         d0ee\ef         f
d@Z^dAe\d0ee.ge.f         fdBZ_dS )F    N)Counter)import_module)AnyCallableDictListOptionalTypeVar)Tensor)rand_strided)is_float_dtype)StorageWeakRef)ContentStoreReaderContentStoreWriter   )config)clone_inputsget_debug_dirTztorch._inductor.config )z1//caffe2/torch/fb/sparsenn:sparsenn_operators_gpuz-//caffe2/torch/fb/sparsenn:sparsenn_operatorsz///deeplearning/fbgemm/fbgemm_gpu:sparse_ops_cpuz+//deeplearning/fbgemm/fbgemm_gpu:sparse_opszfbcode://
c                     g | ]}d | d	S )ztorch.ops.load_library("z") .0xs     U/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/_dynamo/debug_utils.py
<listcomp>r   8   s$    TTTA?!???TTT    )buck2runz@mode/dev-nosanc                   "    e Zd Zd Zd ZddZdS )BuckTargetWriterc                 (   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)selffilename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>N   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/functorch:functorch",
        "//triton:triton",
        "z",
    ],
    cpp_deps = [
z
    ],
    main_module = "z",
    par_style = "xar",
)
)join
extra_depstextwrapdedentr1   r/   
cur_targetr+   )r4   extra_cpp_depss     r   buildzBuckTargetWriter.buildM   s    #J#Jz#J#J#JKK ;	 
 l       I!  
 
 	
r    Tc                 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 )r*   r+   r:   r.   openwriter@   BUCK_CMD_PREFIXr3   logwarning)r4   	print_msgtarget_filefd	cmd_splits        r   rF   zBuckTargetWriter.writef   s    gll4;	::+s## 	#rHHTZZ\\"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# $t'9&::	 	KKX##   s   (A**A.1A.N)T)__name__
__module____qualname__r7   r@   rF   r   r    r   r$   r$   >   sF        6 6 6
 
 
2     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)
r*   r+   r:   r   tempfile
gettempdirgetpassgetuserexistsmakedirs)r+   s    r   minifier_dirrZ   t   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             Zed             ZdS )NNModuleToStringc                    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typer]   
safe_reprsaddlenrH   rI   )gm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    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))r[   )torch.nn.modules.modulerk   r<   r=   r`   __repr__next
parametersis_cuda_buffersitemsnumelMAX_CONSTANT_NUMEL_INLINEtorch._tensor_strrn   	thresholdreprtorchis_floating_pointlistshapedtype_parameterscode)re   rk   tab	model_strmodule_namerh   
module_strexample_parambuffer_namebufferrn   
tensor_str
param_nameparammaybe_devices                  r   convertzNNModuleToString.convert   s   666666O
 
	 $&#4#4#6#6 	G 	GK"OO--/J !!2!2!4!4d;;M(]-B( *333
CEFFFF
FFFFII#%;#4#4#6#6 	Y 	Y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
CEXXXXXXXXII!#!5!5!7!7 	F 	FJ}L} 10s4;L;LssV[VascosssJCEEE
EEzEEEEII 	

27A..2222	r    N)rN   rO   rP   r|   nnLinearConv1dConv2dConv3dBatchNorm1dBatchNorm2dBatchNorm3d	LayerNormDropoutSoftmaxReLUGELUIdentity	MaxPool2d	EmbeddingTanhConvTranspose1dGLULSTMFlattenAdaptiveAvgPool2drb   staticmethodri   r   r   r    r   r]   r]      s         "+J0 	 	 \	 ; ; \; ; ;r    r]   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decoder,   r:   FileNotFoundErrorCalledProcessErrorr   rangedevice_countrv   )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_outputc           	      &   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      s    """"!!!! <;;(/7FFHH	
 $$&&	 	 &&((	 	 ''))	 	 	 	 	 	r    c                  Z    t           j                            t                      d          S )Nzminifier_launcher.py)r*   r+   r:   rZ   r   r    r   get_minifier_repro_pathr     s    7<<(>???r    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:
%srC   r   z(Could not write to {minified_repro_path})
r   rH   rI   use_buckr$   rF   rE   OSError	exceptionNotImplementedError)contentsminified_repro_pathrL   es       r   helper_for_dump_minifyr     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)rN   rO   rP   r   r    r   r   r   $  s        Dr    r   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   rd   
isinstancer|   r   requires_grad_requires_grad)example_inputs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    c                 b   ddl m}m}m} t	          j        |           } |st          |          }t          | d          r|                     d           t          | 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_callN)
testingr   r   r   copydeepcopyr   hasattrr   backward)	re   argsonly_fwddisable_cloner   r   r   outlosss	            r   run_fwd_maybe_bwdr   5  s     SRRRRRRRRR	r		B 5.t44r; 
T b-00
?""T(((bb$iC 
 $$S))?2sD$///r    require_fp64ignore_non_fpc                   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          |||          }n2# t          $ r%}t                              d           Y d}~dS d}~w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RuntimeErrorrH   rI   r   repro_tolerance)re   opt_gmr   r   r   r   r   reffp64_ref
fp64_modelfp64_examplesresr   passings                 r   same_two_modelsr  P  sg   " 
B
9
9CH& ;
	;(4b!!#B>#R#R) )%J )]HMMHH 	; 	; 	; "|   KK9:::::	;
AA    	$	
 	
 	

 ttttt d"#  G Ns*   AA+ +5B#"B#'B9 9
C(C##C(c                 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_functionrm   r   r   r   )graphnodesopr1   r|   opsprimsconvert_element_typedefaultrd   r   r   float64kwargsgetdictlint	recompile)model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    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._pytreer  r  r|   r  r  )r   r  inputsr  s   `   r   cast_tor#    sv    ,,,,,,HHUOOE (..X	 	 	 	 		 F &=r    c                 8    t          t          j        | |          S r   )r#  r|   r  )r  r"  s     r   r   r     s    5=%000r    c                    	  |t          j        |           t          |                    }t          | |||||           S # t          $ r%}t
                              d           Y d }~dS d }~w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   rH   r   )re   r   compiler_fnr   r   r   compiled_gmr   s           r   backend_accuracy_failsr(    s    !kM">~NN
 
 #%'
 
 
 
 	
     	#	
 	
 	

 uuuuus   A A 
A2A--A2strideztorch._prims_common.StrideTyper   ztorch._prims_common.ShapeTypereturnc                2    | | nt          j        |          S r   )r   make_contiguous_strides_for)r)  r   s     r   _stride_or_defaultr-    s    
 '66U-Nu-U-UUr    dc                       fdS )Nc                     | | nS r   r   )r   r.  s    r   r   z_mk_defaulter.<locals>.<lambda>  s    !-QQQ r    r   )r.  s   `r   _mk_defaulterr1    s    .....r    cpuc                   0    e Zd ZddZddddZd Zd ZdS )	NopInputReaderr*  Nc                     d| _         d S )Nr   total)r4   s    r   r7   zNopInputReader.__init__  s    


r    device
dtype_hintc                &    | xj         dz  c_         d S )Nr   r6  )r4   storage_hashnbytesr9  r:  s        r   storagezNopInputReader.storage  s    

a



r    c                     d S r   r   r4   r   r  s      r   tensorzNopInputReader.tensor      r    c                     d S r   r   r@  s      r   symintzNopInputReader.symint  rB  r    r*  NrN   rO   rP   r7   r>  rA  rD  r   r    r   r4  r4    sf            7;t           r    r4  c                   F    e Zd Zd	dddZddddZ	 d	ddddddZd ZdS )
InputReaderN)pbarc                    |t                               d           |t          |          nd | _        g | _        || _        d S )Nz0no save_dir specified, will generate random data)rH   rI   r   storer   rI  )r4   save_dirrI  s      r   r7   zInputReader.__init__  sI    
 KKJKKK5=5I'111t
				r    r8  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   )rI  update_device_or_default_dtype_or_defaultrK  read_storager9  rH   rI   r   itemsizer-  r   untyped_storage)r4   r<  r=  r9  r:  r>  r   r)  s           r   r>  zInputReader.storage  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_leafc                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 )NrN  )r   r9  r   )memory_format)r-  _storage_offset_or_defaultrQ  _is_leaf_or_default_requires_grad_or_defaultr|   rA  r9  no_gradset_enable_gradclonepreserve_format_subclasses
meta_utilssafe_is_leaf_utilsset_tensor_metadatar   append)
r4   r>  r   r)  rU  r   r   rV  metadatats
             r   rA  zInputReader.tensor#  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c                 :    | j                             |           |S r   )r   rf  )r4   vals     r   rD  zInputReader.symintD  s    	
r    r   rF  r   r    r   rH  rH    s        	d 	 	 	 	 	 7;t Q Q Q Q Q4 	     B    r    rH  c                   D    e Zd ZdddZd ZddddefdZdd	Zdd
ZdS )InputWriterFstable_hashc                    g | _         t          j                    | _        || _        |t          ||          nd | _        i | _        d S )Nrm  )_lines	itertoolsr   storage_counterrL  r   rK  seen_storages)r4   rL  rn  s      r   r7   zInputWriter.__init__V  sW    (00  # x[AAAA 	

  r    c                 ~    dg}|                     d | j        D                        |                    d           |S )Nzdef load_args(reader):c              3       K   | ]	}d | V  
dS )rl   Nr   )r   ls     r   r   z$InputWriter.lines.<locals>.<genexpr>f  s(      11111111r    zload_args._version = 0)extendrp  rf  )r4   rs     r   lineszInputWriter.linesb  sK    $
 	
11T[111111 	
)***r    Nr:  device_hintr*  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(, ro   )r   rs  r  rr   rr  rQ  r9  ra   rP  r=  rK  write_storagerp  rf  )r4   rT  r:  r{  wsvmaybe_dtype_hintr   r9  r=  r<  s              r   r>  zInputWriter.storageq  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    c                    ddl m} |                     |                                |j        |j                  }g }t          d |j                  |                                k    rA|	                    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                            |          }t3          d           |k    r|	                    d	|           | j        	                    d
d                    |t          t          |j                            g|          z   d| z              d S )Nr   )statically_known_truerz  rN  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>  rT  r   r9  r-  r   r)  rf  strtuplerQ  rY  rU  r|   rd  get_tensor_metadatarw  rv   r[  r   ra  rb  rc  rZ  rp  r:   )r4   r   rh  r  r>  r   tensor_metadatarV  s           r   rA  zInputWriter.tensor  sJ   OOOOOO,,AG  
 
 d!'222ahhjj@@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    c                     t          |t          j                  r|j        j        }| j                            d|d|            d S )Nzreader.symint(r  )r   r|   SymIntr  hintrp  rf  )r4   r   rj  s      r   rD  zInputWriter.symint  sL    c5<(( 	 (-C>C>>>>?????r    rE  )	rN   rO   rP   r7   ry  r  r>  rA  rD  r   r    r   rl  rl  U  s        05 
  
  
  
  
    6:t   PS    8
 
 
 
>@ @ @ @ @ @r    rl  r   funcr9  
sym_shapesdefault_sym_shapec           	         ddl m} d |                                D             }d                    |                                          }t          j        |           }d| d}d| d}	d	}
 G d
 d          }i }pi fddt          ffd}| 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   keyvalues      r   
<dictcomp>z*aot_graph_input_parser.<locals>.<dictcomp>  s    BBB
UBBBr    |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)rN   rO   rP   __doc__r   r    r   TensorContainerr    s        ----r    r  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   )rD  s   r   r   z=aot_graph_input_parser.<locals>.get_sym_int.<locals>.<lambda>  s    vZZZ r    )r|   _checkr  )rD  r  r  s   `r   get_sym_intz+aot_graph_input_parser.<locals>.get_sym_int  sN    j A$5T$AZZZZ	
 	
 	
 ~~f&7888r    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   r9  )
	enumeratestriprf  intr}   r|   randnzerosr   mark_dynamic)r   r   resolved_shapedynamic_dimsr   dimr   constructorr   r.  r9  r  s             r   
gen_tensorz*aot_graph_input_parser.<locals>.gen_tensor  s    && 	4 	4FAs))++CczzK$$%%a(((##A&&&& 4"))#c((333%*%<Mekk%+k.fEEE 	/ 	/AM&&sA....
r    ,r   r4   )torch.fx.graphr  rv   r:   valuesinspect	getsourcer   __annotations__researchgroupsr  r,   group	signaturers   finditersetattr)r  r9  r  r  r  	dtype_mapdtype_patternsourcetensor_assignment_regextensor_regexsym_shape_regexr  r  r  annotationsr   
annotationmatch	data_type	shape_strr   r   	container	attr_namerg   r  s    ```                     @r   aot_graph_input_parserr    s   * +*****BBk.?.?.A.ABBBIHH[//1122M t$$F |M{{{7777L&O. . . . . . . .  F!rJ9 9 9 9 9 9F       * &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    r5   c                      t          j                    t          j                            t          j                                                  f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                 J     t          j                    fd            }|S )Nc                                                        	  | i |                                 S #                                  w xY wr   )enabledisable)r   r  fnprofs     r   wrapperz3profile_to_file.<locals>.decorator.<locals>.wrapper   sH    KKMMMr4*6**s	   3 A	)	functoolswraps)r  r  r  s   ` r   	decoratorz"profile_to_file.<locals>.decorator  s>    				 	 	 	 	 
		 r    c            	                                           t          j                            t	          j        d  d  d                     d S )Nz!                Wrote profile to z+, view with:

                    snakeviz z

                )
dump_statssysstderrrF   r<   r=   )r5   r  s   r   save_itz profile_to_file.<locals>.save_it*  sn    !!!
O"*  '   		
 		
 		
 		
 		
r    )cProfileProfiler*   r+   r-   
expanduseratexitregister)r5   r  r  r  s   `  @r   profile_to_filer    s     Dwrw11(;;<<H	 	 	 	 	
 
 
 
 
 
 OGr    )FF)F)r   NN)`r  r   r  r  rV   r  rq  loggingr*   r  r   r  rT   r<   collectionsr   	importlibr   typingr   r   r   r   r	   r
   r|   torch._prims_common_prims_commonr   torch._subclasses.meta_utilsr   torch._dynamo.testingr   r    torch.multiprocessing.reductionsr   torch.utils._content_storer   r   r   r   r   r   	getLoggerrN   rH   r   inductor_config	is_fbcoder   libfb.py.build_infolibfbr;   extra_importspy
build_info	BuildInfoget_build_ruler0   r>   r:   rG   r$   rZ   rx   r]   	lru_cacher   r   r   r   r   r   r   r   r  r  r#  r   r(  r-  r1  float32rQ  r9  rP  rY  r[  rZ  r4  rH  rl  r  r  r  r  r   r    r   <module>r     s:                 				 				     



         # # # # # # ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?  # # # # # # # # # #       . . . . . . . . . . . . ; ; ; ; ; ; 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3 3 3 3 3 3 3 3l    a a a a a a a aH T  0 -2     (@ @ @U U U	 	 	 	 	I 	 	 	
 
 
0 0 0 0> 	7 7 7 7 7 7t  *  $1 1 1 	     RV56V +V &	V V V V/Q /8Xa[M1$45 / / / / "M%-00 "]<5<#6#677 *]1-- )M%00 #mE**         E E E E E E E EhZ@ Z@ Z@ Z@ Z@ Z@ Z@ Z@~ +/'+	` `
DL>4</
0`` c3h(`  }	`
 
#s(^` ` ` `F#c #hsAv&6 # # # # # #r    