§
    Ë²Åj=r  ã                   ó¢  — U d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZ d dlmZ d dlmZ d dlZd dlZd dlZd dlmZmZmZmZmZ dadad(d	e fd
„Z! G d„ d¦  «        Z"d„ Z#d„ Z$d„ Z%d„ Z&d„ Z'de"fd„Z(d)dee
eej)        j*        f                  fd„Z+e j,        d)dee         fd„¦   «         Z-e j,        d„ ¦   «         Z. G d„ de"¦  «        Z/ G d„ de¦  «        Z0de1d	ee0         fd„Z2ded	eee0                  fd„Z3d*d„Z4d „ Z5e G d!„ d"¦  «        ¦   «         Z6e G d#„ d$¦  «        ¦   «         Z7i Z8eee7f         e9d%<   d	e7fd&„Z:d'„ Z;dS )+é    N)Ú	dataclass)ÚAnyÚDictÚListÚOptionalÚSetÚUnionÚProtocolÚTupleÚSequenceÚoverloadÚDequeÚType)ÚTypeIs)Údeque)Ú_get_dispatch_stack_atÚ_len_torch_dispatch_stackÚ_pop_torch_dispatch_stackÚ_push_on_torch_dispatch_stackÚDispatchKeyFTÚreturnc                 ó"   — | rt           nt          S ©N)Ú_is_in_torch_dispatch_modeÚ$_is_in_non_infra_torch_dispatch_mode)Úinclude_infra_modess    úMC:\Projects\Historical\venv\Lib\site-packages\torch/utils/_python_dispatch.pyÚis_in_torch_dispatch_moder      s   € Ø)<ÐfÕ%Ð%ÕBfÐfó    c                   ó`   — e Zd ZdZdd„Zd„ Zdd„Zd„ Zd„ Ze	d	„ ¦   «         Z
e	d
„ ¦   «         ZdS )Ú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          |t          j        j        ¦  «        sJ ‚|| j        d<   t          ¦   «         | _        t          ¦   «         | _        d S )NÚ_dispatch_key)Ú
isinstanceÚtorchÚ_Cr   Ú__dict__r   Úold_dispatch_mode_flagsÚ!old_non_infra_dispatch_mode_flags)Úselfr#   s     r   Ú__init__zTorchDispatchMode.__init__B   sM   € ØÐ$Ý˜m­U¬XÔ-AÑBÔBÐBÐBÐBØ-:ˆDŒM˜/Ñ*å49±G´GˆÔ$Ý>C¹g¼gˆÔ.Ð.Ð.r   c                 ó–   — t          | d¦  «        st          ¦   «         | _        t          | d¦  «        st          ¦   «         | _        d S d S )Nr(   r)   )Úhasattrr   r(   r)   ©r*   s    r   Ú"_lazy_init_old_dispatch_mode_flagsz4TorchDispatchMode._lazy_init_old_dispatch_mode_flagsJ   sT   € Ý�tÐ6Ñ7Ô7ð 	@Ý8=¹¼ˆDÔ(å�tÐ@ÑAÔAð 	JÝBGÁ'Ä'ˆDÔ2Ð2Ð2ð	Jð 	Jr   © c                 ó   — t           ‚r   )ÚNotImplementedError©r*   ÚfuncÚtypesÚargsÚkwargss        r   Ú__torch_dispatch__z$TorchDispatchMode.__torch_dispatch__R   s   € Ý!Ð!r   c                 ó  — |                       ¦   «          | j                             t          ¦  «         da| j                             t
          ¦  «         t
          p|                      ¦   «          at          | ¦  «         | S )NT)r/   r(   Úappendr   r)   r   Úis_infra_modeÚ
