
    qi`                   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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$ 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< d dl=m>Z> d dl?m@Z@mAZAmBZBmCZC d dlDmEZE d dl(mFZFmGZGmHZH d dlImJZJ d dlKmLZLmMZMmNZN d dlOmPZP d dlQmRZR d dlSmTZT d dlUmVZVmWZWmXZXmYZYmZZZ d dl[m\Z\ d dl]m^Z^ d dl_m`Z`maZambZb ddlcmdZd ddlemfZf er$d dlgZgd d lmhZh d dliZid d!l'mjZj d d"lkmlZl d d#lmmnZnmoZompZp g d$Zqe d%   Zre&j                  e7fZte e&j                  e7f   Zue&j                  j                  Zwe&j                  j                  Zx ej                  ez      Z{e&j                  j                  ezd&      Z~i ad'ed(<   dZ ed)      Z ed*      Z e!d+      Z ed,      Z e#d-      Z ee      Z e0j                  e&j                  d. d/ d0 d12       e&j                  eiZdd3Ze	 	 	 	 dd4       Z e       Z G d5 d6      Z e       Zd d7lmmZmZ  G d8 d9e      Zdd:Zedd<       Ze	 	 	 	 	 	 	 	 dd=       Ze	 	 	 	 	 	 	 	 dd>       Z G d? d@ej0                        Z e       Zi adAedB<   ddCZeddD       Z	 	 	 	 	 	 	 	 ddEZddFZe^eG   Ze	 	 	 	 	 	 ddG       Ze	 	 	 	 	 	 	 	 ddH       Ze	 	 	 	 	 	 	 	 	 	 ddI       Ze	 	 	 	 	 	 ddJ       Ze	 	 	 	 	 	 	 	 ddK       Ze	 	 	 	 	 	 	 	 	 	 ddL       Ze	 	 	 	 	 	 ddM       Ze	 	 	 	 	 	 	 	 ddN       Ze	 	 	 	 	 	 	 	 	 	 ddO       ZedP f	 	 	 	 	 	 	 	 	 ddQZejB                  ddR       Z	 d	 	 	 	 	 	 	 ddSZdddUZee eeuededV   edW   eedVf   e3eeef
      ZdddXZedYdZ	 	 	 	 	 dd[       Zedd\       Zedd]       Zdd^Z	 	 	 	 	 	 	 	 	 	 dd_Z	 	 	 	 	 	 	 	 	 	 dd`Ze eGeda   eedaf   f   Ze e3edb   eedbf   f   Z	 	 	 	 	 	 	 	 	 	 ddcZe G dd d;             Z	 	 	 	 ddeZe	 	 	 	 	 	 ddf       Ze	 	 	 	 	 	 ddg       Ze	 	 	 	 	 	 ddh       Z	 	 	 	 	 	 ddiZe3e&jr                  jt                  e@fZ	 	 	 	 	 	 ddkZ	 	 	 	 	 	 ddlZ	 	 	 	 	 	 	 	 	 	 	 	 ddmZ G dn do      Ze G dp dq             Z G dr dseH      Z	 d	 	 	 	 	 ddtZ	 	 	 	 dduZe&j                  	 d	 	 	 	 	 	 	 ddv       Z	 	 	 	 	 	 	 	 	 	 ddwZdadxedy<   e	 	 	 	 ddz       Z G d{ d|eT      Z eeɫ      Z G d} d~eT      Z ee˫      Z G d djeZ      Z	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 ddZ G d de)j                  j                        Z G d de)j                        Z G d de)j                        Z	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZedd       Z G d de٫      Z G d d      Z G d de      Z G d d      Zdaded<   edd       Z	 	 	 ddTdTdTdYdTdTd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZddZddZ	 	 	 	 	 	 	 	 ddZedd       Z	 	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZddZy)    )annotationsN)defaultdictOrderedDict)Callable	GeneratorMappingSequence)_GeneratorContextManagercontextmanager	ExitStacknullcontext)	dataclass)AnyConcatenateOptionaloverloadProtocolTYPE_CHECKINGTypeVarUnion)	ParamSpecSelfTypeVarTupleUnpack)WeakKeyDictionary)SymBoolSymIntTensor)enable_python_dispatcher)FakeScriptObject)is_opaque_type)trace_structured)HigherOrderOperator)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autograd_would_have_decomposedTorchDispatchMode)count)Thunk)_WeakHashRefWeakIdKeyDictionaryWeakTensorKeyDictionary   )BackwardState)SymNode)MutableMapping)
OpOverload)PHBase)BoolLikeTypeFloatLikeTypeIntLikeType)PythonKeyTracerdispatch_tracemake_fxDecompositionInterpreterselective_decomposepy_sym_typesget_innermost_proxy_modeget_proxy_modehandle_sym_dispatchmaybe_enable_thunkifymaybe_disable_thunkify)rG   _GraphAppendingTracerExnot_implementedMapping[OpOverload, Callable]CURRENT_DECOMPOSITION_TABLETU_PR_Tsc                    t        |       d fS N)list)xss    h/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/torch/fx/experimental/proxy_tensor.py<lambda>r`      s    R$     c                    t        |       S r\   )tuple)r^   _s     r_   r`   r`      s
    %) ra   c                x    t        |       D cg c]  \  }}t        j                  |      |f c}}d fS c c}}w r\   )	enumeratepytreeSequenceKey)r^   ixs      r_   r`   r`      s5    09">1&

Q
	#>% >s   !6z
torch.Size)flatten_with_keys_fnserialized_type_namec                n    dj                  d t        |      D              }t        d| d| dd| i      S )z*FX gets confused by varargs, de-confuse it,c              3  &   K   | ]	  }d |   yw)argN ).0ri   s     r_   	<genexpr>z!fake_signature.<locals>.<genexpr>   s     8a#aS	8s   zlambda z: fn()fn)joinrangeeval)ru   nargsargnamess      r_   fake_signaturer{      s:    xx85<88H'(5
!4tRjAAra   c              #  H   K   t         }| xs i a 	 t          |a y # |a w xY wwr\   )rU   )decomposition_tableold_decomposition_tables     r_   	decomposer      s1     
 :"5";>))&=#&=#s   " ""c                      e Zd Zy)
_NoDefaultN__name__
__module____qualname__rq   ra   r_   r   r          ra   r   )rL   	PySymTypec                      e Zd ZU ded<   y)_HasMetazdict[str, PySymType]metaNr   r   r   __annotations__rq   ra   r_   r   r      s    
ra   r   c                    t        | d      sJ d       d| j                  v xr t        | j                  d   t              S )Nr   z3All nodes traced with proxy_tensor should have metaval)hasattrr   
isinstancerL   )nodes    r_   is_sym_noder      s<    4 W"WW DIIL*TYYu-=|"LLra   _ProxyTensorc                     y r\   rq   objtracerproxys      r_   set_proxy_slotr      s    TWra   c                     y r\   rq   r   s      r_   r   r           ra   c                     y r\   rq   r   s      r_   r   r      r   ra   c                      e Zd ZU dZded<   y)_DisableUpdateTensorTrackerFboolvalueN)r   r   r   r   r   rq   ra   r_   r   r      s    E4ra   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   rq   ra   r_   /_is_proxy_tensor_update_tensor_tracker_disabledr      s     .333ra   c               #     K   t         j                  } dt         _        	 d | t         _        y# | t         _        w xY w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    r_   +_proxy_tensor_disable_update_tensor_trackerr      s7     J 499J/3&,>3=*0:*0s   ?/ ?<?c                   t         j                  d| t        |       |       t        | t              r-t        |t
              sJ t               s||j                  | <   y y t        | t              r"t        |t              sJ ||j                  | <   y t        | t              sJ t        |              | |j                  vrt        j                  t         |      }||j                  | <   dd l}t        | j$                  j&                  |j(                        r.t+        ||       |j,                  | j$                  j&                  <   y y y )Nzset_proxy_slot %s (%s) %sr   )logdebugidr   r   r   r   tensor_tracker_AnyScriptObjectr+   script_object_trackerrL   typesymnode_trackertypingcast_PySymProxyTypesympyr   exprSymbol_SympyExprTrackerValuesympy_expr_tracker)r   r   r   r   s       r_   r   r   
  s   
 II)33?#v %...>@).F!!#& A	C*	,%''',1$$S) #|,7d3i7,f,,,KK7E*/F""3' #((--6;Q3<))#((--8 7% -ra   c                ~    t        | t        t        f      sJ t        |              t	        t        | |dd             S )NFc                     yNTrq   )rd   s    r_   r`   z has_proxy_slot.<locals>.<lambda>>      ra   )r   r   r@   r   r   get_proxy_slotr   r   s     r_   has_proxy_slotr   ;  s6    cFG,-8tCy8-sFE>BCCra   c                     y r\   rq   r   s     r_   r   r   D  s     ra   c                     y r\   rq   r   r   defaults      r_   r   r   K  s    
 !ra   c                     y r\   rq   r   r   r   	transforms       r_   r   r   S       ra   c                     y r\   rq   r   s     r_   r   r   \  s     ra   c                     y r\   rq   r   s      r_   r   r   c  s    
 ra   c                     y r\   rq   r   s       r_   r   r   k  r   ra   c                     y r\   rq   r   s     r_   r   r   t  s     ra   c                     y r\   rq   r   s      r_   r   r   {  s    
 !$ra   c                     y r\   rq   r   s       r_   r   r     r   ra   c                    | S r\   rq   rj   s    r_   r`   r`     s    A ra   c           	        t        | t              r|j                  }nIt        | t              r|j                  }n,t        | t
              sJ t        |              |j                  }|j                  |       }|t        | t
              r| j                  j                         rp|j                  j                  | j                  j                        x}|j                  }n2t        || j                  j                  |        |j                  |       }|:t        |t              r(t!        |  dt        |        dt#        |        d|       |S  ||      }|S )Nz (z, z)is not tracked with proxy for )r   r   r   r   r   rL   r   r   getr   is_symbolicr   r   r   _build_proxy_for_sym_exprr   RuntimeErrorr   )r   r   r   r   trackerr   tmpress           r_   r   r     s)    #v''	C)	*..#|,7d3i7,(( KKE}C688! 0044SXX]]CCP		 *&#((--EC(}gz*%r$s)Br#wi/NvhW  
E
CJra   c                     ddl } i }| j                  j                  j                  j	                         j                         D ]  \  }}t        t        |d      }||||<     |S )zp
    Returns a dict converting sympy functions to python operators
    (i.e. `sympy.Mul` -> `operator.mul`)
    r   N)torch.utils._sympy.interputils_sympyinterphandlersitemsgetattroperator)torchr   kvops        r_   _sympy_handlersr     sd     %H""))224::< 1Xq$'>HQK Ora   c                   | j                   j                  |      x}|rJ |j                  S t        |t        t
        t        f      r|S |j                  rt	        |      S |j                  rt        |      S g }|j                  D ]#  }t        | |      x} y|j                  |       % t        |      }t               j                  |j                        }|sy| || }|S t        | |||       |S )a  
    Decompose `expr` and look for the pieces as inputs. If `out` is provided
    then that will be the resulting SymNode (and `out.expr` must be the same as
    `expr`).

    This function is used when the ProxyTorchDispatchMode sees a SymNode
    that it hasn't seen before to try to associate it with traced inputs.

    How can this happen?

    First thing to remember is that although sympy.Exprs are interned (so
    `sympy.Expr("s3*s4")` will always have the same `id` and will always compare
    equal) SymNode does not (so doing `SymNode("s3")*SymNode("s4")` twice in a
    row will give two unique SymNodes).

    - On way for this to happen is if we turn off tracing to compute an
      intermediate value and then USE that value with tracing turned on - for
      example if we turn off tracing to do some FakeTensor propagation to
      compute a size (dtensor does this) but then turn tracing back on and use
      that computed size.

    - Another way is if we compute a size in one graph and stash it somewhere
      hidden (such as in some meta-data) and later use it in a different graph
      (dtensor does this too). Since the size was computed in the first graph
      and it's not an official input to the second graph it's not tracked
      properly. This is often going to show up as it usually works in fullgraph
      but a graph break causes a failure.

    To handle this we decompose the sympy.Expr and look for the pieces as
    inputs. But there are problems with this approach:

    - We lose operation provanance: We end up figuring out where to get the
      inputs - but those may not actually be correct. If we have "s1" coming in
      from both tensor1 and tensor2 and we pick the wrong one we could end up
      keeping a tensor alive longer than intended.

    - There's no guarantee that those values are inputs to the graph: If we have
      "s1*s2" computed in a graph #1 and used in graph #2 there's no guarantee
      that the input that holds "s1" is actually an input on graph #2.

    - The decomposition isn't guaranteed to be the same: Sympy can "simplify"
      expressions so it's possible that our inputs are "s1*s2" and "s3" but we
      decompose it into "s1" and "s2*s3" - which wouldn't be found.

    Other ways we could handle this:

    - Don't: Just require that all inputs are tracked properly. This is the
      "correct" solution but harder because you need to track down each
      potential problem one by one and fix them. And when it fails it's a lot of
      work to figure out both why it's failing and the right way to fix it. This
      is complicated by the fact that a stashed value could be incorrect but
      work fine until we happen to get an graph break in the wrong place - so it
      may be a while before the bug is found. (Maybe we need a "dynamo abuse
      mode" where we run tests with as many graph breaks inserted as possible?)

    - Track SymNode ops separately from proxy tracing: Right now SymNode
      operations are tracked as part of the proxy tracing - so when we disable
      proxy tracing we also disable SymNode tracing. But we don't have to do
      that - we could instead always have SymNodes track where they came from
      and just use that when needed. This solves the problem of tracing being
      temporarily turned off but doesn't help if an input isn't present after a
      graph break.

    - Better decomposition: Right now the decomposition is pretty simple. We do
      have a sat-solver available to us so we could theoretically do a better
      job figuring out a "correct" decomposition. But that still relies on
      having the inputs available at all - which isn't a guarantee.
    N)r   r   r   r   intfloatr   
