
    &Vjis                   v   U 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
mZmZ d dlmZmZmZ d dlmZmZmZmZ d dlmZ d dlmZmZmZmZmZmZmZmZ d dlm Z m!Z!m"Z"m#Z#m$Z$ d d	l	m%Z% d dl&Z&d dl'Z&d dl(m)Z) d dl*m)c m+Z, d dl-m.c m/Z0 d d
l&m1Z1m2Z2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z=m>Z>m?Z?m@Z@ d dlAmBZB d dl(mCZCmDZDmEZE d dlFmGZG d dlHmIZImJZJmKZK d dlLmMZM d dlNmOZO d dlPmQZQ d dlRmSZSmTZTmUZUmVZV d dlWmXZX d dlYmZZZ d dl[m\Z\m]Z]m^Z^ ddl_m`Z` ddlambZb er d dlcZcd dlmdZd d dleZed dl'mfZf d d lgmhZh d d!limjZj g d"Zked#         Zle&jm        e7fZnee&jm        e7f         Zoe&jp        jq        Zqe&jp        jr        Zr ejs        et          Zue&jv        w                    etd$          Zxi ayd%ezd&<   dZ{ ed'          Z| ed(          Z} e!d)          Z~ ed*          Z e#d+          Z ee          Z e0j        e&j        d, d- d. d/0           e&j        eiZdd6Zedd:            Z e            Z G d; d<          Z e            Zd d=limZmZ  G d> d?e          ZddBZeddJ            ZeddM            ZeddP            Z G dQ dRej                  Z e            Zi adSezdT<   ddUZeddW            ZddZZdd[ZeZeD         Zedd\            Zed d_            Zeddc            Zeddd            Zeddf            Zeddh            Zeddi            Zeddk            Zeddm            Zedn fddqZd	d
dvZeeeeoe`edw         edx         eedwf         e3eeef
                  Zd	ddyZedzd{dd}            Zedd~            Zedd            ZddZddZddZeeDed         eedf         f         Zee3ed         eedf         f         ZddZe G d dH                      ZddZedd            Zedd            Zedd            ZddZe3e&j        j        e=fZddZddZddZ G d d          Z G d deE          Z	 dddZddZe&j        	 ddd            ZddZdadezd<   eddÄ            Z G dĄ deQ          Z ee          Z G dƄ deQ          Z ee          Z G dȄ deV          Z G dɄ de)j        j                  Z G d˄ de)j                  ZȐddτZeddЄ            Z G dф de˦          Z G dӄ dԦ          Z G dՄ de          Z G dׄ dئ          Zdadezd<   ed dۄ            Z	 	 	 d!drdrdrdzdrdݜd"dZҐd#dZӐd$dZԐd$dZՐd%dZed&d            Zאd'dZ	 	 d(d)dZِd*dZdS (+      )annotationsN)defaultdictOrderedDict)	GeneratorMappingSequence)_GeneratorContextManagercontextmanager	ExitStacknullcontext)	dataclass)AnyCallableOptionaloverloadProtocolTYPE_CHECKINGTypeVarUnion)Concatenate	ParamSpecSelfTypeVarTupleUnpack)WeakKeyDictionary)SymBoolSymIntTensor)enable_python_dispatcher)FakeScriptObject)trace_structured)fast_detach)
FakeTensorFakeTensorModeis_fakeunset_fake_temporarily)is_sparse_any)GraphModuleProxyTracer)_assign_attr)1_side_effectful_need_to_be_preserved_pre_dispatchArgumentTarget)_extract_tensor_metadata)Module)TorchFunctionMode)_disable_infra_mode
_push_mode_unset_infra_modeTorchDispatchMode)count)Thunk)_WeakHashRefWeakIdKeyDictionaryWeakTensorKeyDictionary   )BackwardState)SymNode)MutableMapping)
OpOverload)PHBase)IntLikeType)
PythonKeyTracerdispatch_tracemake_fxDecompositionInterpreterpy_sym_typesget_innermost_proxy_modeget_proxy_modehandle_sym_dispatchmaybe_enable_thunkifymaybe_disable_thunkify)rB   _GraphAppendingTracerExnot_implementedzMapping[OpOverload, Callable]CURRENT_DECOMPOSITION_TABLETU_PR_Tsc                $    t          |           d fS N)listxss    \/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/fx/experimental/proxy_tensor.py<lambda>rZ   }   s    R$     c                     t          |           S rU   )tuple)rX   _s     rY   rZ   rZ   ~   s    %)) r[   c                8    d t          |           D             d fS )Nc                @    g | ]\  }}t          j        |          |fS  )pytreeSequenceKey).0ixs      rY   
<listcomp>z<lambda>.<locals>.<listcomp>   s,    >>>1&
Q

	#>>>r[   )	enumeraterW   s    rY   rZ   rZ      s#    >>	">>>% r[   z
torch.Size)flatten_with_keys_fnserialized_type_namefnCallable[_P, R]nargsintreturnc                    d                     d t          |          D                       }t          d| d| dd| i          S )z*FX gets confused by varargs, de-confuse it,c              3      K   | ]	}d | V  
dS )argNra   )rd   re   s     rY   	<genexpr>z!fake_signature.<locals>.<genexpr>   s(      88a	a		888888r[   zlambda z: fn()rk   )joinrangeeval)rk   rm   argnamess      rY   fake_signaturerz      sP    xx885<<88888H4(44444tRjAAAr[   decomposition_table'Optional[Mapping[OpOverload, Callable]]4Generator[Mapping[OpOverload, Callable], None, None]c              #  F   K   t           }| pi a 	 t           V  |a d S # |a w xY wrU   )rN   )r{   old_decomposition_tables     rY   	decomposer      sI      
 :"5";>))))&=###&=#====s   	  c                      e Zd ZdS )
_NoDefaultN__name__
__module____qualname__ra   r[   rY   r   r              Dr[   r   )rF   	PySymTypec                      e Zd ZU ded<   dS )_HasMetazdict[str, PySymType]metaNr   r   r   __annotations__ra   r[   rY   r   r      s         r[   r   nodeboolc                    t          | d          s
J d            d| j        v ot          | j        d         t                    S )Nr   z3All nodes traced with proxy_tensor should have metaval)hasattrr   
isinstancerF   )r   s    rY   is_sym_noder      sD    4  WW"WWW DIL*TYu-=|"L"LLr[   objr   tracer_ProxyTracerproxy_ProxyTensorNonec                    d S rU   ra   r   r   r   s      rY   set_proxy_slotr      s    TWTWr[   _AnyScriptObjectTyper)   c                    d S rU   ra   r   s      rY   r   r      	     3r[   r   _PySymProxyTypec                    d S rU   ra   r   s      rY   r   r      r   r[   c                      e Zd ZU dZded<   dS )_DisableUpdateTensorTrackerFr   valueN)r   r   r   r   r   ra   r[   rY   r   r      s#         Er[   r   zdict[int, torch.fx.Node]'_FAKE_TENSOR_ID_TO_PROXY_MAP_FOR_EXPORTc                     t           j        S )zC
    Returns current state of disabling update tensor tracker.
    "_disable_update_tensor_tracker_tlsr   ra   r[   rY   /_is_proxy_tensor_update_tensor_tracker_disabledr      s     .33r[   Generator[None, None, None]c               #  ~   K   t           j        } dt           _        	 dV  | t           _        dS # | t           _        w xY w)a  
    NOTE "Do not clobber inplace ops"
    By default tensor_tracker is updated every time.
    This leads to chaining every operation by the FakeTensor.
    For example for mutable ops if we have several consecutive mutable operations:

    def f(x, y, z):
        x.copy_(y)
        x.copy_(z)
        return x

    Default graph result:
    def f_graph(x, y, z)
        x_1 = x.copy_(y)
        x_2 = x_1.copy_(z)
        return x_2

    This chaining simplifies the fx passes and helps to prevent the reordering.
    But in some cases, we want those nodes to be disconnected.
    E.g. in case of splitting joint graph into forward and backward.
    If first inplace op happened in forward, second in backward,
    we want them after split to be properly placed.

    Enabling this context manager for copy_ will result in:
    def f_graph_2(x, y, z):
        x_1 = x.copy_(y)
        x_2 = x.copy_(z)
        return x

    Results of copy_ x1 and x2 will have empty users in the graph.
    The reason why this behavior is not enabled for all inplace ops is that
    some fx passes (e.g. fx quantization) rely on chaining inplace ops like add_
    in their fusions passes.
    We could revisit enabling this logic for all inplace ops in future.
    TNr   )
orig_values    rY   +_proxy_tensor_disable_update_tensor_trackerr      sL      J 49J/3&,>3=*000:*0====s   . <.Union[PySymType, _AnyScriptObjectType, Tensor]objectc                ~   t                               d| t          |           |           t          | t                    r3t          |t
                    sJ t                      s||j        | <   d S d S t          | t                    r#t          |t                    sJ ||j
        | <   d S t          | t                    sJ t          |                       | |j        vr[t          j        t           |          |j        | <   dd l}t          | j        j        |j                  r||j        | j        j        <   d S d S d S )Nzset_proxy_slot %s (%s) %sr   )logdebugidr   r   r   r   tensor_tracker_AnyScriptObjectr)   script_object_trackerrF   typesymnode_trackertypingcastr   sympyr   exprSymbolsympy_expr_tracker)r   r   r   r   s       rY   r   r     sR   
 II)33???#v %A %.....>@@ 	/).F!#&&&	/ 	/	C*	,	, A%''''',1$S))) #|,,77d3ii77,f,,,*0+ou*M*MF"3' LLL#(-66 A;@)#(-888% -,"A Ar[   c                    t          | t          t          f          sJ t          |                       t	          t          | |dd                     S )NFc                    dS NTra   )r^   s    rY   rZ   z has_proxy_slot.<locals>.<lambda>5  s    T r[   )r   r   r=   r   r   get_proxy_slotr   r   s     rY   has_proxy_slotr   3  sI    cFG,--88tCyy88-sFE>>BBCCCr[   c                    d S rU   ra   r   s     rY   r   r   ;  s	     3r[   defaultUnion[_ProxyTensor, U]c                    d S rU   ra   r   r   r   s      rY   r   r   B  s	    
 !Sr[   	transformCallable[[_ProxyTensor], R]Union[R, U]c                    d S rU   ra   r   r   r   r   s       rY   r   r   J  	     #r[   c                    d S rU   ra   r   s     rY   r   r   S  s	     Cr[   Union[Proxy, U]c                    d S rU   ra   r   s      rY   r   r   Z  s	    
 cr[   Callable[[Proxy], R]c                    d S rU   ra   r   s       rY   r   r   b  r   r[   c                    d S rU   ra   r   s     rY   r   r   k  s	     cr[   Union[T, _PySymProxyType]c                    d S rU   ra   r   s      rY   r   r   r  s	    
 !$r[   Callable[[_PySymProxyType], R]c                    d S rU   ra   r   s       rY   r   r   z  r   r[   c                    | S rU   ra   rf   s    rY   rZ   rZ     s    A r[   .Union[Tensor, _AnyScriptObjectType, PySymType]r   c                   t          | t                    r|j        }nPt          | t                    r|j        }n3t          | t
                    sJ t          |                       |j        }| |vr{t          | t
                    r+| j        j	        |j
        v r|j
        | j        j	                 }nCt          |t                    r$t          |  dt          |            d|           |S ||          } ||          }|S )Nz (z)is not tracked with proxy for )r   r   r   r   r   rF   r   r   r   r   r   r   RuntimeErrorr   )r   r   r   r   trackerr   ress          rY   r   r     s    #v )'	C)	*	* ).#|,,77d3ii77,(
'c<(( 	SX]f>W-W-W-chm<EE':.. "NNbggNNfNN   N
)E

CJr[   Fr   include_realOptional[Tensor]c                    t          | t                    rt          | j        | |          S |                                 S rU   )r   r#   r"   	fake_modedetach)r   r   s     rY   snapshot_faker     s8     #z"" 3=#|<<<zz||r[   _ExtractValType)r   .c                N   t          |           rt          | |          S t          | t                    r| S t          | t                    r| S t          | t
                    r| S t          | t          t          f          r|                     d | D                       S t          | t                    rd | 
                                D             S t          | t                    r| j        swddlm}  ||           }|st          d          }|5  t!          j        | j        |                                 | j        | j                  cd d d            S # 1 swxY w Y   n*d S t          | t,          t.          t0          f          r| S | d S t3          j        |            d S )	Nr   c                ,    g | ]}t          |          S ra   extract_valrd   rf   s     rY   rg   zextract_val.<locals>.<listcomp>  s    :::k!nn:::r[   c                4    i | ]\  }}|t          |          S ra   r   )rd   kvs      rY   
<dictcomp>zextract_val.<locals>.<dictcomp>  s$    :::da;q>>:::r[   r   )detect_fake_modeT)allow_fallback_kernels)devicedtype)r%   r   r   rF   r   r<   rV   r]   	__class__dictitemsr   	is_sparsetorch._guardsr   r$   torchempty_stridedshapestrider   r   rn   floatr   typing_extensionsassert_never)r   r   r   fake_tensor_modes       rY   r   r     s    s|| "S|<<<<	C	&	&  
	C)	*	* 
	C	'	' 
	C$	'	' }}::c:::;;;	C		 ::ciikk::::	C	 	  } 	 766666//44# O#1#N#N#N !  *Iszz||CJci                  
 4	C#ud+	,	, 
	t"3'''''s   9EE"ETenabler  c             #  V   K   | j         }|| _         	 dV  || _         dS # || _         w xY w)a{  
    Enable thunkification inside the context manager.  Thunkification prevents
    SymNode computation from directly being traced into an FX graph; instead,
    the compute is only added to the graph if it is actually used.  This helps
    us track SymNode compute when it is computed (since we need /something/
    to put in the tracker) even if it is unlikely to be used.
    N)enable_thunkify)r   r  olds      rY   _enable_thunkifyr	    sH       
 C#F%!$$$$$s    	(c               #     K   t                      } | 5t          | j        d          5  dV  ddd           dS # 1 swxY w Y   dS dV  dS )a[  Within a context, disable thunkification.  See :func:`maybe_enable_thunkify`
    for more details.  This is helpful if you have a wrapper function which
    you want to enable thunkification on, but in some segment on the inside (say,
    the original user function), you want to disable thunkification as you know
    it is not needed there.
    NFr  rH   r	  r   
proxy_modes    rY   rK   rK     s        !!Jj/>>> 	 	EEE	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   ;??c               #     K   t                      } | 3t          | j                  5  dV  ddd           dS # 1 swxY w Y   dS dV  dS )a3  Within this context manager, if you are doing make_fx tracing, we will thunkify
    all SymNode compute and avoid tracing it into the graph unless it is actually needed.
    You should prefer to avoid using this as much as possible, as lazy evaluation of
    SymNode tracing can lead to long chains of thunks which will stack overflow
    if you evaluate them.  However, this is currently sometimes necessary as there
    are buggy parts of PT2 which will fail with "s0 is not tracked with proxy" error
    due to insufficient tracing of SymNode computation.
    Nr  r  s    rY   rJ   rJ   
  s        !!Jj/00 	 	EEE	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   9= =c                ~   t          || j        j        dk              | j        j        d<   t	          | j                  5  t          |          rt          |          | j        j        d<   n8t          |t                    r#|j
        st          |          | j        j        d<   d d d            n# 1 swxY w Y   | S )Nplaceholderr   r   tensor_meta)r   r   opr   r	  r   r%   r/   r   r   r   )r   r   s     rY   set_metar  #  s   (5:=M9  EJOE 
%,	'	' K K3<< 	K-Ec-J-JEJOM**V$$ 	KS] 	K-Ec-J-JEJOM*K K K K K K K K K K K K K K K Ls   A%B22B69B6fargs_P.argskwargs	_P.kwargsThunk[R]c                    | j         r"t          t          j        |g|R i |          S  ||i |t          fd          S )zT
    Delays computation of f until it's called again
    Also caches the result
    c                      S rU   ra   )rs   rY   rZ   zthunkify.<locals>.<lambda><  s    Q r[   )r  r7   	functoolspartial)r   r  r  r  r  s       @rY   thunkifyr  1  sa       Y&q:4:::6::;;;AtvYYYYr[   tensorconstantc                  dfd}t          | j                  D ]\  }} ||fd|           t          |           s8t          |                                           D ]\  }} ||fd|            ||                                 fd           t          |           s" ||                                 fd           t          | t          |                     d S )Nouter_srA   proxy_callable+Callable[Concatenate[PySymType, _P], Proxy]r  r  r  r  ro   r   c           
         t          |          sJ t          | t                    rKt                    5  t	          | t          || g|R i |           d d d            d S # 1 swxY w Y   d S d S rU   )callabler   r   r	  r   r  )r#  r$  r  r  r   s       rY   try_set_proxy_slotz(track_tensor.<locals>.try_set_proxy_slotB  s     '''''gv&& 	!&))  V^WNtNNNvNN                   	 	s   "A&&A*-A*c                    t                              dt          j        j        j        j        |fi           |           S Ncall_function)r  create_proxyr   opsatensym_sizern   rf   re   r   r   s     rY   rZ   ztrack_tensor.<locals>.<lambda>Y  s@    ###UY^%<%@5!*b  	  r[   c                    t                              dt          j        j        j        j        |fi           |           S r*  )r  r,  r   r-  r.  
sym_stridern   r0  s     rY   rZ   ztrack_tensor.<locals>.<lambda>f  sB    X''')B)FPQ
TV  	  r[   c                    t                              dt          j        j        j        j        fi           |           S r*  )r  r,  r   r-  r.  	sym_numelr   rf   r   r   s    rY   rZ   ztrack_tensor.<locals>.<lambda>q  s>    (!9!AE8R  	
 
 r[   c                    t                              dt          j        j        j        j        fi           |           S r*  )r  r,  r   r-  r.  sym_storage_offsetr   r5  s    rY   rZ   ztrack_tensor.<locals>.<lambda>{  sA    h###IN5=H	    r[   )
r#  rA   r$  r%  r  r  r  r  ro   r   )rh   r   r'   r   numelstorage_offsetr   r   )r   r   r!  r   r(  re   ss    ` `   rY   track_tensorr;  ?  s        ( &,'' 

 

1     		
 		
 		
 		
    fmmoo.. 
	 
	DAq     	 	 	 	 	
 	
 	
 	
 	
      
!!##    	
 	
 	
 66<x#@#@AAAAAr[   _NestedProxys_NestedTensors	inner_res	proxy_resOptional[_NestedTensors]c               R    t          | |           d
fd	 | ||           | S )Ner   r   r<  r!  r@  ro   r   c           	        t          | t                    rUt          t                    sJ |t          |t                    sJ t          | |           t	          |            d S t          | t
                    rKt          t                    sJ t	          |            t          | t          fd                     d S t          | t                    r:t          t                    sJ t          |            t	          |            d S t          | t          t          f          rbt          t          j                  rt	          |            d	d}t          |           D ]"\  }} 	||          |||                     #d S t          | t                    r]|J t          t          j                  rt	          |            |                                 D ]\  }} 	||         d            d S t          | t                    r0t          t                    sJ t	          |            | _        d S d S )
N)r   r!  c                      S rU   ra   r   s   rY   rZ   z<track_tensor_tree.<locals>.wrap_with_proxy.<locals>.<lambda>  s    u r[   cr@  idxrn   ro   c                V    | d S t          | t          t          f          sJ | |         S rU   )r   rV   r]   )rF  rG  s     rY   get_constantz@track_tensor_tree.<locals>.wrap_with_proxy.<locals>.get_constant  s1     94%a$77777S6Mr[   )rF  r@  rG  rn   ro   r@  )r   r   r)   r;  r  rF   r   r  r   r]   rV   fxrh   r   r   r<   r   )
rB  r   r!  rI  rG  eekeyr   r   wrap_with_proxys
    `      rY   rM  z*track_tensor_tree.<locals>.wrap_with_proxy  s    a   7	eU+++++#z(F'C'C##CE&8DDDDUA<(( 2	eU+++++UA1fhv}}}}&E&EFFFFF+,, -	eU+++++1fe,,,UAE4=)) )	%** #"""" " " " %Q<< M MR  E#JXs0K0KLLLLM M
 4   	 ###%** #"""GGII 7 7SU3Z66667 7 =)) 	eU+++++UAAGGG Dr[   )rB  r   r   r<  r!  r@  ro   r   )_set_unbacked_bindings)r>  r?  r!  r   rM  s      `@rY   track_tensor_treerO    sV    ( 9i000: : : : : : :x OIy(333r[   c                  $    e Zd ZU ded<   ded<   dS )r   r)   r   r   r!  Nr   ra   r[   rY   r   r     s'         LLLr[   5Callable[[PySymType], Union[bool, int, float, Proxy]]c                     d fd}|S )NrB  r   ro   Union[int, bool, float, Proxy]c                   | j         }|j        |j        S | j         j        j        rut	          | t
                    rt          | j         j                  S t	          | t                    rt          | j         j                  S t          | j         j                  S t	          | t                    sJ t          |                                           S rU   )r   r!  r   	is_numberr   r   r   r   rn   r   rF   r   force)rB  nr   s     rY   innerzfetch_sym_proxy.<locals>.inner  s    F:!:6;  		5!W%% (AFK(((Av&& (16;'''%%%a.....!!V,,22444r[   )rB  r   ro   rS  ra   )r   rX  s   ` rY   fetch_sym_proxyrY    s(    5 5 5 5 5 5 Lr[   tUnion[_ProxyTensor, Tensor]c                    d S rU   ra   r   rZ  s     rY   fetch_object_proxyr^    s	     #&#r[   "Union[Proxy, _AnyScriptObjectType]c                    d S rU   ra   r]  s     rY   r^  r^    s	     *-r[   !Union[_PySymProxyType, PySymType]c                    d S rU   ra   r]  s     rY   r^  r^    s	     ),r[   c                $    t          || |          S rU   r   r]  s     rY   r^  r^    s     !VQ'''r[   funcr  ProxyTorchDispatchModec                   |j         s|j        sdS t          | t          j        j                  rt          j        j        | j        vrdS t          t          t          |j        j        j                                      }|j                            d          }t          |t          j                  r|j        t          j        t          j        fvrdS d|j        d<   dS )zd
    Records pointwise operators in user program (non decomposed) that were output in fp16/bf16
    Nr   Tlow_precision_pointwise_barrier)decomp_layersemulate_precision_castsr   r   _opsr?   Tag	pointwisetagsnextiterreversedr   graphnodesr   getr   r   bfloat16float16)re  r  	last_noderZ  s       rY   _maybe_record_pointwise_barrierrx    s      z'I  tUZ2339di//T(:#4#:#@AABBCCI5!!Aa&& !': + + 	8<IN4555r[   flat_args_kwargs'Union[list[object], tuple[object, ...]]-tuple[list[object], tuple[object, ...], bool]c                    fd| D             }t          d |D                        ot          d | D                        }d |D             }fd|D             }|t          |          |fS )z
    Given flat arguments, fetch the proxies and whether they are all constants.
    This is later used in proxy_call or when someone is trying to stitch together
    graph node in tf or td modes.
    c                l    g | ]0}t          |t          t          f          rt          |          n|1S ra   )r   r   r   r^  )rd   rf   r   s     rY   rg   z8_fetch_proxies_and_all_constant_flag.<locals>.<listcomp>?  sS         !f&6788vq)))	  r[   c              3  R   K   | ]"}t          |t                    |j        d u V  #d S rU   r   r   r!  rd   rZ  s     rY   rt   z7_fetch_proxies_and_all_constant_flag.<locals>.<genexpr>L  sM       
 
