
    ii                     d   U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
Zd dl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mZ 	 g dZdd	d
Zh dZd Z	 d,dedej<                  e   dej>                  fdZ i Z!e"edf   e#d<    G d d      Z$e jJ                   G d d             Z&dedefdZ'deddfdZ(deddfdZ)dede*eef   fdZ+deddfdZ,d ejZ                  de.fd!Z/ded"ej>                  ddfd#Z0d$ejb                  d%eddfd&Z2d' Z3d( Z4d-d)Z5d* Z6d.d+Z7y)/    N)infer_schema)get_ctx)BaseTyBaseTypeFunctionSchemaListTypeOperatorName
SchemaKind   )autograd_kernel_indirectionconstruct_autograd_kernel)	custom_opCustomOpr   CPUCUDA)cpucuda>   atatenprimprimstorchpytorchc                  <    t        j                  dt        d       y )Nzwtorch._custom_op is deprecated and will be removed in PyTorch 2.6, please use the equivalent torch.library API instead.   
stacklevel)warningswarnDeprecationWarning     O/var/www/html/engine/venv/lib/python3.12/site-packages/torch/_custom_op/impl.pywarn_deprecatedr$   5   s    MM	8	r"   qualnamemanual_schemareturnc                 *     t                 fd}|S )L
    This API is deprecated, please use torch.library.custom_op instead
    c           	         t        j                  |       st        dt        |              t	        
      \  }}t        |       | j                  |k7  rt        d
 d| d| j                   d      	t        | d      n	}| | }t        j                  |      }t        |       	t        ||        t        j                  |d      }|j                  |       t        ||j                         }t#        |||||d	
      }| j                  |_        | j$                  |_        | j&                  |_         t        j(                  ||j*                  d      t-        t/        j0                  |                   t2        j4                  j7                  |t9        j:                  t<        t/        j0                  |                   |S )NzDcustom_op(...)(func): Expected `func` to be a Python function, got: zcustom_op(qualname='z-', ...)(func): expected `func` to have name 'z' but got 'zX'. Please either change the name of `func` or the qualname that is passed to `custom_op`r!   )mutates_argsFRAGMENTT_private_accessAutograd)inspect
isfunction
ValueErrortypeparse_qualnamevalidate_namespace__name__r   r   parsevalidate_schema validate_function_matches_schemalibraryLibrarydefinefind_ophandle_or_thrownamer   
__module____doc__impl_opnamer   weakrefproxyr   _C#_dispatch_set_report_error_callback	functoolspartialreport_error_callback)funcnsr>   schema
schema_strfunction_schemalibophandleresultr&   r%   s            r#   innerzcustom_op.<locals>.innerF   s   !!$'""&t*/ 
 "(+D2==D &xj 1!!%k$-- A+,  $ B/ 	
 vfX&
(..z:($,_dCoob*-

:)"o.B.BC_dHd
 -- OO5S&..*5'f(=>	
 	44i''(=w}}V?TU	
 r"   )r$   )r%   r&   rR   s   `` r#   r   r   >   s     /b Lr"   r   global_registryc                   $    e Zd ZdZdd fd
Zd ZddZd Zd Zd	 Z	d
 Z
