parent
12339daddb
commit
43fba4235a
@ -1,63 +0,0 @@
|
||||
import contextlib
|
||||
import ctypes
|
||||
import os
|
||||
import sys
|
||||
import types
|
||||
|
||||
# Query `hasattr` only once.
|
||||
_SET_GLOBAL_FLAGS = hasattr(sys, 'getdlopenflags') and hasattr(sys,
|
||||
'setdlopenflags')
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def dl_open_guard():
|
||||
"""
|
||||
# https://manpages.debian.org/bullseye/manpages-dev/dlopen.3.en.html
|
||||
Context manager to set the RTLD_GLOBAL dynamic linker flag while we open a
|
||||
shared library to load custom operators.
|
||||
"""
|
||||
if _SET_GLOBAL_FLAGS:
|
||||
old_flags = sys.getdlopenflags()
|
||||
sys.setdlopenflags(old_flags | ctypes.RTLD_GLOBAL)
|
||||
yield
|
||||
if _SET_GLOBAL_FLAGS:
|
||||
sys.setdlopenflags(old_flags)
|
||||
|
||||
|
||||
def resolve_library_path(path: str) -> str:
|
||||
return os.path.realpath(path)
|
||||
|
||||
|
||||
class _Ops(types.ModuleType):
|
||||
__file__ = '_ops.py'
|
||||
|
||||
def __init__(self):
|
||||
super(_Ops, self).__init__('paddleaudio.ops')
|
||||
self.loaded_libraries = set()
|
||||
|
||||
def load_library(self, path):
|
||||
"""
|
||||
Loads a shared library from the given path into the current process.
|
||||
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 ``paddleaudio.ops.load_library('path/to/libcustom.so')`` to load the
|
||||
shared object.
|
||||
After the library is loaded, it is added to the
|
||||
``paddleaudio.ops.loaded_libraries`` attribute, a set that may be inspected
|
||||
for the paths of all libraries loaded using this function.
|
||||
Args:
|
||||
path (str): A path to a shared library to load.
|
||||
"""
|
||||
path = resolve_library_path(path)
|
||||
with dl_open_guard():
|
||||
# https://docs.python.org/3/library/ctypes.html?highlight=ctypes#loading-shared-libraries
|
||||
# Import the shared library into the process, thus running its
|
||||
# static (global) initialization code in order to register custom
|
||||
# operators with the JIT.
|
||||
ctypes.CDLL(path)
|
||||
self.loaded_libraries.add(path)
|
||||
|
||||
|
||||
# The ops "namespace"
|
||||
ops = _Ops()
|
@ -1,13 +0,0 @@
|
||||
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
Loading…
Reference in new issue