Skip to content

Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.

perf: optimize _reset_runtime_configs to skip deepcopy of immutable objects

游雁4bb33425a865
diff --git a/funasr/auto/auto_model.py b/funasr/auto/auto_model.py
index dc7550c30..9b0054e32 100644
--- a/funasr/auto/auto_model.py
+++ b/funasr/auto/auto_model.py
@@ -801,6 +801,10 @@ class AutoModel:
baseline["kwargs"] = copy.deepcopy(self.kwargs)
self._base_kwargs_map = baseline
+ _IMMUTABLE_KWARGS_KEYS = frozenset([
+ "token_list", "tokenizer", "frontend", "model", "init_param", "model_path",
+ ])
+
def _reset_runtime_configs(self):
"""Ensure runtime kwargs reset to baseline defaults before inference."""
base_map = getattr(self, "_base_kwargs_map", None)
@@ -808,7 +812,12 @@ class AutoModel:
return
for name, base in base_map.items():
- restored = copy.deepcopy(base)
+ restored = {}
+ for k, v in base.items():
+ if k in self._IMMUTABLE_KWARGS_KEYS or not isinstance(v, (dict, list)):
+ restored[k] = v
+ else:
+ restored[k] = copy.deepcopy(v)
setattr(self, name, restored)
ncpu = _resolve_ncpu(self.kwargs, 4)
diff --git a/tests_models/test_paraformer_streaming.py b/tests_models/test_paraformer_streaming.py
index 99aec136e..5846de7c8 100644
--- a/tests_models/test_paraformer_streaming.py
+++ b/tests_models/test_paraformer_streaming.py
@@ -1,9 +1,11 @@
#!/usr/bin/env python3
-"""Test Paraformer-Streaming: online/streaming ASR"""
+"""Test Paraformer-Streaming: chunk-by-chunk streaming ASR"""
import sys
import time
+import os
def main():
+ import soundfile
from funasr import AutoModel
print("[Paraformer-Streaming] Loading model...")
@@ -12,21 +14,49 @@ def main():
model="iic/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online",
device="cpu",
disable_update=True,
+ disable_pbar=True,
)
- print("[Paraformer-Streaming] Model loaded in %.1fs" % (time.time()-t0))
+ print("[Paraformer-Streaming] Model loaded in %.1fs" % (time.time() - t0))
- print("[Paraformer-Streaming] Running inference...")
+ wav_file = os.path.join(model.model_path, "example/asr_example.wav")
+ speech, sample_rate = soundfile.read(wav_file)
+
+ chunk_size = [0, 10, 5]
+ encoder_chunk_look_back = 4
+ decoder_chunk_look_back = 1
+ chunk_stride = chunk_size[1] * 960 # 600ms
+
+ print("[Paraformer-Streaming] Running streaming inference (%.2fs audio, %d chunks)..." % (
+ len(speech) / sample_rate, int((len(speech) - 1) / chunk_stride + 1)))
t0 = time.time()
- test_wav = "https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.wav"
- res = model.generate(input=test_wav, cache={}, is_final=True)
- print("[Paraformer-Streaming] Inference done in %.1fs" % (time.time()-t0))
- print("[Paraformer-Streaming] Result: %s" % res)
- if res and len(res) > 0 and "text" in res[0]:
+ cache = {}
+ total_chunk_num = int((len(speech) - 1) / chunk_stride + 1)
+ all_text = ""
+
+ for i in range(total_chunk_num):
+ speech_chunk = speech[i * chunk_stride:(i + 1) * chunk_stride]
+ is_final = i == total_chunk_num - 1
+ res = model.generate(
+ input=speech_chunk,
+ cache=cache,
+ is_final=is_final,
+ chunk_size=chunk_size,
+ encoder_chunk_look_back=encoder_chunk_look_back,
+ decoder_chunk_look_back=decoder_chunk_look_back,
+ )
+ txt = res[0].get("text", "") if res else ""
+ all_text += txt
+
+ print("[Paraformer-Streaming] Inference done in %.1fs" % (time.time() - t0))
+ print("[Paraformer-Streaming] Result: '%s'" % all_text)
+
+ expected = "欢迎大家来体验达摩院推出的语音识别模型"
+ if expected in all_text:
print("[Paraformer-Streaming] PASSED")
return 0
else:
- print("[Paraformer-Streaming] FAILED - no text in result")
+ print("[Paraformer-Streaming] FAILED - expected text not found")
return 1
if __name__ == "__main__":