_push_moder.   s    r   Ú	__enter__zTorchDispatchMode.__enter__U   sz   € ð 	×/Ò/Ñ1Ô1Ð1ØÔ$×+Ò+Õ,FÑGÔGÐGØ%)Ð"ØÔ.×5Ò5Õ6ZÑ[Ô[Ð[Ý/SÐ/oÐ[_×[mÒ[mÑ[oÔ[oÐWoÐ,Ý�4ÑÔÐØˆr   c                 óø   — | j                              dd ¦  «        }|€| j                              dd ¦  «        }| j                             ¦   «         a| j                             ¦   «         at          |¦  «         d S )Nr#   Ú	_mode_key)r'   Úgetr(   Úpopr   r)   r   Ú	_pop_mode)r*   Úexc_typeÚexc_valÚexc_tbÚmb_dk_or_mode_keys        r   Ú__exit__zTorchDispatchMode.__exit__d   sy   € Ø œM×-Ò-¨o¸tÑDÔDÐØÐ$ð !%¤× 1Ò 1°+¸tÑ DÔ DÐà%)Ô%A×%EÒ%EÑ%GÔ%GÐ"à/3Ô/U×/YÒ/YÑ/[Ô/[Ð,ÝÐ#Ñ$Ô$Ð$Ð$Ð$r   c                 ó>   — t          j        d¦  «          | |i |¤Ž}|S )NzP`Mode.push()` is no longer necessary and can be replaced with just `with Mode()`)ÚwarningsÚwarn)Úclsr6   r7   Úinstances       r   ÚpushzTorchDispatchMode.pushp   s4   € åŒØ^ñ	
ô 	
ð 	
ð �3˜Ð' Ð'Ð'ˆØˆr   c                 ó   — dS )NFr0   )rK   s    r   r;   zTorchDispatchMode.is_infra_modex   s   € àˆur   r   ©r0   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r+   r/   r8   r=   rG   ÚclassmethodrM   r;   r0   r   r   r!   r!   #   s°   € € € € € ðð ð<Fð Fð Fð FðJð Jð Jð"ð "ð "ð "ðð ð ð
%ð 
%ð 
%ð ðð ñ „[ðð ðð ñ „[ðð ð r   r!   c                  óR   — t          ¦   «         } | dk    rt          | dz
  ¦  «        S d S ©Nr   é   )r   r   ©Ú	stack_lens    r   Ú_get_current_dispatch_moderZ   ~   s-   € Ý)Ñ+Ô+€Ià�1‚}€}Ý% i°!¡mÑ4Ô4Ð4Øˆ4r   c                 óÚ   — | t           j        j        j        t           j        j        j        fv sJ ‚ddlm}  || ¦  «        }t           j                             | ¦  «        }|�|�J ‚|€|S |S )Nr   )Ú_get_dispatch_mode_pre_dispatch)r%   r&   Ú_TorchDispatchModeKeyÚ
FUNCTIONALÚPROXYÚ
torch._opsr\   Ú_get_dispatch_mode)Úkeyr\   Úpre_dispatch_modeÚpost_dispatch_modes       r   Ú_detect_infra_modere   †   s—   € Ø•5”8Ô1Ô<½e¼hÔ>\Ô>bÐcÐcÐcÐcÐcØ:Ð:Ð:Ð:Ð:Ð:à7Ð7Øñô Ðõ œ×4Ò4Øñô Ðð Ð%ØÐ"Ð"ðð Ð Ø!Ð!àÐr   c                 óð   — ddl m}m}  || ¦  «        }t          j                             | ¦  «        }|r|rt          d¦  «        ‚|r || ¦  «        }|S |rt          j                             | ¦  «        S d S )Nr   )r\   Úunset_mode_pre_dispatchzECan't have active infra mode on both pre and post dispatch mode stack)r`   r\   rg   r%   r&   ra   ÚAssertionErrorÚ_unset_dispatch_mode)rb   r\   rg   rc   rd   Úmodes         r   Ú_unset_infra_moderk   ›   s­   € ØSÐSÐSÐSÐSÐSÐSÐSà7Ð7¸Ñ<Ô<ÐÝœ×4Ò4°SÑ9Ô9ÐØð 
Ð/ð 
ÝØSñ
ô 
ð 	
ð ð Ø&Ð& sÑ+Ô+ˆØˆØð 2ÝŒx×,Ò,¨SÑ1Ô1Ð1ð2ð 2r   c              #   óæ   K  — | t           j        j        j        t           j        j        j        fv sJ ‚t          | ¦  «        }	 |V — |�t          |¦  «         d S d S # |�t          |¦  «         w w xY wr   )r%   r&   r]   r^   r_   rk   r<   )rb   Ú
mode_unsets     r   Ú_disable_infra_modern   ¬   s™   è è € ØÝŒÔ&Ô1ÝŒÔ&Ô,ðð ð ð ð õ # 3Ñ'Ô'€Jð#ØÐÐÐàÐ!Ý�zÑ"Ô"Ð"Ð"Ð"ð "Ð!øˆ:Ð!Ý�zÑ"Ô"Ð"Ð"ð "øøøs   ÁA ÁA0c                  óP   — t          ¦   «         } d„ t          | ¦  «        D ¦   «         S )Nc                 ó,   — g | ]}t          |¦  «        ‘ŒS r0   )r   )Ú.0Úis     r   ú
<listcomp>z4_get_current_dispatch_mode_stack.<locals>.<listcomp>»   s!   € Ð@Ð@Ð@¨!Õ" 1Ñ%Ô%Ð@Ð@Ð@r   )r   ÚrangerX   s    r   Ú _get_current_dispatch_mode_stackru   ¹   s)   € Ý)Ñ+Ô+€IØ@Ð@­u°YÑ/?Ô/?Ð@Ñ@Ô@Ð@r   rj   c                 óL  — t          | d¦  «        r| j        nd }|�|t          j        j        j        k    sJ ‚|€t          | ¦  «         d S ddlm}m	} t          j         
                    |¦  «        } |¦   «         D ]}|D ]}|                     |¦  «         ŒŒ || ¦  «         d S )Nr#   r   )Ú_set_mode_pre_dispatchÚget_cached_ops)r-   r#   r%   r&   r   ÚPreDispatchr   r`   rw   rx   Ú_functionality_to_backend_keysÚ_uncache_dispatch)rj   Úkrw   rx   ÚksÚoprb   s          r   r<   r<   ¾   sÛ   € Ý% d¨OÑ<Ô<ÐFˆÔÐÀ$€AØˆ9˜�UœXÔ1Ô=Ò=Ð=Ð=Ð=Ø€yÝ% dÑ+Ô+Ð+ØˆàAÐAÐAÐAÐAÐAÐAÐAõ 
