Commit ae614ed3 authored by moto's avatar moto Committed by Facebook GitHub Bot
Browse files

Remove custom flashlight import (#3246)

Summary:
In https://github.com/pytorch/audio/pull/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: https://github.com/pytorch/audio/pull/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
parent f4d94cab
_INITIALIZED = False _CTC_DECODERS = [
_LAZILY_IMPORTED = [
"CTCHypothesis", "CTCHypothesis",
"CTCDecoder", "CTCDecoder",
"CTCDecoderLM", "CTCDecoderLM",
...@@ -10,12 +9,12 @@ _LAZILY_IMPORTED = [ ...@@ -10,12 +9,12 @@ _LAZILY_IMPORTED = [
def __getattr__(name: str): def __getattr__(name: str):
if name in _LAZILY_IMPORTED: if name in _CTC_DECODERS:
try: try:
from . import _ctc_decoder from . import _ctc_decoder
except AttributeError as err: except Exception as err:
raise RuntimeError( raise RuntimeError(
"CTC decoder requires the decoder extension. Please set BUILD_CTC_DECODER=1 when building from source." "CTC Decoder suit requires flashlight-text package and optionally KenLM. Please install them."
) from err ) from err
item = getattr(_ctc_decoder, name) item = getattr(_ctc_decoder, name)
...@@ -25,7 +24,7 @@ def __getattr__(name: str): ...@@ -25,7 +24,7 @@ def __getattr__(name: str):
def __dir__(): def __dir__():
return sorted(__all__ + _LAZILY_IMPORTED) return sorted(__all__)
__all__ = [] __all__ = _CTC_DECODERS
...@@ -2,69 +2,38 @@ from __future__ import annotations ...@@ -2,69 +2,38 @@ from __future__ import annotations
import itertools as it import itertools as it
import warnings
from abc import abstractmethod from abc import abstractmethod
from collections import namedtuple from collections import namedtuple
from typing import Dict, List, NamedTuple, Optional, Tuple, Union from typing import Dict, List, NamedTuple, Optional, Tuple, Union
import torch import torch
import torchaudio
from torchaudio.utils import download_asset
# We prioritize the version from upstream flashlight here. from flashlight.lib.text.decoder import (
# This will allow applications that use the upstream flashlight CriterionType as _CriterionType,
# alongside torchaudio. LexiconDecoder as _LexiconDecoder,
if torchaudio._internal.module_utils.is_module_available("flashlight"): LexiconDecoderOptions as _LexiconDecoderOptions,
from flashlight.lib.text.decoder import ( LexiconFreeDecoder as _LexiconFreeDecoder,
CriterionType as _CriterionType, LexiconFreeDecoderOptions as _LexiconFreeDecoderOptions,
LexiconDecoder as _LexiconDecoder, LM as _LM,
LexiconDecoderOptions as _LexiconDecoderOptions, LMState as _LMState,
LexiconFreeDecoder as _LexiconFreeDecoder, SmearingMode as _SmearingMode,
LexiconFreeDecoderOptions as _LexiconFreeDecoderOptions, Trie as _Trie,
LM as _LM, ZeroLM as _ZeroLM,
LMState as _LMState, )
SmearingMode as _SmearingMode, from flashlight.lib.text.dictionary import (
Trie as _Trie, create_word_dict as _create_word_dict,
ZeroLM as _ZeroLM, Dictionary as _Dictionary,
) load_words as _load_words,
from flashlight.lib.text.dictionary import ( )
create_word_dict as _create_word_dict, from torchaudio.utils import download_asset
Dictionary as _Dictionary,
load_words as _load_words,
)
try:
from flashlight.lib.text.decoder.kenlm import KenLM as _KenLM
except Exception:
try: try:
from flashlight.lib.text.decoder import KenLM as _KenLM from flashlight.lib.text.decoder import KenLM as _KenLM
except Exception: except Exception:
_KenLM = None _KenLM = None
else:
torchaudio._extension._load_lib("libflashlight-text")
from torchaudio.lib.flashlight_lib_text_decoder import (
CriterionType as _CriterionType,
KenLM as _KenLM,
LexiconDecoder as _LexiconDecoder,
LexiconDecoderOptions as _LexiconDecoderOptions,
LexiconFreeDecoder as _LexiconFreeDecoder,
LexiconFreeDecoderOptions as _LexiconFreeDecoderOptions,
LM as _LM,
LMState as _LMState,
SmearingMode as _SmearingMode,
Trie as _Trie,
ZeroLM as _ZeroLM,
)
from torchaudio.lib.flashlight_lib_text_dictionary import (
create_word_dict as _create_word_dict,
Dictionary as _Dictionary,
load_words as _load_words,
)
warnings.warn(
"The built-in flashlight integration is deprecated, and will be removed in future release. "
"Please install flashlight-text. https://pypi.org/project/flashlight-text/ "
"For the detail of CTC decoder migration, please see https://github.com/pytorch/audio/issues/3088."
)
__all__ = [ __all__ = [
"CTCHypothesis", "CTCHypothesis",
...@@ -450,7 +419,10 @@ def ctc_decoder( ...@@ -450,7 +419,10 @@ def ctc_decoder(
if type(lm) == str: if type(lm) == str:
if _KenLM is None: if _KenLM is None:
raise RuntimeError("flashlight is installed, but KenLM is not installed. Please install KenLM.") raise RuntimeError(
"flashlight-text is installed, but KenLM is not installed. "
"Please refer to https://github.com/kpu/kenlm#python-module for how to install it."
)
lm = _KenLM(lm, word_dict) lm = _KenLM(lm, word_dict)
elif lm is None: elif lm is None:
lm = _ZeroLM() lm = _ZeroLM()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment