README.md 6.51 KB
Newer Older
1
2
3
4
# Torchaudio Unit Test Suite

## How to run test

Nicolas Hug's avatar
Nicolas Hug committed
5
6
You can use `pytest` to run `torchaudio`'s test suites. See
https://docs.pytest.org/ for the detail of how to use `pytest` command.
7

yangarbiter's avatar
yangarbiter committed
8
9
For testing, please refer to [contributing guide](../../CONTRIBUTING.md) for
the installation of the required and optional packages.
Nicolas Hug's avatar
Nicolas Hug committed
10

yangarbiter's avatar
yangarbiter committed
11
For running `kaldi`-related tests:
Nicolas Hug's avatar
Nicolas Hug committed
12
13
14

```bash
export PATH="${PATH}:<path_to_kaldi>/src/featbin/"
15
```
Nicolas Hug's avatar
Nicolas Hug committed
16
17
18
19

Some useful pytest commands:

```bash
20
21
22
23
24
# List up all the tests
pytest test --collect-only
# Run all the test suites
pytest test
# Run tests on sox_effects module
25
pytest test/torchaudio_unittest/sox_effect
26
# use -k to apply filter
27
pytest test/torchaudio_unittest/sox_io_backend -k load  # only runs tests where their names contain load
28
29
30
31
32
33
34
35
# 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.
Nicolas Hug's avatar
Nicolas Hug committed
36
37
38
Also, while we recommend using `pytest` for *running* the tests, we cannot
make `pytest` a testing dependency of `torchaudio`. As a result, you should
not import `pytest` or its submodules in the test files; Use the Python
39
`unittest` builtin module instead, or the `parameterized` package to
Nicolas Hug's avatar
Nicolas Hug committed
40
parametrize tests.
41
42
43
44
45
46
47

## Structure of tests

The following is an overview of the tests and related modules for `torchaudio`.

### Purpose specific test suites

Nicolas Hug's avatar
Nicolas Hug committed
48
#### Numerical compatibility against existing software
hwangjeff's avatar
hwangjeff committed
49
- [Librosa compatibility test](./transforms/librosa_compatibility_test.py)
50
    Test suite for numerical compatibility against librosa.
hwangjeff's avatar
hwangjeff committed
51
- [SoX compatibility test](./transforms/sox_compatibility_test.py)
52
    Test suite for numerical compatibility against SoX.
53
- [Kaldi compatibility test](./transforms/kaldi_compatibility_impl.py)
54
55
56
    Test suite for numerical compatibility against Kaldi.

#### Result consistency with PyTorch framework
hwangjeff's avatar
hwangjeff committed
57
- [TorchScript consistency test](./transforms/torchscript_consistency_impl.py)
58
    Test suite to check 1. if an API is TorchScript-able, and 2. the results from Python and Torchscript match.
hwangjeff's avatar
hwangjeff committed
59
- [Batch consistency test](./transforms/batch_consistency_test.py)
60
61
62
63
64
65
66
67
    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)
hwangjeff's avatar
hwangjeff committed
68
- [`torchaudio.transforms`](./transforms/transforms_test.py)
69
70
- [`torchaudio.compliance.kaldi`](./compliance_kaldi_test.py)
- [`torchaudio.kaldi_io`](./kaldi_io_test.py)
71
- [`torchaudio.sox_effects`](./sox_effect)
yangarbiter's avatar
yangarbiter committed
72
- [`torchaudio.backend`](./backend)
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

### 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(
moto's avatar
moto committed
102
103
104
105
106
107
    frequency=300,
    sample_rate=16000,
    duration=1,  # seconds
    n_channels=1,
    dtype="float32",
    device="cpu",
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
)
```

#### 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.
hwangjeff's avatar
hwangjeff committed
141
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](./transforms/torchscript_consistency_impl.py) and test definitions for [CPU](./transforms/torchscript_consistency_cpu_test.py) and [CUDA](./transforms/torchscript_consistency_cuda_test.py) devices.
142
143
144
145
146
147
148
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`).