Œ×	0Ò	0°Ñ	3Ô	3€BØˆnÑÔð &ð &ˆØð 	&ð 	&ˆCØ× Ò  Ñ%Ô%Ð%Ð%ð	&àÐ˜4Ñ Ô Ð Ð Ð r   r|   c                 óº   — | t           j        j        j        k    rddlm}  |¦   «         S | �t          | t           j        j        ¦  «        rt          | ¦  «        S d S )Nr   )Ú_pop_mode_from_pre_dispatch)	r%   r&   r   ry   r`   r€   r$   r]   r   )r|   r€   s     r   rB   rB   Ð   sc   € Ø�EŒHÔ Ô,Ò,Ð,Ø:Ð:Ð:Ð:Ð:Ð:à*Ð*Ñ,Ô,Ð,à€y•J˜q¥%¤(Ô"@ÑAÔA€yÝ(¨Ñ+Ô+Ð+ð €yr   c              #   óx   K  — t          | ¦  «        }	 |V — t          |¦  «         d S # t          |¦  «         w xY wr   )rB   r<   )r|   Úolds     r   Ú_pop_mode_temporarilyrƒ   Ú   s>   è è € å
�A‰,Œ,€CðØˆ	ˆ	ˆ	å�3‰Œˆˆˆø�
�3‰Œˆˆøøøs   “( ¨9c               #   óL  ‡K  — ddl m} mŠ ddlm} ddlm} ddlm}  | ¦   «         }ˆfd„t          |¦  «        D ¦   «         }d}d}d}|D ]8}	t          |	|¦  «        rd}t          |	|¦  «        rd}t          |	|¦  «        rd}Œ9t          ¦   «         }
d	„ t          |
¦  «        D ¦   «         }|D ]e}t          ||¦  «        r|rt          d
¦  «        ‚t          ||¦  «        r|rt          d¦  «        ‚t          ||¦  «        r|rt          d¦  «        ‚Œf	 ||z   V — t          |¦  «        D ]}t          |¦  «         Œt          |¦  «        D ]}t          |¦  «         Œd S # t          |¦  «        D ]}t          |¦  «         Œt          |¦  «        D ]}t          |¦  «         Œw xY w)Nr   )Ú&_len_torch_dispatch_stack_pre_dispatchr€   )ÚFunctionalTensorMode)ÚProxyTorchDispatchMode)ÚSchemaCheckModec                 ó$   •— g | ]} ‰¦   «         ‘ŒS r0   r0   )rq   Ú_r€   s     €r   rs   z*_disable_current_modes.<locals>.<listcomp>î   s0   ø€ ð ð ð Ø*+Ð#Ð#Ñ%Ô%ðð ð r   FTc                 ó*   — g | ]}t          ¦   «         ‘ŒS r0   )rB   )rq   rŠ   s     r   rs   z*_disable_current_modes.<locals>.<listcomp>ÿ   s   € Ð6Ð6Ð6 •‘”Ð6Ð6Ð6r   zFCan'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.fx.experimental.proxy_tensorr‡   Ú#torch._subclasses.schema_check_moderˆ   rt   r$   r   rh   Úreversedr<   )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_dispatchrr   Úmode_lenÚ	old_modesr‚   rj   r€   s                 @r   Ú_disable_current_modesr—   ã   s¯  øè è € ðð ð ð ð ð ð ð ð IÐHÐHÐHÐHÐHØIÐIÐIÐIÐIÐIØCÐCÐCÐCÐCÐCàBÐBÑDÔDÐðð ð ð Ý/4Ð5JÑ/KÔ/Kðñ ô Ðð &+Ð"Ø*/Ð'Ø,1Ð)à#ð 9ð 9ˆÝ�aÐ/Ñ0Ô0ð 	2Ø-1Ð*Ý�aÐ-Ñ.Ô.ð 	7Ø26Ð/Ý�a˜Ñ)Ô)ð 	9Ø48Ð1øå(Ñ*Ô*€HØ6Ð6¥e¨H¡o¤oÐ6Ñ6Ô6€Iàð ð ˆå�sÐ0Ñ1Ô1ð	à3ð	õ !ØXñô ð õ �cÐ1Ñ2Ô2ð 	Ð7Uð 	Ý Ø`ñô ð õ �s˜OÑ,Ô,ð	à5ð	õ !ØYñô ð øð
Ø$ yÑ0Ð0Ð0Ð0å˜YÑ'Ô'ð 	ð 	ˆDÝ�tÑÔÐÐÝÐ3Ñ4Ô4ð 	ð 	ˆDÝ�tÑÔÐÐð	ð 	øõ ˜YÑ'Ô'ð 	ð 	ˆDÝ�tÑÔÐÐÝÐ3Ñ4Ô4ð 	ð 	ˆDÝ�tÑÔÐÐð	øøøs   ÄE ÅAF#c                   ó   — e Zd Zdd„ZdS )ÚBaseTorchDispatchModer0   Nc                 ó   — |€i } ||i |¤ŽS r   r0   r3   s        r   r8   z(BaseTorchDispatchMode.__torch_dispatch__   s!   € Øˆ>ØˆFØˆt�TÐ$˜VÐ$Ð$Ð$r   rO   )rP   rQ   rR   r8   r0   r   r   r™   r™     s(   € € € € € ð%ð %ð %ð %ð %ð %r   r™   c                   ó¢  — e Zd ZU deee         ef         fd„Zede	de	de	de	de
j        f
d„¦   «         Ze
j        j        ed<   edd
d	dee	df         fd„¦   «         Zed
e	de	fd„¦   «         Zedd
d	dee	df         fd„¦   «         Zed
e	de	fd„¦   «         Zde	fd„Zde	fd„Ze	 	 dd	dœde
j        j        dededee
j                 de
j        f
d„¦   «         Ze	 	 	 	 d d	dœded         dee
j        j                 dededee
j                 de
j        fd„¦   «         Ze	 	 dd	dœde
j        dededee
j                 de
j        f
d„¦   «         Zd	S )!ÚTensorWithFlattenr   c                 ó   — d S r   r0   r.   s    r   Ú__tensor_flatten__z$TensorWithFlatten.__tensor_flatten__(  ó   € Øˆr   Úinner_tensorsÚflatten_specÚ
outer_sizeÚouter_stridec                 ó   — d S r   r0   )r    r¡   r¢   r£   s       r   Ú__tensor_unflatten__z&TensorWithFlatten.__tensor_unflatten__+  ó   € àˆr   ÚshapeNÚdim.c                 ó   — d S r   r0   ©r*   r¨   s     r   ÚstridezTensorWithFlatten.stride5  r¦   r   c                 ó   — d S r   r0   rª   s     r   r«   zTensorWithFlatten.stride9  r¦   r   c                 ó   — d S r   r0   rª   s     r   ÚsizezTensorWithFlatten.size=  r¦   r   c                 ó   — d S r   r0   rª   s     r   r®   zTensorWithFlatten.sizeA  r¦   r   c                 ó   — d S r   r0   r.   s    r   Ústorage_offsetz TensorWithFlatten.storage_offsetE  rŸ   r   c                 ó   — d S r   r0   r.   s    r   r¨   zTensorWithFlatten.dimH  rŸ   r   F)Úmemory_formatÚdtypeÚnon_blockingÚcopyr³   c                ó   — d S r   r0   )r*   r´   rµ   r¶   r³   s        r   ÚtozTensorWithFlatten.toK  ó	   € ð 	ˆr   Údevicez"torch._prims_common.DeviceLikeTypec                ó   — d S r   r0   )r*   rº   r´   rµ   r¶   r³   s         r   r¸   zTensorWithFlatten.toV  s	   € ð 	ˆr   Úotherc                ó   — d S r   r0   )r*   r¼   rµ   r¶   r³   s        r   r¸   zTensorWithFlatten.tob  r¹   r   r   )FF)NNFF)rP   rQ   rR   r   r   ÚstrÚobjectrž   ÚstaticmethodÚintr%   ÚTensorr¥   r&   ÚSizeÚ__annotations__r   r«   r®   r±   r¨   r5   Ú_dtypeÚboolr   r³   r¸   r0   r   r   rœ   rœ   '  s.  € € € € € € ð E¨(°3¬-¸Ð*?Ô$@ð ð ð ð ð ð¨Cð ¸sð ÐPSð Ðcfð ÐkpÔkwð ð ð ñ „\ðð Œ8Œ=ÐÐÑàðð ˜$ð ¨%°°S°¬/ð ð ð ñ „Xðð ð˜#ð  #ð ð ð ñ „Xðð ðð ˜ð ¨¨c°3¨h¬ð ð ð ñ „Xðð ð˜ð  ð ð ð ñ „Xðð ð ð ð ð ð�Sð ð ð ð ð ð "'Øð	ð <@ðð ð à”;Ô%ðð ðð ð	ð $ EÔ$7Ô8ðð 
Œðð ð ñ „Xðð ð FJØ26Ø!&Øð	ð <@ð	ð 	ð 	àÐAÔBð	ð ˜EœKÔ.Ô/ð	ð ð		ð
 ð	ð $ EÔ$7Ô8ð	ð 