is_Integeris_Floatargsr   appendrc   r   func_sym_register)r   r   outr   r   rp   	arg_valuer   s           r_   r   r     s    P **..t44Aw{{$eT*+4y}}T{Dyy 263??IHI ;D-/33DII>D
{Dk J 	fdD#.Jra   Fc                p    t        | t              rt        | j                  | |      S | j	                         S r\   )r   r%   r$   	fake_modedetach)r   include_reals     r_   snapshot_faker   1  s-     #z"3==#|<<zz|ra   _ExtractValType)r   .c                B   t        |       rt        | |      S t        | t              r| S t        | t              r| S t        | t
              r| S t        | t        t        f      r(| j                  | D cg c]  }t        |       c}      S t        | t              r-| j                         D ci c]  \  }}|t        |       c}}S t        | t              rz| j                  smddlm}  ||       }|st!        d      }|5  t#        j$                  | j&                  | j)                         | j*                  | j,                        cd d d        S y t        | t.        t0        t2        f      r| S | y t5        j6                  |        y c c}w c c}}w # 1 sw Y   *xY w)Nr   r   )detect_fake_modeT)allow_fallback_kernels)devicedtype)r'   r   r   rL   r   r?   r]   rc   	__class__extract_valdictr   r   	is_sparsetorch._guardsr   r&   r   empty_stridedshapestrider   r   r   r   r   typing_extensionsassert_never)r   r   rj   r   r   r   fake_tensor_modes          r_   r   r   K  sL   s|S|<<	C	&
	C)	*
	C	'
	C$	'}}c:k!n:;;	C	.1iik:da;q>!::	C	 }} 7/4##1#N ! **IIszz|CJJcii 
 	C#ud+	,
	""3'7 ;: s   4F
0FAFFTenablec             #  b   K   | j                   }|| _         	 d || _         y# || _         w xY w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      r_   _enable_thunkifyr  s  s3      
 
 C#F%!$s   /# /	,/c               #     K   t               } | %t        | j                  d      5  d ddd       yd y# 1 sw Y   yxY ww)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	  rN   r  r   
proxy_modes    r_   rQ   rQ     sG       !Jj//> 		 	 		 	s   $A9AAAc               #     K   t               } | #t        | j                        5  d ddd       yd y# 1 sw Y   yxY ww)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    r_   rP   rP     sE       !Jj//0 		 	 		 	s   "A7AA Ac                   t        || j                  j                  dk(        | j                  j                  d<   t	        | j
                        5  t        |      r#t        |      | j                  j                  d<   n>t        |t              r.|j                  s"t        |      | j                  j                  d<   d d d        | S # 1 sw Y   | S xY w)Nplaceholderr   r   tensor_meta)r   r   r   r   r  r   r'   r1   r   r   r  )r   r   s     r_   set_metar    s    (5::==M9EJJOOE 
%,,	' K3<-Ec-JEJJOOM*V$S]]-Ec-JEJJOOM*K LK Ls   A-CCc                    | j                   r"t        t        j                  |g|i |      S  ||i |t        fd      S )zT
    Delays computation of f until it's called again
    Also caches the result
    c                      S r\   rq   )rs   r_   r`   zthunkify.<locals>.<lambda>  s    Q ra   )r  r:   	functoolspartial)r   fr   kwargsr  s       @r_   thunkifyr    sH     Y&&q:4:6:;;tvYra   c                  	 	 	 	 	 	 	 	 	 	 dfd}t        | j                        D ]  \  }} ||fd|        t        |       s/t        | j                               D ]  \  }} ||fd|         || j	                         fd       t        |       s || j                         fd       t        | t        |             y )Nc           
         t        |      sJ t        | t              r1t              5  t	        | t        || g|i |       d d d        y y # 1 sw Y   y xY wr\   )callabler   r   r  r   r  )outer_sproxy_callabler   r  r   s       r_   try_set_proxy_slotz(track_tensor.<locals>.try_set_proxy_slot  sh     '''gv&!&) V^WNtNvN  ' s   AAc                    t        j                  dt        j                  j                  j
                  j                  |fi       |       S Ncall_function)r  create_proxyr   opsatensym_sizer   rj   ri   r   r   s     r_   r`   ztrack_tensor.<locals>.<lambda>  s@    ###UYY^^%<%<%@%@5!*b 	 ra   c                    t        j                  dt        j                  j                  j
                  j                  |fi       |       S r'  )r  r)  r   r*  r+  
sym_strider   r-  s     r_   r`   ztrack_tensor.<locals>.<lambda>  sB    X''')B)B)F)FPQ
TV 	 ra   c                    t        j                  dt        j                  j                  j
                  j                  fi       |       S r'  )r  r)  r   r*  r+  	sym_numelr   rj   r   r   s    r_   r`   ztrack_tensor.<locals>.<lambda>  s>    (!9!9!A!AE8R 	
 ra   c                    t        j                  dt        j                  j                  j
                  j                  fi       |       S r'  )r  r)  r   r*  r+  sym_storage_offsetr   r2  s    r_   r`   ztrack_tensor.<locals>.<lambda>  sA    h###IINN55==H	  ra   )
r#  rF   r$  z+Callable[Concatenate[PySymType, _P], Proxy]r   _P.argsr  	_P.kwargsreturnNone)rf   r  r)   r  numelstorage_offsetr   r   )tensorr   constantr   r%  ri   ss    ` `   r_   track_tensorr>    s    C  	
 
( &,,' 

1 		


  fmmo. 
	DAq 	
	 	
  !!#	
 66<x#@Ara   _NestedProxys_NestedTensorsc               T    t        | |       	 	 	 	 	 	 	 	 dfd | ||       | S )Nc           	        t        | t              rBt        t              sJ |t        |t              sJ t        | |       t	        |        y t        | t
              r9t        t              sJ t	        |        t        | t        fd             y t        | t              r,t        t              sJ t        |        t	        |        y t        | t        t        f      rXt        t        j                        rt	        |        	 	 	 	 	 	 dd}t        |       D ]  \  }} 	||    |||              y t        | t              rP|J t        t        j                        rt	        |        | j                         D ]  \  }} 	||   d         y t        | t              r&t        t              sJ t	        |        | _        y y )N)r   r<  c                      S r\   rq   r   s   r_   r`   z<track_tensor_tree.<locals>.wrap_with_proxy.<locals>.<lambda>>  s    u ra   c                B    | y t        | t        t        f      sJ | |   S r\   )r   r]   rc   )cidxs     r_   get_constantz@track_tensor_tree.<locals>.wrap_with_proxy.<locals>.get_constantH  s)     9%a$777S6Mra   )rF  Optional[_NestedTensors]rG  r   r7  rI  )r   r   r+   r>  r  rL   r   r  r   rc   r]   fxrf   r   r   r?   r   )
er   r<  rH  rG  eekeyr   r   wrap_with_proxys
    `      r_   rN  z*track_tensor_tree.<locals>.wrap_with_proxy2  s    a eU+++#z(F'CCCE&8DUA<(eU+++UA1fhv}&EF+,eU+++1fe,UAE4=)%*""+"25")" %Q< MR  E#JXs0KLM
 4  ###%*"GGI 7SU3Z67 =)eU+++UAAG ra   )rK  objectr   r?  r<  rI  r7  r8  )_set_unbacked_bindings)	inner_res	proxy_resr<  r   rN  s      `@r_   track_tensor_treerS    sI    ( 9i0::':3K:	:x Iy(3ra   c                  "    e Zd ZU ded<   ded<   y)r   r+   r   Optional[Tensor]r<  Nr   rq   ra   r_   r   r   s  s    Lra   c                     d fd}|S )Nc                   | j                   }|j                  |j                  S | j                   j                  j                  r}t	        | t
              rt        | j                   j                        S t	        | t              rt        | j                   j                        S t        | j                   j                        S t	        | t              sJ t        |       j                         S r\   )r   r<  r   	is_numberr   r   r   r   r   r   rL   r   force)rK  nr   s     r_   innerzfetch_sym_proxy.<locals>.inner|  s    FF::!::66;;  !W%AFFKK((Av&166;;''%%a...!!V,2244ra   )rK  r   r7  zUnion[int, bool, float, Proxy]rq   )r   r[  s   ` r_   fetch_sym_proxyr\  y  s    5 Lra   c                     y r\   rq   r   ts     r_   fetch_object_proxyr`    s     #&ra   c                     y r\   rq   r^  s     r_   r`  r`    s     *-ra   c                     y r\   rq   r^  s     r_   r`  r`    s     ),ra   c                    t        || |      S r\   r   r^  s     r_   r`  r`    s     !VQ''ra   ProxyTorchDispatchModec                   |j                   s|j                  syt        | t        j                  j
                        syt        t        t        |j                  j                  j                                    }|j                  j                  d      }t        j                  t        j                  f}t        |t        j                         xr |j"                  |v }|sg|j$                  D ]X  }t'        |d      r|j                  j                  d      nd}t        |t        j                         sG|j"                  |v sVd} n |syd|j                  d<   y)z
    Records operators whose tensor outputs or inputs are fp16/bf16 so downstream pointwise code can
    emulate eager's rounding behavior when emulate_precision_casts is enabled.
    Nr   r   Tlow_precision_pointwise_barrier)decomp_layersemulate_precision_castsr   r   _opsrB   nextiterreversedr   graphnodesr   r   bfloat16float16r   r   all_input_nodesr   )r   r  	last_noder_  	low_pr_fpoutput_low_precision
