def load_file_system_library(library_filename): """Loads a TensorFlow plugin, containing file system implementation. Pass `library_filename` to a platform-specific mechanism for dynamically loading a library. The rules for determining the exact location of the library are platform-specific and are not documented here. Args: library_filename: Path to the plugin. Relative or absolute filesystem path to a dynamic library file. Returns: None. Raises: RuntimeError: when unable to load the library. """ py_tf.TF_LoadLibrary(library_filename)
def load_op_library(library_filename): """Loads a TensorFlow plugin, containing custom ops and kernels. Pass "library_filename" to a platform-specific mechanism for dynamically loading a library. The rules for determining the exact location of the library are platform-specific and are not documented here. When the library is loaded, ops and kernels registered in the library via the `REGISTER_*` macros are made available in the TensorFlow process. Note that ops with the same name as an existing op are rejected and not registered with the process. Args: library_filename: Path to the plugin. Relative or absolute filesystem path to a dynamic library file. Returns: A python module containing the Python wrappers for Ops defined in the plugin. Raises: RuntimeError: when unable to load the library or get the python wrappers. """ lib_handle = py_tf.TF_LoadLibrary(library_filename) try: wrappers = _pywrap_python_op_gen.GetPythonWrappers( py_tf.TF_GetOpList(lib_handle)) finally: # Delete the library handle to release any memory held in C # that are no longer needed. py_tf.TF_DeleteLibraryHandle(lib_handle) # Get a unique name for the module. module_name = hashlib.sha1(wrappers).hexdigest() if module_name in sys.modules: return sys.modules[module_name] module_spec = importlib.machinery.ModuleSpec(module_name, None) module = importlib.util.module_from_spec(module_spec) # pylint: disable=exec-used exec(wrappers, module.__dict__) # Allow this to be recognized by AutoGraph. setattr(module, '_IS_TENSORFLOW_PLUGIN', True) sys.modules[module_name] = module return module
def load_library(library_location): """Loads a TensorFlow plugin. "library_location" can be a path to a specific shared object, or a folder. If it is a folder, all shared objects that are named "libtfkernel*" will be loaded. When the library is loaded, kernels registered in the library via the `REGISTER_*` macros are made available in the TensorFlow process. Args: library_location: Path to the plugin or the folder of plugins. Relative or absolute filesystem path to a dynamic library file or folder. Returns: None Raises: OSError: When the file to be loaded is not found. RuntimeError: when unable to load the library. """ if os.path.exists(library_location): if os.path.isdir(library_location): directory_contents = os.listdir(library_location) kernel_libraries = [ os.path.join(library_location, f) for f in directory_contents if _is_shared_object(f) ] else: kernel_libraries = [library_location] for lib in kernel_libraries: py_tf.TF_LoadLibrary(lib) else: raise OSError( errno.ENOENT, 'The file or folder to load kernel libraries from does not exist.', library_location)