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): reset the FSMN cache at the end of a VAD stream

Tanishq2700fa5aba4e
diff --git a/runtime/python/onnxruntime/funasr_onnx/vad_bin.py b/runtime/python/onnxruntime/funasr_onnx/vad_bin.py
index 6bfa7ea92..fc132d17c 100644
--- a/runtime/python/onnxruntime/funasr_onnx/vad_bin.py
+++ b/runtime/python/onnxruntime/funasr_onnx/vad_bin.py
@@ -309,6 +309,9 @@ class Fsmn_vad_online:
logging.warning("input wav is silence or noise")
segments = []
param_dict.update({"frontend": frontend, "vad_scorer": vad_scorer})
+ if is_final:
+ # Start the next stream with a fresh FSMN cache, as init_cache() and Reset() do.
+ param_dict["in_cache"] = []
return segments
def load_data(self, wav_content: Union[str, np.ndarray, List[str]], fs: int = None) -> List:
diff --git a/tests/test_funasr_onnx_vad_online_short_chunks.py b/tests/test_funasr_onnx_vad_online_short_chunks.py
index 99b9131c2..012e34b39 100644
--- a/tests/test_funasr_onnx_vad_online_short_chunks.py
+++ b/tests/test_funasr_onnx_vad_online_short_chunks.py
@@ -31,7 +31,8 @@ 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:]
+ # Change the cache so a cache carried into the next stream shows up.
+ return scores, [cache + 1.0 for cache in inputs[1:]]
def make_model():
@@ -115,11 +116,12 @@ class FsmnVadOnlineShortChunkTest(unittest.TestCase):
self.assertEqual(short_stream([size, 0], {"in_cache": []}), [[], []])
def test_param_dict_reuse_after_short_stream(self):
- # The final call of a short stream resets the frontend and scorer for the next stream.
+ # The final call of a short stream resets the frontend, scorer and cache for the next stream.
for sizes in ([320], [480, 0], [480]):
with self.subTest(sizes=sizes):
param_dict = {"in_cache": []}
self.assertEqual(short_stream(sizes, param_dict), [[]] * len(sizes))
+ self.assertEqual(param_dict["in_cache"], [])
self.assertEqual(stream([1600], param_dict), EXPECTED)