
    PhNK                        U d dl Z d dlmZmZmZmZmZmZ d dlZd dl	m
Z
 d dlZd dlZd dlmZmZmZmZmZ  G d d      Zd Zd Zdd	ee   fd
Zdd	eeeej.                  j0                  f      fdZe j4                  dd	ee   fd       Ze j4                  d        Z G d de      Zd Zd Zd Z e
 G d d             Z!e
 G d d             Z"i Z#eee"f   e$d<   de"fdZ%d Z&y)    N)OptionalUnionListSetDictAny)	dataclass)_len_torch_dispatch_stack_get_dispatch_stack_at_pop_torch_dispatch_stack_push_on_torch_dispatch_stackDispatchKeyc                   <    e Zd ZdZddZd	dZd Zd Zed        Z	y)
TorchDispatchModea  
    A ``TorchDispatchMode`` allows you to override the meaning of all
    ``__torch_dispatch__`` overrideable functions within a dynamic scope,
    without having to actually create a tensor subclass or manually
    monkey-patch functions in the PyTorch API.  Some common situations
    where you should use a mode:

        * You want to override the meaning of factory functions, or other
          functions that do not otherwise take a tensor as an argument
          (these cannot be overridden with tensor subclasses).

        * You want to override the behavior of all functions without needing
          to wrap your inputs in tensor subclasses; e.g., if you are just
          interested in logging intermediate computations.

        * You want to control the order of execution of various tensor
          subclasses explicitly, rather than implicitly via the return of
          ``NotImplemented``.

    Independent subclasses of :class:`TorchDispatchMode` are compositional:
    modes can be pushed onto a stack using ``with MyMode():``.
    When you call functions in the PyTorch API inside your
    ``__torch_dispatch__`` implementation, by default, they will forward on to
    the next mode on the mode stack.  If you want recursively call back into
    your current ``__torch_dispatch__`` implementation, either explicitly
    invoke ``self.__torch_dispatch__(...)``, or use the context manager
    ``__torch_dispatch__(self)`` to make PyTorch
    API self-referential (beware of infinite loops, in this case!)
    Nc                 t    |6t        |t        j                  j                        sJ || j                  d<   y y N_dispatch_key)
