Skip to content

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

fix(metrics): size the WER cost matrix for long inputs (#3784)

baolong lin1d066f9d560c
diff --git a/funasr/metrics/wer.py b/funasr/metrics/wer.py
index e6c2afe4d..f32cf9f03 100755
--- a/funasr/metrics/wer.py
+++ b/funasr/metrics/wer.py
@@ -134,7 +134,11 @@ def compute_wer_by_line(hyp, ref):
len_hyp = len(hyp)
len_ref = len(ref)
- cost_matrix = np.zeros((len_hyp + 1, len_ref + 1), dtype=np.int16)
+ # An edit distance can be as large as max(len_hyp, len_ref), so a 16-bit
+ # accumulator overflows as soon as either input reaches 32768 tokens and
+ # the first row/column initialisation tries to store that index. int32
+ # covers any input this O(len_hyp * len_ref) loop can realistically reach.
+ cost_matrix = np.zeros((len_hyp + 1, len_ref + 1), dtype=np.int32)
ops_matrix = np.zeros((len_hyp + 1, len_ref + 1), dtype=np.int8)
diff --git a/tests/test_metrics_wer.py b/tests/test_metrics_wer.py
index 4dd621b3e..7ac98a62f 100644
--- a/tests/test_metrics_wer.py
+++ b/tests/test_metrics_wer.py
@@ -9,6 +9,26 @@ def test_cer_detail_for_empty_reference_does_not_divide_by_zero():
assert print_cer_detail(out_item).endswith("corr:0.00%,cer:100.00%")
+def test_wer_by_line_handles_inputs_at_the_int16_boundary():
+ # 32768 tokens used to raise OverflowError: the cost matrix was int16, so
+ # initialising its first row/column could not store the index 32768.
+ out_item = compute_wer_by_line(["a"] * 32768, ["a"])
+
+ assert out_item["nwords"] == 1
+ assert out_item["cor"] == 1
+ assert out_item["wrong"] == 32767
+ assert out_item["ins"] == 32767
+
+
+def test_wer_by_line_handles_a_long_reference_at_the_int16_boundary():
+ out_item = compute_wer_by_line(["a"], ["a"] * 32768)
+
+ assert out_item["nwords"] == 32768
+ assert out_item["cor"] == 1
+ assert out_item["wrong"] == 32767
+ assert out_item["del"] == 32767
+
+
def test_compute_wer_scores_all_utterances_when_one_reference_is_empty(tmp_path):
ref_file = tmp_path / "ref.txt"
hyp_file = tmp_path / "hyp.txt"