diff --git a/.circleci/config.yml b/.circleci/config.yml index 04b7f3400d..d97d636c4e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -605,6 +605,42 @@ jobs: name: Run style check command: .circleci/unittest/linux/scripts/run_style_checks.sh + torchscript_bc_test: + docker: + - image: "pytorch/torchaudio_unittest_base:manylinux" + resource_class: medium + steps: + - checkout + - generate_cache_key + - restore_cache: + + keys: + - torchscript-bc-test-release-envs-v1-{{ arch }}-{{ checksum ".cachekey" }} + + - run: + name: Generate Objects + command: | + .circleci/torchscript_bc_test/setup_release_envs.sh + .circleci/torchscript_bc_test/generate_objects.sh + - save_cache: + + key: torchscript-bc-test-release-envs-v1-{{ arch }}-{{ checksum ".cachekey" }} + + paths: + - conda + - envs + - store_artifacts: + path: test/torchscript_bc_test/assets + - persist_to_workspace: + root: . + paths: + - test/torchscript_bc_test/assets + - run: + name: Run BC check test + command: | + .circleci/torchscript_bc_test/setup_master_envs.sh + .circleci/torchscript_bc_test/validate_objects.sh + workflows: build: jobs: @@ -691,6 +727,9 @@ workflows: python_version: '3.8' unittest: jobs: + - torchscript_bc_test: + requires: + - download_third_parties_nix - download_third_parties_nix: name: download_third_parties_nix - unittest_linux_cpu: diff --git a/.circleci/config.yml.in b/.circleci/config.yml.in index a0ff63f77f..f02d772915 100644 --- a/.circleci/config.yml.in +++ b/.circleci/config.yml.in @@ -605,6 +605,42 @@ jobs: name: Run style check command: .circleci/unittest/linux/scripts/run_style_checks.sh + torchscript_bc_test: + docker: + - image: "pytorch/torchaudio_unittest_base:manylinux" + resource_class: medium + steps: + - checkout + - generate_cache_key + - restore_cache: + {% raw %} + keys: + - torchscript-bc-test-release-envs-v1-{{ arch }}-{{ checksum ".cachekey" }} + {% endraw %} + - run: + name: Generate Objects + command: | + .circleci/torchscript_bc_test/setup_release_envs.sh + .circleci/torchscript_bc_test/generate_objects.sh + - save_cache: + {% raw %} + key: torchscript-bc-test-release-envs-v1-{{ arch }}-{{ checksum ".cachekey" }} + {% endraw %} + paths: + - conda + - envs + - store_artifacts: + path: test/torchscript_bc_test/assets + - persist_to_workspace: + root: . + paths: + - test/torchscript_bc_test/assets + - run: + name: Run BC check test + command: | + .circleci/torchscript_bc_test/setup_master_envs.sh + .circleci/torchscript_bc_test/validate_objects.sh + workflows: build: jobs: @@ -612,6 +648,9 @@ workflows: {{ build_workflows() }} unittest: jobs: + - torchscript_bc_test: + requires: + - download_third_parties_nix {{ unittest_workflows() }} nightly: jobs: diff --git a/.circleci/torchscript_bc_test/common.sh b/.circleci/torchscript_bc_test/common.sh new file mode 100644 index 0000000000..51d258a2d9 --- /dev/null +++ b/.circleci/torchscript_bc_test/common.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +declare -a TORCHAUDIO_VERSIONS=("0.6.0") +declare -a PYTHON_VERSIONS=("3.6" "3.7" "3.8") + +export TORCHAUDIO_VERSIONS +export PYTHON_VERSIONS + +export KALDI_ROOT="${KALDI_ROOT:-$HOME}" # Just to disable warning emitted from kaldi_io + +_this_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +_root_dir="$(git rev-parse --show-toplevel)" +_conda_dir="${_root_dir}/conda" +case "$(uname -s)" in + Darwin*) _os="MacOSX";; + *) _os="Linux" +esac + +install_conda() { + if [ ! -d "${_conda_dir}" ]; then + printf "* Installing conda\n" + wget -nv -O miniconda.sh "http://repo.continuum.io/miniconda/Miniconda3-latest-${_os}-x86_64.sh" + bash ./miniconda.sh -b -f -p "${_conda_dir}" + rm miniconda.sh + fi +} + +init_conda() { + eval "$("${_conda_dir}/bin/conda" shell.bash hook)" +} + +get_name() { + echo "${1}-py${2}" +} + +get_env_dir() { + echo "${_root_dir}/envs/$(get_name "$1" "$2")" +} + +create_env() { + env_dir="$(get_env_dir "$1" "$2")" + if [ ! -d "${env_dir}" ]; then + printf "* Creating environment torchaudio: %s, Python: %s\n" "$1" "$2" + conda create -q --prefix "${env_dir}" -y python="$2" + fi +} + +activate_env() { + printf "* Activating environment torchaudio: %s, Python: %s\n" "$1" "$2" + conda activate "$(get_env_dir "$1" "$2")" +} + +install_release() { + printf "* Installing torchaudio: %s\n" "$1" + conda install -y -q torchaudio="$1" packaging -c pytorch + # packaging is required in test to validate the torchaudio version for dump +} + +install_build_dependencies() { + printf "* Installing torchaudio dependencies except PyTorch - (Python: %s)\n" "$1" + conda env update -q --file "${_this_dir}/environment.yml" --prune + if [ "${_os}" == Linux ]; then + pip install clang-format + fi +} + +build_master() { + printf "* Installing PyTorch (py%s)\n" "$1" + conda install -y -q pytorch "cpuonly" -c pytorch-nightly + printf "* Installing torchaudio\n" + cd "${_root_dir}" || exit 1 + BUILD_SOX=1 python setup.py clean install +} diff --git a/.circleci/torchscript_bc_test/environment.yml b/.circleci/torchscript_bc_test/environment.yml new file mode 100644 index 0000000000..108d97d193 --- /dev/null +++ b/.circleci/torchscript_bc_test/environment.yml @@ -0,0 +1,17 @@ +channels: + - conda-forge + - defaults +dependencies: + - flake8 + - numpy + - pytest + - pytest-cov + - codecov + - librosa + - llvmlite==0.31 # See https://github.com/pytorch/audio/pull/766 + - pip + - pip: + - kaldi-io + - scipy + - parameterized + - numba==0.48 # See https://github.com/librosa/librosa/issues/1160 diff --git a/.circleci/torchscript_bc_test/generate_objects.sh b/.circleci/torchscript_bc_test/generate_objects.sh new file mode 100755 index 0000000000..352f137ad2 --- /dev/null +++ b/.circleci/torchscript_bc_test/generate_objects.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +set -e + +this_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +pushd "${this_dir}" +. "common.sh" +popd + +init_conda + +# Move to test directory so that the checked out torchaudio source +# will not shadow the conda-installed version of torchaudio +cd test + +for torchaudio in "${TORCHAUDIO_VERSIONS[@]}" ; do + for python in "${PYTHON_VERSIONS[@]}" ; do + activate_env "${torchaudio}" "${python}" + python -m torch.utils.collect_env + printf "***********************************************************\n" + printf "* Generating\n" + printf " Objects: Python: %s, torchaudio: %s\n" "${python}" "${torchaudio}" + printf "***********************************************************\n" + ./torchscript_bc_test/main.py --mode generate --version "${torchaudio}" + done +done diff --git a/.circleci/torchscript_bc_test/setup_master_envs.sh b/.circleci/torchscript_bc_test/setup_master_envs.sh new file mode 100755 index 0000000000..2321ee0529 --- /dev/null +++ b/.circleci/torchscript_bc_test/setup_master_envs.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +set -e + +cd "$( dirname "${BASH_SOURCE[0]}" )" +. "common.sh" + +install_conda +init_conda + +# Install torchaudio environments +for python in "${PYTHON_VERSIONS[@]}" ; do + create_env master "${python}" + activate_env master "${python}" + install_build_dependencies "${python}" + build_master "${python}" +done diff --git a/.circleci/torchscript_bc_test/setup_release_envs.sh b/.circleci/torchscript_bc_test/setup_release_envs.sh new file mode 100755 index 0000000000..3816c091d4 --- /dev/null +++ b/.circleci/torchscript_bc_test/setup_release_envs.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +set -e + +cd "$( dirname "${BASH_SOURCE[0]}" )" +. "common.sh" + +install_conda +init_conda + +# Install torchaudio environments +for torchaudio in "${TORCHAUDIO_VERSIONS[@]}" ; do + for python in "${PYTHON_VERSIONS[@]}" ; do + create_env "${torchaudio}" "${python}" + activate_env "${torchaudio}" "${python}" + install_release "${torchaudio}" + done +done diff --git a/.circleci/torchscript_bc_test/validate_objects.sh b/.circleci/torchscript_bc_test/validate_objects.sh new file mode 100755 index 0000000000..852ed0acc8 --- /dev/null +++ b/.circleci/torchscript_bc_test/validate_objects.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +set -e + +this_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +pushd "${this_dir}" +. "common.sh" +popd + +init_conda + +# Move to test directory so that the checked out torchaudio source +# will not shadow the conda-installed version of torchaudio +cd test + +# Validate torchscript objects for each +for runtime_python in "${PYTHON_VERSIONS[@]}" ; do + activate_env master "${runtime_python}" + python -m torch.utils.collect_env + for object_torchaudio in "${TORCHAUDIO_VERSIONS[@]}" ; do + for object_python in "${PYTHON_VERSIONS[@]}" ; do + printf "***********************************************************\n" + printf "* Validating\n" + printf " Runtime: Python: %s, torchaudio: master (%s)\n" "${runtime_python}" "$(python -c 'import torchaudio;print(torchaudio.__version__)')" + printf " Objects: Python: %s, torchaudio: %s\n" "${object_python}" "${object_torchaudio}" + printf "***********************************************************\n" + ./torchscript_bc_test/main.py --mode validate --version "${object_torchaudio}" + done + done +done diff --git a/test/README.md b/test/README.md index 629e5b54be..62d32b428d 100644 --- a/test/README.md +++ b/test/README.md @@ -1,131 +1,4 @@ # Torchaudio Test Suite -## How to run test - -You can use `pytest` to run `torchaudio`'s test suites. See https://docs.pytest.org/ for the detail of how to use `pytest` command. - -``` -# List up all the tests -pytest test --collect-only -# Run all the test suites -pytest test -# Run tests on sox_effects module -pytest test/sox_effect -# use -k to apply filter -pytest test/sox_io_backend -k load # only runs tests where their names contain load -# Some other useful options; -# Stop on the first failure -x -# Run failure fast --ff -# Only rerun the failure --lf -``` - -**Note** -We use PyTorch's test utilities instead of `pytest` frameworks when writing tests to avoid reinventing the wheel for Tensor comparison. - -## Structure of tests - -The following is an overview of the tests and related modules for `torchaudio`. - -### Purpose specific test suites - -#### Numerical compatibility agains existing software -- [Librosa compatibility test](./torchaudio_unittest/librosa_compatibility_test.py) - Test suite for numerical compatibility against librosa. -- [SoX compatibility test](./torchaudio_unittest/sox_compatibility_test.py) - Test suite for numerical compatibility against SoX. -- [Kaldi compatibility test](./torchaudio_unittest/kaldi_compatibility_test.py) - Test suite for numerical compatibility against Kaldi. - -#### Result consistency with PyTorch framework -- [TorchScript consistency test](./torchaudio_unittest/torchscript_consistency_impl.py) - Test suite to check 1. if an API is TorchScript-able, and 2. the results from Python and Torchscript match. -- [Batch consistency test](./torchaudio_unittest/batch_consistency_test.py) - Test suite to check if functionals/Transforms handle single sample input and batch input and return the same result. - -### Module specific test suites - -The following test modules are defined for corresponding `torchaudio` module/functions. - -- [`torchaudio.datasets`](./torchaudio_unittest/datasets) -- [`torchaudio.functional`](./torchaudio_unittest/functional) -- [`torchaudio.transforms`](./torchaudio_unittest/transforms_test.py) -- [`torchaudio.compliance.kaldi`](./torchaudio_unittest/compliance_kaldi_test.py) -- [`torchaudio.kaldi_io`](./torchaudio_unittest/kaldi_io_test.py) -- [`torchaudio.sox_effects`](test/sox_effects) -- [`torchaudio.save`, `torchaudio.load`, `torchaudio.info`](./torchaudio_unittest/io_test.py) - -### Test modules that do not fall into the above categories -- [test_dataloader.py](./torchaudio_unittest/dataloader_test.py) - Simple test for loading data and applying preprocessing. - -### Support files -- [assets](./torchaudio_unittest/assets): Contain sample audio files. -- [assets/kaldi](./torchaudio_unittest/assets/kaldi): Contains Kaldi format matrix files used in [./torchaudio_unittest/test_compliance_kaldi.py](./torchaudio_unittest/test_compliance_kaldi.py). -- [compliance](./torchaudio_unittest/compliance): Scripts used to generate above Kaldi matrix files. - -### Waveforms for Testing Purposes - -When testing transforms we often need waveforms of specific type (ex: pure tone, noise, or voice), with specific bitrate (ex. 8 or 16 kHz) and number of channels (ex. mono, stereo). Below are some tips on how to construct waveforms and guidance around existing audio files. - -#### Load a Waveform from a File - -```python -filepath = common_utils.get_asset_path('filename.wav') -waveform, sample_rate = common_utils.load_wav(filepath) -``` - -*Note: Should you choose to contribute an audio file, please leave a comment in the issue or pull request, mentioning content source and licensing information. WAV files are preferred. Other formats should be used only when there is no alternative. (i.e. dataset implementation comes with hardcoded non-wav extension).* - -#### Pure Tone - -Code: - -```python -waveform = common_utils.get_sinusoid( - frequency=300, - sample_rate=16000, - duration=1, # seconds - n_channels=1, - dtype="float32", - device="cpu", -) -``` - -#### Noise - -Code: - -```python -tensor = common_utils.get_whitenoise() -``` - -Files: - -* `steam-train-whistle-daniel_simon.wav` - -#### Voice - -Files: - -* `CommonVoice/cv-corpus-4-2019-12-10/tt/clips/common_voice_tt_00000000.wav` -* `VCTK-Corpus/wav48/p224/p224_002.wav` -* `vad-go-stereo-44100.wav` -* `vad-go-mono-32000.wav` - -## Adding test - -The following is the current practice of torchaudio test suite. - -1. Unless the tests are related to I/O, use synthetic data. [`common_utils`](./torchaudio_unittest/common_utils) has some data generator functions. -1. When you add a new test case, use `common_utils.TorchaudioTestCase` as base class unless you are writing tests that are common to CPU / CUDA. - - Set class memeber `dtype`, `device` and `backend` for the desired behavior. - - If you do not set `backend` value in your test suite, then I/O functions will be unassigned and attempt to load/save file will fail. - - For `backend` value, in addition to available backends, you can also provide the value "default" and backend will be picked automatically based on availability. -1. If you are writing tests that should pass on diffrent dtype/devices, write a common class inheriting `common_utils.TestBaseMixin`, then inherit `common_utils.PytorchTestCase` and define class attributes (`dtype` / `device` / `backend`) there. See [Torchscript consistency test implementation](./torchaudio_unittest/torchscript_consistency_impl.py) and test definitions for [CPU](./torchaudio_unittest/torchscript_consistency_cpu_test.py) and [CUDA](./torchaudio_unittest/torchscript_consistency_cuda_test.py) devices. -1. For numerically comparing Tensors, use `assertEqual` method from torchaudio_unittest.common_utils.PytorchTestCase` class. This method has a better support for a wide variety of Tensor types. - -When you add a new feature(functional/transform), consider the following - -1. When you add a new feature, please make it Torchscript-able and batch-consistent unless it degrades the performance. Please add the tests to see if the new feature meet these requirements. -1. If the feature should be numerical compatible against existing software (SoX, Librosa, Kaldi etc), add a corresponding test. -1. If the new feature is unique to `torchaudio` (not a PyTorch implementation of an existing Software functionality), consider adding correctness tests (wheather the expected output is produced for the set of input) under the corresponding test module (`test_functional.py`, `test_transforms.py`). +- [Unit Test](./torchaudio_unittest/) +- [Torchscript Backward Compatibility Test](./torchscript_bc_test/) \ No newline at end of file diff --git a/test/torchaudio_unittest/README.md b/test/torchaudio_unittest/README.md new file mode 100644 index 0000000000..306ca23098 --- /dev/null +++ b/test/torchaudio_unittest/README.md @@ -0,0 +1,131 @@ +# Torchaudio Unit Test Suite + +## How to run test + +You can use `pytest` to run `torchaudio`'s test suites. See https://docs.pytest.org/ for the detail of how to use `pytest` command. + +``` +# List up all the tests +pytest test --collect-only +# Run all the test suites +pytest test +# Run tests on sox_effects module +pytest test/sox_effect +# use -k to apply filter +pytest test/sox_io_backend -k load # only runs tests where their names contain load +# Some other useful options; +# Stop on the first failure -x +# Run failure fast --ff +# Only rerun the failure --lf +``` + +**Note** +We use PyTorch's test utilities instead of `pytest` frameworks when writing tests to avoid reinventing the wheel for Tensor comparison. + +## Structure of tests + +The following is an overview of the tests and related modules for `torchaudio`. + +### Purpose specific test suites + +#### Numerical compatibility agains existing software +- [Librosa compatibility test](./librosa_compatibility_test.py) + Test suite for numerical compatibility against librosa. +- [SoX compatibility test](./sox_compatibility_test.py) + Test suite for numerical compatibility against SoX. +- [Kaldi compatibility test](./kaldi_compatibility_test.py) + Test suite for numerical compatibility against Kaldi. + +#### Result consistency with PyTorch framework +- [TorchScript consistency test](./torchscript_consistency_impl.py) + Test suite to check 1. if an API is TorchScript-able, and 2. the results from Python and Torchscript match. +- [Batch consistency test](./batch_consistency_test.py) + Test suite to check if functionals/Transforms handle single sample input and batch input and return the same result. + +### Module specific test suites + +The following test modules are defined for corresponding `torchaudio` module/functions. + +- [`torchaudio.datasets`](./datasets) +- [`torchaudio.functional`](./functional) +- [`torchaudio.transforms`](./transforms_test.py) +- [`torchaudio.compliance.kaldi`](./compliance_kaldi_test.py) +- [`torchaudio.kaldi_io`](./kaldi_io_test.py) +- [`torchaudio.sox_effects`](test/sox_effects) +- [`torchaudio.save`, `torchaudio.load`, `torchaudio.info`](./io_test.py) + +### Test modules that do not fall into the above categories +- [test_dataloader.py](./dataloader_test.py) + Simple test for loading data and applying preprocessing. + +### Support files +- [assets](./assets): Contain sample audio files. +- [assets/kaldi](./assets/kaldi): Contains Kaldi format matrix files used in [./test_compliance_kaldi.py](./test_compliance_kaldi.py). +- [compliance](./compliance): Scripts used to generate above Kaldi matrix files. + +### Waveforms for Testing Purposes + +When testing transforms we often need waveforms of specific type (ex: pure tone, noise, or voice), with specific bitrate (ex. 8 or 16 kHz) and number of channels (ex. mono, stereo). Below are some tips on how to construct waveforms and guidance around existing audio files. + +#### Load a Waveform from a File + +```python +filepath = common_utils.get_asset_path('filename.wav') +waveform, sample_rate = common_utils.load_wav(filepath) +``` + +*Note: Should you choose to contribute an audio file, please leave a comment in the issue or pull request, mentioning content source and licensing information. WAV files are preferred. Other formats should be used only when there is no alternative. (i.e. dataset implementation comes with hardcoded non-wav extension).* + +#### Pure Tone + +Code: + +```python +waveform = common_utils.get_sinusoid( + frequency=300, + sample_rate=16000, + duration=1, # seconds + n_channels=1, + dtype="float32", + device="cpu", +) +``` + +#### Noise + +Code: + +```python +tensor = common_utils.get_whitenoise() +``` + +Files: + +* `steam-train-whistle-daniel_simon.wav` + +#### Voice + +Files: + +* `CommonVoice/cv-corpus-4-2019-12-10/tt/clips/common_voice_tt_00000000.wav` +* `VCTK-Corpus/wav48/p224/p224_002.wav` +* `vad-go-stereo-44100.wav` +* `vad-go-mono-32000.wav` + +## Adding test + +The following is the current practice of torchaudio test suite. + +1. Unless the tests are related to I/O, use synthetic data. [`common_utils`](./common_utils) has some data generator functions. +1. When you add a new test case, use `common_utils.TorchaudioTestCase` as base class unless you are writing tests that are common to CPU / CUDA. + - Set class memeber `dtype`, `device` and `backend` for the desired behavior. + - If you do not set `backend` value in your test suite, then I/O functions will be unassigned and attempt to load/save file will fail. + - For `backend` value, in addition to available backends, you can also provide the value "default" and backend will be picked automatically based on availability. +1. If you are writing tests that should pass on diffrent dtype/devices, write a common class inheriting `common_utils.TestBaseMixin`, then inherit `common_utils.PytorchTestCase` and define class attributes (`dtype` / `device` / `backend`) there. See [Torchscript consistency test implementation](./torchscript_consistency_impl.py) and test definitions for [CPU](./torchscript_consistency_cpu_test.py) and [CUDA](./torchscript_consistency_cuda_test.py) devices. +1. For numerically comparing Tensors, use `assertEqual` method from torchaudio_unittest.common_utils.PytorchTestCase` class. This method has a better support for a wide variety of Tensor types. + +When you add a new feature(functional/transform), consider the following + +1. When you add a new feature, please make it Torchscript-able and batch-consistent unless it degrades the performance. Please add the tests to see if the new feature meet these requirements. +1. If the feature should be numerical compatible against existing software (SoX, Librosa, Kaldi etc), add a corresponding test. +1. If the new feature is unique to `torchaudio` (not a PyTorch implementation of an existing Software functionality), consider adding correctness tests (wheather the expected output is produced for the set of input) under the corresponding test module (`test_functional.py`, `test_transforms.py`). diff --git a/test/torchscript_bc_test/README.md b/test/torchscript_bc_test/README.md new file mode 100644 index 0000000000..4cf0e2b226 --- /dev/null +++ b/test/torchscript_bc_test/README.md @@ -0,0 +1,23 @@ +# Torchscript Backward Compatibility Test Suite + +This directory contains tools to generate Torchscript object of a specific torchaudio version (given that you have the corresponding environments setup correctly) and validate it in the current version. + +## Usage + +### Generate torchscript object + +``` +./main.py --mode generate --version 0.6.0 +``` + +will generate Torchscript dump files in [`assets`](./assets/) directory. This requries your Python runtime to have torchaudio `0.6.0`. + + +### Validate torchscript object + + +``` +./main.py --mode validate --version 0.6.0 +``` + +will validate if the Torchscript files created in the previous step are compatible with the version of torchaudio available in your environment (master). diff --git a/test/torchscript_bc_test/assets/.gitignore b/test/torchscript_bc_test/assets/.gitignore new file mode 100644 index 0000000000..72e8ffc0db --- /dev/null +++ b/test/torchscript_bc_test/assets/.gitignore @@ -0,0 +1 @@ +* diff --git a/test/torchscript_bc_test/main.py b/test/torchscript_bc_test/main.py new file mode 100755 index 0000000000..3c1c8d974e --- /dev/null +++ b/test/torchscript_bc_test/main.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 +"""Generate torchscript object of specific torhcaudio version. + +This requires that the corresponding torchaudio (and torch) is installed. +""" +import os +import sys +import argparse + + +_BASE_OBJ_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'assets') + + +def _parse_args(): + parser = argparse.ArgumentParser( + description=__doc__ + ) + parser.add_argument( + '--mode', choices=['generate', 'validate'], required=True, + help=( + '"generate" generates Torchscript objects of the specific torchaudio ' + 'in the given directory. ' + '"validate" validates if the objects in the givcen directory are compatible ' + 'with the current torhcaudio.' + ) + ) + parser.add_argument( + '--version', choices=['0.6.0'], required=True, + help='torchaudio version.' + ) + parser.add_argument( + '--base-obj-dir', default=_BASE_OBJ_DIR, + help='Directory where objects are saved/loaded.' + ) + return parser.parse_args() + + +def _generate(version, output_dir): + if version == '0.6.0': + import ver_060 + ver_060.generate(output_dir) + else: + raise ValueError(f'Unexpected torchaudio version: {version}') + + +def _validate(version, input_dir): + if version == '0.6.0': + import ver_060 + ver_060.validate(input_dir) + else: + raise ValueError(f'Unexpected torchaudio version: {version}') + + +def _get_obj_dir(base_dir, version): + py_version = f'{sys.version_info.major}.{sys.version_info.minor}' + return os.path.join(base_dir, f'{version}-py{py_version}') + + +def _main(): + args = _parse_args() + obj_dir = _get_obj_dir(args.base_obj_dir, args.version) + if args.mode == 'generate': + _generate(args.version, obj_dir) + elif args.mode == 'validate': + _validate(args.version, obj_dir) + else: + raise ValueError(f'Unexpected mode: {args.mode}') + + +if __name__ == '__main__': + _main() diff --git a/test/torchscript_bc_test/ver_060.py b/test/torchscript_bc_test/ver_060.py new file mode 100644 index 0000000000..78b8da228c --- /dev/null +++ b/test/torchscript_bc_test/ver_060.py @@ -0,0 +1,71 @@ +import os +import tempfile +from typing import Optional +from packaging import version + +import torch +import torchaudio + +_MIN_VER = version.parse('0.6.0a0') +_MAX_VER = version.parse('0.7.0') +_RUNTIME_VER = version.parse(torchaudio.__version__) + + +def info(filepath: str) -> torchaudio.backend.sox_io_backend.AudioMetaData: + return torchaudio.info(filepath) + + +def load( + filepath: str, + frame_offset: int, + num_frames: int, + normalize: bool, + channels_first: bool): + return torchaudio.load(filepath, frame_offset, num_frames, normalize, channels_first) + + +def save( + filepath: str, + tensor: torch.Tensor, + sample_rate: int, + channels_first: bool = True, + compression: Optional[float] = None, +): + torchaudio.save(filepath, tensor, sample_rate, channels_first, compression) + + +def generate(output_dir): + if not (_MIN_VER <= _RUNTIME_VER < _MAX_VER): + raise RuntimeError(f'Invalid torchaudio runtime version: {_RUNTIME_VER}') + + torchaudio.set_audio_backend('sox_io') + + funcs = [ + info, + load, + save, + ] + + os.makedirs(output_dir, exist_ok=True) + for func in funcs: + torch.jit.script(func).save(os.path.join(output_dir, f'{func.__name__}.zip')) + + +def validate(input_dir): + torchaudio.set_audio_backend('sox_io') + + # See https://github.com/pytorch/pytorch/issues/42258 + # info_ = torch.jit.load(os.path.join(input_dir, 'info.zip')) + load_ = torch.jit.load(os.path.join(input_dir, 'load.zip')) + save_ = torch.jit.load(os.path.join(input_dir, 'save.zip')) + + sample_rate = 44100 + normalize = True + channels_first = True + with tempfile.TemporaryDirectory() as temp_dir: + temp_file = os.path.join(temp_dir, 'test.wav') + temp_data = torch.rand(2, sample_rate, dtype=torch.float32) + + save_(temp_file, temp_data, sample_rate, channels_first, 0.) + # info_(temp_file) + load_(temp_file, 0, -1, normalize, channels_first)