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): integrate empty split-final result handling

zhifu gao643e81720b66
diff --git a/.github/workflows/test-funasr-onnx-package.yml b/.github/workflows/test-funasr-onnx-package.yml
index 58dfd29a8..75d735f0b 100644
--- a/.github/workflows/test-funasr-onnx-package.yml
+++ b/.github/workflows/test-funasr-onnx-package.yml
@@ -10,6 +10,7 @@ on:
- "tests/test_funasr_onnx_release.py"
- "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_rich_transcription_postprocess.py"
- "tests/test_rich_transcription_event_segments.py"
push:
@@ -23,6 +24,7 @@ on:
- "tests/test_funasr_onnx_release.py"
- "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_rich_transcription_postprocess.py"
- "tests/test_rich_transcription_event_segments.py"
workflow_dispatch:
@@ -100,6 +102,9 @@ jobs:
- name: Verify streaming Paraformer overlap cache
run: python tests/test_funasr_onnx_paraformer_online_overlap.py
+ - name: Verify empty split-final Paraformer result
+ run: python tests/test_funasr_onnx_paraformer_online_empty_final.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/paraformer_online_bin.py b/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py
index 33bea03f2..108b6d32f 100644
--- a/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py
+++ b/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py
@@ -183,7 +183,7 @@ class Paraformer:
res[key][1].extend(value[1])
else:
res[key] = [value[0], value[1]]
- return [res]
+ return [res] if res else []
else:
feats = self.add_overlap_chunk(feats, cache)
diff --git a/tests/test_funasr_onnx_paraformer_online_empty_final.py b/tests/test_funasr_onnx_paraformer_online_empty_final.py
new file mode 100644
index 000000000..30b01c0f0
--- /dev/null
+++ b/tests/test_funasr_onnx_paraformer_online_empty_final.py
@@ -0,0 +1,75 @@
+"""Final-result regressions; run with the standalone funasr-onnx dependencies."""
+
+import unittest
+from unittest.mock import Mock
+
+import numpy as np
+
+from funasr_onnx.paraformer_online_bin import Paraformer
+
+
+class ParaformerOnlineEmptyFinalTest(unittest.TestCase):
+ def call_final(self, frames, results):
+ # Keep __call__, cache preparation and overlap splitting real; no model files.
+ model = Paraformer.__new__(Paraformer)
+ model.chunk_size = [5, 10, 5]
+ model.encoder_output_size = model.feats_dims = 1
+ model.fsmn_layer = 0
+ model.pe = Mock()
+ model.pe.forward.side_effect = lambda feats, start_idx: feats
+ model.extract_feat = Mock(return_value=(
+ np.zeros((1, frames, 1), dtype=np.float32),
+ np.array([frames], dtype=np.int32),
+ ))
+ model.infer = Mock(side_effect=results)
+ cache = {}
+ result = model(
+ np.zeros(9600, dtype=np.float32),
+ param_dict={"cache": cache, "is_final": True},
+ )
+ self.assertEqual(model.infer.call_count, len(results))
+ self.assertTrue(cache["last_chunk"])
+ return result
+
+ def test_split_final_without_results(self):
+ result = self.call_final(10, [[], []])
+ self.assertEqual(result, [])
+ # The official demo consumes any nonempty result this way.
+ final_result = ""
+ if len(result) > 0:
+ final_result += result[0]["preds"][0]
+ self.assertEqual(final_result, "")
+
+ def test_split_final_with_only_one_result(self):
+ for results in (
+ [[{"preds": ("hello", ["hello"])}], []],
+ [[], [{"preds": ("hello", ["hello"])}]],
+ ):
+ with self.subTest(results=results):
+ result = self.call_final(10, results)
+ self.assertEqual(result, [{"preds": ["hello", ["hello"]]}])
+ self.assertIsInstance(result[0]["preds"], list)
+
+ def test_split_final_merges_both_results(self):
+ result = self.call_final(10, [
+ [{"preds": ("hello", ["hello"])}],
+ [{"preds": (" world", ["world"])}],
+ ])
+ self.assertEqual(result, [{"preds": ["hello world", ["hello", "world"]]}])
+ self.assertIsInstance(result[0]["preds"], list)
+
+ def test_single_final_with_result(self):
+ expected = [{"preds": ("hello", ["hello"])}]
+ result = self.call_final(5, [expected])
+ self.assertIs(result, expected)
+ self.assertIsInstance(result[0]["preds"], tuple)
+
+ def test_single_final_with_empty_text(self):
+ expected = [{"preds": ("", [])}]
+ result = self.call_final(5, [expected])
+ self.assertIs(result, expected)
+ self.assertIsInstance(result[0]["preds"], tuple)
+
+
+if __name__ == "__main__":
+ unittest.main()