
    Ph\                        d dl Z d dlmZmZ d dlmZmZmZmZ d dl	Z	d dl
mc mZ d dlmZ d dlmZmZ e	j(                  j+                  ed      Z G d de	j0                        Z G d	 d
e      Ze j6                  d        Ze j6                  d        Zd Z G d de      Z G d de      Z  G d de      Z! G d de      Z"y)    N)ABCabstractmethod)AnyCallableContextManagerTuple)$_functionalization_reapply_views_tls)return_and_correct_aliasingTorchDispatchModenot_implementedc                      e Zd ZU dZej
                  ed<   ej                  j                  j                  Z
ej                  j                  j                  ej                  j                  j                        Z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&                  gZ d Z!ej                  jD                  Z#ddZ$d Z%e&d        Z'd Z(dd	Z)dd
Z*ddZ+ddZ,y)FunctionalTensoraF  
    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).
    elemc                    t        j                  |      sJ t        j                  t         j                  j                  |      z  }t         j                  j                  | |j                  |j                         |j                         d |j                  |j                  |j                  d|j                  ddd|      }||_        |S )NFsizes)torch_is_functional_tensorr   _extra_dispatch_keys_C_dispatch_keysTensor_make_wrapper_subclassshapestridestorage_offsetdtypelayoutdevicerequires_gradr   )clsr   extra_dispatch_keysouts       nC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/_subclasses/functional_tensor.py__new__zFunctionalTensor.__new__=   s    **4000& 11EHH4K4KD4QQ 	 ll11
 JJKKM!JJKKKK%
( 
    Nc                    |D cg c]5  }|t         j                  t         j                  j                  t        fvr|7 }}|rt
        j                  d|       t        S |i }|t        j                  v rKd }t        |      dk(  rt        |d   t              sJ t        |      dk(  sJ  ||d   j                        S t        d      c c}w )N.FunctionalTensor unrecognized subclass(es): %sc                     | j                   S Nr   xs    r#   unwrapz3FunctionalTensor.__torch_dispatch__.<locals>.unwrap   s    vvr%      r   zqAttempting to use FunctionalTensor on its own. Instead, please use it with a corresponding FunctionalTensorMode())r   r   _subclasses
FakeTensorr   not_implemented_logdebugNotImplementedmetadata_fnslen
isinstancer   RuntimeError)selffunctypesargskwargstunrecognized_typesr-   s           r#   __torch_dispatch__z#FunctionalTensor.__torch_dispatch__p   s     
u'8'8'C'CEUVV  	 

 %%@BT "!>F
 #000 t9>ja:J&KKKv;!### Q%% 
 	
G
s   :Cc                 4    dt        | j                         dS )NzFunctionalTensor())reprr   r8   s    r#   __repr__zFunctionalTensor.__repr__   s    "4		?"3155r%   c                    t        j                  |       rJ t        j                  |       }t               5  t        j                  | |       t        |      }t        j                  ||       d d d        |S # 1 sw Y   S xY wr)   )r   r   _to_functional_tensorFunctionalTensorMode_mirror_autograd_meta_tor   )r,   x_functionalr"   s      r#   to_functionalzFunctionalTensor.to_functional   su     ..q111
 2215 "#**1l;"<0C**<= $ 
	 $ 
s   8A99Bc                 j    t        j                  |        t        j                  | j                        S r)   )r   _sync_from_functional_tensorr   rC   s    r#   from_functionalz FunctionalTensor.from_functional   s#    D,,TYY77r%   c                 D    t        j                  | j                  |       y r)   )r   _functionalize_replacer   )r8   outputs     r#   replace_zFunctionalTensor.replace_   s    $$TYY7r%   c                 B    t        j                  | j                         y r)   )r   _functionalize_commit_updater   rC   s    r#   commit_updatezFunctionalTensor.commit_update   s    **4995r%   c                 B    t        j                  | j                         y r)   )r   _functionalize_syncr   rC   s    r#   synczFunctionalTensor.sync   s    !!$)),r%   c                 B    t        j                  | j                         y r)   )r   1_functionalize_mark_mutation_hidden_from_autogradr   rC   s    r#   "mark_mutation_hidden_from_autogradz3FunctionalTensor.mark_mutation_hidden_from_autograd   s    ??		Jr%    NreturnN)-__name__
__module____qualname____doc__r   r   __annotations__r   _TorchDispatchModeKey
FUNCTIONAL	_mode_key,_additional_keys_to_prop_for_wrapper_tensorsaddDispatchKey
ZeroTensorr   opsatenis_contiguousdefaultmemory_formatis_strides_like_formatis_non_overlapping_and_densesizesym_sizer   
sym_strider   sym_storage_offsetnumel	sym_numeldimr4   r$   _disabled_torch_function_impl__torch_function__r?   rD   staticmethodrJ   rN   rR   rU   rX   r[   r]   r%   r#   r   r      s    ,, ..99I !88PPTT'' 			$$,,		$$22		--55		33;;		##		''		%%		!!))		%%--		))11		$$		  ((		""L ,b ??&
P6  &886-Kr%   r   c                   6     e Zd Zd Z fdZ fdZddZ xZS )rG   c                     d| _         g | _        t        j                  j                  j
                  | _        d| _        y )NFT)is_on_stackenter_stackr   r   re   rf   rg    decompose_composite_implicit_opsrC   s    r#   __init__zFunctionalTensorMode.__init__   s3      77BB04-r%   c                    t         j                  j                  t         j                  j                  j                        	 )| j
                  j                  d       t        | !         S | j
                  j                  d       | S )NTF)	r   r   _get_dispatch_modere   rf   r   appendsuper	__enter__)r8   	__class__s    r#   r   zFunctionalTensorMode.__enter__   sj    HH''(F(F(Q(QR ##D)7$&&##E*Kr%   c                 b    | j                   j                         }|rt        |   |||       y y r)   )r   popr   __exit__)r8   abcr   r   s        r#   r   zFunctionalTensorMode.__exit__   s0    &&**,GQ1% r%   c                 0	   |i }|D cg c]@  }t        |t        j                  j                        s|t        j                  t
        fvr|B }}|rt        j                  d|       t        S |t
        j                  vrj| j                  r^t        j                  j                  |j                               r1| 5   |j                  |i |}|t        ur|cd d d        S 	 d d d        d }d }	d}
d }ddlm}m}  ||      rZt        j                  j%                  |j                         t        j                  j&                  j(                        s
 ||||      S t+        j,                  t
        |||f      \  }}t        j                  j/                  t        j                  j&                  j(                        }t        j                  j1                  t        j                  j&                  j(                        }|s|rJ t        j                  j3                         t        j                  j5                  t        j                  j&                  j(                        z  }t        j                  j7                         j9                  t        j                  j&                  j(                        t
        j:                  z
  }t        j                  j=                  ||      5  	 t        j>                  d      } ||i |}t+        j,                  t        j                  |	|      }t        j@                          t        j>                  |       	 d d d        t        j                  j/                  t        j                  j&                  j(                        }t        j                  j1                  t        j                  j&                  j(                        }|s|rJ tC        d	 t+        jD                        D              r1|t        jF                  jH                  jJ                  jL                  k(  r|S tO        ||||      S c c}w # 1 sw Y   pxY w# t        j@                          t        j>                         w xY w# 1 sw Y   8xY w)