!\**
J$
 
 
 
 
 
r[   c              3  @   K   | ]}t          |t                    V  d S rU   )r   rF   r   s     rY   rt   z7_fetch_proxies_and_all_constant_flag.<locals>.<genexpr>S  s,      JJAJq,//JJJJJJr[   c                J    g | ] }t          |t                    r|j        n|!S ra   )r   r   r   )rd   rB  s     rY   rg   z8_fetch_proxies_and_all_constant_flag.<locals>.<listcomp>V  s:       :;:a..5A  r[   c                n    g | ]1}t          |t                    r t                    |          n|2S ra   )r   rF   rY  )rd   rB  r   s     rY   rg   z8_fetch_proxies_and_all_constant_flag.<locals>.<listcomp>Z  sP        (2!\'B'B	I	 	 	 	#	#	#  r[   )anyr]   )ry  r   f_flat_args_kwargsall_constantproxy_flat_args_kwargss    `   rY   $_fetch_proxies_and_all_constant_flagr  7  s        "    
 
'
 
 
 
 
 	
 	K JJ9IJJJJJJ  ?Q     '  
 u%;<<lJJr[   r?   pre_dispatchtuple[object, ...]dict[str, object]c           	     z	    g t          j        ||f          \  }}d fdt          fd|D                       s"t                              d           t
          S t           |||          }|t
          urt          |            |S |s|t          j	        j
        j        j        t          j	        j
        j        j        t          j	        j
        j        j        fvr? 5   |j        |i |}|t
          ur|cd d d            S 	 d d d            n# 1 swxY w Y   |t          j	        j
        j        j        u rj 5  t          j        |d                                         d	k    d
            |d         dk                                    cd d d            S # 1 swxY w Y    j        }t+          ||          \  }	}
}t          j        j        |j        v r|rRd |	D             }t          j        ||          \  }}t5                      5   ||i |cd d d            S # 1 swxY w Y    j        r1t          j        t:          d ||f          rt=          d| d          t          j        |
|          \  }}|t          j	        j
        j        j        u rt          j	        j
        j         j        } j        !                    d||| j        j"        #                    |j$        j%                            }tM           j                  5   ||i |}d d d            n# 1 swxY w Y   tO          d |	D                       }d }dd}|t          j	        j
        j         j        u r|                                tP          k    rt5                      5  tS          |d         tT          t:          f          sJ tW          |d                               |d         ,                                }d d d            n# 1 swxY w Y   nt          j        j-        |j        vrr|rp|rnt          j        t:          ||          rSt5                      5  d |	D             }t          j        ||          \  }} ||i |}d d d            n# 1 swxY w Y   nd }t]          ||||           t          |            |S )Nrf   r   ro   r   c                    t          |           t          v pt          | j                  }j        r#|p t          |           t
          j        j        fv }|s"                    t          |                      |S rU   )	r   HANDLED_TYPESr   r   _allow_fake_constantr   _subclassesr#   append)rf   r  r  unrecognized_typess     rY   can_handle_tensorz%proxy_call.<locals>.can_handle_tensorl  sv    GG}$Lq*:K(L(L* 	@?T!WW!2!= ??A 	/%%d1gg...r[   c              3  X   K   | ]$}t          |t                     |          V  %d S rU   )r   r   )rd   rf   r  s     rY   rt   zproxy_call.<locals>.<genexpr>v  s>      WWAvAVAVW  ##WWWWWWr[   zEProxyTensorMode tensors without proxy had unrecognized subclasses: %sr   r;   c                     dS )Nz=Boolean value of Tensor with more than one value is ambiguousra   ra   r[   rY   rZ   zproxy_call.<locals>.<lambda>  s    W r[   c                J    g | ] }t          |t                    r|j        n|!S ra   r  r  s     rY   rg   zproxy_call.<locals>.<listcomp>  =     & & & )L99@

q& & &r[   c                "    t          |            S rU   )r%   rZ  s    rY   rZ   zproxy_call.<locals>.<lambda>  s    '!**n r[   zHIt appears that you're trying to get value out of a tracing tensor with z - erroring out! It's likely that this is caused by data-dependent control flow or similar.  It may be possible to trace this with dynamic shapes; try setting tracing_mode='symbolic' in your make_fx call.r+  )namec              3  R   K   | ]"}t          |t                    |j        d uV  #d S rU   r  r  s     rY   rt   zproxy_call.<locals>.<genexpr>  sM        a&&	
$     r[   rZ  c                <    |                                  t          k    S rU   )r8  CONSTANT_NUMEL_LIMITr  s    rY   tensor_numel_in_limitz)proxy_call.<locals>.tensor_numel_in_limit  s    wwyy000r[   c                J    g | ] }t          |t                    r|j        n|!S ra   r  r  s     rY   rg   zproxy_call.<locals>.<listcomp>  r  r[   r!  r   )rf   r   ro   r   )rZ  r   ro   r   )/rb   tree_flattenallnot_implemented_logr   NotImplementedmaybe_handle_decomprx  r   r-  r.  sizer   r   r9  r   
is_nonzero_checkr8  itemr   r  rl  data_dependent_outputrn  tree_unflattenr&   _error_on_data_dependent_opstree_all_onlyr   r   
lift_freshlift_fresh_copyr,  rr  _target_to_stroverloadpacketr   r	  r  r  r   r)   r   clonenondeterministic_seededrO  )r  re  r  r  r  ry  specr  r   r  r  r  const_flat_args_kwargs
const_argsconst_kwargs
proxy_argsproxy_kwargs	proxy_outoutany_constantr!  r  r  r  s   `                     @@rY   
proxy_callr  b  s    &(#0$@@d       WWWW-=WWWWW !!S	
 	
 	
 JdF;;A'j999  D	#	%	%-)  
  	 	///A&&	 	 	 	 	 	 	 	&	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 uy~(000 	) 	)LQ1$WW   GqL&&((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) F,-=vFF =. y&$)33 		9& &+& & &" (.'<&( ($J ()) 9 9tZ8<889 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
 2 	v7K,,tVn8
 8
 	 ([_ ( ( (    &45KTRRJF uy~(000y~-5!..$33D4G4PQQ /  I 
*+	,	, $ $dD#F##$ $ $ $ $ $ $ $ $ $ $ $ $ $ $0   #    L H1 1 1 1 		.666IIKK///#%% 	' 	'd1gv77FFd1gFF7Aw}}H	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 		):: 	; 	;  )>DD 	; $%% 	9 	9& &+& & &" (.'<&( ($J tZ8<88H	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 c9xGGGG#D*555Jsn   &DDDAFF#&F#H&&H*-H*	L((L,/L,AP  PP-R

RRc                  N    e Zd ZdZddZdd	Zdd
ZddZ	 dddZddZ	ddZ
dS )_SymNodeDictzM
    Wrapper around a dictionary that will hash SymInts with their nodes
    ro   r   c                    i | _         d S rU   )sym_node_dictselfs    rY   __init__z_SymNodeDict.__init__+  s    ?Ar[   rL  r   r   r   c                $    || j         |j        <   d S rU   r  r   )r  rL  r   s      rY   __setitem__z_SymNodeDict.__setitem__.  s    ',38$$$r[   c                &    | j         |j                 S rU   r  r  rL  s     rY   __getitem__z_SymNodeDict.__getitem__1  s    !#(++r[   r   c                    |j         | j        v S rU   )r   r  r  s     rY   __contains__z_SymNodeDict.__contains__4  s    x4---r[   Nr   Optional[_PySymProxyType]c                B    | j                             |j        |          S rU   )r  rt  r   )r  rL  r   s      rY   rt  z_SymNodeDict.get7  s    
 !%%ch888r[   r   c                    t           rU   )NotImplementedErrorr  s    rY   __iter__z_SymNodeDict.__iter__>  s    !!r[   rn   c                *    t          | j                  S rU   )lenr  r  s    rY   __len__z_SymNodeDict.__len__A  s    4%&&&r[   ro   r   )rL  r   r   r   ro   r   )rL  r   ro   r   )rL  r   ro   r   rU   )rL  r   r   r  ro   r   )ro   r   )ro   rn   )r   r   r   __doc__r  r  r  r  rt  r  r  ra   r[   rY   r  r  &  s         B B B B- - - -, , , ,. . . . DH9 9 9 9 9" " " "' ' ' ' ' 'r[   r  c                       e Zd ZU ded<   ded<   ded<   ded<   d	ed
<   dZded<   d= fdZd>dZd?d!Zd@ fd$Ze	dAd(            Z
e	dBd+            Z
e	dCd.            Z
dDd0Z
	 	 dEdF fd<Z xZS )GrB   +MutableMapping[_AnyScriptObjectType, Proxy]r   r  r   dict[sympy.Symbol, object]r   $MutableMapping[Tensor, _ProxyTensor]r   dict[OpOverload, int]torch_fn_countsFr   r  ro   r   c                   t                                          d           t                      | _        t	                      | _        t          d t                    | _        t                      | _
        d | _        i | _        d| _        d S )Nra   )autowrap_modulesr   ref_typeF)superr  r:   r   r  r   r9   r8   r   r   r   torch_fn_metadatar  r  )r  r   s    rY   r  zPythonKeyTracer.__init__M  s    "---577+~~%8&
 &
 &
" #'&& "&  "$r[   mr0   forwardCallable[..., Any]r  tuple[Any, ...]r  dict[str, Any]r   c                     ||i |S rU   ra   )r  r  r  r  r  s        rY   call_modulezPythonKeyTracer.call_module`  s     w''''r[   attrstrattr_valr   parameter_proxy_cachedict[str, Proxy]c                    |S rU   ra   )r  r  r  r  s       rY   getattrzPythonKeyTracer.getattrj  s	     r[   afx.node.Nodec                   t          |t          j        j                  r| j                                        D ]#\  }}||u r|                     d|di           c S $|                     d          }t          | j        ||           |                     d|di           S t          |t                    r|j
        j        J |j
        j        S t                                          |          S )Nget_attrra   _param_constant)r   r   nn	Parameterrootnamed_parameterscreate_nodeget_fresh_qualnamesetattrrF   r   r!  r  
create_arg)r  r  rW  pqualnamer   s        rY   r  zPythonKeyTracer.create_argo  s    a+,, 	#	2244 C C166++J2rBBBBB  ../@AAHDIx+++##J"bAAA<(( 	#6?...6?"ww!!!$$$r[   rB  r   Union[Proxy, Tensor]c                    d S rU   ra   r  rB  s     rY   unwrap_proxyzPythonKeyTracer.unwrap_proxy~  s    ?Bsr[   r   Union[Proxy, PySymType]c                    d S rU   ra   r  s     rY   r  zPythonKeyTracer.unwrap_proxy  s    EHSr[   r   r_  c                    d S rU   ra   r  s     rY   r  zPythonKeyTracer.unwrap_proxy  s	     .1Sr[   rO   c                    t          |t                    rt          || |d           S t          |t                    rt          || |d           S t          |t                    rt          || |          S |S )Nc                    | j         S rU   rE  r   s    rY   rZ   z.PythonKeyTracer.unwrap_proxy.<locals>.<lambda>  s     r[   c                *    |                                  S rU   )rV  )rB  s    rY   rZ   z.PythonKeyTracer.unwrap_proxy.<locals>.<lambda>  s    		 r[   )r   r   r   rF   r   r  s     rY   r  zPythonKeyTracer.unwrap_proxy  s    a   	!!T1.?.?@@@<(( 	!!T1.A.ABBB+,, 	!!T1---Hr[   Nkindtargetr.   tuple[Argument, ...]dict[str, Argument]r  Optional[str]	type_exprOptional[Any]torch.fx.Nodec                2   t                                          ||||||          }|j        dv rd|j        v r|j        d= |dk    rt	          |t
                    sJ t          | j        |          }t	          |t          j	                  r=t                      5  t          |          |j        d<   d d d            n# 1 swxY w Y   dd	}	t          |||f          r6t          j        j                            ||f|	          \  }
}|
|f|j        d
<   |S )Nr  outputstack_tracer  r   r   r   ro   Optional[_ExtractValType]c                   t          | t          j        j                  r	d| j        vrd S | j        d         }t          |t          j                  rt          |t                    sd S t          | j        d                   S )Nr   )r   r   rJ  Noder   r   r#   r   )r   r   s     rY   map_fnz+PythonKeyTracer.create_node.<locals>.map_fn  st    a// 53F3Ft&-C #u|,, ZZ5P5P tqve}---r[   eager_input_vals)r   r   ro   r  )r  r  r  r   r   r  r  r  r   r   disable_proxy_modes_tracingr   _should_save_eager_input_valsrJ  r   map_aggregate)r  r
  r  r  r  r  r  r   r  r  arg_inp	kwarg_inpr   s               rY   r  zPythonKeyTracer.create_node  sm    ww""4vtYOO7///MTY4N4N	-(:fc*****49f--D$-- 9022 9 9'24'8'8DIe$9 9 9 9 9 9 9 9 9 9 9 9 9 9 9	. 	. 	. 	. )$@@ 	A "'!<!<dF^V!T!TGY-4i,@DI()s   C  CCr  )
r  r0   r  r  r  r  r  r  ro   r   r  r  r  r   r  r  ro   r   )r  r   ro   r  )rB  r   ro   r   )rB  r   ro   r  )rB  r   ro   r_  )rB  rO   ro   r   )NN)r
  r  r  r.   r  r  r  r  r  r  r  r  ro   r  )r   r   r   r   r  r  r  r  r  r   r  r  __classcell__r   s   @rY   rB   rB   E  s]        FFFF!!!!22228888****!O!!!!% % % % % %&( ( ( (   
% % % % % % BBB XBHHH XH1 1 1 X1     ##'( ( ( ( ( ( ( ( ( ( (r[   rB   r  r   args_kwargs:Optional[tuple[tuple[Argument, ...], dict[str, Argument]]]c                   ddl m} t          |           sdS t          | t          j        j        j        t          j        j        j        |f          rdS || t          j	        j
        j        u s| t          j	        j
        j        u rV|d         }t          |d         t          j        j        t          j        j        f          sJ t!          |d         d           S | t          j	        j
        j        u rdS t          | t          j        j                  r-t%          j        t           |          rt)          d|  d          t          | t          j        j                  r)ddlm}  ||           t          j        j        j        k    S dS )Nr   )InvokeSubgraphHOPFTzNYI: The HOP z has an input that is an OpOverload that needs exact strides. We probably need special logic to propagate the FakeTensor vals. Please file an issue.)get_layout_constraint_tag)'torch._higher_order_ops.invoke_subgraphr&  r'  r   r   _higher_order_opstriton_kernel_wrapTritonKernelWrapperFunctionalTritonKernelWrapperMutationr-  higher_orderauto_functionalizedauto_functionalized_v2rk  r?   HigherOrderOperatorr  with_effectsrb   tree_anyr   torch._library.utilsr'  _Crl  needs_exact_strides)r  r#  r&  r  r'  s        rY   r  r    s    JIIIIIF u#6T#6R	
   t%)(<<<UY+BBB1~Gej+UZ-KL
 
 	
 	
 
 -T!Wd;;;'444 u&%*899 ?8+FF 	H H H H  
 &%*/00 UBBBBBB((00EHL4TTT5r[   mode_tytype[TorchFunctionMode]CCallable[[], _GeneratorContextManager[Optional[TorchFunctionMode]]]c                .     t           d fd            }|S )Nro   2Generator[Optional[TorchFunctionMode], None, None]c               3    K   ddl m} m}m} g }d } |             dk    r@ |            }t	          |          r|}n#|                    |            |             dk    @t          |          D ]} ||           	 |V  |\t          |          }|dk    r |            }|dz  }|dk    |                    |           t          |          D ]} ||           d S d S # |\t          |          }|dk    r |            }|dz  }|dk    |                    |           t          |          D ]} ||           w w xY w)Nr   )_len_torch_function_stack	_pop_moder3   r;   )torch.overridesr<  r=  r3   r   r  rq  r  )r<  r=  r3   temp_elementsremoved_modemoder6   r6  s          rY   context_manager_fnzB_make_temp_remove_mode_context_manager.<locals>.context_manager_fn  s     TTTTTTTTTT''))A--9;;D$(( +#$$T*** ('))A-- ]++ 	 	DJt	% 'M**aii$9;;DQJE aii $$\222$]33 % %DJt$$$$ ('% % 'M**aii$9;;DQJE aii $$\222$]33 % %DJt$$$$ (%s   >C$ $A!E)ro   r:  )r
   )r6  rB  s   ` rY   &_make_temp_remove_mode_context_managerrC    s6     % % % % % ^%@ r[   r  Union[Module, Callable]r*   concrete_argsOptional[tuple[Any, ...]]r(   c                   |                     | |          }dd}|                    |           ddlm}  ||           t	          | t
                    r| j        j        n| j        }t          j	        
                    |j        ||          S )	NrW  fx.Nodero   r   c                    ddl m} |                                 rdS  ||           rdS t          | j                            d          t                    r t          d | j        D                       rdS dS )Nr;   )is_accessor_nodeTFr   c              3     K   | ]K}t          |t          j                  t          |j                            d           t
                    V  LdS )r   N)r   rJ  r  r   rt  rF   )rd   r  s     rY   rt   z6dispatch_trace.<locals>.impure_pred.<locals>.<genexpr>+  s`        a))16::e,,l;;     r[   )	symbolic_shapesrJ  	is_impurer   r   rt  rF   r  r  )rW  rJ  s     rY   impure_predz#dispatch_trace.<locals>.impure_pred  s    555555 ;;== 	4 A 	5
 qvzz%((,77
	       	
	 5 tr[   r   )dedupe_symints)rW  rH  ro   r   )traceeliminate_dead_code,torch._inductor.fx_passes.dedupe_symint_usesrO  r   r0   r   r   rJ  _lazy_graph_module_make_graph_moduler  )r  r   rE  rr  rN  rO  r  s          rY   rC   rC     s     LL}--E   8 
k***KKKKKKN5&0v&>&>Q4>""DMD 33FKMMMr[   Callable[[Unpack[_Ts]], R]tensorstuple[Unpack[_Ts]]c                     t          j                  \  }t          j                   d fd            }|S )	Nproxiesr  _unusedr  ro   rR   c                    t          j        |           \  }}t          |          t          	          k    sJ t                      5 }t	          |t
                    sJ t          	|d            d d d            n# 1 swxY w Y   dfd} 
 }t          j        t          ||          }t          j        t          fd|          }dfd
}t          j        t          ||          }|S )Nr  rZ  r   ro   Union[Tensor, Proxy]c                *    t          | | d           S )Nc                    | j         S rU   rE  r   s    rY   rZ   zJwrap_key.<locals>.wrapped.<locals>.get_tensor_proxy_slot.<locals>.<lambda>O  s    !' r[   rd  rZ  r   s    rY   get_tensor_proxy_slotz8wrap_key.<locals>.wrapped.<locals>.get_tensor_proxy_slotN  s    !!VQ0A0ABBBr[   c                *    t          | | d           S )Nc                    | S rU   ra   r   s    rY   rZ   z=wrap_key.<locals>.wrapped.<locals>.<lambda>.<locals>.<lambda>T  s    q r[   rd  r_  s    rY   rZ   z+wrap_key.<locals>.wrapped.<locals>.<lambda>T  s    q&![[(Q(Q r[   r   r)   c                H    t          |                                           S rU   )r   rV  r_  s    rY   get_sym_proxy_slotz5wrap_key.<locals>.wrapped.<locals>.get_sym_proxy_slotW  s    !!V,,22444r[   )rZ  r   ro   r\  )rZ  r   ro   r)   )rb   r  r  r  r   rf  rO  tree_map_onlyr   r   rF   )rY  rZ  flat_proxies_proxies_specr  r`  r  rd  r  flat_tensorsrV  r   s           rY   wrappedzwrap_key.<locals>.wrappedF  sz   &,&9'&B&B#m<  C$5$55555(** 	Xaa!788888lL4PVWWWW	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X	C 	C 	C 	C 	C 	C ak"6+@#FF"QQQQSV
 
	5 	5 	5 	5 	5 	5 "<1CSII
s   	+B  BB)rY  r  rZ  r  ro   rR   )rb   r  r  wraps)r  rV  r   r  _tensors_specri  rh  s   ```   @rY   wrap_keyrl  >  sf     #)"5g">">L-_Q        , Nr[   zOptional[object]ORIGINAL_ATENc              #     K   t           Rt          j                    r?| a | t          j        d<   	 d V  d a d t          j        d<   d S # d a d t          j        d<   w xY wd V  d S )Noriginal_aten)rm  fx_tracebackhas_preserved_node_metacurrent_meta)re  s    rY   set_original_aten_oprs  d  s       !E!G!G59!/2	>EEE M9=L%o666 !M9=L%o6====s   A Ac                  $    e Zd ZddZ	 	 dddZdS )TorchFunctionMetadataModer   r   ro   r   c                    || _         d S rU   )r   r  r   s     rY   r  z"TorchFunctionMetadataMode.__init__t  s    r[   ra   Nre  r?   types tuple[torch._C._TensorMeta, ...]r  r  r  Optional[dict[str, object]]r   c                    |pi }|| j         _        | j         j                            |d          dz   | j         j        |<    ||i |S )Nr   r;   )r   r  r  rt  r  re  rx  r  r  s        rY   __torch_function__z,TorchFunctionMetadataMode.__torch_function__w  sW     2(,%,0K,G,K,KDRS,T,TWX,X#D)tT$V$$$r[   r   r   ro   r   ra   N
re  r?   rx  ry  r  r  r  rz  ro   r   r   r   r   r  r}  ra   r[   rY   ru  ru  s  sI            $&.2
% 
% 
% 
% 
% 
% 
%r[   ru  c                  $    e Zd ZddZ	 	 dddZdS )PreDispatchTorchFunctionModer   r   ro   r   c                "    || _         g | _        d S rU   )r   enter_autocast_nodesrw  s     rY   r  z%PreDispatchTorchFunctionMode.__init__  s     :<!!!r[   ra   Nre  Union[OpOverload, Callable]rx  ry  r  r  r  rz  r   c                   |pi }|t           v r|t          j        j        j        k    r| j                                        }|f}| j                            d||i           }|t          j        j        j	        k    r| j        
                    |           |t          j        j        t          j        j        j	        t          j        j        j        fv r
d |j        d<   |t          j        j        u r ||i | |S |t          j        j        j        t          j        j        j        t          j        j        j        t          j        j        j        t          j        j        j        fv rXt-          || j                  \  }}}| j                            d||i           }	 ||i |}
t1          |
|	d | j                   |
S  ||i |S )Nr+  r   r  )r,   r   ampautocast_mode_exit_autocastr  popr   r  _enter_autocastr  r4  _set_grad_enabledr   
_functorchpredispatch_add_batch_dim_remove_batch_dim_vmap_increment_nesting_vmap_decrement_nestingvmaplazy_load_decompositionsr  r,  rO  )r  re  rx  r  r  
enter_noder   r^   rY  	out_proxyr   s              rY   r}  z/PreDispatchTorchFunctionMode.__torch_function__  s    2DDD
 uy.===!6::<<
"};**?D$KKDuy.>>>)00666*	'7	'6  
 $(	%  ux111d%f%%%K
 (7(:(@(@!:
 
 
 At{SSMAw00	 I $'''Cc9tDKPPPPJtT$V$$$r[   r~  r  )
