
    ΅iR                    l   S SK Jr  S SKrS SKrS SKrS SKJr  S SKJr  S SK	J
r
JrJrJr  S SKJr  S SKrS SKrS SKrS SKJrJrJrJrJr  S SKJr  \(       a  S S	KJr  S
qS
qS
qS)S*S jjr S+S jr!S+S jr" " S S5      r#S,S jr$S r%S r&S r'S-S jr(S.S jr)S/S0S jjr*\RV                  S/S1S jj5       r,\RV                  S 5       r- " S S\#5      r. " S S\5      r/S2S jr0S3S jr1S4S jr2S5S  jr3S6S! jr4\ " S" S#5      5       r5\ " S$ S%5      5       r6\Rn                  S7S& j5       r8      S8S' jr9S( r:g)9    )annotationsN)deque)	dataclass)castoverloadProtocolTYPE_CHECKING)TypeIs)_get_dispatch_stack_at_len_torch_dispatch_stack_pop_torch_dispatch_stack_push_on_torch_dispatch_stackDispatchKey)/set_is_in_mode_without_ignore_compile_internals)SequenceFc                (    U (       a  [         $ [        $ N)_is_in_torch_dispatch_mode$_is_in_non_infra_torch_dispatch_mode)include_infra_modess    V/home/james-whalen/.local/lib/python3.13/site-packages/torch/utils/_python_dispatch.pyis_in_torch_dispatch_moder   )   s      	# 2    c                     [         $ r   )0_is_in_any_mode_without_ignore_compile_internals r   r   /is_in_any_mode_without_ignore_compile_internalsr   1   s    ;;r   c                     [         R                  R                  5       n [        U 5       H<  n[	        U5      nUR                  5       (       a  M%  UR                  5       (       a  M<    g   g)NTF)torch_Cr   ranger   is_infra_modeignore_compile_internals)	stack_lenidxmodes      r    any_torch_dispatch_mode_on_stackr'   5   s[    224IY%c* ((**   r   c                     ^  \ rS rSrSrSrU 4S jrSS jrS rSS jr	S r
S	 r\S
 5       r\SS j5       r\SS j5       r\SS j5       rSrU =r$ )TorchDispatchModeF   a  
