o
    !ôjÌ¤  ã                   @   s  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mZmZm	Z	m
Z
mZmZ d dlZd dlm  mZ d dlmZ d dlmZ d dlmZ eedƒoQeedƒZe jdd	„ ƒZG d
d„ dƒZejjZejj Z dede fdd„Z!i Z"e	e#df e$d< e j%e j&e j'e j(e j)e j*gZ+G dd„ deƒZ,dd„ Z-dd„ Z.dd„ Z/dd„ Z0G dd„ dƒZ1e1ƒ a2dd„ Z3dd „ Z4d!d"„ Z5d#d$„ Z6d%d&„ Z7d'd(„ Z8d)d*„ Z9e:ƒ a;e
d+ e$d,< d-d.„ Z<d/d0„ Z=d1d2„ Z>G d3d+„ d+eƒZ?G d4d5„ d5ƒZ@G d6d7„ d7ejAƒZBG d8d9„ d9eBƒZCG d:d;„ d;ejAƒZDeDƒ ZEdS )<é    N)ÚAnyÚCallableÚDictÚSetÚTypeÚUnion)Ú_utils_internal)Údispatch_functorch)ÚTorchDispatchModeÚgetdlopenflagsÚsetdlopenflagsc               	   c   sL   � t sdV  dS t ¡ } t | tjB ¡ zdV  W t | ¡ dS t | ¡ w )z�
    Context manager to set the RTLD_GLOBAL dynamic linker flag while we open a
    shared library to load custom operators.
    N)Ú_SET_GLOBAL_FLAGSÚsysr   r   ÚctypesÚRTLD_GLOBAL)Ú	old_flags© r   ú]/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/torch/_ops.pyÚdl_open_guard   s   €r   c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚOperatorBasez·
    Base class for OpOverload (which represents C++ ATen operators) and HigherOrderOperator
    (which represents Python-only operators that are unrepresentable in TorchScript).
    c                 C   s   i | _ i | _i | _i | _d S ©N)Ú_dispatch_cacheÚ
py_kernelsÚpython_key_mode_tableÚfunctorch_table©Úselfr   r   r   Ú__init__+   s   þ
þ
zOperatorBase.__init__c                 O   ó   t ƒ ‚r   ©ÚNotImplementedError)r   ÚargsÚkwargsr   r   r   Ú__call__W   ó   zOperatorBase.__call__c                 C   s
   || j v S r   )r   ©r   Úkr   r   r   Úhas_kernel_for_dispatch_keyZ   ó   
z(OperatorBase.has_kernel_for_dispatch_keyc                 C   s,   | j D ]}tj |¡s| |¡r dS qdS )NTF)r   ÚtorchÚ_CÚ_dispatch_is_alias_keyÚhas)r   Úksr&   r   r   r   Úhas_kernel_for_any_dispatch_key]   s
   
€z,OperatorBase.has_kernel_for_any_dispatch_keyc                    s   ‡ ‡fdd„}|S )Nc                    sÈ   t  ˆ ¡rtˆ tƒrˆ ˆjvsJ ‚| ˆjˆ < ˆj ¡  | S tˆ tj	j
jƒr3ˆ ˆjvs,J ‚| ˆjˆ < | S tˆ tj	jƒs<J ‚ˆ tj	jjksGJ dƒ‚ˆ ˆjv rXtdˆ › dˆ ¡ › �ƒ‚| ˆjˆ < ˆj ¡  | S )NzGPlease register a mode for the torch._C.DispatchKey.Python key instead.z%Trying to override a python impl for z on operator )ÚinspectÚisclassÚ
issubclassr
   r   r   ÚclearÚ
isinstancer)   r*   Ú
_functorchÚTransformTyper   ÚDispatchKeyÚPythonr   ÚRuntimeErrorÚname)Úfn©r&   r   r   r   Úinnerd   s(   


ÿ
ÿ

z#OperatorBase.py_impl.<locals>.innerr   )r   r&   r<   r   r;   r   Úpy_implc   s   zOperatorBase.py_implc                    s€   ddl m‰ m‰m‰ ‡ ‡fdd„}‡‡fdd„}‡‡fdd„}|  tjjj¡|ƒ |  tj	j
j¡|ƒ |  tjjjj¡|ƒ ˆS )	Nr   )ÚCppFunctionalizeAPIÚFunctorchFunctionalizeAPIÚPythonFunctionalizeAPIc                     s   ˆˆ ƒ g| ¢R i |¤ŽS r   r   ©r!   r"   )Ú_CppFunctionalizeAPIr:   r   r   Úfunctionalize_dk_fn”   s   z?OperatorBase.py_functionalize_impl.<locals>.functionalize_dk_fnc                    ó   ˆˆ | ƒg|¢R i |¤ŽS r   r   )Úmoder!   r"   )Ú_PythonFunctionalizeAPIr:   r   r   Úfunctionalize_dispatch_mode_fn—   ó   zJOperatorBase.py_functionalize_impl.<locals>.functionalize_dispatch_mode_fnc                    rD   r   r   )Úinterpreterr!   r"   )Ú_FunctorchFunctionalizeAPIr:   r   r   Úfunctionalize_functorch_fnš   rH   zFOperatorBase.py_functionalize_impl.<locals>.functionalize_functorch_fn)Ú#torch._subclasses.functional_tensorr>   r?   r@   r=   r)   r*   r6   ÚFunctionalizeZ_subclassesZfunctional_tensorÚFunctionalTensorModer4   r5   )r   r:   rC   rG   rK   r   )rB   rJ   rF   r:   r   Úpy_functionalize_impl‹   s   ÿÿz"OperatorBase.py_functionalize_implc                 C   r   r   r   r   r   r   r   r9   §   r$   zOperatorBase.nameN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r'   r.   r=   rO   r9   r   r   r   r   r   %   s    ,(r   Úopr&   c                 C   sT  |   |¡r|S tj}|tjkst||ƒr|   |¡r|S tj}|tjks(t||ƒr/|   |¡r/|S |  tj 	|¡¡p=|   tj¡}tj
}|tjkrTt||ƒrT|   |¡rT|sT|S tj}|tjksat||ƒrz|   |¡rz|tjkrv|  tjj¡rvtdƒ‚|sz|S tj}t||ƒr‰|   |¡r‰|S tj}t||ƒr˜|   |¡r˜|S tj |¡r |S td| › d|› �ƒ‚)Nzambiguous autogradother kernelzcould not find kernel for ú at dispatch key )r'   r6   Ú&CompositeExplicitAutogradNonFunctionalÚ	UndefinedÚis_included_in_aliasÚCompositeExplicitAutogradr.   r)   r*   Ú*_dispatch_get_backend_keyset_from_autogradÚ%CompositeImplicitAutogradNestedTensorÚCompositeImplicitAutogradÚAutogradOtherÚ _dispatch_autogradother_backendsr8   ÚAutogradÚFuncTorchBatchedDecompositionÚ_dispatch_has_backend_fallbackr    )rT   r&   ZcandZhas_backend_kernelr   r   r   Úresolve_key±   sV   
ÿÿ
ÿ
þÿþÿÿrb   ÚHigherOrderOperatorÚ_higher_order_opsc                       s\   e Zd Z‡ fdd„Z‡ fdd„Zedd„ ƒZdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Z‡  ZS )rc   c                    s’   t ƒ  ¡  |ˆ _|ˆ _ˆ t|< dˆ _ˆ jtu r'ˆ jrdˆ j nd}ˆ j	| ˆ _	t
j ¡ ˆ _tD ]}ˆ  |¡ q/‡ fdd„}ˆ  t
jjj¡|ƒ d S )NÚhigher_orderÚ.Ú c                     sV   t ƒ dkr'tj tj tj¡¡� ˆ | i |¤ŽW  d   ƒ S 1 s"w   Y  tdƒ‚)Nr   z¢
                Can't directly invoke HOP implementation at PreDispatch key
                if there are active modes on PreDispatch mode stack.
                )Ú&_len_torch_dispatch_stack_pre_dispatchr)   r*   Ú_ExcludeDispatchKeyGuardÚDispatchKeySetr6   ÚPreDispatchÚAssertionErrorrA   r   r   r   Ú_  s   
