Skip to content

Commit 1500d04

Browse files
parmeetfacebook-github-bot
authored andcommitted
Import torchaudio #1597 284bd10
Summary: Import from Github Reviewed By: carolineechen Differential Revision: D29518488 fbshipit-source-id: 34b3d3f2f8035bf734d047c7b6e6ec6e15ff65f1
1 parent e225852 commit 1500d04

40 files changed

+2155
-235
lines changed

.circleci/config.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ commands:
5252
name: Adding CONDA_CHANNEL_FLAGS to BASH_ENV
5353
command: |
5454
CONDA_CHANNEL_FLAGS=""
55-
if [[ "${PYTHON_VERSION}" = *3.9* ]]; then
56-
echo "export CONDA_CHANNEL_FLAGS=-c=conda-forge" >> ${BASH_ENV}
57-
fi
55+
# formerly used to add conda-forge flags for Python 3.9, reserving the mechanism for future python upgrades
5856
5957
binary_common: &binary_common
6058
parameters:

.circleci/config.yml.in

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ commands:
5252
name: Adding CONDA_CHANNEL_FLAGS to BASH_ENV
5353
command: |
5454
CONDA_CHANNEL_FLAGS=""
55-
if [[ "${PYTHON_VERSION}" = *3.9* ]]; then
56-
echo "export CONDA_CHANNEL_FLAGS=-c=conda-forge" >> ${BASH_ENV}
57-
fi
55+
# formerly used to add conda-forge flags for Python 3.9, reserving the mechanism for future python upgrades
5856

5957
binary_common: &binary_common
6058
parameters:

.circleci/torchscript_bc_test/common.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ build_master() {
6767
printf "* Installing torchaudio\n"
6868
cd "${_root_dir}" || exit 1
6969
git submodule update --init --recursive
70-
BUILD_TRANSDUCER=1 BUILD_SOX=1 python setup.py clean install
70+
BUILD_RNNT=1 BUILD_SOX=1 python setup.py clean install
7171
}

.circleci/torchscript_bc_test/environment.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
channels:
2-
- conda-forge
32
- defaults
43
dependencies:
54
- flake8

.circleci/unittest/linux/scripts/install.sh

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,26 @@ eval "$("${conda_dir}/bin/conda" shell.bash hook)"
2323
conda activate "${env_dir}"
2424

