
    3j                       S SK Jr  S SK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  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.J/r/  \R`                  Rc                  \2S5      r3S%S jr4\Rj                  " SS9        S&S j5       r6S'S jr7S(S jr8 " S S\Rr                  5      r: " S S\/5      r;\Rx                  S)S j5       r=\;" 5       4     S*S jjr> " S S\5      r? " S S\?5      r@ " S  S!\?5      rA " S" S#\?5      rBS+S$ jrCg),    )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                    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5      $ s  snf )Nz.FunctionalTensor unrecognized subclass(es): %s)torchTensor_subclasses
FakeTensorFunctionalTensornot_implemented_logdebugbool)typestunrecognized_typess      ]/home/wildlama/miniconda3/lib/python3.13/site-packages/torch/_subclasses/functional_tensor.py_has_unrecognized_tensor_typesr'   '   sp     AU\\5#4#4#?#?AQRR 	
  
 !!<>P	
 "##s   7A0 A0i   )maxsizec                   U(       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U(       aG  U(       a?  U R                  S;  a.  U R                  5       (       a  [        R                  " SU  S3S	S
9  ggg)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  8   #    U  H  oR                   v   M     g 7fN)
alias_info).0args     r&   	<genexpr>&_can_decompose_fast.<locals>.<genexpr>E   s     N7M^^7Ms   )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   opsr2   dropoutdefaulttorch._decompr*   any_schema	arguments
is_mutable	namespace_can_decomposewarningswarn)funcexportpre_dispatchr*   alias_info_presents        r&   _can_decompose_fastrF   4   s     $%))..00888A*400Nt||7M7MNNT\\44~~%55$:M:M:O:O!F"57  !	     c                <   [         R                  R                  [         R                  R                  R                  5      n[         R                  R                  [         R                  R                  R                  5      nU(       d  U(       a  [        U 5      eg g 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_activerR   X   sk    ((AA**K ((AA**K ;S!! ';rG   c                    ^  SU 4S jjnU$ )Nc                0   > U R                   " U0 0 UETED6$ r,   )to)selfargskwargsextra_kwargss      r&   _&_conversion_method_template.<locals>._r   s"    ww; :6 :\ :;;rG   )rV   r   rW   r   rX   r   returnr    )rY   rZ   s   ` r&   _conversion_method_templater^   q   s    < HrG   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$                  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$                  R@                  R(                  \R"                  RB                  RD                  R(                  15      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r0S(U 4S jjr1 S)       S*S jjr2\3" \Rh                  S9r5\3" \RD                  " S5      S9r6\3" \Rn                  S9r7\3" \Rp                  S9r9\3" \Rt                  S9r;\3" \Rx                  S9r=\3" \R|                  S9r>\3" \R~                  S9r@\3" \R                  S9rB\3" \R                  S9rD S)SS.     S+S jjjrE\FS,S j5       rGS-S jrHS	rIU =rJ$ ).r   x   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FunctionalTensor | None_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)r   _is_functional_tensorrN   r   _extra_dispatch_keysrI   _dispatch_keysr   _make_wrapper_subclassshaper   stridestorage_offsetdtypelayoutdevicerequires_grad_set_throw_on_mutable_data_ptrrb   _exportconfig(enable_auto_functionalized_v2_for_exportis_inference_mode_enabled	_inductorenable_auto_functionalized_v2is_base_tensorrc   _storage_to_baseuntyped_storage)clsrb   modeextra_dispatch_keysouts        r&   __new__FunctionalTensor.__new__   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
rG   r]   c                   [        U5      (       a  [        $ 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)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lenrN   r   r6   r2   is_strides_like_formatr8   is_contiguousmemory_format
isinstance_from_functional_tensorrb   RuntimeError)rV   rB   r#   rW   rX   s        r&   __torch_dispatch__#FunctionalTensor.__torch_dispatch__   s+    *%00!!>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 
 	
rG   )tensor_contentsc               4    S[        U R                  5       S3$ )NzFunctionalTensor())reprrb   )rV   r   s     r&   __repr__FunctionalTensor.__repr__  s    "4		?"3155rG   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)
r   re   rN   _to_functional_tensorr   rI   _TorchDispatchModeKey
FUNCTIONAL_mirror_autograd_meta_tor   )xx_functionalfunctional_moder}   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,   )r   _syncr   rb   rV   s    r&   from_functional FunctionalTensor.from_functional:  s#    D,,TYY77rG   c                B    [         R                  " U R                  5      $ r,   )r   _is_functional_tensor_baserb   r   s    r&   rw   FunctionalTensor.is_base_tensor>  s    //		::rG   c                F    [         R                  " U R                  U5        g r,   )r   _functionalize_replacerb   )rV   outputs     r&   replace_FunctionalTensor.replace_A  s    $$TYY7rG   c                D    [         R                  " U R                  5        g r,   )r   _functionalize_commit_updaterb   r   s    r&   commit_updateFunctionalTensor.commit_updateD  s    **4995rG   c                D    [         R                  " U R                  5        g r,   )r   _functionalize_syncrb   r   s    r&   syncFunctionalTensor.syncG  s    !!$)),rG   c                D    [         R                  " U R                  5        g r,   )r   1_functionalize_mark_mutation_hidden_from_autogradrb   r   s    r&   "mark_mutation_hidden_from_autograd3FunctionalTensor.mark_mutation_hidden_from_autogradJ  s    ??		JrG   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   r   )rb   dimitemtolist)rV   rb   s     r&   r   FunctionalTensor.tolistM  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)rl   rn   rm   )r   r   rI   r   r   rC   r6   r2   _assert_tensor_metadatarl   rn   rm   superrU   )rV   rW   rX   	__class__s      r&   rU   FunctionalTensor.toU  sj    ehh<<GGHOOIINN22jj{{{{	 3  wz4*6**rG   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   rn   r]   )r   cudacurrent_devicer   rU   )rV   rn   rW   rX   s       r&   r   FunctionalTensor.cuda`  sV     65::446t9q=7763D3F33773&3F33rG   )rl   cpu)rn   )masked_gradc               6    U R                   R                  5       $ r,   )rb   to_dense)rV   rl   r   s      r&   r   FunctionalTensor.to_denseu  s     yy!!##rG   c                .    U R                   R                  $ r,   )rb   rm   r   s    r&   rm   FunctionalTensor.layout}  s     yyrG   c                4    [        U R                  5       5      $ r,   )r"   r   r   s    r&   __bool__FunctionalTensor.__bool__  s    DIIK  rG   )rb   ra   r{   FunctionalTensorModer\   r	   r]   N
rB   r   r#   Sequence[type]rW   tuple[Any, ...]rX   zdict[str, Any] | Noner\   r   )r   zobject | Noner\   str)r   ra   r\   r   )r\   ra   r\   r"   )r   ra   r\   None)r\   r   )r\   r   )rW   r   rX   r   r\   ra   r,   )rn   ztorch.device | int | str | NonerW   r   rX   r   r\   ra   )rl   ztorch.dtype | Noner   zbuiltins.bool | Noner\   ra   )r\   ztorch.layout)r\   zbuiltins.bool)K__name__
__module____qualname____firstlineno____doc____annotations__r   rI   r   r   	_mode_key,_additional_keys_to_prop_for_wrapper_tensorsaddrK   
ZeroTensorrf   	frozensetr6   r2   r   r8   r   r   is_non_overlapping_and_densesizesym_sizerj   
sym_striderk   sym_storage_offsetnumel	sym_numelr   r3   rn   r   rc   r~   r   r   staticmethodr   r   rw   r   r   r   r   r   rU   r   r^   int8charr   bfloat16uint8bytefloat64doublefloat32floatr"   float16halfint32intint64longr   propertyrm   r   __static_attributes____classcell__r   s   @r&   r   r   x   sP     ..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AN !#(,)
)
 )
 	)

 &)
 
)
V <@ 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	$!$ *	$
 