ÿ ýÿz'HigherOrderOperator.__init__.<locals>._)Úsuperr   Ú_namerP   rd   Ú_nsÚ	__class__rc   Ú	namespacerQ   r)   r*   Ú_dispatch_keyset_fullÚnon_fallthrough_keysÚ2_HIGHER_ORDER_OP_DEFAULT_FALLTHROUGH_DISPATCH_KEYSÚfallthroughr=   r6   rk   )r   r9   Zself_name_spaceÚdispatch_keyrm   ©rq   r   r   r   ø   s   


zHigherOrderOperator.__init__c                    s4   t |tjjƒr| j |¡s| j |¡| _tƒ  |¡S r   )	r3   r)   r*   r6   rt   r,   Úaddrn   r=   r%   rx   r   r   r=   $  s   zHigherOrderOperator.py_implc                 C   ó   | j S r   )rp   r   r   r   r   rr   )  ó   zHigherOrderOperator.namespacec                 C   s   | j  |¡| _ d S r   )rt   Úremove)r   rw   r   r   r   rv   -  ó   zHigherOrderOperator.fallthroughc                 O   s  ddl m} || jv r | j| }t|tjjƒrJ ‚||i |¤ŽS |tjjjkr-t| ||ƒS |tjjj	kryddl m
} |ƒ }|d usEJ dƒ‚t|ƒ| jv sTJ d|› d�ƒ‚| jt|ƒ }|ƒ �}	||	g|¢R i |¤ŽW  d   ƒ S 1 stw   Y  tj |¡}
|
tjjjkrØddl m
} tƒ dkrØtj tj	¡sØtƒ }|d us£J dƒ‚t|ƒ| jv s²J d|› d�ƒ‚| jt|ƒ }||
ƒ�}	||	g|¢R i |¤ŽW  d   ƒ S 1 sÓw   Y  t| |ƒ}|| jvrñtd| j› d	|› d
|› d�ƒ‚| j| | j|< | j| }t|tjjƒ�rJ ‚||i |¤ŽS )Nr   ©Ú_get_current_dispatch_mode)Ú_pop_mode_temporarilyúMIllegal invocation of dispatch on torch._C.DispatchKey.Python without a mode.zCurrent active mode z not registeredzRIllegal invocation of dispatch on torch._C.DispatchKey.PreDispatch without a mode.z.could not find kernel for HigherOrderOperator rU   z (resolved from ú))Útorch.utils._python_dispatchr   r   r3   r)   r*   r6   ÚFuncTorchDynamicLayerFrontModer	   r7   r€   Útyper   Ú_to_functionality_keyrk   rh   Ú&_dispatch_tls_is_dispatch_key_excludedÚ'_get_current_dispatch_mode_pre_dispatchrb   r   r    ro   )r   rw   r!   r"   r   Úkernelr€   Ú	curr_modeÚhandlerrE   Úfunctionality_keyÚ	final_keyr   r   r   Údispatch0  sh   


ÿ
ÿ ÿ
ÿÿ
ÿ
ÿ
 ÿ


ÿÿÿ
zHigherOrderOperator.dispatchc                    s0   dd l ‰ddl m} |‡ ‡‡‡fdd„ƒ}|ƒ S )Nr   )Údisablec                     s\   t ˆ ˆƒ} ˆj | ¡rˆjjˆ| gˆ ¢R i ˆ¤ŽS tˆ ˆˆjƒ}ˆj| ¡ gˆ ¢R i ˆ¤ŽS r   )Ú_to_flat_tupleÚ	overridesÚhas_torch_functionÚhandle_torch_functionÚ_compute_keysetrt   rŽ   ÚhighestPriorityTypeId)Ú	flat_argsZdispatch_key_set©r!   r"   r   r)   r   r   Úwrapperv  s"   
ÿÿÿÿÿÿz-HigherOrderOperator.__call__.<locals>.wrapper)Útorch._dynamor�   )r   r!   r"   r�   r˜   r   r—   r   r#   p  s
   zHigherOrderOperator.__call__c                 C   s
   |   ¡ › S r   )r9   r   r   r   r   Ú__str__…  r(   zHigherOrderOperator.__str__c                 C   rz   r   ©ro   r   r   r   r   r9   ˆ  r$   zHigherOrderOperator.name)rP   rQ   rR   r   r=   Úpropertyrr   rv   rŽ   r#   rš   r9   Ú__classcell__r   r   rx   r   rc   ò   s    ,
