
    ^j                       d dl m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	 d dl
mZmZ d dlmZ er d dlZd dlmZmZmZ d dlmZ d d	lmZ d d
lmZ d dlZd dlmc mZ d dlm c m!Z" d dl#m$Z% d dlm&Z&m'Z' d dl(m)Z) d dl*m+Z+m,Z,m-Z-m.Z.m/Z/ ej`                  jc                  e2d      Z3d%dZ4 ejj                  d      	 	 	 	 	 	 	 	 d&d       Z6d'dZ7d(dZ8 G d dejr                        Z: G d de/      Z;ejx                  d)d       Z= e;       f	 	 	 	 	 d*dZ> G d de      Z? G d de?      Z@ G d  d!e?      ZA G d" d#e?      ZBd+d$ZCy),    )annotationsN)ABCabstractmethod)AbstractContextManager)AnyTYPE_CHECKING)Self)Callable	GeneratorSequence)TracebackType)FunctionalizeInterpreter)
OpOverload)$_functionalization_reapply_views_tls)_get_dispatch_mode_pre_dispatchTorchBindOpOverload)is_sparse_any)_detect_infra_mode_disable_infra_modeautograd_would_have_decomposedreturn_and_correct_aliasingTorchDispatchModenot_implementedc                    | D cg c]5  }|t         j                  t         j                  j                  t        fvr|7 }}|rt
        j                  d|       t        |      S c c}w )Nz.FunctionalTensor unrecognized subclass(es): %s)torchTensor_subclasses
FakeTensorFunctionalTensornot_implemented_logdebugbool)typestunrecognized_typess      n/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_subclasses/functional_tensor.py_has_unrecognized_tensor_typesr'   '   sn     U\\5#4#4#?#?AQRR 	
 
 !!<>P	
 "##s   :A$i   )maxsizec                   |r1| t         j                  j                  j                  j                  u ryddlm}  ||       ryt        d | j                  j                  D              }|s| j                  j                  ry|r=|r:| j                  dvr+| j                         rt        j                  d|  dd	
       yyy)zFast path for _can_decompose that depends only on (func, export, pre_dispatch).

    Returns True/False for a definitive answer, or None to fall through
    to the slow path (autograd_would_have_decomposed).
    Fr   )#_should_decompose_because_unsafe_opTc              3  4   K   | ]  }|j                     y wN)
alias_info).0args     r&   	<genexpr>z&_can_decompose_fast.<locals>.<genexpr>E   s     NS^^Ns   )atenprimzAt pre-dispatch tracing, we assume that any custom op marked with CompositeImplicitAutograd and have functional schema are safe to not decompose. Found z to be one such op.   )
stacklevelN)r   opsr1   dropoutdefaulttorch._decompr*   any_schema	arguments
is_mutable	namespace_can_decomposewarningswarn)funcexportpre_dispatchr*   alias_info_presents        r&   _can_decompose_fastrE   4   s     $%))..00888A*40Nt||7M7MNNT\\44~~%55$:M:M:O!F"57  !	     c                (   t         j                  j                  t         j                  j                  j                        }t         j                  j                  t         j                  j                  j                        }|s|rt        |       y y r,   )r   _C&_dispatch_tls_is_dispatch_key_includedDispatchKeyFunctionalize&_dispatch_tls_is_dispatch_key_excludedAssertionError)msgis_includedis_excludeds      r&    _assert_functionalize_not_activerQ   X   sk    ((AA**K ((AA**K ;S!! ';rF   c                      d fd}|S )Nc                0     | j                   |i i |S r,   )to)selfargskwargsextra_kwargss      r&   _z&_conversion_method_template.<locals>._r   s$    tww; :6 :\ :;;rF   )rU   r   rV   r   rW   r   returnr    )rX   rY   s   ` r&   _conversion_method_templater\   q   s    < HrF   c                      e Zd ZU dZded<   ej                  j                  j                  Z	ej                  j                  j                  ej                  j                  j                        Z eej                   j"                  j$                  j&                  ej                   j"                  j$                  j(                  ej                   j"                  j*                  j&                  ej                   j"                  j,                  j&                  ej                   j"                  j.                  j&                  ej                   j"                  j0                  j&                  ej                   j"                  j2                  j&                  ej                   j"                  j4                  j&                  ej                   j"                  j6                  j&                  ej                   j"                  j8                  j&                  ej                   j"                  j:                  j&                  ej                   j"                  j<                  j&                  ej                   j"                  j>                  j&                  ej                   j@                  jB                  j&                  h      Z"dZ#ded<   ddZ$	 	 d	 	 	 	 	 	 	 	 	 ddZ%dd	dd
Z&e'd d       Z(d!dZ)d"dZ*d#dZ+d$dZ,d$dZ-d$dZ.d%dZ/d& fdZ0	 d'	 	 	 	 	 	 	 d(dZ1 e2ejf                        Z4 e2 ejB                  d            Z5 e2ejl                        Z6 e2ejn                        Z8 e2ejr                        Z: e2ejv                        Z< e2ejz                        Z= e2ej|                        Z? e2ej                        ZA e2ej                        ZC	 d'dd	 	 	 	 	 d)dZDeEd*d       ZFd+dZG xZHS ),r   aF  
    Functional tensors represent tensors that will remove mutations
    from a program. If you perform a mutable operation on a functional tensor,
    it will re-dispatch to the functional variant of that operation.

    Historically, functionalization is implemented in C++ in the dispatcher.
    This class is a lightweight python shim around the C++ functionalization logic.

    FunctionalTensor is required to be used with a corresponding
    FunctionalTensorMode active, because it relies
    on using the mode for dispatch (which can properly handle factory functions).
    torch.TensorelemNzFunctionalTensor | None_inference_mode_basec                   t        j                  |      st        d      t        j                  t         j
                  j                  |      z  }t         j                  j                  | |j                  t        |      s|j                         nd t        |      s|j                         nd d |j                  |j                  |j                  d|j                   d dd|      }t         j
                  j#                  |       ||_        t         j&                  j(                  j*                  rt        j,                         rt         j.                  j(                  j0                  r}|j$                  j3                         }|j5                         rd |_        ||j8                  |<   |S |j8                  j;                  |      |_        |j6                  ||j8                  |<   |S )Nz elem must be a functional tensorF)r   _is_functional_tensorrM   r   _extra_dispatch_keysrH   _dispatch_keysr   _make_wrapper_subclassshaper   stridestorage_offsetdtypelayoutdevicerequires_grad_set_throw_on_mutable_data_ptrr_   _exportconfig(enable_auto_functionalized_v2_for_exportis_inference_mode_enabled	_inductorenable_auto_functionalized_v2untyped_storageis_base_tensorr`   _storage_to_baseget)clsr_   modeextra_dispatch_keysoutstorages         r&   __new__zFunctionalTensor.__new__   s   **40 !CDD& 11EHH4K4KD4QQ 	 ll11
 JJ!.t!4DKKM$-:4-@##%dJJKKKK)
, 	//4 MM  II//1&&DDhh..0G!!#+/( 25%%g.
 
 ,0+@+@+D+DW+M(++358D))'2
rF   c                Z   t        |      rt        S |i }|t        j                  v r#t	        |      dk7  rt        d      |t        j                  j                  j                  j                  t        j                  j                  j                  j                  fv rXt	        |      dk7  st        |d   t              st        d       |t        j                  |d   j                        |d         S t	        |      dk7  st        |d   t              st        d       |t        j                  |d   j                              S |t        j                  j                  j                   j                  u r&t	        |      dk(  rt        |d   t              r|d   S t#        d      )Nr   z+kwargs must be empty for metadata functions   z+Expected 2 args with FunctionalTensor first   z$Expected 1 arg with FunctionalTensorzqAttempting to use FunctionalTensor on its own. Instead, please use it with a corresponding FunctionalTensorMode())r'   NotImplementedr   metadata_fnslenrM   r   r5   r1   is_strides_like_formatr7   is_contiguousmemory_format
isinstance_from_functional_tensorr_   detachRuntimeError)rU   rA   r#   rV   rW   s        r&   __torch_dispatch__z#FunctionalTensor.__torch_dispatch__   sb    *%0!!>F #000 6{a$%RSS		55==		,,::  t9>DG=M)N()VWWE99$q',,GaQQ4yA~ZQ9I%J$%KLL55d1gllCDD599>>((000
 4yA~*T!W6F"GAw 
 	
rF   )tensor_contentsc               4    dt        | j                         dS )NzFunctionalTensor())reprr_   )rU   r   s     r&   __repr__zFunctionalTensor.__repr__"  s    "4		?"3155rF   c                   t        j                  |       rt        d      t        j                  |       }t	        t         j
                  j                  j                        }|t        d      |5  t        j                  | |       t        ||      }t        j                  ||       d d d        |S # 1 sw Y   S xY w)Nz)x must not already be a functional tensor functional_mode must not be None)
