
    ǄgHh                        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mZm	Z	m
Z
mZmZ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 d dlZd dl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jD                  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jT                  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)FunctionSchemaOperatorName
SchemaKindBaseTypeListTypeBaseTy)get_ctx   )autograd_kernel_indirectionconstruct_autograd_kernel)infer_schema)	custom_opCustomOpr	   CPUCUDA)cpucuda>   atatenprimprimstorchpytorchc                  8    t        j                  dt               y )Nzwtorch._custom_op is deprecated and will be removed in PyTorch 2.6, please use the equivalent torch.library API instead.)warningswarnDeprecationWarning     ]/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/_custom_op/impl.pywarn_deprecatedr!   -   s    MM	89KM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>.inner;   s   !!$'""&t*/ 
 "(+D2==D &xj 1!!%k$-- A+,  9F8Md4S`vfX&
(..z:($,_dCoob*-

:)"o.B.BC#r?D(TXY-- OO5S&..*5'f(=>	
 	44i''(=w}}V?TU	
 r   )r!   )r"   r#   rO   s   `` r    r   r   3   s     )V 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	_ophandler?   	_qualnamer3   _impls'_registered_autograd_kernel_indirectionrP   )	selfrL   cpp_nsrI   operator_namerM   r+   r;   	__class__s	           r    rU   zCustomOp.__init__w   s    S  M?+%(	5=)" KM7<4*.'r   c                     | j                   rJ | j                  j                  | j                  t	        t        j                  |             d       d| _         y )Nr,   T)r]   rY   r>   r?   r   r@   rA   r^   s    r    %_register_autograd_kernel_indirectionz.CustomOp._register_autograd_kernel_indirection   sA    ????		t||%@tAT%UWab7;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\   locationrV   r[   r-   getframeinfosys	_getframefilenamelinenoFuncAndLocation)r^   kindrG   
stacklevelfunc_and_locationrh   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\   r^   ro   s     r    	_get_implzCustomOp._get_impl   s    {{4  r   c                     || j                   v S ru   rv   rw   s     r    rg   zCustomOp._has_impl   s    t{{""r   c                     | ` t        t        j                  | j                        }t        || j                        rt        || j                         t        | j                  = y ru   )
rY   getattrr   opsrX   hasattrr?   delattrrP   r[   )r^   opnamespaces     r    _destroyzCustomOp._destroy   sE     Ieii6;-K.DNN+r   c                 "    d| j                    dS )Nz<CustomOp(op="z")>)r[   rc   s    r    __repr__zCustomOp.__repr__   s    /s33r   c                 L    t        j                  | j                  g|i |}|S ru   )rB   _dispatch_call_boxedrZ   )r^   argskwargsrN   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 )Nrp   )set_check_doesnt_have_library_implrs   SUPPORTED_DEVICE_TYPE_TO_KEYr7   r>   rY   r?   )fdevice_typedispatch_key_stacklevelr   r^   s      r    rO   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)r^   r   r   r   rO   s   ```  r    r>   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.)rg   r   rB   ._dispatch_has_computed_kernel_for_dispatch_keyr[   rV   )r^   r   keys      r    r   z(CustomOp._check_doesnt_have_library_impl   sd    >>+&*;7<<T^^SQ)+6Ft~~FV WL MN N 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)rs   r7   r>   rY   r?   )r   r^   s    r    rO   z$CustomOp.impl_factory.<locals>.inner   s7    	1-BGLLDLL/B1EHr   r   )r^   rO   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 rV   )rh   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   rh   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_implrs   rx   rh   r[   rD   wrapsrY   r>   r?   )r   r   rh   r"   r   r^   s   ` @@r    rO   z%CustomOp.impl_abstract.<locals>.inner   s}    557
A+F~~j1::H~~H __Q.  .  IINN4<<V<Hr   r   r^   r   rO   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: )rV   r[   )detailrI   r^   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ru   )
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 ))rW   ro   r   
functionalreturnslenanyr   r   r   r   r   r   r   r   r0   listvalues)r^   r   retsis_non_mutating_viewallowed_return_typesretrI   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^ 
 >> 	iCxx//06J6Q6Q6S1T0UU[\_\d\d[eefgh	i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]   rB   %_dispatch_has_kernel_for_dispatch_keyr[   rV   )r^   r   s     r    (_check_doesnt_have_library_autograd_implz1CustomOp._check_doesnt_have_library_autograd_impl7  s    7733DNND_`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.)rg   rB   r   r[   rV   rc   s    r    r   z-CustomOp._check_doesnt_have_library_meta_implO  s    >>*% 44T^^E`a<<T^^VT 33DNND_`3DNN3C D; <= = 33DNNFK3DNN3C DV WX X 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)	rg   r   rW   _output_differentiabilityget_opr[   rx   rG   rs   )r^   kernels     r    _register_autograd_kernelz"CustomOp._register_autograd_kernelx  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]   rd   rs   rg   r   )r   r   r^   s    r    rO   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8 8r   c                     j                          j                          j                  sj                          j	                  d|        _        j                  d      rj                          y y )Nr   r   r   )r   r   r]   rd   rs   r   rg   r   )r   r   r   r^   s    r    rO   z%CustomOp.impl_backward.<locals>.inner  sk    --/99;??::<
