
    ɯwg                         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y)    N)_ExcludeDispatchKeyGuardDispatchKeyDispatchKeySet)
OpOverload)Library)BaseTyBaseTypeFunctionSchemaOperatorNameOptionalType
SchemaKind   )autograd_not_implementedlibnew_op_name
mutable_opreturnc                    t        |       t        ||      }| j                  |       t        |      }| j	                  ||d       t        t        t        j                  | j                        |      j                  }| j	                  |t        |      d       t        t        j                  |      |      }| j	                  ||d       y)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          `/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/torch/_custom_op/functional.pyregister_functional_opr*      s    6 Z{J7FJJv/
;OHH[/+FGGEIIsvv6DLLM HH[2=A:N1'--
2K][HHHZ?3    c                       fd}|S )Nc                  8   g }g }t        t              |       D ]O  \  }}|r7||j                         nd }|j                  |       |j                  |       ?|j                  |       Q  | }|t	        |      S t        |t              rg ||S |g|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!94@ 	%MHc(+T'!!&)$	% X&>$$fe$)V)j))$$$r+    )r   r&   s   ` r)   r   r   E   s    %( r+   c                       fd}|S )Nc                  \   t        j                  t        j                  d |       r1t	        t        t        j                              5   |  cd d d        S t        j                  t        j                  t        j                  |       st        d      g }| D ]~  }t        |t        j                        rQt        j                  |      r<t        j                  |       t        j                  |      }|j                  |       n|j                  |        t	        t        t        j                              5   | }d d d        t        j                  j                         }t        j"                  t        j$                  d |       }||d  }t'        t)              |       D cg c]	  \  }}|r| }	}}t        |      t        |	      k(  sJ t'        ||	      D ]  \  }
}||
||
t        j*                  j-                  ||
       t        j*                  j/                  ||
       t        j*                  j1                  |       t        j                  |        t        |      dk(  r|d   S t        |      dk(  ry |S # 1 sw Y   ?xY w# 1 sw Y   YxY wc c}}w )Nc                 .    t        j                  |        S r.   )r   _is_functional_tensor)xs    r)   <lambda>zDconstruct_functionalization_kernel.<locals>.kernel.<locals>.<lambda>a   s    E<W<WXY<Z8Z r+   z={mutable_op}: expected all args to be FunctionalTensorWrapperr   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_propagater8   inputs_to_replace	new_valuer'   r   s              r)   kernelz2construct_functionalization_kernel.<locals>.kernel^   s\    .Z\`a).9R9R*ST )!4() ) ##ELL%2M2MtT^__ 	+C#u||,1L1LS1QC !99#>	%%i0%%c*	+ &n[5N5N&OP 	4"N3F	4  
 2 2 : :;''0B1B)CE #)):);"<69,z:RTX6Y *]Xs ( ! * **+s3D/EEEE!"9;LM 	NIs	 1s9K`HH((i8HHsI.HH##C(KK	 }" ##1$W) )(	4 	4*s   
J>J/J(JJ%r<   )r   r'   rZ   s   `` r)   r"   r"   ]   s    /b Mr+   c                 x   t        | t              st        dt        |              t	        j
                  t        | j                              }|j                         t        j                  k(  st        d      |j                  D ]  }|j                  t        d       |j                  j                   D ]~  }|j                  j#                         s|j                  t%        t&        j(                        k7  sE|j                  t+        t%        t&        j(                              k7  sut        d       y )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)zNYI: 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strrJ   kindr   mutablerF   rK   
annotationNotImplementedError	argumentsflat_allis_tensor_liker	   r   rE   r   )r   r%   retr9   s       r)   r   r      s   j*-""&z"2!356 	6 !!#j&8&8"9:F;;=J...abb~~ <>>%%;< << (( )88""$HH//L&--)@AA%() ))r+   opc                     t        j                  t        |j                              }|j	                         j                  t        j                  |             }t        |      S r.   )r
   r^   r_   rJ   	signature	with_namer   )r   rh   r%   s      r)   r   r      sH    !!#bjj/2F)),*<*<[*IJFv;r+   c                 N    t        d | j                  j                  D              S )Nc              3   d   K   | ](  }|j                   dn|j                   j                   * y w)NF)
alias_infor8   ).0r9   s     r)   	<genexpr>zmutable_args.<locals>.<genexpr>   s0      2 ..0cnn6M6MM 2s   .0)r3   rJ   rd   )rh   s    r)   r0   r0      s&     2JJ002 2 2r+   )r#   r   torch.utils._pytreeutils_pytreerC   torch._Cr   r   r   
torch._opsr   torch.libraryr   torchgen.modelr   r	   r
   r   r   r   autogradr   r_   r*   r   r"   r   r   r0   r<   r+   r)   <module>ry      s      $ $ J J ! !  /-4	-4-4 -4 
	-4`02j) )Dz 2Z 2r+   