Nr'   c                 2    t        j                  |       sJ y r)   )r   r   r+   s    r#   assert_is_functionalzEFunctionalTensorMode.__torch_dispatch__.<locals>.assert_is_functional   s    ..q111r%   c                     t        | t              rJ t        | t        j                        r t        j                  |       rt        |       S | S r)   )r6   r   r   r   r   r+   s    r#   wrapz5FunctionalTensorMode.__torch_dispatch__.<locals>.wrap   s@     "!%5666!U\\*u/J/J1/M'**Hr%   Fc                     d}| j                   S )NTr*   )r,   any_functional_inputss     r#   r-   z7FunctionalTensorMode.__torch_dispatch__.<locals>.unwrap  s    $(!66Mr%   r   )can_auto_functionalizedo_auto_functionalizeTc              3   <   K   | ]  }t        |t                y wr)   )r6   r   ).0r,   s     r#   	<genexpr>z:FunctionalTensorMode.__torch_dispatch__.<locals>.<genexpr>E  s       9A 1./9s   )(
issubclassr   r/   r0   r   r   r1   r2   r3   r4   r   r   _dispatch_has_kernelname	decompose*torch._higher_order_ops.auto_functionalizer   r   %_dispatch_has_kernel_for_dispatch_keyrj   Functionalizepytree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remover   _ForceDispatchKeyGuard#_functionalize_enable_reapply_views_disable_functionalizationanytree_leavesrl   rm   
lift_freshro   r
   )r8   r9   r:   r;   r<   r=   r>   rr   r   r   r-   r   r   args_unwrappedkwargs_unwrappedis_includedis_excludedinclude_to_setexclude_to_setold_apply_viewsouts_unwrappedouts_wrappeds                          r#   r?   z'FunctionalTensorMode.__torch_dispatch__   s   >F 
a!2!2!=!=>%,,(899  	 
 %%@BT "! (55555 --diik:"DNND3F3N*	  + 	2	 !&		

 "
((@@IIK--;;
 )tV<<+1+?+?ftVn,
(( hhEEHH  ..
 hhEEHH  ..
 +--HH446hh%%ehh&:&:&H&HIJ 	
 HH446==$$22 334 	 XX,,^^LK"'"K"KD"Q!%~!J9I!J%33ELL$W00299/J M hhEEHH  ..
 hhEEHH  ..
 +--  ++L9  uyy~~00888
 +4v|LLi
& J 00299/J MLs7   AQ;Q'R)AQ+)RQ+RRRr\   )r`   ra   rb   r   r   r   r?   __classcell__)r   s   @r#   rG   rG      s    5
&
xMr%   rG   c               #   *  K   t         j                  j                  t         j                  j                  j                        } 	 d  |  t         j                  j                  |        y y # |  t         j                  j                  |        w w xY wwr)   r   r   _unset_dispatch_modere   rf   _set_dispatch_mode)maybe_func_modes    r#   maybe_disable_functional_moder   W  so     hh33&&11O9&HH''8 '?&HH''8 '   ABA, 	#B,$BBc               #   *  K   t         j                  j                  t         j                  j                  j                        } 	 |  |  t         j                  j                  |        y y # |  t         j                  j                  |        w w xY wwr)   r   )olds    r#   unset_functional_temporarilyr   e  sl     
((
'
'(F(F(Q(Q
RC-	?HH'', 3?HH'', r   c                 &     d d  fd}|S )Nc                 d    t        | t        j                        rt        j	                  |       S | S r)   )r6   r   r   r   rJ   r=   s    r#   to_funz&dispatch_functionalize.<locals>.to_funy  s&    a&#11!44r%   c                     t        | t              s3t        | t        j                        rt        j                  |       rJ | S t        j
                  |        t        j                  | j                        S r)   )r6   r   r   r   r   rL   rM   r   r   s    r#   from_funz(dispatch_functionalize.<locals>.from_fun~  sT    !-.!U\\* 66q999HA,,QVV44r%   c                  n   t        j                  t        j                  |       }t        j                  t        j                  |      }t        j                  | }t        j                  di |}t        j
                  j                  t        j
                  j                  t        j
                  j                  j                              }|5  t               5   
|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)Nr]   )r   r   r   r   arg_tree_leavesr   _ExcludeDispatchKeyGuardr   rj   r   rG   r   )r;   r<   	func_argsfunc_kwargsflattened_wrapped_argsflattened_wrapped_kwargsdisable_abovefunc_outputsoutputsr   r9   r   s            r#   innerz%dispatch_functionalize.<locals>.inner  s    ((vtD	**5<<H!'!7!7!C#)#9#9#HK#H 99HH##EHH$8$8$F$FG
 02:k:L**+;X|TG	 32]]22]]]s$   D+%D	D+D	D++D4r]   )r9   r   r   r   s   ` @@r#   dispatch_functionalizer   w  s    
5  Lr%   c                       e Zd Zedee   dee   fd       Zedee   dee   fd       Zededefd       Z	ede
fd       Zedd	       Zedd
       Zedd       Zedd       Zy)BaseFunctionalizeAPIr;   r_   c                      y r)   r]   r8   r;   s     r#   wrap_tensorsz!BaseFunctionalizeAPI.wrap_tensors      r%   c                      y r)   r]   r   s     r#   unwrap_tensorsz#BaseFunctionalizeAPI.unwrap_tensors  r   r%   inner_fc                      y r)   r]   r8   r   s     r#   functionalizez"BaseFunctionalizeAPI.functionalize  r   r%   c                      y r)   r]   rC   s    r#   redispatch_to_nextz'BaseFunctionalizeAPI.redispatch_to_next  r   r%   Nc                      y r)   r]   r8   input_tensoroutput_tensors      r#   replacezBaseFunctionalizeAPI.replace  r   r%   c                      y r)   r]   r8   tensors     r#   rU   z"BaseFunctionalizeAPI.commit_update  r   r%   c                      y r)   r]   r   s     r#   rX   zBaseFunctionalizeAPI.sync  r   r%   c                      y r)   r]   r   s     r#   r[   z7BaseFunctionalizeAPI.mark_mutation_hidden_from_autograd  r   r%   r^   )r`   ra   rb   r   r   r   r   r   r   r   r   r   r   rU   rX   r[   r]   r%   r#   r   r     s    s c
   5: %*   X (   N          r%   r   c                       e Zd Zdee   dee   fdZdee   dee   fdZdedefdZde	fdZ
dd	Zdd
ZddZddZy)PythonFunctionalizeAPIr;   r_   c                 |    t         j                  j                  j                  t        t        j
                  |      S r)   )r   utils_pytreer   r   rJ   r   s     r#   r   z#PythonFunctionalizeAPI.wrap_tensors  s-    {{""00.<<d
 	
r%   c                 |    t         j                  j                  j                  t        t        j
                  |      S r)   )r   r   r   r   r   rN   r   s     r#   r   z%PythonFunctionalizeAPI.unwrap_tensors  s-    {{""00.>>
 	
r%   r   c                     t        |      S r)   )r   r   s     r#   r   z$PythonFunctionalizeAPI.functionalize  s    %g..r%   c                     t               S r)   )r   rC   s    r#   r   z)PythonFunctionalizeAPI.redispatch_to_next  s    +--r%   Nc                 n    t        |t              sJ t        |t              rJ |j                  |       y r)   )r6   r   rR   r   s      r#   r   zPythonFunctionalizeAPI.replace  s2    ,(8999m-=>>>m,r%   c                 H    t        |t              sJ |j                          y r)   )r6   r   rU   r   s     r#   rU   z$PythonFunctionalizeAPI.commit_update  s    &"2333r%   c                 H    t        |t              sJ |j                          y r)   )r6   r   rX   r   s     r#   rX   zPythonFunctionalizeAPI.sync  s    &"2333r%   c                 H    t        |t              sJ |j                          y r)   )r6   r   r[   r   s     r#   r[   z9PythonFunctionalizeAPI.mark_mutation_hidden_from_autograd  s    &"2333113r%   r^   r`   ra   rb   r   r   r   r   r   r   r   r   r   rU   rX   r[   r]   r%   r#   r   r     sk    
s 
c
 


5: 
%* 

