def _init_impl(path): with dll_lock: _IMPORTED_DYNDEPS.add(path) with extension_loader.DlopenGuard(): ctypes.CDLL(path) # reinitialize available ops core.RefreshRegisteredOperators()
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 Python(self, f, grad_f=None): with extension_loader.DlopenGuard(): import caffe2.python.op.python_ops_python as ops_python RefreshRegisteredOperators() assert (IsOperator('Python')) token = ops_python.register(f) if grad_f: ops_python.register_gradient(token, grad_f) return lambda *args, **kwargs: self._CreateAndAddToSelf( 'Python', token=token, *args, **kwargs)
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()
## @package _import_c_extension # Module caffe2.python._import_c_extension import atexit import logging import sys from caffe2.python import extension_loader # We will first try to load the gpu-enabled caffe2. If it fails, we will then # attempt to load the cpu version. The cpu backend is the minimum required, so # if that still fails, we will exit loud. with extension_loader.DlopenGuard(): has_hip_support = False has_cuda_support = False has_gpu_support = False try: from caffe2.python.caffe2_pybind11_state_gpu import * # noqa if num_cuda_devices(): # noqa has_gpu_support = has_cuda_support = True except ImportError as gpu_e: logging.info('Failed to import cuda module: {}'.format(gpu_e)) try: from caffe2.python.caffe2_pybind11_state_hip import * # noqa # we stop checking whether we have AMD GPU devices on the host, # because we may be constructing a net on a machine without GPU, # and run the net on another one with GPU has_gpu_support = has_hip_support = True logging.info('This caffe2 python run has AMD GPU support!') except ImportError as hip_e: logging.info('Failed to import AMD hip module: {}'.format(hip_e))
## @package _import_c_extension # Module caffe2.python._import_c_extension import atexit import logging import sys from caffe2.python import extension_loader # We will first try to load the gpu-enabled caffe2. If it fails, we will then # attempt to load the cpu version. The cpu backend is the minimum required, so # if that still fails, we will exit loud. with extension_loader.DlopenGuard(): has_hip_support = False has_gpu_support = False try: from caffe2.python.caffe2_pybind11_state_gpu import * # noqa if num_cuda_devices(): # noqa has_gpu_support = True except ImportError as gpu_e: logging.info('Failed to import cuda module: {}'.format(gpu_e)) try: RTLD_LAZY = 1 with extension_loader.DlopenGuard(RTLD_LAZY): from caffe2.python.caffe2_pybind11_state_hip import * # noqa if num_hip_devices(): has_hip_support = True logging.info('This caffe2 python run has AMD GPU support!') except ImportError as hip_e: logging.info('Failed to import AMD hip module: {}'.format(hip_e)) logging.warning(