Œð	ð 	ð 	ñ „Xð	ð ð "'Øð	ð <@ðð ð à”<ðð ðð ð	ð $ EÔ$7Ô8ðð 
Œðð ð ñ „Xðð ð r   rœ   Útc                 ó´   — t          | t          j        ¦  «        ot          | ¦  «        t          j        k    }|ot	          | d¦  «        ot	          | d¦  «        S )aW  
    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¥   )r$   r%   rÂ   Útyper-   )rÇ   Úis_subclasss     r   Úis_traceable_wrapper_subclassrË   p  sU   € õ< ˜Q¥¤Ñ-Ô-ÐIµ$°q±'´'½U¼\Ò2I€Kàð 	/Ý�AÐ+Ñ,Ô,ð	/å�AÐ-Ñ.Ô.ðr   c                 ó–   — t          | t          j        ¦  «        o/| t          j        k    ot          | d¦  «        ot          | d¦  «        S )z@Same as above, but takes a type argument instead of an instance.rž   r¥   )Ú
issubclassr%   rÂ   r-   )rÇ   s    r   Ú"is_traceable_wrapper_subclass_typerÎ   •  sN   € å�q�%œ,Ñ'Ô'ð X¨Aµ´Ò,=ð XÝ˜Ð/Ñ0Ô0ðXÝ5<¸QÐ@VÑ5WÔ5WðYr   c                 ó  — |�|n|                       ¦   «         }|�|n|                      ¦   «         }|                      ¦   «         \  }}i }|D ]} ||t          | |¦  «        ¦  «        ||<   Œ t	          | ¦  «                             ||||¦  «        }|j        |k    s%J dt	          | ¦  «        › d|› d|j        › �¦   «         ‚|                     ¦   «         |k    s2J dt	          | ¦  «        › d|› d|                     ¦   «         › �¦   «         ‚|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.
    Nz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Ç   Úcallbackr¢   r£   ÚattrsÚctxÚtransformed_tensors_dictÚattrÚsubs	            r   Útransform_subclassr×   ›  s^  € ð  *Ð5��¸1¿6º6¹8¼8€JØ#/Ð#;�<�<ÀÇÂÁÄ€Là×%Ò%Ñ'Ô'�J€Eˆ3Ø!ÐØð Jð JˆØ)1¨°$½ÀÀ4Ñ8HÔ8HÑ)IÔ)IÐ  Ñ&Ð&Ý