re  r  rx  ry  r  r  r  rz  ro   r   r  ra   r[   rY   r  r    sI        < < < < $&.23% 3% 3% 3% 3% 3% 3%r[   r  c                       e Zd Zed,d            Z	 	 	 d-d. fdZe	 	 d/d0d            Zd1 fdZd2 fd$Z	e
d,d%            Zd3d)Zd4d+Z xZS )5rf  ro   r   c                    dS r   ra   r  s    rY   enable_tracingz%ProxyTorchDispatchMode.enable_tracing      tr[   FTr   r   tracing_moder  r  r  r  r   c                :   |rt           j        j        j        nd }t	                                          |           || _        || _        || _        || _	        || _
        t           j        j        j        | _        g | _        d| _        ddlm} |j        | _        d S )Nr   config)r   r4  DispatchKeyPreDispatchr  r  r   r  r  r  r  _TorchDispatchModeKeyPROXY	_mode_keyenter_stackri  torch._inductorr  rj  )	r  r   r  r  r  r  dkr  r   s	           rY   r  zProxyTorchDispatchMode.__init__  s     2>GUX!--4(($8!,H) 7= DF"#******-3-K$$$r[   ra   Nre  r?   rx  ry  r  r  r  rz  r   c                    t          |          5  |pi }|t          j        j        fv r ||i |cd d d            S t	          | || j        ||          cd d d            S # 1 swxY w Y   d S rU   )rs  primr   r   r  r  r|  s        rY   __torch_dispatch__z)ProxyTorchDispatchMode.__torch_dispatch__  s     "$'' 	K 	K\rF+---tT,V,,		K 	K 	K 	K 	K 	K 	K 	K dD$*;T6JJ	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	Ks    A!A!!A%(A%r   c                    t          t          j        j        j                  }| j                            |           t                                                      S rU   )	r4   r   r4  r  r  r  r  r  	__enter__)r  maybe_prev_proxy_moder   s     rY   r  z ProxyTorchDispatchMode.__enter__   sG     1%(2P2V W W 5666ww  """r[   exc_typeOptional[type[BaseException]]	exc_valueOptional[BaseException]	tracebackOptional[types.TracebackType]Optional[bool]c                    t                                          |||          }| j                                        }|t	          |           |S rU   )r  __exit__r  r  r3   )r  r  r  r  bmb_previous_proxy_moder   s         rY   r  zProxyTorchDispatchMode.__exit__  sQ     GGXy)<< "&!1!5!5!7!7!--...r[   c                    dS r   ra   )clss    rY   is_infra_modez$ProxyTorchDispatchMode.is_infra_mode  r  r[   r  r   r)   c                |    t          |          dk    rEt          |d         t          t          f          r#t           fd|d         D                       f}nt           fd|D                       } j                            d||i           }t          j        | j                  }t          ||           |S )Nr;   r   c              3     K   | ]G}t          |t                    r,t          |j                                                  j        n|V  Hd S rU   r   rF   r   r   rV  r   rd   r  r  s     rY   rt   z8ProxyTorchDispatchMode._compute_proxy.<locals>.<genexpr>   sk          &a66q$+66<<>>CC	     r[   c              3     K   | ]G}t          |t                    r,t          |j                                                  j        n|V  Hd S rU   r  r  s     rY   rt   z8ProxyTorchDispatchMode._compute_proxy.<locals>.<genexpr>*  sk          "!\22N1dk2288::??	     r[   r+  )	r  r   rV   r]   r   r  rJ  r)   r  )r  re  r  r  n_argsn_outp_outs   `      rY   _compute_proxyz%ProxyTorchDispatchMode._compute_proxy  s    
 t99>>ja4-@@>     "!W    	FF          F ''vrJJ,,r[   r  c                   |t           j        k    r^t          |d         t                    r|d         dk    r|d         S t          |d         t                    r|d         dk    r|d         S |rJ  ||i |}t          |t                    r4t          | j        | j        |||          }t          || j        |           |S )Nr;   r   )re  r  r  )	operatormulr   rn   rF   r  r   r  r   )r  re  rx  r  r  r  p_out_thunks          rY   __sym_dispatch__z'ProxyTorchDispatchMode.__sym_dispatch__:  s     8<$q'3'' DGqLLAwDGS)) d1gllAw
 zdD#F## c<(( 	:"T0t$C  K 3[999
r[   ro   r   )FFT)r   r   r  r  r  r   r  r   r  r   ro   r   r  r  )ro   r   )r  r  r  r  r  r  ro   r  )re  r?   r  r  r  r   ro   r)   )
re  r?   rx  ry  r  r  r  r  ro   r   )r   r   r   propertyr  r  r6   r  r  r  classmethodr  r  r  r!  r"  s   @rY   rf  rf    s)          X #%*-1L L L L L L L6 
 $&.2K K K K UK# # # # # #         [   B       r[   c                  l     e Zd ZU ded<   ded<   ded<   ded<   d	ed
<   ded<   dZded<   d fdZ xZS )rL   r  r   z*MutableMapping[PySymType, _PySymProxyType]r   r  r   r  r   zOptional[OpOverload]r  r  r  Fr   r  rr  fx.graph.Graphro   r   c                    t                                          |           t          j                    | _        t                      | _        i | _        t          d t                    | _
        d | _        i | _        d S )Nr  )r  r  weakrefr   r   r:   r   r   r9   r8   r   r  r  )r  rr  r   s     rY   r  z _GraphAppendingTracerEx.__init__e  sx    &8::577"$%8&
 &
 &
" "&  "r[   )rr  r  ro   r   )r   r   r   r   r  r  r!  r"  s   @rY   rL   rL   \  s         FFFF????88882222++++****!O!!!!" " " " " " " " " "r[   rL   c                  T     e Zd Z	 dd fdZd fdZd fdZd fdZd fdZ xZS )rE   Nmodulefx.GraphModule	new_graphfx.Graphr{   r|   r  r   ro   r   c                     t                      j        |fi | || _        t          | j                  | _        |pi | _        t          | j        d          | _        d S )Nreal)r  )r  r  r  rL   r   r{   rf  rA  )r  r  r  r{   r  r   s        rY   r  z!DecompositionInterpreter.__init__w  sc     	**6***"-dn==#6#<" *4;VLLL			r[   r  r  r  r  r  c                    t                                          |||          }t          j        | j                            |          | j                  }t          ||d | j                   |S Nr  )r  r  rJ  r)   r  r   rO  r  r  r  r  r  r   r   s         rY   r  z$DecompositionInterpreter.placeholder  sa     gg!!&$7733F;;T[II#utDKHHHH
r[   c                    t                                          |||          }t          j        | j                            |          | j                  }t          ||d | j                   |S r  )r  r  rJ  r)   r  r   rO  r  s         rY   r  z!DecompositionInterpreter.get_attr  sa     ggvtV440088$+FF#utDKHHHH
r[   c                     t                                          |||          }d
dd fd	} j                            t          j        ||                     |S )Nrf   r   ro   r  c                    | j         j        S rU   )r   r   r   s    rY   get_proxy_nodez7DecompositionInterpreter.output.<locals>.get_proxy_node  s    7<r[   rB  r   Union[Tensor, fx.Node]c                2    t          | j        |           S rU   )r   r   )rB  r  r  s    rY   unwrapz/DecompositionInterpreter.output.<locals>.unwrap  s    !!T[!^DDDr[   )rf   r   ro   r  )rB  r   ro   r  )r  r  r  rb   tree_map)r  r  r  r  r  r  r  r   s   `     @rY   r  zDecompositionInterpreter.output  s     ggnnVT622	  	  	  	 	E 	E 	E 	E 	E 	E 	E 	fofc::;;;
r[   c                    t          | j                  5  | j        5   t                      j        |i |cd d d            cd d d            S # 1 swxY w Y   d d d            d S # 1 swxY w Y   d S rU   )r   r{   rA  r  run)r  r  r  r   s      rY   r  zDecompositionInterpreter.run  s    t/00 	0 	0$) 	0 	0577;///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s3   A(AA(A	A(A	A((A,/A,rU   )
r  r  r  r  r{   r|   r  r   ro   r   )r  r  r  r  r  r  ro   r   )r  r   r  r   ro   r   )	r   r   r   r  r  r  r  r  r!  r"  s   @rY   rE   rE   v  s        
 HL	M M M M M M M
 
 
 
 
 
	 	 	 	 	 	     "0 0 0 0 0 0 0 0 0 0r[   rE   Callable[..., R]0tuple[Callable[[list[object]], R], list[object]]c                N     t          j        ||f          \  }d fd}||fS )N	flat_argslist[object]ro   rR   c                D    t          j        |           \  }} |i |S rU   )rb   r  )r  fn_args	fn_kwargsre  r  s      rY   ri  z-wrapper_and_args_for_make_fx.<locals>.wrapped  s/    #29dCCtW*	***r[   )r  r  ro   rR   )rb   r  )re  r  r  r  ri  r  s   `    @rY   wrapper_and_args_for_make_fxr    sN    
 )4.99OIt+ + + + + + + Ir[   c               #     K   t          j                    } t          j        d           	 d V  t          j        |            d S # t          j        |            w xY wNF)r   is_autocast_cache_enabledset_autocast_cache_enabled)	old_values    rY   disable_autocast_cacher    sa      /11I	$U+++4(33333(3333s   A Ac                      e Zd ZdS )#_ModuleNotInstalledAsSubmoduleErrorNr   ra   r[   rY   r  r    r   r[   r  c                      e Zd Zd	dZdS )

_AttrProxybaser0   pathr  ro   r   c                    d S rU   ra   )r  r  r  s      rY   reset_proxy_mappingz_AttrProxy.reset_proxy_mapping  s    r[   N)r  r0   r  r  ro   r   )r   r   r   r  ra   r[   rY   r  r    s(             r[   r  c                  `     e Zd ZdZd$ fdZd%d
Zd& fdZd' fdZd(dZd)d!Z	d* fd#Z
 xZS )+_ModuleStackTracera  Customized version of PythonKeyTracer that retains module stack
    information in node.meta["nn_module_stack"].

    FX symbolic trace actually does this already, but it relies on `self.root`
    being the actual module being traced. Since make_fx traces a lambda of our
    creation, things don't work properly.

    So for this version we hold onto a reference to the original module
    (scope_root) and use that to match the path. Also when we see,
            A
           / \
          B   C
           \ /
            D
    we want to record the path as A.B.D by recording only one path.
    See Note [Preserving the nn module stack metadata during export non-strict mode]  # noqa: W605
    
scope_rootr(   ro   r   c                   t                                                       d| _        d| _        || _        d| _        i | _        | j                            d          D ]G\  }}|| j        v r/t          	                    d| j        |         |           d| _        =|| j        |<   Ht                      | _        t                      | _        t                      | _        d| _        t          t                     | _        | j                            d          D ]2\  }}| j        t%          |                                       |           3|  G fddt(                    | _        d S )NTF)remove_duplicatez<Shared module found between %s and %s, AttrProxy is enabled.r   c                  l     e Zd ZdfdZd fd
ZdfdZd fdZedfd            Z xZ	S )._ModuleStackTracer.__init__.<locals>.AttrProxyr  Union[Module, _AttrProxy]r  r  ro   r   c                x   t          |t                    r|                                }t          |t                    sJ t	          |j        j        | j        |j        fi           | _        |j        | _        |j        j        | j        _        |j        j	        | j        _	        |j
        | <   |j        | <   d S rU   )r   r  get_baser0   r   r   r   __dict__r   r   proxy_pathsproxy_modules)r  r  r  r   s      rY   r  z7_ModuleStackTracer.__init__.<locals>.AttrProxy.__init__  s    dJ// +==??D!$///// "&N+^T^4" "
 !%,0N,E).2n.I+ ,0"4(-1$T***r[   r  	AttrProxyc                    t          | t                    sJ t                                          |          }t          |t                    s|S  |j        |          dz   |z             S N.)r   r0   r  __getattr__r  )r  r  r  r	  r   r   s      rY   r  z:_ModuleStackTracer.__init__.<locals>.AttrProxy.__getattr__!  sm    !$///// !77..t44!(F33 $#O y6+=d+Cc+ID+PQQQr[   r0   c                    j         |          S rU   )r  rw  s    rY   r  z7_ModuleStackTracer.__init__.<locals>.AttrProxy.get_base,  s    +D11r[   rG  Union[int, slice]c                   t          |t                    rt          | t          j        j                  rrt          j                            t          t          | j                                                  |                             } |j	        |           d|           S t          | t          j        j
                  ret          j        
                    t          | j                                                  |                   } |j	        |           d|           S t                                          |          S r  )r   slicer   r  
Sequentialr   rV   _modulesr   r  
ModuleListvaluesr  r  )r  rG  r   r	  r   r   s      rY   r  z:_ModuleStackTracer.__init__.<locals>.AttrProxy.__getitem__/  s   c5)) 
S!$(;<< 	S#h11'T]-@-@-B-B(C(CC(HII   )y1CD1I.Q.QC.Q.QRRR#D%(*=>> S#h11$t}7K7K7M7M2N2Ns2STT(y1CD1I.Q.QC.Q.QRRRww**3///r[   dict[str, AttrProxy]c                     d j         v sJ  j         d         }t          |t                    sJ  fd|                                D             S )Nr  c           
     t    i | ]4\  }}||* |j                  dz   t          |          z             n|5S r  )r  r  )rd   rL  r   r	  r  r   s      rY   r   zK_ModuleStackTracer.__init__.<locals>.AttrProxy._modules.<locals>.<dictcomp>C  sd        #U  , "	%);D)AC)G#c(()RSSS"	  r[   )r  r   r   r   )r  
submodulesr	  r   s   ` rY   r  z7_ModuleStackTracer.__init__.<locals>.AttrProxy._modules>  s|    !T]2222!]:6
!*d33333      '1&6&6&8&8   r[   )r  r  r  r  ro   r   )r  r  ro   r	  )ro   r0   )rG  r  ro   r	  )ro   r  )
r   r   r   r  r  r  r  r  r  r!  )r   r	  r   s   @rY   r	  r    s        2 2 2 2 2 2*	R 	R 	R 	R 	R 	R 	R 	R2 2 2 2 2 20 0 0 0 0 0 0 0       X    r[   r	  )r  r  record_stack_traces!_record_forward_stack_traces_onlyr  enable_attr_proxysubmodule_pathsnamed_modulesr   infor   r  attr_proxy_mapr  counterr   rV   module_id_cacher   r  r  
proxy_type)r  r  r  r  modr	  r   r   s        @@rY   r  z_ModuleStackTracer.__init__  s   #' 15.$!&!44e4LL 		/ 		/GD!D(((R(+  
 *.&&*.$Q''?P?R?REVEXEXDUDWDW*400666NN 	7 	7ID# C)006666
 ?	 ?	 ?	 ?	 ?	 ?	 ?	 ?	
 ?	 ?	 ?	B $r[   r$  r0   r  c                    || j         u rdS t          |t                    r| j        |         S 	 t	          j        | |          S # t          $ r}t          |d}~ww xY w)z
        Use tracked access path during tracing instead of the default BFS behavior.
        Still use all the possible module paths to verify the result.
         N)r  r   r  r  r*   path_of_module	NameErrorr  )r  r$  rB  s      rY   r'  z!_ModuleStackTracer.path_of_moduleN  su    
 $/!!2c:&& 	)#C((	=(s333 	= 	= 	=51<	=s   A 
AAAr  r  r   r  r  c                   t          |t                    r!t          |t          j                  s| j        s#t                                          |||          S t          |t                    r|S || j        vr| 	                    ||          | j        |<   n!| j        |         
                    ||           | j        |         S rU   )r   r0   rJ  r(   r  r  r  r  r   r#  r  )r  r  r  r  r   s       rY   r  z_ModuleStackTracer.getattr^  s     8V,,	J(BN33	J )	J
 77??43HIIIh
++ 	O 4...,0OOHd,K,KD)))==hMMM"8,,r[   r  rD  rE  rz  r  c                    t                                          ||          }t                                           | j                                        D ]&\  }}|j        j        t          t          |          <   'g }| j	        
                                D ]%\  }}|| j        v r|                    ||f           &dd}	|D ]9\  }
} |	| j	        |
           | j        |         }t          || j	        |
           :|S )	Nr   r0   r  r  ro   r   c                   |                     d          }|d d         |d         }}t          | t                    sJ | }|D ]D}t          ||          s dS t	          ||          }t          |t
          t          f          s dS Et          ||          sdS t          t	          ||          t
                    sdS t          ||           dS )Nr  FT)splitr   r0   r   r  r  delattr)r   r  atomsr  target_submodr$  r  s          rY   _delete_proxy_attrz4_ModuleStackTracer.trace.<locals>._delete_proxy_attr  s     LL%%E"'*eBi-Dc6*****C  ! !sD)) ! 55c4((!#
F';<< ! 55! 3.. u gc=99:FF uC'''4r[   )r   r0   r  r  ro   r   )r  rP  r   clearr   r   r   r   r   r  r  r  r  r+   )r  r  rE  r   rL  r   !proxy_module_names_to_be_replacedr  r  r1  proxy_module_nameproxy_moduleactual_moduler   s                rY   rP  z_ModuleStackTracer.traceq  s.    ggmmD-00  	055777+1133 	N 	NHC?By~3BsGG<< KM) I3355 	I 	ILD&+++188$HHH	 	 	 	8 0Q 	F 	F+|ty*;<<< .|<M	3DEEEE
r[   r  r  r   r  r  r  r  c                    ddl m} t          ||t          f          r ||i |S 	 t	          j        | ||||          S # t          $ r3 t                              dt          |                      ||i |cY S w xY w)zlPythonKeyTracer overrides call_module to avoid the scope handling,
        but we actually want it.
        r   )OptimizedModulezUnable to find the path of the module %s. This might be because the module was not properly registered as a submodule, which is not good practice. We will trace through the module without recording stack information.)
torch._dynamor8  r   r(   r*   r  r  r   r   r  )r  r  r  r  r  r8  s         rY   r  z_ModuleStackTracer.call_module  s     	211111 a/;788 	,7D+F+++
	,%dAwfEEE2 	, 	, 	,IIJ A   7D+F+++++	,s   ? :A<;A<module_qualified_namer   c                    dS r  ra   )r  r  r:  s      rY   is_leaf_modulez!_ModuleStackTracer.is_leaf_module  s    ur[   r  c                &    t                      j        |i |}|j        dvrd|j        vr!| j                                        |j        d<   |j        d                                         D ]?\  }\  }}t          |t                    r"||j	        dz   |j
        z   f|j        d         |<   @|j        dk    rY| j        Rd|j        vrI| j        j         d| j        | j                  | j        j        j         d| j        j         f|j        d<   |S )aF  
        Create node and add on metadata.
        Add nn_module_stack here instead of TracerBase,
        since calls to make_fx() might not want to record module stack metadata.
        Add torch_fn by looking at torch_fn_metadata and torch_fn_counts.
        Add stack_trace by filtering out forward() stack frames.
        r  nn_module_stackr  r+  Ntorch_fnr^   )r  r  r  r   module_stackcopyr   r   r   r   r   r  r   r  r   )r  r  r  r   rL  fqnmod_clsr   s          rY   r  z_ModuleStackTracer.create_node  s@    #uww"D3F33 7333 	11/3/@/E/E/G/G	+,'+y1B'C'I'I'K'K  #^c7gt,, *S073GG9DI/05 G&&&2$)++ )2ccT5I$J`5acc)3<``t?U?^``%DIj!
 r[   )r  r(   ro   r   )r$  r0   ro   r  r   )r  rD  rE  rz  ro   r  )
r  r0   r  r   r  r  r  r  ro   r   )r  r0   r:  r  ro   r   )r  r   r  r   ro   r  )r   r   r   r  r  r'  r  rP  r  r<  r  r!  r"  s   @rY   r  r    s         $a$ a$ a$ a$ a$ a$F= = = = - - - - - -&C C C C C CJ ,  ,  ,  ,D   ! ! ! ! ! ! ! ! ! !r[   r  c                      e Zd Z	 	 d3d4dZd5dZd6d!Zed7d'            Zd8d*Zed9d+            Z	d:d.Z
d;d0Zd<d1Zd:d2ZdS )=_MakefxTracerFNr{   r|   r  r  _allow_non_fake_inputsr   r  record_module_stackr  r  r  parent_tracerOptional[_MakefxTracer]ro   r   c
                   t          |pi           | _        | j                            t          j        j        j        j        t          j        j	        j                   || _
        || _        || _        || _        || _        || _        d | _        t#                      | _        t#                      | _        d | _        t#                      | _        t#                      | _        || _        |	| _        d S rU   )r   r{   
setdefaultr   r-  r.  r4  r   _decompdecompositionsr  rF  r  rG  r  r  r  r   r  proxy_function_mode	fx_tracerpython_dispatcher_modetorch_fn_metadata_moder  rH  )
r  r{   r  rF  r  rG  r  r  r  rH  s
             rY   r  z_MakefxTracer.__init__   s     @D%2@
 @
  	 ++IN$,em.J.T	
 	
 	
 ".,B#".)< *>!2N) ;?FQmmMM 	  59?J}}#MM 	# $7 6Cr[   	list[Any]c                N    | j         | j        | j        | j        | j        | j        gS rU   r  r  rN  rO  rP  rQ  r  s    rY   _checkpoint_modesz_MakefxTracer._checkpoint_modes,  s.    !O$N''
 	
r[   prev_fake_tensor_modeOptional[FakeTensorMode]prev_proxy_mode*Union[nullcontext, ProxyTorchDispatchMode]prev_proxy_function_mode0Union[nullcontext, PreDispatchTorchFunctionMode]prev_fx_tracerOptional[PythonKeyTracer]prev_python_dispatcher_modeUnion[nullcontext, Any]prev_torch_fn_metadata_mode-Union[nullcontext, TorchFunctionMetadataMode]c                Z    || _         || _        || _        || _        || _        || _        d S rU   rT  )r  rV  rX  rZ  r\  r^  r`  s          rY   _restore_modesz_MakefxTracer._restore_modes6  s8     !6)#; '&A#&A###r[   r  r   r  r  r   c              #  &  K   |                                  }	 ddlm} t          |d          r#| j        r|j        }t          |          | _        n7t                      | _        | j	        | j        _	        | j	        rd| j        _
        | j        dk    rdd l}|j        j                            |          }|Wdd lmc m} |                    d          5  t)          d| j         |            d	          }d d d            n# 1 swxY w Y   || _        n| j        d
k    rdd l}|j        j                            |          }|X |            }	dd lmc m} |                    d          5  t)          d| j        |	          }d d d            n# 1 swxY w Y   |j        
J d            || _        n"| j        dk    st1          d| j                   |                     | j                   d V   | j        |  d S #  | j        |  w xY w)Nr;   )ShapeEnv	_orig_modTfaker   F)(fake_tensor_allow_unsafe_data_ptr_access)r   allow_non_fake_inputs	shape_envstatic_shapessymbolic)r   ri  rj  2shape_env should be set if tracing with 'symbolic'r  zUnexpected tracing type: )rU  rL  re  r   rG  rf  r  rO  rB   r  r  r  r9  _dynamoutilsr   torch._functorch.configr  r  patchr$   rF  r  rj  AssertionError_construct_modes_with_fx_tracerrc  )
r  r  r  
prev_modesre  r  r   r  _configrj  s
             rY   _init_modes_from_inputsz%_MakefxTracer._init_modes_from_inputsF  s      ++--
9	-111111q+&& 	L4+C 	L[
 "4J!?!?!0!2!2595M2+ LGKDND F**$$$$#(=#6#G#G#M#M #+========= PUVV  +937262M&.hjj*.	, , ,(               )9%%"j00$$$$#(=#6#G#G#M#M #+ (

I========= PUVV  +938262M&/, , ,(               (1==H >== )9%%(F22(GD4EGG   00@@@EEED,,,,D,,,sV   B?H !D8H DH DA$H 0FH FH FAH HrO  r   c                   t          || j        | j        | j        | j                  | _        | j        rt          |          | _        | j        dk    s| j        rt                      | _	        t          |          | _        d S )N)r  r  r  rl  )rf  r  r  r  r  r  r  rN  r   rP  ru  rQ  )r  rO  s     rY   rs  z-_MakefxTracer._construct_modes_with_fx_tracer  s    0*!%!:)-)J
 
 
  	O'CI'N'ND$ 
**d.?**B*D*DD'&?	&J&J###r[   c              #     K   |                                  }	 |j        | _        dd}|j        J  ||j                  | _        |                     | j                   d V   | j        |  d S #  | j        |  w xY w)NrH  r   ro   rB   c                    t          |           t          k    rt                      S t          |           t          k    rt          | j                  S t	          dt          |            d          )NzUnexpected tracer type: r  )r   rB   r  r  r   rH  s    rY   _create_sub_fx_tracerzD_MakefxTracer._init_modes_from_parent.<locals>._create_sub_fx_tracer  sn    &&/99*,,,-((,>>>-m.FGGG&I43F3FIII  r[   )rH  r   ro   rB   )rU  r  rO  rs  rc  )r  rH  rt  r{  s       rY   _init_modes_from_parentz%_MakefxTracer._init_modes_from_parent  s       ++--
	-$1$BD!    !*66622=3JKKDN00@@@EEED,,,,D,,,s   AA0 0A<r   r(   c           	         dd l t          j        fd|          }d fd}dd} ||          } |||          }t          j        t
           j                  }t                      5 }|                    t           j
                              j        r|                     j                   |                     j                   |                     j                   |                     j                   |                    |           |                    t                                 |                    t!                                 j        J 	 t%          t'          || j         j                   j        t+          |                    }	n%# t,          $ r t/          dd  fd            w xY w	 d d d            n# 1 swxY w Y                                    rOj                            |          x}
r3|
j        ,ddlm}  ||	|
j        d           |	                                  j        dk    r j        J  j        j        |	_        |	S )Nr   c                &    j         j        j        S rU   )rJ  _symbolic_tracePH)r^   r   s    rY   rZ   z,_MakefxTracer._trace_inner.<locals>.<lambda>  s    (@(C r[   r  rO   ro   c                h    ddfd}d ||d}t          j        |j                 |           S )	Nr   rf   r   ro   c                Z   ddl m} j        J  |d           }t          | t                    r!dz  j                            | |          S t          |           t          u rcj        dk    rXj        j	        
J d            j        j	        
                    j        j	                            | |d           | |	          S t          | j                  r%j        j                            j        |           S t          | t                     rJ d
|  d            | S )Nr   )ConstantSourceinputr;   )sourcerl  rm  )positive)hintr  zScriptObject z. has been fakified. Cannot wrap_fake it again.)torch._dynamo.sourcer  r  r   r   from_tensorr   rn   r  rj  create_symintnodecreate_symbolScriptObject_libraryfake_class_registrymaybe_to_fake_objr    )rf   r  r  	arg_countr  r   s      rY   inner_wrap_fakezG_MakefxTracer._trace_inner.<locals>._wrap_fake.<locals>.inner_wrap_fake  s|    @?????,888'(;	(;(;<<a(( NI0<<Qv<NNN!WW^^(9Z(G(G0:FFL GFF  0:LL-7EEv F   % M     5#566  >=OO-q   &a)9::  UAUUU : r[   c                    | S rU   ra   r   s    rY   rZ   z@_MakefxTracer._trace_inner.<locals>._wrap_fake.<locals>.<lambda>  s    ! r[   r  rg  rl  )rf   r   ro   r   )rb   r  r  )r  r  wrap_fn_mapr  r  r   s      @rY   
_wrap_fakez._MakefxTracer._trace_inner.<locals>._wrap_fake  sg    I! ! ! ! ! ! ! !H $'+ K
 ?;t/@#A4HHHr[   r  rl   phsSequence[PHBase]c                    t          t          j        |           d          r+t          j        |           j        j        t          j        z  rt          | t          |                    S | S )N__code__)r   inspectr  r  co_flags
CO_VARARGSrz   r  )r  r  s     rY   
_wrap_funcz._MakefxTracer._trace_inner.<locals>._wrap_func  s[    GN1--z::3>!$$-69KK3 &aS222Hr[   )r   rE  artifactc                     dddS )Nmake_fx_fail_partialstring)r  encodingra   ra   r[   rY   rZ   z,_MakefxTracer._trace_inner.<locals>.<lambda>	  s     6$,) ) r[   c                 T     j         j                            dddd          j        S )Nr  T)root_moduleverboseinclude_strideinclude_device)rO  rr  python_codesrcr  s   rY   rZ   z,_MakefxTracer._trace_inner.<locals>.<lambda>	  s6    t~';'G'G$* $'+'+	 (H ( (
  r[   )metadata_fn
payload_fn)insert_deferred_runtime_assertsreenter_make_fxrl  )r  rO   ro   rO   )r  rl   r  r  ro   rl   ) r9  rb   r  r   r   rf  r  r   enter_contextr   r{   r  rP  rN  rQ  r  _set_make_fx_tracerrO  rC   rl  r  r]   	Exceptionr!   is_hop_subgraph_tracer_guardsr   rj  torch.fx.passes.runtime_assertr  	recompiler  )r  r  r  r  r  r  re  r  stackrZ  r   r  r   s   `           @rY   _trace_innerz_MakefxTracer._trace_inner  s*    	oCCCCTJJ+	I +	I +	I +	I +	I +	I +	IZ	 	 	 	 z$z!S!! .4["DO.
 .

 [[  	E	$*B C CDDD$ ;##D$9::: ;<<< 8999 ;<<<
+++ 6 8 8999 3D 9 9:::>---"T49JKK>"'**  
     ! !             	  	  	  	  	  	  	  	  	  	  	  	  	  	  	F ''))	#m<<TBBB	 #/VVVVVV++Ay/BDUVVVKKMMM
**(444/9AKs+   .C8G'>F&%G&"GGGGr  c                ~    |                      ||          5   | j        |g|R  cd d d            S # 1 swxY w Y   d S rU   )rv  r  )r  r  r  s      rY   rP  z_MakefxTracer.trace2	  s    ))!T22 	/ 	/$4$Q....	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/s   266c                    | j         d uS rU   rz  r  s    rY   r  z$_MakefxTracer.is_hop_subgraph_tracer6	  s    !--r[   c           
         t          | j        d| j        | j        | j        | j        | j        |           }|                    |           5   |j        |g|R  cd d d            S # 1 swxY w Y   d S )Nr  rz  )	rE  r{   rF  r  rG  r  r  r|  r  )r  r  r  
sub_tracers       rY   trace_subgraphz_MakefxTracer.trace_subgraph9	  s    "$'$%-	
 	
 	

 //55 	5 	5*:*14t444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5s   A&&A*-A*)FN)r{   r|   r  r  rF  r   r  r   rG  r   r  r   r  r   r  r   rH  rI  ro   r   )ro   rR  )rV  rW  rX  rY  rZ  r[  r\  r]  r^  r_  r`  ra  ro   r   )r  r   r  r  ro   r   )rO  r   ro   r   )rH  rE  ro   r   )r  r   r  r   ro   r(   )r  r   r  r   ro   r  r  )r   r   r   r  rU  rc  r
   rv  rs  r|  r  rP  r  r  ra   r[   rY   rE  rE    s        %*15*D *D *D *D *DX
 
 
 
B B B B  =- =- =- ^=-~K K K K& - - - ^-6{ { { {z/ / / /. . . .5 5 5 5 5 5r[   rE  rI  _CURRENT_MAKE_FX_TRACERc              #  8   K   t           }	 | a d V  |a d S # |a w xY wrU   )r  )r   prev_tracers     rY   r  r  L	  s?       *K."("-+----s    r  )r  rG  r  r  r  r  r  rF  rG  r  r  r  Callable[..., GraphModule]c                    |dv sJ ddl m}	 t          ||||||||p|	j        j        dk              t          j                   d fd
            }
|
S )a=  
    Given a function f, return a new function which when executed with valid
    arguments to f, returns an FX GraphModule representing the set of operations that
    were executed during the course of execution.

    If record_stack_traces is True, the stack trace will be preserved on node.meta["stack_trace"]
    r  r   r  r;   )r  r  r   ro   r(   c                       j         g| R  S rU   )rP  )r  r  make_fx_tracers    rY   ri  zmake_fx.<locals>.wrapped{	  s    #~#A-----r[   )r  r   ro   r(   )r  r  rE  rP  provenance_tracking_levelr  rj  )r  r{   r  rF  r  rG  r  r  r  r  ri  r  s   `          @rY   rD   rD   W	  s    ( 77777&&&&&&"$/ 7<1Q6
 
 
N _Q. . . . . . . Nr[   list[TorchDispatchMode]c                 H    t           j        j                                        S rU   )r   ro  _python_dispatch _get_current_dispatch_mode_stackra   r[   rY   get_torch_dispatch_modesr  	  s    ;'HHJJJr[    Optional[ProxyTorchDispatchMode]c                     t                      S rU   )rH   ra   r[   rY   rG   rG   	  s    r[   c                     t           j                            t           j        j        j                  } t           j                            t           j        j        j                  }| |J d|  d|             | p|S )z
    Current the currently active proxy tracing mode, or None if
    we are not currently tracing.  This includes pre-dispatch proxy
    tracing.
    Nzpre_dispatch_mode=z, mode=)r   rk  _get_dispatch_mode_pre_dispatchr4  r  r  _get_dispatch_mode)pre_dispatch_moderA  s     rY   rH   rH   	  s}     
BB&,  8&&ux'E'KLLD$=.==t== )54 $$r[   c                    t                      }|sJ t                      5  g }|                    | |||          cddd           S # 1 swxY w Y   dS )z
    Call into the currently active proxy tracing mode to do a
    SymInt/SymFloat/SymBool dispatch trace on a function that operates on
    these arguments.
    N)rH   r  r  )re  r  r  rA  rx  s        rY   rI   rI   	  s     DKK4 
%	&	& @ @$$T5$??@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @s   AAA-Generator[ProxyTorchDispatchMode, None, None]c                 H    t          t          j        j        j                  S rU   )r2   r   r4  r  r  ra   r[   rY   r  r  	  s    ux=CDDDr[   r  c                   ddl m} t          v ro|                    ddfd          rt          S | 5  | xj        dz  c_        t                   |i |}| xj        dz  c_        |cd d d            S # 1 swxY w Y   t          S )Nr   )CompilerBisectoraot_eager_decomp_partitiondecompositionc                 "    t                     S rU   )repr)r  s   rY   rZ   z%maybe_handle_decomp.<locals>.<lambda>	  s    488 r[   r;   )!torch._inductor.compiler_bisectorr  rN   disable_subsystemr  ri  )r  r  r  r  r  r  s    `    rY   r  r  	  s     CBBBBB	(((--(/;K;K;K;K
 
 	" "! 	 	$$)$$-b14B6BBC$$)$$		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 s   4A55A9<A9c                    t          | ||          \  }}t                      5   t          |||          |          }ddd           n# 1 swxY w Y   |S )a  A helper function used to get the GraphModule for the given func.

    It's expected to be used in the ProxyTensor tracing context.
    It detaches the args and kwargs from the current tracer so that the trace of
    the current graph module can be created without any side-effects.
    )r{   r  N)r  r  rD   )re  r  r  r  r{   ri  all_argsgms           rY   get_isolated_graphmoduler  	  s     5T4HHGX	$	&	&  
W)<<
 
 

                Is   AAAr  r  c                   ddl m} t          j                            t          j        j        j                  }|rJ|j        rE ||j        |           x}r4t          |t                    s
J |            ||j
        j        d<   dS dS dS dS )zOA helper function for setting up unbacked_bindings on the destination FX graph.r;   )compute_unbacked_bindingsunbacked_bindingsN)rL  r  r   r4  r  r  FAKErj  r   r)   r   r   )r  r  r  r   symbol_to_paths        rY   rN  rN  	  s    :::::: ++EH,J,OPPI FY( F66y7JCPPP> 	Fi//::::/7EIN 3444F F F F	F 	Fr[   )rk   rl   rm   rn   ro   rl   )r{   r|   ro   r}   )r   r   ro   r   )r   r   r   r   r   r   ro   r   )r   r   r   r   r   r)   ro   r   )r   r   r   r   r   r   ro   r   r  )ro   r   )r   r   r   r   r   r   ro   r   )r   r   r   r   ro   r   )r   r   r   r   ro   r   )r   r   r   r   r   rP   ro   r   )
r   r   r   r   r   rP   r   r   ro   r   )r   r   r   r   ro   r)   )r   r   r   r   r   rP   ro   r   )
r   r   r   r   r   rP   r   r   ro   r   )r   r   r   r   ro   r   )r   r   r   r   r   rO   ro   r   )
r   r   r   r   r   rP   r   r   ro   r   )
r   r   r   r   r   r   r   r   ro   r   )F)r   r   r   r   ro   r   )r   r   r   r   ro   r   )r   r   r  r   ro   r   )r   r)   r   r   ro   r)   )
r   r   r  rl   r  r  r  r  ro   r  )
r   r   r   r)   r!  r   r   r   ro   r   )
r>  rO   r?  r<  r!  r@  r   r   ro   rO   )r   r   ro   rQ  )r   r   rZ  r   ro   r[  )r   r   rZ  r   ro   r_  )r   r   rZ  r   ro   ra  )r   r   rZ  r   ro   r   )re  r   r  rf  ro   r   )ry  rz  r   r   ro   r{  )r  rf  re  r?   r  r   r  r  r  r  ro   r   rU   )r  r   r#  r$  ro   r   )r6  r7  ro   r8  )r  rD  r   r*   rE  rF  ro   r(   )
r  rU  rV  rW  r   r   r  r   ro   rl   )re  r?   ro   r   )re  r  r  r  r  r  ro   r  )r   rE  ro   r   )Nr  F)r  r   r{   r|   r  r  rF  r   r  r   rG  r   r  r   r  r   r  r   ro   r  )ro   r  )ro   r  )re  rl   r  r  r  r  ro   rR   )ro   r  )
r  rf  r  r?   r  r  r  r  ro   r   )r  N)re  r   r  r  r  r  r  r  r{   r|   ro   r(   )r  r   r  r<  ro   r   )
__future__r   r  r  loggingr  	threadingr   r  r  collectionsr   r   collections.abcr   r   r   
contextlibr	   r
   r   r   dataclassesr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   
torch._opstorch.fxrJ  torch.fx.tracebackr  rp  torch.utils._pytreero  _pytreerb   r   r   r   torch._dispatch.pythonr   "torch._library.fake_class_registryr    torch._loggingr!   torch._subclasses.fake_implsr"   torch._subclasses.fake_tensorr#   r$   r%   r&   torch._subclasses.meta_utilsr'   r(   r)   r*   torch.fx.graph_moduler+   torch.fx.noder,   r-   r.   torch.fx.passes.shape_propr/   torch.nnr0   r>  r1   torch.utils._python_dispatchr2   r3   r4   r5   torch.utils._statsr6   torch.utils._thunkr7   torch.utils.weakr8   r9   r:   _backward_stater<   sym_noder=   rx  r>   r   r?   torch.fx._symbolic_tracer@   torch.typesrA   __all__r   r  r   r   r-  r.  r  	getLoggerr   r   _logginggetArtifactLoggerr  rN   r   r  rO   rP   rQ   rR   rS   r   null_ctx_typeregister_pytree_nodeSizer]   !_pytree_subclasses_that_lose_inforz   r   r   
proxy_slotr   
no_defaultrF   r   r   r   r   localr   r   r   r   r   r   r   r   r   rV   r   r  rn   r   r   r   r   r	  rK   rJ   r  r  r;  r<  r=  rO  r   rY  r^  r  r  r  rx  r  r  r  rB   r  rC  _disable_dynamorC   rl  rm  rs  ru  )_temp_remove_metadata_torch_function_moder  -_temp_remove_pre_dispatch_torch_function_moderf  r   GraphAppendingTracerrL   InterpreterrE   r  r  r(  r  r  r  rE  r  r  rD   r  rG   rH   rI   r  r  r  rN  ra   r[   rY   <module>r     s   # " " " " " "                  0 0 0 0 0 0 0 0 8 8 8 8 8 8 8 8 8 8 W W W W W W W W W W W W ! ! ! ! ! !	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 Q P P P P P P P P P P P P P % % % % % %            ) ) ) ) ) ) ) ) ) $ $ $ $ $ $ $ $ $ ) ) ) ) ) ) ) ) ) ) ; ; ; ; ; ; ? ? ? ? ? ? + + + + + + 4 4 4 4 4 4            7 6 6 6 6 6 / / / / / / / / / / . . . . . .         
 @ ? ? ? ? ?       - - - - - -            % $ $ $ $ $ $ $ $ $ $ $ W W W W W W W W W W * * * * * *        (LLL......LLL%%%%%%//////''''''   AB&(89 U/1AAB y~y~g!!n66xARSS =?  ? ? ? ? GCLLGCLLYt__GCLLl5[!!  	J  &	 	 	 	  &+Z$7 !B B B B 	> 	> 	> 	> VXX
	 	 	 	 	 	 	 	 Z\\
 / / / / / / / /    x   M M M M
 
 W W W 
 W 
   

 
   

    )/    &A%@%B%B " EG ' F F F F4 4 4 4 )> )> )> )>X+A +A +A +A\D D D D
 , 
   
 
! ! ! 
! 
   
 
   
 
   
 
   
 
   
 
$ $ $ 
$ 
   
 !%+	    >     	$%S##$		 %( %( %( %( %(P ,0% % % % % %$        0          FB FB FB FBR 	8O$gfo.E&FF 
H%&8H0H(II
T T T Tn        
   * 
& & & 
&
 
- - - 
-
 
, , , 
,
( ( ( ( +Z8= = = =4(K (K (K (KVA A A AH' ' ' ' ' ' ' '>v v v v vf v v vv OS) ) ) ) )X$ $ $ $N  04)N )N )N )N )NX   F #' & & & &    % % % % % 1 % % %" -S,R- - );% ;% ;% ;% ;%#4 ;% ;% ;%| 1W0V 1 1 -
J J J J J. J J JZ" " " " "bh; " " "4=0 =0 =0 =0 =0r~ =0 =0 =0@    4 4 4 4	 	 	 	 	) 	 	 	
       
d d d d d d d dN	G5 G5 G5 G5 G5 G5 G5 G5T
 48  7 7 7 7 . . . . DH#(	(  %!&)- %( ( ( ( ( (VK K K K   % % % % @ @ @ @( E E E E   6 CG    ,F F F F F Fr[   