2525
# 1. Install PyTorch
26-
# [2021/06/22 Temporary workaround] Disabling the original installation
27-
# The orignal, conda-based instartion is working for GPUs, but not for CPUs
28-
# For CPUs we use pip-based installation
29-
# if [ -z "${CUDA_VERSION:-}" ] ; then
30-
# if [ "${os}" == MacOSX ] ; then
31-
# cudatoolkit=''
32-
# else
33-
# cudatoolkit="cpuonly"
34-
# fi
35-
# else
36-
# version="$(python -c "print('.'.join(\"${CUDA_VERSION}\".split('.')[:2]))")"
37-
# cudatoolkit="cudatoolkit=${version}"
38-
# fi
39-
# printf "Installing PyTorch with %s\n" "${cudatoolkit}"
40-
# (
41-
# set -x
42-
# conda install ${CONDA_CHANNEL_FLAGS:-} -y -c "pytorch-${UPLOAD_CHANNEL}" "pytorch-${UPLOAD_CHANNEL}::pytorch" ${cudatoolkit}
43-
# )
44-
45-
if [ "${os}" == MacOSX ] || [ -z "${CUDA_VERSION:-}" ] ; then
46-
device="cpu"
26+
if [ -z "${CUDA_VERSION:-}" ] ; then
27+
if [ "${os}" == MacOSX ] ; then
28+
cudatoolkit=''
29+
else
30+
cudatoolkit="cpuonly"
31+
fi
4732
else
48-
device=cu"$(python -c "print(''.join(\"${CUDA_VERSION}\".split('.')[:2]))")"
33+
version="$(python -c "print('.'.join(\"${CUDA_VERSION}\".split('.')[:2]))")"
34+
cudatoolkit="cudatoolkit=${version}"
4935
fi
50-
printf "Installing PyTorch with %s\n" "${device}"
36+
printf "Installing PyTorch with %s\n" "${cudatoolkit}"
5137
(
5238
set -x
53-
pip install --pre torch==1.10.0.dev20210618 -f "https://download.pytorch.org/whl/nightly/${device}/torch_nightly.html"
39+
conda install ${CONDA_CHANNEL_FLAGS:-} -y -c "pytorch-${UPLOAD_CHANNEL}" "pytorch-${UPLOAD_CHANNEL}::pytorch" ${cudatoolkit}
5440
)
5541

5642
# 2. Install torchaudio
5743
printf "* Installing torchaudio\n"
5844
git submodule update --init --recursive
59-
BUILD_TRANSDUCER=1 BUILD_SOX=1 python setup.py install
45+
BUILD_RNNT=1 BUILD_SOX=1 python setup.py install
6046

6147
# 3. Install Test tools
6248
printf "* Installing test tools\n"

.circleci/unittest/windows/scripts/environment.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
channels:
2-
- conda-forge
32
- defaults
43
dependencies:
54
- flake8

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ endif()
5656
# Options
5757
option(BUILD_SOX "Build libsox statically" OFF)
5858
option(BUILD_KALDI "Build kaldi statically" ON)
59-
option(BUILD_TRANSDUCER "Enable transducer" OFF)
59+
option(BUILD_RNNT "Enable RNN transducer" OFF)
6060
option(BUILD_LIBTORCHAUDIO "Build C++ Library" ON)
6161
option(BUILD_TORCHAUDIO_PYTHON_EXTENSION "Build Python extension" OFF)
6262
option(USE_CUDA "Enable CUDA support" OFF)

build_tools/setup_helpers/extension.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ def _get_build(var, default=False):
3636

3737
_BUILD_SOX = False if platform.system() == 'Windows' else _get_build("BUILD_SOX")
3838
_BUILD_KALDI = False if platform.system() == 'Windows' else _get_build("BUILD_KALDI", True)
39-
_BUILD_TRANSDUCER = _get_build("BUILD_TRANSDUCER")
39+
_BUILD_RNNT = _get_build("BUILD_RNNT")
4040
_USE_ROCM = _get_build("USE_ROCM")
41-
_USE_CUDA = torch.cuda.is_available()
41+
_USE_CUDA = _get_build("USE_CUDA", torch.cuda.is_available())
4242

4343

4444
def get_ext_modules():
@@ -73,7 +73,7 @@ def build_extension(self, ext):
7373
f"-DPython_INCLUDE_DIR={distutils.sysconfig.get_python_inc()}",
7474
f"-DBUILD_SOX:BOOL={'ON' if _BUILD_SOX else 'OFF'}",
7575
f"-DBUILD_KALDI:BOOL={'ON' if _BUILD_KALDI else 'OFF'}",
76-
f"-DBUILD_TRANSDUCER:BOOL={'ON' if _BUILD_TRANSDUCER else 'OFF'}",
76+
f"-DBUILD_RNNT:BOOL={'ON' if _BUILD_RNNT else 'OFF'}",
7777
"-DBUILD_TORCHAUDIO_PYTHON_EXTENSION:BOOL=ON",
7878
"-DBUILD_LIBTORCHAUDIO:BOOL=OFF",
7979
f"-DUSE_ROCM:BOOL={'ON' if _USE_ROCM else 'OFF'}",

examples/libtorchaudio/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ SET(BUILD_LIBTORCHAUDIO ON CACHE BOOL "Build libtorchaudio")
66
SET(BUILD_SOX ON CACHE BOOL "Build libsox into libtorchaudio")
77

88
SET(BUILD_KALDI OFF CACHE BOOL "Build Kaldi into libtorchaudio")
9-
SET(BUILD_TRANSDUCER OFF CACHE BOOL "Build transducer into libtorchaudio")
9+
SET(BUILD_RNNT OFF CACHE BOOL "Build RNN transducer into libtorchaudio")
1010
SET(BUILD_TORCHAUDIO_PYTHON_EXTENSION OFF CACHE BOOL "Build Python binding")
1111

1212
find_package(Torch REQUIRED)

examples/pipeline_wav2letter/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
from torch.optim.lr_scheduler import ExponentialLR, ReduceLROnPlateau
1212
from torch.utils.data import DataLoader
1313
from torchaudio.datasets.utils import bg_iterator
14+
from torchaudio.functional import edit_distance
1415
from torchaudio.models.wav2letter import Wav2Letter
1516

1617
from ctc_decoders import GreedyDecoder
1718
from datasets import collate_factory, split_process_librispeech
1819
from languagemodels import LanguageModel
19-
from metrics import levenshtein_distance
2020
from transforms import Normalize, UnsqueezeFirst
2121
from utils import MetricLogger, count_parameters, save_checkpoint
2222

@@ -217,7 +217,7 @@ def compute_error_rates(outputs, targets, decoder, language_model, metric):
217217
target_print = target[i].ljust(print_length)[:print_length]
218218
logging.info("Target: %s Output: %s", target_print, output_print)
219219

220-
cers = [levenshtein_distance(t, o) for t, o in zip(target, output)]
220+
cers = [edit_distance(t, o) for t, o in zip(target, output)]
221221
cers = sum(cers)
222222
n = sum(len(t) for t in target)
223223
metric["batch char error"] = cers
@@ -232,7 +232,7 @@ def compute_error_rates(outputs, targets, decoder, language_model, metric):
232232
output = [o.split(language_model.char_space) for o in output]
233233
target = [t.split(language_model.char_space) for t in target]
234234

235-
wers = [levenshtein_distance(t, o) for t, o in zip(target, output)]
235+
wers = [edit_distance(t, o) for t, o in zip(target, output)]
236236
wers = sum(wers)
237237
n = sum(len(t) for t in target)
238238
metric["batch word error"] = wers

examples/pipeline_wav2letter/metrics.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

packaging/build_wheel.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ if [[ "$OSTYPE" == "msys" ]]; then
1515
python_tag="$(echo "cp$PYTHON_VERSION" | tr -d '.')"
1616
"$script_dir/vc_env_helper.bat" python setup.py bdist_wheel --plat-name win_amd64 --python-tag $python_tag
1717
else
18-
BUILD_TRANSDUCER=1 BUILD_SOX=1 python setup.py bdist_wheel
18+
BUILD_RNNT=1 BUILD_SOX=1 python setup.py bdist_wheel
1919
fi

packaging/pkg_helpers.bash

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,6 @@ setup_conda_pytorch_constraint() {
231231
else
232232
export CONDA_CHANNEL_FLAGS="${CONDA_CHANNEL_FLAGS} -c pytorch -c pytorch-test -c pytorch-nightly"
233233
fi
234-
# Some dependencies for Python 3.9 are only on conda-forge
235-
if [[ "${PYTHON_VERSION}" = "3.9" ]]; then
236-
export CONDA_CHANNEL_FLAGS="${CONDA_CHANNEL_FLAGS} -c conda-forge"
237-
fi
238234
if [[ "$CU_VERSION" == cpu ]]; then
239235
export CONDA_PYTORCH_BUILD_CONSTRAINT="- pytorch==$PYTORCH_VERSION${PYTORCH_VERSION_SUFFIX}"
240236
export CONDA_PYTORCH_CONSTRAINT="- pytorch==$PYTORCH_VERSION"

packaging/torchaudio/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env bash
22
set -ex
33

4-
BUILD_TRANSDUCER=1 BUILD_SOX=1 python setup.py install --single-version-externally-managed --record=record.txt
4+
BUILD_RNNT=1 BUILD_SOX=1 python setup.py install --single-version-externally-managed --record=record.txt

test/torchaudio_unittest/functional/functional_impl.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,46 @@ def test_phase_vocoder_shape(self, rate, test_pseudo_complex):
382382
output_shape = (torch.view_as_complex(spec_stretch) if test_pseudo_complex else spec_stretch).shape
383383
assert output_shape == expected_shape
384384

385+
@parameterized.expand(
386+
[
387+
# words
388+
["", "", 0], # equal
389+
["abc", "abc", 0],
390+
["ᑌᑎIᑕO", "ᑌᑎIᑕO", 0],
391+
392+
["abc", "", 3], # deletion
393+
["aa", "aaa", 1],
394+
["aaa", "aa", 1],
395+
["ᑌᑎI", "ᑌᑎIᑕO", 2],
396+
397+
["aaa", "aba", 1], # substitution
398+
["aba", "aaa", 1],
399+
["aba", " ", 3],
400+
401+
["abc", "bcd", 2], # mix deletion and substitution
402+
["0ᑌᑎI", "ᑌᑎIᑕO", 3],
403+
404+
# sentences
405+
[["hello", "", "Tᕮ᙭T"], ["hello", "", "Tᕮ᙭T"], 0], # equal
406+
[[], [], 0],
407+
408+
[["hello", "world"], ["hello", "world", "!"], 1], # deletion
409+
[["hello", "world"], ["world"], 1],
410+
[["hello", "world"], [], 2],
411+
412+
[["Tᕮ᙭T", ], ["world"], 1], # substitution
413+
[["Tᕮ᙭T", "XD"], ["world", "hello"], 2],
414+
[["", "XD"], ["world", ""], 2],
415+
["aba", " ", 3],
416+
417+
[["hello", "world"], ["world", "hello", "!"], 2], # mix deletion and substitution
418+
[["Tᕮ᙭T", "world", "LOL", "XD"], ["world", "hello", "ʕ•́ᴥ•̀ʔっ"], 3],
419+
]
420+
)
421+
def test_simple_case_edit_distance(self, seq1, seq2, distance):
422+
assert F.edit_distance(seq1, seq2) == distance
423+
assert F.edit_distance(seq2, seq1) == distance
424+
385425

386426
class FunctionalCPUOnly(TestBaseMixin):
387427
def test_create_fb_matrix_no_warning_high_n_freq(self):
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import torch
22
from .autograd_impl import Autograd
33
from torchaudio_unittest import common_utils
4-
from .utils import skipIfNoTransducer
4+
from .utils import skipIfNoRNNT
55

66

7-
@skipIfNoTransducer
7+
@skipIfNoRNNT
88
class TestAutograd(Autograd, common_utils.PytorchTestCase):
99
dtype = torch.float32
1010
device = torch.device('cpu')

test/torchaudio_unittest/rnnt/autograd_cuda_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import torch
22
from .autograd_impl import Autograd
33
from torchaudio_unittest import common_utils
4-
from .utils import skipIfNoTransducer
4+
from .utils import skipIfNoRNNT
55

66

7-
@skipIfNoTransducer
7+
@skipIfNoRNNT
88
@common_utils.skipIfNoCuda
99
class TestAutograd(Autograd, common_utils.PytorchTestCase):
1010
dtype = torch.float32
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import torch
22
from torchaudio_unittest import common_utils
3-
from .utils import skipIfNoTransducer
3+
from .utils import skipIfNoRNNT
44
from .rnnt_loss_impl import RNNTLossTest
55

66

7-
@skipIfNoTransducer
7+
@skipIfNoRNNT
88
class TestRNNTLoss(RNNTLossTest, common_utils.PytorchTestCase):
99
device = torch.device('cpu')
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import torch
22
from .rnnt_loss_impl import RNNTLossTest
33
from torchaudio_unittest import common_utils
4-
from .utils import skipIfNoTransducer
4+
from .utils import skipIfNoRNNT
55

66

7-
@skipIfNoTransducer
7+
@skipIfNoRNNT
88
@common_utils.skipIfNoCuda
99
class TestRNNTLoss(RNNTLossTest, common_utils.PytorchTestCase):
1010
device = torch.device('cuda')
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import torch
22

33
from torchaudio_unittest.common_utils import PytorchTestCase
4-
from .utils import skipIfNoTransducer
4+
from .utils import skipIfNoRNNT
55
from .torchscript_consistency_impl import RNNTLossTorchscript
66

77

8-
@skipIfNoTransducer
8+
@skipIfNoRNNT
99
class TestRNNTLoss(RNNTLossTorchscript, PytorchTestCase):
1010
device = torch.device('cpu')
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import torch
22

33
from torchaudio_unittest.common_utils import PytorchTestCase, skipIfNoCuda
4-
from .utils import skipIfNoTransducer
4+
from .utils import skipIfNoRNNT
55
from .torchscript_consistency_impl import RNNTLossTorchscript
66

77

8-
@skipIfNoTransducer
8+
@skipIfNoRNNT
99
@skipIfNoCuda
1010
class TestRNNTLoss(RNNTLossTorchscript, PytorchTestCase):
1111
device = torch.device('cuda')

test/torchaudio_unittest/rnnt/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def compute_with_pytorch_transducer(data, reuse_logits_for_grads=False):
3131
blank=data["blank"],
3232
fused_log_softmax=data.get("fused_log_softmax", True),
3333
reuse_logits_for_grads=reuse_logits_for_grads,
34+
reduction="none",
3435
)(
3536
logits=data["logits"],
3637
logit_lengths=data["logit_lengths"],
@@ -442,7 +443,7 @@ def grad_hook(grad):
442443
return data
443444

444445

445-
def skipIfNoTransducer(test_item):
446+
def skipIfNoRNNT(test_item):
446447
try:
447448
torch.ops.torchaudio.rnnt_loss
448449
return test_item

third_party/sox/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ExternalProject_Add(mad
1919
DOWNLOAD_DIR ${ARCHIVE_DIR}
2020
URL https://downloads.sourceforge.net/project/mad/libmad/0.15.1b/libmad-0.15.1b.tar.gz
2121
URL_HASH SHA256=bbfac3ed6bfbc2823d3775ebb931087371e142bb0e9bb1bee51a76a6e0078690
22-
PATCH_COMMAND patch < ${CMAKE_CURRENT_SOURCE_DIR}/patch/libmad.patch && cp ${CMAKE_CURRENT_SOURCE_DIR}/patch/config.guess ${CMAKE_CURRENT_BINARY_DIR}/src/mad/config.guess
22+
PATCH_COMMAND patch < ${CMAKE_CURRENT_SOURCE_DIR}/patch/libmad.patch && cp ${CMAKE_CURRENT_SOURCE_DIR}/patch/config.guess ${CMAKE_CURRENT_BINARY_DIR}/src/mad/config.guess && cp ${CMAKE_CURRENT_SOURCE_DIR}/patch/config.sub ${CMAKE_CURRENT_BINARY_DIR}/src/mad/config.sub
2323
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env ${envs} ${CMAKE_CURRENT_BINARY_DIR}/src/mad/configure ${COMMON_ARGS}
2424
DOWNLOAD_NO_PROGRESS ON
2525
LOG_DOWNLOAD ON
@@ -53,7 +53,7 @@ ExternalProject_Add(lame
5353
URL https://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
5454
URL_HASH SHA256=24346b4158e4af3bd9f2e194bb23eb473c75fb7377011523353196b19b9a23ff
5555
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env ${envs} ${CMAKE_CURRENT_BINARY_DIR}/src/lame/configure ${COMMON_ARGS} --enable-nasm
56-
PATCH_COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/patch/config.guess ${CMAKE_CURRENT_BINARY_DIR}/src/lame/config.guess
56+
PATCH_COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/patch/config.guess ${CMAKE_CURRENT_BINARY_DIR}/src/lame/config.guess && cp ${CMAKE_CURRENT_SOURCE_DIR}/patch/config.sub ${CMAKE_CURRENT_BINARY_DIR}/src/lame/config.sub
5757
DOWNLOAD_NO_PROGRESS ON
5858
LOG_DOWNLOAD ON
5959
LOG_UPDATE ON

0 commit comments

Comments
 (0)