@c                 C   s   t j| i |¤ŽS r   )ÚpytreeÚarg_tree_leavesrA   r   r   r   r�   Œ  ó   r�   c                 C   s   t | |ƒ}t||ƒS r   )Ú_get_tensorsÚkey_extractor)r!   r"   rt   Útensorsr   r   r   r”   �  s   

r”   c                 C   s    t | |ƒ}dd„ |D ƒ}t|ƒS )Nc                 S   s   g | ]
}t |tjƒr|‘qS r   )r3   r)   ÚTensor)Ú.0Útr   r   r   Ú
<listcomp>—  s    z _get_tensors.<locals>.<listcomp>)r�   Útuple)r!   r"   Úflat_allZtensor_argsr   r   r   r¡   •  s   
r¡   c                 C   s>   t j ¡ }| D ]
}|t j |¡B }q|t j ¡  }||@ }|S r   )r)   r*   Ú_dispatch_tls_local_include_setÚ_dispatch_keysÚ_dispatch_tls_local_exclude_set)r£   Zkey_maskZkey_setÚtensorr   r   r   r¢   �  s   
r¢   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
Ú_ModeStackStateForPreDispatchc                 C   s   d d g| _ d S r   )Ú*_ModeStackStateForPreDispatch__infra_modesr   r   r   r   r   ­  s   z&_ModeStackStateForPreDispatch.__init__c                 C   s    |t | jƒk s	J ‚|| j|< d S r   ©Úlenr¯   )r   ÚindexrE   r   r   r   Úset°  s   z!_ModeStackStateForPreDispatch.setc                 C   s   |t | jƒk s	J ‚| j| S r   r°   )r   r²   r   r   r   Úget´  s   
z!_ModeStackStateForPreDispatch.getc                 C   s   t dd„ | jD ƒƒS )Nc                 S   s   g | ]}|d ur|‘qS r   r   )r¥   Úir   r   r   r§   ¹  ó    z7_ModeStackStateForPreDispatch.count.<locals>.<listcomp>r°   r   r   r   r   Úcount¸  s   z#_ModeStackStateForPreDispatch.countN)rP   rQ   rR   r   r³   r´   r·   r   r   r   r   r®   ¬  s
    r®   c                 C   sh   t ƒ }| tjjjtjjjfv sJ ‚| tjjjkr&| d¡}t ƒ  dd ¡ |S | d¡}t ƒ  dd ¡ |S ©Nr   é   )Ú!mode_stack_state_for_pre_dispatchr)   r*   Ú_TorchDispatchModeKeyÚPROXYÚ
FUNCTIONALr´   r³   )Úmode_keyZcurrent_mode_stack_pre_dispatchÚcurrent_moder   r   r   Úunset_mode_pre_dispatch¿  s   
þ

rÀ   c                 C   sˆ   ddl m} ddlm} t| ||fƒsJ ‚t| |ƒr/tƒ  d¡}|d u s&J ‚tƒ  d| ¡ d S tƒ  d¡}|d u s;J ‚tƒ  d| ¡ d S )Nr   )rN   )ÚProxyTorchDispatchModer¹   )rL   rN   Ú"torch.fx.experimental.proxy_tensorrÁ   r3   rº   r´   r³   )rE   rN   rÁ   r¿   r   r   r   Ú_set_mode_pre_dispatchÏ  s   
rÃ   c                  C   s^   t ƒ } |  d¡d ur|  d¡}|  dd ¡ |S |  d¡d ur+|  d¡}|  dd ¡ |S tdƒ‚)Nr¹   r   zTrying to pop empty mode stack)rº   r´   r³   rl   )Z
mode_stackÚresr   r   r   Ú_pop_mode_from_pre_dispatchß  s   

rÅ   c                   C   s
   t ƒ  ¡ S r   )rº   r·   r   r   r   r   rh   î  r(   rh   c                 C   sB   | t jjjt jjjfv sJ ‚| t jjjkrtƒ  d¡S tƒ  d¡S r¸   )r)   r*   r»   r¼   r½   rº   r´   )r¾   r   r   r   Ú_get_dispatch_mode_pre_dispatchò  s   
