@@ -41,6 +41,71 @@ class InstalledOnnxPackageTest(unittest.TestCase):
4141 LOADERS = (funasr_onnx .Paraformer , ParaformerOnline ,
4242 funasr_onnx .SenseVoiceSmall , funasr_onnx .Fsmn_vad )
4343
44+ def test_long_and_empty_hotwords_stay_within_embedding_bounds (self ):
45+ for cls in (funasr_onnx .ContextualParaformer , funasr_onnx .SeacoParaformer ):
46+ with self .subTest (wrapper = cls .__name__ ):
47+ model = cls .__new__ (cls )
48+ model .vocab = {'a' : 3 , 'b' : 4 }
49+ matrix , positions = model .proc_hotword (' ab abababababab ' )
50+ self .assertEqual (matrix .tolist (), [
51+ [3 , 4 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ],
52+ [3 , 4 , 3 , 4 , 3 , 4 , 3 , 4 , 3 , 4 ],
53+ [1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ],
54+ ])
55+ self .assertEqual (positions .tolist (), [1 , 9 , 0 ])
56+ for value in ('' , ' \t \n ' ):
57+ matrix , positions = model .proc_hotword (value )
58+ self .assertEqual (matrix .tolist (), [[1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]])
59+ self .assertEqual (positions .tolist (), [0 ])
60+
61+ def test_batched_timestamps_use_each_input_length (self ):
62+ def features (lengths ):
63+ return np .zeros ((len (lengths ), max (lengths ), 4 ), dtype = np .float32 ), np .array (lengths )
64+
65+ def acoustic_output (feats , lengths ):
66+ peaks = np .zeros ((len (lengths ), feats .shape [1 ] * 3 ), dtype = np .float32 )
67+ for row , length in enumerate (lengths ):
68+ peaks [row , np .linspace (5 , length * 3 - 6 , 3 ).round ().astype (int )] = 1
69+ return None , None , np .zeros_like (peaks ), peaks
70+
71+ # Acoustic outputs are synthetic. The installed batching, timestamp and
72+ # postprocessing code must keep the shorter sample independent of padding.
73+ for cls in (funasr_onnx .Paraformer , funasr_onnx .ContextualParaformer ,
74+ funasr_onnx .SeacoParaformer ):
75+ with self .subTest (wrapper = cls .__name__ ):
76+ model = cls .__new__ (cls )
77+ model .batch_size = 2
78+ model .plot_timestamp_to = ''
79+ model .language = 'zh'
80+ model .frontend = SimpleNamespace (opts = SimpleNamespace (
81+ frame_opts = SimpleNamespace (samp_freq = 16000 )))
82+ model .load_data = lambda values , fs : values
83+ model .extract_feat = features
84+ model .decode = lambda scores , lens : [['\u4f60 ' , '\u597d ' ] for _ in range (model ._rows )]
85+
86+ def infer (feats , lengths , * unused ):
87+ model ._rows = len (lengths )
88+ return acoustic_output (feats , lengths )
89+
90+ model .infer = infer
91+ model .bb_infer = infer
92+ model .vocab = {'a' : 3 }
93+ model .eb_infer = lambda hotwords , lengths : [np .zeros ((10 , len (hotwords ), 4 ), dtype = np .float32 )]
94+ args = () if cls is funasr_onnx .Paraformer else ('a' ,)
95+ single = [model ([length ], * args )[0 ] for length in [17 , 50 ]]
96+ self .assertEqual ([x ['timestamp' ] for x in single ], [
97+ [[70 , 470 ], [470 , 945 ]], [[70 , 670 ], [1450 , 2925 ]],
98+ ])
99+ self .assertEqual (model ([17 , 50 ], * args ), single )
100+
101+ def test_terminal_abbreviation_preserves_text_and_timestamps (self ):
102+ from funasr_onnx .utils .postprocess_utils import abbr_dispose
103+
104+ self .assertEqual (abbr_dispose (['a' , ' ' , 'b' ]), ['AB' ])
105+ self .assertEqual (abbr_dispose (['a' , ' ' , 'b' ], [[0 , 100 ], [100 , 200 ]]),
106+ (['AB' ], [[0 , 200 ]]))
107+ self .assertEqual (abbr_dispose (['a' , ' ' , 'b' , ' ' , 'c' ]), ['ABC' ])
108+
44109 def test_lossless_non_wav_without_sidecar (self ):
45110 samples = (np .arange (4800 , dtype = np .int16 ) - 2400 ).astype (np .float32 ) / 32768
46111 with tempfile .TemporaryDirectory () as directory :
@@ -91,7 +156,7 @@ def test_invalid_audio_remains_visible_without_sidecar(self):
91156
92157 def test_installed_distribution_version_and_origin (self ):
93158 distribution = importlib .metadata .distribution ('funasr-onnx' )
94- self .assertEqual (distribution .version , '0.4.10 ' )
159+ self .assertEqual (distribution .version , '0.4.11 ' )
95160 self .assertEqual (
96161 Path (funasr_onnx .__file__ ).resolve ().parent ,
97162 Path (distribution .locate_file ('funasr_onnx' )).resolve (),
0 commit comments