
    N jG                    V   S SK Jr  S SKrS SKrS SKrS SKJrJr  S SKJr  S SK	J
r
JrJrJr  S SKJr  \(       a   S SKrS SKJrJrJr  S SKJr  S S	KJr  S S
KJr  S SKrS SKJs  Jr  S SK J!s  J"r#  S SK$J%r&  S SKJ'r'J(r(  S SK)J*r*  S SK+J,r,J-r-J.r.J/r/J0r0  \Rb                  Re                  \3S5      r4S S jr5 " S S\Rl                  5      r7 " S S\05      r8\Rr                  S!S j5       r:\8" 5       4     S"S jjr; " S S\5      r< " S S\<5      r= " S S\<5      r> " S S\<5      r?S#S jr@g)$    )annotationsN)ABCabstractmethod)AbstractContextManager)AnyOptionalTYPE_CHECKINGUnion)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                    ^  SU 4S jjnU$ )Nc                0   > U R                   " U0 0 UETED6$ N)to)selfargskwargsextra_kwargss      t/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/torch/_subclasses/functional_tensor.py_&_conversion_method_template.<locals>._5   s"    ww; :6 :\ :;;    )r    FunctionalTensorr!   r   r"   r   returnr    )r#   r%   s   ` r$   _conversion_method_templater+   4   s    < Hr'   c                    ^  \ rS rSr% SrS\S'   \R                  R                  R                  r
\R                  R                  R                  \R                  R                  R                  5      r\R                   R"                  R$                  R&                  \R                   R"                  R$                  R(                  \R                   R"                  R*                  R&                  \R                   R"                  R,                  R&                  \R                   R"                  R.                  R&                  \R                   R"                  R0                  R&                  \R                   R"                  R2                  R&                  \R                   R"                  R4                  R&                  \R                   R"                  R6                  R&                  \R                   R"                  R8                  R&                  \R                   R"                  R:                  R&                  \R                   R"                  R<                  R&                  \R                   R"                  R>                  R&                  \R                   R@                  RB                  R&                  /r"Sr#S\S'   SS jr$  S         S S
 jjr%SS.S!S jjr&\'S"S j5       r(S#S jr)S$S jr*S%S jr+S&S jr,S&S jr-S&S jr.S'S jr/S(U 4S jjr0 S)       S*S jjr1\2" \Rf                  S9r4\2" \RB                  " S5      S9r5\2" \Rl                  S9r6\2" \Rn                  S9r8\2" \Rr                  S9r:\2" \Rv                  S9r<\2" \Rz                  S9r=\2" \R|                  S9r?\2" \R                  S9rA\2" \R                  S9rC S)SS.     S+S jjjrD\ES,S j5       rFS-S jrGS	rHU =rI$ ).r(   ;   a"  
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Optional[FunctionalTensor]_inference_mode_basec                    [         R                  " U5      (       d  [        S5      e[        R                  [         R
                  R                  U5      -  n[         R                  R                  U UR                  [        U5      (       d  UR                  5       OS [        U5      (       d  UR                  5       OS S UR                  UR                  UR                  SUR                   S SSU5      n[         R
                  R#                  U5        Xl        [         R&                  R(                  R*                  (       a  [         R,                  " 5       (       a  [         R.                  R(                  R0                  (       a  UR3                  5       (       a/  S Ul        XBR6                  UR$                  R9                  5       '   U$ UR6                  UR$                  R9                  5          Ul        UR4                  c  [        S5      eU$ )Nz elem must be a functional tensorFz)out._inference_mode_base must not be None)torch_is_functional_tensorAssertionErrorr(   _extra_dispatch_keys_C_dispatch_keysTensor_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is_base_tensorr0   _storage_to_baseuntyped_storage)clsr/   modeextra_dispatch_keysouts        r$   __new__FunctionalTensor.__new__o   s   **400 !CDD& 11EHH4K4KD4QQ 	 ll11
 JJ!.t!4!4DKKM$-:4-@-@##%dJJKKKK)
, 	//4 MM  II//11&&DD!!##+/( EH%%chh&>&>&@A 
 ,0+@+@HH,,.,( ++3()TUU
r'   r*   c                   U Vs/ s H:  nU[         R                  [         R                  R                  [        4;  d  M8  UPM<     nnU(       a  [
        R                  SU5        [        $ Uc  0 nU[        R                  ;   Ga4  [        U5      S:w  a  [        S5      eU[         R                  R                  R                  R                  [         R                  R                  R                  R                   4;   a_  [        U5      S:w  d  [#        US   [        5      (       d  [        S5      eU" [         R$                  " US   R&                  5      US   5      $ [        U5      S:w  d  [#        US   [        5      (       d  [        S5      eU" [         R$                  " US   R&                  5      5      $ [)        S5      es  snf )	N.FunctionalTensor unrecognized subclass(es): %sr   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())r2   r8   _subclasses
FakeTensorr(   not_implemented_logdebugNotImplementedmetadata_fnslenr4   opsatenis_strides_like_formatdefaultis_contiguousmemory_format
isinstance_from_functional_tensorr/   RuntimeError)r    functypesr!   r"   tunrecognized_typess          r$   __torch_dispatch__#FunctionalTensor.__torch_dispatch__   s    
u'8'8'C'CEUVV  	 

 %%@BT "!>F #000 6{a$%RSS		55==		,,::  t9>DG=M)N)N()VWWE99$q',,GaQQ4yA~ZQ9I%J%J$%KLL55d1gllCDD 
 	
Q
s   7G G)tensor_contentsc               4    S[        U R                  5       S3$ )NzFunctionalTensor())reprr/   )r    rk   s     r$   __repr__FunctionalTensor.__repr__   s    "4		?"3155r'   c                   [         R                  " U 5      (       a  [        S5      e[         R                  " U 5      n[	        [         R
                  R                  R                  5      nUc  [        S5      eU   [         R                  " X5        [        X5      n[         R                  " X5        S S S 5        U$ ! , (       d  f       W$ = f)Nz)x must not already be a functional tensorz functional_mode must not be None)
r2   r3   r4   _to_functional_tensorr   r6   _TorchDispatchModeKey
FUNCTIONAL_mirror_autograd_meta_tor(   )xx_functionalfunctional_moderN   s       r$   to_functionalFunctionalTensor.to_functional   s     &&q)) !LMM
 2215 -UXX-K-K-V-VW" !CDD**1;"<AC**<=  
	 _ 
s   :8B<<
Cc                n    [         R                  " U 5        [         R                  " U R                  5      $ r   )r2   _syncrc   r/   r    s    r$   from_functional FunctionalTensor.from_functional  s#    D,,TYY77r'   c                B    [         R                  " U R                  5      $ r   )r2   _is_functional_tensor_baser/   r}   s    r$   rH   FunctionalTensor.is_base_tensor  s    //		::r'   c                F    [         R                  " U R                  U5        g r   )r2   _functionalize_replacer/   )r    outputs     r$   replace_FunctionalTensor.replace_
  s    $$TYY7r'   c                D    [         R                  " U R                  5        g r   )r2   _functionalize_commit_updater/   r}   s    r$   commit_updateFunctionalTensor.commit_update  s    **4995r'   c                D    [         R                  " U R                  5        g r   )r2   _functionalize_syncr/   r}   s    r$   syncFunctionalTensor.sync  s    !!$)),r'   c                D    [         R                  " U R                  5        g r   )r2   1_functionalize_mark_mutation_hidden_from_autogradr/   r}   s    r$   "mark_mutation_hidden_from_autograd3FunctionalTensor.mark_mutation_hidden_from_autograd  s    ??		Jr'   c                j   U R                   R                  5       S:X  a  U R                   R                  5       $ U R                   R                  5       S:X  a*  U R                    Vs/ s H  oR                  5       PM     sn$ U R                    Vs/ s H  oR                  5       PM     sn$ s  snf s  snf )Nr   rT   )r/   dimitemtolist)r    r/   s     r$   r   FunctionalTensor.tolist  sy    99==?a99>>##YY]]_!,0II6IDIIKI66.2ii8idKKMi88 78s   %B+B0c                ,  > [        [        R                  R                  R                  5      R
                  (       aH  [        R                  R                  R                  U U R                  U R                  U R                  S9  [        TU ]4  " U0 UD6$ )N)r=   r?   r>   )r   r2   r6   rs   rt   exportr\   r]   _assert_tensor_metadatar=   r?   r>   superr   )r    r!   r"   	__class__s      r$   r   FunctionalTensor.to  sj    ehh<<GGHOOIINN22jj{{{{	 3  wz4*6**r'   c                    U=(       d    [         R                  R                  5       n[        U5      S:  a  U R                  " U/UQ70 UD6$ U R                  " SSU0UD6$ )Nr   r?   r*   )r2   cudacurrent_devicer[   r   )r    r?   r!   r"   s       r$   r   FunctionalTensor.cuda)  sV     65::446t9q=7763D3F33773&3F33r'   )r=   cpu)r?   )masked_gradc               6    U R                   R                  5       $ r   )r/   to_dense)r    r=   r   s      r$   r   FunctionalTensor.to_dense>  s     yy!!##r'   c                .    U R                   R                  $ r   )r/   r>   r}   s    r$   r>   FunctionalTensor.layoutF  s     yyr'   c                4    [        U R                  5       5      $ r   )boolr   r}   s    r$   __bool__FunctionalTensor.__bool__K  s    DIIK  r'   )r/   r.   rL   FunctionalTensorModer)   r   r*   N
re   r   rf   zSequence[type]r!   tuple[Any, ...]r"   zdict[str, Any] | Noner)   r   )rk   zobject | Noner)   str)rv   r.   r)   r(   )r)   r.   r)   r   )r   r.   r)   None)r)   r   )r)   r   )r!   r   r"   r   r)   r.   r   )r?   ztorch.device | int | str | Noner!   r   r"   r   r)   r.   )r=   ztorch.dtype | Noner   zbuiltins.bool | Noner)   r.   )r)   ztorch.layout)r)   zbuiltins.bool)J__name__
__module____qualname____firstlineno____doc____annotations__r2   r6   rs   rt   	_mode_key,_additional_keys_to_prop_for_wrapper_tensorsaddDispatchKey
ZeroTensorr5   r\   r]   r`   r_   ra   r^   is_non_overlapping_and_densesizesym_sizer;   
sym_strider<   sym_storage_offsetnumel	sym_numelr   primr?   rZ   r0   rO   ri   ro   staticmethodry   r~   rH   r   r   r   r   r   r   r   r+   int8charr   bfloat16uint8bytefloat64doublefloat32floatr   float16halfint32intint64longr   propertyr>   r   __static_attributes____classcell__r   s   @r$   r(   r(   ;   sH     ..99I !88PPTT'' 			$$,,		$$22		--55		33;;		##		''		%%		!!))		%%--		))11		$$		  ((		""		%%L$ 8<4;AN !#(,1
1
 1
 	1

 &1
 
1
f <@ 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	$!$ *	$
 
$    ! !r'   r(   c                     ^  \ rS rSr   S       S	U 4S jjjrS
U 4S jjr        SU 4S jjr  S         SS jjr\SS j5       r	Sr
U =r$ )r   iO  c                h  > [         TU ]  5         X l        SU l        / U l        [
        R                  R                  R                  U l	        Xl
        U(       a$  [
        R                  R                  R                  OS U l        0 U l        0 U l        X0l        ["        R$                  " 5       U l        g NF)r   __init__r   is_on_stackenter_stackr2   r6   rs   rt   r   pre_dispatchr   PreDispatch_dispatch_key_tokens_tokens_forward_output_allow_token_discoveryweakrefWeakKeyDictionaryrI   )r    r   r   r   r   s       r$   r   FunctionalTensorMode.__init__P  s     	  77BB(AMUXX11==SW 13 @B# '=# %%' 	r'   c                   >^  SU 4S jjnU" 5       c)  T R                   R                  S5        [        TT ]  5       $ T R                   R                  S5        T $ )Nc                 D  > T R                   [        R                  R                  R                  :X  a-  [        [        R                  R                  R                  5      $ [        R                  R                  [        R                  R                  R                  5      $ r   )	r   r2   r6   r   r   r   rs   rt   _get_dispatch_moder}   s   r$   _get_prev_mode6FunctionalTensorMode.__enter__.<locals>._get_prev_modev  sm    !!UXX%9%9%E%EE6HH22==  88....99 r'   TF)r)   Optional[FunctionalTensorMode])r   appendr   	__enter__)r    r   r   s   ` r$   r   FunctionalTensorMode.__enter__u  sN    	 ###D)7$&&##E*Kr'   c                j   > U R                   R                  5       nU(       a  [        TU ]  XU5        g g r   )r   popr   __exit__)r    exc_typeexc_valexc_tbr   r   s        r$   r   FunctionalTensorMode.__exit__  s0     &&**,GX7 r'   c                X  ^ ^^ Tc  0 mU Vs/ s HL  n[        U[        R                  R                  5      (       a  M.  U[        R                  [
        4;  d  MJ  UPMN     nnU(       a  [        R                  SU5        [        $ SUUU 4S jjnU[
        R                  ;  aq  U" U5      (       ad  [        R                  R                  UR                  5       5      (       a2  T    UR                  " T0 TD6nU[        La  UsS S S 5        $  S S S 5        SU 4S jjn	SS jn
SSKJnJnJn  U" U5      (       a  [        R                  R%                  UR                  5       [        R                  R&                  R(                  5      (       d  SS KJs  Jn  SS KJs  Jn  [        R4                  R7                  5       (       a'  UR8                  (       a  U" T UTT5      $ U" T UTT5      $ UR:                  (       a  U" T UTT5      $ U" T UTT5      $ SSKJnJ n  U" U5      (       a  [        R                  R%                  UR                  5       [        R                  R&                  R(                  5      (       a  [C        SUR                  5        S	35      eU" T RD                  T RF                  UTT5      $ [H        RJ                  " [
        U
TT45      u  nn[        R                  RM                  [        R                  R&                  R(                  5      n[        R                  RO                  [        R                  R&                  R(                  5      nU(       d  U(       a  [C        S
5      e[        R                  RQ                  5       [        R                  RS                  [        R                  R&                  R(                  5      -  n[        R                  RU                  5       RW                  [        R                  R&                  R(                  5      [
        RX                  -
  n[[        U[\        5      (       aG  [_        5       nURa                  T5      nURa                  T5      nU" U0 UD6nURc                  U5      nGOi[        R                  Re                  UU5          [        Rf                  " S5      nU[
        R                  ;   a0  U" U0 UD6n[H        RJ                  " [        R                  U	U5      nGO[        R                  Ri                  [        R                  Rj                  Rl                  5      =n(       a  [H        Rn                  " TT/5       H  n[[        U[
        5      (       d  M  [        Rp                  " URr                  5      n  URt                  Rv                  U    n!U!R|                  R~                  n"[        R                  " U"5         [        R                  " U5        S S S 5        M     UR                  " [        R                  R&                  R(                  /UQ70 UD6nT R                  (       aG  U[        R                  R                  R                  R                  L a  [        R                  " U5        [H        RJ                  " [        R                  U	U5      n[        R                  " 5         [        Rf                  " U5         S S S 5        [        R                  RM                  [        R                  R&                  R(                  5      n[        R                  RO                  [        R                  R&                  R(                  5      nU(       d  U(       a  [C        S5      e[        S [H        Rn                  " W5       5       5      (       a1  U[        R                  R                  R                  R                  L a  U$ [        R                  R                  UR                  ;   aj  U[        R                  R                  R                  R                  La9  [        R                  R                  R                  5          U" T0 TD6  S S S 5        [        UTTU5      $ s  snf ! , (       d  f       GN= f! [x         a    [{        SU  S35      S ef = f! , (       d  f       GMk  = f! [        R                  " 5         [        Rf                  " W5        f = f! , (       d  f       GN = f! , (       d  f       N= f)NrR   c                R  > TR                   (       a2  U [        R                  R                  R                  R
                  L a  gSSKJn  U" U 5      (       a  g[        S U R                  R                   5       5      nU(       d  U R                  R                  (       a  gTR                   (       aQ  TR                  (       a?  U R                  S;  a.  U R                  5       (       a  [        R                   " SU  S3S	S
9  gg["        R$                  " TT45      u  p4['        X5      $ )NFr   )#_should_decompose_because_unsafe_opTc              3  8   #    U  H  oR                   v   M     g 7fr   )
alias_info).0args     r$   	<genexpr>RFunctionalTensorMode.__torch_dispatch__.<locals>._can_decompose.<locals>.<genexpr>  s     $V?U^^?Us   )r]   r   zAt 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.rS   )
stacklevel)r   r2   r\   r]   dropoutr_   torch._decompr   any_schema	arguments
is_mutabler   	namespace_can_decomposewarningswarnpytreetree_flattenr   )re   r   alias_info_presentflat_args_kwargsr%   r!   r"   r    s        r$   r  ?FunctionalTensorMode.__torch_dispatch__.<locals>._can_decompose  s     {{tuyy~~'='='E'EE J2488 "%$Vt||?U?U$V!V!T\\%<%< {{$$~~-==$BUBUBWBW %%)F*=? ()	 ! #)"5"5tVn"E1$IIr'   c                   > [        U [        5      (       a  [        S5      e[        U [        R                  5      (       a'  [        R
                  " U 5      (       a  [        U T5      $ U $ )Nz*x must not be a FunctionalTensor in wrap())rb   r(   r4   r2   r8   r3   )rv   r    s    r$   wrap5FunctionalTensorMode.__torch_dispatch__.<locals>.wrap  sR     !-..$%QRR!U\\**u/J/J1/M/M'400Hr'   c                    U R                   $ r   )r/   )rv   s    r$   unwrap7FunctionalTensorMode.__torch_dispatch__.<locals>.unwrap  s    66Mr'   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 modeTzcannot find z in tensor_trackerzNFunctionalization should not already be enabled above this mode after dispatchc              3  B   #    U  H  n[        U[        5      v   M     g 7fr   )rb   r(   )r   rv   s     r$   r  :FunctionalTensorMode.__torch_dispatch__.<locals>.<genexpr>  s"      9A 1.//9s   )re   r   r)   r   )rv   objectr)   r!  )rv   r(   r)   r.   )V
issubclassr2   rU   rV   r8   r(   rW   rX   rY   rZ   r6   _dispatch_has_kernelname	decompose*torch._higher_order_ops.auto_functionalizer  r  r  %_dispatch_has_kernel_for_dispatch_keyr   Functionalizetorch._export.configrB   rC   torch._inductor.configrF   compileris_exportingrD   rG   torch._higher_order_ops.effectsr  r  r4   r   r   r  tree_map_only&_dispatch_tls_is_dispatch_key_included&_dispatch_tls_is_dispatch_key_excluded_dispatch_tls_local_include_setDispatchKeySet_dispatch_tls_local_exclude_setremover5   rb   r   PythonFunctionalizeAPIunwrap_tensorswrap_tensors_ForceDispatchKeyGuard#_functionalize_enable_reapply_viewsr   rs   PROXYtree_leavesrc   r/   tracertensor_trackerKeyErrorrd   proxynodefx_tracebackset_current_replay_noder|   _op_dkr   r\   r]   r  r_   _freeze_functional_tensor_disable_functionalizationr  
lift_freshTaginplace_viewtagsset_source_Tensorutils_mode_utilsno_dispatchr   )#r    re   rf   r!   r"   rg   rh   r  rr  r  r  r  r  export_configinductor_configr  r  args_unwrappedkwargs_unwrappedis_includedis_excludedinclude_to_setexclude_to_setctxfully_unwrapped_argsfully_unwrapped_kwargsouts_unwrappedouts_wrappedold_apply_viewsma	unwrappedtracker_entry	curr_nodes#   `  ``                              r$   ri   'FunctionalTensorMode.__torch_dispatch__  s2    >F 
a!2!2!=!=>  %,,(899  	 
 %%@BT "!-	J -	J` (555t$$ --diik::NND3F3N* * 
			
 	
 "
 
((@@IIK--;;
 
 98<<~~**,, II3D$fMM,T4vFF<</dD&II(tT6BBOtxx==		UXX11??  %DIIK=(mn  "++T\\4v  ,2+?+?ftVn,
(( hhEEHH  ..
 hhEEHH  ..
 { Q  HH446hh%%ehh&:&:&H&HIJ 	
 HH446==$$22 334 	 d/00
 )*C#&#5#5d#; %(%7%7&" "%(N ++N;L 00P<O&+&O&OPT&UO /<<<)-~)RAQ)R'-';';!LL$(  !& ; ;!HH::@@! 1  &,%7%7v%G'1!5E'F'F$,,1,I,I!&&,Q	!045HH4K4KI4VM
 -:,?,?,D,D	%1%I%I)%T$)KKN &U%T &H& *.!HH00>>*+* /*  ;;#uyy~~'='='E'EE % ? ? O'-';';!LL$( 446==oN{ Q~ hhEEHH  ..
 hhEEHH  ..
 { `   ++L9   uyy~~00888 II""dii/EIINN//===((446d%f% 7 +4v|LLU
F N (0 !0*6*6ykAS(T+&+/%0!0
 &U%T* 446==oN{ QPx 76s~   -bbb$b/d	1C4c&b(?,c+cCc+d	2	d
b%(cc
c	c-dd		
d
d)c                    g)NTr*   )rK   s    r$   is_infra_mode"FunctionalTensorMode.is_infra_mode  s    r'   )
r   r   r   rI   r   r   r   r   r   r   )FFF)r   r   r   r   r   r   r)   r   )r)   r   )r   zOptional[type[BaseException]]r   zOptional[BaseException]r   zOptional[TracebackType]r)   r   r   r   r   )r   r   r   r   r   r   r   ri   classmethodre  r   r   r   s   @r$   r   r   O  s     #',	"("( "( !%	"(
 
"( "(J"8/8 )8 (	8
 
8 !#(,TMTM TM 	TM
 &TM 
TMl  r'   r   c                 \    [        [        R                  R                  R                  5      $ r   )r   r2   r6   rs   rt   r*   r'   r$   disable_functional_moderi    s    uxx==HHIIr'   c                8   ^ ^^^ SS jmSS jmSUU UU4S jjnU$ )Nc                n    [        U [        R                  5      (       a  [        R	                  U 5      $ U $ r   )rb   r2   r8   r(   ry   rg   s    r$   to_fun&dispatch_functionalize.<locals>.to_fun  s)    a&&#11!44r'   c                &   [        U [        5      (       dG  [        U [        R                  5      (       a&  [        R                  " U 5      (       a  [        S5      eU $ [        R                  " U 5        [        R                  " U R                  5      $ )NzCNon-FunctionalTensor torch.Tensor should not be a functional tensor)	rb   r(   r2   r8   r3   r4   r|   rc   r/   rl  s    r$   from_fun(dispatch_functionalize.<locals>.from_fun  si    !-..!U\\**..q11(]  HA,,QVV44r'   c                 *  > [         R                  R                  [         R                  R                  [         R                  R                  R
                  5      5      nU   T	   [        R                  " [         R                  T
U 5      n[        R                  " [         R                  T
U5      nT" U0 UD6n[        R                  " [        TU5      nUsS S S 5        sS S S 5        $ ! , (       d  f       O= fS S S 5        g ! , (       d  f       g = fr   )
r2   r6   _ExcludeDispatchKeyGuardr2  r   r(  r  r.  r8   r(   )r!   r"   disable_above	func_argsfunc_kwargsfunc_outputsoutputsrp  re   rL   rm  s          r$   inner%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 !D]]DD]]]s%   "D%A2C*	D*
C8	4D
D)rg   r!  r)   r!  )r!   r   r"   r   r)   r   r*   )re   rL   ry  rp  rm  s   `` @@r$   dispatch_functionalizer{    s    

5
 
 Lr'   c                      \ rS rSr\SS j5       r\SS j5       r\SS j5       r\SS j5       r\SS j5       r	\SS j5       r
\SS j5       r\SS	 j5       rS
rg)BaseFunctionalizeAPIi  c                    g r   r*   r    r!   s     r$   r7  !BaseFunctionalizeAPI.wrap_tensors      r'   c                    g r   r*   r  s     r$   r6  #BaseFunctionalizeAPI.unwrap_tensors  r  r'   c                    g r   r*   r    inner_fs     r$   functionalize"BaseFunctionalizeAPI.functionalize  r  r'   c                    g r   r*   r}   s    r$   redispatch_to_next'BaseFunctionalizeAPI.redispatch_to_next  r  r'   c                    g r   r*   r    input_tensoroutput_tensors      r$   replaceBaseFunctionalizeAPI.replace  r  r'   c                    g r   r*   r    tensors     r$   r   "BaseFunctionalizeAPI.commit_update  r  r'   c                    g r   r*   r  s     r$   r   BaseFunctionalizeAPI.sync  r  r'   c                    g r   r*   r  s     r$   r   7BaseFunctionalizeAPI.mark_mutation_hidden_from_autograd  r  r'   r*   Nr!   r   r)   r   )r!   z'torch.Tensor | tuple[torch.Tensor, ...]r)   r   )r  Callable[..., Any]r)   r  r)   zAbstractContextManager[None]r  r.   r  r.   r)   r   r  r.   r)   r   )r   r   r   r   r   r7  r6  r  r  r  r   r   r   r   r*   r'   r$   r}  r}    s                   r'   r}  c                     ^  \ rS rSr S     SU 4S jjjrSS jr    SS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSrU =r$ )r5  i  c                ^   > [         TU ]  5         U(       a  UO	[        5       U l        X l        g r   )r   r   r   rL   r   )r    rL   r   r   s      r$   r   PythonFunctionalizeAPI.__init__  s%     	 D&:&<	(r'   c                    U R                      [        R                  R                  R	                  [        R
                  [        R                  U5      sS S S 5        $ ! , (       d  f       g = fr   )rL   r2   rL  _pytreer.  r8   r(   ry   r  s     r$   r7  #PythonFunctionalizeAPI.wrap_tensors  s=    YY;;&&44.<<d YYs   AA
A,c                |    [         R                  R                  R                  [        [        R
                  U5      $ r   )r2   rL  r  r.  r(   r~   r  s     r$   r6  %PythonFunctionalizeAPI.unwrap_tensors  s/     {{""00.>>
 	
r'   c                ,    [        XR                  5      $ r   )r{  rL   r  s     r$   r  $PythonFunctionalizeAPI.functionalize  s    %gyy99r'   c                ,    [         R                  " 5       $ r   )
contextlibnullcontextr}   s    r$   r  )PythonFunctionalizeAPI.redispatch_to_next  s     %%''r'   c                    [        U[        5      (       d  [        S[        U5       35      e[        U[        5      (       a  [        S5      eUR	                  U5        g )Nz-input_tensor must be a FunctionalTensor, got z,output_tensor must not be a FunctionalTensor)rb   r(   r4   typer   r  s      r$   r  PythonFunctionalizeAPI.replace  sW    ,(899 ?\@R?ST  m%566 !OPPm,r'   c                |    [        U[        5      (       d  [        S[        U5       35      eUR	                  5         g Nz'tensor must be a FunctionalTensor, got )rb   r(   r4   r  r   r  s     r$   r   $PythonFunctionalizeAPI.commit_update(  s9    &"233 9$v,H  	r'   c                |    [        U[        5      (       d  [        S[        U5       35      eUR	                  5         g r  )rb   r(   r4   r  r   r  s     r$   r   PythonFunctionalizeAPI.sync/  s6    &"233 9$v,H  	r'   c                |    [        U[        5      (       d  [        S[        U5       35      eUR	                  5         g r  )rb   r(   r4   r  r   r  s     r$   r   9PythonFunctionalizeAPI.mark_mutation_hidden_from_autograd6  s9    &"233 9$v,H  	113r'   )rL   r   r   )rL   r   r   r   r)   r   r!   
tuple[Any]r)   r  )r!   zAUnion[torch.Tensor, tuple[torch.Tensor, ...], list[torch.Tensor]]r)   r   r  r   r)   r   r  r  r  )r   r   r   r   r   r7  r6  r  r  r  r   r   r   r   r   r   s   @r$   r5  r5    se    PU)2)IM)	) )
U
	
:(-4 4r'   r5  c                  l    \ rS rSrSS jr    SS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrS
rg)CppFunctionalizeAPIi>  c                    SSK Jn  U" USS9$ Nr   )_wrap_all_tensors_to_functional)level)!torch._functorch.eager_transformsr  r    r!   r  s      r$   r7   CppFunctionalizeAPI.wrap_tensors?  s    U.t1==r'   c                ,    SSK Jn  U" U[        5       S9$ Nr   )#_unwrap_all_tensors_from_functional)reapply_views)r  r  _reapply_viewsr    r!   r  s      r$   r6  "CppFunctionalizeAPI.unwrap_tensorsD  s    	
 34~GWXXr'   c                @    [         R                  R                  U5      $ r   )r2   re   r  r  s     r$   r  !CppFunctionalizeAPI.functionalizeN  s    zz''00r'   c                    [         R                  R                  [         R                  R                  [         R                  R                  R
                  5      5      $ r   )r2   r6   rs  r2  r   r(  r}   s    r$   r  &CppFunctionalizeAPI.redispatch_to_nextQ  s;    xx00HH##EHH$8$8$F$FG
 	
r'   c                0    [         R                  " X5        g r   r2   r   r  s      r$   r  CppFunctionalizeAPI.replaceV      $$\Ar'   c                0    [         R                  " U5        g r   r2   r   r  s     r$   r   !CppFunctionalizeAPI.commit_updateY      **62r'   c                0    [         R                  " U5        g r   r2   r   r  s     r$   r   CppFunctionalizeAPI.sync\      !!&)r'   c                0    [         R                  " U5        g r   r2   r   r  s     r$   r   6CppFunctionalizeAPI.mark_mutation_hidden_from_autograd_      ??Gr'   r*   Nr  r!   -Union[torch.Tensor, tuple[torch.Tensor, ...]]r)   r  r  r  r  r  )r   r   r   r   r7  r6  r  r  r  r   r   r   r   r*   r'   r$   r  r  >  s?    >
YAY	6Y1

B3*Hr'   r  c                  v    \ rS rSrSS jrSS jr    SS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSrg)FunctorchFunctionalizeAPIic  c                    Xl         g r   interpreter)r    r  s     r$   r   "FunctorchFunctionalizeAPI.__init__d  s    &r'   c                J    SSK Jn  U" XR                  R                  5       S9$ r  )r  r  r  r  r  s      r$   r7  &FunctorchFunctionalizeAPI.wrap_tensorsg  s    U.t;K;K;Q;Q;STTr'   c                J    SSK Jn  U" XR                  R                  5       S9$ r  )r  r  r  functionalize_add_back_viewsr  s      r$   r6  (FunctorchFunctionalizeAPI.unwrap_tensorsl  s(    	
 3 0 0 M M O
 	
r'   c                    [         R                  R                  UU R                  R	                  5       (       a  SS9$ SS9$ )Nmutations_and_views	mutations)r4  )r2   re   r  r  r  r  s     r$   r  'FunctorchFunctionalizeAPI.functionalizex  sQ    zz'' ##@@BB & ( 
 	

 ! ( 
 	
r'   c                6    U R                   R                  5       $ r   )r  lowerr}   s    r$   r  ,FunctorchFunctionalizeAPI.redispatch_to_next  s    %%''r'   c                0    [         R                  " X5        g r   r  r  s      r$   r  !FunctorchFunctionalizeAPI.replace  r  r'   c                0    [         R                  " U5        g r   r  r  s     r$   r   'FunctorchFunctionalizeAPI.commit_update  r  r'   c                0    [         R                  " U5        g r   r  r  s     r$   r   FunctorchFunctionalizeAPI.sync  r  r'   c                0    [         R                  " U5        g r   r  r  s     r$   r   <FunctorchFunctionalizeAPI.mark_mutation_hidden_from_autograd  r  r'   r  N)r  r   r)   r   r  r  r  r  r  r  )r   r   r   r   r   r7  r6  r  r  r  r   r   r   r   r*   r'   r$   r  r  c  sB    'U
	
A	
	6	

(B3*Hr'   r  c                p    [        U [        5      (       a   [        R                  " U R                  5      $ U $ r   )rb   r(   r2   rc   r/   )r  s    r$   mb_unwrap_functional_tensorr    s*    &*++,,V[[99Mr'   )r#   r   r)   r  )r)   zGenerator[None, None, None])re   r  rL   r   r)   r  )r  r.   r)   r.   )A
__future__r   r  r  r   abcr   r   r   typingr   r   r	   r
   typing_extensionsr   builtinscollections.abcr   r   r   rf   r   torch._functorch.pyfunctorchr   
torch._opsr   r2   torch.fx.tracebackfx	tracebackrA  torch.utils._pytreerL  r  r  torch._Cr   r  r   r   torch._subclasses.meta_utilsr   torch.utils._python_dispatchr   r   r   r   r   _logginggetArtifactLoggerr   rW   r+   r8   r(   r   contextmanagerri  r{  r}  r5  r  r  r  r*   r'   r$   <module>r     s   "    # - 6 6 " ==#E%  ) ) $ $ K K 6  nn66xARS "Q!u|| Q!hY, Yx
 J J <P;Q!
!$8!!H3 D=41 =4@"H. "HJ,H 4 ,H^r'   