
    ɯwgwl                        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jV                  e,      Z- ed      Z. ed      Z/e/ja                         Z1e1rd dl2Z3g Z4dZ5e1reg dZ4e3jl                  jn                  jp                  js                         ju                  dd      Z;djy                  e4D  cg c]  } d|  d
 c}       Z5g dZ= G d d      Z>d Z?dZ@ G d d      ZA ej                  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/d0d1d.fd2ZPd3e.d1eee.   ge.f   fd4ZQ eQej                        ZS eQ ej                  d5            ZU eQd       ZV eQd      ZW eQd      ZX G d6 d7      ZY G d8 d9      ZZ G d: d;      Z[	 	 	 dEd<eee   gee   f   d=e\d>eee\e]f      d?ee]   d1ee\ef   f
d@Z^dAe\d1ee.ge.f   fdBZ_yc c} w )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://
ztorch.ops.load_library("z"))buck2runz@mode/dev-nosanc                        e Zd Zd Zd Z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      ^/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/torch/_dynamo/debug_utils.py__init__zBuckTargetWriter.__init__?   s    $&GGMM"''//(2K$L!T\ll**5"5 {{**345Qt{{mD	IIdiinnY79:	IIabM	 kk#((9%'(,!#a}5    c                     dj                  t        D cg c]  }d| d
 c}      }t        j                  d| j                   d| j
                   dt         d| d| j                   d	      S c c}w )
Nr   z	        "z",za
load("@fbcode_macros//build_defs:python_binary.bzl", "python_binary")