ˆq‰'Œ'×
&Ò
&Ø  # z°<ñô €Cð Œ9˜
Ò"Ð"Ð"ð	=¥d¨1¡g¤gð 	=ð 	=Ø$ð	=ð 	=Ø14´ð	=ð 	=ñ #Ô"Ð"ð �:Š:‰<Œ<˜<Ò'Ð'Ð'ð	C¥d¨1¡g¤gð 	Cð 	CØ'ð	Cð 	CØ47·J²J±L´Lð	Cð 	Cñ (Ô'Ð'ð
 €Jr   c                 óä  ‡ — t          ‰ t          j        j        ¦  «        sJ ‚t          |t          ¦  «        sJ ‚t          |t
          t          f¦  «        sJ ‚ˆ fd„}d„ }t          ‰ j        j        ¦  «        }t          ‰ j        j	        ¦  «        }t          |¦  «        D ]N}t          |¦  «        D ]<}	 ||j        |         |j        |	         ¦  «        r |||         ||	         ¦  «         Œ=ŒOdS )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           
      ó,  •— t          | ¦  «        st          |¦  «        rzt          |t          ¦  «        r|n|g}|D ]]}t          | ¦  «        t          |¦  «        k    s;J dt	          ‰¦  «        › dt          | ¦  «        › dt          |¦  «        › d�¦   «         ‚Œ^t          |t          ¦  «        r|D ]}t          j        || ¦  «         Œd S t          |t
          j        ¦  «        sJ dt          |¦  «        › �¦   «         ‚t          j        || ¦  «         d S )NzCalled z with input of type z
and output of type z. But expected types to match.ztype: )rË   r$   ÚlistrÉ   r¾   r%   Ú_functionalize_unsafe_setrÂ   )ÚargÚretÚret_listÚrr4   s       €r   Úalias_non_inplace_storagez<_correct_storage_aliasing.<locals>.alias_non_inplace_storageÓ  sc  ø€ õ )¨Ñ-Ô-ð 	@Õ1NÈsÑ1SÔ1Sð 	@Ý(¨­dÑ3Ô3Ð>�s�s¸#¸ˆHØð @ð @�Ý˜C‘y”y¥DØñ%ô %ò ð ð ð@�s 4™yœyð @ð @½dÀ3¹i¼ið @ð @Ý˜‘I”Ið@ð @ð @ñô ð ð õ �c�4Ñ Ô ð 	6Øð 8ð 8�ÝÔ/°°3Ñ7Ô7Ð7Ð7ð8ð 8õ ˜c¥5¤<Ñ0Ô0ÐFÐFÐ2F½4À¹9¼9Ð2FÐ2FÑFÔFÐ0ÝÔ+¨C°Ñ5Ô5Ð5Ð5Ð5r   c                 óV   — | j         |j         z  }t          |¦  «        dk    o| 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ó  s-   € Øœ¨¬Ñ6ˆÝ�>Ñ"Ô" QÒ&Ð;¨s¬|Ð+;Ð;r   N)r$   r%   Ú_opsÚ
OpOverloadÚtuplerÚ   rã   Ú_schemaÚ	argumentsÚreturnsrt   r6   Úouts)
r4   Úschema_infor6   rí   rà   ræ   Únum_argsÚnum_returnsÚarg_idxÚ
return_idxs
   `         r   Ú_correct_storage_aliasingró   Ã  s+  ø€ õ �d�EœJÔ1Ñ2Ô2Ð2Ð2Ð2Ý�d�EÑ"Ô"Ð"Ð"Ð"Ý�d�T¥5˜MÑ*Ô*Ð*Ð*Ð*ð6ð 6ð 6ð 6ð 6ð@<ð <ð <õ �4”<Ô)Ñ*Ô*€HÝ�d”lÔ*Ñ+Ô+€KÝ˜‘?”?ð Kð KˆÝ Ñ,Ô,ð 	Kð 	KˆJØ'Ð'ØÔ  Ô)¨;Ô+;¸JÔ+Gñô ð Kð *Ð)¨$¨w¬-¸¸jÔ9IÑJÔJÐJøð		KðKð Kr   c                   óF   — e Zd ZU ee         ed<   eed<   ee         ed<   dS )Ú	AliasInforâ   rä   ÚnameN)rP   rQ   rR   r   r¾   rÄ   rÆ   r   r0   r   r   rõ   rõ     s;   € € € € € € à�3ŒxÐÐÑØ€N€N�NØ
�3Œ-ÐÐÑÐÐr   rõ   c                   ó<   — e Zd ZU ee         ed<   ee         ed<   dS )Ú
SchemaInfor6   rí   N)rP   rQ   rR   r   rõ   rÄ   r0   r   r   rø   rø     s2   € € € € € € à
ˆyŒ/ÐÐÑØ
ˆyŒ/ÐÐÑÐÐr   rø   Úparsed_schema_mapc                 ój  — | t           v rt           |          S | j        dk    rÊt          | j        ¦  «        }|                     d¦  «        sJ ‚|dd …         }dd l}|                     dd|¦  «        }|                     dd|¦  «        }|                     d	d
|¦  «        }t          j        j	         
                    |¦  «        }d„ |j        j        D ¦   «         }d„ |j        D ¦   «         }n,d„ | j        j        D ¦   «         }d„ | j        j        D ¦   «         }t          ||¬¦  «        }|t           | <   |S )NÚatenzaten::é   r   z=\[[0, ]+\]z=0z=\[[1, ]+\]z=1z=\[(-?[0-9]+), (-?[0-9]+)\]z=[\1,\2]c                 óº   — g | ]X}t          |j        €t          ¦   «         nt          |j        j        ¦  «        |j        d uo|j        j        |j        ¬¦  «        ‘ŒYS ©N)râ   rä   rö   ©rõ   Ú
annotationÚsetrâ   rä   rö   ©rq   Úas     r   rs   z"get_alias_info.<locals>.<listcomp>+  óq   € ð 	
ð 	
ð 	
ð õ àœ\Ð1•C‘E”E�Eµs¸1¼<Ô;QÑ7RÔ7Ràœ¨TÐ1ÐK°a´lÔ6KØ”Vðñ ô ð	
ð 	
ð 	
r   c                 óº   — g | ]X}t          |j        €t          ¦   «         nt          |j        j        ¦  «        |j        d uo|j        j        |j        ¬¦  «        ‘ŒYS rþ   rÿ   r  s     r   rs   z"get_alias_info.<locals>.<listcomp>5  r  r   c                 óº   — g | ]X}t          |j        €t          ¦   «         nt          |j        j        ¦  «        |j        d uo|j        j        |j        ¬¦  «        ‘ŒYS rþ   ©rõ   Ú
alias_infor  Ú
before_seträ   rö   r  s     r   rs   z"get_alias_info.<locals>.<listcomp>A  óq   € ð 	
ð 	
ð 	
ð õ àœ\Ð1•C‘E”E�Eµs¸1¼<Ô;RÑ7SÔ7Sàœ¨TÐ1ÐK°a´lÔ6KØ”Vðñ ô ð	
ð 	
ð 	
r   c                 óº   — g | ]X}t          |j        €t          ¦   «         nt          |j        j        ¦  «        |j        d uo|j        j        |j        ¬¦  «        ‘ŒYS rþ   r  r  s     r   rs   z"get_alias_info.<locals>.<listcomp>K  r
  r   )r6   rí   )rù   Ú	namespacer¾   rê   Ú
startswithÚrerÖ   ÚtorchgenÚmodelÚFunctionSchemaÚparserë   Úflat_allrì   rø   )r4   Útorchgen_schema_strr  Útorchgen_schemaÚarg_schemasÚout_schemasrî   s          r   Úget_alias_infor    s‡  € ØÕ Ð Ð Ý  Ô&Ð&ð „~˜ÒÐÝ! $¤,Ñ/Ô/ÐØ"×-Ò-¨hÑ7Ô7Ð7Ð7Ð7ð 2°!°"°"Ô5ÐØˆ	ˆ	ˆ	ð !Ÿfšf ^°TÐ;NÑOÔOÐØ Ÿfšf ^°TÐ;NÑOÔOÐà ŸfšfÐ%CÀ[ÐReÑfÔfÐÝ"œ.Ô7×=Ò=Ð>QÑRÔRˆð	
ð 	
ð %Ô.Ô7ð	
ñ 	
ô 	
ˆð	
ð 	
ð %Ô,ð	
ñ 	
ô 	
ˆˆð	
ð 	
ð ”\Ô+ð	
ñ 	
ô 	
ˆð	
ð 	
ð ”\Ô)ð	
ñ 	
ô 	
ˆõ  +°KÐ@Ñ@Ô@€KØ)Õ�dÑØÐr   c           	      óÆ  ‡ ‡‡‡‡	‡
— t          ‰ ¦  «        Š
d„ Š	ˆ fd„Št          ‰ ‰
‰t          |t          ¦  «        s|fn|¦  «         t          j        j        ‰ j        v �rˆ	ˆ
fd„t          ‰¦  «        D ¦   «         }t          |¦  «        dk    sJ ‚ddl
m} t          |d         |¦  «        sÆt          j        j                             ¦   «         5  t          j                             ¦   «         }t          j                             d¦  «         	  ‰ ‰i ‰¤Ž t          j                             |¦  «         n$# t          j                             |¦  «         w xY w	 ddd¦  «         n# 1 swxY w Y   t%          ˆ	fd	„‰
j        D ¦   «         ¦  «        s|S t)          ˆ	fd
„‰
j        D ¦   «         ¦  «        s$t+          dt-          ‰ j        ¦  «        z   ¦  «        ‚t          ‰ j        j        ¦  «        dk    r" ‰ ‰	‰
j        d         ¦  «        ‰
‰‰¦  «        S  t3          |¦  «        ˆˆˆ	ˆˆ
fd„t5          t          ‰
j        ¦  «        |¦  «        D ¦   «         ¦  «        }|S )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d S t          | j        ¦  «        }t          |¦  «        dk    sJ ‚| j        r|d         S d S rV   )rã   râ   rÚ   rä   )Úxrâ   s     r   Úget_write_aliasz4return_and_correct_aliasing.<locals>.get_write_aliasn  s[   € ÝˆqŒ{ÑÔ˜qÒ Ð Ø�4Ý˜œÑ%Ô%ˆ	å�9‰~Œ~ Ò"Ð"Ð"Ð"ØŒ:ð 	 Ø˜Q”<ÐØˆtr   c                 ó8  •‡ — t           j        j                             ‰	||¬¦  «        \  }}ˆ fd„t	          |j        ¦  «        D ¦   «         }t          |¦  «        dk    sJ ‚|d         }|j        |         }|j        �|j        |v r||j                 S ||         S )N)r6   r7   c                 ó,   •— g | ]\  }}‰|j         v ¯|‘ŒS r0   )râ   )rq   rr   r  Úoutput_aliass      €r   rs   zKreturn_and_correct_aliasing.<locals>.get_arg_from_alias.<locals>.<listcomp>}  s0   ø€ ð 
ð 
ð 
Ù�!�Q¸ÈÌÐ9TÐ9TˆAÐ9TÐ9TÐ9Tr   rW   r   )r%   ÚfxÚoperator_schemasÚnormalize_functionÚ	enumerater6   rã   rö   )
r  rî   r6   r7   Únew_argsÚ
new_kwargsÚarg_indicesÚidxÚarg_infor4   s
   `        €r   Úget_arg_from_aliasz7return_and_correct_aliasing.<locals>.get_arg_from_aliasx  s¿   øø€ Ý$œxÔ8×KÒKØ�t Fð  Lñ  