d Z	 ddej                  eej                  e   f   dej                   fdZd Zdej                   fdZddej                   fdZd Zd Zd Zd ZddZddZ xZS )r   r)   Fr-   c                   t         |           t                |st        d      | d| }|| _        || _        || _        || _        || _        || _	        d | _
        i | _        d| _        | t        | j                  <   y )Nz|The CustomOp constructor is private and we do not guarantee BC for it. Please use custom_op(...) to create a CustomOp object::F)super__init__r$   RuntimeError_schema_cpp_ns_lib	_ophandlerB   	_qualnamer6   _impls'_registered_autograd_kernel_indirectionrS   )	selfrO   cpp_nsrL   operator_namerP   r.   r>   	__class__s	           r#   rX   zCustomOp.__init__   s     	S  M?+%(	5=)" DF7<4*.'r"   c                     | j                   rJ | j                  j                  | j                  t	        t        j                  |             d       d| _         y )Nr/   T)r`   r\   rA   rB   r   rC   rD   ra   s    r#   %_register_autograd_kernel_indirectionz.CustomOp._register_autograd_kernel_indirection   sE    ????		LL5gmmD6IJJ	
 8<4r"   c                 `   | j                  |      rA| j                  |   }|J |j                  }t        d| d| j                   d| d| d	      t        j                  t        j                  |            }|j                   d|j                   }t        ||      | j                  |<   y )NzAttempting to register a z impl for operator z that already has a z  impl registered from Python at z. This is not supported.:)	_has_implr_   locationrY   r^   r0   getframeinfosys	_getframefilenamelinenoFuncAndLocation)ra   kindrJ   r   func_and_locationrk   frames          r#   _register_implzCustomOp._register_impl   s    >>$ $D 1$000(11H+D61DT^^DT U&&*V+K*46 
 $$S]]:%>?nn%Qu||n5+D(;Dr"   c                      | j                   |   S Nr_   ra   rr   s     r#   	_get_implzCustomOp._get_impl   s    {{4  r"   c                     || j                   v S rw   rx   ry   s     r#   rj   zCustomOp._has_impl   s    t{{""r"   c                     | ` t        t        j                  | j                        }t        || j                        rt        || j                         t        | j                  = y rw   )
r\   getattrr   opsr[   hasattrrB   delattrrS   r^   )ra   opnamespaces     r#   _destroyzCustomOp._destroy   sE     Ieii6;-K.DNN+r"   c                 "    d| j                    dS )Nz<CustomOp(op="z")>)r^   rf   s    r#   __repr__zCustomOp.__repr__   s    /s33r"   c                 L    t        j                  | j                  g|i |}|S rw   )rE   _dispatch_call_boxedr]   )ra   argskwargsrQ   s       r#   __call__zCustomOp.__call__   s'     ((I$I&Ir"   device_typesr'   c                 d     t        t              rgD ]  }t        |         fd}|S )T
        This API is deprecated, please use torch.library.custom_op instead
        c                     t              D ]a  }j                  |       j                  ||        t        |   } t	        j
                  j                  j                  |      |        c | S )Nr   )set_check_doesnt_have_library_implru   SUPPORTED_DEVICE_TYPE_TO_KEYr:   rA   r\   rB   )fdevice_typedispatch_key_stacklevelr   ra   s      r#   rR   zCustomOp.impl.<locals>.inner   sl    "<0 G44[A##K{#K;KHCTYYlCAF	G
 Hr"   )
isinstancestrvalidate_device_type)ra   r   r   r   rR   s   ```  r#   rA   zCustomOp.impl   s9     lC((>L' 	.K -	.	 r"   c                     | j                  |      ry t        |   }t        j                  | j                  |      rt        d| d| j                   d      y )Nzimpl(..., device_types=z): the operator zs already has an implementation for this device type via a pre-existing torch.library or TORCH_LIBRARY registration.)rj   r   rE   ._dispatch_has_computed_kernel_for_dispatch_keyr^   rY   )ra   r   keys      r#   r   z(CustomOp._check_doesnt_have_library_impl   sb    >>+&*;7<<T^^SQ)+6Ft~~FV WL M  Rr"   c                       fd}|S )z2Register an implementation for a factory function.c                     j                  d|         t        j                  j                  j                  d      |        | S )NfactoryBackendSelect)ru   r:   rA   r\   rB   )r   ra   s    r#   rR   z$CustomOp.impl_factory.<locals>.inner   s7    	1-BGLLDLL/B1EHr"   r!   )ra   rR   s   ` r#   impl_factoryzCustomOp.impl_factory   s    	
 r"   c                       fd}|S )r   c                 0    j                          j                  d        j                  d      j                  j                  t        j                          fd       }j                  j                  j                  |d        S )Nabstractr   c                      fd}t         j                  j                  j                  |      5   | i |cd d d        S # 1 sw Y   y xY w)Nc                  &    t        d d        )Nz<Attempted to call get_ctx() for the meta implementation for a  .You have presumably called get_ctx() because the operator has a data-dependent output shape; if so, there is no such meta implementation and this error is the correct behavior. Otherwise, please remove the call to get_ctx() in the implementation registered with impl_abstract at rY   )rk   r%   s   r#   error_on_ctxzOCustomOp.impl_abstract.<locals>.inner.<locals>.f_with_ctx.<locals>.error_on_ctx  s+    &'j ) 'Z)	 	r"   )r   _library	fake_implset_ctx_getter)r   r   r   r   rk   r%   s      r#   
