mirror of
https://github.com/modelscope/FunASR.git
synced 2026-10-12 02:19:18 +00:00
* Remove hard torchaudio dependency for inference; add kaldi-native-fbank fbank backend Make torchaudio an optional dependency for the inference path so FunASR can run on Ascend NPU (aarch64) servers, where no matching torchaudio wheel exists (Huawei's Ascend torch is version-decoupled from the PyTorch upstream release line). - Add funasr/utils/fbank.py: an fbank shim that delegates to torchaudio.compliance.kaldi when torchaudio is installed, otherwise falls back to kaldi-native-fbank (a torch-free C++ library with aarch64 wheels). Options and frame-shift API are mapped to torchaudio semantics so outputs are equivalent. - Switch funasr/frontends/wav_frontend.py, funasr/utils/speaker_utils.py and funasr/models/campplus/utils.py to the shim. - Make the torchaudio import optional in funasr/utils/load_utils.py (resampler falls back to librosa). No device-specific branching is introduced — a pure backend fallback that also benefits CPU-only and other accelerator platforms. Verified end-to-end on Ascend 910B (npu:0) with torchaudio absent: paraformer-large loads and infers correctly, RTF ~0.016 (~60x realtime). * Make torchaudio import optional in paraformer_v2, fun_asr_nano and dataset preprocessors Follow-up to the inference-path change: make the remaining top-level torchaudio imports optional so these modules import cleanly when torchaudio is not installed (e.g. on Ascend NPU servers). - funasr/models/paraformer_v2_community/model.py - funasr/models/fun_asr_nano/tools/utils.py - funasr/datasets/audio_datasets/preprocessor.py - funasr/datasets/llm_datasets/preprocessor.py Functions that still require torchaudio (forced_align, Resample) degrade to a clear error only when called, rather than a hard import failure. * Address review: declare knf extra, harden fbank fallback and torchaudio guards Respond to maintainer review feedback. - Declare kaldi-native-fbank as an optional 'knf' extra in setup.py; the fbank module now raises an actionable ImportError only when fbank() is actually called with neither torchaudio nor kaldi-native-fbank installed (previously it failed at import time with a bare ModuleNotFoundError). - Harden the fbank fallback contract: handle the channel argument explicitly and reject torchaudio-only options (subtract_mean, min_duration, VTLN warping, non-default blackman_coeff) with NotImplementedError instead of silently dropping them via **kwargs. - Add a shared guard funasr/utils/torchaudio_compat.py and use it for resampling, forced alignment and speed perturbation so a missing torchaudio fails loudly instead of returning a silently wrong result (empty alignment). - Add regression tests (tests/test_torchaudio_optional.py) that import the affected modules with torchaudio absent.
49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
import os
|
|
import json
|
|
import torch
|
|
import logging
|
|
import concurrent.futures
|
|
import librosa
|
|
import torch.distributed as dist
|
|
from typing import Collection
|
|
import torch
|
|
try:
|
|
import torchaudio
|
|
except ImportError:
|
|
torchaudio = None
|
|
from torch import nn
|
|
import random
|
|
import re
|
|
import string
|
|
from funasr.tokenizer.cleaner import TextCleaner
|
|
from funasr.register import tables
|
|
|
|
|
|
@tables.register("preprocessor_classes", "TextPreprocessRemovePunctuation")
|
|
class TextPreprocessRemovePunctuation(nn.Module):
|
|
def __init__(self, **kwargs):
|
|
"""Initialize TextPreprocessRemovePunctuation.
|
|
|
|
Args:
|
|
**kwargs: Additional keyword arguments.
|
|
"""
|
|
super().__init__()
|
|
|
|
def forward(self, text, **kwargs):
|
|
# 定义英文标点符号
|
|
"""Forward pass for training.
|
|
|
|
Args:
|
|
text: Text tensor or string input.
|
|
**kwargs: Additional keyword arguments.
|
|
"""
|
|
en_punct = string.punctuation
|
|
# 定义中文标点符号(部分常用的)
|
|
cn_punct = "。?!,、;:“”‘’()《》【】…—~·"
|
|
# 合并英文和中文标点符号
|
|
all_punct = en_punct + cn_punct
|
|
# 创建正则表达式模式,匹配任何在all_punct中的字符
|
|
punct_pattern = re.compile("[{}]".format(re.escape(all_punct)))
|
|
# 使用正则表达式的sub方法替换掉这些字符
|
|
return punct_pattern.sub("", text)
|