input_noder   s           r_   _maybe_record_pointwise_barrierrw    s    z'I'IdEJJ112T(:#4#4#:#:#@#@ABCI5!A/I%a6O177i;O#33 	J07
F0K*//%%e,QUC#u||,i1G'+$		  8<INN45ra   c                   | D cg c]&  }t        |t        t        f      rt        ||      n|( }}t	        d |D               xr t	        d | D               }|D cg c]   }t        |t
              r|j                  n|" }}|D cg c]%  }t        |t              r t        |      |      n|' }}|t        |      |fS c c}w c c}w c c}w )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              3  X   K   | ]"  }t        |t              r|j                  d u  $ y wr\   r   r   r<  rr   r_  s     r_   rs   z7_fetch_proxies_and_all_constant_flag.<locals>.<genexpr>  s+      
!\* JJ$
   (*c              3  <   K   | ]  }t        |t                y wr\   )r   rL   )rr   rj   s     r_   rs   z7_fetch_proxies_and_all_constant_flag.<locals>.<genexpr>  s     JAJq,/Js   )
r   r   r   r`  anyr   r   rL   r\  rc   )flat_args_kwargsr   rj   f_flat_args_kwargsall_constantrK  proxy_flat_args_kwargss          r_   $_fetch_proxies_and_all_constant_flagr    s    "  !f&678 vq)   
'
 
 	
 	K J9IJJJ  @R:;:a.A5  ( (2!\'B	 	 	#	I 
 u%;<lJJA.s   +C%C	*Cc           	     
    g t        j                  ||f      \  }}d fdt        fd|D              st        j	                  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=t        ||      r1 5   |j                   |i |}|t
        ur|cd d d        S 	 d d d        |t        j                  j                  j"                  j                  u rM 5  t        j$                  |d   j'                         dk(  d        |d   dk7  j)                         cd d d        S  j*                  }t-        ||      \  }	}
}t        j.                  j0                  |j2                  v r|r`|	D cg c]   }t5        |t6              r|j8                  n|" }}t        j:                  ||      \  }}t=               5   ||i |cd d d        S  j>                  r-t        j@                  tB        d ||f      rtE        d| d	      t        j:                  |
|      \  }}|t        j                  j                  jF                  j                  u r.t        j                  j                  jH                  j                  } j*                  jK                  d
||| j*                  jL                  jO                  |jP                  jR                              }tU         j*                        5   ||i |}d d d        tW        d |	D              }d }dd}|t        j                  j                  jH                  j                  u rjj'                         tX        k  rSt=               5  t5        |d   tZ        tB        f      sJ t]        |d                |d   j_                         }d d d        nt        j.                  j`                  |j2                  vr|r}|r{t        j@                  tB        |      r`t=               5  |	D cg c]   }t5        |t6              r|j8                  n|" }}t        j:                  ||      \  }} ||i |}d d d        nd }tc        |||       t        |        |S # 1 sw Y   xY w# 1 sw Y   ^xY wc c}w # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   _xY wc c}w # 1 sw Y   pxY w)Nc                    t        |       t        v xs t        | j                        }j                  r)|xs% t        |       t
        j                  j                  u }|sj                  t        |              |S r\   )	r   HANDLED_TYPESr   r   _allow_fake_constantr   _subclassesr%   r   )rj   r  r  unrecognized_typess     r_   can_handle_tensorz%proxy_call.<locals>.can_handle_tensor  se    G}$Lq*:K:K(L**<T!W 1 1 < <<A%%d1g.ra   c              3  P   K   | ]  }t        |t              s |        y wr\   )r   r   )rr   rj   r  s     r_   rs   zproxy_call.<locals>.<genexpr>  s      WAvAV #Ws   &&zEProxyTensorMode tensors without proxy had unrecognized subclasses: %sr   r>   c                      y)Nz=Boolean value of Tensor with more than one value is ambiguousrq   rq   ra   r_   r`   zproxy_call.<locals>.<lambda>(  r   ra   c                    t        |        S r\   )r'   r_  s    r_   r`   zproxy_call.<locals>.<lambda>A  s    '!*n ra   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  X   K   | ]"  }t        |t              r|j                  d u $ y wr\   rz  r{  s     r_   rs   zproxy_call.<locals>.<genexpr>  s+      a& 	


$r|  c                0    | j                         t        k  S r\   )r9  CONSTANT_NUMEL_LIMITr  s    r_   tensor_numel_in_limitz)proxy_call.<locals>.tensor_numel_in_limit  s    wwy000ra   r<  r   )rj   r   r7  r   )r_  r   r7  r   )2rg   tree_flattenallnot_implemented_logr   NotImplementedmaybe_handle_decomprw  r   r*  r+  sizer   r  r:  r7   r   
is_nonzero_checkr9  itemr   r  Tagdata_dependent_outputtagsr   r   r<  tree_unflattenr(   _error_on_data_dependent_opstree_all_onlyr   r   
lift_freshlift_fresh_copyr)  rn  _target_to_stroverloadpacketr   r  r~  r  r+   r   clonenondeterministic_seededrS  )r  r   pre_dispatchr   r  r  specr  r   r  r  r  r_  const_flat_args_kwargs
const_argsconst_kwargs
proxy_argsproxy_kwargs	proxy_outr   any_constantr<  r  r  r  s   `                      @@r_   
proxy_callr    s    &(#00$@d W-=WW!!S	
 JdF;A'j9 IINN''IINN!!))IINN))11

 +41AB 	//A&	 	&	
 uyy~~((000 	)LLQ1$W GqL&&(	) 	) F,-=vF =. yy&&$))3 ,& )L9

q@&" & (.'<'<&($J () 9Z8<89 9
 22v7K7K,tVn8
 Z[_Z` a( (   &445KTRJF uyy~~((000yy~~--55!!..$$33D4G4G4P4PQ / I 
*++	, $D#F#$0  # L H1 			..666IIK//#% 	'd1gv7Fd1gF7Aw}}H	' 	' 			)):  )>D $% 	9 ,& )L9

q@&" & (.'<'<&($J Z8<8H	9 	9 c9xG#D*5Jw	 		) 	)&9 9z$ $T	' 	'&	9 	9sb   T	7AT
%T#T(	T5<A UU%U>#U	TT (T25T?UUUc                  V    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
y)_SymNodeDictzM
    Wrapper around a dictionary that will hash SymInts with their nodes
    c                    i | _         y r\   )sym_node_dictselfs    r_   __init__z_SymNodeDict.__init__  s
    ?Ara   c                6    || j                   |j                  <   y r\   r  r   )r  rM  r   s      r_   __setitem__z_SymNodeDict.__setitem__  s    ',388$ra   c                4    | j                   |j                     S r\   r  r  rM  s     r_   __getitem__z_SymNodeDict.__getitem__  s    !!#((++ra   c                2    |j                   | j                  v S r\   )r   r  r  s     r_   __contains__z_SymNodeDict.__contains__  s    xx4----ra   Nc                N    | j                   j                  |j                  |      S r\   )r  r   r   )r  rM  r   s      r_   r   z_SymNodeDict.get  s!    
 !!%%chh88ra   c                    t         r\   )NotImplementedErrorr  s    r_   __iter__z_SymNodeDict.__iter__  s    !!ra   c                ,    t        | j                        S r\   )lenr  r  s    r_   __len__z_SymNodeDict.__len__  s    4%%&&ra   r7  r8  )rM  r   r   r   r7  r8  )rM  r   r7  r   )rM  r   r7  r   r\   )rM  r   r   zOptional[_PySymProxyType]r7  r   )r7  r   )r7  r   )r   r   r   __doc__r  r  r  r  r   r  r  rq   ra   r_   r  r    sJ    B-,. DH99'@9	9"'ra   r  c                  "    e Zd ZU ded<   ded<   y)r   r   r   r   r   Nr   rq   ra   r_   r   r     s    ra   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	dd       Z
