Skip to content

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

fix(onnx): avoid UnboundLocalError in Fsmn_vad_online for chunks without feature frames

Tanishqa45faf98febc
diff --git a/.github/workflows/test-funasr-onnx-package.yml b/.github/workflows/test-funasr-onnx-package.yml
index 75d735f0b..e2418bb07 100644
--- a/.github/workflows/test-funasr-onnx-package.yml
+++ b/.github/workflows/test-funasr-onnx-package.yml
@@ -11,6 +11,7 @@ on:
- "tests/test_funasr_onnx_installed.py"
- "tests/test_funasr_onnx_paraformer_online_overlap.py"
- "tests/test_funasr_onnx_paraformer_online_empty_final.py"
+ - "tests/test_funasr_onnx_vad_online_short_chunks.py"
- "tests/test_rich_transcription_postprocess.py"
- "tests/test_rich_transcription_event_segments.py"
push:
@@ -25,6 +26,7 @@ on:
- "tests/test_funasr_onnx_installed.py"
- "tests/test_funasr_onnx_paraformer_online_overlap.py"
- "tests/test_funasr_onnx_paraformer_online_empty_final.py"
+ - "tests/test_funasr_onnx_vad_online_short_chunks.py"
- "tests/test_rich_transcription_postprocess.py"
- "tests/test_rich_transcription_event_segments.py"
workflow_dispatch:
@@ -105,6 +107,9 @@ jobs:
- name: Verify empty split-final Paraformer result
run: python tests/test_funasr_onnx_paraformer_online_empty_final.py
+ - name: Verify streaming VAD short chunks
+ run: python tests/test_funasr_onnx_vad_online_short_chunks.py
+
- name: Verify source distribution error paths
run: |
python -m pip install --force-reinstall --no-deps dist/funasr-onnx/*.tar.gz
diff --git a/runtime/python/onnxruntime/funasr_onnx/vad_bin.py b/runtime/python/onnxruntime/funasr_onnx/vad_bin.py
index 1c98b7f92..6bfa7ea92 100644
--- a/runtime/python/onnxruntime/funasr_onnx/vad_bin.py
+++ b/runtime/python/onnxruntime/funasr_onnx/vad_bin.py
@@ -289,11 +289,11 @@ class Fsmn_vad_online:
is_final = param_dict.get("is_final", False)
frontend: WavFrontendOnline = param_dict.get("frontend", WavFrontendOnline(cmvn_file=self.cmvn_file, **self.config["frontend_conf"]))
feats, feats_len = self.extract_feat(frontend=frontend, waveforms=waveforms, is_final=is_final)
+ vad_scorer = param_dict.get("vad_scorer", E2EVadModel(self.config["model_conf"]))
segments = []
if feats.size != 0:
in_cache = param_dict.get("in_cache", list())
in_cache = self.prepare_cache(in_cache)
- vad_scorer = param_dict.get("vad_scorer", E2EVadModel(self.config["model_conf"]))
try:
inputs = [feats]
inputs.extend(in_cache)
diff --git a/tests/test_funasr_onnx_vad_online_short_chunks.py b/tests/test_funasr_onnx_vad_online_short_chunks.py
new file mode 100644
index 000000000..5602e1fe3
--- /dev/null
+++ b/tests/test_funasr_onnx_vad_online_short_chunks.py
@@ -0,0 +1,95 @@
+"""Run against an installed funasr-onnx package."""
+
+import importlib.util
+import itertools
+import unittest
+
+import numpy as np
+
+if importlib.util.find_spec("funasr_onnx") is None:
+ raise unittest.SkipTest("Install the standalone funasr-onnx distribution first.")
+
+from funasr_onnx.vad_bin import Fsmn_vad_online
+
+# frontend_conf of funasr/models/fsmn_vad_streaming/template.yaml
+FRONTEND_CONF = {
+ "fs": 16000,
+ "window": "hamming",
+ "n_mels": 80,
+ "frame_length": 25,
+ "frame_shift": 10,
+ "dither": 0.0,
+ "lfr_m": 5,
+ "lfr_n": 1,
+}
+
+# Segments (ms) of make_audio() streamed in 1600-sample chunks, the step used by the demo.
+EXPECTED = [[[[220, -1]]], [[[-1, 1660]]], [[[2220, -1]]], [[[-1, 3360]]]]
+
+
+def fake_infer(inputs):
+ # Score loud frames as speech instead of running the ONNX session.
+ sil = np.where(inputs[0][0].mean(-1) > 5.0, 0.05, 0.95)
+ scores = np.stack((sil, 1.0 - sil), axis=-1)[None].astype(np.float32)
+ return scores, inputs[1:]
+
+
+def make_model():
+ # Keep __call__, WavFrontendOnline and E2EVadModel real; no model files.
+ model = Fsmn_vad_online.__new__(Fsmn_vad_online)
+ model.cmvn_file = None
+ # E2EVadModel's defaults equal the template's model_conf.
+ model.config = {"frontend_conf": FRONTEND_CONF, "model_conf": {}}
+ model.max_end_sil = 800
+ model.encoder_conf = {"fsmn_layers": 1, "proj_dim": 2, "lorder": 2}
+ model.infer = fake_infer
+ return model
+
+
+def make_audio():
+ # 4 s at 16 kHz: noise at 0.5-1.5 s and 2.5-3.2 s, silence elsewhere.
+ rng = np.random.default_rng(0)
+ audio = np.zeros(64000, dtype=np.float32)
+ for start, end in ((8000, 24000), (40000, 51200)):
+ audio[start:end] = rng.uniform(-0.3, 0.3, end - start)
+ return audio
+
+
+def stream(chunk_sizes):
+ # Feed the audio in chunks, cycling through chunk_sizes; the last chunk is final.
+ model = make_model()
+ audio = make_audio()
+ param_dict = {"in_cache": []}
+ results = []
+ offset = 0
+ for size in itertools.cycle(chunk_sizes):
+ chunk = audio[offset : offset + size]
+ offset += size
+ param_dict["is_final"] = offset >= len(audio)
+ segments = model(audio_in=chunk, param_dict=param_dict)
+ if segments:
+ results.append(segments)
+ if param_dict["is_final"]:
+ return results
+
+
+class FsmnVadOnlineShortChunkTest(unittest.TestCase):
+ def test_demo_chunk_size(self):
+ self.assertEqual(stream([1600]), EXPECTED)
+
+ def test_chunks_shorter_than_first_lfr_frame(self):
+ # The first LFR frame needs 720 samples (45 ms), so these first calls yield no features.
+ for size in (160, 320, 480):
+ with self.subTest(size=size):
+ self.assertEqual(stream([size]), EXPECTED)
+
+ def test_packets_that_complete_no_fbank_frame(self):
+ # The first 1600-sample chunk leaves 320 samples for the next 400-sample fbank window,
+ # so a 40-sample packet, or the second of two 80-sample packets, completes no frame.
+ for chunk_sizes in ([1600, 40], [1600, 80, 80]):
+ with self.subTest(chunk_sizes=chunk_sizes):
+ self.assertEqual(stream(chunk_sizes), EXPECTED)
+
+
+if __name__ == "__main__":
+ unittest.main()