Skip to content

Remove custom flashlight import #3246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions torchaudio/models/decoder/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
_INITIALIZED = False
_LAZILY_IMPORTED = [
_CTC_DECODERS = [
"CTCHypothesis",
"CTCDecoder",
"CTCDecoderLM",
Expand All @@ -10,12 +9,12 @@


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)
Expand All @@ -25,7 +24,7 @@ def __getattr__(name: str):


def __dir__():
return sorted(__all__ + _LAZILY_IMPORTED)
return sorted(__all__)


__all__ = []
__all__ = _CTC_DECODERS
78 changes: 25 additions & 53 deletions torchaudio/models/decoder/_ctc_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,38 @@

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",
Expand Down Expand Up @@ -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()
Expand Down