Commit 29903c5c authored by moto's avatar moto Committed by Facebook GitHub Bot
Browse files

Add switch to disable sox integration and ffmpeg integration at runtime (#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: https://github.com/pytorch/audio/pull/3500

Differential Revision: D47808178

Pulled By: mthrok

fbshipit-source-id: 80c1c6b5f4bc608d4e209473702680db093c95ee
parent c977afe0
......@@ -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
......
......@@ -2,7 +2,7 @@ import logging
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
......@@ -57,14 +57,30 @@ if _IS_TORCHAUDIO_EXT_AVAILABLE:
# 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:
......@@ -76,15 +92,11 @@ if _IS_TORCHAUDIO_EXT_AVAILABLE:
_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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment