Skip to content

fix(metrics): score utterances with an empty reference in funasr.metrics.wer - #3779

Merged
LauraGPT merged 1 commit into
modelscope:mainfrom
MohammadHijjawi97:fix/wer-empty-reference
Oct 18, 2026
Merged

LauraGPT merged 1 commit into
modelscope:mainfrom
MohammadHijjawi97:fix/wer-empty-reference

Conversation

@MohammadHijjawi97

Copy link
Copy Markdown
Contributor

Summary

python -m funasr.metrics.wer crashes when any utterance in the reference file has an empty transcript (for example a silent clip written as just utt2). print_cer_detail divides the per-utterance correct/error counts by the number of reference words, which is 0 for that line. The ZeroDivisionError aborts the run before the %WER / %SER summary is written, so a single empty reference loses the whole report.

This reports that utterance's rates against one word instead. The counts in the detail line, and the overall WER/SER, are unchanged.

Reproduction:

# ref.txt                 # hyp.txt
utt1 the cat sat          utt1 the cat sat
utt2                      utt2 uh
utt3 good morning         utt3 good morning

python -m funasr.metrics.wer ++ref_file=ref.txt ++hyp_file=hyp.txt ++cer_file=out.wer

Before:

  File "funasr/metrics/wer.py", line 222, in print_cer_detail
    + "{:.2%}".format(rst["cor"] / rst["nwords"])
ZeroDivisionError: division by zero

out.wer stops after utt1, with no summary.

After (out.wer):

utt2(nwords=0,cor=0,ins=1,del=0,sub=0) corr:0.00%,cer:100.00%
...
%WER 20.0 [ 1 / 5, 1 ins, 0 del, 0 sub ]
%SER 33.33 [ 1 / 3 ]
Scored 3 sentences, 0 not present in hyp.

Type of change

  • Bug fix
  • Documentation
  • Example or demo
  • Runtime or deployment
  • Benchmark or evaluation
  • Model/training change

Validation

  • python -m compileall funasr examples tests

  • Docs or links checked

  • Runtime/deployment command tested

  • pytest tests/test_metrics_wer.py: 2 passed; both fail with ZeroDivisionError without the fix. No model or audio is needed.

  • pytest tests/test_metrics_common_rapidfuzz.py: passes.

  • Ran python -m funasr.metrics.wer on the files above before and after the change (Windows 11 / Python 3.13).

  • black --line-length=100 (24.4.0, as pinned in pre-commit) on the changed lines and the new test.

User impact

Anyone scoring a model with funasr.metrics.wer (as in examples/industrial_data_pretraining/paraformer/lora_cer.sh) on a test set containing a silent or untranscribed utterance gets a complete CER/WER report instead of a traceback.

Notes for reviewers

Only the per-utterance percentages in print_cer_detail change, and only when the reference is empty: the cer shown is then the number of inserted words x 100%. The aggregate %WER already handled this case (it divides by the total word count), so it is unchanged. The recipe copies under examples/aishell/*/utils/compute_wer.py have the same division and are not touched here.

…ics.wer

print_cer_detail divided by the reference word count, so a single
utterance with an empty reference (for example a silent clip) raised
ZeroDivisionError and aborted the whole report before the %WER/%SER
summary was written. Report that utterance's rates against one word
instead; the overall WER is unchanged.

@LauraGPT LauraGPT left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validated exact head 9ff47d29e1007c0cb6e6677641bb6bdf46cb178b and synthetic merge 33c746408ea2482bd49b9087b41aa60e3a24d18d over protected main e5be9c9827ee139a158c9e87f893275f34ca5e9d.

  • Base plus the submitted regression tests: 2 failed with the reproduced ZeroDivisionError.
  • Exact head: 2/2 submitted WER regressions and 2/2 adjacent RapidFuzz tests passed.
  • Synthetic merge: the same 4/4 tests passed.
  • Changed-file compileall and recursive tree reconciliation passed; exactly funasr/metrics/wer.py and tests/test_metrics_wer.py differ from main.

This is a CPU-only metrics change; no model loading, ASR inference, or GPU runtime behavior is involved. The validation is targeted, not a claim that the full repository suite ran.

The two search matches, closed PRs #3105 and #3114, changed funasr/metrics/common.py aggregate metrics; this PR fixes the separate per-utterance formatter in funasr/metrics/wer.py.

@LauraGPT
LauraGPT merged commit a4ab3b9 into modelscope:main Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants