Esempio n. 1
0
def _init_impl(path):
    with dll_lock:
        _IMPORTED_DYNDEPS.add(path)
        with extension_loader.DlopenGuard():
            ctypes.CDLL(path)
        # reinitialize available ops
        core.RefreshRegisteredOperators()
Esempio n. 2
0
def InitOpsLibrary(name):
    """Loads 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.

    Args:
        name: a name that ends in .so, such as "my_custom_op.so". Otherwise,
            the command will simply be ignored.
    Returns:
        None
    """
    if not os.path.exists(name):
        # Note(jiayq): if the name does not exist, instead of immediately
        # failing we will simply print a warning, deferring failure to the
        # time when an actual call is made.
        print('Ignoring {} as it is not a valid file.'.format(name))
        return
    with extension_loader.DlopenGuard():
        ctypes.CDLL(name)
    # reinitialize available ops
    core.RefreshRegisteredOperators()
    def test_importaftererror(self):
        from caffe2.python import core, lazy_dyndep
        import tempfile

        with tempfile.NamedTemporaryFile() as f:
            lazy_dyndep.RegisterOpsLibrary(f.name)

            def handler(e):
                raise ValueError("test")
            lazy_dyndep.SetErrorHandler(handler)
            with self.assertRaises(ValueError):
                core.RefreshRegisteredOperators()

            def handlernoop(e):
                raise
            lazy_dyndep.SetErrorHandler(handlernoop)
            lazy_dyndep.RegisterOpsLibrary("@/caffe2/caffe2/distributed:file_store_handler_ops")
            core.RefreshRegisteredOperators()
    def test_errorhandler(self):
        from caffe2.python import core, lazy_dyndep
        import tempfile

        with tempfile.NamedTemporaryFile() as f:
            lazy_dyndep.RegisterOpsLibrary(f.name)

            def handler(e):
                raise ValueError("test")
            lazy_dyndep.SetErrorHandler(handler)
            with self.assertRaises(ValueError, msg="test"):
                core.RefreshRegisteredOperators()
Esempio n. 5
0
def InitOpsLibrary(name):
    """Loads 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.

    Args:
        name: a name that ends in .so, such as "my_custom_op.so". Otherwise,
            the command will simply be ignored.
    Returns:
        None
    """
    if not name.endswith('.so'):
        # TODO(jiayq): deal with extensions on platforms that do not use .so
        # as extensions.
        print('Ignoring {} as it is not an .so file.'.format(name))
        return
    with extension_loader.DlopenGuard():
        ctypes.CDLL(name)
    # reinitialize available ops
    core.RefreshRegisteredOperators()