From 4b00a314cde3ac843b2aff205d63f0803452797f Mon Sep 17 00:00:00 2001 From: Tanishq Date: Fri, 20 Nov 2026 11:44:00 +0530 Subject: [PATCH] fix(onnx): reset the frontend after a short final chunk in streaming Paraformer A final call to the streaming Paraformer with fewer than 960 samples decodes the overlap frames cached by the earlier calls and returns without calling extract_feat. extract_feat is what resets WavFrontendOnline at the end of a stream, so the frontend kept its cached samples, LFR splice frames and reserved waveform. The frontend belongs to the model, not to the caller's cache, so the next stream on the same model was framed as a continuation of the previous one: its first LFR frame was built from the end of the previous stream, and the 240-399 samples left in the frontend went in front of its audio, which also moved every later 25 ms window unless their number was a multiple of 160. This happens after any stream that has produced features and ends with an empty final call, and in the demo's 600 ms chunking whenever the audio ends with a chunk of 2 to 959 samples. Reset the frontend in that branch, as a final extract_feat call does. The C++ ParaformerOnline::Forward resets its frontend caches in the same branch, and the PyTorch model clears its frontend cache on is_final. The short final call decodes the same frames as before. --- .../funasr_onnx/paraformer_online_bin.py | 2 ++ ...funasr_onnx_paraformer_online_empty_final.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py b/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py index 4d353b733..eb36cd2ab 100644 --- a/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py +++ b/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py @@ -140,6 +140,8 @@ def __call__(self, audio_in: np.ndarray, **kwargs): asr_res = [] if waveforms.shape[1] < 16 * 60 and is_final and len(cache) > 0: + # extract_feat is skipped here, so reset the frontend as its final call would. + self.frontend.cache_reset() cache["last_chunk"] = True feats = cache["feats"] feats_len = np.array([feats.shape[1]]).astype(np.int32) diff --git a/tests/test_funasr_onnx_paraformer_online_empty_final.py b/tests/test_funasr_onnx_paraformer_online_empty_final.py index 5a89b5221..55940dcf2 100644 --- a/tests/test_funasr_onnx_paraformer_online_empty_final.py +++ b/tests/test_funasr_onnx_paraformer_online_empty_final.py @@ -151,6 +151,23 @@ def test_short_final_does_not_contaminate_next_stream(self): reference(np.zeros(960, np.float32), param_dict={"cache": {}}) np.testing.assert_array_equal(model.infer.call_args.args[0], reference.infer.call_args.args[0]) + def test_short_final_after_features_does_not_contaminate_next_stream(self): + audio = (0.1 * np.sin(np.arange(10560) * 2 * np.pi * 440 / 16000)).astype(np.float32) + next_audio = (0.05 * np.sin(np.arange(9600) * 2 * np.pi * 800 / 16000)).astype(np.float32) + reference = self.make_model() + reference(next_audio, param_dict={"cache": {}}) + for samples in (0, 1, 500, 959, 960): + with self.subTest(samples=samples): + model = self.make_model() + cache = {} + model(audio[:9600], param_dict={"cache": cache}) + model(audio[9600 : 9600 + samples], param_dict={"cache": cache, "is_final": True}) + self.assertTrue(cache["last_chunk"]) + model(next_audio, param_dict={"cache": {}}) + np.testing.assert_array_equal( + model.infer.call_args.args[0], reference.infer.call_args.args[0] + ) + def test_empty_nonfinal_call_preserves_existing_cache(self): model = self.make_model() cache = {}