Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions runtime/python/libtorch/funasr_torch/paraformer_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,10 @@ def __call__(
return asr_res

def proc_hotword(self, hotwords):
hotwords = hotwords.split(" ")
# Like the C++ runtime, skip empty hotwords and keep the first max_hotword_len
# characters of longer ones: the exported embedding model takes that many per hotword.
max_hotword_len = 10
hotwords = [hotword[:max_hotword_len] for hotword in hotwords.split()]
hotwords_length = [len(i) - 1 for i in hotwords]
hotwords_length.append(0)
hotwords_length = np.array(hotwords_length)
Expand All @@ -385,7 +388,7 @@ def word_map(word):

hotword_int = [word_map(i) for i in hotwords]
hotword_int.append(np.array([1]))
hotwords = pad_list(hotword_int, pad_value=0, max_len=10)
hotwords = pad_list(hotword_int, pad_value=0, max_len=max_hotword_len)
return torch.tensor(hotwords), hotwords_length

def bb_infer(
Expand Down
7 changes: 5 additions & 2 deletions runtime/python/onnxruntime/funasr_onnx/paraformer_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,10 @@ def __call__(
return asr_res

def proc_hotword(self, hotwords):
hotwords = hotwords.split(" ")
# Like the C++ runtime, skip empty hotwords and keep the first max_hotword_len
# characters of longer ones: the exported embedding model takes that many per hotword.
max_hotword_len = 10
hotwords = [hotword[:max_hotword_len] for hotword in hotwords.split()]
hotwords_length = [len(i) - 1 for i in hotwords]
hotwords_length.append(0)
hotwords_length = np.array(hotwords_length)
Expand All @@ -428,7 +431,7 @@ def word_map(word):
hotword_int = [word_map(i) for i in hotwords]

hotword_int.append(np.array([1]))
hotwords = pad_list(hotword_int, pad_value=0, max_len=10)
hotwords = pad_list(hotword_int, pad_value=0, max_len=max_hotword_len)

return hotwords, hotwords_length

Expand Down
29 changes: 29 additions & 0 deletions tests/test_runtime_eval_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,32 @@ def test_onnx_wrappers_read_non_wav_paths(native_fixture, tmp_path, monkeypatch,
model = funasr_onnx.Paraformer(str(directory), device_id=DEVICE, intra_op_num_threads=1)
assert model(str(audio)) == model(expected)
assert {path.name: path.read_bytes() for path in tmp_path.iterdir()} == files


@pytest.mark.parametrize("backend", ["torch", "onnx"])
@pytest.mark.parametrize("wrapper", ["ContextualParaformer", "SeacoParaformer"])
def test_hotword_wrappers_skip_empty_and_cut_long_hotwords(
hotword_fixture, monkeypatch, backend, wrapper
):
directory, paths = hotword_fixture
location = "libtorch" if backend == "torch" else "onnxruntime"
monkeypatch.syspath_prepend(str(ROOT / "runtime/python" / location))
module = importlib.import_module("funasr_" + backend + ".paraformer_bin")
model = getattr(module, wrapper)(str(directory), intra_op_num_threads=1)
# Like the C++ runtime: skip empty hotwords, and keep the first 10 characters of a longer
# one (the exported model_eb takes 10 per hotword). Ids: <s> 1, 你 3, 好 4, 魔 5, 搭 6.
long_hotwords = " 魔搭 你好魔搭你好魔搭你好魔搭 "
matrix, positions = model.proc_hotword(long_hotwords)
assert matrix.tolist() == [
[5, 6, 0, 0, 0, 0, 0, 0, 0, 0],
[3, 4, 5, 6, 3, 4, 5, 6, 3, 4],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
assert positions.tolist() == [1, 9, 0]
matrix, positions = model.proc_hotword("")
assert matrix.tolist() == [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
assert positions.tolist() == [0]
# Both decode every file; the 12-character hotword used to stop the call with a ValueError.
for hotwords in (long_hotwords, ""):
result = model(paths, hotwords)
assert [row["preds"][0] for row in result] == ["你" * 4, "你" * 11, "你" * 17]
Loading