e	dd       Z
e		 	 	 	 dd       Z
ddZ
	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 d  fdZ xZS )!rG   +MutableMapping[_AnyScriptObjectType, Proxy]r   r  r   *dict[sympy.Symbol, _SympyExprTrackerValue]r   $MutableMapping[Tensor, _ProxyTensor]r   dict[OpOverload, int]torch_fn_countsFr   r  c                    t         |   d       t               | _        t	               | _        t        d t              | _        i | _	        d | _
        i | _        d| _        y )Nrq   )autowrap_modulesr   ref_typeF)superr  r=   r   r  r   r<   r;   r   r   torch_fn_metadatar  r  )r  r   s    r_   r  zPythonKeyTracer.__init__  s`    "-57+~%8&
" #% "&  "$ra   c                     ||i |S r\   rq   )r  mforwardr   r  s        r_   call_modulezPythonKeyTracer.call_module  s     '''ra   c                    |S r\   rq   )r  attrattr_valparameter_proxy_caches       r_   r   zPythonKeyTracer.getattr  s	     ra   c                   t        |t        j                  j                        rx| j                  j                         D ]  \  }}||u s| j                  d|di       c S  | j                  d      }t        | j                  ||       | j                  d|di       S t        |t              r.|j                  j                  J |j                  j                  S t        | 5  |      S )Nget_attrrq   _param_constant)r   r   nn	Parameterrootnamed_parameterscreate_nodeget_fresh_qualnamesetattrrL   r   r<  r  
create_arg)r  arZ  pqualnamer   s        r_   r  zPythonKeyTracer.create_arg  s    a++,		224 C16++J2rBBC ../@AHDIIx+##J"bAA<(66??...66??"w!!$$ra   c                     y r\   rq   r  rK  s     r_   unwrap_proxyzPythonKeyTracer.unwrap_proxy  s    ?Bra   c                     y r\   rq   r  s     r_   r  zPythonKeyTracer.unwrap_proxy  s    EHra   c                     y r\   rq   r  s     r_   r  zPythonKeyTracer.unwrap_proxy!  s     .1ra   c                    t        |t              rt        || |d       S t        |t              rt        || |d       S t        |t              rt        || |      S |S )Nc                    | j                   S r\   rD  r   s    r_   r`   z.PythonKeyTracer.unwrap_proxy.<locals>.<lambda>(  s
     ra   c                "    | j                         S r\   )rY  )rK  s    r_   r`   z.PythonKeyTracer.unwrap_proxy.<locals>.<lambda>*  s    	 ra   )r   r   r   rL   r   r  s     r_   r  zPythonKeyTracer.unwrap_proxy&  sY    a !!T1.?@@<(!!T1.ABB+,!!T1--Hra   c                "   t         |   ||||||      }|j                  dv rd|j                  v r|j                  d= |dk(  rmt	        |t
              sJ t        | j                  |      }t	        |t        j                        r+t               5  t        |      |j                  d<   d d d        dd}	t        |||f      r@t        j                  j                  j                  ||f|	      \  }
}|
|f|j                  d<   |S # 1 sw Y   ]xY w)Nr  outputstack_tracer  r   c                   t        | t        j                  j                        rd| j                  vry | j                  d   }t        |t        j
                        rt        |t              sy t        | j                  d         S )Nr   )r   r   rJ  Noder   r   r%   r   )r   r   s     r_   map_fnz+PythonKeyTracer.create_node.<locals>.map_fnE  s^    a/53F&&-C #u||,ZZ5Pqvve}--ra   eager_input_vals)r   r   r7  zOptional[_ExtractValType])r  r  r   r   r   strr   r  r   r   disable_proxy_modes_tracingr   _should_save_eager_input_valsrJ  r   map_aggregate)r  kindtargetr   r  r  	type_exprr   r  r  arg_inp	kwarg_inpr   s               r_   r  zPythonKeyTracer.create_node0  s     w"4vtYO77//MTYY4N		-(:fc***499f-D$-02 9'24'8DIIe$9	. )$@ "'!<!<dF^V!TGY-4i,@DII()-9 9s   DDr  )
r  r2   r  zCallable[..., Any]r   ztuple[Any, ...]r  zdict[str, Any]r7  r   r  r  r  rO  r  zdict[str, Proxy]r7  rO  )r  rO  r7  fx.node.Node)rK  r   r7  zUnion[Proxy, Tensor])rK  r   r7  zUnion[Proxy, PySymType])rK  _AnyScriptObjectTyper7  "Union[Proxy, _AnyScriptObjectType])rK  rV   r7  rO  )NN)r  r  r  r0   r   ztuple[Argument, ...]r  zdict[str, Argument]r  zOptional[str]r  zOptional[Any]r7  ztorch.fx.Node)r   r   r   r   r  r  r  r   r  r   r  r  __classcell__r   s   @r_   rG   rG     s*   FF!!BB88**!OT!%&(( $( 	(
 ( 
(#)BR	
% B BH H1%1	+1 1  ##'(( ( #	(
 $( ( !( 
( (ra   rG   c                   ddl m} t        |       syt        | t        j
                  j                  j                  t        j
                  j                  j                  |f      ry|| t        j                  j                  j                  u s&| t        j                  j                  j                  u rW|d   }t        |d   t        j                  j                  t        j                  j                  f      sJ t!        |d   d       S | t        j                  j                  j"                  u ryt        | t        j                  j                        r)t%        j&                  t         |      rt)        d|  d      t        | t        j                  j                        r3ddlm}  ||       t        j.                  j0                  j2                  k(  S y)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_v2rj  rB   r#   r  with_effectsrg   tree_anyr   torch._library.utilsr  _Cr  needs_exact_strides)r  args_kwargsr  r   r  s        r_   r  r  [  su    JF##66TT##66RR	
 %))((<<<UYY++BBB1~Gejj++UZZ-K-KL
 	
 
 -T!Wd;;''444 &%**889??8+Fx (G H 
 &%**//0B(0EHHLL4T4TTTra   c                &     t         d fd       }|S )Nc               3    K   ddl m} m}m} g }d } |        dkD  r2 |       }t	        |      r|}n|j                  |        |        dkD  r2t        |      D ]
  } ||        	 | |Lt        |      }|dkD  r |       }|dz  }|dkD  r|j                  |       t        |      D ]
  } ||        y y # |Lt        |      }|dkD  r |       }|dz  }|dkD  r|j                  |       t        |      D ]
  } ||        w w xY ww)Nr   )_len_torch_function_stack	_pop_moder5   r>   )torch.overridesr!  r"  r5   r   r   rm  r  )r!  r"  r5   temp_elementsremoved_modemoder9   mode_tys          r_   context_manager_fnzB_make_temp_remove_mode_context_manager.<locals>.context_manager_fn  sC    TT')A-;D$(#$$T* ()A- ]+ 	Dt		% 'M*ai$;DQJE ai $$\2$]3 %Dt$% (|'M*ai$;DQJE ai $$\2$]3 %Dt$% (s0   A
DD'B: +#D+D:$D
+D

D)r7  z2Generator[Optional[TorchFunctionMode], None, None])r   )r'  r(  s   ` r_   &_make_temp_remove_mode_context_managerr)    s      % %@ ra   c                &   |j                  | |      }dd}|j                  |       ddlm}  ||       t	        | t
              r| j                  j                  n| j                  }t        j                  j                  |j                  ||      S )Nc                    ddl m} | j                         ry ||       ryt        | j                  j                  d      t              rt        d | j                  D              ryy)Nr>   )is_accessor_nodeTFr   c              3     K   | ]G  }t        |t        j                        r+t        |j                  j	                  d       t
               I yw)r   N)r   rJ  r  r   r   rL   )rr   r  s     r_   rs   z6dispatch_trace.<locals>.impure_pred.<locals>.<genexpr>  s:      a) 166::e,l;s   AA)	symbolic_shapesr,  	is_impurer   r   r   rL   r  r   )rZ  r,  s     r_   impure_predz#dispatch_trace.<locals>.impure_pred  s^    5 ;;= A
 qvvzz%(,7     ra   r   )dedupe_symints)rZ  zfx.Noder7  r   )traceeliminate_dead_code,torch._inductor.fx_passes.dedupe_symint_usesr1  r   r2   r   r   rJ  _lazy_graph_module_make_graph_moduler  )r  r   concrete_argsrn  r0  r1  r  s          r_   rH   rH     sq     LL}-E8 
k*K5&0v&>4>>""DMMD  33FKKMMra   c                     t        j                        \  }t        j                         d fd       }|S )Nc                 d   t        j                  |       \  }}t        |      t              k(  sJ t               5 }t	        |t
              sJ t        |d        d d d        t        dd      rEt        |       D cg c]-  \  }}t	        |t        j                  j                        r|n|/ c}}dfd} 
 }t        j                  t        ||      }t        j                  t        fd|      }dfd}	t        j                  t        |	|      }|S # 1 sw Y   xY wc c}}w )	Nr  proxy_module_inputsFc                "    t        | | d       S )Nc                    | j                   S r\   rD  r   s    r_   r`   zJwrap_key.<locals>.wrapped.<locals>.get_tensor_proxy_slot.<locals>.<lambda>  s
    !'' ra   rd  r_  r   s    r_   get_tensor_proxy_slotz8wrap_key.<locals>.wrapped.<locals>.get_tensor_proxy_slot  s    !!VQ0ABBra   c                "    t        | | d       S )Nc                    | S r\   rq   r   s    r_   r`   z=wrap_key.<locals>.wrapped.<locals>.<lambda>.<locals>.<lambda>  s    q ra   rd  r=  s    r_   r`   z+wrap_key.<locals>.wrapped.<locals>.<lambda>  s    q&![(Q ra   c                8    t        |       j                         S r\   )r   rY  r=  s    r_   get_sym_proxy_slotz5wrap_key.<locals>.wrapped.<locals>.get_sym_proxy_slot  s    !!V,2244ra   )r_  r   r7  zUnion[Tensor, Proxy])r_  r   r7  r+   )rg   r  r  r   r   re  rS  r   zipr   r  r2   tree_map_onlyr   r   rL   )proxies_unusedflat_proxies_proxies_specr  r_  r  r>  r   rB  r  flat_tensorstensorsr   s             r_   wrappedzwrap_key.<locals>.wrapped  s!    '-&9&9'&B#m< C$5555(* 	Xaa!7888lL4PVW	X 60%8  1Aq  588??3:G
	C k""6+@#F""QSV
	5 ""<1CSI
/	X 	X
s   "D 2D, D))rE  r5  rF  r6  r7  rY   )rg   r  r  wraps)r  rJ  r   r  _tensors_specrK  rI  s   ```   @r_   wrap_keyrN    s<     #)"5"5g">L-__Q < Nra   zOptional[object]ORIGINAL_ATENc              #     K   t         Dt        j                         r0| a | t        j                  d<   	 d  d a d t        j                  d<   y d  y # d a d t        j                  d<   w xY ww)Noriginal_aten)rO  fx_tracebackhas_preserved_node_metacurrent_meta)r   s    r_   set_original_aten_oprU  	  sf     
 !E!E!G59!!/2	> M9=L%%o6 !M9=L%%o6s   0A,A A,A))A,c                  4    e Zd ZddZ	 	 d	 	 	 	 	 	 	 	 	 ddZy)TorchFunctionMetadataModec                    || _         y r\   )r   r  r   s     r_   r  z"TorchFunctionMetadataMode.__init__  s	    ra   Nc                    |xs i }|| j                   _        | j                   j                  j                  |d      dz   | j                   j                  |<    ||i |S )Nr   r>   )r   r  r  r   r  r   typesr   r  s        r_   __torch_function__z,TorchFunctionMetadataMode.__torch_function__  s[     2(,%,0KK,G,G,K,KDRS,TWX,X##D)T$V$$ra   r   _ProxyTracerr7  r8  rq   N
r   rB   r\   tuple[torch._C._TensorMeta, ...]r   tuple[object, ...]r  Optional[dict[str, object]]r7  rO  r   r   r   r  r]  rq   ra   r_   rW  rW    sD     $&.2%% 0% !	%
 ,% 
