Skip to content

Commit

Permalink
Add switch to disable sox integration and ffmpeg integration at runti…
Browse files Browse the repository at this point in the history
…me (#3500)

Summary:
Since libsox and ffmpeg extensions now depend on external libraries, their initialization processes might cause unrecoverable issue, such as segfault.

This commit adds environment variable to disable them so that importing torchaudio won't attempt to load these libraries.

Pull Request resolved: #3500

Differential Revision: D47808178

Pulled By: mthrok

fbshipit-source-id: 80c1c6b5f4bc608d4e209473702680db093c95ee
  • Loading branch information
mthrok authored and facebook-github-bot committed Jul 27, 2023
1 parent c977afe commit 29903c5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/unittest-windows-cpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_MOD_sentencepiece=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_AUDIO_OUT_DEVICE=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_MACOS=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_TEMPORARY_DISABLED=true
.github/scripts/unittest-windows/setup_env.sh
.github/scripts/unittest-windows/install.sh
Expand Down
34 changes: 23 additions & 11 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 @@ -57,14 +57,30 @@
# At that point, this initialization should handle the case where
# sox integration is built but libsox is not found.
_SOX_INITIALIZED = False
if is_module_available("torchaudio.lib._torchaudio_sox"):
_USE_SOX = False if os.name == "nt" else eval_env("TORCHAUDIO_USE_SOX", True)
_SOX_MODULE_AVAILABLE = is_module_available("torchaudio.lib._torchaudio_sox")
if _USE_SOX and _SOX_MODULE_AVAILABLE:
_init_sox()
_SOX_INITIALIZED = True


if os.name == "nt":
fail_if_no_sox = fail_with_message("requires sox extension, which is not supported on Windows.")
elif not _USE_SOX:
fail_if_no_sox = fail_with_message("requires sox extension, but it is disabled. (TORCHAUDIO_USE_SOX=0)")
elif not _SOX_MODULE_AVAILABLE:
fail_if_no_sox = fail_with_message(
"requires sox extension, but TorchAudio is not compiled with it. "
"Please build TorchAudio with libsox support. (BUILD_SOX=1)"
)
else:
fail_if_no_sox = no_op


# Initialize FFmpeg-related features
_FFMPEG_EXT = None
if _IS_TORCHAUDIO_EXT_AVAILABLE:
_USE_FFMPEG = eval_env("TORCHAUDIO_USE_FFMPEG", True)
if _USE_FFMPEG and _IS_TORCHAUDIO_EXT_AVAILABLE:
try:
_FFMPEG_EXT = _init_ffmpeg()
except Exception:
Expand All @@ -76,15 +92,11 @@
_LG.debug("Failed to initialize ffmpeg bindings", exc_info=True)


fail_if_no_sox = (
no_op
if _SOX_INITIALIZED
else fail_with_message(
"requires sox extension, but TorchAudio is not compiled with it. Please build TorchAudio with libsox support."
)
)
if _USE_FFMPEG:
fail_if_no_ffmpeg = _fail_since_no_ffmpeg if _FFMPEG_EXT is None else no_op
else:
fail_if_no_ffmpeg = fail_with_message("requires ffmpeg extension, but it is disabled. (TORCHAUDIO_USE_FFMPEG=0)")

fail_if_no_ffmpeg = _fail_since_no_ffmpeg if _FFMPEG_EXT is None else no_op

fail_if_no_rir = (
no_op
Expand Down

0 comments on commit 29903c5

Please sign in to comment.