diff --git a/test/torchaudio_unittest/common_utils/case_utils.py b/test/torchaudio_unittest/common_utils/case_utils.py index ad760afa53b..60aa6b8e080 100644 --- a/test/torchaudio_unittest/common_utils/case_utils.py +++ b/test/torchaudio_unittest/common_utils/case_utils.py @@ -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 is_module_available, eval_env from torchaudio.utils.ffmpeg_utils import get_video_decoders, get_video_encoders from .backend_utils import set_audio_backend @@ -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): @@ -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): @@ -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}.)") @@ -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", ) diff --git a/torchaudio/_extension/__init__.py b/torchaudio/_extension/__init__.py index 216e1747752..dc635efa29f 100644 --- a/torchaudio/_extension/__init__.py +++ b/torchaudio/_extension/__init__.py @@ -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 fail_with_message, is_module_available, no_op, eval_env try: from .fb import _init_ffmpeg @@ -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 diff --git a/torchaudio/_extension/utils.py b/torchaudio/_extension/utils.py index f3e81dd316f..27662aacd88 100644 --- a/torchaudio/_extension/utils.py +++ b/torchaudio/_extension/utils.py @@ -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 = { diff --git a/torchaudio/_internal/module_utils.py b/torchaudio/_internal/module_utils.py index d5ab186b0c2..e0fb541fc1a 100644 --- a/torchaudio/_internal/module_utils.py +++ b/torchaudio/_internal/module_utils.py @@ -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