f_with_ctxz9CustomOp.impl_abstract.<locals>.inner.<locals>.f_with_ctx  sD    
 ^^--<<\J .d-f-. . .s   AAMeta)
$_check_doesnt_have_library_meta_implru   rz   rk   r^   rG   wrapsr\   rA   rB   )r   r   rk   r%   r   ra   s   ` @@r#   rR   z%CustomOp.impl_abstract.<locals>.inner  s}    557
A+F~~j1::H~~H __Q.  .  IINN4<<V<Hr"   r!   ra   r   rR   s   `` r#   impl_abstractzCustomOp.impl_abstract  s    
	8 r"   c                      fd} j                   j                         t        j                  k7  r |d       j                  }j                  s |d       t        |      dkD  sJ t        d |D              }|r |d       t        t        j                        dt        t        j                        dt        t        j                        d	t        t        j                        d
t        t        j                        dt        t        t        j                        d       di}j                  D ]A  }|j                  |v r |dt!        |j#                                d|j                   d       C y )Nc                 @    t        d|  dj                   d       )NzCCannot use torch._custom_ops APIs to register backward formula for z. Got operator z with schema: )rY   r^   )detailrL   ra   s    r#   errorz4CustomOp._check_can_register_backward.<locals>.error+  s1    %ho>>".: r"   znon-functional operatorzoperator with no returnsr   c              3   j   K   | ]+  }|j                   d uxr |j                   j                    - y wrw   )
annotationis_write).0rs     r#   	<genexpr>z8CustomOp._check_can_register_backward.<locals>.<genexpr>;  s3      #
GHALL$BQ\\-B-B)BB#
s   13zoperator that returns viewsintSymIntboolfloatTensorzList[Tensor]zoperator with return not in z (got ))rZ   rr   r
   
functionalreturnslenanyr   r   r   r   r   r   r   r   r3   listvalues)ra   r   retsis_non_mutating_viewallowed_return_typesretrL   s   `     @r#   _check_can_register_backwardz%CustomOp._check_can_register_backward*  s=   	 ;;=J111+,~~~~,-4y1}}" #
LP#
  
  /0 VZZ %V]]#XV[[!6V\\"GV]]#XXfmm,d3^ 
 >> 	Cxx//.t4H4O4O4Q/R.SSYZ]ZbZbYccde	r"   c                    | j                   ry t        j                  | j                  d      rt	        d| j                   d      dD ]>  }t        j                  | j                  |      s$t	        d| j                   d| d       y )NCompositeImplicitAutogradz3impl_backward/impl_save_for_backward: the operator a3   already has an implementation for this device type via a pre-existing registration to DispatchKey::CompositeImplicitAutograd.CompositeImplicitAutograd operators do not need an autograd formula; instead, the operator will decompose into its constituents and those can have autograd formulas defined on them.)r/   AutogradCPUAutogradCUDAz; already has an Autograd kernel registered to DispatchKey::z vi a pre-existing torch.library or TORCH_LIBRARY registration. Please either remove those registrations or don't use the torch._custom_ops APIs)r`   rE   %_dispatch_has_kernel_for_dispatch_keyr^   rY   )ra   r   s     r#   (_check_doesnt_have_library_autograd_implz1CustomOp._check_doesnt_have_library_autograd_implQ  s    7733NN7
 EdnnEU V> ?  ? 	C77L"$$(NN#3 4225 7YZ 	r"   c                    | j                  d      ry t        j                  | j                  d      r!t        j                  | j                  d      sy t        j                  | j                  d      rt	        d| j                   d      t        j                  | j                  d      rt	        d| j                   d      y )Nr   CompositeExplicitAutogradr   r   z!impl_abstract(...): the operator a-   already has an implementation for this device type via a pre-existing registration to DispatchKey::CompositeImplicitAutograd.CompositeImplicitAutograd operators do not need an abstract impl; instead, the operator will decompose into its constituents and those can have abstract impls defined on them.z already has an DispatchKey::Meta implementation via a pre-existing torch.library or TORCH_LIBRARY registration. Please either remove that registration or don't call impl_abstract.)rj   rE   r   r^   rY   rf   s    r#   r   z-CustomOp._check_doesnt_have_library_meta_implm  s    >>*% 33NN7