A ``TorchDispatchMode`` allows you to override the meaning of all
``__torch_dispatch__`` overridable 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
``self`` to make PyTorch
API self-referential (beware of infinite loops, in this case!)
Fc                   > [         TU ]  " S0 UD6  U R                  5       (       aQ  SU R                  ;   a@  U R                  S   n[	        U[
        5      (       d  [        R                  " USS9U l        g g g g )N__torch_dispatch__T)	recursiver   )	super__init_subclass___should_skip_dynamo__dict__
isinstanceclassmethodr   _disable_dynamor,   )clskwargsraw	__class__s      r   r/   #TorchDispatchMode.__init_subclass__k   so    !+F+""$$#s||3ll#78!#{33-2-B-B3RV-WC* 4 4 %r   c                    UbB  [        U[        R                  R                  5      (       d  [	        S5      eXR
                  S'   [        5       U l        [        5       U l        [        5       U l	        g )Nz,_dispatch_key must be a torch._C.DispatchKey_dispatch_key)
r2   r   r    r   AssertionErrorr1   r   old_dispatch_mode_flags!old_non_infra_dispatch_mode_flags8old_without_ignore_compile_internals_dispatch_mode_flags)selfr;   s     r   __init__TorchDispatchMode.__init__s   sX    $mUXX-A-ABB$%STT-:MM/*49G$>Cg.G 	Er   c                    [        U S5      (       d  [        5       U l        [        U S5      (       d  [        5       U l        [        U S5      (       d  [        5       U l        g g )Nr=   r>   r?   )hasattrr   r=   r>   r?   r@   s    r   "_lazy_init_old_dispatch_mode_flags4TorchDispatchMode._lazy_init_old_dispatch_mode_flags   sY    t6778=D(t@AABG'D2L
 

  I
r   c                    [         er   )NotImplementedErrorr@   functypesargsr6   s        r   r,   $TorchDispatchMode.__torch_dispatch__   s    !!r   c                   U R                  5         U R                  R                  [        5        SqU R                  R                  [
        5        [
        =(       d    U R                  5       (       + qU R                  R                  [        5        [        =(       d    U R                  5       (       + q[        [        5        [        U 5        U $ )NT)rF   r=   appendr   r>   r   r"   r?   r   r#   r   
_push_moderE   s    r   	__enter__TorchDispatchMode.__enter__   s     	//1$$++,FG%)"..550	
 1L8J8J8L4L 	- 	EELL<	
 = 30022 	9 	8<	
 	4r   c                J   U R                   R                  SS 5      nUc  U R                   R                  SS 5      nU R                  R                  5       qU R
                  R                  5       qU R                  R                  5       q[        [        5        [        U5        g )Nr;   	_mode_key)r1   getr=   popr   r>   r   r?   r   r   	_pop_mode)r@   exc_typeexc_valexc_tbmb_dk_or_mode_keys        r   __exit__TorchDispatchMode.__exit__   s     MM--otD$ !% 1 1+t D%)%A%A%E%E%G" 22668 	-
 IIMMO 	9 	8<	
 	#$r   c                @    [         R                  " SSS9  U " U0 UD6nU$ )NzP`Mode.push()` is no longer necessary and can be replaced with just `with Mode()`   )
stacklevel)warningswarn)r5   rM   r6   instances       r   pushTorchDispatchMode.push   s*    ^	
 ''r   c                    g)NFr   r5   s    r   r"   TorchDispatchMode.is_infra_mode   s    r   c                    g)a  Skip Dynamo when the flag is set to True

This is temporary measure to rollout a feature
that skips PT2 compilation inside __torch_dispatch__
frames.

If this flag is off, we would expect following:

class YoloMode(TorchDispatchMode):
    @classmethod
    def _should_skip_dynamo(cls):
        return False
    def __torch_dispatch__(self, func, types, args=(), kwargs=None):
        return torch.ops.aten.mul.Tensor(args[0], args[1])

x = torch.ones(5)
with YoloMode():
    out = torch.compile(torch.add, backend=backend, fullgraph=True)(x, x)

# instead of recursively disabling, we are compiling into __torch_dispatch__
assert len(backend.graphs) == 1
Tr   rh   s    r   r0   %TorchDispatchMode._should_skip_dynamo   s    0 r   c                0    U R                  5       (       a  gg)a  Ignore operators that are compiled via torch.compile.

If ``True``, then this TorchDispatchMode ignores operators that
are optimized by :func:`torch.compile`. Mechanically, this involves
turning off the TorchDispatchMode throughout the whole compilation process,
and turning it back on for the runtime of the compiled artifact(s).
For example,

@torch.compile
def f(x):
    return x.sin().cos()

with LoggingMode():
    f(x)

The above example will not log anything if
``LoggingMode.ignore_compile_internals()`` is True.
torch.compile will fuse sin() and cos() into a single operation
and this TorchDispatchMode will not be passed sin and cos.

If ``False`` (default), :func:`torch.compile` will respect
the eager semantics of passing this TorchDispatchMode all
operators that would have run during eager execution.
The way this will usually happen is that :func:`torch.compile`
will just fallback to eager-mode PyTorch.
TF)r"   rh   s    r   r#   *TorchDispatchMode.ignore_compile_internals   s    8 r   )r=   r>   r?   r   r   Nreturnbool)__name__
__module____qualname____firstlineno____doc__supports_higher_order_operatorsr/   rA   rF   r,   rR   r]   r3   re   r"   r0   r#   __static_attributes____classcell__)r8   s   @r   r)   r)   F   s    D ',#X

">%*      2  r   r)   c                 @    [        5       n U S:  a  [        U S-
  5      $ g)z_
Return the top user mode on the stack (the next one that would be
executed) if there are any.
r      N)r   r   )r$   s    r   _get_current_dispatch_moder|     s%    
 *+I1}%i!m44r   c                   U [         R                  R                  R                  [         R                  R                  R                  4;  aX  [        S[         R                  R                  R                   S[         R                  R                  R                   SU  35      eSSKJn  U" U 5      n[         R                  R                  U 5      nUb  Ub  [        S5      eUc  U$ U$ )Nzkey must be either FUNCTIONAL (z)                 or PROXY (z1) _TorchDispatchModeKey,                     got r   )_get_dispatch_mode_pre_dispatchzEAt most one of pre_dispatch_mode and post_dispatch_mode may be active)	r   r    _TorchDispatchModeKey
FUNCTIONALPROXYr<   
torch._opsr~   _get_dispatch_mode)keyr~   pre_dispatch_modepost_dispatch_modes       r   _detect_infra_moder     s    
&&11&&,,  -ehh.L.L.W.W-X Y 8899??@ A
 	

 ;7<44S9$);)GS
 	
  !!r   c                   SSK JnJn  U" U 5      n[        R                  R                  U 5      nU(       a  U(       a  [        S5      eU(       a
  U" U 5      nU$ U(       a  [        R                  R                  U 5      $ g )Nr   )r~   unset_mode_pre_dispatchzECan't have active infra mode on both pre and post dispatch mode stack)r   r~   r   r   r    r   r<   _unset_dispatch_mode)r   r~   r   r   r   r&   s         r   _unset_infra_moder   1  sl    S7<44S9/S
 	
 &s+xx,,S11 r   c              #     #    U [         R                  R                  R                  [         R                  R                  R                  4;  a  [        S5      e[        U 5      n Uv   Ub  [        U5        g g ! Ub  [        U5        f f = f7f)Nz<key must be either FUNCTIONAL or PROXY _TorchDispatchModeKey)r   r    r   r   r   r<   r   rQ   )r   
mode_unsets     r   _disable_infra_moder   B  s     
&&11&&,,  J
 	
 #3'J#!z" ":!z" "s   A#B&A: *B:BBc                 j    [        5       n [        U 5       Vs/ s H  n[        U5      PM     sn$ s  snf )z
Returns the current stack of dispatch modes, with the most recent
(i.e., the one that will be processed first) at the end of the
list (standard stack convention).
)r   r!   r   )r$   is     r    _get_current_dispatch_mode_stackr   R  s1     *+I/4Y/?@/?!"1%/?@@@s   0c                z   [        U S5      (       a  U R                  OS nUb3  U[        R                  R                  R
                  :w  a  [        S5      eUc  [        U 5        g SSKJ	nJ
n  [        R                  R                  U5      nU" 5        H  nU H  nUR                  U5        M     M     U" U 5        g )Nr;   z:mode._dispatch_key must be None or DispatchKey.PreDispatchr   )_set_mode_pre_dispatchget_cached_ops)rD   r;   r   r    r   PreDispatchr<   r   r   r   r   _functionality_to_backend_keys_uncache_dispatch)r&   kr   r   ksopr   s          r   rQ   rQ   \  s    %dO<<$A}ehh22>>>H
 	
 	y%d+A 
	0	0	3BC  %   4 r   c                    U [         R                  R                  R                  :X  a  SSKJn  U" 5       $ U b)  [        U [         R                  R                  5      (       a  [        U 5      $ g )Nr   )_pop_mode_from_pre_dispatch)	r   r    r   r   r   r   r2   r   r   )r   r   s     r   rX   rX   q  sR    EHH  ,,,:*,,yJq%(("@"@AA(++ Br   c              #  b   #    [        U 5      n Uv   [        U5        g ! [        U5        f = f7fr   )rX   rQ   )r   olds     r   _pop_mode_temporarilyr   {  s&     
A,C	3
3s   / /,/c               #  j  #    SSK Jn Jn  SSKJn  SSKJn  SSKJn  U " 5       n[        U5       Vs/ s H	  oa" 5       PM     nnSnSn	Sn
U H;  n[        X5      (       a  Sn[        X5      (       a  Sn	[        X5      (       d  M9  Sn
M=     [        5       n[        U5       Vs/ s H  n[        5       PM     nnU Hk  n[        X5      (       a  U	(       a  [        S5      e[        X5      (       a  U(       a  [        S	5      e[        X5      (       d  MY  U
(       d  Mb  [        S
5      e    X}-   v   [        U5       H  n[        U5        M     [        U5       H  n[        U5        M     g s  snf s  snf ! [        U5       H  n[        U5        M     [        U5       H  n[        U5        M     f = f7f)Nr   )&_len_torch_dispatch_stack_pre_dispatchr   )FunctionalTensorMode)SchemaCheckMode)ProxyTorchDispatchModeFTzFCan't have FunctionalMode available both in PreDispatch and Python KeyzNCan't have ProxyTorchDispatchMode available both in PreDispatch and Python KeyzGCan't have SchemaCheckMode available both in PreDispatch and Python Key)r   r   r   #torch._subclasses.functional_tensorr   #torch._subclasses.schema_check_moder   "torch.fx.experimental.proxy_tensorr   r!   r2   r   rX   r<   reversedrQ   )r   r   r   r   r   mode_len_pre_dispatch_old_pre_dispatch_modeshas_proxy_mode_in_pre_dispatch#has_functional_mode_in_pre_dispatch%has_schema_check_mode_in_pre_dispatchr   mode_len	old_modesr   r&   s                   r   _disable_current_modesr     s     ICIBD/45J/K/K!#%/K   &+"*/',1)#a00-1*a..26/a))481 $ )*H&+Ho6ooI6s113 X  c227U `  c++0U0U Y  $$00Y'Dt (34Dt 5U" 7. Y'Dt (34Dt 5sH   /F3E*?F3F3!E/5AF3F3F3)E4 /AF34<F00F3c                      \ rS rSrSS jrSrg)BaseTorchDispatchModei  r   Nc                    Uc  0 nU" U0 UD6$ r   r   rJ   s        r   r,   (BaseTorchDispatchMode.__torch_dispatch__  s    >FT$V$$r   rn   )rr   rs   rt   ru   r,   rx   r   r   r   r   r     s    %r   r   c                  h   \ rS rSr% SS jr\          SS j5       rS\S'   \SSS jj5       r	\SS j5       r	\SSS	 jj5       r
\SS
 j5       r
SS jrSS jr\  SSS.         SS jjj5       r\    SSS.           SS jjj5       r\  SSS.         SS jjj5       rSrg)TensorWithFlatteni  c                    g r   r   rE   s    r   __tensor_flatten__$TensorWithFlatten.__tensor_flatten__  s    #r   c                    g r   r   )inner_tensorsflatten_spec
outer_sizeouter_strides       r   __tensor_unflatten__&TensorWithFlatten.__tensor_unflatten__  s     r   ztorch._C.SizeshapeNc                    g r   r   r@   dims     r   strideTensorWithFlatten.stride  s    ;>r   c                    g r   r   r   s     r   r   r     s    '*r   c                    g r   r   r   s     r   sizeTensorWithFlatten.size  s    9<r   c                    g r   r   r   s     r   r   r     s    %(r   c                    g r   r   rE   s    r   storage_offset TensorWithFlatten.storage_offset  s    Sr   c                    g r   r   rE   s    r   r   TensorWithFlatten.dim  s    #r   )memory_formatc                   g r   r   )r@   dtypenon_blockingcopyr   s        r   toTensorWithFlatten.to       r   c                   g r   r   )r@   devicer   r   r   r   s         r   r   r     s     r   c                   g r   r   )r@   otherr   r   r   s        r   r   r     r   r   r   )rp   ztuple[Sequence[str], object])
r   intr   r   r   r   r   r   rp   torch.Tensorr   )r   Nonerp   ztuple[int, ...])r   r   rp   r   )rp   r   )FF)
r   ztorch.types._dtyper   rq   r   rq   r   torch.memory_format | Nonerp   r   )NNFF)r   z)torch._prims_common.DeviceLikeType | Noner   ztorch.types._dtype | Noner   rq   r   rq   r   r   rp   r   )
r   r   r   rq   r   rq   r   r   rp   r   )rr   rs   rt   ru   r   staticmethodr   __annotations__r   r   r   r   r   r   rx   r   r   r   r   r     s   E*-;>NQ	  > >* *< <( (( #	 59!  	 2 
   =A+/" 599 ) 	
  2 
   #	 59  	 2 
 r   r   c                    [        U [        R                  5      =(       a    [        U 5      [        R                  LnU=(       a    [	        U S5      =(       a    [	        U S5      $ )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:
    * The subclass's __torch_dispatch__() implementation should desugar into pytorch
        dispatcher operations that can be traced into a graph.
    * The subclass should use return_and_correct_aliasing(). This is needed today to make
        sure that torch.compile does the right thing in a few cases around input mutation
        and output aliasing.

Expected magic method signatures:
    attrs, ctx = t.__tensor_flatten__()
        attrs: list of attribute name strings for inner tensors
        ctx: dict containing any other subclass-specific metadata needed for unflattening

    t = MySubClass.__tensor_unflatten__(inner_tensors, ctx, outer_size, outer_stride)
        inner_tensors: dict mapping attribute name -> tensor for each inner tensor
        ctx: dict with subclass metadata in the form that __tensor_flatten__() produces
        outer_size: expected (possibly symbolic) size that the returned subclass
            instance should have. Note that this arg is useful for certain subclasses
            that require the shape info to be constructed. In most cases, this arg can be
            safely ignored.
        outer_stride: expected (possibly symbolic) stride that the returned subclass
            instance should have. Note that this arg is useful for certain subclasses
            that require the stride info to be constructed. In most cases, this arg can be
            safely ignored.
r   r   )r2   r   TensortyperD   )tis_subclasss     r   is_traceable_wrapper_subclassr     sN    < Q-M$q'2MK 	/A+,	/A-.r   c                    [        U [        R                  5      =(       a8    U [        R                  L=(       a    [        U S5      =(       a    [        U S5      $ )z@Same as above, but takes a type argument instead of an instance.r   r   )
issubclassr   r   rD   )r   s    r   "is_traceable_wrapper_subclass_typer   )  sJ     	1ell# 	/U\\!	/A+,	/ A-.	r   c           	        Ub  UOU R                  5       nUb  UOU R                  5       nU R                  5       u  pE0 nU H  nU" U[        X5      5      Xg'   M     [	        U 5      R                  XeX#5      nUR                  U:w  a'  [        S[	        U 5       SU SUR                   35      eUR                  5       U:w  a+  [        S[	        U 5       SU SUR                  5        35      eU$ )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.
zExpected return value from z.__tensor_unflatten__() to have shape equal to z, but got: z/__tensor_unflatten__() to have stride equal to )r   r   r   getattrr   r   r   r<   )	r   callbackr   r   attrsctxtransformed_tensors_dictattrsubs	            r   transform_subclassr   3  s     *51668J#/#;<L%%'JE!)1$8H)I & 
q'
&
& zC yyJ)$q' 3(\SYYKA
 	
 zz||#)$q' 3+nK

~G
 	

 Jr   c                  ^  [        T [        R                  R                  5      (       d  [	        S[        U5       35      e[        U[        5      (       d  [	        S[        U5       35      e[        U[        [        45      (       d  [	        S[        U5       35      eSU 4S jjnUR                   H  u  pVU" X%   X6   5        M     g)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.
z func must be an OpOverload, got zargs must be a tuple, got z"outs must be a list or tuple, got c                P  > [        U 5      n[        U5      nX#La  [        U5      (       d  [        U5      (       aj  [        U[        5      (       a  UOU/nU HJ  n[        U 5      [        U5      Ld  M  [	        S[        T5       S[        U 5       S[        U5       S35      e   [        U[        5      (       a   U H  n[        R                  " XP5        M     g [        U[        R                  5      (       d  [	        S[        U5       35      e[        R                  " X5        g )NzCalled z with input of type z
and output of type z. But expected types to match.zexpected torch.Tensor, got )	r   r   r2   listr<   strr   _functionalize_unsafe_setr   )argretarg_typeret_typeret_listrrK   s         r   alias_non_inplace_storage<_correct_storage_aliasing.<locals>.alias_non_inplace_storagep  s     99#.x881(;;(d33s#H9DG+(!#d),@c L..23i[8VX  " c4  //7  c5<<00$'B49+%NOO++C5r   Nrp   r   )	r2   r   _ops
OpOverloadr<   r   tupler   read_only_alias_match_indexes)rK   schema_inforM   outsr  arg_idx
return_idxs   `      r   _correct_storage_aliasingr  ]  s     dEJJ1122?T
|LMMdE""9$t*FGGdT5M**A$t*NOO)6V  +HH!$-1AB  Ir   c                    U R                   nU(       a  U R                  (       d  g [        U5      S:w  a  [        S5      e[	        [        U5      5      $ )Nr{   z1Expected alias_set to contain exactly one element)	alias_setis_writelenr<   nextiter)xr  s     r   _get_write_aliasr    s@    IAJJ
9~PQQ Y  r   c                  4    \ rS rSr% S\S'   S\S'   S\S'   Srg	)
	AliasInfoi  zset[str]r  rq   r  
str | Nonenamer   Nrr   rs   rt   ru   r   rx   r   r   r   r  r    s    N
r   r  c                  H    \ rS rSr% S\S'   S\S'   S\S'   S\S'   S	\S
'   Srg)
SchemaInfoi  zlist[AliasInfo]rM   r	  rq   is_inplace_view_opzlist[str] | Noneouts_write_aliaseszlist[tuple[int, int]]r  r   Nr  r   r   r   r  r    s$    


 )( $98r   r  c           	        U R                   S:X  Ga  [        U R                  5      nUR                  S5      (       d  [	        S5      eUSS  nSS KnUR                  SSU5      nUR                  SS	U5      nUR                  S
SU5      n[        R                  R                  R                  U5      nUR                  R                   Vs/ s Hu  n[        UR                  c
  [        5       O[        UR                  R                   5      UR                  S L=(       a    UR                  R"                  UR$                  S9PMw     nnUR&                   Vs/ s Hu  n[        UR                  c
  [        5       O[        UR                  R                   5      UR                  S L=(       a    UR                  R"                  UR$                  S9PMw     nnGO,U R                  R                   Vs/ s Hu  n[        UR(                  c
  [        5       O[        UR(                  R*                  5      UR(                  S L=(       a    UR(                  R"                  UR$                  S9PMw     nnU R                  R&                   Vs/ s Hu  n[        UR(                  c
  [        5       O[        UR(                  R*                  5      UR(                  S L=(       a    UR(                  R"                  UR$                  S9PMw     nn/ n[-        U5       He  u  p[-        U5       HQ  u  pU	R                   UR                   -  =(       a    U	R"                  (       + nU(       d  M?  UR/                  X45        MS     Mg     U Vs/ s H  n[1        U5      PM     nn[3        S U 5       5      nUS:X  a  S nOGU[5        U5      :w  a!  [7        S[        U R                  5      -   5      e[9        [:        [           U5      n[=        UU[>        R@                  RB                  U RD                  ;   UUS9nU$ s  snf s  snf s  snf s  snf s  snf )Natenzaten::z6Expected torchgen schema string to start with 'aten::'   r   z=\[[0, ]+\]z=0z=\[[1, ]+\]z=1z=\[(-?[0-9]+), (-?[0-9]+)\]z=[\1,\2])r  r  r  c              3  (   #    U  H  oS Lv   M
     g 7fr   r   ).0r  s     r   	<genexpr>!get_alias_info.<locals>.<genexpr>  s     C+BaTM+Bs   zUnsupported schema: )rM   r	  r  r  r  )#	namespacer   _schema
startswithr<   rer   torchgenmodelFunctionSchemaparse	argumentsflat_allr  
annotationsetr  r  r  returns
alias_info
before_set	enumeraterP   r  sumr  RuntimeErrorr   r   r  r   Taginplace_viewtags)rK   torchgen_schema_strr(  torchgen_schemaaarg_schemasout_schemasr  r
  
schema_argr  
schema_outis_read_only_alias_matchr   outs_write_aliases_list	non_nonesr  r  s                     r   get_alias_inforD    s    ~~!$,,/"--h77 H 
 2!"5 !ff^T;NO ff^T;NO ff*K9L
 #..77==>QR %..77	
 8 \\1CEs1<<;Q;Q7RT1Kall6K6KVV 8 	 	
$ %,,	
 - \\1CEs1<<;Q;Q7RT1Kall6K6KVV - 	 	
( \\++	
 , \\1CEs1<<;R;R7ST1Kall6K6KVV , 	 	
$ \\))	
 * \\1CEs1<<;R;R7ST1Kall6K6KVV * 	 	
 %'!(5&/&<"J$$z';';;(* ))) % ('-44g5JK '=  6 &11%0[  1 C+BCCIA~/3	c12	21C4EEFF!$s)-DE !9911TYY>-&CK Q	
	
	
	
&1s"   A<P*A<P//A<P4A<P9=P>c                   SnU H  n[        U[        R                  5      (       d  M$  [        R                  R	                  [        R                  R                  UR                  R                  5      5      nUc"  [        SUR                  R                   35      e[        R                  R                  U R                  5       U5      n  U(       + $    U(       + $ )a  
Suppose that an operator has CompositeImplicitAutograd decomp registered.
Would autograd have used this decomposition?  It will only use it if there
isn't an explicit backend registration for the device as well.  This function
will tell if this would have occurred.

Why do we need to apply these decompositions later?  When inference mode is
on, the autograd key is bypassed entirely, so a lower level mode cannot rely
on the decomposition have been applied.  It's easy to accidentally never apply
the decomposition, resulting in an operator showing up in a graph that
is unexpected.

Why do we need to AVOID applying the decomposition when autograd wouldn't
have decomposed?  If autograd doesn't decompose, this means in eager mode
we would have run the fused kernel.  It must be possible to trace this
fused kernel directly into the graph for fidelity with eager (NB: a user
has the option of then further decomposing at proxy tensor mode via
decomposition table, but we must preserve it to proxy mode to have the
choice.)

Why does functionalization need to also perform the test here?  This is
because some CompositeImplicitAutograd decompositions are not functional.
If we are eventually going to decompose, we need to do this while we can
still turn functionalization back on, so those decompositions get functionalized.
So an early decomposition in functionalization may still be necessary.  Note that
if proxy tensor decomposition process could turn functionalization back on, this
wouldn't be necessary, and maybe that is a useful thing to do anyway because
the decomposition table is user specified and a user could violate the functional
decomp requirement with a bad decomp.  If this happened, then you could always
pass through functionalization.
Fz(failed to parse dispatch key for device )r2   r   r   r    _parse_dispatch_key_dispatch_key_for_devicer   r   r<   %_dispatch_has_kernel_for_dispatch_keyr  )rK   	flat_argshas_backend_registrationr<  backend_keys        r   autograd_would_have_decomposedrL  +  s    D  %a&&((6611!((--@K "$>qxx}}oN  (-xx'U'U		[($ ''') ( (''r   c                  ^  [        T 5      nU 4S jn[        T XA[        U[        5      (       d  U4OU5        UR                  (       Ga  [        U5       VVs/ s H"  u  pg[        UR                  U   5      c  M   UPM$     nnn[        U5      S:w  a  [        S5      eSSK
Jn	  [        US   U	5      (       d  [        R                  R                  R                  5          [        R                   R#                  5       n
[        R                   R%                  S5         T " U0 UD6  [        R                   R%                  U
5         SSS5        UR&                  nUc  U$ [        U5      S:X  a  U" US   XAU5      $ [)        U5      " U Vs/ s H  nU" XX5      PM     sn5      nU$ s  snnf ! [        R                   R%                  U
5        f = f! , (       d  f       N= fs  snf )a.  
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                  > [         R                  R                  R                  TX#S9u  pE[	        UR
                  5       VVs/ s H  u  pgXR                  ;   d  M  UPM     nnn[        U5      S:w  a  [        S5      eUS   n	UR
                  U	   n
U
R                  b  U
R                  U;   a  XZR                     $ XI   $ s  snnf )N)rM   r6   r{   z>Expected exactly one argument index for the given output aliasr   )
r   fxoperator_schemasnormalize_functionr4  rM   r  r  r<   r  )output_aliasr  rM   r6   new_args
new_kwargsr   r<  arg_indicesr%   arg_inforK   s              r   get_arg_from_alias7return_and_correct_aliasing.<locals>.get_arg_from_aliasy  s    $xx88KKt  L  

 $K$4$45
5$!9TA5 	 
 {q  P  !n##C(==$*)Dmm,,}
s   CCNr{   z5expected exactly one mutated arg for inplace_view opsr   )FunctionalTensorT)rD  r  r2   r  r  r4  r  rM   r  r<   r   rY  r   utils_mode_utilsno_dispatchr    _meta_in_tls_dispatch_include!_set_meta_in_tls_dispatch_includer  r   )rK   rM   r6   outr  rW  r   r  mutated_argsrY  meta_in_tlsschema_info_outs_write_aliaseswrite_aliasouts_to_returns   `             r   return_and_correct_aliasingre  e  s   $ !&K* kz#u/E/E#3 %%%
 "$
' 0 0 34 ' 	 
 |! G  	I,q/+;<<((446 $hhDDF::4@L$)&)HH>>{K 7 &1%C%C"%-

)*a/!*1-{&
 	

 #Y  >	
=  $G=	
N ]
2 HH>>{K 760	
s6   F';F' >GF-'GG"-!GG
G)T)r   rq   rp   rq   ro   )rp   zTorchDispatchMode | None)rp   zlist[TorchDispatchMode])r&   r)   rp   r   r   )r   z3DispatchKey | torch._C._TorchDispatchModeKey | None)r   zDispatchKey | None)r   objectrp   zTypeIs[TensorWithFlatten])r   r   rp   zTypeIs[type[TensorWithFlatten]])NNr  )rp   r  )rp   r  )rK   ztorch._ops.OpOverloadrI  zSequence[torch.Tensor | object]rp   rq   );
__future__r   
contextlib	functoolsrb   collectionsr   dataclassesr   typingr   r   r   r	   typing_extensionsr
   r   r)  torchgen.modeltorch._Cr   r   r   r   r   torch._C._dynamo.guardsr   collections.abcr   r   r   r   r   r   r'   r)   r|   r   r   r   r   rQ   rX   contextmanagerr   r   r   r   r   r   r   r  r  r  r  cacherD  rL  re  r   r   r   <module>rt     sr   "     ! : : $     T ( # ', $38 0<"C CL42"# A!*,   5 5p%- %; ;|#L'T?CD	!    9 9 9$ _ _D7(
7(,K7(	7(t`r   