§
    ž²ÅjÌ  ã                   óÈ   — d dl Z d dlZd dlmc mZ d dlmZmZm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZmZmZ ddlmZ ded	ed
eddfd„Zd„ Zd„ Zd
efd„Zdefd„Zdefd„ZdS )é    N)Ú_ExcludeDispatchKeyGuardÚDispatchKeyÚDispatchKeySet)Ú
OpOverload)ÚLibrary)ÚBaseTyÚBaseTypeÚFunctionSchemaÚOperatorNameÚOptionalTypeÚ
SchemaKindé   )Úautograd_not_implementedÚlibÚnew_op_nameÚ
mutable_opÚreturnc                 óØ  — t          |¦  «         t          ||¦  «        }|                      |¦  «         t          |¦  «        }|                      ||d¦  «         t          t          t          j        | j        ¦  «        |¦  «        j	        }|                      |t          |¦  «        d¦  «         t          t          j        |¦  «        |¦  «        }|                      ||d¦  «         dS )a)  Given a mutable operator, registers the functional variant.

    This API also correctly links the functional variant with the mutable
    operator for the purposes of functionalization.

    All of the new registrations are performed on the ``lib`` passed in.

    Arguments:
        lib (Library): Should be a torch.library.Library object that has
            the same namespace as ``mutable_op``'s namespace.
            lib will be used to register the new functional op as well
            as a functionalization kernel for the ``mutable_op``
            If you don't have a library handy, use
            ``torch.library.Library(ns, 'FRAGMENT')`` to construct one.
        new_op_name (str): The name of the functional operator (without the
            namespace). If no namespace, the new functional variant will be
            accessible under ``torch.ops.{lib.ns}.new_op_name``.
        mutable_op (OpOverload): The mutable custom operator. Note
            that you may need to add a `.default` to it, like
            `torch.ops.aten.abs_.default`.

    ÚCompositeExplicitAutogradÚAutogradÚFunctionalizeN)ÚvalidateÚfunctional_schemaÚdefineÚconstruct_functional_implÚimplÚgetattrÚtorchÚopsÚnsÚdefaultr   Ú"construct_functionalization_kernelÚweakrefÚproxy)r   r   r   ÚschemaÚfunctional_implÚfunctional_opÚf_kernels          úLC:\Projects\Historical\venv\Lib\site-packages\torch/_custom_op/functional.pyÚregister_functional_opr*      sÍ   € õ6 ˆZÑÔÐÝ˜{¨JÑ7Ô7€FØ‡J‚JˆvÑÔÐå/°