r   rb   rM   _to_functional_tensorr   rH   _TorchDispatchModeKey
FUNCTIONAL_mirror_autograd_meta_tor   )xx_functionalfunctional_moder{   s       r&   to_functionalzFunctionalTensor.to_functional%  s     &&q) !LMM
 2215 -UXX-K-K-V-VW" !CDD 	>**1l;"<AC**<=	> 
		> 
s   29B55B?c                j    t        j                  |        t        j                  | j                        S r,   )r   _syncr   r_   rU   s    r&   from_functionalz FunctionalTensor.from_functional@  s#    D,,TYY77rF   c                @    t        j                  | j                        S r,   )r   _is_functional_tensor_baser_   r   s    r&   ru   zFunctionalTensor.is_base_tensorD  s    //		::rF   c                D    t        j                  | j                  |       y r,   )r   _functionalize_replacer_   )rU   outputs     r&   replace_zFunctionalTensor.replace_G  s    $$TYY7rF   c                B    t        j                  | j                         y r,   )r   _functionalize_commit_updater_   r   s    r&   commit_updatezFunctionalTensor.commit_updateJ  s    **4995rF   c                B    t        j                  | j                         y r,   )r   _functionalize_syncr_   r   s    r&   synczFunctionalTensor.syncM  s    !!$)),rF   c                B    t        j                  | j                         y r,   )r   1_functionalize_mark_mutation_hidden_from_autogradr_   r   s    r&   "mark_mutation_hidden_from_autogradz3FunctionalTensor.mark_mutation_hidden_from_autogradP  s    ??		JrF   c                ^   | j                   j                         dk(  r| j                   j                         S | j                   j                         dk(  r(| j                   D cg c]  }|j                          c}S | j                   D cg c]  }|j                          c}S c c}w c c}w )Nr   r   )r_   dimitemtolist)rU   r_   s     r&   r   zFunctionalTensor.tolistS  su    99==?a99>>##YY]]_!,0II6DDIIK66.2ii8dDKKM88 78s   #B%B*c                &   t        t        j                  j                  j                        j
                  rKt        j                  j                  j                  | | j                  | j                  | j                         t        | 4  |i |S )N)ri   rk   rj   )r   r   rH   r   r   rB   r5   r1   _assert_tensor_metadatari   rk   rj   superrT   )rU   rV   rW   	__class__s      r&   rT   zFunctionalTensor.to[  sj    ehh<<GGHOOIINN22jj{{{{	 3  wz4*6**rF   c                    |xs t         j                  j                         }t        |      dkD  r | j                  |g|i |S  | j                  dd|i|S )Nr   rk   r[   )r   cudacurrent_devicer   rT   )rU   rk   rV   rW   s       r&   r   zFunctionalTensor.cudaf  sZ     65::446t9q=47763D3F334773&3F33rF   ri   cpu)rk   )masked_gradc                  | j                   t        j                  k(  r|| S | j                  |      S | j                  j                  ||      }t        |t        j                        rgt        j                  |      rRt        t        j                  j                  j                        }|t        d      |5  t        ||      cd d d        S |S # 1 sw Y   |S xY w)Nr   )ri   r   r   )rj   r   stridedrT   r_   to_denser   r   rb   r   rH   r   r   rM   r   )rU   ri   r   r{   r   s        r&   r   zFunctionalTensor.to_dense{  s     ;;%--'}777''ii  u+ Fc5<<(U-H-H-M0..99O &$%GHH  >'_=> >
>
s   <CCc                .    | j                   j                  S r,   )r_   rj   r   s    r&   rj   zFunctionalTensor.layout  s     yyrF   c                4    t        | j                               S r,   )r"   r   r   s    r&   __bool__zFunctionalTensor.__bool__  s    DIIK  rF   )r_   r^   ry   FunctionalTensorModerZ   r	   r[   N
rA   r   r#   Sequence[type]rV   tuple[Any, ...]rW   zdict[str, Any] | NonerZ   r   )r   zobject | NonerZ   str)r   r^   rZ   r   )rZ   r^   rZ   r"   )r   r^   rZ   None)rZ   r   )rZ   r   )rV   r   rW   r   rZ   r^   r,   )rk   ztorch.device | int | str | NonerV   r   rW   r   rZ   r^   )ri   ztorch.dtype | Noner   zbuiltins.bool | NonerZ   r^   )rZ   ztorch.layout)rZ   zbuiltins.bool)I__name__
__module____qualname____doc____annotations__r   rH   r   r   	_mode_key,_additional_keys_to_prop_for_wrapper_tensorsaddrJ   
ZeroTensorrc   	frozensetr5   r1   r   r7   r   r   is_non_overlapping_and_densesizesym_sizerg   
sym_striderh   sym_storage_offsetnumel	sym_numelr   r2   rk   r   r`   r}   r   r   staticmethodr   r   ru   r   r   r   r   r   rT   r   r\   int8charr   bfloat16uint8bytefloat64doublefloat32floatr"   float16halfint32intint64longr   propertyrj   r   __classcell__r   s   @r&   r   r   x   sM     ..99I !88PPTT'' IINN((00IINN((66IINN1199IINN77??IINN''IINN##++IINN!!))IINN%%--IINN))11IINN--55IINN  ((IINN$$,,IINN&&IINN!!))	
L( 5918@L !#(,0
0
 0
 	0

 &0
 
0
d <@ 6  48;86-K9+ 9=454EH4TW4	4 'UZZ8D
%\U\\%-@
AC*@H&U[[9D(u}}=F'emm<E&UZZ8D&U]];D
%EKK
8C&U[[9D
 %) -1	! *	
 
,    !rF   r   c                       e Zd Z	 	 	 d	 	 	 	 	 	 	 d	 fdZd
 fdZ	 	 	 	 	 	 	 	 d fdZ	 	 d	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZe	dd       Z
 xZS )r   c                b   t         |           || _        d| _        g | _        t
        j                  j                  j                  | _	        || _
        |r$t
        j                  j                  j                  nd | _        i | _        i | _        || _        t#        j$                         | _        y NF)r   __init__rB   is_on_stackenter_stackr   rH   r   r   r   rC   rJ   PreDispatch_dispatch_key_tokens_tokens_forward_output_allow_token_discoveryweakrefWeakKeyDictionaryrv   )rU   rC   rB   r   r   s       r&   r   zFunctionalTensorMode.__init__  s     	  77BB(AMUXX11==SW 13 @B# '=# %%' 	rF   c                     d fd} |       ) j                   j                  d       t                  S  j                   j                  d        S )Nc                 B    j                   t        j                  j                  j                  k(  r-t        t        j                  j                  j                        S t        j                  j                  t        j                  j                  j                        S r,   )	r   r   rH   rJ   r   r   r   r   _get_dispatch_moder   s   r&   _get_prev_modez6FunctionalTensorMode.__enter__.<locals>._get_prev_mode  sm    !!UXX%9%9%E%EE6HH22==  88....99 rF   TF)rZ   FunctionalTensorMode | None)r   appendr   	__enter__)rU   r   r   s   ` r&   r  zFunctionalTensorMode.__enter__  sN    	 ###D)7$&&##E*KrF   c                b    | j                   j                         }|rt        |   |||       y y r,   )r   popr   __exit__)rU   exc_typeexc_valexc_tbr   r   s        r&   r  zFunctionalTensorMode.__exit__  s2     &&**,GXw7 rF   c                    |i }t        |      rt        S |t        j                  vrq j	                  |||      r^t
        j                  j                  |j                               r1 5   |j                  |i |}|t        ur|cd d d        S 	 d d d        d fd}dd}ddl
