Skip to content

Commit

Permalink
Use dlopen
Browse files Browse the repository at this point in the history
  • Loading branch information
mthrok committed Jul 26, 2023
1 parent 85db60a commit 52b18f1
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 199 deletions.
13 changes: 5 additions & 8 deletions third_party/sox/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include(FetchContent)

FetchContent_Declare(
sox_src
sox
URL https://downloads.sourceforge.net/project/sox/sox/14.4.2/sox-14.4.2.tar.bz2
URL_HASH SHA256=81a6956d4330e75b5827316e44ae381e6f1e8928003c6aa45896da9041ea149c
PATCH_COMMAND ""
Expand All @@ -10,12 +10,9 @@ FetchContent_Declare(
)
# FetchContent_MakeAvailable will parse the downloaded content and setup the targets.
# We want to only download and not build, so we run Populate manually.
if(NOT sox_src_POPULATED)
FetchContent_Populate(sox_src)
if(NOT sox_POPULATED)
FetchContent_Populate(sox)
endif()

add_library(sox SHARED stub.c)
if(APPLE)
set_target_properties(sox PROPERTIES SUFFIX .dylib)
endif(APPLE)
target_include_directories(sox PUBLIC ${sox_src_SOURCE_DIR}/src)
add_library(libsox INTERFACE)
target_include_directories(libsox INTERFACE ${sox_SOURCE_DIR}/src)
85 changes: 0 additions & 85 deletions third_party/sox/stub.c

This file was deleted.

1 change: 1 addition & 0 deletions tools/setup_helpers/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def build_extension(self, ext):
"-DCMAKE_VERBOSE_MAKEFILE=ON",
f"-DPython_INCLUDE_DIR={distutils.sysconfig.get_python_inc()}",
f"-DBUILD_SOX:BOOL={'ON' if _BUILD_SOX else 'OFF'}",
"-DDLOPEN_SOX:BOOL=ON",
f"-DBUILD_RIR:BOOL={'ON' if _BUILD_RIR else 'OFF'}",
f"-DBUILD_RNNT:BOOL={'ON' if _BUILD_RNNT else 'OFF'}",
f"-DBUILD_ALIGN:BOOL={'ON' if _BUILD_ALIGN else 'OFF'}",
Expand Down
25 changes: 11 additions & 14 deletions torchaudio/_extension/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import sys

from torchaudio._internal.module_utils import fail_with_message, is_module_available, no_op
from torchaudio._internal.module_utils import eval_env, fail_with_message, is_module_available, no_op

try:
from .fb import _init_ffmpeg
Expand Down Expand Up @@ -51,23 +51,15 @@
_IS_ALIGN_AVAILABLE = torchaudio.lib._torchaudio.is_align_available()


# Similar to libtorchaudio, sox-related features should be importable when present.
#
# Note: This will be change in the future when sox is dynamically linked.
# At that point, this initialization should handle the case where
# sox integration is built but libsox is not found.
# Initialize libsox-related features
_SOX_ENABLED = eval_env("TORCHAUDIO_USE_SOX", True)
_SOX_INITIALIZED = False
if is_module_available("torchaudio.lib._torchaudio_sox"):
if _SOX_ENABLED and is_module_available("torchaudio.lib._torchaudio_sox"):
try:
_init_sox()
_SOX_INITIALIZED = True
except Exception:
# The initialization of sox extension will fail if supported sox
# libraries are not found in the system.
# Since the rest of the torchaudio works without it, we do not report the
# error here.
# The error will be raised when user code attempts to use these features.
_LG.debug("Failed to initialize sox extension", exc_info=True)
_LG.debug("Failed to initialize libsox bindings", exc_info=True)


# Initialize FFmpeg-related features
Expand All @@ -83,8 +75,13 @@
# The error will be raised when user code attempts to use these features.
_LG.debug("Failed to initialize ffmpeg bindings", exc_info=True)

if _SOX_INITIALIZED:
fail_if_no_sox = no_op
elif not _SOX_ENABLED:
fail_if_no_sox = fail_with_message("requires sox extension, but it is disabled. (TORCHAUDIO_USE_SOX=0)")
else:
fail_if_no_sox = _fail_since_no_sox

fail_if_no_sox = no_op if _SOX_INITIALIZED else _fail_since_no_sox
fail_if_no_ffmpeg = _fail_since_no_ffmpeg if _FFMPEG_EXT is None else no_op

fail_if_no_rir = (
Expand Down
11 changes: 2 additions & 9 deletions torchaudio/_extension/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ def _init_sox():
_load_lib("libtorchaudio_sox")
import torchaudio.lib._torchaudio_sox # noqa

torchaudio.lib._torchaudio_sox.set_verbosity(0)

import atexit

torch.ops.torchaudio.sox_effects_initialize_sox_effects()
atexit.register(torch.ops.torchaudio.sox_effects_shutdown_sox_effects)


def _try_access_avutil(ffmpeg_ver):
libname_template = {
Expand Down Expand Up @@ -202,12 +195,12 @@ def _fail_since_no_sox(func):
def wrapped(*_args, **_kwargs):
try:
# Note:
# We run _init_ffmpeg again just to show users the stacktrace.
# We run _init_sox again just to show users the stacktrace.
# _init_ffmpeg would not succeed here.
_init_sox()
except Exception as err:
raise RuntimeError(
f"{func.__name__} requires sox extension which is not available. "
f"{func.__name__} requires libsox extension which is not available. "
"Please refer to the stacktrace above for how to resolve this."
) from err
# This should not happen in normal execution, but just in case.
Expand Down
12 changes: 9 additions & 3 deletions torchaudio/csrc/sox/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
set(
sources
stub.cpp
io.cpp
utils.cpp
effects.cpp
effects_chain.cpp
types.cpp
)
set(
compiler_definitions
DLOPEN_SOX
)

torchaudio_library(
libtorchaudio_sox
"${sources}"
""
"torch;sox"
""
"torch;libsox"
"${compiler_definitions}"
)

if (BUILD_TORCHAUDIO_PYTHON_EXTENSION)
Expand All @@ -20,6 +26,6 @@ if (BUILD_TORCHAUDIO_PYTHON_EXTENSION)
"pybind/pybind.cpp;"
""
"libtorchaudio_sox"
""
"${compiler_definitions}"
)
endif()
49 changes: 2 additions & 47 deletions torchaudio/csrc/sox/effects.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,10 @@
#include <sox.h>
#include <torchaudio/csrc/sox/effects.h>
#include <torchaudio/csrc/sox/effects_chain.h>
#include <torchaudio/csrc/sox/stub.h>
#include <torchaudio/csrc/sox/utils.h>

namespace torchaudio::sox {
namespace {

enum SoxEffectsResourceState { NotInitialized, Initialized, ShutDown };
SoxEffectsResourceState SOX_RESOURCE_STATE = NotInitialized;
std::mutex SOX_RESOUCE_STATE_MUTEX;

} // namespace

void initialize_sox_effects() {
const std::lock_guard<std::mutex> lock(SOX_RESOUCE_STATE_MUTEX);

switch (SOX_RESOURCE_STATE) {
case NotInitialized:
TORCH_CHECK(
sox_init() == SOX_SUCCESS, "Failed to initialize sox effects.");
SOX_RESOURCE_STATE = Initialized;
break;
case Initialized:
break;
case ShutDown:
TORCH_CHECK(
false, "SoX Effects has been shut down. Cannot initialize again.");
}
};

void shutdown_sox_effects() {
const std::lock_guard<std::mutex> lock(SOX_RESOUCE_STATE_MUTEX);

switch (SOX_RESOURCE_STATE) {
case NotInitialized:
TORCH_CHECK(false, "SoX Effects is not initialized. Cannot shutdown.");
case Initialized:
TORCH_CHECK(
sox_quit() == SOX_SUCCESS, "Failed to initialize sox effects.");
SOX_RESOURCE_STATE = ShutDown;
break;
case ShutDown:
break;
}
}

auto apply_effects_tensor(
torch::Tensor waveform,
Expand Down Expand Up @@ -91,7 +52,7 @@ auto apply_effects_file(
const c10::optional<std::string>& format)
-> c10::optional<std::tuple<torch::Tensor, int64_t>> {
// Open input file
SoxFormat sf(sox_open_read(
SoxFormat sf(SOX sox_open_read(
path.c_str(),
/*signal=*/nullptr,
/*encoding=*/nullptr,
Expand Down Expand Up @@ -129,21 +90,15 @@ auto apply_effects_file(
dtype,
normalize.value_or(true),
channels_first_);

return std::tuple<torch::Tensor, int64_t>(
tensor, chain.getOutputSampleRate());
}

namespace {

TORCH_LIBRARY_FRAGMENT(torchaudio, m) {
m.def(
"torchaudio::sox_effects_initialize_sox_effects",
&initialize_sox_effects);
m.def("torchaudio::sox_effects_shutdown_sox_effects", &shutdown_sox_effects);
m.def("torchaudio::sox_effects_apply_effects_tensor", &apply_effects_tensor);
m.def("torchaudio::sox_effects_apply_effects_file", &apply_effects_file);
}

} // namespace
} // namespace torchaudio::sox
Loading

0 comments on commit 52b18f1

Please sign in to comment.