From 9038340be707aa7cbf62fcbf33ab615bb266abdb Mon Sep 17 00:00:00 2001
From: 王梦迪 <73778524+di-osc@users.noreply.github.com>
Date: 星期一, 26 五月 2025 14:11:02 +0800
Subject: [PATCH] 修复Fsmn_vad_online多线程调用报错 (#2528)

---
 runtime/python/onnxruntime/funasr_onnx/utils/frontend.py |   67 ++++++++++++++++++++-------------
 1 files changed, 41 insertions(+), 26 deletions(-)

diff --git a/runtime/python/onnxruntime/funasr_onnx/utils/frontend.py b/runtime/python/onnxruntime/funasr_onnx/utils/frontend.py
index f4cc8ff..54f9deb 100644
--- a/runtime/python/onnxruntime/funasr_onnx/utils/frontend.py
+++ b/runtime/python/onnxruntime/funasr_onnx/utils/frontend.py
@@ -2,6 +2,7 @@
 from pathlib import Path
 from typing import Any, Dict, Iterable, List, NamedTuple, Set, Tuple, Union
 import copy
+from functools import lru_cache
 
 import numpy as np
 import kaldi_native_fbank as knf
@@ -45,7 +46,7 @@
         self.cmvn_file = cmvn_file
 
         if self.cmvn_file:
-            self.cmvn = self.load_cmvn()
+            self.cmvn = load_cmvn(self.cmvn_file)
         self.fbank_fn = None
         self.fbank_beg_idx = 0
         self.reset_status()
@@ -122,33 +123,47 @@
         inputs = (inputs + means) * vars
         return inputs
 
-    def load_cmvn(
-        self,
-    ) -> np.ndarray:
-        with open(self.cmvn_file, "r", encoding="utf-8") as f:
-            lines = f.readlines()
+@lru_cache()
+def load_cmvn(cmvn_file: Union[str, Path]) -> np.ndarray:
+    """load cmvn file to numpy array. 
 
-        means_list = []
-        vars_list = []
-        for i in range(len(lines)):
-            line_item = lines[i].split()
-            if line_item[0] == "<AddShift>":
-                line_item = lines[i + 1].split()
-                if line_item[0] == "<LearnRateCoef>":
-                    add_shift_line = line_item[3 : (len(line_item) - 1)]
-                    means_list = list(add_shift_line)
-                    continue
-            elif line_item[0] == "<Rescale>":
-                line_item = lines[i + 1].split()
-                if line_item[0] == "<LearnRateCoef>":
-                    rescale_line = line_item[3 : (len(line_item) - 1)]
-                    vars_list = list(rescale_line)
-                    continue
+    Args:
+        cmvn_file (Union[str, Path]): cmvn file path.
 
-        means = np.array(means_list).astype(np.float64)
-        vars = np.array(vars_list).astype(np.float64)
-        cmvn = np.array([means, vars])
-        return cmvn
+    Raises:
+        FileNotFoundError: cmvn file not exits.
+
+    Returns:
+        np.ndarray: cmvn array. shape is (2, dim).The first row is means, the second row is vars.
+    """
+
+    cmvn_file = Path(cmvn_file)
+    if not cmvn_file.exists():
+        raise FileNotFoundError("cmvn file not exits")
+    
+    with open(cmvn_file, "r", encoding="utf-8") as f:
+        lines = f.readlines()
+    means_list = []
+    vars_list = []
+    for i in range(len(lines)):
+        line_item = lines[i].split()
+        if line_item[0] == "<AddShift>":
+            line_item = lines[i + 1].split()
+            if line_item[0] == "<LearnRateCoef>":
+                add_shift_line = line_item[3 : (len(line_item) - 1)]
+                means_list = list(add_shift_line)
+                continue
+        elif line_item[0] == "<Rescale>":
+            line_item = lines[i + 1].split()
+            if line_item[0] == "<LearnRateCoef>":
+                rescale_line = line_item[3 : (len(line_item) - 1)]
+                vars_list = list(rescale_line)
+                continue
+
+    means = np.array(means_list).astype(np.float64)
+    vars = np.array(vars_list).astype(np.float64)
+    cmvn = np.array([means, vars])
+    return cmvn
 
 
 class WavFrontendOnline(WavFrontend):

--
Gitblit v1.9.1