::4>>6R 33NN7
 3DNN3C D; <  33DNNFK3DNN3C DV W  Lr"   c           
      F   | j                  d      sJ | j                  d      sJ t        | j                  | j                  | t	        | j
                        | j                  d      j                  | j                  d      j                        }| j                  d|       y )Nbackwardsave_for_backwardautograd)	rj   r   rZ   _output_differentiabilityget_opr^   rz   rJ   ru   )ra   kernels     r#   _register_autograd_kernelz"CustomOp._register_autograd_kernel  s    ~~j)))~~1222*LL**4>>"NN./44NN:&++
 	J/r"   c                       fd}|S )zyRegister a function that tells us what to save for backward.

        Please see impl_backward for more details.
        c                     j                          j                          j                  sj                          j	                  d|        j                  d      rj                          y y )Nr   r   r   )r   r   r`   rg   ru   rj   r   )r   r   ra   s    r#   rR   z.CustomOp.impl_save_for_backward.<locals>.inner  sc    --/99;??::< 3Q;O~~j)..0 *r"   r!   r   s   `` r#   impl_save_for_backwardzCustomOp.impl_save_for_backward  s    	1 r"   c                      mfd}t        t              s |        D ]  }t        |t              r |         t         j                  j
                        t              k7  r |         fd}|S )r   c                       t        d        )Nzimpl_backward(output_differentiability): expected output_differentiability to be a list of bools with length equal to the number of outputs of this CustomOp got: r   )output_differentiabilitys   r#   yellz$CustomOp.impl_backward.<locals>.yell  s"    " 557 r"   c                     j                          j                          j                  sj                          j	                  d|        _        j                  d      rj                          y y )Nr   r   r   )r   r   r`   rg   ru   r   rj   r   )r   r   r   ra   s    r#   rR   z%CustomOp.impl_backward.<locals>.inner  sk    --/99;??::<
A+F-ED*~~12..0 3r"   )r   r   r   r   rZ   r   )ra   r   r   r   diffrR   s   ```   r#   impl_backwardzCustomOp.impl_backward  sp     $/ 6=0 !$-F 4<<''(C0H,II	1 r"   )r   )Nr   )r6   r?   __qualname__r@   rX   rg   ru   rz   rj   r   r   r   typingUnionr   IterableCallablerA   r   r   r   r   r   r   r   r   r   __classcell__)rd   s   @r#   r   r      s    
 PU/8<<!#,4 ll3(<#<= 
	.	foo !foo !F%N8%X0" r"   c                   6    e Zd ZU ej                  ed<   eed<   y)rq   rJ   rk   N)r6   r?   r   r   r   __annotations__r   r!   r"   r#   rq   rq     s    
//Mr"   rq   rb   rc   c                     |j                   dn|j                   }t        j                  |  dt        |j                         |      S )N rV   )overload_namerE   _dispatch_find_schema_or_throwr   r>   )rb   rc   r   s      r#   r=   r=     sN    ))1}7R7R  ,,("S++,-. r"   rK   c                 ^    d| v rt        d|  d      | t        v rt        d|  d|  d      y )N.zcustom_op(..., ns="zC"): expected ns to not contain any . (and be a valid variable name)zcustom_op(..., ns='z'): 'z9' is a reserved namespace, please choose something else. )r2   RESERVED_NS)rK   s    r#   r5   r5     sZ    
by!" &# $
 	
 
[!"U2$ /- .
 	
 r"   rL   c                     t         j                  j                  j                  |       st	        d|        | j
                  j                  t	        d|        y )Nzcustom_op only supports functional operators (ops that do not mutate any inputs, do not return views of the inputs, and has at least one return). Got the following non-functional schema: zUcustom_op does not support arguments named 'self'. Please rename your argument. Got: )r   r   utilsis_functional_schemar2   	argumentsself_arg)rL   s    r#   r8   r8     sn    >>44V<8 9?xA
 	
   ,**03
 	
 -r"   c                     | j                  dd      }t        |      dk7  rt        d|  d      d|d   v rt        d|        |d   |d   fS )	NrV   r   r   z$Expected there to be a namespace in z;, i.e. The operator name should look something like ns::foor   zThe torch.custom_ops APIs do not handle overloads, i.e. operator names with '.' in them. Please name your operator something like ns::foo. Got: r   )splitr   r2   )r%   namess     r#   r4   r4     s|    NN4#E