python_binary(
    name="z",
    srcs = ["z"],
    compile = False,
    deps = [
        "//caffe2:torch",
        "//caffe2/functorch:functorch",
        "//triton:triton",
        "z",
    ],
    cpp_deps = [
z
    ],
    main_module = "z",
    par_style = "xar",
)
)join
extra_depstextwrapdedentr)   r'   
cur_targetr#   )r,   xextra_cpp_depss      r/   buildzBuckTargetWriter.buildM   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#   r3   r&   openwriter:   BUCK_CMD_PREFIXr+   logwarning)r,   	print_msgtarget_filefd	cmd_splits        r/   r@   zBuckTargetWriter.writef   s    ggll4;;	:+s# 	#rHHTZZ\"	# $t'9'9&::	KKX# 	# 	#s    BB%N)T)__name__
__module____qualname__r0   r:   r@    r1   r/   r   r   >   s    6
2r1   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#   r3   r   tempfile
gettempdirgetpassgetuserexistsmakedirs)r#   s    r/   minifier_dirrU   t   sb    77<<4D|%%'(
7??3D2EF77>>$
D4(Kr1      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        Ze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typerX   
safe_reprsaddlenrB   rC   )gmcant_convert_modules       r/   can_convert_to_stringz&NNModuleToString.can_convert_to_string   sg    u**, 	)IAvF|#3#>#>>  (	) |q KKGVr1   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))rV   )torch.nn.modules.modulerf   r5   r6   r[   __repr__next
parametersis_cuda_buffersitemsnumelMAX_CONSTANT_NUMEL_INLINEtorch._tensor_strri   	thresholdreprtorchis_floating_pointlistshapedtype_parameterscode)r`   rf   tab	model_strmodule_namerc   
module_strexample_parambuffer_namebufferri   
tensor_str
param_nameparammaybe_devices                  r/   convertzNNModuleToString.convert   s>   6OO
	 $&#4#4#6 	GK"OO-.J !!2!2!4d;M(]-B-B *|73
CE7%}C
|2FFI	G $&;;#4#4#6 	YK~||~!::8!++/HHHH!&\
((0+D,>+?x~UVW
 -T&,,-?,@VWX  ~~ *|73
CE7"8STWXXI#	Y& "$!5!5!7 	FJ}L}}0:4;L:MXV[VaVaUbcobpprsJCE7%
|3zl"EEI	F  	
277A./r22	r1   N)rH   rI   rJ   rw   nnLinearConv1dConv2dConv3dBatchNorm1dBatchNorm2dBatchNorm3d	LayerNormDropoutSoftmaxReLUGELUIdentity	MaxPool2d	EmbeddingTanhConvTranspose1dGLULSTMFlattenAdaptiveAvgPool2dr]   staticmethodrd   r   rK   r1   r/   rX   rX      s0     ""+J0 	 	 ; ;r1   rX   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 
z# nvcc not found
c              3   Z   K   | ]#  }t         j                  j                  |       % y wN)rw   cudaget_device_name).0is     r/   	<genexpr>z,_cuda_system_info_comment.<locals>.<genexpr>   s$      *+

""1%s   )+z# GPU Hardware Info: 
z : )rw   r   is_available
subprocesscheck_outputdecoder$   r3   FileNotFoundErrorCalledProcessErrorr   rangedevice_countrq   )r   cuda_version_outcuda_version_linesscomment	gpu_namesnamecounts           r/   _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           	      ^   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   rw   experimental_configs      r/   generate_config_stringr      s    "!;((//77FFH
 $$& ' (&&( ) *'') * +  	 	r1   c                  R    t         j                  j                  t               d      S )Nzminifier_launcher.py)r"   r#   r3   rU   rK   r1   r/   get_minifier_repro_pathr     s    77<<(>??r1   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:
%sr=   r   z(Could not write to {minified_repro_path})
r   rB   rC   use_buckr   r@   r?   OSError	exceptionNotImplementedError)contentsminified_repro_pathrF   es       r/   helper_for_dump_minifyr     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)rH   rI   rJ   rK   r1   r/   r   r   $  s    r1   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   r_   
isinstancerw   r   requires_grad_requires_grad)example_inputscloned_inputsidxs      r/   clone_inputs_retaining_gradnessr   (  sb     !0MS() QmC(%,,7#--nS.A.O.OPQ r1   c                 $   ddl m}m}m} t	        j
                  |       } |st        |      }t        | d      r| j                  d       t        | 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_callN)
testingr   r   r   copydeepcopyr   hasattrr   backward)	r`   argsonly_fwddisable_cloner   r   r   outlosss	            r/   run_fwd_maybe_bwdr   5  s     SR	r	B.t4r;
T b-0"T(b$iC
$S)2sD$//r1   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 d}~yd}~w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RuntimeErrorrB   rC   r   repro_tolerance)r`   opt_gmr   r   r   r   r   reffp64_ref
fp64_modelfp64_examplesresr   passings                 r/   same_two_modelsr  P  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$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_functionrh   r   r   r{   )graphnodesopr)   rw   opsprimsconvert_element_typedefaultr_   r   r   float64kwargsgetdictlint	recompile)modelnoder{   
new_kwargss       r/   cast_dtype_args_to_fp64r    s   !! )GG&uyyCCKKKtyy>Q&&&diil+		!0M!YYq\5==9	77o%KKOOG,E ^E%:!$++.
&+mm
7#() 
KK	OOLr1   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   rw   r   rx   to)r8   r{   s    r/   <lambda>zcast_to.<locals>.<lambda>  s3    a&1+>+>+@ $$u+  r1   )torch.utils._pytreer  r  rw   r  r  )r{   r  inputsr  s   `   r/   cast_tor    sK    ,HHUOE (.	 		F &=r1   c                 8    t        t        j                  | |      S r   )r  rw   r  )r  r  s     r/   r   r     s    5==%00r1   c                    	  |t        j                  |       t        |            }t        | |||||       S # t        $ r}t
        j                  d       Y d }~y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   rB   r   )r`   r   compiler_fnr   r   r   compiled_gmr   s           r/   backend_accuracy_failsr!    su    !MM">~N
 #%'
 
 	
   	#	

 s   69 	A!AA!strideztorch._prims_common.StrideTyperz   ztorch._prims_common.ShapeTypereturnc                4    | | S t        j                  |      S r   )r   make_contiguous_strides_for)r"  rz   s     r/   _stride_or_defaultr&    s    
 '6UU-N-Nu-UUr1   dc                       fdS )Nc                     | | S S r   rK   )r8   r'  s    r/   r  z_mk_defaulter.<locals>.<lambda>  s    !-Q Q r1   rK   )r'  s   `r/   _mk_defaulterr*    s	    ..r1   cpuc                   .    e Zd ZddZddddZd Zd Zy)NopInputReaderNc                     d| _         y )Nr   total)r,   s    r/   r0   zNopInputReader.__init__  s	    
r1   device
dtype_hintc                .    | xj                   dz  c_         y )Nr   r/  )r,   storage_hashnbytesr2  r3  s        r/   storagezNopInputReader.storage  s    

a
r1   c                      y r   rK   r,   r   r  s      r/   tensorzNopInputReader.tensor      r1   c                      y r   rK   r9  s      r/   symintzNopInputReader.symint  r;  r1   r#  NrH   rI   rJ   r0   r7  r:  r=  rK   r1   r/   r-  r-    s     7;t r1   r-  c                   D    e Zd Zd	dddZddddZ	 d	ddddddZd Zy)
InputReaderN)pbarc                v    |t         j                  d       |t        |      nd | _        g | _        || _        y )Nz0no save_dir specified, will generate random data)rB   rC   r   storer   rB  )r,   save_dirrB  s      r/   r0   zInputReader.__init__  s9    
 KKJK5=5I'1t
		r1   r1  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rz   )rB  update_device_or_default_dtype_or_defaultrD  read_storager2  rB   rC   r   itemsizer&  r   untyped_storage)r,   r5  r6  r2  r3  r7  rz   r"  s           r/   r7  zInputReader.storage  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)NrG  )r{   r2  r   )memory_format)r&  _storage_offset_or_defaultrJ  _is_leaf_or_default_requires_grad_or_defaultrw   r:  r2  no_gradset_enable_gradclonepreserve_format_subclasses
meta_utilssafe_is_leaf_utilsset_tensor_metadatar   append)
r,   r7  rz   r"  rN  r{   r   rO  metadatats
             r/   r:  zInputReader.tensor#  sC    $F%83NC!%(%g.1-@LLeGNN-
 ]]_ 	;FF7NE6:	;""$ AGG%*?*?G@A ?wv>?  ++88;wFFF((H5			; 	;A A? ?s$   1E)$!E5!F)E25E>F
c                 <    | j                   j                  |       |S r   )r   r_  )r,   vals     r/   r=  zInputReader.symintD  s    		
r1   r   r?  rK   r1   r/   rA  rA    s>    	d 	 7;t Q4 	 Br1   rA  c                   B    e Zd ZdddZd ZddddefdZdd	Zdd
Zy)InputWriterFstable_hashc                    g | _         t        j                         | _        || _        |t        ||      nd | _        i | _        y )Nrf  )_lines	itertoolsr   storage_counterrE  r   rD  seen_storages)r,   rE  rg  s      r/   r0   zInputWriter.__init__V  sG    (0  # x[A 	

  r1   c                 r    dg}|j                  d | j                  D               |j                  d       |S )Nzdef load_args(reader):c              3   &   K   | ]	  }d |   yw)rg   NrK   )r   ls     r/   r   z$InputWriter.lines.<locals>.<genexpr>f  s     14s1s   zload_args._version = 0)extendri  r_  )r,   rs     r/   lineszInputWriter.linesb  s8    $
 	
1T[[11 	
)*r1   Nr3  device_hintr#  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(, rj   )r   rl  r  rm   rk  rJ  r2  r\   rI  r6  rD  write_storageri  r_  )r,   rM  r3  rt  wsvmaybe_dtype_hintr   r2  r6  r5  s              r/   r7  zInputWriter.storageq  sB   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r1   c                 \   ddl m} | j                  |j                         |j                  |j
                        }g }t        d |j                        |j                         k7  r1|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,                  j.                  j1                  |      }t3        d       |k7  r|j                  d	|       | j4                  j                  d