%ra   rW  c                  4    e Zd ZddZ	 	 d	 	 	 	 	 	 	 	 	 ddZy)PreDispatchTorchFunctionModec                     || _         g | _        y r\   )r   enter_autocast_nodesrY  s     r_   r  z%PreDispatchTorchFunctionMode.__init__5  s     :<!ra   Nc                   |xs i }|t         v r:|t        j                  j                  j                  u r| j
                  j                         }|f}| j                  j                  d||i       }|t        j                  j                  j                  u r| j
                  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 r[t-        || j                        \  }}}| j                  j/                  d||i       }	 ||i |}
t1        |
|	d | j                         |
S  ||i |S )Nr(  r   r  )r.   r   ampautocast_mode_exit_autocastri  popr   r  _enter_autocastr   r  _set_grad_enabledr   
_functorchpredispatch_add_batch_dim_remove_batch_dim_vmap_increment_nesting_vmap_decrement_nestingvmaplazy_load_decompositionsr  r)  rS  )r  r   r\  r   r  
enter_noder   rd   rE  	out_proxyr   s              r_   r]  z/PreDispatchTorchFunctionMode.__torch_function__<  s    2DD
 uyy..===!66::<
"};;**?D$KDuyy..>>>))006**		''77		''66 
 $(		%  uxx111d%f%K
 ((77((::((@@((@@!!::
 
 At{{SMAw00	I ''Cc9tDKKPJT$V$$ra   r^  r`  )
r   zUnion[OpOverload, Callable]r\  rb  r   rc  r  rd  r7  rO  re  rq   ra   r_   rg  rg  4  sD    < $&.24%)4% 04% !	4%
 ,4% 
4%ra   rg  c                       e Zd Zedd       Z	 	 	 d		 	 	 	 	 	 	 	 	 	 	 d
 fdZe	 	 d	 	 	 	 	 	 	 	 	 dd       Zd fdZ	 	 	 	 	 	 	 	 d fdZ	e
dd       Z	 	 	 	 	 	 	 	 	 	 ddZ xZS )re  c                     yr   rq   r  s    r_   enable_tracingz%ProxyTorchDispatchMode.enable_tracingz      ra   c                V   |r$t         j                  j                  j                  nd }t        |   |       || _        || _        || _        || _	        || _
        t         j                  j                  j                  | _        g | _        d| _        ddlm} |j$                  | _        y )Nr   config)r   r  DispatchKeyPreDispatchr  r  r   tracing_moder  r  r  _TorchDispatchModeKeyPROXY	_mode_keyenter_stackrh  torch._inductorr  ri  )	r  r   r  r  r  r  dkr  r   s	           r_   r  zProxyTorchDispatchMode.__init__~  s     2>UXX!!--4(($8!,H) 77== DF"#*-3-K-K$ra   c                    t        |      5  |xs i }|t        j                  j                  k(  r ||i |cd d d        S t	        | || j
                  ||      cd d d        S # 1 sw Y   y xY wr\   )rU  primr   r   r  r  r[  s        r_   __torch_dispatch__z)ProxyTorchDispatchMode.__torch_dispatch__  sq     "$' 	K\rFt{{***T,V,		K 	K dD$*;*;T6J	K 	K 	Ks   +A#A##A,c                    t        t        j                  j                  j                        }| j
                  j                  |       t        | !         S r\   )	r6   r   r  r  r  r  r   r  	__enter__)r  maybe_prev_proxy_moder   s     r_   r  z ProxyTorchDispatchMode.__enter__  sA     1%((2P2P2V2V W 56w ""ra   c                x    t         |   |||      }| j                  j                         }|t	        |       |S r\   )r  __exit__r  rn  r5   )r  exc_type	exc_value	tracebackbmb_previous_proxy_moder   s         r_   r  zProxyTorchDispatchMode.__exit__  sC     GXy)< "&!1!1!5!5!7!--.ra   c                     yr   rq   )clss    r_   is_infra_modez$ProxyTorchDispatchMode.is_infra_mode  r~  ra   c                    |t         j                  u 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	        | j
                  |||       |S )Nr>   r   )r   mulr   r   r   r   )r  r   r\  r   r  r   s         r_   __sym_dispatch__z'ProxyTorchDispatchMode.__sym_dispatch__  s     8<<$q'3'DGqLAwDGS)d1glAw
 zD#F#dkk4s3
ra   r7  r   )FFT)r   r_  r  r  r  r   r  r   r  r   r7  r8  r`  ra  )r7  r   )r  zOptional[type[BaseException]]r  zOptional[BaseException]r  zOptional[types.TracebackType]r7  zOptional[bool])
r   rB   r\  rb  r   rc  r  dict[str, object]r7  rO  )r   r   r   propertyr}  r  r9   r  r  r  classmethodr  r  r  r  s   @r_   re  re  x  s2     #%*-1LL L 	L
 #L '+L 
L6 
 $&.2KK 0K !	K
 ,K 
K K#/ + 1	
 
   0 !	
 " 
ra   c                j    t        |t              r#t        | t        | |||      }t	        || |       y y )N)r   r   r   )r   rL   r  _compute_proxyr   )r   r   r   r   p_out_thunks        r_   r   r     s8     #|$NFDc
 	sFK0	 %ra   c                    t        |      dk(  r2t        |d   t        t        f      rt         fd|d   D              f}nt         fd|D              } j	                  d||i       }t        j                  |       }t        ||       |S )Nr>   r   c              3     K   | ]:  }t        |t              r$t        |      j                         j                  n| < y wr\   r   rL   r   rY  r   rr   r  r   s     r_   rs   z!_compute_proxy.<locals>.<genexpr>  sE        "!\2 #1f-335::   A Ac              3     K   | ]:  }t        |t              r$t        |      j                         j                  n| < y wr\   r  r  s     r_   rs   z!_compute_proxy.<locals>.<genexpr>  sE      
  a. q&)//166
r  r(  )r  r   r]   rc   r  rJ  r+   r  )r   r   r   r   n_argsn_outp_outs   `      r_   r  r    s    
 4yA~*T!WtUm<  a 	
  
 
 
 fbAEHHUF#EUCLra   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 )rR   r  r   z*MutableMapping[PySymType, _PySymProxyType]r   r  r   r  r   zOptional[OpOverload]r  r  r  Fr   r  c                    t         |   |       t        j                         | _        t               | _        i | _        t        d t              | _
        d | _        i | _        y )Nr  )r  r  weakrefr   r   r=   r   r   r<   r;   r   r  r  )r  rn  r   s     r_   r  z _GraphAppendingTracerEx.__init__  sZ    &88:57"$%8&
" "&  "ra   )rn  zfx.graph.Graphr7  r8  )r   r   r   r   r  r  r  r  s   @r_   rR   rR   
  s;    FF??88BB++**!OT!" "ra   rR   c                       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 )
rJ   c                    t        |   |fi | || _        t        | j                        | _        |xs i | _        t        | j                  d      | _        y )Nreal)r  )r  r  	new_graphrR   r   r}   re  r&  )r  moduler  r}   r  r   s        r_   r  z!DecompositionInterpreter.__init__%  sM     	*6*"-dnn=#6#<" *4;;VL	ra   c                    t         |   |||      }t        j                  | j                  j                  |      | j
                        }t        ||d | j
                         |S Nr  )r  r  rJ  r+   r  r   rS  r  r  r   r  r   r   r   s         r_   r  z$DecompositionInterpreter.placeholder4  sR     g!&$733F;T[[I#utDKKH
ra   c                    t         |   |||      }t        j                  | j                  j                  |      | j
                        }t        ||d | j
                         |S r  )r  r  rJ  r+   r  r   rS  r  s         r_   r  z!DecompositionInterpreter.get_attrA  sR     gvtV4008$++F#utDKKH
ra   c                     t            |||      }ddd fd} j                  j                  t        j                  ||             |S )Nc                .    | j                   j                  S r\   )r   r   r   s    r_   get_proxy_nodez7DecompositionInterpreter.output.<locals>.get_proxy_nodeW  s    77<<ra   c                4    t        | j                  |       S r\   )r   r   )rK  r  r  s    r_   unwrapz/DecompositionInterpreter.output.<locals>.unwrapZ  s    !!T[[!^DDra   )rj   r   r7  r	  )rK  r   r7  zUnion[Tensor, fx.Node])r  r  r  rg   tree_map)r  r  r   r  r   r  r  r   s   `     @r_   r  zDecompositionInterpreter.outputO  sF     gnVT62	 	E 	foofc:;
ra   c                    t        | j                        5  | j                  5  t        |   |i |cd d d        cd d d        S # 1 sw Y   nxY wd d d        y # 1 sw Y   y xY wr\   )r   r}   r&  r  run)r  r   r  r   s      r_   r  zDecompositionInterpreter.run`  sY     t//0 	0$)) 	07;//	0 	0 	0 	0 	0 	0 	0s!   AA	AA	AA$r\   )
r  fx.GraphModuler  fx.Graphr}   'Optional[Mapping[OpOverload, Callable]]r  rO  r7  r8  )r  r  r   rc  r  r  r7  rO  )r   rO  r  rO  r7  rO  )	r   r   r   r  r  r  r  r  r  r  s   @r_   rJ   rJ   $  s    
 HL	MM M E	M
 M 
M

 !
 "	

 

		 !	 "		
 
	 ! "	
 
"0 0ra   rJ   c                  d     e Zd Z	 	 	 	 	 	 	 	 	 	 d fdZe	 	 	 	 	 	 	 	 	 	 dd       Z fdZ xZS )_SelectiveDecomposeInterpreterc                B    t        |   |fi | || _        || _        y)z
        For all nodes in `module`, selectively decompose if is `should_decompose`,
        following the given `decomposition_table`.
        N)r  r  should_decomposer}   )r  r  r  r}   r  r   s        r_   r  z'_SelectiveDecomposeInterpreter.__init__h  s'     	*6* 0#6 ra   c                |   | j                   j                  D ]  }|j                  dk(  st        |j                  t
              s.g }|j                  D ]H  }t        |t        j                        rt        j                  |||fi |}n|}|j                  |       J t        |      |_         t        | ||fi |S )a  
        Recursively wrap gm and its sub graph modules. Specifically, HOP takes
        sub graph module as args. We may not want to decompose all nodes within
        these sub graph modules. So we also need to wrap these sub graph modules.
        As a result:
        - if should_decompose(hop) is True, we decompose all nodes within the hop.
        - if should_decompose(hop) is False, we check each node within the hop
            and decide whether decompose or not.
        r(  )rn  ro  r   r   r  r#   r   rJ  r*   r  recursive_wrapr   rc   )gmr  r}   r  r   new_argsrp   new_args           r_   r  z-_SelectiveDecomposeInterpreter.recursive_wrapw  s      HHNN 	,Dww/)j0/ 99 -C!#r~~6"@"O"O!13F#JP# #&OOG,- "(O		, . "5