isinstancetorch_Cr   __dict__)selfr   s     gC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/utils/_python_dispatch.py__init__zTorchDispatchMode.__init__1   s5    $mUXX-A-ABBB-:DMM/* %    c                     t               N)NotImplementedErrorr   functypesargskwargss        r   __torch_dispatch__z$TorchDispatchMode.__torch_dispatch__6   s    !##r   c                 R    t        | | j                  j                  dd              | S r   )
_push_moder   get)r   s    r   	__enter__zTorchDispatchMode.__enter__9   s"    4**?DABr   c                     | j                   j                  dd       }|| j                   j                  dd       }t        |       y )Nr   	_mode_key)r   r'   	_pop_mode)r   exc_typeexc_valexc_tbmb_dk_or_mode_keys        r   __exit__zTorchDispatchMode.__exit__=   sA     MM--otD$ !% 1 1+t D#$r   c                 @    t        j                  d        | |i |}|S )NzP`Mode.push()` is no longer necessary and can be replaced with just `with Mode()`)warningswarn)clsr"   r#   instances       r   pushzTorchDispatchMode.pushE   s#    hi''r   r    N)
__name__
__module____qualname____doc__r   r$   r(   r0   classmethodr6   r8   r   r   r   r      s/    <;
$%  r   r   c                  >    t               } | dkD  rt        | dz
        S y Nr      )r
   r   )	stack_lens    r   _get_current_dispatch_moderB   K   s#    )+I1}%i!m44r   c                  d    t               } t        |       D cg c]  }t        |       c}S c c}w r   )r
   ranger   )rA   is     r    _get_current_dispatch_mode_stackrF   S   s/    )+I/4Y/?@/?!"1%/?@@@s   -kc                     |Uddl m}m} t        j                  j                  |      } |       D ]  }|D ]  }|j                  |          |||        y t        |        y )Nr   )push_mode_for_keyget_cached_ops)
torch._opsrI   rJ   r   r   _functionality_to_backend_keys_uncache_dispatchr   )moderG   rI   rJ   ksopkeys          r   r&   r&   W   sY    }@ XX44Q7 "B$$S)  # 	!T"%d+r   c                     | $t        | t        j                  j                        rt	        |       S ddlm}  ||       S )Nr   )pop_mode_for_key)r   r   r   _TorchDispatchModeKeyr   rK   rS   )rG   rS   s     r   r+   r+   e   s5    yJq%(("@"@A(+++ Ar   c              #   f   K   t        |       }	 | t        ||        y # t        ||        w xY wwr   )r+   r&   )rG   olds     r   _pop_mode_temporarilyrW   o   s-     
A,C	3
3s   1  1.1c               #      K   t               } t        |       D cg c]  }t                }}	 | t        |      D ]  }t	        |        y c c}w # t        |      D ]  }t	        |        w xY wwr   )r
   rD   r+   reversedr&   )mode_len_	old_modesrN   s       r   _disable_current_modesr]   x   sh     (*H&+Ho6ooI6Y'Dt ( 7 Y'Dt (s%   A3AA3A !A3A00A3c                       e Zd ZddZy)BaseTorchDispatchModeNc                     |i } ||i |S r   r8   r   s        r   r$   z(BaseTorchDispatchMode.__torch_dispatch__   s    >FT$V$$r   r7   )r9   r:   r;   r$   r8   r   r   r_   r_      s    %r   r_   c                     t        | t        j                        xr t        |       t        j                  k7  }|xr t	        | d      xr t	        | d      S )a  
    Returns whether or not a tensor subclass that implements __torch_dispatch__
    is 'traceable' with torch.compile.
    In order for a tensor subclass to support TorchDispatchMode-style tracing in PT2,
    It must implement two magic methods: __tensor_flatten__ and __tensor_unflatten__.
    It is also expected to obey some restrictions around traceability and aliasing
    (TODO: add clear documentation around this.)
    __tensor_flatten____tensor_unflatten__)r   r   Tensortypehasattr)tis_subclasss     r   is_traceable_wrapper_subclassri      sF     Q-I$q'U\\2IKb71&:;bKa@bbr   c                     | j                         \  }}i }|D ]  } ||t        | |            ||<    t        |       j                  ||      S )a  
    Given a traceable, wrapper tensor subclass ``t`` that implements
    ``__torch_dispatch__`` and holds some inner tensors,
    and a callback of type ``Callable[[str, torch.Tensor], torch.Tensor]``,
    `transform_subclass` will construct a fresh instance of the wrapper tensor subclass.
    It will do so by grabbing each inner tensor attribute from the wrapper,
    passing them into ``callback`` to get a transformed tensor,
    and putting each transformed tensor into the fresh tensor subclass instance.

    Note: this function will not handle ensuring that the fresh subclass
    gets the same (autograd, and aliasing) metadata as the original tensor.
    This is generally handled in other subsystems like AOTAutograd.
    )rb   getattrre   rc   )rg   callbackattrsctxtransformed_tensors_dictattrs         r   transform_subclassrq      sW     %%'JE3!)1$48H)I & 7''(@#FFr   c                 *    t         t        j                  j                        sJ t        |t              sJ t        |t
        t        f      sJ t        j                  j                  j                  |      } fd}d }t         j                  j                        }t         j                  j                        }t        |      D ]E  }	t        |      D ]5  }
 ||j                  |	   |j                  |
         s' |||	   ||
          7 G y)a  
    Given: an OpOverload, a SchemaInfo (cached information from torchgen about schema),
    and the inputs/outputs to the OpOverload,
    this function checks to see if func is a view operator
    (by checking if any of the outputs in the op's schema
     are immutable aliases of inputs).
    If so, this function manually aliases the storage of the output tensor
    with its corresponding input tensor alias.
    It does this by unsafely overwriting the storage field of the output tensor
    to be the same storage as the input.
    c           
      h   t        |       st        |      r_t        |t              r|n|g}|D ]E  }t        |       t        |      k(  rJ dt	               dt        |        dt        |       d        t
        j                  j                  j                         5  t
        j                  j                         }t
        j                  j                  d       	 t        |t              rd|D ]^  }t
        j                  j                  j                  j                  || j!                         |j#                         |j$                         ` nt        |t
        j&                        sJ dt        |              t
        j                  j                  j                  j                  || j!                         |j#                         |j$                         t
        j                  j                  |       	 d d d        y # t
        j                  j                  |       w xY w# 1 sw Y   y xY w)NzCalled z with input of type z
and output of type z. But expected types to match.Tztype: )ri   r   listre   strr   utils_mode_utilsno_dispatchr   _meta_in_tls_dispatch_include!_set_meta_in_tls_dispatch_includeopsatenset_source_Storage_storage_offsetuntyped_storagestorage_offsetshaperd   )argretret_listrmeta_in_tlsr    s        r   alias_non_inplace_storagez<_correct_storage_aliasing.<locals>.alias_non_inplace_storage   s    )-1Ns1S(d3s#HCyDG+ @TCWX\]`XaWb cI;<.@ @+  [[$$002  ((@@BKHH66t<H c4( 		++II!SM`M`Mbdedtdtdvxyxx  A ! &c5<<8NF49+:NN8IINN''EEc3K^K^K`bebtbtbvx{  yB  yB  C::;G+ 32* ::;G+ 32s%   >H(C=HH(!H%%H((H1c                 n    | j                   |j                   z  }t        |      dkD  xr | j                   S )Nr   )	alias_setlenis_write)r   r   shared_aliasess      r   is_read_only_alias_matchz;_correct_storage_aliasing.<locals>.is_read_only_alias_match   s0    6>"Q&;s||+;;r   N)r   r   _ops
OpOverloadtuplert   rv   _pytreetree_leavesr   _schema	argumentsreturnsrD   r"   outs)r    schema_infor"   r   	flat_outsr   r   num_argsnum_returnsarg_idx
return_idxs   `          r   _correct_storage_aliasingr      s     dEJJ11222dE"""dT5M***##//5I$HL< 4<<))*Hdll**+K?,J'(8(8(A;CSCST^C_`)$w-j9IJ - #r   c                   8    e Zd ZU ee   ed<   eed<   ee   ed<   y)	AliasInfor   r   nameN)r9   r:   r;   r   ru   __annotations__boolr   r8   r   r   r   r      s    3xN
3-r   r   c                   .    e Zd ZU ee   ed<   ee   ed<   y)
SchemaInfor"   r   N)r9   r:   r;   r   r   r   r8   r   r   r   r      s    
y/
y/r   r   parsed_schema_mapreturnc                 0   | t         v r	t         |    S | j                  dk(  rt        | j                        }|j	                  d      sJ |dd  }dd l}|j                  dd|      }|j                  dd|      }|j                  d	d
      }t        j                  j                  j                  |      }|j                  j                  D cg c]q  }t        |j                  
t!               nt!        |j                  j"                        |j                  d uxr |j                  j$                  |j&                        s }}|j(                  D cg c]q  }t        |j                  
t!               nt!        |j                  j"                        |j                  d uxr |j                  j$                  |j&                        s }}n | j                  j                  D cg c]q  }t        |j*                  
t!               nt!        |j*                  j,                        |j*                  d uxr |j*                  j$                  |j&                        s }}| j                  j(                  D cg c]q  }t        |j*                  
t!               nt!        |j*                  j,                        |j*                  d uxr |j*                  j$                  |j&                        s }}t/        ||      }|t         | <   |S c c}w c c}w c c}w c c}w )Nr|   zaten::   r   z=\[[0, ]+\]z=0z=\[[1, ]+\]z=1z=[0, 1]z=[0,1])r   r   r   )r"   r   )r   	namespaceru   r   
startswithresubreplacetorchgenmodelFunctionSchemaparser   flat_allr   
annotationsetr   r   r   r   
alias_info
before_setr   )r    torchgen_schema_strr   torchgen_schemaaarg_schemasout_schemasr   s           r   get_alias_infor      s      && ~~!$,,/"--h777 2!"5 !ff^T;NO ff^T;NO199)XN"..77==>QR
 #,,55	7 6a	 ! ||3ceQ\\=S=S9T\\-G!,,2G2G
 6	 	 7 #**	, +a	 ! ||3ceQ\\=S=S9T\\-G!,,2G2G
 +	 	 , <<))	+ *a	 ! ||3ceQ\\=T=T9U\\-G!,,2G2G
 *	 	 + <<''	) (a	 ! ||3ceQ\\=T=T9U\\-G!,,2G2G
 (	 	 )
 +K@K)d17
,+
)s   A6LA6L	$A6L4A6Lc                 .    t               }d  fd}t         ||t        |t              s|fn|       t        j
                  j                   j                  v rt        |      D cg c]  \  }} |j                  |         | }}}t        |      dk(  sJ ddlm}	 t        |d   |	      st        j                  j                  j                         5  t        j                   j#                         }
t        j                   j%                  d       	   |i | t        j                   j%                  |
       	 ddd       t'        fd|j(                  D              s|S t+        fd	|j(                  D              s!t-        d
t/         j0                        z         t         j0                  j2                        dk(  r | |j(                  d         |||      S  t5        |      t7        t        |j(                        |      D cg c]0  \  \  }}} |       | |j(                  |         |||      n|2 c}}}      }|S c c}}w # t        j                   j%                  |
       w xY w# 1 sw Y   AxY wc c}}}w )aZ  
    This function should be used by wrapper tensor ``__torch_dispatch__`` subclasses
    that would like to work with torch.compile. It ensures that the subclass
    properly implements the aliasing behavior of every op,
    which is needed for correctness in AOTAutograd.
    This function will handle:

        * When we see a view op, we will alias the storages of any
          input and output tensor subclasses

        * When we see an inplace or out= op, we will directly
          return the corresponding input tensor, instead of returning
          a (potentially) fresh output tensor.
    c                     t        | j                        dk(  ry t        | j                        }t        |      dk(  sJ | j                  r|d   S y r?   )r   r   rt   r   )xr   s     r   get_write_aliasz4return_and_correct_aliasing.<locals>.get_write_aliasA  sJ    q{{q %	9~"""::Q<r   c                 |   t         j                  j                  j                  ||      \  }}t	        |j
                        D cg c]  \  }}| |j                  v r| }}}t        |      dk(  sJ |d   }	|j
                  |	   }
|
j                  |
j                  |v r||
j                     S ||	   S c c}}w )N)r"   r#   r@   r   )	r   fxoperator_schemasnormalize_function	enumerater"   r   r   r   )output_aliasr   r"   r#   new_args
new_kwargsrE   r   arg_indicesidxarg_infor    s              r   get_arg_from_aliasz7return_and_correct_aliasing.<locals>.get_arg_from_aliasK  s    $xx88KKDW[djKk* $K$4$45
5$!Qq{{* 5 	 

 ;1$$$!n##C(==$*)Dhmm,,}
s   	B8Nr@   r   )FunctionalTensorTc              3   2   K   | ]  } |      d u  y wr   r8   .0r   r   s     r   	<genexpr>z.return_and_correct_aliasing.<locals>.<genexpr>y       H7G!q!-7G   c              3   2   K   | ]  } |      d u  y wr   r8   r   s     r   r   z.return_and_correct_aliasing.<locals>.<genexpr>}  r   r   zUnsupported schema: )r   r   r   r   r   Taginplace_viewtagsr   r"   r   #torch._subclasses.functional_tensorr   rv   rw   rx   r   ry   rz   anyr   allRuntimeErrorru   r   r   re   zip)r    r"   r#   outr   r   rE   r   mutated_argsr   r   r   oouts_to_returnr   s   `             @r   return_and_correct_aliasingr   -  sI   $ !&K" dKZPSUZE[vade yy* '0ojodaIYIYZ[I\9]9ioj < A%%% 	I,q/+;<((446 $hhDDF::4@L$)&)HH>>{K 7 H{7G7GHH
 H{7G7GHH1C4EEFF
4<< A%!/+2B2B12E"FUY[abb T#Y y)9)9:C@  AKVaQ 1) 	?;+;+;A+>?dTZ[/0	1@  N
 M k" HH>>{K 760 s6   +I	I>JI$J5J!J  JJr   )'
contextlibtypingr   r   r   r   r   r   r2   dataclassesr	   r   r   torch._Cr
   r   r   r   r   r   rB   rF   r&   r   rT   r+   contextmanagerrW   r]   r_   ri   rq   r   r   r   r   r   r   r   r8   r   r   <module>r      s.    8 8  !  J J7 7rA,- ,%UXX-K-K KLM  Xk2   	 	%- %
cG(@KJ   
   
 ,. 4Z( -+J +Z\r   