m}m}	m}
  ||      r:t
        j                  j                  |j                         t
        j                  j                  j                         sdd lmc m} dd lmc m} t
        j,                  j/                         r"|j0                  r |
 |||      S  |	 |||      S |j2                  svt5        |t
        j6                  j8                        r]t
        j:                  j<                  j?                  |      s)t
        j:                  j<                  jA                  |      r |
 |||      S  |	 |||      S ddl!m"}m#}  ||      rt
        j                  j                  |j                         t
        j                  j                  j                         rtI        d|j                          d       | jJ                   jL                  |||      S tO        jP                  t        |||f      \  }}tS        d       t
        j                  jU                         t
        j                  jW                  t
        j                  j                  j                         z  }t
        j                  jY                         j[                  t
        j                  j                  j                         t        j\                  z
  }t5        |t^              rGta               }|jc                  |      }|jc                  |      } ||i |}|je                  |      }nt
        j                  jg                  ||      5  	 t        jh                  d	      }|t        j                  v rit
        jj                  jl                  jn                  jq                         5   ||i |}d d d        tO        jP                  t
        jr                  |      }n ju                  ||        |jv                  t
        j                  j                  j                   g|i |} jx                  rE|t
        jz                  j|                  j~                  j                  u rt        j                  |       tO        jP                  t
        jr                  ||      }t        j                          t        jh                  |       	 d d d        tS        d
       t        d tO        j                        D              r0|t
        jz                  j|                  j                  j                  u r|S t
        j                  j                  |j                  v ri|t
        jz                  j|                  j                  j                  ur9t
        j<                  j                  j                         5   ||i | d d d        t        ||||      S # 1 sw Y   xY w# 1 sw Y   .xY w# t        j                          t        jh                         w xY w# 1 sw Y   QxY w# 1 sw Y   lxY w)Nc                    t        | t              rt        d      t        | t        j                        r!t        j
                  |       rt        |       S | S )Nz*x must not be a FunctionalTensor in wrap())r   r   rM   r   r   rb   )r   rU   s    r&   wrapz5FunctionalTensorMode.__torch_dispatch__.<locals>.wrap  sI     !-.$%QRR!U\\*u/J/J1/M'400HrF   c                    | j                   S r,   )r_   )r   s    r&   unwrapz7FunctionalTensorMode.__torch_dispatch__.<locals>.unwrap  s    66MrF   r   )can_auto_functionalizedo_auto_functionalizedo_auto_functionalize_v2)handle_effectshas_effectszfunc zE with effects should not have a kernel for Functionalize dispatch keyz?Functionalization should not already be enabled above this modeTzNFunctionalization should not already be enabled above this mode after dispatchc              3  <   K   | ]  }t        |t                y wr,   )r   r   )r.   r   s     r&   r0   z:FunctionalTensorMode.__torch_dispatch__.<locals>.<genexpr>  s        1./s   )r   objectrZ   r  )r   r   rZ   r^   )Nr'   r   r   r   r>   r   rH   _dispatch_has_kernelname	decompose*torch._higher_order_ops.auto_functionalizer  r  r  %_dispatch_has_kernel_for_dispatch_keyrJ   rK   torch._export.configrn   ro   torch._inductor.configrr   compileris_exportingrp   rs   r   _opsr   _libraryutilsis_out
