3
I4W^×  ã               @   s�   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e	edƒoRe	edƒZ
ejdd„ ƒZG dd„ dejƒZG dd	„ d	ejƒZeƒ ZdS )
é    NÚgetdlopenflagsÚsetdlopenflagsc              c   s4   t rtjƒ } tj| tjB ƒ dV  t r0tj| ƒ dS )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)Z	old_flags© r   ú+/tmp/pip-build-e66j97b2/torch/torch/_ops.pyÚdl_open_guard   s    r
   c                   s(   e Zd ZdZ‡ f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 t| ƒjd| ƒ || _d S )Nz
torch.ops.)Úsuperr   Ú__init__Úname)Úselfr   )Ú	__class__r   r	   r   5   s    z_OpNamespace.__init__c             C   sJ   dj | j|ƒ}tjj|ƒ}tjj||ƒ t| ||ƒ | jd | j |_|S )Nz{}::{}Ú.)	Úformatr   ÚtorchZ_CZ_jit_get_operationZjitZ_register_builtinÚsetattrÚ
__module__)r   Zop_nameZqualified_op_nameÚopr   r   r	   Ú__getattr__9   s    z_OpNamespace.__getattr__)Ú__name__r   Ú__qualname__Ú__doc__r   r   Ú__classcell__r   r   )r   r	   r   !   s   r   c                   sB   e Zd ZejjejjeƒdƒZ‡ fdd„Zdd„ Z	dd„ Z
‡  ZS )Ú_Opsz_ops.pyc                s   t t| ƒjdƒ tƒ | _d S )Nz	torch.ops)r   r   r   ÚsetÚloaded_libraries)r   )r   r   r	   r   I   s    z_Ops.__init__c             C   s   t |ƒ}t| ||ƒ |S )N)r   r   )r   r   Ú	namespacer   r   r	   r   M   s    z_Ops.__getattr__c          	   C   s8   t jj|ƒ}tƒ � tj|ƒ W dQ R X | j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.

        Arguments:
            path (str): A path to a shared library to load.
        N)r   Z_utils_internalZresolve_library_pathr
   r   ÚCDLLr   Úadd)r   Úpathr   r   r	   Úload_libraryS   s    z_Ops.load_library)r   r   r   Úosr"   ÚjoinÚdirnameÚ__file__r   r   r#   r   r   r   )r   r	   r   F   s   r   )Ztorch._Cr   Ú
contextlibr   r$   r   ÚtypesZ	torch.jitZtorch._utils_internalÚhasattrr   Úcontextmanagerr
   Ú
ModuleTyper   r   Úopsr   r   r   r	   Ú<module>   s   %(