9?
 	
ra   c                    | j                  |      r/t        | j                        5  t        |   |      }d d d        |S t        |   |      }|S # 1 sw Y   S xY wr\   )r  r   r}   r  run_node)r  rZ  resultr   s      r_   r  z'_SelectiveDecomposeInterpreter.run_node  sb      #4334 -)!,-  W%a(F	- s   AA)
r  r  r  Callable[[fx.Node], bool]r}   rT   r  rO  r7  r8  )
r  r  r  r  r}   rT   r  rO  r7  r  )r   r   r   r  staticmethodr  r  r  r  s   @r_   r  r  g  s    77 47 ;	7
 7 
7  
 
3 
 ; 
 	 

 
( 
  
D ra   r  c               P     |r
d fd}n fd} t        |i        S )zARetrace a joint graph module and selectively apply decomposition.c                J    t        j                        j                   S r\   r  r  r  )primalstangentsr   decompositionjoint_gmr  s     r_   wrap_fnz$selective_decompose.<locals>.wrap_fn  *    1@@*Mc4 ra   c                 J    t        j                        j                  |  S r\   r  )r   r  r  r  s    r_   r  z$selective_decompose.<locals>.wrap_fn  r  ra   )r}   )r  	list[Any]r  r  )rI   )r  r  r  trace_joint_graphr   r  s   ``` ` r_   rK   rK     s-     	 		
 4773T::ra   c                P     t        j                  ||f      \  }d fd}||fS )Nc                F    t        j                  |       \  }} |i |S r\   )rg   r  )	flat_argsfn_args	fn_kwargsr   r  s      r_   rK  z-wrapper_and_args_for_make_fx.<locals>.wrapped  s*    #229dCW*	**ra   )r  zlist[object]r7  rY   )rg   r  )r   r   r  r  rK  r  s   `    @r_   wrapper_and_args_for_make_fxr    s/    
 ))4.9OIt+ Ira   c               #     K   t        j                         } t        j                  d       	 d  t        j                  |        y # t        j                  |        w xY wwNF)r   is_autocast_cache_enabledset_autocast_cache_enabled)	old_values    r_   disable_autocast_cacher    sF     //1I	$$U+4((3((3s   *A!A A!AA!c                      e Zd Zy)#_ModuleNotInstalledAsSubmoduleErrorNr   rq   ra   r_   r  r    r   ra   r  c                      e Zd ZddZy)
_AttrProxyc                     y r\   rq   )r  basepaths      r_   reset_proxy_mappingz_AttrProxy.reset_proxy_mapping  s    ra   N)r  r2   r  r  r7  r8  )r   r   r   r  rq   ra   r_   r  r    s    ra   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
    c                   t         |           d| _        d| _        || _        d| _        i | _        | j                  j                  d      D ]N  \  }}|| j                  v r,t        j                  d| j                  |   |       d| _        @|| j                  |<   P t               | _        t               | _        t               | _        d| _        t        t               | _        | j                  j                  d      D ],  \  }}| j"                  t%        |         j'                  |       . |  G fddt(              | _        y )NTF)remove_duplicatez<Shared module found between %s and %s, AttrProxy is enabled.r   c                  f     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>.AttrProxyc                   t        |t              r|j                         }t        |t              sJ t	        |j
                  j                  | j
                  |j
                  fi       | _        |j                  | _        |j
                  j                  | j
                  _        |j
                  j                  | j
                  _	        |j                  | <   |j                  | <   y r\   )r   r  get_baser2   r   r   r   __dict__r   r   proxy_pathsproxy_modules)r  r  r  r   s      r_   r  z7_ModuleStackTracer.__init__.<locals>.AttrProxy.__init__  s    dJ/==?D!$/// "&NN++^^T^^4"
 !%,0NN,E,E).2nn.I.I+ ,0""4(-1$$T*ra   c                    t        | t              sJ t        |   |      }t        |t              s|S  |j                  |    dz   |z         S N.)r   r2   r  __getattr__r  )r  r  r  	AttrProxyr   r   s      r_   r  z:_ModuleStackTracer.__init__.<locals>.AttrProxy.__getattr__+  sW    !$/// !7.t4!(F3#O !6+=+=d+Cc+ID+PQQra   c                "    j                   |    S r\   )r  rY  s    r_   r  z7_ModuleStackTracer.__init__.<locals>.AttrProxy.get_base7  s    ++D11ra   c                ^   t        |t              rt        | t        j                  j                        rgt        j                  j	                  t        t        | j                  j                               |               } |j                  |     d|       S t        | t        j                  j                        r^t        j                  j                  t        | j                  j                               |         } |j                  |     d|       S t        | 5  |      S r  )r   slicer   r  
Sequentialr   r]   _modulesr   r  
ModuleListvaluesr  r  )r  rG  r   r  r   r   s      r_   r  z:_ModuleStackTracer.__init__.<locals>.AttrProxy.__getitem__:  s    c5)!$(;(;<#hh11'T]]-@-@-B(CC(HI  )1C1CD1I0J!C5.QRR#D%((*=*=>#hh11$t}}7K7K7M2Ns2ST(1C1CD1I0J!C5.QRRw*3//ra   c                   d| j                   v sJ | j                   d   }t        |t              sJ |j                         D ci c]/  \  }}||% |j                  |    dz   t        |      z         n|1 c}}S c c}}w )Nr  r   )r  r   r   r   r  r  )r  
submodulesrM  r   r  r   s       r_   r  z7_ModuleStackTracer.__init__.<locals>.AttrProxy._modulesK  s    !T]]222!]]:6
!*d333 '1&6&6&8 #U  , "%););D)AC)G#c()RS"#  s   4A>)r  zUnion[Module, _AttrProxy]r  r  r7  r8  )r  r  r7  r  )r7  r2   )rG  zUnion[int, slice]r7  r  )r7  zdict[str, AttrProxy])
r   r   r   r  r  r  r  r  r  r  )r   r  r   s   @r_   r  r    s,    2.
R20"  ra   r  )r  r  record_stack_traces!_record_forward_stack_traces_only
scope_rootenable_attr_proxysubmodule_pathsnamed_modulesr   infor   r  attr_proxy_mapr  counterr   r]   module_id_cacher   r   r  
proxy_type)r  r  r  r  modr  r   r   s        @@r_   r  z_ModuleStackTracer.__init__  s<   #' 15.$!&!44e4L 		/GD!D(((R((+
 *.&*.$$Q'		/ @Q?REVEXDUDW*40666N 	7ID#  C)006	7 D	
 D	L $ra   c                    || j                   u ry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  rK  s      r_   r  z!_ModuleStackTracer.path_of_module[  s^    
 $//!c:&##C((	=((s33 	=51<	=s   A 	AAAc                n   t        |t              r&t        |t        j                        s| j                  st
        |   |||      S t        |t              r|S || j                  vr | j                  ||      | j                  |<   n| j                  |   j                  ||       | j                  |   S r\   )r   r2   rJ  r*   r  r  r   r  r  r  r  )r  r  r  r  r   s       r_   r   z_ModuleStackTracer.getattrk  s     8V,(BNN3))7?43HIIh
+O 4...,0OOHd,KD))==hM""8,,ra   c                   t         |   ||      }t        j                          | j                  j                         D ]+  \  }}|j                  j                  t        t        |      <   - g }| j                  j                         D ]'  \  }}|| j                  v s|j                  ||f       ) dd}	|D ]>  \  }
} |	| j                  |
       | j                  |   }t        || j                  |
       @ |S )Nc                <   |j                  d      }|d d |d   }}t        | t              sJ | }|D ]4  }t        ||      s yt	        ||      }t        |t
        t        f      r4 y t        ||      syt        t	        ||      t
              syt        ||       y)Nr   FT)splitr   r2   r   r   r  delattr)r   r  atomsr  target_submodr  r  s          r_   _delete_proxy_attrz4_ModuleStackTracer.trace.<locals>._delete_proxy_attr  s     LL%E"'*eBi-Dc6***C  !sD) c4(!#
F';< ! 3. gc=9:FC'ra   )r   r2   r  r  r7  r   )r  r2  r   clearr   r   r   r   r   r  r  r  r   r-   )r  r  r7  r   rM  r   !proxy_module_names_to_be_replacedr  r  r$  proxy_module_nameproxy_moduleactual_moduler   s                r_   r2  z_ModuleStackTracer.trace~  s     gmD-0 	0557++113 	NHC?Byy~~3BsG<	N KM) II335 	ILD&+++188$H	I	8 0Q 	F+|tyy*;< ..|<M		3DE	F
 
ra   c                    ddl m} t        ||t        f      r ||i |S 	 t	        j
                  | ||||      S # t        $ r* t        j                  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._dynamor+  r   r*   r,   r  r  r   r   r  )r  r  r  r   r  r+  s         r_   r  z_ModuleStackTracer.call_module  s     	2 a/;78D+F++
	,%%dAwfEE2 	,IIJ A D+F++	,s   ; 0A.-A.c                     yr  rq   )r  r  module_qualified_names      r_   is_leaf_modulez!_ModuleStackTracer.is_leaf_module  s    ra   c                   t        |   |i |}|j                  dvr|j                  j	                  d      '| j
                  j                         |j                  d<   |j                  d   j                         D ]G  \  }\  }}t        |t              s||j                  dz   |j                  z   f|j                  d   |<   I |j                  dk(  r| j                  d|j                  vrx| 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(  torch_fnrd   )r  r  r   r   r   module_stackcopyr   r   r   r   r   r  r   r  r   )r  r   r  r   rM  fqnmod_clsr   s          r_   r  z_ModuleStackTracer.create_node  s[    w"D3F3 7733yy}}./7/3/@/@/E/E/G		+,'+yy1B'C'I'I'K #^c7gt,**S073G3GG9DII/05 GG&&&2$))+ ))2231T5I5I$J`J`5a4bc))33<<=Qt?U?U?^?^>_`%DIIj!
 ra   )r  r*   r7  r8  )r  r2   r7  r  r  )r  Union[Module, Callable]r7  rd  r7  r  )
r  r2   r  r   r   rc  r  r  r7  r8  )r  r2   r.  r  r7  r   )r   rO  r  rO  r7  r	  )r   r   r   r  r  r  r   r2  r  r/  r  r  r  s   @r_   r  r    s    $f$P= --#)-BR-	-&A+A<WA	AF , ,  , !	 ,
 " , 
 ,D! !ra   r  c                      e Zd Z	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZe	 	 	 	 	 	 dd       ZddZe	 	 	 	 dd       Z	ddZ
dd	Zdd
ZddZy)_MakefxTracerNc                   t        |xs i       | _        | j                  j                  t        j                  j
                  j                  j                  t        j                  j                  j                         || _
        || _        || _        || _        || _        || _        d | _        t#               | _        t#               | _        d | _        t#               | _        t#               | _        || _        |	| _        |
