Skip to content

Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.

fix(nano): avoid duplicate fs and preserve audio loading errors (#3778)

Zishuo Ding879a219c7171
diff --git a/examples/industrial_data_pretraining/fun_asr_nano/model.py b/examples/industrial_data_pretraining/fun_asr_nano/model.py
index 85ca78537..2e50e9e43 100644
--- a/examples/industrial_data_pretraining/fun_asr_nano/model.py
+++ b/examples/industrial_data_pretraining/fun_asr_nano/model.py
@@ -403,12 +403,13 @@ class FunASRNano(nn.Module):
try:
time1 = time.perf_counter()
data_src = load_audio_text_image_video(
- sub_str, fs=frontend.fs, **kwargs
+ sub_str, **{**kwargs, "fs": frontend.fs}
)
time2 = time.perf_counter()
meta_data["load_data"] = f"{time2 - time1:0.3f}"
except Exception as e:
logging.error(f"Loading wav failed! {str(e)}, {traceback.format_exc()}")
+ raise
speech, speech_lengths = extract_fbank(
data_src,
diff --git a/funasr/models/fun_asr_nano/model.py b/funasr/models/fun_asr_nano/model.py
index 049ba6a06..78d75ce59 100644
--- a/funasr/models/fun_asr_nano/model.py
+++ b/funasr/models/fun_asr_nano/model.py
@@ -549,12 +549,13 @@ class FunASRNano(nn.Module):
try:
time1 = time.perf_counter()
data_src = load_audio_text_image_video(
- sub_str, fs=frontend.fs, **kwargs
+ sub_str, **{**kwargs, "fs": frontend.fs}
)
time2 = time.perf_counter()
meta_data["load_data"] = f"{time2 - time1:0.3f}"
except Exception as e:
logging.error(f"Loading wav failed! {str(e)}, {traceback.format_exc()}")
+ raise
speech, speech_lengths = extract_fbank(
data_src,
diff --git a/tests/test_fun_asr_nano_train_opts.py b/tests/test_fun_asr_nano_train_opts.py
index df4d29388..967860db3 100644
--- a/tests/test_fun_asr_nano_train_opts.py
+++ b/tests/test_fun_asr_nano_train_opts.py
@@ -6,6 +6,7 @@ import os
import re
import sys
import types
+from unittest.mock import Mock
import pytest
import torch
@@ -80,6 +81,32 @@ def model_class(request):
return funasr_model.FunASRNano if request.param == "funasr" else _load_recipe_model()
[email protected]("options", [{}, {"fs": 16000}, {"fs": 48000, "audio_fs": 48000}])
[email protected]("fail", [False, True])
+def test_audio_loading(model_class, monkeypatch, options, fail):
+ module = sys.modules[model_class.__module__]
+ error = OSError("Cannot decode audio")
+ loader = Mock(side_effect=error) if fail else Mock(return_value=torch.zeros(160))
+ extract = Mock(return_value=(torch.zeros(1, 4, 80), torch.tensor([4])))
+ monkeypatch.setattr(module, "load_audio_text_image_video", loader)
+ monkeypatch.setattr(module, "extract_fbank", extract)
+ args = (
+ types.SimpleNamespace(use_low_frame_rate=False),
+ {"system": [""], "user": ["<|startofspeech|>!sample.wav<|endofspeech|>"], "assistant": [""]},
+ types.SimpleNamespace(encode=lambda text: [1]),
+ types.SimpleNamespace(fs=16000, frame_shift=10, lfr_n=1),
+ )
+ if fail:
+ with pytest.raises(OSError) as caught:
+ model_class.data_load_speech(*args, meta_data={}, **options)
+ assert caught.value is error
+ extract.assert_not_called()
+ else:
+ output = model_class.data_load_speech(*args, meta_data={}, **options)
+ assert output["speech"].shape == (1, 4, 80)
+ loader.assert_called_once_with("sample.wav", **{**options, "fs": 16000})
+
+
@pytest.fixture
def build(monkeypatch, model_class):
"""``build(llm_conf) -> FunASRNano`` on the CPU, with the transformers loaders stubbed."""