U
    ªÊ¹jº  ã                   @   sŒ   U d dl mZ ddlmZ ddlZddlZddlZddddgZeƒ Z	ee
 ed	< d
gZdd„ ZG dd„ dƒZdd„ Zddd„Zddd„ZdS )é   )Ú
OpOverloadé    )ÚSetNÚLibraryÚimplÚdefineÚfallthrough_kernelÚ_implsZprimc                   C   s   t dƒ‚dS )zZ
    A dummy function to pass to ``Library.impl`` in order to register a fallthrough.
    z,fallthrough_kernel() should never be called.N)ÚNotImplementedError© r   r   úL/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/library.pyr      s    c                   @   s6   e Zd ZdZddd„Zdd„ Zddd„Zdd	d
„ZdS )r   aC  
    A class to create libraries that can be used to register new operators or
    override operators in existing libraries from Python.
    A user can optionally pass in a dispatch keyname if they only want to register
    kernels corresponding to only one specific dispatch key.

    To create a library to override operators in an existing library (with name ns), set the kind to "IMPL".
    To create a new library (with name ns) to register new operators, set the kind to "DEF".
    To create a fragment of a possibly existing library to register operators (and bypass
    the limitation that there is only one library for a given namespace), set the kind to
    "FRAGMENT".

    Args:
        ns: library name
        kind: "DEF", "IMPL" (default: "IMPL"), "FRAGMENT"
        dispatch_key: PyTorch dispatch key (default: "")
    Ú c                 C   s˜   |dkrt d|ƒ‚|tkr4|dks*|dkr4t |dƒ‚tjdd�d }|j|j }}tj |||||¡| _	|| _
tƒ | _|| _|| _t | tt| j¡ d S )	N)ZIMPLÚDEFÚFRAGMENTzUnsupported kind: r   r   zJ is a reserved namespace. Please try creating a library with another name.é   )Úlimitr   )Ú
ValueErrorÚ_reserved_namespacesÚ	tracebackÚextract_stackÚfilenameÚlinenoÚtorchÚ_CZ_dispatch_libraryÚmÚnsÚsetÚ	_op_implsÚkindÚdispatch_keyÚweakrefÚfinalizeÚ_del_libraryr	   )Úselfr   r   r   Úframer   r   r   r   r   Ú__init__*   s    

zLibrary.__init__c                 C   s   d| j › d| j› d| j› d�S )NzLibrary(kind=z, ns=z, dispatch_key=z)>)r   r   r   )r#   r   r   r   Ú__repr__>   s    zLibrary.__repr__c                 C   s$   |dkrt d|› �ƒ‚| j ||¡S )az  Defines a new operator and its semantics in the ns namespace.

        Args:
            schema: function schema to define a new operator.
            alias_analysis (optional): Indicates if the aliasing properties of the operator arguments can be
                                       inferred from the schema (default behavior) or not ("CONSERVATIVE").
        Returns:
            name of the operator as inferred from the schema.

        Example::
            >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_LIBRARY)
            >>> my_lib = Library("foo", "DEF")
            >>> my_lib.define("sum(Tensor self) -> Tensor")
        )r   ZFROM_SCHEMAZCONSERVATIVEzInvalid alias_analysis type )ÚRuntimeErrorr   r   )r#   ÚschemaÚalias_analysisr   r   r   r   A   s    zLibrary.definec                 C   s0  t |ƒstdt|ƒ› �ƒ‚|dkr(| j}t|tƒr8|}n8t|tƒrh|jj}|jj	}|dkrp|d | }nt
dƒ‚| jd | d¡d  d | }|tkr¶t
d | d¡d || j¡ƒ‚|d	krø|}d|krÚ| j› d|› �}tj |d
¡røt
d|› d�ƒ‚| j ||dk�r|nd
|¡ t |¡ | j |¡ dS )aê  Registers the function implementation for an operator defined in the library.

        Args:
            op_name: operator name (along with the overload) or OpOverload object.
            fn: function that's the operator implementation for the input dispatch key or :func:`~fallthrough_kernel`
                to register a fallthrough.
            dispatch_key: dispatch key that the input function should be registered for. By default, it uses
                          the dispatch key that the library was created with.

        Example::
            >>> my_lib = Library("aten", "IMPL")
            >>> def div_cpu(self, other):
            >>>     return self * (1 / other)
            >>> my_lib.impl("div.Tensor", div_cpu, "CPU")
        z;Input function is required to be a callable but found type r   Ú.zQimpl should be passed either a name or an OpOverload object as the first argumentú/z::éÿÿÿÿzˆThis is not allowed since there's already a kernel registered from python overriding {}'s behavior for {} dispatch key and {} namespace.ZMetaZCompositeImplicitAutogradz?We should not register a meta kernel directly to the operator 'zº', because it has a CompositeImplicitAutograd kernel in core. Instead we should let the operator decompose, and ensure that we have meta kernels for the base ops that it decomposes into.N)ÚcallableÚ	TypeErrorÚtyper   Ú
isinstanceÚstrr   Z_schemaÚnameÚoverload_namer'   r   Úsplitr	   Úformatr   r   Z%_dispatch_has_kernel_for_dispatch_keyr   r   Úaddr   )r#   Zop_nameÚfnr   r2   r3   ÚkeyZdispatcher_op_namer   r   r   r   V   s<    

   þ
ÿ
zLibrary.implN)r   )r   )r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r&   r   r   r   r   r   r   r      s
   

c                 C   s   | |8 } d S ©Nr   )Zcaptured_implsZop_implsr   r   r   r"   “   s    r"   r   c                    s   ‡ ‡‡fdd„}|S )Nc                    s   ˆ  ˆ| ˆ ¡ | S r=   )r   )Úf©r   Úlibr2   r   r   Úwrapš   s    zimpl.<locals>.wrapr   )r@   r2   r   rA   r   r?   r   r   ™   s    c                    s   ‡ ‡‡fdd„}|S )Nc                    s   ˆ  ˆˆ ¡}ˆ || ¡ | S r=   )r   r   )r>   r2   ©r)   r@   r(   r   r   rA   ¡   s    zdefine.<locals>.wrapr   )r@   r(   r)   rA   r   rB   r   r       s    )r   )r   )Z_opsr   Útypingr   r   r   r    Ú__all__r   r	   r1   Ú__annotations__r   r   r   r"   r   r   r   r   r   r   Ú<module>   s    {