þrÆ   c                  C   sR   t ƒ  ¡ } | dkrt ƒ  d¡S | dkr't ƒ  d¡d ur!t ƒ  d¡S t ƒ  d¡S d S )Né   r¹   r   )rº   r·   r´   )Ú	stack_lenr   r   r   rˆ   ü  s   

ÿÿ
ýrˆ   c                   C   ó   t S r   )Ú"_mode_stack_state_for_pre_dispatchr   r   r   r   rº   	  ó   rº   Ú
OpOverloadÚ
cached_opsc                 C   s   t  | ¡ d S r   )rÍ   ry   )Zop_overloadr   r   r   Úadd_cached_op  s   rÎ   c                   C   s   t  ¡  d S r   )rÍ   r2   r   r   r   r   Úreset_cached_ops  s   rÏ   c                   C   rÉ   r   )rÍ   r   r   r   r   Úget_cached_ops  rË   rÐ   c                       s¶   e Zd Z‡ fdd„Zd$dd„Zdd„ Zdd	„ Zd
d„ Zdd„ Z‡ fdd„Z	‡ fdd„Z
edd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZed d!„ ƒZed"d#„ ƒZ‡  ZS )%rÌ   c                    s   t ƒ  ¡  || _|| _|| _|| _|| _|jdkrdn|j| _| jj	| _
|jr1|  j
d|j 7  _
| jj	 d¡d › d| j› �| _|j| _|j|_| j
| _i | _| jtjjv | _d }| jjD ]}|jd u rfq^|d u ro|jj}q^|jjpt|}q^|d uo|| | _d S )Nrg   Údefaultrf   ú::r¹   )rn   r   Ú_opÚ_op_dkÚ_schemaÚ_overloadpacketÚ_tagsÚoverload_nameÚ_overloadnamer9   ro   ÚsplitrP   rQ   rR   Ú__annotations__r)   ÚlibraryZ_defsZ_defined_in_pythonÚ	argumentsÚ
alias_infoÚis_writeÚis_view)r   ÚoverloadpacketrT   Zop_dkÚschemaÚtagsrß   Úarx   r   r   r   #  s2   
ÿ
 

