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
_LAZILY_IMPORTED = [
_CTC_DECODERS = [
"CTCHypothesis",
"CTCDecoder",
"CTCDecoderLM",
......@@ -10,12 +9,12 @@ _LAZILY_IMPORTED = [
def __getattr__(name: str):
if name in _LAZILY_IMPORTED:
if name in _CTC_DECODERS:
try:
from . import _ctc_decoder
except AttributeError as err:
except Exception as err:
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
item = getattr(_ctc_decoder, name)
......@@ -25,7 +24,7 @@ def __getattr__(name: str):
def __dir__():
return sorted(__all__ + _LAZILY_IMPORTED)
return sorted(__all__)
__all__ = []
__all__ = _CTC_DECODERS
......@@ -2,69 +2,38 @@ from __future__ import annotations
import itertools as it
import warnings
from abc import abstractmethod
from collections import namedtuple
from typing import Dict, List, NamedTuple, Optional, Tuple, Union
import torch
import torchaudio
from torchaudio.utils import download_asset
# We prioritize the version from upstream flashlight here.
# This will allow applications that use the upstream flashlight
# alongside torchaudio.
if torchaudio._internal.module_utils.is_module_available("flashlight"):
from flashlight.lib.text.decoder import (
CriterionType as _CriterionType,
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 flashlight.lib.text.dictionary import (
create_word_dict as _create_word_dict,
Dictionary as _Dictionary,
load_words as _load_words,
)
from flashlight.lib.text.decoder import (
CriterionType as _CriterionType,
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 flashlight.lib.text.dictionary import (
create_word_dict as _create_word_dict,
Dictionary as _Dictionary,
load_words as _load_words,
)
from torchaudio.utils import download_asset
try:
from flashlight.lib.text.decoder.kenlm import KenLM as _KenLM
except Exception:
try:
from flashlight.lib.text.decoder import KenLM as _KenLM
except Exception:
_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__ = [
"CTCHypothesis",
......@@ -450,7 +419,10 @@ def ctc_decoder(
if type(lm) == str:
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)
elif lm is None:
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