/X /( /.N .-
4r%   r   c                       e Zd Zdee   dee   fdZdee   dee   fdZdedefdZde	fdZ
dd	Zdd
ZddZddZy)CppFunctionalizeAPIr;   r_   c                 "    ddl m}  ||d      S Nr   )_wrap_all_tensors_to_functional)level)!torch._functorch.eager_transformsr   r8   r;   r   s      r#   r   z CppFunctionalizeAPI.wrap_tensors  s    U.t1==r%   c                 2    ddl m}  ||t                     S Nr   )#_unwrap_all_tensors_from_functional)reapply_views)r  r  _reapply_viewsr8   r;   r  s      r#   r   z"CppFunctionalizeAPI.unwrap_tensors  s    	
 34~GWXXr%   r   c                 @    t         j                  j                  |      S r)   )r   r9   r   r   s     r#   r   z!CppFunctionalizeAPI.functionalize  s    zz''00r%   c                     t         j                  j                  t         j                  j                  t         j                  j                  j
                              S r)   )r   r   r   r   rj   r   rC   s    r#   r   z&CppFunctionalizeAPI.redispatch_to_next  s;    xx00HH##EHH$8$8$F$FG
 	
r%   Nc                 0    t        j                  ||       y r)   r   rP   r   s      r#   r   zCppFunctionalizeAPI.replace      $$\=Ar%   c                 .    t        j                  |       y r)   r   rT   r   s     r#   rU   z!CppFunctionalizeAPI.commit_update      **62r%   c                 .    t        j                  |       y r)   r   rW   r   s     r#   rX   zCppFunctionalizeAPI.sync      !!&)r%   c                 .    t        j                  |       y r)   r   rZ   r   s     r#   r[   z6CppFunctionalizeAPI.mark_mutation_hidden_from_autograd      ??Gr%   r^   r   r]   r%   r#   r   r     sp    >s >c
 >
Y5: Y%* Y1X 1( 1
N 

B3*Hr%   r   c                       e Zd Zd Zdee   dee   fdZdee   dee   fdZdedefdZ	de
fdZdd
ZddZddZddZy	)FunctorchFunctionalizeAPIc                     || _         y r)   )interpreter)r8   r  s     r#   r   z"FunctorchFunctionalizeAPI.__init__  s
    &r%   r;   r_   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Tr%   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
 	
r%   r   c                     t         j                  j                  || j                  j	                         rd      S d      S )Nmutations_and_views	mutations)r   )r   r9   r   r  r  r   s     r#   r   z'FunctorchFunctionalizeAPI.functionalize  sL    zz''<<> ) ( 
 	
 	 ( 
 	
r%   c                 6    | j                   j                         S r)   )r  lowerrC   s    r#   r   z,FunctorchFunctionalizeAPI.redispatch_to_next  s    %%''r%   Nc                 0    t        j                  ||       y r)   r  r   s      r#   r   z!FunctorchFunctionalizeAPI.replace  r  r%   c                 .    t        j                  |       y r)   r  r   s     r#   rU   z'FunctorchFunctionalizeAPI.commit_update!  r  r%   c                 .    t        j                  |       y r)   r  r   s     r#   rX   zFunctorchFunctionalizeAPI.sync$  r  r%   c                 .    t        j                  |       y r)   r  r   s     r#   r[   z<FunctorchFunctionalizeAPI.mark_mutation_hidden_from_autograd'  r  r%   r^   )r`   ra   rb   r   r   r   r   r   r   r   r   r   r   rU   rX   r[   r]   r%   r#   r  r    su    'Us Uc
 U

5: 
%* 

X 
( 
(N (B3*Hr%   r  )#
contextlibabcr   r   typingr   r   r   r   r   torch.utils._pytreer   r   r   torch._Cr	   r  torch.utils._python_dispatchr
   r   _logginggetArtifactLoggerr`   r1   r   r   rG   contextmanagerr   r   r   r   r   r   r  r]   r%   r#   <module>r1     s     # 7 7  $ $ K Wnn66xARS pKu|| pKfTM, TMn 9 9 - -" F3 D 41  4FH. HD'H 4 'Hr%   