5zQ28* =? @
 	
 eAh :
 	
 8U1Xr"   r   c                 \    | t         vr$t        d|  dt         j                          d      y )NzCustomOp.impl(device_types=[z(, ...]): we only support device_type in r   )r   r2   keys)r   s    r#   r   r     s?    66*;- 8.3356a9
 	
 7r"   paramc                     | j                   t        j                  j                  t        j                  j                  fv S rw   )rr   r0   	ParameterPOSITIONAL_OR_KEYWORDKEYWORD_ONLY)r   s    r#   supported_paramr     s6    :://&&  r"   rJ   c                 f   	 t        j                  |      	t        d 	j                  j	                         D              st        d	       t        d 	j                  j	                         D              s&	j                  t         j                  j                  urt        d	       	j                  j	                         D cg c]0  \  }}|j                  t         j                  j                  k(  r||f2 }}}	j                  j	                         D cg c]0  \  }}|j                  t         j                  j                  k(  r||f2 }}} 	fd 	fdfd} || j                  j                          || j                  j                          y c c}}w c c}}w )Nc              3   8   K   | ]  \  }}t        |        y wrw   )r  r   _ps      r#   r   z3validate_function_matches_schema.<locals>.<genexpr>,  s     Edaq!Es   zcustom_op(..., manual_schema)(func): positional-only args, varargs, and kwargs are not supported. Please rewrite `func` to not have them. Got `func` with signature: c              3   n   K   | ]-  \  }}|j                   t        j                  j                  u / y wrw   )r   r0   r   emptyr  s      r#   r   z3validate_function_matches_schema.<locals>.<genexpr>4  s1      
1 LL 1 1 7 77
s   35zcustom_op(..., manual_schema)(func): When passing in a manual schema, we expect `func` to have no type annotations to avoid ambiguity. Got `func` with signature: c                  &    t        d d        )Nzcustom_op(..., manual_schema)(func): When passing in a manual schema, we expect `func`'s signature to match `manual_schema` (aside from type annotations). func's signature: , manual_schema: r2   rL   sigs   r#   r   z/validate_function_matches_schema.<locals>.errorK  )    ! "%%6vh@
 	
r"   c                  &    t        d d        )Nzycustom_op(..., manual_schema)(func): neither func nor manual_schema should have default arguments. Got func's signature: r  r  r  s   r#   error_default_argsz<validate_function_matches_schema.<locals>.error_default_argsS  r  r"   c                    t        |       t        |      k7  r         t        | |      D ]X  \  \  }}}||j                  k7  r         |j                  t        j
                  j                  us|j                  R         Z y rw   )r   zipr>   defaultr0   r   r
  )sig_argsschema_argsr>   r   argr   r  s        r#   comparez1validate_function_matches_schema.<locals>.compare[  sp    x=C,,G"%h"< 	%MT53sxx}}G$5$5$;$;;s{{?V"$		%r"   )r0   	signatureall
parametersitemsr2   r   return_annotation	Signaturer
  rr   r   r  r  r   flat_positionalflat_kwarg_only)
