Skip to content

Commit ae614ed

Browse files
mthrokfacebook-github-bot
authored andcommitted
Remove custom flashlight import (#3246)
Summary: In #3232, the CTC decoder is excluded from binary distribution. To use CTCDecoder, users need to install flashlight-text. Currently, if flashlight-text is not available, torchaudio still attempts to import the custom bundle. This commit clean up this behavior by delaying the error until one of the components is actually used, and providing a better message. Pull Request resolved: #3246 Test Plan: Binary smoke tests import torchaudio without installing flashlight. Unit test CI jobs run the CTC decoder with flashlight installed. Reviewed By: jacobkahn Differential Revision: D44748413 Pulled By: mthrok fbshipit-source-id: 21d2cbd9961ed88405a739cc682071066712f5e4
1 parent f4d94ca commit ae614ed

File tree

2 files changed

+31
-60
lines changed

2 files changed

+31
-60
lines changed

torchaudio/models/decoder/__init__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
_INITIALIZED = False
2-
_LAZILY_IMPORTED = [
1+
_CTC_DECODERS = [
32
"CTCHypothesis",
43
"CTCDecoder",
54
"CTCDecoderLM",
@@ -10,12 +9,12 @@
109

1110

1211
def __getattr__(name: str):
13-
if name in _LAZILY_IMPORTED:
12+
if name in _CTC_DECODERS:
1413
try:
1514
from . import _ctc_decoder
16-
except AttributeError as err:
15+
except Exception as err:
1716
raise RuntimeError(
18-
"CTC decoder requires the decoder extension. Please set BUILD_CTC_DECODER=1 when building from source."
17+
"CTC Decoder suit requires flashlight-text package and optionally KenLM. Please install them."
1918
) from err
2019

2120
item = getattr(_ctc_decoder, name)
@@ -25,7 +24,7 @@ def __getattr__(name: str):
2524

2625

2726
def __dir__():
28-
return sorted(__all__ + _LAZILY_IMPORTED)
27+
return sorted(__all__)
2928

3029

31-
__all__ = []
30+
__all__ = _CTC_DECODERS

torchaudio/models/decoder/_ctc_decoder.py

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,38 @@
22

33
import itertools as it
44

5-
import warnings
65
from abc import abstractmethod
76
from collections import namedtuple
87
from typing import Dict, List, NamedTuple, Optional, Tuple, Union
98

109
import torch
11-
import torchaudio
12-
from torchaudio.utils import download_asset
13-
1410

15-
# We prioritize the version from upstream flashlight here.
16-
# This will allow applications that use the upstream flashlight
17-
# alongside torchaudio.
18-
if torchaudio._internal.module_utils.is_module_available("flashlight"):
19-
from flashlight.lib.text.decoder import (
20-
CriterionType as _CriterionType,
21-
LexiconDecoder as _LexiconDecoder,
22-
LexiconDecoderOptions as _LexiconDecoderOptions,
23-
LexiconFreeDecoder as _LexiconFreeDecoder,
24-
LexiconFreeDecoderOptions as _LexiconFreeDecoderOptions,
25-
LM as _LM,
26-
LMState as _LMState,
27-
SmearingMode as _SmearingMode,
28-
Trie as _Trie,
29-
ZeroLM as _ZeroLM,
30-
)
31-
from flashlight.lib.text.dictionary import (
32-
create_word_dict as _create_word_dict,
33-
Dictionary as _Dictionary,
34-
load_words as _load_words,
35-
)
11+
from flashlight.lib.text.decoder import (
12+
CriterionType as _CriterionType,
13+
LexiconDecoder as _LexiconDecoder,
14+
LexiconDecoderOptions as _LexiconDecoderOptions,
15+
LexiconFreeDecoder as _LexiconFreeDecoder,
16+
LexiconFreeDecoderOptions as _LexiconFreeDecoderOptions,
17+
LM as _LM,
18+
LMState as _LMState,
19+
SmearingMode as _SmearingMode,
20+
Trie as _Trie,
21+
ZeroLM as _ZeroLM,
22+
)
23+
from flashlight.lib.text.dictionary import (
24+
create_word_dict as _create_word_dict,
25+
Dictionary as _Dictionary,
26+
load_words as _load_words,
27+
)
28+
from torchaudio.utils import download_asset
3629

30+
try:
31+
from flashlight.lib.text.decoder.kenlm import KenLM as _KenLM
32+
except Exception:
3733
try:
3834
from flashlight.lib.text.decoder import KenLM as _KenLM
3935
except Exception:
4036
_KenLM = None
41-
else:
42-
torchaudio._extension._load_lib("libflashlight-text")
43-
from torchaudio.lib.flashlight_lib_text_decoder import (
44-
CriterionType as _CriterionType,
45-
KenLM as _KenLM,
46-
LexiconDecoder as _LexiconDecoder,
47-
LexiconDecoderOptions as _LexiconDecoderOptions,
48-
LexiconFreeDecoder as _LexiconFreeDecoder,
49-
LexiconFreeDecoderOptions as _LexiconFreeDecoderOptions,
50-
LM as _LM,
51-
LMState as _LMState,
52-
SmearingMode as _SmearingMode,
53-
Trie as _Trie,
54-
ZeroLM as _ZeroLM,
55-
)
56-
from torchaudio.lib.flashlight_lib_text_dictionary import (
57-
create_word_dict as _create_word_dict,
58-
Dictionary as _Dictionary,
59-
load_words as _load_words,
60-
)
61-
62-
warnings.warn(
63-
"The built-in flashlight integration is deprecated, and will be removed in future release. "
64-
"Please install flashlight-text. https://pypi.org/project/flashlight-text/ "
65-
"For the detail of CTC decoder migration, please see https://github.com/pytorch/audio/issues/3088."
66-
)
67-
6837

6938
__all__ = [
7039
"CTCHypothesis",
@@ -450,7 +419,10 @@ def ctc_decoder(
450419

451420
if type(lm) == str:
452421
if _KenLM is None:
453-
raise RuntimeError("flashlight is installed, but KenLM is not installed. Please install KenLM.")
422+
raise RuntimeError(
423+
"flashlight-text is installed, but KenLM is not installed. "
424+
"Please refer to https://github.com/kpu/kenlm#python-module for how to install it."
425+
)
454426
lm = _KenLM(lm, word_dict)
455427
elif lm is None:
456428
lm = _ZeroLM()

0 commit comments

Comments
 (0)