is_inplacetorch._higher_order_ops.effectsr  r  rM   r   r   pytreetree_map_onlyrQ   _dispatch_tls_local_include_setDispatchKeySet_dispatch_tls_local_exclude_setremoverc   r   PythonFunctionalizeAPIunwrap_tensorswrap_tensors_ForceDispatchKeyGuard#_functionalize_enable_reapply_viewsfxexperimentalproxy_tensordisable_proxy_modes_tracingr   _sync_view_replay_annotations_op_dkrB   r5   r1   r6   r7   _freeze_functional_tensor_disable_functionalizationr9   tree_leaves
lift_freshTaginplace_viewtagsset_source_Tensor_mode_utilsno_dispatchr   )rU   rA   r#   rV   rW   rr
  r  r  r  r  export_configinductor_configr  r  args_unwrappedkwargs_unwrappedinclude_to_setexclude_to_setctxfully_unwrapped_argsfully_unwrapped_kwargsouts_unwrappedouts_wrappedold_apply_viewss   `                        r&   r   z'FunctionalTensorMode.__torch_dispatch__  s    >F)%0!! (555##D$7 --diik: "DNND3F3N* *
			
 	
 "
((@@IIK--;;
 98<<~~**, II3D$fMM,T4vFF<<4!6!67NN((//5~~++66t< 0dD&II(tT6BBOtxx==		UXX11?? %DIIK=(mn  "++T\\4v  ,2+?+?ftVn,
(( 	)M	
 HH446hh%%ehh&:&:&H&HIJ 	
 HH446==$$22 334 	 d/0
 )*C#&#5#5d#; %(%7%7&" "%(N ++N;L 00P +O*O&+&O&OPT&UO /<<< #XX22??[[] W-1>-VEU-VNW'-';';!LL$( ::4H *5!HH00>>*+* /*  ;;#uyy~~'='='E'EE % ? ? O'-';';!LL$( 446==oNW+OZ 	)\	
  ++L9  uyy~~00888 II""dii/EIINN//===((446 &d%f%& +4v|LL] jW W4 446==oNW+O +OF& &sV   +Y30Z;2AZ	Z C.Z)Z;	[3Y= Z
	Z+Z88Z;;[[c                X   t         j                  j                  t         j                  j                  j                        }|t        j                  ||g      D ]  }t        |t              st        j                  |j                        }	 |j                  j                  |   }|j                   j"                  }t%        j&                  |      5  t        j(                  |       ddd        yy# t        $ r |j                  rt        d| d      dY w xY w# 1 sw Y   xY w)a[  Sync FunctionalTensor args so view replay uses correct fx node metadata.

        When functionalization encounters a mutation, it handles aliases by lazily
        regenerating them at the first time they are next used. This is a problem when
        plumbing user annotations during tracing: we want view ops from view replay to
        have the same annotation the user specified on the original views. But view
        replay happens the next time the alias is used (e.g.
        second_op(alias_with_pending_mutation)), so the regenerated views would get the
        metadata for second_op instead.

        To fix this, we remember the node metadata from the original views and globally
        set it when we lazily perform view replay. The globally set metadata will be
        used to populate the fx node created for the replayed operation.
        Nzcannot find z in tensor_tracker)r   rH   r   r   PROXYr#  r6  r   r   r   r_   tracertensor_trackerKeyErrorrl   r   proxynodefx_tracebackset_current_replay_noder   )rU   rV   rW   ma	unwrappedtracker_entry	curr_nodes           r&   r2  z2FunctionalTensorMode._sync_view_replay_annotations  s   & HH''(F(F(L(LM=''v7 #!!%56!99!&&A	$%HH$;$;I$FM *//44	!99)D #KKN# #)#     !..**9+5GH#$ # #s   C5D 5%DD D)	c                    t        || j                  | j                        }||S t        j                  ||f      \  }}t        ||      S r,   )rE   rB   rC   r#  tree_flattenr   )rU   rA   rV   rW   resultflat_args_kwargsrY   s          r&   r>   z#FunctionalTensorMode._can_decompose  sQ     %T4;;8I8IJM %114.A!-d4DEErF   c                     y)NTr[   )rx   s    r&   is_infra_modez"FunctionalTensorMode.is_infra_mode  s    rF   )FFF)rC   r"   rB   r"   r   r"   rZ   r   )rZ   r	   )r  ztype[BaseException] | Noner  zBaseException | Noner  zTracebackType | NonerZ   r   r   r   )rV   r   rW   dict[str, Any]rZ   r   )rA   r   rV   r   rW   r`  rZ   r"   r   )r   r   r   r   r  r  r   r2  r>   classmethodr_  r   r   s   @r&   r   r     s    #',	"("( "( !%	"(
 
"(J"8,8 &8 %	8
 
8 !#(,BMBM BM 	BM
 &BM 
BMH*#*# *# 
	*#XFF F 	F
 
F$  rF   r   c                 \    t        t        j                  j                  j                        S r,   )r   r   rH   r   r   r[   rF   r&   disable_functional_moderc    s    uxx==HHIIrF   c                0     ddddd fd}|S )Nc                d    t        | t        j                        rt        j	                  |       S | S r,   )r   r   r   r   r   r$   s    r&   to_funz&dispatch_functionalize.<locals>.to_fun  s&    a&#11!44rF   c                   t        | t              s<t        | t        j                        r t        j                  |       rt        d      | S t        j                  |        t        j                  | j                        S )NzCNon-FunctionalTensor torch.Tensor should not be a functional tensor)	r   r   r   r   rb   rM   r   r   r_   rf  s    r&   from_funz(dispatch_functionalize.<locals>.from_fun  s`    !-.!U\\*..q1(]  HA,,QVV44rF   c                    t         j                  j                  t         j                  j                  t         j                  j                  j
                              }|5  	5  t        j                  t         j                  
|       }t        j                  t         j                  
|      } |i |}t        j                  t        |      }|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   rH   _ExcludeDispatchKeyGuardr&  rJ   rK   r#  r$  r   r   )rV   rW   disable_above	func_argsfunc_kwargsfunc_outputsoutputsri  rA   ry   rg  s          r&   innerz%dispatch_functionalize.<locals>.inner  s    99HH##EHH$8$8$F$FG
  	D 	,,U\\64HI ..u||VVLK:k:L**+;X|TG	 	 	 	 	 	 	s%   "C<%A/C'	C<'C0	,C<<D)r$   r  rZ   r  )rV   r   rW   r   rZ   r   r[   )rA   ry   rq  ri  rg  s   `` @@r&   dispatch_functionalizerr    s    