zOpOverload.__init__Nc                 C   ó   | S r   r   ©r   Úmemor   r   r   Ú__deepcopy__G  ó   zOpOverload.__deepcopy__c                 C   ó    dj g | jj d¡¢| j‘R Ž S )Nz'<OpOverload(op='{}.{}', overload='{}')>rÒ   ©ÚformatrÕ   r9   rÚ   rÙ   r   r   r   r   Ú__repr__J  s
   ÿÿzOpOverload.__repr__c                 O   s   | j |i |¤ŽS r   ©rÓ   ©Zself_r!   r"   r   r   r   r#   O  s   zOpOverload.__call__c                 C   ó
   t | jƒS r   ©ÚhashrÓ   r   r   r   r   Ú__hash__T  r(   zOpOverload.__hash__c                 C   rê   )Nz{}.{}.{}rÒ   rë   r   r   r   r   rš   X  s    zOpOverload.__str__c                    s   t ƒ  |¡ptj |  ¡ |¡S r   )rn   r'   r)   r*   Ú%_dispatch_has_kernel_for_dispatch_keyr9   r%   rx   r   r   r'   [  s
   ÿþz&OpOverload.has_kernel_for_dispatch_keyc                    s   t j |  ¡ |¡ptƒ  |¡S r   )r)   r*   Ú)_dispatch_has_kernel_for_any_dispatch_keyr9   rn   r.   )r   r-   rx   r   r   r.   `  s
   ÿ
þz*OpOverload.has_kernel_for_any_dispatch_keyc                 C   s   | j j d¡d S )NrÒ   r   )rÕ   r9   rÚ   r   r   r   r   rr   e  s   zOpOverload.namespacec                 C   s   t j | jj| jj¡S r   )r)   r*   Ú_dispatch_find_schema_or_throwrÕ   r9   rØ   r   r   r   r   Ú_handlei  s   ÿzOpOverload._handlec                 O   sV   t jjj}|| jv r| j| |i |¤ŽS t j |  ¡ |¡r)| j|g|¢R i |¤ŽS tS r   )	r)   r*   r6   r\   r   rô   r9   rÔ   ÚNotImplemented)r   r!   r"   Zdkr   r   r   Ú	decomposen  s   

zOpOverload.decomposec                 C   s   | j  |d ¡ d S r   )r   Úpop)r   Úkeyr   r   r   Ú_uncache_dispatchƒ  r}   zOpOverload._uncache_dispatchc           	         s<  ˆ ˆj vsJ ˆ› dˆ › �ƒ‚ˆ tjjjkr5ˆjs#ˆ ˆj ˆ < tˆƒ ˆ S ‡ ‡fdd„}|ˆj ˆ < tˆƒ |S tj ˆ ¡}|tjjjkrXt	ƒ }|dkrXtj 
tj¡sX‡fdd„}|S tˆˆ ƒ}ˆ tjjjk}ˆ tjjjkrŠdd lm  m} |jrŠ| ˆ|¡}|rˆ|ˆj ˆ < tˆƒ |S ˆj ||¡}|rœ|ˆj ˆ < tˆƒ |S )Nú c                     s\   ddl m} t|ƒ ƒ}|d usJ dƒ‚|ˆjvr$ˆjˆ g| ¢R i |¤ŽS ˆj| | i |¤ŽS )Nr   r~   r�   )rƒ   r   r…   r   rÔ   )r!   r"   r   rŠ   ©rû   r   r   r   r‹   ‘  s   

ÿ
z)OpOverload._get_dispatch.<locals>.handlerr   c                     s°   t jdd„ ƒ}|ƒ �D}t|tƒsJ ‚g }tjj | | ¡ f¡\}}|D ]}t|tj	ƒr>tj
 |¡ tj
jj¡r>| t|ƒ¡ q#| ˆ || |¡W  d   ƒ S 1 sQw   Y  d S )Nc                  s   s(   � t ƒ } z
| V  W t| ƒ d S t| ƒ w r   )rÅ   rÃ   )Ztop_moder   r   r   Ú(_temporarily_pop_modes_from_pre_dispatch²  s
   €z[OpOverload._get_dispatch.<locals>.handler.<locals>._temporarily_pop_modes_from_pre_dispatch)Ú
contextlibÚcontextmanagerr3   r
   r)   ÚutilsÚ_pytreeÚtree_flattenÚvaluesr¤   r*   r«   r,   r6   r7   Úappendr…   Ú__torch_dispatch__)r!   r"   rÿ   rŠ   Zoverload_typesZargs_flattenedrm   rä   r   r   r   r‹   ±  s(   

ÿÿþ€ÿ$ï)r   r)   r*   r6   r7   r   rÎ   r†   rk   rh   r‡   rb   rM   Ztorch._dispatch.pythonÚ	_dispatchÚpythonZCROSSREF_FUNCTIONALIZEZmake_crossref_functionalizer   r´   )	r   rû   r‹   rŒ   Zcurr_stack_lenr�   Zcache_resultZ
pydispatchÚrr   rþ   r   Ú_get_dispatch‡  sF   

ÿÿ#


zOpOverload._get_dispatchc                 C   rz   r   r›   r   r   r   r   r9   ï  r$   zOpOverload.namec                 C   rz   r   )rÖ   r   r   r   r   rá   ò  r{   zOpOverload.overloadpacketc                 C   rz   r   rî   r   r   r   r   rT   ö  r{   zOpOverload.opc                 C   rz   r   )r×   r   r   r   r   rã   ú  r{   zOpOverload.tagsr   )rP   rQ   rR   r   rè   rí   r#   ró   rš   r'   r.   rœ   rr   r÷   rù   rü   r  r9   rá   rT   rã   r�   r   r   rx   r   rÌ   "  s,    
$
h

c                   @   sb   e Zd Zdd„ Zddd„Zdd„ Zdd	„ Zd
d„ Zedd„ ƒZ	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚOpOverloadPacketc                 C   s"   || _ || _|| _|| _g | _d S r   )Ú_qualified_op_namerP   rÓ   Ú_overload_namesÚ_dir)r   Úqualified_op_nameÚop_namerT   Úoverload_namesr   r   r   r     s
   
zOpOverloadPacket.__init__Nc                 C   rå   r   r   ræ   r   r   r   rè     ré   zOpOverloadPacket.__deepcopy__c                 C   ó   dj | j d¡Ž S )Nz<OpOverloadPacket(op='{}.{}')>rÒ   ©rì   r  rÚ   r   r   r   r   rí     s   
ÿzOpOverloadPacket.__repr__c                 C   rð   r   rñ   r   r   r   r   ró     r(   zOpOverloadPacket.__hash__c                 C   r  )Nz{}.{}rÒ   r  r   r   r   r   rš     r}   zOpOverloadPacket.__str__c                 C   rz   r   rî   r   r   r   r   rT     r{   zOpOverloadPacket.opc                 C   sð   |dkrdS z|  d¡rt| j|ƒW S W n ty/   tdt| ƒ› dt| jƒ› d|› d�ƒd ‚w z2|dkr7d	n|}tj | j|¡\}}}tj 	| j|¡}t
| ||||ƒ}t| ||ƒ | j |¡ |W S  tyw   td
t| ƒ› d|› d�ƒd ‚w )NÚ__file__ú	torch.opsÚ__ú'zH' can't have an overload name beginning with '__' and the underlying op z has no attribute z either.rÑ   rg   zThe underlying op of 'z' has no overload name ')Ú
startswithÚgetattrrÓ   ÚAttributeErrorÚstrr)   r*   Ú_get_operation_overloadr  Ú_get_schemarÌ   Úsetattrr  r  r8   )r   rû   Zuse_keyZop_Zop_dk_rã   râ   Úoverloadr   r   r   Ú__getattr__   sD   	
ÿÿÿÿýû

ÿÿþÿzOpOverloadPacket.__getattr__c                 C   rð   r   ©Úiterr  r   r   r   r   Ú__iter__K  r(   zOpOverloadPacket.__iter__c                 O   s   | j |i |pi ¤ŽS r   rî   rï   r   r   r   r#   N  s   zOpOverloadPacket.__call__c                 C   s   dd„ | j D ƒS )Nc                 S   s   g | ]}|r|nd ‘qS )rÑ   r   )r¥   Únr   r   r   r§   Z  r¶   z.OpOverloadPacket.overloads.<locals>.<listcomp>)r  r   r   r   r   Ú	overloadsY  r    zOpOverloadPacket.overloadsr   )rP   rQ   rR   r   rè   rí   ró   rš   rœ   rT   r!  r$  r#   r&  r   r   r   r   r    s    


+r  c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )Ú_OpNamespacea0  
    An op namespace to dynamically bind Operators into Python.

    Say a user has created a custom Operator called "my_namespace::my_op". To
    call this op, the user will write torch.ops.my_namespace.my_op(...).
    At startup, this operation will not yet be bound into Python. Instead, the
    following sequence of magic tricks will occur:
    1. `torch.ops.my_namespace` will invoke the `__getattr__` magic method
       on the `torch.ops` object, which will create a new `_OpNamespace`
       object called `my_namespace` and set it as an attribute on the `ops`
       object.
    2. `torch.ops.my_namespace.my_op` will then invoke `__getattr__` on
       the `my_namespace` object, which will retrieve the operation via
       `torch.get_operation`, a function bound from C++, and then in a similar
       fashion bind this new object onto the `my_namespace` object.
    3. `torch.ops.my_namespace.my_op(...)` then calls this new operation
        and subsequent accesses will incur no further lookup (the namespace and
        operation will already exist).
    c                    s    t ƒ  d| ¡ || _g | _d S )Nz
torch.ops.)rn   r   r9   r  )r   r9   rx   r   r   r   �  s   
z_OpNamespace.__init__c                 C   rð   r   r"  r   r   r   r   r$  †  r(   z_OpNamespace.__iter__c              
   C   s  |dkrdS |dv rt d|› d| j› d�ƒ‚| j}|› d|› �}ztj |¡\}}|d u r9t d| j› d	|› d�ƒ‚W n tyS } zt d| j› d	|› d�ƒ|‚d }~ww tjj ||¡ | j	d
 | |_	t
||||ƒ}| j	d
 | |_	t| ||ƒ | j |¡ |S )Nr  r  )Ú
__origin__Ú__self__zInvalid attribute 'z' for '_OpNamespace' 'r  rÒ   z'_OpNamespace' 'ú' object has no attribute 'rf   )r  r9   r)   r*   Ú_jit_get_operationr8   ÚjitÚ	_builtinsZ_register_builtinrQ   r  r  r  r  )r   r  Znamespace_namer  rT   r  ÚeZopoverloadpacketr   r   r   r!  ‰  s@   ÿÿÿÿþ€ý	ÿz_OpNamespace.__getattr__)rP   rQ   rR   rS   r   r$  r!  r�   r   r   rx   r   r'  l  s
    r'  c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )Ú_PyOpNamespacec                    s   t ƒ  |¡ || _d S r   )rn   r   Ú_ops)r   r9   Úopsrx   r   r   r   ³  s   
z_PyOpNamespace.__init__c                 C   s>   | j  |d ¡}|d u rtd| j› d|› d�ƒ‚t| ||ƒ |S )Nz'_PyOpNamespace' 'r*  r  )r0  r´   r  r9   r  )r   r9   rT   r   r   r   r!  ·  s   ÿz_PyOpNamespace.__getattr__)rP   rQ   rR   r   r!  r�   r   r   rx   r   r/  ²  s    r/  c                       s@   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Z‡  Z	S )Ú_Opsz_ops.pyc                    s*   t ƒ  d¡ tƒ | _tdtƒ| _g | _d S )Nr  ztorch.ops.higher_order)rn   r   r³   Úloaded_librariesr/  rd   Ú_higher_order_op_namespacer  r   rx   r   r   r   Å  s   ÿ
z_Ops.__init__c                 C   s2   |dkr| j S t|ƒ}t| ||ƒ | j |¡ |S )Nre   )r4  r'  r  r  r  )r   r9   rr   r   r   r   r!  Í  s   z_Ops.__getattr__c                 C   rð   r   r"  r   r   r   r   r$  Ø  r(   z_Ops.__iter__c                 C   s   t  |¡ dS )a{  
        Imports a Python module that has torch.library registrations.

        Generally, to extend PyTorch with custom operators, a user will
        create a Python module whose import triggers registration of
        the custom operators via a torch.ops.load_library call or a call
        to one or more torch.library.* APIs.

        It is unexpected for Python modules to have side effects, so some
        linters and formatters will complain. Use this API to import Python
        modules that contain these torch.library side effects.

        Args:
            module (str): The name of the Python module to import

        N)Ú	importlibÚimport_module)r   Úmoduler   r   r   r6  Û  s   z_Ops.import_modulec                 C   sV   t  ¡ rdS t |¡}tƒ � t |¡ W d  ƒ n1 sw   Y  | j |¡ dS )a  
        Loads a shared library from the given path into the current process.

        The library being loaded may run global initialization code to register
        custom operators with the PyTorch JIT runtime. This allows dynamically
        loading custom operators. For this, you should compile your operator
        and the static registration code into a shared library object, and then
        call ``torch.ops.load_library('path/to/libcustom.so')`` to load the
        shared object.

        After the library is loaded, it is added to the
        ``torch.ops.loaded_libraries`` attribute, a set that may be inspected
        for the paths of all libraries loaded using this function.

        Args:
            path (str): A path to a shared library to load.
        N)	r)   Ú_running_with_deployr   Úresolve_library_pathr   r   ÚCDLLr3  ry   )r   Úpathr   r   r   Úload_libraryî  s   
üz_Ops.load_library)
rP   rQ   rR   r  r   r!  r$  r6  r<  r�   r   r   rx   r   r2  Â  s    r2  )Fr   r   r5  r/   r   ÚtypesÚtypingr   r   r   r   r   r   Útorch._Cr)   Útorch.utils._pytreer  r  rž   r   Ztorch._functorch.pyfunctorchr	   rƒ   r
   Úhasattrr   r  r   r   r*   Ú_dispatch_is_included_in_aliasrX   r6   rb   rd   r  rÛ   ÚPythonDispatcherÚPythonTLSSnapshotÚADInplaceOrViewÚBackendSelectÚAutocastCPUÚAutocastCUDAru   rc   r�   r”   r¡   r¢   r®   rÊ   rÀ   rÃ   rÅ   rh   rÆ   rˆ   rº   r³   rÍ   rÎ   rÏ   rÐ   rÌ   r  Ú
ModuleTyper'  r/  r2  r1  r   r   r   r   Ú<module>   sl   
  
 5ú
 
 biF
K