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 = {}