5
 
 LrF   c                  t    e Zd Zed
d       Zedd       Zedd       Zedd       ZddZddZ	ddZ
ddZy	)BaseFunctionalizeAPIc                     y r,   r[   rU   rV   s     r&   r+  z!BaseFunctionalizeAPI.wrap_tensors      rF   c                     y r,   r[   rv  s     r&   r*  z#BaseFunctionalizeAPI.unwrap_tensors  rw  rF   c                     y r,   r[   rU   inner_fs     r&   functionalizez"BaseFunctionalizeAPI.functionalize  rw  rF   c                     y r,   r[   r   s    r&   redispatch_to_nextz'BaseFunctionalizeAPI.redispatch_to_next   rw  rF   c                0    t        j                  ||       y r,   )r   r   )rU   input_tensoroutput_tensors      r&   replacezBaseFunctionalizeAPI.replace$  s    $$\=ArF   c                .    t        j                  |       y r,   )r   r   rU   tensors     r&   r   z"BaseFunctionalizeAPI.commit_update'  s    **62rF   c                .    t        j                  |       y r,   )r   r   r  s     r&   r   zBaseFunctionalizeAPI.sync*  s    !!&)rF   c                .    t        j                  |       y r,   )r   r   r  s     r&   r   z7BaseFunctionalizeAPI.mark_mutation_hidden_from_autograd-  s    ??GrF   NrV   r   rZ   r   )rV   'torch.Tensor | tuple[torch.Tensor, ...]rZ   r   )r{  Callable[..., Any]rZ   r  rZ   zAbstractContextManager[None]r  r^   r  r^   rZ   r   r  r^   rZ   r   )r   r   r   r   r+  r*  r|  r~  r  r   r   r   r[   rF   r&   rt  rt    sf           B3*HrF   rt  c                       e Zd Z	 d	 	 	 	 	 d fdZddZ	 	 	 	 ddZddZddZedd       Z	ddZ
ddZdd	Zdd
Z xZS )r)  c                V    t         |           |r|n	t               | _        || _        y r,   )r   r   r   ry   rC   )rU   ry   rC   r   s      r&   r   zPythonFunctionalizeAPI.__init__2  s'     	 D&:&<	(rF   c                    | j                   5  t        j                  j                  j	                  t        j
                  t        j                  |      cd d d        S # 1 sw Y   y xY wr,   )ry   r   r  _pytreer$  r   r   r   rv  s     r&   r+  z#PythonFunctionalizeAPI.wrap_tensors9  sH    YY 	;;&&44.<<d	 	 	s   AAA'c                |    t         j                  j                  j                  t        t        j
                  |      S r,   )r   r  r  r$  r   r   rv  s     r&   r*  z%PythonFunctionalizeAPI.unwrap_tensors?  s/     {{""00.>>
 	
rF   c                .    t        || j                        S r,   )rr  ry   rz  s     r&   r|  z$PythonFunctionalizeAPI.functionalizeG  s    %gtyy99rF   c                *    t        j                         S r,   )
contextlibnullcontextr   s    r&   r~  z)PythonFunctionalizeAPI.redispatch_to_nextJ  s     %%''rF   c                X    t        | t              st        | dt        |              | S )Nz! must be a FunctionalTensor, got )r   r   rM   type)r  r  s     r&   _check_cast_functionalz-PythonFunctionalizeAPI._check_cast_functionalR  s4    &"23 &9$v,H  rF   c                    | j                  |d      }t        |t              rt        d      |j	                  |       y )Nr  z,output_tensor must not be a FunctionalTensor)r  r   r   rM   r   )rU   r  r  fts       r&   r  zPythonFunctionalizeAPI.replaceZ  s7    ((~Fm%56 !OPP
M"rF   c                D    | j                  |d      j                          y Nr  )r  r   r  s     r&   r   z$PythonFunctionalizeAPI.commit_update`  s    ##FH5CCErF   c                D    | j                  |d      j                          y r  )r  r   r  s     r&   r   zPythonFunctionalizeAPI.syncc  s    ##FH5::<rF   c                D    | j                  |d      j                          y r  )r  r   r  s     r&   r   z9PythonFunctionalizeAPI.mark_mutation_hidden_from_autogradf  s    ##H	