$    ! !rG   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r        SS jr	\
SS j5       rS	rU =r$ )r   i  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__rC   is_on_stackenter_stackr   rI   r   r   r   rD   rK   PreDispatch_dispatch_key_tokens_tokens_forward_output_allow_token_discoveryweakrefWeakKeyDictionaryrx   )rV   rD   rC   r  r   s       r&   r   FunctionalTensorMode.__init__  s     	  77BB(AMUXX11==SW 13 @B# '=# %%' 	rG   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  r   rI   rK   r  r   r   r   _get_dispatch_moder   s   r&   _get_prev_mode6FunctionalTensorMode.__enter__.<locals>._get_prev_mode  sm    !!UXX%9%9%E%EE6HH22==  88....99 rG   TF)r\   FunctionalTensorMode | None)r  appendr   	__enter__)rV   r  r   s   ` r&   r  FunctionalTensorMode.__enter__  sN    	 ###D)7$&&##E*KrG   c                j   > U R                   R                  5       nU(       a  [        TU ]  XU5        g g r,   )r  popr   __exit__)rV   exc_typeexc_valexc_tbr  r   s        r&   r  FunctionalTensorMode.__exit__  s0     &&**,GX7 rG   c                  ^  Uc  0 n[        U5      (       a  [        $ U[        R                  ;  a{  T R	                  XU5      (       ad  [
        R                  R                  UR                  5       5      (       a2  T    UR                  " U0 U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  [
        R,                  R/                  5       (       a%  UR0                  (       a
  U
" T XU5      $ U	" T XU5      $ UR2                  (       a
  U
" T XU5      $ U	" T XU5      $ SSKJnJn  U" U5      (       a  [
        R                  R                  UR                  5       [
        R                  R                  R                   5      (       a  [;        SUR                  5        S35      eU" T R<                  T R>                  XU5      $ [@        RB                  " [        XsU45      u  nn[E        S5        [
        R                  RG                  5       [
        R                  RI                  [
        R                  R                  R                   5      -  n[
        R                  RK                  5       RM                  [
        R                  R                  R                   5      [        RN                  -
  n[Q        U[R        5      (       aG  [U        5       nURW                  U5      nURW                  U5      nU" U0 UD6nURY                  U5      nGOv[
        R                  R[                  UU5          [
        R\                  " S	5      nU[        R                  ;   a/  U" U0 UD6n[@        RB                  " [
        R^                  UU5      nOT Ra                  X45        URb                  " [
        R                  R                  R                   /UQ70 UD6nT Rd                  (       aG  U[
        Rf                  Rh                  Rj                  Rl                  L a  [
        Rn                  " U5        [@        RB                  " [
        R^                  UU5      n[
        Rp                  " 5         [
        R\                  " U5         S S S 5        [E        S
5        [s        S [@        Rt                  " W5       5       5      (       a1  U[
        Rf                  Rh                  Rv                  Rl                  L a  U$ [
        Rx                  Rz                  UR|                  ;   aj  U[
        Rf                  Rh                  R~                  R                  La9  [
        R                  R                  R                  5          U" U0 UD6  S S S 5        [        XUU5      $ ! , (       d  f       GNr= f! [
        Rp                  " 5         [
        R\                  " W5        f = f! , (       d  f       GNY= f! , (       d  f       No= f)Nc                   > [        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())r   r   rN   r   r   re   )r   rV   s    r&   wrap5FunctionalTensorMode.__torch_dispatch__.<locals>.wrap  sR     !-..$%QRR!U\\**u/J/J1/M/M'400HrG   c                    U R                   $ r,   )rb   )r   s    r&   unwrap7FunctionalTensorMode.__torch_dispatch__.<locals>.unwrap  s    66MrG   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  B   #    U  H  n[        U[        5      v   M     g 7fr,   )r   r   )r.   r   s     r&   r0   :FunctionalTensorMode.__torch_dispatch__.<locals>.<genexpr>c  s"      9A 1.//9s   )r   objectr\   r)  )r   r   r\   ra   )Er'   r   r   r   r?   r   rI   _dispatch_has_kernelname	decompose*torch._higher_order_ops.auto_functionalizer"  r#  r$  %_dispatch_has_kernel_for_dispatch_keyrK   rL   torch._export.configrq   rr   torch._inductor.configru   compileris_exportingrs   rv   torch._higher_order_ops.effectsr%  r&  rN   r  r  pytreetree_map_onlyrR   _dispatch_tls_local_include_setDispatchKeySet_dispatch_tls_local_exclude_setremoverf   r   r   PythonFunctionalizeAPIunwrap_tensorswrap_tensors_ForceDispatchKeyGuard#_functionalize_enable_reapply_viewsr   _sync_view_replay_annotations_op_dkrC   r6   r2   r7   r8   _freeze_functional_tensor_disable_functionalizationr:   tree_leaves
lift_freshTaginplace_viewtagsset_source_Tensorutils_mode_utilsno_dispatchr   )rV   rB   r#   rW   rX   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   'FunctionalTensorMode.__torch_dispatch__  s    >F)%00!! (555##D77 --diik::NND3F3N* * 
			
 	
 "
 
((@@IIK--;;
 
 98<<~~**,, II3D$fMM,T4vFF<</d&II(t6BBOtxx==		UXX11??  %DIIK=(mn  "++T\\4v  ,2+?+?fVn,
(( 	)M	
 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$( ::4H *.!HH00>>*+* /*  ;;#uyy~~'='='E'EE % ? ? O'-';';!LL$( 446==oNE QH 	)\	
  ++L9   uyy~~00888 II""dii/EIINN//===((446d%f% 7 +4v|LL} ~ 446==oNE QPt 76s=   <X1Y3D X!+Y1	Y#
X!-YY
Y #
Y1c                b   [         R                  R                  [         R                  R                  R                  5      nUb  [
        R                  " X/5       H  n[        U[        5      (       d  M  [         R                  " UR                  5      n UR                  R                  U   nUR                  R                   n["        R$                  " U5         [         R&                  " U5        SSS5        M     gg! [         a    [        SU S35      Sef = f! , (       d  f       M  = f)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   rI   r  r   PROXYr4  rC  r   r   r   rb   tracertensor_trackerKeyErrorr   proxynodefx_tracebackset_current_replay_noder   )rV   rW   rX   ma	unwrappedtracker_entry	curr_nodes           r&   r?  2FunctionalTensorMode._sync_view_replay_annotations{  s    & HH''(F(F(L(LM=''7!!%566!99!&&A	 $%HH$;$;I$FM
 *//44	!99)DKKN ED 8     &&yk1CD  
 EDs   DDD
D.	c                    [        XR                  U R                  5      nUb  U$ [        R                  " X#45      u  pV[        X5      $ r,   )rF   rC   rD   r4  tree_flattenr   )rV   rB   rW   rX   resultflat_args_kwargsrZ   s          r&   r?   #FunctionalTensorMode._can_decompose  sH     %T;;8I8IJM %114.A-dEErG   c                    g)NTr]   )rz   s    r&   is_infra_mode"FunctionalTensorMode.is_infra_mode  s    rG   )
r  r  r   rx   r  r  r  rC   r  rD   )FFF)rD   r"   rC   r"   r  r"   r\   r   )r\   r	   )r  ztype[BaseException] | Noner  zBaseException | Noner  zTracebackType | Noner\   r   r   r   )rW   r   rX   dict[str, Any]r\   r   )rB   r   rW   r   rX   rr  r\   r"   r   )r   r   r   r   r   r  r  r   r?  r?   classmethodrp  r   r   r   s   @r&   r   r     s    #',	"("( "( !%	"(
 
"( "(J"8,8 &8 %	8
 
8 !#(,rMrM rM 	rM
 &rM 
rMh!#!# !# 
	!#FFF F 	F
 
F$  rG   r   c                 \    [        [        R                  R                  R                  5      $ r,   )r   r   rI   r   r   r]   rG   r&   disable_functional_moderu    s    uxx==HHIIrG   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,   )r   r   r   r   r   r$   s    r&   to_fun&dispatch_functionalize.<locals>.to_fun  s)    a&&#11!44rG   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)	r   r   r   r   re   rN   r   r   rb   rx  s    r&   from_fun(dispatch_functionalize.<locals>.from_fun  si    !-..!U\\**..q11(]  HA,,QVV44rG   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,   )
r   rI   _ExcludeDispatchKeyGuardr7  rK   rL   r4  r5  r   r   )rW   rX   disable_above	func_argsfunc_kwargsfunc_outputsoutputsr|  rB   r{   ry  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)r$   r)  r\   r)  )rW   r   rX   r   r\   r   r]   )rB   r{   r  r|  ry  s   `` @@r&   dispatch_functionalizer    s    

5
 
 LrG   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r	SS jr
SS jrSS	 jrS
rg)BaseFunctionalizeAPIi  c                    g r,   r]   rV   rW   s     r&   r<  !BaseFunctionalizeAPI.wrap_tensors      rG   c                    g r,   r]   r  s     r&   r;  #BaseFunctionalizeAPI.unwrap_tensors  r  rG   c                    g r,   r]   rV   inner_fs     r&   functionalize"BaseFunctionalizeAPI.functionalize  r  rG   c                    g r,   r]   r   s    r&   redispatch_to_next'BaseFunctionalizeAPI.redispatch_to_next  r  rG   c                0    [         R                  " X5        g r,   )r   r   )rV   input_tensoroutput_tensors      r&   replaceBaseFunctionalizeAPI.replace  s    $$\ArG   c                0    [         R                  " U5        g r,   )r   r   rV   tensors     r&   r   "BaseFunctionalizeAPI.commit_update  s    **62rG   c                0    [         R                  " U5        g r,   )r   r   r  s     r&   r   BaseFunctionalizeAPI.sync  s    !!&)rG   c                0    [         R                  " U5        g r,   )r   r   r  s     r&   r   7BaseFunctionalizeAPI.mark_mutation_hidden_from_autograd   s    ??GrG   r]   NrW   r   r\   r   )rW   'torch.Tensor | tuple[torch.Tensor, ...]r\   r   )r  Callable[..., Any]r\   r  r\   zAbstractContextManager[None]r  ra   r  ra   r\   r   r  ra   r\   r   )r   r   r   r   r   r<  r;  r  r  r  r   r   r   r   r]   rG   r&   r  r    sf           B3*HrG   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5       r
SS jrSS	 jrSS
 jrSS jrSrU =r$ )r:  i  c                ^   > [         TU ]  5         U(       a  UO	[        5       U l        X l        g r,   )r   r   r   r{   rD   )rV   r{   rD   r   s      r&   r   PythonFunctionalizeAPI.__init__  s%     	 D&:&<	(rG   c                    U R                      [        R                  R                  R	                  [        R
                  [        R                  U5      sS S S 5        $ ! , (       d  f       g = fr,   )r{   r   rJ  _pytreer5  r   r   r   r  s     r&   r<  #PythonFunctionalizeAPI.wrap_tensors  s=    YY;;&&44.<<d YYs   AA
A,c                |    [         R                  R                  R                  [        [        R
                  U5      $ r,   )r   rJ  r  r5  r   r   r  s     r&   r;  %PythonFunctionalizeAPI.unwrap_tensors  s/     {{""00.>>
 	
rG   c                ,    [        XR                  5      $ r,   )r  r{   r  s     r&   r  $PythonFunctionalizeAPI.functionalize  s    %gyy99rG   c                ,    [         R                  " 5       $ r,   )
contextlibnullcontextr   s    r&   r  )PythonFunctionalizeAPI.redispatch_to_next  s     %%''rG   c                b    [        U [        5      (       d  [        U S[        U 5       35      eU $ )Nz! must be a FunctionalTensor, got )r   r   rN   type)r  r+  s     r&   _check_cast_functional-PythonFunctionalizeAPI._check_cast_functional%  s7    &"233 &9$v,H  rG   c                    U R                  US5      n[        U[        5      (       a  [        S5      eUR	                  U5        g )Nr  z,output_tensor must not be a FunctionalTensor)r  r   r   rN   r   )rV   r  r  fts       r&   r  PythonFunctionalizeAPI.replace-  s:    ((~Fm%566 !OPP
M"rG   c                D    U R                  US5      R                  5         g Nr  )r  r   r  s     r&   r   $PythonFunctionalizeAPI.commit_update3  s    ##FH5CCErG   c                D    U R                  US5      R                  5         g r  )r  r   r  s     r&   r   PythonFunctionalizeAPI.sync6  s    ##FH5::<rG   c                D    U R                  US5      R                  5         g r  )r  r   r  s     r&   r   9PythonFunctionalizeAPI.mark_mutation_hidden_from_autograd9  s    ##H	

,
,
.rG   )r{   rD   r   )r{   r  rD   r"   r\   r   rW   
tuple[Any]r\   r  )rW   z<torch.Tensor | tuple[torch.Tensor, ...] | list[torch.Tensor]r\   r   r  r
   r\   r
   r  )r  ra   r+  r   r\   r   r  r  )r   r   r   r   r   r<  r;  r  r  r   r  r  r   r   r   r   r   r   s   @r&   r:  r:    sz    MR)/)FJ)	) )
P
	
:(  #F=/ /rG   r:  c                  D    \ rS rS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  rV   rW   r  s      r&   r<   CppFunctionalizeAPI.wrap_tensors@  s    U.t1==rG   c                ,    SSK Jn  U" U[        5       S9$ Nr   )#_unwrap_all_tensors_from_functional)reapply_views)r  r  _reapply_viewsrV   rW   r  s      r&   r;  "CppFunctionalizeAPI.unwrap_tensorsE  s    	
 34~GWXXrG   c                @    [         R                  R                  U5      $ r,   )r   rB   r  r  s     r&   r  !CppFunctionalizeAPI.functionalizeO  s    zz''00rG   c                    [         R                  R                  [         R                  R                  [         R                  R                  R
                  5      5      $ r,   )r   rI   r  r7  rK   rL   r   s    r&   r  &CppFunctionalizeAPI.redispatch_to_nextR  s;    xx00HH##EHH$8$8$F$FG
 	
rG   r]   Nr  rW   r  r\   r  r  r  )	r   r   r   r   r<  r;  r  r  r   r]   rG   r&   r  r  ?  s)    >
Y;Y	0Y1
rG   r  c                  N    \ 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r	g)FunctorchFunctionalizeAPIiX  c                    Xl         g r,   interpreter)rV   r  s     r&   r   "FunctorchFunctionalizeAPI.__init__Y  s    &rG   c                J    SSK Jn  U" XR                  R                  5       S9$ r  )r  r  r  r  r  s      r&   r<  &FunctorchFunctionalizeAPI.wrap_tensors\  s    U.t;K;K;Q;Q;STTrG   c                J    SSK Jn  U" XR                  R                  5       S9$ r  )r  r  r  functionalize_add_back_viewsr  s      r&   r;  (FunctorchFunctionalizeAPI.unwrap_tensorsa  s(    	
 3 0 0 M M O
 	
rG   c                    [         R                  R                  UU R                  R	                  5       (       a  SS9$ SS9$ )Nmutations_and_views	mutations)r9  )r   rB   r  r  r  r  s     r&   r  'FunctorchFunctionalizeAPI.functionalizem  sQ    zz'' ##@@BB & ( 
 	

 ! ( 
 	
rG   c                6    U R                   R                  5       $ r,   )r  lowerr   s    r&   r  ,FunctorchFunctionalizeAPI.redispatch_to_nextw  s    %%''rG   r  N)r  r   r\   r   r  r  r  r  )
r   r   r   r   r   r<  r;  r  r  r   r]   rG   r&   r  r  X  s,    'U
	
;	
	0	

(rG   r  c                p    [        U [        5      (       a   [        R                  " U R                  5      $ U $ r,   )r   r   r   r   rb   )r  s    r&   mb_unwrap_functional_tensorr  {  s*    &*++,,V[[99MrG   )r#   r   r\   r"   )rB   r   rC   r"   rD   r"   r\   zbool | None)rO   r   r\   r   )rY   r   r\   r  )r\   zGenerator[None, None, None])rB   r  r{   r   r\   r  )r  ra   r\   ra   )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.tracebackfx	tracebackrb  torch.utils._pytreerJ  r  r4  torch._Cr   r  r   r   torch._subclasses.meta_utilsr   torch.utils._python_dispatchr   r   r   r   r   _logginggetArtifactLoggerr   r    r'   	lru_cacherF   rR   r^   r   r   r   contextmanagerru  r  r  r:  r  r  r  r]   rG   r&   <module>r     sd   "     # - % " ==#E%  ) ) $ $ K K 6  nn66xARS 
$ S! 
 " 26   " F"2K!u|| K!\l, l^	 J J <P;Q!
!$8!!HH3 H<8/1 8/v
. 
2 ( 4  (FrG   