diff --git a/.github/workflows/test-numpy-compatibility.yml b/.github/workflows/test-numpy-compatibility.yml index bfeeff440..d62d0f4cd 100644 --- a/.github/workflows/test-numpy-compatibility.yml +++ b/.github/workflows/test-numpy-compatibility.yml @@ -8,6 +8,7 @@ on: - "funasr/cli.py" - "tests/test_cli.py" - "funasr/auto/auto_model.py" + - "tests/test_auto_model_zero_speech_time.py" - "funasr/tokenizer/sentencepiece_tokenizer.py" - "funasr/tokenizer/char_tokenizer.py" - "funasr/tokenizer/phoneme_tokenizer.py" @@ -54,6 +55,7 @@ on: - "funasr/cli.py" - "tests/test_cli.py" - "funasr/auto/auto_model.py" + - "tests/test_auto_model_zero_speech_time.py" - "funasr/tokenizer/sentencepiece_tokenizer.py" - "funasr/tokenizer/char_tokenizer.py" - "funasr/tokenizer/phoneme_tokenizer.py" @@ -158,6 +160,7 @@ jobs: tests/test_paraformer_hotword_file.py \ tests/test_ct_transformer_streaming_empty_input.py \ tests/test_cli.py \ + tests/test_auto_model_zero_speech_time.py \ --junitxml=numpy-results.xml - uses: actions/upload-artifact@v4 if: always() 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()