From af9018a3a9b243aeccf3dddc7abb4e705f5fceb3 Mon Sep 17 00:00:00 2001 From: sohailshk <122166523+sohailshk@users.noreply.github.com> Date: Fri, 13 Nov 2026 09:29:25 +0530 Subject: [PATCH] fix(auto): do not divide by zero speech time in AutoModel.inference Input shorter than one feature frame (about 25 ms) makes the model report batch_data_time == 0, and the rtf statistic then raised ZeroDivisionError after the model had already returned its (empty) result. --- funasr/auto/auto_model.py | 7 ++-- tests/test_auto_model_zero_speech_time.py | 39 +++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 tests/test_auto_model_zero_speech_time.py diff --git a/funasr/auto/auto_model.py b/funasr/auto/auto_model.py index b8320dfa7..ec9a9f362 100644 --- a/funasr/auto/auto_model.py +++ b/funasr/auto/auto_model.py @@ -983,7 +983,9 @@ def inference( speed_stats["extract_feat"] = meta_data.get("extract_feat", 0.0) speed_stats["forward"] = f"{time_escape:0.3f}" speed_stats["batch_size"] = f"{len(results)}" - speed_stats["rtf"] = f"{(time_escape) / batch_data_time:0.3f}" + # Input shorter than one feature frame reports zero speech time. + rtf = time_escape / batch_data_time if batch_data_time else float("nan") + speed_stats["rtf"] = f"{rtf:0.3f}" description = f"{speed_stats}, " if pbar: pbar.update(end_idx - beg_idx) @@ -998,7 +1000,8 @@ def inference( if pbar: # pbar.update(1) - pbar.set_description(f"rtf_avg: {time_escape_total/time_speech_total:0.3f}") + rtf_avg = time_escape_total / time_speech_total if time_speech_total else float("nan") + pbar.set_description(f"rtf_avg: {rtf_avg:0.3f}") device = next(model.parameters()).device if device.type == "cuda": diff --git a/tests/test_auto_model_zero_speech_time.py b/tests/test_auto_model_zero_speech_time.py new file mode 100644 index 000000000..f1bdad4be --- /dev/null +++ b/tests/test_auto_model_zero_speech_time.py @@ -0,0 +1,39 @@ +import unittest + +import torch + +from funasr.auto.auto_model import AutoModel + + +class _ZeroSpeechTimeModel: + """Reports no speech time, as models do for input shorter than one feature frame.""" + + def __init__(self): + self.param = torch.nn.Parameter(torch.zeros(1)) + + def parameters(self): + return iter([self.param]) + + def inference(self, data_in, **kwargs): + return [{"key": d, "value": []} for d in data_in], {"batch_data_time": 0.0} + + +def _auto_model(disable_pbar): + am = AutoModel.__new__(AutoModel) + am.model = _ZeroSpeechTimeModel() + am.kwargs = {"batch_size": 1, "disable_pbar": disable_pbar} + return am + + +class TestInferenceZeroSpeechTime(unittest.TestCase): + def test_zero_speech_time_returns_results_without_progress_bar(self): + res = AutoModel.inference(_auto_model(True), ["a", "b"]) + self.assertEqual(res, [{"key": "a", "value": []}, {"key": "b", "value": []}]) + + def test_zero_speech_time_returns_results_with_progress_bar(self): + res = AutoModel.inference(_auto_model(False), ["a"]) + self.assertEqual(res, [{"key": "a", "value": []}]) + + +if __name__ == "__main__": + unittest.main()