Skip to content

Commit

Permalink
add switch to disable sox
Browse files Browse the repository at this point in the history
  • Loading branch information
mthrok committed Jul 25, 2023
1 parent c86067f commit c412aa9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
26 changes: 4 additions & 22 deletions test/torchaudio_unittest/common_utils/case_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import torch
import torchaudio
from torch.testing._internal.common_utils import TestCase as PytorchTestCase
from torchaudio._internal.module_utils import is_module_available
from torchaudio._internal.module_utils import eval_env, is_module_available
from torchaudio.utils.ffmpeg_utils import get_video_decoders, get_video_encoders

from .backend_utils import set_audio_backend
Expand Down Expand Up @@ -143,24 +143,6 @@ def is_cuda_ctc_decoder_available():
return _IS_CUDA_CTC_DECODER_AVAILABLE


def _eval_env(var, default):
if var not in os.environ:
return default

val = os.environ.get(var, "0")
trues = ["1", "true", "TRUE", "on", "ON", "yes", "YES"]
falses = ["0", "false", "FALSE", "off", "OFF", "no", "NO"]
if val in trues:
return True
if val not in falses:
# fmt: off
raise RuntimeError(
f"Unexpected environment variable value `{var}={val}`. "
f"Expected one of {trues + falses}")
# fmt: on
return False


def _fail(reason):
def deco(test_item):
if isinstance(test_item, type):
Expand All @@ -185,7 +167,7 @@ def _pass(test_item):
return test_item


_IN_CI = _eval_env("CI", default=False)
_IN_CI = eval_env("CI", default=False)


def _skipIf(condition, reason, key):
Expand All @@ -195,7 +177,7 @@ def _skipIf(condition, reason, key):
# In CI, default to fail, so as to prevent accidental skip.
# In other env, default to skip
var = f"TORCHAUDIO_TEST_ALLOW_SKIP_IF_{key}"
skip_allowed = _eval_env(var, default=not _IN_CI)
skip_allowed = eval_env(var, default=not _IN_CI)
if skip_allowed:
return unittest.skip(reason)
return _fail(f"{reason} But the test cannot be skipped. (CI={_IN_CI}, {var}={skip_allowed}.)")
Expand Down Expand Up @@ -268,7 +250,7 @@ def skipIfNoSoxEncoder(ext):
key="NO_CUCTC_DECODER",
)
skipIfRocm = _skipIf(
_eval_env("TORCHAUDIO_TEST_WITH_ROCM", default=False),
eval_env("TORCHAUDIO_TEST_WITH_ROCM", default=False),
reason="The test doesn't currently work on the ROCm stack.",
key="ON_ROCM",
)
Expand Down
4 changes: 2 additions & 2 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 @@ -53,7 +53,7 @@

# Initialize libsox-related features
_SOX_INITIALIZED = False
if is_module_available("torchaudio.lib._torchaudio_sox"):
if eval_env("TORCHAUDIO_USE_SOX", True) and is_module_available("torchaudio.lib._torchaudio_sox"):
try:
_init_sox()
_SOX_INITIALIZED = True
Expand Down
3 changes: 0 additions & 3 deletions torchaudio/_extension/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ def _init_sox():
_load_lib("libtorchaudio_sox")
import torchaudio.lib._torchaudio_sox # noqa

# Dry-run
torchaudio.lib._torchaudio_sox.list_effects()


def _try_access_avutil(ffmpeg_ver):
libname_template = {
Expand Down
20 changes: 20 additions & 0 deletions torchaudio/_internal/module_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import importlib.util
import os
import warnings
from functools import wraps
from typing import Optional


def eval_env(var, default):
"""Check if environment varable has True-y value"""
if var not in os.environ:
return default

val = os.environ.get(var, "0")
trues = ["1", "true", "TRUE", "on", "ON", "yes", "YES"]
falses = ["0", "false", "FALSE", "off", "OFF", "no", "NO"]
if val in trues:
return True
if val not in falses:
# fmt: off
raise RuntimeError(
f"Unexpected environment variable value `{var}={val}`. "
f"Expected one of {trues + falses}")
# fmt: on
return False


def is_module_available(*modules: str) -> bool:
r"""Returns if a top-level module with :attr:`name` exists *without**
importing it. This is generally safer than try-catch block around a
Expand Down

0 comments on commit c412aa9

Please sign in to comment.