Ñ;Ô;€OØ‡H‚Hˆ[˜/Ð+FÑGÔGÐGå�G¥E¤I¨s¬vÑ6Ô6¸ÑDÔDÔL€Mð ‡H‚Hˆ[Õ2°=ÑAÔAÀ:ÑNÔNÐNå1µ'´-À
Ñ2KÔ2KÈ]Ñ[Ô[€Hà‡H‚HˆZ˜ ?Ñ3Ô3Ð3Ð3Ð3ó    c                 ó   ‡ — ˆ fd„}|S )Nc                  óv  •— g }g }t          t          ‰¦  «        | ¦  «        D ]_\  }}|rC|�|                     ¦   «         nd }|                     |¦  «         |                     |¦  «         ŒJ|                     |¦  «         Œ` ‰|Ž }|€t	          |¦  «        S t          |t          ¦  «        rg |¢|¢R S |g|¢R S ©N)ÚzipÚmutable_argsÚcloneÚappendÚtupleÚ
isinstance)ÚargsÚnew_argsÚ
extra_retsÚis_writeÚargÚclonedÚresultr   s          €r)   r&   z2construct_functional_impl.<locals>.functional_implF   sä   ø€ ð
 ˆØˆ
Ý ¥¨jÑ!9Ô!9¸4Ñ@Ô@ð 	%ð 	%‰MˆH�cØð %Ø(+¨˜Ÿš™œ˜¸T�Ø—’ Ñ'Ô'Ð'Ø×!Ò! &Ñ)Ô)Ð)Ð)à—’ Ñ$Ô$Ð$Ð$Ø�˜XÐ&ˆØˆ>Ý˜Ñ$Ô$Ð$Ý�f�eÑ$Ô$ð 	*Ø)�VÐ)˜jÐ)Ð)Ð)ØÐ$˜Ð$Ð$Ð$r+   © )r   r&   s   ` r)   r   r   E   s$   ø€ ð%ð %ð %ð %ð %ð( Ðr+   c                 ó   ‡ ‡— ˆˆ fd„}|S )Nc                  ó‚  •— t          j        t          j        d„ | ¦  «        rCt	          t          t          j        ¦  «        ¦  «        5   ‰| Ž cd d d ¦  «         S # 1 swxY w Y   t          j        t          j        t          j        | ¦  «        st          d¦  «        ‚g }| D ]ƒ}t          |t          j        ¦  «        rRt          j        |¦  «        r>t          j        |¦  «         t          j        |¦  «        }|                     |¦  «         Œn|                     |¦  «         Œ„t	          t          t          j        ¦  «        ¦  «        5   ‰
|Ž }d d d ¦  «         n# 1 swxY w Y   t          ‰j        j        ¦  «        }t          j        t          j        |d |…         ¦  «        }||d …         }d„ t'          t)          ‰¦  «        | ¦  «        D ¦   «         }t          |¦  «        t          |¦  «        k    sJ ‚t'          ||¦  «        D ]�\  }	}|€|	�|�|	�Œt          j                             ||	¦  «         t          j                             ||	¦  «         t          j                             |¦  «         t          j        |¦  «         Œ‚t          |¦  «        dk    r|d         S t          |¦  «        dk    rd S |S )Nc                 ó,   — t          j        | ¦  «         S r.   )r   Ú_is_functional_tensor)Úxs    r)   ú<lambda>zDconstruct_functionalization_kernel.<locals>.kernel.<locals>.<lambda>a   s   € ½EÔ<WÐXYÑ<ZÔ<ZÐ8Z€ r+   z={mutable_op}: expected all args to be FunctionalTensorWrapperc                 ó   — g | ]	\  }}|¯|‘Œ
S r<   r<   )Ú.0r8   r9   s      r)   ú
<listcomp>zFconstruct_functionalization_kernel.<locals>.kernel.<locals>.<listcomp>~   s0   € ð *ð *ð *¡] X¨sØ (ð*˜Sð *ð *ð *r+   r   r   )ÚpytreeÚtree_all_onlyr   ÚTensorr   r   r   r   r@   ÚRuntimeErrorr4   Ú_syncÚ_from_functional_tensorr2   ÚlenÚ_schemaÚreturnsÚtree_mapÚ_to_functional_tensorr/   r0   Ú_CÚ_propagate_xla_dataÚ	_replace_Ú_commit_update)r5   Úunwrapped_argsr9   Ú	unwrappedÚoutputÚnum_actual_outputÚactual_outputÚnew_values_to_propagateÚinputs_to_replaceÚ	new_valuer'   r   s             €€r)   Úkernelz2construct_functionalization_kernel.<locals>.kernel^   s?  ø€ õ Ô¥¤Ð.ZÐ.ZÐ\`ÑaÔað 	)Ý)­.½Ô9RÑ*SÔ*SÑTÔTð )ð )Ø!�z 4Ð(ð)ð )ð )ð )ñ )ô )ð )ð )ð )ð )ð )ð )øøøð )ð )ð )ð )õ Ô#¥E¤Lµ%Ô2MÈtÑTÔTð 	`ÝÐ^Ñ_Ô_Ð_àˆØð 	+ð 	+ˆCÝ˜#�uœ|Ñ,Ô,ð +µÔ1LÈSÑ1QÔ1Qð +Ý”˜CÑ Ô Ð Ý!Ô9¸#Ñ>Ô>�	Ø×%Ò% iÑ0Ô0Ð0Ð0à×%Ò% cÑ*Ô*Ð*Ð*å%¥nµ[Ô5NÑ&OÔ&OÑPÔPð 	4ð 	4Ø"�] NÐ3ˆFð	4ð 	4ð 	4ñ 	4ô 	4ð 	4ð 	4ð 	4ð 	4ð 	4ð 	4øøøð 	4ð 	4ð 	4ð 	4õ   
Ô 2Ô :Ñ;Ô;ÐÝœÝÔ'¨Ð0BÐ1BÐ0BÔ)CñEô Eˆð #)Ð):Ð);Ð);Ô"<Ðð*ð *µc½,ÀzÑ:RÔ:RÐTXÑ6YÔ6Yð *ñ *ô *ÐåÐ*Ñ+Ô+­sÐ3DÑ/EÔ/EÒEÐEÐEÐEÝ!Ð"9Ð;LÑMÔMð 	ð 	‰NˆI�sØ� 	Ð 1°s°È9ÐK`ØÝŒH×(Ò(¨¨iÑ8Ô8Ð8ÝŒH×Ò˜s IÑ.Ô.Ð.ÝŒH×#Ò# CÑ(Ô(Ð(ÝŒK˜ÑÔÐÐåˆ}ÑÔ Ò"Ð"Ø  Ô#Ð#Ý�ÑÔ 1Ò$Ð$Ø�4ØÐs$   Á	AÁAÁ"AÅEÅE#Å&E#r<   )r   r'   r]   s   `` r)   r"   r"   ]   s*   øø€ ð/ð /ð /ð /ð /ð /ðb €Mr+   c                 ól  — t          | t          ¦  «        st          dt          | ¦  «        › �¦  «        ‚t	          j        t          | j        ¦  «        ¦  «        }|                     ¦   «         t          j
        k    st          d¦  «        ‚|j        D ]}|j        �t          d¦  «        ‚Œ|j        j        D ]{}|j                             ¦   «         r`|j        t%          t&          j        ¦  «        k    r>|j        t+          t%          t&          j        ¦  «        ¦  «        k    rt          d¦  «        ‚Œ|d S )Nz]register_functional_op(mutable_op): expected mutable_op to be instance of OpOverload but got zDExpected op to be mutable (as opposed to functional, inplace or out)z¸NYI: register_functional_op(op) where op returns a mutated or aliased value. Please file an issue (and as a workaround, modify your operator to not return the mutated value or aliases)zWNYI: register_functional_op(op) where op has a List[Tensor] input.Please file an issue.)r4   r   Ú	TypeErrorÚtyper
   ÚparseÚstrrM   Úkindr   ÚmutablerI   rN   Ú
annotationÚNotImplementedErrorÚ	argumentsÚflat_allÚis_tensor_liker	   r   rH   r   )r   r%   Úretr9   s       r)   r   r   ’   s<  € Ý�j¥*Ñ-Ô-ð 6Ýð5Ý"& zÑ"2Ô"2ð5ð 5ñ6ô 6ð 	6õ Ô!¥# jÔ&8Ñ"9Ô"9Ñ:Ô:€FØ�;Š;‰=Œ=�JÔ.Ò.Ð.ÝÐaÑbÔbÐbØŒ~ð <ð <ˆàŒ>Ð%Ý%ð;ñ<ô <ð <ð &ð
 ÔÔ(ð )ð )ˆàŒ8×"Ò"Ñ$Ô$ð 	)ØŒH�¥¤Ñ/Ô/Ò/Ð/Ø”�L­µ&´-Ñ)@Ô)@ÑAÔAÒAÐAå%ð(ñ)ô )ð )øð)ð )r+   Úopc                 óÞ   — t          j        t          |j        ¦  «        ¦  «        }|                     ¦   «                              t          j        | ¦  «        ¦  «        }t          |¦  «        S r.   )r
   ra   rb   rM   Ú	signatureÚ	with_namer   )r   rk   r%   s      r)   r   r   ´   sP   € ÝÔ!¥# b¤j¡/¤/Ñ2Ô2€FØ×ÒÑÔ×)Ò)­,Ô*<¸[Ñ*IÔ*IÑJÔJ€FÝˆv‰;Œ;Ðr+   c                 óH   — t          d„ | j        j        D ¦   «         ¦  «        S )Nc              3   ó@   K  — | ]}|j         €dn|j         j        V — Œd S )NF)Ú
alias_infor8   )rD   r9   s     r)   ú	<genexpr>zmutable_args.<locals>.<genexpr>»   sF   è è € ð 2ð 2Øð œ.Ð0��°c´nÔ6Mð 2ð 2ð 2ð 2ð 2ð 2r+   )r3   rM   rg   )rk   s    r)   r0   r0   º   s5   € Ýð 2ð 2ØœJÔ0ð2ñ 2ô 2ñ 2ô 2ð 2r+   )r#   r   Útorch.utils._pytreeÚutilsÚ_pytreerF   Útorch._Cr   r   r   Ú
torch._opsr   Útorch.libraryr   Útorchgen.modelr   r	   r
   r   r   r   Úautogradr   rb   r*   r   r"   r   r   r0   r<   r+   r)   ú<module>r{      sŒ  ðà €€€à €€€Ø $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ø JÐ JÐ JÐ JÐ JÐ JÐ JÐ JÐ JÐ JØ !Ð !Ð !Ð !Ð !Ð !Ø !Ð !Ð !Ð !Ð !Ð !ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð /Ð .Ð .Ð .Ð .Ð .ð-4Ø	ð-4àð-4ð ð-4ð 
ð	-4ð -4ð -4ð -4ð`ð ð ð02ð 2ð 2ðj)˜ð )ð )ð )ð )ðD zð ð ð ð ð2�Zð 2ð 2ð 2ð 2ð 2ð 2r+   