ô  
Ñˆ�*ð
ð 
ð 
ð 
Ý# KÔ$4Ñ5Ô5ð
ñ 
ô 
ˆõ �;ÑÔ 1Ò$Ð$Ð$Ð$Ø˜!ŒnˆØÔ# CÔ(ˆØŒ=Ð$¨¬¸*Ð)DÐ)DØ˜hœmÔ,Ð,Ø˜Œ}Ðr   c                 óF   •— g | ]\  }} ‰‰j         |         ¦  «        ®|‘ŒS r   )r6   )rq   rr   r  r  rî   s      €€r   rs   z/return_and_correct_aliasing.<locals>.<listcomp>“  s?   ø€ ð 
ð 
ð 
á��1Øˆ˜{Ô/°Ô2Ñ3Ô3Ð?ð à?Ð?Ð?r   rW   r   )ÚFunctionalTensorTNc              3   ó2   •K  — | ]} ‰|¦  «        d uV — Œd S r   r0   ©rq   rß   r  s     €r   ú	<genexpr>z.return_and_correct_aliasing.<locals>.<genexpr>®  ó2   øè è € ÐHÐH°!ˆˆ˜qÑ!Ô!¨Ð-ÐHÐHÐHÐHÐHÐHr   c              3   ó2   •K  — | ]} ‰|¦  «        d uV — Œd S r   r0   r-  s     €r   r.  z.return_and_correct_aliasing.<locals>.<genexpr>²  r/  r   zUnsupported schema: c                 óz   •— g | ]7\  \  }}} ‰|¦  «        �" ‰ ‰‰j         |         ¦  «        ‰‰‰¦  «        n|‘Œ8S r   )rí   )	rq   rr   rß   Úor6   r)  r  r7   rî   s	       €€€€€r   rs   z/return_and_correct_aliasing.<locals>.<listcomp>¼  sw   ø€ ð 		
ð 		
ð 		
ñ ‘�!�Q˜ð #�? 1Ñ%Ô%Ð1ð #Ð"Ø#�O KÔ$4°QÔ$7Ñ8Ô8¸+ÀtÈVñô ð ð ð		
ð 		
ð 		
r   )r  ró   r$   ré   r%   ÚTagÚinplace_viewÚtagsr#  rã   rŒ   r+  ÚutilsÚ_mode_utilsÚno_dispatchr&   Ú_meta_in_tls_dispatch_includeÚ!_set_meta_in_tls_dispatch_includeÚanyrí   ÚallÚRuntimeErrorr¾   rê   rì   rÉ   Úzip)r4   r6   r7   ÚoutÚmutated_argsr+  Úmeta_in_tlsÚouts_to_returnr)  r  rî   s   ```     @@@r   Úreturn_and_correct_aliasingrC  Z  s-  øøøøøø€ õ$ ! Ñ&Ô&€Kðð ð ðð ð ð ð õ$ Øˆk˜4­z¸#½uÑ/EÔ/EÐ!N #  È3ñô ð õ „yÔ ¤Ð*Ñ*ð
ð 
ð 
ð 
ð 
å! $™œð
ñ 
ô 
ˆõ �<Ñ Ô  AÒ%Ð%Ð%Ð%ð 	IÐHÐHÐHÐHÐHå˜, qœ/Ð+;Ñ<Ô<ð 		LÝ”Ô(×4Ò4Ñ6Ô6ð Lð Lõ $œh×DÒDÑFÔF�Ý”×:Ò:¸4Ñ@Ô@Ð@ðLØ�D˜$Ð) &Ð)Ð)Ð)å”H×>Ò>¸{ÑKÔKÐKÐKø•E”H×>Ò>¸{ÑKÔKÐKÐKøøøÐKðLð Lð Lñ Lô Lð Lð Lð Lð Lð Lð Løøøð Lð Lð Lð Lõ ÐHÐHÐHÐH°{Ô7GÐHÑHÔHÑHÔHð Øˆ
õ ÐHÐHÐHÐH°{Ô7GÐHÑHÔHÑHÔHð GÝÐ1µC¸¼Ñ4EÔ4EÑEÑFÔFÐFå
ˆ4Œ<ÔÑ Ô  AÒ%Ð%Ø!Ð!ØˆO˜KÔ,¨QÔ/Ñ0Ô0°+¸tÀVñ
ô 
ð 	
ð
 •T˜#‘Y”Yð		
ð 		
ð 		
ð 		
ð 		
ð 		
ð 		
ð 		
õ  #¥9¨[Ô-=Ñ#>Ô#>ÀÑDÔDð		
ñ 		
ô 		
ñô €Nð Ðs*   Ã>E)ÄD8Ä E)Ä8!EÅE)Å)E-Å0E-)Tr   )NN)<Ú
contextlibrI   Údataclassesr   Útypingr   r   r   r   r   r	   r
   r   r   r   r   r   Útyping_extensionsr   Úcollectionsr   r%   r  Útorchgen.modelÚtorch._Cr   r   r   r   r   r   r   rÆ   r   r!   rZ   re   rk   rn   ru   r<   r&   r]   rB   Úcontextmanagerrƒ   r—   r™   rœ   r¿   rË   rÎ   r×   ró   rõ   rø   rù   rÄ   r  rC  r0   r   r   ú<module>rL     s  ðà Ð Ð Ð Ð à €€€Ø !Ð !Ð !Ð !Ð !Ð !Ø jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jÐ jØ $Ð $Ð $Ð $Ð $Ð $Ø Ð Ð Ð Ð Ð à €€€Ø €€€Ø Ð Ð Ð ðð ð ð ð ð ð ð ð ð ð ð ð ð ð #Ð Ø',Ð $ðgð g¸4ð gð gð gð gðWð Wð Wð Wð Wñ Wô Wð Wðvð ð ðð ð ð*2ð 2ð 2ð"
#ð 
#ð 
#ðAð Að Að
!Ð&ð !ð !ð !ð !ð$,ð ,�˜% ¨U¬XÔ-KÐ KÔLÔMð ,ð ,ð ,ð ,ð Ôðð ˜X kÔ2ð ð ð ñ Ôðð Ôð8ð 8ñ Ôð8ðv%ð %ð %ð %ð %Ð-ñ %ô %ð %ðDð Dð Dð Dð D˜ñ Dô Dð DðR# Vð #°Ð7HÔ0Ið #ð #ð #ð #ðJY¨$ð Y°6¸$Ð?PÔ:QÔ3Rð Yð Yð Yð Yð%ð %ð %ð %ðP;Kð ;Kð ;KðB ðð ð ð ð ñ ô ñ „ðð ðð ð ð ð ñ ô ñ „ðð ,.Ð �4˜˜Z˜Ô(Ð -Ð -Ñ -ð
@˜Jð @ð @ð @ð @ðFmð mð mð mð mr   