,
,
.rF   r   )ry   r   rC   r"   rZ   r   rV   
tuple[Any]rZ   r  )rV   z<torch.Tensor | tuple[torch.Tensor, ...] | list[torch.Tensor]rZ   r   r{  r
   rZ   r
   r  )r  r^   r  r   rZ   r   r  r  )r   r   r   r   r+  r*  r|  r~  r   r  r  r   r   r   r   r   s   @r&   r)  r)  1  sp    MR)/)FJ)	)
P
	
:(  #F=/rF   r)  c                  4    e Zd ZddZ	 	 	 	 ddZddZd	dZy)
CppFunctionalizeAPIc                "    ddl m}  ||d      S Nr   )_wrap_all_tensors_to_functional)level)!torch._functorch.eager_transformsr  rU   rV   r  s      r&   r+  z CppFunctionalizeAPI.wrap_tensorsm  s    U.t1==rF   c                2    ddl m}  ||t                     S Nr   )#_unwrap_all_tensors_from_functional)reapply_views)r  r  _reapply_viewsrU   rV   r  s      r&   r*  z"CppFunctionalizeAPI.unwrap_tensorsr  s    	
 34~GWXXrF   c                @    t         j                  j                  |      S r,   )r   rA   r|  rz  s     r&   r|  z!CppFunctionalizeAPI.functionalize|  s    zz''00rF   c                    t         j                  j                  t         j                  j                  t         j                  j                  j
                              S r,   )r   rH   rk  r&  rJ   rK   r   s    r&   r~  z&CppFunctionalizeAPI.redispatch_to_next  s;    xx00HH##EHH$8$8$F$FG
 	
rF   Nr  rV   r  rZ   r  r  r  )r   r   r   r+  r*  r|  r~  r[   rF   r&   r  r  l  s)    >
Y;Y	0Y1
rF   r  c                  <    e Zd ZddZddZ	 	 	 	 d	dZd
dZddZy)FunctorchFunctionalizeAPIc                    || _         y r,   )interpreter)rU   r  s     r&   r   z"FunctorchFunctionalizeAPI.__init__  s
    &rF   c                R    ddl m}  ||| j                  j                               S r  )r  r  r  r  r  s      r&   r+  z&FunctorchFunctionalizeAPI.wrap_tensors  s!    U.t4;K;K;Q;Q;STTrF   c                R    ddl m}  ||| j                  j                               S r  )r  r  r  functionalize_add_back_viewsr  s      r&   r*  z(FunctorchFunctionalizeAPI.unwrap_tensors  s*    	
 3 0 0 M M O
 	
rF   c                    t         j                  j                  || j                  j	                         rd      S d      S )Nmutations_and_views	mutations)r(  )r   rA   r|  r  r  rz  s     r&   r|  z'FunctorchFunctionalizeAPI.functionalize  sN    zz'' ##@@B & ( 
 	

 ! ( 
 	
rF   c                6    | j                   j                         S r,   )r  lowerr   s    r&   r~  z,FunctorchFunctionalizeAPI.redispatch_to_next  s    %%''rF   N)r  r   rZ   r   r  r  r  r  )r   r   r   r   r+  r*  r|  r~  r[   rF   r&   r  r    s,    'U
	
;	
	0	

(rF   r  c                d    t        | t              rt        j                  | j                        S | S r,   )r   r   r   r   r_   )r  s    r&   mb_unwrap_functional_tensorr    s'    &*+,,V[[99MrF   )r#   r   rZ   r"   )rA   r   rB   r"   rC   r"   rZ   zbool | None)rN   r   rZ   r   )rX   r   rZ   r  )rZ   zGenerator[None, None, None])rA   r  ry   r   rZ   r  )r  r^   rZ   r^   )D
__future__r   r  	functoolsr?   r   abcr   r   r   typingr   r   typing_extensionsr	   builtinscollections.abcr
   r   r   r#   r   torch._functorch.pyfunctorchr   
torch._opsr   r   torch.fx.tracebackr.  	tracebackrS  torch.utils._pytreer  r  r#  torch._Cr   r  r   r   torch._subclasses.meta_utilsr   torch.utils._python_dispatchr   r   r   r   r   _logginggetArtifactLoggerr   r    r'   	lru_cacherE   rQ   r\   r   r   r   contextmanagerrc  rr  rt  r)  r  r  r  r[   rF   r&   <module>r     sf   "     # - % " ==#E%  ) ) $ $ K K 6  nn66xARS 
$ S! 
 " 26   " F"2_!u|| _!D	E, EP
 J J <P;Q!
!$8!!HH3 H<8/1 8/v
. 
2 ( 4  (FrF   