U
    d
                     @   sP   d dl Z d dlmZmZ dd Ze adadd Zdd Z	d	d
 Z
ee
 dS )    N)dyndeplazyc                 C   s,   t j| std|  dS t|  dS )a  Registers a dynamic library that contains custom operators into Caffe2.

    Since Caffe2 uses static variable registration, you can optionally load a
    separate .so file that contains custom operators and registers that into
    the caffe2 core binary. In C++, this is usually done by either declaring
    dependency during compilation time, or via dynload. This allows us to do
    registration similarly on the Python side.

    Unlike dyndep.InitOpsLibrary, this does not actually parse the c++ file
    and refresh operators until caffe2 is called in a fashion which requires
    operators. In some large codebases this saves a large amount of time
    during import.

    It is safe to use within a program that also uses dyndep.InitOpsLibrary

    Args:
        name: a name that ends in .so, such as "my_custom_op.so". Otherwise,
            the command will simply be ignored.
    Returns:
        None
    z&Ignoring {} as it is not a valid file.N)ospathexistsprintformat_LAZY_IMPORTED_DYNDEPSadd)name r   =/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/lazy_dyndep.pyRegisterOpsLibrary   s    r   c                 C   s   | a dS )a  Registers an error handler for errors from registering operators

    Since the lazy registration may happen at a much later time, having a dedicated
    error handler allows for custom error handling logic. It is highly
    recomended to set this to prevent errors from bubbling up in weird parts of the
    code.

    Args:
        handler: a function that takes an exception as a single handler.
    Returns:
        None
    N)_error_handler)handlerr   r   r   SetErrorHandler0   s    r   c                   C   s   t   t S )N)_import_lazyr   GetImportedOpsLibrariesr   r   r   r   r   B   s    r   c                  C   sn   t sd S tt D ]X} zFztj| dd W n. tk
rV } ztrFt| W 5 d }~X Y nX W 5 t |  X qd S )NF)Ztrigger_lazy)r	   listremover   ZInitOpLibraryBaseExceptionr   )r   er   r   r   r   G   s    r   )r   Zcaffe2.pythonr   r   r   setr	   r   r   r   r   ZRegisterLazyImportr   r   r   r   <module>   s    