rL   rJ   r>   r   
positional	kwargonlyr  r   r  r  s
   `      @@@r#   r9   r9   '  s    

D
!CEcnn.B.B.DEE<<?5B
 	
 	 
,,.
 	
   (9(9(?(??558E;
 	
 >>//1D%::**@@@ 
uJ  >>//1D%::**777 
uI 

% J((889Iv''778K
s   5F'"5F-r   r   c           
          |dk(  rt        |  d      |dk(  rt        |  d      |dv r'|j                         }t        |  d| d| d| d	      t        |  d
| d      )N	Undefineda  : There were no Tensor inputs to this operator (e.g. you passed an empty list of Tensors). If your operator is a factory function (that is, it takes no Tensors and constructs a new one), then please use CustomOp.impl_factory to register an implementation for itr   z: when running with device='Meta' tensors: there is no abstract impl registered for this CustomOp. Please register one via CustomOp.impl_abstract to get this CustomOp to work with Meta tensors)r   r   z: when running with device='z' tensors: there is no zW impl registered for this CustomOp. Please register one via CustomOp.impl(device_type='z')z%: No implementation for dispatch key z. It is likely that we have not added this functionality yet, please either open an issue or if you're feeling adventurous, use the low-level torch.library API)NotImplementedErrorlower)r   r   devices      r#   rI   rI   h  s    
k!k ' (
 	
 f}!k T U
 	

 o!k5fX=Th **05
 	

 +:3% @ 	 r"   c                 @   | j                   }t        j                  j                  |d      }| j	                         j                  d      d   }t        | j                        }|j                  dd      d   }t        j                  |      }t        ||||| d      S )Nr,   rV   r   )maxsplitTr-   )	namespacer   r:   r;   r>   r   r   rZ   rsplitr   r7   r   )oprK   rO   r>   rM   rL   s         r#   custom_op_from_existingr/    s    	B
--

J
/C779??4 $DRZZJ""4!"4R8J!!*-FCVT2tDDr"   c                      fd}t               \  }}t        t        j                  |      s |        t	        t        j                  |      }t        ||      s |        t	        ||      }t        |d      s |        |j
                  S )Nc                  "    t        d  d      )NzCould not find the operator z~. Please make sure you have already registered the operator and (if registered from C++) loaded it via torch.ops.load_library.r  )r%   s   r#   error_not_foundzget_op.<locals>.error_not_found  s!    *8* 54 5
 	
r"   r  )r4   r   r   r~   r}   r  )r%   r2  rK   r>   r   packets   `     r#   r   r     sr    
 h'HB599b!%))R(K;%[$'F69%>>r"   c                 v    | t         v r	t         |    S |st        d|  d      t        |       }t        |      }|S )NzCould not find custom op "z5". Did you register it via the torch._custom_ops API?)rS   rY   r   r/  )r%   also_check_torch_libraryoverloadrQ   s       r#   _find_custom_opr7    sQ    ?"x((#(
 3) *
 	
 hH$X.FMr"   c                     | t         j                  j                  j                  vry t         j                  j                  j                  |    }|y |j	                  d      sy |j                  d      j                  S )Nr   )r   
_custom_oprA   rS   rj   rz   rJ   )r%   r   s     r#   get_abstract_implr:    sl    u'',,<<<  %%55h?Iz*z*///r"   c           	      4   | j                  d      \  }}| | }t        j                  |      }t        |       |r%t        j
                  j                  j                  gng }t        j                  |d      }|j                  ||       t        ||j                        }	t        |||||	d      }
|
j                          t        j
                  j                  |	t!        j"                  t$        t'        j(                  |
                   t+        |       S )NrV   r,   )tagsTr-   )r   r   r7   r8   r   rE   Tagneeds_fixed_stride_orderr:   r;   r<   r=   r>   r   rg   rF   rG   rH   rI   rC   rD   r   )r%   rL   r>  rK   r>   rM   rN   r<  rO   rP   rQ   s              r#   _custom_op_with_schemar?    s    ~~d#HB6&"J$**:6OO$6NEHHLL112TVD
//"j
)CJJzJ%%b/*>*>?Hc2hPTUF
002	HH00)##$97==;PQ (r"   rw   )F)T)8dataclassesrG   r0   rm   r   r   rC   r   torch._CrE   torch._library.infer_schematorch.libraryr:   r   r   torchgen.modelr   r   r   r   r	   r
   r   r   r   __all__r   r   r$   r   Optionalr   r   rS   dictr   r   	dataclassrq   r=   r5   r8   tupler4   r   r   r   r  r9   AnyrI   r/  r   r7  r:  r?  r!   r"   r#   <module>rK     s      
      "  4 !  M
 /    :>99"(//#"69__9D *,c:o& +T Tn
   
3 | 

3 

4 


N 
t 
"S U38_ "
c 
d 
7,,  >9>9"(//>9	>9BVZZ c d <E(
0r"   