dj7                  |t        t        |j                              g|      z   d| z          y )Nr   )statically_known_truers  rG  zdtype=zstorage_offset=c              3   0   K   | ]  \  }}| d |  yw)=NrK   )r   kr{  s      r/   r   z%InputWriter.tensor.<locals>.<genexpr>  s     IA1#QqeIs   zrequires_grad=zis_leaf=zreader.tensor(rx  )  # )%torch.fx.experimental.symbolic_shapesr~  r7  rM  r{   r2  r&  rz   r"  r_  strtuplerJ  rR  rN  rw   r]  get_tensor_metadatarp  rq   rT  r   rZ  r[  r\  rS  ri  r3   )r,   r   ra  r~  r7  r   tensor_metadatarO  s           r/   r:  zInputWriter.tensor  s   O,,AGG  
 d!''2ahhj@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	
r1   c                     t        |t        j                        r|j                  j                  }| j
                  j                  d|d|        y )Nzreader.symint(r  )r   rw   SymIntr  hintri  r_  )r,   r   rc  s      r/   r=  zInputWriter.symint  s<    c5<<(((--C^C7%v>?r1   r>  )	rH   rI   rJ   r0   rr  r  r7  r:  r=  rK   r1   r/   re  re  U  s.    05 
  6:t PS 8
>@r1   re  funcr2  
sym_shapesdefault_sym_shapec           	         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 fddt        f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)rH   rI   rJ   __doc__rK   r1   r/   TensorContainerr    s    -r1   r  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 inrK   )r=  s   r/   r  z=aot_graph_input_parser.<locals>.get_sym_int.<locals>.<lambda>  s    vhYZ r1   )rw   _checkr  )r=  r  r  s   `r/   get_sym_intz+aot_graph_input_parser.<locals>.get_sym_int  s9    j A$5T$AZ	
 ~~f&788r1   r#  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{   r2  )
	enumeratestripr_  intrx   rw   randnzerosr   mark_dynamic)rz   r{   resolved_shapedynamic_dimsr   dimr   constructorr   r'  r2  r  s             r/   
gen_tensorz*aot_graph_input_parser.<locals>.gen_tensor  s    & 	4FAs))+Ccz$%%a(##A&"))#c(3	4 &+%<%<ekk%++.fE 	/AMM&&sA.	/
r1   ,r   r,   )torch.fx.graphr  rq   r3   valuesinspect	getsourcer   __annotations__researchgroupsr  r$   group	signaturern   finditersetattr)r  r2  r  r  r  keyvalue	dtype_mapdtype_patternsourcetensor_assignment_regextensor_regexsym_shape_regexr  r  r  annotationsr   
annotationmatch	data_type	shape_strrz   r{   	container	attr_namerb   r  s    ```                       @r/   aot_graph_input_parserr    s   * +.9.?.?.AB
UBIBHH[//12M t$F "<M?Jz{&67L&O. .  F!rJ9F * &&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 MS Cs   Gr-   c                      t        j                         t        j                  j	                  t        j                  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                 F     t        j                          fd       }|S )Nc                      j                          	  | i |j                          S # j                          w xY wr   )enabledisable)r   r  fnprofs     r/   wrapperz3profile_to_file.<locals>.decorator.<locals>.wrapper   s1    KKM4*6*s   + =)	functoolswraps)r  r  r  s   ` r/   	decoratorz"profile_to_file.<locals>.decorator  s%    			 
	 r1   c            	          j                          t        j                  j                  t	        j
                  d  d  d             y )Nz!                Wrote profile to z+, view with:

                    snakeviz z

                )
dump_statssysstderrr@   r5   r6   )r-   r  s   r/   save_itz profile_to_file.<locals>.save_it*  sK    !

OO""* ,&Z (		
r1   )cProfileProfiler"   r#   r%   
expanduseratexitregister)r-   r  r  r  s   `  @r/   profile_to_filer    sR     Dwwrww11(;<H	
 OOGr1   )FF)F)r   NN)`r  r   r  r  rQ   r  rj  loggingr"   r  r   r  rO   r5   collectionsr   	importlibr   typingr   r   r   r   r	   r
   rw   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   	getLoggerrH   rB   r   inductor_config	is_fbcoder   libfb.py.build_infolibfbr4   extra_importspy
build_info	BuildInfoget_build_ruler(   r7   r3   rA   r   rU   rs   rX   	lru_cacher   r   r   r   r   r   r   r   r  r  r  r   r!  r&  r*  float32rJ  r2  rI  rR  rT  rS  r-  rA  re  r  r  r  r  )r8   s   0r/   <module>r     s           	 	  
    # ? ?  # #  . . ; M  . g!CL   89$$& 
J $$..==?GG	SWXJIITA!9!B?TUM 63 3l  a aH T 0 -2 (@U	I 	
0> 	7 7t*$1 	 RV56V +V &	V/Q /8Xa[M1$45 / "%--0 "<5<<#67 *1- )%0 #E*   E EhZ@ Z@~ +/'+	`
DL>4</
0`` c3h(`  }	`
 
#s(^`F#c #hsAv&6 #{ Us    I