Skip to content

Commit

Permalink
Remove onnx and onnxruntime from requirements.txt (#640)
Browse files Browse the repository at this point in the history
* Remove onnx and onnxruntime from requirements.txt
  • Loading branch information
csukuangfj authored Oct 31, 2022
1 parent 1abf286 commit 7f1c0e0
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
import logging
from typing import List, Optional, Tuple

from icefall import is_module_available

if not is_module_available("onnxruntime"):
raise ValueError("Please 'pip install onnxruntime' first.")

import onnxruntime as ort
import sentencepiece as spm
import torch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
import argparse
import logging

from icefall import is_module_available

if not is_module_available("onnxruntime"):
raise ValueError("Please 'pip install onnxruntime' first.")

import onnxruntime as ort
import torch

Expand Down
5 changes: 5 additions & 0 deletions egs/librispeech/ASR/pruned_transducer_stateless3/test_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
"""
import os

from icefall import is_module_available

if not is_module_available("onnxruntime"):
raise ValueError("Please 'pip install onnxruntime' first.")

import onnxruntime as ort
import torch
from conformer import (
Expand Down
9 changes: 8 additions & 1 deletion egs/librispeech/ASR/pruned_transducer_stateless6/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import torch
import torch.nn as nn
from encoder_interface import EncoderInterface
from multi_quantization.prediction import JointCodebookLoss
from scaling import ScaledLinear

from icefall.utils import add_sos
Expand Down Expand Up @@ -74,6 +73,14 @@ def __init__(
encoder_dim, vocab_size, initial_speed=0.5
)
self.simple_lm_proj = ScaledLinear(decoder_dim, vocab_size)

from icefall import is_module_available

if not is_module_available("multi_quantization"):
raise ValueError("Please 'pip install multi_quantization' first.")

from multi_quantization.prediction import JointCodebookLoss

if num_codebooks > 0:
self.codebook_loss_net = JointCodebookLoss(
predictor_channels=encoder_dim,
Expand Down
13 changes: 8 additions & 5 deletions egs/librispeech/ASR/pruned_transducer_stateless6/vq_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,21 @@
import numpy as np
import torch
import torch.multiprocessing as mp
import multi_quantization as quantization

from icefall import is_module_available

if not is_module_available("multi_quantization"):
raise ValueError("Please 'pip install multi_quantization' first.")

import multi_quantization as quantization
from asr_datamodule import LibriSpeechAsrDataModule
from hubert_xlarge import HubertXlargeFineTuned
from icefall.utils import (
AttributeDict,
setup_logger,
)
from lhotse import CutSet, load_manifest
from lhotse.cut import MonoCut
from lhotse.features.io import NumpyHdf5Writer

from icefall.utils import AttributeDict, setup_logger


class CodebookIndexExtractor:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
import argparse
import logging

from icefall import is_module_available

if not is_module_available("onnxruntime"):
raise ValueError("Please 'pip install onnxruntime' first.")

import onnxruntime as ort
import torch

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
import k2
import kaldifeat
import numpy as np

from icefall import is_module_available

if not is_module_available("onnxruntime"):
raise ValueError("Please 'pip install onnxruntime' first.")

import onnxruntime as ort
import torch
import torchaudio
Expand Down
1 change: 1 addition & 0 deletions icefall/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
get_executor,
get_texts,
is_jit_tracing,
is_module_available,
l1_norm,
l2_norm,
linf_norm,
Expand Down
9 changes: 8 additions & 1 deletion icefall/ngram_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from collections import defaultdict
from typing import List, Optional, Tuple

import kaldifst
from icefall.utils import is_module_available


class NgramLm:
Expand All @@ -36,6 +36,11 @@ def __init__(
is_binary:
True if the given file is a binary FST.
"""
if not is_module_available("kaldifst"):
raise ValueError("Please 'pip install kaldifst' first.")

import kaldifst

if is_binary:
lm = kaldifst.StdVectorFst.read(fst_filename)
else:
Expand Down Expand Up @@ -85,6 +90,8 @@ def _get_next_state_and_cost_without_backoff(
self, state: int, label: int
) -> Tuple[int, float]:
"""TODO: Add doc."""
import kaldifst

arc_iter = kaldifst.ArcIterator(self.lm, state)
num_arcs = self.lm.num_arcs(state)

Expand Down
14 changes: 14 additions & 0 deletions icefall/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,3 +976,17 @@ def display_and_save_batch(
y = sp.encode(supervisions["text"], out_type=int)
num_tokens = sum(len(i) for i in y)
logging.info(f"num tokens: {num_tokens}")


# `is_module_available` is copied from
# https://github.com/pytorch/audio/blob/6bad3a66a7a1c7cc05755e9ee5931b7391d2b94c/torchaudio/_internal/module_utils.py#L9
def is_module_available(*modules: str) -> bool:
r"""Returns if a top-level module with :attr:`name` exists *without**
importing it. This is generally safer than try-catch block around a
`import X`.
Note: "borrowed" from torchaudio:
"""
import importlib

return all(importlib.util.find_spec(m) is not None for m in modules)
5 changes: 0 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,4 @@ kaldialign
sentencepiece>=0.1.96
tensorboard
typeguard
multi_quantization
onnx
onnxruntime
--extra-index-url https://pypi.ngc.nvidia.com
dill
kaldifst
6 changes: 6 additions & 0 deletions test/test_ngram_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
# limitations under the License.

import graphviz

from icefall import is_module_available

if not is_module_available("kaldifst"):
raise ValueError("Please 'pip install kaldifst' first.")

import kaldifst

from icefall import NgramLm, NgramLmStateCost
Expand Down

0 comments on commit 7f1c0e0

Please sign in to comment.