| _        y r\   )r   r}   
setdefaultr   r*  r+  r1  r   _decompdecompositionsr  _allow_non_fake_inputsr  record_module_stackr  r  r  r   r  proxy_function_mode	fx_tracerpython_dispatcher_modetorch_fn_metadata_moder  parent_tracerr:  )r  r}   r  r>  r  r?  r  r  r  rD  r:  s              r_   r  z_MakefxTracer.__init__	  s     @D%2@
  	  ++IINN$$,,emm.J.J.T.T	
 ".,B#".)< *>!2N) ;?FQmM 	  59?J}#M 	# $7 6C#6 ra   c                    | j                   | j                  | j                  | j                  | j                  | j
                  gS r\   r  r  r@  rA  rB  rC  r  s    r_   _checkpoint_modesz_MakefxTracer._checkpoint_modes9	  s>    !!OO$$NN''''
 	
ra   c                X    || _         || _        || _        || _        || _        || _        y r\   rF  )r  prev_fake_tensor_modeprev_proxy_modeprev_proxy_function_modeprev_fx_tracerprev_python_dispatcher_modeprev_torch_fn_metadata_modes          r_   _restore_modesz_MakefxTracer._restore_modesC	  s2     !6)#; '&A#&A#ra   c              #  l  K   | j                         }	 ddlm} t        |d      r)| j                  r|j
                  }t        |      | _        nGt               | _        | j                  | j                  _	        | j                  rd| j                  _
        | j                  dk(  rudd l}|j                  j                  j                  |      }|Bdd lmc m} |j'                  d      5  t)        d| j*                   |       d	      }d d d        || _        n| j                  d
k(  rdd l}|j                  j                  j                  |      }|C |       }	dd lmc m} |j'                  d      5  t)        d| j*                  |	      }d d d        |j.                  J d       || _        n'| j                  dk(  st1        d| j                         | j3                  | j                         d   | j4                  |  y # 1 sw Y   xY w# 1 sw Y   xY w#  | j4                  |  w xY w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   rU  rV  2shape_env should be set if tracing with 'symbolic'r  zUnexpected tracing type: )rG  r.  rQ  r   r?  rR  r  rA  rG   r  r  r  r,  _dynamor   r   torch._functorch.configrq  r  patchr&   r>  r  rV  AssertionError_construct_modes_with_fx_tracerrO  )
r  r  r   
prev_modesrQ  r  r   r  _configrV  s
             r_   _init_modes_from_inputsz%_MakefxTracer._init_modes_from_inputsS	  s     ++-
9	-1q+&4+C+C[[
 "4J!?!0!2595M5M2++GKDNND  F*$#(==#6#6#G#G#M #+== PUV +937262M2M&.j*.	,( )9%""j0$#(==#6#6#G#G#M #+ (
I== PUV +938262M2M&/,( (11= H= )9%((F2(3D4E4E3FG  00@D,G   &  D,sO   H4CH  +H
A,H  6HA)H  8H4HH  HH   H11H4c                L   t        || j                  | j                  | j                  | j                        | _        | j                  rt        |      | _        | j                  dk(  s| j                  rt               | _	        t        |      | _        | j                  |_        y )N)r  r  r  rX  )re  r  r  r  r  r  rg  r@  r   rB  rW  rC  r:  )r  rA  s     r_   r^  z-_MakefxTracer._construct_modes_with_fx_tracer	  s    0**!%!:!:)-)J)J
 'CI'ND$ 
*d.?.?*B*DD'&?	&J#(,(@(@	%ra   c              #  &  K   | j                         }	 |j                  | _        dd}|j                  J  ||j                        | _        | j                  | j                         d   | j                  |  y #  | j                  |  w xY ww)Nc                    t        |       t        u r
t               S t        |       t        u rt        | j                        S t	        dt        |        d      )NzUnexpected tracer type: r   )r   rG   r  r  r   rD  s    r_   _create_sub_fx_tracerzD_MakefxTracer._init_modes_from_parent.<locals>._create_sub_fx_tracer	  sV    &/9*,,-(,>>-m.F.FGG&243F2GqI ra   )rD  r_  r7  rG   )rG  r  rA  r^  rO  )r  rD  r_  rf  s       r_   _init_modes_from_parentz%_MakefxTracer._init_modes_from_parent	  s      ++-
	-$1$B$BD! !**6662=3J3JKDN00@D,D,s   BAA= -B=BBc           	         dd l t        j                  fd|      }d fd}dd} ||      } |||      }t        j                  t
         j                        }t               5 }|j                  t         j                                j                  r|j                   j                         |j                   j                         |j                   j                         |j                   j                         |j                  |       |j                  t                      |j                  t!                       j"                  J 	 t%        t'        || j"                   j(                         j"                  t+        |            }		 d d d         j1                         rSj2                  j5                  |      x}
r6|
j6                  *dd
lm}  |	|
j6                  d       |	j=                           j>                  dk(  r) j                  J  j                  j6                  	_        	S # t,        $ r t/        dd  fd	        w xY w# 1 sw Y   xY w)Nr   c                D    j                   j                  j                  S r\   )rJ  _symbolic_tracePH)rd   r   s    r_   r`   z,_MakefxTracer._trace_inner.<locals>.<lambda>	  s    (@(@(C(C ra   c                n    ddfd}d ||d}t        j                  |j                     |       S )Nr   c                   ddl m} j                  J  |d       }t        | t              r"dz  j                  j                  | |      S t        |       t        u rzj                  dk(  rkj                  j                  J d       j                  j                  j                  j                  j                  j                  | |d       | |	      S t        | j                        st        t        |             r0j                  j                  j!                  j                  |       S t        | t"              rJ d
|  d       | S )Nr   )ConstantSourceinputr>   )sourcerX  rY  )positive)hintrp  zScriptObject z. has been fakified. Cannot wrap_fake it again.)torch._dynamo.sourcern  r  r   r   from_tensorr   r   r  rV  create_symintnodecreate_symbolScriptObjectr!   _libraryfake_class_registrymaybe_to_fake_objr    )rj   rn  rp  	arg_countr  r   s      r_   inner_wrap_fakezG_MakefxTracer._trace_inner.<locals>._wrap_fake.<locals>.inner_wrap_fake	  s^    @,,888'%	{(;<a(NI00<<Qv<NN!W^(9(9Z(G00::F LF  00::LL--77EEv F  % M    5#5#56.a:Q >>==OO--q  &a)9: #A3&TU: ra   c                    | S r\   rq   r   s    r_   r`   z@_MakefxTracer._trace_inner.<locals>._wrap_fake.<locals>.<lambda>	  s    ! ra   r  rS  rX  )rj   rO  r7  rO  )rg   r  r  )r   r|  wrap_fn_mapr{  r  r   s      @r_   
_wrap_fakez._MakefxTracer._trace_inner.<locals>._wrap_fake	  s>    I!H $'+K
 ??;t/@/@#A4HHra   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_VARARGSr{   r  )r  phss     r_   
_wrap_funcz._MakefxTracer._trace_inner.<locals>._wrap_func	  sP    GNN1-z:>>!$--669K9KK &aS22Hra   )r   r7  artifactc                     dddS )Nmake_fx_fail_partialstring)r  encodingrq   rq   ra   r_   r`   z,_MakefxTracer._trace_inner.<locals>.<lambda>$
  s     6$,) ra   c                 j     j                   j                  j                  dddd      j                  S )Nr  T)root_moduleverboseinclude_strideinclude_device)rA  rn  python_codesrcr  s   r_   r`   z,_MakefxTracer._trace_inner.<locals>.<lambda>(
  s8    t~~';';'G'G$* $'+'+	 (H (
 c ra   )metadata_fn
payload_fn)insert_deferred_runtime_assertsreenter_make_fxrX  )r   rV   r7  rV   )r  Callable[_P, R]r  zSequence[PHBase]r7  r  ) r,  rg   r  r   r   re  r  r   enter_contextr   r}   r  rB  r@  rC  r  _set_make_fx_tracerrA  rH   rN  r  rc   	Exceptionr"   is_hop_subgraph_tracer_guardsr   rV  torch.fx.passes.runtime_assertr  	recompiler  )r  r  r   r  r  r  r   r  stackr_  r   r  r   s   `           @r_   _trace_innerz_MakefxTracer._trace_inner	  s    	ooCTJ+	IZ	 $!S! .4[["DOO.

 [  	E	$*B*B CD$$##D$9$9: ; ;< 8 89 ; ;<
+ 6 89 3D 9:>>---"T49J9JK>>"'* 	F '')#mm<<TBBB##/V+Ay/B/BDUVKKM
*((444//99AK;   !  % 	  	s    *C/I!AIII!!I*c                x    | j                  ||      5   | j                  |g| cd d d        S # 1 sw Y   y xY wr\   )ra  r  )r  r  r   s      r_   r2  z_MakefxTracer.trace@
  s<    ))!T2 	/$4$$Q..	/ 	/ 	/s   09c                    | j                   d uS r\   re  r  s    r_   r  z$_MakefxTracer.is_hop_subgraph_tracerD
  s    !!--ra   c           
        t        | j                  d| j                  | j                  | j                  | j
                  | j                  |       }|j                  |       5   |j                  |g| cd d d        S # 1 sw Y   y xY w)Nr  re  )	r9  r}   r>  r  r?  r  r  rg  r  )r  r  r   
sub_tracers       r_   trace_subgraphz_MakefxTracer.trace_subgraphG
  s    "$$''$$%%--	

 //5 	5*:**14t4	5 	5 	5s   !A>>B)FNF)r}   r  r  r  r>  r   r  r   r?  r   r  r   r  r   r  r   rD  Optional[_MakefxTracer]r:  r   r7  r8  )r7  r  )rI  zOptional[FakeTensorMode]rJ  z*Union[nullcontext, ProxyTorchDispatchMode]rK  z0Union[nullcontext, PreDispatchTorchFunctionMode]rL  zOptional[PythonKeyTracer]rM  zUnion[nullcontext, Any]rN  z-Union[nullcontext, TorchFunctionMetadataMode]r7  r8  )r  r   r   rc  r7  Generator[None, None, None])rA  r_  r7  r8  )rD  r9  r7  r  )r  r   r   rO  r7  r*   )r  r   r   rO  r7  r  r  )r   r   r   r  rG  rO  r   ra  r^  rg  r  r2  r  r  rq   ra   r_   r9  r9  
	  sE    %*15$),7D,7 ,7 !%	,7
 ,7 ",7 #,7 '+,7 ",7 /,7 ",7 
,7\
B7B DB #S	B
 2B &=B &SB 
B  =-=-!3=-	$=- =-~A( -*-	$- -6{z/.5ra   r9  r  _CURRENT_MAKE_FX_TRACERc              #  8   K   t         }	 | a d  |a y # |a w xY wwr\   )r  )r   prev_tracers     r_   r  r  Z
  s'      *K."("-+s    )r  r?  r  r  r  r:  c                    |dv sJ ddl m}
 t        ||||||||xs |
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:  c                 *     j                   g|  S r\   )r2  )r   r  make_fx_tracers    r_   rK  zmake_fx.<locals>.wrapped
  s    #~##A---ra   )r   rO  r7  r*   )r  r  r9  r2  provenance_tracking_levelr  rL  )r  r}   r  r>  r  r?  r  r  r  r:  r  rK  r  s   `           @r_   rI   rI   e
  sw    * 7777&"$/ 7<<11Q6/N __Q. . Nra   c                 R    t         j                  j                  j                         S r\   )r   r   _python_dispatch _get_current_dispatch_mode_stackrq   ra   r_   get_torch_dispatch_modesr  
  s    ;;''HHJJra   c                     t               S r\   )rN   rq   ra   r_   rM   rM   
  s    ra   c                 4   t         j                  j                  t         j                  j                  j
                        } t         j                  j                  t         j                  j                  j
                        }| |J d|  d|        | xs |S )z
    Current the currently active proxy tracing mode, or None if
    we are not currently tracing.  This includes pre-dispatch proxy
    tracing.
    zpre_dispatch_mode=z, mode=)r   rj  _get_dispatch_mode_pre_dispatchr  r  r  _get_dispatch_mode)pre_dispatch_moder&  s     r_   rN   rN   
  s     

BB&&,, 88&&uxx'E'E'K'KLD$ 
./wtf=4 $$ra   c                    t               }|sJ t               5  g }|j                  | |||      cddd       S # 1 sw Y   yxY w)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)rN   r   r  )r   r   r  r&  r\  s        r_   rO   rO   
  sM     DK4 
%	& @$$T5$?@ @ @s	   9Ac                 \    t        t        j                  j                  j                        S r\   )r4   r   r  r  r  rq   ra   r_   r   r   
  s    uxx==CCDDra   c                   ddl m} t        v rb|j                  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 t        S # 1 sw Y   t        S xY w)Nr   )CompilerBisectoraot_eager_decomp_partitionr  c                     t               S r\   )repr)r   s   r_   r`   z%maybe_handle_decomp.<locals>.<lambda>
  s    48 ra   r>   )!torch._inductor.compiler_bisectorr  rU   disable_subsystemr  rh  )r  r   r   r  r  r   s    `    r_   r  r  
  s     C	((--(/;K
 "! 	$$)$-b14B6BC$$)$		 	 	 s   :A88Bc                    t        | ||      \  }}t               5   t        |||      |      }ddd       |S # 1 sw Y   S xY w)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   rI   )r   r   r  r  r}   rK  all_argsr  s           r_   get_isolated_graphmoduler  
  sY     5T4HGX	$	& 
W)<<

 I	 Is	   :Ac                >   ddl m} t        j                  j	                  t        j                  j
                  j                        }|rT|j                  rG ||j                  |       x}r1t        |t              sJ |       ||j                  j                  d<   yyyy)zOA helper function for setting up unbacked_bindings on the destination FX graph.r>   )compute_unbacked_bindingsunbacked_bindingsN)r.  r  r   r  r  r  FAKErV  r   r+   r   r   )r   rz  r  r   symbol_to_paths        r_   rP  rP  
  s    : ++EHH,J,J,O,OPIY((6y7J7JCPP>Pi/::/7EINN 34 Q )yra   )ru   r  ry   r   r7  r  )r}   r  r7  z4Generator[Mapping[OpOverload, Callable], None, None])r   r   r7  r   )r   r   r   r_  r   r   r7  r8  )r   r
  r   r_  r   r+   r7  r8  )r   r   r   r_  r   r   r7  r8  r  )r7  r  )r   z.Union[PySymType, _AnyScriptObjectType, Tensor]r   r_  r   rO  r7  r8  )r   r   r   r_  r7  r   )r   r   r   r_  r7  r   )r   r   r   r_  r   rW   r7  zUnion[_ProxyTensor, U])
r   r   r   r_  r   rW   r   zCallable[[_ProxyTensor], R]r7  Union[R, U])r   r
  r   r_  r7  r+   )r   r
  r   r_  r   rW   r7  zUnion[Proxy, U])
r   r
  r   r_  r   rW   r   zCallable[[Proxy], R]r7  r  )r   r   r   r_  r7  r   )r   r   r   r_  r   rV   r7  zUnion[T, _PySymProxyType])
r   r   r   r_  r   rW   r   zCallable[[_PySymProxyType], R]r7  r  )
r   .Union[Tensor, _AnyScriptObjectType, PySymType]r   r_  r   rO  r   r   r7  rO  )r7  z*dict[type[sympy.Expr], Callable[..., Any]]r\   )r   r_  r   z
sympy.Exprr   zPySymType | Noner7  z1IntLikeType | FloatLikeType | BoolLikeType | None)F)r   r   r   r   r7  rU  )r   r   r   r   r7  r   )r   r_  r
  r   r7  r  )r   r+   r   r   r7  r+   )
r   r_  r  r  r   r5  r  r6  r7  zThunk[R])
r;  r   r   r+   r<  rU  r   r_  r7  r8  )
rQ  rV   rR  r?  r<  rI  r   r_  r7  rV   )r   r_  r7  z5Callable[[PySymType], Union[bool, int, float, Proxy]])r   r_  r_  r   r7  zUnion[_ProxyTensor, Tensor])r   r_  r_  r
  r7  r  )r   r_  r_  r   r7  z!Union[_PySymProxyType, PySymType])r   r_  r_  r  r7  rO  )r   rO  r  re  r7  r8  )r  z'Union[list[object], tuple[object, ...]]r   r_  r7  z-tuple[list[object], tuple[object, ...], bool])r  re  r   rB   r  r   r   rc  r  r  r7  rO  )r  r   r  z:Optional[tuple[tuple[Argument, ...], dict[str, Argument]]]r7  r   )r'  ztype[TorchFunctionMode]r7  zCCallable[[], _GeneratorContextManager[Optional[TorchFunctionMode]]])r  r7  r   r,   r7  zOptional[tuple[Any, ...]]r7  r*   )
r  zCallable[[Unpack[_Ts]], R]rJ  ztuple[Unpack[_Ts]]r   r_  r  r   r7  r  )r   z+OpOverload | torch._ops.HigherOrderOperatorr7  r  )
r   r_  r   rB   r   rc  r   rO  r7  r8  )
r   r_  r   rB   r   rc  r   r   r7  r+   )r  r  r  r   r7  r  )r   zCallable[..., R]r   rc  r  r  r7  z0tuple[Callable[[list[object]], R], list[object]])r   r9  r7  r  )Nr  F)r  r   r}   r  r  r  r>  r   r  r   r?  r   r  r   r  r   r  r   r:  r   r7  zCallable[..., GraphModule])r7  zlist[TorchDispatchMode])r7  z Optional[ProxyTorchDispatchMode])r   r  r   r5  r  r6  r7  rY   )r7  z-Generator[ProxyTorchDispatchMode, None, None])
r  re  r   rB   r   rc  r  r  r7  rO  )r  N)r   r   r   rc  r  r  r  r  r}   r  r7  r*   )r   rO  rz  r?  r7  r8  )
__future__r   r  r  loggingr   	threadingr   r  r  collectionsr   r   collections.abcr   r   r   r	   
contextlibr
   r   r   r   dataclassesr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   
torch._opstorch.fxrJ  torch.fx.tracebackr  rR  torch.utils._pytreer   _pytreerg   r   r   r   torch._dispatch.pythonr   "torch._library.fake_class_registryr    torch._library.opaque_objectr!   torch._loggingr"   r#   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/   r0   torch.fx.passes.shape_propr1   torch.nnr2   r#  r3   torch.utils._python_dispatchr4   r5   r6   r7   r8   torch.utils._statsr9   torch.utils._thunkr:   torch.utils.weakr;   r<   r=   _backward_stater?   sym_noder@   r\  rA   r   rB   torch.fx._symbolic_tracerC   torch.typesrD   rE   rF   __all__r_  rw  r   r
  r*  r+  r  	getLoggerr   r   _logginggetArtifactLoggerr  rU   r   r  rV   rW   rX   rY   rZ   r   null_ctx_typeregister_pytree_nodeSizerc   !_pytree_subclasses_that_lose_infor{   r   rO  
proxy_slotr   
no_defaultrL   r   r   r   r   localr   r   r   r   r   r   r   r   cacher   r   r   r]   r   r  r   r   r   r   r   r  rQ   rP   r  r  r>  r?  r@  rS  r   r\  r`  r  r  r  rw  r  r  r  r   rG   r  r)  _disable_dynamorH   rN  rO  rU  rW  )_temp_remove_metadata_torch_function_moderg  -_temp_remove_pre_dispatch_torch_function_modere  r   r  r   GraphAppendingTracerrR   InterpreterrJ   r  rK   r  r  r  r  r  r  r9  r  r  rI   r  rM   rN   rO   r   r  r  rP  rq   ra   r_   <module>r     s   #         0 B B W W !	 	 	 D C %    ) ) $ $ ) ) ; ? 7 + * 4  7 / / . 
 @  -  % $ W W *  .%/DD AB&&(89 U//1AAB yy~~yy~~g!nn66xARS =? : ? CLCLt_CL5[!   	JJ &
" &+ZZ$7 !B 	>@	>9	> 	> X
	 	 \
 /x M
 
 W 
 W 
	'3<A	 

 
	(1@	 

)//  &A%B " EG ')A F4 )> )>X.	7.. . 
	.bD , 
	  
 
!	!! ! 	! 
! 
	  +	
  
 
	  
 
	  	 
 
	  $	
  
 
	  
 
$	$$ $ 	$ 
$ 
	  .	
  
 !%	)	7)) ) 	)
 )X    EIcc *c1Ac6cL 	$%S##$		 %(P ,0%%%)% % %$    0  , 5< HQ  FBFB FB/?FBIUFB	FBR 	8O$gfo.E&FF 
H%&8H0H(II
TTT '	T
 T Tn   
:* 
&&#& & 
&
 
--1-'- 
-
 
,,&,&, 
,
((K(( ++Z8=
=4=	=@(K=(KGS(K2(KVF&F
F F 	F
 F FR' '>   
vf vv OS))K) 
)X$$$H$N  04)N
!)N)N -)N 	)N )NX'!'' ' 	'
 'V #' & 
5   % 1 %$ -S- )<%#4 <%~ 1W 1 -
_. _D11 *12D1KQ1	1 *2DKT
D"bhh;; "4@0r~~ @0F9R^^ 9x;;
 ; ;6
"4>O5 4 4	) 	
 
g gT	J5 J5Z
 48 0 7 . . DH#(	*  %!&)- % %**@* * !	* * * * #'* * *  *ZK% @
@
@ @ 	@( E E&  	
 6 CG

  	
 A ,Fra   