A+F-ED*~~12..0 3r   )r   r   r   r   rW   r   )r^   r   r   r   diffrO   s   ```   r    impl_backwardzCustomOp.impl_backward  sp     $/8 6=0 !$-F 4<<''(C0H,II	1 r   )   )Nr   )r3   r<   __qualname__r=   rU   rd   rs   rx   rg   r   r   r   typingUnionr   IterableCallabler>   r   r   r   r   r   r   r   r   r   __classcell__)ra   s   @r    r   r   r   s     Y^ /4<<!#,4 RS"LLfooc.B)BC	*Nfoo !foo !F#iJ[0"XR
0r   c                   6    e Zd ZU ej                  ed<   eed<   y)rn   rG   rh   N)r3   r<   r   r   r   __annotations__r   r   r   r    rn   rn     s    
//Mr   rn   r_   r`   c                     |j                   dn|j                   }t        j                  |  dt        |j                         |      S )N rS   )overload_namerB   _dispatch_find_schema_or_throwr   r;   )r_   r`   r   s      r    r:   r:     sN    ))1}7R7R  ,,("S++,-. r   rH   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. )r/   RESERVED_NS)rH   s    r    r2   r2     sZ    
by!" &# $
 	
 
[!"U2$ /- .
 	
 r   rI   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_schemar/   	argumentsself_arg)rI   s    r    r5   r5     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 )	NrS   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   r/   )r"   namess     r    r1   r1     s~    NN4#E
5zQ?z JL M N 	N
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   r/   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 ru   )ro   r-   	ParameterPOSITIONAL_OR_KEYWORDKEYWORD_ONLY)r   s    r    supported_paramr    s6    :://&&  r   rG   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ru   )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ru   )r   r-   r   emptyr  s      r    r   z3validate_function_matches_schema.<locals>.<genexpr>  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: r/   rI   sigs   r    r   z/validate_function_matches_schema.<locals>.error  )    ! "%%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_args'  r  r   c                    t        |       t        |      k7  r         t        | |      D ]X  \  \  }}}||j                  k7  r         |j                  t        j
                  j                  us|j                  R         Z y ru   )r   zipr;   defaultr-   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   )r-   	signatureall
parametersitemsr/   r   return_annotation	Signaturer
  ro   r   r  r  r   flat_positionalflat_kwarg_only)
rI   rG   r;   r   
positional	kwargonlyr  r   r  r  s
   `      @@@r    r6   r6     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    rF   rF   <  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   }t        j                  |      }t        ||||| d      S )Nr)   rS   Tr*   )	namespacer   r7   r8   r;   r   r   rW   r   r4   r   )oprH   rL   r;   rJ   rI   s         r    custom_op_from_existingr-  Z  s    	B
--

J
/C779??4 $DRZZJ!!$'+J!!*-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_foundf  s!    *8* 54 56 	6r   r  )r1   r}   r   r|   r{   r  )r"   r0  rH   r;   r   packets   `     r    r   r   e  sr    6 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?)rP   rV   r   r-  )r"   also_check_torch_libraryoverloadrN   s       r    _find_custom_opr5  x  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_opr>   rP   rg   rx   rG   )r"   r   s     r    get_abstract_implr8    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 )NrS   r)   )tagsTr*   )r   r   r4   r5   r   rB   Tagneeds_fixed_stride_orderr7   r8   r9   r:   r;   r   rd   rC   rD   rE   rF   r@   rA   r   )r"   rI   r<  rH   r;   rJ   rK   r:  rL   rM   rN   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   ru   )F)T)8dataclassesrD   r-   rj   r   r@   r   torchgen.modelr   r   r   r   r   r   r   torch._CrB   torch.libraryr7   r	   r   r   r   torch._library.infer_schemar   __all__r   r   r!   r   Optionalr   r   rP   Dictr   r   	dataclassrn   r:   r2   r5   Tupler1   r   r   r   r  r6   AnyrF   r-  r   r5  r8  r=  r   r   r    <module>rI     s      
    _ _    ! L " 4
 /   M :>33"(//#"63__3x 13S*_- 2~ ~B
   
3 | 

3 

4 


N 
t 
"
S 
V\\#s(%; 

c 
d 
7,,  >9>9"(//>9	>9BVZZ c d <E&	0r   